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 4527345 7026376
26 * @summary Unit test for DatagramChannel's multicast support
27 * @build MulticastSendReceiveTests NetworkConfiguration
28 * @run main MulticastSendReceiveTests
29 */
30
31 import java.nio.ByteBuffer;
32 import java.nio.channels.*;
33 import java.net.*;
34 import static java.net.StandardProtocolFamily.*;
35 import java.util.*;
36 import java.io.IOException;
37
38 public class MulticastSendReceiveTests {
39
40 static final Random rand = new Random();
41
42 static final ProtocolFamily UNSPEC = new ProtocolFamily() {
43 public String name() {
44 return "UNSPEC";
45 }
46 };
47
48 /**
169
170 // receive message and check id matches
171 receiveDatagram(dc, source, id);
172
173 // exclude-mode filtering
174
175 try {
176 System.out.format("block %s\n", source.getHostAddress());
177
178 // may throw UOE
179 key.block(source);
180 id = sendDatagram(source, nif, group, port);
181 receiveDatagram(dc, null, id);
182
183 // unblock source, send message, message should be received
184 System.out.format("unblock %s\n", source.getHostAddress());
185 key.unblock(source);
186 id = sendDatagram(source, nif, group, port);
187 receiveDatagram(dc, source, id);
188 } catch (UnsupportedOperationException x) {
189 System.out.println("Exclude-mode filtering not supported!");
190 }
191
192 key.drop();
193
194 // include-mode filtering
195
196 InetAddress bogus = (group instanceof Inet6Address) ?
197 InetAddress.getByName("fe80::1234") :
198 InetAddress.getByName("1.2.3.4");
199 System.out.format("join %s @ %s only-source %s\n", group.getHostAddress(),
200 nif.getName(), bogus.getHostAddress());
201 try {
202 // may throw UOE
203 key = dc.join(group, nif, bogus);
204
205 id = sendDatagram(source, nif, group, port);
206 receiveDatagram(dc, null, id);
207
208 System.out.format("join %s @ %s only-source %s\n", group.getHostAddress(),
209 nif.getName(), source.getHostAddress());
210 key = dc.join(group, nif, source);
211
212 id = sendDatagram(source, nif, group, port);
213 receiveDatagram(dc, source, id);
214 } catch (UnsupportedOperationException x) {
215 System.out.println("Include-mode filtering not supported!");
216 }
217 }
218 }
219
220 public static void main(String[] args) throws IOException {
221 NetworkConfiguration config = NetworkConfiguration.probe();
222
223 // multicast groups used for the test
224 InetAddress ip4Group = InetAddress.getByName("225.4.5.6");
225 InetAddress ip6Group = InetAddress.getByName("ff02::a");
226
227 for (NetworkInterface nif: config.ip4Interfaces()) {
228 InetAddress source = config.ip4Addresses(nif).iterator().next();
229 test(INET, nif, ip4Group, source);
230 test(UNSPEC, nif, ip4Group, source);
231 }
232
233 for (NetworkInterface nif: config.ip6Interfaces()) {
234 InetAddress source = config.ip6Addresses(nif).iterator().next();
|
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 4527345 7026376 6633549
26 * @summary Unit test for DatagramChannel's multicast support
27 * @build MulticastSendReceiveTests NetworkConfiguration
28 * @run main MulticastSendReceiveTests
29 * @run main/othervm -Djava.net.preferIPv4Stack=true MulticastSendReceiveTests
30 */
31
32 import java.nio.ByteBuffer;
33 import java.nio.channels.*;
34 import java.net.*;
35 import static java.net.StandardProtocolFamily.*;
36 import java.util.*;
37 import java.io.IOException;
38
39 public class MulticastSendReceiveTests {
40
41 static final Random rand = new Random();
42
43 static final ProtocolFamily UNSPEC = new ProtocolFamily() {
44 public String name() {
45 return "UNSPEC";
46 }
47 };
48
49 /**
170
171 // receive message and check id matches
172 receiveDatagram(dc, source, id);
173
174 // exclude-mode filtering
175
176 try {
177 System.out.format("block %s\n", source.getHostAddress());
178
179 // may throw UOE
180 key.block(source);
181 id = sendDatagram(source, nif, group, port);
182 receiveDatagram(dc, null, id);
183
184 // unblock source, send message, message should be received
185 System.out.format("unblock %s\n", source.getHostAddress());
186 key.unblock(source);
187 id = sendDatagram(source, nif, group, port);
188 receiveDatagram(dc, source, id);
189 } catch (UnsupportedOperationException x) {
190 String os = System.getProperty("os.name");
191 // Exclude-mode filtering supported on these platforms so UOE should never be thrown
192 if (os.equals("SunOS") || os.equals("Linux"))
193 throw x;
194 System.out.println("Exclude-mode filtering not supported!");
195 }
196
197 key.drop();
198
199 // include-mode filtering
200
201 InetAddress bogus = (group instanceof Inet6Address) ?
202 InetAddress.getByName("fe80::1234") :
203 InetAddress.getByName("1.2.3.4");
204 System.out.format("join %s @ %s only-source %s\n", group.getHostAddress(),
205 nif.getName(), bogus.getHostAddress());
206 try {
207 // may throw UOE
208 key = dc.join(group, nif, bogus);
209
210 id = sendDatagram(source, nif, group, port);
211 receiveDatagram(dc, null, id);
212
213 System.out.format("join %s @ %s only-source %s\n", group.getHostAddress(),
214 nif.getName(), source.getHostAddress());
215 key = dc.join(group, nif, source);
216
217 id = sendDatagram(source, nif, group, port);
218 receiveDatagram(dc, source, id);
219 } catch (UnsupportedOperationException x) {
220 String os = System.getProperty("os.name");
221 // Include-mode filtering supported on these platforms so UOE should never be thrown
222 if (os.equals("SunOS") || os.equals("Linux"))
223 throw x;
224 System.out.println("Include-mode filtering not supported!");
225 }
226 }
227 }
228
229 public static void main(String[] args) throws IOException {
230 NetworkConfiguration config = NetworkConfiguration.probe();
231
232 // multicast groups used for the test
233 InetAddress ip4Group = InetAddress.getByName("225.4.5.6");
234 InetAddress ip6Group = InetAddress.getByName("ff02::a");
235
236 for (NetworkInterface nif: config.ip4Interfaces()) {
237 InetAddress source = config.ip4Addresses(nif).iterator().next();
238 test(INET, nif, ip4Group, source);
239 test(UNSPEC, nif, ip4Group, source);
240 }
241
242 for (NetworkInterface nif: config.ip6Interfaces()) {
243 InetAddress source = config.ip6Addresses(nif).iterator().next();
|