blob: 0f933a980075387fbf917ab44617b019f98b346c [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any 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
32import java.util.concurrent.*;
33import java.util.*;
34import java.security.*;
35import static java.util.concurrent.Executors.*;
36
37public 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 //----------------------------------------------------------------
58 static class Policy extends java.security.Policy {
59 private Permissions perms;
60
61 public void setPermissions(Permission...permissions) {
62 perms = new Permissions();
63 for (Permission permission : permissions)
64 perms.add(permission);
65 }
66
67 public Policy() { setPermissions(/* Nothing */); }
68
69 public PermissionCollection getPermissions(CodeSource cs) {
70 return perms;
71 }
72
73 public PermissionCollection getPermissions(ProtectionDomain pd) {
74 return perms;
75 }
76
77 public boolean implies(ProtectionDomain pd, Permission p) {
78 return perms.implies(p);
79 }
80
81 public void refresh() {}
82 }
83
84 void test(String[] args) {
85 testPrivileged();
86
87 final Policy policy = new Policy();
88 Policy.setPolicy(policy);
89 policy.setPermissions(new RuntimePermission("getClassLoader"),
90 new RuntimePermission("setContextClassLoader"),
91 new RuntimePermission("stopThread"));
92 System.setSecurityManager(new SecurityManager());
93
94 testPrivileged();
95
96 policy.setPermissions(/* Nothing */);
97
98 THROWS(AccessControlException.class,
99 new F() {void f(){ privilegedCallableUsingCurrentClassLoader(realCaller); }},
100 new F() {void f(){ privilegedThreadFactory(); }});
101
102 policy.setPermissions(new RuntimePermission("setSecurityManager"));
103 System.setSecurityManager(null);
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();}
145 void equal(Object x, Object y) {
146 if (x == null ? y == null : x.equals(y)) pass();
147 else fail(x + " not equal to " + y);}
148 public static void main(String[] args) throws Throwable {
149 new PrivilegedCallables().instanceMain(args);}
150 void instanceMain(String[] args) throws Throwable {
151 try {test(args);} catch (Throwable t) {unexpected(t);}
152 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
153 if (failed > 0) throw new AssertionError("Some tests failed");}
154 abstract class F {abstract void f() throws Throwable;}
155 void THROWS(Class<? extends Throwable> k, F... fs) {
156 for (F f : fs)
157 try {f.f(); fail("Expected " + k.getName() + " not thrown");}
158 catch (Throwable t) {
159 if (k.isAssignableFrom(t.getClass())) pass();
160 else unexpected(t);}}
161}