michaelm | 58c1654 | 2013-10-14 22:09:15 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 24 | import java.net.URLPermission; |
| 25 | import java.io.*; |
| 26 | |
| 27 | /** |
| 28 | * @test |
michaelm | 4f96e0f | 2013-10-30 18:37:50 +0000 | [diff] [blame^] | 29 | * @bug 8010464 8027570 |
michaelm | 58c1654 | 2013-10-14 22:09:15 +0100 | [diff] [blame] | 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 | // 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 | |
michaelm | 4f96e0f | 2013-10-30 18:37:50 +0000 | [diff] [blame^] | 113 | static class HashCodeTest extends Test { |
| 114 | String arg1, arg2; |
| 115 | int hash; |
| 116 | |
| 117 | HashCodeTest(String arg1, String arg2, int hash) { |
| 118 | this.arg1 = arg1; |
| 119 | this.arg2 = arg2; |
| 120 | this.hash = hash; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | boolean execute() { |
| 125 | URLPermission p = new URLPermission(arg1, arg2); |
| 126 | int h = p.hashCode(); |
| 127 | return h == hash; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | static HashCodeTest hashtest(String arg1, String arg2, int expected) { |
| 132 | return new HashCodeTest(arg1, arg2, expected); |
| 133 | } |
| 134 | |
michaelm | 58c1654 | 2013-10-14 22:09:15 +0100 | [diff] [blame] | 135 | static class URLEqualityTest extends Test { |
| 136 | String arg1, arg2; |
| 137 | |
| 138 | URLEqualityTest(String arg1, String arg2, boolean expected) { |
| 139 | this.arg1 = arg1; |
| 140 | this.arg2 = arg2; |
| 141 | this.expected = expected; |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | boolean execute() { |
| 146 | URLPermission p1 = new URLPermission(arg1); |
| 147 | URLPermission p2 = new URLPermission(arg2); |
| 148 | boolean result = p1.equals(p2); |
| 149 | |
| 150 | return result == expected; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | static URLEqualityTest eqtest(String arg1, String arg2, boolean expected) { |
| 155 | return new URLEqualityTest(arg1, arg2, expected); |
| 156 | } |
| 157 | |
| 158 | static Test[] pathImplies = { |
| 159 | // single |
| 160 | imtest("http://www.foo.com/", "http://www.foo.com/", true), |
| 161 | imtest("http://www.bar.com/", "http://www.foo.com/", false), |
| 162 | imtest("http://www.foo.com/a/b", "http://www.foo.com/", false), |
| 163 | imtest("http://www.foo.com/a/b", "http://www.foo.com/a/b/c", false), |
| 164 | // wildcard |
| 165 | imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c", true), |
| 166 | imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/*", true), |
| 167 | imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c#frag", true), |
| 168 | imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c#frag?foo=foo", true), |
| 169 | imtest("http://www.foo.com/a/b/*", "http://www.foo.com/b/b/c", false), |
| 170 | imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c.html", true), |
| 171 | imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c.html", true), |
| 172 | imtest("http://www.foo.com/a/b/*", "https://www.foo.com/a/b/c", false), |
| 173 | // recursive |
| 174 | imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/-", true), |
| 175 | imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c", true), |
| 176 | imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c#frag", true), |
| 177 | imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c#frag?foo=foo", true), |
| 178 | imtest("http://www.foo.com/a/b/-", "http://www.foo.com/b/b/c", false), |
| 179 | imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c.html", true), |
| 180 | imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c.html", true), |
| 181 | imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c/d/e.html", true), |
| 182 | imtest("https://www.foo.com/a/b/-", "http://www.foo.com/a/b/c/d/e.html", false), |
| 183 | imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c/d/e#frag", true), |
| 184 | imtest("http://www.foo.com/a/b/-", "https://www.foo.com/a/b/c", false), |
| 185 | // special cases |
| 186 | imtest("http:*", "https://www.foo.com/a/b/c", false), |
| 187 | imtest("http:*", "http://www.foo.com/a/b/c", true), |
| 188 | imtest("http:*", "http://foo/bar", true), |
| 189 | imtest("http://foo/bar", "https://foo/bar", false) |
| 190 | }; |
| 191 | |
| 192 | // new functionality |
| 193 | |
| 194 | static Test[] exceptionTests = { |
| 195 | extest("http://1.2.3.4.5/a/b/c"), |
| 196 | extest("http://www.*.com"), |
| 197 | //extest("http://www.foo.com:1-X"), |
| 198 | extest("http://[foo.com]:99"), |
| 199 | extest("http://[fec0::X]:99"), |
| 200 | extest("http:") |
| 201 | }; |
| 202 | |
michaelm | 4f96e0f | 2013-10-30 18:37:50 +0000 | [diff] [blame^] | 203 | static Test[] hashTests = { |
| 204 | hashtest("http://www.foo.com/path", "GET:X-Foo", 388644203), |
| 205 | hashtest("http:*", "*:*", 3255810) |
| 206 | }; |
| 207 | |
michaelm | 58c1654 | 2013-10-14 22:09:15 +0100 | [diff] [blame] | 208 | static Test[] pathImplies2 = { |
| 209 | imtest("http://[FE80::]:99", "http://[fe80:0::]:99", true), |
| 210 | |
| 211 | // hostnames |
| 212 | imtest("http://*.foo.com/a/b/-", "http://www.foo.com/a/b/c/d", true), |
| 213 | imtest("http://*.foo.com/a/b/-", "http://www.bar.com/a/b/c/d", false), |
| 214 | imtest("http://*.foo.com/a/b/-", "http://www.biz.bar.foo.com/a/b/c/d", true), |
| 215 | imtest("http://*.foo.com/a/b/-", "http://www.biz.bar.foo.como/a/b/c/d", false), |
| 216 | imtest("http://*/a/b/-", "http://www.biz.bar.foo.fuzz/a/b/c/d", true), |
| 217 | imtest("http://*/a/b/-", "http://*/a/b/c/d", true), |
| 218 | imtest("http://*.foo.com/a/b/-", "http://*/a/b/c/d", false), |
| 219 | imtest("http:*", "http://*/a/b/c/d", true), |
| 220 | |
| 221 | // literal IPv4 addresses |
| 222 | imtest("http://1.2.3.4/a/b/-", "http://www.biz.bar.foo.com/a/b/c/d", false), |
| 223 | imtest("http://1.2.3.4/a/b/-", "http://1.2.3.4/a/b/c/d", true), |
| 224 | imtest("http://1.2.3.4/a/b/-", "http://1.2.88.4/a/b/c/d", false), |
| 225 | imtest("http:*", "http://1.2.88.4/a/b/c/d", true), |
| 226 | |
| 227 | // literal IPv6 addresses |
| 228 | imtest("http://[fe80::]/a/b/-", "http://[fe80::0]/a/b/c", true), |
| 229 | imtest("http://[fe80::]/a/b/-", "http://[fe80::3]/a/b/c", false), |
| 230 | 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), |
| 231 | 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), |
| 232 | imtest("http://[1::2]/a/b/-", "http://[1:0:0:0::2]/a/b/c", true), |
| 233 | imtest("http://[1::2]/a/b/-", "http://[1:0:0:0::3]/a/b/c", false), |
| 234 | imtest("http://[FE80::]:99", "http://[fe80:0::]:99", true), |
| 235 | imtest("http:*", "http://[fe80:0::]:99", true), |
| 236 | |
| 237 | // portranges |
| 238 | imtest("http://*.foo.com:1-2/a/b/-", "http://www.foo.com:1/a/b/c/d", true), |
| 239 | imtest("http://*.foo.com:1-2/a/b/-", "http://www.foo.com:3/a/b/c/d", false), |
| 240 | imtest("http://*.foo.com:3-/a/b/-", "http://www.foo.com:1/a/b/c/d", false), |
| 241 | imtest("http://*.foo.com:3-/a/b/-", "http://www.foo.com:4-5/a/b/c/d", true), |
| 242 | imtest("http://*.foo.com:3-/a/b/-", "http://www.foo.com:3-3/a/b/c/d", true), |
| 243 | imtest("http://*.foo.com:3-99/a/b/-", "http://www.foo.com:55-100/a/b/c/d", false), |
| 244 | imtest("http://*.foo.com:-44/a/b/-", "http://www.foo.com:1/a/b/c/d", true), |
| 245 | imtest("http://*.foo.com:-44/a/b/-", "http://www.foo.com:1-10/a/b/c/d", true), |
| 246 | imtest("http://*.foo.com:-44/a/b/-", "http://www.foo.com:44/a/b/c/d", true), |
| 247 | imtest("http://*.foo.com:-44/a/b/-", "http://www.foo.com:45/a/b/c/d", false), |
| 248 | imtest("http://www.foo.com:70-90/a/b", "http://www.foo.com/a/b", true), |
| 249 | imtest("https://www.foo.com/a/b", "https://www.foo.com:80/a/b", false), |
| 250 | imtest("https://www.foo.com:70-90/a/b", "https://www.foo.com/a/b", false), |
| 251 | imtest("https://www.foo.com/a/b", "https://www.foo.com:443/a/b", true), |
| 252 | imtest("https://www.foo.com:200-500/a/b", "https://www.foo.com/a/b", true), |
| 253 | imtest("http://www.foo.com:*/a/b", "http://www.foo.com:1-12345/a/b", true), |
| 254 | |
| 255 | // misc |
| 256 | imtest("https:*", "http://www.foo.com", false), |
| 257 | imtest("https:*", "http:*", false) |
| 258 | }; |
| 259 | |
| 260 | static Test[] actionImplies = { |
| 261 | actest("GET", "GET", true), |
| 262 | actest("GET", "POST", false), |
| 263 | actest("GET:", "PUT", false), |
| 264 | actest("GET:", "GET", true), |
| 265 | actest("GET,POST", "GET", true), |
| 266 | actest("GET,POST:", "GET", true), |
| 267 | actest("GET:X-Foo", "GET:x-foo", true), |
| 268 | actest("GET:X-Foo,X-bar", "GET:x-foo", true), |
| 269 | actest("GET:X-Foo", "GET:x-boo", false), |
| 270 | actest("GET:X-Foo,X-Bar", "GET:x-bar,x-foo", true), |
| 271 | actest("GET:X-Bar,X-Foo,X-Bar,Y-Foo", "GET:x-bar,x-foo", true), |
| 272 | actest("GET:*", "GET:x-bar,x-foo", true), |
| 273 | actest("*:*", "GET:x-bar,x-foo", true) |
| 274 | }; |
| 275 | |
| 276 | static Test[] equalityTests = { |
| 277 | eqtest("http://www.foo.com", "http://www.FOO.CoM", true), |
| 278 | eqtest("http://[fe80:0:0::]:1-2", "HTTP://[FE80::]:1-2", true), |
| 279 | eqtest("HTTP://1.2.3.5/A/B/C", "http://1.2.3.5/A/b/C", false), |
| 280 | eqtest("HTTP://1.2.3.5/A/B/C", "HTTP://1.2.3.5/A/b/C", false), |
| 281 | eqtest("http:*", "http:*", true), |
| 282 | eqtest("http://www.foo.com/a/b", "https://www.foo.com/a/b", false), |
| 283 | eqtest("http://w.foo.com", "http://w.foo.com/", false), |
| 284 | eqtest("http://*.foo.com", "http://*.foo.com", true), |
| 285 | eqtest("http://www.foo.com/a/b", "http://www.foo.com:80/a/b", true), |
| 286 | eqtest("http://www.foo.com/a/b", "http://www.foo.com:82/a/b", false), |
| 287 | eqtest("https://www.foo.com/a/b", "https://www.foo.com:443/a/b", true), |
| 288 | eqtest("https://www.foo.com/a/b", "https://www.foo.com:444/a/b", false), |
| 289 | }; |
| 290 | |
| 291 | static boolean failed = false; |
| 292 | |
| 293 | public static void main(String args[]) throws Exception { |
| 294 | for (int i=0; i<pathImplies.length ; i++) { |
| 295 | URLImpliesTest test = (URLImpliesTest)pathImplies[i]; |
| 296 | Exception caught = null; |
| 297 | boolean result = false; |
| 298 | try { |
| 299 | result = test.execute(); |
| 300 | } catch (Exception e) { |
| 301 | caught = e; |
| 302 | e.printStackTrace(); |
| 303 | } |
| 304 | if (!result) { |
| 305 | failed = true; |
| 306 | System.out.printf("path test %d failed: %s : %s\n", i, test.arg1, |
| 307 | test.arg2); |
| 308 | } else { |
| 309 | System.out.println ("path test " + i + " OK"); |
| 310 | } |
| 311 | |
| 312 | } |
| 313 | |
| 314 | // new tests for functionality added in revision of API |
| 315 | |
| 316 | for (int i=0; i<pathImplies2.length ; i++) { |
| 317 | URLImpliesTest test = (URLImpliesTest)pathImplies2[i]; |
| 318 | Exception caught = null; |
| 319 | boolean result = false; |
| 320 | try { |
| 321 | result = test.execute(); |
| 322 | } catch (Exception e) { |
| 323 | caught = e; |
| 324 | e.printStackTrace(); |
| 325 | } |
| 326 | if (!result) { |
| 327 | failed = true; |
| 328 | System.out.printf("path2 test %d failed: %s : %s\n", i, test.arg1, |
| 329 | test.arg2); |
| 330 | } else { |
| 331 | System.out.println ("path2 test " + i + " OK"); |
| 332 | } |
| 333 | |
| 334 | } |
| 335 | |
| 336 | for (int i=0; i<equalityTests.length ; i++) { |
| 337 | URLEqualityTest test = (URLEqualityTest)equalityTests[i]; |
| 338 | Exception caught = null; |
| 339 | boolean result = false; |
| 340 | try { |
| 341 | result = test.execute(); |
| 342 | } catch (Exception e) { |
| 343 | caught = e; |
| 344 | e.printStackTrace(); |
| 345 | } |
| 346 | if (!result) { |
| 347 | failed = true; |
| 348 | System.out.printf("equality test %d failed: %s : %s\n", i, test.arg1, |
| 349 | test.arg2); |
| 350 | } else { |
| 351 | System.out.println ("equality test " + i + " OK"); |
| 352 | } |
| 353 | |
| 354 | } |
| 355 | |
michaelm | 4f96e0f | 2013-10-30 18:37:50 +0000 | [diff] [blame^] | 356 | for (int i=0; i<hashTests.length; i++) { |
| 357 | HashCodeTest test = (HashCodeTest)hashTests[i]; |
| 358 | boolean result = test.execute(); |
| 359 | if (!result) { |
| 360 | System.out.printf ("test failed: %s %s %d\n", test.arg1, test.arg2, test.hash); |
| 361 | failed = true; |
| 362 | } else { |
| 363 | System.out.println ("hash test " + i + " OK"); |
| 364 | } |
| 365 | } |
| 366 | |
michaelm | 58c1654 | 2013-10-14 22:09:15 +0100 | [diff] [blame] | 367 | for (int i=0; i<exceptionTests.length; i++) { |
| 368 | ExTest test = (ExTest)exceptionTests[i]; |
| 369 | boolean result = test.execute(); |
| 370 | if (!result) { |
| 371 | System.out.println ("test failed: " + test.arg); |
| 372 | failed = true; |
| 373 | } else { |
| 374 | System.out.println ("exception test " + i + " OK"); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | for (int i=0; i<actionImplies.length ; i++) { |
| 379 | ActionImpliesTest test = (ActionImpliesTest)actionImplies[i]; |
| 380 | Exception caught = null; |
| 381 | boolean result = false; |
| 382 | try { |
| 383 | result = test.execute(); |
| 384 | } catch (Exception e) { |
| 385 | caught = e; |
| 386 | e.printStackTrace(); |
| 387 | } |
| 388 | if (!result) { |
| 389 | failed = true; |
| 390 | System.out.println ("test failed: " + test.arg1 + ": " + |
| 391 | test.arg2 + " Exception: " + caught); |
| 392 | } |
| 393 | System.out.println ("action test " + i + " OK"); |
| 394 | } |
| 395 | |
| 396 | serializationTest("http://www.foo.com/-", "GET,DELETE:*"); |
| 397 | serializationTest("https://www.foo.com/-", "POST:X-Foo"); |
| 398 | serializationTest("https:*", "*:*"); |
| 399 | serializationTest("http://www.foo.com/a/b/s/", "POST:X-Foo"); |
| 400 | serializationTest("http://www.foo.com/a/b/s/*", "POST:X-Foo"); |
| 401 | |
| 402 | if (failed) { |
| 403 | throw new RuntimeException("some tests failed"); |
| 404 | } |
| 405 | |
| 406 | } |
| 407 | |
| 408 | static void serializationTest(String name, String actions) |
| 409 | throws Exception { |
| 410 | |
| 411 | URLPermission out = new URLPermission(name, actions); |
| 412 | |
| 413 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 414 | ObjectOutputStream o = new ObjectOutputStream(baos); |
| 415 | o.writeObject(out); |
| 416 | ByteArrayInputStream bain = new ByteArrayInputStream(baos.toByteArray()); |
| 417 | ObjectInputStream i = new ObjectInputStream(bain); |
| 418 | URLPermission in = (URLPermission)i.readObject(); |
| 419 | if (!in.equals(out)) { |
| 420 | System.out.println ("FAIL"); |
| 421 | System.out.println ("in = " + in); |
| 422 | System.out.println ("out = " + out); |
| 423 | failed = true; |
| 424 | } |
| 425 | } |
| 426 | } |