test/jdk/java/net/MulticastSocket/Test.java
changeset 53426 1b665a4f343a
parent 47216 71c04702a3d5
child 54770 62b6e7587b1f
equal deleted inserted replaced
53425:312880c38a7f 53426:1b665a4f343a
     1 /*
     1 /*
     2  * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    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
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 import java.io.IOException;
       
    25 import java.net.DatagramPacket;
       
    26 import java.net.DatagramSocket;
       
    27 import java.net.InetAddress;
       
    28 import java.net.MulticastSocket;
       
    29 import java.net.SocketTimeoutException;
       
    30 
       
    31 import jdk.test.lib.NetworkConfiguration;
       
    32 
       
    33 /**
    25  * @test
    34  * @test
    26  * @bug 4488458
    35  * @bug 4488458
    27  * @summary IPv4 and IPv6 multicasting broken on Linux
    36  * @summary IPv4 and IPv6 multicasting broken on Linux
       
    37  * @library /test/lib
       
    38  * @build jdk.test.lib.NetworkConfiguration
       
    39  *        jdk.test.lib.Platform
       
    40  * @run main Test
       
    41  * @run main/othervm -Djava.net.preferIPv4Stack=true Test
    28  */
    42  */
    29 import java.net.*;
       
    30 import java.io.IOException;
       
    31 import java.util.Enumeration;
       
    32 
       
    33 public class Test {
    43 public class Test {
    34 
    44 
    35     static int count = 0;
    45     static int count = 0;
    36     static int failures = 0;
    46     static int failures = 0;
    37 
    47 
    38     static boolean isSolaris = System.getProperty("os.name")
    48     void doTest(String address) throws IOException {
    39         .toLowerCase()
       
    40         .startsWith("sunos");
       
    41 
       
    42     void doTest(String address) throws Exception {
       
    43         boolean failed = false;
    49         boolean failed = false;
    44 
    50 
    45         InetAddress ia = InetAddress.getByName(address);
    51         InetAddress ia = InetAddress.getByName(address);
    46 
    52 
    47         count++;
    53         count++;
    63                 System.out.println("Join: " + ia);
    69                 System.out.println("Join: " + ia);
    64                 mc.joinGroup(ia);
    70                 mc.joinGroup(ia);
    65 
    71 
    66                 /* packets should be received */
    72                 /* packets should be received */
    67 
    73 
    68                 for (int j=0; j<2; j++) {
    74                 for (int j = 0; j < 2; j++) {
    69                     p.setAddress(ia);
    75                     p.setAddress(ia);
    70                     p.setPort(port);
    76                     p.setPort(port);
    71 
    77 
    72                     System.out.println("Send packet to: " + ia);
    78                     System.out.println("Send packet to: " + ia);
    73                     s1.send(p);
    79                     s1.send(p);
   125         } else {
   131         } else {
   126             System.out.println("Test passed.");
   132             System.out.println("Test passed.");
   127         }
   133         }
   128     }
   134     }
   129 
   135 
   130     static boolean isValidIpv6Address(InetAddress addr) {
   136     void allTests() throws IOException {
   131         if (! (addr instanceof Inet6Address))
   137         NetworkConfiguration nc = NetworkConfiguration.probe();
   132             return false;
       
   133         if (!isSolaris)
       
   134             return true;
       
   135         return !(addr.isAnyLocalAddress() || addr.isLoopbackAddress());
       
   136     }
       
   137 
   138 
   138     void allTests() throws Exception {
   139         // unconditionally test IPv4 address
   139 
       
   140         /*
       
   141          * Assume machine has IPv4 address
       
   142          */
       
   143         doTest("224.80.80.80");
   140         doTest("224.80.80.80");
   144 
   141 
   145         /*
   142         // If IPv6 is enabled perform multicast tests with various scopes
   146          * Check if IPv6 is enabled and the scope of the addresses
   143         if (nc.hasTestableIPv6Address()) {
   147          */
       
   148         boolean has_ipv6 = false;
       
   149         boolean has_siteaddress = false;
       
   150         boolean has_linklocaladdress = false;
       
   151         boolean has_globaladdress = false;
       
   152 
       
   153         Enumeration nifs = NetworkInterface.getNetworkInterfaces();
       
   154         while (nifs.hasMoreElements()) {
       
   155             NetworkInterface ni = (NetworkInterface)nifs.nextElement();
       
   156             Enumeration addrs = ni.getInetAddresses();
       
   157 
       
   158             while (addrs.hasMoreElements()) {
       
   159                 InetAddress ia = (InetAddress)addrs.nextElement();
       
   160 
       
   161                 if (isValidIpv6Address(ia)) {
       
   162                     has_ipv6 = true;
       
   163                     if (ia.isLinkLocalAddress()) has_linklocaladdress = true;
       
   164                     if (ia.isSiteLocalAddress()) has_siteaddress = true;
       
   165 
       
   166                     if (!ia.isLinkLocalAddress() &&
       
   167                         !ia.isSiteLocalAddress() &&
       
   168                         !ia.isLoopbackAddress()) {
       
   169                         has_globaladdress = true;
       
   170                     }
       
   171                 }
       
   172             }
       
   173         }
       
   174 
       
   175         /*
       
   176          * If IPv6 is enabled perform multicast tests with various scopes
       
   177          */
       
   178         if (has_ipv6) {
       
   179             doTest("ff01::a");
   144             doTest("ff01::a");
   180         }
   145         }
   181 
   146 
   182         if (has_linklocaladdress) {
   147         if (nc.hasLinkLocalAddress()) {
   183             doTest("ff02::a");
   148             doTest("ff02::a");
   184         }
   149         }
   185 
   150 
   186         if (has_siteaddress) {
   151         if (nc.hasSiteLocalAddress()) {
   187             doTest("ff05::a");
   152             doTest("ff05::a");
   188         }
   153         }
   189 
   154 
   190         if (has_globaladdress) {
   155         if (nc.has_globaladdress()) {
   191             doTest("ff0e::a");
   156             doTest("ff0e::a");
   192         }
   157         }
   193     }
   158     }
   194 
   159 
   195     public static void main(String args[]) throws Exception {
   160     public static void main(String args[]) throws Exception {
   196         Test t = new Test();
   161         Test t = new Test();
   197 
   162 
   198         if (args.length == 0) {
   163         if (args.length == 0) {
   199             t.allTests();
   164             t.allTests();
   200         } else {
   165         } else {
   201             for (int i=0; i<args.length; i++) {
   166             for (int i = 0; i < args.length; i++) {
   202                 t.doTest(args[i]);
   167                 t.doTest(args[i]);
   203             }
   168             }
   204         }
   169         }
   205 
   170 
   206         System.out.println("**********************");
   171         System.out.println("**********************");