Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ssl/SSLSocketImpl/ReuseAddr.java
38854 views
/*1* Copyright (c) 2001, 2011, 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* @test25* @bug 448244626* @summary java.net.SocketTimeoutException on 98, NT, 2000 for JSSE27* @run main/othervm ReuseAddr28*29* SunJSSE does not support dynamic system properties, no way to re-use30* system properties in samevm/agentvm mode.31* @author Brad Wetmore32*/3334import java.io.*;35import java.net.*;36import javax.net.ssl.*;3738public class ReuseAddr {3940/*41* =============================================================42* Set the various variables needed for the tests, then43* specify what tests to run on each side.44*/4546/*47* Should we run the client or server in a separate thread?48* Both sides can throw exceptions, but do you have a preference49* as to which side should be the main thread.50*/51static boolean separateServerThread = true;5253/*54* Where do we find the keystores?55*/56private final static String pathToStores = "../../../../javax/net/ssl/etc";57static String keyStoreFile = "keystore";58static String trustStoreFile = "truststore";59static String passwd = "passphrase";6061/*62* Is the server ready to serve?63*/64volatile static boolean serverReady = false;6566/*67* Turn on SSL debugging?68*/69static boolean debug = false;7071/*72* If the client or server is doing some kind of object creation73* that the other side depends on, and that thread prematurely74* exits, you may experience a hang. The test harness will75* terminate all hung threads after its timeout has expired,76* currently 3 minutes by default, but you might try to be77* smart about it....78*/7980/*81* Define the server side of the test.82*83* If the server prematurely exits, serverReady will be set to true84* to avoid infinite hangs.85*/86void doServerSide() throws Exception {87SSLServerSocketFactory sslssf =88(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();89SSLServerSocket sslServerSocket =90(SSLServerSocket) sslssf.createServerSocket(serverPort);91serverPort = sslServerSocket.getLocalPort();9293/*94* Signal Client, we're ready for his connect.95*/96serverReady = true;9798SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();99InputStream sslIS = sslSocket.getInputStream();100OutputStream sslOS = sslSocket.getOutputStream();101102sslIS.read();103sslOS.write(85);104sslOS.flush();105106sslSocket.close();107108// Close original server socket109sslServerSocket.close();110111// Try rebinding to same port112sslServerSocket =113(SSLServerSocket) sslssf.createServerSocket(serverPort);114sslServerSocket.close();115}116117/*118* Define the client side of the test.119*120* If the server prematurely exits, serverReady will be set to true121* to avoid infinite hangs.122*/123void doClientSide() throws Exception {124125/*126* Wait for server to get started.127*/128while (!serverReady) {129Thread.sleep(50);130}131132SSLSocketFactory sslsf =133(SSLSocketFactory) SSLSocketFactory.getDefault();134SSLSocket sslSocket = (SSLSocket)135sslsf.createSocket("localhost", serverPort);136137InputStream sslIS = sslSocket.getInputStream();138OutputStream sslOS = sslSocket.getOutputStream();139140sslOS.write(280);141sslOS.flush();142sslIS.read();143144sslSocket.close();145}146147/*148* =============================================================149* The remainder is just support stuff150*/151152// use any free port by default153volatile int serverPort = 0;154155volatile Exception serverException = null;156volatile Exception clientException = null;157158public static void main(String[] args) throws Exception {159String keyFilename =160System.getProperty("test.src", "./") + "/" + pathToStores +161"/" + keyStoreFile;162String trustFilename =163System.getProperty("test.src", "./") + "/" + pathToStores +164"/" + trustStoreFile;165166System.setProperty("javax.net.ssl.keyStore", keyFilename);167System.setProperty("javax.net.ssl.keyStorePassword", passwd);168System.setProperty("javax.net.ssl.trustStore", trustFilename);169System.setProperty("javax.net.ssl.trustStorePassword", passwd);170171if (debug)172System.setProperty("javax.net.debug", "all");173174/*175* Start the tests.176*/177new ReuseAddr();178}179180Thread clientThread = null;181Thread serverThread = null;182183/*184* Primary constructor, used to drive remainder of the test.185*186* Fork off the other side, then do your work.187*/188ReuseAddr() throws Exception {189if (separateServerThread) {190startServer(true);191startClient(false);192} else {193startClient(true);194startServer(false);195}196197/*198* Wait for other side to close down.199*/200if (separateServerThread) {201serverThread.join();202} else {203clientThread.join();204}205206/*207* When we get here, the test is pretty much over.208*209* If the main thread excepted, that propagates back210* immediately. If the other thread threw an exception, we211* should report back.212*/213if (serverException != null)214throw serverException;215if (clientException != null)216throw clientException;217}218219void startServer(boolean newThread) throws Exception {220if (newThread) {221serverThread = new Thread() {222public void run() {223try {224doServerSide();225} catch (Exception e) {226/*227* Our server thread just died.228*229* Release the client, if not active already...230*/231System.err.println("Server died...");232serverReady = true;233serverException = e;234}235}236};237serverThread.start();238} else {239doServerSide();240}241}242243void startClient(boolean newThread) throws Exception {244if (newThread) {245clientThread = new Thread() {246public void run() {247try {248doClientSide();249} catch (Exception e) {250/*251* Our client thread just died.252*/253System.err.println("Client died...");254clientException = e;255}256}257};258clientThread.start();259} else {260doClientSide();261}262}263}264265266