Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/util/Resources/Usages.java
41152 views
1
/*
2
* Copyright (c) 2018, 2020, 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 8215937
27
* @modules java.base/sun.security.util
28
* java.base/sun.security.tools.keytool
29
* jdk.jartool/sun.security.tools.jarsigner
30
* @summary Check usages of security-related Resources files
31
*/
32
33
import java.io.IOException;
34
import java.io.UncheckedIOException;
35
import java.nio.file.Files;
36
import java.nio.file.Path;
37
import java.util.Collections;
38
import java.util.HashSet;
39
import java.util.List;
40
import java.util.ListResourceBundle;
41
import java.util.Map;
42
import java.util.Set;
43
import java.util.regex.Matcher;
44
import java.util.regex.Pattern;
45
46
/**
47
* This test checks if the strings in various Resources files are used
48
* properly. Each string must be used somewhere, and each getString() call
49
* must use an existing string.
50
* <p>
51
* For each Resources file, the test maintains a list of where the strings are
52
* used (a file or a directory) and how they are used (one or more patterns).
53
* <p>
54
* If this test fails, there can be several reasons:
55
* <p>
56
* 1. If a string is not found, it has not been added to a Resources file.
57
* <p>
58
* 2. If a string is not used, maybe the call was removed earlier but the
59
* Resources file was not updated. Or, the file is not listed or the
60
* pattern is not correct and the usage is not found.
61
* <p>
62
* Because of #2 above, this test might not be complete. If a getString()
63
* is called but either the file and calling pattern is not listed here,
64
* we cannot guarantee it exists in a Resources file.
65
*/
66
public class Usages {
67
68
// src folder
69
static Path SRC = Path.of(
70
System.getProperty("test.src"), "../../../../../../src/")
71
.normalize();
72
73
// rb.getString(). Used by keytool, jarsigner, and KeyStoreUtil.
74
static Pattern RB_GETSTRING = Pattern.compile(
75
"(?m)rb[ \\n]*\\.getString[ \\n]*\\([ \\n]*\"(.*?)\"\\)");
76
77
static Pattern EVENT_OCSP_CRL = Pattern.compile(
78
"Event\\.report\\(.*, \"(.*?)\",");
79
80
// Command and Option enums in keytool
81
static Pattern KT_ENUM = Pattern.compile("\\n +[A-Z]+\\(.*\"(.*)\"");
82
83
// ResourceMgr.getAuthResourceString
84
static Pattern GETAUTHSTRING = Pattern.compile(
85
"getAuthResourceString[ \\n]*\\([ \\n]*\"(.*?)\"\\)");
86
87
// ResourceMgr.getString
88
static Pattern MGR_GETSTRING = Pattern.compile(
89
"ResourcesMgr\\.getString[ \\n]*\\([ \\n]*\"(.*?)\"\\)");
90
91
// LocalizedMessage.getNonlocalized("...")
92
static Pattern LOC_GETNONLOC = Pattern.compile(
93
"LocalizedMessage\\.getNonlocalized[ \\n]*\\([ \\n]*\"(.*?)\"");
94
95
// LocalizedMessage.getNonlocalized(POLICY + "...")
96
static Pattern LOC_GETNONLOC_POLICY = Pattern.compile(
97
"LocalizedMessage\\.getNonlocalized[ \\n]*\\([ \\n]*(POLICY \\+ \".*?)\"");
98
99
// new LocalizedMessage("...")
100
static Pattern NEW_LOC = Pattern.compile(
101
"new LocalizedMessage[ \\n]*\\([ \\n]*\"(.*?)\"");
102
103
// ioException in ConfigFile.java
104
static Pattern IOEXCEPTION = Pattern.compile(
105
"ioException[ \\n]*\\([ \\n]*\"(.*?)\",");
106
107
// For each Resources file, where and how the strings are used.
108
static Map<ListResourceBundle, List<Pair>> MAP = Map.of(
109
new sun.security.tools.keytool.Resources(), List.of(
110
new Pair("java.base/share/classes/sun/security/tools/keytool/Main.java",
111
List.of(RB_GETSTRING, KT_ENUM)),
112
new Pair("java.base/share/classes/sun/security/tools/KeyStoreUtil.java",
113
List.of(RB_GETSTRING))),
114
new sun.security.util.AuthResources(), List.of(
115
new Pair("java.base/share/classes/sun/security/provider/ConfigFile.java",
116
List.of(GETAUTHSTRING, IOEXCEPTION)),
117
new Pair("jdk.security.auth/share/classes/com/sun/security/auth/",
118
List.of(GETAUTHSTRING))),
119
new sun.security.tools.jarsigner.Resources(), List.of(
120
new Pair("jdk.jartool/share/classes/sun/security/tools/jarsigner/Main.java",
121
List.of(RB_GETSTRING)),
122
new Pair("java.base/share/classes/sun/security/provider/certpath/OCSP.java",
123
List.of(EVENT_OCSP_CRL)),
124
new Pair("java.base/share/classes/sun/security/provider/certpath/DistributionPointFetcher.java",
125
List.of(EVENT_OCSP_CRL)),
126
new Pair("java.base/share/classes/sun/security/tools/KeyStoreUtil.java",
127
List.of(RB_GETSTRING))),
128
new sun.security.util.Resources(), List.of(
129
new Pair("jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/SunPKCS11.java",
130
List.of(MGR_GETSTRING)),
131
new Pair("java.base/share/classes/sun/security/provider/PolicyParser.java",
132
List.of(LOC_GETNONLOC, NEW_LOC)),
133
new Pair("java.base/share/classes/sun/security/provider/PolicyFile.java",
134
List.of(MGR_GETSTRING, LOC_GETNONLOC, LOC_GETNONLOC_POLICY)),
135
new Pair("java.base/share/classes/javax/security/auth/",
136
List.of(MGR_GETSTRING)))
137
);
138
139
public static void main(String[] args) {
140
if (Files.exists(SRC)) {
141
MAP.forEach(Usages::check);
142
} else {
143
System.out.println("No src directory. Test skipped.");
144
}
145
}
146
147
private static void check(ListResourceBundle res, List<Pair> fnps) {
148
try {
149
System.out.println(">>>> Checking " + res.getClass().getName());
150
151
List<String> keys = Collections.list(res.getKeys());
152
153
// Initialize unused to be all keys. Each time a key is used it
154
// is removed. We cannot reuse keys because a key might be used
155
// multiple times. Make it a Set so we can check duplicates.
156
Set<String> unused = new HashSet<>(keys);
157
158
keys.forEach(Usages::checkKeyFormat);
159
if (keys.size() != unused.size()) {
160
throw new RuntimeException("Duplicates found");
161
}
162
163
for (Pair fnp : fnps) {
164
Files.find(SRC.resolve(fnp.path), Integer.MAX_VALUE,
165
(p, attr) -> p.toString().endsWith(".java"))
166
.forEach(pa -> {
167
try {
168
String content = Files.readString(pa);
169
for (Pattern p : fnp.patterns) {
170
Matcher m = p.matcher(content);
171
while (m.find()) {
172
String arg = m.group(1);
173
// Special case in PolicyFile.java:
174
if (arg.startsWith("POLICY + \"")) {
175
arg = "java.security.policy"
176
+ arg.substring(10);
177
}
178
if (!keys.contains(arg)) {
179
throw new RuntimeException(
180
"Not found: " + arg);
181
}
182
unused.remove(arg);
183
}
184
}
185
} catch (IOException e) {
186
throw new UncheckedIOException(e);
187
}
188
});
189
}
190
if (!unused.isEmpty()) {
191
throw new RuntimeException("Unused keys: " + unused);
192
}
193
} catch (Exception e) {
194
throw new RuntimeException(e);
195
}
196
}
197
198
private static void checkKeyFormat(String key) {
199
for (char c : key.toCharArray()) {
200
if (Character.isLetter(c) || Character.isDigit(c) ||
201
c == '{' || c == '}' || c == '.') {
202
// OK
203
} else {
204
throw new RuntimeException(
205
"Illegal char [" + c + "] in key: " + key);
206
}
207
}
208
}
209
210
static class Pair {
211
212
public final String path;
213
public final List<Pattern> patterns;
214
215
public Pair(String path, List<Pattern> patterns) {
216
this.path = path;
217
this.patterns = patterns;
218
}
219
}
220
}
221
222