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

Comparing jsr166/src/test/tck/JSR166TestCase.java (file contents):
Revision 1.48 by jsr166, Tue Dec 1 22:51:44 2009 UTC vs.
Revision 1.49 by jsr166, Tue Jan 5 02:08:37 2010 UTC

# Line 87 | Line 87 | import java.security.*;
87   * </ul>
88   */
89   public class JSR166TestCase extends TestCase {
90 +    private static final boolean useSecurityManager =
91 +        Boolean.getBoolean("jsr166.useSecurityManager");
92 +
93      /**
94       * Runs all JSR166 unit tests using junit.textui.TestRunner
95       */
96      public static void main(String[] args) {
97 +        if (useSecurityManager) {
98 +            System.err.println("Setting a permissive security manager");
99 +            Policy.setPolicy(permissivePolicy());
100 +            System.setSecurityManager(new SecurityManager());
101 +        }
102          int iters = 1;
103          if (args.length > 0)
104              iters = Integer.parseInt(args[0]);
# Line 390 | Line 398 | public class JSR166TestCase extends Test
398  
399  
400      /**
401 +     * Runs Runnable r with a security policy that permits precisely
402 +     * the specified permissions.  If there is no current security
403 +     * manager, the runnable is run twice, both with and without a
404 +     * security manager.  We require that any security manager permit
405 +     * getPolicy/setPolicy.
406 +     */
407 +    public void runWithPermissions(Runnable r, Permission... permissions) {
408 +        SecurityManager sm = System.getSecurityManager();
409 +        if (sm == null) {
410 +            r.run();
411 +            Policy savedPolicy = Policy.getPolicy();
412 +            try {
413 +                Policy.setPolicy(permissivePolicy());
414 +                System.setSecurityManager(new SecurityManager());
415 +                runWithPermissions(r, permissions);
416 +            } finally {
417 +                System.setSecurityManager(null);
418 +                Policy.setPolicy(savedPolicy);
419 +            }
420 +        } else {
421 +            Policy savedPolicy = Policy.getPolicy();
422 +            AdjustablePolicy policy = new AdjustablePolicy(permissions);
423 +            Policy.setPolicy(policy);
424 +
425 +            try {
426 +                r.run();
427 +            } finally {
428 +                policy.addPermission(new SecurityPermission("setPolicy"));
429 +                Policy.setPolicy(savedPolicy);
430 +            }
431 +        }
432 +    }
433 +
434 +    /**
435 +     * Runs a runnable without any permissions.
436 +     */
437 +    public void runWithoutPermissions(Runnable r) {
438 +        runWithPermissions(r);
439 +    }
440 +
441 +    /**
442       * A security policy where new permissions can be dynamically added
443       * or all cleared.
444       */
445      public static class AdjustablePolicy extends java.security.Policy {
446          Permissions perms = new Permissions();
447 <        AdjustablePolicy() { }
447 >        AdjustablePolicy(Permission... permissions) {
448 >            for (Permission permission : permissions)
449 >                perms.add(permission);
450 >        }
451          void addPermission(Permission perm) { perms.add(perm); }
452          void clearPermissions() { perms = new Permissions(); }
453          public PermissionCollection getPermissions(CodeSource cs) {
# Line 411 | Line 463 | public class JSR166TestCase extends Test
463      }
464  
465      /**
466 +     * Returns a policy containing all the permissions we ever need.
467 +     */
468 +    public static Policy permissivePolicy() {
469 +        return new AdjustablePolicy
470 +            // Permissions j.u.c. needs directly
471 +            (new RuntimePermission("modifyThread"),
472 +             new RuntimePermission("getClassLoader"),
473 +             new RuntimePermission("setContextClassLoader"),
474 +             // Permissions needed to change permissions!
475 +             new SecurityPermission("getPolicy"),
476 +             new SecurityPermission("setPolicy"),
477 +             new RuntimePermission("setSecurityManager"),
478 +             // Permissions needed by the junit test harness
479 +             new RuntimePermission("accessDeclaredMembers"),
480 +             new PropertyPermission("*", "read"),
481 +             new java.io.FilePermission("<<ALL FILES>>", "read"));
482 +    }
483 +
484 +    /**
485       * Sleep until the timeout has elapsed, or interrupted.
486       * Does <em>NOT</em> throw InterruptedException.
487       */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines