Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/util/FilePermCompat/CompatImpact.java
41152 views
1
/*
2
* Copyright (c) 2016, 2019, 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
/*
25
* @test
26
* @bug 8164705 8168410
27
* @summary check compatibility after FilePermission change
28
* @library /test/lib
29
* @run main CompatImpact prepare
30
* @run main CompatImpact builtin
31
* @run main/othervm -Djdk.security.filePermCompat=true CompatImpact mine
32
* @run main/fail CompatImpact mine
33
* @run main CompatImpact dopriv
34
*/
35
36
import jdk.test.lib.process.Proc;
37
38
import java.io.File;
39
import java.io.FilePermission;
40
import java.nio.file.Files;
41
import java.nio.file.Paths;
42
import java.security.AccessController;
43
import java.security.AllPermission;
44
import java.security.CodeSource;
45
import java.security.Permission;
46
import java.security.PermissionCollection;
47
import java.security.Policy;
48
import java.security.PrivilegedAction;
49
import java.security.ProtectionDomain;
50
import java.security.SecurityPermission;
51
52
public class CompatImpact {
53
54
public static void main(String[] args) throws Exception {
55
switch (args[0]) {
56
// copy class files to future classpath
57
case "prepare":
58
// cp in .
59
String cp = System.getProperty("test.classes");
60
Files.copy(Paths.get(cp, "CompatImpact.class"),
61
Paths.get("CompatImpact.class"));
62
Files.copy(Paths.get(cp, "CompatImpact$MP.class"),
63
Paths.get("CompatImpact$MP.class"));
64
Files.write(Paths.get("f"), new byte[10]);
65
// cp in ./sub
66
Files.createDirectory(Paths.get("sub"));
67
Files.copy(Paths.get(cp, "CompatImpact.class"),
68
Paths.get("sub", "CompatImpact.class"));
69
Files.copy(Paths.get(cp, "CompatImpact$MP.class"),
70
Paths.get("sub", "CompatImpact$MP.class"));
71
Files.write(Paths.get("sub", "f"), new byte[10]);
72
// cp in ./inner
73
Files.createDirectory(Paths.get("inner"));
74
Files.copy(Paths.get(cp, "CompatImpact$DoPrivInner.class"),
75
Paths.get("inner", "CompatImpact$DoPrivInner.class"));
76
break;
77
// default policy always covered, user-defined depends on
78
// system property jdk.security.filePermCompact.
79
case "builtin":
80
case "mine":
81
cp = System.getProperty("test.classes");
82
Proc p;
83
String failed = "";
84
String testcase = "";
85
String cwd = System.getProperty("user.dir");
86
87
// Granting a FilePermission on an absolute path
88
testcase = "PonA";
89
p = p(args[0], cwd + "/f")
90
.args("f", cwd + "/f")
91
.debug(testcase)
92
.start();
93
if (p.waitFor() != 0) {
94
Files.copy(Paths.get(testcase + ".stderr"), System.out);
95
failed += testcase + " ";
96
}
97
98
// Granting a FilePermission on a relative path
99
testcase = "PonR";
100
p = p(args[0], "f")
101
.args("f", cwd + "/f")
102
.debug(testcase)
103
.start();
104
if (p.waitFor() != 0) {
105
Files.copy(Paths.get(testcase + ".stderr"), System.out);
106
failed += testcase + " ";
107
}
108
109
// Reading file on classpath, not cwd
110
testcase = "cp";
111
String cprel = Paths.get(cwd).relativize(Paths.get(cp))
112
.normalize().toString();
113
p = p(args[0], "x")
114
.args(cp + "/f", cprel + "/f")
115
.debug(testcase)
116
.start();
117
if (p.waitFor() != 0) {
118
Files.copy(Paths.get(testcase + ".stderr"), System.out);
119
failed += testcase + " ";
120
}
121
122
// Reading file on classpath, cwd
123
testcase = "cpHere";
124
p = p(args[0], "x")
125
.args(cwd + "/f", "f", "RES")
126
.cp(".") // Must! cancel the old CLASSPATH.
127
.debug(testcase)
128
.start();
129
if (p.waitFor() != 0) {
130
Files.copy(Paths.get(testcase + ".stderr"), System.out);
131
failed += testcase + " ";
132
}
133
134
// Reading file on classpath, cwd
135
testcase = "cpSub";
136
p = p(args[0], "x")
137
.args(cwd + "/sub/f", "sub/f", "RES")
138
.cp("sub") // Must! There's CLASSPATH.
139
.debug(testcase)
140
.start();
141
if (p.waitFor() != 0) {
142
Files.copy(Paths.get(testcase + ".stderr"), System.out);
143
failed += testcase + " ";
144
}
145
146
if (!failed.isEmpty()) {
147
throw new Exception(failed + "failed");
148
}
149
break;
150
// test <policy_type> <grant> <read...>
151
case "test":
152
if (args[1].equals("mine")) {
153
Policy.setPolicy(new MP(args[2]));
154
}
155
Exception e = null;
156
for (int i = 3; i < args.length; i++) {
157
try {
158
System.out.println(args[i]);
159
if (args[i].equals("RES")) {
160
CompatImpact.class.getResourceAsStream("f")
161
.close();
162
} else {
163
new File(args[i]).exists();
164
}
165
} catch (Exception e2) {
166
e = e2;
167
e2.printStackTrace(System.out);
168
}
169
}
170
if (e != null) {
171
System.err.println("====================");
172
throw e;
173
}
174
break;
175
// doPrivWithPerm test launcher
176
case "dopriv":
177
cwd = System.getProperty("user.dir");
178
// caller (CompatImpact doprivouter, no permission) in sub,
179
// executor (DoPrivInner, AllPermission) in inner.
180
p = Proc.create("CompatImpact")
181
.args("doprivouter")
182
.prop("java.security.manager", "")
183
.grant(new File("inner"))
184
.perm(new AllPermission())
185
.cp("sub", "inner")
186
.debug("doPriv")
187
.args(cwd)
188
.start();
189
if (p.waitFor() != 0) {
190
throw new Exception("dopriv test fails");
191
}
192
break;
193
// doprivouter <cwd>
194
case "doprivouter":
195
DoPrivInner.main(args);
196
break;
197
default:
198
throw new Exception("unknown " + args[0]);
199
}
200
}
201
202
// Call by CompatImpact doprivouter, with AllPermission
203
public static class DoPrivInner {
204
public static void main(String[] args) throws Exception {
205
AccessController.doPrivileged((PrivilegedAction<Boolean>)
206
() -> new File("x").exists(),
207
null,
208
new FilePermission(args[1] + "/x", "read"));
209
AccessController.doPrivileged((PrivilegedAction<Boolean>)
210
() -> new File(args[1] + "/x").exists(),
211
null,
212
new FilePermission("x", "read"));
213
try {
214
AccessController.doPrivileged((PrivilegedAction<Boolean>)
215
() -> new File("x").exists(),
216
null,
217
new FilePermission("y", "read"));
218
throw new Exception("Should not read");
219
} catch (SecurityException se) {
220
// Expected
221
}
222
}
223
}
224
225
// Return a Proc object for different policy types
226
private static Proc p(String type, String f) throws Exception {
227
Proc p = Proc.create("CompatImpact")
228
.prop("java.security.manager", "")
229
.inheritProp("jdk.security.filePermCompat");
230
p.args("test", type);
231
switch (type) {
232
case "builtin":
233
// For builtin policy, reading access to f can be
234
// granted as a permission
235
p.perm(new FilePermission(f, "read"));
236
p.args("-");
237
break;
238
case "mine":
239
// For my policy, f is passed into test and new MP(f)
240
// will be set as new policy
241
p.perm(new SecurityPermission("setPolicy"));
242
p.perm(new SecurityPermission("getPolicy"));
243
p.args(f);
244
break;
245
default:
246
throw new Exception("unknown " + type);
247
}
248
return p;
249
}
250
251
// My own Policy impl, with only one granted permission, also not smart
252
// enough to know whether ProtectionDomain grants any permission
253
static class MP extends Policy {
254
static final Policy DEFAULT_POLICY = Policy.getPolicy();
255
final PermissionCollection pc;
256
257
MP(String f) {
258
FilePermission p = new FilePermission(f, "read");
259
pc = p.newPermissionCollection();
260
pc.add(p);
261
}
262
@Override
263
public PermissionCollection getPermissions(CodeSource codesource) {
264
return pc;
265
}
266
267
@Override
268
public PermissionCollection getPermissions(ProtectionDomain domain) {
269
return pc;
270
}
271
272
@Override
273
public boolean implies(ProtectionDomain domain, Permission permission) {
274
return pc.implies(permission) || DEFAULT_POLICY.implies(domain, permission);
275
}
276
}
277
}
278
279