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/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java
38889 views
1
/*
2
* Copyright (c) 2003, 2018, 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 4811482 4700777 4905410
32
* @summary sun.net.client.defaultConnectTimeout should work with
33
* HttpsURLConnection; HTTP client: Connect and read timeouts;
34
* Https needs to support new tiger features that went into http
35
* @run main/othervm ReadTimeout
36
*/
37
38
import java.io.*;
39
import java.net.*;
40
import javax.net.ssl.*;
41
42
public class ReadTimeout {
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 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
* Turn on SSL debugging?
72
*/
73
static boolean debug = false;
74
75
/*
76
* Message posted
77
*/
78
static String postMsg = "Testing HTTP post on a https server";
79
80
/*
81
* If the client or server is doing some kind of object creation
82
* that the other side depends on, and that thread prematurely
83
* exits, you may experience a hang. The test harness will
84
* terminate all hung threads after its timeout has expired,
85
* currently 3 minutes by default, but you might try to be
86
* smart about it....
87
*/
88
89
/*
90
* Define the server side of the test.
91
*
92
* If the server prematurely exits, serverReady will be set to true
93
* to avoid infinite hangs.
94
*/
95
void doServerSide() throws Exception {
96
SSLServerSocketFactory sslssf =
97
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
98
SSLServerSocket sslServerSocket =
99
(SSLServerSocket) sslssf.createServerSocket(serverPort);
100
serverPort = sslServerSocket.getLocalPort();
101
102
/*
103
* Signal Client, we're ready for his connect.
104
*/
105
serverReady = true;
106
try {
107
try (SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept()) {
108
InputStream sslIS = sslSocket.getInputStream();
109
BufferedReader br =
110
new BufferedReader(new InputStreamReader(sslIS));
111
br.readLine();
112
while (!finished()) {
113
Thread.sleep(2000);
114
}
115
}
116
117
reset();
118
// doing second test
119
try (SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept()) {
120
InputStream sslIS = sslSocket.getInputStream();
121
BufferedReader br =
122
new BufferedReader(new InputStreamReader(sslIS));
123
br.readLine();
124
while (!finished()) {
125
Thread.sleep(2000);
126
}
127
}
128
} catch (Exception e) {
129
System.out.println("Should be an expected exception: " + e);
130
} finally {
131
sslServerSocket.close();
132
}
133
}
134
135
boolean isFinished = false;
136
137
synchronized boolean finished () {
138
return (isFinished);
139
}
140
synchronized void done () {
141
isFinished = true;
142
}
143
144
synchronized void reset() {
145
isFinished = false;
146
}
147
148
/*
149
* Define the client side of the test.
150
*
151
* If the server prematurely exits, serverReady will be set to true
152
* to avoid infinite hangs.
153
*/
154
void doClientSide() throws Exception {
155
HostnameVerifier reservedHV =
156
HttpsURLConnection.getDefaultHostnameVerifier();
157
try {
158
/*
159
* Wait for server to get started.
160
*/
161
while (!serverReady) {
162
Thread.sleep(50);
163
}
164
HttpsURLConnection http = null;
165
try {
166
URL url = new URL("https://localhost:" + serverPort);
167
168
// set read timeout through system property
169
System.setProperty("sun.net.client.defaultReadTimeout", "2000");
170
HttpsURLConnection.setDefaultHostnameVerifier(
171
new NameVerifier());
172
http = (HttpsURLConnection)url.openConnection();
173
174
InputStream is = http.getInputStream();
175
176
throw new Exception(
177
"system property timeout configuration does not work");
178
} catch (SSLException | SocketTimeoutException ex) {
179
System.out.println("Got expected timeout exception for " +
180
"system property timeout configuration: " + getCause(ex));
181
} finally {
182
done();
183
http.disconnect();
184
}
185
186
try {
187
URL url = new URL("https://localhost:" + serverPort);
188
189
HttpsURLConnection.setDefaultHostnameVerifier(
190
new NameVerifier());
191
http = (HttpsURLConnection)url.openConnection();
192
// set read timeout through API
193
http.setReadTimeout(2000);
194
195
InputStream is = http.getInputStream();
196
197
throw new Exception(
198
"HttpsURLConnection.setReadTimeout() does not work");
199
} catch (SSLException | SocketTimeoutException ex) {
200
System.out.println("Got expected timeout exception for " +
201
"HttpsURLConnection.setReadTimeout(): " + getCause(ex));
202
} finally {
203
done();
204
http.disconnect();
205
}
206
} finally {
207
HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);
208
}
209
}
210
211
private Exception getCause(Exception ex) {
212
Exception cause = null;
213
if (ex instanceof SSLException) {
214
cause = (Exception) ex.getCause();
215
if (!(cause instanceof SocketTimeoutException)) {
216
throw new RuntimeException("Unexpected cause", cause);
217
}
218
} else {
219
cause = ex;
220
}
221
222
return cause;
223
}
224
225
static class NameVerifier implements HostnameVerifier {
226
public boolean verify(String hostname, SSLSession session) {
227
return true;
228
}
229
}
230
231
/*
232
* =============================================================
233
* The remainder is just support stuff
234
*/
235
236
// use any free port by default
237
volatile int serverPort = 0;
238
239
volatile Exception serverException = null;
240
volatile Exception clientException = null;
241
242
public static void main(String[] args) throws Exception {
243
String keyFilename =
244
System.getProperty("test.src", "./") + "/" + pathToStores +
245
"/" + keyStoreFile;
246
String trustFilename =
247
System.getProperty("test.src", "./") + "/" + pathToStores +
248
"/" + trustStoreFile;
249
250
System.setProperty("javax.net.ssl.keyStore", keyFilename);
251
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
252
System.setProperty("javax.net.ssl.trustStore", trustFilename);
253
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
254
255
if (debug)
256
System.setProperty("javax.net.debug", "all");
257
258
/*
259
* Start the tests.
260
*/
261
new ReadTimeout();
262
}
263
264
Thread clientThread = null;
265
Thread serverThread = null;
266
267
/*
268
* Primary constructor, used to drive remainder of the test.
269
*
270
* Fork off the other side, then do your work.
271
*/
272
ReadTimeout() throws Exception {
273
if (separateServerThread) {
274
startServer(true);
275
startClient(false);
276
} else {
277
startClient(true);
278
startServer(false);
279
}
280
281
/*
282
* Wait for other side to close down.
283
*/
284
if (separateServerThread) {
285
serverThread.join();
286
} else {
287
clientThread.join();
288
}
289
290
/*
291
* When we get here, the test is pretty much over.
292
*
293
* If the main thread excepted, that propagates back
294
* immediately. If the other thread threw an exception, we
295
* should report back.
296
*/
297
if (serverException != null)
298
throw serverException;
299
if (clientException != null)
300
throw clientException;
301
}
302
303
void startServer(boolean newThread) throws Exception {
304
if (newThread) {
305
serverThread = new Thread() {
306
public void run() {
307
try {
308
doServerSide();
309
} catch (Exception e) {
310
/*
311
* Our server thread just died.
312
*
313
* Release the client, if not active already...
314
*/
315
System.err.println("Server died...");
316
serverReady = true;
317
serverException = e;
318
}
319
}
320
};
321
serverThread.start();
322
} else {
323
doServerSide();
324
}
325
}
326
327
void startClient(boolean newThread) throws Exception {
328
if (newThread) {
329
clientThread = new Thread() {
330
public void run() {
331
try {
332
doClientSide();
333
} catch (Exception e) {
334
/*
335
* Our client thread just died.
336
*/
337
System.err.println("Client died...");
338
clientException = e;
339
}
340
}
341
};
342
clientThread.start();
343
} else {
344
doClientSide();
345
}
346
}
347
}
348
349