diff --git a/src/test/java/net/snowflake/client/core/CoreUtilsMiscellaneousTest.java b/src/test/java/net/snowflake/client/core/CoreUtilsMiscellaneousTest.java index 9f86db711..8a9843051 100644 --- a/src/test/java/net/snowflake/client/core/CoreUtilsMiscellaneousTest.java +++ b/src/test/java/net/snowflake/client/core/CoreUtilsMiscellaneousTest.java @@ -273,4 +273,18 @@ public void testSizeOfHttpClientMapWithGzipAndUserAgentSuffix() { // Assert there are 3 entries because userAgentSuffix has changed assertEquals(3, HttpUtil.httpClient.size()); } + + @Test + public void testNullAndEmptyProxySettingsForS3() { + HttpClientSettingsKey testKey = + new HttpClientSettingsKey(OCSPMode.FAIL_OPEN, null, 443, null, null, null, "", "", false); + ClientConfiguration clientConfig = new ClientConfiguration(); + HttpUtil.setProxyForS3(testKey, clientConfig); + assertEquals(Protocol.HTTP, clientConfig.getProxyProtocol()); + assertEquals("", clientConfig.getProxyHost()); + assertEquals(443, clientConfig.getProxyPort()); + assertEquals("", clientConfig.getNonProxyHosts()); + assertNull(clientConfig.getProxyUsername()); + assertNull(clientConfig.getProxyPassword()); + } } diff --git a/src/test/java/net/snowflake/client/core/SessionUtilLatestIT.java b/src/test/java/net/snowflake/client/core/SessionUtilLatestIT.java index 17e458fb3..2da471f48 100644 --- a/src/test/java/net/snowflake/client/core/SessionUtilLatestIT.java +++ b/src/test/java/net/snowflake/client/core/SessionUtilLatestIT.java @@ -1,11 +1,12 @@ /* - * Copyright (c) 2012-2022 Snowflake Computing Inc. All rights reserved. + * Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved. */ package net.snowflake.client.core; import static net.snowflake.client.TestUtil.systemGetEnv; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; @@ -16,14 +17,17 @@ import java.util.Map; import java.util.Map.Entry; import java.util.UUID; +import java.util.concurrent.atomic.AtomicBoolean; import net.snowflake.client.category.TestCategoryCore; import net.snowflake.client.jdbc.BaseJDBCTest; import net.snowflake.client.jdbc.ErrorCode; import net.snowflake.client.jdbc.SnowflakeSQLException; import net.snowflake.common.core.ClientAuthnDTO; +import net.snowflake.common.core.SqlState; import org.apache.commons.io.IOUtils; import org.apache.http.Header; import org.apache.http.HttpEntity; +import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpRequestBase; import org.junit.Ignore; @@ -237,7 +241,150 @@ private SFLoginInput createLoginInput() { input.setHttpClientSettingsKey(new HttpClientSettingsKey(OCSPMode.FAIL_OPEN)); input.setLoginTimeout(1000); input.setSessionParameters(new HashMap<>()); - return input; } + + @Test + public void testOktaAuthPostFail() throws Throwable { + SFLoginInput loginInput = createLoginInput(); + loginInput.setAuthenticator("https://testauth.okta.com"); + Map connectionPropertiesMap = initConnectionPropertiesMap(); + + try (MockedStatic mockedHttpUtil = mockStatic(HttpUtil.class)) { + mockedHttpUtil + .when( + () -> + HttpUtil.executeGeneralRequest( + Mockito.any(HttpPost.class), + Mockito.anyInt(), + Mockito.anyInt(), + Mockito.anyInt(), + Mockito.anyInt(), + Mockito.nullable(HttpClientSettingsKey.class))) + .thenReturn("{\"code\":null,\"message\":\"POST request failed\",\"success\":false}"); + + SessionUtil.openSession(loginInput, connectionPropertiesMap, "ALL"); + fail("Exception should have been thrown"); + } catch (SnowflakeSQLException e) { + assertEquals("POST request failed", e.getMessage()); + assertEquals(SqlState.SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION, e.getSQLState()); + } + } + + @Test + public void testOktaAuthMalformedUrl() throws Throwable { + SFLoginInput loginInput = createLoginInput(); + loginInput.setAuthenticator("invalid!@url$%^"); + Map connectionPropertiesMap = initConnectionPropertiesMap(); + + try (MockedStatic mockedHttpUtil = mockStatic(HttpUtil.class)) { + mockedHttpUtil + .when( + () -> + HttpUtil.executeGeneralRequest( + Mockito.any(HttpPost.class), + Mockito.anyInt(), + Mockito.anyInt(), + Mockito.anyInt(), + Mockito.anyInt(), + Mockito.nullable(HttpClientSettingsKey.class))) + .thenReturn( + "{\"data\":{\"tokenUrl\":\"invalid!@url$%^\"," + + "\"ssoUrl\":\"invalid!@url$%^\"," + + "\"proofKey\":null},\"code\":null,\"message\":null,\"success\":true}"); + + SessionUtil.openSession(loginInput, connectionPropertiesMap, "ALL"); + fail("Exception should have been thrown"); + } catch (SnowflakeSQLException e) { + assertEquals((int) ErrorCode.NETWORK_ERROR.getMessageCode(), e.getErrorCode()); + assertEquals(SqlState.IO_ERROR, e.getSQLState()); + } + } + + @Test + public void testOktaAuthURISyntaxError() throws Throwable { + SFLoginInput loginInput = createLoginInput(); + loginInput.setAuthenticator("https://testauth.okta.com/^123"); + Map connectionPropertiesMap = initConnectionPropertiesMap(); + + try (MockedStatic mockedHttpUtil = mockStatic(HttpUtil.class)) { + mockedHttpUtil + .when( + () -> + HttpUtil.executeGeneralRequest( + Mockito.any(HttpPost.class), + Mockito.anyInt(), + Mockito.anyInt(), + Mockito.anyInt(), + Mockito.anyInt(), + Mockito.nullable(HttpClientSettingsKey.class))) + .thenReturn( + "{\"data\":{\"tokenUrl\":\"https://testauth.okta.com/^123\"," + + "\"ssoUrl\":\"https://testauth.okta.com/^123\"," + + "\"proofKey\":null},\"code\":null,\"message\":null,\"success\":true}"); + + SessionUtil.openSession(loginInput, connectionPropertiesMap, "ALL"); + fail("Exception should have been thrown"); + } catch (SnowflakeSQLException e) { + assertEquals((int) ErrorCode.CONNECTION_ERROR.getMessageCode(), e.getErrorCode()); + assertEquals(SqlState.SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION, e.getSQLState()); + } + } + + @Test + public void testOktaAuthGetFail() throws Throwable { + SFLoginInput loginInput = createLoginInput(); + loginInput.setAuthenticator("https://testauth.okta.com"); + Map connectionPropertiesMap = initConnectionPropertiesMap(); + + try (MockedStatic mockedHttpUtil = mockStatic(HttpUtil.class)) { + mockedHttpUtil + .when( + () -> + HttpUtil.executeGeneralRequest( + Mockito.any(HttpPost.class), + Mockito.anyInt(), + Mockito.anyInt(), + Mockito.anyInt(), + Mockito.anyInt(), + Mockito.nullable(HttpClientSettingsKey.class))) + .thenReturn( + "{\"data\":{\"tokenUrl\":\"https://testauth.okta.com/api/v1/authn\"," + + "\"ssoUrl\":\"https://testauth.okta.com/app/snowflake/abcdefghijklmnopqrstuvwxyz/sso/saml\"," + + "\"proofKey\":null},\"code\":null,\"message\":null,\"success\":true}"); + + mockedHttpUtil + .when( + () -> + HttpUtil.executeRequestWithoutCookies( + Mockito.any(HttpRequestBase.class), + Mockito.anyInt(), + Mockito.anyInt(), + Mockito.anyInt(), + Mockito.anyInt(), + Mockito.anyInt(), + Mockito.nullable(AtomicBoolean.class), + Mockito.nullable(HttpClientSettingsKey.class))) + .thenReturn( + "{\"expiresAt\":\"2023-10-13T19:18:09.000Z\",\"status\":\"SUCCESS\",\"sessionToken\":\"testsessiontoken\"}"); + + mockedHttpUtil + .when( + () -> + HttpUtil.executeGeneralRequest( + Mockito.any(HttpGet.class), + Mockito.anyInt(), + Mockito.anyInt(), + Mockito.anyInt(), + Mockito.anyInt(), + Mockito.nullable(HttpClientSettingsKey.class))) + .thenThrow(new IOException()); + + SessionUtil.openSession(loginInput, connectionPropertiesMap, "ALL"); + fail("Exception should have been thrown"); + } catch (SnowflakeSQLException e) { + assertEquals((int) ErrorCode.NETWORK_ERROR.getMessageCode(), e.getErrorCode()); + assertEquals(SqlState.IO_ERROR, e.getSQLState()); + } + } } diff --git a/src/test/java/net/snowflake/client/jdbc/SnowflakeDriverLatestIT.java b/src/test/java/net/snowflake/client/jdbc/SnowflakeDriverLatestIT.java index 2cea0e5fb..da29b287e 100644 --- a/src/test/java/net/snowflake/client/jdbc/SnowflakeDriverLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/SnowflakeDriverLatestIT.java @@ -416,6 +416,17 @@ public void testGetPropertyInfo() throws SQLException { "server URL in form of ://:/", info[0].description); + // Test with null URL and no properties. ServerURL is needed. + url = null; + props = new Properties(); + driver = DriverManager.getDriver("jdbc:snowflake://snowflake.reg.local:8082"); + info = driver.getPropertyInfo(url, props); + assertEquals(1, info.length); + assertEquals("serverURL", info[0].name); + assertEquals( + "server URL in form of ://:/", + info[0].description); + // Test with URL that requires username and password. url = "jdbc:snowflake://snowflake.reg.local:8082"; info = driver.getPropertyInfo(url, props);