1 /*
   2  * Copyright (c) 2015, 2016, 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  * @key intermittent
  28  * @library /lib/testlibrary
  29  * @build jdk.testlibrary.SimpleSSLContext
  30  * @modules java.httpclient
  31  *          jdk.security.auth
  32  * @compile/module=java.httpclient java/net/http/BodyOutputStream.java
  33  * @compile/module=java.httpclient java/net/http/BodyInputStream.java
  34  * @compile/module=java.httpclient java/net/http/EchoHandler.java
  35  * @compile/module=java.httpclient java/net/http/Http2Handler.java
  36  * @compile/module=java.httpclient java/net/http/Http2TestExchange.java
  37  * @compile/module=java.httpclient java/net/http/Http2TestServerConnection.java
  38  * @compile/module=java.httpclient java/net/http/Http2TestServer.java
  39  * @compile/module=java.httpclient java/net/http/OutgoingPushPromise.java
  40  * @compile/module=java.httpclient java/net/http/TestUtil.java
  41  * @run testng/othervm -Djava.net.http.HttpClient.log=ssl,errors ErrorTest
  42  * @summary check exception thrown when bad TLS parameters selected
  43  */
  44 
  45 import java.io.IOException;
  46 import java.net.URI;
  47 import java.net.http.EchoHandler;
  48 import java.net.http.HttpClient;
  49 import java.net.http.Http2TestServer;
  50 import java.net.http.HttpRequest;
  51 import java.net.http.HttpResponse;
  52 import java.util.concurrent.ExecutorService;
  53 import javax.net.ssl.SSLContext;
  54 import javax.net.ssl.SSLParameters;
  55 import jdk.testlibrary.SimpleSSLContext;
  56 
  57 import org.testng.annotations.Test;
  58 
  59 import static java.net.http.HttpClient.Version.HTTP_2;
  60 
  61 /**
  62  * When selecting an unacceptable cipher suite the TLS handshake will fail.
  63  * But, the exception that was thrown was not being returned up to application
  64  * causing hang problems
  65  */
  66 @Test
  67 public class ErrorTest {
  68     static int httpsPort;
  69     static Http2TestServer httpsServer;
  70     static HttpClient client = null;
  71     static ExecutorService exec;
  72     static SSLContext sslContext;
  73 
  74     static String httpsURIString;
  75 
  76     static HttpClient getClient() {
  77         if (client == null) {
  78             client = HttpClient.create()
  79                 .sslContext(sslContext)
  80                 .sslParameters(new SSLParameters(
  81                     new String[]{"TLS_KRB5_WITH_3DES_EDE_CBC_SHA"}))
  82                 .version(HTTP_2)
  83                 .build();
  84         }
  85         return client;
  86     }
  87 
  88     static URI getURI() {
  89         return URI.create(httpsURIString);
  90     }
  91 
  92     static final String SIMPLE_STRING = "Hello world Goodbye world";
  93 
  94     @Test(timeOut=5000)
  95     static void test() throws Exception {
  96         try {
  97             SimpleSSLContext sslct = new SimpleSSLContext();
  98             sslContext = sslct.get();
  99             client = getClient();
 100             exec = client.executorService();
 101 
 102             httpsServer = new Http2TestServer(true, 0, new EchoHandler(),
 103                     exec, sslContext);
 104 
 105             httpsPort = httpsServer.getAddress().getPort();
 106             httpsURIString = "https://127.0.0.1:" +
 107                 Integer.toString(httpsPort) + "/bar/";
 108 
 109             httpsServer.start();
 110             URI uri = getURI();
 111             System.err.println("Request to " + uri);
 112 
 113             HttpClient client = getClient();
 114             HttpRequest req = client.request(uri)
 115                     .body(HttpRequest.fromString(SIMPLE_STRING))
 116                     .POST();
 117             HttpResponse response = null;
 118             try {
 119                 response = req.response();
 120                 throw new RuntimeException("Expected exception");
 121             } catch (IOException e) {
 122                 System.err.println("Expected IOException received " + e);
 123             }
 124             System.err.println("DONE");
 125         } finally {
 126             httpsServer.stop();
 127             exec.shutdownNow();
 128         }
 129     }
 130 }