jdk/test/java/net/httpclient/http2/TLSConnection.java
author michaelm
Fri, 09 Dec 2016 11:35:02 +0000
changeset 42460 7133f144981a
parent 38745 be430178b177
child 45713 ee3f2cbfe23a
permissions -rw-r--r--
8170648: Move java.net.http package out of Java SE to incubator namespace Reviewed-by: chegar, dfuchs, michaelm, prappo, skuksenko Contributed-by: Chris Hegarty <chris.hegarty@oracle.com>, Daniel Fuchs <daniel.fuchs@oracle.com>, Michael McMahon <michael.x.mcmahon@oracle.com>, Pavel Rappo <pavel.rappo@oracle.com>, Sergey Kuksenko <sergey.kuksenko@oracle.com>, anthony.vanelverdinghe@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38381
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
     1
/*
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
     4
 *
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
     8
 *
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    13
 * accompanied this code).
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    14
 *
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    18
 *
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    21
 * questions.
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    22
 */
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    23
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    24
import java.io.File;
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    25
import java.io.IOException;
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    26
import java.io.InputStream;
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    27
import java.io.OutputStream;
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    28
import java.net.URI;
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    29
import java.net.URISyntaxException;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    30
import jdk.incubator.http.HttpClient;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    31
import jdk.incubator.http.HttpRequest;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    32
import jdk.incubator.http.HttpResponse;
38381
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    33
import javax.net.ssl.SSLParameters;
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    34
import javax.net.ssl.SSLSession;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    35
import static jdk.incubator.http.HttpRequest.BodyProcessor.fromString;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    36
import static jdk.incubator.http.HttpResponse.BodyHandler.asString;
38381
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    37
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    38
/*
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    39
 * @test
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    40
 * @bug 8150769 8157107
38745
be430178b177 8157816: Mark 4 httpclient tests as intermittently failing
xiaofeya
parents: 38381
diff changeset
    41
 * @key intermittent
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    42
 * @library server
38381
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    43
 * @summary Checks that SSL parameters can be set for HTTP/2 connection
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    44
 * @modules jdk.incubator.httpclient/jdk.incubator.http.internal.common
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    45
 *          jdk.incubator.httpclient/jdk.incubator.http.internal.frame
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    46
 *          jdk.incubator.httpclient/jdk.incubator.http.internal.hpack
38381
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    47
 * @run main/othervm TLSConnection
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    48
 */
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    49
public class TLSConnection {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    50
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    51
    private static final String KEYSTORE = System.getProperty("test.src")
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    52
            + File.separator + "keystore.p12";
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    53
   private static final String PASSWORD = "password";
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    54
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    55
    public static void main(String[] args) throws Exception {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    56
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    57
        // enable all logging
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    58
        System.setProperty("jdk.httpclient.HttpClient.log", "all,frames:all");
38381
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    59
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    60
        // initialize JSSE
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    61
        System.setProperty("javax.net.ssl.keyStore", KEYSTORE);
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    62
        System.setProperty("javax.net.ssl.keyStorePassword", PASSWORD);
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    63
        System.setProperty("javax.net.ssl.trustStore", KEYSTORE);
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    64
        System.setProperty("javax.net.ssl.trustStorePassword", PASSWORD);
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    65
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    66
        Handler handler = new Handler();
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    67
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    68
        try (Http2TestServer server = new Http2TestServer(true, 0)) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    69
            server.addHandler(handler, "/");
38381
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    70
            server.start();
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    71
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    72
            int port = server.getAddress().getPort();
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    73
            String uriString = "https://127.0.0.1:" + Integer.toString(port);
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    74
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    75
            // run test cases
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    76
            boolean success = true;
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    77
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    78
            SSLParameters parameters = null;
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    79
            success &= expectFailure(
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    80
                    "Test #1: SSL parameters is null, expect NPE",
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    81
                    () -> connect(uriString, parameters),
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    82
                    NullPointerException.class);
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    83
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    84
            success &= expectSuccess(
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    85
                    "Test #2: default SSL parameters, "
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    86
                            + "expect successful connection",
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    87
                    () -> connect(uriString, new SSLParameters()));
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    88
            success &= checkProtocol(handler.getSSLSession(), "TLSv1.2");
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    89
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    90
            // set SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA cipher suite
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    91
            // which has less priority in default cipher suite list
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    92
            success &= expectSuccess(
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    93
                    "Test #3: SSL parameters with "
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    94
                            + "SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA cipher suite, "
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    95
                            + "expect successful connection",
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    96
                    () -> connect(uriString, new SSLParameters(
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    97
                            new String[] { "SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA" })));
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    98
            success &= checkProtocol(handler.getSSLSession(), "TLSv1.2");
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
    99
            success &= checkCipherSuite(handler.getSSLSession(),
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   100
                    "SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA");
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   101
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   102
            // set TLS_RSA_WITH_AES_128_CBC_SHA cipher suite
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   103
            // which has less priority in default cipher suite list
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   104
            // also set TLSv11 protocol
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   105
            success &= expectSuccess(
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   106
                    "Test #4: SSL parameters with "
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   107
                            + "TLS_RSA_WITH_AES_128_CBC_SHA cipher suite,"
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   108
                            + " expect successful connection",
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   109
                    () -> connect(uriString, new SSLParameters(
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   110
                            new String[] { "TLS_RSA_WITH_AES_128_CBC_SHA" },
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   111
                            new String[] { "TLSv1.1" })));
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   112
            success &= checkProtocol(handler.getSSLSession(), "TLSv1.1");
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   113
            success &= checkCipherSuite(handler.getSSLSession(),
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   114
                    "TLS_RSA_WITH_AES_128_CBC_SHA");
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   115
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   116
            if (success) {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   117
                System.out.println("Test passed");
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   118
            } else {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   119
                throw new RuntimeException("At least one test case failed");
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   120
            }
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   121
        }
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   122
    }
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   123
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   124
    private static interface Test {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   125
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   126
        public void run() throws Exception;
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   127
    }
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   128
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   129
    private static class Handler implements Http2Handler {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   130
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   131
        private static final byte[] BODY = "Test response".getBytes();
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   132
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   133
        private volatile SSLSession sslSession;
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   134
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   135
        @Override
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   136
        public void handle(Http2TestExchange t) throws IOException {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   137
            System.out.println("Handler: received request to "
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   138
                    + t.getRequestURI());
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   139
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   140
            try (InputStream is = t.getRequestBody()) {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   141
                byte[] body = is.readAllBytes();
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   142
                System.out.println("Handler: read " + body.length
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   143
                        + " bytes of body: ");
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   144
                System.out.println(new String(body));
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   145
            }
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   146
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   147
            try (OutputStream os = t.getResponseBody()) {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   148
                t.sendResponseHeaders(200, BODY.length);
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   149
                os.write(BODY);
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   150
            }
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   151
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   152
            sslSession = t.getSSLSession();
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   153
        }
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   154
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   155
        SSLSession getSSLSession() {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   156
            return sslSession;
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   157
        }
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   158
    }
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   159
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   160
    private static void connect(String uriString, SSLParameters sslParameters)
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   161
        throws URISyntaxException, IOException, InterruptedException
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   162
    {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   163
        HttpClient client = HttpClient.newBuilder()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   164
                                      .sslParameters(sslParameters)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   165
                                      .version(HttpClient.Version.HTTP_2)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   166
                                      .build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   167
        HttpRequest request = HttpRequest.newBuilder(new URI(uriString))
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   168
                                         .POST(fromString("body"))
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   169
                                         .build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   170
        String body = client.send(request, asString()).body();
38381
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   171
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   172
        System.out.println("Response: " + body);
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   173
    }
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   174
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   175
    private static boolean checkProtocol(SSLSession session, String protocol) {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   176
        if (session == null) {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   177
            System.out.println("Check protocol: no session provided");
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   178
            return false;
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   179
        }
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   180
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   181
        System.out.println("Check protocol: negotiated protocol: "
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   182
                + session.getProtocol());
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   183
        System.out.println("Check protocol: expected protocol: "
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   184
                + protocol);
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   185
        if (!protocol.equals(session.getProtocol())) {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   186
            System.out.println("Check protocol: unexpected negotiated protocol");
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   187
            return false;
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   188
        }
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   189
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   190
        return true;
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   191
    }
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   192
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   193
    private static boolean checkCipherSuite(SSLSession session, String ciphersuite) {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   194
        if (session == null) {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   195
            System.out.println("Check protocol: no session provided");
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   196
            return false;
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   197
        }
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   198
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   199
        System.out.println("Check protocol: negotiated ciphersuite: "
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   200
                + session.getCipherSuite());
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   201
        System.out.println("Check protocol: expected ciphersuite: "
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   202
                + ciphersuite);
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   203
        if (!ciphersuite.equals(session.getCipherSuite())) {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   204
            System.out.println("Check protocol: unexpected negotiated ciphersuite");
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   205
            return false;
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   206
        }
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   207
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   208
        return true;
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   209
    }
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   210
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   211
    private static boolean expectSuccess(String message, Test test) {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   212
        System.out.println(message);
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   213
        try {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   214
            test.run();
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   215
            System.out.println("Passed");
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   216
            return true;
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   217
        } catch (Exception e) {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   218
            System.out.println("Failed: unexpected exception:");
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   219
            e.printStackTrace(System.out);
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   220
            return false;
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   221
        }
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   222
    }
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   223
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   224
    private static boolean expectFailure(String message, Test test,
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   225
            Class<? extends Throwable> expectedException) {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   226
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   227
        System.out.println(message);
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   228
        try {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   229
            test.run();
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   230
            System.out.println("Failed: unexpected successful connection");
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   231
            return false;
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   232
        } catch (Exception e) {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   233
            System.out.println("Got an exception:");
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   234
            e.printStackTrace(System.out);
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   235
            if (expectedException != null
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   236
                    && !expectedException.isAssignableFrom(e.getClass())) {
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   237
                System.out.printf("Failed: expected %s, but got %s%n",
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   238
                        expectedException.getName(),
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   239
                        e.getClass().getName());
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   240
                return false;
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   241
            }
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   242
            System.out.println("Passed: expected exception");
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   243
            return true;
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   244
        }
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   245
    }
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   246
a2105ea409ec 8157107: HTTP/2 client may fail with NPE if additional logging enabled
michaelm
parents:
diff changeset
   247
}