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/CookieHttpsClientTest.java
41161 views
1
/*
2
* Copyright (c) 2012, 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
// SunJSSE does not support dynamic system properties, no way to re-use
25
// system properties in samevm/agentvm mode.
26
27
/*
28
* @test
29
* @bug 7129083
30
* @summary Cookiemanager does not store cookies if url is read
31
* before setting cookiemanager
32
* @library /test/lib
33
* @run main/othervm CookieHttpsClientTest
34
*/
35
36
import java.net.CookieHandler;
37
import java.net.CookieManager;
38
import java.net.CookiePolicy;
39
import java.net.InetAddress;
40
import java.net.URL;
41
import java.io.InputStream;
42
import java.io.IOException;
43
import javax.net.ssl.HostnameVerifier;
44
import javax.net.ssl.HttpsURLConnection;
45
import javax.net.ssl.SSLHandshakeException;
46
import javax.net.ssl.SSLServerSocket;
47
import javax.net.ssl.SSLServerSocketFactory;
48
import javax.net.ssl.SSLSession;
49
import javax.net.ssl.SSLSocket;
50
import jdk.test.lib.net.URIBuilder;
51
52
public class CookieHttpsClientTest {
53
static final int TIMEOUT = 10 * 1000;
54
55
static final String replyString = "HTTP/1.1 200 OK\r\n" +
56
"Set-Cookie: name=test\r\n" +
57
"Content-Length: 10\r\n\r\n" +
58
"1234567890";
59
60
/*
61
* =============================================================
62
* Set the various variables needed for the tests, then
63
* specify what tests to run on each side.
64
*/
65
66
/*
67
* Should we run the client or server in a separate thread?
68
* Both sides can throw exceptions, but do you have a preference
69
* as to which side should be the main thread.
70
*/
71
static boolean separateServerThread = true;
72
73
/*
74
* Where do we find the keystores?
75
*/
76
static String pathToStores = "../../../../../../javax/net/ssl/etc";
77
static String keyStoreFile = "keystore";
78
static String trustStoreFile = "truststore";
79
static String passwd = "passphrase";
80
81
/*
82
* Is the server ready to serve?
83
*/
84
volatile static boolean serverReady = false;
85
86
/*
87
* Turn on SSL debugging?
88
*/
89
static boolean debug = false;
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
SSLSocket sslSocket = null;
110
try {
111
sslSocket = (SSLSocket) sslServerSocket.accept();
112
sslSocket.setSoTimeout(TIMEOUT);
113
readOneRequest(sslSocket.getInputStream());
114
sslSocket.getOutputStream().write(replyString.getBytes());
115
116
readOneRequest(sslSocket.getInputStream());
117
sslSocket.getOutputStream().write(replyString.getBytes());
118
} catch (Exception e) {
119
e.printStackTrace();
120
} finally {
121
try {
122
if (sslSocket != null) { sslSocket.close(); }
123
sslServerSocket.close();
124
} catch (IOException unused) { /* gulp!burp! */ }
125
}
126
}
127
128
/*
129
* Define the client side of the test.
130
*
131
* If the server prematurely exits, serverReady will be set to true
132
* to avoid infinite hangs.
133
*/
134
void doClientSide() throws Exception {
135
// Wait for server to get started.
136
while (!serverReady) {
137
Thread.sleep(50);
138
}
139
140
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
141
public boolean verify(String hostname, SSLSession session) {
142
return true;
143
}});
144
145
URL url = URIBuilder.newBuilder()
146
.scheme("https")
147
.loopback()
148
.port(serverPort)
149
.path("/")
150
.toURL();
151
152
System.out.println("Client ready to connect to: " + url);
153
154
// Run without a CookieHandler first
155
InputStream in = url.openConnection(java.net.Proxy.NO_PROXY).getInputStream();
156
while (in.read() != -1); // read response body so connection can be reused
157
158
// Set a CookeHandler and retest using the HttpClient from the KAC
159
CookieManager manager = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
160
CookieHandler.setDefault(manager);
161
162
in = url.openConnection().getInputStream();
163
while (in.read() != -1);
164
165
if (manager.getCookieStore().getCookies().isEmpty()) {
166
throw new RuntimeException("Failed: No cookies in the cookie Handler.");
167
}
168
}
169
170
static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n' };
171
172
// Read until the end of a HTTP request
173
static void readOneRequest(InputStream is) throws IOException {
174
int requestEndCount = 0, r;
175
while ((r = is.read()) != -1) {
176
if (r == requestEnd[requestEndCount]) {
177
requestEndCount++;
178
if (requestEndCount == 4) {
179
break;
180
}
181
} else {
182
requestEndCount = 0;
183
}
184
}
185
}
186
187
/*
188
* =============================================================
189
* The remainder is just support stuff
190
*/
191
192
// use any free port by default
193
volatile int serverPort = 0;
194
195
volatile Exception serverException = null;
196
volatile Exception clientException = null;
197
198
private boolean sslConnectionFailed() {
199
return clientException instanceof SSLHandshakeException;
200
}
201
202
public static void main(String args[]) throws Exception {
203
String keyFilename =
204
System.getProperty("test.src", ".") + "/" + pathToStores +
205
"/" + keyStoreFile;
206
String trustFilename =
207
System.getProperty("test.src", ".") + "/" + pathToStores +
208
"/" + trustStoreFile;
209
210
System.setProperty("javax.net.ssl.keyStore", keyFilename);
211
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
212
System.setProperty("javax.net.ssl.trustStore", trustFilename);
213
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
214
215
if (debug)
216
System.setProperty("javax.net.debug", "all");
217
218
new CookieHttpsClientTest();
219
}
220
221
Thread clientThread = null;
222
Thread serverThread = null;
223
224
/*
225
* Primary constructor, used to drive remainder of the test.
226
*
227
* Fork off the other side, then do your work.
228
*/
229
CookieHttpsClientTest() throws Exception {
230
Exception startException = null;
231
try {
232
if (separateServerThread) {
233
startServer(true);
234
startClient(false);
235
} else {
236
startClient(true);
237
startServer(false);
238
}
239
} catch (Exception e) {
240
startException = e;
241
}
242
243
/*
244
* Wait for other side to close down.
245
*/
246
if (separateServerThread) {
247
if (serverThread != null) {
248
// don't join the server thread if the
249
// client failed to connect
250
if (!sslConnectionFailed()) {
251
serverThread.join();
252
}
253
}
254
} else {
255
if (clientThread != null) {
256
clientThread.join();
257
}
258
}
259
260
/*
261
* When we get here, the test is pretty much over.
262
* Which side threw the error?
263
*/
264
Exception local;
265
Exception remote;
266
267
if (separateServerThread) {
268
remote = serverException;
269
local = clientException;
270
} else {
271
remote = clientException;
272
local = serverException;
273
}
274
275
Exception exception = null;
276
277
/*
278
* Check various exception conditions.
279
*/
280
if ((local != null) && (remote != null)) {
281
// If both failed, return the curthread's exception.
282
local.addSuppressed(remote);
283
exception = local;
284
} else if (local != null) {
285
exception = local;
286
} else if (remote != null) {
287
exception = remote;
288
} else if (startException != null) {
289
exception = startException;
290
}
291
292
/*
293
* If there was an exception *AND* a startException,
294
* output it.
295
*/
296
if (exception != null) {
297
if (exception != startException && startException != null) {
298
exception.addSuppressed(startException);
299
}
300
throw exception;
301
}
302
303
// Fall-through: no exception to throw!
304
}
305
306
void startServer(boolean newThread) throws Exception {
307
if (newThread) {
308
serverThread = new Thread() {
309
public void run() {
310
try {
311
doServerSide();
312
} catch (Exception e) {
313
/*
314
* Our server thread just died.
315
*
316
* Release the client, if not active already...
317
*/
318
System.err.println("Server died...");
319
serverReady = true;
320
serverException = e;
321
}
322
}
323
};
324
serverThread.start();
325
} else {
326
try {
327
doServerSide();
328
} catch (Exception e) {
329
serverException = e;
330
} finally {
331
serverReady = true;
332
}
333
}
334
}
335
336
void startClient(boolean newThread) throws Exception {
337
if (newThread) {
338
clientThread = new Thread() {
339
public void run() {
340
try {
341
doClientSide();
342
} catch (Exception e) {
343
/*
344
* Our client thread just died.
345
*/
346
System.err.println("Client died: " + e);
347
clientException = e;
348
}
349
}
350
};
351
clientThread.start();
352
} else {
353
try {
354
doClientSide();
355
} catch (Exception e) {
356
System.err.println("Client died: " + e);
357
clientException = e;
358
}
359
}
360
}
361
}
362
363