# HG changeset patch # User chegar # Date 1492163228 -3600 # Node ID d1d011d4654d14116587611ca65b5ec08397ccb9 # Parent 77e54dd0675f180ba70992081cf5ea36f3d9dec4 8177536: Avoid Apple Peer-to-Peer interfaces in networking tests Reviewed-by: michaelm, rriggs diff -r 77e54dd0675f -r d1d011d4654d jdk/test/java/net/Inet6Address/B6206527.java --- a/jdk/test/java/net/Inet6Address/B6206527.java Fri Apr 14 00:05:54 2017 -0700 +++ b/jdk/test/java/net/Inet6Address/B6206527.java Fri Apr 14 10:47:08 2017 +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); } } diff -r 77e54dd0675f -r d1d011d4654d jdk/test/java/net/Inet6Address/B6558853.java --- a/jdk/test/java/net/Inet6Address/B6558853.java Fri Apr 14 00:05:54 2017 -0700 +++ b/jdk/test/java/net/Inet6Address/B6558853.java Fri Apr 14 10:47:08 2017 +0100 @@ -21,43 +21,42 @@ * questions. */ -/** +/* * @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; + Optional oaddr = NetworkConfiguration.probe() + .ip6Addresses() + .filter(a -> a.isLinkLocalAddress()) + .findFirst(); - for (InterfaceAddress a : nif.getInterfaceAddresses()) { - if (a.getAddress() instanceof Inet6Address) { - Inet6Address a6 = (Inet6Address) a.getAddress(); - if (a6.isLinkLocalAddress()) { - dest = a6; - } - break; - } - } + if (!oaddr.isPresent()) { + System.out.println("No suitable interface found. Exiting."); + return; } + + Inet6Address dest = oaddr.get(); System.out.println("Using " + dest); - if (dest != null) { + + try (ServerSocket ss = new ServerSocket(0)) { + int port = ss.getLocalPort(); B6558853 test = new B6558853(dest, port); Thread thread = new Thread(test); thread.start(); diff -r 77e54dd0675f -r d1d011d4654d jdk/test/java/net/InetAddress/CheckJNI.java --- a/jdk/test/java/net/InetAddress/CheckJNI.java Fri Apr 14 00:05:54 2017 -0700 +++ b/jdk/test/java/net/InetAddress/CheckJNI.java Fri Apr 14 10:47:08 2017 +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); + /* Find link local IPv6 addrs to test */ + List addrs = NetworkConfiguration.probe() + .ip6Addresses() + .filter(Inet6Address::isLinkLocalAddress) + .collect(Collectors.toList()); - 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(); + 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); - } - } - } + 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"); diff -r 77e54dd0675f -r d1d011d4654d jdk/test/java/net/MulticastSocket/B6427403.java --- a/jdk/test/java/net/MulticastSocket/B6427403.java Fri Apr 14 00:05:54 2017 -0700 +++ b/jdk/test/java/net/MulticastSocket/B6427403.java Fri Apr 14 10:47:08 2017 +0100 @@ -23,15 +23,12 @@ /* * @test - * * @bug 6427403 - * * @summary java.net.MulticastSocket.joinGroup() reports 'socket closed' - * */ import java.net.*; import java.io.*; -import java.util.*; + public class B6427403 { public static void main( String[] args ) throws IOException { InetAddress lh = InetAddress.getLocalHost(); @@ -39,4 +36,4 @@ ms.joinGroup( InetAddress.getByName("224.80.80.80") ); ms.close(); } -} +} \ No newline at end of file diff -r 77e54dd0675f -r d1d011d4654d jdk/test/java/net/MulticastSocket/JoinGroup.java --- a/jdk/test/java/net/MulticastSocket/JoinGroup.java Fri Apr 14 00:05:54 2017 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -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); - } -} diff -r 77e54dd0675f -r d1d011d4654d jdk/test/java/net/MulticastSocket/JoinLeave.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jdk/test/java/net/MulticastSocket/JoinLeave.java Fri Apr 14 10:47:08 2017 +0100 @@ -0,0 +1,62 @@ +/* + * 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"); + + 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()) { + soc.setNetworkInterface(nif); + soc.joinGroup(group); + soc.leaveGroup(group); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } +} diff -r 77e54dd0675f -r d1d011d4654d jdk/test/java/net/MulticastSocket/Leave.java --- a/jdk/test/java/net/MulticastSocket/Leave.java Fri Apr 14 00:05:54 2017 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -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(); - } -} diff -r 77e54dd0675f -r d1d011d4654d jdk/test/java/net/NetworkConfigurationProbe.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jdk/test/java/net/NetworkConfigurationProbe.java Fri Apr 14 10:47:08 2017 +0100 @@ -0,0 +1,69 @@ +/* + * 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. + */ + +/** + * @test + * @summary NOT A TEST. Captures the network interface configuration. + * @library /lib/testlibrary + * @build jdk.testlibrary.NetworkConfiguration + * @run main NetworkConfigurationProbe + */ + +import java.net.Inet4Address; +import java.net.Inet6Address; +import java.net.NetworkInterface; +import jdk.testlibrary.NetworkConfiguration; +import static java.util.stream.Collectors.joining; +import static java.lang.System.out; + +/** + * Not a test. Captures the network interface configuration. + */ +public class NetworkConfigurationProbe { + + public static void main(String... args) throws Exception { + NetworkConfiguration.printSystemConfiguration(out); + + NetworkConfiguration nc = NetworkConfiguration.probe(); + String list; + list = nc.ip4MulticastInterfaces() + .map(NetworkInterface::getName) + .collect(joining(" ")); + out.println("ip4MulticastInterfaces: " + list); + + list = nc.ip4Addresses() + .map(Inet4Address::toString) + .collect(joining(" ")); + out.println("ip4Addresses: " + list); + + list = nc.ip6MulticastInterfaces() + .map(NetworkInterface::getName) + .collect(joining(" ")); + out.println("ip6MulticastInterfaces: " + list); + + list = nc.ip6Addresses() + .map(Inet6Address::toString) + .collect(joining(" ")); + out.println("ip6Addresses: " + list); + } +} diff -r 77e54dd0675f -r d1d011d4654d jdk/test/java/net/Socket/LinkLocal.java --- a/jdk/test/java/net/Socket/LinkLocal.java Fri Apr 14 00:05:54 2017 -0700 +++ b/jdk/test/java/net/Socket/LinkLocal.java Fri Apr 14 10:47:08 2017 +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; + List addrs = NetworkConfiguration.probe() + .ip6Addresses() + .filter(Inet6Address::isLinkLocalAddress) + .collect(Collectors.toList()); - Enumeration addrs = ni.getInetAddresses(); - while (addrs.hasMoreElements()) { - InetAddress addr = (InetAddress)addrs.nextElement(); - - if (addr instanceof Inet6Address && - addr.isLinkLocalAddress()) { - - TestAddress(addr); - } - } + for (Inet6Address addr : addrs) { + TestAddress(addr); } } diff -r 77e54dd0675f -r d1d011d4654d jdk/test/java/net/SocketPermission/SocketPermissionTest.java --- a/jdk/test/java/net/SocketPermission/SocketPermissionTest.java Fri Apr 14 00:05:54 2017 -0700 +++ b/jdk/test/java/net/SocketPermission/SocketPermissionTest.java Fri Apr 14 10:47:08 2017 +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 { diff -r 77e54dd0675f -r d1d011d4654d jdk/test/java/net/ipv6tests/B6521014.java --- a/jdk/test/java/net/ipv6tests/B6521014.java Fri Apr 14 00:05:54 2017 -0700 +++ b/jdk/test/java/net/ipv6tests/B6521014.java Fri Apr 14 10:47:08 2017 +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; + static Optional getLocalAddr() throws Exception { + return NetworkConfiguration.probe() + .ip6Addresses() + .filter(Inet6Address::isLinkLocalAddress) + .map(B6521014::removeScope) + .findFirst(); + } - ssock = new ServerSocket(0); - port = ssock.getLocalPort(); - sock = new Socket(); - try { + 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); } } diff -r 77e54dd0675f -r d1d011d4654d jdk/test/java/net/ipv6tests/Tests.java --- a/jdk/test/java/net/ipv6tests/Tests.java Fri Apr 14 00:05:54 2017 -0700 +++ b/jdk/test/java/net/ipv6tests/Tests.java Fri Apr 14 10:47:08 2017 +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").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()) diff -r 77e54dd0675f -r d1d011d4654d jdk/test/lib/testlibrary/jdk/testlibrary/NetworkConfiguration.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jdk/test/lib/testlibrary/jdk/testlibrary/NetworkConfiguration.java Fri Apr 14 10:47:08 2017 +0100 @@ -0,0 +1,267 @@ +/* + * 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.PrintStream; +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.Arrays; +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.net.NetworkInterface.getNetworkInterfaces; +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").startsWith("Windows"); + static final boolean isMacOS = + System.getProperty("os.name").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(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); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + interfaces().forEach(nif -> sb.append(interfaceInformation(nif))); + return sb.toString(); + } + + /** Returns detailed information for the given interface. */ + public static String interfaceInformation(NetworkInterface nif) { + StringBuilder sb = new StringBuilder(); + try { + sb.append("Display name: " + nif.getDisplayName() + "\n"); + sb.append("Name: " + nif.getName() + "\n"); + for (InetAddress inetAddress : list(nif.getInetAddresses())) + sb.append("InetAddress: " + inetAddress + "\n"); + sb.append("Up? " + nif.isUp() + "\n"); + sb.append("Loopback? " + nif.isLoopback() + "\n"); + sb.append("PointToPoint? " + nif.isPointToPoint() + "\n"); + sb.append("Supports multicast? " + nif.supportsMulticast() + "\n"); + sb.append("Virtual? " + nif.isVirtual() + "\n"); + sb.append("Hardware address: " + + Arrays.toString(nif.getHardwareAddress()) + "\n"); + sb.append("MTU: " + nif.getMTU() + "\n"); + sb.append("Index: " + nif.getIndex() + "\n"); + sb.append("\n"); + return sb.toString(); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + /** Prints all the system interface information to the give stream. */ + public static void printSystemConfiguration(PrintStream out) { + try { + out.println("*** all system network interface configuration ***"); + List nifs = list(getNetworkInterfaces()); + for (NetworkInterface nif : nifs) + out.print(interfaceInformation(nif)); + out.println("*** end ***"); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } +} diff -r 77e54dd0675f -r d1d011d4654d jdk/test/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java --- a/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java Fri Apr 14 00:05:54 2017 -0700 +++ b/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java Fri Apr 14 10:47:08 2017 +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); - return null; + 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 oaddr.orElseGet(() -> null); } public static void startHttpServer() throws IOException {