Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/CookieHandler/CookieManagerTest.java
41149 views
1
/*
2
* Copyright (c) 2005, 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
* @test
26
* @summary Unit test for java.net.CookieManager
27
* @bug 6244040 7150552 7051862
28
* @modules jdk.httpserver
29
* java.logging
30
* @run main/othervm -ea -esa CookieManagerTest
31
* @author Edward Wang
32
*/
33
34
import com.sun.net.httpserver.*;
35
import java.util.Collections;
36
import java.util.LinkedList;
37
import java.util.List;
38
import java.io.IOException;
39
import java.net.*;
40
import java.util.logging.Level;
41
import java.util.logging.Logger;
42
import static java.net.Proxy.NO_PROXY;
43
44
public class CookieManagerTest {
45
46
static CookieTransactionHandler httpTrans;
47
static HttpServer server;
48
49
static final String hostAddress = getAddr();
50
51
/** Returns an IP literal suitable for use by the test. */
52
static String getAddr() {
53
try {
54
InetAddress lh = InetAddress.getLocalHost();
55
System.out.println("Trying: " + lh);
56
if (lh.isReachable(5_000)) {
57
System.out.println("Using: " + lh);
58
return lh.getHostAddress();
59
}
60
} catch (IOException x) {
61
System.out.println("Debug: caught:" + x);
62
}
63
InetAddress loopback = InetAddress.getLoopbackAddress();
64
System.out.println("Using: \"" + loopback.getHostAddress() + "\"");
65
return loopback.getHostAddress();
66
}
67
68
public static void main(String[] args) throws Exception {
69
// logs everything...
70
Logger root = Logger.getLogger("");
71
root.setLevel(Level.ALL);
72
root.getHandlers()[0].setLevel(Level.ALL);
73
74
startHttpServer();
75
makeHttpCall();
76
77
if (httpTrans.badRequest) {
78
throw new RuntimeException("Test failed : bad cookie header");
79
}
80
checkCookiePolicy();
81
}
82
83
public static void startHttpServer() throws IOException {
84
httpTrans = new CookieTransactionHandler();
85
server = HttpServer.create(new InetSocketAddress(hostAddress, 0), 0);
86
server.createContext("/", httpTrans);
87
server.start();
88
}
89
90
/*
91
* Checks if CookiePolicy.ACCEPT_ORIGINAL_SERVER#shouldAccept()
92
* returns false for null arguments
93
*/
94
private static void checkCookiePolicy() throws Exception {
95
CookiePolicy cp = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
96
boolean retVal;
97
retVal = cp.shouldAccept(null, null);
98
checkValue(retVal);
99
retVal = cp.shouldAccept(null, new HttpCookie("CookieName", "CookieVal"));
100
checkValue(retVal);
101
retVal = cp.shouldAccept((new URL("http", "localhost", 2345, "/")).toURI(),
102
null);
103
checkValue(retVal);
104
}
105
106
private static void checkValue(boolean val) {
107
if (val)
108
throw new RuntimeException("Return value is not false!");
109
}
110
111
public static void makeHttpCall() throws IOException {
112
try {
113
int port = server.getAddress().getPort();
114
System.out.println("http server listenining on: " + port);
115
116
// install CookieManager to use
117
CookieHandler.setDefault(new CookieManager());
118
119
for (int i = 0; i < CookieTransactionHandler.testCount; i++) {
120
System.out.println("====== CookieManager test " + (i+1)
121
+ " ======");
122
((CookieManager)CookieHandler.getDefault())
123
.setCookiePolicy(CookieTransactionHandler.testPolicies[i]);
124
((CookieManager)CookieHandler.getDefault())
125
.getCookieStore().removeAll();
126
URL url = new URL("http" ,
127
hostAddress,
128
server.getAddress().getPort(),
129
CookieTransactionHandler.testCases[i][0]
130
.serverPath);
131
System.out.println("Requesting " + url);
132
HttpURLConnection uc = (HttpURLConnection)url.openConnection(NO_PROXY);
133
uc.getResponseCode();
134
uc.disconnect();
135
}
136
} finally {
137
server.stop(0);
138
}
139
}
140
}
141
142
class CookieTransactionHandler implements HttpHandler {
143
144
private int testcaseDone = 0;
145
private int testDone = 0;
146
147
public static boolean badRequest = false;
148
// the main test control logic will also loop exactly this number
149
// to send http request
150
public static final int testCount = 6;
151
152
@Override
153
public void handle(HttpExchange exchange) throws IOException {
154
if (testDone < testCases[testcaseDone].length) {
155
// still have other tests to run,
156
// check the Cookie header and then redirect it
157
if (testDone > 0) checkRequest(exchange.getRequestHeaders());
158
exchange.getResponseHeaders().add("Location",
159
testCases[testcaseDone][testDone].serverPath);
160
exchange.getResponseHeaders()
161
.add(testCases[testcaseDone][testDone].headerToken,
162
testCases[testcaseDone][testDone].cookieToSend);
163
exchange.sendResponseHeaders(302, -1);
164
testDone++;
165
} else {
166
// the last test of this test case
167
if (testDone > 0) checkRequest(exchange.getRequestHeaders());
168
testcaseDone++;
169
testDone = 0;
170
exchange.sendResponseHeaders(200, -1);
171
}
172
exchange.close();
173
}
174
175
private static String trim(String s) {
176
StringBuilder sb = new StringBuilder();
177
for (int i=0; i<s.length(); i++) {
178
char c = s.charAt(i);
179
if (!Character.isWhitespace(c))
180
sb.append(c);
181
}
182
return sb.toString();
183
}
184
185
private static boolean cookieEquals(String s1, String s2) {
186
s1 = trim(s1);
187
s2 = trim(s2);
188
String[] s1a = s1.split(";");
189
String[] s2a = s2.split(";");
190
List<String> l1 = new LinkedList(List.of(s1a));
191
List<String> l2 = new LinkedList(List.of(s2a));
192
Collections.sort(l1);
193
Collections.sort(l2);
194
int i = 0;
195
for (String s : l1) {
196
if (!s.equals(l2.get(i++))) {
197
return false;
198
}
199
}
200
return true;
201
}
202
203
private void checkRequest(Headers hdrs) {
204
205
assert testDone > 0;
206
String cookieHeader = hdrs.getFirst("Cookie");
207
if (cookieHeader != null && cookieEquals(
208
cookieHeader, testCases[testcaseDone][testDone-1].cookieToRecv))
209
{
210
System.out.printf("%15s %s\n", "PASSED:", cookieHeader);
211
} else {
212
System.out.printf("%15s %s\n", "FAILED:", cookieHeader);
213
System.out.printf("%15s %s\n\n", "should be:",
214
testCases[testcaseDone][testDone-1].cookieToRecv);
215
badRequest = true;
216
}
217
}
218
219
// test cases
220
public static class CookieTestCase {
221
public String headerToken;
222
public String cookieToSend;
223
public String cookieToRecv;
224
public String serverPath;
225
226
public CookieTestCase(String h, String cts, String ctr, String sp) {
227
headerToken = h;
228
cookieToSend = cts;
229
cookieToRecv = ctr;
230
serverPath = sp;
231
}
232
};
233
234
/*
235
* these two must match each other,
236
* i.e. testCases.length == testPolicies.length
237
*/
238
239
// the test cases to run; each test case may contain multiple roundtrips
240
public static CookieTestCase[][] testCases = null;
241
// indicates what CookiePolicy to use with each test cases
242
public static CookiePolicy[] testPolicies = null;
243
244
CookieTransactionHandler() {
245
testCases = new CookieTestCase[testCount][];
246
testPolicies = new CookiePolicy[testCount];
247
248
String localHostAddr = CookieManagerTest.hostAddress;
249
250
int count = 0;
251
252
// an http session with Netscape cookies exchanged
253
testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
254
testCases[count++] = new CookieTestCase[]{
255
new CookieTestCase("Set-Cookie",
256
"CUSTOMER=WILE:BOB; " +
257
"path=/; expires=Sat, 09-Nov-2030 23:12:40 GMT;" + "domain=." +
258
localHostAddr,
259
"CUSTOMER=WILE:BOB",
260
"/"
261
),
262
new CookieTestCase("Set-Cookie",
263
"PART_NUMBER=ROCKET_LAUNCHER_0001; path=/;" + "domain=." + localHostAddr,
264
"CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001",
265
"/"
266
),
267
new CookieTestCase("Set-Cookie",
268
"SHIPPING=FEDEX; path=/foo;" + "domain=." + localHostAddr,
269
"CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001",
270
"/"
271
),
272
new CookieTestCase("Set-Cookie",
273
"SHIPPING=FEDEX; path=/foo;" + "domain=." + localHostAddr,
274
"CUSTOMER=WILE:BOB; PART_NUMBER=ROCKET_LAUNCHER_0001; SHIPPING=FEDEX",
275
"/foo"
276
)
277
};
278
279
// check whether or not path rule is applied
280
testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
281
testCases[count++] = new CookieTestCase[]{
282
new CookieTestCase("Set-Cookie",
283
"PART_NUMBER=ROCKET_LAUNCHER_0001; path=/;" + "domain=." + localHostAddr,
284
"PART_NUMBER=ROCKET_LAUNCHER_0001",
285
"/"
286
),
287
new CookieTestCase("Set-Cookie",
288
"PART_NUMBER=RIDING_ROCKET_0023; path=/ammo;" + "domain=." + localHostAddr,
289
"PART_NUMBER=RIDING_ROCKET_0023; PART_NUMBER=ROCKET_LAUNCHER_0001",
290
"/ammo"
291
)
292
};
293
294
// an http session with rfc2965 cookies exchanged
295
testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
296
testCases[count++] = new CookieTestCase[]{
297
new CookieTestCase("Set-Cookie2",
298
"Customer=\"WILE_E_COYOTE\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
299
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",
300
"/acme/login"
301
),
302
new CookieTestCase("Set-Cookie2",
303
"Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\";Path=\"/acme\";" + "domain=." + localHostAddr,
304
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";" + "$Domain=\"." +
305
localHostAddr + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";"
306
+ "$Domain=\"." + localHostAddr + "\"",
307
"/acme/pickitem"
308
),
309
new CookieTestCase("Set-Cookie2",
310
"Shipping=\"FedEx\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
311
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr +
312
"\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"."
313
+ localHostAddr + "\"" + "; Shipping=\"FedEx\";$Path=\"/acme\";" +
314
"$Domain=\"." + localHostAddr + "\"",
315
"/acme/shipping"
316
)
317
};
318
319
// check whether or not the path rule is applied
320
testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
321
testCases[count++] = new CookieTestCase[]{
322
new CookieTestCase("Set-Cookie2",
323
"Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
324
"$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",
325
"/acme/ammo"
326
),
327
new CookieTestCase("Set-Cookie2",
328
"Part_Number=\"Riding_Rocket_0023\"; Version=\"1\"; Path=\"/acme/ammo\";" + "domain=."
329
+ localHostAddr,
330
"$Version=\"1\"; Part_Number=\"Riding_Rocket_0023\";$Path=\"/acme/ammo\";$Domain=\"."
331
+ localHostAddr + "\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";"
332
+ "$Domain=\"." + localHostAddr + "\"",
333
"/acme/ammo"
334
),
335
new CookieTestCase("",
336
"",
337
"$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";" + "$Domain=\"." + localHostAddr + "\"",
338
"/acme/parts"
339
)
340
};
341
342
// new cookie should overwrite old cookie
343
testPolicies[count] = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
344
testCases[count++] = new CookieTestCase[]{
345
new CookieTestCase("Set-Cookie2",
346
"Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
347
"$Version=\"1\"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",
348
"/acme"
349
),
350
new CookieTestCase("Set-Cookie2",
351
"Part_Number=\"Rocket_Launcher_2000\"; Version=\"1\"; Path=\"/acme\";" + "domain=." + localHostAddr,
352
"$Version=\"1\"; Part_Number=\"Rocket_Launcher_2000\";$Path=\"/acme\";$Domain=\"." + localHostAddr + "\"",
353
"/acme"
354
)
355
};
356
357
// cookies without domain attributes
358
// RFC 2965 states that domain should default to host
359
testPolicies[count] = CookiePolicy.ACCEPT_ALL;
360
testCases[count++] = new CookieTestCase[]{
361
new CookieTestCase("Set-Cookie2",
362
"Customer=\"WILE_E_COYOTE\"; Version=\"1\"; Path=\"/acme\"",
363
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"",
364
"/acme/login"
365
),
366
new CookieTestCase("Set-Cookie2",
367
"Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\";Path=\"/acme\"",
368
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" +
369
"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"",
370
"/acme/pickitem"
371
),
372
new CookieTestCase("Set-Cookie2",
373
"Shipping=\"FedEx\"; Version=\"1\"; Path=\"/acme\"",
374
"$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" +
375
"; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" +
376
"; Shipping=\"FedEx\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"",
377
"/acme/shipping"
378
)
379
};
380
381
assert count == testCount;
382
}
383
}
384
385