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); |
298 |
|
* access control context and context class loader |
299 |
|
*/ |
300 |
|
public void testPrivilegedThreadFactory() throws Exception { |
301 |
< |
Policy savedPolicy = null; |
302 |
< |
try { |
303 |
< |
savedPolicy = Policy.getPolicy(); |
304 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
305 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
306 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
307 |
< |
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 { |
301 |
> |
Runnable r = new CheckedRunnable() { |
302 |
> |
public void realRun() throws Exception { |
303 |
> |
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
304 |
> |
final ClassLoader thisccl = Thread.currentThread().getContextClassLoader(); |
305 |
> |
final AccessControlContext thisacc = AccessController.getContext(); |
306 |
> |
Runnable r = new CheckedRunnable() { |
307 |
> |
public void realRun() { |
308 |
|
Thread current = Thread.currentThread(); |
309 |
< |
threadAssertTrue(!current.isDaemon()); |
310 |
< |
threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
309 |
> |
assertTrue(!current.isDaemon()); |
310 |
> |
assertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
311 |
|
ThreadGroup g = current.getThreadGroup(); |
312 |
|
SecurityManager s = System.getSecurityManager(); |
313 |
|
if (s != null) |
314 |
< |
threadAssertTrue(g == s.getThreadGroup()); |
314 |
> |
assertTrue(g == s.getThreadGroup()); |
315 |
|
else |
316 |
< |
threadAssertTrue(g == egroup); |
316 |
> |
assertTrue(g == egroup); |
317 |
|
String name = current.getName(); |
318 |
< |
threadAssertTrue(name.endsWith("thread-1")); |
319 |
< |
threadAssertTrue(thisccl == current.getContextClassLoader()); |
320 |
< |
threadAssertTrue(thisacc.equals(AccessController.getContext())); |
321 |
< |
} catch (SecurityException ok) { |
322 |
< |
// Also pass if not allowed to change settings |
323 |
< |
} |
324 |
< |
} |
325 |
< |
}; |
326 |
< |
ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); |
318 |
> |
assertTrue(name.endsWith("thread-1")); |
319 |
> |
assertTrue(thisccl == current.getContextClassLoader()); |
320 |
> |
assertTrue(thisacc.equals(AccessController.getContext())); |
321 |
> |
}}; |
322 |
> |
ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); |
323 |
> |
e.execute(r); |
324 |
> |
e.shutdown(); |
325 |
> |
Thread.sleep(SHORT_DELAY_MS); |
326 |
> |
joinPool(e); |
327 |
> |
}}; |
328 |
> |
|
329 |
> |
runWithPermissions(r, |
330 |
> |
new RuntimePermission("getClassLoader"), |
331 |
> |
new RuntimePermission("setContextClassLoader"), |
332 |
> |
new RuntimePermission("modifyThread")); |
333 |
> |
} |
334 |
|
|
335 |
< |
Policy.setPolicy(savedPolicy); |
336 |
< |
e.execute(r); |
337 |
< |
try { |
338 |
< |
e.shutdown(); |
339 |
< |
} catch (SecurityException ok) { |
340 |
< |
} |
341 |
< |
try { |
342 |
< |
Thread.sleep(SHORT_DELAY_MS); |
343 |
< |
} finally { |
346 |
< |
joinPool(e); |
335 |
> |
boolean haveCCLPermissions() { |
336 |
> |
SecurityManager sm = System.getSecurityManager(); |
337 |
> |
if (sm != null) { |
338 |
> |
try { |
339 |
> |
sm.checkPermission(new RuntimePermission("setContextClassLoader")); |
340 |
> |
sm.checkPermission(new RuntimePermission("getClassLoader")); |
341 |
> |
} catch (AccessControlException e) { |
342 |
> |
return false; |
343 |
> |
} |
344 |
|
} |
345 |
+ |
return true; |
346 |
|
} |
347 |
|
|
348 |
|
void checkCCL() { |
366 |
|
* privilegedCallableUsingCurrentClassLoader throws ACE |
367 |
|
*/ |
368 |
|
public void testCreatePrivilegedCallableUsingCCLWithNoPrivs() { |
369 |
< |
Policy savedPolicy = null; |
370 |
< |
try { |
371 |
< |
savedPolicy = Policy.getPolicy(); |
372 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
373 |
< |
Policy.setPolicy(policy); |
374 |
< |
} catch (AccessControlException ok) { |
375 |
< |
return; |
376 |
< |
} |
377 |
< |
|
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 |
< |
} |
369 |
> |
Runnable r = new CheckedRunnable() { |
370 |
> |
public void realRun() throws Exception { |
371 |
> |
if (System.getSecurityManager() == null) |
372 |
> |
return; |
373 |
> |
try { |
374 |
> |
Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
375 |
> |
shouldThrow(); |
376 |
> |
} catch (AccessControlException success) {} |
377 |
> |
}}; |
378 |
|
|
379 |
< |
try { |
390 |
< |
Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
391 |
< |
shouldThrow(); |
392 |
< |
} catch (AccessControlException success) { |
393 |
< |
} finally { |
394 |
< |
Policy.setPolicy(savedPolicy); |
395 |
< |
} |
379 |
> |
runWithoutPermissions(r); |
380 |
|
} |
381 |
|
|
382 |
|
/** |
384 |
|
* privilegedCallableUsingCurrentClassLoader does not throw ACE |
385 |
|
*/ |
386 |
|
public void testprivilegedCallableUsingCCLWithPrivs() throws Exception { |
387 |
< |
Policy savedPolicy = null; |
388 |
< |
try { |
389 |
< |
savedPolicy = Policy.getPolicy(); |
390 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
391 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
392 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
393 |
< |
Policy.setPolicy(policy); |
394 |
< |
} catch (AccessControlException ok) { |
395 |
< |
return; |
396 |
< |
} |
413 |
< |
|
414 |
< |
try { |
415 |
< |
Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
416 |
< |
task.call(); |
417 |
< |
} |
418 |
< |
finally { |
419 |
< |
Policy.setPolicy(savedPolicy); |
420 |
< |
} |
387 |
> |
Runnable r = new CheckedRunnable() { |
388 |
> |
public void realRun() throws Exception { |
389 |
> |
Executors.privilegedCallableUsingCurrentClassLoader |
390 |
> |
(new NoOpCallable()) |
391 |
> |
.call(); |
392 |
> |
}}; |
393 |
> |
|
394 |
> |
runWithPermissions(r, |
395 |
> |
new RuntimePermission("getClassLoader"), |
396 |
> |
new RuntimePermission("setContextClassLoader")); |
397 |
|
} |
398 |
|
|
399 |
|
/** |
400 |
|
* Without permissions, calling privilegedCallable throws ACE |
401 |
|
*/ |
402 |
|
public void testprivilegedCallableWithNoPrivs() throws Exception { |
403 |
< |
Callable task; |
404 |
< |
Policy savedPolicy = null; |
405 |
< |
AdjustablePolicy policy = null; |
406 |
< |
AccessControlContext noprivAcc = null; |
407 |
< |
try { |
408 |
< |
savedPolicy = Policy.getPolicy(); |
409 |
< |
policy = new AdjustablePolicy(); |
410 |
< |
Policy.setPolicy(policy); |
411 |
< |
noprivAcc = AccessController.getContext(); |
412 |
< |
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 |
< |
} |
403 |
> |
Runnable r = new CheckedRunnable() { |
404 |
> |
public void realRun() throws Exception { |
405 |
> |
if (System.getSecurityManager() == null) |
406 |
> |
return; |
407 |
> |
Callable task = Executors.privilegedCallable(new CheckCCL()); |
408 |
> |
try { |
409 |
> |
task.call(); |
410 |
> |
shouldThrow(); |
411 |
> |
} catch (AccessControlException success) {} |
412 |
> |
}}; |
413 |
|
|
414 |
< |
// Make sure that program doesn't have too many permissions |
443 |
< |
try { |
444 |
< |
AccessController.doPrivileged(new PrivilegedAction() { |
445 |
< |
public Object run() { |
446 |
< |
checkCCL(); |
447 |
< |
return null; |
448 |
< |
}}, noprivAcc); |
449 |
< |
// too many permssions; skip test |
450 |
< |
return; |
451 |
< |
} catch (AccessControlException ok) { |
452 |
< |
} |
414 |
> |
runWithoutPermissions(r); |
415 |
|
|
416 |
< |
try { |
417 |
< |
task.call(); |
418 |
< |
shouldThrow(); |
419 |
< |
} catch (AccessControlException success) {} |
416 |
> |
// It seems rather difficult to test that the |
417 |
> |
// AccessControlContext of the privilegedCallable is used |
418 |
> |
// instead of its caller. Below is a failed attempt to do |
419 |
> |
// that, which does not work because the AccessController |
420 |
> |
// cannot capture the internal state of the current Policy. |
421 |
> |
// It would be much more work to differentiate based on, |
422 |
> |
// e.g. CodeSource. |
423 |
> |
|
424 |
> |
// final AccessControlContext[] noprivAcc = new AccessControlContext[1]; |
425 |
> |
// final Callable[] task = new Callable[1]; |
426 |
> |
|
427 |
> |
// runWithPermissions |
428 |
> |
// (new CheckedRunnable() { |
429 |
> |
// public void realRun() { |
430 |
> |
// if (System.getSecurityManager() == null) |
431 |
> |
// return; |
432 |
> |
// noprivAcc[0] = AccessController.getContext(); |
433 |
> |
// task[0] = Executors.privilegedCallable(new CheckCCL()); |
434 |
> |
// try { |
435 |
> |
// AccessController.doPrivileged(new PrivilegedAction<Void>() { |
436 |
> |
// public Void run() { |
437 |
> |
// checkCCL(); |
438 |
> |
// return null; |
439 |
> |
// }}, noprivAcc[0]); |
440 |
> |
// shouldThrow(); |
441 |
> |
// } catch (AccessControlException success) {} |
442 |
> |
// }}); |
443 |
> |
|
444 |
> |
// runWithPermissions |
445 |
> |
// (new CheckedRunnable() { |
446 |
> |
// public void realRun() throws Exception { |
447 |
> |
// if (System.getSecurityManager() == null) |
448 |
> |
// return; |
449 |
> |
// // Verify that we have an underprivileged ACC |
450 |
> |
// try { |
451 |
> |
// AccessController.doPrivileged(new PrivilegedAction<Void>() { |
452 |
> |
// public Void run() { |
453 |
> |
// checkCCL(); |
454 |
> |
// return null; |
455 |
> |
// }}, noprivAcc[0]); |
456 |
> |
// shouldThrow(); |
457 |
> |
// } catch (AccessControlException success) {} |
458 |
> |
|
459 |
> |
// try { |
460 |
> |
// task[0].call(); |
461 |
> |
// shouldThrow(); |
462 |
> |
// } catch (AccessControlException success) {} |
463 |
> |
// }}, |
464 |
> |
// new RuntimePermission("getClassLoader"), |
465 |
> |
// new RuntimePermission("setContextClassLoader")); |
466 |
|
} |
467 |
|
|
468 |
|
/** |
469 |
|
* With permissions, calling privilegedCallable succeeds |
470 |
|
*/ |
471 |
|
public void testprivilegedCallableWithPrivs() throws Exception { |
472 |
< |
Policy savedPolicy = null; |
473 |
< |
try { |
474 |
< |
savedPolicy = Policy.getPolicy(); |
475 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
476 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
477 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
478 |
< |
Policy.setPolicy(policy); |
479 |
< |
} 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 |
< |
} |
472 |
> |
Runnable r = new CheckedRunnable() { |
473 |
> |
public void realRun() throws Exception { |
474 |
> |
Executors.privilegedCallable(new CheckCCL()).call(); |
475 |
> |
}}; |
476 |
> |
|
477 |
> |
runWithPermissions(r, |
478 |
> |
new RuntimePermission("getClassLoader"), |
479 |
> |
new RuntimePermission("setContextClassLoader")); |
480 |
|
} |
481 |
|
|
482 |
|
/** |