Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/CloseSocketException.java
41152 views
/*1* Copyright (c) 2003, 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 496979926* @summary javax.net.ssl.SSLSocket.SSLSocket(InetAddress,int) shouldn't27* throw exception28* @run main/othervm CloseSocketException29*30* SunJSSE does not support dynamic system properties, no way to re-use31* system properties in samevm/agentvm mode.32*33* This is making sure that starting a new handshake throws the right34* exception. There is a similar test for SSLEngine.35*36* @author Brad Wetmore37*/3839import java.io.*;40import java.net.*;41import javax.net.ssl.*;4243public class CloseSocketException {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 = false;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* If the client or server is doing some kind of object creation78* that the other side depends on, and that thread prematurely79* exits, you may experience a hang. The test harness will80* terminate all hung threads after its timeout has expired,81* currently 3 minutes by default, but you might try to be82* smart about it....83*/8485/*86* Define the server side of the test.87*88* If the server prematurely exits, serverReady will be set to true89* to avoid infinite hangs.90*/91void doServerSide() throws Exception {92SSLServerSocketFactory sslssf =93(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();94SSLServerSocket sslServerSocket =95(SSLServerSocket) sslssf.createServerSocket(serverPort);9697serverPort = sslServerSocket.getLocalPort();9899/*100* Signal Client, we're ready for his connect.101*/102serverReady = true;103104SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();105InputStream sslIS = sslSocket.getInputStream();106OutputStream sslOS = sslSocket.getOutputStream();107108sslIS.read();109sslOS.write(85);110sslOS.flush();111112sslSocket.close();113}114115/*116* Define the client side of the test.117*118* If the server prematurely exits, serverReady will be set to true119* to avoid infinite hangs.120*/121void doClientSide() throws Exception {122123/*124* Wait for server to get started.125*/126while (!serverReady) {127Thread.sleep(50);128}129130SSLSocketFactory sslsf =131(SSLSocketFactory) SSLSocketFactory.getDefault();132SSLSocket sslSocket = (SSLSocket)133sslsf.createSocket("localhost", serverPort);134135InputStream sslIS = sslSocket.getInputStream();136OutputStream sslOS = sslSocket.getOutputStream();137138sslOS.write(280);139sslOS.flush();140sslIS.read();141142sslSocket.close();143144try {145sslSocket.startHandshake();146throw new Exception("TEST FAILED: didn't throw SSLException");147} catch (IOException e) {148System.out.println("TEST PASSED: Caught right Exception");149}150}151152/*153* =============================================================154* The remainder is just support stuff155*/156157// use any free port by default158volatile int serverPort = 0;159160volatile Exception serverException = null;161volatile Exception clientException = null;162163public static void main(String[] args) throws Exception {164String keyFilename =165System.getProperty("test.src", "./") + "/" + pathToStores +166"/" + keyStoreFile;167String trustFilename =168System.getProperty("test.src", "./") + "/" + pathToStores +169"/" + trustStoreFile;170171System.setProperty("javax.net.ssl.keyStore", keyFilename);172System.setProperty("javax.net.ssl.keyStorePassword", passwd);173System.setProperty("javax.net.ssl.trustStore", trustFilename);174System.setProperty("javax.net.ssl.trustStorePassword", passwd);175176if (debug)177System.setProperty("javax.net.debug", "all");178179/*180* Start the tests.181*/182new CloseSocketException();183}184185Thread clientThread = null;186Thread serverThread = null;187188/*189* Primary constructor, used to drive remainder of the test.190*191* Fork off the other side, then do your work.192*/193CloseSocketException() throws Exception {194try {195if (separateServerThread) {196startServer(true);197startClient(false);198} else {199startClient(true);200startServer(false);201}202} catch (Exception e) {203// swallow for now. Show later204}205206/*207* Wait for other side to close down.208*/209if (separateServerThread) {210serverThread.join();211} else {212clientThread.join();213}214215/*216* When we get here, the test is pretty much over.217* Which side threw the error?218*/219Exception local;220Exception remote;221String whichRemote;222223if (separateServerThread) {224remote = serverException;225local = clientException;226whichRemote = "server";227} else {228remote = clientException;229local = serverException;230whichRemote = "client";231}232233/*234* If both failed, return the curthread's exception, but also235* print the remote side Exception236*/237if ((local != null) && (remote != null)) {238System.out.println(whichRemote + " also threw:");239remote.printStackTrace();240System.out.println();241throw local;242}243244if (remote != null) {245throw remote;246}247248if (local != null) {249throw local;250}251}252253void startServer(boolean newThread) throws Exception {254if (newThread) {255serverThread = new Thread() {256public void run() {257try {258doServerSide();259} catch (Exception e) {260/*261* Our server thread just died.262*263* Release the client, if not active already...264*/265System.err.println("Server died...");266serverReady = true;267serverException = e;268}269}270};271serverThread.start();272} else {273try {274doServerSide();275} catch (Exception e) {276serverException = e;277} finally {278serverReady = true;279}280}281}282283void startClient(boolean newThread) throws Exception {284if (newThread) {285clientThread = new Thread() {286public void run() {287try {288doClientSide();289} catch (Exception e) {290/*291* Our client thread just died.292*/293System.err.println("Client died...");294clientException = e;295}296}297};298clientThread.start();299} else {300try {301doClientSide();302} catch (Exception e) {303clientException = e;304}305}306}307}308309310