524N/A/*
2362N/A * Copyright (c) 2007, 2009, Oracle and/or its affiliates. All rights reserved.
524N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
524N/A *
524N/A * This code is free software; you can redistribute it and/or modify it
524N/A * under the terms of the GNU General Public License version 2 only, as
524N/A * published by the Free Software Foundation.
524N/A *
524N/A * This code is distributed in the hope that it will be useful, but WITHOUT
524N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
524N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
524N/A * version 2 for more details (a copy is included in the LICENSE file that
524N/A * accompanied this code).
524N/A *
524N/A * You should have received a copy of the GNU General Public License version
524N/A * 2 along with this work; if not, write to the Free Software Foundation,
524N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
524N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
524N/A */
524N/A
524N/A/* @test
524N/A * @bug 4527345
524N/A * @summary Unit test for DatagramChannel's multicast support
524N/A * @build BasicMulticastTests NetworkConfiguration
1477N/A * @run main BasicMulticastTests
524N/A */
524N/A
524N/Aimport java.nio.ByteBuffer;
524N/Aimport java.nio.channels.*;
524N/Aimport java.net.*;
524N/Aimport java.util.*;
524N/Aimport java.io.IOException;
524N/A
524N/Apublic class BasicMulticastTests {
524N/A
524N/A /**
524N/A * Tests that existing membership key is returned by join methods and that
524N/A * membership key methods return the expected results
524N/A */
524N/A static void membershipKeyTests(NetworkInterface nif,
524N/A InetAddress group,
524N/A InetAddress source)
524N/A throws IOException
524N/A {
524N/A System.out.format("MembershipKey test using %s @ %s\n",
524N/A group.getHostAddress(), nif.getName());
524N/A
524N/A ProtocolFamily family = (group instanceof Inet4Address) ?
524N/A StandardProtocolFamily.INET : StandardProtocolFamily.INET6;
524N/A
524N/A DatagramChannel dc = DatagramChannel.open(family)
4216N/A .setOption(StandardSocketOptions.SO_REUSEADDR, true)
524N/A .bind(new InetSocketAddress(source, 0));
524N/A
524N/A // check existing key is returned
524N/A MembershipKey key = dc.join(group, nif);
524N/A MembershipKey other = dc.join(group, nif);
524N/A if (other != key) {
524N/A throw new RuntimeException("existing key not returned");
524N/A }
524N/A
524N/A // check key
524N/A if (!key.isValid())
524N/A throw new RuntimeException("key is not valid");
893N/A if (!key.group().equals(group))
524N/A throw new RuntimeException("group is incorrect");
893N/A if (!key.networkInterface().equals(nif))
524N/A throw new RuntimeException("network interface is incorrect");
893N/A if (key.sourceAddress() != null)
524N/A throw new RuntimeException("key is source specific");
524N/A
524N/A // drop membership
524N/A key.drop();
524N/A if (key.isValid()) {
524N/A throw new RuntimeException("key is still valid");
524N/A }
524N/A
524N/A // source-specific
524N/A try {
524N/A key = dc.join(group, nif, source);
524N/A other = dc.join(group, nif, source);
524N/A if (other != key) {
524N/A throw new RuntimeException("existing key not returned");
524N/A }
524N/A if (!key.isValid())
524N/A throw new RuntimeException("key is not valid");
893N/A if (!key.group().equals(group))
524N/A throw new RuntimeException("group is incorrect");
893N/A if (!key.networkInterface().equals(nif))
524N/A throw new RuntimeException("network interface is incorrect");
893N/A if (!key.sourceAddress().equals(source))
524N/A throw new RuntimeException("key's source address incorrect");
524N/A
524N/A // drop membership
524N/A key.drop();
524N/A if (key.isValid()) {
524N/A throw new RuntimeException("key is still valid");
524N/A }
524N/A } catch (UnsupportedOperationException x) {
524N/A }
524N/A
524N/A // done
524N/A dc.close();
524N/A }
524N/A
524N/A /**
524N/A * Tests exceptions for invalid arguments or scenarios
524N/A */
524N/A static void exceptionTests(NetworkInterface nif)
524N/A throws IOException
524N/A {
524N/A System.out.println("Exception Tests");
524N/A
524N/A DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET)
4216N/A .setOption(StandardSocketOptions.SO_REUSEADDR, true)
524N/A .bind(new InetSocketAddress(0));
524N/A
524N/A InetAddress group = InetAddress.getByName("225.4.5.6");
524N/A InetAddress notGroup = InetAddress.getByName("1.2.3.4");
524N/A InetAddress thisHost = InetAddress.getLocalHost();
524N/A
524N/A // IllegalStateException
524N/A MembershipKey key;
524N/A key = dc.join(group, nif);
524N/A try {
524N/A dc.join(group, nif, thisHost);
524N/A throw new RuntimeException("IllegalStateException not thrown");
524N/A } catch (IllegalStateException x) {
524N/A } catch (UnsupportedOperationException x) {
524N/A }
524N/A key.drop();
524N/A try {
524N/A key = dc.join(group, nif, thisHost);
524N/A try {
524N/A dc.join(group, nif);
524N/A throw new RuntimeException("IllegalStateException not thrown");
524N/A } catch (IllegalStateException x) {
524N/A }
524N/A key.drop();
524N/A } catch (UnsupportedOperationException x) {
524N/A }
524N/A
524N/A // IllegalArgumentException
524N/A try {
524N/A dc.join(notGroup, nif);
524N/A throw new RuntimeException("IllegalArgumentException not thrown");
524N/A } catch (IllegalArgumentException x) {
524N/A }
524N/A try {
524N/A dc.join(notGroup, nif, thisHost);
524N/A throw new RuntimeException("IllegalArgumentException not thrown");
524N/A } catch (IllegalArgumentException x) {
524N/A } catch (UnsupportedOperationException x) {
524N/A }
524N/A
524N/A // NullPointerException
524N/A try {
524N/A dc.join(null, nif);
524N/A throw new RuntimeException("NullPointerException not thrown");
524N/A } catch (NullPointerException x) {
524N/A }
524N/A try {
524N/A dc.join(group, null);
524N/A throw new RuntimeException("NullPointerException not thrown");
524N/A } catch (NullPointerException x) {
524N/A }
524N/A try {
524N/A dc.join(group, nif, null);
524N/A throw new RuntimeException("NullPointerException not thrown");
524N/A } catch (NullPointerException x) {
524N/A } catch (UnsupportedOperationException x) {
524N/A }
524N/A
524N/A dc.close();
524N/A
524N/A // ClosedChannelException
524N/A try {
524N/A dc.join(group, nif);
524N/A throw new RuntimeException("ClosedChannelException not thrown");
524N/A } catch (ClosedChannelException x) {
524N/A }
524N/A try {
524N/A dc.join(group, nif, thisHost);
524N/A throw new RuntimeException("ClosedChannelException not thrown");
524N/A } catch (ClosedChannelException x) {
524N/A } catch (UnsupportedOperationException x) {
524N/A }
524N/A }
524N/A
524N/A
524N/A /**
524N/A * Probe interfaces to get interfaces that support IPv4 or IPv6 multicasting
524N/A * and invoke tests.
524N/A */
524N/A public static void main(String[] args) throws IOException {
524N/A
524N/A // multicast groups used for the test
524N/A InetAddress ip4Group = InetAddress.getByName("225.4.5.6");
524N/A InetAddress ip6Group = InetAddress.getByName("ff02::a");
524N/A
524N/A
524N/A NetworkConfiguration config = NetworkConfiguration.probe();
524N/A
524N/A NetworkInterface nif = config.ip4Interfaces().iterator().next();
524N/A InetAddress anySource = config.ip4Addresses(nif).iterator().next();
524N/A membershipKeyTests(nif, ip4Group, anySource);
524N/A exceptionTests(nif);
524N/A
524N/A // re-run the membership key tests with IPv6 if available
524N/A
524N/A Iterator<NetworkInterface> iter = config.ip6Interfaces().iterator();
524N/A if (iter.hasNext()) {
524N/A nif = iter.next();
524N/A anySource = config.ip6Addresses(nif).iterator().next();
524N/A membershipKeyTests(nif, ip6Group, anySource);
524N/A }
524N/A }
524N/A}