test/jdk/java/net/httpclient/http2/ErrorTest.java
changeset 47216 71c04702a3d5
parent 45832 f77d4be2786a
child 48083 b1c1b4ef4be2
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 8157105
       
    27  * @library /lib/testlibrary server
       
    28  * @build jdk.testlibrary.SimpleSSLContext
       
    29  * @modules jdk.incubator.httpclient/jdk.incubator.http.internal.common
       
    30  *          jdk.incubator.httpclient/jdk.incubator.http.internal.frame
       
    31  *          jdk.incubator.httpclient/jdk.incubator.http.internal.hpack
       
    32  *          java.security.jgss
       
    33  * @run testng/othervm/timeout=60 -Djavax.net.debug=ssl -Djdk.httpclient.HttpClient.log=all ErrorTest
       
    34  * @summary check exception thrown when bad TLS parameters selected
       
    35  */
       
    36 
       
    37 import java.io.IOException;
       
    38 import java.net.URI;
       
    39 import jdk.incubator.http.HttpClient;
       
    40 import jdk.incubator.http.HttpRequest;
       
    41 import jdk.incubator.http.HttpResponse;
       
    42 import javax.net.ssl.SSLContext;
       
    43 import javax.net.ssl.SSLParameters;
       
    44 import java.util.concurrent.Executors;
       
    45 import java.util.concurrent.ExecutorService;
       
    46 import jdk.testlibrary.SimpleSSLContext;
       
    47 import static jdk.incubator.http.HttpClient.Version.HTTP_2;
       
    48 import static jdk.incubator.http.HttpRequest.BodyProcessor.fromString;
       
    49 import static jdk.incubator.http.HttpResponse.BodyHandler.discard;
       
    50 
       
    51 import org.testng.annotations.Test;
       
    52 
       
    53 /**
       
    54  * When selecting an unacceptable cipher suite the TLS handshake will fail.
       
    55  * But, the exception that was thrown was not being returned up to application
       
    56  * causing hang problems
       
    57  */
       
    58 public class ErrorTest {
       
    59 
       
    60     static final String[] CIPHER_SUITES = new String[]{ "TLS_KRB5_WITH_3DES_EDE_CBC_SHA" };
       
    61 
       
    62     static final String SIMPLE_STRING = "Hello world Goodbye world";
       
    63 
       
    64     //@Test(timeOut=5000)
       
    65     @Test
       
    66     public void test() throws Exception {
       
    67         SSLContext sslContext = (new SimpleSSLContext()).get();
       
    68         ExecutorService exec = Executors.newCachedThreadPool();
       
    69         HttpClient client = HttpClient.newBuilder()
       
    70                                       .executor(exec)
       
    71                                       .sslContext(sslContext)
       
    72                                       .sslParameters(new SSLParameters(CIPHER_SUITES))
       
    73                                       .version(HTTP_2)
       
    74                                       .build();
       
    75 
       
    76         Http2TestServer httpsServer = null;
       
    77         try {
       
    78             SSLContext serverContext = (new SimpleSSLContext()).get();
       
    79             SSLParameters p = serverContext.getSupportedSSLParameters();
       
    80             p.setApplicationProtocols(new String[]{"h2"});
       
    81             httpsServer = new Http2TestServer(true,
       
    82                                               0,
       
    83                                               exec,
       
    84                                               serverContext);
       
    85             httpsServer.addHandler(new Http2EchoHandler(), "/");
       
    86             int httpsPort = httpsServer.getAddress().getPort();
       
    87             String httpsURIString = "https://127.0.0.1:" + httpsPort + "/bar/";
       
    88 
       
    89             httpsServer.start();
       
    90             URI uri = URI.create(httpsURIString);
       
    91             System.err.println("Request to " + uri);
       
    92 
       
    93             HttpRequest req = HttpRequest.newBuilder(uri)
       
    94                                     .POST(fromString(SIMPLE_STRING))
       
    95                                     .build();
       
    96             HttpResponse response;
       
    97             try {
       
    98                 response = client.send(req, discard(null));
       
    99                 throw new RuntimeException("Unexpected response: " + response);
       
   100             } catch (IOException e) {
       
   101                 System.err.println("Caught Expected IOException: " + e);
       
   102             }
       
   103             System.err.println("DONE");
       
   104         } finally {
       
   105             if (httpsServer != null )  { httpsServer.stop(); }
       
   106             exec.shutdownNow();
       
   107         }
       
   108     }
       
   109 }