8180644: move jdk.testlibrary.NetworkConfiguration to the top level test library

Reviewed-by: duke
This commit is contained in:
Igor Ignatyev 2017-05-19 13:33:47 -07:00
parent cc347a7a7c
commit f8c5d25081
15 changed files with 49 additions and 411 deletions

View File

@ -25,14 +25,13 @@
* @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
* @library /test/lib
* @run main B6206527
*/
import java.net.*;
import java.util.*;
import jdk.testlibrary.NetworkConfiguration;
import jdk.test.lib.NetworkConfiguration;
public class B6206527 {

View File

@ -25,8 +25,7 @@
* @test
* @bug 6558853
* @summary getHostAddress() on connections using IPv6 link-local addrs should have zone id
* @library /lib/testlibrary
* @build jdk.testlibrary.NetworkConfiguration
* @library /test/lib
* @run main B6558853
*/
@ -35,7 +34,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;
import java.util.Optional;
import jdk.testlibrary.NetworkConfiguration;
import jdk.test.lib.NetworkConfiguration;
public class B6558853 implements Runnable {
private InetAddress addr = null;

View File

@ -22,17 +22,16 @@
*/
/* @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
*/
* @bug 4889870 4890033
* @summary java -Xcheck:jni failing in net code on Solaris / [Datagram]Socket.getLocalAddress() failure
* @library /test/lib
* @run main/othervm -Xcheck:jni CheckJNI
*/
import java.net.*;
import java.util.*;
import java.util.stream.Collectors;
import jdk.testlibrary.NetworkConfiguration;
import jdk.test.lib.NetworkConfiguration;
public class CheckJNI {
static Socket s;

View File

@ -25,8 +25,7 @@
* @test
* @bug 4091811 4148753 4102731
* @summary Test java.net.MulticastSocket joinGroup and leaveGroup
* @library /lib/testlibrary
* @build jdk.testlibrary.NetworkConfiguration
* @library /test/lib
* @run main JoinLeave
*/
@ -35,7 +34,7 @@ import java.io.UncheckedIOException;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.NetworkInterface;
import jdk.testlibrary.NetworkConfiguration;
import jdk.test.lib.NetworkConfiguration;
public class JoinLeave {

View File

@ -25,14 +25,14 @@
* @test
* @bug 4686717
* @summary Test MulticastSocket.setLoopbackMode
* @library /lib/testlibrary
* @build jdk.testlibrary.NetworkConfiguration
* @library /test/lib
* @run main/othervm SetLoopbackMode
*/
import java.net.*;
import java.io.IOException;
import java.util.Enumeration;
import jdk.testlibrary.NetworkConfiguration;
import jdk.test.lib.NetworkConfiguration;
public class SetLoopbackMode {

View File

@ -24,15 +24,14 @@
/**
* @test
* @summary NOT A TEST. Captures the network interface configuration.
* @library /lib/testlibrary
* @build jdk.testlibrary.NetworkConfiguration
* @library /test/lib
* @run main NetworkConfigurationProbe
*/
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.NetworkInterface;
import jdk.testlibrary.NetworkConfiguration;
import jdk.test.lib.NetworkConfiguration;
import static java.util.stream.Collectors.joining;
import static java.lang.System.out;

View File

@ -26,11 +26,11 @@
* @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
* @library /test/lib
* @run main LinkLocal
*/
import jdk.testlibrary.NetworkConfiguration;
import jdk.test.lib.NetworkConfiguration;
import java.net.*;
import java.util.List;

View File

@ -25,8 +25,7 @@
* @test
* @bug 8047031
* @summary SocketPermission tests for legacy socket types
* @library /lib/testlibrary
* @build jdk.testlibrary.NetworkConfiguration
* @library /test/lib
* @run testng/othervm SocketPermissionTest
*/
@ -55,7 +54,7 @@ import org.testng.annotations.Test;
import static org.testng.Assert.*;
import static jdk.testlibrary.NetworkConfiguration.probe;
import static jdk.test.lib.NetworkConfiguration.probe;
import static java.nio.charset.StandardCharsets.UTF_8;
public class SocketPermissionTest {

View File

@ -25,15 +25,14 @@
* @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
* @library /test/lib
* @run main B6521014
*/
import java.net.*;
import java.io.*;
import java.util.*;
import jdk.testlibrary.NetworkConfiguration;
import jdk.test.lib.NetworkConfiguration;
/*
*

View File

@ -24,7 +24,8 @@
/* @test
* @bug 4527345
* @summary Unit test for DatagramChannel's multicast support
* @build BasicMulticastTests NetworkConfiguration
* @library /test/lib
* @build BasicMulticastTests
* @run main BasicMulticastTests
*/
@ -34,6 +35,8 @@ import java.net.*;
import java.util.*;
import java.io.IOException;
import jdk.test.lib.NetworkConfiguration;
public class BasicMulticastTests {
/**
@ -204,14 +207,14 @@ public class BasicMulticastTests {
NetworkConfiguration config = NetworkConfiguration.probe();
NetworkInterface nif = config.ip4Interfaces().iterator().next();
NetworkInterface nif = config.ip4MulticastInterfaces().iterator().next();
InetAddress anySource = config.ip4Addresses(nif).iterator().next();
membershipKeyTests(nif, ip4Group, anySource);
exceptionTests(nif);
// re-run the membership key tests with IPv6 if available
Iterator<NetworkInterface> iter = config.ip6Interfaces().iterator();
Iterator<NetworkInterface> iter = config.ip6MulticastInterfaces().iterator();
if (iter.hasNext()) {
nif = iter.next();
anySource = config.ip6Addresses(nif).iterator().next();

View File

@ -24,7 +24,8 @@
/* @test
* @bug 4527345 7026376 6633549
* @summary Unit test for DatagramChannel's multicast support
* @build MulticastSendReceiveTests NetworkConfiguration
* @library /test/lib
* @build MulticastSendReceiveTests
* @run main MulticastSendReceiveTests
* @run main/othervm -Djava.net.preferIPv4Stack=true MulticastSendReceiveTests
* @key randomness
@ -36,6 +37,9 @@ import java.net.*;
import static java.net.StandardProtocolFamily.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;
import jdk.test.lib.NetworkConfiguration;
public class MulticastSendReceiveTests {
@ -238,14 +242,15 @@ public class MulticastSendReceiveTests {
// multicast groups used for the test
InetAddress ip4Group = InetAddress.getByName("225.4.5.6");
InetAddress ip6Group = InetAddress.getByName("ff02::a");
for (NetworkInterface nif: config.ip4Interfaces()) {
for (NetworkInterface nif: config.ip4MulticastInterfaces()
.collect(Collectors.toList())) {
InetAddress source = config.ip4Addresses(nif).iterator().next();
test(INET, nif, ip4Group, source);
test(UNSPEC, nif, ip4Group, source);
}
for (NetworkInterface nif: config.ip6Interfaces()) {
for (NetworkInterface nif: config.ip6MulticastInterfaces()
.collect(Collectors.toList())) {
InetAddress source = config.ip6Addresses(nif).iterator().next();
test(INET6, nif, ip6Group, source);
test(UNSPEC, nif, ip6Group, source);

View File

@ -1,101 +0,0 @@
/*
* Copyright (c) 2007, 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.
*/
import java.net.*;
import java.util.*;
import java.io.IOException;
/**
* Helper class for multicasting tests.
*/
class NetworkConfiguration {
private Map<NetworkInterface,List<InetAddress>> ip4Interfaces;
private Map<NetworkInterface,List<InetAddress>> ip6Interfaces;
private NetworkConfiguration(Map<NetworkInterface,List<InetAddress>> ip4Interfaces,
Map<NetworkInterface,List<InetAddress>> ip6Interfaces)
{
this.ip4Interfaces = ip4Interfaces;
this.ip6Interfaces = ip6Interfaces;
}
Iterable<NetworkInterface> ip4Interfaces() {
return ip4Interfaces.keySet();
}
Iterable<NetworkInterface> ip6Interfaces() {
return ip6Interfaces.keySet();
}
Iterable<InetAddress> ip4Addresses(NetworkInterface nif) {
return ip4Interfaces.get(nif);
}
Iterable<InetAddress> ip6Addresses(NetworkInterface nif) {
return ip6Interfaces.get(nif);
}
private static final boolean isMacOs =
System.getProperty("os.name").equals("Mac OS X");
static NetworkConfiguration probe() throws IOException {
Map<NetworkInterface,List<InetAddress>> ip4Interfaces =
new HashMap<NetworkInterface,List<InetAddress>>();
Map<NetworkInterface,List<InetAddress>> ip6Interfaces =
new HashMap<NetworkInterface,List<InetAddress>>();
// find the interfaces that support IPv4 and IPv6
List<NetworkInterface> nifs = Collections
.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nif: nifs) {
// ignore intertaces that are down or don't support multicast
if (!nif.isUp() || !nif.supportsMulticast() || nif.isLoopback()
|| (isMacOs && nif.getName().contains("awdl")))
continue;
List<InetAddress> addrs = Collections.list(nif.getInetAddresses());
for (InetAddress addr: addrs) {
if (!addr.isAnyLocalAddress()) {
if (addr instanceof Inet4Address) {
List<InetAddress> list = ip4Interfaces.get(nif);
if (list == null) {
list = new LinkedList<InetAddress>();
}
list.add(addr);
ip4Interfaces.put(nif, list);
} else if (addr instanceof Inet6Address) {
List<InetAddress> list = ip6Interfaces.get(nif);
if (list == null) {
list = new LinkedList<InetAddress>();
}
list.add(addr);
ip6Interfaces.put(nif, list);
}
}
}
}
return new NetworkConfiguration(ip4Interfaces, ip6Interfaces);
}
}

View File

@ -25,7 +25,8 @@
* @bug 8014377
* @summary Test for interference when two sockets are bound to the same
* port but joined to different multicast groups
* @build Promiscuous NetworkConfiguration
* @library /test/lib
* @build Promiscuous
* @run main Promiscuous
* @run main/othervm -Djava.net.preferIPv4Stack=true Promiscuous
* @key randomness
@ -37,6 +38,9 @@ import java.net.*;
import static java.net.StandardProtocolFamily.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;
import jdk.test.lib.NetworkConfiguration;
public class Promiscuous {
@ -208,7 +212,8 @@ public class Promiscuous {
InetAddress ip4Group1 = InetAddress.getByName("225.4.5.6");
InetAddress ip4Group2 = InetAddress.getByName("225.4.6.6");
for (NetworkInterface nif: config.ip4Interfaces()) {
for (NetworkInterface nif: config.ip4MulticastInterfaces()
.collect(Collectors.toList())) {
InetAddress source = config.ip4Addresses(nif).iterator().next();
test(INET, nif, ip4Group1, ip4Group2);

View File

@ -1,267 +0,0 @@
/*
* 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<NetworkInterface,List<Inet4Address>> ip4Interfaces;
private Map<NetworkInterface,List<Inet6Address>> ip6Interfaces;
private NetworkConfiguration(Map<NetworkInterface,List<Inet4Address>> ip4Interfaces,
Map<NetworkInterface,List<Inet6Address>> ip6Interfaces)
{
this.ip4Interfaces = ip4Interfaces;
this.ip6Interfaces = ip6Interfaces;
}
/**
* Returns a stream of interfaces suitable for functional tests.
*/
public Stream<NetworkInterface> interfaces() {
return Stream.concat(ip4Interfaces(), ip6Interfaces())
.distinct();
}
/**
* Returns a stream of interfaces suitable for IPv4 functional tests.
*/
public Stream<NetworkInterface> ip4Interfaces() {
return ip4Interfaces.keySet().stream()
.filter(NetworkConfiguration::isNotExcludedInterface)
.filter(hasIp4Addresses);
}
/**
* Returns a stream of interfaces suitable for IPv6 functional tests.
*/
public Stream<NetworkInterface> 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<NetworkInterface> hasIp4Addresses = nif -> {
Optional<?> addr = ip4Interfaces.get(nif).stream()
.filter(a -> !a.isAnyLocalAddress())
.findAny();
return addr.isPresent();
};
private final Predicate<NetworkInterface> 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<NetworkInterface> ip4MulticastInterfaces() {
return ip4Interfaces().filter(supportsIp4Multicast);
}
/**
* Returns a stream of interfaces suitable for IPv6 multicast tests.
*/
public Stream<NetworkInterface> ip6MulticastInterfaces() {
return ip6Interfaces().filter(supportsIp6Multicast);
}
private final Predicate<NetworkInterface> 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<NetworkInterface> 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<InetAddress> addresses(NetworkInterface nif) {
return Stream.concat(ip4Interfaces.get(nif).stream(),
ip6Interfaces.get(nif).stream());
}
/**
* Returns all IPv4 addresses on all "functional" interfaces.
*/
public Stream<Inet4Address> ip4Addresses() {
return ip4Interfaces().flatMap(nif -> ip4Addresses(nif));
}
/**
* Returns all IPv6 addresses on all "functional" interfaces.
*/
public Stream<Inet6Address> ip6Addresses() {
return ip6Interfaces().flatMap(nif -> ip6Addresses(nif));
}
/**
* Returns all IPv4 addresses the given interface.
*/
public Stream<Inet4Address> ip4Addresses(NetworkInterface nif) {
return ip4Interfaces.get(nif).stream();
}
/**
* Returns all IPv6 addresses for the given interface.
*/
public Stream<Inet6Address> ip6Addresses(NetworkInterface nif) {
return ip6Interfaces.get(nif).stream();
}
/**
* Return a NetworkConfiguration instance.
*/
public static NetworkConfiguration probe() throws IOException {
Map<NetworkInterface, List<Inet4Address>> ip4Interfaces = new HashMap<>();
Map<NetworkInterface, List<Inet6Address>> ip6Interfaces = new HashMap<>();
List<NetworkInterface> nifs = list(getNetworkInterfaces());
for (NetworkInterface nif : nifs) {
// ignore interfaces that are down
if (!nif.isUp() || nif.isPointToPoint())
continue;
List<Inet4Address> ip4Addresses = new LinkedList<>();
List<Inet6Address> 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<NetworkInterface> nifs = list(getNetworkInterfaces());
for (NetworkInterface nif : nifs)
out.print(interfaceInformation(nif));
out.println("*** end ***");
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}

View File

@ -31,9 +31,9 @@
* @bug 6216082
* @summary Redirect problem with HttpsURLConnection using a proxy
* @modules java.base/sun.net.www
* @library .. /lib/testlibrary
* @library .. /test/lib
* @build HttpCallback TestHttpsServer ClosedChannelList
* HttpTransaction TunnelProxy jdk.testlibrary.NetworkConfiguration
* HttpTransaction TunnelProxy
* @key intermittent
* @run main/othervm B6216082
*/
@ -43,7 +43,7 @@ import java.net.*;
import javax.net.ssl.*;
import java.util.*;
import jdk.testlibrary.NetworkConfiguration;
import jdk.test.lib.NetworkConfiguration;
public class B6216082 {
static SimpleHttpTransaction httpTrans;