Path: blob/master/test/jdk/java/net/URLPermission/URLPermissionTest.java
41149 views
/*1* Copyright (c) 2013, 2016, 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 8029354 8114860 8071660 816129129*/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;112String url1 = "http://www.foo.com/-";113String url2 = "http://www.foo.com/a/b";114115ActionImpliesTest(String arg1, String arg2, boolean expected) {116this.arg1 = arg1;117this.arg2 = arg2;118this.expected = expected;119}120121ActionImpliesTest(String ur11, String url2, String arg1, String arg2,122boolean expected) {123this.url1 = ur11;124this.url2 = url2;125this.arg1 = arg1;126this.arg2 = arg2;127this.expected = expected;128}129130@Override131boolean execute() {132URLPermission p1 = new URLPermission(url1, arg1);133URLPermission p2 = new URLPermission(url2, arg2);134boolean result = p1.implies(p2);135136return result == expected;137}138}139140static ActionsStringTest actionstest(String arg, String expectedActions) {141return new ActionsStringTest(arg, expectedActions);142}143144static class ActionsStringTest extends Test {145146String expectedActions;147String arg;148149public ActionsStringTest(String arg, String expectedActions) {150this.arg = arg;151this.expectedActions = expectedActions;152}153154@Override155boolean execute() {156String url = "http://www.foo.com/";157URLPermission urlp = new URLPermission(url, arg);158return (expectedActions.equals(urlp.getActions()));159}160}161162static ActionImpliesTest actest(String arg1, String arg2, boolean expected) {163return new ActionImpliesTest(arg1, arg2, expected);164}165166static ActionImpliesTest actest(String url1, String url2, String arg1,167String arg2, boolean expected) {168return new ActionImpliesTest(url1, url2, arg1, arg2, expected);169}170171static class HashCodeTest extends Test {172String arg1, arg2;173int hash;174175HashCodeTest(String arg1, String arg2, int hash) {176this.arg1 = arg1;177this.arg2 = arg2;178this.hash = hash;179}180181@Override182boolean execute() {183URLPermission p = new URLPermission(arg1, arg2);184int h = p.hashCode();185return h == hash;186}187}188189static HashCodeTest hashtest(String arg1, String arg2, int expected) {190return new HashCodeTest(arg1, arg2, expected);191}192193static class URLEqualityTest extends Test {194String arg1, arg2;195196URLEqualityTest(String arg1, String arg2, boolean expected) {197this.arg1 = arg1;198this.arg2 = arg2;199this.expected = expected;200}201202@Override203boolean execute() {204URLPermission p1 = new URLPermission(arg1);205URLPermission p2 = new URLPermission(arg2);206boolean result = p1.equals(p2);207208return result == expected;209}210}211212static URLEqualityTest eqtest(String arg1, String arg2, boolean expected) {213return new URLEqualityTest(arg1, arg2, expected);214}215216static Test[] pathImplies = {217// single218imtest("http://www.foo.com/", "http://www.foo.com/", true),219imtest("http://www.bar.com/", "http://www.foo.com/", false),220imtest("http://www.foo.com/a/b", "http://www.foo.com/", false),221imtest("http://www.foo.com/a/b", "http://www.foo.com/a/b/c", false),222// wildcard223imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c", true),224imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/*", true),225imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c#frag", true),226imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c#frag?foo=foo", true),227imtest("http://www.foo.com/a/b/*", "http://www.foo.com/b/b/c", false),228imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c.html", true),229imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c.html", true),230imtest("http://www.foo.com/a/b/*", "https://www.foo.com/a/b/c", false),231// recursive232imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/-", true),233imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c", true),234imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c#frag", true),235imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c#frag?foo=foo", true),236imtest("http://www.foo.com/a/b/-", "http://www.foo.com/b/b/c", false),237imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c.html", true),238imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c.html", true),239imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c/d/e.html", true),240imtest("https://www.foo.com/a/b/-", "http://www.foo.com/a/b/c/d/e.html", false),241imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c/d/e#frag", true),242imtest("http://www.foo.com/a/b/-", "https://www.foo.com/a/b/c", false),243// special cases244imtest("http:*", "https://www.foo.com/a/b/c", false),245imtest("http:*", "http://www.foo.com/a/b/c", true),246imtest("http:*", "http://foo/bar", true),247imtest("http://WWW.foO.cOM/a/b/*", "http://wwW.foo.com/a/b/c", true),248imtest("http://wWw.fOo.cOm/a/b/*", "http://Www.foo.com/a/b/*", true),249imtest("http://www.FOO.com/", "http://www.foo.COM/", true),250imtest("http://66ww-w.F-O012O.com/", "http://66ww-w.f-o012o.COM/",true),251imtest("http://xn--ire-9la.com/", "http://xn--ire-9la.COM/", true),252imtest("http://x/", "http://X/", true),253imtest("http://x/", "http://x/", true),254imtest("http://X/", "http://X/", true),255imtest("http://foo/bar", "https://foo/bar", false),256imtest("http://www.foo.com/*", "http://www.foo.com/#foo", true),257imtest("http://www.foo.com/a/*#foo", "http://www.foo.com/a/b#foo", true),258imtest("http://www.foo.com/a/-", "http://www.foo.com/a/b#foo", true),259imtest("http://www.foo.com/?q1=1&q2=2#foo", "http://www.foo.com/?q1=1&q2=2#bar", true),260imtest("http://www.foo.com/", "http://www.foo.com/?q1=1&q2=2#bar", true),261imtest("http://www.foo.com/", "http://www.foo.com?q1=1&q2=2#bar", false),262imtest("http://www.foo.com", "http://www.foo.com?q1=1&q2=2#bar", true)263};264265// new functionality266267static Test[] exceptionTests = {268extest("http://1.2.3.4.5/a/b/c"),269extest("http://www.*.com"),270extest("http://[foo.com]:99"),271extest("http://[fec0::X]:99"),272extest("http:\\www.foo.com"),273extest("http://w_09ww.foo.com"),274extest("http://w&09ww.foo.com/p"),275extest("http://www+foo.com"),276extest("http:")277};278279static Test[] hashTests = {280hashtest("http://www.foo.com/path", "GET:X-Foo", 388644203),281hashtest("http:*", "*:*", 3255810)282};283284static Test[] pathImplies2 = {285imtest("http://[FE80::]:99", "http://[fe80:0::]:99", true),286287// hostnames288imtest("http://*.foo.com/a/b/-", "http://www.foo.com/a/b/c/d", true),289imtest("http://*.foo.com/a/b/-", "http://www.bar.com/a/b/c/d", false),290imtest("http://*.foo.com/a/b/-", "http://www.biz.bar.foo.com/a/b/c/d", true),291imtest("http://*.foo.com/a/b/-", "http://www.biz.bar.foo.como/a/b/c/d", false),292imtest("http://*/a/b/-", "http://www.biz.bar.foo.fuzz/a/b/c/d", true),293imtest("http://*/a/b/-", "http://*/a/b/c/d", true),294imtest("http://*.foo.com/a/b/-", "http://*/a/b/c/d", false),295imtest("http:*", "http://*/a/b/c/d", true),296297// literal IPv4 addresses298imtest("http://1.2.3.4/a/b/-", "http://www.biz.bar.foo.com/a/b/c/d", false),299imtest("http://1.2.3.4/a/b/-", "http://1.2.3.4/a/b/c/d", true),300imtest("http://1.2.3.4/a/b/-", "http://1.2.88.4/a/b/c/d", false),301imtest("http:*", "http://1.2.88.4/a/b/c/d", true),302303// literal IPv6 addresses304imtest("http://[fe80::]/a/b/-", "http://[fe80::0]/a/b/c", true),305imtest("http://[fe80::]/a/b/-", "http://[fe80::3]/a/b/c", false),306imtest("http://[1:2:3:4:5:6:7:8]/a/b/-","http://[1:002:03:4:0005:6:07:8]/a/b/c", true),307imtest("http://[1:2:3:4:5:6:7:8]/a/b/-","http://[1:002:03:4:0033:6:07:8]/a/b/c", false),308imtest("http://[1::2]/a/b/-", "http://[1:0:0:0::2]/a/b/c", true),309imtest("http://[1::2]/a/b/-", "http://[1:0:0:0::3]/a/b/c", false),310imtest("http://[FE80::]:99", "http://[fe80:0::]:99", true),311imtest("http:*", "http://[fe80:0::]:99", true),312313// portranges314imtest("http://*.foo.com:1-2/a/b/-", "http://www.foo.com:1/a/b/c/d", true),315imtest("http://*.foo.com:1-2/a/b/-", "http://www.foo.com:3/a/b/c/d", false),316imtest("http://*.foo.com:3-/a/b/-", "http://www.foo.com:1/a/b/c/d", false),317imtest("http://*.foo.com:3-/a/b/-", "http://www.foo.com:4-5/a/b/c/d", true),318imtest("http://*.foo.com:3-/a/b/-", "http://www.foo.com:3-3/a/b/c/d", true),319imtest("http://*.foo.com:3-99/a/b/-", "http://www.foo.com:55-100/a/b/c/d", false),320imtest("http://*.foo.com:-44/a/b/-", "http://www.foo.com:1/a/b/c/d", true),321imtest("http://*.foo.com:-44/a/b/-", "http://www.foo.com:1-10/a/b/c/d", true),322imtest("http://*.foo.com:-44/a/b/-", "http://www.foo.com:44/a/b/c/d", true),323imtest("http://*.foo.com:-44/a/b/-", "http://www.foo.com:45/a/b/c/d", false),324imtest("http://www.foo.com:70-90/a/b", "http://www.foo.com/a/b", true),325imtest("https://www.foo.com/a/b", "https://www.foo.com:80/a/b", false),326imtest("https://www.foo.com:70-90/a/b", "https://www.foo.com/a/b", false),327imtest("https://www.foo.com/a/b", "https://www.foo.com:443/a/b", true),328imtest("https://www.foo.com:200-500/a/b", "https://www.foo.com/a/b", true),329imtest("http://www.foo.com:*/a/b", "http://www.foo.com:1-12345/a/b", true),330imtest("http://host/a/b", "http://HOST/a/b", true),331332// misc333imtest("https:*", "http://www.foo.com", false),334imtest("https:*", "http:*", false)335};336337static final String FOO_URL = "http://www.foo.com/";338static final String BAR_URL = "http://www.bar.com/";339340static Test[] actionImplies = {341actest("GET", "GET", true),342actest("GET", "POST", false),343actest("GET:", "PUT", false),344actest("GET:", "GET", true),345actest("GET,POST", "GET", true),346actest("GET,POST:", "GET", true),347actest("GET:X-Foo", "GET:x-foo", true),348actest("GET:X-Foo,X-bar", "GET:x-foo", true),349actest("GET:X-Foo", "GET:x-boo", false),350actest("GET:X-Foo,X-Bar", "GET:x-bar,x-foo", true),351actest("GET:X-Bar,X-Foo,X-Bar,Y-Foo", "GET:x-bar,x-foo", true),352actest("GET:*", "GET:x-bar,x-foo", true),353actest("*:*", "GET:x-bar,x-foo", true),354actest("", "GET:x-bar,x-foo", false),355actest("GET:x-bar,x-foo", "", true),356actest("", "", true),357actest("GET,DELETE", "GET,DELETE:x-foo", false),358actest(FOO_URL, BAR_URL, "", "GET:x-bar,x-foo", false),359actest(FOO_URL, BAR_URL, "GET:x-bar,x-foo", "", false),360actest(FOO_URL, BAR_URL, "", "", false)361};362363static Test[] actionsStringTest = {364actionstest("", ":"),365actionstest(":", ":"),366actionstest(":X-Bar", ":X-Bar"),367actionstest("GET", "GET:"),368actionstest("get", "GET:"),369actionstest("GET,POST", "GET,POST:"),370actionstest("GET,post", "GET,POST:"),371actionstest("get,post", "GET,POST:"),372actionstest("get,post,DELETE", "DELETE,GET,POST:"),373actionstest("GET,POST:", "GET,POST:"),374actionstest("GET:X-Foo,X-bar", "GET:X-Bar,X-Foo"),375actionstest("GET,POST,DELETE:X-Bar,X-Foo,X-Bar,Y-Foo", "DELETE,GET,POST:X-Bar,X-Bar,X-Foo,Y-Foo")376};377378static Test[] equalityTests = {379eqtest("http://www.foo.com", "http://www.FOO.CoM", true),380eqtest("http://[fe80:0:0::]:1-2", "HTTP://[FE80::]:1-2", true),381eqtest("HTTP://1.2.3.5/A/B/C", "http://1.2.3.5/A/b/C", false),382eqtest("HTTP://1.2.3.5/A/B/C", "HTTP://1.2.3.5/A/b/C", false),383eqtest("http:*", "http:*", true),384eqtest("http://www.foo.com/a/b", "https://www.foo.com/a/b", false),385eqtest("http://w.foo.com", "http://w.foo.com/", false),386eqtest("http://*.foo.com", "http://*.foo.com", true),387eqtest("http://www.foo.com/a/b", "http://www.foo.com:80/a/b", true),388eqtest("http://www.foo.com/a/b", "http://www.foo.com:82/a/b", false),389eqtest("https://www.foo.com/a/b", "https://www.foo.com:443/a/b", true),390eqtest("https://www.foo.com/a/b", "https://www.foo.com:444/a/b", false),391eqtest("http://[email protected]/bar","http://[email protected]/bar", true),392eqtest("http://[email protected]/bar","http://[email protected]/bar",false),393eqtest("http://[email protected]/bar","http://[email protected]/bar", true),394eqtest("http://@foo.com/bar","http://foo.com/bar", true)395};396397static Test[] createTests = {398createtest("http://[email protected]/a/b/c"),399createtest("http://user:[email protected]/a/b/c"),400createtest("http://user:@foo.com/a/b/c")401};402403static boolean failed = false;404405public static void main(String args[]) throws Exception {406for (int i=0; i<pathImplies.length ; i++) {407URLImpliesTest test = (URLImpliesTest)pathImplies[i];408Exception caught = null;409boolean result = false;410try {411result = test.execute();412} catch (Exception e) {413caught = e;414e.printStackTrace();415}416if (!result) {417failed = true;418System.out.printf("path test %d failed: %s : %s\n", i, test.arg1,419test.arg2);420} else {421System.out.println ("path test " + i + " OK");422}423424}425426// new tests for functionality added in revision of API427428for (int i=0; i<pathImplies2.length ; i++) {429URLImpliesTest test = (URLImpliesTest)pathImplies2[i];430Exception caught = null;431boolean result = false;432try {433result = test.execute();434} catch (Exception e) {435caught = e;436e.printStackTrace();437}438if (!result) {439failed = true;440System.out.printf("path2 test %d failed: %s : %s\n", i, test.arg1,441test.arg2);442} else {443System.out.println ("path2 test " + i + " OK");444}445446}447448for (int i=0; i<equalityTests.length ; i++) {449URLEqualityTest test = (URLEqualityTest)equalityTests[i];450Exception caught = null;451boolean result = false;452try {453result = test.execute();454} catch (Exception e) {455caught = e;456e.printStackTrace();457}458if (!result) {459failed = true;460System.out.printf("equality test %d failed: %s : %s\n", i, test.arg1,461test.arg2);462} else {463System.out.println ("equality test " + i + " OK");464}465466}467468for (int i=0; i<hashTests.length; i++) {469HashCodeTest test = (HashCodeTest)hashTests[i];470boolean result = test.execute();471if (!result) {472System.out.printf ("test failed: %s %s %d\n", test.arg1, test.arg2, test.hash);473failed = true;474} else {475System.out.println ("hash test " + i + " OK");476}477}478479for (int i=0; i<exceptionTests.length; i++) {480ExTest test = (ExTest)exceptionTests[i];481boolean result = test.execute();482if (!result) {483System.out.println ("test failed: " + test.arg);484failed = true;485} else {486System.out.println ("exception test " + i + " OK");487}488}489490for (int i=0; i<createTests.length; i++) {491CreateTest test = (CreateTest)createTests[i];492boolean result = test.execute();493if (!result) {494System.out.println ("test failed: " + test.arg);495failed = true;496} else {497System.out.println ("create test " + i + " OK");498}499}500501for (int i=0; i<actionImplies.length ; i++) {502ActionImpliesTest test = (ActionImpliesTest)actionImplies[i];503Exception caught = null;504boolean result = false;505try {506result = test.execute();507} catch (Exception e) {508caught = e;509e.printStackTrace();510}511if (!result) {512failed = true;513System.out.println ("test failed: " + test.arg1 + ": " +514test.arg2 + " Exception: " + caught);515}516System.out.println ("action test " + i + " OK");517}518519for (int i = 0; i < actionsStringTest.length; i++) {520ActionsStringTest test = (ActionsStringTest) actionsStringTest[i];521Exception caught = null;522boolean result = false;523try {524result = test.execute();525} catch (Exception e) {526caught = e;527}528if (!result) {529failed = true;530System.out.println("test failed: " + test.arg + ": "531+ test.expectedActions + " Exception: " + caught);532}533System.out.println("Actions String test " + i + " OK");534}535536serializationTest("http://www.foo.com/-", "GET,DELETE:*");537serializationTest("https://www.foo.com/-", "POST:X-Foo");538serializationTest("https:*", "*:*");539serializationTest("http://www.foo.com/a/b/s/", "POST:X-Foo");540serializationTest("http://www.foo.com/a/b/s/*", "POST:X-Foo");541542if (failed) {543throw new RuntimeException("some tests failed");544}545546}547548static void serializationTest(String name, String actions)549throws Exception {550551URLPermission out = new URLPermission(name, actions);552553ByteArrayOutputStream baos = new ByteArrayOutputStream();554ObjectOutputStream o = new ObjectOutputStream(baos);555o.writeObject(out);556ByteArrayInputStream bain = new ByteArrayInputStream(baos.toByteArray());557ObjectInputStream i = new ObjectInputStream(bain);558URLPermission in = (URLPermission)i.readObject();559if (!in.equals(out)) {560System.out.println ("FAIL");561System.out.println ("in = " + in);562System.out.println ("out = " + out);563failed = true;564}565}566}567568569