Path: blob/master/test/jdk/sun/security/ssl/SSLSocketImpl/SSLSocketBruceForceClose.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 SSLSocketBruceForceClose34*/3536import javax.net.ssl.*;37import java.io.*;38import java.net.InetAddress;3940public class SSLSocketBruceForceClose 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 SSLSocketBruceForceClose().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("Reading more data from client");79data = serverReader.readLine();80System.err.println("Received data from client: " + data);81} finally {82if (listenSocket != null) {83listenSocket.close();84}8586if (serverSocket != null) {87serverSocket.close();88}89}9091if (clientSocket != null && clientSocket.clientException != null) {92throw clientSocket.clientException;93}94}9596private class ClientSocket extends Thread{97int serverPort = 0;98Exception clientException;99100public ClientSocket(int serverPort) {101this.serverPort = serverPort;102}103104@Override105public void run() {106SSLSocket clientSocket = null;107String clientData = "Hi, I am client";108try {109System.err.println(110"Connecting to server at port " + serverPort);111SSLSocketFactory sslSocketFactory =112createClientSSLContext().getSocketFactory();113clientSocket = (SSLSocket)sslSocketFactory.createSocket(114InetAddress.getLocalHost(), serverPort);115clientSocket.setSoLinger(true, 3);116clientSocket.setSoTimeout(1000);117118119System.err.println("Sending data to server ...");120121BufferedWriter os = new BufferedWriter(122new OutputStreamWriter(clientSocket.getOutputStream()));123os.write(clientData, 0, clientData.length());124os.newLine();125os.flush();126127System.err.println("Sending more data to server ...");128os.write(clientData, 0, clientData.length());129os.newLine();130os.flush();131} catch (Exception e) {132clientException = e;133} finally {134if (clientSocket != null) {135try{136clientSocket.close();137System.err.println("client socket closed");138} catch (IOException ioe) {139clientException = ioe;140}141}142}143}144}145}146147148149