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