test/jdk/java/net/URLPermission/URLTest.java
author jjiang
Mon, 15 Oct 2018 22:47:03 +0800
changeset 52121 934969c63223
parent 47216 71c04702a3d5
child 55031 37a077319427
permissions -rw-r--r--
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary Summary: Move SimpleSSLContext.java and testkeys to test/lib/jdk/test/lib/net Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
     1
/*
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 47216
diff changeset
     2
 * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
     4
 *
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
     8
 *
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    13
 * accompanied this code).
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    14
 *
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    18
 *
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    21
 * questions.
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    22
 */
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    23
20787
ab071ce90368 8014719: HttpClient/ProxyTest.java failing with IAE HttpURLPermission.parseURI
michaelm
parents: 17473
diff changeset
    24
import java.net.URLPermission;
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    25
/*
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    26
 * @test
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    27
 * @bug 8010464
38883
d5de564f8089 8151913: Fix module dependencies in java/net tests
mli
parents: 36432
diff changeset
    28
 * @modules jdk.httpserver
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 47216
diff changeset
    29
 * @library /test/lib
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 47216
diff changeset
    30
 * @build jdk.test.lib.net.SimpleSSLContext
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    31
 * @run main/othervm URLTest
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    32
 * @summary check URLPermission with Http(s)URLConnection
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    33
 */
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    34
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    35
import java.net.*;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    36
import java.io.*;
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    37
import java.security.*;
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    38
import java.util.concurrent.*;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    39
import com.sun.net.httpserver.*;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    40
import javax.net.ssl.*;
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 47216
diff changeset
    41
import jdk.test.lib.net.SimpleSSLContext;
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    42
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    43
public class URLTest {
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    44
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    45
    static boolean failed;
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    46
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    47
    public static void main (String[] args) throws Exception {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    48
        createServers();
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    49
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    50
        try {
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    51
            // Verify without a Security Manager
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    52
            test1();
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    53
            test2();
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    54
            test3();
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    55
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    56
            // Set the security manager. Each test will set its own policy.
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    57
            Policy.setPolicy(new CustomPolicy());
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    58
            System.setSecurityManager(new SecurityManager());
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    59
            System.out.println("\n Security Manager has been set.");
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    60
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    61
            test1();
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    62
            test2();
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    63
            test3();
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    64
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    65
            if (failed)
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    66
                throw new RuntimeException("Test failed");
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    67
        } finally {
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    68
            shutdown();
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    69
        }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    70
    }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
    71
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    72
    static void test1() throws IOException {
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    73
        System.out.println("\n--- Test 1 ---");
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    74
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    75
        boolean expectException = false;
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    76
        SecurityManager sm = System.getSecurityManager();
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    77
        if (sm != null) {
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    78
            expectException = true;
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    79
            Policy.setPolicy(new CustomPolicy(
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    80
                new URLPermission("http://127.0.0.1:"+httpPort+"/foo.html", "GET:X-Foo,Z-Bar"),
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    81
                new URLPermission("https://127.0.0.1:"+httpsPort+"/foo.html", "POST:X-Fob,T-Bar")));
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    82
        }
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    83
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    84
        String url1 = "http://127.0.0.1:"+httpPort+"/foo.html";
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    85
        String url2 = "https://127.0.0.1:"+httpsPort+"/foo.html";
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    86
        String url3 = "http://127.0.0.1:"+httpPort+"/bar.html";
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    87
        String url4 = "https://127.0.0.1:"+httpsPort+"/bar.html";
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    88
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    89
        // simple positive test. Should succeed
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    90
        test(url1, "GET", "X-Foo");
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    91
        test(url1, "GET", "Z-Bar", "X-Foo");
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    92
        test(url1, "GET", "X-Foo", "Z-Bar");
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    93
        test(url1, "GET", "Z-Bar");
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    94
        test(url2, "POST", "X-Fob");
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    95
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    96
        // reverse the methods, should fail
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    97
        test(url1, "POST", "X-Foo", expectException);
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    98
        test(url2, "GET", "X-Fob", expectException);
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
    99
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   100
        // different URLs, should fail
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   101
        test(url3, "GET", "X-Foo", expectException);
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   102
        test(url4, "POST", "X-Fob", expectException);
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   103
    }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   104
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   105
    static void test2() throws IOException {
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   106
        System.out.println("\n--- Test 2 ---");
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   107
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   108
        SecurityManager sm = System.getSecurityManager();
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   109
        if (sm != null) {
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   110
            Policy.setPolicy(new CustomPolicy(
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   111
                new URLPermission("http://127.0.0.1:"+httpPort+"/*", "GET:X-Foo"),
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   112
                new URLPermission("https://127.0.0.1:"+httpsPort+"/*", "POST:X-Fob")));
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   113
        }
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   114
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   115
        String url1 = "http://127.0.0.1:"+httpPort+"/foo.html";
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   116
        String url2 = "https://127.0.0.1:"+httpsPort+"/foo.html";
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   117
        String url3 = "http://127.0.0.1:"+httpPort+"/bar.html";
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   118
        String url4 = "https://127.0.0.1:"+httpsPort+"/bar.html";
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   119
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   120
        // simple positive test. Should succeed
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   121
        test(url1, "GET", "X-Foo");
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   122
        test(url2, "POST", "X-Fob");
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   123
        test(url3, "GET", "X-Foo");
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   124
        test(url4, "POST", "X-Fob");
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   125
    }
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   126
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   127
    static void test3() throws IOException {
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   128
        System.out.println("\n--- Test 3 ---");
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   129
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   130
        boolean expectException = false;
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   131
        SecurityManager sm = System.getSecurityManager();
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   132
        if (sm != null) {
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   133
            expectException = true;
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   134
            Policy.setPolicy(new CustomPolicy(
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   135
                new URLPermission("http://127.0.0.1:"+httpPort+"/a/b/-", "DELETE,GET:X-Foo,Y-Foo"),
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   136
                new URLPermission("https://127.0.0.1:"+httpsPort+"/a/c/-", "POST:*")));
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   137
        }
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   138
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   139
        String url1 = "http://127.0.0.1:"+httpPort+"/foo.html";
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   140
        String url2 = "https://127.0.0.1:"+httpsPort+"/a/c/d/e/foo.html";
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   141
        String url3 = "http://127.0.0.1:"+httpPort+"/a/b/c";
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   142
        String url4 = "https://127.0.0.1:"+httpsPort+"/a/b/c";
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   143
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   144
        test(url1, "GET", "X-Foo", expectException);
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   145
        test(url2, "POST", "X-Zxc");
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   146
        test(url3, "DELETE", "Y-Foo");
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   147
        test(url4, "POST", "Y-Foo", expectException);
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   148
    }
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   149
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   150
    // Convenience methods to simplify previous explicit test scenarios.
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   151
    static void test(String u, String method, String header) throws IOException {
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   152
        test(u, method, header, null, false);
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   153
    }
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   154
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   155
    static void test(String u, String method, String header, boolean expectException)
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   156
        throws IOException
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   157
    {
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   158
        test(u, method, header, null, expectException);
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   159
    }
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   160
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   161
    static void test(String u, String method, String header1, String header2)
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   162
        throws IOException
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   163
    {
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   164
        test(u, method, header1, header2, false);
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   165
    }
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   166
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   167
    static void test(String u,
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   168
                     String method,
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   169
                     String header1,
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   170
                     String header2,
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   171
                     boolean expectException)
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   172
        throws IOException
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   173
    {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   174
        URL url = new URL(u);
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   175
        System.out.println("url=" + u + " method=" + method +
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   176
                           " header1=" + header1 + " header2=" + header2 +
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   177
                           " expectException=" + expectException);
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   178
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   179
        if (urlc instanceof HttpsURLConnection) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   180
            HttpsURLConnection ssl = (HttpsURLConnection)urlc;
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   181
            ssl.setHostnameVerifier((host, sess) -> true);
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   182
            ssl.setSSLSocketFactory(ctx.getSocketFactory());
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   183
        }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   184
        urlc.setRequestMethod(method);
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   185
        if (header1 != null)
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   186
            urlc.addRequestProperty(header1, "foo");
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   187
        if (header2 != null)
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   188
            urlc.addRequestProperty(header2, "bar");
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   189
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   190
        try {
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   191
            int code = urlc.getResponseCode();
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   192
            if (expectException) {
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   193
                failed = true;
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   194
                System.out.println("FAIL");
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   195
                return;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   196
            }
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   197
            if (code != 200)
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   198
                throw new RuntimeException("Unexpected response " + code);
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   199
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   200
            InputStream is = urlc.getInputStream();
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   201
            is.readAllBytes();
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   202
            is.close();
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   203
        } catch (RuntimeException e) {
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   204
            if (!expectException || !(e.getCause() instanceof SecurityException)) {
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   205
                System.out.println ("FAIL. Unexpected: " + e.getMessage());
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   206
                e.printStackTrace();
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   207
                failed = true;
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   208
                return;
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   209
            } else {
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   210
                System.out.println("Got expected exception: " + e.getMessage());
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   211
            }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   212
        }
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   213
        System.out.println ("PASS");
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   214
    }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   215
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   216
    static HttpServer httpServer;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   217
    static HttpsServer httpsServer;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   218
    static HttpContext c, cs;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   219
    static ExecutorService e, es;
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   220
    static SSLContext ctx;
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   221
    static int httpPort;
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   222
    static int httpsPort;
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   223
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   224
    static void createServers() throws Exception {
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   225
        InetSocketAddress any = new InetSocketAddress(0);
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   226
        httpServer = HttpServer.create(any, 0);
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   227
        httpsServer = HttpsServer.create(any, 0);
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   228
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   229
        OkHandler h = new OkHandler();
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   230
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   231
        c = httpServer.createContext("/", h);
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   232
        cs = httpsServer.createContext("/", h);
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   233
        e = Executors.newCachedThreadPool();
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   234
        es = Executors.newCachedThreadPool();
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   235
        httpServer.setExecutor(e);
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   236
        httpsServer.setExecutor(es);
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   237
26206
fb87c4051d65 8055747: Move SimpleSSLContext to jdk/testlibrary
michaelm
parents: 20787
diff changeset
   238
        ctx = new SimpleSSLContext().get();
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   239
        httpsServer.setHttpsConfigurator(new HttpsConfigurator (ctx));
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   240
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   241
        httpServer.start();
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   242
        httpsServer.start();
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   243
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   244
        httpPort = httpServer.getAddress().getPort();
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   245
        httpsPort = httpsServer.getAddress().getPort();
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   246
    }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   247
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   248
    static void shutdown() {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   249
        httpServer.stop(1);
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   250
        httpsServer.stop(1);
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   251
        e.shutdown();
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   252
        es.shutdown();
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   253
    }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   254
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   255
    static class OkHandler implements HttpHandler {
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   256
        public void handle(HttpExchange x) throws IOException {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   257
            x.sendResponseHeaders(200, -1);
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   258
            x.close();
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   259
        }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   260
    }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   261
39769
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   262
    static class CustomPolicy extends Policy {
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   263
        final PermissionCollection perms = new Permissions();
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   264
        CustomPolicy(Permission... permissions) {
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   265
            java.util.Arrays.stream(permissions).forEach(perms::add);
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   266
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   267
            // needed for the HTTP(S) server
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   268
            perms.add(new SocketPermission("localhost:1024-", "listen,resolve,accept"));
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   269
            // needed by the test to reset the policy, per testX method
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   270
            perms.add(new SecurityPermission("setPolicy"));
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   271
            // needed to shutdown the ThreadPoolExecutor ( used by the servers )
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   272
            perms.add(new RuntimePermission("modifyThread"));
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   273
            // needed by the client code forHttpsURLConnection.setSSLSocketFactory
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   274
            perms.add(new RuntimePermission("setFactory"));
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   275
        }
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   276
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   277
        public PermissionCollection getPermissions(ProtectionDomain domain) {
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   278
            return perms;
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   279
        }
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   280
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   281
        public PermissionCollection getPermissions(CodeSource codesource) {
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   282
            return perms;
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   283
        }
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   284
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   285
        public boolean implies(ProtectionDomain domain, Permission perm) {
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   286
            return perms.implies(perm);
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   287
        }
17e283ed590d 8078568: java/net/URLPermission/URLTest.java fails intermittently with BindException
chegar
parents: 38883
diff changeset
   288
    }
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents:
diff changeset
   289
}