blob: 07299b2ef509919157ecba253090f3655bdbf4af [file] [log] [blame]
michaelm58c16542013-10-14 22:09:15 +01001/*
2 * Copyright (c) 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
24import java.net.URLPermission;
25import java.io.*;
26
27/**
28 * @test
29 * @bug 8010464
30 */
31
32public 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 // Should throw an IAE on construction
41 static class ExTest extends Test {
42 String arg;
43 ExTest(String arg) {
44 this.arg = arg;
45 }
46
47 @Override
48 boolean execute() {
49 try {
50 URLPermission p = new URLPermission(arg);
51 return false;
52 } catch (IllegalArgumentException e) {
53 return true;
54 }
55 }
56 };
57
58 static ExTest extest(String arg) {
59 return new ExTest(arg);
60 }
61
62 // Tests URL part of implies() method. This is the main test.
63 static class URLImpliesTest extends Test {
64 String arg1, arg2;
65
66 URLImpliesTest(String arg1, String arg2, boolean expected) {
67 this.arg1 = arg1;
68 this.arg2 = arg2;
69 this.expected = expected;
70 }
71
72 boolean execute() {
73 URLPermission p1 = new URLPermission (arg1, "GET:*");
74 URLPermission p2 = new URLPermission (arg2, "GET:*");
75 boolean result = p1.implies(p2);
76 if (result != expected) {
77 System.out.println("p1 = " + p1);
78 System.out.println("p2 = " + p2);
79 }
80 return result == expected;
81 }
82 };
83
84 static URLImpliesTest imtest(String arg1, String arg2, boolean expected) {
85 return new URLImpliesTest(arg1, arg2, expected);
86 }
87
88 static class ActionImpliesTest extends Test {
89 String arg1, arg2;
90
91 ActionImpliesTest(String arg1, String arg2, boolean expected) {
92 this.arg1 = arg1;
93 this.arg2 = arg2;
94 this.expected = expected;
95 }
96
97 @Override
98 boolean execute() {
99 String url1 = "http://www.foo.com/-";
100 String url2 = "http://www.foo.com/a/b";
101 URLPermission p1 = new URLPermission(url1, arg1);
102 URLPermission p2 = new URLPermission(url2, arg2);
103 boolean result = p1.implies(p2);
104
105 return result == expected;
106 }
107 }
108
109 static ActionImpliesTest actest(String arg1, String arg2, boolean expected) {
110 return new ActionImpliesTest(arg1, arg2, expected);
111 }
112
113 static class URLEqualityTest extends Test {
114 String arg1, arg2;
115
116 URLEqualityTest(String arg1, String arg2, boolean expected) {
117 this.arg1 = arg1;
118 this.arg2 = arg2;
119 this.expected = expected;
120 }
121
122 @Override
123 boolean execute() {
124 URLPermission p1 = new URLPermission(arg1);
125 URLPermission p2 = new URLPermission(arg2);
126 boolean result = p1.equals(p2);
127
128 return result == expected;
129 }
130 }
131
132 static URLEqualityTest eqtest(String arg1, String arg2, boolean expected) {
133 return new URLEqualityTest(arg1, arg2, expected);
134 }
135
136 static Test[] pathImplies = {
137 // single
138 imtest("http://www.foo.com/", "http://www.foo.com/", true),
139 imtest("http://www.bar.com/", "http://www.foo.com/", false),
140 imtest("http://www.foo.com/a/b", "http://www.foo.com/", false),
141 imtest("http://www.foo.com/a/b", "http://www.foo.com/a/b/c", false),
142 // wildcard
143 imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c", true),
144 imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/*", true),
145 imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c#frag", true),
146 imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c#frag?foo=foo", true),
147 imtest("http://www.foo.com/a/b/*", "http://www.foo.com/b/b/c", false),
148 imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c.html", true),
149 imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c.html", true),
150 imtest("http://www.foo.com/a/b/*", "https://www.foo.com/a/b/c", false),
151 // recursive
152 imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/-", true),
153 imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c", true),
154 imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c#frag", true),
155 imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c#frag?foo=foo", true),
156 imtest("http://www.foo.com/a/b/-", "http://www.foo.com/b/b/c", false),
157 imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c.html", true),
158 imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c.html", true),
159 imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c/d/e.html", true),
160 imtest("https://www.foo.com/a/b/-", "http://www.foo.com/a/b/c/d/e.html", false),
161 imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c/d/e#frag", true),
162 imtest("http://www.foo.com/a/b/-", "https://www.foo.com/a/b/c", false),
163 // special cases
164 imtest("http:*", "https://www.foo.com/a/b/c", false),
165 imtest("http:*", "http://www.foo.com/a/b/c", true),
166 imtest("http:*", "http://foo/bar", true),
167 imtest("http://foo/bar", "https://foo/bar", false)
168 };
169
170 // new functionality
171
172 static Test[] exceptionTests = {
173 extest("http://1.2.3.4.5/a/b/c"),
174 extest("http://www.*.com"),
175 //extest("http://www.foo.com:1-X"),
176 extest("http://[foo.com]:99"),
177 extest("http://[fec0::X]:99"),
178 extest("http:")
179 };
180
181 static Test[] pathImplies2 = {
182 imtest("http://[FE80::]:99", "http://[fe80:0::]:99", true),
183
184 // hostnames
185 imtest("http://*.foo.com/a/b/-", "http://www.foo.com/a/b/c/d", true),
186 imtest("http://*.foo.com/a/b/-", "http://www.bar.com/a/b/c/d", false),
187 imtest("http://*.foo.com/a/b/-", "http://www.biz.bar.foo.com/a/b/c/d", true),
188 imtest("http://*.foo.com/a/b/-", "http://www.biz.bar.foo.como/a/b/c/d", false),
189 imtest("http://*/a/b/-", "http://www.biz.bar.foo.fuzz/a/b/c/d", true),
190 imtest("http://*/a/b/-", "http://*/a/b/c/d", true),
191 imtest("http://*.foo.com/a/b/-", "http://*/a/b/c/d", false),
192 imtest("http:*", "http://*/a/b/c/d", true),
193
194 // literal IPv4 addresses
195 imtest("http://1.2.3.4/a/b/-", "http://www.biz.bar.foo.com/a/b/c/d", false),
196 imtest("http://1.2.3.4/a/b/-", "http://1.2.3.4/a/b/c/d", true),
197 imtest("http://1.2.3.4/a/b/-", "http://1.2.88.4/a/b/c/d", false),
198 imtest("http:*", "http://1.2.88.4/a/b/c/d", true),
199
200 // literal IPv6 addresses
201 imtest("http://[fe80::]/a/b/-", "http://[fe80::0]/a/b/c", true),
202 imtest("http://[fe80::]/a/b/-", "http://[fe80::3]/a/b/c", false),
203 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),
204 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),
205 imtest("http://[1::2]/a/b/-", "http://[1:0:0:0::2]/a/b/c", true),
206 imtest("http://[1::2]/a/b/-", "http://[1:0:0:0::3]/a/b/c", false),
207 imtest("http://[FE80::]:99", "http://[fe80:0::]:99", true),
208 imtest("http:*", "http://[fe80:0::]:99", true),
209
210 // portranges
211 imtest("http://*.foo.com:1-2/a/b/-", "http://www.foo.com:1/a/b/c/d", true),
212 imtest("http://*.foo.com:1-2/a/b/-", "http://www.foo.com:3/a/b/c/d", false),
213 imtest("http://*.foo.com:3-/a/b/-", "http://www.foo.com:1/a/b/c/d", false),
214 imtest("http://*.foo.com:3-/a/b/-", "http://www.foo.com:4-5/a/b/c/d", true),
215 imtest("http://*.foo.com:3-/a/b/-", "http://www.foo.com:3-3/a/b/c/d", true),
216 imtest("http://*.foo.com:3-99/a/b/-", "http://www.foo.com:55-100/a/b/c/d", false),
217 imtest("http://*.foo.com:-44/a/b/-", "http://www.foo.com:1/a/b/c/d", true),
218 imtest("http://*.foo.com:-44/a/b/-", "http://www.foo.com:1-10/a/b/c/d", true),
219 imtest("http://*.foo.com:-44/a/b/-", "http://www.foo.com:44/a/b/c/d", true),
220 imtest("http://*.foo.com:-44/a/b/-", "http://www.foo.com:45/a/b/c/d", false),
221 imtest("http://www.foo.com:70-90/a/b", "http://www.foo.com/a/b", true),
222 imtest("https://www.foo.com/a/b", "https://www.foo.com:80/a/b", false),
223 imtest("https://www.foo.com:70-90/a/b", "https://www.foo.com/a/b", false),
224 imtest("https://www.foo.com/a/b", "https://www.foo.com:443/a/b", true),
225 imtest("https://www.foo.com:200-500/a/b", "https://www.foo.com/a/b", true),
226 imtest("http://www.foo.com:*/a/b", "http://www.foo.com:1-12345/a/b", true),
227
228 // misc
229 imtest("https:*", "http://www.foo.com", false),
230 imtest("https:*", "http:*", false)
231 };
232
233 static Test[] actionImplies = {
234 actest("GET", "GET", true),
235 actest("GET", "POST", false),
236 actest("GET:", "PUT", false),
237 actest("GET:", "GET", true),
238 actest("GET,POST", "GET", true),
239 actest("GET,POST:", "GET", true),
240 actest("GET:X-Foo", "GET:x-foo", true),
241 actest("GET:X-Foo,X-bar", "GET:x-foo", true),
242 actest("GET:X-Foo", "GET:x-boo", false),
243 actest("GET:X-Foo,X-Bar", "GET:x-bar,x-foo", true),
244 actest("GET:X-Bar,X-Foo,X-Bar,Y-Foo", "GET:x-bar,x-foo", true),
245 actest("GET:*", "GET:x-bar,x-foo", true),
246 actest("*:*", "GET:x-bar,x-foo", true)
247 };
248
249 static Test[] equalityTests = {
250 eqtest("http://www.foo.com", "http://www.FOO.CoM", true),
251 eqtest("http://[fe80:0:0::]:1-2", "HTTP://[FE80::]:1-2", true),
252 eqtest("HTTP://1.2.3.5/A/B/C", "http://1.2.3.5/A/b/C", false),
253 eqtest("HTTP://1.2.3.5/A/B/C", "HTTP://1.2.3.5/A/b/C", false),
254 eqtest("http:*", "http:*", true),
255 eqtest("http://www.foo.com/a/b", "https://www.foo.com/a/b", false),
256 eqtest("http://w.foo.com", "http://w.foo.com/", false),
257 eqtest("http://*.foo.com", "http://*.foo.com", true),
258 eqtest("http://www.foo.com/a/b", "http://www.foo.com:80/a/b", true),
259 eqtest("http://www.foo.com/a/b", "http://www.foo.com:82/a/b", false),
260 eqtest("https://www.foo.com/a/b", "https://www.foo.com:443/a/b", true),
261 eqtest("https://www.foo.com/a/b", "https://www.foo.com:444/a/b", false),
262 };
263
264 static boolean failed = false;
265
266 public static void main(String args[]) throws Exception {
267 for (int i=0; i<pathImplies.length ; i++) {
268 URLImpliesTest test = (URLImpliesTest)pathImplies[i];
269 Exception caught = null;
270 boolean result = false;
271 try {
272 result = test.execute();
273 } catch (Exception e) {
274 caught = e;
275 e.printStackTrace();
276 }
277 if (!result) {
278 failed = true;
279 System.out.printf("path test %d failed: %s : %s\n", i, test.arg1,
280 test.arg2);
281 } else {
282 System.out.println ("path test " + i + " OK");
283 }
284
285 }
286
287 // new tests for functionality added in revision of API
288
289 for (int i=0; i<pathImplies2.length ; i++) {
290 URLImpliesTest test = (URLImpliesTest)pathImplies2[i];
291 Exception caught = null;
292 boolean result = false;
293 try {
294 result = test.execute();
295 } catch (Exception e) {
296 caught = e;
297 e.printStackTrace();
298 }
299 if (!result) {
300 failed = true;
301 System.out.printf("path2 test %d failed: %s : %s\n", i, test.arg1,
302 test.arg2);
303 } else {
304 System.out.println ("path2 test " + i + " OK");
305 }
306
307 }
308
309 for (int i=0; i<equalityTests.length ; i++) {
310 URLEqualityTest test = (URLEqualityTest)equalityTests[i];
311 Exception caught = null;
312 boolean result = false;
313 try {
314 result = test.execute();
315 } catch (Exception e) {
316 caught = e;
317 e.printStackTrace();
318 }
319 if (!result) {
320 failed = true;
321 System.out.printf("equality test %d failed: %s : %s\n", i, test.arg1,
322 test.arg2);
323 } else {
324 System.out.println ("equality test " + i + " OK");
325 }
326
327 }
328
329 for (int i=0; i<exceptionTests.length; i++) {
330 ExTest test = (ExTest)exceptionTests[i];
331 boolean result = test.execute();
332 if (!result) {
333 System.out.println ("test failed: " + test.arg);
334 failed = true;
335 } else {
336 System.out.println ("exception test " + i + " OK");
337 }
338 }
339
340 for (int i=0; i<actionImplies.length ; i++) {
341 ActionImpliesTest test = (ActionImpliesTest)actionImplies[i];
342 Exception caught = null;
343 boolean result = false;
344 try {
345 result = test.execute();
346 } catch (Exception e) {
347 caught = e;
348 e.printStackTrace();
349 }
350 if (!result) {
351 failed = true;
352 System.out.println ("test failed: " + test.arg1 + ": " +
353 test.arg2 + " Exception: " + caught);
354 }
355 System.out.println ("action test " + i + " OK");
356 }
357
358 serializationTest("http://www.foo.com/-", "GET,DELETE:*");
359 serializationTest("https://www.foo.com/-", "POST:X-Foo");
360 serializationTest("https:*", "*:*");
361 serializationTest("http://www.foo.com/a/b/s/", "POST:X-Foo");
362 serializationTest("http://www.foo.com/a/b/s/*", "POST:X-Foo");
363
364 if (failed) {
365 throw new RuntimeException("some tests failed");
366 }
367
368 }
369
370 static void serializationTest(String name, String actions)
371 throws Exception {
372
373 URLPermission out = new URLPermission(name, actions);
374
375 ByteArrayOutputStream baos = new ByteArrayOutputStream();
376 ObjectOutputStream o = new ObjectOutputStream(baos);
377 o.writeObject(out);
378 ByteArrayInputStream bain = new ByteArrayInputStream(baos.toByteArray());
379 ObjectInputStream i = new ObjectInputStream(bain);
380 URLPermission in = (URLPermission)i.readObject();
381 if (!in.equals(out)) {
382 System.out.println ("FAIL");
383 System.out.println ("in = " + in);
384 System.out.println ("out = " + out);
385 failed = true;
386 }
387 }
388}