From e5bee1354787adf0fe0eceacebe8851d88458dab Mon Sep 17 00:00:00 2001 From: Joyce Ling <115662568+sfc-gh-ext-simba-jl@users.noreply.github.com> Date: Wed, 18 Oct 2023 21:23:34 -0700 Subject: [PATCH 1/3] add okta auth tests to increase SessionUtil coverage --- .../client/core/SessionUtilLatestIT.java | 151 +++++++++++++++++- 1 file changed, 149 insertions(+), 2 deletions(-) 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()); + } + } } From 470e8c02caf4a311ee92b8e3a28dc01eb25aaa0b Mon Sep 17 00:00:00 2001 From: Joyce Ling <115662568+sfc-gh-ext-simba-jl@users.noreply.github.com> Date: Thu, 19 Oct 2023 00:56:22 -0700 Subject: [PATCH 2/3] Add more tests to cover some partial hits --- .../core/CoreUtilsMiscellaneousTest.java | 23 +++++++++++++++++++ .../client/jdbc/SnowflakeDriverLatestIT.java | 11 +++++++++ 2 files changed, 34 insertions(+) diff --git a/src/test/java/net/snowflake/client/core/CoreUtilsMiscellaneousTest.java b/src/test/java/net/snowflake/client/core/CoreUtilsMiscellaneousTest.java index 9f86db711..d1eaa390d 100644 --- a/src/test/java/net/snowflake/client/core/CoreUtilsMiscellaneousTest.java +++ b/src/test/java/net/snowflake/client/core/CoreUtilsMiscellaneousTest.java @@ -273,4 +273,27 @@ 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/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); From 4f607071ad77b50437031f0bdc7034c5b0c4e34c Mon Sep 17 00:00:00 2001 From: Joyce Ling <115662568+sfc-gh-ext-simba-jl@users.noreply.github.com> Date: Thu, 19 Oct 2023 09:19:31 -0700 Subject: [PATCH 3/3] fix style --- .../client/core/CoreUtilsMiscellaneousTest.java | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/test/java/net/snowflake/client/core/CoreUtilsMiscellaneousTest.java b/src/test/java/net/snowflake/client/core/CoreUtilsMiscellaneousTest.java index d1eaa390d..8a9843051 100644 --- a/src/test/java/net/snowflake/client/core/CoreUtilsMiscellaneousTest.java +++ b/src/test/java/net/snowflake/client/core/CoreUtilsMiscellaneousTest.java @@ -277,16 +277,7 @@ public void testSizeOfHttpClientMapWithGzipAndUserAgentSuffix() { @Test public void testNullAndEmptyProxySettingsForS3() { HttpClientSettingsKey testKey = - new HttpClientSettingsKey( - OCSPMode.FAIL_OPEN, - null, - 443, - null, - null, - null, - "", - "", - false); + new HttpClientSettingsKey(OCSPMode.FAIL_OPEN, null, 443, null, null, null, "", "", false); ClientConfiguration clientConfig = new ClientConfiguration(); HttpUtil.setProxyForS3(testKey, clientConfig); assertEquals(Protocol.HTTP, clientConfig.getProxyProtocol());