Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/SetSoTimeout.java
41152 views
/*1* Copyright (c) 2020, 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// SunJSSE does not support dynamic system properties, no way to re-use24// system properties in samevm/agentvm mode.2526/*27* @test28* @bug 824603129* @summary Socket timeouts for getSession.30* @run main/othervm SetSoTimeout31*/3233import java.io.*;34import java.net.*;35import java.util.*;36import java.security.*;37import javax.net.ssl.*;3839public class SetSoTimeout {4041/*42* =============================================================43* Set the various variables needed for the tests, then44* specify what tests to run on each side.45*/4647/*48* Should we run the client or server in a separate thread?49* Both sides can throw exceptions, but do you have a preference50* as to which side should be the main thread.51*/52static boolean separateServerThread = true;5354/*55* Where do we find the keystores?56*/57static String pathToStores = "../../../../javax/net/ssl/etc";58static String keyStoreFile = "keystore";59static String trustStoreFile = "truststore";60static String passwd = "passphrase";6162/*63* Is the server ready to serve?64*/65volatile static boolean serverReady = false;6667/*68* Turn on SSL debugging?69*/70static boolean debug = false;717273/*74* Define the server side of the test.75*76* If the server prematurely exits, serverReady will be set to true77* to avoid infinite hangs.78*/79void doServerSide() throws Exception {80SSLServerSocketFactory sslssf =81(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();82SSLServerSocket sslServerSocket =83(SSLServerSocket) sslssf.createServerSocket(serverPort);8485serverPort = sslServerSocket.getLocalPort();8687/*88* Signal Client, we're ready for his connect.89*/90serverReady = true;9192SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();9394//Make server busy.95Thread.sleep(500);9697sslSocket.close();98}99100/*101* Define the client side of the test.102*103* If the server prematurely exits, serverReady will be set to true104* to avoid infinite hangs.105*/106void doClientSide() throws Exception {107boolean caught = false;108109/*110* Wait for server to get started.111*/112while (!serverReady) {113Thread.sleep(50);114}115116SSLSocketFactory sslsf = (SSLSocketFactory)SSLSocketFactory.getDefault();117SSLSocket sslSocket = (SSLSocket)sslsf.createSocket(118InetAddress.getLocalHost(),119serverPort );120121sslSocket.setSoTimeout(100); // The stall timeout.122123sslSocket.getSession();124//Check connection state.125if (!sslSocket.isClosed()) {126throw new RuntimeException("Expected connection to be closed");127}128129sslSocket.close();130131}132133134// use any free port by default135volatile int serverPort = 0;136137volatile Exception serverException = null;138volatile Exception clientException = null;139140141public static void main(String[] args) throws Exception {142String keyFilename =143System.getProperty("test.src", "./") + "/" + pathToStores +144"/" + keyStoreFile;145String trustFilename =146System.getProperty("test.src", "./") + "/" + pathToStores +147"/" + trustStoreFile;148149System.setProperty("javax.net.ssl.keyStore", keyFilename);150System.setProperty("javax.net.ssl.keyStorePassword", passwd);151System.setProperty("javax.net.ssl.trustStore", trustFilename);152System.setProperty("javax.net.ssl.trustStorePassword", passwd);153154if (debug)155System.setProperty("javax.net.debug", "all");156157/*158* Start the tests.159*/160new SetSoTimeout();161}162163Thread clientThread = null;164Thread serverThread = null;165166/*167* Primary constructor, used to drive remainder of the test.168*169* Fork off the other side, then do your work.170*/171SetSoTimeout() throws Exception {172if (separateServerThread) {173startServer(true);174startClient(false);175} else {176startClient(true);177startServer(false);178}179180/*181* Wait for other side to close down.182*/183if (separateServerThread) {184serverThread.join();185} else {186clientThread.join();187}188189/*190* When we get here, the test is pretty much over.191*192* If the main thread excepted, that propagates back193* immediately. If the other thread threw an exception, we194* should report back.195*/196if (serverException != null) {197System.out.print("Server Exception:");198throw serverException;199}200if (clientException != null) {201System.out.print("Client Exception:");202throw clientException;203}204}205206void startServer(boolean newThread) throws Exception {207if (newThread) {208serverThread = new Thread() {209public void run() {210try {211doServerSide();212} catch (Exception e) {213/*214* Our server thread just died.215*216* Release the client, if not active already...217*/218System.err.println("Server died...");219System.err.println(e);220serverReady = true;221serverException = e;222}223}224};225serverThread.start();226} else {227doServerSide();228}229}230231void startClient(boolean newThread) throws Exception {232if (newThread) {233clientThread = new Thread() {234public void run() {235try {236doClientSide();237} catch (Exception e) {238/*239* Our client thread just died.240*/241System.err.println("Client died...");242clientException = e;243}244}245};246clientThread.start();247} else {248doClientSide();249}250}251}252253254