test/jdk/java/net/httpclient/security/Security.java
author dfuchs
Tue, 01 Oct 2019 12:10:33 +0100
changeset 58423 54de0c861d32
parent 52554 5f1ca46703f9
permissions -rw-r--r--
8231506: Fix some instabilities in a few networking tests Reviewed-by: alanb, chegar, msheppar
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
/*
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
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
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
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
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 49765
diff changeset
    30
 * @library /test/lib
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 49765
diff changeset
    31
 * @build jdk.test.lib.net.SimpleSSLContext
36131
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;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
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;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    69
import java.net.http.HttpHeaders;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    70
import java.net.http.HttpClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    71
import java.net.http.HttpRequest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
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;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
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
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    99
    static HttpServer s1;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
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
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
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 {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   110
        private final ThrowingRunnable runnable;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   111
        private final boolean expectSecurityException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   112
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   113
        TestAndResult(boolean expectSecurityException, ThrowingRunnable runnable) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   114
            this.expectSecurityException = expectSecurityException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   115
            this.runnable = runnable;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   116
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   117
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   118
        static TestAndResult of(boolean expectSecurityException,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   119
                                ThrowingRunnable runnable) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   120
            return new TestAndResult(expectSecurityException, runnable);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   121
        }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   122
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   123
        void runWithPolicy(String policy) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   124
            System.out.println("Using policy file: " + policy);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   125
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   126
                runnable.run();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   127
                if (expectSecurityException) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   128
                    String msg = "FAILED: expected security exception not thrown";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   129
                    System.out.println(msg);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   130
                    throw new RuntimeException(msg);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   131
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   132
                System.out.println (policy + " succeeded as expected");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   133
            } catch (BindException e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   134
                System.exit(10);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   135
            } catch (SecurityException e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   136
                if (!expectSecurityException) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   137
                    System.out.println("UNEXPECTED security Exception: " + e);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   138
                    throw new RuntimeException("UNEXPECTED security Exception", e);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   139
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   140
                System.out.println(policy + " threw SecurityException as expected: " + e);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   141
            } catch (Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   142
                throw new AssertionError(t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   143
            }
36131
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
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   146
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   147
    static TestAndResult[] tests = createTests();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   148
    static String testclasses;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   149
    static File subdir;
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
    /**
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   152
     * The ProxyServer class is compiled by jtreg, but we want to
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   153
     * 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
   154
     * 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
   155
     * protection domain and security permissions.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   156
     *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   157
     * 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
   158
     */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   159
    static void setupProxy() throws IOException, ClassNotFoundException, NoSuchMethodException {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   160
        testclasses = System.getProperty("test.classes");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   161
        subdir = new File (testclasses, "proxydir");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   162
        subdir.mkdir();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   163
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   164
        movefile("ProxyServer.class");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   165
        movefile("ProxyServer$Connection.class");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   166
        movefile("ProxyServer$1.class");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   167
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   168
        URL url = subdir.toURI().toURL();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   169
        System.out.println("URL for class loader = " + url);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   170
        URLClassLoader urlc = new URLClassLoader(new URL[] {url});
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   171
        proxyClass = Class.forName("ProxyServer", true, urlc);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   172
        proxyConstructor = proxyClass.getConstructor(Integer.class, Boolean.class);
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
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   175
    static void movefile(String f) throws IOException {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   176
        Path src = Paths.get(testclasses, f);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   177
        Path dest = subdir.toPath().resolve(f);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   178
        if (!dest.toFile().exists()) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   179
            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
   180
            Files.move(src, dest,  StandardCopyOption.REPLACE_EXISTING);
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   181
        } else if (src.toFile().exists()) {
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   182
            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
   183
            Files.delete(src);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   184
        } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   185
            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
   186
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   187
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   188
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   189
    static Object createProxy(int port, boolean b) throws Throwable {
37897
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   190
        try {
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   191
            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
   192
        } catch (InvocationTargetException e) {
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   193
            throw e.getTargetException();
bc8dc7bc4a03 8156801: java/net/httpclient/security/Driver.java failed with RuntimeException: Non zero return value
michaelm
parents: 37791
diff changeset
   194
        }
36131
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
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   197
    static Class<?> proxyClass;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   198
    static Constructor<?> proxyConstructor;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   199
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   200
    static TestAndResult[] createTests() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   201
        return new TestAndResult[] {
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   202
            // (0) policy does not have permission for file. Should fail
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   203
            TestAndResult.of(true, () -> { // Policy 0
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   204
                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
   205
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   206
                HttpResponse<?> response = client.send(request, ofString());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   207
                System.out.println("Received response:" + response);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   208
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   209
            // (1) policy has permission for file URL
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   210
            TestAndResult.of(false, () -> { //Policy 1
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   211
                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
   212
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   213
                HttpResponse<?> response = client.send(request, ofString());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   214
                System.out.println("Received response:" + response);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   215
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   216
            // (2) policy has permission for all file URLs under /files
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   217
            TestAndResult.of(false, () -> { // Policy 2
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   218
                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
   219
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   220
                HttpResponse<?> response = client.send(request, ofString());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   221
                System.out.println("Received response:" + response);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   222
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   223
            // (3) policy has permission for first URL but not redirected URL
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   224
            TestAndResult.of(true, () -> { // Policy 3
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   225
                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
   226
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   227
                HttpResponse<?> response = client.send(request, ofString());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   228
                System.out.println("Received response:" + response);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   229
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   230
            // (4) policy has permission for both first URL and redirected URL
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   231
            TestAndResult.of(false, () -> { // Policy 4
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   232
                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
   233
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   234
                HttpResponse<?> response = client.send(request, ofString());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   235
                System.out.println("Received response:" + response);
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
            // (5) policy has permission for redirected but not first URL
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   238
            TestAndResult.of(true, () -> { // Policy 5
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   239
                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
   240
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   241
                HttpResponse<?> response = client.send(request, ofString());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   242
                System.out.println("Received response:" + response);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   243
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   244
            // (6) policy has permission for file URL, but not method
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   245
            TestAndResult.of(true, () -> { //Policy 6
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   246
                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
   247
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   248
                HttpResponse<?> response = client.send(request, ofString());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   249
                System.out.println("Received response:" + response);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   250
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   251
            // (7) policy has permission for file URL, method, but not header
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   252
            TestAndResult.of(true, () -> { //Policy 7
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   253
                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
   254
                HttpRequest request = HttpRequest.newBuilder(u)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   255
                                                 .header("X-Foo", "bar")
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   256
                                                 .GET()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   257
                                                 .build();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   258
                HttpResponse<?> response = client.send(request, ofString());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   259
                System.out.println("Received response:" + response);
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
            // (8) policy has permission for file URL, method and header
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   262
            TestAndResult.of(false, () -> { //Policy 8
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
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
                                                 .header("X-Foo", "bar")
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();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   268
                HttpResponse<?> response = client.send(request, ofString());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   269
                System.out.println("Received response:" + response);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   270
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   271
            // (9) policy has permission for file URL, method and header
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   272
            TestAndResult.of(false, () -> { //Policy 9
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   273
                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
   274
                HttpRequest request = HttpRequest.newBuilder(u)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   275
                                                 .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
   276
                                                 .GET()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   277
                                                 .build();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   278
                HttpResponse<?> response = client.send(request, ofString());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   279
                System.out.println("Received response:" + response);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   280
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   281
            // (10) policy has permission for destination URL but not for proxy
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   282
            TestAndResult.of(true, () -> { //Policy 10
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   283
                directProxyTest(proxyPort, true);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   284
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   285
            // (11) policy has permission for both destination URL and proxy
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   286
            TestAndResult.of(false, () -> { //Policy 11
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   287
                directProxyTest(proxyPort, true);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   288
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   289
            // (12) policy has permission for both destination URL and proxy
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   290
            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
   291
                directProxyTest(proxyPort, false);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   292
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   293
            // (13) async version of test 0
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   294
            TestAndResult.of(true, () -> { // Policy 0
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   295
                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
   296
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   297
                try {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   298
                    HttpResponse<?> response = client.sendAsync(request, ofString()).get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   299
                    System.out.println("Received response:" + response);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   300
                } catch (ExecutionException e) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   301
                    if (e.getCause() instanceof SecurityException) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   302
                        throw (SecurityException)e.getCause();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   303
                    } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   304
                        throw new RuntimeException(e);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   305
                    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   306
                }
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
            // (14) async version of test 1
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   309
            TestAndResult.of(false, () -> { //Policy 1
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   310
                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
   311
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   312
                try {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   313
                    HttpResponse<?> response = client.sendAsync(request, ofString()).get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   314
                    System.out.println("Received response:" + response);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   315
                } catch (ExecutionException e) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   316
                    if (e.getCause() instanceof SecurityException) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   317
                        throw (SecurityException)e.getCause();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   318
                    } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   319
                        throw new RuntimeException(e);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   320
                    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   321
                }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   322
            }),
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   323
            // (15) check that user provided unprivileged code running on a worker
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   324
            //      thread does not gain ungranted privileges.
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   325
            TestAndResult.of(true, () -> { //Policy 12
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   326
                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
   327
                HttpRequest request = HttpRequest.newBuilder(u).GET().build();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   328
                HttpResponse.BodyHandler<String> sth = ofString();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   329
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   330
                CompletableFuture<HttpResponse<String>> cf =
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   331
                    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
   332
                        @Override
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   333
                        public HttpResponse.BodySubscriber<String> apply(HttpResponse.ResponseInfo rinfo) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   334
                            final HttpResponse.BodySubscriber<String> stproc = sth.apply(rinfo);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   335
                            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
   336
                                @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   337
                                public CompletionStage<String> getBody() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   338
                                    return stproc.getBody();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   339
                                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   340
                                @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   341
                                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
   342
                                    SecurityManager sm = System.getSecurityManager();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   343
                                    // should succeed.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   344
                                    sm.checkPermission(new RuntimePermission("foobar"));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   345
                                    // do some mischief here
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   346
                                    System.setSecurityManager(null);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   347
                                    System.setSecurityManager(sm);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   348
                                    // 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
   349
                                    stproc.onNext(item);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   350
                                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   351
                                @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   352
                                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
   353
                                    stproc.onSubscribe(subscription);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   354
                                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   355
                                @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   356
                                public void onError(Throwable throwable) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   357
                                    stproc.onError(throwable);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   358
                                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   359
                                @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   360
                                public void onComplete() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   361
                                    stproc.onComplete();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   362
                                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   363
                            };
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   364
                        }
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
                try {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   368
                    HttpResponse<String> response = cf.join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   369
                    System.out.println("Received response:" + response);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   370
                } catch (CompletionException e) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   371
                    Throwable t = e.getCause();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   372
                    if (t instanceof SecurityException)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   373
                        throw (SecurityException)t;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   374
                    else if ((t instanceof IOException)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   375
                              && (t.getCause() instanceof SecurityException))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   376
                        throw ((SecurityException)t.getCause());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   377
                    else
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   378
                        throw new RuntimeException(t);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   379
                }
52554
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   380
            }),
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   381
            // (16) allowed to set Host header but does not have permission
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   382
            TestAndResult.of(true, () -> { //Policy 16
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   383
                URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   384
                HttpRequest request = HttpRequest.newBuilder(u)
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   385
                        .header("Host", "foohost:123")
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   386
                        .GET().build();
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   387
                HttpResponse<?> response = client.send(request, ofString());
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   388
                System.out.println("Received response:" + response);
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   389
            }),
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   390
            // (17) allowed to set Host header and does have permission
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   391
            TestAndResult.of(false, () -> { //Policy 17
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   392
                URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   393
                HttpRequest request = HttpRequest.newBuilder(u)
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   394
                        .header("Host", "foohost:123")
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   395
                        .GET().build();
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   396
                HttpResponse<?> response = client.send(request, ofString());
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   397
                System.out.println("Received response:" + response);
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
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   401
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   402
    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
   403
        throws IOException, InterruptedException
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   404
    {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   405
        System.out.println("proxyPort:" + proxyPort + ", samePort:" + samePort);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   406
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   407
        int p = proxyPort;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   408
        if (samePort) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   409
            Object proxy;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   410
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   411
                proxy = createProxy(p, true);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   412
            } catch (BindException e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   413
                System.out.println("Bind failed");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   414
                throw e;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   415
            } catch (Throwable ee) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   416
                throw new RuntimeException(ee);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   417
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   418
        } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   419
            while (p == proxyPort || p == port) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   420
                // avoid ports that may be granted permission
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   421
                p++;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   422
                if (p > 65535) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   423
                    p = 32000; // overflow
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   424
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   425
            }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   426
        }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   427
        System.out.println("Proxy port, p:" + p);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   428
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   429
        InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   430
                                                       p);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   431
        HttpClient cl = HttpClient.newBuilder()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   432
                                    .proxy(ProxySelector.of(addr))
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   433
                                    .build();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   434
        clients.add(cl);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   435
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   436
        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
   437
        HttpRequest request = HttpRequest.newBuilder(u)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   438
                                         .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
   439
                                         .build();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   440
        HttpResponse<?> response = cl.send(request, ofString());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   441
        System.out.println("Received response:" + response);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   442
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   443
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   444
    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
   445
        try {
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   446
            initServer();
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   447
            setupProxy();
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   448
        } catch (BindException e) {
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   449
            System.exit(10);
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   450
        }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   451
        fileroot = System.getProperty("test.src")+ "/docs";
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   452
        int testnum = Integer.parseInt(args[0]);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   453
        String policy = args[0];
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   454
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   455
        client = HttpClient.newBuilder()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   456
                           .followRedirects(HttpClient.Redirect.ALWAYS)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41890
diff changeset
   457
                           .build();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   458
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   459
        clients.add(client);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   460
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   461
        try {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   462
            TestAndResult tr = tests[testnum];
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   463
            tr.runWithPolicy(policy);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   464
        } finally {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   465
            s1.stop(0);
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   466
            executor.shutdownNow();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   467
        }
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
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   470
    public static void initServer() throws Exception {
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   471
        String portstring = System.getProperty("port.number");
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   472
        port = portstring != null ? Integer.parseInt(portstring) : 0;
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   473
        portstring = System.getProperty("port.number1");
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   474
        proxyPort = portstring != null ? Integer.parseInt(portstring) : 0;
36131
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
        Logger logger = Logger.getLogger("com.sun.net.httpserver");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   477
        ConsoleHandler ch = new ConsoleHandler();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   478
        logger.setLevel(Level.ALL);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   479
        ch.setLevel(Level.ALL);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   480
        logger.addHandler(ch);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   481
        String root = System.getProperty("test.src")+ "/docs";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   482
        InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), port);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   483
        s1 = HttpServer.create (addr, 0);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   484
        if (s1 instanceof HttpsServer) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   485
            throw new RuntimeException("should not be httpsserver");
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   486
        }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   487
        s1.createContext("/files", new FileServerHandler(root));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   488
        s1.createContext("/redirect", new RedirectHandler("/redirect"));
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   489
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   490
        executor = Executors.newCachedThreadPool();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   491
        s1.setExecutor(executor);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   492
        s1.start();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   493
37791
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   494
        if (port == 0)
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   495
            port = s1.getAddress().getPort();
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   496
        else {
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   497
            if (s1.getAddress().getPort() != port)
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   498
                throw new RuntimeException("Error wrong port");
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   499
            System.out.println("Port was assigned by Driver");
ae33107fd8b3 8155928: Remove hardcoded port numbers from httpclient/Security.java test
michaelm
parents: 36131
diff changeset
   500
        }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   501
        System.out.println("HTTP server port = " + port);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   502
        httproot = "http://localhost:" + port + "/files/";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   503
        redirectroot = "http://localhost:" + port + "/redirect/";
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   504
        uri = new URI(httproot);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   505
        fileuri = httproot + "foo.txt";
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   506
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   507
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   508
    static class RedirectHandler implements HttpHandler {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   509
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   510
        String root;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   511
        int count = 0;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   512
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   513
        RedirectHandler(String root) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   514
            this.root = root;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   515
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   516
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   517
        synchronized int count() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   518
            return count;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   519
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   520
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   521
        synchronized void increment() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   522
            count++;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   523
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   524
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   525
        @Override
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   526
        public synchronized void handle(HttpExchange t) throws IOException {
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   527
            System.out.println("Server: " + t.getRequestURI());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   528
            try (InputStream is = t.getRequestBody()) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   529
               is.readAllBytes();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   530
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   531
            increment();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   532
            if (count() == 1) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   533
                Headers map = t.getResponseHeaders();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   534
                String redirect = "/redirect/bar.txt";
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   535
                map.add("Location", redirect);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   536
                t.sendResponseHeaders(301, -1);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   537
                t.close();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   538
            } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   539
                String response = "Hello world";
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   540
                t.sendResponseHeaders(200, response.length());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   541
                OutputStream os = t.getResponseBody();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   542
                os.write(response.getBytes(StandardCharsets.ISO_8859_1));
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   543
                t.close();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   544
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   545
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   546
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   547
}