Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/SSLSocketImplThrowsWrongExceptions.java
41152 views
1
/*
2
* Copyright (c) 2001, 2021, 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 4361124 4325806
32
* @summary SSLServerSocket isn't throwing exceptions when negotiations are
33
* failing & java.net.SocketException: occures in Auth and clientmode
34
* @run main/othervm SSLSocketImplThrowsWrongExceptions
35
* @author Brad Wetmore
36
*/
37
38
import java.net.SocketException;
39
import java.util.concurrent.CountDownLatch;
40
import javax.net.ssl.*;
41
42
public class SSLSocketImplThrowsWrongExceptions {
43
44
/*
45
* =============================================================
46
* Set the various variables needed for the tests, then
47
* specify what tests to run on each side.
48
*/
49
50
/*
51
* Should we run the client or server in a separate thread?
52
* Both sides can throw exceptions, but do you have a preference
53
* as to which side should be the main thread.
54
*/
55
static boolean separateServerThread = true;
56
57
/*
58
* Where do we find the keystores?
59
*/
60
static String pathToStores = "../../../../javax/net/ssl/etc";
61
static String keyStoreFile = "keystore";
62
static String passwd = "passphrase";
63
64
/*
65
* Is the server ready to serve?
66
*/
67
private CountDownLatch serverReadyLatch = new CountDownLatch(1);
68
69
/*
70
* Turn on SSL debugging?
71
*/
72
static boolean debug = false;
73
74
75
/*
76
* Define the server side of the test.
77
*/
78
void doServerSide() throws Exception {
79
System.out.println("starting Server");
80
SSLServerSocketFactory sslssf =
81
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
82
SSLServerSocket sslServerSocket =
83
(SSLServerSocket) sslssf.createServerSocket(serverPort);
84
serverPort = sslServerSocket.getLocalPort();
85
System.out.println("got server socket");
86
87
/*
88
* Signal Client, we're ready for his connect.
89
*/
90
serverReadyLatch.countDown();
91
92
try {
93
System.out.println("Server socket accepting...");
94
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
95
System.out.println("Server starting handshake");
96
sslSocket.startHandshake();
97
throw new Exception("Handshake was successful");
98
} catch (SSLException | SocketException se){
99
/*
100
* Caught the right Exception. Swallow it.
101
*/
102
System.out.println("Server reported the right exception");
103
System.out.println(se);
104
} catch (Exception e) {
105
/*
106
* Caught the wrong exception. Rethrow it.
107
*/
108
System.out.println("Server reported the wrong exception");
109
throw e;
110
}
111
112
}
113
114
/*
115
* Define the client side of the test.
116
*/
117
void doClientSide() throws Exception {
118
119
System.out.println(" Client starting");
120
121
/*
122
* Wait for server to get started.
123
*/
124
serverReadyLatch.await();
125
126
SSLSocketFactory sslsf =
127
(SSLSocketFactory) SSLSocketFactory.getDefault();
128
try {
129
System.out.println(" Client creating socket");
130
SSLSocket sslSocket = (SSLSocket)
131
sslsf.createSocket("localhost", serverPort);
132
System.out.println(" Client starting handshake");
133
sslSocket.startHandshake();
134
throw new Exception("Handshake was successful");
135
} catch (SSLException e) {
136
/*
137
* Caught the right Exception. Swallow it.
138
*/
139
System.out.println(" Client reported correct exception");
140
System.out.println(" " + e.toString());
141
} catch (Exception e) {
142
/*
143
* Caught the wrong exception. Rethrow it.
144
*/
145
System.out.println(" Client reported the wrong exception");
146
throw e;
147
}
148
}
149
150
/*
151
* =============================================================
152
* The remainder is just support stuff
153
*/
154
155
// use any free port by default
156
volatile int serverPort = 0;
157
158
volatile Exception serverException = null;
159
volatile Exception clientException = null;
160
161
public static void main(String[] args) throws Exception {
162
String keyFilename =
163
System.getProperty("test.src", "./") + "/" + pathToStores +
164
"/" + keyStoreFile;
165
166
System.setProperty("javax.net.ssl.keyStore", keyFilename);
167
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
168
169
if (debug)
170
System.setProperty("javax.net.debug", "all");
171
172
/*
173
* Start the tests.
174
*/
175
new SSLSocketImplThrowsWrongExceptions();
176
}
177
178
Thread clientThread = null;
179
Thread serverThread = null;
180
181
/*
182
* Primary constructor, used to drive remainder of the test.
183
*
184
* Fork off the other side, then do your work.
185
*/
186
SSLSocketImplThrowsWrongExceptions () throws Exception {
187
Exception startException = null;
188
try {
189
if (separateServerThread) {
190
startServer(true);
191
startClient(false);
192
} else {
193
startClient(true);
194
startServer(false);
195
}
196
} catch (Exception e) {
197
startException = e;
198
}
199
200
/*
201
* Wait for other side to close down.
202
*/
203
if (separateServerThread) {
204
if (serverThread != null) {
205
serverThread.join();
206
}
207
} else {
208
if (clientThread != null) {
209
clientThread.join();
210
}
211
}
212
213
/*
214
* When we get here, the test is pretty much over.
215
* Which side threw the error?
216
*/
217
Exception local;
218
Exception remote;
219
220
if (separateServerThread) {
221
remote = serverException;
222
local = clientException;
223
} else {
224
remote = clientException;
225
local = serverException;
226
}
227
228
Exception exception = null;
229
230
/*
231
* Check various exception conditions.
232
*/
233
if ((local != null) && (remote != null)) {
234
// If both failed, return the curthread's exception.
235
local.initCause(remote);
236
exception = local;
237
} else if (local != null) {
238
exception = local;
239
} else if (remote != null) {
240
exception = remote;
241
} else if (startException != null) {
242
exception = startException;
243
}
244
245
/*
246
* If there was an exception *AND* a startException,
247
* output it.
248
*/
249
if (exception != null) {
250
if (exception != startException && startException != null) {
251
exception.addSuppressed(startException);
252
}
253
throw exception;
254
}
255
256
// Fall-through: no exception to throw!
257
}
258
259
void startServer(boolean newThread) throws Exception {
260
if (newThread) {
261
serverThread = new Thread() {
262
public void run() {
263
try {
264
doServerSide();
265
} catch (Exception e) {
266
/*
267
* Our server thread just died.
268
*
269
* Release the client, if not active already...
270
*/
271
System.err.println("Server died...");
272
serverReadyLatch.countDown();
273
serverException = e;
274
}
275
}
276
};
277
serverThread.start();
278
} else {
279
try {
280
doServerSide();
281
} catch (Exception e) {
282
serverException = e;
283
} finally {
284
serverReadyLatch.countDown();
285
}
286
}
287
}
288
289
void startClient(boolean newThread) throws Exception {
290
if (newThread) {
291
clientThread = new Thread() {
292
public void run() {
293
try {
294
doClientSide();
295
} catch (Exception e) {
296
/*
297
* Our client thread just died.
298
*/
299
System.err.println("Client died...");
300
clientException = e;
301
}
302
}
303
};
304
clientThread.start();
305
} else {
306
try {
307
doClientSide();
308
} catch (Exception e) {
309
clientException = e;
310
}
311
}
312
}
313
}
314
315