Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java
41161 views
1
/*
2
* Copyright (c) 2003, 2019, 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
* @library /test/lib
36
* @run main/othervm ReadTimeout
37
*/
38
39
import java.io.*;
40
import java.net.*;
41
import javax.net.ssl.*;
42
import jdk.test.lib.net.URIBuilder;
43
44
public class ReadTimeout {
45
46
/*
47
* =============================================================
48
* Set the various variables needed for the tests, then
49
* specify what tests to run on each side.
50
*/
51
52
/*
53
* Should we run the client or server in a separate thread?
54
* Both sides can throw exceptions, but do you have a preference
55
* as to which side should be the main thread.
56
*/
57
static boolean separateServerThread = true;
58
59
/*
60
* Where do we find the keystores?
61
*/
62
static String pathToStores = "../../../../../../javax/net/ssl/etc";
63
static String keyStoreFile = "keystore";
64
static String trustStoreFile = "truststore";
65
static String passwd = "passphrase";
66
67
/*
68
* Is the server ready to serve?
69
*/
70
volatile static boolean serverReady = false;
71
72
/*
73
* Turn on SSL debugging?
74
*/
75
static boolean debug = false;
76
77
/*
78
* Message posted
79
*/
80
static String postMsg = "Testing HTTP post on a https server";
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
InetAddress loopback = InetAddress.getLoopbackAddress();
99
SSLServerSocketFactory sslssf =
100
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
101
SSLServerSocket sslServerSocket =
102
(SSLServerSocket) sslssf.createServerSocket(serverPort, 0, loopback);
103
serverPort = sslServerSocket.getLocalPort();
104
105
/*
106
* Signal Client, we're ready for his connect.
107
*/
108
serverReady = true;
109
try {
110
try (SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept()) {
111
InputStream sslIS = sslSocket.getInputStream();
112
BufferedReader br =
113
new BufferedReader(new InputStreamReader(sslIS));
114
br.readLine();
115
while (!finished()) {
116
Thread.sleep(2000);
117
}
118
}
119
120
reset();
121
// doing second test
122
try (SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept()) {
123
InputStream sslIS = sslSocket.getInputStream();
124
BufferedReader br =
125
new BufferedReader(new InputStreamReader(sslIS));
126
br.readLine();
127
while (!finished()) {
128
Thread.sleep(2000);
129
}
130
}
131
} catch (Exception e) {
132
System.out.println("Should be an expected exception: " + e);
133
} finally {
134
sslServerSocket.close();
135
}
136
}
137
138
boolean isFinished = false;
139
140
synchronized boolean finished () {
141
return (isFinished);
142
}
143
synchronized void done () {
144
isFinished = true;
145
}
146
147
synchronized void reset() {
148
isFinished = false;
149
}
150
151
/*
152
* Define the client side of the test.
153
*
154
* If the server prematurely exits, serverReady will be set to true
155
* to avoid infinite hangs.
156
*/
157
void doClientSide() throws Exception {
158
HostnameVerifier reservedHV =
159
HttpsURLConnection.getDefaultHostnameVerifier();
160
try {
161
/*
162
* Wait for server to get started.
163
*/
164
while (!serverReady) {
165
Thread.sleep(50);
166
}
167
HttpsURLConnection http = null;
168
try {
169
URL url = URIBuilder.newBuilder()
170
.scheme("https")
171
.loopback()
172
.port(serverPort)
173
.toURL();
174
175
// set read timeout through system property
176
System.setProperty("sun.net.client.defaultReadTimeout", "2000");
177
HttpsURLConnection.setDefaultHostnameVerifier(
178
new NameVerifier());
179
http = (HttpsURLConnection)url.openConnection();
180
181
InputStream is = http.getInputStream();
182
183
throw new Exception(
184
"system property timeout configuration does not work");
185
} catch (SSLException | SocketTimeoutException ex) {
186
System.out.println("Got expected timeout exception for " +
187
"system property timeout configuration: " + getCause(ex));
188
} finally {
189
done();
190
http.disconnect();
191
}
192
193
try {
194
URL url = URIBuilder.newBuilder()
195
.scheme("https")
196
.loopback()
197
.port(serverPort)
198
.toURL();
199
200
HttpsURLConnection.setDefaultHostnameVerifier(
201
new NameVerifier());
202
http = (HttpsURLConnection)url.openConnection();
203
// set read timeout through API
204
http.setReadTimeout(2000);
205
206
InputStream is = http.getInputStream();
207
208
throw new Exception(
209
"HttpsURLConnection.setReadTimeout() does not work");
210
} catch (SSLException | SocketTimeoutException ex) {
211
System.out.println("Got expected timeout exception for " +
212
"HttpsURLConnection.setReadTimeout(): " + getCause(ex));
213
} finally {
214
done();
215
http.disconnect();
216
}
217
} finally {
218
HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);
219
}
220
}
221
222
private Exception getCause(Exception ex) {
223
Exception cause = null;
224
if (ex instanceof SSLException) {
225
cause = (Exception) ex.getCause();
226
if (!(cause instanceof SocketTimeoutException)) {
227
throw new RuntimeException("Unexpected cause", cause);
228
}
229
} else {
230
cause = ex;
231
}
232
233
return cause;
234
}
235
236
static class NameVerifier implements HostnameVerifier {
237
public boolean verify(String hostname, SSLSession session) {
238
return true;
239
}
240
}
241
242
/*
243
* =============================================================
244
* The remainder is just support stuff
245
*/
246
247
// use any free port by default
248
volatile int serverPort = 0;
249
250
volatile Exception serverException = null;
251
volatile Exception clientException = null;
252
253
private boolean sslConnectionFailed() {
254
return clientException instanceof SSLHandshakeException;
255
}
256
257
public static void main(String[] args) throws Exception {
258
String keyFilename =
259
System.getProperty("test.src", "./") + "/" + pathToStores +
260
"/" + keyStoreFile;
261
String trustFilename =
262
System.getProperty("test.src", "./") + "/" + pathToStores +
263
"/" + trustStoreFile;
264
265
System.setProperty("javax.net.ssl.keyStore", keyFilename);
266
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
267
System.setProperty("javax.net.ssl.trustStore", trustFilename);
268
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
269
270
if (debug)
271
System.setProperty("javax.net.debug", "all");
272
273
/*
274
* Start the tests.
275
*/
276
new ReadTimeout();
277
}
278
279
Thread clientThread = null;
280
Thread serverThread = null;
281
282
/*
283
* Primary constructor, used to drive remainder of the test.
284
*
285
* Fork off the other side, then do your work.
286
*/
287
ReadTimeout() throws Exception {
288
if (separateServerThread) {
289
startServer(true);
290
startClient(false);
291
} else {
292
startClient(true);
293
startServer(false);
294
}
295
296
/*
297
* Wait for other side to close down.
298
*/
299
if (separateServerThread) {
300
if (!sslConnectionFailed()) {
301
serverThread.join();
302
}
303
} else {
304
clientThread.join();
305
}
306
307
/*
308
* When we get here, the test is pretty much over.
309
*
310
* If the main thread excepted, that propagates back
311
* immediately. If the other thread threw an exception, we
312
* should report back.
313
*/
314
if (serverException != null)
315
throw serverException;
316
if (clientException != null)
317
throw clientException;
318
}
319
320
void startServer(boolean newThread) throws Exception {
321
if (newThread) {
322
serverThread = new Thread() {
323
public void run() {
324
try {
325
doServerSide();
326
} catch (Exception e) {
327
/*
328
* Our server thread just died.
329
*
330
* Release the client, if not active already...
331
*/
332
System.err.println("Server died...");
333
serverReady = true;
334
serverException = e;
335
}
336
}
337
};
338
serverThread.start();
339
} else {
340
doServerSide();
341
}
342
}
343
344
void startClient(boolean newThread) throws Exception {
345
if (newThread) {
346
clientThread = new Thread() {
347
public void run() {
348
try {
349
doClientSide();
350
} catch (Exception e) {
351
/*
352
* Our client thread just died.
353
*/
354
System.err.println("Client died...");
355
clientException = e;
356
}
357
}
358
};
359
clientThread.start();
360
} else {
361
doClientSide();
362
}
363
}
364
}
365
366