ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ExecutorsTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ExecutorsTest.java (file contents):
Revision 1.28 by jsr166, Wed Dec 2 19:17:01 2009 UTC vs.
Revision 1.29 by jsr166, Tue Jan 5 02:08:37 2010 UTC

# Line 298 | Line 298 | public class ExecutorsTest extends JSR16
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());
327 <
328 <        Policy.setPolicy(savedPolicy);
329 <        e.execute(r);
330 <        try {
331 <            e.shutdown();
332 <        } catch (SecurityException ok) {
333 <        }
334 <        try {
335 <            Thread.sleep(SHORT_DELAY_MS);
336 <        } finally {
337 <            joinPool(e);
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 >    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() {
# Line 368 | Line 366 | public class ExecutorsTest extends JSR16
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 <        }
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 <        // 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 <        }
388 <
389 <        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      /**
# Line 400 | Line 384 | public class ExecutorsTest extends JSR16
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());
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) {
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      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines