Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/ProcessHandle/PermissionTest.java
41149 views
1
/*
2
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. 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 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.lang.reflect.ReflectPermission;
27
import java.security.CodeSource;
28
import java.security.Permission;
29
import java.security.PermissionCollection;
30
import java.security.Permissions;
31
import java.security.Policy;
32
import java.security.ProtectionDomain;
33
import java.security.SecurityPermission;
34
import java.util.Arrays;
35
import java.util.PropertyPermission;
36
37
import org.testng.Assert;
38
import org.testng.annotations.AfterClass;
39
import org.testng.annotations.Test;
40
41
/*
42
* @test
43
* @run testng/othervm -Djava.security.manager=allow PermissionTest
44
* @summary Test Permissions to access Info
45
*/
46
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
70
public void descendantsWithPermission() {
71
Policy.setPolicy(new TestPolicy(new RuntimePermission("manageProcess")));
72
currentHndl.descendants();
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
/**
122
* Setup a policy that would reject ProcessHandle requests without Permissions ManageProcess.
123
*/
124
public void noPermissionsSetup(){
125
Policy.setPolicy(new TestPolicy());
126
SecurityManager sm = new SecurityManager();
127
System.setSecurityManager(sm);
128
}
129
130
@Test(expectedExceptions = SecurityException.class)
131
public void noPermissionAllChildren() {
132
noPermissionsSetup();
133
currentHndl.descendants();
134
}
135
136
@Test(expectedExceptions = SecurityException.class)
137
public void noPermissionAllProcesses() {
138
noPermissionsSetup();
139
ProcessHandle.allProcesses();
140
}
141
142
@Test(expectedExceptions = SecurityException.class)
143
public void noPermissionChildren() {
144
noPermissionsSetup();
145
currentHndl.children();
146
}
147
148
@Test(expectedExceptions = SecurityException.class)
149
public void noPermissionCurrent() {
150
noPermissionsSetup();
151
ProcessHandle.current();
152
}
153
154
@Test(expectedExceptions = SecurityException.class)
155
public void noPermissionOf() {
156
noPermissionsSetup();
157
ProcessHandle.of(0);
158
}
159
160
@Test(expectedExceptions = SecurityException.class)
161
public void noPermissionParent() {
162
noPermissionsSetup();
163
currentHndl.parent();
164
}
165
166
@Test(expectedExceptions = SecurityException.class)
167
public void noPermissionProcessToHandle() throws IOException {
168
noPermissionsSetup();
169
Process p = null;
170
try {
171
ProcessBuilder pb = new ProcessBuilder("sleep", "30");
172
p = pb.start();
173
ProcessHandle ph = p.toHandle();
174
Assert.assertNotNull(ph, "ProcessHandle expected from Process");
175
} finally {
176
if (p != null) {
177
p.destroy();
178
}
179
}
180
}
181
182
@AfterClass
183
public void tearDownClass() throws Exception {
184
System.setSecurityManager(sm);
185
Policy.setPolicy(policy);
186
}
187
}
188
189
class TestPolicy extends Policy {
190
191
static final Policy DEFAULT_POLICY = Policy.getPolicy();
192
193
private final PermissionCollection permissions = new Permissions();
194
195
public TestPolicy() {
196
setBasicPermissions();
197
}
198
199
/*
200
* Defines the minimal permissions required by testNG and set security
201
* manager permission when running these tests.
202
*/
203
public void setBasicPermissions() {
204
permissions.add(new SecurityPermission("getPolicy"));
205
permissions.add(new SecurityPermission("setPolicy"));
206
permissions.add(new RuntimePermission("getClassLoader"));
207
permissions.add(new RuntimePermission("setSecurityManager"));
208
permissions.add(new RuntimePermission("createSecurityManager"));
209
permissions.add(new PropertyPermission("user.dir", "read"));
210
permissions.add(new PropertyPermission("test.src", "read"));
211
permissions.add(new PropertyPermission("file.separator", "read"));
212
permissions.add(new PropertyPermission("line.separator", "read"));
213
permissions.add(new PropertyPermission("fileStringBuffer", "read"));
214
permissions.add(new PropertyPermission("dataproviderthreadcount", "read"));
215
permissions.add(new PropertyPermission("testng.show.stack.frames",
216
"read"));
217
permissions.add(new PropertyPermission("testng.thread.affinity", "read"));
218
permissions.add(new PropertyPermission("testng.memory.friendly", "read"));
219
permissions.add(new PropertyPermission("testng.mode.dryrun", "read"));
220
permissions.add(new PropertyPermission("testng.report.xml.name", "read"));
221
permissions.add(new PropertyPermission("testng.timezone", "read"));
222
permissions.add(new ReflectPermission("suppressAccessChecks"));
223
permissions.add(new FilePermission("<<ALL FILES>>", "execute"));
224
}
225
226
public TestPolicy(Permission... ps) {
227
setBasicPermissions();
228
Arrays.stream(ps).forEach(p -> permissions.add(p));
229
}
230
231
@Override
232
public PermissionCollection getPermissions(ProtectionDomain domain) {
233
return permissions;
234
}
235
236
@Override
237
public PermissionCollection getPermissions(CodeSource codesource) {
238
return permissions;
239
}
240
241
@Override
242
public boolean implies(ProtectionDomain domain, Permission perm) {
243
return permissions.implies(perm) || DEFAULT_POLICY.implies(domain, perm);
244
}
245
}
246
247