Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/SSLSocketClose.java
41152 views
/*1* Copyright (c) 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// 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 820933331* @summary Socket reset issue for TLS 1.3 socket close32* @library /javax/net/ssl/templates33* @run main/othervm SSLSocketClose34*/3536import javax.net.ssl.*;37import java.io.*;38import java.net.InetAddress;3940public class SSLSocketClose implements SSLContextTemplate {4142public static void main(String[] args) throws Exception {43for (int i = 0; i<= 10; i++) {44System.err.println("===================================");45System.err.println("loop " + i);46System.err.println("===================================");47new SSLSocketClose().test();48}49}5051private void test() throws Exception {52SSLServerSocket listenSocket = null;53SSLSocket serverSocket = null;54ClientSocket clientSocket = null;55try {56SSLServerSocketFactory serversocketfactory =57createServerSSLContext().getServerSocketFactory();58listenSocket =59(SSLServerSocket)serversocketfactory.createServerSocket(0);60listenSocket.setNeedClientAuth(false);61listenSocket.setEnableSessionCreation(true);62listenSocket.setUseClientMode(false);636465System.err.println("Starting client");66clientSocket = new ClientSocket(listenSocket.getLocalPort());67clientSocket.start();6869System.err.println("Accepting client requests");70serverSocket = (SSLSocket) listenSocket.accept();7172System.err.println("Reading data from client");73BufferedReader serverReader = new BufferedReader(74new InputStreamReader(serverSocket.getInputStream()));75String data = serverReader.readLine();76System.err.println("Received data from client: " + data);7778System.err.println("Sending data to client ...");79String serverData = "Hi, I am server";80BufferedWriter os = new BufferedWriter(81new OutputStreamWriter(serverSocket.getOutputStream()));82os.write(serverData, 0, serverData.length());83os.newLine();84os.flush();8586System.err.println("Reading more data from client");87data = serverReader.readLine();88System.err.println("Received data from client: " + data);89} finally {90if (listenSocket != null) {91listenSocket.close();92}9394if (serverSocket != null) {95serverSocket.close();96}97}9899if (clientSocket != null && clientSocket.clientException != null) {100throw clientSocket.clientException;101}102}103104private class ClientSocket extends Thread{105int serverPort = 0;106Exception clientException;107108public ClientSocket(int serverPort) {109this.serverPort = serverPort;110}111112@Override113public void run() {114SSLSocket clientSocket = null;115String clientData = "Hi, I am client";116try {117System.err.println(118"Connecting to server at port " + serverPort);119SSLSocketFactory sslSocketFactory =120createClientSSLContext().getSocketFactory();121clientSocket = (SSLSocket)sslSocketFactory.createSocket(122InetAddress.getLocalHost(), serverPort);123clientSocket.setSoLinger(true, 3);124125System.err.println("Sending data to server ...");126127BufferedWriter os = new BufferedWriter(128new OutputStreamWriter(clientSocket.getOutputStream()));129os.write(clientData, 0, clientData.length());130os.newLine();131os.flush();132133System.err.println("Reading data from server");134BufferedReader is = new BufferedReader(135new InputStreamReader(clientSocket.getInputStream()));136String data = is.readLine();137System.err.println("Received Data from server: " + data);138139System.err.println("Sending more data to server ...");140os.write(clientData, 0, clientData.length());141os.newLine();142os.flush();143} catch (Exception e) {144clientException = e;145} finally {146if (clientSocket != null) {147try{148clientSocket.close();149System.err.println("client socket closed");150} catch (IOException ioe) {151clientException = ioe;152}153}154}155}156}157}158159160161