16 |
|
|
17 |
|
public class ExecutorsTest extends JSR166TestCase { |
18 |
|
public static void main(String[] args) { |
19 |
< |
junit.textui.TestRunner.run (suite()); |
19 |
> |
junit.textui.TestRunner.run(suite()); |
20 |
|
} |
21 |
|
public static Test suite() { |
22 |
|
return new TestSuite(ExecutorsTest.class); |
220 |
|
} |
221 |
|
|
222 |
|
/** |
223 |
< |
* Future.get on submitted tasks will time out if they compute too long. |
223 |
> |
* Future.get on submitted tasks will time out if they compute too long. |
224 |
|
*/ |
225 |
|
public void testTimedCallable() throws Exception { |
226 |
< |
final Runnable sleeper = |
227 |
< |
new RunnableShouldThrow(InterruptedException.class) { |
228 |
< |
public void realRun() throws InterruptedException { |
229 |
< |
Thread.sleep(LONG_DELAY_MS); |
230 |
< |
}}; |
226 |
> |
final Runnable sleeper = new CheckedInterruptedRunnable() { |
227 |
> |
public void realRun() throws InterruptedException { |
228 |
> |
Thread.sleep(LONG_DELAY_MS); |
229 |
> |
}}; |
230 |
|
for (ExecutorService executor : |
231 |
|
new ExecutorService[] { |
232 |
|
Executors.newSingleThreadExecutor(), |
257 |
|
*/ |
258 |
|
public void testDefaultThreadFactory() throws Exception { |
259 |
|
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
260 |
< |
Runnable r = new Runnable() { |
261 |
< |
public void run() { |
262 |
< |
try { |
263 |
< |
Thread current = Thread.currentThread(); |
264 |
< |
threadAssertTrue(!current.isDaemon()); |
265 |
< |
threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
266 |
< |
ThreadGroup g = current.getThreadGroup(); |
267 |
< |
SecurityManager s = System.getSecurityManager(); |
268 |
< |
if (s != null) |
269 |
< |
threadAssertTrue(g == s.getThreadGroup()); |
270 |
< |
else |
271 |
< |
threadAssertTrue(g == egroup); |
272 |
< |
String name = current.getName(); |
273 |
< |
threadAssertTrue(name.endsWith("thread-1")); |
274 |
< |
} catch (SecurityException ok) { |
275 |
< |
// Also pass if not allowed to change setting |
277 |
< |
} |
260 |
> |
Runnable r = new CheckedRunnable() { |
261 |
> |
public void realRun() { |
262 |
> |
try { |
263 |
> |
Thread current = Thread.currentThread(); |
264 |
> |
assertTrue(!current.isDaemon()); |
265 |
> |
assertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
266 |
> |
ThreadGroup g = current.getThreadGroup(); |
267 |
> |
SecurityManager s = System.getSecurityManager(); |
268 |
> |
if (s != null) |
269 |
> |
assertTrue(g == s.getThreadGroup()); |
270 |
> |
else |
271 |
> |
assertTrue(g == egroup); |
272 |
> |
String name = current.getName(); |
273 |
> |
assertTrue(name.endsWith("thread-1")); |
274 |
> |
} catch (SecurityException ok) { |
275 |
> |
// Also pass if not allowed to change setting |
276 |
|
} |
277 |
< |
}; |
277 |
> |
}}; |
278 |
|
ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()); |
279 |
|
|
280 |
|
e.execute(r); |
296 |
|
* access control context and context class loader |
297 |
|
*/ |
298 |
|
public void testPrivilegedThreadFactory() throws Exception { |
299 |
< |
Policy savedPolicy = null; |
300 |
< |
try { |
301 |
< |
savedPolicy = Policy.getPolicy(); |
302 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
303 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
304 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
305 |
< |
Policy.setPolicy(policy); |
308 |
< |
} catch (AccessControlException ok) { |
309 |
< |
return; |
310 |
< |
} |
311 |
< |
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
312 |
< |
final ClassLoader thisccl = Thread.currentThread().getContextClassLoader(); |
313 |
< |
final AccessControlContext thisacc = AccessController.getContext(); |
314 |
< |
Runnable r = new Runnable() { |
315 |
< |
public void run() { |
316 |
< |
try { |
299 |
> |
Runnable r = new CheckedRunnable() { |
300 |
> |
public void realRun() throws Exception { |
301 |
> |
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
302 |
> |
final ClassLoader thisccl = Thread.currentThread().getContextClassLoader(); |
303 |
> |
final AccessControlContext thisacc = AccessController.getContext(); |
304 |
> |
Runnable r = new CheckedRunnable() { |
305 |
> |
public void realRun() { |
306 |
|
Thread current = Thread.currentThread(); |
307 |
< |
threadAssertTrue(!current.isDaemon()); |
308 |
< |
threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
307 |
> |
assertTrue(!current.isDaemon()); |
308 |
> |
assertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
309 |
|
ThreadGroup g = current.getThreadGroup(); |
310 |
|
SecurityManager s = System.getSecurityManager(); |
311 |
|
if (s != null) |
312 |
< |
threadAssertTrue(g == s.getThreadGroup()); |
312 |
> |
assertTrue(g == s.getThreadGroup()); |
313 |
|
else |
314 |
< |
threadAssertTrue(g == egroup); |
314 |
> |
assertTrue(g == egroup); |
315 |
|
String name = current.getName(); |
316 |
< |
threadAssertTrue(name.endsWith("thread-1")); |
317 |
< |
threadAssertTrue(thisccl == current.getContextClassLoader()); |
318 |
< |
threadAssertTrue(thisacc.equals(AccessController.getContext())); |
319 |
< |
} catch (SecurityException ok) { |
320 |
< |
// Also pass if not allowed to change settings |
321 |
< |
} |
322 |
< |
} |
323 |
< |
}; |
324 |
< |
ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); |
316 |
> |
assertTrue(name.endsWith("thread-1")); |
317 |
> |
assertSame(thisccl, current.getContextClassLoader()); |
318 |
> |
assertEquals(thisacc, AccessController.getContext()); |
319 |
> |
}}; |
320 |
> |
ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); |
321 |
> |
e.execute(r); |
322 |
> |
e.shutdown(); |
323 |
> |
Thread.sleep(SHORT_DELAY_MS); |
324 |
> |
joinPool(e); |
325 |
> |
}}; |
326 |
> |
|
327 |
> |
runWithPermissions(r, |
328 |
> |
new RuntimePermission("getClassLoader"), |
329 |
> |
new RuntimePermission("setContextClassLoader"), |
330 |
> |
new RuntimePermission("modifyThread")); |
331 |
> |
} |
332 |
|
|
333 |
< |
Policy.setPolicy(savedPolicy); |
334 |
< |
e.execute(r); |
335 |
< |
try { |
336 |
< |
e.shutdown(); |
337 |
< |
} catch (SecurityException ok) { |
338 |
< |
} |
339 |
< |
try { |
340 |
< |
Thread.sleep(SHORT_DELAY_MS); |
341 |
< |
} finally { |
346 |
< |
joinPool(e); |
333 |
> |
boolean haveCCLPermissions() { |
334 |
> |
SecurityManager sm = System.getSecurityManager(); |
335 |
> |
if (sm != null) { |
336 |
> |
try { |
337 |
> |
sm.checkPermission(new RuntimePermission("setContextClassLoader")); |
338 |
> |
sm.checkPermission(new RuntimePermission("getClassLoader")); |
339 |
> |
} catch (AccessControlException e) { |
340 |
> |
return false; |
341 |
> |
} |
342 |
|
} |
343 |
+ |
return true; |
344 |
|
} |
345 |
|
|
346 |
|
void checkCCL() { |
364 |
|
* privilegedCallableUsingCurrentClassLoader throws ACE |
365 |
|
*/ |
366 |
|
public void testCreatePrivilegedCallableUsingCCLWithNoPrivs() { |
367 |
< |
Policy savedPolicy = null; |
368 |
< |
try { |
369 |
< |
savedPolicy = Policy.getPolicy(); |
370 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
371 |
< |
Policy.setPolicy(policy); |
372 |
< |
} catch (AccessControlException ok) { |
373 |
< |
return; |
374 |
< |
} |
375 |
< |
|
380 |
< |
// Check if program still has too many permissions to run test |
381 |
< |
try { |
382 |
< |
checkCCL(); |
383 |
< |
// too many privileges to test; so return |
384 |
< |
Policy.setPolicy(savedPolicy); |
385 |
< |
return; |
386 |
< |
} catch (AccessControlException ok) { |
387 |
< |
} |
367 |
> |
Runnable r = new CheckedRunnable() { |
368 |
> |
public void realRun() throws Exception { |
369 |
> |
if (System.getSecurityManager() == null) |
370 |
> |
return; |
371 |
> |
try { |
372 |
> |
Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
373 |
> |
shouldThrow(); |
374 |
> |
} catch (AccessControlException success) {} |
375 |
> |
}}; |
376 |
|
|
377 |
< |
try { |
390 |
< |
Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
391 |
< |
shouldThrow(); |
392 |
< |
} catch (AccessControlException success) { |
393 |
< |
} finally { |
394 |
< |
Policy.setPolicy(savedPolicy); |
395 |
< |
} |
377 |
> |
runWithoutPermissions(r); |
378 |
|
} |
379 |
|
|
380 |
|
/** |
382 |
|
* privilegedCallableUsingCurrentClassLoader does not throw ACE |
383 |
|
*/ |
384 |
|
public void testprivilegedCallableUsingCCLWithPrivs() throws Exception { |
385 |
< |
Policy savedPolicy = null; |
386 |
< |
try { |
387 |
< |
savedPolicy = Policy.getPolicy(); |
388 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
389 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
390 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
391 |
< |
Policy.setPolicy(policy); |
392 |
< |
} catch (AccessControlException ok) { |
393 |
< |
return; |
394 |
< |
} |
413 |
< |
|
414 |
< |
try { |
415 |
< |
Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
416 |
< |
task.call(); |
417 |
< |
} |
418 |
< |
finally { |
419 |
< |
Policy.setPolicy(savedPolicy); |
420 |
< |
} |
385 |
> |
Runnable r = new CheckedRunnable() { |
386 |
> |
public void realRun() throws Exception { |
387 |
> |
Executors.privilegedCallableUsingCurrentClassLoader |
388 |
> |
(new NoOpCallable()) |
389 |
> |
.call(); |
390 |
> |
}}; |
391 |
> |
|
392 |
> |
runWithPermissions(r, |
393 |
> |
new RuntimePermission("getClassLoader"), |
394 |
> |
new RuntimePermission("setContextClassLoader")); |
395 |
|
} |
396 |
|
|
397 |
|
/** |
398 |
|
* Without permissions, calling privilegedCallable throws ACE |
399 |
|
*/ |
400 |
|
public void testprivilegedCallableWithNoPrivs() throws Exception { |
401 |
< |
Callable task; |
402 |
< |
Policy savedPolicy = null; |
429 |
< |
AdjustablePolicy policy = null; |
430 |
< |
AccessControlContext noprivAcc = null; |
431 |
< |
try { |
432 |
< |
savedPolicy = Policy.getPolicy(); |
433 |
< |
policy = new AdjustablePolicy(); |
434 |
< |
Policy.setPolicy(policy); |
435 |
< |
noprivAcc = AccessController.getContext(); |
436 |
< |
task = Executors.privilegedCallable(new CheckCCL()); |
437 |
< |
Policy.setPolicy(savedPolicy); |
438 |
< |
} catch (AccessControlException ok) { |
439 |
< |
return; // program has too few permissions to set up test |
440 |
< |
} |
401 |
> |
// Avoid classloader-related SecurityExceptions in swingui.TestRunner |
402 |
> |
Executors.privilegedCallable(new CheckCCL()); |
403 |
|
|
404 |
< |
// Make sure that program doesn't have too many permissions |
405 |
< |
try { |
406 |
< |
AccessController.doPrivileged(new PrivilegedAction() { |
407 |
< |
public Object run() { |
408 |
< |
checkCCL(); |
409 |
< |
return null; |
410 |
< |
}}, noprivAcc); |
411 |
< |
// too many permssions; skip test |
412 |
< |
return; |
413 |
< |
} catch (AccessControlException ok) { |
452 |
< |
} |
404 |
> |
Runnable r = new CheckedRunnable() { |
405 |
> |
public void realRun() throws Exception { |
406 |
> |
if (System.getSecurityManager() == null) |
407 |
> |
return; |
408 |
> |
Callable task = Executors.privilegedCallable(new CheckCCL()); |
409 |
> |
try { |
410 |
> |
task.call(); |
411 |
> |
shouldThrow(); |
412 |
> |
} catch (AccessControlException success) {} |
413 |
> |
}}; |
414 |
|
|
415 |
< |
try { |
416 |
< |
task.call(); |
417 |
< |
shouldThrow(); |
418 |
< |
} catch (AccessControlException success) {} |
415 |
> |
runWithoutPermissions(r); |
416 |
> |
|
417 |
> |
// It seems rather difficult to test that the |
418 |
> |
// AccessControlContext of the privilegedCallable is used |
419 |
> |
// instead of its caller. Below is a failed attempt to do |
420 |
> |
// that, which does not work because the AccessController |
421 |
> |
// cannot capture the internal state of the current Policy. |
422 |
> |
// It would be much more work to differentiate based on, |
423 |
> |
// e.g. CodeSource. |
424 |
> |
|
425 |
> |
// final AccessControlContext[] noprivAcc = new AccessControlContext[1]; |
426 |
> |
// final Callable[] task = new Callable[1]; |
427 |
> |
|
428 |
> |
// runWithPermissions |
429 |
> |
// (new CheckedRunnable() { |
430 |
> |
// public void realRun() { |
431 |
> |
// if (System.getSecurityManager() == null) |
432 |
> |
// return; |
433 |
> |
// noprivAcc[0] = AccessController.getContext(); |
434 |
> |
// task[0] = Executors.privilegedCallable(new CheckCCL()); |
435 |
> |
// try { |
436 |
> |
// AccessController.doPrivileged(new PrivilegedAction<Void>() { |
437 |
> |
// public Void run() { |
438 |
> |
// checkCCL(); |
439 |
> |
// return null; |
440 |
> |
// }}, noprivAcc[0]); |
441 |
> |
// shouldThrow(); |
442 |
> |
// } catch (AccessControlException success) {} |
443 |
> |
// }}); |
444 |
> |
|
445 |
> |
// runWithPermissions |
446 |
> |
// (new CheckedRunnable() { |
447 |
> |
// public void realRun() throws Exception { |
448 |
> |
// if (System.getSecurityManager() == null) |
449 |
> |
// return; |
450 |
> |
// // Verify that we have an underprivileged ACC |
451 |
> |
// try { |
452 |
> |
// AccessController.doPrivileged(new PrivilegedAction<Void>() { |
453 |
> |
// public Void run() { |
454 |
> |
// checkCCL(); |
455 |
> |
// return null; |
456 |
> |
// }}, noprivAcc[0]); |
457 |
> |
// shouldThrow(); |
458 |
> |
// } catch (AccessControlException success) {} |
459 |
> |
|
460 |
> |
// try { |
461 |
> |
// task[0].call(); |
462 |
> |
// shouldThrow(); |
463 |
> |
// } catch (AccessControlException success) {} |
464 |
> |
// }}, |
465 |
> |
// new RuntimePermission("getClassLoader"), |
466 |
> |
// new RuntimePermission("setContextClassLoader")); |
467 |
|
} |
468 |
|
|
469 |
|
/** |
470 |
|
* With permissions, calling privilegedCallable succeeds |
471 |
|
*/ |
472 |
|
public void testprivilegedCallableWithPrivs() throws Exception { |
473 |
< |
Policy savedPolicy = null; |
474 |
< |
try { |
475 |
< |
savedPolicy = Policy.getPolicy(); |
476 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
477 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
478 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
479 |
< |
Policy.setPolicy(policy); |
480 |
< |
} catch (AccessControlException ok) { |
472 |
< |
return; |
473 |
< |
} |
474 |
< |
|
475 |
< |
Callable task = Executors.privilegedCallable(new CheckCCL()); |
476 |
< |
try { |
477 |
< |
task.call(); |
478 |
< |
} finally { |
479 |
< |
Policy.setPolicy(savedPolicy); |
480 |
< |
} |
473 |
> |
Runnable r = new CheckedRunnable() { |
474 |
> |
public void realRun() throws Exception { |
475 |
> |
Executors.privilegedCallable(new CheckCCL()).call(); |
476 |
> |
}}; |
477 |
> |
|
478 |
> |
runWithPermissions(r, |
479 |
> |
new RuntimePermission("getClassLoader"), |
480 |
> |
new RuntimePermission("setContextClassLoader")); |
481 |
|
} |
482 |
|
|
483 |
|
/** |