Skip to content

Commit ed275ba

Browse files
committed
8246741: NetworkInterface/UniqueMacAddressesTest: mac address uniqueness test failed
Reviewed-by: mdoerr Backport-of: f5d36e6
1 parent 2ed46d8 commit ed275ba

File tree

1 file changed

+51
-66
lines changed

1 file changed

+51
-66
lines changed
Lines changed: 51 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,87 +21,75 @@
2121
* questions.
2222
*/
2323

24-
import java.net.InetAddress;
24+
import java.io.PrintStream;
25+
import java.io.UncheckedIOException;
2526
import java.net.NetworkInterface;
2627
import java.net.SocketException;
2728
import java.util.ArrayList;
2829
import java.util.Arrays;
29-
import java.util.Enumeration;
3030
import java.util.List;
31+
import java.util.stream.Collectors;
3132

33+
import jdk.test.lib.NetworkConfiguration;
3234

3335
/*
3436
* @test
3537
* @bug 8021372
3638
* @summary Tests that the MAC addresses returned by NetworkInterface.getNetworkInterfaces are unique for each adapter.
37-
*
39+
* @library /test/lib
40+
* @build jdk.test.lib.NetworkConfiguration
41+
* @run main/othervm UniqueMacAddressesTest
3842
*/
3943
public class UniqueMacAddressesTest {
4044

45+
static PrintStream log = System.err;
46+
47+
// A record pair (NetworkInterface::name, NetworkInterface::hardwareAddress)
48+
class NetIfPair {
49+
String interfaceName;
50+
byte[] address;
51+
public NetIfPair(String name, byte[] adr) {
52+
interfaceName = name;
53+
address = adr;
54+
}
55+
}
56+
4157
public static void main(String[] args) throws Exception {
4258
new UniqueMacAddressesTest().execute();
43-
System.out.println("UniqueMacAddressesTest: OK");
59+
log.println("UniqueMacAddressesTest: OK");
4460
}
4561

4662
public UniqueMacAddressesTest() {
47-
System.out.println("UniqueMacAddressesTest: start ");
63+
log.println("UniqueMacAddressesTest: start");
4864
}
4965

5066
public void execute() throws Exception {
51-
Enumeration<NetworkInterface> networkInterfaces;
52-
boolean areMacAddressesUnique = false;
53-
List<NetworkInterface> networkInterfaceList = new ArrayList<NetworkInterface>();
54-
networkInterfaces = NetworkInterface.getNetworkInterfaces();
55-
56-
// build a list of NetworkInterface objects to test MAC address
57-
// uniqueness
58-
createNetworkInterfaceList(networkInterfaces, networkInterfaceList);
59-
areMacAddressesUnique = checkMacAddressesAreUnique(networkInterfaceList);
60-
if (!areMacAddressesUnique) {
67+
// build a list of NetworkInterface name address pairs
68+
// to test MAC address uniqueness
69+
List<NetIfPair> netIfList = createNetworkInterfaceList(NetworkConfiguration.probe());
70+
if (!macAddressesAreUnique(netIfList))
6171
throw new RuntimeException("mac address uniqueness test failed");
62-
}
6372
}
6473

65-
private boolean checkMacAddressesAreUnique (
66-
List<NetworkInterface> networkInterfaces) throws Exception {
67-
boolean uniqueMacAddresses = true;
68-
for (NetworkInterface networkInterface : networkInterfaces) {
69-
for (NetworkInterface comparisonNetIf : networkInterfaces) {
70-
System.out.println("Comparing netif "
71-
+ networkInterface.getName() + " and netif "
72-
+ comparisonNetIf.getName());
73-
if (testMacAddressesEqual(networkInterface, comparisonNetIf)) {
74-
uniqueMacAddresses = false;
75-
break;
76-
}
74+
private boolean macAddressesAreUnique(List<NetIfPair> netIfPairs) {
75+
for (NetIfPair netIfPair : netIfPairs) {
76+
for (NetIfPair compNetIfPair : netIfPairs) {
77+
if (!netIfPair.interfaceName.equals(compNetIfPair.interfaceName) &&
78+
testMacAddressesEqual(netIfPair, compNetIfPair))
79+
return false;
7780
}
78-
if (uniqueMacAddresses != true)
79-
break;
8081
}
81-
return uniqueMacAddresses;
82+
return true;
8283
}
8384

84-
private boolean testMacAddressesEqual(NetworkInterface netIf1,
85-
NetworkInterface netIf2) throws Exception {
86-
87-
byte[] rawMacAddress1 = null;
88-
byte[] rawMacAddress2 = null;
89-
boolean macAddressesEqual = false;
90-
if (!netIf1.getName().equals(netIf2.getName())) {
91-
System.out.println("compare hardware addresses "
92-
+ createMacAddressString(netIf1) + " and " + createMacAddressString(netIf2));
93-
rawMacAddress1 = netIf1.getHardwareAddress();
94-
rawMacAddress2 = netIf2.getHardwareAddress();
95-
macAddressesEqual = Arrays.equals(rawMacAddress1, rawMacAddress2);
96-
} else {
97-
// same interface
98-
macAddressesEqual = false;
99-
}
100-
return macAddressesEqual;
85+
private boolean testMacAddressesEqual(NetIfPair if1, NetIfPair if2) {
86+
log.println("Compare hardware addresses of " + if1.interfaceName + " ("
87+
+ createMacAddressString(if1.address) + ")" + " and " + if2.interfaceName
88+
+ " (" + createMacAddressString(if2.address) + ")");
89+
return (Arrays.equals(if1.address, if2.address));
10190
}
10291

103-
private String createMacAddressString (NetworkInterface netIf) throws Exception {
104-
byte[] macAddr = netIf.getHardwareAddress();
92+
private String createMacAddressString(byte[] macAddr) {
10593
StringBuilder sb = new StringBuilder();
10694
if (macAddr != null) {
10795
for (int i = 0; i < macAddr.length; i++) {
@@ -112,21 +100,18 @@ private String createMacAddressString (NetworkInterface netIf) throws Exception
112100
return sb.toString();
113101
}
114102

115-
private void createNetworkInterfaceList(Enumeration<NetworkInterface> nis,
116-
List<NetworkInterface> networkInterfaceList) throws Exception {
117-
byte[] macAddr = null;
118-
NetworkInterface netIf = null;
119-
while (nis.hasMoreElements()) {
120-
netIf = (NetworkInterface) nis.nextElement();
121-
if (netIf.isUp()) {
122-
macAddr = netIf.getHardwareAddress();
123-
if (macAddr != null) {
124-
System.out.println("Adding NetworkInterface "
125-
+ netIf.getName() + " with mac address "
126-
+ createMacAddressString(netIf));
127-
networkInterfaceList.add(netIf);
128-
}
129-
}
103+
private byte[] getNetworkInterfaceHardwareAddress(NetworkInterface inf) {
104+
try {
105+
return inf.getHardwareAddress();
106+
} catch (SocketException se) {
107+
throw new UncheckedIOException(se);
130108
}
131109
}
110+
111+
private List<NetIfPair> createNetworkInterfaceList(NetworkConfiguration netConf) {
112+
return netConf.interfaces()
113+
.map(netIf -> new NetIfPair(netIf.getName(), getNetworkInterfaceHardwareAddress(netIf)))
114+
.collect(Collectors.filtering(netIfPair -> netIfPair.address != null,
115+
Collectors.toCollection(ArrayList::new)));
116+
}
132117
}

0 commit comments

Comments
 (0)