Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/SecureRandom/DefaultAlgo.java
41149 views
1
/*
2
* Copyright (c) 2019, 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
import static java.lang.System.out;
25
import java.security.Provider;
26
import java.security.Security;
27
import java.security.SecureRandom;
28
import java.security.Provider.Service;
29
import java.util.Arrays;
30
import sun.security.provider.SunEntries;
31
32
/**
33
* @test
34
* @bug 8228613 8246613 8248505
35
* @summary Ensure that the default SecureRandom algo used is based
36
* on the registration ordering, and falls to next provider
37
* if none are found
38
* @modules java.base/sun.security.provider
39
*/
40
public class DefaultAlgo {
41
42
private static final String SR_IMPLCLASS =
43
"sun.security.provider.SecureRandom";
44
45
public static void main(String[] args) throws Exception {
46
String[] algos = { "A", "B", "C" };
47
test3rdParty(algos);
48
// reverse the order and re-check
49
String[] algosReversed = { "C", "B", "A" };
50
test3rdParty(algosReversed);
51
}
52
53
private static void test3rdParty(String[] algos) {
54
Provider[] provs = {
55
new SampleLegacyProvider(algos),
56
new SampleServiceProvider(algos),
57
new CustomProvider(algos)
58
};
59
for (Provider p : provs) {
60
checkDefault(p, algos);
61
}
62
}
63
64
// validate the specified SecureRandom obj to be from the specified
65
// provider and matches the specified algorithm
66
private static void validate(SecureRandom sr, String pName, String algo) {
67
if (!sr.getProvider().getName().equals(pName)) {
68
throw new RuntimeException("Failed provider check, exp: " +
69
pName + ", got " + sr.getProvider().getName());
70
}
71
if (!sr.getAlgorithm().equals(algo)) {
72
throw new RuntimeException("Failed algo check, exp: " +
73
algo + ", got " + sr.getAlgorithm());
74
}
75
}
76
77
private static void checkDefault(Provider p, String ... algos) {
78
String pName = p.getName();
79
out.println(pName + " with " + Arrays.toString(algos));
80
int pos = Security.insertProviderAt(p, 1);
81
82
boolean isLegacy = pName.equals("SampleLegacy");
83
try {
84
if (isLegacy) {
85
for (String s : algos) {
86
validate(new SecureRandom(), pName, s);
87
p.remove("SecureRandom." + s);
88
out.println("removed " + s);
89
}
90
validate(new SecureRandom(), "SUN",
91
SunEntries.DEF_SECURE_RANDOM_ALGO);
92
} else {
93
validate(new SecureRandom(), pName, algos[0]);
94
}
95
out.println("=> Test Passed");
96
} finally {
97
if (pos != -1) {
98
Security.removeProvider(pName);
99
}
100
if (isLegacy) {
101
// add back the removed algos
102
for (String s : algos) {
103
p.put("SecureRandom." + s, SR_IMPLCLASS);
104
}
105
}
106
}
107
}
108
109
private static class SampleLegacyProvider extends Provider {
110
SampleLegacyProvider(String[] listOfSupportedRNGs) {
111
super("SampleLegacy", "1.0", "test provider using legacy put");
112
for (String s : listOfSupportedRNGs) {
113
put("SecureRandom." + s, SR_IMPLCLASS);
114
}
115
}
116
}
117
118
private static class SampleServiceProvider extends Provider {
119
SampleServiceProvider(String[] listOfSupportedRNGs) {
120
super("SampleService", "1.0", "test provider using putService");
121
for (String s : listOfSupportedRNGs) {
122
putService(new Provider.Service(this, "SecureRandom", s,
123
SR_IMPLCLASS, null, null));
124
}
125
}
126
}
127
128
// custom provider which overrides putService(...)/getService() and uses
129
// its own Service impl
130
private static class CustomProvider extends Provider {
131
private static class CustomService extends Provider.Service {
132
CustomService(Provider p, String type, String algo, String cName) {
133
super(p, type, algo, cName, null, null);
134
}
135
}
136
137
CustomProvider(String[] listOfSupportedRNGs) {
138
super("Custom", "1.0", "test provider overrides putService with " +
139
" custom service with legacy registration");
140
for (String s : listOfSupportedRNGs) {
141
putService(new CustomService(this, "SecureRandom", s ,
142
SR_IMPLCLASS));
143
}
144
}
145
@Override
146
protected void putService(Provider.Service s) {
147
// convert to legacy puts
148
put(s.getType() + "." + s.getAlgorithm(), s.getClassName());
149
put(s.getType() + ":" + s.getAlgorithm(), s);
150
}
151
@Override
152
public Provider.Service getService(String type, String algo) {
153
return (Provider.Service) get(type + ":" + algo);
154
}
155
}
156
}
157
158