57310
|
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
|
57311
|
26 |
* @compile/module=java.base java/net/PlatformSocketImpl.java
|
|
27 |
* @run testng/othervm BadUsages
|
|
28 |
* @summary Test the platform SocketImpl in illegal state/bad input scenarios
|
57310
|
29 |
*/
|
|
30 |
|
|
31 |
import java.io.IOException;
|
|
32 |
import java.io.InputStream;
|
|
33 |
import java.io.OutputStream;
|
|
34 |
import java.net.InetAddress;
|
|
35 |
import java.net.InetSocketAddress;
|
|
36 |
import java.net.ServerSocket;
|
|
37 |
import java.net.SocketAddress;
|
|
38 |
import java.net.SocketException;
|
|
39 |
import java.net.SocketImpl;
|
|
40 |
import java.net.SocketOption;
|
57311
|
41 |
import java.net.SocketOptions;
|
57310
|
42 |
import java.net.StandardSocketOptions;
|
|
43 |
import java.util.Set;
|
|
44 |
|
57311
|
45 |
import java.net.PlatformSocketImpl; // test helper
|
|
46 |
|
57310
|
47 |
import org.testng.annotations.Test;
|
|
48 |
import static org.testng.Assert.*;
|
|
49 |
|
57311
|
50 |
/**
|
|
51 |
* SocketImpl does not specify how the SocketImpl behaves when used in ways
|
|
52 |
* that are not intended, e.g. invoking socket operations before the socket is
|
|
53 |
* created or trying to establish a connection after the socket is connected or
|
|
54 |
* closed.
|
|
55 |
*
|
|
56 |
* This test exercises the platform SocketImpl to test that it is reliable, and
|
|
57 |
* throws reasonable exceptions, for these scenarios.
|
|
58 |
*/
|
|
59 |
|
57310
|
60 |
@Test
|
57311
|
61 |
public class BadUsages {
|
57310
|
62 |
|
|
63 |
/**
|
57311
|
64 |
* Test create when already created.
|
57310
|
65 |
*/
|
|
66 |
public void testCreate1() throws IOException {
|
|
67 |
try (var impl = new PlatformSocketImpl(false)) {
|
|
68 |
impl.create(true);
|
|
69 |
expectThrows(IOException.class, () -> impl.create(true));
|
|
70 |
}
|
|
71 |
}
|
|
72 |
|
|
73 |
/**
|
57311
|
74 |
* Test create when closed.
|
57310
|
75 |
*/
|
|
76 |
public void testCreate2() throws IOException {
|
|
77 |
var impl = new PlatformSocketImpl(false);
|
|
78 |
impl.close();
|
|
79 |
expectThrows(IOException.class, () -> impl.create(true));
|
|
80 |
}
|
|
81 |
|
|
82 |
/**
|
57311
|
83 |
* Test connect when not created.
|
57310
|
84 |
*/
|
|
85 |
public void testConnect1() throws IOException {
|
|
86 |
try (var ss = new ServerSocket(0)) {
|
|
87 |
var impl = new PlatformSocketImpl(false);
|
57311
|
88 |
var address = ss.getInetAddress();
|
|
89 |
int port = ss.getLocalPort();
|
|
90 |
expectThrows(IOException.class, () -> impl.connect(address, port));
|
57310
|
91 |
}
|
|
92 |
}
|
|
93 |
|
|
94 |
/**
|
57311
|
95 |
* Test connect with unsupported address type.
|
57310
|
96 |
*/
|
|
97 |
public void testConnect2() throws IOException {
|
|
98 |
try (var impl = new PlatformSocketImpl(false)) {
|
|
99 |
impl.create(true);
|
|
100 |
var remote = new SocketAddress() { };
|
|
101 |
expectThrows(IOException.class, () -> impl.connect(remote, 0));
|
|
102 |
}
|
|
103 |
}
|
|
104 |
|
|
105 |
/**
|
57311
|
106 |
* Test connect with an unresolved address.
|
57310
|
107 |
*/
|
|
108 |
public void testConnect3() throws IOException {
|
|
109 |
try (var impl = new PlatformSocketImpl(false)) {
|
|
110 |
impl.create(true);
|
|
111 |
var remote = new InetSocketAddress("blah-blah.blah-blah", 80);
|
|
112 |
expectThrows(IOException.class, () -> impl.connect(remote, 0));
|
|
113 |
}
|
|
114 |
}
|
|
115 |
|
|
116 |
/**
|
57311
|
117 |
* Test connect when already connected.
|
57310
|
118 |
*/
|
|
119 |
public void testConnect4() throws IOException {
|
|
120 |
try (var ss = new ServerSocket(0);
|
|
121 |
var impl = new PlatformSocketImpl(false)) {
|
|
122 |
impl.create(true);
|
57311
|
123 |
String host = ss.getInetAddress().getHostAddress();
|
|
124 |
int port = ss.getLocalPort();
|
|
125 |
impl.connect(host, port);
|
|
126 |
expectThrows(IOException.class, () -> impl.connect(host, port));
|
57310
|
127 |
}
|
|
128 |
}
|
|
129 |
|
|
130 |
/**
|
57311
|
131 |
* Test connect when closed.
|
57310
|
132 |
*/
|
|
133 |
public void testConnect5() throws IOException {
|
|
134 |
try (var ss = new ServerSocket(0)) {
|
|
135 |
var impl = new PlatformSocketImpl(false);
|
|
136 |
impl.close();
|
57311
|
137 |
String host = ss.getInetAddress().getHostAddress();
|
|
138 |
int port = ss.getLocalPort();
|
|
139 |
expectThrows(IOException.class, () -> impl.connect(host, port));
|
57310
|
140 |
}
|
|
141 |
}
|
|
142 |
|
|
143 |
/**
|
57311
|
144 |
* Test bind when not created.
|
57310
|
145 |
*/
|
|
146 |
public void testBind1() throws IOException {
|
|
147 |
var impl = new PlatformSocketImpl(false);
|
|
148 |
var loopback = InetAddress.getLoopbackAddress();
|
|
149 |
expectThrows(IOException.class, () -> impl.bind(loopback, 0));
|
|
150 |
}
|
|
151 |
|
|
152 |
/**
|
57311
|
153 |
* Test bind when already bound.
|
57310
|
154 |
*/
|
|
155 |
public void testBind2() throws IOException {
|
|
156 |
try (var impl = new PlatformSocketImpl(false)) {
|
|
157 |
impl.create(true);
|
|
158 |
var loopback = InetAddress.getLoopbackAddress();
|
|
159 |
impl.bind(loopback, 0);
|
|
160 |
expectThrows(IOException.class, () -> impl.bind(loopback, 0));
|
|
161 |
}
|
|
162 |
}
|
|
163 |
|
|
164 |
/**
|
57311
|
165 |
* Test bind when connected.
|
57310
|
166 |
*/
|
|
167 |
public void testBind3() throws IOException {
|
|
168 |
try (var ss = new ServerSocket(0);
|
|
169 |
var impl = new PlatformSocketImpl(false)) {
|
|
170 |
impl.create(true);
|
|
171 |
impl.connect(ss.getLocalSocketAddress(), 0);
|
|
172 |
var loopback = InetAddress.getLoopbackAddress();
|
|
173 |
expectThrows(IOException.class, () -> impl.bind(loopback, 0));
|
|
174 |
}
|
|
175 |
}
|
|
176 |
|
|
177 |
/**
|
57311
|
178 |
* Test bind when closed.
|
57310
|
179 |
*/
|
|
180 |
public void testBind4() throws IOException {
|
|
181 |
var impl = new PlatformSocketImpl(false);
|
|
182 |
impl.close();
|
|
183 |
var loopback = InetAddress.getLoopbackAddress();
|
|
184 |
expectThrows(IOException.class, () -> impl.bind(loopback, 0));
|
|
185 |
}
|
|
186 |
|
|
187 |
|
|
188 |
/**
|
57311
|
189 |
* Test listen when not created.
|
57310
|
190 |
*/
|
|
191 |
public void testListen1() {
|
|
192 |
var impl = new PlatformSocketImpl(false);
|
|
193 |
expectThrows(IOException.class, () -> impl.listen(16));
|
|
194 |
}
|
|
195 |
|
|
196 |
/**
|
57311
|
197 |
* Test listen when not bound.
|
57310
|
198 |
*/
|
|
199 |
public void testListen2() throws IOException {
|
|
200 |
try (var impl = new PlatformSocketImpl(false)) {
|
|
201 |
impl.create(true);
|
|
202 |
expectThrows(IOException.class, () -> impl.listen(16));
|
|
203 |
}
|
|
204 |
}
|
|
205 |
|
|
206 |
/**
|
57311
|
207 |
* Test listen when closed.
|
57310
|
208 |
*/
|
|
209 |
public void testListen3() throws IOException {
|
|
210 |
var impl = new PlatformSocketImpl(false);
|
|
211 |
impl.close();
|
|
212 |
expectThrows(IOException.class, () -> impl.listen(16));
|
|
213 |
}
|
|
214 |
|
|
215 |
/**
|
57311
|
216 |
* Test accept when not created.
|
57310
|
217 |
*/
|
|
218 |
public void testAccept1() throws IOException {
|
|
219 |
var impl = new PlatformSocketImpl(true);
|
|
220 |
var si = new PlatformSocketImpl(false);
|
|
221 |
expectThrows(IOException.class, () -> impl.accept(si));
|
|
222 |
}
|
|
223 |
|
|
224 |
/**
|
57311
|
225 |
* Test accept when not bound.
|
57310
|
226 |
*/
|
|
227 |
public void testAccept2() throws IOException {
|
|
228 |
try (var impl = new PlatformSocketImpl(true)) {
|
|
229 |
impl.create(true);
|
|
230 |
var si = new PlatformSocketImpl(false);
|
|
231 |
expectThrows(IOException.class, () -> impl.accept(si));
|
|
232 |
}
|
|
233 |
}
|
|
234 |
|
|
235 |
/**
|
57311
|
236 |
* Test accept when not a stream socket.
|
57310
|
237 |
*/
|
|
238 |
public void testAccept3() throws IOException {
|
|
239 |
try (var impl = new PlatformSocketImpl(false)) {
|
|
240 |
impl.create(false);
|
|
241 |
impl.bind(InetAddress.getLoopbackAddress(), 0);
|
|
242 |
var si = new PlatformSocketImpl(false);
|
|
243 |
expectThrows(IOException.class, () -> impl.accept(si));
|
|
244 |
}
|
|
245 |
}
|
|
246 |
|
|
247 |
/**
|
57311
|
248 |
* Test accept when closed.
|
57310
|
249 |
*/
|
|
250 |
public void testAccept4() throws IOException {
|
|
251 |
var impl = new PlatformSocketImpl(true);
|
|
252 |
impl.close();
|
|
253 |
var si = new PlatformSocketImpl(false);
|
|
254 |
expectThrows(IOException.class, () -> impl.accept(si));
|
|
255 |
}
|
|
256 |
|
|
257 |
/**
|
57311
|
258 |
* Test accept with SocketImpl that is already created.
|
57310
|
259 |
*/
|
|
260 |
public void testAccept5() throws IOException {
|
|
261 |
try (var impl = new PlatformSocketImpl(true);
|
|
262 |
var si = new PlatformSocketImpl(false)) {
|
|
263 |
impl.create(true);
|
|
264 |
impl.bind(InetAddress.getLoopbackAddress(), 0);
|
|
265 |
si.create(true);
|
|
266 |
expectThrows(IOException.class, () -> impl.accept(si));
|
|
267 |
}
|
|
268 |
}
|
|
269 |
|
|
270 |
/**
|
57311
|
271 |
* Test accept with SocketImpl that is closed.
|
57310
|
272 |
*/
|
|
273 |
public void testAccept6() throws IOException {
|
|
274 |
try (var impl = new PlatformSocketImpl(true);
|
|
275 |
var si = new PlatformSocketImpl(false)) {
|
|
276 |
impl.create(true);
|
|
277 |
impl.bind(InetAddress.getLoopbackAddress(), 0);
|
|
278 |
si.create(true);
|
|
279 |
si.close();
|
|
280 |
expectThrows(IOException.class, () -> impl.accept(si));
|
|
281 |
}
|
|
282 |
}
|
|
283 |
|
|
284 |
/**
|
57311
|
285 |
* Test available when not created.
|
57310
|
286 |
*/
|
|
287 |
public void testAvailable1() throws IOException {
|
|
288 |
var impl = new PlatformSocketImpl(false);
|
|
289 |
expectThrows(IOException.class, () -> impl.available());
|
|
290 |
}
|
|
291 |
|
|
292 |
/**
|
57311
|
293 |
* Test available when created but not connected.
|
57310
|
294 |
*/
|
|
295 |
public void testAvailable2() throws IOException {
|
|
296 |
try (var impl = new PlatformSocketImpl(false)) {
|
|
297 |
impl.create(true);
|
|
298 |
expectThrows(IOException.class, () -> impl.available());
|
|
299 |
}
|
|
300 |
}
|
|
301 |
|
|
302 |
/**
|
57311
|
303 |
* Test available when closed.
|
57310
|
304 |
*/
|
|
305 |
public void testAvailable3() throws IOException {
|
|
306 |
var impl = new PlatformSocketImpl(false);
|
|
307 |
impl.close();
|
|
308 |
expectThrows(IOException.class, () -> impl.available());
|
|
309 |
}
|
|
310 |
|
57311
|
311 |
/**
|
|
312 |
* Test setOption when not created.
|
|
313 |
*/
|
|
314 |
public void testSetOption1() throws IOException {
|
|
315 |
var impl = new PlatformSocketImpl(false);
|
|
316 |
expectThrows(IOException.class,
|
|
317 |
() -> impl.setOption(StandardSocketOptions.SO_REUSEADDR, true));
|
|
318 |
// legacy
|
|
319 |
expectThrows(SocketException.class,
|
|
320 |
() -> impl.setOption(SocketOptions.SO_REUSEADDR, true));
|
|
321 |
}
|
57310
|
322 |
|
|
323 |
/**
|
57311
|
324 |
* Test setOption when closed.
|
57310
|
325 |
*/
|
57311
|
326 |
public void testSetOption2() throws IOException {
|
|
327 |
var impl = new PlatformSocketImpl(false);
|
|
328 |
impl.close();
|
|
329 |
expectThrows(IOException.class,
|
|
330 |
() -> impl.setOption(StandardSocketOptions.SO_REUSEADDR, true));
|
|
331 |
// legacy
|
|
332 |
expectThrows(SocketException.class,
|
|
333 |
() -> impl.setOption(SocketOptions.SO_REUSEADDR, true));
|
|
334 |
}
|
|
335 |
|
|
336 |
/**
|
|
337 |
* Test setOption with unsupported option.
|
|
338 |
*/
|
|
339 |
public void testSetOption3() throws IOException {
|
57310
|
340 |
try (var impl = new PlatformSocketImpl(false)) {
|
|
341 |
impl.create(true);
|
|
342 |
var opt = new SocketOption<String>() {
|
|
343 |
@Override public String name() { return "birthday"; }
|
|
344 |
@Override public Class<String> type() { return String.class; }
|
|
345 |
};
|
57311
|
346 |
expectThrows(UnsupportedOperationException.class, () -> impl.setOption(opt, ""));
|
|
347 |
// legacy
|
|
348 |
expectThrows(SocketException.class, () -> impl.setOption(-1, ""));
|
|
349 |
}
|
|
350 |
}
|
|
351 |
|
|
352 |
/**
|
|
353 |
* Test setOption(int, Object) with invalid values.
|
|
354 |
*/
|
|
355 |
public void testSetOption4() throws IOException {
|
|
356 |
try (var impl = new PlatformSocketImpl(false)) {
|
|
357 |
impl.create(true);
|
|
358 |
expectThrows(SocketException.class,
|
|
359 |
() -> impl.setOption(SocketOptions.SO_REUSEADDR, -1));
|
|
360 |
expectThrows(SocketException.class,
|
|
361 |
() -> impl.setOption(SocketOptions.SO_TIMEOUT, -1));
|
|
362 |
expectThrows(SocketException.class,
|
|
363 |
() -> impl.setOption(SocketOptions.SO_SNDBUF, -1));
|
|
364 |
expectThrows(SocketException.class,
|
|
365 |
() -> impl.setOption(SocketOptions.SO_RCVBUF, -1));
|
57310
|
366 |
}
|
|
367 |
}
|
|
368 |
|
|
369 |
/**
|
57311
|
370 |
* Test getOption when not created.
|
57310
|
371 |
*/
|
57311
|
372 |
public void testGetOption1() throws IOException {
|
57310
|
373 |
var impl = new PlatformSocketImpl(false);
|
57311
|
374 |
expectThrows(IOException.class,
|
|
375 |
() -> impl.getOption(StandardSocketOptions.SO_REUSEADDR));
|
|
376 |
expectThrows(SocketException.class,
|
|
377 |
() -> impl.getOption(-1));
|
57310
|
378 |
}
|
|
379 |
|
|
380 |
/**
|
57311
|
381 |
* Test getOption when closed.
|
57310
|
382 |
*/
|
57311
|
383 |
public void testGetOption2() throws IOException {
|
57310
|
384 |
var impl = new PlatformSocketImpl(false);
|
|
385 |
impl.close();
|
57311
|
386 |
expectThrows(IOException.class,
|
|
387 |
() -> impl.getOption(StandardSocketOptions.SO_REUSEADDR));
|
|
388 |
expectThrows(SocketException.class,
|
|
389 |
() -> impl.getOption(SocketOptions.SO_REUSEADDR));
|
57310
|
390 |
}
|
|
391 |
|
|
392 |
/**
|
57311
|
393 |
* Test getOption with unsupported option.
|
57310
|
394 |
*/
|
57311
|
395 |
public void testGetOption3() throws IOException {
|
57310
|
396 |
try (var impl = new PlatformSocketImpl(false)) {
|
|
397 |
impl.create(true);
|
|
398 |
var opt = new SocketOption<String>() {
|
|
399 |
@Override public String name() { return "birthday"; }
|
|
400 |
@Override public Class<String> type() { return String.class; }
|
|
401 |
};
|
|
402 |
expectThrows(UnsupportedOperationException.class, () -> impl.getOption(opt));
|
57311
|
403 |
expectThrows(SocketException.class, () -> impl.getOption(-1));
|
57310
|
404 |
}
|
|
405 |
}
|
|
406 |
|
|
407 |
/**
|
57311
|
408 |
* Test shutdownInput when not created.
|
57310
|
409 |
*/
|
|
410 |
public void testShutdownInput1() throws IOException {
|
|
411 |
var impl = new PlatformSocketImpl(false);
|
|
412 |
expectThrows(IOException.class, () -> impl.shutdownInput());
|
|
413 |
}
|
|
414 |
|
|
415 |
/**
|
57311
|
416 |
* Test shutdownInput when not connected.
|
57310
|
417 |
*/
|
|
418 |
public void testShutdownInput2() throws IOException {
|
|
419 |
try (var impl = new PlatformSocketImpl(false)) {
|
|
420 |
impl.create(true);
|
|
421 |
expectThrows(IOException.class, () -> impl.shutdownInput());
|
|
422 |
}
|
|
423 |
}
|
|
424 |
|
|
425 |
/**
|
57311
|
426 |
* Test shutdownInput when closed.
|
57310
|
427 |
*/
|
|
428 |
public void testShutdownInput3() throws IOException {
|
|
429 |
var impl = new PlatformSocketImpl(false);
|
|
430 |
impl.close();
|
|
431 |
expectThrows(IOException.class, () -> impl.shutdownInput());
|
|
432 |
}
|
|
433 |
|
|
434 |
/**
|
57311
|
435 |
* Test shutdownOutput when not created.
|
57310
|
436 |
*/
|
|
437 |
public void testShutdownOutput1() throws IOException {
|
|
438 |
var impl = new PlatformSocketImpl(false);
|
|
439 |
expectThrows(IOException.class, () -> impl.shutdownOutput());
|
|
440 |
}
|
|
441 |
|
|
442 |
/**
|
57311
|
443 |
* Test shutdownOutput when not connected.
|
57310
|
444 |
*/
|
|
445 |
public void testShutdownOutput2() throws IOException {
|
|
446 |
try (var impl = new PlatformSocketImpl(false)) {
|
|
447 |
impl.create(true);
|
|
448 |
expectThrows(IOException.class, () -> impl.shutdownOutput());
|
|
449 |
}
|
|
450 |
}
|
|
451 |
|
|
452 |
/**
|
57311
|
453 |
* Test shutdownOutput when closed.
|
57310
|
454 |
*/
|
|
455 |
public void testShutdownOutput3() throws IOException {
|
|
456 |
var impl = new PlatformSocketImpl(false);
|
|
457 |
impl.close();
|
|
458 |
expectThrows(IOException.class, () -> impl.shutdownOutput());
|
|
459 |
}
|
|
460 |
|
|
461 |
/**
|
57311
|
462 |
* Test sendUrgentData when not created.
|
57310
|
463 |
*/
|
|
464 |
public void testSendUrgentData1() throws IOException {
|
|
465 |
var impl = new PlatformSocketImpl(false);
|
|
466 |
expectThrows(IOException.class, () -> impl.sendUrgentData(0));
|
|
467 |
}
|
|
468 |
|
|
469 |
/**
|
57311
|
470 |
* Test sendUrgentData when not connected.
|
57310
|
471 |
*/
|
|
472 |
public void testSendUrgentData2() throws IOException {
|
|
473 |
try (var impl = new PlatformSocketImpl(false)) {
|
|
474 |
impl.create(true);
|
|
475 |
expectThrows(IOException.class, () -> impl.sendUrgentData(0));
|
|
476 |
}
|
|
477 |
}
|
|
478 |
|
|
479 |
/**
|
57311
|
480 |
* Test sendUrgentData when closed.
|
57310
|
481 |
*/
|
|
482 |
public void testSendUrgentData3() throws IOException {
|
|
483 |
var impl = new PlatformSocketImpl(false);
|
|
484 |
impl.close();
|
|
485 |
expectThrows(IOException.class, () -> impl.sendUrgentData(0));
|
|
486 |
}
|
|
487 |
}
|