Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/ssl/AppInputStream/ReadBlocksClose.java
41152 views
1
/*
2
* Copyright (c) 2003, 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
// 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 4814140
32
* @summary AppInputStream: read can block a close
33
* @run main/othervm -Djdk.tls.acknowledgeCloseNotify=true ReadBlocksClose
34
* @author Brad Wetmore
35
*/
36
37
import java.io.*;
38
import java.net.*;
39
import javax.net.ssl.*;
40
41
public class ReadBlocksClose {
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
try {
107
sslIS.read();
108
} catch (IOException e) {
109
// this is ok, we expect this to time out anyway if the bug
110
// is not fixed. This is just to make sure that we
111
// don't inadvertantly fail.
112
}
113
114
sslSocket.close();
115
}
116
117
/*
118
* Define the client side of the test.
119
*
120
* If the server prematurely exits, serverReady will be set to true
121
* to avoid infinite hangs.
122
*/
123
void doClientSide() throws Exception {
124
125
/*
126
* Wait for server to get started.
127
*/
128
while (!serverReady) {
129
Thread.sleep(50);
130
}
131
132
SSLSocketFactory sslsf =
133
(SSLSocketFactory) SSLSocketFactory.getDefault();
134
SSLSocket sslSocket = (SSLSocket)
135
sslsf.createSocket("localhost", serverPort);
136
137
final InputStream sslIS = sslSocket.getInputStream();
138
final OutputStream sslOS = sslSocket.getOutputStream();
139
140
new Thread(new Runnable() {
141
public void run() {
142
try {
143
System.out.println("Closing Thread started");
144
Thread.sleep(3000);
145
System.out.println("Closing Thread closing");
146
sslOS.close();
147
System.out.println("Closing Thread closed");
148
} catch (Exception e) {
149
RuntimeException rte =
150
new RuntimeException("Check this out");
151
rte.initCause(e);
152
throw rte;
153
}
154
}
155
}).start();
156
157
try {
158
/*
159
* This should timeout and fail the test
160
*/
161
System.out.println("Client starting read");
162
sslIS.read();
163
} catch (IOException e) {
164
// this is ok, we expect this to time out anyway if the bug
165
// is not fixed. This is just to make sure that we
166
// don't inadvertantly fail.
167
}
168
169
sslSocket.close();
170
}
171
172
/*
173
* =============================================================
174
* The remainder is just support stuff
175
*/
176
177
// use any free port by default
178
volatile int serverPort = 0;
179
180
volatile Exception serverException = null;
181
volatile Exception clientException = null;
182
183
public static void main(String[] args) throws Exception {
184
String keyFilename =
185
System.getProperty("test.src", "./") + "/" + pathToStores +
186
"/" + keyStoreFile;
187
String trustFilename =
188
System.getProperty("test.src", "./") + "/" + pathToStores +
189
"/" + trustStoreFile;
190
191
System.setProperty("javax.net.ssl.keyStore", keyFilename);
192
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
193
System.setProperty("javax.net.ssl.trustStore", trustFilename);
194
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
195
196
if (debug)
197
System.setProperty("javax.net.debug", "all");
198
199
/*
200
* Start the tests.
201
*/
202
new ReadBlocksClose();
203
}
204
205
Thread clientThread = null;
206
Thread serverThread = null;
207
208
/*
209
* Primary constructor, used to drive remainder of the test.
210
*
211
* Fork off the other side, then do your work.
212
*/
213
ReadBlocksClose() throws Exception {
214
try {
215
if (separateServerThread) {
216
startServer(true);
217
startClient(false);
218
} else {
219
startClient(true);
220
startServer(false);
221
}
222
} catch (Exception e) {
223
// swallow for now. Show later
224
}
225
226
/*
227
* Wait for other side to close down.
228
*/
229
if (separateServerThread) {
230
serverThread.join();
231
} else {
232
clientThread.join();
233
}
234
235
/*
236
* When we get here, the test is pretty much over.
237
* Which side threw the error?
238
*/
239
Exception local;
240
Exception remote;
241
String whichRemote;
242
243
if (separateServerThread) {
244
remote = serverException;
245
local = clientException;
246
whichRemote = "server";
247
} else {
248
remote = clientException;
249
local = serverException;
250
whichRemote = "client";
251
}
252
253
/*
254
* If both failed, return the curthread's exception, but also
255
* print the remote side Exception
256
*/
257
if ((local != null) && (remote != null)) {
258
System.out.println(whichRemote + " also threw:");
259
remote.printStackTrace();
260
System.out.println();
261
throw local;
262
}
263
264
if (remote != null) {
265
throw remote;
266
}
267
268
if (local != null) {
269
throw local;
270
}
271
}
272
273
void startServer(boolean newThread) throws Exception {
274
if (newThread) {
275
serverThread = new Thread() {
276
public void run() {
277
try {
278
doServerSide();
279
} catch (Exception e) {
280
/*
281
* Our server thread just died.
282
*
283
* Release the client, if not active already...
284
*/
285
System.err.println("Server died...");
286
serverReady = true;
287
serverException = e;
288
}
289
}
290
};
291
serverThread.start();
292
} else {
293
try {
294
doServerSide();
295
} catch (Exception e) {
296
serverException = e;
297
} finally {
298
serverReady = true;
299
}
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
try {
321
doClientSide();
322
} catch (Exception e) {
323
clientException = e;
324
}
325
}
326
}
327
}
328
329