Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/SSLSocketImplThrowsWrongExceptions.java
41152 views
/*1* Copyright (c) 2001, 2021, 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 4361124 432580631* @summary SSLServerSocket isn't throwing exceptions when negotiations are32* failing & java.net.SocketException: occures in Auth and clientmode33* @run main/othervm SSLSocketImplThrowsWrongExceptions34* @author Brad Wetmore35*/3637import java.net.SocketException;38import java.util.concurrent.CountDownLatch;39import javax.net.ssl.*;4041public class SSLSocketImplThrowsWrongExceptions {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 passwd = "passphrase";6263/*64* Is the server ready to serve?65*/66private CountDownLatch serverReadyLatch = new CountDownLatch(1);6768/*69* Turn on SSL debugging?70*/71static boolean debug = false;727374/*75* Define the server side of the test.76*/77void doServerSide() throws Exception {78System.out.println("starting Server");79SSLServerSocketFactory sslssf =80(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();81SSLServerSocket sslServerSocket =82(SSLServerSocket) sslssf.createServerSocket(serverPort);83serverPort = sslServerSocket.getLocalPort();84System.out.println("got server socket");8586/*87* Signal Client, we're ready for his connect.88*/89serverReadyLatch.countDown();9091try {92System.out.println("Server socket accepting...");93SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();94System.out.println("Server starting handshake");95sslSocket.startHandshake();96throw new Exception("Handshake was successful");97} catch (SSLException | SocketException se){98/*99* Caught the right Exception. Swallow it.100*/101System.out.println("Server reported the right exception");102System.out.println(se);103} catch (Exception e) {104/*105* Caught the wrong exception. Rethrow it.106*/107System.out.println("Server reported the wrong exception");108throw e;109}110111}112113/*114* Define the client side of the test.115*/116void doClientSide() throws Exception {117118System.out.println(" Client starting");119120/*121* Wait for server to get started.122*/123serverReadyLatch.await();124125SSLSocketFactory sslsf =126(SSLSocketFactory) SSLSocketFactory.getDefault();127try {128System.out.println(" Client creating socket");129SSLSocket sslSocket = (SSLSocket)130sslsf.createSocket("localhost", serverPort);131System.out.println(" Client starting handshake");132sslSocket.startHandshake();133throw new Exception("Handshake was successful");134} catch (SSLException e) {135/*136* Caught the right Exception. Swallow it.137*/138System.out.println(" Client reported correct exception");139System.out.println(" " + e.toString());140} catch (Exception e) {141/*142* Caught the wrong exception. Rethrow it.143*/144System.out.println(" Client reported the wrong exception");145throw e;146}147}148149/*150* =============================================================151* The remainder is just support stuff152*/153154// use any free port by default155volatile int serverPort = 0;156157volatile Exception serverException = null;158volatile Exception clientException = null;159160public static void main(String[] args) throws Exception {161String keyFilename =162System.getProperty("test.src", "./") + "/" + pathToStores +163"/" + keyStoreFile;164165System.setProperty("javax.net.ssl.keyStore", keyFilename);166System.setProperty("javax.net.ssl.keyStorePassword", passwd);167168if (debug)169System.setProperty("javax.net.debug", "all");170171/*172* Start the tests.173*/174new SSLSocketImplThrowsWrongExceptions();175}176177Thread clientThread = null;178Thread serverThread = null;179180/*181* Primary constructor, used to drive remainder of the test.182*183* Fork off the other side, then do your work.184*/185SSLSocketImplThrowsWrongExceptions () throws Exception {186Exception startException = null;187try {188if (separateServerThread) {189startServer(true);190startClient(false);191} else {192startClient(true);193startServer(false);194}195} catch (Exception e) {196startException = e;197}198199/*200* Wait for other side to close down.201*/202if (separateServerThread) {203if (serverThread != null) {204serverThread.join();205}206} else {207if (clientThread != null) {208clientThread.join();209}210}211212/*213* When we get here, the test is pretty much over.214* Which side threw the error?215*/216Exception local;217Exception remote;218219if (separateServerThread) {220remote = serverException;221local = clientException;222} else {223remote = clientException;224local = serverException;225}226227Exception exception = null;228229/*230* Check various exception conditions.231*/232if ((local != null) && (remote != null)) {233// If both failed, return the curthread's exception.234local.initCause(remote);235exception = local;236} else if (local != null) {237exception = local;238} else if (remote != null) {239exception = remote;240} else if (startException != null) {241exception = startException;242}243244/*245* If there was an exception *AND* a startException,246* output it.247*/248if (exception != null) {249if (exception != startException && startException != null) {250exception.addSuppressed(startException);251}252throw exception;253}254255// Fall-through: no exception to throw!256}257258void startServer(boolean newThread) throws Exception {259if (newThread) {260serverThread = new Thread() {261public void run() {262try {263doServerSide();264} catch (Exception e) {265/*266* Our server thread just died.267*268* Release the client, if not active already...269*/270System.err.println("Server died...");271serverReadyLatch.countDown();272serverException = e;273}274}275};276serverThread.start();277} else {278try {279doServerSide();280} catch (Exception e) {281serverException = e;282} finally {283serverReadyLatch.countDown();284}285}286}287288void startClient(boolean newThread) throws Exception {289if (newThread) {290clientThread = new Thread() {291public void run() {292try {293doClientSide();294} catch (Exception e) {295/*296* Our client thread just died.297*/298System.err.println("Client died...");299clientException = e;300}301}302};303clientThread.start();304} else {305try {306doClientSide();307} catch (Exception e) {308clientException = e;309}310}311}312}313314315