< prev index next >

test/java/net/MulticastSocket/SetOutgoingIf.java

Print this page
rev 17065 : 7155591: test/java/net/MulticastSocket/SetOutgoingIf.java fails on macOS
Reviewed-by:
   1 /*
   2  * Copyright (c) 2007, 2010, 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 4742177
  27  * @summary Re-test IPv6 (and specifically MulticastSocket) with latest Linux & USAGI code



  28  */
  29 import java.net.*;
  30 import java.util.*;
  31 

  32 
  33 public class SetOutgoingIf {
  34     private static int PORT = 9001;
  35     private static String osname;
  36 
  37     static boolean isWindows() {
  38         if (osname == null)
  39             osname = System.getProperty("os.name");
  40         return osname.contains("Windows");
  41     }
  42 
  43     private static boolean hasIPv6() throws Exception {
  44         List<NetworkInterface> nics = Collections.list(
  45                                         NetworkInterface.getNetworkInterfaces());
  46         for (NetworkInterface nic : nics) {
  47             List<InetAddress> addrs = Collections.list(nic.getInetAddresses());
  48             for (InetAddress addr : addrs) {
  49                 if (addr instanceof Inet6Address)
  50                     return true;
  51             }
  52         }
  53 
  54         return false;
  55     }
  56 
  57     public static void main(String[] args) throws Exception {
  58         if (isWindows()) {
  59             System.out.println("The test only run on non-Windows OS. Bye.");
  60             return;
  61         }
  62 
  63         if (!hasIPv6()) {
  64             System.out.println("No IPv6 available. Bye.");
  65             return;
  66         }
  67 


  68         // We need 2 or more network interfaces to run the test
  69         //
  70         List<NetIf> netIfs = new ArrayList<NetIf>();
  71         int index = 1;
  72         for (NetworkInterface nic : Collections.list(NetworkInterface.getNetworkInterfaces())) {
  73             // we should use only network interfaces with multicast support which are in "up" state
  74             if (!nic.isLoopback() && nic.supportsMulticast() && nic.isUp()) {
  75                 NetIf netIf = NetIf.create(nic);
  76 
  77                 // now determine what (if any) type of addresses are assigned to this interface
  78                 for (InetAddress addr : Collections.list(nic.getInetAddresses())) {
  79                     if (addr.isAnyLocalAddress())
  80                         continue;
  81 
  82                     System.out.println("    addr " + addr);
  83                     if (addr instanceof Inet4Address) {
  84                         netIf.ipv4Address(true);
  85                     } else if (addr instanceof Inet6Address) {
  86                         netIf.ipv6Address(true);
  87                     }
  88                 }
  89                 if (netIf.ipv4Address() || netIf.ipv6Address()) {
  90                     netIf.index(index++);
  91                     netIfs.add(netIf);
  92                     debug("Using: " + nic);
  93                 }
  94             }
  95         }
  96         if (netIfs.size() <= 1) {
  97             System.out.println("Need 2 or more network interfaces to run. Bye.");
  98             return;
  99         }
 100 
 101         // We will send packets to one ipv4, and one ipv6
 102         // multicast group using each network interface :-
 103         //      224.1.1.1        --|
 104         //      ff02::1:1        --|--> using network interface #1
 105         //      224.1.2.1        --|
 106         //      ff02::1:2        --|--> using network interface #2
 107         // and so on.
 108         //
 109         for (NetIf netIf : netIfs) {
 110             int NetIfIndex = netIf.index();
 111             List<InetAddress> groups = new ArrayList<InetAddress>();
 112 


   1 /*
   2  * Copyright (c) 2007, 2017, 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 4742177
  27  * @summary Re-test IPv6 (and specifically MulticastSocket) with latest Linux & USAGI code
  28  * @library /lib/testlibrary
  29  * @build jdk.testlibrary.NetworkConfiguration
  30  * @run main SetOutgoingIf
  31  */
  32 import java.net.*;
  33 import java.util.*;
  34 import jdk.testlibrary.NetworkConfiguration;
  35 import static java.util.stream.Collectors.toList;
  36 
  37 public class SetOutgoingIf {
  38     private static int PORT = 9001;
  39     private static String osname;
  40 
  41     static boolean isWindows() {
  42         if (osname == null)
  43             osname = System.getProperty("os.name");
  44         return osname.contains("Windows");
  45     }
  46 
  47     private static boolean hasIPv6() throws Exception {
  48         return NetworkConfiguration.probe().ip6Addresses().findAny().isPresent();










  49     }
  50 
  51     public static void main(String[] args) throws Exception {
  52         if (isWindows()) {
  53             System.out.println("The test only run on non-Windows OS. Bye.");
  54             return;
  55         }
  56 
  57         if (!hasIPv6()) {
  58             System.out.println("No IPv6 available. Bye.");
  59             return;
  60         }
  61 
  62         NetworkConfiguration nc = NetworkConfiguration.probe();
  63 
  64         // We need 2 or more network interfaces to run the test
  65         //
  66         List<NetIf> netIfs = new ArrayList<NetIf>();
  67         int index = 1;
  68         for (NetworkInterface nic : nc.interfaces().collect(toList())) {
  69             // only use network interfaces with multicast support
  70             if (!nic.isLoopback() && nic.supportsMulticast()) {
  71                 NetIf netIf = NetIf.create(nic);
  72 
  73                 // determine what (if any) type of addresses are assigned
  74                 List<Inet4Address> ipv4Addrs = nc.ip4Addresses(nic).collect(toList());
  75                 List<Inet6Address> ipv6Addrs = nc.ip6Addresses(nic).collect(toList());
  76                 if (!ipv4Addrs.isEmpty())



  77                     netIf.ipv4Address(true);
  78                 if (!ipv6Addrs.isEmpty())
  79                     netIf.ipv6Address(true);
  80 

  81                 if (netIf.ipv4Address() || netIf.ipv6Address()) {
  82                     netIf.index(index++);
  83                     netIfs.add(netIf);
  84                     debug("Using: " + nic + "\n  " + ipv4Addrs + ipv6Addrs);
  85                 }
  86             }
  87         }
  88         if (netIfs.size() <= 1) {
  89             System.out.println("Need 2 or more network interfaces to run. Bye.");
  90             return;
  91         }
  92 
  93         // We will send packets to one ipv4, and one ipv6
  94         // multicast group using each network interface :-
  95         //      224.1.1.1        --|
  96         //      ff02::1:1        --|--> using network interface #1
  97         //      224.1.2.1        --|
  98         //      ff02::1:2        --|--> using network interface #2
  99         // and so on.
 100         //
 101         for (NetIf netIf : netIfs) {
 102             int NetIfIndex = netIf.index();
 103             List<InetAddress> groups = new ArrayList<InetAddress>();
 104 


< prev index next >