Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/ssl/SSLSocketImpl/SSLSocketImplThrowsWrongExceptions.java
38853 views
/*1* Copyright (c) 2001, 2013, 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.io.*;38import java.net.*;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*/66volatile static boolean serverReady = false;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*/89serverReady = true;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 e) {98/*99* Caught the right Exeption. Swallow it.100*/101System.out.println("Server reported the right exception");102System.out.println(e.toString());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*/123while (!serverReady) {124Thread.sleep(50);125}126127SSLSocketFactory sslsf =128(SSLSocketFactory) SSLSocketFactory.getDefault();129try {130System.out.println(" Client creating socket");131SSLSocket sslSocket = (SSLSocket)132sslsf.createSocket("localhost", serverPort);133System.out.println(" Client starting handshake");134sslSocket.startHandshake();135throw new Exception("Handshake was successful");136} catch (SSLException e) {137/*138* Caught the right Exception. Swallow it.139*/140System.out.println(" Client reported correct exception");141System.out.println(" " + e.toString());142} catch (Exception e) {143/*144* Caught the wrong exception. Rethrow it.145*/146System.out.println(" Client reported the wrong exception");147throw e;148}149}150151/*152* =============================================================153* The remainder is just support stuff154*/155156// use any free port by default157volatile int serverPort = 0;158159volatile Exception serverException = null;160volatile Exception clientException = null;161162public static void main(String[] args) throws Exception {163String keyFilename =164System.getProperty("test.src", "./") + "/" + pathToStores +165"/" + keyStoreFile;166167System.setProperty("javax.net.ssl.keyStore", keyFilename);168System.setProperty("javax.net.ssl.keyStorePassword", passwd);169170if (debug)171System.setProperty("javax.net.debug", "all");172173/*174* Start the tests.175*/176new SSLSocketImplThrowsWrongExceptions();177}178179Thread clientThread = null;180Thread serverThread = null;181182/*183* Primary constructor, used to drive remainder of the test.184*185* Fork off the other side, then do your work.186*/187SSLSocketImplThrowsWrongExceptions () throws Exception {188Exception startException = null;189try {190if (separateServerThread) {191startServer(true);192startClient(false);193} else {194startClient(true);195startServer(false);196}197} catch (Exception e) {198startException = e;199}200201/*202* Wait for other side to close down.203*/204if (separateServerThread) {205if (serverThread != null) {206serverThread.join();207}208} else {209if (clientThread != null) {210clientThread.join();211}212}213214/*215* When we get here, the test is pretty much over.216* Which side threw the error?217*/218Exception local;219Exception remote;220221if (separateServerThread) {222remote = serverException;223local = clientException;224} else {225remote = clientException;226local = serverException;227}228229Exception exception = null;230231/*232* Check various exception conditions.233*/234if ((local != null) && (remote != null)) {235// If both failed, return the curthread's exception.236local.initCause(remote);237exception = local;238} else if (local != null) {239exception = local;240} else if (remote != null) {241exception = remote;242} else if (startException != null) {243exception = startException;244}245246/*247* If there was an exception *AND* a startException,248* output it.249*/250if (exception != null) {251if (exception != startException && startException != null) {252exception.addSuppressed(startException);253}254throw exception;255}256257// Fall-through: no exception to throw!258}259260void startServer(boolean newThread) throws Exception {261if (newThread) {262serverThread = new Thread() {263public void run() {264try {265doServerSide();266} catch (Exception e) {267/*268* Our server thread just died.269*270* Release the client, if not active already...271*/272System.err.println("Server died...");273serverReady = true;274serverException = e;275}276}277};278serverThread.start();279} else {280try {281doServerSide();282} catch (Exception e) {283serverException = e;284} finally {285serverReady = true;286}287}288}289290void startClient(boolean newThread) throws Exception {291if (newThread) {292clientThread = new Thread() {293public void run() {294try {295doClientSide();296} catch (Exception e) {297/*298* Our client thread just died.299*/300System.err.println("Client died...");301clientException = e;302}303}304};305clientThread.start();306} else {307try {308doClientSide();309} catch (Exception e) {310clientException = e;311}312}313}314}315316317