test/jdk/java/net/MulticastSocket/NoLoopbackPackets.java
changeset 54770 62b6e7587b1f
parent 47216 71c04702a3d5
child 55649 ad8e3b295615
child 58678 9cf78a70fa4f
equal deleted inserted replaced
54769:925ee7a89325 54770:62b6e7587b1f
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 4742177
    26  * @bug 4742177
       
    27  * @library /test/lib
    27  * @summary Re-test IPv6 (and specifically MulticastSocket) with latest Linux & USAGI code
    28  * @summary Re-test IPv6 (and specifically MulticastSocket) with latest Linux & USAGI code
    28  */
    29  */
    29 import java.util.*;
    30 import java.util.*;
    30 import java.net.*;
    31 import java.net.*;
       
    32 import jdk.test.lib.net.IPSupport;
    31 
    33 
    32 public class NoLoopbackPackets {
    34 public class NoLoopbackPackets {
    33     private static String osname;
    35     private static String osname;
    34 
    36 
    35     static boolean isWindows() {
    37     static boolean isWindows() {
    36         if (osname == null)
    38         if (osname == null)
    37             osname = System.getProperty("os.name");
    39             osname = System.getProperty("os.name");
    38         return osname.contains("Windows");
    40         return osname.contains("Windows");
    39     }
    41     }
    40 
    42 
    41     private static boolean hasIPv6() throws Exception {
       
    42         List<NetworkInterface> nics = Collections.list(
       
    43                                         NetworkInterface.getNetworkInterfaces());
       
    44         for (NetworkInterface nic : nics) {
       
    45             if (!nic.isLoopback()) {
       
    46                 List<InetAddress> addrs = Collections.list(nic.getInetAddresses());
       
    47                 for (InetAddress addr : addrs) {
       
    48                     if (addr instanceof Inet6Address) {
       
    49                         return true;
       
    50                     }
       
    51                 }
       
    52             }
       
    53         }
       
    54 
       
    55         return false;
       
    56     }
       
    57 
       
    58     public static void main(String[] args) throws Exception {
    43     public static void main(String[] args) throws Exception {
    59         if (isWindows()) {
    44         if (isWindows()) {
    60             System.out.println("The test only run on non-Windows OS. Bye.");
    45             System.out.println("The test only run on non-Windows OS. Bye.");
    61             return;
       
    62         }
       
    63 
       
    64         if (!hasIPv6()) {
       
    65             System.out.println("No IPv6 available. Bye.");
       
    66             return;
    46             return;
    67         }
    47         }
    68 
    48 
    69         MulticastSocket msock = null;
    49         MulticastSocket msock = null;
    70         List<SocketAddress> failedGroups = new ArrayList<SocketAddress>();
    50         List<SocketAddress> failedGroups = new ArrayList<SocketAddress>();
    75 
    55 
    76             // we will send packets to three multicast groups :-
    56             // we will send packets to three multicast groups :-
    77             // 224.1.1.1, ::ffff:224.1.1.2, and ff02::1:1
    57             // 224.1.1.1, ::ffff:224.1.1.2, and ff02::1:1
    78             //
    58             //
    79             List<SocketAddress> groups = new ArrayList<SocketAddress>();
    59             List<SocketAddress> groups = new ArrayList<SocketAddress>();
    80             groups.add(new InetSocketAddress(InetAddress.getByName("224.1.1.1"), port));
    60             if (IPSupport.hasIPv4()) {
    81             groups.add(new InetSocketAddress(InetAddress.getByName("::ffff:224.1.1.2"), port));
    61                 groups.add(new InetSocketAddress(InetAddress.getByName("224.1.1.1"), port));
    82             groups.add(new InetSocketAddress(InetAddress.getByName("ff02::1:1"), port));
    62             }
       
    63             if (IPSupport.hasIPv6()) {
       
    64                 groups.add(new InetSocketAddress(InetAddress.getByName("::ffff:224.1.1.2"), port));
       
    65                 groups.add(new InetSocketAddress(InetAddress.getByName("ff02::1:1"), port));
       
    66             }
       
    67             if (groups.isEmpty()) {
       
    68                 System.err.println("Nothing to test: there are no network interfaces");
       
    69             }
    83 
    70 
    84             sender = new Sender(groups);
    71             sender = new Sender(groups);
    85             new Thread(sender).start();
    72             new Thread(sender).start();
    86 
    73 
    87             // Now try to receive multicast packets. we should not see any of them
    74             // Now try to receive multicast packets. we should not see any of them