author | kvn |
Fri, 21 Jun 2019 13:04:14 -0700 | |
changeset 55462 | 6dfdcd31463d |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
30899 | 1 |
/* |
55462
6dfdcd31463d
8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. |
30899 | 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 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 |
import java.io.FilePermission; |
|
25 |
import java.io.IOException; |
|
26 |
import java.security.CodeSource; |
|
27 |
import java.security.Permission; |
|
28 |
import java.security.PermissionCollection; |
|
29 |
import java.security.Permissions; |
|
30 |
import java.security.Policy; |
|
31 |
import java.security.ProtectionDomain; |
|
32 |
import java.security.SecurityPermission; |
|
33 |
import java.util.Arrays; |
|
34 |
import java.util.PropertyPermission; |
|
35 |
||
36 |
import org.testng.Assert; |
|
37 |
import org.testng.annotations.AfterClass; |
|
38 |
import org.testng.annotations.BeforeGroups; |
|
39 |
import org.testng.annotations.Test; |
|
40 |
||
34891 | 41 |
/* |
42 |
* @test |
|
43 |
* @run testng/othervm PermissionTest |
|
44 |
* @summary Test Permissions to access Info |
|
45 |
*/ |
|
46 |
||
30899 | 47 |
public class PermissionTest { |
48 |
/** |
|
49 |
* Backing up policy. |
|
50 |
*/ |
|
51 |
protected static Policy policy; |
|
52 |
||
53 |
/** |
|
54 |
* Backing up security manager. |
|
55 |
*/ |
|
56 |
private static SecurityManager sm; |
|
57 |
||
58 |
/** |
|
59 |
* Current process handle. |
|
60 |
*/ |
|
61 |
private final ProcessHandle currentHndl; |
|
62 |
||
63 |
PermissionTest() { |
|
64 |
policy = Policy.getPolicy(); |
|
65 |
sm = System.getSecurityManager(); |
|
66 |
currentHndl = ProcessHandle.current(); |
|
67 |
} |
|
68 |
||
69 |
@Test |
|
33648
9564031a20e0
8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents:
30899
diff
changeset
|
70 |
public void descendantsWithPermission() { |
30899 | 71 |
Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess"))); |
33648
9564031a20e0
8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents:
30899
diff
changeset
|
72 |
currentHndl.descendants(); |
30899 | 73 |
} |
74 |
||
75 |
@Test |
|
76 |
public void allProcessesWithPermission() { |
|
77 |
Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess"))); |
|
78 |
ProcessHandle.allProcesses(); |
|
79 |
} |
|
80 |
||
81 |
@Test |
|
82 |
public void childrenWithPermission() { |
|
83 |
Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess"))); |
|
84 |
currentHndl.children(); |
|
85 |
} |
|
86 |
||
87 |
@Test |
|
88 |
public void currentWithPermission() { |
|
89 |
Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess"))); |
|
90 |
ProcessHandle.current(); |
|
91 |
} |
|
92 |
||
93 |
@Test |
|
94 |
public void ofWithPermission() { |
|
95 |
Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess"))); |
|
96 |
ProcessHandle.of(0); |
|
97 |
} |
|
98 |
||
99 |
@Test |
|
100 |
public void parentWithPermission() { |
|
101 |
Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess"))); |
|
102 |
currentHndl.parent(); |
|
103 |
} |
|
104 |
||
105 |
@Test |
|
106 |
public void processToHandleWithPermission() throws IOException { |
|
107 |
Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess"))); |
|
108 |
Process p = null; |
|
109 |
try { |
|
110 |
ProcessBuilder pb = new ProcessBuilder("sleep", "30"); |
|
111 |
p = pb.start(); |
|
112 |
ProcessHandle ph = p.toHandle(); |
|
113 |
Assert.assertNotNull(ph, "ProcessHandle expected from Process"); |
|
114 |
} finally { |
|
115 |
if (p != null) { |
|
116 |
p.destroy(); |
|
117 |
} |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
@BeforeGroups (groups = {"NoManageProcessPermission"}) |
|
122 |
public void noPermissionsSetup(){ |
|
123 |
Policy.setPolicy(new TestPolicy()); |
|
124 |
SecurityManager sm = new SecurityManager(); |
|
125 |
System.setSecurityManager(sm); |
|
126 |
} |
|
127 |
||
128 |
@Test(groups = { "NoManageProcessPermission" }, expectedExceptions = SecurityException.class) |
|
129 |
public void noPermissionAllChildren() { |
|
33648
9564031a20e0
8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents:
30899
diff
changeset
|
130 |
currentHndl.descendants(); |
30899 | 131 |
} |
132 |
||
133 |
@Test(groups = { "NoManageProcessPermission" }, expectedExceptions = SecurityException.class) |
|
134 |
public void noPermissionAllProcesses() { |
|
135 |
ProcessHandle.allProcesses(); |
|
136 |
} |
|
137 |
||
138 |
@Test(groups = { "NoManageProcessPermission" }, expectedExceptions = SecurityException.class) |
|
139 |
public void noPermissionChildren() { |
|
140 |
currentHndl.children(); |
|
141 |
} |
|
142 |
||
143 |
@Test(groups = { "NoManageProcessPermission" }, expectedExceptions = SecurityException.class) |
|
144 |
public void noPermissionCurrent() { |
|
145 |
ProcessHandle.current(); |
|
146 |
} |
|
147 |
||
148 |
@Test(groups = { "NoManageProcessPermission" }, expectedExceptions = SecurityException.class) |
|
149 |
public void noPermissionOf() { |
|
150 |
ProcessHandle.of(0); |
|
151 |
} |
|
152 |
||
153 |
@Test(groups = { "NoManageProcessPermission" }, expectedExceptions = SecurityException.class) |
|
154 |
public void noPermissionParent() { |
|
155 |
currentHndl.parent(); |
|
156 |
} |
|
157 |
||
158 |
@Test(groups = { "NoManageProcessPermission" }, expectedExceptions = SecurityException.class) |
|
159 |
public void noPermissionProcessToHandle() throws IOException { |
|
160 |
Process p = null; |
|
161 |
try { |
|
162 |
ProcessBuilder pb = new ProcessBuilder("sleep", "30"); |
|
163 |
p = pb.start(); |
|
164 |
ProcessHandle ph = p.toHandle(); |
|
165 |
Assert.assertNotNull(ph, "ProcessHandle expected from Process"); |
|
166 |
} finally { |
|
167 |
if (p != null) { |
|
168 |
p.destroy(); |
|
169 |
} |
|
170 |
} |
|
171 |
} |
|
172 |
||
173 |
@AfterClass |
|
174 |
public void tearDownClass() throws Exception { |
|
175 |
System.setSecurityManager(sm); |
|
176 |
Policy.setPolicy(policy); |
|
177 |
} |
|
178 |
} |
|
179 |
||
180 |
class TestPolicy extends Policy { |
|
55462
6dfdcd31463d
8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents:
47216
diff
changeset
|
181 |
|
6dfdcd31463d
8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents:
47216
diff
changeset
|
182 |
static final Policy DEFAULT_POLICY = Policy.getPolicy(); |
6dfdcd31463d
8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents:
47216
diff
changeset
|
183 |
|
30899 | 184 |
private final PermissionCollection permissions = new Permissions(); |
185 |
||
186 |
public TestPolicy() { |
|
187 |
setBasicPermissions(); |
|
188 |
} |
|
189 |
||
190 |
/* |
|
191 |
* Defines the minimal permissions required by testNG and set security |
|
192 |
* manager permission when running these tests. |
|
193 |
*/ |
|
194 |
public void setBasicPermissions() { |
|
195 |
permissions.add(new SecurityPermission("getPolicy")); |
|
196 |
permissions.add(new SecurityPermission("setPolicy")); |
|
197 |
permissions.add(new RuntimePermission("getClassLoader")); |
|
198 |
permissions.add(new RuntimePermission("setSecurityManager")); |
|
199 |
permissions.add(new RuntimePermission("createSecurityManager")); |
|
200 |
permissions.add(new PropertyPermission("testng.show.stack.frames", |
|
201 |
"read")); |
|
202 |
permissions.add(new PropertyPermission("user.dir", "read")); |
|
203 |
permissions.add(new PropertyPermission("test.src", "read")); |
|
204 |
permissions.add(new PropertyPermission("file.separator", "read")); |
|
205 |
permissions.add(new PropertyPermission("line.separator", "read")); |
|
206 |
permissions.add(new PropertyPermission("fileStringBuffer", "read")); |
|
207 |
permissions.add(new PropertyPermission("dataproviderthreadcount", "read")); |
|
208 |
permissions.add(new FilePermission("<<ALL FILES>>", "execute")); |
|
209 |
} |
|
210 |
||
211 |
public TestPolicy(Permission... ps) { |
|
212 |
setBasicPermissions(); |
|
213 |
Arrays.stream(ps).forEach(p -> permissions.add(p)); |
|
214 |
} |
|
215 |
||
216 |
@Override |
|
217 |
public PermissionCollection getPermissions(ProtectionDomain domain) { |
|
218 |
return permissions; |
|
219 |
} |
|
220 |
||
221 |
@Override |
|
222 |
public PermissionCollection getPermissions(CodeSource codesource) { |
|
223 |
return permissions; |
|
224 |
} |
|
225 |
||
226 |
@Override |
|
227 |
public boolean implies(ProtectionDomain domain, Permission perm) { |
|
55462
6dfdcd31463d
8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents:
47216
diff
changeset
|
228 |
return permissions.implies(perm) || DEFAULT_POLICY.implies(domain, perm); |
30899 | 229 |
} |
230 |
} |