Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/SSLSocketCloseHang.java
41152 views
1
/*
2
* Copyright (c) 2017, 2020, 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 8184328 8253368 8260923
27
* @summary JDK8u131-b34-socketRead0 hang at SSL read
28
* @run main/othervm SSLSocketCloseHang TLSv1.2
29
* @run main/othervm SSLSocketCloseHang TLSv1.2 shutdownInput
30
* @run main/othervm SSLSocketCloseHang TLSv1.2 shutdownOutput
31
* @run main/othervm SSLSocketCloseHang TLSv1.3
32
* @run main/othervm SSLSocketCloseHang TLSv1.3 shutdownInput
33
* @run main/othervm SSLSocketCloseHang TLSv1.3 shutdownOutput
34
*/
35
36
37
import java.io.*;
38
import java.net.*;
39
import java.util.*;
40
import java.security.*;
41
import javax.net.ssl.*;
42
43
public class SSLSocketCloseHang {
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 trustStoreFile = "truststore";
63
static String passwd = "passphrase";
64
65
/*
66
* Is the server ready to serve?
67
*/
68
volatile static boolean serverReady = false;
69
70
/*
71
* Was the client responsible for closing the socket
72
*/
73
volatile static boolean clientClosed = false;
74
75
/*
76
* Turn on SSL debugging?
77
*/
78
static boolean debug = false;
79
80
static String socketCloseType;
81
82
/*
83
* If the client or server is doing some kind of object creation
84
* that the other side depends on, and that thread prematurely
85
* exits, you may experience a hang. The test harness will
86
* terminate all hung threads after its timeout has expired,
87
* currently 3 minutes by default, but you might try to be
88
* smart about it....
89
*/
90
91
/*
92
* Define the server side of the test.
93
*
94
* If the server prematurely exits, serverReady will be set to true
95
* to avoid infinite hangs.
96
*/
97
void doServerSide() throws Exception {
98
SSLServerSocketFactory sslssf =
99
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
100
SSLServerSocket sslServerSocket =
101
(SSLServerSocket) sslssf.createServerSocket(serverPort);
102
103
serverPort = sslServerSocket.getLocalPort();
104
105
/*
106
* Signal Client, we're ready for his connect.
107
*/
108
serverReady = true;
109
110
System.err.println("Server accepting: " + System.nanoTime());
111
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
112
System.err.println("Server accepted: " + System.nanoTime());
113
sslSocket.startHandshake();
114
System.err.println("Server handshake complete: " + System.nanoTime());
115
while (!clientClosed) {
116
Thread.sleep(500);
117
}
118
}
119
120
/*
121
* Define the client side of the test.
122
*
123
* If the server prematurely exits, serverReady will be set to true
124
* to avoid infinite hangs.
125
*/
126
void doClientSide() throws Exception {
127
boolean caught = false;
128
129
/*
130
* Wait for server to get started.
131
*/
132
System.out.println("waiting on server");
133
while (!serverReady) {
134
Thread.sleep(50);
135
}
136
Thread.sleep(500);
137
System.out.println("server ready");
138
139
Socket baseSocket = new Socket("localhost", serverPort);
140
baseSocket.setSoTimeout(1000);
141
142
SSLSocketFactory sslsf =
143
(SSLSocketFactory) SSLSocketFactory.getDefault();
144
SSLSocket sslSocket = (SSLSocket)
145
sslsf.createSocket(baseSocket, "localhost", serverPort, false);
146
147
// handshaking
148
System.err.println("Client starting handshake: " + System.nanoTime());
149
sslSocket.startHandshake();
150
System.err.println("Client handshake done: " + System.nanoTime());
151
152
Thread.sleep(500);
153
System.err.println("Client closing: " + System.nanoTime());
154
155
closeConnection(sslSocket);
156
157
clientClosed = true;
158
System.err.println("Client closed: " + System.nanoTime());
159
}
160
161
private void closeConnection(SSLSocket sslSocket) throws IOException {
162
if ("shutdownInput".equals(socketCloseType)) {
163
shutdownInput(sslSocket);
164
// second call to shutdownInput() should just return,
165
// shouldn't throw any exception
166
sslSocket.shutdownInput();
167
// invoking shutdownOutput() just after shutdownInput()
168
sslSocket.shutdownOutput();
169
} else if ("shutdownOutput".equals(socketCloseType)) {
170
sslSocket.shutdownOutput();
171
// second call to shutdownInput() should just return,
172
// shouldn't throw any exception
173
sslSocket.shutdownOutput();
174
// invoking shutdownInput() just after shutdownOutput()
175
shutdownInput(sslSocket);
176
} else {
177
sslSocket.close();
178
}
179
}
180
181
private void shutdownInput(SSLSocket sslSocket) throws IOException {
182
try {
183
sslSocket.shutdownInput();
184
} catch (SSLException e) {
185
if (!e.getMessage().contains
186
("closing inbound before receiving peer's close_notify")) {
187
throw new RuntimeException("expected different exception "
188
+ "message. " + e.getMessage());
189
}
190
}
191
if (!sslSocket.getSession().isValid()) {
192
throw new RuntimeException("expected session to remain valid");
193
}
194
}
195
196
/*
197
* =============================================================
198
* The remainder is just support stuff
199
*/
200
201
// use any free port by default
202
volatile int serverPort = 0;
203
204
volatile Exception serverException = null;
205
volatile Exception clientException = null;
206
207
volatile byte[] serverDigest = null;
208
209
public static void main(String[] args) throws Exception {
210
String keyFilename =
211
System.getProperty("test.src", "./") + "/" + pathToStores +
212
"/" + keyStoreFile;
213
String trustFilename =
214
System.getProperty("test.src", "./") + "/" + pathToStores +
215
"/" + trustStoreFile;
216
217
System.setProperty("javax.net.ssl.keyStore", keyFilename);
218
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
219
System.setProperty("javax.net.ssl.trustStore", trustFilename);
220
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
221
System.setProperty("jdk.tls.client.protocols", args[0]);
222
223
if (debug)
224
System.setProperty("javax.net.debug", "all");
225
226
socketCloseType = args.length > 1 ? args[1] : "";
227
228
229
/*
230
* Start the tests.
231
*/
232
new SSLSocketCloseHang();
233
}
234
235
Thread clientThread = null;
236
Thread serverThread = null;
237
238
/*
239
* Primary constructor, used to drive remainder of the test.
240
*
241
* Fork off the other side, then do your work.
242
*/
243
SSLSocketCloseHang() throws Exception {
244
if (separateServerThread) {
245
startServer(true);
246
startClient(false);
247
} else {
248
startClient(true);
249
startServer(false);
250
}
251
252
/*
253
* Wait for other side to close down.
254
*/
255
if (separateServerThread) {
256
serverThread.join();
257
} else {
258
clientThread.join();
259
}
260
261
/*
262
* When we get here, the test is pretty much over.
263
*
264
* If the main thread excepted, that propagates back
265
* immediately. If the other thread threw an exception, we
266
* should report back.
267
*/
268
if (serverException != null) {
269
System.out.print("Server Exception:");
270
throw serverException;
271
}
272
if (clientException != null) {
273
System.out.print("Client Exception:");
274
throw clientException;
275
}
276
}
277
278
void startServer(boolean newThread) throws Exception {
279
if (newThread) {
280
serverThread = new Thread() {
281
public void run() {
282
try {
283
doServerSide();
284
} catch (Exception e) {
285
/*
286
* Our server thread just died.
287
*
288
* Release the client, if not active already...
289
*/
290
System.err.println("Server died...");
291
System.err.println(e);
292
serverReady = true;
293
serverException = e;
294
}
295
}
296
};
297
serverThread.start();
298
} else {
299
doServerSide();
300
}
301
}
302
303
void startClient(boolean newThread) throws Exception {
304
if (newThread) {
305
clientThread = new Thread() {
306
public void run() {
307
try {
308
doClientSide();
309
} catch (Exception e) {
310
/*
311
* Our client thread just died.
312
*/
313
System.err.println("Client died...");
314
clientException = e;
315
}
316
}
317
};
318
clientThread.start();
319
} else {
320
doClientSide();
321
}
322
}
323
}
324
325