Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/crypto/CryptoPermission/TestUnlimited.java
38840 views
1
/*
2
* Copyright (c) 2016, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
/**
27
* @test
28
* @bug 8061842
29
* @summary Package jurisdiction policy files as something other than JAR
30
* @run main/othervm TestUnlimited use_default default
31
* @run main/othervm TestUnlimited "" exception
32
* @run main/othervm TestUnlimited limited limited
33
* @run main/othervm TestUnlimited unlimited unlimited
34
* @run main/othervm TestUnlimited unlimited/ unlimited
35
* @run main/othervm TestUnlimited NosuchDir exception
36
* @run main/othervm TestUnlimited . exception
37
* @run main/othervm TestUnlimited /tmp/unlimited exception
38
* @run main/othervm TestUnlimited ../policy/unlimited exception
39
* @run main/othervm TestUnlimited ./unlimited exception
40
* @run main/othervm TestUnlimited /unlimited exception
41
*/
42
import javax.crypto.*;
43
import java.security.Security;
44
import java.nio.file.*;
45
import java.util.stream.*;
46
47
public class TestUnlimited {
48
49
private enum Result {
50
UNLIMITED,
51
LIMITED,
52
EXCEPTION,
53
UNKNOWN
54
};
55
56
/*
57
* Grab the default policy entry from java.security.
58
*
59
* If the input java.security file is malformed
60
* (missing crypto.policy, attribute/no value, etc), throw
61
* exception. split() might throw AIOOB which
62
* is ok behavior.
63
*/
64
private static String getDefaultPolicy() throws Exception {
65
String javaHome = System.getProperty("java.home");
66
Path path = Paths.get(javaHome, "lib", "security", "java.security");
67
68
try (Stream<String> lines = Files.lines(path)) {
69
String s = lines.filter(x -> x.startsWith("crypto.policy="))
70
.findFirst().orElse("");
71
if (!s.isEmpty())
72
return s.split("=")[1].trim();
73
return s;
74
}
75
}
76
77
public static void main(String[] args) throws Exception {
78
/*
79
* Override the Security property to allow for unlimited policy.
80
* Would need appropriate permissions if Security Manager were
81
* active.
82
*/
83
if (args.length != 2) {
84
throw new Exception("Two args required");
85
}
86
87
String testStr = args[0];
88
String expectedStr = args[1];
89
if (testStr.equals("use_default")) {
90
expectedStr = getDefaultPolicy();
91
}
92
93
Result expected = Result.UNKNOWN; // avoid NPE warnings
94
Result result;
95
96
switch (expectedStr) {
97
case "":
98
case "unlimited":
99
expected = Result.UNLIMITED;
100
break;
101
case "limited":
102
expected = Result.LIMITED;
103
break;
104
case "exception":
105
expected = Result.EXCEPTION;
106
break;
107
default:
108
throw new Exception("Unexpected argument");
109
}
110
111
System.out.println("Testing: " + testStr);
112
if (testStr.equals("\"\"")) {
113
Security.setProperty("crypto.policy", "");
114
} else {
115
// skip default case.
116
if (!testStr.equals("use_default")) {
117
Security.setProperty("crypto.policy", testStr);
118
}
119
}
120
121
/*
122
* Use the AES as the test Cipher
123
* If there is an error initializing, we will never get past here.
124
*/
125
try {
126
int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
127
System.out.println("max AES key len:" + maxKeyLen);
128
if (maxKeyLen > 128) {
129
System.out.println("Unlimited policy is active");
130
result = Result.UNLIMITED;
131
} else {
132
System.out.println("Unlimited policy is NOT active");
133
result = Result.LIMITED;
134
}
135
} catch (Throwable e) {
136
//ExceptionInInitializerError's
137
result = Result.EXCEPTION;
138
}
139
140
System.out.println(
141
"Expected:\t" + expected + "\nResult:\t\t" + result);
142
if (!expected.equals(result)) {
143
throw new Exception("Didn't match");
144
}
145
146
System.out.println("DONE!");
147
}
148
}
149
150