Skip to content

Commit f5d36e6

Browse files
c-clearydfuch
authored andcommitted
8246741: NetworkInterface/UniqueMacAddressesTest: mac address uniqueness test failed
Reviewed-by: chegar, dfuchs
1 parent 688b10b commit f5d36e6

File tree

1 file changed

+44
-66
lines changed

1 file changed

+44
-66
lines changed
Lines changed: 44 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,68 @@
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+
record NetIfPair(String interfaceName, byte[] address) {}
49+
4150
public static void main(String[] args) throws Exception {
4251
new UniqueMacAddressesTest().execute();
43-
System.out.println("UniqueMacAddressesTest: OK");
52+
log.println("UniqueMacAddressesTest: OK");
4453
}
4554

4655
public UniqueMacAddressesTest() {
47-
System.out.println("UniqueMacAddressesTest: start ");
56+
log.println("UniqueMacAddressesTest: start");
4857
}
4958

5059
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) {
60+
// build a list of NetworkInterface name address pairs
61+
// to test MAC address uniqueness
62+
List<NetIfPair> netIfList = createNetworkInterfaceList(NetworkConfiguration.probe());
63+
if (!macAddressesAreUnique(netIfList))
6164
throw new RuntimeException("mac address uniqueness test failed");
62-
}
6365
}
6466

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-
}
67+
private boolean macAddressesAreUnique(List<NetIfPair> netIfPairs) {
68+
for (NetIfPair netIfPair : netIfPairs) {
69+
for (NetIfPair compNetIfPair : netIfPairs) {
70+
if (!netIfPair.interfaceName.equals(compNetIfPair.interfaceName) &&
71+
testMacAddressesEqual(netIfPair, compNetIfPair))
72+
return false;
7773
}
78-
if (uniqueMacAddresses != true)
79-
break;
8074
}
81-
return uniqueMacAddresses;
75+
return true;
8276
}
8377

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;
78+
private boolean testMacAddressesEqual(NetIfPair if1, NetIfPair if2) {
79+
log.println("Compare hardware addresses of " + if1.interfaceName + " ("
80+
+ createMacAddressString(if1.address) + ")" + " and " + if2.interfaceName
81+
+ " (" + createMacAddressString(if2.address) + ")");
82+
return (Arrays.equals(if1.address, if2.address));
10183
}
10284

103-
private String createMacAddressString (NetworkInterface netIf) throws Exception {
104-
byte[] macAddr = netIf.getHardwareAddress();
85+
private String createMacAddressString(byte[] macAddr) {
10586
StringBuilder sb = new StringBuilder();
10687
if (macAddr != null) {
10788
for (int i = 0; i < macAddr.length; i++) {
@@ -112,21 +93,18 @@ private String createMacAddressString (NetworkInterface netIf) throws Exception
11293
return sb.toString();
11394
}
11495

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-
}
96+
private byte[] getNetworkInterfaceHardwareAddress(NetworkInterface inf) {
97+
try {
98+
return inf.getHardwareAddress();
99+
} catch (SocketException se) {
100+
throw new UncheckedIOException(se);
130101
}
131102
}
103+
104+
private List<NetIfPair> createNetworkInterfaceList(NetworkConfiguration netConf) {
105+
return netConf.interfaces()
106+
.map(netIf -> new NetIfPair(netIf.getName(), getNetworkInterfaceHardwareAddress(netIf)))
107+
.collect(Collectors.filtering(netIfPair -> netIfPair.address != null,
108+
Collectors.toCollection(ArrayList::new)));
109+
}
132110
}

0 commit comments

Comments
 (0)