1 /*
   2  * Copyright (c) 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 import java.io.File;
  25 import java.io.IOException;
  26 import java.net.URI;
  27 import jdk.incubator.http.HttpClient;
  28 import jdk.incubator.http.HttpRequest;
  29 import jdk.incubator.http.HttpResponse;
  30 import jdk.incubator.http.HttpTimeoutException;
  31 import java.time.Duration;
  32 import java.util.concurrent.TimeUnit;
  33 import java.util.concurrent.CompletionException;
  34 import javax.net.ssl.SSLServerSocket;
  35 import javax.net.ssl.SSLParameters;
  36 import javax.net.ssl.SSLServerSocketFactory;
  37 import javax.net.ssl.SSLSocket;
  38 import static jdk.incubator.http.HttpRequest.BodyProcessor.fromString;
  39 import static jdk.incubator.http.HttpResponse.BodyHandler.asString;
  40 
  41 /*
  42  * @test
  43  * @bug 8156710
  44  * @summary Check if HttpTimeoutException is thrown if a server doesn't reply
  45  * @run main/othervm Timeout
  46  */
  47 public class Timeout {
  48 
  49     private static final int RANDOM_PORT = 0;
  50     private static final int TIMEOUT = 3 * 1000; // in millis
  51     private static final String KEYSTORE = System.getProperty("test.src")
  52             + File.separator + "keystore.p12";
  53     private static final String PASSWORD = "password";
  54 
  55     // indicates if server is ready to accept connections
  56     private static volatile boolean ready = false;
  57 
  58     public static void main(String[] args) throws Exception {
  59         test(false);
  60         test(true);
  61     }
  62 
  63     public static void test(boolean async) throws Exception {
  64         System.setProperty("javax.net.ssl.keyStore", KEYSTORE);
  65         System.setProperty("javax.net.ssl.keyStorePassword", PASSWORD);
  66         System.setProperty("javax.net.ssl.trustStore", KEYSTORE);
  67         System.setProperty("javax.net.ssl.trustStorePassword", PASSWORD);
  68 
  69         SSLServerSocketFactory factory =
  70                 (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
  71 
  72         try (SSLServerSocket ssocket =
  73                 (SSLServerSocket) factory.createServerSocket(RANDOM_PORT)) {
  74 
  75             // start server
  76             Thread server = new Thread(() -> {
  77                 while (true) {
  78                     System.out.println("server: ready");
  79                     SSLParameters params = ssocket.getSSLParameters();
  80                     params.setApplicationProtocols(new String[]{"h2"});
  81                     ssocket.setSSLParameters(params);
  82                     ready = true;
  83                     try (SSLSocket socket = (SSLSocket) ssocket.accept()) {
  84 
  85                         // just read forever
  86                         System.out.println("server: accepted");
  87                         while (true) {
  88                             socket.getInputStream().read();
  89                         }
  90                     } catch (IOException e) {
  91                         // ignore exceptions on server side
  92                         System.out.println("server: exception: " + e);
  93                     }
  94                 }
  95             });
  96             server.setDaemon(true);
  97             server.start();
  98 
  99             // wait for server is ready
 100             do {
 101                 Thread.sleep(1000);
 102             } while (!ready);
 103 
 104             String uri = "https://localhost:" + ssocket.getLocalPort();
 105             if (async) {
 106                 System.out.println(uri + ": Trying to connect asynchronously");
 107                 connectAsync(uri);
 108             } else {
 109                 System.out.println(uri + ": Trying to connect synchronously");
 110                 connect(uri);
 111             }
 112         }
 113     }
 114 
 115     private static void connect(String server) throws Exception {
 116         try {
 117             HttpClient client = HttpClient.newBuilder()
 118                                           .version(HttpClient.Version.HTTP_2)
 119                                           .build();
 120             HttpRequest request = HttpRequest.newBuilder(new URI(server))
 121                                              .timeout(Duration.ofMillis(TIMEOUT))
 122                                              .POST(fromString("body"))
 123                                              .build();
 124             HttpResponse<String> response = client.send(request, asString());
 125             System.out.println("Received unexpected reply: " + response.statusCode());
 126             throw new RuntimeException("unexpected successful connection");
 127         } catch (HttpTimeoutException e) {
 128             System.out.println("expected exception: " + e);
 129         }
 130     }
 131 
 132     private static void connectAsync(String server) throws Exception {
 133         try {
 134             HttpClient client = HttpClient.newBuilder()
 135                     .version(HttpClient.Version.HTTP_2)
 136                     .build();
 137             HttpRequest request = HttpRequest.newBuilder(new URI(server))
 138                     .timeout(Duration.ofMillis(TIMEOUT))
 139                     .POST(fromString("body"))
 140                     .build();
 141             HttpResponse<String> response = client.sendAsync(request, asString()).join();
 142             System.out.println("Received unexpected reply: " + response.statusCode());
 143             throw new RuntimeException("unexpected successful connection");
 144         } catch (CompletionException e) {
 145             if (e.getCause() instanceof HttpTimeoutException) {
 146                 System.out.println("expected exception: " + e.getCause());
 147             } else {
 148                 throw new RuntimeException("Unexpected exception received: " + e.getCause(), e);
 149             }
 150         }
 151     }
 152 
 153 }