Path: blob/master/test/jdk/sun/security/ssl/AppOutputStream/NoExceptionOnClose.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* @test 1.3 01/03/0830* @bug 437839731* @summary JSSE socket output stream doesn't throw after socket is closed32* @run main/othervm NoExceptionOnClose33* @author Jaya Hangal34*/3536import java.io.*;37import java.net.*;38import javax.net.ssl.*;3940public class NoExceptionOnClose {4142/*43* =============================================================44* Set the various variables needed for the tests, then45* specify what tests to run on each side.46*/4748/*49* Should we run the client or server in a separate thread?50* Both sides can throw exceptions, but do you have a preference51* as to which side should be the main thread.52*/53static boolean separateServerThread = true;5455/*56* Where do we find the keystores?57*/58static String pathToStores = "../../../../javax/net/ssl/etc";59static String keyStoreFile = "keystore";60static String trustStoreFile = "truststore";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;7273boolean useSSL = true;74/*75* If the client or server is doing some kind of object creation76* that the other side depends on, and that thread prematurely77* exits, you may experience a hang. The test harness will78* terminate all hung threads after its timeout has expired,79* currently 3 minutes by default, but you might try to be80* smart about it....81*/8283/*84* Define the server side of the test.85*86* If the server prematurely exits, serverReady will be set to true87* to avoid infinite hangs.88*/89void doServerSide() throws Exception {90ServerSocket serverSocket;91if (useSSL) {92SSLServerSocketFactory sslssf =93(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();94serverSocket = (SSLServerSocket) sslssf.95createServerSocket(serverPort);96} else {97serverSocket = new ServerSocket(serverPort);98}99serverPort = serverSocket.getLocalPort();100101/*102* Signal Client, we're ready for his connect.103*/104serverReady = true;105106Socket socket = serverSocket.accept();107InputStream sslIS = socket.getInputStream();108OutputStream sslOS = socket.getOutputStream();109110int read = sslIS.read();111System.out.println("Server read: " + read);112sslOS.write(85);113sslOS.flush();114socket.close();115socket.close();116}117118/*119* Define the client side of the test.120*121* If the server prematurely exits, serverReady will be set to true122* to avoid infinite hangs.123*/124void doClientSide() throws Exception {125126/*127* Wait for server to get started.128*/129while (!serverReady) {130Thread.sleep(50);131}132Socket socket;133if (useSSL) {134SSLSocketFactory sslsf =135(SSLSocketFactory) SSLSocketFactory.getDefault();136socket = (SSLSocket)137sslsf.createSocket("localhost", serverPort);138} else139socket = new Socket("localhost", serverPort);140141InputStream sslIS = socket.getInputStream();142OutputStream sslOS = socket.getOutputStream();143144sslOS.write(80);145sslOS.flush();146int read = sslIS.read();147System.out.println("client read: " + read);148socket.close();149/*150* The socket closed exception must be thrown here151*/152boolean isSocketClosedThrown = false;153try {154sslOS.write(22);155sslOS.flush();156} catch (SSLException | SocketException socketClosed) {157System.out.println("Received \"" + socketClosed.getMessage()158+ "\" exception as expected");159isSocketClosedThrown = true;160}161if (!isSocketClosedThrown) {162throw new Exception("No Exception thrown on write() after"163+ " close()");164}165}166167/*168* =============================================================169* The remainder is just support stuff170*/171172// use any free port by default173volatile int serverPort = 0;174175volatile Exception serverException = null;176volatile Exception clientException = null;177178public static void main(String[] args) throws Exception {179String keyFilename =180System.getProperty("test.src", "./") + "/" + pathToStores +181"/" + keyStoreFile;182String trustFilename =183System.getProperty("test.src", "./") + "/" + pathToStores +184"/" + trustStoreFile;185186System.setProperty("javax.net.ssl.keyStore", keyFilename);187System.setProperty("javax.net.ssl.keyStorePassword", passwd);188System.setProperty("javax.net.ssl.trustStore", trustFilename);189System.setProperty("javax.net.ssl.trustStorePassword", passwd);190191if (debug)192System.setProperty("javax.net.debug", "all");193194/*195* Start the tests.196*/197new NoExceptionOnClose();198}199200Thread clientThread = null;201Thread serverThread = null;202203/*204* Primary constructor, used to drive remainder of the test.205*206* Fork off the other side, then do your work.207*/208NoExceptionOnClose() throws Exception {209if (separateServerThread) {210startServer(true);211startClient(false);212} else {213startClient(true);214startServer(false);215}216217/*218* Wait for other side to close down.219*/220if (separateServerThread) {221serverThread.join();222} else {223clientThread.join();224}225226/*227* When we get here, the test is pretty much over.228*229* If the main thread excepted, that propagates back230* immediately. If the other thread threw an exception, we231* should report back.232*/233if (serverException != null)234throw serverException;235if (clientException != null)236throw clientException;237}238239void startServer(boolean newThread) throws Exception {240if (newThread) {241serverThread = new Thread() {242public void run() {243try {244doServerSide();245} catch (Exception e) {246/*247* Our server thread just died.248*249* Release the client, if not active already...250*/251System.err.println("Server died...");252serverReady = true;253serverException = e;254}255}256};257serverThread.start();258} else {259doServerSide();260}261}262263void startClient(boolean newThread) throws Exception {264if (newThread) {265clientThread = new Thread() {266public void run() {267try {268doClientSide();269} catch (Exception e) {270/*271* Our client thread just died.272*/273System.err.println("Client died...");274clientException = e;275}276}277};278clientThread.start();279} else {280doClientSide();281}282}283}284285286