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
+ record NetIfPair (String interfaceName , byte [] address ) {}
49
+
41
50
public static void main (String [] args ) throws Exception {
42
51
new UniqueMacAddressesTest ().execute ();
43
- System . out .println ("UniqueMacAddressesTest: OK" );
52
+ log .println ("UniqueMacAddressesTest: OK" );
44
53
}
45
54
46
55
public UniqueMacAddressesTest () {
47
- System . out . println ("UniqueMacAddressesTest: start " );
56
+ log . println ("UniqueMacAddressesTest: start" );
48
57
}
49
58
50
59
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 ))
61
64
throw new RuntimeException ("mac address uniqueness test failed" );
62
- }
63
65
}
64
66
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 ;
77
73
}
78
- if (uniqueMacAddresses != true )
79
- break ;
80
74
}
81
- return uniqueMacAddresses ;
75
+ return true ;
82
76
}
83
77
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 ));
101
83
}
102
84
103
- private String createMacAddressString (NetworkInterface netIf ) throws Exception {
104
- byte [] macAddr = netIf .getHardwareAddress ();
85
+ private String createMacAddressString (byte [] macAddr ) {
105
86
StringBuilder sb = new StringBuilder ();
106
87
if (macAddr != null ) {
107
88
for (int i = 0 ; i < macAddr .length ; i ++) {
@@ -112,21 +93,18 @@ private String createMacAddressString (NetworkInterface netIf) throws Exception
112
93
return sb .toString ();
113
94
}
114
95
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 );
130
101
}
131
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
+ }
132
110
}
0 commit comments