Skip to content

Commit 72939fe

Browse files
slfan1989jojochuangHexiaoqiaocnaurothzhtttylz
authored
HADOOP-19417. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-minikdc. (#7636)
* HADOOP-19417. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-minikdc. Co-authored-by: Wei-Chiu Chuang <[email protected]> Co-authored-by: He Xiaoqiao <[email protected]> Co-authored-by: Chris Nauroth <[email protected]> Co-authored-by: Hualong Zhang <[email protected]> Reviewed-by: Wei-Chiu Chuang <[email protected]> Reviewed-by: He Xiaoqiao <[email protected]> Reviewed-by: Chris Nauroth <[email protected]> Reviewed-by: Hualong Zhang <[email protected]> Signed-off-by: Shilun Fan <[email protected]>
1 parent 96c380d commit 72939fe

File tree

10 files changed

+333
-247
lines changed

10 files changed

+333
-247
lines changed

hadoop-common-project/hadoop-auth/src/test/java/org/apache/hadoop/security/authentication/client/TestKerberosAuthenticator.java

+62-44
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
*/
1414
package org.apache.hadoop.security.authentication.client;
1515

16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertTrue;
18+
import static org.mockito.Mockito.mock;
19+
import static org.mockito.Mockito.when;
1620
import static org.apache.hadoop.security.authentication.server.MultiSchemeAuthenticationHandler.SCHEMES_PROPERTY;
1721
import static org.apache.hadoop.security.authentication.server.MultiSchemeAuthenticationHandler.AUTH_HANDLER_PROPERTY;
1822
import static org.apache.hadoop.security.authentication.server.AuthenticationFilter.AUTH_TYPE;
@@ -34,10 +38,9 @@
3438
import org.apache.hadoop.security.authentication.server.MultiSchemeAuthenticationHandler;
3539
import org.apache.hadoop.security.authentication.server.PseudoAuthenticationHandler;
3640
import org.apache.hadoop.security.authentication.server.KerberosAuthenticationHandler;
37-
import org.junit.Assert;
38-
import org.junit.Before;
39-
import org.junit.Test;
40-
import org.mockito.Mockito;
41+
import org.junit.jupiter.api.BeforeEach;
42+
import org.junit.jupiter.api.Test;
43+
import org.junit.jupiter.api.Timeout;
4144

4245
import java.io.File;
4346
import java.net.HttpURLConnection;
@@ -54,7 +57,7 @@ public class TestKerberosAuthenticator extends KerberosSecurityTestcase {
5457
public TestKerberosAuthenticator() {
5558
}
5659

57-
@Before
60+
@BeforeEach
5861
public void setup() throws Exception {
5962
// create keytab
6063
File keytabFile = new File(KerberosTestUtils.getKeytabFile());
@@ -89,7 +92,8 @@ private Properties getMultiAuthHandlerConfiguration() {
8992
return props;
9093
}
9194

92-
@Test(timeout=60000)
95+
@Test
96+
@Timeout(value = 60)
9397
public void testFallbacktoPseudoAuthenticator() throws Exception {
9498
AuthenticatorTestCase auth = new AuthenticatorTestCase();
9599
Properties props = new Properties();
@@ -99,7 +103,8 @@ public void testFallbacktoPseudoAuthenticator() throws Exception {
99103
auth._testAuthentication(new KerberosAuthenticator(), false);
100104
}
101105

102-
@Test(timeout=60000)
106+
@Test
107+
@Timeout(value = 60)
103108
public void testFallbacktoPseudoAuthenticatorAnonymous() throws Exception {
104109
AuthenticatorTestCase auth = new AuthenticatorTestCase();
105110
Properties props = new Properties();
@@ -109,7 +114,8 @@ public void testFallbacktoPseudoAuthenticatorAnonymous() throws Exception {
109114
auth._testAuthentication(new KerberosAuthenticator(), false);
110115
}
111116

112-
@Test(timeout=60000)
117+
@Test
118+
@Timeout(value = 60)
113119
public void testNotAuthenticated() throws Exception {
114120
AuthenticatorTestCase auth = new AuthenticatorTestCase();
115121
AuthenticatorTestCase.setAuthenticationHandlerConfig(getAuthenticationHandlerConfiguration());
@@ -118,14 +124,15 @@ public void testNotAuthenticated() throws Exception {
118124
URL url = new URL(auth.getBaseURL());
119125
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
120126
conn.connect();
121-
Assert.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, conn.getResponseCode());
122-
Assert.assertTrue(conn.getHeaderField(KerberosAuthenticator.WWW_AUTHENTICATE) != null);
127+
assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, conn.getResponseCode());
128+
assertTrue(conn.getHeaderField(KerberosAuthenticator.WWW_AUTHENTICATE) != null);
123129
} finally {
124130
auth.stop();
125131
}
126132
}
127133

128-
@Test(timeout=60000)
134+
@Test
135+
@Timeout(value = 60)
129136
public void testAuthentication() throws Exception {
130137
final AuthenticatorTestCase auth = new AuthenticatorTestCase();
131138
AuthenticatorTestCase.setAuthenticationHandlerConfig(
@@ -139,7 +146,8 @@ public Void call() throws Exception {
139146
});
140147
}
141148

142-
@Test(timeout=60000)
149+
@Test
150+
@Timeout(value = 60)
143151
public void testAuthenticationPost() throws Exception {
144152
final AuthenticatorTestCase auth = new AuthenticatorTestCase();
145153
AuthenticatorTestCase.setAuthenticationHandlerConfig(
@@ -153,7 +161,8 @@ public Void call() throws Exception {
153161
});
154162
}
155163

156-
@Test(timeout=60000)
164+
@Test
165+
@Timeout(value = 60)
157166
public void testAuthenticationHttpClient() throws Exception {
158167
final AuthenticatorTestCase auth = new AuthenticatorTestCase();
159168
AuthenticatorTestCase.setAuthenticationHandlerConfig(
@@ -167,7 +176,8 @@ public Void call() throws Exception {
167176
});
168177
}
169178

170-
@Test(timeout=60000)
179+
@Test
180+
@Timeout(value = 60)
171181
public void testAuthenticationHttpClientPost() throws Exception {
172182
final AuthenticatorTestCase auth = new AuthenticatorTestCase();
173183
AuthenticatorTestCase.setAuthenticationHandlerConfig(
@@ -181,7 +191,8 @@ public Void call() throws Exception {
181191
});
182192
}
183193

184-
@Test(timeout = 60000)
194+
@Test
195+
@Timeout(value = 60)
185196
public void testNotAuthenticatedWithMultiAuthHandler() throws Exception {
186197
AuthenticatorTestCase auth = new AuthenticatorTestCase();
187198
AuthenticatorTestCase
@@ -191,16 +202,17 @@ public void testNotAuthenticatedWithMultiAuthHandler() throws Exception {
191202
URL url = new URL(auth.getBaseURL());
192203
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
193204
conn.connect();
194-
Assert.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED,
205+
assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED,
195206
conn.getResponseCode());
196-
Assert.assertTrue(conn
207+
assertTrue(conn
197208
.getHeaderField(KerberosAuthenticator.WWW_AUTHENTICATE) != null);
198209
} finally {
199210
auth.stop();
200211
}
201212
}
202213

203-
@Test(timeout = 60000)
214+
@Test
215+
@Timeout(value = 60)
204216
public void testAuthenticationWithMultiAuthHandler() throws Exception {
205217
final AuthenticatorTestCase auth = new AuthenticatorTestCase();
206218
AuthenticatorTestCase
@@ -214,7 +226,8 @@ public Void call() throws Exception {
214226
});
215227
}
216228

217-
@Test(timeout = 60000)
229+
@Test
230+
@Timeout(value = 60)
218231
public void testAuthenticationHttpClientPostWithMultiAuthHandler()
219232
throws Exception {
220233
final AuthenticatorTestCase auth = new AuthenticatorTestCase();
@@ -229,77 +242,81 @@ public Void call() throws Exception {
229242
});
230243
}
231244

232-
@Test(timeout = 60000)
245+
@Test
246+
@Timeout(value = 60)
233247
public void testWrapExceptionWithMessage() {
234248
IOException ex;
235249
ex = new IOException("Induced exception");
236250
ex = KerberosAuthenticator.wrapExceptionWithMessage(ex, "Error while "
237251
+ "authenticating with endpoint: localhost");
238-
Assert.assertEquals("Induced exception", ex.getCause().getMessage());
239-
Assert.assertEquals("Error while authenticating with endpoint: localhost",
252+
assertEquals("Induced exception", ex.getCause().getMessage());
253+
assertEquals("Error while authenticating with endpoint: localhost",
240254
ex.getMessage());
241255

242256
ex = new AuthenticationException("Auth exception");
243257
ex = KerberosAuthenticator.wrapExceptionWithMessage(ex, "Error while "
244258
+ "authenticating with endpoint: localhost");
245-
Assert.assertEquals("Auth exception", ex.getCause().getMessage());
246-
Assert.assertEquals("Error while authenticating with endpoint: localhost",
259+
assertEquals("Auth exception", ex.getCause().getMessage());
260+
assertEquals("Error while authenticating with endpoint: localhost",
247261
ex.getMessage());
248262

249263
// Test for Exception with no (String) constructor
250264
// redirect the LOG to and check log message
251265
ex = new CharacterCodingException();
252266
Exception ex2 = KerberosAuthenticator.wrapExceptionWithMessage(ex,
253267
"Error while authenticating with endpoint: localhost");
254-
Assert.assertTrue(ex instanceof CharacterCodingException);
255-
Assert.assertTrue(ex.equals(ex2));
268+
assertTrue(ex instanceof CharacterCodingException);
269+
assertTrue(ex.equals(ex2));
256270
}
257271

258-
@Test(timeout = 60000)
272+
@Test
273+
@Timeout(value = 60)
259274
public void testNegotiate() throws NoSuchMethodException, InvocationTargetException,
260275
IllegalAccessException, IOException {
261276
KerberosAuthenticator kerberosAuthenticator = new KerberosAuthenticator();
262277

263-
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
264-
Mockito.when(conn.getHeaderField(KerberosAuthenticator.WWW_AUTHENTICATE)).
278+
HttpURLConnection conn = mock(HttpURLConnection.class);
279+
when(conn.getHeaderField(KerberosAuthenticator.WWW_AUTHENTICATE)).
265280
thenReturn(KerberosAuthenticator.NEGOTIATE);
266-
Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_UNAUTHORIZED);
281+
when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_UNAUTHORIZED);
267282

268283
Method method = KerberosAuthenticator.class.getDeclaredMethod("isNegotiate",
269284
HttpURLConnection.class);
270285
method.setAccessible(true);
271286

272-
Assert.assertTrue((boolean)method.invoke(kerberosAuthenticator, conn));
287+
assertTrue((boolean)method.invoke(kerberosAuthenticator, conn));
273288
}
274289

275-
@Test(timeout = 60000)
290+
@Test
291+
@Timeout(value = 60)
276292
public void testNegotiateLowerCase() throws NoSuchMethodException, InvocationTargetException,
277293
IllegalAccessException, IOException {
278294
KerberosAuthenticator kerberosAuthenticator = new KerberosAuthenticator();
279295

280-
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
281-
Mockito.when(conn.getHeaderField("www-authenticate"))
296+
HttpURLConnection conn = mock(HttpURLConnection.class);
297+
when(conn.getHeaderField("www-authenticate"))
282298
.thenReturn(KerberosAuthenticator.NEGOTIATE);
283-
Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_UNAUTHORIZED);
299+
when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_UNAUTHORIZED);
284300

285301
Method method = KerberosAuthenticator.class.getDeclaredMethod("isNegotiate",
286302
HttpURLConnection.class);
287303
method.setAccessible(true);
288304

289-
Assert.assertTrue((boolean)method.invoke(kerberosAuthenticator, conn));
305+
assertTrue((boolean)method.invoke(kerberosAuthenticator, conn));
290306
}
291307

292-
@Test(timeout = 60000)
308+
@Test
309+
@Timeout(value = 60)
293310
public void testReadToken() throws NoSuchMethodException, IOException, IllegalAccessException,
294311
InvocationTargetException {
295312
KerberosAuthenticator kerberosAuthenticator = new KerberosAuthenticator();
296313
FieldUtils.writeField(kerberosAuthenticator, "base64", new Base64(), true);
297314

298315
Base64 base64 = new Base64();
299316

300-
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
301-
Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_UNAUTHORIZED);
302-
Mockito.when(conn.getHeaderField(KerberosAuthenticator.WWW_AUTHENTICATE))
317+
HttpURLConnection conn = mock(HttpURLConnection.class);
318+
when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_UNAUTHORIZED);
319+
when(conn.getHeaderField(KerberosAuthenticator.WWW_AUTHENTICATE))
303320
.thenReturn(KerberosAuthenticator.NEGOTIATE + " " +
304321
Arrays.toString(base64.encode("foobar".getBytes())));
305322

@@ -310,17 +327,18 @@ public void testReadToken() throws NoSuchMethodException, IOException, IllegalAc
310327
method.invoke(kerberosAuthenticator, conn); // expecting this not to throw an exception
311328
}
312329

313-
@Test(timeout = 60000)
330+
@Test
331+
@Timeout(value = 60)
314332
public void testReadTokenLowerCase() throws NoSuchMethodException, IOException,
315333
IllegalAccessException, InvocationTargetException {
316334
KerberosAuthenticator kerberosAuthenticator = new KerberosAuthenticator();
317335
FieldUtils.writeField(kerberosAuthenticator, "base64", new Base64(), true);
318336

319337
Base64 base64 = new Base64();
320338

321-
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
322-
Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_UNAUTHORIZED);
323-
Mockito.when(conn.getHeaderField("www-authenticate"))
339+
HttpURLConnection conn = mock(HttpURLConnection.class);
340+
when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_UNAUTHORIZED);
341+
when(conn.getHeaderField("www-authenticate"))
324342
.thenReturn(KerberosAuthenticator.NEGOTIATE +
325343
Arrays.toString(base64.encode("foobar".getBytes())));
326344

hadoop-common-project/hadoop-auth/src/test/java/org/apache/hadoop/security/authentication/server/TestAltKerberosAuthenticationHandler.java

+38-18
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313
*/
1414
package org.apache.hadoop.security.authentication.server;
1515

16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
import static org.mockito.Mockito.mock;
18+
import static org.mockito.Mockito.when;
19+
1620
import java.io.IOException;
1721
import java.util.Properties;
1822
import javax.servlet.http.HttpServletRequest;
1923
import javax.servlet.http.HttpServletResponse;
2024
import org.apache.hadoop.security.authentication.client.AuthenticationException;
21-
import org.junit.Assert;
22-
import org.junit.Test;
23-
import org.mockito.Mockito;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.Timeout;
2427

2528
public class TestAltKerberosAuthenticationHandler
2629
extends TestKerberosAuthenticationHandler {
@@ -47,25 +50,41 @@ protected String getExpectedType() {
4750
return AltKerberosAuthenticationHandler.TYPE;
4851
}
4952

50-
@Test(timeout=60000)
53+
@Test
54+
@Timeout(value = 60)
5155
public void testAlternateAuthenticationAsBrowser() throws Exception {
52-
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
53-
HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
56+
if (handler != null) {
57+
handler.destroy();
58+
handler = null;
59+
}
60+
handler = getNewAuthenticationHandler();
61+
Properties props = getDefaultProperties();
62+
props.setProperty("alt-kerberos.non-browser.user-agents", "foo, bar");
63+
try {
64+
handler.init(props);
65+
} catch (Exception ex) {
66+
handler = null;
67+
throw ex;
68+
}
69+
70+
HttpServletRequest request = mock(HttpServletRequest.class);
71+
HttpServletResponse response = mock(HttpServletResponse.class);
5472

5573
// By default, a User-Agent without "java", "curl", "wget", or "perl" in it
5674
// is considered a browser
57-
Mockito.when(request.getHeader("User-Agent")).thenReturn("Some Browser");
75+
when(request.getHeader("User-Agent")).thenReturn("Some Browser");
5876

5977
AuthenticationToken token = handler.authenticate(request, response);
60-
Assert.assertEquals("A", token.getUserName());
61-
Assert.assertEquals("B", token.getName());
62-
Assert.assertEquals(getExpectedType(), token.getType());
78+
assertEquals("A", token.getUserName());
79+
assertEquals("B", token.getName());
80+
assertEquals(getExpectedType(), token.getType());
6381
}
6482

65-
@Test(timeout=60000)
83+
@Test
84+
@Timeout(value = 60)
6685
public void testNonDefaultNonBrowserUserAgentAsBrowser() throws Exception {
67-
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
68-
HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
86+
HttpServletRequest request = mock(HttpServletRequest.class);
87+
HttpServletResponse response = mock(HttpServletResponse.class);
6988

7089
if (handler != null) {
7190
handler.destroy();
@@ -82,15 +101,16 @@ public void testNonDefaultNonBrowserUserAgentAsBrowser() throws Exception {
82101
}
83102

84103
// Pretend we're something that will not match with "foo" (or "bar")
85-
Mockito.when(request.getHeader("User-Agent")).thenReturn("blah");
104+
when(request.getHeader("User-Agent")).thenReturn("blah");
86105
// Should use alt authentication
87106
AuthenticationToken token = handler.authenticate(request, response);
88-
Assert.assertEquals("A", token.getUserName());
89-
Assert.assertEquals("B", token.getName());
90-
Assert.assertEquals(getExpectedType(), token.getType());
107+
assertEquals("A", token.getUserName());
108+
assertEquals("B", token.getName());
109+
assertEquals(getExpectedType(), token.getType());
91110
}
92111

93-
@Test(timeout=60000)
112+
@Test
113+
@Timeout(value = 60)
94114
public void testNonDefaultNonBrowserUserAgentAsNonBrowser() throws Exception {
95115
if (handler != null) {
96116
handler.destroy();

0 commit comments

Comments
 (0)