Skip to content

Commit 8fc3d89

Browse files
committed
8221259: New tests for java.net.Socket to exercise long standing behavior
Backport-of: fd14375
1 parent 16ef602 commit 8fc3d89

File tree

4 files changed

+927
-0
lines changed

4 files changed

+927
-0
lines changed
+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @requires (os.family == "linux" | os.family == "mac")
27+
* @run testng AsyncShutdown
28+
* @summary Test shutdownInput/shutdownOutput with threads blocked in read/write
29+
*/
30+
31+
import java.io.IOException;
32+
import java.net.ServerSocket;
33+
import java.net.Socket;
34+
import java.net.SocketTimeoutException;
35+
import java.util.concurrent.Executors;
36+
import java.util.concurrent.ScheduledExecutorService;
37+
import java.util.concurrent.TimeUnit;
38+
39+
import org.testng.annotations.Test;
40+
import static org.testng.Assert.*;
41+
42+
@Test
43+
public class AsyncShutdown {
44+
45+
public void testShutdownInput1() throws IOException {
46+
withConnection((s1, s2) -> {
47+
scheduleShutdownInput(s1, 2000);
48+
int n = s1.getInputStream().read();
49+
assertTrue(n == -1);
50+
});
51+
}
52+
53+
public void testShutdownInput2() throws IOException {
54+
withConnection((s1, s2) -> {
55+
scheduleShutdownInput(s1, 2000);
56+
s1.setSoTimeout(30*1000);
57+
int n = s1.getInputStream().read();
58+
assertTrue(n == -1);
59+
});
60+
}
61+
62+
public void testShutdownOutput1() throws IOException {
63+
withConnection((s1, s2) -> {
64+
scheduleShutdownOutput(s1, 2000);
65+
byte[] data = new byte[128*1024];
66+
try {
67+
while (true) {
68+
s1.getOutputStream().write(data);
69+
}
70+
} catch (IOException expected) { }
71+
});
72+
}
73+
74+
public void testShutdownOutput2() throws IOException {
75+
withConnection((s1, s2) -> {
76+
s1.setSoTimeout(100);
77+
try {
78+
s1.getInputStream().read();
79+
assertTrue(false);
80+
} catch (SocketTimeoutException e) { }
81+
82+
scheduleShutdownOutput(s1, 2000);
83+
byte[] data = new byte[128*1024];
84+
try {
85+
while (true) {
86+
s1.getOutputStream().write(data);
87+
}
88+
} catch (IOException expected) { }
89+
});
90+
}
91+
92+
static void scheduleShutdownInput(Socket s, long delay) {
93+
schedule(() -> {
94+
try {
95+
s.shutdownInput();
96+
} catch (IOException ioe) { }
97+
}, delay);
98+
}
99+
100+
static void scheduleShutdownOutput(Socket s, long delay) {
101+
schedule(() -> {
102+
try {
103+
s.shutdownOutput();
104+
} catch (IOException ioe) { }
105+
}, delay);
106+
}
107+
108+
static void schedule(Runnable task, long delay) {
109+
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
110+
try {
111+
executor.schedule(task, delay, TimeUnit.MILLISECONDS);
112+
} finally {
113+
executor.shutdown();
114+
}
115+
}
116+
117+
interface ThrowingBiConsumer<T, U> {
118+
void accept(T t, U u) throws IOException;
119+
}
120+
121+
static void withConnection(ThrowingBiConsumer<Socket, Socket> consumer)
122+
throws IOException
123+
{
124+
Socket s1 = null;
125+
Socket s2 = null;
126+
try (ServerSocket ss = new ServerSocket(0)) {
127+
s1 = new Socket();
128+
s1.connect(ss.getLocalSocketAddress());
129+
s2 = ss.accept();
130+
consumer.accept(s1, s2);
131+
} finally {
132+
if (s1 != null) s1.close();
133+
if (s2 != null) s2.close();
134+
}
135+
}
136+
137+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
/*
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/**
25+
* @test
26+
* @requires os.family != "solaris"
27+
* @run testng ConnectionReset
28+
* @summary Test behavior of read and available when a connection is reset
29+
*/
30+
31+
import java.io.IOException;
32+
import java.io.InputStream;
33+
import java.net.InetAddress;
34+
import java.net.InetSocketAddress;
35+
import java.net.ServerSocket;
36+
import java.net.Socket;
37+
38+
import org.testng.annotations.Test;
39+
import static org.testng.Assert.*;
40+
41+
@Test
42+
public class ConnectionReset {
43+
44+
static final int REPEAT_COUNT = 5;
45+
46+
/**
47+
* Tests available before read when there are no bytes to read
48+
*/
49+
public void testAvailableBeforeRead1() throws IOException {
50+
System.out.println("testAvailableBeforeRead1");
51+
withResetConnection(null, s -> {
52+
InputStream in = s.getInputStream();
53+
for (int i=0; i<REPEAT_COUNT; i++) {
54+
int bytesAvailable = in.available();
55+
System.out.format("available => %d%n", bytesAvailable);
56+
assertTrue(bytesAvailable == 0);
57+
try {
58+
int bytesRead = in.read();
59+
if (bytesRead == -1) {
60+
System.out.println("read => EOF");
61+
} else {
62+
System.out.println("read => 1 byte");
63+
}
64+
assertTrue(false);
65+
} catch (IOException ioe) {
66+
System.out.format("read => %s (expected)%n", ioe);
67+
}
68+
}
69+
});
70+
}
71+
72+
/**
73+
* Tests available before read when there are bytes to read
74+
*/
75+
public void testAvailableBeforeRead2() throws IOException {
76+
System.out.println("testAvailableBeforeRead2");
77+
byte[] data = { 1, 2, 3 };
78+
withResetConnection(data, s -> {
79+
InputStream in = s.getInputStream();
80+
int remaining = data.length;
81+
for (int i=0; i<REPEAT_COUNT; i++) {
82+
int bytesAvailable = in.available();
83+
System.out.format("available => %d%n", bytesAvailable);
84+
assertTrue(bytesAvailable <= remaining);
85+
try {
86+
int bytesRead = in.read();
87+
if (bytesRead == -1) {
88+
System.out.println("read => EOF");
89+
assertTrue(false);
90+
} else {
91+
System.out.println("read => 1 byte");
92+
assertTrue(remaining > 0);
93+
remaining--;
94+
}
95+
} catch (IOException ioe) {
96+
System.out.format("read => %s%n", ioe);
97+
remaining = 0;
98+
}
99+
}
100+
});
101+
}
102+
103+
/**
104+
* Tests read before available when there are no bytes to read
105+
*/
106+
public void testReadBeforeAvailable1() throws IOException {
107+
System.out.println("testReadBeforeAvailable1");
108+
withResetConnection(null, s -> {
109+
InputStream in = s.getInputStream();
110+
for (int i=0; i<REPEAT_COUNT; i++) {
111+
try {
112+
int bytesRead = in.read();
113+
if (bytesRead == -1) {
114+
System.out.println("read => EOF");
115+
} else {
116+
System.out.println("read => 1 byte");
117+
}
118+
assertTrue(false);
119+
} catch (IOException ioe) {
120+
System.out.format("read => %s (expected)%n", ioe);
121+
}
122+
int bytesAvailable = in.available();
123+
System.out.format("available => %d%n", bytesAvailable);
124+
assertTrue(bytesAvailable == 0);
125+
}
126+
});
127+
}
128+
129+
/**
130+
* Tests read before available when there are bytes to read
131+
*/
132+
public void testReadBeforeAvailable2() throws IOException {
133+
System.out.println("testReadBeforeAvailable2");
134+
byte[] data = { 1, 2, 3 };
135+
withResetConnection(data, s -> {
136+
InputStream in = s.getInputStream();
137+
int remaining = data.length;
138+
for (int i=0; i<REPEAT_COUNT; i++) {
139+
try {
140+
int bytesRead = in.read();
141+
if (bytesRead == -1) {
142+
System.out.println("read => EOF");
143+
assertTrue(false);
144+
} else {
145+
System.out.println("read => 1 byte");
146+
assertTrue(remaining > 0);
147+
remaining--;
148+
}
149+
} catch (IOException ioe) {
150+
System.out.format("read => %s%n", ioe);
151+
remaining = 0;
152+
}
153+
int bytesAvailable = in.available();
154+
System.out.format("available => %d%n", bytesAvailable);
155+
assertTrue(bytesAvailable <= remaining);
156+
}
157+
});
158+
}
159+
160+
/**
161+
* Tests available and read on a socket closed after connection reset
162+
*/
163+
public void testAfterClose() throws IOException {
164+
System.out.println("testAfterClose");
165+
withResetConnection(null, s -> {
166+
InputStream in = s.getInputStream();
167+
try {
168+
in.read();
169+
assertTrue(false);
170+
} catch (IOException ioe) {
171+
// expected
172+
}
173+
s.close();
174+
try {
175+
int bytesAvailable = in.available();
176+
System.out.format("available => %d%n", bytesAvailable);
177+
assertTrue(false);
178+
} catch (IOException ioe) {
179+
System.out.format("available => %s (expected)%n", ioe);
180+
}
181+
try {
182+
int n = in.read();
183+
System.out.format("read => %d%n", n);
184+
assertTrue(false);
185+
} catch (IOException ioe) {
186+
System.out.format("read => %s (expected)%n", ioe);
187+
}
188+
});
189+
}
190+
191+
interface ThrowingConsumer<T> {
192+
void accept(T t) throws IOException;
193+
}
194+
195+
/**
196+
* Invokes a consumer with a Socket connected to a peer that has closed the
197+
* connection with a "connection reset". The peer sends the given data bytes
198+
* before closing (when data is not null).
199+
*/
200+
static void withResetConnection(byte[] data, ThrowingConsumer<Socket> consumer)
201+
throws IOException
202+
{
203+
var loopback = InetAddress.getLoopbackAddress();
204+
try (var listener = new ServerSocket()) {
205+
listener.bind(new InetSocketAddress(loopback, 0));
206+
try (var socket = new Socket()) {
207+
socket.connect(listener.getLocalSocketAddress());
208+
try (Socket peer = listener.accept()) {
209+
if (data != null) {
210+
peer.getOutputStream().write(data);
211+
}
212+
peer.setSoLinger(true, 0);
213+
}
214+
consumer.accept(socket);
215+
}
216+
}
217+
}
218+
}

0 commit comments

Comments
 (0)