Path: blob/master/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java
41161 views
/*1* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223//24// SunJSSE does not support dynamic system properties, no way to re-use25// system properties in samevm/agentvm mode.26//2728/*29* @test30* @bug 4811482 4700777 490541031* @summary sun.net.client.defaultConnectTimeout should work with32* HttpsURLConnection; HTTP client: Connect and read timeouts;33* Https needs to support new tiger features that went into http34* @library /test/lib35* @run main/othervm ReadTimeout36*/3738import java.io.*;39import java.net.*;40import javax.net.ssl.*;41import jdk.test.lib.net.URIBuilder;4243public class ReadTimeout {4445/*46* =============================================================47* Set the various variables needed for the tests, then48* specify what tests to run on each side.49*/5051/*52* Should we run the client or server in a separate thread?53* Both sides can throw exceptions, but do you have a preference54* as to which side should be the main thread.55*/56static boolean separateServerThread = true;5758/*59* Where do we find the keystores?60*/61static String pathToStores = "../../../../../../javax/net/ssl/etc";62static String keyStoreFile = "keystore";63static String trustStoreFile = "truststore";64static String passwd = "passphrase";6566/*67* Is the server ready to serve?68*/69volatile static boolean serverReady = false;7071/*72* Turn on SSL debugging?73*/74static boolean debug = false;7576/*77* Message posted78*/79static String postMsg = "Testing HTTP post on a https server";8081/*82* If the client or server is doing some kind of object creation83* that the other side depends on, and that thread prematurely84* exits, you may experience a hang. The test harness will85* terminate all hung threads after its timeout has expired,86* currently 3 minutes by default, but you might try to be87* smart about it....88*/8990/*91* Define the server side of the test.92*93* If the server prematurely exits, serverReady will be set to true94* to avoid infinite hangs.95*/96void doServerSide() throws Exception {97InetAddress loopback = InetAddress.getLoopbackAddress();98SSLServerSocketFactory sslssf =99(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();100SSLServerSocket sslServerSocket =101(SSLServerSocket) sslssf.createServerSocket(serverPort, 0, loopback);102serverPort = sslServerSocket.getLocalPort();103104/*105* Signal Client, we're ready for his connect.106*/107serverReady = true;108try {109try (SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept()) {110InputStream sslIS = sslSocket.getInputStream();111BufferedReader br =112new BufferedReader(new InputStreamReader(sslIS));113br.readLine();114while (!finished()) {115Thread.sleep(2000);116}117}118119reset();120// doing second test121try (SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept()) {122InputStream sslIS = sslSocket.getInputStream();123BufferedReader br =124new BufferedReader(new InputStreamReader(sslIS));125br.readLine();126while (!finished()) {127Thread.sleep(2000);128}129}130} catch (Exception e) {131System.out.println("Should be an expected exception: " + e);132} finally {133sslServerSocket.close();134}135}136137boolean isFinished = false;138139synchronized boolean finished () {140return (isFinished);141}142synchronized void done () {143isFinished = true;144}145146synchronized void reset() {147isFinished = false;148}149150/*151* Define the client side of the test.152*153* If the server prematurely exits, serverReady will be set to true154* to avoid infinite hangs.155*/156void doClientSide() throws Exception {157HostnameVerifier reservedHV =158HttpsURLConnection.getDefaultHostnameVerifier();159try {160/*161* Wait for server to get started.162*/163while (!serverReady) {164Thread.sleep(50);165}166HttpsURLConnection http = null;167try {168URL url = URIBuilder.newBuilder()169.scheme("https")170.loopback()171.port(serverPort)172.toURL();173174// set read timeout through system property175System.setProperty("sun.net.client.defaultReadTimeout", "2000");176HttpsURLConnection.setDefaultHostnameVerifier(177new NameVerifier());178http = (HttpsURLConnection)url.openConnection();179180InputStream is = http.getInputStream();181182throw new Exception(183"system property timeout configuration does not work");184} catch (SSLException | SocketTimeoutException ex) {185System.out.println("Got expected timeout exception for " +186"system property timeout configuration: " + getCause(ex));187} finally {188done();189http.disconnect();190}191192try {193URL url = URIBuilder.newBuilder()194.scheme("https")195.loopback()196.port(serverPort)197.toURL();198199HttpsURLConnection.setDefaultHostnameVerifier(200new NameVerifier());201http = (HttpsURLConnection)url.openConnection();202// set read timeout through API203http.setReadTimeout(2000);204205InputStream is = http.getInputStream();206207throw new Exception(208"HttpsURLConnection.setReadTimeout() does not work");209} catch (SSLException | SocketTimeoutException ex) {210System.out.println("Got expected timeout exception for " +211"HttpsURLConnection.setReadTimeout(): " + getCause(ex));212} finally {213done();214http.disconnect();215}216} finally {217HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);218}219}220221private Exception getCause(Exception ex) {222Exception cause = null;223if (ex instanceof SSLException) {224cause = (Exception) ex.getCause();225if (!(cause instanceof SocketTimeoutException)) {226throw new RuntimeException("Unexpected cause", cause);227}228} else {229cause = ex;230}231232return cause;233}234235static class NameVerifier implements HostnameVerifier {236public boolean verify(String hostname, SSLSession session) {237return true;238}239}240241/*242* =============================================================243* The remainder is just support stuff244*/245246// use any free port by default247volatile int serverPort = 0;248249volatile Exception serverException = null;250volatile Exception clientException = null;251252private boolean sslConnectionFailed() {253return clientException instanceof SSLHandshakeException;254}255256public static void main(String[] args) throws Exception {257String keyFilename =258System.getProperty("test.src", "./") + "/" + pathToStores +259"/" + keyStoreFile;260String trustFilename =261System.getProperty("test.src", "./") + "/" + pathToStores +262"/" + trustStoreFile;263264System.setProperty("javax.net.ssl.keyStore", keyFilename);265System.setProperty("javax.net.ssl.keyStorePassword", passwd);266System.setProperty("javax.net.ssl.trustStore", trustFilename);267System.setProperty("javax.net.ssl.trustStorePassword", passwd);268269if (debug)270System.setProperty("javax.net.debug", "all");271272/*273* Start the tests.274*/275new ReadTimeout();276}277278Thread clientThread = null;279Thread serverThread = null;280281/*282* Primary constructor, used to drive remainder of the test.283*284* Fork off the other side, then do your work.285*/286ReadTimeout() throws Exception {287if (separateServerThread) {288startServer(true);289startClient(false);290} else {291startClient(true);292startServer(false);293}294295/*296* Wait for other side to close down.297*/298if (separateServerThread) {299if (!sslConnectionFailed()) {300serverThread.join();301}302} else {303clientThread.join();304}305306/*307* When we get here, the test is pretty much over.308*309* If the main thread excepted, that propagates back310* immediately. If the other thread threw an exception, we311* should report back.312*/313if (serverException != null)314throw serverException;315if (clientException != null)316throw clientException;317}318319void startServer(boolean newThread) throws Exception {320if (newThread) {321serverThread = new Thread() {322public void run() {323try {324doServerSide();325} catch (Exception e) {326/*327* Our server thread just died.328*329* Release the client, if not active already...330*/331System.err.println("Server died...");332serverReady = true;333serverException = e;334}335}336};337serverThread.start();338} else {339doServerSide();340}341}342343void startClient(boolean newThread) throws Exception {344if (newThread) {345clientThread = new Thread() {346public void run() {347try {348doClientSide();349} catch (Exception e) {350/*351* Our client thread just died.352*/353System.err.println("Client died...");354clientException = e;355}356}357};358clientThread.start();359} else {360doClientSide();361}362}363}364365366