Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/URLPermission/URLPermissionTest.java
41149 views
1
/*
2
* Copyright (c) 2013, 2016, 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
import java.net.URLPermission;
25
import java.io.*;
26
27
/**
28
* @test
29
* @bug 8010464 8027570 8027687 8029354 8114860 8071660 8161291
30
*/
31
32
public class URLPermissionTest {
33
34
// super class for all test types
35
abstract static class Test {
36
boolean expected;
37
abstract boolean execute();
38
};
39
40
// Instantiation: should succeed
41
static class CreateTest extends Test {
42
String arg;
43
CreateTest(String arg) {
44
this.arg = arg;
45
}
46
47
@Override
48
boolean execute() {
49
try {
50
URLPermission p = new URLPermission(arg);
51
return true;
52
} catch (Exception e) {
53
return false;
54
}
55
}
56
};
57
58
static CreateTest createtest(String arg) {
59
return new CreateTest(arg);
60
}
61
62
// Should throw an IAE on construction
63
64
static class ExTest extends Test {
65
String arg;
66
ExTest(String arg) {
67
this.arg = arg;
68
}
69
70
@Override
71
boolean execute() {
72
try {
73
URLPermission p = new URLPermission(arg);
74
return false;
75
} catch (IllegalArgumentException e) {
76
return true;
77
}
78
}
79
};
80
81
static ExTest extest(String arg) {
82
return new ExTest(arg);
83
}
84
85
// Tests URL part of implies() method. This is the main test.
86
static class URLImpliesTest extends Test {
87
String arg1, arg2;
88
89
URLImpliesTest(String arg1, String arg2, boolean expected) {
90
this.arg1 = arg1;
91
this.arg2 = arg2;
92
this.expected = expected;
93
}
94
95
boolean execute() {
96
URLPermission p1 = new URLPermission (arg1, "GET:*");
97
URLPermission p2 = new URLPermission (arg2, "GET:*");
98
boolean result = p1.implies(p2);
99
if (result != expected) {
100
System.out.println("p1 = " + p1);
101
System.out.println("p2 = " + p2);
102
}
103
return result == expected;
104
}
105
};
106
107
static URLImpliesTest imtest(String arg1, String arg2, boolean expected) {
108
return new URLImpliesTest(arg1, arg2, expected);
109
}
110
111
static class ActionImpliesTest extends Test {
112
String arg1, arg2;
113
String url1 = "http://www.foo.com/-";
114
String url2 = "http://www.foo.com/a/b";
115
116
ActionImpliesTest(String arg1, String arg2, boolean expected) {
117
this.arg1 = arg1;
118
this.arg2 = arg2;
119
this.expected = expected;
120
}
121
122
ActionImpliesTest(String ur11, String url2, String arg1, String arg2,
123
boolean expected) {
124
this.url1 = ur11;
125
this.url2 = url2;
126
this.arg1 = arg1;
127
this.arg2 = arg2;
128
this.expected = expected;
129
}
130
131
@Override
132
boolean execute() {
133
URLPermission p1 = new URLPermission(url1, arg1);
134
URLPermission p2 = new URLPermission(url2, arg2);
135
boolean result = p1.implies(p2);
136
137
return result == expected;
138
}
139
}
140
141
static ActionsStringTest actionstest(String arg, String expectedActions) {
142
return new ActionsStringTest(arg, expectedActions);
143
}
144
145
static class ActionsStringTest extends Test {
146
147
String expectedActions;
148
String arg;
149
150
public ActionsStringTest(String arg, String expectedActions) {
151
this.arg = arg;
152
this.expectedActions = expectedActions;
153
}
154
155
@Override
156
boolean execute() {
157
String url = "http://www.foo.com/";
158
URLPermission urlp = new URLPermission(url, arg);
159
return (expectedActions.equals(urlp.getActions()));
160
}
161
}
162
163
static ActionImpliesTest actest(String arg1, String arg2, boolean expected) {
164
return new ActionImpliesTest(arg1, arg2, expected);
165
}
166
167
static ActionImpliesTest actest(String url1, String url2, String arg1,
168
String arg2, boolean expected) {
169
return new ActionImpliesTest(url1, url2, arg1, arg2, expected);
170
}
171
172
static class HashCodeTest extends Test {
173
String arg1, arg2;
174
int hash;
175
176
HashCodeTest(String arg1, String arg2, int hash) {
177
this.arg1 = arg1;
178
this.arg2 = arg2;
179
this.hash = hash;
180
}
181
182
@Override
183
boolean execute() {
184
URLPermission p = new URLPermission(arg1, arg2);
185
int h = p.hashCode();
186
return h == hash;
187
}
188
}
189
190
static HashCodeTest hashtest(String arg1, String arg2, int expected) {
191
return new HashCodeTest(arg1, arg2, expected);
192
}
193
194
static class URLEqualityTest extends Test {
195
String arg1, arg2;
196
197
URLEqualityTest(String arg1, String arg2, boolean expected) {
198
this.arg1 = arg1;
199
this.arg2 = arg2;
200
this.expected = expected;
201
}
202
203
@Override
204
boolean execute() {
205
URLPermission p1 = new URLPermission(arg1);
206
URLPermission p2 = new URLPermission(arg2);
207
boolean result = p1.equals(p2);
208
209
return result == expected;
210
}
211
}
212
213
static URLEqualityTest eqtest(String arg1, String arg2, boolean expected) {
214
return new URLEqualityTest(arg1, arg2, expected);
215
}
216
217
static Test[] pathImplies = {
218
// single
219
imtest("http://www.foo.com/", "http://www.foo.com/", true),
220
imtest("http://www.bar.com/", "http://www.foo.com/", false),
221
imtest("http://www.foo.com/a/b", "http://www.foo.com/", false),
222
imtest("http://www.foo.com/a/b", "http://www.foo.com/a/b/c", false),
223
// wildcard
224
imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c", true),
225
imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/*", true),
226
imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c#frag", true),
227
imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c#frag?foo=foo", true),
228
imtest("http://www.foo.com/a/b/*", "http://www.foo.com/b/b/c", false),
229
imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c.html", true),
230
imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c.html", true),
231
imtest("http://www.foo.com/a/b/*", "https://www.foo.com/a/b/c", false),
232
// recursive
233
imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/-", true),
234
imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c", true),
235
imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c#frag", true),
236
imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c#frag?foo=foo", true),
237
imtest("http://www.foo.com/a/b/-", "http://www.foo.com/b/b/c", false),
238
imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c.html", true),
239
imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c.html", true),
240
imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c/d/e.html", true),
241
imtest("https://www.foo.com/a/b/-", "http://www.foo.com/a/b/c/d/e.html", false),
242
imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c/d/e#frag", true),
243
imtest("http://www.foo.com/a/b/-", "https://www.foo.com/a/b/c", false),
244
// special cases
245
imtest("http:*", "https://www.foo.com/a/b/c", false),
246
imtest("http:*", "http://www.foo.com/a/b/c", true),
247
imtest("http:*", "http://foo/bar", true),
248
imtest("http://WWW.foO.cOM/a/b/*", "http://wwW.foo.com/a/b/c", true),
249
imtest("http://wWw.fOo.cOm/a/b/*", "http://Www.foo.com/a/b/*", true),
250
imtest("http://www.FOO.com/", "http://www.foo.COM/", true),
251
imtest("http://66ww-w.F-O012O.com/", "http://66ww-w.f-o012o.COM/",true),
252
imtest("http://xn--ire-9la.com/", "http://xn--ire-9la.COM/", true),
253
imtest("http://x/", "http://X/", true),
254
imtest("http://x/", "http://x/", true),
255
imtest("http://X/", "http://X/", true),
256
imtest("http://foo/bar", "https://foo/bar", false),
257
imtest("http://www.foo.com/*", "http://www.foo.com/#foo", true),
258
imtest("http://www.foo.com/a/*#foo", "http://www.foo.com/a/b#foo", true),
259
imtest("http://www.foo.com/a/-", "http://www.foo.com/a/b#foo", true),
260
imtest("http://www.foo.com/?q1=1&q2=2#foo", "http://www.foo.com/?q1=1&q2=2#bar", true),
261
imtest("http://www.foo.com/", "http://www.foo.com/?q1=1&q2=2#bar", true),
262
imtest("http://www.foo.com/", "http://www.foo.com?q1=1&q2=2#bar", false),
263
imtest("http://www.foo.com", "http://www.foo.com?q1=1&q2=2#bar", true)
264
};
265
266
// new functionality
267
268
static Test[] exceptionTests = {
269
extest("http://1.2.3.4.5/a/b/c"),
270
extest("http://www.*.com"),
271
extest("http://[foo.com]:99"),
272
extest("http://[fec0::X]:99"),
273
extest("http:\\www.foo.com"),
274
extest("http://w_09ww.foo.com"),
275
extest("http://w&09ww.foo.com/p"),
276
extest("http://www+foo.com"),
277
extest("http:")
278
};
279
280
static Test[] hashTests = {
281
hashtest("http://www.foo.com/path", "GET:X-Foo", 388644203),
282
hashtest("http:*", "*:*", 3255810)
283
};
284
285
static Test[] pathImplies2 = {
286
imtest("http://[FE80::]:99", "http://[fe80:0::]:99", true),
287
288
// hostnames
289
imtest("http://*.foo.com/a/b/-", "http://www.foo.com/a/b/c/d", true),
290
imtest("http://*.foo.com/a/b/-", "http://www.bar.com/a/b/c/d", false),
291
imtest("http://*.foo.com/a/b/-", "http://www.biz.bar.foo.com/a/b/c/d", true),
292
imtest("http://*.foo.com/a/b/-", "http://www.biz.bar.foo.como/a/b/c/d", false),
293
imtest("http://*/a/b/-", "http://www.biz.bar.foo.fuzz/a/b/c/d", true),
294
imtest("http://*/a/b/-", "http://*/a/b/c/d", true),
295
imtest("http://*.foo.com/a/b/-", "http://*/a/b/c/d", false),
296
imtest("http:*", "http://*/a/b/c/d", true),
297
298
// literal IPv4 addresses
299
imtest("http://1.2.3.4/a/b/-", "http://www.biz.bar.foo.com/a/b/c/d", false),
300
imtest("http://1.2.3.4/a/b/-", "http://1.2.3.4/a/b/c/d", true),
301
imtest("http://1.2.3.4/a/b/-", "http://1.2.88.4/a/b/c/d", false),
302
imtest("http:*", "http://1.2.88.4/a/b/c/d", true),
303
304
// literal IPv6 addresses
305
imtest("http://[fe80::]/a/b/-", "http://[fe80::0]/a/b/c", true),
306
imtest("http://[fe80::]/a/b/-", "http://[fe80::3]/a/b/c", false),
307
imtest("http://[1:2:3:4:5:6:7:8]/a/b/-","http://[1:002:03:4:0005:6:07:8]/a/b/c", true),
308
imtest("http://[1:2:3:4:5:6:7:8]/a/b/-","http://[1:002:03:4:0033:6:07:8]/a/b/c", false),
309
imtest("http://[1::2]/a/b/-", "http://[1:0:0:0::2]/a/b/c", true),
310
imtest("http://[1::2]/a/b/-", "http://[1:0:0:0::3]/a/b/c", false),
311
imtest("http://[FE80::]:99", "http://[fe80:0::]:99", true),
312
imtest("http:*", "http://[fe80:0::]:99", true),
313
314
// portranges
315
imtest("http://*.foo.com:1-2/a/b/-", "http://www.foo.com:1/a/b/c/d", true),
316
imtest("http://*.foo.com:1-2/a/b/-", "http://www.foo.com:3/a/b/c/d", false),
317
imtest("http://*.foo.com:3-/a/b/-", "http://www.foo.com:1/a/b/c/d", false),
318
imtest("http://*.foo.com:3-/a/b/-", "http://www.foo.com:4-5/a/b/c/d", true),
319
imtest("http://*.foo.com:3-/a/b/-", "http://www.foo.com:3-3/a/b/c/d", true),
320
imtest("http://*.foo.com:3-99/a/b/-", "http://www.foo.com:55-100/a/b/c/d", false),
321
imtest("http://*.foo.com:-44/a/b/-", "http://www.foo.com:1/a/b/c/d", true),
322
imtest("http://*.foo.com:-44/a/b/-", "http://www.foo.com:1-10/a/b/c/d", true),
323
imtest("http://*.foo.com:-44/a/b/-", "http://www.foo.com:44/a/b/c/d", true),
324
imtest("http://*.foo.com:-44/a/b/-", "http://www.foo.com:45/a/b/c/d", false),
325
imtest("http://www.foo.com:70-90/a/b", "http://www.foo.com/a/b", true),
326
imtest("https://www.foo.com/a/b", "https://www.foo.com:80/a/b", false),
327
imtest("https://www.foo.com:70-90/a/b", "https://www.foo.com/a/b", false),
328
imtest("https://www.foo.com/a/b", "https://www.foo.com:443/a/b", true),
329
imtest("https://www.foo.com:200-500/a/b", "https://www.foo.com/a/b", true),
330
imtest("http://www.foo.com:*/a/b", "http://www.foo.com:1-12345/a/b", true),
331
imtest("http://host/a/b", "http://HOST/a/b", true),
332
333
// misc
334
imtest("https:*", "http://www.foo.com", false),
335
imtest("https:*", "http:*", false)
336
};
337
338
static final String FOO_URL = "http://www.foo.com/";
339
static final String BAR_URL = "http://www.bar.com/";
340
341
static Test[] actionImplies = {
342
actest("GET", "GET", true),
343
actest("GET", "POST", false),
344
actest("GET:", "PUT", false),
345
actest("GET:", "GET", true),
346
actest("GET,POST", "GET", true),
347
actest("GET,POST:", "GET", true),
348
actest("GET:X-Foo", "GET:x-foo", true),
349
actest("GET:X-Foo,X-bar", "GET:x-foo", true),
350
actest("GET:X-Foo", "GET:x-boo", false),
351
actest("GET:X-Foo,X-Bar", "GET:x-bar,x-foo", true),
352
actest("GET:X-Bar,X-Foo,X-Bar,Y-Foo", "GET:x-bar,x-foo", true),
353
actest("GET:*", "GET:x-bar,x-foo", true),
354
actest("*:*", "GET:x-bar,x-foo", true),
355
actest("", "GET:x-bar,x-foo", false),
356
actest("GET:x-bar,x-foo", "", true),
357
actest("", "", true),
358
actest("GET,DELETE", "GET,DELETE:x-foo", false),
359
actest(FOO_URL, BAR_URL, "", "GET:x-bar,x-foo", false),
360
actest(FOO_URL, BAR_URL, "GET:x-bar,x-foo", "", false),
361
actest(FOO_URL, BAR_URL, "", "", false)
362
};
363
364
static Test[] actionsStringTest = {
365
actionstest("", ":"),
366
actionstest(":", ":"),
367
actionstest(":X-Bar", ":X-Bar"),
368
actionstest("GET", "GET:"),
369
actionstest("get", "GET:"),
370
actionstest("GET,POST", "GET,POST:"),
371
actionstest("GET,post", "GET,POST:"),
372
actionstest("get,post", "GET,POST:"),
373
actionstest("get,post,DELETE", "DELETE,GET,POST:"),
374
actionstest("GET,POST:", "GET,POST:"),
375
actionstest("GET:X-Foo,X-bar", "GET:X-Bar,X-Foo"),
376
actionstest("GET,POST,DELETE:X-Bar,X-Foo,X-Bar,Y-Foo", "DELETE,GET,POST:X-Bar,X-Bar,X-Foo,Y-Foo")
377
};
378
379
static Test[] equalityTests = {
380
eqtest("http://www.foo.com", "http://www.FOO.CoM", true),
381
eqtest("http://[fe80:0:0::]:1-2", "HTTP://[FE80::]:1-2", true),
382
eqtest("HTTP://1.2.3.5/A/B/C", "http://1.2.3.5/A/b/C", false),
383
eqtest("HTTP://1.2.3.5/A/B/C", "HTTP://1.2.3.5/A/b/C", false),
384
eqtest("http:*", "http:*", true),
385
eqtest("http://www.foo.com/a/b", "https://www.foo.com/a/b", false),
386
eqtest("http://w.foo.com", "http://w.foo.com/", false),
387
eqtest("http://*.foo.com", "http://*.foo.com", true),
388
eqtest("http://www.foo.com/a/b", "http://www.foo.com:80/a/b", true),
389
eqtest("http://www.foo.com/a/b", "http://www.foo.com:82/a/b", false),
390
eqtest("https://www.foo.com/a/b", "https://www.foo.com:443/a/b", true),
391
eqtest("https://www.foo.com/a/b", "https://www.foo.com:444/a/b", false),
392
eqtest("http://[email protected]/bar","http://[email protected]/bar", true),
393
eqtest("http://[email protected]/bar","http://[email protected]/bar",false),
394
eqtest("http://[email protected]/bar","http://[email protected]/bar", true),
395
eqtest("http://@foo.com/bar","http://foo.com/bar", true)
396
};
397
398
static Test[] createTests = {
399
createtest("http://[email protected]/a/b/c"),
400
createtest("http://user:[email protected]/a/b/c"),
401
createtest("http://user:@foo.com/a/b/c")
402
};
403
404
static boolean failed = false;
405
406
public static void main(String args[]) throws Exception {
407
for (int i=0; i<pathImplies.length ; i++) {
408
URLImpliesTest test = (URLImpliesTest)pathImplies[i];
409
Exception caught = null;
410
boolean result = false;
411
try {
412
result = test.execute();
413
} catch (Exception e) {
414
caught = e;
415
e.printStackTrace();
416
}
417
if (!result) {
418
failed = true;
419
System.out.printf("path test %d failed: %s : %s\n", i, test.arg1,
420
test.arg2);
421
} else {
422
System.out.println ("path test " + i + " OK");
423
}
424
425
}
426
427
// new tests for functionality added in revision of API
428
429
for (int i=0; i<pathImplies2.length ; i++) {
430
URLImpliesTest test = (URLImpliesTest)pathImplies2[i];
431
Exception caught = null;
432
boolean result = false;
433
try {
434
result = test.execute();
435
} catch (Exception e) {
436
caught = e;
437
e.printStackTrace();
438
}
439
if (!result) {
440
failed = true;
441
System.out.printf("path2 test %d failed: %s : %s\n", i, test.arg1,
442
test.arg2);
443
} else {
444
System.out.println ("path2 test " + i + " OK");
445
}
446
447
}
448
449
for (int i=0; i<equalityTests.length ; i++) {
450
URLEqualityTest test = (URLEqualityTest)equalityTests[i];
451
Exception caught = null;
452
boolean result = false;
453
try {
454
result = test.execute();
455
} catch (Exception e) {
456
caught = e;
457
e.printStackTrace();
458
}
459
if (!result) {
460
failed = true;
461
System.out.printf("equality test %d failed: %s : %s\n", i, test.arg1,
462
test.arg2);
463
} else {
464
System.out.println ("equality test " + i + " OK");
465
}
466
467
}
468
469
for (int i=0; i<hashTests.length; i++) {
470
HashCodeTest test = (HashCodeTest)hashTests[i];
471
boolean result = test.execute();
472
if (!result) {
473
System.out.printf ("test failed: %s %s %d\n", test.arg1, test.arg2, test.hash);
474
failed = true;
475
} else {
476
System.out.println ("hash test " + i + " OK");
477
}
478
}
479
480
for (int i=0; i<exceptionTests.length; i++) {
481
ExTest test = (ExTest)exceptionTests[i];
482
boolean result = test.execute();
483
if (!result) {
484
System.out.println ("test failed: " + test.arg);
485
failed = true;
486
} else {
487
System.out.println ("exception test " + i + " OK");
488
}
489
}
490
491
for (int i=0; i<createTests.length; i++) {
492
CreateTest test = (CreateTest)createTests[i];
493
boolean result = test.execute();
494
if (!result) {
495
System.out.println ("test failed: " + test.arg);
496
failed = true;
497
} else {
498
System.out.println ("create test " + i + " OK");
499
}
500
}
501
502
for (int i=0; i<actionImplies.length ; i++) {
503
ActionImpliesTest test = (ActionImpliesTest)actionImplies[i];
504
Exception caught = null;
505
boolean result = false;
506
try {
507
result = test.execute();
508
} catch (Exception e) {
509
caught = e;
510
e.printStackTrace();
511
}
512
if (!result) {
513
failed = true;
514
System.out.println ("test failed: " + test.arg1 + ": " +
515
test.arg2 + " Exception: " + caught);
516
}
517
System.out.println ("action test " + i + " OK");
518
}
519
520
for (int i = 0; i < actionsStringTest.length; i++) {
521
ActionsStringTest test = (ActionsStringTest) actionsStringTest[i];
522
Exception caught = null;
523
boolean result = false;
524
try {
525
result = test.execute();
526
} catch (Exception e) {
527
caught = e;
528
}
529
if (!result) {
530
failed = true;
531
System.out.println("test failed: " + test.arg + ": "
532
+ test.expectedActions + " Exception: " + caught);
533
}
534
System.out.println("Actions String test " + i + " OK");
535
}
536
537
serializationTest("http://www.foo.com/-", "GET,DELETE:*");
538
serializationTest("https://www.foo.com/-", "POST:X-Foo");
539
serializationTest("https:*", "*:*");
540
serializationTest("http://www.foo.com/a/b/s/", "POST:X-Foo");
541
serializationTest("http://www.foo.com/a/b/s/*", "POST:X-Foo");
542
543
if (failed) {
544
throw new RuntimeException("some tests failed");
545
}
546
547
}
548
549
static void serializationTest(String name, String actions)
550
throws Exception {
551
552
URLPermission out = new URLPermission(name, actions);
553
554
ByteArrayOutputStream baos = new ByteArrayOutputStream();
555
ObjectOutputStream o = new ObjectOutputStream(baos);
556
o.writeObject(out);
557
ByteArrayInputStream bain = new ByteArrayInputStream(baos.toByteArray());
558
ObjectInputStream i = new ObjectInputStream(bain);
559
URLPermission in = (URLPermission)i.readObject();
560
if (!in.equals(out)) {
561
System.out.println ("FAIL");
562
System.out.println ("in = " + in);
563
System.out.println ("out = " + out);
564
failed = true;
565
}
566
}
567
}
568
569