|
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 the DatagramChannel socket adaptor getter methods |
|
27 * @run testng AdaptorGetters |
|
28 */ |
|
29 |
|
30 import java.net.DatagramSocket; |
|
31 import java.net.InetAddress; |
|
32 import java.net.InetSocketAddress; |
|
33 import java.nio.channels.DatagramChannel; |
|
34 |
|
35 import org.testng.annotations.Test; |
|
36 import static org.testng.Assert.*; |
|
37 |
|
38 @Test |
|
39 public class AdaptorGetters { |
|
40 |
|
41 /** |
|
42 * Test getters on unbound socket, before and after it is closed. |
|
43 */ |
|
44 public void testUnboundSocket() throws Exception { |
|
45 DatagramChannel dc = DatagramChannel.open(); |
|
46 DatagramSocket s = dc.socket(); |
|
47 try { |
|
48 |
|
49 // state |
|
50 assertFalse(s.isBound()); |
|
51 assertFalse(s.isConnected()); |
|
52 assertFalse(s.isClosed()); |
|
53 |
|
54 // local address |
|
55 assertTrue(s.getLocalAddress().isAnyLocalAddress()); |
|
56 assertTrue(s.getLocalPort() == 0); |
|
57 assertTrue(s.getLocalSocketAddress() == null); |
|
58 |
|
59 // remote address |
|
60 assertTrue(s.getInetAddress() == null); |
|
61 assertTrue(s.getPort() == -1); |
|
62 |
|
63 } finally { |
|
64 dc.close(); |
|
65 } |
|
66 |
|
67 // state |
|
68 assertFalse(s.isBound()); |
|
69 assertFalse(s.isConnected()); |
|
70 assertTrue(s.isClosed()); |
|
71 |
|
72 // local address |
|
73 assertTrue(s.getLocalAddress() == null); |
|
74 assertTrue(s.getLocalPort() == -1); |
|
75 assertTrue(s.getLocalSocketAddress() == null); |
|
76 |
|
77 // remote address |
|
78 assertTrue(s.getInetAddress() == null); |
|
79 assertTrue(s.getPort() == -1); |
|
80 assertTrue((s.getRemoteSocketAddress() == null)); |
|
81 } |
|
82 |
|
83 /** |
|
84 * Test getters on bound socket, before and after it is closed. |
|
85 */ |
|
86 public void testBoundSocket() throws Exception { |
|
87 DatagramChannel dc = DatagramChannel.open(); |
|
88 DatagramSocket s = dc.socket(); |
|
89 try { |
|
90 dc.bind(new InetSocketAddress(0)); |
|
91 var localAddress = (InetSocketAddress) dc.getLocalAddress(); |
|
92 |
|
93 // state |
|
94 assertTrue(s.isBound()); |
|
95 assertFalse(s.isConnected()); |
|
96 assertFalse(s.isClosed()); |
|
97 |
|
98 // local address |
|
99 assertEquals(s.getLocalAddress(), localAddress.getAddress()); |
|
100 assertTrue(s.getLocalPort() == localAddress.getPort()); |
|
101 assertEquals(s.getLocalSocketAddress(), localAddress); |
|
102 |
|
103 // remote address |
|
104 assertTrue(s.getInetAddress() == null); |
|
105 assertTrue(s.getPort() == -1); |
|
106 assertTrue((s.getRemoteSocketAddress() == null)); |
|
107 |
|
108 } finally { |
|
109 dc.close(); |
|
110 } |
|
111 |
|
112 // state |
|
113 assertTrue(s.isBound()); |
|
114 assertFalse(s.isConnected()); |
|
115 assertTrue(s.isClosed()); |
|
116 |
|
117 // local address |
|
118 assertTrue(s.getLocalAddress() == null); |
|
119 assertTrue(s.getLocalPort() == -1); |
|
120 assertTrue(s.getLocalSocketAddress() == null); |
|
121 |
|
122 // remote address |
|
123 assertTrue(s.getInetAddress() == null); |
|
124 assertTrue(s.getPort() == -1); |
|
125 assertTrue((s.getRemoteSocketAddress() == null)); |
|
126 } |
|
127 |
|
128 /** |
|
129 * Test getters on connected socket, before and after it is closed. |
|
130 */ |
|
131 public void testConnectedSocket() throws Exception { |
|
132 var loopback = InetAddress.getLoopbackAddress(); |
|
133 var remoteAddress = new InetSocketAddress(loopback, 7777); |
|
134 DatagramChannel dc = DatagramChannel.open(); |
|
135 DatagramSocket s = dc.socket(); |
|
136 try { |
|
137 dc.connect(remoteAddress); |
|
138 var localAddress = (InetSocketAddress) dc.getLocalAddress(); |
|
139 |
|
140 // state |
|
141 assertTrue(s.isBound()); |
|
142 assertTrue(s.isConnected()); |
|
143 assertFalse(s.isClosed()); |
|
144 |
|
145 // local address |
|
146 assertEquals(s.getLocalAddress(), localAddress.getAddress()); |
|
147 assertTrue(s.getLocalPort() == localAddress.getPort()); |
|
148 assertEquals(s.getLocalSocketAddress(), localAddress); |
|
149 |
|
150 // remote address |
|
151 assertEquals(s.getInetAddress(), remoteAddress.getAddress()); |
|
152 assertTrue(s.getPort() == remoteAddress.getPort()); |
|
153 assertEquals(s.getRemoteSocketAddress(), remoteAddress); |
|
154 |
|
155 } finally { |
|
156 dc.close(); |
|
157 } |
|
158 |
|
159 // state |
|
160 assertTrue(s.isBound()); |
|
161 assertTrue(s.isConnected()); |
|
162 assertTrue(s.isClosed()); |
|
163 |
|
164 // local address |
|
165 assertTrue(s.getLocalAddress() == null); |
|
166 assertTrue(s.getLocalPort() == -1); |
|
167 assertTrue(s.getLocalSocketAddress() == null); |
|
168 |
|
169 // remote address |
|
170 assertEquals(s.getInetAddress(), remoteAddress.getAddress()); |
|
171 assertTrue(s.getPort() == remoteAddress.getPort()); |
|
172 assertEquals(s.getRemoteSocketAddress(), remoteAddress); |
|
173 } |
|
174 |
|
175 /** |
|
176 * Test getters on disconnected socket, before and after it is closed. |
|
177 */ |
|
178 public void testDisconnectedSocket() throws Exception { |
|
179 DatagramChannel dc = DatagramChannel.open(); |
|
180 DatagramSocket s = dc.socket(); |
|
181 try { |
|
182 var loopback = InetAddress.getLoopbackAddress(); |
|
183 dc.connect(new InetSocketAddress(loopback, 7777)); |
|
184 dc.disconnect(); |
|
185 |
|
186 var localAddress = (InetSocketAddress) dc.getLocalAddress(); |
|
187 |
|
188 // state |
|
189 assertTrue(s.isBound()); |
|
190 assertFalse(s.isConnected()); |
|
191 assertFalse(s.isClosed()); |
|
192 |
|
193 // local address |
|
194 assertEquals(s.getLocalAddress(), localAddress.getAddress()); |
|
195 assertTrue(s.getLocalPort() == localAddress.getPort()); |
|
196 assertEquals(s.getLocalSocketAddress(), localAddress); |
|
197 |
|
198 // remote address |
|
199 assertTrue(s.getInetAddress() == null); |
|
200 assertTrue(s.getPort() == -1); |
|
201 assertTrue((s.getRemoteSocketAddress() == null)); |
|
202 |
|
203 |
|
204 } finally { |
|
205 dc.close(); |
|
206 } |
|
207 |
|
208 // state |
|
209 assertTrue(s.isBound()); |
|
210 assertFalse(s.isConnected()); |
|
211 assertTrue(s.isClosed()); |
|
212 |
|
213 // local address |
|
214 assertTrue(s.getLocalAddress() == null); |
|
215 assertTrue(s.getLocalPort() == -1); |
|
216 assertTrue(s.getLocalSocketAddress() == null); |
|
217 |
|
218 // remote address |
|
219 assertTrue(s.getInetAddress() == null); |
|
220 assertTrue(s.getPort() == -1); |
|
221 assertTrue((s.getRemoteSocketAddress() == null)); |
|
222 } |
|
223 } |