1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
21
21
* questions.
22
22
*/
23
23
24
- import java .net .InetAddress ;
24
+ import java .io .PrintStream ;
25
+ import java .io .UncheckedIOException ;
25
26
import java .net .NetworkInterface ;
26
27
import java .net .SocketException ;
27
28
import java .util .ArrayList ;
28
29
import java .util .Arrays ;
29
- import java .util .Enumeration ;
30
30
import java .util .List ;
31
+ import java .util .stream .Collectors ;
31
32
33
+ import jdk .test .lib .NetworkConfiguration ;
32
34
33
35
/*
34
36
* @test
35
37
* @bug 8021372
36
38
* @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
38
42
*/
39
43
public class UniqueMacAddressesTest {
40
44
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
+
41
57
public static void main (String [] args ) throws Exception {
42
58
new UniqueMacAddressesTest ().execute ();
43
- System . out .println ("UniqueMacAddressesTest: OK" );
59
+ log .println ("UniqueMacAddressesTest: OK" );
44
60
}
45
61
46
62
public UniqueMacAddressesTest () {
47
- System . out . println ("UniqueMacAddressesTest: start " );
63
+ log . println ("UniqueMacAddressesTest: start" );
48
64
}
49
65
50
66
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 ))
61
71
throw new RuntimeException ("mac address uniqueness test failed" );
62
- }
63
72
}
64
73
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 ;
77
80
}
78
- if (uniqueMacAddresses != true )
79
- break ;
80
81
}
81
- return uniqueMacAddresses ;
82
+ return true ;
82
83
}
83
84
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 ));
101
90
}
102
91
103
- private String createMacAddressString (NetworkInterface netIf ) throws Exception {
104
- byte [] macAddr = netIf .getHardwareAddress ();
92
+ private String createMacAddressString (byte [] macAddr ) {
105
93
StringBuilder sb = new StringBuilder ();
106
94
if (macAddr != null ) {
107
95
for (int i = 0 ; i < macAddr .length ; i ++) {
@@ -112,21 +100,18 @@ private String createMacAddressString (NetworkInterface netIf) throws Exception
112
100
return sb .toString ();
113
101
}
114
102
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 );
130
108
}
131
109
}
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
+ }
132
117
}
0 commit comments