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 * @test
0N/A * @bug 4488458
0N/A * @summary IPv4 and IPv6 multicasting broken on Linux
0N/A */
0N/Aimport java.net.*;
0N/Aimport java.io.IOException;
0N/Aimport java.util.Enumeration;
0N/A
0N/Apublic class Test {
0N/A
0N/A static int count = 0;
0N/A static int failures = 0;
0N/A
0N/A void doTest(String address) throws Exception {
0N/A boolean failed = false;
0N/A
0N/A InetAddress ia = InetAddress.getByName(address);
0N/A
0N/A count++;
0N/A System.out.println("**********************");
0N/A System.out.println("Test " + count + ": " + ia);
0N/A
0N/A MulticastSocket mc = new MulticastSocket();
0N/A int port = mc.getLocalPort();
0N/A DatagramSocket s1 = new DatagramSocket();
0N/A
0N/A byte msg[] = "Hello".getBytes();
0N/A DatagramPacket p = new DatagramPacket(msg, msg.length);
0N/A
0N/A mc.setSoTimeout(2000);
0N/A
0N/A try {
0N/A for (int i=0; i<2; i++) {
0N/A
0N/A System.out.println("Join: " + ia);
0N/A mc.joinGroup(ia);
0N/A
0N/A /* packets should be received */
0N/A
0N/A for (int j=0; j<2; j++) {
0N/A p.setAddress(ia);
0N/A p.setPort(port);
0N/A
0N/A System.out.println("Send packet to: " + ia);
0N/A s1.send(p);
0N/A
0N/A try {
0N/A mc.receive(p);
0N/A System.out.println("Got packet! - Good.");
0N/A } catch (SocketTimeoutException e) {
0N/A failed = true;
0N/A System.out.println("Failed: No packet received within timeout!!!");
0N/A }
0N/A }
0N/A
0N/A System.out.println("Leave: " + ia);
0N/A mc.leaveGroup(ia);
0N/A
0N/A /*
0N/A * If there are multiple interface we might be a couple of
0N/A * copies still in our queue
0N/A */
0N/A try {
0N/A while (true) {
0N/A mc.receive(p);
0N/A }
0N/A } catch (SocketTimeoutException e) { }
0N/A
0N/A /* packets should not be received */
0N/A
0N/A p.setAddress(ia);
0N/A p.setPort(port);
0N/A
0N/A s1.send(p);
0N/A
0N/A try {
0N/A mc.receive(p);
0N/A System.out.println("Failed: Got packet after leaving group!!!");
0N/A failed = true;
0N/A } catch (SocketTimeoutException e) {
0N/A System.out.println("No packet received within timeout! - Good.");
0N/A }
0N/A }
0N/A
0N/A } catch (IOException ioe) {
0N/A System.out.println("Failed: Unexpected exception thrown: ");
0N/A ioe.printStackTrace();
0N/A failed = true;
0N/A }
0N/A
0N/A mc.close();
0N/A s1.close();
0N/A
0N/A if (failed) {
0N/A failures++;
0N/A System.out.println("Test failed!!");
0N/A } else {
0N/A System.out.println("Test passed.");
0N/A }
0N/A }
0N/A
0N/A void allTests() throws Exception {
0N/A
0N/A /*
0N/A * Assume machine has IPv4 address
0N/A */
0N/A doTest("224.80.80.80");
0N/A
0N/A /*
0N/A * Check if IPv6 is enabled and the scope of the addresses
0N/A */
0N/A boolean has_ipv6 = false;
0N/A boolean has_siteaddress = false;
0N/A boolean has_linklocaladdress = false;
0N/A boolean has_globaladdress = false;
0N/A
0N/A Enumeration nifs = NetworkInterface.getNetworkInterfaces();
0N/A while (nifs.hasMoreElements()) {
0N/A NetworkInterface ni = (NetworkInterface)nifs.nextElement();
0N/A Enumeration addrs = ni.getInetAddresses();
0N/A
0N/A while (addrs.hasMoreElements()) {
0N/A InetAddress ia = (InetAddress)addrs.nextElement();
0N/A
0N/A if (ia instanceof Inet6Address) {
0N/A has_ipv6 = true;
0N/A if (ia.isLinkLocalAddress()) has_linklocaladdress = true;
0N/A if (ia.isSiteLocalAddress()) has_siteaddress = true;
0N/A
0N/A if (!ia.isLinkLocalAddress() &&
0N/A !ia.isSiteLocalAddress() &&
0N/A !ia.isLoopbackAddress()) {
0N/A has_globaladdress = true;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * If IPv6 is enabled perform multicast tests with various scopes
0N/A */
0N/A if (has_ipv6) {
0N/A doTest("ff01::a");
0N/A }
0N/A
0N/A if (has_linklocaladdress) {
0N/A doTest("ff02::a");
0N/A }
0N/A
0N/A if (has_siteaddress) {
0N/A doTest("ff05::a");
0N/A }
0N/A
0N/A if (has_globaladdress) {
0N/A doTest("ff0e::a");
0N/A }
0N/A }
0N/A
0N/A public static void main(String args[]) throws Exception {
0N/A Test t = new Test();
0N/A
0N/A if (args.length == 0) {
0N/A t.allTests();
0N/A } else {
0N/A for (int i=0; i<args.length; i++) {
0N/A t.doTest(args[i]);
0N/A }
0N/A }
0N/A
0N/A System.out.println("**********************");
0N/A System.out.println(count + " test(s) executed. " + failures +
0N/A " test(s) failed.");
0N/A
0N/A if (failures > 0) {
0N/A throw new Exception("Test failed - see log file for details");
0N/A }
0N/A }
0N/A}