Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/NetworkInterface/UniqueMacAddressesTest.java
41149 views
1
/*
2
* Copyright (c) 2013, 2020, 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.io.PrintStream;
25
import java.io.UncheckedIOException;
26
import java.net.NetworkInterface;
27
import java.net.SocketException;
28
import java.util.ArrayList;
29
import java.util.Arrays;
30
import java.util.List;
31
import java.util.stream.Collectors;
32
33
import jdk.test.lib.NetworkConfiguration;
34
35
/*
36
* @test
37
* @bug 8021372
38
* @summary Tests that the MAC addresses returned by NetworkInterface.getNetworkInterfaces are unique for each adapter.
39
* @library /test/lib
40
* @build jdk.test.lib.NetworkConfiguration
41
* @run main/othervm UniqueMacAddressesTest
42
*/
43
public class UniqueMacAddressesTest {
44
45
static PrintStream log = System.err;
46
47
// A record pair (NetworkInterface::name, NetworkInterface::hardwareAddress)
48
record NetIfPair(String interfaceName, byte[] address) {}
49
50
public static void main(String[] args) throws Exception {
51
new UniqueMacAddressesTest().execute();
52
log.println("UniqueMacAddressesTest: OK");
53
}
54
55
public UniqueMacAddressesTest() {
56
log.println("UniqueMacAddressesTest: start");
57
}
58
59
public void execute() throws Exception {
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))
64
throw new RuntimeException("mac address uniqueness test failed");
65
}
66
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;
73
}
74
}
75
return true;
76
}
77
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));
83
}
84
85
private String createMacAddressString(byte[] macAddr) {
86
StringBuilder sb = new StringBuilder();
87
if (macAddr != null) {
88
for (int i = 0; i < macAddr.length; i++) {
89
sb.append(String.format("%02X%s", macAddr[i],
90
(i < macAddr.length - 1) ? "-" : ""));
91
}
92
}
93
return sb.toString();
94
}
95
96
private byte[] getNetworkInterfaceHardwareAddress(NetworkInterface inf) {
97
try {
98
return inf.getHardwareAddress();
99
} catch (SocketException se) {
100
throw new UncheckedIOException(se);
101
}
102
}
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
}
110
}
111
112