57202
|
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 |
* @run testng/othervm -Djdk.net.usePlainSocketImpl ConnectionReset
|
|
29 |
* @summary Test behavior of read and available when a connection is reset
|
|
30 |
*/
|
|
31 |
|
|
32 |
import java.io.IOException;
|
|
33 |
import java.io.InputStream;
|
|
34 |
import java.net.ServerSocket;
|
|
35 |
import java.net.Socket;
|
|
36 |
|
|
37 |
import org.testng.annotations.Test;
|
|
38 |
import static org.testng.Assert.*;
|
|
39 |
|
|
40 |
@Test
|
|
41 |
public class ConnectionReset {
|
|
42 |
|
|
43 |
static final int REPEAT_COUNT = 5;
|
|
44 |
|
|
45 |
/**
|
|
46 |
* Tests available before read when there are no bytes to read
|
|
47 |
*/
|
|
48 |
public void testAvailableBeforeRead1() throws IOException {
|
|
49 |
System.out.println("testAvailableBeforeRead1");
|
|
50 |
acceptResetConnection(null, s -> {
|
|
51 |
InputStream in = s.getInputStream();
|
|
52 |
for (int i=0; i<REPEAT_COUNT; i++) {
|
|
53 |
int bytesAvailable = in.available();
|
|
54 |
System.out.format("available => %d%n", bytesAvailable);
|
|
55 |
assertTrue(bytesAvailable == 0);
|
|
56 |
try {
|
|
57 |
int bytesRead = in.read();
|
|
58 |
if (bytesRead == -1) {
|
|
59 |
System.out.println("read => EOF");
|
|
60 |
} else {
|
|
61 |
System.out.println("read => 1 byte");
|
|
62 |
}
|
|
63 |
assertTrue(false);
|
|
64 |
} catch (IOException ioe) {
|
|
65 |
System.out.format("read => %s (expected)%n", ioe);
|
|
66 |
}
|
|
67 |
}
|
|
68 |
});
|
|
69 |
}
|
|
70 |
|
|
71 |
/**
|
|
72 |
* Tests available before read when there are bytes to read
|
|
73 |
*/
|
|
74 |
public void testAvailableBeforeRead2() throws IOException {
|
|
75 |
System.out.println("testAvailableBeforeRead2");
|
|
76 |
byte[] data = { 1, 2, 3 };
|
|
77 |
acceptResetConnection(data, s -> {
|
|
78 |
InputStream in = s.getInputStream();
|
|
79 |
int remaining = data.length;
|
|
80 |
for (int i=0; i<REPEAT_COUNT; i++) {
|
|
81 |
int bytesAvailable = in.available();
|
|
82 |
System.out.format("available => %d%n", bytesAvailable);
|
|
83 |
assertTrue(bytesAvailable <= remaining);
|
|
84 |
try {
|
|
85 |
int bytesRead = in.read();
|
|
86 |
if (bytesRead == -1) {
|
|
87 |
System.out.println("read => EOF");
|
|
88 |
assertTrue(false);
|
|
89 |
} else {
|
|
90 |
System.out.println("read => 1 byte");
|
|
91 |
assertTrue(remaining > 0);
|
|
92 |
remaining--;
|
|
93 |
}
|
|
94 |
} catch (IOException ioe) {
|
|
95 |
System.out.format("read => %s%n", ioe);
|
|
96 |
remaining = 0;
|
|
97 |
}
|
|
98 |
}
|
|
99 |
});
|
|
100 |
}
|
|
101 |
|
|
102 |
/**
|
|
103 |
* Tests read before available when there are no bytes to read
|
|
104 |
*/
|
|
105 |
public void testReadBeforeAvailable1() throws IOException {
|
|
106 |
System.out.println("testReadBeforeAvailable1");
|
|
107 |
acceptResetConnection(null, s -> {
|
|
108 |
InputStream in = s.getInputStream();
|
|
109 |
for (int i=0; i<REPEAT_COUNT; i++) {
|
|
110 |
try {
|
|
111 |
int bytesRead = in.read();
|
|
112 |
if (bytesRead == -1) {
|
|
113 |
System.out.println("read => EOF");
|
|
114 |
} else {
|
|
115 |
System.out.println("read => 1 byte");
|
|
116 |
}
|
|
117 |
assertTrue(false);
|
|
118 |
} catch (IOException ioe) {
|
|
119 |
System.out.format("read => %s (expected)%n", ioe);
|
|
120 |
}
|
|
121 |
int bytesAvailable = in.available();
|
|
122 |
System.out.format("available => %d%n", bytesAvailable);
|
|
123 |
assertTrue(bytesAvailable == 0);
|
|
124 |
}
|
|
125 |
});
|
|
126 |
}
|
|
127 |
|
|
128 |
/**
|
|
129 |
* Tests read before available when there are bytes to read
|
|
130 |
*/
|
|
131 |
public void testReadBeforeAvailable2() throws IOException {
|
|
132 |
System.out.println("testReadBeforeAvailable2");
|
|
133 |
byte[] data = { 1, 2, 3 };
|
|
134 |
acceptResetConnection(data, s -> {
|
|
135 |
InputStream in = s.getInputStream();
|
|
136 |
int remaining = data.length;
|
|
137 |
for (int i=0; i<REPEAT_COUNT; i++) {
|
|
138 |
try {
|
|
139 |
int bytesRead = in.read();
|
|
140 |
if (bytesRead == -1) {
|
|
141 |
System.out.println("read => EOF");
|
|
142 |
assertTrue(false);
|
|
143 |
} else {
|
|
144 |
System.out.println("read => 1 byte");
|
|
145 |
assertTrue(remaining > 0);
|
|
146 |
remaining--;
|
|
147 |
}
|
|
148 |
} catch (IOException ioe) {
|
|
149 |
System.out.format("read => %s%n", ioe);
|
|
150 |
remaining = 0;
|
|
151 |
}
|
|
152 |
int bytesAvailable = in.available();
|
|
153 |
System.out.format("available => %d%n", bytesAvailable);
|
|
154 |
assertTrue(bytesAvailable <= remaining);
|
|
155 |
}
|
|
156 |
});
|
|
157 |
}
|
|
158 |
|
57207
|
159 |
/**
|
|
160 |
* Tests available and read on a socket closed after connection reset
|
|
161 |
*/
|
|
162 |
public void testAfterClose() throws IOException {
|
|
163 |
System.out.println("testAfterClose");
|
|
164 |
acceptResetConnection(null, s -> {
|
|
165 |
InputStream in = s.getInputStream();
|
|
166 |
try {
|
|
167 |
in.read();
|
|
168 |
assertTrue(false);
|
|
169 |
} catch (IOException ioe) {
|
|
170 |
// expected
|
|
171 |
}
|
|
172 |
s.close();
|
|
173 |
try {
|
|
174 |
int bytesAvailable = in.available();
|
|
175 |
System.out.format("available => %d%n", bytesAvailable);
|
|
176 |
assertTrue(false);
|
|
177 |
} catch (IOException ioe) {
|
|
178 |
System.out.format("available => %s (expected)%n", ioe);
|
|
179 |
}
|
|
180 |
try {
|
|
181 |
int n = in.read();
|
|
182 |
System.out.format("read => %d%n", n);
|
|
183 |
assertTrue(false);
|
|
184 |
} catch (IOException ioe) {
|
|
185 |
System.out.format("read => %s (expected)%n", ioe);
|
|
186 |
}
|
|
187 |
});
|
|
188 |
}
|
|
189 |
|
57202
|
190 |
interface ThrowingConsumer<T> {
|
|
191 |
void accept(T t) throws IOException;
|
|
192 |
}
|
|
193 |
|
|
194 |
/**
|
|
195 |
* Invokes a consumer with a Socket connected to a peer that has closed the
|
|
196 |
* connection with a "connection reset". The peer sends the given data bytes
|
|
197 |
* before closing (when data is not null).
|
|
198 |
*/
|
|
199 |
static void acceptResetConnection(byte[] data, ThrowingConsumer<Socket> consumer)
|
|
200 |
throws IOException
|
|
201 |
{
|
|
202 |
try (var listener = new ServerSocket(0)) {
|
|
203 |
try (var socket = new Socket()) {
|
|
204 |
socket.connect(listener.getLocalSocketAddress());
|
|
205 |
try (Socket peer = listener.accept()) {
|
|
206 |
if (data != null) {
|
|
207 |
peer.getOutputStream().write(data);
|
|
208 |
}
|
|
209 |
peer.setSoLinger(true, 0);
|
|
210 |
}
|
|
211 |
consumer.accept(socket);
|
|
212 |
}
|
|
213 |
}
|
|
214 |
}
|
|
215 |
}
|