Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/URLPermission/URLPermissionTest.java
47217 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.net.URLPermission;24import java.io.*;2526/**27* @test28* @bug 8010464 8027570 8027687 802935429*/3031public class URLPermissionTest {3233// super class for all test types34abstract static class Test {35boolean expected;36abstract boolean execute();37};3839// Instantiation: should succeed40static class CreateTest extends Test {41String arg;42CreateTest(String arg) {43this.arg = arg;44}4546@Override47boolean execute() {48try {49URLPermission p = new URLPermission(arg);50return true;51} catch (Exception e) {52return false;53}54}55};5657static CreateTest createtest(String arg) {58return new CreateTest(arg);59}6061// Should throw an IAE on construction6263static class ExTest extends Test {64String arg;65ExTest(String arg) {66this.arg = arg;67}6869@Override70boolean execute() {71try {72URLPermission p = new URLPermission(arg);73return false;74} catch (IllegalArgumentException e) {75return true;76}77}78};7980static ExTest extest(String arg) {81return new ExTest(arg);82}8384// Tests URL part of implies() method. This is the main test.85static class URLImpliesTest extends Test {86String arg1, arg2;8788URLImpliesTest(String arg1, String arg2, boolean expected) {89this.arg1 = arg1;90this.arg2 = arg2;91this.expected = expected;92}9394boolean execute() {95URLPermission p1 = new URLPermission (arg1, "GET:*");96URLPermission p2 = new URLPermission (arg2, "GET:*");97boolean result = p1.implies(p2);98if (result != expected) {99System.out.println("p1 = " + p1);100System.out.println("p2 = " + p2);101}102return result == expected;103}104};105106static URLImpliesTest imtest(String arg1, String arg2, boolean expected) {107return new URLImpliesTest(arg1, arg2, expected);108}109110static class ActionImpliesTest extends Test {111String arg1, arg2;112113ActionImpliesTest(String arg1, String arg2, boolean expected) {114this.arg1 = arg1;115this.arg2 = arg2;116this.expected = expected;117}118119@Override120boolean execute() {121String url1 = "http://www.foo.com/-";122String url2 = "http://www.foo.com/a/b";123URLPermission p1 = new URLPermission(url1, arg1);124URLPermission p2 = new URLPermission(url2, arg2);125boolean result = p1.implies(p2);126127return result == expected;128}129}130131static ActionImpliesTest actest(String arg1, String arg2, boolean expected) {132return new ActionImpliesTest(arg1, arg2, expected);133}134135static class HashCodeTest extends Test {136String arg1, arg2;137int hash;138139HashCodeTest(String arg1, String arg2, int hash) {140this.arg1 = arg1;141this.arg2 = arg2;142this.hash = hash;143}144145@Override146boolean execute() {147URLPermission p = new URLPermission(arg1, arg2);148int h = p.hashCode();149return h == hash;150}151}152153static HashCodeTest hashtest(String arg1, String arg2, int expected) {154return new HashCodeTest(arg1, arg2, expected);155}156157static class URLEqualityTest extends Test {158String arg1, arg2;159160URLEqualityTest(String arg1, String arg2, boolean expected) {161this.arg1 = arg1;162this.arg2 = arg2;163this.expected = expected;164}165166@Override167boolean execute() {168URLPermission p1 = new URLPermission(arg1);169URLPermission p2 = new URLPermission(arg2);170boolean result = p1.equals(p2);171172return result == expected;173}174}175176static URLEqualityTest eqtest(String arg1, String arg2, boolean expected) {177return new URLEqualityTest(arg1, arg2, expected);178}179180static Test[] pathImplies = {181// single182imtest("http://www.foo.com/", "http://www.foo.com/", true),183imtest("http://www.bar.com/", "http://www.foo.com/", false),184imtest("http://www.foo.com/a/b", "http://www.foo.com/", false),185imtest("http://www.foo.com/a/b", "http://www.foo.com/a/b/c", false),186// wildcard187imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c", true),188imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/*", true),189imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c#frag", true),190imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c#frag?foo=foo", true),191imtest("http://www.foo.com/a/b/*", "http://www.foo.com/b/b/c", false),192imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c.html", true),193imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c.html", true),194imtest("http://www.foo.com/a/b/*", "https://www.foo.com/a/b/c", false),195// recursive196imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/-", true),197imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c", true),198imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c#frag", true),199imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c#frag?foo=foo", true),200imtest("http://www.foo.com/a/b/-", "http://www.foo.com/b/b/c", false),201imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c.html", true),202imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c.html", true),203imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c/d/e.html", true),204imtest("https://www.foo.com/a/b/-", "http://www.foo.com/a/b/c/d/e.html", false),205imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c/d/e#frag", true),206imtest("http://www.foo.com/a/b/-", "https://www.foo.com/a/b/c", false),207// special cases208imtest("http:*", "https://www.foo.com/a/b/c", false),209imtest("http:*", "http://www.foo.com/a/b/c", true),210imtest("http:*", "http://foo/bar", true),211imtest("http://WWW.foO.cOM/a/b/*", "http://wwW.foo.com/a/b/c", true),212imtest("http://wWw.fOo.cOm/a/b/*", "http://Www.foo.com/a/b/*", true),213imtest("http://www.FOO.com/", "http://www.foo.COM/", true),214imtest("http://66ww-w.F-O012O.com/", "http://66ww-w.f-o012o.COM/",true),215imtest("http://xn--ire-9la.com/", "http://xn--ire-9la.COM/", true),216imtest("http://x/", "http://X/", true),217imtest("http://x/", "http://x/", true),218imtest("http://X/", "http://X/", true),219imtest("http://foo/bar", "https://foo/bar", false)220};221222// new functionality223224static Test[] exceptionTests = {225extest("http://1.2.3.4.5/a/b/c"),226extest("http://www.*.com"),227extest("http://[foo.com]:99"),228extest("http://[fec0::X]:99"),229extest("http:\\www.foo.com"),230extest("http://w_09ww.foo.com"),231extest("http://w&09ww.foo.com/p"),232extest("http://www+foo.com"),233extest("http:")234};235236static Test[] hashTests = {237hashtest("http://www.foo.com/path", "GET:X-Foo", 388644203),238hashtest("http:*", "*:*", 3255810)239};240241static Test[] pathImplies2 = {242imtest("http://[FE80::]:99", "http://[fe80:0::]:99", true),243244// hostnames245imtest("http://*.foo.com/a/b/-", "http://www.foo.com/a/b/c/d", true),246imtest("http://*.foo.com/a/b/-", "http://www.bar.com/a/b/c/d", false),247imtest("http://*.foo.com/a/b/-", "http://www.biz.bar.foo.com/a/b/c/d", true),248imtest("http://*.foo.com/a/b/-", "http://www.biz.bar.foo.como/a/b/c/d", false),249imtest("http://*/a/b/-", "http://www.biz.bar.foo.fuzz/a/b/c/d", true),250imtest("http://*/a/b/-", "http://*/a/b/c/d", true),251imtest("http://*.foo.com/a/b/-", "http://*/a/b/c/d", false),252imtest("http:*", "http://*/a/b/c/d", true),253254// literal IPv4 addresses255imtest("http://1.2.3.4/a/b/-", "http://www.biz.bar.foo.com/a/b/c/d", false),256imtest("http://1.2.3.4/a/b/-", "http://1.2.3.4/a/b/c/d", true),257imtest("http://1.2.3.4/a/b/-", "http://1.2.88.4/a/b/c/d", false),258imtest("http:*", "http://1.2.88.4/a/b/c/d", true),259260// literal IPv6 addresses261imtest("http://[fe80::]/a/b/-", "http://[fe80::0]/a/b/c", true),262imtest("http://[fe80::]/a/b/-", "http://[fe80::3]/a/b/c", false),263imtest("http://[1:2:3:4:5:6:7:8]/a/b/-","http://[1:002:03:4:0005:6:07:8]/a/b/c", true),264imtest("http://[1:2:3:4:5:6:7:8]/a/b/-","http://[1:002:03:4:0033:6:07:8]/a/b/c", false),265imtest("http://[1::2]/a/b/-", "http://[1:0:0:0::2]/a/b/c", true),266imtest("http://[1::2]/a/b/-", "http://[1:0:0:0::3]/a/b/c", false),267imtest("http://[FE80::]:99", "http://[fe80:0::]:99", true),268imtest("http:*", "http://[fe80:0::]:99", true),269270// portranges271imtest("http://*.foo.com:1-2/a/b/-", "http://www.foo.com:1/a/b/c/d", true),272imtest("http://*.foo.com:1-2/a/b/-", "http://www.foo.com:3/a/b/c/d", false),273imtest("http://*.foo.com:3-/a/b/-", "http://www.foo.com:1/a/b/c/d", false),274imtest("http://*.foo.com:3-/a/b/-", "http://www.foo.com:4-5/a/b/c/d", true),275imtest("http://*.foo.com:3-/a/b/-", "http://www.foo.com:3-3/a/b/c/d", true),276imtest("http://*.foo.com:3-99/a/b/-", "http://www.foo.com:55-100/a/b/c/d", false),277imtest("http://*.foo.com:-44/a/b/-", "http://www.foo.com:1/a/b/c/d", true),278imtest("http://*.foo.com:-44/a/b/-", "http://www.foo.com:1-10/a/b/c/d", true),279imtest("http://*.foo.com:-44/a/b/-", "http://www.foo.com:44/a/b/c/d", true),280imtest("http://*.foo.com:-44/a/b/-", "http://www.foo.com:45/a/b/c/d", false),281imtest("http://www.foo.com:70-90/a/b", "http://www.foo.com/a/b", true),282imtest("https://www.foo.com/a/b", "https://www.foo.com:80/a/b", false),283imtest("https://www.foo.com:70-90/a/b", "https://www.foo.com/a/b", false),284imtest("https://www.foo.com/a/b", "https://www.foo.com:443/a/b", true),285imtest("https://www.foo.com:200-500/a/b", "https://www.foo.com/a/b", true),286imtest("http://www.foo.com:*/a/b", "http://www.foo.com:1-12345/a/b", true),287imtest("http://host/a/b", "http://HOST/a/b", true),288289// misc290imtest("https:*", "http://www.foo.com", false),291imtest("https:*", "http:*", false)292};293294static Test[] actionImplies = {295actest("GET", "GET", true),296actest("GET", "POST", false),297actest("GET:", "PUT", false),298actest("GET:", "GET", true),299actest("GET,POST", "GET", true),300actest("GET,POST:", "GET", true),301actest("GET:X-Foo", "GET:x-foo", true),302actest("GET:X-Foo,X-bar", "GET:x-foo", true),303actest("GET:X-Foo", "GET:x-boo", false),304actest("GET:X-Foo,X-Bar", "GET:x-bar,x-foo", true),305actest("GET:X-Bar,X-Foo,X-Bar,Y-Foo", "GET:x-bar,x-foo", true),306actest("GET:*", "GET:x-bar,x-foo", true),307actest("*:*", "GET:x-bar,x-foo", true)308};309310static Test[] equalityTests = {311eqtest("http://www.foo.com", "http://www.FOO.CoM", true),312eqtest("http://[fe80:0:0::]:1-2", "HTTP://[FE80::]:1-2", true),313eqtest("HTTP://1.2.3.5/A/B/C", "http://1.2.3.5/A/b/C", false),314eqtest("HTTP://1.2.3.5/A/B/C", "HTTP://1.2.3.5/A/b/C", false),315eqtest("http:*", "http:*", true),316eqtest("http://www.foo.com/a/b", "https://www.foo.com/a/b", false),317eqtest("http://w.foo.com", "http://w.foo.com/", false),318eqtest("http://*.foo.com", "http://*.foo.com", true),319eqtest("http://www.foo.com/a/b", "http://www.foo.com:80/a/b", true),320eqtest("http://www.foo.com/a/b", "http://www.foo.com:82/a/b", false),321eqtest("https://www.foo.com/a/b", "https://www.foo.com:443/a/b", true),322eqtest("https://www.foo.com/a/b", "https://www.foo.com:444/a/b", false),323eqtest("http://[email protected]/bar","http://[email protected]/bar", true),324eqtest("http://[email protected]/bar","http://[email protected]/bar",false),325eqtest("http://[email protected]/bar","http://[email protected]/bar", true),326eqtest("http://@foo.com/bar","http://foo.com/bar", true)327};328329static Test[] createTests = {330createtest("http://[email protected]/a/b/c"),331createtest("http://user:[email protected]/a/b/c"),332createtest("http://user:@foo.com/a/b/c")333};334335static boolean failed = false;336337public static void main(String args[]) throws Exception {338for (int i=0; i<pathImplies.length ; i++) {339URLImpliesTest test = (URLImpliesTest)pathImplies[i];340Exception caught = null;341boolean result = false;342try {343result = test.execute();344} catch (Exception e) {345caught = e;346e.printStackTrace();347}348if (!result) {349failed = true;350System.out.printf("path test %d failed: %s : %s\n", i, test.arg1,351test.arg2);352} else {353System.out.println ("path test " + i + " OK");354}355356}357358// new tests for functionality added in revision of API359360for (int i=0; i<pathImplies2.length ; i++) {361URLImpliesTest test = (URLImpliesTest)pathImplies2[i];362Exception caught = null;363boolean result = false;364try {365result = test.execute();366} catch (Exception e) {367caught = e;368e.printStackTrace();369}370if (!result) {371failed = true;372System.out.printf("path2 test %d failed: %s : %s\n", i, test.arg1,373test.arg2);374} else {375System.out.println ("path2 test " + i + " OK");376}377378}379380for (int i=0; i<equalityTests.length ; i++) {381URLEqualityTest test = (URLEqualityTest)equalityTests[i];382Exception caught = null;383boolean result = false;384try {385result = test.execute();386} catch (Exception e) {387caught = e;388e.printStackTrace();389}390if (!result) {391failed = true;392System.out.printf("equality test %d failed: %s : %s\n", i, test.arg1,393test.arg2);394} else {395System.out.println ("equality test " + i + " OK");396}397398}399400for (int i=0; i<hashTests.length; i++) {401HashCodeTest test = (HashCodeTest)hashTests[i];402boolean result = test.execute();403if (!result) {404System.out.printf ("test failed: %s %s %d\n", test.arg1, test.arg2, test.hash);405failed = true;406} else {407System.out.println ("hash test " + i + " OK");408}409}410411for (int i=0; i<exceptionTests.length; i++) {412ExTest test = (ExTest)exceptionTests[i];413boolean result = test.execute();414if (!result) {415System.out.println ("test failed: " + test.arg);416failed = true;417} else {418System.out.println ("exception test " + i + " OK");419}420}421422for (int i=0; i<createTests.length; i++) {423CreateTest test = (CreateTest)createTests[i];424boolean result = test.execute();425if (!result) {426System.out.println ("test failed: " + test.arg);427failed = true;428} else {429System.out.println ("create test " + i + " OK");430}431}432433for (int i=0; i<actionImplies.length ; i++) {434ActionImpliesTest test = (ActionImpliesTest)actionImplies[i];435Exception caught = null;436boolean result = false;437try {438result = test.execute();439} catch (Exception e) {440caught = e;441e.printStackTrace();442}443if (!result) {444failed = true;445System.out.println ("test failed: " + test.arg1 + ": " +446test.arg2 + " Exception: " + caught);447}448System.out.println ("action test " + i + " OK");449}450451serializationTest("http://www.foo.com/-", "GET,DELETE:*");452serializationTest("https://www.foo.com/-", "POST:X-Foo");453serializationTest("https:*", "*:*");454serializationTest("http://www.foo.com/a/b/s/", "POST:X-Foo");455serializationTest("http://www.foo.com/a/b/s/*", "POST:X-Foo");456457if (failed) {458throw new RuntimeException("some tests failed");459}460461}462463static void serializationTest(String name, String actions)464throws Exception {465466URLPermission out = new URLPermission(name, actions);467468ByteArrayOutputStream baos = new ByteArrayOutputStream();469ObjectOutputStream o = new ObjectOutputStream(baos);470o.writeObject(out);471ByteArrayInputStream bain = new ByteArrayInputStream(baos.toByteArray());472ObjectInputStream i = new ObjectInputStream(bain);473URLPermission in = (URLPermission)i.readObject();474if (!in.equals(out)) {475System.out.println ("FAIL");476System.out.println ("in = " + in);477System.out.println ("out = " + out);478failed = true;479}480}481}482483484