Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ssl/SSLContextImpl/BadTSProvider.java
38853 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
// SunJSSE does not support dynamic system properties, no way to re-use
26
// system properties in samevm/agentvm mode.
27
//
28
29
/*
30
* @test
31
* @bug 4919147
32
* @summary Support for token-based KeyStores
33
* @run main/othervm BadTSProvider
34
*/
35
36
import java.io.*;
37
import java.net.*;
38
import java.security.*;
39
import javax.net.ssl.*;
40
41
public class BadTSProvider {
42
43
/*
44
* =============================================================
45
* Set the various variables needed for the tests, then
46
* specify what tests to run on each side.
47
*/
48
49
/*
50
* Should we run the client or server in a separate thread?
51
* Both sides can throw exceptions, but do you have a preference
52
* as to which side should be the main thread.
53
*/
54
static boolean separateServerThread = false;
55
56
/*
57
* Where do we find the keystores?
58
*/
59
static String pathToStores = "../../../../javax/net/ssl/etc";
60
static String keyStoreFile = "keystore";
61
static String trustStoreFile = "truststore";
62
static String passwd = "passphrase";
63
64
/*
65
* Is the server ready to serve?
66
*/
67
volatile static boolean serverReady = false;
68
69
/*
70
* Turn on SSL debugging?
71
*/
72
static boolean debug = false;
73
74
/*
75
* If the client or server is doing some kind of object creation
76
* that the other side depends on, and that thread prematurely
77
* exits, you may experience a hang. The test harness will
78
* terminate all hung threads after its timeout has expired,
79
* currently 3 minutes by default, but you might try to be
80
* smart about it....
81
*/
82
83
/*
84
* Define the server side of the test.
85
*
86
* If the server prematurely exits, serverReady will be set to true
87
* to avoid infinite hangs.
88
*/
89
void doServerSide() throws Exception {
90
SSLServerSocketFactory sslssf =
91
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
92
SSLServerSocket sslServerSocket =
93
(SSLServerSocket) sslssf.createServerSocket(serverPort);
94
95
serverPort = sslServerSocket.getLocalPort();
96
97
/*
98
* Signal Client, we're ready for his connect.
99
*/
100
serverReady = true;
101
102
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
103
InputStream sslIS = sslSocket.getInputStream();
104
OutputStream sslOS = sslSocket.getOutputStream();
105
106
sslIS.read();
107
sslOS.write(85);
108
sslOS.flush();
109
110
sslSocket.close();
111
}
112
113
/*
114
* Define the client side of the test.
115
*
116
* If the server prematurely exits, serverReady will be set to true
117
* to avoid infinite hangs.
118
*/
119
void doClientSide() throws Exception {
120
121
/*
122
* Wait for server to get started.
123
*/
124
while (!serverReady) {
125
Thread.sleep(50);
126
}
127
128
SSLSocketFactory sslsf =
129
(SSLSocketFactory) SSLSocketFactory.getDefault();
130
SSLSocket sslSocket = (SSLSocket)
131
sslsf.createSocket("localhost", serverPort);
132
133
InputStream sslIS = sslSocket.getInputStream();
134
OutputStream sslOS = sslSocket.getOutputStream();
135
136
sslOS.write(280);
137
sslOS.flush();
138
sslIS.read();
139
140
sslSocket.close();
141
}
142
143
/*
144
* =============================================================
145
* The remainder is just support stuff
146
*/
147
148
// use any free port by default
149
volatile int serverPort = 0;
150
151
volatile Exception serverException = null;
152
volatile Exception clientException = null;
153
154
public static void main(String[] args) throws Exception {
155
String keyFilename =
156
System.getProperty("test.src", "./") + "/" + pathToStores +
157
"/" + keyStoreFile;
158
String trustFilename =
159
System.getProperty("test.src", "./") + "/" + pathToStores +
160
"/" + trustStoreFile;
161
162
// first test a good provider name
163
164
System.setProperty("javax.net.ssl.keyStore", keyFilename);
165
System.setProperty("javax.net.ssl.keyStoreProvider", "SUN");
166
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
167
System.setProperty("javax.net.ssl.trustStore", trustFilename);
168
System.setProperty("javax.net.ssl.trustStoreProvider", "BAD-PROVIDER");
169
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
170
171
if (debug)
172
System.setProperty("javax.net.debug", "ssl,defaultctx");
173
174
try {
175
new BadTSProvider();
176
throw new SecurityException("expected no-such-provider exception");
177
} catch (SocketException se) {
178
179
// catching the exception is ok,
180
// but let's try to confirm it is the right exception.
181
//
182
// XXX this test must be updated if the exception message changes
183
184
Throwable cause = se.getCause();
185
if (!(cause instanceof NoSuchAlgorithmException)) {
186
se.printStackTrace();
187
throw new Exception("Unexpected exception" + se);
188
}
189
190
cause = cause.getCause();
191
if (!(cause instanceof KeyStoreException)) {
192
se.printStackTrace();
193
throw new Exception("Unexpected exception" + se);
194
}
195
196
cause = cause.getCause();
197
if (!(cause instanceof NoSuchProviderException)) {
198
se.printStackTrace();
199
throw new Exception("Unexpected exception" + se);
200
}
201
202
System.out.println("OK");
203
}
204
}
205
206
Thread clientThread = null;
207
Thread serverThread = null;
208
209
/*
210
* Primary constructor, used to drive remainder of the test.
211
*
212
* Fork off the other side, then do your work.
213
*/
214
BadTSProvider() throws Exception {
215
try {
216
if (separateServerThread) {
217
startServer(true);
218
startClient(false);
219
} else {
220
startClient(true);
221
startServer(false);
222
}
223
} catch (Exception e) {
224
//swallow for now. Show later
225
}
226
227
/*
228
* Wait for other side to close down.
229
*/
230
if (separateServerThread) {
231
serverThread.join();
232
} else {
233
clientThread.join();
234
}
235
236
/*
237
* When we get here, the test is pretty much over.
238
* Which side threw the error?
239
*/
240
Exception local;
241
Exception remote;
242
String whichRemote;
243
244
if (separateServerThread) {
245
remote = serverException;
246
local = clientException;
247
whichRemote = "server";
248
} else {
249
remote = clientException;
250
local = serverException;
251
whichRemote = "client";
252
}
253
254
/*
255
* If both failed, return the curthread's exception, but also
256
* print the remote side Exception
257
*/
258
if ((local != null) && (remote != null)) {
259
System.out.println(whichRemote + " also threw:");
260
//remote.printStackTrace();
261
System.out.println();
262
throw local;
263
}
264
265
if (remote != null) {
266
throw remote;
267
}
268
269
if (local != null) {
270
throw local;
271
}
272
}
273
274
void startServer(boolean newThread) throws Exception {
275
if (newThread) {
276
serverThread = new Thread() {
277
public void run() {
278
try {
279
doServerSide();
280
} catch (Exception e) {
281
/*
282
* Our server thread just died.
283
*
284
* Release the client, if not active already...
285
*/
286
System.err.println("Server died...");
287
serverReady = true;
288
serverException = e;
289
}
290
}
291
};
292
serverThread.start();
293
} else {
294
try {
295
doServerSide();
296
} catch (Exception e) {
297
serverException = e;
298
} finally {
299
serverReady = true;
300
}
301
}
302
}
303
304
void startClient(boolean newThread) throws Exception {
305
if (newThread) {
306
clientThread = new Thread() {
307
public void run() {
308
try {
309
doClientSide();
310
} catch (Exception e) {
311
/*
312
* Our client thread just died.
313
*/
314
System.err.println("Client died...");
315
clientException = e;
316
}
317
}
318
};
319
clientThread.start();
320
} else {
321
try {
322
doClientSide();
323
} catch (Exception e) {
324
clientException = e;
325
}
326
}
327
}
328
}
329
330