jdk/test/java/util/concurrent/Executors/PrivilegedCallables.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright (c) 2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6552961 6558429
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test privilegedCallable, privilegedCallableUsingCurrentClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @run main/othervm PrivilegedCallables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import static java.util.concurrent.Executors.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class PrivilegedCallables {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    Callable<Integer> real;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    final Callable<Integer> realCaller = new Callable<Integer>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        public Integer call() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            return real.call(); }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    final Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    @SuppressWarnings("serial") Throwable[] throwables = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        new Exception() {},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        new RuntimeException() {},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        new Error() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    Throwable randomThrowable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        return throwables[rnd.nextInt(throwables.length)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // A Policy class designed to make permissions fiddling very easy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static class Policy extends java.security.Policy {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        private Permissions perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        public void setPermissions(Permission...permissions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            perms = new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            for (Permission permission : permissions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                perms.add(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        public Policy() { setPermissions(/* Nothing */); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        public PermissionCollection getPermissions(CodeSource cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            return perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        public PermissionCollection getPermissions(ProtectionDomain pd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            return perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        public boolean implies(ProtectionDomain pd, Permission p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            return perms.implies(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        public void refresh() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    void test(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        testPrivileged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        final Policy policy = new Policy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        Policy.setPolicy(policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        policy.setPermissions(new RuntimePermission("getClassLoader"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                              new RuntimePermission("setContextClassLoader"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                              new RuntimePermission("stopThread"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        System.setSecurityManager(new SecurityManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        testPrivileged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        policy.setPermissions(/* Nothing */);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        THROWS(AccessControlException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
               new F() {void f(){ privilegedCallableUsingCurrentClassLoader(realCaller); }},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
               new F() {void f(){ privilegedThreadFactory(); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        policy.setPermissions(new RuntimePermission("setSecurityManager"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        System.setSecurityManager(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    void testPrivileged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        try { test(privilegedCallable(realCaller)); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        try { test(privilegedCallableUsingCurrentClassLoader(realCaller)); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        try { privilegedThreadFactory(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    void test(final Callable<Integer> c) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        for (int i = 0; i < 20; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (rnd.nextBoolean()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                final Throwable t = randomThrowable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                real = new Callable<Integer>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    @SuppressWarnings("deprecation")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    public Integer call() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                        Thread.currentThread().stop(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                        return null; }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    c.call();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    fail("Expected exception not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                } catch (Throwable tt) { check(t == tt); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                final int n = rnd.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                real = new Callable<Integer>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    public Integer call() { return n; }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                equal(c.call(), n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    void fail(String msg) {System.err.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        new PrivilegedCallables().instanceMain(args);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    void instanceMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        try {test(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    abstract class F {abstract void f() throws Throwable;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    void THROWS(Class<? extends Throwable> k, F... fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        for (F f : fs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            try {f.f(); fail("Expected " + k.getName() + " not thrown");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                if (k.isAssignableFrom(t.getClass())) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                else unexpected(t);}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
}