Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/ReadTimeout.java
38889 views
/*1* Copyright (c) 2003, 2018, 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* @run main/othervm ReadTimeout35*/3637import java.io.*;38import java.net.*;39import javax.net.ssl.*;4041public class ReadTimeout {4243/*44* =============================================================45* Set the various variables needed for the tests, then46* specify what tests to run on each side.47*/4849/*50* Should we run the client or server in a separate thread?51* Both sides can throw exceptions, but do you have a preference52* as to which side should be the main thread.53*/54static boolean separateServerThread = true;5556/*57* Where do we find the keystores?58*/59static String pathToStores = "../../../../../../javax/net/ssl/etc";60static String keyStoreFile = "keystore";61static String trustStoreFile = "truststore";62static String passwd = "passphrase";6364/*65* Is the server ready to serve?66*/67volatile static boolean serverReady = false;6869/*70* Turn on SSL debugging?71*/72static boolean debug = false;7374/*75* Message posted76*/77static String postMsg = "Testing HTTP post on a https server";7879/*80* If the client or server is doing some kind of object creation81* that the other side depends on, and that thread prematurely82* exits, you may experience a hang. The test harness will83* terminate all hung threads after its timeout has expired,84* currently 3 minutes by default, but you might try to be85* smart about it....86*/8788/*89* Define the server side of the test.90*91* If the server prematurely exits, serverReady will be set to true92* to avoid infinite hangs.93*/94void doServerSide() throws Exception {95SSLServerSocketFactory sslssf =96(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();97SSLServerSocket sslServerSocket =98(SSLServerSocket) sslssf.createServerSocket(serverPort);99serverPort = sslServerSocket.getLocalPort();100101/*102* Signal Client, we're ready for his connect.103*/104serverReady = true;105try {106try (SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept()) {107InputStream sslIS = sslSocket.getInputStream();108BufferedReader br =109new BufferedReader(new InputStreamReader(sslIS));110br.readLine();111while (!finished()) {112Thread.sleep(2000);113}114}115116reset();117// doing second test118try (SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept()) {119InputStream sslIS = sslSocket.getInputStream();120BufferedReader br =121new BufferedReader(new InputStreamReader(sslIS));122br.readLine();123while (!finished()) {124Thread.sleep(2000);125}126}127} catch (Exception e) {128System.out.println("Should be an expected exception: " + e);129} finally {130sslServerSocket.close();131}132}133134boolean isFinished = false;135136synchronized boolean finished () {137return (isFinished);138}139synchronized void done () {140isFinished = true;141}142143synchronized void reset() {144isFinished = false;145}146147/*148* Define the client side of the test.149*150* If the server prematurely exits, serverReady will be set to true151* to avoid infinite hangs.152*/153void doClientSide() throws Exception {154HostnameVerifier reservedHV =155HttpsURLConnection.getDefaultHostnameVerifier();156try {157/*158* Wait for server to get started.159*/160while (!serverReady) {161Thread.sleep(50);162}163HttpsURLConnection http = null;164try {165URL url = new URL("https://localhost:" + serverPort);166167// set read timeout through system property168System.setProperty("sun.net.client.defaultReadTimeout", "2000");169HttpsURLConnection.setDefaultHostnameVerifier(170new NameVerifier());171http = (HttpsURLConnection)url.openConnection();172173InputStream is = http.getInputStream();174175throw new Exception(176"system property timeout configuration does not work");177} catch (SSLException | SocketTimeoutException ex) {178System.out.println("Got expected timeout exception for " +179"system property timeout configuration: " + getCause(ex));180} finally {181done();182http.disconnect();183}184185try {186URL url = new URL("https://localhost:" + serverPort);187188HttpsURLConnection.setDefaultHostnameVerifier(189new NameVerifier());190http = (HttpsURLConnection)url.openConnection();191// set read timeout through API192http.setReadTimeout(2000);193194InputStream is = http.getInputStream();195196throw new Exception(197"HttpsURLConnection.setReadTimeout() does not work");198} catch (SSLException | SocketTimeoutException ex) {199System.out.println("Got expected timeout exception for " +200"HttpsURLConnection.setReadTimeout(): " + getCause(ex));201} finally {202done();203http.disconnect();204}205} finally {206HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);207}208}209210private Exception getCause(Exception ex) {211Exception cause = null;212if (ex instanceof SSLException) {213cause = (Exception) ex.getCause();214if (!(cause instanceof SocketTimeoutException)) {215throw new RuntimeException("Unexpected cause", cause);216}217} else {218cause = ex;219}220221return cause;222}223224static class NameVerifier implements HostnameVerifier {225public boolean verify(String hostname, SSLSession session) {226return true;227}228}229230/*231* =============================================================232* The remainder is just support stuff233*/234235// use any free port by default236volatile int serverPort = 0;237238volatile Exception serverException = null;239volatile Exception clientException = null;240241public static void main(String[] args) throws Exception {242String keyFilename =243System.getProperty("test.src", "./") + "/" + pathToStores +244"/" + keyStoreFile;245String trustFilename =246System.getProperty("test.src", "./") + "/" + pathToStores +247"/" + trustStoreFile;248249System.setProperty("javax.net.ssl.keyStore", keyFilename);250System.setProperty("javax.net.ssl.keyStorePassword", passwd);251System.setProperty("javax.net.ssl.trustStore", trustFilename);252System.setProperty("javax.net.ssl.trustStorePassword", passwd);253254if (debug)255System.setProperty("javax.net.debug", "all");256257/*258* Start the tests.259*/260new ReadTimeout();261}262263Thread clientThread = null;264Thread serverThread = null;265266/*267* Primary constructor, used to drive remainder of the test.268*269* Fork off the other side, then do your work.270*/271ReadTimeout() throws Exception {272if (separateServerThread) {273startServer(true);274startClient(false);275} else {276startClient(true);277startServer(false);278}279280/*281* Wait for other side to close down.282*/283if (separateServerThread) {284serverThread.join();285} else {286clientThread.join();287}288289/*290* When we get here, the test is pretty much over.291*292* If the main thread excepted, that propagates back293* immediately. If the other thread threw an exception, we294* should report back.295*/296if (serverException != null)297throw serverException;298if (clientException != null)299throw clientException;300}301302void startServer(boolean newThread) throws Exception {303if (newThread) {304serverThread = new Thread() {305public void run() {306try {307doServerSide();308} catch (Exception e) {309/*310* Our server thread just died.311*312* Release the client, if not active already...313*/314System.err.println("Server died...");315serverReady = true;316serverException = e;317}318}319};320serverThread.start();321} else {322doServerSide();323}324}325326void startClient(boolean newThread) throws Exception {327if (newThread) {328clientThread = new Thread() {329public void run() {330try {331doClientSide();332} catch (Exception e) {333/*334* Our client thread just died.335*/336System.err.println("Client died...");337clientException = e;338}339}340};341clientThread.start();342} else {343doClientSide();344}345}346}347348349