undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
113
test/jdk/java/net/NetworkInterface/Equals.java
Normal file
113
test/jdk/java/net/NetworkInterface/Equals.java
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
/*
|
||||
* Copyright (c) 2011, 2024, 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 7003398
|
||||
* @run main/othervm Equals
|
||||
*/
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Equals {
|
||||
|
||||
static final boolean isWindows = System.getProperty("os.name").startsWith("Windows");
|
||||
|
||||
public static void main(String args[]) throws Exception {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
PrintStream bufferedOut = new PrintStream(baos);
|
||||
|
||||
Enumeration<NetworkInterface> nifs1 = NetworkInterface.getNetworkInterfaces();
|
||||
HashMap<String,Integer> hashes = new HashMap<>();
|
||||
HashMap<String,NetworkInterface> nicMap = new HashMap<>();
|
||||
|
||||
while (nifs1.hasMoreElements()) {
|
||||
NetworkInterface ni = nifs1.nextElement();
|
||||
hashes.put(ni.getName(),ni.hashCode());
|
||||
nicMap.put(ni.getName(),ni);
|
||||
displayInterfaceInformation(ni, bufferedOut);
|
||||
bufferedOut.flush();
|
||||
}
|
||||
|
||||
Enumeration<NetworkInterface> nifs2 = NetworkInterface.getNetworkInterfaces();
|
||||
while (nifs2.hasMoreElements()) {
|
||||
NetworkInterface ni = nifs2.nextElement();
|
||||
|
||||
// JDK-8022963, Skip (Windows)Teredo Tunneling Pseudo-Interface
|
||||
String dName = ni.getDisplayName();
|
||||
if (isWindows && dName != null && dName.contains("Teredo"))
|
||||
continue;
|
||||
|
||||
NetworkInterface niOrig = nicMap.get(ni.getName());
|
||||
|
||||
int h = ni.hashCode();
|
||||
if (h != hashes.get(ni.getName())) {
|
||||
System.out.printf("%nSystem information:%n");
|
||||
System.out.printf("%s", baos.toString("UTF8"));
|
||||
System.out.printf("%nni.hashCode() returned %d, expected %d, for:%n",
|
||||
h, hashes.get(ni.getName()));
|
||||
displayInterfaceInformation(ni, System.out);
|
||||
throw new RuntimeException("Hashcodes different for " +
|
||||
ni.getName());
|
||||
}
|
||||
if (!ni.equals(niOrig)) {
|
||||
System.out.printf("%nSystem information:%n");
|
||||
System.out.printf("%s", baos.toString("UTF8"));
|
||||
System.out.printf("%nExpected the following interfaces to be the same:%n");
|
||||
displayInterfaceInformation(niOrig, System.out);
|
||||
displayInterfaceInformation(ni, System.out);
|
||||
throw new RuntimeException("equality different for " +
|
||||
ni.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void displayInterfaceInformation(NetworkInterface netint,
|
||||
PrintStream out) throws SocketException {
|
||||
out.printf("Display name: %s%n", netint.getDisplayName());
|
||||
out.printf("Name: %s%n", netint.getName());
|
||||
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
|
||||
|
||||
for (InetAddress inetAddress : Collections.list(inetAddresses))
|
||||
out.printf("InetAddress: %s%n", inetAddress);
|
||||
|
||||
out.printf("Up? %s%n", netint.isUp());
|
||||
out.printf("Loopback? %s%n", netint.isLoopback());
|
||||
out.printf("PointToPoint? %s%n", netint.isPointToPoint());
|
||||
out.printf("Supports multicast? %s%n", netint.supportsMulticast());
|
||||
out.printf("Virtual? %s%n", netint.isVirtual());
|
||||
out.printf("Hardware address: %s%n",
|
||||
Arrays.toString(netint.getHardwareAddress()));
|
||||
out.printf("MTU: %s%n", netint.getMTU());
|
||||
out.printf("Index: %s%n", netint.getIndex());
|
||||
out.printf("%n");
|
||||
}
|
||||
}
|
||||
|
||||
131
test/jdk/java/net/NetworkInterface/GetMacAddress.java
Normal file
131
test/jdk/java/net/NetworkInterface/GetMacAddress.java
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
/*
|
||||
* 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
|
||||
* @bug 8182672
|
||||
* @summary Java 8u121 on Linux intermittently returns null for MAC address
|
||||
*/
|
||||
|
||||
import java.net.NetworkInterface;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.Phaser;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class GetMacAddress implements Callable<Exception> {
|
||||
static final int NUM_THREADS = 5;
|
||||
static final int NUM_ITERS = 100;
|
||||
static volatile boolean failed; // false
|
||||
|
||||
final String threadName;
|
||||
final NetworkInterface ni;
|
||||
final Phaser startingGate;
|
||||
|
||||
public GetMacAddress(NetworkInterface ni, String name, Phaser phaser) {
|
||||
this.ni = ni;
|
||||
this.threadName = name;
|
||||
this.startingGate = phaser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Exception call() {
|
||||
int count = 0;
|
||||
startingGate.arriveAndAwaitAdvance();
|
||||
try {
|
||||
for (int i = 0; i < NUM_ITERS; i++) {
|
||||
ni.getMTU();
|
||||
byte[] addr = ni.getHardwareAddress();
|
||||
if (addr == null) {
|
||||
System.out.println(threadName + ". mac id is null");
|
||||
failed = true;
|
||||
}
|
||||
count = count + 1;
|
||||
if (count % 100 == 0) {
|
||||
System.out.println(threadName + ". count is " + count);
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
System.out.println(threadName + ". Not expecting exception:" + ex.getMessage());
|
||||
failed = true;
|
||||
return ex;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static final Predicate<NetworkInterface> hasHardwareAddress = ni -> {
|
||||
try {
|
||||
if (ni.getHardwareAddress() == null) {
|
||||
System.out.println("Not testing null addr: " + ni.getName());
|
||||
return false;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
System.out.println("Not testing: " + ni.getName() +
|
||||
" " + ex.getMessage());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
public static Stream<NetworkInterface> getNetworkInterfacesAsStream() throws Exception {
|
||||
// JDK 9 and later
|
||||
return NetworkInterface.networkInterfaces();
|
||||
// pre JDK 9
|
||||
//return Collections.list(NetworkInterface.getNetworkInterfaces()).stream();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
List<NetworkInterface> toTest = getNetworkInterfacesAsStream()
|
||||
.filter(hasHardwareAddress)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
|
||||
|
||||
for (NetworkInterface ni : toTest) {
|
||||
Phaser startingGate = new Phaser(NUM_THREADS);
|
||||
System.out.println("Testing: " + ni.getName());
|
||||
List<Callable<Exception>> list = new ArrayList<>();
|
||||
for (int i = 0; i < NUM_THREADS; i++)
|
||||
list.add(new GetMacAddress(ni, ni.getName() + "-Thread-" + i, startingGate));
|
||||
List<Future<Exception>> futures = executor.invokeAll(list);
|
||||
for (Future<Exception> f : futures) {
|
||||
if (f.get() != null)
|
||||
f.get().printStackTrace(System.out);
|
||||
}
|
||||
if (failed)
|
||||
break;
|
||||
}
|
||||
executor.shutdownNow();
|
||||
if (!failed) {
|
||||
System.out.println("PASSED - Finished all threads");
|
||||
} else {
|
||||
throw new RuntimeException("Failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
73
test/jdk/java/net/NetworkInterface/IPv4Only.java
Normal file
73
test/jdk/java/net/NetworkInterface/IPv4Only.java
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
* Copyright (c) 2010, 2025, 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 6964714
|
||||
* @library /test/lib
|
||||
* @run main/othervm -Djava.net.preferIPv4Stack=true IPv4Only
|
||||
* @summary Test the networkinterface listing with java.net.preferIPv4Stack=true.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
import jdk.test.lib.net.IPSupport;
|
||||
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.util.Enumeration;
|
||||
|
||||
public class IPv4Only {
|
||||
public static void main(String[] args) throws Exception {
|
||||
IPSupport.printPlatformSupport(System.out);
|
||||
if (IPSupport.hasIPv4()) {
|
||||
System.out.println("Testing IPv4");
|
||||
Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
|
||||
while (nifs.hasMoreElements()) {
|
||||
NetworkInterface nif = nifs.nextElement();
|
||||
Enumeration<InetAddress> addrs = nif.getInetAddresses();
|
||||
while (addrs.hasMoreElements()) {
|
||||
InetAddress hostAddr = addrs.nextElement();
|
||||
if (hostAddr instanceof Inet6Address){
|
||||
throw new RuntimeException( "NetworkInterfaceV6List failed - found v6 address " + hostAddr.getHostAddress() );
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
NetworkInterface.getNetworkInterfaces();
|
||||
throw new RuntimeException("NetworkInterface.getNetworkInterfaces() should throw SocketException");
|
||||
} catch (SocketException expected) {
|
||||
System.out.println("caught expected exception: " + expected);
|
||||
}
|
||||
|
||||
try {
|
||||
NetworkInterface.networkInterfaces();
|
||||
throw new RuntimeException("NetworkInterface.networkInterfaces() should throw SocketException");
|
||||
} catch (SocketException expected) {
|
||||
System.out.println("caught expected exception: " + expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
94
test/jdk/java/net/NetworkInterface/IndexTest.java
Normal file
94
test/jdk/java/net/NetworkInterface/IndexTest.java
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
/*
|
||||
* Copyright (c) 2008, 2013, 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 6717876
|
||||
* @summary Make java.net.NetworkInterface.getIndex() public
|
||||
*/
|
||||
|
||||
import java.net.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import static java.lang.System.out;
|
||||
|
||||
public class IndexTest {
|
||||
static final boolean isWindows = System.getProperty("os.name").startsWith("Windows");
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Enumeration<NetworkInterface> netifs = NetworkInterface.getNetworkInterfaces();
|
||||
NetworkInterface nif;
|
||||
while (netifs.hasMoreElements()) {
|
||||
nif = netifs.nextElement();
|
||||
// JDK-8022212, Skip (Windows) Teredo Tunneling Pseudo-Interface
|
||||
String dName = nif.getDisplayName();
|
||||
if (isWindows && dName != null && dName.contains("Teredo"))
|
||||
continue;
|
||||
int index = nif.getIndex();
|
||||
if (index >= 0) {
|
||||
NetworkInterface nif2 = NetworkInterface.getByIndex(index);
|
||||
if (! nif.equals(nif2)) {
|
||||
out.printf("%nExpected interfaces to be the same, but got:%n");
|
||||
displayInterfaceInformation(nif);
|
||||
displayInterfaceInformation(nif2);
|
||||
throw new RuntimeException("both interfaces should be equal");
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
nif = NetworkInterface.getByIndex(-1);
|
||||
out.printf("%ngetByIndex(-1) should have thrown, but instead returned:%n");
|
||||
displayInterfaceInformation(nif);
|
||||
throw new RuntimeException("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException e) {
|
||||
// OK
|
||||
}
|
||||
// In all likelyhood, this interface should not exist.
|
||||
nif = NetworkInterface.getByIndex(Integer.MAX_VALUE - 1);
|
||||
if (nif != null) {
|
||||
out.printf("%ngetByIndex(MAX_VALUE - 1), expected null, got:%n");
|
||||
displayInterfaceInformation(nif);
|
||||
throw new RuntimeException("getByIndex() should have returned null");
|
||||
}
|
||||
}
|
||||
|
||||
static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
|
||||
out.printf("Display name: %s%n", netint.getDisplayName());
|
||||
out.printf("Name: %s%n", netint.getName());
|
||||
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
|
||||
|
||||
for (InetAddress inetAddress : Collections.list(inetAddresses))
|
||||
out.printf("InetAddress: %s%n", inetAddress);
|
||||
|
||||
out.printf("Up? %s%n", netint.isUp());
|
||||
out.printf("Loopback? %s%n", netint.isLoopback());
|
||||
out.printf("PointToPoint? %s%n", netint.isPointToPoint());
|
||||
out.printf("Supports multicast? %s%n", netint.supportsMulticast());
|
||||
out.printf("Virtual? %s%n", netint.isVirtual());
|
||||
out.printf("Hardware address: %s%n",
|
||||
Arrays.toString(netint.getHardwareAddress()));
|
||||
out.printf("MTU: %s%n", netint.getMTU());
|
||||
out.printf("Index: %s%n", netint.getIndex());
|
||||
out.printf("%n");
|
||||
}
|
||||
}
|
||||
74
test/jdk/java/net/NetworkInterface/NetParamsTest.java
Normal file
74
test/jdk/java/net/NetworkInterface/NetParamsTest.java
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* Copyright (c) 2005, 2011, 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 4691932
|
||||
* @summary Programmatic access to network parameters
|
||||
*/
|
||||
|
||||
import java.net.*;
|
||||
import java.util.Enumeration;
|
||||
|
||||
public class NetParamsTest {
|
||||
private static void printIF(NetworkInterface netif) throws SocketException {
|
||||
System.out.println(netif.getName() + " : ");
|
||||
System.out.println("\tStatus: " + (netif.isUp() ? " UP" : "DOWN"));
|
||||
byte[] mac = netif.getHardwareAddress();
|
||||
if (mac != null) {
|
||||
System.out.print("\tHardware Address: ");
|
||||
for (byte b : mac) {
|
||||
System.out.print(Integer.toHexString(b) + ":");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
System.out.println("\tLoopback: " + netif.isLoopback());
|
||||
System.out.println("\tPoint to Point: " + netif.isPointToPoint());
|
||||
System.out.println("\tVirtual: " + netif.isVirtual());
|
||||
if (netif.isVirtual()) {
|
||||
NetworkInterface parent = netif.getParent();
|
||||
String parentName = parent == null ? "null" : parent.getName();
|
||||
System.out.println("\tParent Interface: " + parentName);
|
||||
}
|
||||
System.out.println("\tMulticast: " + netif.supportsMulticast());
|
||||
|
||||
System.out.println("\tMTU: " + netif.getMTU());
|
||||
System.out.println("\tBindings:");
|
||||
java.util.List<InterfaceAddress> binds = netif.getInterfaceAddresses();
|
||||
for (InterfaceAddress b : binds) {
|
||||
System.out.println("\t\t" + b);
|
||||
}
|
||||
Enumeration<NetworkInterface> ifs = netif.getSubInterfaces();
|
||||
while(ifs.hasMoreElements()) {
|
||||
NetworkInterface subif = ifs.nextElement();
|
||||
printIF(subif);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces();
|
||||
while (ifs.hasMoreElements()) {
|
||||
NetworkInterface netif = ifs.nextElement();
|
||||
printIF(netif);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,93 @@
|
|||
/*
|
||||
* Copyright (c) 2017, 2019, 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 8179559 8225239
|
||||
* @library /test/lib
|
||||
* @modules java.base/java.net:open
|
||||
*/
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.util.Enumeration;
|
||||
import java.lang.reflect.Method;
|
||||
import jdk.test.lib.Platform;
|
||||
|
||||
public class NetworkInterfaceRetrievalTests {
|
||||
public static void main(String[] args) throws Exception {
|
||||
int checkFailureCount = 0;
|
||||
|
||||
Method isBound = NetworkInterface.class.getDeclaredMethod("isBoundInetAddress", InetAddress.class);
|
||||
isBound.setAccessible(true);
|
||||
|
||||
try {
|
||||
Enumeration<NetworkInterface> en = NetworkInterface
|
||||
.getNetworkInterfaces();
|
||||
while (en.hasMoreElements()) {
|
||||
NetworkInterface ni = en.nextElement();
|
||||
|
||||
//JDK-8230132: Should not test on Windows with Teredo Tunneling Pseudo-Interface
|
||||
String dName = ni.getDisplayName();
|
||||
if (Platform.isWindows() && dName != null && dName.contains("Teredo"))
|
||||
continue;
|
||||
|
||||
Enumeration<InetAddress> addrs = ni.getInetAddresses();
|
||||
System.out.println("############ Checking network interface + "
|
||||
+ ni + " #############");
|
||||
while (addrs.hasMoreElements()) {
|
||||
InetAddress addr = addrs.nextElement();
|
||||
System.out.println("************ Checking address + "
|
||||
+ addr + " *************");
|
||||
NetworkInterface addrNetIf = NetworkInterface
|
||||
.getByInetAddress(addr);
|
||||
if (addrNetIf.equals(ni)) {
|
||||
System.out.println("Retreived net if " + addrNetIf
|
||||
+ " equal to owning net if " + ni);
|
||||
} else {
|
||||
System.out.println("Retreived net if " + addrNetIf
|
||||
+ "NOT equal to owning net if " + ni
|
||||
+ "***********");
|
||||
checkFailureCount++;
|
||||
}
|
||||
|
||||
// Any bound address should return true when calling isBoundInetAddress
|
||||
if (!((boolean)isBound.invoke(null, addr))) {
|
||||
System.out.println("Retreived net if bound addr " + addr
|
||||
+ "NOT shown as bound using NetworkInterface.isBoundAddress "
|
||||
+ "***********");
|
||||
checkFailureCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception ex) {
|
||||
|
||||
}
|
||||
|
||||
if (checkFailureCount > 0) {
|
||||
throw new RuntimeException(
|
||||
"NetworkInterface lookup by address didn't match owner network interface");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,175 @@
|
|||
/*
|
||||
* Copyright (c) 2015, 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
|
||||
* @bug 8081678 8131155
|
||||
* @summary Tests for stream returning methods
|
||||
* @library /lib/testlibrary/bootlib /test/lib
|
||||
* @build java.base/java.util.stream.OpTestCase
|
||||
* @run testng/othervm NetworkInterfaceStreamTest
|
||||
* @run testng/othervm -Djava.net.preferIPv4Stack=true NetworkInterfaceStreamTest
|
||||
*/
|
||||
|
||||
import org.testng.annotations.BeforeTest;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.OpTestCase;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.TestData;
|
||||
|
||||
import jdk.test.lib.net.IPSupport;
|
||||
|
||||
public class NetworkInterfaceStreamTest extends OpTestCase {
|
||||
|
||||
private final static boolean IS_WINDOWS = System.getProperty("os.name").startsWith("Windows");
|
||||
|
||||
@BeforeTest
|
||||
void setup() {
|
||||
IPSupport.throwSkippedExceptionIfNonOperational();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNetworkInterfaces() throws SocketException {
|
||||
Supplier<Stream<NetworkInterface>> ss = () -> {
|
||||
try {
|
||||
return NetworkInterface.networkInterfaces()
|
||||
.filter(ni -> isIncluded(ni));
|
||||
}
|
||||
catch (SocketException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
|
||||
Collection<NetworkInterface> enums = Collections.list(NetworkInterface.getNetworkInterfaces());
|
||||
Collection<NetworkInterface> expected = new ArrayList<>();
|
||||
enums.forEach(ni -> {
|
||||
if (isIncluded(ni)) {
|
||||
expected.add(ni);
|
||||
}
|
||||
});
|
||||
withData(TestData.Factory.ofSupplier("Top-level network interfaces", ss))
|
||||
.stream(s -> s)
|
||||
.expectedResult(expected)
|
||||
.exercise();
|
||||
}
|
||||
|
||||
private Collection<NetworkInterface> getAllNetworkInterfaces() throws SocketException {
|
||||
Collection<NetworkInterface> anis = new ArrayList<>();
|
||||
for (NetworkInterface ni : Collections.list(NetworkInterface.getNetworkInterfaces())) {
|
||||
getAllSubNetworkInterfaces(ni, anis);
|
||||
}
|
||||
return anis;
|
||||
}
|
||||
|
||||
private void getAllSubNetworkInterfaces(NetworkInterface ni, Collection<NetworkInterface> result) {
|
||||
if (isIncluded(ni)) {
|
||||
result.add(ni);
|
||||
}
|
||||
|
||||
for (NetworkInterface sni : Collections.list(ni.getSubInterfaces())) {
|
||||
getAllSubNetworkInterfaces(sni, result);
|
||||
}
|
||||
}
|
||||
|
||||
private Stream<NetworkInterface> allNetworkInterfaces() throws SocketException {
|
||||
return NetworkInterface.networkInterfaces()
|
||||
.filter(ni -> isIncluded(ni))
|
||||
.flatMap(this::allSubNetworkInterfaces);
|
||||
}
|
||||
|
||||
private Stream<NetworkInterface> allSubNetworkInterfaces(NetworkInterface ni) {
|
||||
return Stream.concat(
|
||||
Stream.of(ni),
|
||||
ni.subInterfaces().filter(sni -> isIncluded(sni)).flatMap(this::allSubNetworkInterfaces));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubNetworkInterfaces() throws SocketException {
|
||||
Supplier<Stream<NetworkInterface>> ss = () -> {
|
||||
try {
|
||||
return allNetworkInterfaces();
|
||||
}
|
||||
catch (SocketException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
|
||||
Collection<NetworkInterface> expected = getAllNetworkInterfaces();
|
||||
withData(TestData.Factory.ofSupplier("All network interfaces", ss))
|
||||
.stream(s -> s)
|
||||
.expectedResult(expected)
|
||||
.exercise();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testInetAddresses() throws SocketException {
|
||||
Supplier<Stream<InetAddress>> ss = () -> {
|
||||
try {
|
||||
return NetworkInterface.networkInterfaces()
|
||||
.filter(ni -> isIncluded(ni))
|
||||
.flatMap(NetworkInterface::inetAddresses);
|
||||
}
|
||||
catch (SocketException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
|
||||
Collection<NetworkInterface> nis = Collections.list(NetworkInterface.getNetworkInterfaces());
|
||||
Collection<InetAddress> expected = new ArrayList<>();
|
||||
for (NetworkInterface ni : nis) {
|
||||
if (isIncluded(ni)) {
|
||||
expected.addAll(Collections.list(ni.getInetAddresses()));
|
||||
}
|
||||
}
|
||||
withData(TestData.Factory.ofSupplier("All inet addresses", ss))
|
||||
.stream(s -> s)
|
||||
.expectedResult(expected)
|
||||
.exercise();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the input network interface should be included in the test. It is necessary to exclude
|
||||
* "Teredo Tunneling Pseudo-Interface" whose configuration can be variable during a test run.
|
||||
*
|
||||
* @param ni a network interace
|
||||
* @return false if it is a "Teredo Tunneling Pseudo-Interface", otherwise true.
|
||||
*/
|
||||
private boolean isIncluded(NetworkInterface ni) {
|
||||
if (!IS_WINDOWS) {
|
||||
return true;
|
||||
}
|
||||
|
||||
String dName = ni.getDisplayName();
|
||||
return dName == null || !dName.contains("Teredo");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
77
test/jdk/java/net/NetworkInterface/NullMacAddress.java
Normal file
77
test/jdk/java/net/NetworkInterface/NullMacAddress.java
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* Copyright (c) 2020, 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 8059309
|
||||
* @summary Test that querrying the mac address of the loopback interface
|
||||
* returns null and doesn't throw a SocketException.
|
||||
* @library /test/lib
|
||||
* @run testng/othervm NullMacAddress
|
||||
* @run testng/othervm -Djava.net.preferIPv6Addresses=true NullMacAddress
|
||||
* @run testng/othervm -Djava.net.preferIPv4Stack=true NullMacAddress
|
||||
*/
|
||||
|
||||
import org.testng.annotations.BeforeTest;
|
||||
import org.testng.annotations.Test;
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
import java.io.UncheckedIOException;
|
||||
import java.math.BigInteger;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.util.Locale;
|
||||
|
||||
import jdk.test.lib.net.IPSupport;
|
||||
|
||||
public class NullMacAddress {
|
||||
|
||||
@BeforeTest
|
||||
void setup() {
|
||||
IPSupport.throwSkippedExceptionIfNonOperational();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNetworkInterfaces() throws SocketException {
|
||||
NetworkInterface.networkInterfaces().forEach(this::testMacAddress);
|
||||
}
|
||||
|
||||
private void testMacAddress(NetworkInterface ni) {
|
||||
try {
|
||||
var name = ni.getDisplayName();
|
||||
System.out.println("Testing: " + name);
|
||||
var loopback = ni.isLoopback();
|
||||
var macAddress = ni.getHardwareAddress();
|
||||
var hdr = macAddress == null ? "null"
|
||||
: "0x" + new BigInteger(1, macAddress)
|
||||
.toString(16).toUpperCase(Locale.ROOT);
|
||||
System.out.println(" MAC address: " + hdr + (loopback ? " (loopback)" : ""));
|
||||
if (loopback) {
|
||||
assertNull(macAddress, "Loopback interface \""
|
||||
+ name + "\" doesn't have a null MAC Address");
|
||||
}
|
||||
} catch (SocketException ex) {
|
||||
throw new UncheckedIOException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Copyright (c) 2016, 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 8168840
|
||||
* @summary InetAddress.getByName() throws java.net.UnknownHostException no such
|
||||
* interface when used with virtual interfaces on Solaris
|
||||
*/
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
|
||||
public class SubNetworkInterfaceTest {
|
||||
|
||||
public static void main(String args[]) throws SocketException, UnknownHostException {
|
||||
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
|
||||
for (NetworkInterface netIf : Collections.list(nets)) {
|
||||
doReverseLookup(netIf);
|
||||
}
|
||||
}
|
||||
|
||||
static void doReverseLookup(NetworkInterface netIf) throws SocketException, UnknownHostException {
|
||||
for (NetworkInterface subIf : Collections.list(netIf.getSubInterfaces())) {
|
||||
Enumeration<InetAddress> subInetAddresses = subIf.getInetAddresses();
|
||||
while (subInetAddresses != null && subInetAddresses.hasMoreElements()) {
|
||||
InetAddress inetAddress = subInetAddresses.nextElement();
|
||||
String reversalString = inetAddress.getHostAddress();
|
||||
//should not throw UHE in case of virtual sub interface
|
||||
InetAddress.getByName(reversalString);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
123
test/jdk/java/net/NetworkInterface/Test.java
Normal file
123
test/jdk/java/net/NetworkInterface/Test.java
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
/*
|
||||
* Copyright (c) 2001, 2013, 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 4405354 6594296 8058216
|
||||
* @library /test/lib
|
||||
* @run main/othervm -Xcheck:jni Test
|
||||
* @run main/othervm -Xcheck:jni -Djava.net.preferIPv4Stack=true Test
|
||||
* @summary Basic tests for NetworkInterface
|
||||
*/
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.InetAddress;
|
||||
import java.util.Enumeration;
|
||||
import jdk.test.lib.net.IPSupport;
|
||||
|
||||
public class Test {
|
||||
static final boolean isWindows = System.getProperty("os.name").startsWith("Windows");
|
||||
|
||||
public static void main(String args[]) throws Exception {
|
||||
IPSupport.throwSkippedExceptionIfNonOperational();
|
||||
|
||||
Enumeration nifs = NetworkInterface.getNetworkInterfaces();
|
||||
|
||||
while (nifs.hasMoreElements()) {
|
||||
NetworkInterface ni = (NetworkInterface)nifs.nextElement();
|
||||
|
||||
//JDK-8038276: Should not test on Windows with Teredo Tunneling Pseudo-Interface
|
||||
String dName = ni.getDisplayName();
|
||||
if (isWindows && dName != null && dName.contains("Teredo"))
|
||||
continue;
|
||||
|
||||
String name = ni.getName();
|
||||
System.out.println("\n" + name);
|
||||
|
||||
/*
|
||||
* Enumeration the IP addresses on this interface
|
||||
*/
|
||||
Enumeration addrs = ni.getInetAddresses();
|
||||
while (addrs.hasMoreElements()) {
|
||||
InetAddress addr = (InetAddress)addrs.nextElement();
|
||||
System.out.println(addr);
|
||||
if (NetworkInterface.getByInetAddress(addr) == null) {
|
||||
throw new Exception("getByInetAddress returned null");
|
||||
}
|
||||
}
|
||||
System.out.println("getInetAddresses() test passed.");
|
||||
|
||||
/*
|
||||
* Check equals and hashCode contract
|
||||
*/
|
||||
NetworkInterface ni2 = NetworkInterface.getByName(name);
|
||||
if (!ni2.equals(ni)) {
|
||||
throw new Exception("getByName returned: " + ni2);
|
||||
}
|
||||
if (!ni.equals(ni2)) {
|
||||
throw new Exception("equals specification broken");
|
||||
}
|
||||
System.out.println("equals() tests passed.");
|
||||
if (ni2.hashCode() != ni.hashCode()) {
|
||||
throw new Exception("hashCode contract broken");
|
||||
}
|
||||
System.out.println("hashCode() test passed.");
|
||||
|
||||
byte[] ba = ni.getHardwareAddress();
|
||||
if (ba != null && ba.length == 0) {
|
||||
throw new Exception("getHardwareAddress returned 0 length byte array");
|
||||
}
|
||||
System.out.println("getHardwareAddress() test passed.");
|
||||
}
|
||||
|
||||
// misc tests :-
|
||||
// getByXXX(null) should throw NPE
|
||||
// getByXXX("garbage") should return null
|
||||
|
||||
System.out.println("\nMiscellenous tests: ");
|
||||
|
||||
try {
|
||||
NetworkInterface.getByName(null);
|
||||
} catch (NullPointerException npe) {
|
||||
}
|
||||
System.out.println("getByName(null) test passed.");
|
||||
|
||||
try {
|
||||
NetworkInterface.getByInetAddress(null);
|
||||
} catch (NullPointerException npe) {
|
||||
}
|
||||
System.out.println("getByInetAddress(null) test passed.");
|
||||
|
||||
if (NetworkInterface.getByName("not-a-valid-name") != null) {
|
||||
throw new Exception
|
||||
("getByName returned unexpected interface: null expected");
|
||||
}
|
||||
System.out.println("getByName(<unknown>) test passed.");
|
||||
|
||||
InetAddress ia = InetAddress.getByName("255.255.255.255");
|
||||
if (NetworkInterface.getByInetAddress(ia) != null) {
|
||||
throw new Exception
|
||||
("getByInetAddress returned unexpected interface: null expected");
|
||||
}
|
||||
System.out.println("getByName(getByInetAddress(<unknown>) test passed.");
|
||||
|
||||
}
|
||||
}
|
||||
110
test/jdk/java/net/NetworkInterface/UniqueMacAddressesTest.java
Normal file
110
test/jdk/java/net/NetworkInterface/UniqueMacAddressesTest.java
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
/*
|
||||
* Copyright (c) 2013, 2020, 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.io.PrintStream;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import jdk.test.lib.NetworkConfiguration;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8021372
|
||||
* @summary Tests that the MAC addresses returned by NetworkInterface.getNetworkInterfaces are unique for each adapter.
|
||||
* @library /test/lib
|
||||
* @build jdk.test.lib.NetworkConfiguration
|
||||
* @run main/othervm UniqueMacAddressesTest
|
||||
*/
|
||||
public class UniqueMacAddressesTest {
|
||||
|
||||
static PrintStream log = System.err;
|
||||
|
||||
// A record pair (NetworkInterface::name, NetworkInterface::hardwareAddress)
|
||||
record NetIfPair(String interfaceName, byte[] address) {}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
new UniqueMacAddressesTest().execute();
|
||||
log.println("UniqueMacAddressesTest: OK");
|
||||
}
|
||||
|
||||
public UniqueMacAddressesTest() {
|
||||
log.println("UniqueMacAddressesTest: start");
|
||||
}
|
||||
|
||||
public void execute() throws Exception {
|
||||
// build a list of NetworkInterface name address pairs
|
||||
// to test MAC address uniqueness
|
||||
List<NetIfPair> netIfList = createNetworkInterfaceList(NetworkConfiguration.probe());
|
||||
if (!macAddressesAreUnique(netIfList))
|
||||
throw new RuntimeException("mac address uniqueness test failed");
|
||||
}
|
||||
|
||||
private boolean macAddressesAreUnique(List<NetIfPair> netIfPairs) {
|
||||
for (NetIfPair netIfPair : netIfPairs) {
|
||||
for (NetIfPair compNetIfPair : netIfPairs) {
|
||||
if (!netIfPair.interfaceName.equals(compNetIfPair.interfaceName) &&
|
||||
testMacAddressesEqual(netIfPair, compNetIfPair))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean testMacAddressesEqual(NetIfPair if1, NetIfPair if2) {
|
||||
log.println("Compare hardware addresses of " + if1.interfaceName + " ("
|
||||
+ createMacAddressString(if1.address) + ")" + " and " + if2.interfaceName
|
||||
+ " (" + createMacAddressString(if2.address) + ")");
|
||||
return (Arrays.equals(if1.address, if2.address));
|
||||
}
|
||||
|
||||
private String createMacAddressString(byte[] macAddr) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (macAddr != null) {
|
||||
for (int i = 0; i < macAddr.length; i++) {
|
||||
sb.append(String.format("%02X%s", macAddr[i],
|
||||
(i < macAddr.length - 1) ? "-" : ""));
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private byte[] getNetworkInterfaceHardwareAddress(NetworkInterface inf) {
|
||||
try {
|
||||
return inf.getHardwareAddress();
|
||||
} catch (SocketException se) {
|
||||
throw new UncheckedIOException(se);
|
||||
}
|
||||
}
|
||||
|
||||
private List<NetIfPair> createNetworkInterfaceList(NetworkConfiguration netConf) {
|
||||
return netConf.interfaces()
|
||||
.map(netIf -> new NetIfPair(netIf.getName(), getNetworkInterfaceHardwareAddress(netIf)))
|
||||
.collect(Collectors.filtering(netIfPair -> netIfPair.address != null,
|
||||
Collectors.toCollection(ArrayList::new)));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue