12 |
|
import java.util.concurrent.*; |
13 |
|
import java.math.BigInteger; |
14 |
|
import java.security.*; |
15 |
+ |
import sun.security.util.SecurityConstants; |
16 |
|
|
17 |
|
public class ExecutorsTest extends JSR166TestCase{ |
18 |
|
public static void main(String[] args) { |
70 |
|
e.execute(new NoOpRunnable()); |
71 |
|
e.execute(new NoOpRunnable()); |
72 |
|
e.execute(new NoOpRunnable()); |
73 |
< |
e.shutdown(); |
73 |
> |
joinPool(e); |
74 |
|
} |
75 |
|
|
76 |
|
/** |
81 |
|
e.execute(new NoOpRunnable()); |
82 |
|
e.execute(new NoOpRunnable()); |
83 |
|
e.execute(new NoOpRunnable()); |
84 |
< |
e.shutdown(); |
84 |
> |
joinPool(e); |
85 |
|
} |
86 |
|
|
87 |
|
/** |
105 |
|
e.execute(new NoOpRunnable()); |
106 |
|
e.execute(new NoOpRunnable()); |
107 |
|
e.execute(new NoOpRunnable()); |
108 |
< |
e.shutdown(); |
108 |
> |
joinPool(e); |
109 |
|
} |
110 |
|
|
111 |
|
/** |
116 |
|
e.execute(new NoOpRunnable()); |
117 |
|
e.execute(new NoOpRunnable()); |
118 |
|
e.execute(new NoOpRunnable()); |
119 |
< |
e.shutdown(); |
119 |
> |
joinPool(e); |
120 |
|
} |
121 |
|
|
122 |
|
/** |
153 |
|
e.execute(new NoOpRunnable()); |
154 |
|
e.execute(new NoOpRunnable()); |
155 |
|
e.execute(new NoOpRunnable()); |
156 |
< |
e.shutdown(); |
156 |
> |
joinPool(e); |
157 |
|
} |
158 |
|
|
159 |
|
/** |
164 |
|
e.execute(new NoOpRunnable()); |
165 |
|
e.execute(new NoOpRunnable()); |
166 |
|
e.execute(new NoOpRunnable()); |
167 |
< |
e.shutdown(); |
167 |
> |
joinPool(e); |
168 |
|
} |
169 |
|
|
170 |
|
/** |
200 |
|
e.execute(new NoOpRunnable()); |
201 |
|
e.execute(new NoOpRunnable()); |
202 |
|
e.execute(new NoOpRunnable()); |
203 |
< |
e.shutdown(); |
203 |
> |
joinPool(e); |
204 |
|
} |
205 |
|
|
206 |
|
/** |
238 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
239 |
|
assertTrue(callable.done); |
240 |
|
assertEquals(Boolean.TRUE, f.get()); |
240 |
– |
p1.shutdown(); |
241 |
|
joinPool(p1); |
242 |
|
} catch(RejectedExecutionException e){} |
243 |
|
catch(Exception e){ |
258 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
259 |
|
assertTrue(callable.done); |
260 |
|
assertEquals(Boolean.TRUE, f.get()); |
261 |
– |
p1.shutdown(); |
261 |
|
joinPool(p1); |
262 |
|
} catch(RejectedExecutionException e){} |
263 |
|
catch(Exception e){ |
278 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
279 |
|
assertTrue(callable.done); |
280 |
|
assertEquals(Boolean.TRUE, f.get()); |
282 |
– |
p1.shutdown(); |
281 |
|
joinPool(p1); |
282 |
|
} catch(RejectedExecutionException e){} |
283 |
|
catch(Exception e){ |
335 |
|
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
336 |
|
Runnable r = new Runnable() { |
337 |
|
public void run() { |
338 |
< |
Thread current = Thread.currentThread(); |
339 |
< |
threadAssertTrue(!current.isDaemon()); |
340 |
< |
threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY); |
341 |
< |
ThreadGroup g = current.getThreadGroup(); |
342 |
< |
SecurityManager s = System.getSecurityManager(); |
343 |
< |
if (s != null) |
344 |
< |
threadAssertTrue(g == s.getThreadGroup()); |
345 |
< |
else |
346 |
< |
threadAssertTrue(g == egroup); |
347 |
< |
String name = current.getName(); |
348 |
< |
threadAssertTrue(name.endsWith("thread-1")); |
338 |
> |
try { |
339 |
> |
Thread current = Thread.currentThread(); |
340 |
> |
threadAssertTrue(!current.isDaemon()); |
341 |
> |
threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
342 |
> |
ThreadGroup g = current.getThreadGroup(); |
343 |
> |
SecurityManager s = System.getSecurityManager(); |
344 |
> |
if (s != null) |
345 |
> |
threadAssertTrue(g == s.getThreadGroup()); |
346 |
> |
else |
347 |
> |
threadAssertTrue(g == egroup); |
348 |
> |
String name = current.getName(); |
349 |
> |
threadAssertTrue(name.endsWith("thread-1")); |
350 |
> |
} catch (SecurityException ok) { |
351 |
> |
// Also pass if not allowed to change setting |
352 |
> |
} |
353 |
|
} |
354 |
|
}; |
355 |
|
ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()); |
356 |
|
|
357 |
|
e.execute(r); |
358 |
< |
e.shutdown(); |
358 |
> |
try { |
359 |
> |
e.shutdown(); |
360 |
> |
} catch(SecurityException ok) { |
361 |
> |
} |
362 |
> |
|
363 |
|
try { |
364 |
|
Thread.sleep(SHORT_DELAY_MS); |
365 |
|
} catch (Exception eX) { |
375 |
|
* access control context and context class loader |
376 |
|
*/ |
377 |
|
public void testPrivilegedThreadFactory() { |
378 |
< |
Policy savedPolicy = Policy.getPolicy(); |
379 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
380 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
381 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
382 |
< |
Policy.setPolicy(policy); |
378 |
> |
Policy savedPolicy = null; |
379 |
> |
try { |
380 |
> |
savedPolicy = Policy.getPolicy(); |
381 |
> |
AdjustablePolicy policy = new AdjustablePolicy(); |
382 |
> |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
383 |
> |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
384 |
> |
Policy.setPolicy(policy); |
385 |
> |
} catch (AccessControlException ok) { |
386 |
> |
return; |
387 |
> |
} |
388 |
|
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
389 |
|
final ClassLoader thisccl = Thread.currentThread().getContextClassLoader(); |
390 |
|
final AccessControlContext thisacc = AccessController.getContext(); |
391 |
|
Runnable r = new Runnable() { |
392 |
|
public void run() { |
393 |
< |
Thread current = Thread.currentThread(); |
394 |
< |
threadAssertTrue(!current.isDaemon()); |
395 |
< |
threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY); |
396 |
< |
ThreadGroup g = current.getThreadGroup(); |
397 |
< |
SecurityManager s = System.getSecurityManager(); |
398 |
< |
if (s != null) |
399 |
< |
threadAssertTrue(g == s.getThreadGroup()); |
400 |
< |
else |
401 |
< |
threadAssertTrue(g == egroup); |
402 |
< |
String name = current.getName(); |
403 |
< |
threadAssertTrue(name.endsWith("thread-1")); |
404 |
< |
threadAssertTrue(thisccl == current.getContextClassLoader()); |
405 |
< |
threadAssertTrue(thisacc.equals(AccessController.getContext())); |
393 |
> |
try { |
394 |
> |
Thread current = Thread.currentThread(); |
395 |
> |
threadAssertTrue(!current.isDaemon()); |
396 |
> |
threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
397 |
> |
ThreadGroup g = current.getThreadGroup(); |
398 |
> |
SecurityManager s = System.getSecurityManager(); |
399 |
> |
if (s != null) |
400 |
> |
threadAssertTrue(g == s.getThreadGroup()); |
401 |
> |
else |
402 |
> |
threadAssertTrue(g == egroup); |
403 |
> |
String name = current.getName(); |
404 |
> |
threadAssertTrue(name.endsWith("thread-1")); |
405 |
> |
threadAssertTrue(thisccl == current.getContextClassLoader()); |
406 |
> |
threadAssertTrue(thisacc.equals(AccessController.getContext())); |
407 |
> |
} catch(SecurityException ok) { |
408 |
> |
// Also pass if not allowed to change settings |
409 |
> |
} |
410 |
|
} |
411 |
|
}; |
412 |
|
ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); |
413 |
|
|
414 |
|
Policy.setPolicy(savedPolicy); |
415 |
|
e.execute(r); |
416 |
< |
e.shutdown(); |
416 |
> |
try { |
417 |
> |
e.shutdown(); |
418 |
> |
} catch(SecurityException ok) { |
419 |
> |
} |
420 |
|
try { |
421 |
|
Thread.sleep(SHORT_DELAY_MS); |
422 |
|
} catch (Exception ex) { |
427 |
|
|
428 |
|
} |
429 |
|
|
430 |
< |
static class CheckCCL implements Callable<Object> { |
430 |
> |
void checkCCL() { |
431 |
> |
SecurityManager sm = System.getSecurityManager(); |
432 |
> |
if (sm != null) { |
433 |
> |
sm.checkPermission(new RuntimePermission("setContextClassLoader")); |
434 |
> |
sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION); |
435 |
> |
} |
436 |
> |
} |
437 |
> |
|
438 |
> |
class CheckCCL implements Callable<Object> { |
439 |
|
public Object call() { |
440 |
< |
AccessControlContext acc = AccessController.getContext(); |
415 |
< |
acc.checkPermission(new RuntimePermission("getContextClassLoader")); |
440 |
> |
checkCCL(); |
441 |
|
return null; |
442 |
|
} |
443 |
|
} |
448 |
|
* privilegedCallableUsingCurrentClassLoader throws ACE |
449 |
|
*/ |
450 |
|
public void testCreatePrivilegedCallableUsingCCLWithNoPrivs() { |
451 |
< |
Policy savedPolicy = Policy.getPolicy(); |
452 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
453 |
< |
Policy.setPolicy(policy); |
451 |
> |
Policy savedPolicy = null; |
452 |
> |
try { |
453 |
> |
savedPolicy = Policy.getPolicy(); |
454 |
> |
AdjustablePolicy policy = new AdjustablePolicy(); |
455 |
> |
Policy.setPolicy(policy); |
456 |
> |
} catch (AccessControlException ok) { |
457 |
> |
return; |
458 |
> |
} |
459 |
> |
|
460 |
> |
// Check if program still has too many permissions to run test |
461 |
> |
try { |
462 |
> |
checkCCL(); |
463 |
> |
// too many privileges to test; so return |
464 |
> |
Policy.setPolicy(savedPolicy); |
465 |
> |
return; |
466 |
> |
} catch(AccessControlException ok) { |
467 |
> |
} |
468 |
> |
|
469 |
|
try { |
470 |
|
Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
471 |
|
shouldThrow(); |
479 |
|
} |
480 |
|
|
481 |
|
/** |
482 |
< |
* Without class loader permissions, calling |
483 |
< |
* privilegedCallableUsingCurrentClassLoader throws ACE |
482 |
> |
* With class loader permissions, calling |
483 |
> |
* privilegedCallableUsingCurrentClassLoader does not throw ACE |
484 |
|
*/ |
485 |
|
public void testprivilegedCallableUsingCCLWithPrivs() { |
486 |
< |
Policy savedPolicy = Policy.getPolicy(); |
487 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
488 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
489 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
490 |
< |
Policy.setPolicy(policy); |
486 |
> |
Policy savedPolicy = null; |
487 |
> |
try { |
488 |
> |
savedPolicy = Policy.getPolicy(); |
489 |
> |
AdjustablePolicy policy = new AdjustablePolicy(); |
490 |
> |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
491 |
> |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
492 |
> |
Policy.setPolicy(policy); |
493 |
> |
} catch (AccessControlException ok) { |
494 |
> |
return; |
495 |
> |
} |
496 |
> |
|
497 |
|
try { |
498 |
|
Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
499 |
|
task.call(); |
509 |
|
* Without permissions, calling privilegedCallable throws ACE |
510 |
|
*/ |
511 |
|
public void testprivilegedCallableWithNoPrivs() { |
512 |
< |
Policy savedPolicy = Policy.getPolicy(); |
513 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
514 |
< |
Policy.setPolicy(policy); |
515 |
< |
Callable task = Executors.privilegedCallable(new CheckCCL()); |
516 |
< |
Policy.setPolicy(savedPolicy); |
512 |
> |
Callable task; |
513 |
> |
Policy savedPolicy = null; |
514 |
> |
AdjustablePolicy policy = null; |
515 |
> |
AccessControlContext noprivAcc = null; |
516 |
> |
try { |
517 |
> |
savedPolicy = Policy.getPolicy(); |
518 |
> |
policy = new AdjustablePolicy(); |
519 |
> |
Policy.setPolicy(policy); |
520 |
> |
noprivAcc = AccessController.getContext(); |
521 |
> |
task = Executors.privilegedCallable(new CheckCCL()); |
522 |
> |
Policy.setPolicy(savedPolicy); |
523 |
> |
} catch (AccessControlException ok) { |
524 |
> |
return; // program has too few permissions to set up test |
525 |
> |
} |
526 |
> |
|
527 |
> |
// Make sure that program doesn't have too many permissions |
528 |
> |
try { |
529 |
> |
AccessController.doPrivileged(new PrivilegedAction() { |
530 |
> |
public Object run() { |
531 |
> |
checkCCL(); |
532 |
> |
return null; |
533 |
> |
}}, noprivAcc); |
534 |
> |
// too many permssions; skip test |
535 |
> |
return; |
536 |
> |
} catch(AccessControlException ok) { |
537 |
> |
} |
538 |
> |
|
539 |
|
try { |
540 |
|
task.call(); |
541 |
|
shouldThrow(); |
542 |
|
} catch(AccessControlException success) { |
543 |
|
} catch(Exception ex) { |
544 |
|
unexpectedException(); |
545 |
< |
} finally { |
478 |
< |
} |
545 |
> |
} |
546 |
|
} |
547 |
|
|
548 |
|
/** |
549 |
|
* With permissions, calling privilegedCallable succeeds |
550 |
|
*/ |
551 |
|
public void testprivilegedCallableWithPrivs() { |
552 |
< |
Policy savedPolicy = Policy.getPolicy(); |
553 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
554 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
555 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
556 |
< |
Policy.setPolicy(policy); |
552 |
> |
Policy savedPolicy = null; |
553 |
> |
try { |
554 |
> |
savedPolicy = Policy.getPolicy(); |
555 |
> |
AdjustablePolicy policy = new AdjustablePolicy(); |
556 |
> |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
557 |
> |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
558 |
> |
Policy.setPolicy(policy); |
559 |
> |
} catch (AccessControlException ok) { |
560 |
> |
return; |
561 |
> |
} |
562 |
> |
|
563 |
|
Callable task = Executors.privilegedCallable(new CheckCCL()); |
564 |
|
try { |
565 |
|
task.call(); |