13
13
*/
14
14
package org .apache .hadoop .security .authentication .client ;
15
15
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 ;
16
20
import static org .apache .hadoop .security .authentication .server .MultiSchemeAuthenticationHandler .SCHEMES_PROPERTY ;
17
21
import static org .apache .hadoop .security .authentication .server .MultiSchemeAuthenticationHandler .AUTH_HANDLER_PROPERTY ;
18
22
import static org .apache .hadoop .security .authentication .server .AuthenticationFilter .AUTH_TYPE ;
34
38
import org .apache .hadoop .security .authentication .server .MultiSchemeAuthenticationHandler ;
35
39
import org .apache .hadoop .security .authentication .server .PseudoAuthenticationHandler ;
36
40
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 ;
41
44
42
45
import java .io .File ;
43
46
import java .net .HttpURLConnection ;
@@ -54,7 +57,7 @@ public class TestKerberosAuthenticator extends KerberosSecurityTestcase {
54
57
public TestKerberosAuthenticator () {
55
58
}
56
59
57
- @ Before
60
+ @ BeforeEach
58
61
public void setup () throws Exception {
59
62
// create keytab
60
63
File keytabFile = new File (KerberosTestUtils .getKeytabFile ());
@@ -89,7 +92,8 @@ private Properties getMultiAuthHandlerConfiguration() {
89
92
return props ;
90
93
}
91
94
92
- @ Test (timeout =60000 )
95
+ @ Test
96
+ @ Timeout (value = 60 )
93
97
public void testFallbacktoPseudoAuthenticator () throws Exception {
94
98
AuthenticatorTestCase auth = new AuthenticatorTestCase ();
95
99
Properties props = new Properties ();
@@ -99,7 +103,8 @@ public void testFallbacktoPseudoAuthenticator() throws Exception {
99
103
auth ._testAuthentication (new KerberosAuthenticator (), false );
100
104
}
101
105
102
- @ Test (timeout =60000 )
106
+ @ Test
107
+ @ Timeout (value = 60 )
103
108
public void testFallbacktoPseudoAuthenticatorAnonymous () throws Exception {
104
109
AuthenticatorTestCase auth = new AuthenticatorTestCase ();
105
110
Properties props = new Properties ();
@@ -109,7 +114,8 @@ public void testFallbacktoPseudoAuthenticatorAnonymous() throws Exception {
109
114
auth ._testAuthentication (new KerberosAuthenticator (), false );
110
115
}
111
116
112
- @ Test (timeout =60000 )
117
+ @ Test
118
+ @ Timeout (value = 60 )
113
119
public void testNotAuthenticated () throws Exception {
114
120
AuthenticatorTestCase auth = new AuthenticatorTestCase ();
115
121
AuthenticatorTestCase .setAuthenticationHandlerConfig (getAuthenticationHandlerConfiguration ());
@@ -118,14 +124,15 @@ public void testNotAuthenticated() throws Exception {
118
124
URL url = new URL (auth .getBaseURL ());
119
125
HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
120
126
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 );
123
129
} finally {
124
130
auth .stop ();
125
131
}
126
132
}
127
133
128
- @ Test (timeout =60000 )
134
+ @ Test
135
+ @ Timeout (value = 60 )
129
136
public void testAuthentication () throws Exception {
130
137
final AuthenticatorTestCase auth = new AuthenticatorTestCase ();
131
138
AuthenticatorTestCase .setAuthenticationHandlerConfig (
@@ -139,7 +146,8 @@ public Void call() throws Exception {
139
146
});
140
147
}
141
148
142
- @ Test (timeout =60000 )
149
+ @ Test
150
+ @ Timeout (value = 60 )
143
151
public void testAuthenticationPost () throws Exception {
144
152
final AuthenticatorTestCase auth = new AuthenticatorTestCase ();
145
153
AuthenticatorTestCase .setAuthenticationHandlerConfig (
@@ -153,7 +161,8 @@ public Void call() throws Exception {
153
161
});
154
162
}
155
163
156
- @ Test (timeout =60000 )
164
+ @ Test
165
+ @ Timeout (value = 60 )
157
166
public void testAuthenticationHttpClient () throws Exception {
158
167
final AuthenticatorTestCase auth = new AuthenticatorTestCase ();
159
168
AuthenticatorTestCase .setAuthenticationHandlerConfig (
@@ -167,7 +176,8 @@ public Void call() throws Exception {
167
176
});
168
177
}
169
178
170
- @ Test (timeout =60000 )
179
+ @ Test
180
+ @ Timeout (value = 60 )
171
181
public void testAuthenticationHttpClientPost () throws Exception {
172
182
final AuthenticatorTestCase auth = new AuthenticatorTestCase ();
173
183
AuthenticatorTestCase .setAuthenticationHandlerConfig (
@@ -181,7 +191,8 @@ public Void call() throws Exception {
181
191
});
182
192
}
183
193
184
- @ Test (timeout = 60000 )
194
+ @ Test
195
+ @ Timeout (value = 60 )
185
196
public void testNotAuthenticatedWithMultiAuthHandler () throws Exception {
186
197
AuthenticatorTestCase auth = new AuthenticatorTestCase ();
187
198
AuthenticatorTestCase
@@ -191,16 +202,17 @@ public void testNotAuthenticatedWithMultiAuthHandler() throws Exception {
191
202
URL url = new URL (auth .getBaseURL ());
192
203
HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
193
204
conn .connect ();
194
- Assert . assertEquals (HttpURLConnection .HTTP_UNAUTHORIZED ,
205
+ assertEquals (HttpURLConnection .HTTP_UNAUTHORIZED ,
195
206
conn .getResponseCode ());
196
- Assert . assertTrue (conn
207
+ assertTrue (conn
197
208
.getHeaderField (KerberosAuthenticator .WWW_AUTHENTICATE ) != null );
198
209
} finally {
199
210
auth .stop ();
200
211
}
201
212
}
202
213
203
- @ Test (timeout = 60000 )
214
+ @ Test
215
+ @ Timeout (value = 60 )
204
216
public void testAuthenticationWithMultiAuthHandler () throws Exception {
205
217
final AuthenticatorTestCase auth = new AuthenticatorTestCase ();
206
218
AuthenticatorTestCase
@@ -214,7 +226,8 @@ public Void call() throws Exception {
214
226
});
215
227
}
216
228
217
- @ Test (timeout = 60000 )
229
+ @ Test
230
+ @ Timeout (value = 60 )
218
231
public void testAuthenticationHttpClientPostWithMultiAuthHandler ()
219
232
throws Exception {
220
233
final AuthenticatorTestCase auth = new AuthenticatorTestCase ();
@@ -229,77 +242,81 @@ public Void call() throws Exception {
229
242
});
230
243
}
231
244
232
- @ Test (timeout = 60000 )
245
+ @ Test
246
+ @ Timeout (value = 60 )
233
247
public void testWrapExceptionWithMessage () {
234
248
IOException ex ;
235
249
ex = new IOException ("Induced exception" );
236
250
ex = KerberosAuthenticator .wrapExceptionWithMessage (ex , "Error while "
237
251
+ "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" ,
240
254
ex .getMessage ());
241
255
242
256
ex = new AuthenticationException ("Auth exception" );
243
257
ex = KerberosAuthenticator .wrapExceptionWithMessage (ex , "Error while "
244
258
+ "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" ,
247
261
ex .getMessage ());
248
262
249
263
// Test for Exception with no (String) constructor
250
264
// redirect the LOG to and check log message
251
265
ex = new CharacterCodingException ();
252
266
Exception ex2 = KerberosAuthenticator .wrapExceptionWithMessage (ex ,
253
267
"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 ));
256
270
}
257
271
258
- @ Test (timeout = 60000 )
272
+ @ Test
273
+ @ Timeout (value = 60 )
259
274
public void testNegotiate () throws NoSuchMethodException , InvocationTargetException ,
260
275
IllegalAccessException , IOException {
261
276
KerberosAuthenticator kerberosAuthenticator = new KerberosAuthenticator ();
262
277
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 )).
265
280
thenReturn (KerberosAuthenticator .NEGOTIATE );
266
- Mockito . when (conn .getResponseCode ()).thenReturn (HttpURLConnection .HTTP_UNAUTHORIZED );
281
+ when (conn .getResponseCode ()).thenReturn (HttpURLConnection .HTTP_UNAUTHORIZED );
267
282
268
283
Method method = KerberosAuthenticator .class .getDeclaredMethod ("isNegotiate" ,
269
284
HttpURLConnection .class );
270
285
method .setAccessible (true );
271
286
272
- Assert . assertTrue ((boolean )method .invoke (kerberosAuthenticator , conn ));
287
+ assertTrue ((boolean )method .invoke (kerberosAuthenticator , conn ));
273
288
}
274
289
275
- @ Test (timeout = 60000 )
290
+ @ Test
291
+ @ Timeout (value = 60 )
276
292
public void testNegotiateLowerCase () throws NoSuchMethodException , InvocationTargetException ,
277
293
IllegalAccessException , IOException {
278
294
KerberosAuthenticator kerberosAuthenticator = new KerberosAuthenticator ();
279
295
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" ))
282
298
.thenReturn (KerberosAuthenticator .NEGOTIATE );
283
- Mockito . when (conn .getResponseCode ()).thenReturn (HttpURLConnection .HTTP_UNAUTHORIZED );
299
+ when (conn .getResponseCode ()).thenReturn (HttpURLConnection .HTTP_UNAUTHORIZED );
284
300
285
301
Method method = KerberosAuthenticator .class .getDeclaredMethod ("isNegotiate" ,
286
302
HttpURLConnection .class );
287
303
method .setAccessible (true );
288
304
289
- Assert . assertTrue ((boolean )method .invoke (kerberosAuthenticator , conn ));
305
+ assertTrue ((boolean )method .invoke (kerberosAuthenticator , conn ));
290
306
}
291
307
292
- @ Test (timeout = 60000 )
308
+ @ Test
309
+ @ Timeout (value = 60 )
293
310
public void testReadToken () throws NoSuchMethodException , IOException , IllegalAccessException ,
294
311
InvocationTargetException {
295
312
KerberosAuthenticator kerberosAuthenticator = new KerberosAuthenticator ();
296
313
FieldUtils .writeField (kerberosAuthenticator , "base64" , new Base64 (), true );
297
314
298
315
Base64 base64 = new Base64 ();
299
316
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 ))
303
320
.thenReturn (KerberosAuthenticator .NEGOTIATE + " " +
304
321
Arrays .toString (base64 .encode ("foobar" .getBytes ())));
305
322
@@ -310,17 +327,18 @@ public void testReadToken() throws NoSuchMethodException, IOException, IllegalAc
310
327
method .invoke (kerberosAuthenticator , conn ); // expecting this not to throw an exception
311
328
}
312
329
313
- @ Test (timeout = 60000 )
330
+ @ Test
331
+ @ Timeout (value = 60 )
314
332
public void testReadTokenLowerCase () throws NoSuchMethodException , IOException ,
315
333
IllegalAccessException , InvocationTargetException {
316
334
KerberosAuthenticator kerberosAuthenticator = new KerberosAuthenticator ();
317
335
FieldUtils .writeField (kerberosAuthenticator , "base64" , new Base64 (), true );
318
336
319
337
Base64 base64 = new Base64 ();
320
338
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" ))
324
342
.thenReturn (KerberosAuthenticator .NEGOTIATE +
325
343
Arrays .toString (base64 .encode ("foobar" .getBytes ())));
326
344
0 commit comments