Path: blob/master/test/jdk/java/net/Socket/ReadAfterReset.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/* @test24* @requires (os.family == "linux" | os.family == "mac")25* @bug 820393726* @summary Test reading bytes from a socket after the connection has been27* reset by the peer28*/2930import java.io.IOException;31import java.io.PrintStream;32import java.net.InetAddress;33import java.net.InetSocketAddress;34import java.net.ServerSocket;35import java.net.Socket;3637/**38* This test exercises platform specific and unspecified behavior. It exists39* only to ensure that the behavior doesn't change between JDK releases.40*/4142public class ReadAfterReset {43private static final PrintStream out = System.out;4445// number of bytes to write before the connection reset46private static final int NUM_BYTES_TO_WRITE = 1000;4748public static void main(String[] args) throws IOException {49try (ServerSocket ss = new ServerSocket()) {50ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));5152/**53* Connect to the server which will write some bytes and reset the54* connection. The client then attempts to read the bytes sent by55* the server before it closed the connection.56*/57out.println("Test connection ...");58try (Socket s = new Socket()) {59s.connect(ss.getLocalSocketAddress());60int nwrote = acceptAndResetConnection(ss);61int nread = readUntilIOException(s);62if (nread != nwrote) {63throw new RuntimeException("Client read " + nread + ", expected " + nwrote);64}65}6667/**68* Connect to the server which will write some bytes and reset the69* connection. The client then writes to its end of the connection,70* failing as the connection is reset. It then attempts to read the71* bytes sent by the server before it closed the connection.72*/73out.println();74out.println("Test connection ...");75try (Socket s = new Socket()) {76s.connect(ss.getLocalSocketAddress());77int nwrote = acceptAndResetConnection(ss);78writeUntilIOException(s);79int nread = readUntilIOException(s);80if (nread != nwrote) {81throw new RuntimeException("Client read " + nread + ", expected " + nwrote);82}83}84}85}8687/**88* Accept a connection, write bytes, and then reset the connection89*/90static int acceptAndResetConnection(ServerSocket ss) throws IOException {91int count = NUM_BYTES_TO_WRITE;92try (Socket peer = ss.accept()) {93peer.getOutputStream().write(new byte[count]);94peer.setSoLinger(true, 0);95out.format("Server wrote %d bytes and reset connection%n", count);96}97return count;98}99100/**101* Write bytes to a socket until I/O exception is thrown102*/103static void writeUntilIOException(Socket s) {104try {105byte[] bytes = new byte[100];106while (true) {107s.getOutputStream().write(bytes);108out.format("Client wrote %d bytes%n", bytes.length);109}110} catch (IOException ioe) {111out.format("Client write failed: %s (expected)%n", ioe);112}113}114115/**116* Read bytes from a socket until I/O exception is thrown.117*118* @return the number of bytes read before the I/O exception was thrown119*/120static int readUntilIOException(Socket s) {121int nread = 0;122try {123byte[] bytes = new byte[100];124while (true) {125int n = s.getInputStream().read(bytes);126if (n < 0) {127out.println("Client read EOF");128break;129} else {130out.format("Client read %s bytes%n", n);131nread += n;132}133}134} catch (IOException ioe) {135out.format("Client read failed: %s (expected)%n", ioe);136}137return nread;138}139}140141142