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/CookieHandlerTest.java
38889 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
/* @test
25
* @bug 4696506 4942650
26
* @summary Unit test for java.net.CookieHandler
27
* @run main/othervm CookieHandlerTest
28
*
29
* SunJSSE does not support dynamic system properties, no way to re-use
30
* system properties in samevm/agentvm mode.
31
* @author Yingxian Wang
32
*/
33
34
import java.net.*;
35
import java.util.*;
36
import java.io.*;
37
import javax.net.ssl.*;
38
39
public class CookieHandlerTest {
40
static Map<String,String> cookies;
41
ServerSocket ss;
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 = true;
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
* Define the server side of the test.
76
*
77
* If the server prematurely exits, serverReady will be set to true
78
* to avoid infinite hangs.
79
*/
80
void doServerSide() throws Exception {
81
SSLServerSocketFactory sslssf =
82
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
83
SSLServerSocket sslServerSocket =
84
(SSLServerSocket) sslssf.createServerSocket(serverPort);
85
serverPort = sslServerSocket.getLocalPort();
86
87
/*
88
* Signal Client, we're ready for his connect.
89
*/
90
serverReady = true;
91
SSLSocket sslSocket = null;
92
try {
93
sslSocket = (SSLSocket) sslServerSocket.accept();
94
95
// check request contains "Cookie"
96
InputStream is = sslSocket.getInputStream ();
97
BufferedReader r = new BufferedReader(new InputStreamReader(is));
98
boolean flag = false;
99
String x;
100
while ((x=r.readLine()) != null) {
101
if (x.length() ==0) {
102
break;
103
}
104
String header = "Cookie: ";
105
if (x.startsWith(header)) {
106
if (x.equals("Cookie: "+((String)cookies.get("Cookie")))) {
107
flag = true;
108
}
109
}
110
}
111
if (!flag) {
112
throw new RuntimeException("server should see cookie in request");
113
}
114
115
PrintStream out = new PrintStream(
116
new BufferedOutputStream(
117
sslSocket.getOutputStream() ));
118
119
/* send the header */
120
out.print("HTTP/1.1 200 OK\r\n");
121
out.print("Set-Cookie2: "+((String)cookies.get("Set-Cookie2")+"\r\n"));
122
out.print("Content-Type: text/html; charset=iso-8859-1\r\n");
123
out.print("Connection: close\r\n");
124
out.print("\r\n");
125
out.print("<HTML>");
126
out.print("<HEAD><TITLE>Testing cookie</TITLE></HEAD>");
127
out.print("<BODY>OK.</BODY>");
128
out.print("</HTML>");
129
out.flush();
130
131
sslSocket.close();
132
sslServerSocket.close();
133
} catch (Exception e) {
134
e.printStackTrace();
135
}
136
}
137
138
/*
139
* Define the client side of the test.
140
*
141
* If the server prematurely exits, serverReady will be set to true
142
* to avoid infinite hangs.
143
*/
144
void doClientSide() throws Exception {
145
146
/*
147
* Wait for server to get started.
148
*/
149
while (!serverReady) {
150
Thread.sleep(50);
151
}
152
HttpsURLConnection http = null;
153
/* establish http connection to server */
154
String uri = "https://localhost:" + +serverPort ;
155
URL url = new URL(uri);
156
HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());
157
http = (HttpsURLConnection)url.openConnection();
158
159
int respCode = http.getResponseCode();
160
http.disconnect();
161
162
}
163
164
static class NameVerifier implements HostnameVerifier {
165
public boolean verify(String hostname, SSLSession session) {
166
return true;
167
}
168
}
169
170
/*
171
* =============================================================
172
* The remainder is just support stuff
173
*/
174
175
// use any free port by default
176
volatile int serverPort = 0;
177
178
volatile Exception serverException = null;
179
volatile Exception clientException = null;
180
181
public static void main(String args[]) throws Exception {
182
String keyFilename =
183
System.getProperty("test.src", "./") + "/" + pathToStores +
184
"/" + keyStoreFile;
185
String trustFilename =
186
System.getProperty("test.src", "./") + "/" + pathToStores +
187
"/" + trustStoreFile;
188
189
CookieHandler reservedCookieHandler = CookieHandler.getDefault();
190
HostnameVerifier reservedHV =
191
HttpsURLConnection.getDefaultHostnameVerifier();
192
try {
193
System.setProperty("javax.net.ssl.keyStore", keyFilename);
194
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
195
System.setProperty("javax.net.ssl.trustStore", trustFilename);
196
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
197
198
if (debug)
199
System.setProperty("javax.net.debug", "all");
200
201
/*
202
* Start the tests.
203
*/
204
cookies = new HashMap<String, String>();
205
cookies.put("Cookie",
206
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\"; $Path=\"/acme\"");
207
cookies.put("Set-Cookie2",
208
"$Version=\"1\"; Part_Number=\"Riding_Rocket_0023\"; " +
209
"$Path=\"/acme/ammo\"; Part_Number=\"Rocket_Launcher_0001\"; "+
210
"$Path=\"/acme\"");
211
CookieHandler.setDefault(new MyCookieHandler());
212
new CookieHandlerTest();
213
} finally {
214
HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);
215
CookieHandler.setDefault(reservedCookieHandler);
216
}
217
}
218
219
Thread clientThread = null;
220
Thread serverThread = null;
221
/*
222
* Primary constructor, used to drive remainder of the test.
223
*
224
* Fork off the other side, then do your work.
225
*/
226
CookieHandlerTest() throws Exception {
227
if (separateServerThread) {
228
startServer(true);
229
startClient(false);
230
} else {
231
startClient(true);
232
startServer(false);
233
}
234
235
/*
236
* Wait for other side to close down.
237
*/
238
if (separateServerThread) {
239
serverThread.join();
240
} else {
241
clientThread.join();
242
}
243
244
/*
245
* When we get here, the test is pretty much over.
246
*
247
* If the main thread excepted, that propagates back
248
* immediately. If the other thread threw an exception, we
249
* should report back.
250
*/
251
if (serverException != null)
252
throw serverException;
253
if (clientException != null)
254
throw clientException;
255
256
if (!getCalled || !putCalled) {
257
throw new RuntimeException ("Either get or put method is not called");
258
}
259
}
260
261
void startServer(boolean newThread) throws Exception {
262
if (newThread) {
263
serverThread = new Thread() {
264
public void run() {
265
try {
266
doServerSide();
267
} catch (Exception e) {
268
/*
269
* Our server thread just died.
270
*
271
* Release the client, if not active already...
272
*/
273
System.err.println("Server died...");
274
serverReady = true;
275
serverException = e;
276
}
277
}
278
};
279
serverThread.start();
280
} else {
281
doServerSide();
282
}
283
}
284
285
void startClient(boolean newThread) throws Exception {
286
if (newThread) {
287
clientThread = new Thread() {
288
public void run() {
289
try {
290
doClientSide();
291
} catch (Exception e) {
292
/*
293
* Our client thread just died.
294
*/
295
System.err.println("Client died...");
296
clientException = e;
297
}
298
}
299
};
300
clientThread.start();
301
} else {
302
doClientSide();
303
}
304
}
305
306
static boolean getCalled = false, putCalled = false;
307
308
static class MyCookieHandler extends CookieHandler {
309
public Map<String,List<String>>
310
get(URI uri, Map<String,List<String>> requestHeaders)
311
throws IOException {
312
getCalled = true;
313
// returns cookies[0]
314
// they will be include in request
315
Map<String,List<String>> map = new HashMap<>();
316
List<String> l = new ArrayList<>();
317
l.add(cookies.get("Cookie"));
318
map.put("Cookie",l);
319
return Collections.unmodifiableMap(map);
320
}
321
322
public void
323
put(URI uri, Map<String,List<String>> responseHeaders)
324
throws IOException {
325
putCalled = true;
326
// check response has cookies[1]
327
List<String> l = responseHeaders.get("Set-Cookie2");
328
String value = l.get(0);
329
if (!value.equals((String)cookies.get("Set-Cookie2"))) {
330
throw new RuntimeException("cookie should be available for handle to put into cache");
331
}
332
}
333
}
334
335
}
336
337