Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/SSLSocketReset.java
66645 views
/*1* Copyright (c) 2021, Azul, Inc. 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// Please run in othervm mode. SunJSSE does not support dynamic system25// properties, no way to re-use system properties in samevm/agentvm mode.26//2728/*29* @test30* @bug 826896531* @summary Socket reset issue for TLS socket close32* @run main/othervm -Djdk.net.usePlainSocketImpl=true SSLSocketReset33*/3435import javax.net.ssl.*;36import java.io.*;37import java.net.*;3839public class SSLSocketReset {4041public static void main(String[] args){42ServerThread serverThread = null;43Exception clientException = null;44try {45SSLServerSocketFactory sslserversocketfactory =46SSLContext.getDefault().getServerSocketFactory();47SSLServerSocket sslServerSocket =48(SSLServerSocket) sslserversocketfactory.createServerSocket(0);49serverThread = new ServerThread(sslServerSocket);50serverThread.start();51try {52Socket socket = new Socket(sslServerSocket.getInetAddress(), sslServerSocket.getLocalPort());53DataInputStream in = new DataInputStream(socket.getInputStream());54DataOutputStream out = new DataOutputStream(socket.getOutputStream());5556String msg = "Hello";57out.writeUTF(msg);58out.flush();59msg = in.readUTF();60} catch(Exception e) {61clientException = e;62e.printStackTrace();63}64serverThread.join();65} catch(Exception e) {66throw new RuntimeException("Fails to start SSL server");67}68if (serverThread.exception instanceof SSLException &&69serverThread.exception.getMessage().equals("Unsupported or unrecognized SSL message") &&70!(clientException instanceof SocketException &&71clientException.getMessage().equals("Connection reset"))) {72System.out.println("Test PASSED");73} else {74throw new RuntimeException("TCP connection reset");75}76}7778// Thread handling the server socket79private static class ServerThread extends Thread {80private SSLServerSocket sslServerSocket = null;81private SSLSocket sslSocket = null;82Exception exception;8384ServerThread(SSLServerSocket sslServerSocket){85this.sslServerSocket = sslServerSocket;86}8788public void run(){89try {90SSLSocket sslsocket = null;91while (true) {92sslsocket = (SSLSocket) sslServerSocket.accept();93DataInputStream in = new DataInputStream(sslsocket.getInputStream());94DataOutputStream out = new DataOutputStream(sslsocket.getOutputStream());95String string;96while ((string = in.readUTF()) != null) {97out.writeUTF(string);98out.flush();99}100}101} catch(Exception e) {102exception = e;103e.printStackTrace();104}105}106}107}108109110