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.46 by jsr166, Tue Dec 1 05:41:40 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 370 | Line 378 | public class JSR166TestCase extends Test
378  
379      // Some convenient Integer constants
380  
381 <    public static final Integer zero = new Integer(0);
382 <    public static final Integer one = new Integer(1);
383 <    public static final Integer two = new Integer(2);
384 <    public static final Integer three  = new Integer(3);
381 >    public static final Integer zero  = new Integer(0);
382 >    public static final Integer one   = new Integer(1);
383 >    public static final Integer two   = new Integer(2);
384 >    public static final Integer three = new Integer(3);
385      public static final Integer four  = new Integer(4);
386      public static final Integer five  = new Integer(5);
387 <    public static final Integer six = new Integer(6);
387 >    public static final Integer six   = new Integer(6);
388      public static final Integer seven = new Integer(7);
389      public static final Integer eight = new Integer(8);
390 <    public static final Integer nine = new Integer(9);
390 >    public static final Integer nine  = new Integer(9);
391      public static final Integer m1  = new Integer(-1);
392      public static final Integer m2  = new Integer(-2);
393      public static final Integer m3  = new Integer(-3);
394 <    public static final Integer m4 = new Integer(-4);
395 <    public static final Integer m5 = new Integer(-5);
396 <    public static final Integer m6 = new Integer(-6);
394 >    public static final Integer m4  = new Integer(-4);
395 >    public static final Integer m5  = new Integer(-5);
396 >    public static final Integer m6  = new Integer(-6);
397      public static final Integer m10 = new Integer(-10);
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       */
# Line 540 | Line 611 | public class JSR166TestCase extends Test
611          public String call() { return TEST_STRING; }
612      }
613  
614 +    public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) {
615 +        return new CheckedCallable<String>() {
616 +            public String realCall() {
617 +                try {
618 +                    latch.await();
619 +                } catch (InterruptedException quittingTime) {}
620 +                return TEST_STRING;
621 +            }};
622 +    }
623 +
624      public static class NPETask implements Callable<String> {
625          public String call() { throw new NullPointerException(); }
626      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines