|
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 /* @test |
|
25 * @bug 8232673 |
|
26 * @summary Test DatagramChannel socket adaptor connect method with illegal args |
|
27 * @run testng AdaptorConnect |
|
28 */ |
|
29 |
|
30 import java.net.DatagramSocket; |
|
31 import java.net.InetSocketAddress; |
|
32 import java.net.SocketAddress; |
|
33 import java.net.SocketException; |
|
34 import java.nio.channels.DatagramChannel; |
|
35 import static java.net.InetAddress.getLoopbackAddress; |
|
36 |
|
37 import org.testng.annotations.Test; |
|
38 import static org.testng.Assert.*; |
|
39 |
|
40 @Test |
|
41 public class AdaptorConnect { |
|
42 |
|
43 /** |
|
44 * Invoke the given socket's connect method with illegal arguments. |
|
45 */ |
|
46 private void testConnectWithIllegalArguments(DatagramSocket s) { |
|
47 assertThrows(IllegalArgumentException.class, () -> s.connect(null)); |
|
48 assertThrows(IllegalArgumentException.class, () -> s.connect(null, 7000)); |
|
49 assertThrows(IllegalArgumentException.class, () -> s.connect(getLoopbackAddress(), -1)); |
|
50 assertThrows(IllegalArgumentException.class, () -> s.connect(getLoopbackAddress(), 100_000)); |
|
51 |
|
52 SocketAddress sillyAddress = new SocketAddress() { }; |
|
53 assertThrows(IllegalArgumentException.class, () -> s.connect(sillyAddress)); |
|
54 |
|
55 SocketAddress unresolved = InetSocketAddress.createUnresolved("foo", 7777); |
|
56 assertThrows(SocketException.class, () -> s.connect(unresolved)); |
|
57 } |
|
58 |
|
59 /** |
|
60 * Test connect method with an open socket. |
|
61 */ |
|
62 public void testOpenSocket() throws Exception { |
|
63 try (DatagramChannel dc = DatagramChannel.open()) { |
|
64 DatagramSocket s = dc.socket(); |
|
65 |
|
66 testConnectWithIllegalArguments(s); |
|
67 |
|
68 // should not be bound or connected |
|
69 assertTrue(s.getLocalSocketAddress() == null); |
|
70 assertTrue(s.getRemoteSocketAddress() == null); |
|
71 |
|
72 // connect(SocketAddress) |
|
73 var remote1 = new InetSocketAddress(getLoopbackAddress(), 7001); |
|
74 s.connect(remote1); |
|
75 assertEquals(s.getRemoteSocketAddress(), remote1); |
|
76 testConnectWithIllegalArguments(s); |
|
77 assertEquals(s.getRemoteSocketAddress(), remote1); |
|
78 |
|
79 // connect(SocketAddress) |
|
80 var remote2 = new InetSocketAddress(getLoopbackAddress(), 7002); |
|
81 s.connect(remote2); |
|
82 assertEquals(s.getRemoteSocketAddress(), remote2); |
|
83 testConnectWithIllegalArguments(s); |
|
84 assertEquals(s.getRemoteSocketAddress(), remote2); |
|
85 |
|
86 // connect(InetAddress, int) |
|
87 var remote3 = new InetSocketAddress(getLoopbackAddress(), 7003); |
|
88 s.connect(remote3.getAddress(), remote3.getPort()); |
|
89 assertEquals(s.getRemoteSocketAddress(), remote3); |
|
90 testConnectWithIllegalArguments(s); |
|
91 assertEquals(s.getRemoteSocketAddress(), remote3); |
|
92 |
|
93 // connect(InetAddress, int) |
|
94 var remote4 = new InetSocketAddress(getLoopbackAddress(), 7004); |
|
95 s.connect(remote4.getAddress(), remote4.getPort()); |
|
96 assertEquals(s.getRemoteSocketAddress(), remote4); |
|
97 testConnectWithIllegalArguments(s); |
|
98 assertEquals(s.getRemoteSocketAddress(), remote4); |
|
99 } |
|
100 } |
|
101 |
|
102 /** |
|
103 * Test connect method with a closed socket. |
|
104 */ |
|
105 public void testClosedSocket() throws Exception { |
|
106 DatagramChannel dc = DatagramChannel.open(); |
|
107 DatagramSocket s = dc.socket(); |
|
108 dc.close(); |
|
109 |
|
110 testConnectWithIllegalArguments(s); |
|
111 |
|
112 // connect does not throw an exception when closed |
|
113 var remote = new InetSocketAddress(getLoopbackAddress(), 7001); |
|
114 s.connect(remote); |
|
115 s.connect(remote.getAddress(), remote.getPort()); |
|
116 } |
|
117 } |