test/jdk/java/net/httpclient/security/Security.java
author chegar
Fri, 06 Apr 2018 16:05:55 +0100
branchhttp-client-branch
changeset 56391 a44e40b6aa43
parent 56388 701dd37aa97a
child 56396 8e8423347c1e
permissions -rw-r--r--
http-client-branch: review comment - cleanup Security test
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     1
/*
56369
24a8fafec3ff http-client-branch: review comment - update copyright year ranges in tests
chegar
parents: 56265
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     4
 *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
     7
 * published by the Free Software Foundation.
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     8
 *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    13
 * accompanied this code).
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    14
 *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    18
 *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
40684
2e37c119dc2a 8164982: Fix legal notices in java/lang, java/net, java/util tests.
shurailine
parents: 38883
diff changeset
    21
 * questions.
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    22
 */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    23
41890
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    24
/*
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    25
 * @test
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    26
 * @bug 8087112
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55973
diff changeset
    27
 * @modules java.net.http
41890
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    28
 *          java.logging
38883
d5de564f8089 8151913: Fix module dependencies in java/net tests
mli
parents: 37897
diff changeset
    29
 *          jdk.httpserver
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    30
 * @library /lib/testlibrary/
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    31
 * @build jdk.testlibrary.SimpleSSLContext
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    32
 * @compile ../../../../com/sun/net/httpserver/LogFilter.java
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    33
 * @compile ../../../../com/sun/net/httpserver/FileServerHandler.java
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    34
 * @compile ../ProxyServer.java
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    35
 *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    36
 * @run main/othervm/secure=java.lang.SecurityManager/policy=0.policy Security 0
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    37
 * @run main/othervm/secure=java.lang.SecurityManager/policy=2.policy Security 2
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    38
 * @run main/othervm/secure=java.lang.SecurityManager/policy=3.policy Security 3
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    39
 * @run main/othervm/secure=java.lang.SecurityManager/policy=4.policy Security 4
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    40
 * @run main/othervm/secure=java.lang.SecurityManager/policy=5.policy Security 5
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    41
 * @run main/othervm/secure=java.lang.SecurityManager/policy=6.policy Security 6
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    42
 * @run main/othervm/secure=java.lang.SecurityManager/policy=7.policy Security 7
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    43
 * @run main/othervm/secure=java.lang.SecurityManager/policy=8.policy Security 8
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    44
 * @run main/othervm/secure=java.lang.SecurityManager/policy=9.policy Security 9
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    45
 * @run main/othervm/secure=java.lang.SecurityManager/policy=0.policy Security 13
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
    46
 * @run main/othervm/secure=java.lang.SecurityManager/policy=14.policy Security 14
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
    47
 * @run main/othervm/secure=java.lang.SecurityManager/policy=15.policy -Djava.security.debug=access:domain,failure Security 15
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    48
 */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    49
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
    50
// Tests 1, 10, 11 and 12 executed from Driver
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
    51
41890
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    52
import com.sun.net.httpserver.Headers;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    53
import com.sun.net.httpserver.HttpExchange;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    54
import com.sun.net.httpserver.HttpHandler;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    55
import com.sun.net.httpserver.HttpServer;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    56
import com.sun.net.httpserver.HttpsServer;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    57
import java.io.IOException;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    58
import java.io.InputStream;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    59
import java.io.File;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    60
import java.io.OutputStream;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    61
import java.lang.reflect.Constructor;
41890
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    62
import java.net.BindException;
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
    63
import java.net.InetAddress;
41890
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    64
import java.net.InetSocketAddress;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    65
import java.net.ProxySelector;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    66
import java.net.URI;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    67
import java.net.URLClassLoader;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    68
import java.net.URL;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55973
diff changeset
    69
import java.net.http.HttpHeaders;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55973
diff changeset
    70
import java.net.http.HttpClient;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55973
diff changeset
    71
import java.net.http.HttpRequest;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55973
diff changeset
    72
import java.net.http.HttpResponse;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    73
import java.nio.charset.StandardCharsets;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    74
import java.nio.file.Files;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    75
import java.nio.file.Path;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    76
import java.nio.ByteBuffer;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    77
import java.nio.file.Paths;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    78
import java.nio.file.StandardCopyOption;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    79
import java.util.LinkedList;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    80
import java.util.List;
41890
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    81
import java.util.concurrent.CompletableFuture;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
    82
import java.util.concurrent.CompletionException;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
    83
import java.util.concurrent.CompletionStage;
41890
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    84
import java.util.concurrent.ExecutionException;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    85
import java.util.concurrent.Executors;
ab67b8f4f2fe 8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents: 40684
diff changeset
    86
import java.util.concurrent.ExecutorService;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
    87
import java.util.concurrent.Flow;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    88
import java.util.logging.ConsoleHandler;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    89
import java.util.logging.Level;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    90
import java.util.logging.Logger;
37897
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
    91
import java.lang.reflect.InvocationTargetException;
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
    92
import static java.net.http.HttpResponse.BodyHandlers.ofString;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    93
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    94
/**
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    95
 * Security checks test
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    96
 */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    97
public class Security {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    98
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
    99
    static HttpServer s1;
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   100
    static ExecutorService executor;
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   101
    static int port, proxyPort;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   102
    static HttpClient client;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   103
    static String httproot, fileuri, fileroot, redirectroot;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   104
    static List<HttpClient> clients = new LinkedList<>();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   105
    static URI uri;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   106
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   107
    interface ThrowingRunnable { void run() throws Throwable; }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   108
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   109
    static class TestAndResult {
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   110
        private final ThrowingRunnable runnable;
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   111
        private final boolean expectSecurityException;
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   112
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   113
        TestAndResult(boolean expectSecurityException, ThrowingRunnable runnable) {
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   114
            this.expectSecurityException = expectSecurityException;
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   115
            this.runnable = runnable;
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   116
        }
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   117
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   118
        static TestAndResult of(boolean expectSecurityException,
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   119
                                ThrowingRunnable runnable) {
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   120
            return new TestAndResult(expectSecurityException, runnable);
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   121
        }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   122
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   123
        void runWithPolicy(String policy) {
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   124
            System.out.println("Using policy file: " + policy);
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   125
            try {
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   126
                runnable.run();
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   127
                if (expectSecurityException) {
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   128
                    System.out.println("FAILED: expected security exception");
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   129
                    throw new RuntimeException("FAILED: expected security exception\"");
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   130
                }
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   131
                System.out.println (policy + " succeeded as expected");
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   132
            } catch (BindException e) {
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   133
                System.exit(10);
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   134
            } catch (SecurityException e) {
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   135
                if (!expectSecurityException) {
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   136
                    System.out.println("Unexpected security Exception: " + e);
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   137
                    throw new RuntimeException(e);
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   138
                }
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   139
                System.out.println(policy + " threw exception as expected");
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   140
            } catch (Throwable t) {
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   141
                throw new AssertionError(t);
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   142
            }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   143
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   144
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   145
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   146
    static TestAndResult[] tests = createTests();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   147
    static String testclasses;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   148
    static File subdir;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   149
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   150
    /**
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   151
     * The ProxyServer class is compiled by jtreg, but we want to
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   152
     * move it so it is not on the application claspath. We want to
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   153
     * load it through a separate classloader so that it has a separate
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   154
     * protection domain and security permissions.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   155
     *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   156
     * Its permissions are in the second grant block in each policy file
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   157
     */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   158
    static void setupProxy() throws IOException, ClassNotFoundException, NoSuchMethodException {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   159
        testclasses = System.getProperty("test.classes");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   160
        subdir = new File (testclasses, "proxydir");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   161
        subdir.mkdir();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   162
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   163
        movefile("ProxyServer.class");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   164
        movefile("ProxyServer$Connection.class");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   165
        movefile("ProxyServer$1.class");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   166
56388
701dd37aa97a http-client-branch: review comment: minor cleanup in security test
chegar
parents: 56369
diff changeset
   167
        URL url = subdir.toURI().toURL();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   168
        System.out.println("URL for class loader = " + url);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   169
        URLClassLoader urlc = new URLClassLoader(new URL[] {url});
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   170
        proxyClass = Class.forName("ProxyServer", true, urlc);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   171
        proxyConstructor = proxyClass.getConstructor(Integer.class, Boolean.class);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   172
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   173
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   174
    static void movefile(String f) throws IOException {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   175
        Path src = Paths.get(testclasses, f);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   176
        Path dest = subdir.toPath().resolve(f);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   177
        if (!dest.toFile().exists()) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   178
            System.out.printf("moving %s to %s\n", src.toString(), dest.toString());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   179
            Files.move(src, dest,  StandardCopyOption.REPLACE_EXISTING);
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   180
        } else if (src.toFile().exists()) {
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   181
            System.out.printf("%s exists, deleting %s\n", dest.toString(), src.toString());
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   182
            Files.delete(src);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   183
        } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   184
            System.out.printf("NOT moving %s to %s\n", src.toString(), dest.toString());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   185
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   186
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   187
37897
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   188
    static Object getProxy(int port, boolean b) throws Throwable {
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   189
        try {
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   190
            return proxyConstructor.newInstance(port, b);
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   191
        } catch (InvocationTargetException e) {
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   192
            throw e.getTargetException();
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   193
        }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   194
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   195
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   196
    static Class<?> proxyClass;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   197
    static Constructor<?> proxyConstructor;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   198
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   199
    static TestAndResult[] createTests() {
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   200
        return new TestAndResult[] {
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   201
            // (0) policy does not have permission for file. Should fail
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   202
            TestAndResult.of(true, () -> { // Policy 0
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   203
                URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   204
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   205
                HttpResponse<?> response = client.send(request, ofString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   206
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   207
            // (1) policy has permission for file URL
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   208
            TestAndResult.of(false, () -> { //Policy 1
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   209
                URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   210
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   211
                HttpResponse<?> response = client.send(request, ofString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   212
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   213
            // (2) policy has permission for all file URLs under /files
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   214
            TestAndResult.of(false, () -> { // Policy 2
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   215
                URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   216
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   217
                HttpResponse<?> response = client.send(request, ofString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   218
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   219
            // (3) policy has permission for first URL but not redirected URL
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   220
            TestAndResult.of(true, () -> { // Policy 3
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   221
                URI u = URI.create("http://localhost:" + port + "/redirect/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   222
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   223
                HttpResponse<?> response = client.send(request, ofString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   224
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   225
            // (4) policy has permission for both first URL and redirected URL
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   226
            TestAndResult.of(false, () -> { // Policy 4
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   227
                URI u = URI.create("http://localhost:" + port + "/redirect/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   228
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   229
                HttpResponse<?> response = client.send(request, ofString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   230
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   231
            // (5) policy has permission for redirected but not first URL
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   232
            TestAndResult.of(true, () -> { // Policy 5
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   233
                URI u = URI.create("http://localhost:" + port + "/redirect/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   234
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   235
                HttpResponse<?> response = client.send(request, ofString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   236
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   237
            // (6) policy has permission for file URL, but not method
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   238
            TestAndResult.of(true, () -> { //Policy 6
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   239
                URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   240
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   241
                HttpResponse<?> response = client.send(request, ofString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   242
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   243
            // (7) policy has permission for file URL, method, but not header
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   244
            TestAndResult.of(true, () -> { //Policy 7
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   245
                URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   246
                HttpRequest request = HttpRequest.newBuilder(u)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   247
                                                 .header("X-Foo", "bar")
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   248
                                                 .GET()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   249
                                                 .build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   250
                HttpResponse<?> response = client.send(request, ofString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   251
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   252
            // (8) policy has permission for file URL, method and header
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   253
            TestAndResult.of(false, () -> { //Policy 8
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   254
                URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   255
                HttpRequest request = HttpRequest.newBuilder(u)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   256
                                                 .header("X-Foo", "bar")
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   257
                                                 .GET()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   258
                                                 .build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   259
                HttpResponse<?> response = client.send(request, ofString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   260
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   261
            // (9) policy has permission for file URL, method and header
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   262
            TestAndResult.of(false, () -> { //Policy 9
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   263
                URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   264
                HttpRequest request = HttpRequest.newBuilder(u)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   265
                                                 .headers("X-Foo", "bar", "X-Bar", "foo")
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   266
                                                 .GET()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   267
                                                 .build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   268
                HttpResponse<?> response = client.send(request, ofString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   269
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   270
            // (10) policy has permission for destination URL but not for proxy
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   271
            TestAndResult.of(true, () -> { //Policy 10
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   272
                directProxyTest(proxyPort, true);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   273
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   274
            // (11) policy has permission for both destination URL and proxy
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   275
            TestAndResult.of(false, () -> { //Policy 11
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   276
                directProxyTest(proxyPort, true);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   277
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   278
            // (12) policy has permission for both destination URL and proxy
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   279
            TestAndResult.of(true, () -> { //Policy 12 ( 11 & 12 are the same )
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   280
                directProxyTest(proxyPort, false);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   281
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   282
            // (13) async version of test 0
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   283
            TestAndResult.of(true, () -> { // Policy 0
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   284
                URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   285
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   286
                try {
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   287
                    HttpResponse<?> response = client.sendAsync(request, ofString()).get();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   288
                } catch (ExecutionException e) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   289
                    if (e.getCause() instanceof SecurityException) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   290
                        throw (SecurityException)e.getCause();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   291
                    } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   292
                        throw new RuntimeException(e);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   293
                    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   294
                }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   295
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   296
            // (14) async version of test 1
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   297
            TestAndResult.of(false, () -> { //Policy 1
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   298
                URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   299
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   300
                try {
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   301
                    HttpResponse<?> response = client.sendAsync(request, ofString()).get();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   302
                } catch (ExecutionException e) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   303
                    if (e.getCause() instanceof SecurityException) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   304
                        throw (SecurityException)e.getCause();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   305
                    } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   306
                        throw new RuntimeException(e);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   307
                    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   308
                }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   309
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   310
            // (15) check that user provided unprivileged code running on a worker
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   311
            //      thread does not gain ungranted privileges.
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   312
            TestAndResult.of(true, () -> { //Policy 12
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   313
                URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   314
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   315
                HttpResponse.BodyHandler<String> sth = ofString();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   316
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   317
                CompletableFuture<HttpResponse<String>> cf =
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   318
                    client.sendAsync(request, new HttpResponse.BodyHandler<String>() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   319
                        @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   320
                        public HttpResponse.BodySubscriber<String> apply(int status, HttpHeaders responseHeaders)  {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   321
                            final HttpResponse.BodySubscriber<String> stproc = sth.apply(status, responseHeaders);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   322
                            return new HttpResponse.BodySubscriber<String>() {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   323
                                @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   324
                                public CompletionStage<String> getBody() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   325
                                    return stproc.getBody();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   326
                                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   327
                                @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   328
                                public void onNext(List<ByteBuffer> item) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   329
                                    SecurityManager sm = System.getSecurityManager();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   330
                                    // should succeed.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   331
                                    sm.checkPermission(new RuntimePermission("foobar"));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   332
                                    // do some mischief here
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   333
                                    System.setSecurityManager(null);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   334
                                    System.setSecurityManager(sm);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   335
                                    // problem if we get this far
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   336
                                    stproc.onNext(item);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   337
                                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   338
                                @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   339
                                public void onSubscribe(Flow.Subscription subscription) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   340
                                    stproc.onSubscribe(subscription);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   341
                                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   342
                                @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   343
                                public void onError(Throwable throwable) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   344
                                    stproc.onError(throwable);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   345
                                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   346
                                @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   347
                                public void onComplete() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   348
                                    stproc.onComplete();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   349
                                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   350
                            };
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   351
                        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   352
                    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   353
                );
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   354
                try {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   355
                    cf.join();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   356
                } catch (CompletionException e) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   357
                    Throwable t = e.getCause();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   358
                    if (t instanceof SecurityException)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   359
                        throw (SecurityException)t;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   360
                    else if ((t instanceof IOException)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   361
                              && (t.getCause() instanceof SecurityException))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   362
                        throw ((SecurityException)t.getCause());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   363
                    else
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   364
                        throw new RuntimeException(t);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   365
                }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   366
            })
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   367
        };
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   368
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   369
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   370
    private static void directProxyTest(int proxyPort, boolean samePort)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   371
        throws IOException, InterruptedException
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   372
    {
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   373
        Object proxy = null;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   374
        try {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   375
            proxy = getProxy(proxyPort, true);
37897
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   376
        } catch (BindException e) {
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   377
            System.out.println("Bind failed");
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   378
            throw e;
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   379
        } catch (Throwable ee) {
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   380
            throw new RuntimeException(ee);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   381
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   382
        System.out.println("Proxy port = " + proxyPort);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   383
        if (!samePort)
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   384
            proxyPort++;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   385
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   386
        InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(),
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   387
                                                       proxyPort);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   388
        HttpClient cl = HttpClient.newBuilder()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   389
                                    .proxy(ProxySelector.of(addr))
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   390
                                    .build();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   391
        clients.add(cl);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   392
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   393
        URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   394
        HttpRequest request = HttpRequest.newBuilder(u)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   395
                                         .headers("X-Foo", "bar", "X-Bar", "foo")
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   396
                                         .build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
   397
        HttpResponse<?> response = cl.send(request, ofString());
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   398
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   399
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   400
    public static void main(String[] args) throws Exception {
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   401
        try {
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   402
            initServer();
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   403
            setupProxy();
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   404
        } catch (BindException e) {
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   405
            System.exit(10);
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   406
        }
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   407
        fileroot = System.getProperty("test.src")+ "/docs";
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   408
        int testnum = Integer.parseInt(args[0]);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   409
        String policy = args[0];
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   410
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   411
        client = HttpClient.newBuilder()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   412
                           .followRedirects(HttpClient.Redirect.ALWAYS)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   413
                           .build();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   414
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   415
        clients.add(client);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   416
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   417
        try {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   418
            TestAndResult tr = tests[testnum];
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   419
            tr.runWithPolicy(policy);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   420
        } finally {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   421
            s1.stop(0);
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   422
            executor.shutdownNow();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   423
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   424
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   425
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   426
    public static void initServer() throws Exception {
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   427
        String portstring = System.getProperty("port.number");
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   428
        port = portstring != null ? Integer.parseInt(portstring) : 0;
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   429
        portstring = System.getProperty("port.number1");
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   430
        proxyPort = portstring != null ? Integer.parseInt(portstring) : 0;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   431
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   432
        Logger logger = Logger.getLogger("com.sun.net.httpserver");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   433
        ConsoleHandler ch = new ConsoleHandler();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   434
        logger.setLevel(Level.ALL);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   435
        ch.setLevel(Level.ALL);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   436
        logger.addHandler(ch);
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   437
        String root = System.getProperty("test.src")+ "/docs";
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   438
        InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), port);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   439
        s1 = HttpServer.create (addr, 0);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   440
        if (s1 instanceof HttpsServer) {
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   441
            throw new RuntimeException("should not be httpsserver");
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   442
        }
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   443
        s1.createContext("/files", new FileServerHandler(root));
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   444
        s1.createContext("/redirect", new RedirectHandler("/redirect"));
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   445
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   446
        executor = Executors.newCachedThreadPool();
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   447
        s1.setExecutor(executor);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   448
        s1.start();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   449
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   450
        if (port == 0)
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   451
            port = s1.getAddress().getPort();
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   452
        else {
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   453
            if (s1.getAddress().getPort() != port)
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   454
                throw new RuntimeException("Error wrong port");
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   455
            System.out.println("Port was assigned by Driver");
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   456
        }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   457
        System.out.println("HTTP server port = " + port);
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   458
        httproot = "http://localhost:" + port + "/files/";
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   459
        redirectroot = "http://localhost:" + port + "/redirect/";
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   460
        uri = new URI(httproot);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   461
        fileuri = httproot + "foo.txt";
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   462
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   463
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   464
    static class RedirectHandler implements HttpHandler {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   465
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   466
        String root;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   467
        int count = 0;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   468
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   469
        RedirectHandler(String root) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   470
            this.root = root;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   471
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   472
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   473
        synchronized int count() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   474
            return count;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   475
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   476
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   477
        synchronized void increment() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   478
            count++;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   479
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   480
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   481
        @Override
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   482
        public synchronized void handle(HttpExchange t) throws IOException {
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   483
            System.out.println("Server: " + t.getRequestURI());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   484
            try (InputStream is = t.getRequestBody()) {
56391
a44e40b6aa43 http-client-branch: review comment - cleanup Security test
chegar
parents: 56388
diff changeset
   485
               is.readAllBytes();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   486
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   487
            increment();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   488
            if (count() == 1) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   489
                Headers map = t.getResponseHeaders();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   490
                String redirect = "/redirect/bar.txt";
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   491
                map.add("Location", redirect);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   492
                t.sendResponseHeaders(301, -1);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   493
                t.close();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   494
            } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   495
                String response = "Hello world";
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   496
                t.sendResponseHeaders(200, response.length());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   497
                OutputStream os = t.getResponseBody();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   498
                os.write(response.getBytes(StandardCharsets.ISO_8859_1));
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   499
                t.close();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   500
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   501
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   502
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   503
}