Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/AsyncSSLSocketClose.java
41152 views
/*1* Copyright (c) 2007, 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 644741231* @summary Issue with socket.close() for ssl sockets when poweroff on32* other system33* @run main/othervm AsyncSSLSocketClose34*/3536import javax.net.ssl.*;37import java.io.*;38import java.net.SocketException;39import java.util.concurrent.CountDownLatch;40import java.util.concurrent.TimeUnit;4142public class AsyncSSLSocketClose implements Runnable {43SSLSocket socket;44SSLServerSocket ss;4546// Is the socket ready to close?47private final CountDownLatch closeCondition = new CountDownLatch(1);4849// Where do we find the keystores?50static String pathToStores = "../../../../javax/net/ssl/etc";51static String keyStoreFile = "keystore";52static String trustStoreFile = "truststore";53static String passwd = "passphrase";5455public static void main(String[] args) throws Exception {56String keyFilename =57System.getProperty("test.src", "./") + "/" + pathToStores +58"/" + keyStoreFile;59String trustFilename =60System.getProperty("test.src", "./") + "/" + pathToStores +61"/" + trustStoreFile;6263System.setProperty("javax.net.ssl.keyStore", keyFilename);64System.setProperty("javax.net.ssl.keyStorePassword", passwd);65System.setProperty("javax.net.ssl.trustStore", trustFilename);66System.setProperty("javax.net.ssl.trustStorePassword", passwd);6768new AsyncSSLSocketClose();69}7071public AsyncSSLSocketClose() throws Exception {72SSLServerSocketFactory sslssf =73(SSLServerSocketFactory)SSLServerSocketFactory.getDefault();74ss = (SSLServerSocket) sslssf.createServerSocket(0);7576SSLSocketFactory sslsf =77(SSLSocketFactory)SSLSocketFactory.getDefault();78socket = (SSLSocket)sslsf.createSocket("localhost", ss.getLocalPort());79SSLSocket serverSoc = (SSLSocket)ss.accept();80ss.close();8182(new Thread(this)).start();83serverSoc.startHandshake();8485boolean closeIsReady = closeCondition.await(90L, TimeUnit.SECONDS);86if (!closeIsReady) {87System.out.println(88"Ignore, the closure is not ready yet in 90 seconds.");89return;90}9192socket.setSoLinger(true, 10);93System.out.println("Calling Socket.close");94socket.close();95System.out.println("ssl socket get closed");96System.out.flush();97}9899// block in write100public void run() {101byte[] ba = new byte[1024];102for (int i = 0; i < ba.length; i++) {103ba[i] = 0x7A;104}105106try {107OutputStream os = socket.getOutputStream();108int count = 0;109110// 1st round write111count += ba.length;112System.out.println(count + " bytes to be written");113os.write(ba);114System.out.println(count + " bytes written");115116// Signal, ready to close.117closeCondition.countDown();118119// write more120while (true) {121count += ba.length;122System.out.println(count + " bytes to be written");123os.write(ba);124System.out.println(count + " bytes written");125}126} catch (SocketException se) {127// the closing may be in progress128System.out.println("interrupted? " + se);129} catch (Exception e) {130if (socket.isClosed() || socket.isOutputShutdown()) {131System.out.println("interrupted, the socket is closed");132} else {133throw new RuntimeException("interrupted?", e);134}135}136}137}138139140141