0N/A/*
2362N/A * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/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.
0N/A */
0N/A
0N/A/*
0N/A *
0N/A * @bug 4488458
0N/A * @summary Test that MutlicastSocket.joinGroup is working for
0N/A * various multicast and non-multicast addresses.
0N/A */
0N/Aimport java.net.*;
0N/Aimport java.util.Enumeration;
0N/Aimport java.io.IOException;
0N/A
0N/Apublic class MulticastAddresses {
0N/A
0N/A public static void main(String args[]) throws Exception {
0N/A
0N/A boolean ipv6_available = false;
0N/A NetworkInterface ni = null;
0N/A
0N/A /*
0N/A * Examine the network interfaces and determine :-
0N/A *
0N/A * 1. If host has IPv6 support
0N/A * 2. Get reference to a non-loopback interface
0N/A */
0N/A Enumeration nifs = NetworkInterface.getNetworkInterfaces();
0N/A while (nifs.hasMoreElements()) {
0N/A NetworkInterface this_ni = (NetworkInterface)nifs.nextElement();
0N/A
0N/A Enumeration addrs = this_ni.getInetAddresses();
0N/A while (addrs.hasMoreElements()) {
0N/A InetAddress addr = (InetAddress)addrs.nextElement();
0N/A if (addr instanceof Inet6Address) {
0N/A ipv6_available = true;
0N/A }
0N/A
0N/A if (!addr.isLoopbackAddress() && ni == null) {
0N/A ni = this_ni;
0N/A }
0N/A }
0N/A
0N/A if (ipv6_available) {
0N/A break;
0N/A }
0N/A }
0N/A
0N/A int failures = 0;
0N/A
0N/A String multicasts[] = {
0N/A "224.80.80.80",
0N/A "ff01::1",
0N/A "ff02::1234",
0N/A "ff05::a",
0N/A "ff0e::1234:a" };
0N/A
0N/A String non_multicasts[] = {
0N/A "129.1.1.1",
0N/A "::1",
0N/A "::129.1.1.1",
0N/A "fe80::a00:20ff:fee5:bc02" };
0N/A
0N/A MulticastSocket s = new MulticastSocket();
0N/A
0N/A /* test valid multicast addresses */
0N/A
0N/A for (int i=0; i<multicasts.length; i++) {
0N/A InetAddress ia = InetAddress.getByName(multicasts[i]);
0N/A if (ia instanceof Inet6Address && !ipv6_available) {
0N/A continue;
0N/A }
0N/A
0N/A System.out.println("Test: " + ia);
0N/A
0N/A try {
0N/A
0N/A System.out.print(" joinGroup(InetAddress) ");
0N/A s.joinGroup(ia);
0N/A s.leaveGroup(ia);
0N/A System.out.println(" Passed.");
0N/A
0N/A System.out.print(" joinGroup(InetAddress,NetworkInterface) ");
0N/A s.joinGroup(new InetSocketAddress(ia,0), ni);
0N/A s.leaveGroup(new InetSocketAddress(ia,0), ni);
0N/A System.out.println(" Passed.");
0N/A } catch (IOException e) {
0N/A failures++;
0N/A System.out.println("Failed: " + e.getMessage());
0N/A }
0N/A
0N/A }
0N/A
0N/A /* test non-multicast addresses */
0N/A
0N/A for (int i=0; i<non_multicasts.length; i++) {
0N/A InetAddress ia = InetAddress.getByName(non_multicasts[i]);
0N/A if (ia instanceof Inet6Address && !ipv6_available) {
0N/A continue;
0N/A }
0N/A
0N/A boolean failed = false;
0N/A
0N/A System.out.println("Test: " + ia + " ");
0N/A try {
0N/A System.out.println(" joinGroup(InetAddress) ");
0N/A s.joinGroup(ia);
0N/A
0N/A System.out.println("Failed!! -- incorrectly joined group");
0N/A failed = true;
0N/A } catch (IOException e) {
0N/A System.out.println(" Passed: " + e.getMessage());
0N/A }
0N/A
0N/A if (failed) {
0N/A s.leaveGroup(ia);
0N/A failures++;
0N/A }
0N/A }
0N/A
0N/A /* done */
0N/A
0N/A s.close();
0N/A
0N/A if (failures > 0) {
0N/A throw new Exception(failures + " test(s) failed - see log file.");
0N/A }
0N/A }
0N/A
0N/A}