Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/ProtectionDomain/AllPerm.java
41152 views
1
/*
2
* Copyright (c) 2005, 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 6256734
27
* @summary ProtectionDomain could optimize implies by first checking for
28
* AllPermission in internal collection
29
* @run main/othervm -Djava.security.manager=allow AllPerm
30
*/
31
32
import java.io.*;
33
import java.net.*;
34
import java.security.*;
35
import java.lang.reflect.*;
36
37
public class AllPerm {
38
39
private static final Class[] ARGS = new Class[] { };
40
private static ProtectionDomain allPermClassDomain;
41
42
public static void main(String[]args) throws Exception {
43
44
// create custom class loader that assigns AllPermission to
45
// classes it loads
46
47
File file = new File(System.getProperty("test.src"), "AllPerm.jar");
48
URL[] urls = new URL[] { file.toURL() };
49
ClassLoader parent = Thread.currentThread().getContextClassLoader();
50
AllPermLoader loader = new AllPermLoader(urls, parent);
51
52
// load a class from AllPerm.jar using custom loader
53
54
Object o = loader.loadClass("AllPermClass").newInstance();
55
Method doCheck = o.getClass().getMethod("doCheck", ARGS);
56
allPermClassDomain = o.getClass().getProtectionDomain();
57
58
// set a custom Policy and set the SecurityManager
59
60
Policy.setPolicy(new AllPermPolicy());
61
System.setSecurityManager(new SecurityManager());
62
63
// invoke method on loaded class, which will perform a
64
// security-sensitive operation. custom policy will check
65
// to see if it is called (it should not be called)
66
67
doCheck.invoke(o, ARGS);
68
}
69
70
/**
71
* this class loader assigns AllPermission to classes that it loads
72
*/
73
private static class AllPermLoader extends URLClassLoader {
74
75
public AllPermLoader(URL[] urls, ClassLoader parent) {
76
super(urls, parent);
77
}
78
79
protected PermissionCollection getPermissions(CodeSource codesource) {
80
Permissions perms = new Permissions();
81
perms.add(new AllPermission());
82
return perms;
83
}
84
}
85
86
/**
87
* this policy should not be called if domain is allPermClassDomain
88
*/
89
private static class AllPermPolicy extends Policy {
90
public boolean implies(ProtectionDomain domain, Permission permission) {
91
if (domain == allPermClassDomain) {
92
throw new SecurityException
93
("Unexpected call into AllPermPolicy");
94
}
95
return true;
96
}
97
}
98
}
99
100
/**
101
* here is the source code for AllPermClass inside AllPerm.jar
102
*/
103
/*
104
public class AllPermClass {
105
public void doCheck() {
106
System.getProperty("user.name");
107
}
108
}
109
*/
110
111