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