1 /*
   2  * Copyright (c) 2001, 2002, Oracle and/or its affiliates. All rights reserved.
   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  *
  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 /*
  25  * @test
  26  * @bug 4686717
  27  * @summary Test MulticastSocket.setLoopbackMode
  28  * @library /test/lib
  29  * @build jdk.test.lib.NetworkConfiguration
  30  *        jdk.test.lib.Utils
  31  *        jdk.test.lib.Asserts
  32  *        jdk.test.lib.JDKToolFinder
  33  *        jdk.test.lib.JDKToolLauncher
  34  *        jdk.test.lib.Platform
  35  *        jdk.test.lib.process.*
  36  * @run main/othervm SetLoopbackMode
  37  */
  38 
  39 import java.net.*;
  40 import java.io.IOException;
  41 import java.util.Enumeration;
  42 import jdk.test.lib.NetworkConfiguration;
  43 
  44 public class SetLoopbackMode {
  45 
  46     static final boolean FAILED = true;
  47     static final boolean PASSED = false;
  48 
  49     static boolean test(MulticastSocket mc, InetAddress grp) throws IOException {
  50 
  51         boolean disabled = mc.getLoopbackMode();
  52 
  53         if (disabled) {
  54             System.out.println("Loopback mode is disabled.");
  55         } else {
  56             System.out.println("Loopback mode is enabled.");
  57         }
  58 
  59         byte b[] = "hello".getBytes();
  60         DatagramPacket p = new DatagramPacket(b, b.length, grp,
  61                                 mc.getLocalPort());
  62         mc.send(p);
  63 
  64         boolean gotPacket = false;
  65 
  66         mc.setSoTimeout(1000);
  67         try {
  68             b = new byte[16];
  69             p = new DatagramPacket(b, b.length);
  70             mc.receive(p);
  71             gotPacket = true;
  72 
  73             /* purge any additional copies of the packet */
  74             for (;;) {
  75                 mc.receive(p);
  76             }
  77 
  78         } catch (SocketTimeoutException x) {
  79         }
  80 
  81         if (gotPacket && disabled) {
  82             System.out.println("Packet received (unexpected as loopback is disabled)");
  83             return FAILED;
  84         }
  85         if (!gotPacket && !disabled) {
  86             System.out.println
  87                 ("Packet not received (packet excepted as loopback is enabled)");
  88             return FAILED;
  89         }
  90 
  91         if (gotPacket && !disabled) {
  92             System.out.println("Packet received - correct.");
  93         } else {
  94             System.out.println("Packet not received - correct.");
  95         }
  96 
  97         return PASSED;
  98     }
  99 
 100     private static boolean canUseIPv6(NetworkConfiguration nc) {
 101         return nc.ip6MulticastInterfaces().toArray().length > 0;
 102     }
 103 
 104     public static void main (String args[]) throws Exception {
 105         int failures = 0;
 106         NetworkConfiguration nc = NetworkConfiguration.probe();
 107 
 108         MulticastSocket mc = new MulticastSocket();
 109         InetAddress grp = InetAddress.getByName("224.80.80.80");
 110 
 111 
 112         /*
 113          * If IPv6 is available then use IPv6 multicast group - needed
 114          * to workaround Linux IPv6 bug whereby !IPV6_MULTICAST_LOOP
 115          * doesn't prevent loopback of IPv4 multicast packets.
 116          */
 117 
 118         if (canUseIPv6(nc)) {
 119             grp = InetAddress.getByName("ff01::1");
 120         }
 121 
 122         //mc.setNetworkInterface(NetworkInterface.getByInetAddress(lb));
 123         System.out.println("\nTest will use multicast group: " + grp);
 124         mc.joinGroup(grp);
 125 
 126         System.out.println("\n******************\n");
 127 
 128         mc.setLoopbackMode(true);
 129         if (test(mc, grp) == FAILED) failures++;
 130 
 131         System.out.println("\n******************\n");
 132 
 133         mc.setLoopbackMode(false);
 134         if (test(mc, grp) == FAILED) failures++;
 135 
 136         System.out.println("\n******************\n");
 137 
 138         if (failures > 0) {
 139             throw new RuntimeException("Test failed");
 140         }
 141     }
 142 }