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 java.base/sun.net.www.http
  30  *          jdk.incubator.httpclient/jdk.incubator.http.internal.common
  31  *          jdk.incubator.httpclient/jdk.incubator.http.internal.frame
  32  *          jdk.incubator.httpclient/jdk.incubator.http.internal.hpack
  33  *          java.security.jgss
  34  * @run testng/othervm/timeout=60 -Djavax.net.debug=ssl -Djdk.httpclient.HttpClient.log=all ErrorTest
  35  * @summary check exception thrown when bad TLS parameters selected
  36  */
  37 
  38 import java.io.IOException;
  39 import java.net.URI;
  40 import jdk.incubator.http.HttpClient;
  41 import jdk.incubator.http.HttpRequest;
  42 import jdk.incubator.http.HttpResponse;
  43 import javax.net.ssl.SSLContext;
  44 import javax.net.ssl.SSLParameters;
  45 import java.util.concurrent.Executors;
  46 import java.util.concurrent.ExecutorService;
  47 import jdk.testlibrary.SimpleSSLContext;
  48 import static jdk.incubator.http.HttpClient.Version.HTTP_2;
  49 import static jdk.incubator.http.HttpRequest.BodyPublisher.fromString;
  50 import static jdk.incubator.http.HttpResponse.BodyHandler.discard;
  51 
  52 import org.testng.annotations.Test;
  53 
  54 /**
  55  * When selecting an unacceptable cipher suite the TLS handshake will fail.
  56  * But, the exception that was thrown was not being returned up to application
  57  * causing hang problems
  58  */
  59 public class ErrorTest {
  60 
  61     static final String[] CIPHER_SUITES = new String[]{ "TLS_KRB5_WITH_3DES_EDE_CBC_SHA" };
  62 
  63     static final String SIMPLE_STRING = "Hello world Goodbye world";
  64 
  65     //@Test(timeOut=5000)
  66     @Test
  67     public void test() throws Exception {
  68         SSLContext sslContext = (new SimpleSSLContext()).get();
  69         ExecutorService exec = Executors.newCachedThreadPool();
  70         HttpClient client = HttpClient.newBuilder()
  71                                       .executor(exec)
  72                                       .sslContext(sslContext)
  73                                       .sslParameters(new SSLParameters(CIPHER_SUITES))
  74                                       .version(HTTP_2)
  75                                       .build();
  76 
  77         Http2TestServer httpsServer = null;
  78         try {
  79             SSLContext serverContext = (new SimpleSSLContext()).get();
  80             SSLParameters p = serverContext.getSupportedSSLParameters();
  81             p.setApplicationProtocols(new String[]{"h2"});
  82             httpsServer = new Http2TestServer(true,
  83                                               0,
  84                                               exec,
  85                                               serverContext);
  86             httpsServer.addHandler(new Http2EchoHandler(), "/");
  87             int httpsPort = httpsServer.getAddress().getPort();
  88             String httpsURIString = "https://127.0.0.1:" + httpsPort + "/bar/";
  89 
  90             httpsServer.start();
  91             URI uri = URI.create(httpsURIString);
  92             System.err.println("Request to " + uri);
  93 
  94             HttpRequest req = HttpRequest.newBuilder(uri)
  95                                     .POST(fromString(SIMPLE_STRING))
  96                                     .build();
  97             HttpResponse response;
  98             try {
  99                 response = client.send(req, discard(null));
 100                 throw new RuntimeException("Unexpected response: " + response);
 101             } catch (IOException e) {
 102                 System.err.println("Caught Expected IOException: " + e);
 103             }
 104             System.err.println("DONE");
 105         } finally {
 106             if (httpsServer != null )  { httpsServer.stop(); }
 107             exec.shutdownNow();
 108         }
 109     }
 110 }