-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ported fedauth tests from internal test lab to junit (#1079)
- Loading branch information
1 parent
a87bccf
commit ff90cdb
Showing
15 changed files
with
2,421 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/test/java/com/microsoft/sqlserver/jdbc/fedauth/ConcurrentLoginTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
src/test/java/com/microsoft/sqlserver/jdbc/fedauth/ConnectionEncryptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.