Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/cert/PKIXRevocationChecker/UnitTest.java
41153 views
1
/*
2
* Copyright (c) 2012, 2013, 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 6854712 7171570 8010748 8025287
27
* @summary Basic unit test for PKIXRevocationChecker
28
*/
29
30
import java.io.ByteArrayInputStream;
31
import java.io.InputStream;
32
import java.io.IOException;
33
import java.io.OutputStream;
34
import java.net.URI;
35
import java.security.cert.*;
36
import java.security.cert.PKIXRevocationChecker.Option;
37
import java.util.*;
38
39
public class UnitTest {
40
41
public static void main(String[] args) throws Exception {
42
43
CertPathValidator cpv = CertPathValidator.getInstance("PKIX");
44
CertPathChecker cpc = cpv.getRevocationChecker();
45
PKIXRevocationChecker prc = (PKIXRevocationChecker)cpc;
46
47
prc.init(false);
48
49
System.out.println("Testing that get methods return null or " +
50
"empty lists/sets/maps");
51
requireNull(prc.getOcspResponder(), "getOcspResponder()");
52
requireNull(prc.getOcspResponderCert(), "getOcspResponderCert()");
53
requireEmpty(prc.getOcspExtensions(), "getOcspExtensions()");
54
requireEmpty(prc.getOcspResponses(), "getOcspResponses()");
55
requireEmpty(prc.getOptions(), "getOptions()");
56
requireEmpty(prc.getSoftFailExceptions(), "getSoftFailExceptions()");
57
58
System.out.println("Testing that get methods return same parameters " +
59
"that are passed to set methods");
60
URI uri = new URI("http://localhost");
61
prc.setOcspResponder(uri);
62
requireEquals(uri, prc.getOcspResponder(), "getOcspResponder()");
63
64
X509Certificate cert = getCert();
65
prc.setOcspResponderCert(cert);
66
requireEquals(cert, prc.getOcspResponderCert(),
67
"getOcspResponderCert()");
68
69
List<Extension> exts = new ArrayList<>();
70
for (String oid : cert.getNonCriticalExtensionOIDs()) {
71
System.out.println(oid);
72
exts.add(new ExtensionImpl(oid,
73
cert.getExtensionValue(oid), false));
74
}
75
prc.setOcspExtensions(exts);
76
requireEquals(exts, prc.getOcspExtensions(), "getOcspExtensions()");
77
78
Set<Option> options = EnumSet.of(Option.ONLY_END_ENTITY);
79
prc.setOptions(options);
80
requireEquals(options, prc.getOptions(), "getOptions()");
81
82
System.out.println("Testing that parameters are re-initialized to " +
83
"default values if null is passed to set methods");
84
prc.setOcspResponder(null);
85
requireNull(prc.getOcspResponder(), "getOcspResponder()");
86
prc.setOcspResponderCert(null);
87
requireNull(prc.getOcspResponderCert(), "getOcspResponderCert()");
88
prc.setOcspExtensions(null);
89
requireEmpty(prc.getOcspExtensions(), "getOcspExtensions()");
90
prc.setOcspResponses(null);
91
requireEmpty(prc.getOcspResponses(), "getOcspResponses()");
92
prc.setOptions(null);
93
requireEmpty(prc.getOptions(), "getOptions()");
94
95
System.out.println("Testing that getRevocationChecker returns new " +
96
"instance each time");
97
CertPathChecker first = cpv.getRevocationChecker();
98
CertPathChecker second = cpv.getRevocationChecker();
99
if (first == second) {
100
throw new Exception("FAILED: CertPathCheckers not new instances");
101
}
102
CertPathBuilder cpb = CertPathBuilder.getInstance("PKIX");
103
first = cpb.getRevocationChecker();
104
second = cpb.getRevocationChecker();
105
if (first == second) {
106
throw new Exception("FAILED: CertPathCheckers not new instances");
107
}
108
}
109
110
static void requireNull(Object o, String msg) throws Exception {
111
if (o != null) {
112
throw new Exception("FAILED: " + msg + " must return null");
113
}
114
}
115
116
static void requireEmpty(Map<?,?> m, String msg) throws Exception {
117
if (!m.isEmpty()) {
118
throw new Exception("FAILED: " + msg + " must return an empty map");
119
}
120
}
121
122
static void requireEmpty(List<?> l, String msg) throws Exception {
123
if (!l.isEmpty()) {
124
throw new Exception("FAILED: " + msg +" must return an empty list");
125
}
126
}
127
128
static void requireEmpty(Set<?> s, String msg) throws Exception {
129
if (!s.isEmpty()) {
130
throw new Exception("FAILED: " + msg + " must return an empty set");
131
}
132
}
133
134
static void requireEquals(Object a, Object b, String msg) throws Exception {
135
if (!a.equals(b)) {
136
throw new Exception("FAILED: " + msg + " does not return the " +
137
"same object that was set");
138
}
139
}
140
141
static X509Certificate getCert() throws Exception {
142
String b64 =
143
"-----BEGIN CERTIFICATE-----\n" +
144
"MIIBLTCB2KADAgECAgEDMA0GCSqGSIb3DQEBBAUAMA0xCzAJBgNVBAMTAkNBMB4X\n" +
145
"DTAyMTEwNzExNTcwM1oXDTIyMTEwNzExNTcwM1owFTETMBEGA1UEAxMKRW5kIEVu\n" +
146
"dGl0eTBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQDVBDfF+uBr5s5jzzDs1njKlZNt\n" +
147
"h8hHzEt3ASh67Peos+QrDzgpUyFXT6fdW2h7iPf0ifjM8eW2xa+3EnPjjU5jAgMB\n" +
148
"AAGjGzAZMBcGA1UdIAQQMA4wBgYEVR0gADAEBgIqADANBgkqhkiG9w0BAQQFAANB\n" +
149
"AFo//WOboCNOCcA1fvcWW9oc4MvV8ZPvFIAbyEbgyFd4id5lGDTRbRPvvNZRvdsN\n" +
150
"NM2gXYr+f87NHIXc9EF3pzw=\n" +
151
"-----END CERTIFICATE-----";
152
153
InputStream is = new ByteArrayInputStream(b64.getBytes("UTF-8"));
154
CertificateFactory cf = CertificateFactory.getInstance("X.509");
155
return (X509Certificate)cf.generateCertificate(is);
156
}
157
158
static class ExtensionImpl implements Extension {
159
private final String oid;
160
private final byte[] val;
161
private final boolean critical;
162
163
ExtensionImpl(String oid, byte[] val, boolean critical) {
164
this.oid = oid;
165
this.val = val;
166
this.critical = critical;
167
}
168
169
public void encode(OutputStream out) throws IOException {
170
throw new UnsupportedOperationException();
171
}
172
173
public String getId() {
174
return oid;
175
}
176
177
public byte[] getValue() {
178
return val.clone();
179
}
180
181
public boolean isCritical() {
182
return critical;
183
}
184
}
185
}
186
187