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