Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/UnconnectedSocketWrongExceptions.java
41152 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 448044126* @summary startHandshake giving wrong message when unconnected.27* @run main/othervm UnconnectedSocketWrongExceptions28*29* SunJSSE does not support dynamic system properties, no way to re-use30* system properties in samevm/agentvm mode.31*32* @author Brad Wetmore33*/3435import java.io.*;36import java.net.*;37import javax.net.ssl.*;3839public class UnconnectedSocketWrongExceptions {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;7172/*73* If the client or server is doing some kind of object creation74* that the other side depends on, and that thread prematurely75* exits, you may experience a hang. The test harness will76* terminate all hung threads after its timeout has expired,77* currently 3 minutes by default, but you might try to be78* smart about it....79*/8081/*82* Define the server side of the test.83*84* If the server prematurely exits, serverReady will be set to true85* to avoid infinite hangs.86*/87void doServerSide() throws Exception {88SSLServerSocketFactory sslssf =89(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();90SSLServerSocket sslServerSocket =91(SSLServerSocket) sslssf.createServerSocket(serverPort);9293serverPort = sslServerSocket.getLocalPort();9495/*96* Signal Client, we're ready for his connect.97*/98serverReady = true;99100SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();101102InputStream sslIS = sslSocket.getInputStream();103OutputStream sslOS = sslSocket.getOutputStream();104105sslIS.read();106sslOS.write(85);107sslOS.flush();108109sslSocket.close();110}111112/*113* Define the client side of the test.114*115* If the server prematurely exits, serverReady will be set to true116* to avoid infinite hangs.117*/118void doClientSide() throws Exception {119120/*121* Wait for server to get started.122*/123while (!serverReady) {124Thread.sleep(50);125}126127SSLSocketFactory sslsf =128(SSLSocketFactory) SSLSocketFactory.getDefault();129SSLSocket sslSocket = (SSLSocket)130sslsf.createSocket();131132InputStream sslIS;133OutputStream sslOS;134135try {136System.out.print("getInputStream()...");137sslIS = sslSocket.getInputStream();138throw new Exception("getInputStream didn't throw properly");139} catch (SocketException e) {140System.out.println("Caught proper exception: " + e.toString());141}142143try {144System.out.print("getOutputStream()...");145sslOS = sslSocket.getOutputStream();146throw new Exception("getOutputStream didn't throw properly");147} catch (SocketException e) {148System.out.println("Caught proper exception: " + e.toString());149}150151try {152System.out.print("startHandshake()...");153sslSocket.startHandshake();154throw new Exception("startHandshake() didn't throw properly");155} catch (SocketException e) {156System.out.println("Caught proper exception: " + e.toString());157}158159InetSocketAddress addr = new InetSocketAddress(160"localhost", serverPort);161sslSocket.connect(addr, 2000);162sslOS = sslSocket.getOutputStream();163sslIS = sslSocket.getInputStream();164165sslOS.write(280);166sslOS.flush();167System.out.println("reading: " + sslIS.read());168169sslSocket.close();170}171172/*173* =============================================================174* The remainder is just support stuff175*/176177// use any free port by default178volatile int serverPort = 0;179180volatile Exception serverException = null;181volatile Exception clientException = null;182183public static void main(String[] args) throws Exception {184String keyFilename =185System.getProperty("test.src", "./") + "/" + pathToStores +186"/" + keyStoreFile;187String trustFilename =188System.getProperty("test.src", "./") + "/" + pathToStores +189"/" + trustStoreFile;190191System.setProperty("javax.net.ssl.keyStore", keyFilename);192System.setProperty("javax.net.ssl.keyStorePassword", passwd);193System.setProperty("javax.net.ssl.trustStore", trustFilename);194System.setProperty("javax.net.ssl.trustStorePassword", passwd);195196if (debug)197System.setProperty("javax.net.debug", "all");198199/*200* Start the tests.201*/202new UnconnectedSocketWrongExceptions();203}204205Thread clientThread = null;206Thread serverThread = null;207208/*209* Primary constructor, used to drive remainder of the test.210*211* Fork off the other side, then do your work.212*/213UnconnectedSocketWrongExceptions() throws Exception {214if (separateServerThread) {215startServer(true);216startClient(false);217} else {218startClient(true);219startServer(false);220}221222/*223* Wait for other side to close down.224*/225if (separateServerThread) {226serverThread.join();227} else {228clientThread.join();229}230231/*232* When we get here, the test is pretty much over.233*234* If the main thread excepted, that propagates back235* immediately. If the other thread threw an exception, we236* should report back.237*/238if (serverException != null) {239System.out.print("Server Exception:");240throw serverException;241}242if (clientException != null) {243System.out.print("Client Exception:");244throw clientException;245}246}247248void startServer(boolean newThread) throws Exception {249if (newThread) {250serverThread = new Thread() {251public void run() {252try {253doServerSide();254} catch (Exception e) {255/*256* Our server thread just died.257*258* Release the client, if not active already...259*/260System.err.println("Server died...");261serverReady = true;262serverException = e;263}264}265};266serverThread.start();267} else {268doServerSide();269}270}271272void startClient(boolean newThread) throws Exception {273if (newThread) {274clientThread = new Thread() {275public void run() {276try {277doClientSide();278} catch (Exception e) {279/*280* Our client thread just died.281*/282System.err.println("Client died...");283clientException = e;284}285}286};287clientThread.start();288} else {289doClientSide();290}291}292}293294295