author | phh |
Sat, 30 Nov 2019 14:33:05 -0800 | |
changeset 59330 | 5b96c12f909d |
parent 55264 | b7ad292e686c |
permissions | -rw-r--r-- |
1152 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
9679
diff
changeset
|
2 |
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. |
1152 | 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 |
* |
|
5506 | 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. |
|
1152 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
25 |
* @bug 4527345 |
|
26 |
* @summary Unit test for DatagramChannel's multicast support |
|
45186
387a39577f09
8180644: move jdk.testlibrary.NetworkConfiguration to the top level test library
iignatyev
parents:
14342
diff
changeset
|
27 |
* @library /test/lib |
45467
99c87a16a8e4
8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents:
45186
diff
changeset
|
28 |
* @build jdk.test.lib.NetworkConfiguration |
99c87a16a8e4
8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents:
45186
diff
changeset
|
29 |
* jdk.test.lib.Platform |
99c87a16a8e4
8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents:
45186
diff
changeset
|
30 |
* BasicMulticastTests |
3424 | 31 |
* @run main BasicMulticastTests |
1152 | 32 |
*/ |
33 |
||
34 |
import java.nio.ByteBuffer; |
|
35 |
import java.nio.channels.*; |
|
36 |
import java.net.*; |
|
37 |
import java.util.*; |
|
38 |
import java.io.IOException; |
|
39 |
||
45186
387a39577f09
8180644: move jdk.testlibrary.NetworkConfiguration to the top level test library
iignatyev
parents:
14342
diff
changeset
|
40 |
import jdk.test.lib.NetworkConfiguration; |
387a39577f09
8180644: move jdk.testlibrary.NetworkConfiguration to the top level test library
iignatyev
parents:
14342
diff
changeset
|
41 |
|
1152 | 42 |
public class BasicMulticastTests { |
43 |
||
44 |
/** |
|
45 |
* Tests that existing membership key is returned by join methods and that |
|
46 |
* membership key methods return the expected results |
|
47 |
*/ |
|
54947
69d1dff1bb70
8224019: test/jdk/java/nio/channels/DatagramChannel/BasicMulticastTests.java assumes IPv4 is always available
aeubanks
parents:
47216
diff
changeset
|
48 |
static void membershipKeyTests(StandardProtocolFamily family, |
1152 | 49 |
InetAddress group, |
54947
69d1dff1bb70
8224019: test/jdk/java/nio/channels/DatagramChannel/BasicMulticastTests.java assumes IPv4 is always available
aeubanks
parents:
47216
diff
changeset
|
50 |
NetworkInterface nif, |
1152 | 51 |
InetAddress source) |
52 |
throws IOException |
|
53 |
{ |
|
54 |
System.out.format("MembershipKey test using %s @ %s\n", |
|
55 |
group.getHostAddress(), nif.getName()); |
|
56 |
||
57 |
DatagramChannel dc = DatagramChannel.open(family) |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
5506
diff
changeset
|
58 |
.setOption(StandardSocketOptions.SO_REUSEADDR, true) |
1152 | 59 |
.bind(new InetSocketAddress(source, 0)); |
60 |
||
61 |
// check existing key is returned |
|
62 |
MembershipKey key = dc.join(group, nif); |
|
63 |
MembershipKey other = dc.join(group, nif); |
|
64 |
if (other != key) { |
|
65 |
throw new RuntimeException("existing key not returned"); |
|
66 |
} |
|
67 |
||
68 |
// check key |
|
69 |
if (!key.isValid()) |
|
70 |
throw new RuntimeException("key is not valid"); |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
71 |
if (!key.group().equals(group)) |
1152 | 72 |
throw new RuntimeException("group is incorrect"); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
73 |
if (!key.networkInterface().equals(nif)) |
1152 | 74 |
throw new RuntimeException("network interface is incorrect"); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
75 |
if (key.sourceAddress() != null) |
1152 | 76 |
throw new RuntimeException("key is source specific"); |
77 |
||
78 |
// drop membership |
|
79 |
key.drop(); |
|
80 |
if (key.isValid()) { |
|
81 |
throw new RuntimeException("key is still valid"); |
|
82 |
} |
|
83 |
||
84 |
// source-specific |
|
85 |
try { |
|
86 |
key = dc.join(group, nif, source); |
|
87 |
other = dc.join(group, nif, source); |
|
88 |
if (other != key) { |
|
89 |
throw new RuntimeException("existing key not returned"); |
|
90 |
} |
|
91 |
if (!key.isValid()) |
|
92 |
throw new RuntimeException("key is not valid"); |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
93 |
if (!key.group().equals(group)) |
1152 | 94 |
throw new RuntimeException("group is incorrect"); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
95 |
if (!key.networkInterface().equals(nif)) |
1152 | 96 |
throw new RuntimeException("network interface is incorrect"); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1152
diff
changeset
|
97 |
if (!key.sourceAddress().equals(source)) |
1152 | 98 |
throw new RuntimeException("key's source address incorrect"); |
99 |
||
100 |
// drop membership |
|
101 |
key.drop(); |
|
102 |
if (key.isValid()) { |
|
103 |
throw new RuntimeException("key is still valid"); |
|
104 |
} |
|
105 |
} catch (UnsupportedOperationException x) { |
|
106 |
} |
|
107 |
||
108 |
// done |
|
109 |
dc.close(); |
|
110 |
} |
|
111 |
||
112 |
/** |
|
113 |
* Tests exceptions for invalid arguments or scenarios |
|
114 |
*/ |
|
54947
69d1dff1bb70
8224019: test/jdk/java/nio/channels/DatagramChannel/BasicMulticastTests.java assumes IPv4 is always available
aeubanks
parents:
47216
diff
changeset
|
115 |
static void exceptionTests(StandardProtocolFamily family, |
69d1dff1bb70
8224019: test/jdk/java/nio/channels/DatagramChannel/BasicMulticastTests.java assumes IPv4 is always available
aeubanks
parents:
47216
diff
changeset
|
116 |
InetAddress group, |
69d1dff1bb70
8224019: test/jdk/java/nio/channels/DatagramChannel/BasicMulticastTests.java assumes IPv4 is always available
aeubanks
parents:
47216
diff
changeset
|
117 |
InetAddress notGroup, |
69d1dff1bb70
8224019: test/jdk/java/nio/channels/DatagramChannel/BasicMulticastTests.java assumes IPv4 is always available
aeubanks
parents:
47216
diff
changeset
|
118 |
NetworkInterface nif, |
69d1dff1bb70
8224019: test/jdk/java/nio/channels/DatagramChannel/BasicMulticastTests.java assumes IPv4 is always available
aeubanks
parents:
47216
diff
changeset
|
119 |
InetAddress loopback) |
1152 | 120 |
throws IOException |
121 |
{ |
|
55264
b7ad292e686c
8224645: Only test multicast interfaces if they exist
aeubanks
parents:
54947
diff
changeset
|
122 |
System.out.format("Exception Tests using %s, %s @ %s\n", |
b7ad292e686c
8224645: Only test multicast interfaces if they exist
aeubanks
parents:
54947
diff
changeset
|
123 |
group.getHostAddress(), |
b7ad292e686c
8224645: Only test multicast interfaces if they exist
aeubanks
parents:
54947
diff
changeset
|
124 |
notGroup.getHostAddress(), |
b7ad292e686c
8224645: Only test multicast interfaces if they exist
aeubanks
parents:
54947
diff
changeset
|
125 |
nif.getName()); |
1152 | 126 |
|
54947
69d1dff1bb70
8224019: test/jdk/java/nio/channels/DatagramChannel/BasicMulticastTests.java assumes IPv4 is always available
aeubanks
parents:
47216
diff
changeset
|
127 |
DatagramChannel dc = DatagramChannel.open(family) |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
5506
diff
changeset
|
128 |
.setOption(StandardSocketOptions.SO_REUSEADDR, true) |
1152 | 129 |
.bind(new InetSocketAddress(0)); |
130 |
||
131 |
// IllegalStateException |
|
132 |
MembershipKey key; |
|
133 |
key = dc.join(group, nif); |
|
134 |
try { |
|
54947
69d1dff1bb70
8224019: test/jdk/java/nio/channels/DatagramChannel/BasicMulticastTests.java assumes IPv4 is always available
aeubanks
parents:
47216
diff
changeset
|
135 |
dc.join(group, nif, loopback); |
1152 | 136 |
throw new RuntimeException("IllegalStateException not thrown"); |
137 |
} catch (IllegalStateException x) { |
|
138 |
} catch (UnsupportedOperationException x) { |
|
139 |
} |
|
140 |
key.drop(); |
|
141 |
try { |
|
54947
69d1dff1bb70
8224019: test/jdk/java/nio/channels/DatagramChannel/BasicMulticastTests.java assumes IPv4 is always available
aeubanks
parents:
47216
diff
changeset
|
142 |
key = dc.join(group, nif, loopback); |
1152 | 143 |
try { |
144 |
dc.join(group, nif); |
|
145 |
throw new RuntimeException("IllegalStateException not thrown"); |
|
146 |
} catch (IllegalStateException x) { |
|
147 |
} |
|
148 |
key.drop(); |
|
149 |
} catch (UnsupportedOperationException x) { |
|
150 |
} |
|
151 |
||
152 |
// IllegalArgumentException |
|
153 |
try { |
|
154 |
dc.join(notGroup, nif); |
|
155 |
throw new RuntimeException("IllegalArgumentException not thrown"); |
|
156 |
} catch (IllegalArgumentException x) { |
|
157 |
} |
|
158 |
try { |
|
54947
69d1dff1bb70
8224019: test/jdk/java/nio/channels/DatagramChannel/BasicMulticastTests.java assumes IPv4 is always available
aeubanks
parents:
47216
diff
changeset
|
159 |
dc.join(notGroup, nif, loopback); |
1152 | 160 |
throw new RuntimeException("IllegalArgumentException not thrown"); |
161 |
} catch (IllegalArgumentException x) { |
|
162 |
} catch (UnsupportedOperationException x) { |
|
163 |
} |
|
164 |
||
165 |
// NullPointerException |
|
166 |
try { |
|
167 |
dc.join(null, nif); |
|
168 |
throw new RuntimeException("NullPointerException not thrown"); |
|
169 |
} catch (NullPointerException x) { |
|
170 |
} |
|
171 |
try { |
|
172 |
dc.join(group, null); |
|
173 |
throw new RuntimeException("NullPointerException not thrown"); |
|
174 |
} catch (NullPointerException x) { |
|
175 |
} |
|
176 |
try { |
|
177 |
dc.join(group, nif, null); |
|
178 |
throw new RuntimeException("NullPointerException not thrown"); |
|
179 |
} catch (NullPointerException x) { |
|
180 |
} catch (UnsupportedOperationException x) { |
|
181 |
} |
|
182 |
||
183 |
dc.close(); |
|
184 |
||
185 |
// ClosedChannelException |
|
186 |
try { |
|
187 |
dc.join(group, nif); |
|
188 |
throw new RuntimeException("ClosedChannelException not thrown"); |
|
189 |
} catch (ClosedChannelException x) { |
|
190 |
} |
|
191 |
try { |
|
54947
69d1dff1bb70
8224019: test/jdk/java/nio/channels/DatagramChannel/BasicMulticastTests.java assumes IPv4 is always available
aeubanks
parents:
47216
diff
changeset
|
192 |
dc.join(group, nif, loopback); |
1152 | 193 |
throw new RuntimeException("ClosedChannelException not thrown"); |
194 |
} catch (ClosedChannelException x) { |
|
195 |
} catch (UnsupportedOperationException x) { |
|
196 |
} |
|
197 |
} |
|
198 |
||
199 |
||
200 |
/** |
|
201 |
* Probe interfaces to get interfaces that support IPv4 or IPv6 multicasting |
|
202 |
* and invoke tests. |
|
203 |
*/ |
|
204 |
public static void main(String[] args) throws IOException { |
|
55264
b7ad292e686c
8224645: Only test multicast interfaces if they exist
aeubanks
parents:
54947
diff
changeset
|
205 |
NetworkConfiguration.printSystemConfiguration(System.out); |
b7ad292e686c
8224645: Only test multicast interfaces if they exist
aeubanks
parents:
54947
diff
changeset
|
206 |
|
1152 | 207 |
NetworkConfiguration config = NetworkConfiguration.probe(); |
208 |
||
54947
69d1dff1bb70
8224019: test/jdk/java/nio/channels/DatagramChannel/BasicMulticastTests.java assumes IPv4 is always available
aeubanks
parents:
47216
diff
changeset
|
209 |
// test IPv4 if available |
55264
b7ad292e686c
8224645: Only test multicast interfaces if they exist
aeubanks
parents:
54947
diff
changeset
|
210 |
{ |
b7ad292e686c
8224645: Only test multicast interfaces if they exist
aeubanks
parents:
54947
diff
changeset
|
211 |
var multicastInterfaces = config.ip4MulticastInterfaces().iterator(); |
54947
69d1dff1bb70
8224019: test/jdk/java/nio/channels/DatagramChannel/BasicMulticastTests.java assumes IPv4 is always available
aeubanks
parents:
47216
diff
changeset
|
212 |
InetAddress group = InetAddress.getByName("225.4.5.6"); |
69d1dff1bb70
8224019: test/jdk/java/nio/channels/DatagramChannel/BasicMulticastTests.java assumes IPv4 is always available
aeubanks
parents:
47216
diff
changeset
|
213 |
InetAddress notGroup = InetAddress.getByName("1.2.3.4"); |
69d1dff1bb70
8224019: test/jdk/java/nio/channels/DatagramChannel/BasicMulticastTests.java assumes IPv4 is always available
aeubanks
parents:
47216
diff
changeset
|
214 |
InetAddress loopback = InetAddress.getByName("127.0.0.1"); |
55264
b7ad292e686c
8224645: Only test multicast interfaces if they exist
aeubanks
parents:
54947
diff
changeset
|
215 |
while (multicastInterfaces.hasNext()) { |
b7ad292e686c
8224645: Only test multicast interfaces if they exist
aeubanks
parents:
54947
diff
changeset
|
216 |
NetworkInterface nif = multicastInterfaces.next(); |
b7ad292e686c
8224645: Only test multicast interfaces if they exist
aeubanks
parents:
54947
diff
changeset
|
217 |
InetAddress anySource = config.ip4Addresses(nif).iterator().next(); |
b7ad292e686c
8224645: Only test multicast interfaces if they exist
aeubanks
parents:
54947
diff
changeset
|
218 |
membershipKeyTests(StandardProtocolFamily.INET, group, nif, anySource); |
b7ad292e686c
8224645: Only test multicast interfaces if they exist
aeubanks
parents:
54947
diff
changeset
|
219 |
exceptionTests(StandardProtocolFamily.INET, group, notGroup, nif, loopback); |
b7ad292e686c
8224645: Only test multicast interfaces if they exist
aeubanks
parents:
54947
diff
changeset
|
220 |
} |
54947
69d1dff1bb70
8224019: test/jdk/java/nio/channels/DatagramChannel/BasicMulticastTests.java assumes IPv4 is always available
aeubanks
parents:
47216
diff
changeset
|
221 |
} |
1152 | 222 |
|
54947
69d1dff1bb70
8224019: test/jdk/java/nio/channels/DatagramChannel/BasicMulticastTests.java assumes IPv4 is always available
aeubanks
parents:
47216
diff
changeset
|
223 |
// test IPv6 if available |
55264
b7ad292e686c
8224645: Only test multicast interfaces if they exist
aeubanks
parents:
54947
diff
changeset
|
224 |
{ |
b7ad292e686c
8224645: Only test multicast interfaces if they exist
aeubanks
parents:
54947
diff
changeset
|
225 |
var multicastInterfaces = config.ip6MulticastInterfaces().iterator(); |
54947
69d1dff1bb70
8224019: test/jdk/java/nio/channels/DatagramChannel/BasicMulticastTests.java assumes IPv4 is always available
aeubanks
parents:
47216
diff
changeset
|
226 |
InetAddress group = InetAddress.getByName("ff02::a"); |
69d1dff1bb70
8224019: test/jdk/java/nio/channels/DatagramChannel/BasicMulticastTests.java assumes IPv4 is always available
aeubanks
parents:
47216
diff
changeset
|
227 |
InetAddress notGroup = InetAddress.getByName("fe80::1234"); |
69d1dff1bb70
8224019: test/jdk/java/nio/channels/DatagramChannel/BasicMulticastTests.java assumes IPv4 is always available
aeubanks
parents:
47216
diff
changeset
|
228 |
InetAddress loopback = InetAddress.getByName("::1"); |
55264
b7ad292e686c
8224645: Only test multicast interfaces if they exist
aeubanks
parents:
54947
diff
changeset
|
229 |
while (multicastInterfaces.hasNext()) { |
b7ad292e686c
8224645: Only test multicast interfaces if they exist
aeubanks
parents:
54947
diff
changeset
|
230 |
NetworkInterface nif = multicastInterfaces.next(); |
b7ad292e686c
8224645: Only test multicast interfaces if they exist
aeubanks
parents:
54947
diff
changeset
|
231 |
InetAddress anySource = config.ip6Addresses(nif).iterator().next(); |
b7ad292e686c
8224645: Only test multicast interfaces if they exist
aeubanks
parents:
54947
diff
changeset
|
232 |
membershipKeyTests(StandardProtocolFamily.INET6, group, nif, anySource); |
b7ad292e686c
8224645: Only test multicast interfaces if they exist
aeubanks
parents:
54947
diff
changeset
|
233 |
exceptionTests(StandardProtocolFamily.INET6, group, notGroup, nif, loopback); |
b7ad292e686c
8224645: Only test multicast interfaces if they exist
aeubanks
parents:
54947
diff
changeset
|
234 |
} |
1152 | 235 |
} |
236 |
} |
|
237 |
} |