Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/Provider/GetInstance.java
41152 views
1
/*
2
* Copyright (c) 2003, 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.
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 4856968 7054918 8130181
27
* @library ../testlibrary
28
* @summary make sure getInstance() works correctly, including failover
29
* and delayed provider selection for Signatures
30
* @author Andreas Sterbenz
31
*/
32
33
import java.util.*;
34
35
import java.security.*;
36
import java.security.cert.*;
37
38
public class GetInstance {
39
40
private static void same(Provider p1, Provider p2) throws Exception {
41
if (p1 != p2) {
42
throw new Exception("Wrong provider");
43
}
44
}
45
46
public static void main(String[] args) throws Exception {
47
ProvidersSnapshot snapshot = ProvidersSnapshot.create();
48
try {
49
main0(args);
50
} finally {
51
snapshot.restore();
52
}
53
}
54
55
public static void main0(String[] args) throws Exception {
56
long start = System.currentTimeMillis();
57
58
Provider foo = new FooProvider();
59
Provider bar = new BarProvider();
60
Provider baz = new BazProvider();
61
62
Security.addProvider(foo);
63
Security.addProvider(bar);
64
Security.addProvider(baz);
65
66
System.out.println("Testing MessageDigest.getInstance()...");
67
MessageDigest m;
68
m = MessageDigest.getInstance("foo");
69
m = MessageDigest.getInstance("foo", "foo");
70
m = MessageDigest.getInstance("foo", foo);
71
72
System.out.println("Testing Signature.getInstance() for SPI...");
73
Signature sig;
74
PrivateKey privateKey = new FooPrivateKey();
75
sig = Signature.getInstance("foo");
76
same(foo, sig.getProvider());
77
sig = Signature.getInstance("foo");
78
sig.initSign(privateKey);
79
same(foo, sig.getProvider());
80
sig = Signature.getInstance("foo", "foo");
81
sig.initSign(privateKey);
82
same(foo, sig.getProvider());
83
sig = Signature.getInstance("foo", foo);
84
sig.initSign(privateKey);
85
same(foo, sig.getProvider());
86
87
System.out.println("Testing Signature.getInstance() for Signature...");
88
sig = Signature.getInstance("fuu");
89
same(foo, sig.getProvider());
90
sig = Signature.getInstance("fuu");
91
sig.initSign(privateKey);
92
same(foo, sig.getProvider());
93
sig = Signature.getInstance("fuu", "foo");
94
sig.initSign(privateKey);
95
same(foo, sig.getProvider());
96
sig = Signature.getInstance("fuu", foo);
97
sig.initSign(privateKey);
98
same(foo, sig.getProvider());
99
100
System.out.println("Testing CertStore.getInstance()...");
101
CertStoreParameters params = new CollectionCertStoreParameters(Collections.EMPTY_LIST);
102
CertStore cs;
103
cs = CertStore.getInstance("foo", params);
104
cs = CertStore.getInstance("foo", params, "foo");
105
cs = CertStore.getInstance("foo", params, foo);
106
107
System.out.println("Testing failover...");
108
m = MessageDigest.getInstance("bar");
109
same(m.getProvider(), baz);
110
sig = Signature.getInstance("bar");
111
same(sig.getProvider(), baz);
112
cs = CertStore.getInstance("bar", params);
113
same(cs.getProvider(), baz);
114
115
System.out.println("Testing Signature delayed provider selection...");
116
sig = Signature.getInstance("baz");
117
sig.initVerify(new FooPublicKey());
118
same(sig.getProvider(), baz);
119
120
Provider.Service s = foo.getService("CertStore", "foo");
121
s.newInstance(null);
122
s.newInstance(params);
123
try {
124
s.newInstance(0);
125
throw new Exception("call should not succeed");
126
} catch (NoSuchAlgorithmException e) {
127
e.printStackTrace();
128
Throwable cause = e.getCause();
129
if (cause instanceof InvalidParameterException == false) {
130
throw new Exception("incorrect exception");
131
}
132
}
133
134
long stop = System.currentTimeMillis();
135
System.out.println("Done (" + (stop - start) + " ms).");
136
}
137
138
public static class FooProvider extends Provider {
139
FooProvider() {
140
super("foo", "1.0", "none");
141
put("MessageDigest.foo", "GetInstance$FooDigest");
142
put("CertStore.foo", "GetInstance$FooStore");
143
put("Signature.foo", "GetInstance$FooSignatureSpi");
144
145
put("Signature.fuu", "GetInstance$BazSignature");
146
147
// throws InvalidKeyException, skipped in delayed provider selection
148
put("Signature.baz", "GetInstance$BazSignatureSpi");
149
}
150
}
151
152
public static class BarProvider extends Provider {
153
BarProvider() {
154
super("bar", "1.0", "none");
155
// all entries invalid for failover
156
put("MessageDigest.bar", "GetInstance$FooKey");
157
put("Signature.bar", "GetInstance$FooKey");
158
put("Certstore.bar", "GetInstance$FooKey");
159
160
// not an SPI, skipped in delayed provider selection
161
put("Signature.baz", "GetInstance$BazSignature");
162
}
163
}
164
165
public static class BazProvider extends Provider {
166
BazProvider() {
167
super("baz", "1.0", "none");
168
put("MessageDigest.bar", "GetInstance$FooDigest");
169
put("CertStore.bar", "GetInstance$FooStore");
170
put("Signature.bar", "GetInstance$FooSignatureSpi");
171
172
put("Signature.baz", "GetInstance$FooSignatureSpi");
173
}
174
}
175
176
public static class FooDigest extends MessageDigestSpi {
177
public byte[] engineDigest() { return new byte[0]; }
178
public void engineReset() {}
179
public void engineUpdate(byte input) {}
180
public void engineUpdate(byte[] b, int ofs, int len) {}
181
}
182
183
public static class FooStore extends CertStoreSpi {
184
public FooStore(CertStoreParameters params) throws InvalidAlgorithmParameterException { super(params); }
185
public Collection engineGetCertificates(CertSelector sel) { return Collections.EMPTY_LIST; }
186
public Collection engineGetCRLs(CRLSelector sel) { return Collections.EMPTY_LIST; }
187
}
188
189
public static class BaseSignatureSpi extends SignatureSpi {
190
protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
191
}
192
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
193
}
194
protected void engineUpdate(byte b) throws SignatureException { }
195
protected void engineUpdate(byte[] b, int off, int len) throws SignatureException { }
196
protected byte[] engineSign() throws SignatureException {
197
return new byte[0];
198
}
199
protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
200
return false;
201
}
202
protected void engineSetParameter(String param, Object value) throws InvalidParameterException {
203
}
204
protected Object engineGetParameter(String param) throws InvalidParameterException {
205
return null;
206
}
207
}
208
209
public static class BaseSignature extends Signature {
210
BaseSignature(String s) {
211
super(s);
212
}
213
protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
214
//
215
}
216
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException { }
217
protected void engineUpdate(byte b) throws SignatureException { }
218
protected void engineUpdate(byte[] b, int off, int len) throws SignatureException { }
219
protected byte[] engineSign() throws SignatureException {
220
return new byte[0];
221
}
222
protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
223
return false;
224
}
225
protected void engineSetParameter(String param, Object value) throws InvalidParameterException {
226
}
227
protected Object engineGetParameter(String param) throws InvalidParameterException {
228
return null;
229
}
230
}
231
232
public static abstract class FooKey implements Key {
233
public String getFormat() { return null; }
234
public byte[] getEncoded() { return null; }
235
public String getAlgorithm() { return "foo"; }
236
}
237
238
public static class FooPrivateKey extends FooKey implements PrivateKey { }
239
240
public static class FooPublicKey extends FooKey implements PublicKey { }
241
242
public static class FooSignatureSpi extends BaseSignatureSpi {
243
public FooSignatureSpi() {
244
super();
245
System.out.println("FooSignatureSpi constructor");
246
}
247
}
248
249
public static class BazSignatureSpi extends BaseSignatureSpi {
250
public BazSignatureSpi() {
251
super();
252
System.out.println("BazSignatureSpi constructor");
253
}
254
protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
255
throw new InvalidKeyException("verify not supported");
256
}
257
}
258
259
public static class BazSignature extends BaseSignature {
260
public BazSignature() {
261
super("baz");
262
System.out.println("BazSignature constructor");
263
}
264
}
265
266
}
267
268