Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/NetworkInterface/UniqueMacAddressesTest.java
38811 views
1
/*
2
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.net.InetAddress;
25
import java.net.NetworkInterface;
26
import java.net.SocketException;
27
import java.util.ArrayList;
28
import java.util.Arrays;
29
import java.util.Enumeration;
30
import java.util.List;
31
32
33
/*
34
* @test
35
* @bug 8021372
36
* @summary Tests that the MAC addresses returned by NetworkInterface.getNetworkInterfaces are unique for each adapter.
37
*
38
*/
39
public class UniqueMacAddressesTest {
40
41
public static void main(String[] args) throws Exception {
42
new UniqueMacAddressesTest().execute();
43
System.out.println("UniqueMacAddressesTest: OK");
44
}
45
46
public UniqueMacAddressesTest() {
47
System.out.println("UniqueMacAddressesTest: start ");
48
}
49
50
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) {
61
throw new RuntimeException("mac address uniqueness test failed");
62
}
63
}
64
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
}
77
}
78
if (uniqueMacAddresses != true)
79
break;
80
}
81
return uniqueMacAddresses;
82
}
83
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;
101
}
102
103
private String createMacAddressString (NetworkInterface netIf) throws Exception {
104
byte[] macAddr = netIf.getHardwareAddress();
105
StringBuilder sb = new StringBuilder();
106
if (macAddr != null) {
107
for (int i = 0; i < macAddr.length; i++) {
108
sb.append(String.format("%02X%s", macAddr[i],
109
(i < macAddr.length - 1) ? "-" : ""));
110
}
111
}
112
return sb.toString();
113
}
114
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
}
130
}
131
}
132
}
133
134