diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/AESetup.java b/src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/AESetup.java index 6e4009533..6344ccf13 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/AESetup.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/AESetup.java @@ -163,12 +163,12 @@ public static void setUpConnection() throws TestAbortedException, Exception { readFromFile(Constants.JAVA_KEY_STORE_FILENAME, "Alias name"); - String enclaveAttestationUrl = TestUtils.getConfiguredProperty("enclaveAttestationUrl"); + String enclaveAttestationUrl = getConfiguredProperty("enclaveAttestationUrl"); if (null != enclaveAttestationUrl) { AETestConnectionString = TestUtils.addOrOverrideProperty(AETestConnectionString, "enclaveAttestationUrl", enclaveAttestationUrl); } - String enclaveAttestationProtocol = TestUtils.getConfiguredProperty("enclaveAttestationProtocol"); + String enclaveAttestationProtocol = getConfiguredProperty("enclaveAttestationProtocol"); if (null != enclaveAttestationProtocol) { AETestConnectionString = TestUtils.addOrOverrideProperty(AETestConnectionString, "enclaveAttestationProtocol", enclaveAttestationProtocol); diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/JDBCEncryptionDecryptionTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/JDBCEncryptionDecryptionTest.java index 2a75721ab..0e51d0adc 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/JDBCEncryptionDecryptionTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/JDBCEncryptionDecryptionTest.java @@ -17,6 +17,9 @@ import java.sql.Time; import java.sql.Timestamp; import java.util.LinkedList; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @@ -24,8 +27,15 @@ import org.junit.runner.RunWith; import org.opentest4j.TestAbortedException; +import com.microsoft.aad.adal4j.AuthenticationContext; +import com.microsoft.aad.adal4j.AuthenticationResult; +import com.microsoft.aad.adal4j.ClientCredential; import com.microsoft.sqlserver.jdbc.RandomData; +import com.microsoft.sqlserver.jdbc.SQLServerColumnEncryptionAzureKeyVaultProvider; +import com.microsoft.sqlserver.jdbc.SQLServerColumnEncryptionJavaKeyStoreProvider; import com.microsoft.sqlserver.jdbc.SQLServerConnection; +import com.microsoft.sqlserver.jdbc.SQLServerException; +import com.microsoft.sqlserver.jdbc.SQLServerKeyVaultAuthenticationCallback; import com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement; import com.microsoft.sqlserver.jdbc.SQLServerResultSet; import com.microsoft.sqlserver.jdbc.SQLServerStatement; @@ -59,6 +69,247 @@ enum TestCase { NULL } + /* + * Test getting/setting JKS name + */ + @Test + public void testJksName() { + try { + SQLServerColumnEncryptionJavaKeyStoreProvider jksp = new SQLServerColumnEncryptionJavaKeyStoreProvider( + javaKeyPath, new char[1]); + String keystoreName = "keystoreName"; + jksp.setName(keystoreName); + assertTrue(jksp.getName().equals(keystoreName)); + } catch (SQLServerException e) { + fail(TestResource.getResource("R_unexpectedException") + e.getMessage()); + } + } + + /* + * Test getting/setting AKV name + */ + @Test + public void testAkvName() { + try { + SQLServerColumnEncryptionAzureKeyVaultProvider akv = new SQLServerColumnEncryptionAzureKeyVaultProvider( + authenticationCallback); + String keystoreName = "keystoreName"; + akv.setName(keystoreName); + assertTrue(akv.getName().equals(keystoreName)); + } catch (SQLServerException e) { + fail(TestResource.getResource("R_unexpectedException") + e.getMessage()); + } + } + + /* + * Test bad Java Key Store + */ + @SuppressWarnings("unused") + @Test + public void testBadJks() { + try { + SQLServerColumnEncryptionJavaKeyStoreProvider jksp = new SQLServerColumnEncryptionJavaKeyStoreProvider(null, + null); + fail(TestResource.getResource("R_expectedExceptionNotThrown")); + } catch (SQLServerException e) { + assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_InvalidConnectionSetting"))); + } + } + + /* + * Test bad Azure Key Vault + */ + @SuppressWarnings("unused") + @Test + public void testBadAkv() { + try { + SQLServerColumnEncryptionAzureKeyVaultProvider akv = new SQLServerColumnEncryptionAzureKeyVaultProvider( + null); + fail(TestResource.getResource("R_expectedExceptionNotThrown")); + } catch (SQLServerException e) { + assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_NullValue"))); + } + } + + /* + * Test bad encryptColumnEncryptionKey for JKS + */ + @Test + public void testJksBadEncryptColumnEncryptionKey() { + SQLServerColumnEncryptionJavaKeyStoreProvider jksp = null; + char[] secret = new char[1]; + try { + jksp = new SQLServerColumnEncryptionJavaKeyStoreProvider(javaKeyPath, secret); + } catch (SQLServerException e) { + fail(TestResource.getResource("R_unexpectedException") + e.getMessage()); + } + + // null masterKeyPath + try { + jksp.encryptColumnEncryptionKey(null, null, null); + fail(TestResource.getResource("R_expectedExceptionNotThrown")); + } catch (SQLServerException e) { + assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_InvalidMasterKeyDetails"))); + } + + // empty cek + try { + byte[] emptyCek = new byte[0]; + jksp.encryptColumnEncryptionKey(javaKeyPath, Constants.CEK_ALGORITHM, emptyCek); + fail(TestResource.getResource("R_expectedExceptionNotThrown")); + } catch (SQLServerException e) { + assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_EmptyColumnEncryptionKey"))); + } + } + + /* + * Test bad encryptColumnEncryptionKey for AKV + */ + @Test + public void testAkvBadEncryptColumnEncryptionKey() { + SQLServerColumnEncryptionAzureKeyVaultProvider akv = null; + try { + akv = new SQLServerColumnEncryptionAzureKeyVaultProvider(authenticationCallback); + } catch (SQLServerException e) { + fail(TestResource.getResource("R_unexpectedException") + e.getMessage()); + } + + // null encryptedColumnEncryptionKey + try { + akv.encryptColumnEncryptionKey(keyIDs[0], Constants.CEK_ALGORITHM, null); + fail(TestResource.getResource("R_expectedExceptionNotThrown")); + } catch (SQLServerException e) { + assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_NullColumnEncryptionKey"))); + } + + // empty encryptedColumnEncryptionKey + try { + byte[] emptyCek = new byte[0]; + akv.encryptColumnEncryptionKey(keyIDs[0], Constants.CEK_ALGORITHM, emptyCek); + fail(TestResource.getResource("R_expectedExceptionNotThrown")); + } catch (SQLServerException e) { + assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_EmptyCEK"))); + } + } + + /* + * Test decryptColumnEncryptionKey for JKS + */ + @Test + public void testJksDecryptColumnEncryptionKey() { + SQLServerColumnEncryptionJavaKeyStoreProvider jksp = null; + char[] secret = new char[1]; + try { + jksp = new SQLServerColumnEncryptionJavaKeyStoreProvider("badkeypath", secret); + } catch (SQLServerException e) { + fail(TestResource.getResource("R_unexpectedException") + e.getMessage()); + } + + // null masterKeyPath + try { + jksp.decryptColumnEncryptionKey(null, null, null); + fail(TestResource.getResource("R_expectedExceptionNotThrown")); + } catch (SQLServerException e) { + assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_InvalidMasterKeyDetails"))); + } + + // bad keystore + try { + byte[] emptyCek = new byte[0]; + jksp.decryptColumnEncryptionKey("keypath", "algorithm", emptyCek); + fail(TestResource.getResource("R_expectedExceptionNotThrown")); + } catch (SQLServerException e) { + assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_KeyStoreNotFound"))); + } + + try { + jksp = new SQLServerColumnEncryptionJavaKeyStoreProvider(javaKeyPath, secret); + } catch (SQLServerException e) { + fail(TestResource.getResource("R_unexpectedException") + e.getMessage()); + } + + // bad cert + try { + byte[] badCek = new byte[1]; + jksp.decryptColumnEncryptionKey(javaKeyAliases, "RSA_OAEP", badCek); + fail(TestResource.getResource("R_expectedExceptionNotThrown")); + } catch (SQLServerException e) { + assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_invalidKeyStoreFile"))); + } + } + + /* + * Test decryptColumnEncryptionKey for AKV + */ + @Test + public void testAkvDecryptColumnEncryptionKey() { + SQLServerColumnEncryptionAzureKeyVaultProvider akv = null; + try { + akv = new SQLServerColumnEncryptionAzureKeyVaultProvider(authenticationCallback); + } catch (SQLServerException e) { + fail(TestResource.getResource("R_unexpectedException") + e.getMessage()); + } + + // null akvpath + try { + akv.decryptColumnEncryptionKey(null, "", null); + fail(TestResource.getResource("R_expectedExceptionNotThrown")); + } catch (SQLServerException e) { + assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_AKVPathNull"))); + } + + // invalid akvpath + try { + akv.decryptColumnEncryptionKey("keypath", "", null); + fail(TestResource.getResource("R_expectedExceptionNotThrown")); + } catch (SQLServerException e) { + assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_AKVMasterKeyPathInvalid"))); + } + + // invalid akvpath url + try { + akv.decryptColumnEncryptionKey("http:///^[!#$&-;=?-[]_a-", "", null); + fail(TestResource.getResource("R_expectedExceptionNotThrown")); + } catch (SQLServerException e) { + assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_AKVURLInvalid"))); + } + + // null encryptedColumnEncryptionKey + try { + akv.decryptColumnEncryptionKey(keyIDs[0], Constants.CEK_ALGORITHM, null); + fail(TestResource.getResource("R_expectedExceptionNotThrown")); + } catch (SQLServerException e) { + assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_NullEncryptedColumnEncryptionKey"))); + } + + // empty encryptedColumnEncryptionKey + try { + byte[] emptyCek = new byte[0]; + akv.decryptColumnEncryptionKey(keyIDs[0], Constants.CEK_ALGORITHM, emptyCek); + fail(TestResource.getResource("R_expectedExceptionNotThrown")); + } catch (SQLServerException e) { + assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_EmptyEncryptedColumnEncryptionKey"))); + } + + // invalid algorithm + try { + byte[] badCek = new byte[1]; + akv.decryptColumnEncryptionKey(keyIDs[0], "invalidAlgo", badCek); + fail(TestResource.getResource("R_expectedExceptionNotThrown")); + } catch (SQLServerException e) { + assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_InvalidKeyEncryptionAlgorithm"))); + } + + // bad encryptedColumnEncryptionKey + try { + byte[] badCek = new byte[1]; + akv.decryptColumnEncryptionKey(keyIDs[0], Constants.CEK_ALGORITHM, badCek); + fail(TestResource.getResource("R_expectedExceptionNotThrown")); + } catch (SQLServerException e) { + assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_InvalidEcryptionAlgorithmVersion"))); + } + } + /** * Junit test case for char set string for string values * @@ -1656,4 +1907,23 @@ void testNumerics(SQLServerStatement stmt, String cekName, String[][] table, Str testRichQuery(stmt, NUMERIC_TABLE_AE, table, values2); } } + + SQLServerKeyVaultAuthenticationCallback authenticationCallback = new SQLServerKeyVaultAuthenticationCallback() { + // @Override + ExecutorService service = Executors.newFixedThreadPool(2); + + public String getAccessToken(String authority, String resource, String scope) { + + AuthenticationResult result = null; + try { + AuthenticationContext context = new AuthenticationContext(authority, false, service); + ClientCredential cred = new ClientCredential(applicationClientID, applicationKey); + Future future = context.acquireToken(resource, cred, null); + result = future.get(); + } catch (Exception e) { + fail(TestResource.getResource("R_unexpectedException") + e.getMessage()); + } + return result.getAccessToken(); + } + }; } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/EnclavePackageTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/EnclavePackageTest.java index 8d4a21f51..c1a4bec19 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/EnclavePackageTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/EnclavePackageTest.java @@ -192,11 +192,11 @@ public static void setupEnclave() throws Exception { connectionStringEnclave = TestUtils.addOrOverrideProperty(connectionString, "columnEncryptionSetting", ColumnEncryptionSetting.Enabled.toString()); - String enclaveAttestationUrl = TestUtils.getConfiguredProperty("enclaveAttestationUrl"); + String enclaveAttestationUrl = System.getProperty("enclaveAttestationUrl"); connectionStringEnclave = TestUtils.addOrOverrideProperty(connectionStringEnclave, "enclaveAttestationUrl", (null != enclaveAttestationUrl) ? enclaveAttestationUrl : "http://blah"); - String enclaveAttestationProtocol = TestUtils.getConfiguredProperty("enclaveAttestationProtocol"); + String enclaveAttestationProtocol = System.getProperty("enclaveAttestationProtocol"); connectionStringEnclave = TestUtils.addOrOverrideProperty(connectionStringEnclave, "enclaveAttestationProtocol", (null != enclaveAttestationProtocol) ? enclaveAttestationProtocol : AttestationProtocol.HGS.toString()); @@ -260,6 +260,34 @@ public static void testInvalidProperties() { "R_enclaveInvalidAttestationProtocol"); } + /* + * Test bad Java Key Store + */ + @SuppressWarnings("unused") + public static void testBadJks() { + try { + SQLServerColumnEncryptionJavaKeyStoreProvider jksp = new SQLServerColumnEncryptionJavaKeyStoreProvider(null, + null); + fail(TestResource.getResource("R_expectedExceptionNotThrown")); + } catch (SQLServerException e) { + assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_InvalidConnectionSetting"))); + } + } + + /* + * Test bad Azure Key Vault + */ + @SuppressWarnings("unused") + public static void testBadAkv() { + try { + SQLServerColumnEncryptionAzureKeyVaultProvider akv = new SQLServerColumnEncryptionAzureKeyVaultProvider( + null); + fail(TestResource.getResource("R_expectedExceptionNotThrown")); + } catch (SQLServerException e) { + assertTrue(e.getMessage().matches(TestUtils.formatErrorMsg("R_NullValue"))); + } + } + /* * Test calling verifyColumnMasterKeyMetadata for non enclave computation */ diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/SQLServerConnectionTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/SQLServerConnectionTest.java index 2f5edccee..b09d959d6 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/SQLServerConnectionTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/SQLServerConnectionTest.java @@ -49,21 +49,207 @@ public class SQLServerConnectionTest extends AbstractTest { @Test public void testDataSource() { SQLServerDataSource ds = new SQLServerDataSource(); - ds.setUser("User"); - ds.setPassword("sUser"); - ds.setApplicationName("User"); - ds.setURL("jdbc:sqlserver://" + randomServer + ";packetSize=512"); + String stringPropValue = "stringPropValue"; + boolean booleanPropValue = true; + int intPropValue = 1; - String trustStore = "Store"; - String trustStorePassword = "pwd"; + ds.setInstanceName(stringPropValue); + assertEquals(stringPropValue, ds.getInstanceName(), TestResource.getResource("R_valuesAreDifferent")); - ds.setTrustStore(trustStore); - ds.setEncrypt(true); - ds.setTrustStorePassword(trustStorePassword); - ds.setTrustServerCertificate(true); - assertEquals(trustStore, ds.getTrustStore(), TestResource.getResource("R_valuesAreDifferent")); - assertEquals(true, ds.getEncrypt(), TestResource.getResource("R_valuesAreDifferent")); - assertEquals(true, ds.getTrustServerCertificate(), TestResource.getResource("R_valuesAreDifferent")); + ds.setUser(stringPropValue); + assertEquals(stringPropValue, ds.getUser(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setPassword(stringPropValue); + assertEquals(stringPropValue, ds.getPassword(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setApplicationName(stringPropValue); + assertEquals(stringPropValue, ds.getApplicationName(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setDatabaseName(stringPropValue); + assertEquals(stringPropValue, ds.getDatabaseName(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setPortNumber(intPropValue); + assertEquals(intPropValue, ds.getPortNumber(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setURL(stringPropValue); + assertEquals(stringPropValue, ds.getURL(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setDescription(stringPropValue); + assertEquals(stringPropValue, ds.getDescription(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setPacketSize(intPropValue); + assertEquals(intPropValue, ds.getPacketSize(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setQueryTimeout(intPropValue); + assertEquals(intPropValue, ds.getQueryTimeout(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setCancelQueryTimeout(intPropValue); + assertEquals(intPropValue, ds.getCancelQueryTimeout(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setEnablePrepareOnFirstPreparedStatementCall(booleanPropValue); + assertEquals(booleanPropValue, ds.getEnablePrepareOnFirstPreparedStatementCall(), + TestResource.getResource("R_valuesAreDifferent")); + + ds.setEnablePrepareOnFirstPreparedStatementCall(booleanPropValue); + assertEquals(booleanPropValue, ds.getEnablePrepareOnFirstPreparedStatementCall(), + TestResource.getResource("R_valuesAreDifferent")); + + ds.setServerPreparedStatementDiscardThreshold(intPropValue); + assertEquals(intPropValue, ds.getServerPreparedStatementDiscardThreshold(), + TestResource.getResource("R_valuesAreDifferent")); + + ds.setStatementPoolingCacheSize(intPropValue); + assertEquals(intPropValue, ds.getStatementPoolingCacheSize(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setDisableStatementPooling(booleanPropValue); + assertEquals(booleanPropValue, ds.getDisableStatementPooling(), + TestResource.getResource("R_valuesAreDifferent")); + + ds.setSocketTimeout(intPropValue); + assertEquals(intPropValue, ds.getSocketTimeout(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setUseBulkCopyForBatchInsert(booleanPropValue); + assertEquals(booleanPropValue, ds.getUseBulkCopyForBatchInsert(), + TestResource.getResource("R_valuesAreDifferent")); + + ds.setJASSConfigurationName(stringPropValue); + assertEquals(stringPropValue, ds.getJASSConfigurationName(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setMSIClientId(stringPropValue); + assertEquals(stringPropValue, ds.getMSIClientId(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setAuthenticationScheme(stringPropValue); + // there is no corresponding getAuthenticationScheme + + ds.setAuthentication(stringPropValue); + assertEquals(stringPropValue, ds.getAuthentication(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setAccessToken(stringPropValue); + assertEquals(stringPropValue, ds.getAccessToken(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setLastUpdateCount(booleanPropValue); + assertEquals(booleanPropValue, ds.getLastUpdateCount(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setTransparentNetworkIPResolution(booleanPropValue); + assertEquals(booleanPropValue, ds.getTransparentNetworkIPResolution(), + TestResource.getResource("R_valuesAreDifferent")); + + ds.setTrustServerCertificate(booleanPropValue); + assertEquals(booleanPropValue, ds.getTrustServerCertificate(), + TestResource.getResource("R_valuesAreDifferent")); + + ds.setTrustStoreType(stringPropValue); + assertEquals(stringPropValue, ds.getTrustStoreType(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setTrustStore(stringPropValue); + assertEquals(stringPropValue, ds.getTrustStore(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setTrustStorePassword(stringPropValue); + assertEquals(stringPropValue, ds.getTrustStorePassword(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setEncrypt(booleanPropValue); + assertEquals(booleanPropValue, ds.getEncrypt(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setHostNameInCertificate(stringPropValue); + assertEquals(stringPropValue, ds.getHostNameInCertificate(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setLockTimeout(intPropValue); + assertEquals(intPropValue, ds.getLockTimeout(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setSelectMethod(stringPropValue); + assertEquals(stringPropValue, ds.getSelectMethod(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setResponseBuffering(stringPropValue); + assertEquals(stringPropValue, ds.getResponseBuffering(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setApplicationIntent(stringPropValue); + assertEquals(stringPropValue, ds.getApplicationIntent(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setSendTimeAsDatetime(booleanPropValue); + assertEquals(booleanPropValue, ds.getSendTimeAsDatetime(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setUseFmtOnly(booleanPropValue); + assertEquals(booleanPropValue, ds.getUseFmtOnly(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setSendStringParametersAsUnicode(booleanPropValue); + assertEquals(booleanPropValue, ds.getSendStringParametersAsUnicode(), + TestResource.getResource("R_valuesAreDifferent")); + + ds.setServerNameAsACE(booleanPropValue); + assertEquals(booleanPropValue, ds.getServerNameAsACE(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setServerName(stringPropValue); + assertEquals(stringPropValue, ds.getServerName(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setServerSpn(stringPropValue); + assertEquals(stringPropValue, ds.getServerSpn(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setFailoverPartner(stringPropValue); + assertEquals(stringPropValue, ds.getFailoverPartner(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setMultiSubnetFailover(booleanPropValue); + assertEquals(booleanPropValue, ds.getMultiSubnetFailover(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setWorkstationID(stringPropValue); + assertEquals(stringPropValue, ds.getWorkstationID(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setXopenStates(booleanPropValue); + assertEquals(booleanPropValue, ds.getXopenStates(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setFIPS(booleanPropValue); + assertEquals(booleanPropValue, ds.getFIPS(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setSSLProtocol(stringPropValue); + assertEquals(stringPropValue, ds.getSSLProtocol(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setTrustManagerClass(stringPropValue); + assertEquals(stringPropValue, ds.getTrustManagerClass(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setTrustManagerConstructorArg(stringPropValue); + assertEquals(stringPropValue, ds.getTrustManagerConstructorArg(), + TestResource.getResource("R_valuesAreDifferent")); + + ds.setHostNameInCertificate(stringPropValue); + assertEquals(stringPropValue, ds.getHostNameInCertificate(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setDomain(stringPropValue); + assertEquals(stringPropValue, ds.getDomain(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setColumnEncryptionSetting(stringPropValue); + assertEquals(stringPropValue, ds.getColumnEncryptionSetting(), + TestResource.getResource("R_valuesAreDifferent")); + + ds.setKeyStoreAuthentication(stringPropValue); + assertEquals(stringPropValue, ds.getKeyStoreAuthentication(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setKeyStoreSecret(stringPropValue); + // there is no corresponding getKeyStoreSecret + + ds.setKeyStoreLocation(stringPropValue); + assertEquals(stringPropValue, ds.getKeyStoreLocation(), TestResource.getResource("R_valuesAreDifferent")); + + ds.setEnclaveAttestationUrl(stringPropValue); + assertTrue(ds.getEnclaveAttestationUrl().equals(stringPropValue)); + + ds.setEnclaveAttestationProtocol(stringPropValue); + assertTrue(ds.getEnclaveAttestationProtocol().equals(stringPropValue)); + + ds.setKeyVaultProviderClientId(stringPropValue); + assertTrue(ds.getKeyVaultProviderClientId().equals(stringPropValue)); + + ds.setKeyVaultProviderClientKey(stringPropValue); + // there is no corresponding getKeyVaultProviderClientKey + } + + @Test + public void testDSConnection() { + SQLServerDataSource ds = new SQLServerDataSource(); + updateDataSource(connectionString, ds); + + // getPassword can only be accessed within package + try (Connection con = ds.getConnection(ds.getUser(), ds.getPassword())) {} catch (Exception e) { + fail(TestResource.getResource("R_unexpectedErrorMessage") + e.getMessage()); + } } @Test diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/TestUtils.java b/src/test/java/com/microsoft/sqlserver/jdbc/TestUtils.java index 208d9a1aa..2b5d6ab74 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/TestUtils.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/TestUtils.java @@ -119,38 +119,6 @@ public static boolean isAEv2(Connection con) { return ((SQLServerConnection) con).isAEv2(); } - /** - * Read variable from property files if found null try to read from env. - * - * @param key - * @return Value - */ - public static String getConfiguredProperty(String key) { - String value = System.getProperty(key); - - if (value == null) { - value = System.getenv(key); - } - - return value; - } - - /** - * Convenient method for {@link #getConfiguredProperty(String)} - * - * @param key - * @return Value - */ - public static String getConfiguredProperty(String key, String defaultValue) { - String value = getConfiguredProperty(key); - - if (value == null) { - value = defaultValue; - } - - return value; - } - /** * * @param javatype diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/connection/NativeMSSQLDataSourceTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/connection/NativeMSSQLDataSourceTest.java index fab794b2f..ec232b2d0 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/connection/NativeMSSQLDataSourceTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/connection/NativeMSSQLDataSourceTest.java @@ -46,10 +46,12 @@ public void testSerialization() throws IOException { try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ObjectOutput objectOutput = new ObjectOutputStream(outputStream)) { SQLServerDataSource ds = new SQLServerDataSource(); - ds.setLogWriter(new PrintWriter(new ByteArrayOutputStream())); - + PrintWriter out = new PrintWriter(new ByteArrayOutputStream()); + ds.setLogWriter(out); objectOutput.writeObject(ds); objectOutput.flush(); + + assertTrue(out == ds.getLogWriter()); } } @@ -109,6 +111,13 @@ public void testInterfaceWrapping() throws ClassNotFoundException, SQLException ids3.setApplicationName("AppName"); } + @Test + public void testDSReference() { + SQLServerDataSource ds = new SQLServerDataSource(); + assertTrue(ds.getReference().getClassName() + .equals("com.microsoft.sqlserver.jdbc.SQLServerDataSource")); + } + private SQLServerDataSource testSerial(SQLServerDataSource ds) throws IOException, ClassNotFoundException { try (java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream(); java.io.ObjectOutput objectOutput = new java.io.ObjectOutputStream(outputStream)) { diff --git a/src/test/java/com/microsoft/sqlserver/testframework/AbstractTest.java b/src/test/java/com/microsoft/sqlserver/testframework/AbstractTest.java index ee3332b33..98c63a83e 100644 --- a/src/test/java/com/microsoft/sqlserver/testframework/AbstractTest.java +++ b/src/test/java/com/microsoft/sqlserver/testframework/AbstractTest.java @@ -6,6 +6,9 @@ package com.microsoft.sqlserver.testframework; import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; import java.io.PrintStream; import java.sql.Connection; import java.sql.ResultSet; @@ -13,6 +16,7 @@ import java.sql.Statement; import java.util.HashMap; import java.util.Map; +import java.util.Properties; import java.util.logging.ConsoleHandler; import java.util.logging.FileHandler; import java.util.logging.Handler; @@ -79,6 +83,7 @@ public abstract class AbstractTest { */ private static ByteArrayOutputStream logOutputStream = null; private static PrintStream logPrintStream = null; + private static Properties configProperties = null; /** * This will take care of all initialization before running the Test Suite. @@ -91,7 +96,15 @@ public static void setup() throws Exception { // Invoke fine logging... invokeLogging(); - connectionString = getConfiguredProperty(Constants.MSSQL_JDBC_TEST_CONNECTION_PROPERTIES); + // get Properties from config file + try (InputStream input = new FileInputStream(Constants.CONFIG_PROPERTIES_FILE)) { + configProperties = new Properties(); + configProperties.load(input); + } catch (FileNotFoundException | SecurityException e) { + // no config file used + } + + connectionString = getConfiguredPropertyOrEnv(Constants.MSSQL_JDBC_TEST_CONNECTION_PROPERTIES); connectionStringNTLM = connectionString; applicationClientID = getConfiguredProperty("applicationClientID"); @@ -118,9 +131,9 @@ public static void setup() throws Exception { } // if these properties are defined then NTLM is desired, modify connection string accordingly - String domain = System.getProperty("domainNTLM"); - String user = System.getProperty("userNTLM"); - String password = System.getProperty("passwordNTLM"); + String domain = getConfiguredProperty("domainNTLM"); + String user = getConfiguredProperty("userNTLM"); + String password = getConfiguredProperty("passwordNTLM"); if (null != domain) { connectionStringNTLM = TestUtils.addOrOverrideProperty(connectionStringNTLM, "domain", domain); @@ -302,30 +315,6 @@ public static void teardown() throws Exception { } } - /** - * Read variable from property files if found null try to read from env. - * - * @param key - * Key - * @return Value - */ - public static String getConfiguredProperty(String key) { - return TestUtils.getConfiguredProperty(key); - } - - /** - * Convenient method for {@link #getConfiguredProperty(String)} - * - * @param key - * Key - * @param defaultValue - * Default Value - * @return Value - */ - public static String getConfiguredProperty(String key, String defaultValue) { - return TestUtils.getConfiguredProperty(key, defaultValue); - } - /** * Invoke logging. */ @@ -333,14 +322,14 @@ public static void invokeLogging() { Handler handler = null; // enable logging to stream by default for tests - String enableLogging = getConfiguredProperty(Constants.MSSQL_JDBC_LOGGING, Boolean.TRUE.toString()); + String enableLogging = getConfiguredPropertyOrEnv(Constants.MSSQL_JDBC_LOGGING, Boolean.TRUE.toString()); // If logging is not enable then return. if (!Boolean.TRUE.toString().equalsIgnoreCase(enableLogging)) { return; } - String loggingHandler = getConfiguredProperty(Constants.MSSQL_JDBC_LOGGING_HANDLER, + String loggingHandler = getConfiguredPropertyOrEnv(Constants.MSSQL_JDBC_LOGGING_HANDLER, Constants.LOGGING_HANDLER_STREAM); try { @@ -414,4 +403,72 @@ private static void isSqlAzureOrAzureDW(Connection con) throws SQLException { determinedSqlAzureOrSqlServer = true; } } + + /** + * Read property from system or config properties file + * + * @param key + * @return property value + */ + protected static String getConfiguredProperty(String key) { + String value = System.getProperty(key); + + if (null == value && null != configProperties) { + return configProperties.getProperty(key); + } + + return value; + } + + /** + * Read property from system or config properties file or read from env var + * + * @param key + * @return property value + */ + private static String getConfiguredPropertyOrEnv(String key) { + String value = getConfiguredProperty(key); + + if (null == value) { + return System.getenv(key); + } + + return value; + } + + /** + * Read property from system or config properties file if not set return default value + * + * @param key + * @return property value or default value + */ + private static String getConfiguredProperty(String key, String defaultValue) { + String value = getConfiguredProperty(key); + + if (null == value) { + return defaultValue; + } + + return value; + } + + /** + * Read property from system or config properties file or env var if not set return default value + * + * @param key + * @return property value or default value + */ + private static String getConfiguredPropertyOrEnv(String key, String defaultValue) { + String value = getConfiguredProperty(key); + + if (null == value) { + value = System.getenv(key); + } + + if (null == value) { + value = defaultValue; + } + + return value; + } } diff --git a/src/test/java/com/microsoft/sqlserver/testframework/Constants.java b/src/test/java/com/microsoft/sqlserver/testframework/Constants.java index 98c3c5501..e6f16958e 100644 --- a/src/test/java/com/microsoft/sqlserver/testframework/Constants.java +++ b/src/test/java/com/microsoft/sqlserver/testframework/Constants.java @@ -138,6 +138,8 @@ private Constants() {} public static final String ENCLAVE_ATTESTATIONURL = "enclaveAttestationUrl"; public static final String ENCLAVE_ATTESTATIONPROTOCOL = "enclaveAttestationProtocol"; + public static final String CONFIG_PROPERTIES_FILE = "config.properties"; + public enum LOB { CLOB, NCLOB,