Path: blob/master/test/jdk/java/nio/channels/SocketChannel/ConnectionReset.java
41154 views
/*1* Copyright (c) 2019, 2020, 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* @test25* @run testng ConnectionReset26* @summary Test behavior of SocketChannel.read and the Socket adaptor read27* and available methods when a connection is reset28*/2930import java.io.InputStream;31import java.io.IOException;32import java.net.InetAddress;33import java.net.InetSocketAddress;34import java.net.ServerSocket;35import java.net.Socket;36import java.nio.ByteBuffer;37import java.nio.channels.SocketChannel;38import java.lang.reflect.Method;3940import org.testng.annotations.Test;41import static org.testng.Assert.*;4243@Test44public class ConnectionReset {4546static final int REPEAT_COUNT = 5;4748/**49* Tests SocketChannel.read when the connection is reset and there are no50* bytes to read.51*/52public void testSocketChannelReadNoData() throws IOException {53System.out.println("testSocketChannelReadNoData");54withResetConnection(null, sc -> {55ByteBuffer bb = ByteBuffer.allocate(100);56for (int i=0; i<REPEAT_COUNT; i++) {57try {58sc.read(bb);59assertTrue(false);60} catch (IOException ioe) {61System.out.format("read => %s (expected)%n", ioe);62}63}64});65}6667/**68* Tests SocketChannel.read when the connection is reset and there are bytes69* to read.70*/71public void testSocketChannelReadData() throws IOException {72System.out.println("testSocketChannelReadData");73byte[] data = { 1, 2, 3 };74withResetConnection(data, sc -> {75int remaining = data.length;76ByteBuffer bb = ByteBuffer.allocate(remaining + 100);77for (int i=0; i<REPEAT_COUNT; i++) {78try {79int bytesRead = sc.read(bb);80if (bytesRead == -1) {81System.out.println("read => EOF");82} else {83System.out.println("read => " + bytesRead + " byte(s)");84}85assertTrue(bytesRead > 0);86remaining -= bytesRead;87assertTrue(remaining >= 0);88} catch (IOException ioe) {89System.out.format("read => %s%n", ioe);90remaining = 0;91}92}93});94}959697/**98* Tests available before Socket read when the connection is reset and there99* are no bytes to read.100*/101public void testAvailableBeforeSocketReadNoData() throws IOException {102System.out.println("testAvailableBeforeSocketReadNoData");103withResetConnection(null, sc -> {104Socket s = sc.socket();105InputStream in = s.getInputStream();106for (int i=0; i<REPEAT_COUNT; i++) {107int bytesAvailable = in.available();108System.out.format("available => %d%n", bytesAvailable);109assertTrue(bytesAvailable == 0);110try {111int bytesRead = in.read();112if (bytesRead == -1) {113System.out.println("read => EOF");114} else {115System.out.println("read => 1 byte");116}117assertTrue(false);118} catch (IOException ioe) {119System.out.format("read => %s (expected)%n", ioe);120}121}122});123}124125/**126* Tests available before Socket read when the connection is reset and there127* are bytes to read.128*/129public void testAvailableBeforeSocketReadData() throws IOException {130System.out.println("testAvailableBeforeSocketReadData");131byte[] data = { 1, 2, 3 };132withResetConnection(data, sc -> {133Socket s = sc.socket();134InputStream in = s.getInputStream();135int remaining = data.length;136for (int i=0; i<REPEAT_COUNT; i++) {137int bytesAvailable = in.available();138System.out.format("available => %d%n", bytesAvailable);139assertTrue(bytesAvailable <= remaining);140try {141int bytesRead = in.read();142if (bytesRead == -1) {143System.out.println("read => EOF");144assertTrue(false);145} else {146System.out.println("read => 1 byte");147assertTrue(remaining > 0);148remaining--;149}150} catch (IOException ioe) {151System.out.format("read => %s%n", ioe);152remaining = 0;153}154}155});156}157158/**159* Tests Socket read before available when the connection is reset and there160* are no bytes to read.161*/162public void testSocketReadNoDataBeforeAvailable() throws IOException {163System.out.println("testSocketReadNoDataBeforeAvailable");164withResetConnection(null, sc -> {165Socket s = sc.socket();166InputStream in = s.getInputStream();167for (int i=0; i<REPEAT_COUNT; i++) {168try {169int bytesRead = in.read();170if (bytesRead == -1) {171System.out.println("read => EOF");172} else {173System.out.println("read => 1 byte");174}175assertTrue(false);176} catch (IOException ioe) {177System.out.format("read => %s (expected)%n", ioe);178}179int bytesAvailable = in.available();180System.out.format("available => %d%n", bytesAvailable);181assertTrue(bytesAvailable == 0);182}183});184}185186/**187* Tests Socket read before available when the connection is reset and there188* are bytes to read.189*/190public void testSocketReadDataBeforeAvailable() throws IOException {191System.out.println("testSocketReadDataBeforeAvailable");192byte[] data = { 1, 2, 3 };193withResetConnection(data, sc -> {194Socket s = sc.socket();195InputStream in = s.getInputStream();196int remaining = data.length;197for (int i=0; i<REPEAT_COUNT; i++) {198try {199int bytesRead = in.read();200if (bytesRead == -1) {201System.out.println("read => EOF");202assertTrue(false);203} else {204System.out.println("read => 1 byte");205assertTrue(remaining > 0);206remaining--;207}208} catch (IOException ioe) {209System.out.format("read => %s%n", ioe);210remaining = 0;211}212int bytesAvailable = in.available();213System.out.format("available => %d%n", bytesAvailable);214assertTrue(bytesAvailable <= remaining);215}216});217}218219interface ThrowingConsumer<T> {220void accept(T t) throws IOException;221}222223/**224* Invokes a consumer with a SocketChannel connected to a peer that has closed225* the connection with a "connection reset". The peer sends the given data226* bytes before closing (when data is not null).227*/228static void withResetConnection(byte[] data, ThrowingConsumer<SocketChannel> consumer)229throws IOException230{231var loopback = InetAddress.getLoopbackAddress();232try (var listener = new ServerSocket()) {233listener.bind(new InetSocketAddress(loopback, 0));234try (var sc = SocketChannel.open()) {235sc.connect(listener.getLocalSocketAddress());236try (Socket peer = listener.accept()) {237if (data != null) {238peer.getOutputStream().write(data);239}240peer.setSoLinger(true, 0);241}242consumer.accept(sc);243}244}245}246}247248249