--- old/test/java/net/Inet6Address/B6206527.java 2017-04-12 14:44:16.000000000 +0100 +++ new/test/java/net/Inet6Address/B6206527.java 2017-04-12 14:44:15.000000000 +0100 @@ -25,10 +25,14 @@ * @test 1.1 05/01/05 * @bug 6206527 * @summary "cannot assign address" when binding ServerSocket on Suse 9 + * @library /lib/testlibrary + * @build jdk.testlibrary.NetworkConfiguration + * @run main B6206527 */ import java.net.*; import java.util.*; +import jdk.testlibrary.NetworkConfiguration; public class B6206527 { @@ -53,21 +57,12 @@ ss.bind(new InetSocketAddress(addr, 0)); } - public static Inet6Address getLocalAddr () throws Exception { - Enumeration e = NetworkInterface.getNetworkInterfaces(); - while (e.hasMoreElements()) { - NetworkInterface ifc = (NetworkInterface) e.nextElement(); - Enumeration addrs = ifc.getInetAddresses(); - while (addrs.hasMoreElements()) { - InetAddress a = (InetAddress)addrs.nextElement(); - if (a instanceof Inet6Address) { - Inet6Address ia6 = (Inet6Address) a; - if (ia6.isLinkLocalAddress()) { - return ia6; - } - } - } - } - return null; + public static Inet6Address getLocalAddr() throws Exception { + Optional oaddr = NetworkConfiguration.probe() + .ip6Addresses() + .filter(a -> a.isLinkLocalAddress()) + .findFirst(); + + return oaddr.orElseGet(() -> null); } } --- old/test/java/net/Inet6Address/B6558853.java 2017-04-12 14:44:16.000000000 +0100 +++ new/test/java/net/Inet6Address/B6558853.java 2017-04-12 14:44:16.000000000 +0100 @@ -25,52 +25,48 @@ * @test * @bug 6558853 * @summary getHostAddress() on connections using IPv6 link-local addrs should have zone id + * * @library /lib/testlibrary + * @build jdk.testlibrary.NetworkConfiguration + * @run main B6558853 */ + import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.*; -import java.util.Enumeration; +import java.util.Optional; +import jdk.testlibrary.NetworkConfiguration; public class B6558853 implements Runnable { private InetAddress addr = null; private int port = 0; public static void main(String[] args) throws Exception { - ServerSocket ss = new ServerSocket(0); - int port = ss.getLocalPort(); - Enumeration l = NetworkInterface.getNetworkInterfaces(); - InetAddress dest = null; - while (l.hasMoreElements() && dest == null) { - NetworkInterface nif = l.nextElement(); - if (!nif.isUp()) - continue; - - for (InterfaceAddress a : nif.getInterfaceAddresses()) { - if (a.getAddress() instanceof Inet6Address) { - Inet6Address a6 = (Inet6Address) a.getAddress(); - if (a6.isLinkLocalAddress()) { - dest = a6; - } - break; + Optional oaddr = NetworkConfiguration.probe() + .ip6Addresses() + .filter(a -> a.isLinkLocalAddress()) + .findFirst(); + + if (oaddr.isPresent()) { + Inet6Address dest = oaddr.get(); + System.out.println("Using " + dest); + + try (ServerSocket ss = new ServerSocket(0)) { + int port = ss.getLocalPort(); + B6558853 test = new B6558853(dest, port); + Thread thread = new Thread(test); + thread.start(); + Socket s = ss.accept(); + InetAddress a = s.getInetAddress(); + OutputStream out = s.getOutputStream(); + out.write(1); + out.close(); + if (!(a instanceof Inet6Address) || a.getHostAddress().indexOf("%") == -1) { + // No Scope found in the address String + throw new RuntimeException("Wrong address: " + a.getHostAddress()); } } } - System.out.println("Using " + dest); - if (dest != null) { - B6558853 test = new B6558853(dest, port); - Thread thread = new Thread(test); - thread.start(); - Socket s = ss.accept(); - InetAddress a = s.getInetAddress(); - OutputStream out = s.getOutputStream(); - out.write(1); - out.close(); - if (!(a instanceof Inet6Address) || a.getHostAddress().indexOf("%") == -1) { - // No Scope found in the address String - throw new RuntimeException("Wrong address: " + a.getHostAddress()); - } - } } public B6558853(InetAddress a, int port) { --- old/test/java/net/InetAddress/CheckJNI.java 2017-04-12 14:44:17.000000000 +0100 +++ new/test/java/net/InetAddress/CheckJNI.java 2017-04-12 14:44:17.000000000 +0100 @@ -24,11 +24,15 @@ /* @test @bug 4889870 4890033 @summary java -Xcheck:jni failing in net code on Solaris / [Datagram]Socket.getLocalAddress() failure + @library /lib/testlibrary + @build jdk.testlibrary.NetworkConfiguration @run main/othervm -Xcheck:jni CheckJNI */ import java.net.*; import java.util.*; +import java.util.stream.Collectors; +import jdk.testlibrary.NetworkConfiguration; public class CheckJNI { static Socket s; @@ -49,32 +53,23 @@ dg2 = new DatagramSocket (0, InetAddress.getByName ("127.0.0.1")); testDatagrams (dg1, dg2); - /* Use NetworkInterface to find link local IPv6 addrs to test */ - - Enumeration ifs = NetworkInterface.getNetworkInterfaces(); - server = new ServerSocket (0); - - while (ifs.hasMoreElements()) { - NetworkInterface nif = (NetworkInterface)ifs.nextElement(); - if (!nif.isUp()) - continue; - Enumeration addrs = nif.getInetAddresses(); - while (addrs.hasMoreElements()) { - InetAddress addr = (InetAddress) addrs.nextElement(); - if (addr instanceof Inet6Address) { - Inet6Address ia6 = (Inet6Address) addr; - if (ia6.isLinkLocalAddress()) { - System.out.println ("Testing IPv6 Socket"); - s = new Socket (ia6, server.getLocalPort()); - s.close(); - - System.out.println ("Testing IPv6 DatagramSocket"); - dg1 = new DatagramSocket (0, ia6); - dg2 = new DatagramSocket (0, ia6); - testDatagrams (dg1, dg2); - } - } - } + /* Find link local IPv6 addrs to test */ + List addrs = NetworkConfiguration.probe() + .ip6Addresses() + .filter(Inet6Address::isLinkLocalAddress) + .collect(Collectors.toList()); + + server = new ServerSocket(0); + for (Inet6Address ia6 : addrs) { + System.out.println("Address:" + ia6); + System.out.println("Testing IPv6 Socket"); + s = new Socket(ia6, server.getLocalPort()); + s.close(); + + System.out.println("Testing IPv6 DatagramSocket"); + dg1 = new DatagramSocket(0, ia6); + dg2 = new DatagramSocket(0, ia6); + testDatagrams(dg1, dg2); } server.close(); System.out.println ("OK"); --- old/test/java/net/MulticastSocket/B6427403.java 2017-04-12 14:44:18.000000000 +0100 +++ new/test/java/net/MulticastSocket/B6427403.java 2017-04-12 14:44:18.000000000 +0100 @@ -23,20 +23,37 @@ /* * @test - * * @bug 6427403 - * * @summary java.net.MulticastSocket.joinGroup() reports 'socket closed' - * + * @library /lib/testlibrary + * @build jdk.testlibrary.NetworkConfiguration + * @run main B6427403 */ + +import java.io.IOException; import java.net.*; -import java.io.*; -import java.util.*; +import java.util.Optional; +import java.util.Set; +import jdk.testlibrary.NetworkConfiguration; + public class B6427403 { public static void main( String[] args ) throws IOException { InetAddress lh = InetAddress.getLocalHost(); - MulticastSocket ms = new MulticastSocket( new InetSocketAddress(lh, 0) ); - ms.joinGroup( InetAddress.getByName("224.80.80.80") ); - ms.close(); + System.out.println("localhost:" + lh); + + Optional onif = NetworkConfiguration.probe() + .ip4MulticastInterfaces() + .filter(addr -> addr.equals(lh)) + .findFirst(); + + if (onif.isPresent()) { + NetworkInterface nif = onif.get(); + System.out.println("nif:" + nif); + SocketAddress bindAddr = new InetSocketAddress(lh, 0); + try (MulticastSocket ms = new MulticastSocket(bindAddr)) { + ms.setNetworkInterface(nif); + ms.joinGroup(InetAddress.getByName("224.80.80.80")); + } + } } } --- old/test/java/net/Socket/LinkLocal.java 2017-04-12 14:44:19.000000000 +0100 +++ new/test/java/net/Socket/LinkLocal.java 2017-04-12 14:44:19.000000000 +0100 @@ -26,9 +26,15 @@ * @bug 4469866 * @summary Connecting to a link-local IPv6 address should not * causes a SocketException to be thrown. + * @library /lib/testlibrary + * @build jdk.testlibrary.NetworkConfiguration + * @run main LinkLocal */ +import jdk.testlibrary.NetworkConfiguration; + import java.net.*; -import java.util.Enumeration; +import java.util.List; +import java.util.stream.Collectors; public class LinkLocal { @@ -134,22 +140,13 @@ * IPv6 address. */ if (args.length == 0) { - Enumeration nifs = NetworkInterface.getNetworkInterfaces(); - while (nifs.hasMoreElements()) { - NetworkInterface ni = (NetworkInterface)nifs.nextElement(); - if (!ni.isUp()) - continue; - - Enumeration addrs = ni.getInetAddresses(); - while (addrs.hasMoreElements()) { - InetAddress addr = (InetAddress)addrs.nextElement(); - - if (addr instanceof Inet6Address && - addr.isLinkLocalAddress()) { - - TestAddress(addr); - } - } + List addrs = NetworkConfiguration.probe() + .ip6Addresses() + .filter(Inet6Address::isLinkLocalAddress) + .collect(Collectors.toList()); + + for (Inet6Address addr : addrs) { + TestAddress(addr); } } --- old/test/java/net/SocketPermission/SocketPermissionTest.java 2017-04-12 14:44:20.000000000 +0100 +++ new/test/java/net/SocketPermission/SocketPermissionTest.java 2017-04-12 14:44:20.000000000 +0100 @@ -25,13 +25,17 @@ * @test * @bug 8047031 * @summary SocketPermission tests for legacy socket types + * @library /lib/testlibrary + * @build jdk.testlibrary.NetworkConfiguration * @run testng/othervm SocketPermissionTest */ + import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.MulticastSocket; +import java.net.NetworkInterface; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketPermission; @@ -44,11 +48,14 @@ import java.security.Policy; import java.security.PrivilegedExceptionAction; import java.security.ProtectionDomain; +import java.util.Optional; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; + import static org.testng.Assert.*; +import static jdk.testlibrary.NetworkConfiguration.probe; import static java.nio.charset.StandardCharsets.UTF_8; public class SocketPermissionTest { @@ -210,12 +217,17 @@ new SocketPermission(addr, "listen,resolve"), new SocketPermission("229.227.226.221", "connect,accept")); - // Positive - AccessController.doPrivileged((PrivilegedExceptionAction) () -> { - s.joinGroup(group); - s.leaveGroup(group); - return null; - }, acc); + // Positive ( requires a functional network interface ) + Optional onif = probe().ip4MulticastInterfaces().findFirst(); + if (!onif.isPresent()) { + s.setNetworkInterface(onif.get()); + + AccessController.doPrivileged((PrivilegedExceptionAction) () -> { + s.joinGroup(group); + s.leaveGroup(group); + return null; + }, acc); + } // Negative try { --- old/test/java/net/ipv6tests/B6521014.java 2017-04-12 14:44:21.000000000 +0100 +++ new/test/java/net/ipv6tests/B6521014.java 2017-04-12 14:44:21.000000000 +0100 @@ -25,13 +25,15 @@ * @test * @bug 6521014 6543428 * @summary IOException thrown when Socket tries to bind to an local IPv6 address on SuSE Linux + * @library /lib/testlibrary + * @build jdk.testlibrary.NetworkConfiguration + * @run main B6521014 */ - import java.net.*; import java.io.*; import java.util.*; - +import jdk.testlibrary.NetworkConfiguration; /* * @@ -52,38 +54,26 @@ */ public class B6521014 { - static InetAddress sin; - - static Inet6Address getLocalAddr () throws Exception { - Enumeration e = NetworkInterface.getNetworkInterfaces(); - while (e.hasMoreElements()) { - NetworkInterface ifc = (NetworkInterface) e.nextElement(); - if (!ifc.isUp()) - continue; - Enumeration addrs = ifc.getInetAddresses(); - while (addrs.hasMoreElements()) { - InetAddress a = (InetAddress)addrs.nextElement(); - if (a instanceof Inet6Address) { - Inet6Address ia6 = (Inet6Address) a; - if (ia6.isLinkLocalAddress()) { - // remove %scope suffix - return (Inet6Address)InetAddress.getByAddress(ia6.getAddress()); - } - } - } + static Inet6Address removeScope(Inet6Address addr) { + try { + return (Inet6Address)InetAddress.getByAddress(addr.getAddress()); + } catch (IOException e) { + throw new UncheckedIOException(e); } - return null; } - static void test1() throws Exception { - ServerSocket ssock; - Socket sock; - int port; - - ssock = new ServerSocket(0); - port = ssock.getLocalPort(); - sock = new Socket(); - try { + static Optional getLocalAddr() throws Exception { + return NetworkConfiguration.probe() + .ip6Addresses() + .filter(Inet6Address::isLinkLocalAddress) + .map(B6521014::removeScope) + .findFirst(); + } + + static void test1(Inet6Address sin) throws Exception { + try (ServerSocket ssock = new ServerSocket(0); + Socket sock = new Socket()) { + int port = ssock.getLocalPort(); sock.connect(new InetSocketAddress(sin, port), 100); } catch (SocketTimeoutException e) { // time out exception is okay @@ -91,36 +81,29 @@ } } - static void test2() throws Exception { - Socket sock; - ServerSocket ssock; - int port; - - ssock = new ServerSocket(0); - ssock.setSoTimeout(100); - port = ssock.getLocalPort(); - sock = new Socket(); - sock.bind(new InetSocketAddress(sin, 0)); - try { + static void test2(Inet6Address sin) throws Exception { + try (ServerSocket ssock = new ServerSocket(0); + Socket sock = new Socket()) { + int port = ssock.getLocalPort(); + ssock.setSoTimeout(100); + sock.bind(new InetSocketAddress(sin, 0)); sock.connect(new InetSocketAddress(sin, port), 100); - } catch (SocketTimeoutException e) { + } catch (SocketTimeoutException expected) { // time out exception is okay System.out.println("timed out when connecting."); } } public static void main(String[] args) throws Exception { - sin = getLocalAddr(); - if (sin == null) { + Optional oaddr = getLocalAddr(); + if (!oaddr.isPresent()) { System.out.println("Cannot find a link-local address."); return; } - try { - test1(); - test2(); - } catch (IOException e) { - throw new RuntimeException("Test failed: cannot create socket.", e); - } + Inet6Address addr = oaddr.get(); + System.out.println("Using " + addr); + test1(addr); + test2(addr); } } --- old/test/java/net/ipv6tests/Tests.java 2017-04-12 14:44:22.000000000 +0100 +++ new/test/java/net/ipv6tests/Tests.java 2017-04-12 14:44:22.000000000 +0100 @@ -27,7 +27,10 @@ public class Tests { - static boolean isWindows = System.getProperty("os.name").startsWith("Windows"); + static final boolean isWindows = + System.getProperty("os.name").startsWith("Windows"); + static final boolean isMacOS = + System.getProperty("os.name", "unknown").contains("OS X"); /** * performs a simple exchange of data between the two sockets @@ -278,6 +281,8 @@ String dName = nic.getDisplayName(); if (dName != null && dName.contains("Teredo")) continue; + } else if (isMacOS && nic.getName().contains("awdl")) { + continue; } try { if (nic.isUp() && !nic.isLoopback()) --- old/test/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java 2017-04-12 14:44:23.000000000 +0100 +++ new/test/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java 2017-04-12 14:44:22.000000000 +0100 @@ -31,9 +31,9 @@ * @bug 6216082 * @summary Redirect problem with HttpsURLConnection using a proxy * @modules java.base/sun.net.www - * @library .. + * @library .. /lib/testlibrary * @build HttpCallback TestHttpsServer ClosedChannelList - * HttpTransaction TunnelProxy + * HttpTransaction TunnelProxy jdk.testlibrary.NetworkConfiguration * @key intermittent * @run main/othervm B6216082 */ @@ -43,6 +43,8 @@ import javax.net.ssl.*; import java.util.*; +import jdk.testlibrary.NetworkConfiguration; + public class B6216082 { static SimpleHttpTransaction httpTrans; static TestHttpsServer server; @@ -118,21 +120,17 @@ } public static InetAddress getNonLoAddress() throws Exception { - NetworkInterface loNIC = NetworkInterface.getByInetAddress(InetAddress.getByName("localhost")); - Enumeration nics = NetworkInterface.getNetworkInterfaces(); - while (nics.hasMoreElements()) { - NetworkInterface nic = nics.nextElement(); - if (!nic.getName().equalsIgnoreCase(loNIC.getName())) { - Enumeration addrs = nic.getInetAddresses(); - while (addrs.hasMoreElements()) { - InetAddress addr = addrs.nextElement(); - if (!addr.isLoopbackAddress()) - return addr; - } - } - } + InetAddress lh = InetAddress.getByName("localhost"); + NetworkInterface loNIC = NetworkInterface.getByInetAddress(lh); + + NetworkConfiguration nc = NetworkConfiguration.probe(); + Optional oaddr = nc.interfaces() + .filter(nif -> !nif.getName().equalsIgnoreCase(loNIC.getName())) + .flatMap(nif -> nc.addresses(nif)) + .filter(a -> !a.isLoopbackAddress()) + .findFirst(); - return null; + return oaddr.orElseGet(() -> null); } public static void startHttpServer() throws IOException { --- old/test/java/net/MulticastSocket/JoinGroup.java 2017-04-12 14:44:24.000000000 +0100 +++ /dev/null 2017-04-12 14:44:24.000000000 +0100 @@ -1,46 +0,0 @@ -/* - * Copyright (c) 1998, 1999, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -/* - * @test - * @bug 4091811 4148753 - * @summary Test java.net.MulticastSocket joinGroup and leaveGroup - * - */ - -import java.io.*; -import java.net.*; - - -public class JoinGroup { - - public static void main(String args[]) throws Exception { - MulticastSocket soc = null; - InetAddress sin = null; - - soc = new MulticastSocket(); - sin = InetAddress.getByName("224.80.80.80"); - soc.joinGroup(sin); - soc.leaveGroup(sin); - } -} --- /dev/null 2017-04-12 14:44:24.000000000 +0100 +++ new/test/java/net/MulticastSocket/JoinLeave.java 2017-04-12 14:44:23.000000000 +0100 @@ -0,0 +1,66 @@ +/* + * Copyright (c) 1998, 1999, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 4091811 4148753 4102731 + * @summary Test java.net.MulticastSocket joinGroup and leaveGroup + * @library /lib/testlibrary + * @build jdk.testlibrary.NetworkConfiguration + * @run main JoinLeave + */ + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.net.InetAddress; +import java.net.MulticastSocket; +import java.net.NetworkInterface; +import jdk.testlibrary.NetworkConfiguration; + +public class JoinLeave { + + public static void main(String args[]) throws IOException { + InetAddress ip4Group = InetAddress.getByName("224.80.80.80"); + InetAddress ip6Group = InetAddress.getByName("ff02::a"); + + joinLeave(ip4Group, null); + joinLeave(ip6Group, null); + + NetworkConfiguration nc = NetworkConfiguration.probe(); + nc.ip4MulticastInterfaces().forEach(nic -> joinLeave(ip4Group, nic)); + nc.ip6MulticastInterfaces().forEach(nic -> joinLeave(ip6Group, nic)); + } + + static void joinLeave(InetAddress group, NetworkInterface nif) + { + System.out.println("Joining:" + group + " on " + nif); + try (MulticastSocket soc = new MulticastSocket()) { + if (nif != null) + soc.setNetworkInterface(nif); + soc.joinGroup(group); + soc.leaveGroup(group); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } +} --- /dev/null 2017-04-12 14:44:25.000000000 +0100 +++ new/test/lib/testlibrary/jdk/testlibrary/NetworkConfiguration.java 2017-04-12 14:44:24.000000000 +0100 @@ -0,0 +1,221 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package jdk.testlibrary; + +import java.io.UncheckedIOException; +import java.io.IOException; +import java.net.Inet4Address; +import java.net.Inet6Address; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.Predicate; +import java.util.stream.Stream; +import static java.util.Collections.list; + +/** + * Helper class for retrieving network interfaces and local addresses + * suitable for testing. + */ +public class NetworkConfiguration { + + static final boolean isWindows = + System.getProperty("os.name", "unknown").startsWith("Windows"); + static final boolean isMacOS = + System.getProperty("os.name", "unknown").contains("OS X"); + + private Map> ip4Interfaces; + private Map> ip6Interfaces; + + private NetworkConfiguration(Map> ip4Interfaces, + Map> ip6Interfaces) + { + this.ip4Interfaces = ip4Interfaces; + this.ip6Interfaces = ip6Interfaces; + } + + /** + * Returns a stream of interfaces suitable for functional tests. + */ + public Stream interfaces() { + return Stream.concat(ip4Interfaces(), ip6Interfaces()) + .distinct(); + } + + /** + * Returns a stream of interfaces suitable for IPv4 functional tests. + */ + public Stream ip4Interfaces() { + return ip4Interfaces.keySet().stream() + .filter(NetworkConfiguration::isNotExcludedInterface) + .filter(hasIp4Addresses); + } + + /** + * Returns a stream of interfaces suitable for IPv6 functional tests. + */ + public Stream ip6Interfaces() { + return ip6Interfaces.keySet().stream() + .filter(NetworkConfiguration::isNotExcludedInterface) + .filter(hasIp6Addresses); + } + + private static boolean isNotExcludedInterface(NetworkInterface nif) { + if (isMacOS && nif.getName().contains("awdl")) + return false; + String dName = nif.getDisplayName(); + if (isWindows && dName != null && dName.contains("Teredo")) + return false; + return true; + } + + private final Predicate hasIp4Addresses = nif -> { + Optional addr = ip4Interfaces.get(nif).stream() + .filter(a -> !a.isAnyLocalAddress()) + .findAny(); + + return addr.isPresent(); + }; + + private final Predicate hasIp6Addresses = nif -> { + Optional addr = ip6Interfaces.get(nif).stream() + .filter(a -> !a.isAnyLocalAddress()) + .findAny(); + + return addr.isPresent(); + }; + + + /** + * Returns a stream of interfaces suitable for IPv4 multicast tests. + */ + public Stream ip4MulticastInterfaces() { + return ip4Interfaces().filter(supportsIp4Multicast); + } + + /** + * Returns a stream of interfaces suitable for IPv6 multicast tests. + */ + public Stream ip6MulticastInterfaces() { + return ip6Interfaces().filter(supportsIp6Multicast); + } + + private final Predicate supportsIp4Multicast = nif -> { + try { + if (!nif.supportsMulticast() || nif.isLoopback()) + return false; + + Optional addr = ip4Interfaces.get(nif).stream() + .filter(a -> !a.isAnyLocalAddress()) + .findAny(); + + return addr.isPresent(); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }; + + private final Predicate supportsIp6Multicast = nif -> { + try { + if (!nif.supportsMulticast() || nif.isLoopback()) + return false; + + Optional addr = ip6Interfaces.get(nif).stream() + .filter(a -> !a.isAnyLocalAddress()) + .findAny(); + + return addr.isPresent(); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }; + + /** + * Returns all addresses on all "functional" interfaces. + */ + public Stream addresses(NetworkInterface nif) { + return Stream.concat(ip4Interfaces.get(nif).stream(), + ip6Interfaces.get(nif).stream()); + } + + /** + * Returns all IPv4 addresses on all "functional" interfaces. + */ + public Stream ip4Addresses() { + return ip4Interfaces().flatMap(nif -> ip4Addresses(nif)); + } + + /** + * Returns all IPv6 addresses on all "functional" interfaces. + */ + public Stream ip6Addresses() { + return ip6Interfaces().flatMap(nif -> ip6Addresses(nif)); + } + + /** + * Returns all IPv4 addresses the given interface. + */ + public Stream ip4Addresses(NetworkInterface nif) { + return ip4Interfaces.get(nif).stream(); + } + + /** + * Returns all IPv6 addresses for the given interface. + */ + public Stream ip6Addresses(NetworkInterface nif) { + return ip6Interfaces.get(nif).stream(); + } + + /** + * Return a NetworkConfiguration instance. + */ + public static NetworkConfiguration probe() throws IOException { + Map> ip4Interfaces = new HashMap<>(); + Map> ip6Interfaces = new HashMap<>(); + + List nifs = list(NetworkInterface.getNetworkInterfaces()); + for (NetworkInterface nif : nifs) { + // ignore interfaces that are down + if (!nif.isUp() || nif.isPointToPoint()) + continue; + + List ip4Addresses = new LinkedList<>(); + List ip6Addresses = new LinkedList<>(); + ip4Interfaces.put(nif, ip4Addresses); + ip6Interfaces.put(nif, ip6Addresses); + for (InetAddress addr : list(nif.getInetAddresses())) { + if (addr instanceof Inet4Address) + ip4Addresses.add((Inet4Address)addr); + else if (addr instanceof Inet6Address) + ip6Addresses.add((Inet6Address)addr); + } + } + return new NetworkConfiguration(ip4Interfaces, ip6Interfaces); + } +} + --- old/test/java/net/MulticastSocket/Leave.java 2017-04-12 14:44:25.000000000 +0100 +++ /dev/null 2017-04-12 14:44:25.000000000 +0100 @@ -1,46 +0,0 @@ -/* - * Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -/* - * @test - * @bug 4102731 - * @summary Test the java.net.multicastsocket.leave method - * - */ - -import java.net.*; -import java.io.*; - -public class Leave { - - public static void main(String args[]) throws Exception { - MulticastSocket socket = null; - InetAddress mca = null; - - mca = InetAddress.getByName("224.80.80.80"); - socket = new MulticastSocket(); - socket.joinGroup(mca); - socket.leaveGroup(mca); - socket.close(); - } -}