22 |
|
return new TestSuite(ExecutorsTest.class); |
23 |
|
} |
24 |
|
|
25 |
– |
static class TimedCallable<T> implements Callable<T> { |
26 |
– |
private final ExecutorService exec; |
27 |
– |
private final Callable<T> func; |
28 |
– |
private final long msecs; |
29 |
– |
|
30 |
– |
TimedCallable(ExecutorService exec, Callable<T> func, long msecs) { |
31 |
– |
this.exec = exec; |
32 |
– |
this.func = func; |
33 |
– |
this.msecs = msecs; |
34 |
– |
} |
35 |
– |
|
36 |
– |
public T call() throws Exception { |
37 |
– |
Future<T> ftask = exec.submit(func); |
38 |
– |
try { |
39 |
– |
return ftask.get(msecs, MILLISECONDS); |
40 |
– |
} finally { |
41 |
– |
ftask.cancel(true); |
42 |
– |
} |
43 |
– |
} |
44 |
– |
} |
45 |
– |
|
46 |
– |
|
47 |
– |
private static class Fib implements Callable<BigInteger> { |
48 |
– |
private final BigInteger n; |
49 |
– |
Fib(long n) { |
50 |
– |
if (n < 0) throw new IllegalArgumentException("need non-negative arg, but got " + n); |
51 |
– |
this.n = BigInteger.valueOf(n); |
52 |
– |
} |
53 |
– |
public BigInteger call() { |
54 |
– |
BigInteger f1 = BigInteger.ONE; |
55 |
– |
BigInteger f2 = f1; |
56 |
– |
for (BigInteger i = BigInteger.ZERO; i.compareTo(n) < 0; i = i.add(BigInteger.ONE)) { |
57 |
– |
BigInteger t = f1.add(f2); |
58 |
– |
f1 = f2; |
59 |
– |
f2 = t; |
60 |
– |
} |
61 |
– |
return f1; |
62 |
– |
} |
63 |
– |
}; |
64 |
– |
|
25 |
|
/** |
26 |
|
* A newCachedThreadPool can execute runnables |
27 |
|
*/ |
220 |
|
} |
221 |
|
|
222 |
|
/** |
223 |
< |
* timeouts from execute 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 |
< |
int N = 10000; |
227 |
< |
ExecutorService executor = Executors.newSingleThreadExecutor(); |
228 |
< |
List<Callable<BigInteger>> tasks = new ArrayList<Callable<BigInteger>>(N); |
229 |
< |
try { |
230 |
< |
long startTime = System.currentTimeMillis(); |
231 |
< |
|
232 |
< |
long i = 0; |
233 |
< |
while (tasks.size() < N) { |
234 |
< |
tasks.add(new TimedCallable<BigInteger>(executor, new Fib(i), 1)); |
235 |
< |
i += 10; |
236 |
< |
} |
237 |
< |
|
238 |
< |
int iters = 0; |
239 |
< |
BigInteger sum = BigInteger.ZERO; |
280 |
< |
for (Iterator<Callable<BigInteger>> it = tasks.iterator(); it.hasNext();) { |
226 |
> |
final Runnable sleeper = |
227 |
> |
new RunnableShouldThrow(InterruptedException.class) { |
228 |
> |
public void realRun() throws InterruptedException { |
229 |
> |
Thread.sleep(LONG_DELAY_MS); |
230 |
> |
}}; |
231 |
> |
for (ExecutorService executor : |
232 |
> |
new ExecutorService[] { |
233 |
> |
Executors.newSingleThreadExecutor(), |
234 |
> |
Executors.newCachedThreadPool(), |
235 |
> |
Executors.newFixedThreadPool(2), |
236 |
> |
Executors.newScheduledThreadPool(2), |
237 |
> |
}) { |
238 |
> |
try { |
239 |
> |
Future future = executor.submit(sleeper); |
240 |
|
try { |
241 |
< |
++iters; |
242 |
< |
sum = sum.add(it.next().call()); |
243 |
< |
} |
244 |
< |
catch (TimeoutException success) { |
245 |
< |
assertTrue(iters > 0); |
287 |
< |
return; |
241 |
> |
future.get(SHORT_DELAY_MS, MILLISECONDS); |
242 |
> |
shouldThrow(); |
243 |
> |
} catch (TimeoutException success) { |
244 |
> |
} finally { |
245 |
> |
future.cancel(true); |
246 |
|
} |
247 |
|
} |
248 |
< |
// if by chance we didn't ever time out, total time must be small |
249 |
< |
long elapsed = System.currentTimeMillis() - startTime; |
250 |
< |
assertTrue(elapsed < N); |
293 |
< |
} |
294 |
< |
finally { |
295 |
< |
joinPool(executor); |
248 |
> |
finally { |
249 |
> |
joinPool(executor); |
250 |
> |
} |
251 |
|
} |
252 |
|
} |
253 |
|
|
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); |
353 |
< |
} catch (AccessControlException ok) { |
354 |
< |
return; |
355 |
< |
} |
356 |
< |
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
357 |
< |
final ClassLoader thisccl = Thread.currentThread().getContextClassLoader(); |
358 |
< |
final AccessControlContext thisacc = AccessController.getContext(); |
359 |
< |
Runnable r = new Runnable() { |
360 |
< |
public void run() { |
361 |
< |
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 { |
391 |
< |
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 |
< |
|
425 |
< |
// Check if program still has too many permissions to run test |
426 |
< |
try { |
427 |
< |
checkCCL(); |
428 |
< |
// too many privileges to test; so return |
429 |
< |
Policy.setPolicy(savedPolicy); |
430 |
< |
return; |
431 |
< |
} catch (AccessControlException ok) { |
432 |
< |
} |
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 { |
435 |
< |
Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
436 |
< |
shouldThrow(); |
437 |
< |
} catch (AccessControlException success) { |
438 |
< |
} finally { |
439 |
< |
Policy.setPolicy(savedPolicy); |
440 |
< |
} |
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 |
< |
} |
458 |
< |
|
459 |
< |
try { |
460 |
< |
Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
461 |
< |
task.call(); |
462 |
< |
} |
463 |
< |
finally { |
464 |
< |
Policy.setPolicy(savedPolicy); |
465 |
< |
} |
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()); |
413 |
< |
Policy.setPolicy(savedPolicy); |
414 |
< |
} catch (AccessControlException ok) { |
415 |
< |
return; // program has too few permissions to set up test |
416 |
< |
} |
417 |
< |
|
418 |
< |
// Make sure that program doesn't have too many permissions |
419 |
< |
try { |
420 |
< |
AccessController.doPrivileged(new PrivilegedAction() { |
421 |
< |
public Object run() { |
422 |
< |
checkCCL(); |
423 |
< |
return null; |
424 |
< |
}}, noprivAcc); |
425 |
< |
// too many permssions; skip test |
426 |
< |
return; |
427 |
< |
} catch (AccessControlException ok) { |
428 |
< |
} |
429 |
< |
|
430 |
< |
try { |
431 |
< |
task.call(); |
432 |
< |
shouldThrow(); |
433 |
< |
} catch (AccessControlException success) {} |
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 |
> |
runWithoutPermissions(r); |
415 |
> |
|
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) { |
517 |
< |
return; |
518 |
< |
} |
519 |
< |
|
520 |
< |
Callable task = Executors.privilegedCallable(new CheckCCL()); |
521 |
< |
try { |
522 |
< |
task.call(); |
523 |
< |
} finally { |
524 |
< |
Policy.setPolicy(savedPolicy); |
525 |
< |
} |
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 |
|
/** |