Skip to content

Commit b774f68

Browse files
authored
Enable AD integrated tests for non-Windows (#1425)
1 parent 664c240 commit b774f68

File tree

7 files changed

+51
-32
lines changed

7 files changed

+51
-32
lines changed

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerADAL4JUtils.java

+24-11
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,34 @@ static SqlFedAuthToken getSqlFedAuthToken(SqlFedAuthInfo fedAuthInfo, String use
4141
return new SqlFedAuthToken(authenticationResult.getAccessToken(), authenticationResult.getExpiresOnDate());
4242
} catch (MalformedURLException | InterruptedException e) {
4343
throw new SQLServerException(e.getMessage(), e);
44-
} catch (ExecutionException e) {
44+
} catch (ExecutionException | AuthenticationException e) {
45+
if (adal4jLogger.isLoggable(Level.SEVERE)) {
46+
adal4jLogger.fine(adal4jLogger.toString() + " ADAL exception:" + e.getMessage());
47+
}
48+
4549
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_ADALExecution"));
4650
Object[] msgArgs = {user, authenticationString};
4751

4852
/*
4953
* the cause error message uses \\n\\r which does not give correct format change it to \r\n to provide
5054
* correct format
5155
*/
52-
String correctedErrorMessage = e.getCause().getMessage().replaceAll("\\\\r\\\\n", "\r\n");
53-
AuthenticationException correctedAuthenticationException = new AuthenticationException(
54-
correctedErrorMessage);
56+
if (null == e.getCause() || null == e.getCause().getMessage()) {
57+
throw new SQLServerException(form.format(msgArgs), null);
58+
} else {
59+
String correctedErrorMessage = e.getCause().getMessage().replaceAll("\\\\r\\\\n", "\r\n");
60+
AuthenticationException correctedAuthenticationException = new AuthenticationException(
61+
correctedErrorMessage);
5562

56-
/*
57-
* SQLServerException is caused by ExecutionException, which is caused by AuthenticationException to match
58-
* the exception tree before error message correction
59-
*/
60-
ExecutionException correctedExecutionException = new ExecutionException(correctedAuthenticationException);
63+
/*
64+
* SQLServerException is caused by ExecutionException, which is caused by AuthenticationException to
65+
* match the exception tree before error message correction
66+
*/
67+
ExecutionException correctedExecutionException = new ExecutionException(
68+
correctedAuthenticationException);
6169

62-
throw new SQLServerException(form.format(msgArgs), null, 0, correctedExecutionException);
70+
throw new SQLServerException(form.format(msgArgs), null, 0, correctedExecutionException);
71+
}
6372
} finally {
6473
executorService.shutdown();
6574
}
@@ -90,7 +99,11 @@ static SqlFedAuthToken getSqlFedAuthTokenIntegrated(SqlFedAuthInfo fedAuthInfo,
9099
return new SqlFedAuthToken(authenticationResult.getAccessToken(), authenticationResult.getExpiresOnDate());
91100
} catch (InterruptedException | IOException e) {
92101
throw new SQLServerException(e.getMessage(), e);
93-
} catch (ExecutionException e) {
102+
} catch (ExecutionException | AuthenticationException e) {
103+
if (adal4jLogger.isLoggable(Level.SEVERE)) {
104+
adal4jLogger.fine(adal4jLogger.toString() + " ADAL exception:" + e.getMessage());
105+
}
106+
94107
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_ADALExecution"));
95108
Object[] msgArgs = {"", authenticationString};
96109

src/test/java/com/microsoft/sqlserver/jdbc/fedauth/ConcurrentLoginTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void testConcurrentLogin() throws Exception {
9595

9696
t1.start();
9797
t2.start();
98-
if (isWindows && enableADIntegrated) {
98+
if (enableADIntegrated) {
9999
Thread t3 = new Thread(r3);
100100
t3.setUncaughtExceptionHandler(handler);
101101
t3.start();

src/test/java/com/microsoft/sqlserver/jdbc/fedauth/ConnectionSuspensionTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void testAccessTokenExpiredThenCreateNewStatementADPassword() throws SQLE
4242

4343
@Test
4444
public void testAccessTokenExpiredThenCreateNewStatementADIntegrated() throws SQLException {
45-
org.junit.Assume.assumeTrue(isWindows && enableADIntegrated);
45+
org.junit.Assume.assumeTrue(enableADIntegrated);
4646

4747
testAccessTokenExpiredThenCreateNewStatement(SqlAuthentication.ActiveDirectoryIntegrated);
4848
}
@@ -107,7 +107,7 @@ public void testAccessTokenExpiredThenExecuteUsingSameStatementADPassword() thro
107107

108108
@Test
109109
public void testAccessTokenExpiredThenExecuteUsingSameStatementADIntegrated() throws SQLException {
110-
org.junit.Assume.assumeTrue(isWindows && enableADIntegrated);
110+
org.junit.Assume.assumeTrue(enableADIntegrated);
111111

112112
testAccessTokenExpiredThenExecuteUsingSameStatement(SqlAuthentication.ActiveDirectoryIntegrated);
113113
}

src/test/java/com/microsoft/sqlserver/jdbc/fedauth/ErrorMessageTest.java

+12-11
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
@Tag(Constants.fedAuth)
2727
public class ErrorMessageTest extends FedauthCommon {
2828

29-
String userName = "abc" + azureUserName;
29+
String badUserName = "abc" + azureUserName;
3030
String connectionUrl = "jdbc:sqlserver://" + azureServer + ";database=" + azureDatabase;
3131

3232
@Test
@@ -214,13 +214,14 @@ public void testSQLPasswordWithUntrustedSqlDB() throws SQLException {
214214

215215
@Test
216216
public void testADPasswordUnregisteredUserWithConnectionStringUserName() throws SQLException {
217-
try (Connection connection = DriverManager.getConnection(connectionUrl + ";userName=" + userName + ";password="
218-
+ azurePassword + ";Authentication=" + SqlAuthentication.ActiveDirectoryPassword.toString())) {
217+
try (Connection connection = DriverManager
218+
.getConnection(connectionUrl + ";userName=" + badUserName + ";password=" + azurePassword
219+
+ ";Authentication=" + SqlAuthentication.ActiveDirectoryPassword.toString())) {
219220
fail(EXPECTED_EXCEPTION_NOT_THROWN);
220221
} catch (SQLServerException e) {
221222
assertTrue(INVALID_EXCEPION_MSG + ": " + e.getMessage(),
222223
e.getMessage()
223-
.contains(ERR_MSG_FAILED_AUTHENTICATE + " the user " + userName
224+
.contains(ERR_MSG_FAILED_AUTHENTICATE + " the user " + badUserName
224225
+ " in Active Directory (Authentication=ActiveDirectoryPassword).")
225226
&& e.getCause().getCause().getMessage().contains(ERR_MSG_SIGNIN_ADD));
226227
}
@@ -232,7 +233,7 @@ public void testADPasswordUnregisteredUserWithDatasource() throws SQLException {
232233
SQLServerDataSource ds = new SQLServerDataSource();
233234
ds.setServerName(azureServer);
234235
ds.setDatabaseName(azureDatabase);
235-
ds.setUser(userName);
236+
ds.setUser(badUserName);
236237
ds.setPassword(azurePassword);
237238
ds.setAuthentication(SqlAuthentication.ActiveDirectoryPassword.toString());
238239

@@ -241,21 +242,21 @@ public void testADPasswordUnregisteredUserWithDatasource() throws SQLException {
241242
} catch (SQLServerException e) {
242243
assertTrue(INVALID_EXCEPION_MSG + ": " + e.getMessage(),
243244
e.getMessage()
244-
.contains(ERR_MSG_FAILED_AUTHENTICATE + " the user " + userName
245+
.contains(ERR_MSG_FAILED_AUTHENTICATE + " the user " + badUserName
245246
+ " in Active Directory (Authentication=ActiveDirectoryPassword).")
246247
&& e.getCause().getCause().getMessage().contains(ERR_MSG_SIGNIN_ADD));
247248
}
248249
}
249250

250251
@Test
251252
public void testADPasswordUnregisteredUserWithConnectionStringUser() throws SQLException {
252-
try (Connection connection = DriverManager.getConnection(connectionUrl + ";user=" + userName + ";password="
253+
try (Connection connection = DriverManager.getConnection(connectionUrl + ";user=" + badUserName + ";password="
253254
+ azurePassword + ";Authentication=" + SqlAuthentication.ActiveDirectoryPassword.toString())) {
254255
fail(EXPECTED_EXCEPTION_NOT_THROWN);
255256
} catch (SQLServerException e) {
256257
assertTrue(INVALID_EXCEPION_MSG + ": " + e.getMessage(),
257258
e.getMessage()
258-
.contains(ERR_MSG_FAILED_AUTHENTICATE + " the user " + userName
259+
.contains(ERR_MSG_FAILED_AUTHENTICATE + " the user " + badUserName
259260
+ " in Active Directory (Authentication=ActiveDirectoryPassword).")
260261
&& e.getCause().getCause().getMessage().contains(ERR_MSG_SIGNIN_ADD));
261262
}
@@ -268,20 +269,20 @@ public void testAuthenticationAgainstSQLServerWithActivedirectorypassword() thro
268269
info.put("Authentication", SqlAuthentication.ActiveDirectoryPassword.toString());
269270

270271
try (Connection connection = DriverManager
271-
.getConnection(connectionUrl + ";user=" + userName + ";password=" + azurePassword, info)) {
272+
.getConnection(connectionUrl + ";user=" + badUserName + ";password=" + azurePassword, info)) {
272273
fail(EXPECTED_EXCEPTION_NOT_THROWN);
273274
} catch (Exception e) {
274275
if (!(e instanceof SQLServerException)) {
275276
fail(EXPECTED_EXCEPTION_NOT_THROWN);
276277
}
277278
assertTrue(INVALID_EXCEPION_MSG + ": " + e.getMessage(), e.getMessage().contains(ERR_MSG_FAILED_AUTHENTICATE
278-
+ " the user " + userName + " in Active Directory (Authentication=ActiveDirectoryPassword)."));
279+
+ " the user " + badUserName + " in Active Directory (Authentication=ActiveDirectoryPassword)."));
279280
}
280281
}
281282

282283
@Test
283284
public void testAuthenticationAgainstSQLServerWithActivedirectoryIntegrated() throws SQLException {
284-
org.junit.Assume.assumeTrue(isWindows && enableADIntegrated);
285+
org.junit.Assume.assumeTrue(enableADIntegrated);
285286

286287
java.util.Properties info = new Properties();
287288
info.put("TrustServerCertificate", "true");

src/test/java/com/microsoft/sqlserver/jdbc/fedauth/FedauthCommon.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public static void getConfigs() throws Exception {
116116
azureGroupUserName = getConfiguredProperty("azureGroupUserName");
117117

118118
String prop = getConfiguredProperty("enableADIntegrated");
119-
enableADIntegrated = (isWindows && null != prop && prop.equalsIgnoreCase("true")) ? true : false;
119+
enableADIntegrated = (null != prop && prop.equalsIgnoreCase("true")) ? true : false;
120120

121121
adPasswordConnectionStr = "jdbc:sqlserver://" + azureServer + ";database=" + azureDatabase + ";user="
122122
+ azureUserName + ";password=" + azurePassword + ";Authentication="
@@ -158,7 +158,12 @@ void testUserName(Connection conn, String user, SqlAuthentication authentication
158158
if (SqlAuthentication.ActiveDirectoryIntegrated != authentication) {
159159
assertTrue(user.equals(rs.getString(1)));
160160
} else {
161-
assertTrue(rs.getString(1).contains(System.getProperty("user.name")));
161+
if (isWindows) {
162+
assertTrue(rs.getString(1).contains(System.getProperty("user.name")));
163+
} else {
164+
// cannot verify user in kerberos tickets so just check it's not empty
165+
assertTrue(!rs.getString(1).isEmpty());
166+
}
162167
}
163168
}
164169
}

src/test/java/com/microsoft/sqlserver/jdbc/fedauth/FedauthTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void testActiveDirectoryPasswordDS() throws Exception {
106106

107107
@Test
108108
public void testActiveDirectoryIntegratedDS() throws Exception {
109-
org.junit.Assume.assumeTrue(isWindows && enableADIntegrated);
109+
org.junit.Assume.assumeTrue(enableADIntegrated);
110110

111111
SQLServerDataSource ds = new SQLServerDataSource();
112112
ds.setServerName(azureServer);
@@ -173,7 +173,7 @@ public void testNotValidSqlPassword() throws SQLException {
173173

174174
@Test
175175
public void testNotValidActiveDirectoryIntegrated() throws SQLException {
176-
org.junit.Assume.assumeTrue(isWindows && enableADIntegrated);
176+
org.junit.Assume.assumeTrue(enableADIntegrated);
177177

178178
testNotValid(SqlAuthentication.ActiveDirectoryIntegrated.toString(), false, true);
179179
testNotValid(SqlAuthentication.ActiveDirectoryIntegrated.toString(), true, true);
@@ -200,7 +200,7 @@ public void testValidSqlPassword() throws SQLException {
200200

201201
@Test
202202
public void testValidActiveDirectoryIntegrated() throws SQLException {
203-
org.junit.Assume.assumeTrue(isWindows && enableADIntegrated);
203+
org.junit.Assume.assumeTrue(enableADIntegrated);
204204

205205
testValid(SqlAuthentication.ActiveDirectoryIntegrated.toString(), false, true);
206206
testValid(SqlAuthentication.ActiveDirectoryIntegrated.toString(), true, true);

src/test/java/com/microsoft/sqlserver/jdbc/fedauth/PooledConnectionTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void testPooledConnectionAccessTokenExpiredThenReconnectADPassword() thro
5858

5959
@Test
6060
public void testPooledConnectionAccessTokenExpiredThenReconnectADIntegrated() throws SQLException {
61-
org.junit.Assume.assumeTrue(isWindows && enableADIntegrated);
61+
org.junit.Assume.assumeTrue(enableADIntegrated);
6262

6363
// suspend 5 mins
6464
testPooledConnectionAccessTokenExpiredThenReconnect((long) 5 * 60, SqlAuthentication.ActiveDirectoryIntegrated);
@@ -132,7 +132,7 @@ public void testPooledConnectionMultiThreadADPassword() throws SQLException {
132132

133133
@Test
134134
public void testPooledConnectionMultiThreadADIntegrated() throws SQLException {
135-
org.junit.Assume.assumeTrue(isWindows && enableADIntegrated);
135+
org.junit.Assume.assumeTrue(enableADIntegrated);
136136

137137
testPooledConnectionMultiThread(secondsBeforeExpiration, SqlAuthentication.ActiveDirectoryIntegrated);
138138
}

0 commit comments

Comments
 (0)