jdk/test/java/net/HttpURLPermission/HttpURLPermissionTest.java
changeset 20854 242194ae1563
parent 20853 505b28fe2b98
parent 20792 8d5c16e4f64c
child 20855 000e45ee4aea
equal deleted inserted replaced
20853:505b28fe2b98 20854:242194ae1563
     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.HttpURLPermission;
       
    25 import java.io.*;
       
    26 
       
    27 /**
       
    28  * @test
       
    29  * @bug 8010464
       
    30  */
       
    31 
       
    32 public class HttpURLPermissionTest {
       
    33 
       
    34     // super class for all test types
       
    35     abstract static class Test {
       
    36         boolean expected;
       
    37         abstract boolean execute();
       
    38     };
       
    39 
       
    40     // Tests URL part of implies() method. This is the main test.
       
    41     static class URLImpliesTest extends Test {
       
    42         String arg1, arg2;
       
    43 
       
    44         URLImpliesTest(String arg1, String arg2, boolean expected) {
       
    45             this.arg1 = arg1;
       
    46             this.arg2 = arg2;
       
    47             this.expected = expected;
       
    48         }
       
    49 
       
    50           boolean execute() {
       
    51             HttpURLPermission p1 = new HttpURLPermission (arg1, "GET:*");
       
    52             HttpURLPermission p2 = new HttpURLPermission (arg2, "GET:*");
       
    53             boolean result = p1.implies(p2);
       
    54             return result == expected;
       
    55         }
       
    56     };
       
    57 
       
    58     static URLImpliesTest imtest(String arg1, String arg2, boolean expected) {
       
    59         return new URLImpliesTest(arg1, arg2, expected);
       
    60     }
       
    61 
       
    62     static class ActionImpliesTest extends Test {
       
    63         String arg1, arg2;
       
    64 
       
    65         ActionImpliesTest(String arg1, String arg2, boolean expected) {
       
    66             this.arg1 = arg1;
       
    67             this.arg2 = arg2;
       
    68             this.expected = expected;
       
    69         }
       
    70 
       
    71           boolean execute() {
       
    72             String url1 = "http://www.foo.com/-";
       
    73             String url2 = "http://www.foo.com/a/b";
       
    74             HttpURLPermission p1 = new HttpURLPermission(url1, arg1);
       
    75             HttpURLPermission p2 = new HttpURLPermission(url2, arg2);
       
    76             boolean result = p1.implies(p2);
       
    77             return result == expected;
       
    78         }
       
    79     }
       
    80 
       
    81     static ActionImpliesTest actest(String arg1, String arg2, boolean expected) {
       
    82         return new ActionImpliesTest(arg1, arg2, expected);
       
    83     }
       
    84 
       
    85     static Test[] pathImplies = {
       
    86         // single
       
    87         imtest("http://www.foo.com/", "http://www.foo.com/", true),
       
    88         imtest("http://www.bar.com/", "http://www.foo.com/", false),
       
    89         imtest("http://www.foo.com/a/b", "http://www.foo.com/", false),
       
    90         imtest("http://www.foo.com/a/b", "http://www.foo.com/a/b/c", false),
       
    91         // wildcard
       
    92         imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c", true),
       
    93         imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/*", true),
       
    94         imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c#frag", true),
       
    95         imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c#frag?foo=foo", true),
       
    96         imtest("http://www.foo.com/a/b/*", "http://www.foo.com/b/b/c", false),
       
    97         imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c.html", true),
       
    98         imtest("http://www.foo.com/a/b/*", "http://www.foo.com/a/b/c.html", true),
       
    99         imtest("http://www.foo.com/a/b/*", "https://www.foo.com/a/b/c", false),
       
   100         // recursive
       
   101         imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/-", true),
       
   102         imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c", true),
       
   103         imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c#frag", true),
       
   104         imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c#frag?foo=foo", true),
       
   105         imtest("http://www.foo.com/a/b/-", "http://www.foo.com/b/b/c", false),
       
   106         imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c.html", true),
       
   107         imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c.html", true),
       
   108         imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c/d/e.html", true),
       
   109         imtest("https://www.foo.com/a/b/-", "http://www.foo.com/a/b/c/d/e.html", false),
       
   110         imtest("http://www.foo.com/a/b/-", "http://www.foo.com/a/b/c/d/e#frag", true),
       
   111         imtest("http://www.foo.com/a/b/-", "https://www.foo.com/a/b/c", false),
       
   112         // special cases
       
   113         imtest("http:*", "https://www.foo.com/a/b/c", false),
       
   114         imtest("http:*", "http://www.foo.com/a/b/c", true),
       
   115         imtest("http:*", "http://foo/bar", true),
       
   116         imtest("http://foo/bar", "https://foo/bar", false)
       
   117     };
       
   118 
       
   119     static Test[] actionImplies = {
       
   120         actest("GET", "GET", true),
       
   121         actest("GET", "POST", false),
       
   122         actest("GET:", "PUT", false),
       
   123         actest("GET:", "GET", true),
       
   124         actest("GET,POST", "GET", true),
       
   125         actest("GET,POST:", "GET", true),
       
   126         actest("GET:X-Foo", "GET:x-foo", true),
       
   127         actest("GET:X-Foo,X-bar", "GET:x-foo", true),
       
   128         actest("GET:X-Foo", "GET:x-boo", false),
       
   129         actest("GET:X-Foo,X-Bar", "GET:x-bar,x-foo", true),
       
   130         actest("GET:X-Bar,X-Foo,X-Bar,Y-Foo", "GET:x-bar,x-foo", true),
       
   131         actest("GET:*", "GET:x-bar,x-foo", true),
       
   132         actest("*:*", "GET:x-bar,x-foo", true)
       
   133     };
       
   134 
       
   135     static boolean failed = false;
       
   136 
       
   137     public static void main(String args[]) throws Exception {
       
   138         for (int i=0; i<pathImplies.length ; i++) {
       
   139             URLImpliesTest test = (URLImpliesTest)pathImplies[i];
       
   140             Exception caught = null;
       
   141             boolean result = false;
       
   142             try {
       
   143                 result = test.execute();
       
   144             } catch (Exception e) {
       
   145                 caught = e;
       
   146                 e.printStackTrace();
       
   147             }
       
   148             if (!result) {
       
   149                 failed = true;
       
   150                 System.out.println ("test failed: " + test.arg1 + ": " +
       
   151                         test.arg2 + " Exception: " + caught);
       
   152             }
       
   153             System.out.println ("path test " + i + " OK");
       
   154 
       
   155         }
       
   156         for (int i=0; i<actionImplies.length ; i++) {
       
   157             ActionImpliesTest test = (ActionImpliesTest)actionImplies[i];
       
   158             Exception caught = null;
       
   159             boolean result = false;
       
   160             try {
       
   161                 result = test.execute();
       
   162             } catch (Exception e) {
       
   163                 caught = e;
       
   164                 e.printStackTrace();
       
   165             }
       
   166             if (!result) {
       
   167                 failed = true;
       
   168                 System.out.println ("test failed: " + test.arg1 + ": " +
       
   169                         test.arg2 + " Exception: " + caught);
       
   170             }
       
   171             System.out.println ("action test " + i + " OK");
       
   172         }
       
   173 
       
   174         serializationTest("http://www.foo.com/-", "GET,DELETE:*");
       
   175         serializationTest("https://www.foo.com/-", "POST:X-Foo");
       
   176         serializationTest("https:*", "*:*");
       
   177         serializationTest("http://www.foo.com/a/b/s/", "POST:X-Foo");
       
   178         serializationTest("http://www.foo.com/a/b/s/*", "POST:X-Foo");
       
   179 
       
   180         if (failed) {
       
   181             throw new RuntimeException("some tests failed");
       
   182         }
       
   183 
       
   184     }
       
   185 
       
   186     static void serializationTest(String name, String actions)
       
   187         throws Exception {
       
   188 
       
   189         HttpURLPermission out = new HttpURLPermission(name, actions);
       
   190 
       
   191         ByteArrayOutputStream baos = new ByteArrayOutputStream();
       
   192         ObjectOutputStream o = new ObjectOutputStream(baos);
       
   193         o.writeObject(out);
       
   194         ByteArrayInputStream bain = new ByteArrayInputStream(baos.toByteArray());
       
   195         ObjectInputStream i = new ObjectInputStream(bain);
       
   196         HttpURLPermission in = (HttpURLPermission)i.readObject();
       
   197         if (!in.equals(out)) {
       
   198             System.out.println ("FAIL");
       
   199             System.out.println ("in = " + in);
       
   200             System.out.println ("out = " + out);
       
   201             failed = true;
       
   202         }
       
   203     }
       
   204 }