test/jdk/java/net/httpclient/http2/ErrorTest.java
author chegar
Thu, 08 Mar 2018 17:42:16 +0000
branchhttp-client-branch
changeset 56265 ec34ae013fbe
parent 56167 96fa4f49a9ff
child 56369 24a8fafec3ff
permissions -rw-r--r--
http-client-branch: tests should bind to the loopback only
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38464
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
     1
/*
45535
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 44854
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
38464
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
     4
 *
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
     8
 *
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    13
 * accompanied this code).
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    14
 *
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    18
 *
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    21
 * questions.
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    22
 */
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    23
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    24
/*
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    25
 * @test
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    26
 * @bug 8157105
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    27
 * @library /lib/testlibrary server
38464
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    28
 * @build jdk.testlibrary.SimpleSSLContext
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    29
 * @modules java.base/sun.net.www.http
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    30
 *          java.net.http/jdk.internal.net.http.common
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    31
 *          java.net.http/jdk.internal.net.http.frame
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    32
 *          java.net.http/jdk.internal.net.http.hpack
41355
f80c213ae8ff 8166791: Fix module dependencies for networking component tests
skovalev
parents: 38745
diff changeset
    33
 *          java.security.jgss
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 42460
diff changeset
    34
 * @run testng/othervm/timeout=60 -Djavax.net.debug=ssl -Djdk.httpclient.HttpClient.log=all ErrorTest
38464
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    35
 * @summary check exception thrown when bad TLS parameters selected
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    36
 */
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    37
41355
f80c213ae8ff 8166791: Fix module dependencies for networking component tests
skovalev
parents: 38745
diff changeset
    38
import java.io.IOException;
f80c213ae8ff 8166791: Fix module dependencies for networking component tests
skovalev
parents: 38745
diff changeset
    39
import java.net.URI;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    40
import java.net.http.HttpClient;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    41
import java.net.http.HttpRequest;
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56092
diff changeset
    42
import java.net.http.HttpRequest.BodyPublishers;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    43
import java.net.http.HttpResponse;
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56092
diff changeset
    44
import java.net.http.HttpResponse.BodyHandlers;
41355
f80c213ae8ff 8166791: Fix module dependencies for networking component tests
skovalev
parents: 38745
diff changeset
    45
import javax.net.ssl.SSLContext;
f80c213ae8ff 8166791: Fix module dependencies for networking component tests
skovalev
parents: 38745
diff changeset
    46
import javax.net.ssl.SSLParameters;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    47
import java.util.concurrent.Executors;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    48
import java.util.concurrent.ExecutorService;
38464
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    49
import jdk.testlibrary.SimpleSSLContext;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    50
import static java.net.http.HttpClient.Version.HTTP_2;
38464
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    51
41355
f80c213ae8ff 8166791: Fix module dependencies for networking component tests
skovalev
parents: 38745
diff changeset
    52
import org.testng.annotations.Test;
38464
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    53
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    54
/**
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    55
 * When selecting an unacceptable cipher suite the TLS handshake will fail.
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    56
 * But, the exception that was thrown was not being returned up to application
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    57
 * causing hang problems
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    58
 */
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    59
public class ErrorTest {
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    60
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    61
    static final String[] CIPHER_SUITES = new String[]{ "TLS_KRB5_WITH_3DES_EDE_CBC_SHA" };
38464
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    62
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    63
    static final String SIMPLE_STRING = "Hello world Goodbye world";
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    64
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    65
    //@Test(timeOut=5000)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    66
    @Test
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    67
    public void test() throws Exception {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    68
        SSLContext sslContext = (new SimpleSSLContext()).get();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    69
        ExecutorService exec = Executors.newCachedThreadPool();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    70
        HttpClient client = HttpClient.newBuilder()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    71
                                      .executor(exec)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    72
                                      .sslContext(sslContext)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    73
                                      .sslParameters(new SSLParameters(CIPHER_SUITES))
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    74
                                      .version(HTTP_2)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    75
                                      .build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    76
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    77
        Http2TestServer httpsServer = null;
38464
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    78
        try {
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 42460
diff changeset
    79
            SSLContext serverContext = (new SimpleSSLContext()).get();
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 42460
diff changeset
    80
            SSLParameters p = serverContext.getSupportedSSLParameters();
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 42460
diff changeset
    81
            p.setApplicationProtocols(new String[]{"h2"});
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    82
            httpsServer = new Http2TestServer(true,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    83
                                              0,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    84
                                              exec,
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 42460
diff changeset
    85
                                              serverContext);
45535
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 44854
diff changeset
    86
            httpsServer.addHandler(new Http2EchoHandler(), "/");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    87
            int httpsPort = httpsServer.getAddress().getPort();
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
    88
            String httpsURIString = "https://localhost:" + httpsPort + "/bar/";
38464
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    89
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    90
            httpsServer.start();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    91
            URI uri = URI.create(httpsURIString);
38464
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    92
            System.err.println("Request to " + uri);
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    93
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    94
            HttpRequest req = HttpRequest.newBuilder(uri)
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56092
diff changeset
    95
                                    .POST(BodyPublishers.ofString(SIMPLE_STRING))
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    96
                                    .build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
    97
            HttpResponse response;
38464
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
    98
            try {
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56092
diff changeset
    99
                response = client.send(req, BodyHandlers.discarding());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
   100
                throw new RuntimeException("Unexpected response: " + response);
38464
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
   101
            } catch (IOException e) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
   102
                System.err.println("Caught Expected IOException: " + e);
38464
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
   103
            }
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
   104
            System.err.println("DONE");
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
   105
        } finally {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41355
diff changeset
   106
            if (httpsServer != null )  { httpsServer.stop(); }
38464
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
   107
            exec.shutdownNow();
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
   108
        }
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
   109
    }
08512546de5f 8157105: HTTP/2 client hangs in blocking mode if an invalid frame has been received
michaelm
parents:
diff changeset
   110
}