Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/NonAutoClose.java
41152 views
/*1* Copyright (c) 2001, 2018, 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 440439931* @ignore this test does not work any more as the TLS spec changes the32* behaviors of close_notify.33* @summary When a layered SSL socket is closed, it should wait for close_notify34* @run main/othervm NonAutoClose35* @author Brad Wetmore36*/3738import java.io.*;39import java.net.ServerSocket;40import java.net.Socket;41import javax.net.ssl.*;42import java.security.cert.X509Certificate;43import java.security.cert.CertificateException;444546public class NonAutoClose {47/*48* =============================================================49* Set the various variables needed for the tests, then50* specify what tests to run on each side.51*/5253/*54* Should we run the client or server in a separate thread?55* Both sides can throw exceptions, but do you have a preference56* as to which side should be the main thread.57*/58private static boolean separateServerThread = true;5960/*61* Where do we find the keystores?62*/63private final static String pathToStores = "../../../../javax/net/ssl/etc";64private final static String keyStoreFile = "keystore";65private final static String trustStoreFile = "truststore";66private final static String passwd = "passphrase";67private final static char[] cpasswd = "passphrase".toCharArray();6869/*70* Is the server ready to serve?71*/72volatile static boolean serverReady = false;7374/*75* Turn on SSL debugging?76*/77private final static boolean DEBUG = false;78private final static boolean VERBOSE = true;79private final static int NUM_ITERATIONS = 10;80private final static int PLAIN_SERVER_VAL = 1;81private final static int PLAIN_CLIENT_VAL = 2;82private final static int TLS_SERVER_VAL = 3;83private final static int TLS_CLIENT_VAL = 4;8485/*86* If the client or server is doing some kind of object creation87* that the other side depends on, and that thread prematurely88* exits, you may experience a hang. The test harness will89* terminate all hung threads after its timeout has expired,90* currently 3 minutes by default, but you might try to be91* smart about it....92*/9394void expectValue(int got, int expected, String msg) throws IOException {95if (VERBOSE) {96System.err.println(msg + ": read (" + got + ")");97}98if (got != expected) {99throw new IOException(msg + ": read (" + got100+ ") but expecting(" + expected + ")");101}102}103104105/*106* Define the server side of the test.107*108* If the server prematurely exits, serverReady will be set to true109* to avoid infinite hangs.110*/111112void doServerSide() throws Exception {113if (VERBOSE) {114System.err.println("Starting server");115}116117/*118* Setup the SSL stuff119*/120SSLSocketFactory sslsf =121(SSLSocketFactory) SSLSocketFactory.getDefault();122123ServerSocket serverSocket = new ServerSocket(SERVER_PORT);124125SERVER_PORT = serverSocket.getLocalPort();126127/*128* Signal Client, we're ready for his connect.129*/130serverReady = true;131132Socket plainSocket = serverSocket.accept();133InputStream is = plainSocket.getInputStream();134OutputStream os = plainSocket.getOutputStream();135136expectValue(is.read(), PLAIN_CLIENT_VAL, "Server");137138os.write(PLAIN_SERVER_VAL);139os.flush();140141for (int i = 1; i <= NUM_ITERATIONS; i++) {142if (VERBOSE) {143System.err.println("=================================");144System.err.println("Server Iteration #" + i);145}146147SSLSocket ssls = (SSLSocket) sslsf.createSocket(plainSocket,148SERVER_NAME, plainSocket.getPort(), false);149150ssls.setUseClientMode(false);151InputStream sslis = ssls.getInputStream();152OutputStream sslos = ssls.getOutputStream();153154expectValue(sslis.read(), TLS_CLIENT_VAL, "Server");155156sslos.write(TLS_SERVER_VAL);157sslos.flush();158159sslis.close();160sslos.close();161ssls.close();162163if (VERBOSE) {164System.err.println("TLS socket is closed");165}166}167168expectValue(is.read(), PLAIN_CLIENT_VAL, "Server");169170os.write(PLAIN_SERVER_VAL);171os.flush();172173is.close();174os.close();175plainSocket.close();176177if (VERBOSE) {178System.err.println("Server plain socket is closed");179}180}181182/*183* Define the client side of the test.184*185* If the server prematurely exits, serverReady will be set to true186* to avoid infinite hangs.187*/188private void doClientSide() throws Exception {189/*190* Wait for server to get started.191*/192while (!serverReady) {193Thread.sleep(50);194}195196if (VERBOSE) {197System.err.println("Starting client");198}199200/*201* Setup the SSL stuff202*/203SSLSocketFactory sslsf =204(SSLSocketFactory) SSLSocketFactory.getDefault();205206Socket plainSocket = new Socket(SERVER_NAME, SERVER_PORT);207InputStream is = plainSocket.getInputStream();208OutputStream os = plainSocket.getOutputStream();209210os.write(PLAIN_CLIENT_VAL);211os.flush();212213expectValue(is.read(), PLAIN_SERVER_VAL, "Client");214215for (int i = 1; i <= NUM_ITERATIONS; i++) {216if (VERBOSE) {217System.err.println("===================================");218System.err.println("Client Iteration #" + i);219}220221SSLSocket ssls = (SSLSocket) sslsf.createSocket(plainSocket,222SERVER_NAME, plainSocket.getPort(), false);223224ssls.setUseClientMode(true);225226InputStream sslis = ssls.getInputStream();227OutputStream sslos = ssls.getOutputStream();228229sslos.write(TLS_CLIENT_VAL);230sslos.flush();231232expectValue(sslis.read(), TLS_SERVER_VAL, "Client");233234sslis.close();235sslos.close();236ssls.close();237238if (VERBOSE) {239System.err.println("Client TLS socket is closed");240}241}242243os.write(PLAIN_CLIENT_VAL);244os.flush();245246expectValue(is.read(), PLAIN_SERVER_VAL, "Client");247248is.close();249os.close();250plainSocket.close();251252if (VERBOSE) {253System.err.println("Client plain socket is closed");254}255}256257/*258* =============================================================259* The remainder is just support stuff260*/261262private volatile int SERVER_PORT = 0;263private final static String SERVER_NAME = "localhost";264265private volatile Exception serverException = null;266private volatile Exception clientException = null;267268private final static String keyFilename =269System.getProperty("test.src", ".") + "/" + pathToStores +270"/" + keyStoreFile;271private final static String trustFilename =272System.getProperty("test.src", ".") + "/" + pathToStores +273"/" + trustStoreFile;274275276// Used for running test standalone277public static void main(String[] args) throws Exception {278System.setProperty("javax.net.ssl.keyStore", keyFilename);279System.setProperty("javax.net.ssl.keyStorePassword", passwd);280System.setProperty("javax.net.ssl.trustStore", trustFilename);281System.setProperty("javax.net.ssl.trustStorePassword", passwd);282283if (DEBUG)284System.setProperty("javax.net.debug", "all");285286/*287* Start the tests.288*/289new NonAutoClose();290}291292private Thread clientThread = null;293private Thread serverThread = null;294295/*296* Primary constructor, used to drive remainder of the test.297*298* Fork off the other side, then do your work.299*/300NonAutoClose() throws Exception {301if (separateServerThread) {302startServer(true);303startClient(false);304} else {305startClient(true);306startServer(false);307}308309/*310* Wait for other side to close down.311*/312if (separateServerThread) {313serverThread.join();314} else {315clientThread.join();316}317318/*319* When we get here, the test is pretty much over.320*321* If the main thread excepted, that propagates back322* immediately. If the other thread threw an exception, we323* should report back.324*/325if (serverException != null) {326System.err.print("Server Exception:");327throw serverException;328}329if (clientException != null) {330System.err.print("Client Exception:");331throw clientException;332}333}334335private void startServer(boolean newThread) throws Exception {336if (newThread) {337serverThread = new Thread() {338public void run() {339try {340doServerSide();341} catch (Exception e) {342/*343* Our server thread just died.344*345* Release the client, if not active already...346*/347System.err.println("Server died...");348serverReady = true;349serverException = e;350}351}352};353serverThread.start();354} else {355doServerSide();356}357}358359private void startClient(boolean newThread) throws Exception {360if (newThread) {361clientThread = new Thread() {362public void run() {363try {364doClientSide();365} catch (Exception e) {366/*367* Our client thread just died.368*/369System.err.println("Client died...");370clientException = e;371}372}373};374clientThread.start();375} else {376doClientSide();377}378}379}380381382