Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/BlockedAsyncClose.java
41152 views
/*1* Copyright (c) 2019, 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 822482931* @summary AsyncSSLSocketClose.java has timing issue32* @run main/othervm BlockedAsyncClose33*/3435import javax.net.ssl.*;36import java.io.*;37import java.net.SocketException;38import java.net.InetAddress;39import java.net.InetSocketAddress;40import java.util.concurrent.CountDownLatch;41import java.util.concurrent.TimeUnit;4243public class BlockedAsyncClose implements Runnable {44SSLSocket socket;45SSLServerSocket ss;4647// Is the socket ready to close?48private final CountDownLatch closeCondition = new CountDownLatch(1);4950// Where do we find the keystores?51static String pathToStores = "../../../../javax/net/ssl/etc";52static String keyStoreFile = "keystore";53static String trustStoreFile = "truststore";54static String passwd = "passphrase";5556public static void main(String[] args) throws Exception {57String keyFilename =58System.getProperty("test.src", "./") + "/" + pathToStores +59"/" + keyStoreFile;60String trustFilename =61System.getProperty("test.src", "./") + "/" + pathToStores +62"/" + trustStoreFile;6364System.setProperty("javax.net.ssl.keyStore", keyFilename);65System.setProperty("javax.net.ssl.keyStorePassword", passwd);66System.setProperty("javax.net.ssl.trustStore", trustFilename);67System.setProperty("javax.net.ssl.trustStorePassword", passwd);6869new BlockedAsyncClose();70}7172public BlockedAsyncClose() throws Exception {73SSLServerSocketFactory sslssf =74(SSLServerSocketFactory)SSLServerSocketFactory.getDefault();75InetAddress loopback = InetAddress.getLoopbackAddress();76ss = (SSLServerSocket)sslssf.createServerSocket();77ss.bind(new InetSocketAddress(loopback, 0));7879SSLSocketFactory sslsf =80(SSLSocketFactory)SSLSocketFactory.getDefault();81socket = (SSLSocket)sslsf.createSocket(loopback, ss.getLocalPort());82SSLSocket serverSoc = (SSLSocket)ss.accept();83ss.close();8485(new Thread(this)).start();86serverSoc.startHandshake();8788boolean closeIsReady = closeCondition.await(90L, TimeUnit.SECONDS);89if (!closeIsReady) {90System.out.println(91"Ignore, the closure is not ready yet in 90 seconds.");92return;93}9495socket.setSoLinger(true, 10);96System.out.println("Calling Socket.close");9798// Sleep for a while so that the write thread blocks by hitting the99// output stream buffer limit.100Thread.sleep(1000);101102socket.close();103System.out.println("ssl socket get closed");104System.out.flush();105}106107// block in write108public void run() {109byte[] ba = new byte[1024];110for (int i = 0; i < ba.length; i++) {111ba[i] = 0x7A;112}113114try {115OutputStream os = socket.getOutputStream();116int count = 0;117118// 1st round write119count += ba.length;120System.out.println(count + " bytes to be written");121os.write(ba);122System.out.println(count + " bytes written");123124// Signal, ready to close.125closeCondition.countDown();126127// write more128while (true) {129count += ba.length;130System.out.println(count + " bytes to be written");131os.write(ba);132System.out.println(count + " bytes written");133}134} catch (SocketException se) {135// the closing may be in progress136System.out.println("interrupted? " + se);137} catch (Exception e) {138if (socket.isClosed() || socket.isOutputShutdown()) {139System.out.println("interrupted, the socket is closed");140} else {141throw new RuntimeException("interrupted?", e);142}143}144}145}146147148149