Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/ClientSocketCloseHang.java
66645 views
/*1* Copyright (c) 2021, 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* @test25* @bug 827452426* @summary 8274524: SSLSocket.close() hangs if it is called during the ssl handshake27* @library /javax/net/ssl/templates28* @run main/othervm ClientSocketCloseHang TLSv1.229* @run main/othervm ClientSocketCloseHang TLSv1.330*/313233import javax.net.ssl.*;34import java.net.InetAddress;3536public class ClientSocketCloseHang implements SSLContextTemplate {3738public static void main(String[] args) throws Exception {39System.setProperty("jdk.tls.client.protocols", args[0]);40for (int i = 0; i<= 20; i++) {41System.err.println("===================================");42System.err.println("loop " + i);43System.err.println("===================================");44new ClientSocketCloseHang().test();45}46}4748private void test() throws Exception {49SSLServerSocket listenSocket = null;50SSLSocket serverSocket = null;51ClientSocket clientSocket = null;52try {53SSLServerSocketFactory serversocketfactory =54createServerSSLContext().getServerSocketFactory();55listenSocket =56(SSLServerSocket)serversocketfactory.createServerSocket(0);57listenSocket.setNeedClientAuth(false);58listenSocket.setEnableSessionCreation(true);59listenSocket.setUseClientMode(false);606162System.err.println("Starting client");63clientSocket = new ClientSocket(listenSocket.getLocalPort());64clientSocket.start();6566System.err.println("Accepting client requests");67serverSocket = (SSLSocket) listenSocket.accept();6869serverSocket.startHandshake();70} finally {71if (clientSocket != null) {72clientSocket.close();73}74if (listenSocket != null) {75listenSocket.close();76}7778if (serverSocket != null) {79serverSocket.close();80}81}82}8384private class ClientSocket extends Thread{85int serverPort = 0;86SSLSocket clientSocket = null;8788public ClientSocket(int serverPort) {89this.serverPort = serverPort;90}9192@Override93public void run() {94try {95System.err.println(96"Connecting to server at port " + serverPort);97SSLSocketFactory sslSocketFactory =98createClientSSLContext().getSocketFactory();99clientSocket = (SSLSocket)sslSocketFactory.createSocket(100InetAddress.getLocalHost(), serverPort);101clientSocket.setSoLinger(true, 3);102clientSocket.startHandshake();103} catch (Exception e) {104}105}106107public void close() {108Thread t = new Thread() {109@Override110public void run() {111try {112if (clientSocket != null) {113clientSocket.close();114}115} catch (Exception ex) {116}117}118};119try {120// Close client connection121t.start();122t.join(2000); // 2 sec123} catch (InterruptedException ex) {124return;125}126127if (t.isAlive()) {128throw new RuntimeException("SSL Client hangs on close");129}130}131}132}133134135136