Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/SecurityManager/PackageAccessTest.java
41149 views
1
/*
2
* Copyright (c) 1999, 2018, 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 4256530 4136245
27
* @summary Check that restricted packages that are supposed to be restricted
28
* and explicit grants accessClassInPackage permission overridden in
29
* privileged block
30
* @modules java.base/sun.security.x509
31
* @run main/othervm PackageAccessTest
32
* @run main/othervm/java.security.policy=test.policy PackageAccessTest access
33
* @run main/othervm/java.security.policy=empty.policy PackageAccessTest deny
34
*/
35
36
import java.security.AccessControlException;
37
import java.security.AccessController;
38
import java.security.PrivilegedExceptionAction;
39
40
public class PackageAccessTest {
41
42
public static void main(String[] args) {
43
boolean access = true;
44
45
if (args != null && args.length > 0) {
46
switch (args[0]) {
47
case "access":
48
access = true;
49
break;
50
case "deny":
51
access = false;
52
break;
53
default:
54
throw new RuntimeException(
55
"Invalid input parameter " + args[0]);
56
}
57
}
58
59
testPkgAccess(access);
60
testPkgAccessWithPrivileged(access);
61
}
62
63
private static void testPkgAccess(boolean access) {
64
try {
65
sun.security.x509.X509CertInfo x = new sun.security.x509.X509CertInfo();
66
if (!access) {
67
throw new RuntimeException(
68
"application unexpectedly able to access the internal package");
69
}
70
} catch (SecurityException se) {
71
if (access) {
72
throw new RuntimeException("Unexpected security exception", se);
73
}
74
}
75
}
76
77
private static void testPkgAccessWithPrivileged(boolean access) {
78
sun.security.x509.X509CertInfo o = null;
79
try {
80
o = (sun.security.x509.X509CertInfo) AccessController.doPrivileged(
81
(PrivilegedExceptionAction) () -> new sun.security.x509.X509CertInfo());
82
if (!access) {
83
throw new RuntimeException(
84
"application unexpectedly able to access the internal package");
85
}
86
} catch (AccessControlException ace) {
87
if (access) {
88
throw new RuntimeException("Unexpected AccessControlException", ace);
89
}
90
} catch (Exception ex) {
91
throw new RuntimeException("Test failed with unexpected exception", ex);
92
}
93
if (access && o == null)
94
throw new RuntimeException(
95
"Test failed: unable to instantiate object");
96
}
97
98
}
99
100