Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/ssl/spi/ProviderInit.java
41152 views
1
/*
2
* Copyright (c) 2002, 2011, 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 1.3 01/03/08
26
* @bug 4522550
27
* @summary SSLContext TrustMananagerFactory and KeyManagerFactory
28
* should throw if not init
29
* @run main/othervm ProviderInit
30
*
31
* SunJSSE does not support dynamic system properties, no way to re-use
32
* system properties in samevm/agentvm mode.
33
* @author Jaya Hangal
34
*/
35
36
/**
37
* This test case makes sure that the providers throw
38
* IllegalStateException when used without getting initialized.
39
* The relevant tests are in doClientSide method.
40
*/
41
42
import java.io.*;
43
import java.net.*;
44
import javax.net.ssl.*;
45
46
public class ProviderInit {
47
48
/*
49
* =============================================================
50
* Set the various variables needed for the tests, then
51
* specify what tests to run on each side.
52
*/
53
54
/*
55
* Should we run the client or server in a separate thread?
56
* Both sides can throw exceptions, but do you have a preference
57
* as to which side should be the main thread.
58
*/
59
static boolean separateServerThread = true;
60
61
/*
62
* Where do we find the keystores?
63
*/
64
static String pathToStores = "../../../../javax/net/ssl/etc";
65
static String keyStoreFile = "keystore";
66
static String trustStoreFile = "truststore";
67
static String passwd = "passphrase";
68
69
/*
70
* Is the server ready to serve?
71
*/
72
volatile static boolean serverReady = false;
73
74
/*
75
* Turn on SSL debugging?
76
*/
77
static boolean debug = false;
78
79
/*
80
* If the client or server is doing some kind of object creation
81
* that the other side depends on, and that thread prematurely
82
* exits, you may experience a hang. The test harness will
83
* terminate all hung threads after its timeout has expired,
84
* currently 3 minutes by default, but you might try to be
85
* smart about it....
86
*/
87
88
/*
89
* Define the server side of the test.
90
*
91
* If the server prematurely exits, serverReady will be set to true
92
* to avoid infinite hangs.
93
*/
94
void doServerSide() throws Exception {
95
96
SSLContext context = SSLContext.getInstance("TLS");
97
SSLServerSocketFactory sslssf =
98
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
99
SSLServerSocket sslServerSocket =
100
(SSLServerSocket) sslssf.createServerSocket(serverPort);
101
serverPort = sslServerSocket.getLocalPort();
102
/*
103
* Signal Client, we're ready for his connect.
104
*/
105
serverReady = true;
106
107
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
108
sslSocket.setNeedClientAuth(true);
109
InputStream sslIS = sslSocket.getInputStream();
110
OutputStream sslOS = sslSocket.getOutputStream();
111
112
sslIS.read();
113
sslOS.write(85);
114
sslOS.flush();
115
116
sslSocket.close();
117
}
118
119
/*
120
* Define the client side of the test.
121
*
122
* If the server prematurely exits, serverReady will be set to true
123
* to avoid infinite hangs.
124
*/
125
void doClientSide() throws Exception {
126
/*
127
* Wait for server to get started.
128
*/
129
while (!serverReady) {
130
Thread.sleep(50);
131
}
132
133
SSLContext context = SSLContext.getInstance("TLS");
134
SSLSocketFactory sslsf = null;
135
136
/*
137
* Try using SSLContext without calling init()
138
* A call to getSocketFactory() will throw an exception
139
*/
140
try {
141
sslsf =
142
(SSLSocketFactory) context.getSocketFactory();
143
communicate(sslsf, false);
144
} catch (IllegalStateException e) {
145
System.out.println("Caught the right exception" + e);
146
}
147
148
/*
149
* Try using TrustManagerFactory without calling init()
150
* A call to getTrustManagers() will throw an exception
151
*/
152
try {
153
TrustManagerFactory tmf = TrustManagerFactory.getInstance(
154
"sunX509");
155
TrustManager[] tms = tmf.getTrustManagers();
156
context.init(null, tms, null);
157
sslsf =
158
(SSLSocketFactory) context.getSocketFactory();
159
communicate(sslsf, false);
160
} catch (IllegalStateException e) {
161
System.out.println("Caught the right exception" + e);
162
}
163
164
/*
165
* Try using KeyManagerFactory without calling init()
166
* A call to getKeyManagers() will throw an exception
167
*/
168
try {
169
KeyManagerFactory kmf = KeyManagerFactory.getInstance("sunX509");
170
KeyManager kms[] = kmf.getKeyManagers();
171
context.init(kms, null, null);
172
sslsf =
173
(SSLSocketFactory) context.getSocketFactory();
174
communicate(sslsf, true);
175
} catch (Exception e) {
176
System.out.println("Caught the right exception" + e);
177
sslsf = (SSLSocketFactory) SSLSocketFactory.getDefault();
178
communicate(sslsf, false);
179
}
180
}
181
182
void communicate(SSLSocketFactory sslsf, boolean needClientAuth)
183
throws Exception {
184
185
SSLSocket sslSocket = (SSLSocket) sslsf.createSocket(
186
"localhost", serverPort);
187
sslSocket.setNeedClientAuth(needClientAuth);
188
InputStream sslIS = sslSocket.getInputStream();
189
OutputStream sslOS = sslSocket.getOutputStream();
190
191
sslOS.write(280);
192
sslOS.flush();
193
sslIS.read();
194
sslSocket.close();
195
}
196
197
/*
198
* =============================================================
199
* The remainder is just support stuff
200
*/
201
202
// use any free port by default
203
volatile int serverPort = 0;
204
205
volatile Exception serverException = null;
206
volatile Exception clientException = null;
207
208
public static void main(String[] args) throws Exception {
209
String keyFilename =
210
System.getProperty("test.src", "./") + "/" + pathToStores +
211
"/" + keyStoreFile;
212
String trustFilename =
213
System.getProperty("test.src", "./") + "/" + pathToStores +
214
"/" + trustStoreFile;
215
216
System.setProperty("javax.net.ssl.keyStore", keyFilename);
217
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
218
System.setProperty("javax.net.ssl.trustStore", trustFilename);
219
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
220
221
if (debug)
222
System.setProperty("javax.net.debug", "all");
223
224
/*
225
* Start the tests.
226
*/
227
new ProviderInit();
228
}
229
230
Thread clientThread = null;
231
Thread serverThread = null;
232
233
/*
234
* Primary constructor, used to drive remainder of the test.
235
*
236
* Fork off the other side, then do your work.
237
*/
238
ProviderInit() throws Exception {
239
if (separateServerThread) {
240
startServer(true);
241
startClient(false);
242
} else {
243
startClient(true);
244
startServer(false);
245
}
246
247
/*
248
* Wait for other side to close down.
249
*/
250
if (separateServerThread) {
251
serverThread.join();
252
} else {
253
clientThread.join();
254
}
255
256
/*
257
* When we get here, the test is pretty much over.
258
*
259
* If the main thread excepted, that propagates back
260
* immediately. If the other thread threw an exception, we
261
* should report back.
262
*/
263
if (serverException != null)
264
throw serverException;
265
if (clientException != null)
266
throw clientException;
267
}
268
269
void startServer(boolean newThread) throws Exception {
270
if (newThread) {
271
serverThread = new Thread() {
272
public void run() {
273
try {
274
doServerSide();
275
} catch (Exception e) {
276
/*
277
* Our server thread just died.
278
*
279
* Release the client, if not active already...
280
*/
281
System.err.println("Server died..." + e);
282
e.printStackTrace();
283
serverReady = true;
284
serverException = e;
285
}
286
}
287
};
288
serverThread.start();
289
} else {
290
doServerSide();
291
}
292
}
293
294
void startClient(boolean newThread) throws Exception {
295
if (newThread) {
296
clientThread = new Thread() {
297
public void run() {
298
try {
299
doClientSide();
300
} catch (Exception e) {
301
/*
302
* Our client thread just died.
303
*/
304
System.err.println("Client died...");
305
clientException = e;
306
}
307
}
308
};
309
clientThread.start();
310
} else {
311
doClientSide();
312
}
313
}
314
}
315
316