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