test/java/util/concurrent/Executors/PrivilegedCallables.java

Print this page




  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6552961 6558429
  27  * @summary Test privilegedCallable, privilegedCallableUsingCurrentClassLoader
  28  * @run main/othervm PrivilegedCallables
  29  * @author Martin Buchholz
  30  */
  31 
  32 import java.util.concurrent.*;
  33 import java.util.*;
  34 import java.security.*;
  35 import static java.util.concurrent.Executors.*;


  36 
  37 public class PrivilegedCallables {











  38     Callable<Integer> real;
  39 
  40     final Callable<Integer> realCaller = new Callable<Integer>() {
  41         public Integer call() throws Exception {
  42             return real.call(); }};
  43 
  44     final Random rnd = new Random();
  45 
  46     @SuppressWarnings("serial") Throwable[] throwables = {
  47         new Exception() {},
  48         new RuntimeException() {},
  49         new Error() {}
  50     };
  51     Throwable randomThrowable() {
  52         return throwables[rnd.nextInt(throwables.length)];
  53     }
  54 
  55     //----------------------------------------------------------------
  56     // A Policy class designed to make permissions fiddling very easy.
  57     //----------------------------------------------------------------


 104     }
 105 
 106     void testPrivileged() {
 107         try { test(privilegedCallable(realCaller)); }
 108         catch (Throwable t) { unexpected(t); }
 109 
 110         try { test(privilegedCallableUsingCurrentClassLoader(realCaller)); }
 111         catch (Throwable t) { unexpected(t); }
 112 
 113         try { privilegedThreadFactory(); }
 114         catch (Throwable t) { unexpected(t); }
 115     }
 116 
 117     void test(final Callable<Integer> c) throws Throwable {
 118         for (int i = 0; i < 20; i++)
 119             if (rnd.nextBoolean()) {
 120                 final Throwable t = randomThrowable();
 121                 real = new Callable<Integer>() {
 122                     @SuppressWarnings("deprecation")
 123                     public Integer call() throws Exception {
 124                         Thread.currentThread().stop(t);
 125                         return null; }};
 126                 try {
 127                     c.call();
 128                     fail("Expected exception not thrown");
 129                 } catch (Throwable tt) { check(t == tt); }
 130             } else {
 131                 final int n = rnd.nextInt();
 132                 real = new Callable<Integer>() {
 133                     public Integer call() { return n; }};
 134                 equal(c.call(), n);
 135             }
 136     }
 137 
 138     //--------------------- Infrastructure ---------------------------
 139     volatile int passed = 0, failed = 0;
 140     void pass() {passed++;}
 141     void fail() {failed++; Thread.dumpStack();}
 142     void fail(String msg) {System.err.println(msg); fail();}
 143     void unexpected(Throwable t) {failed++; t.printStackTrace();}
 144     void check(boolean cond) {if (cond) pass(); else fail();}


  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6552961 6558429
  27  * @summary Test privilegedCallable, privilegedCallableUsingCurrentClassLoader
  28  * @run main/othervm PrivilegedCallables
  29  * @author Martin Buchholz
  30  */
  31 
  32 import java.util.concurrent.*;
  33 import java.util.*;
  34 import java.security.*;
  35 import static java.util.concurrent.Executors.*;
  36 import java.lang.reflect.Field;
  37 import sun.misc.Unsafe;
  38 
  39 public class PrivilegedCallables {
  40     static final Unsafe UNSAFE;
  41     static {
  42         try {
  43             Field f = Unsafe.class.getDeclaredField("theUnsafe");
  44             f.setAccessible(true);
  45             UNSAFE = (Unsafe)f.get(null);
  46         } catch (NoSuchFieldException | IllegalAccessException e) {
  47             throw new RuntimeException(e);
  48         }
  49     }
  50 
  51     Callable<Integer> real;
  52 
  53     final Callable<Integer> realCaller = new Callable<Integer>() {
  54         public Integer call() throws Exception {
  55             return real.call(); }};
  56 
  57     final Random rnd = new Random();
  58 
  59     @SuppressWarnings("serial") Throwable[] throwables = {
  60         new Exception() {},
  61         new RuntimeException() {},
  62         new Error() {}
  63     };
  64     Throwable randomThrowable() {
  65         return throwables[rnd.nextInt(throwables.length)];
  66     }
  67 
  68     //----------------------------------------------------------------
  69     // A Policy class designed to make permissions fiddling very easy.
  70     //----------------------------------------------------------------


 117     }
 118 
 119     void testPrivileged() {
 120         try { test(privilegedCallable(realCaller)); }
 121         catch (Throwable t) { unexpected(t); }
 122 
 123         try { test(privilegedCallableUsingCurrentClassLoader(realCaller)); }
 124         catch (Throwable t) { unexpected(t); }
 125 
 126         try { privilegedThreadFactory(); }
 127         catch (Throwable t) { unexpected(t); }
 128     }
 129 
 130     void test(final Callable<Integer> c) throws Throwable {
 131         for (int i = 0; i < 20; i++)
 132             if (rnd.nextBoolean()) {
 133                 final Throwable t = randomThrowable();
 134                 real = new Callable<Integer>() {
 135                     @SuppressWarnings("deprecation")
 136                     public Integer call() throws Exception {
 137                         UNSAFE.throwException(t);
 138                         return null; }};
 139                 try {
 140                     c.call();
 141                     fail("Expected exception not thrown");
 142                 } catch (Throwable tt) { check(t == tt); }
 143             } else {
 144                 final int n = rnd.nextInt();
 145                 real = new Callable<Integer>() {
 146                     public Integer call() { return n; }};
 147                 equal(c.call(), n);
 148             }
 149     }
 150 
 151     //--------------------- Infrastructure ---------------------------
 152     volatile int passed = 0, failed = 0;
 153     void pass() {passed++;}
 154     void fail() {failed++; Thread.dumpStack();}
 155     void fail(String msg) {System.err.println(msg); fail();}
 156     void unexpected(Throwable t) {failed++; t.printStackTrace();}
 157     void check(boolean cond) {if (cond) pass(); else fail();}