Skip to content

Commit

Permalink
Ported fedauth tests from internal test lab to junit (#1079)
Browse files Browse the repository at this point in the history
  • Loading branch information
lilgreenbird authored Jul 31, 2020
1 parent a87bccf commit ff90cdb
Show file tree
Hide file tree
Showing 15 changed files with 2,421 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ AE_Certificates/
*.bak
*.swp
*~.nib
*.cer
*.jks
local.properties
config.properties
.classpath
.vscode/
.settings/
Expand Down
4 changes: 2 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ jobs:
matrix:
SQL-2019:
Target_SQL: 'HGS-2k19-01'
Ex_Groups: 'xSQLv15,MSI,clientCertAuth'
Ex_Groups: 'xSQLv15,MSI,clientCertAuth,fedAuth'
SQL-2012:
Target_SQL: 'SQL-2K12-SP3-1'
Ex_Groups: 'xSQLv12,MSI'
Ex_Groups: 'xSQLv12,MSI,fedAuth'
maxParallel: 2
steps:
- powershell: |
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
clientCertAuth - - For tests requiring client certificate authentication setup (excluded by default)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Default testing enabled with SQL Server 2019 (SQLv15) -->
<excludedGroups>xSQLv12,xSQLv15,NTLM,MSI,reqExternalSetup,clientCertAuth</excludedGroups>
<excludedGroups>xSQLv12,xSQLv15,NTLM,MSI,reqExternalSetup,clientCertAuth,fedAuth</excludedGroups>

<!-- Use -preview for preview release, leave empty for official release.-->
<releaseExt></releaseExt>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ public class AESetup extends AbstractTest {
static Properties AEInfo;
static Map<String, SQLServerColumnEncryptionKeyStoreProvider> map = new HashMap<String, SQLServerColumnEncryptionKeyStoreProvider>();

// test that only run on Windows will be skipped
static boolean isWindows = System.getProperty("os.name").startsWith("Windows");

public static final String tableName = TestUtils
.escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("AETest_")));
public static final String CHAR_TABLE_AE = TestUtils
Expand Down
11 changes: 7 additions & 4 deletions src/test/java/com/microsoft/sqlserver/jdbc/TestResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ protected Object[][] getContents() {
{"R_failedValidate", "failed to validate values in $0} "}, {"R_tableNotDropped", "table not dropped. "},
{"R_connectionReset", "Connection reset"}, {"R_unknownException", "Unknown exception"},
{"R_deadConnection", "Dead connection should be invalid"},
{"R_wrongExceptionMessage", "Wrong exception message"},
{"R_wrongSqlState", "Wrong sql state"},
{"R_wrongExceptionMessage", "Wrong exception message"}, {"R_wrongSqlState", "Wrong sql state"},
{"R_parameterNotDefined", "Parameter {0} was not defined"},
{"R_unexpectedExceptionContent", "Unexpected content in exception message"},
{"R_connectionClosed", "The connection has been closed"},
Expand Down Expand Up @@ -188,7 +187,11 @@ protected Object[][] getContents() {
{"R_invalidEnclaveSessionFailed", "invalidate enclave session failed."},
{"R_invalidEnclaveType", "Invalid enclave type {0}."},
{"R_keystorePassword", "keystore password was incorrect"},
{"R_enclaveNotEnabled", "The statement triggers enclave computations"},
{"R_aeStreamReadError", "The multi-part identifier"},
{"R_enclaveNotEnabled", "The statement triggers enclave computations"}, {"R_hasClosed", "has closed"},
{"R_hasBeenClosed", "has been closed"}, {"R_cannotOpenServer", "Cannot open server"},
{"R_failedToAuthenticate", "Failed to authenticate"},
{"R_signinTooManyTimes", "You've tried to sign in too many times with an incorrect user ID or password."},
{"R_toSigninAdd", "To sign into this application, the account must be added to"},
{"R_socketClosed", "Socket closed"}, {"R_aeStreamReadError", "The multi-part identifier"},
{"R_dataClassificationNotSupported", "Data Classification is not supported on this server."}};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Microsoft JDBC Driver for SQL Server Copyright(c) Microsoft Corporation All rights reserved. This program is made
* available under the terms of the MIT License. See the LICENSE file in the project root for more information.
*/
package com.microsoft.sqlserver.jdbc.fedauth;

import static org.junit.jupiter.api.Assertions.fail;

import java.sql.Connection;
import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import com.microsoft.sqlserver.testframework.Constants;


@RunWith(JUnitPlatform.class)
@Tag(Constants.fedAuth)
public class ConcurrentLoginTest extends FedauthCommon {

final AtomicReference<Throwable> throwableRef = new AtomicReference<Throwable>();
Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {

@Override
public void uncaughtException(Thread t, Throwable e) {
throwableRef.set(e);
}
};

@Test
public void testConcurrentLogin() throws Exception {
Random rand = new Random();
int numberOfThreadsForEachType = rand.nextInt(15) + 1; // 1 to 15

Runnable r1 = () -> {
// Access token based authentication
try {
SQLServerDataSource ds = new SQLServerDataSource();
ds.setServerName(azureServer);
ds.setDatabaseName(azureDatabase);
ds.setAccessToken(accessToken);

try (Connection conn = ds.getConnection()) {
testUserName(conn, azureUserName, SqlAuthentication.NotSpecified);
}
} catch (Exception e) {
fail(e.getMessage());
}
};

Runnable r2 = () -> {
// active directory password
try {
SQLServerDataSource ds = new SQLServerDataSource();
ds.setServerName(azureServer);
ds.setDatabaseName(azureDatabase);
ds.setUser(azureUserName);
ds.setPassword(azurePassword);
ds.setAuthentication(SqlAuthentication.ActiveDirectoryPassword.toString());

try (Connection conn = ds.getConnection()) {
testUserName(conn, azureUserName, SqlAuthentication.ActiveDirectoryPassword);
}
} catch (Exception e) {
fail(e.getMessage());
}
};

Runnable r3 = () -> {
// active directory integrated
try {
SQLServerDataSource ds = new SQLServerDataSource();
ds.setServerName(azureServer);
ds.setDatabaseName(azureDatabase);
ds.setAuthentication(SqlAuthentication.ActiveDirectoryIntegrated.toString());

try (Connection conn = ds.getConnection()) {
testUserName(conn, azureUserName, SqlAuthentication.ActiveDirectoryIntegrated);
}
} catch (Exception e) {
fail(e.getMessage());
}
};

for (int i = 0; i < numberOfThreadsForEachType; i++) {
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.setUncaughtExceptionHandler(handler);
t2.setUncaughtExceptionHandler(handler);

t1.start();
t2.start();
if (isWindows && enableADIntegrated) {
Thread t3 = new Thread(r3);
t3.setUncaughtExceptionHandler(handler);
t3.start();
t3.join();
}
t1.join();
t2.join();

Throwable throwable = (Throwable) throwableRef.get();
if (throwable != null) {
fail(throwable.getMessage());
}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Microsoft JDBC Driver for SQL Server Copyright(c) Microsoft Corporation All rights reserved. This program is made
* available under the terms of the MIT License. See the LICENSE file in the project root for more information.
*/
package com.microsoft.sqlserver.jdbc.fedauth;

import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.MessageFormat;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

import com.microsoft.sqlserver.jdbc.RandomUtil;
import com.microsoft.sqlserver.jdbc.SQLServerException;
import com.microsoft.sqlserver.jdbc.TestUtils;
import com.microsoft.sqlserver.testframework.AbstractSQLGenerator;
import com.microsoft.sqlserver.testframework.Constants;


@RunWith(JUnitPlatform.class)
@Tag(Constants.fedAuth)
public class ConnectionEncryptionTest extends FedauthCommon {

static String charTable = TestUtils.escapeSingleQuotes(
AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("JDBC_ConnectionEncryption")));

@Test
public void testCorrectCertificate() throws SQLException {
try (Connection conn = DriverManager.getConnection(adPasswordConnectionStr);
Statement stmt = conn.createStatement()) {
testUserName(conn, azureUserName, SqlAuthentication.ActiveDirectoryPassword);

try {
TestUtils.dropTableIfExists(charTable, stmt);
createTable(stmt, charTable);
populateCharTable(conn, charTable);
testChar(stmt, charTable);
} finally {
TestUtils.dropTableIfExists(charTable, stmt);
}
} catch (Exception e) {
fail(e.getMessage());
}
}

@Test
public void testWrongCertificate() throws SQLException {
try (Connection conn = DriverManager
.getConnection(adPasswordConnectionStr + ";HostNameInCertificate=WrongCertificate")) {
fail(EXPECTED_EXCEPTION_NOT_THROWN);
} catch (Exception e) {
if (!(e instanceof SQLServerException)) {
fail(EXPECTED_EXCEPTION_NOT_THROWN);
}

MessageFormat form = new MessageFormat(TestUtils.R_BUNDLE.getString("R_sslFailed"));
Object[] msgArgs = {e.getCause().getLocalizedMessage()};
assertTrue(INVALID_EXCEPION_MSG + ": " + e.getMessage(),
e.getMessage().contains(form.format(msgArgs)));
}
}

// set TrustServerCertificate to true, which skips server certificate validation.
@Test
public void testWrongCertificateButTrustServerCertificate() throws SQLException {
try (Connection conn = DriverManager.getConnection(
adPasswordConnectionStr + ";HostNameInCertificate=WrongCertificate" + ";TrustServerCertificate=true");
Statement stmt = conn.createStatement()) {
testUserName(conn, azureUserName, SqlAuthentication.ActiveDirectoryPassword);

try {
TestUtils.dropTableIfExists(charTable, stmt);
createTable(stmt, charTable);
populateCharTable(conn, charTable);
testChar(stmt, charTable);
} finally {
TestUtils.dropTableIfExists(charTable, stmt);
}
} catch (Exception e) {
fail(e.getMessage());
}
}

@AfterAll
public static void terminate() throws SQLException {
try (Connection conn = DriverManager.getConnection(adPasswordConnectionStr);
Statement stmt = conn.createStatement()) {
TestUtils.dropTableIfExists(charTable, stmt);
}
}
}
Loading

0 comments on commit ff90cdb

Please sign in to comment.