|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8274524 |
| 27 | + * @summary 8274524: SSLSocket.close() hangs if it is called during the ssl handshake |
| 28 | + * @library /javax/net/ssl/templates |
| 29 | + * @run main/othervm ClientSocketCloseHang TLSv1.2 |
| 30 | + * @run main/othervm ClientSocketCloseHang TLSv1.3 |
| 31 | + */ |
| 32 | + |
| 33 | + |
| 34 | +import javax.net.ssl.*; |
| 35 | +import java.net.InetAddress; |
| 36 | + |
| 37 | +public class ClientSocketCloseHang implements SSLContextTemplate { |
| 38 | + |
| 39 | + public static void main(String[] args) throws Exception { |
| 40 | + System.setProperty("jdk.tls.client.protocols", args[0]); |
| 41 | + for (int i = 0; i<= 20; i++) { |
| 42 | + System.err.println("==================================="); |
| 43 | + System.err.println("loop " + i); |
| 44 | + System.err.println("==================================="); |
| 45 | + new ClientSocketCloseHang().test(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private void test() throws Exception { |
| 50 | + SSLServerSocket listenSocket = null; |
| 51 | + SSLSocket serverSocket = null; |
| 52 | + ClientSocket clientSocket = null; |
| 53 | + try { |
| 54 | + SSLServerSocketFactory serversocketfactory = |
| 55 | + createServerSSLContext().getServerSocketFactory(); |
| 56 | + listenSocket = |
| 57 | + (SSLServerSocket)serversocketfactory.createServerSocket(0); |
| 58 | + listenSocket.setNeedClientAuth(false); |
| 59 | + listenSocket.setEnableSessionCreation(true); |
| 60 | + listenSocket.setUseClientMode(false); |
| 61 | + |
| 62 | + |
| 63 | + System.err.println("Starting client"); |
| 64 | + clientSocket = new ClientSocket(listenSocket.getLocalPort()); |
| 65 | + clientSocket.start(); |
| 66 | + |
| 67 | + System.err.println("Accepting client requests"); |
| 68 | + serverSocket = (SSLSocket) listenSocket.accept(); |
| 69 | + |
| 70 | + serverSocket.startHandshake(); |
| 71 | + } finally { |
| 72 | + if (clientSocket != null) { |
| 73 | + clientSocket.close(); |
| 74 | + } |
| 75 | + if (listenSocket != null) { |
| 76 | + listenSocket.close(); |
| 77 | + } |
| 78 | + |
| 79 | + if (serverSocket != null) { |
| 80 | + serverSocket.close(); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private class ClientSocket extends Thread{ |
| 86 | + int serverPort = 0; |
| 87 | + SSLSocket clientSocket = null; |
| 88 | + |
| 89 | + public ClientSocket(int serverPort) { |
| 90 | + this.serverPort = serverPort; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void run() { |
| 95 | + try { |
| 96 | + System.err.println( |
| 97 | + "Connecting to server at port " + serverPort); |
| 98 | + SSLSocketFactory sslSocketFactory = |
| 99 | + createClientSSLContext().getSocketFactory(); |
| 100 | + clientSocket = (SSLSocket)sslSocketFactory.createSocket( |
| 101 | + InetAddress.getLocalHost(), serverPort); |
| 102 | + clientSocket.setSoLinger(true, 3); |
| 103 | + clientSocket.startHandshake(); |
| 104 | + } catch (Exception e) { |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public void close() { |
| 109 | + Thread t = new Thread() { |
| 110 | + @Override |
| 111 | + public void run() { |
| 112 | + try { |
| 113 | + if (clientSocket != null) { |
| 114 | + clientSocket.close(); |
| 115 | + } |
| 116 | + } catch (Exception ex) { |
| 117 | + } |
| 118 | + } |
| 119 | + }; |
| 120 | + try { |
| 121 | + // Close client connection |
| 122 | + t.start(); |
| 123 | + t.join(2000); // 2 sec |
| 124 | + } catch (InterruptedException ex) { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + if (t.isAlive()) { |
| 129 | + throw new RuntimeException("SSL Client hangs on close"); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
0 commit comments