Path: blob/master/test/jdk/java/net/NetworkInterface/UniqueMacAddressesTest.java
41149 views
/*1* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.PrintStream;24import java.io.UncheckedIOException;25import java.net.NetworkInterface;26import java.net.SocketException;27import java.util.ArrayList;28import java.util.Arrays;29import java.util.List;30import java.util.stream.Collectors;3132import jdk.test.lib.NetworkConfiguration;3334/*35* @test36* @bug 802137237* @summary Tests that the MAC addresses returned by NetworkInterface.getNetworkInterfaces are unique for each adapter.38* @library /test/lib39* @build jdk.test.lib.NetworkConfiguration40* @run main/othervm UniqueMacAddressesTest41*/42public class UniqueMacAddressesTest {4344static PrintStream log = System.err;4546// A record pair (NetworkInterface::name, NetworkInterface::hardwareAddress)47record NetIfPair(String interfaceName, byte[] address) {}4849public static void main(String[] args) throws Exception {50new UniqueMacAddressesTest().execute();51log.println("UniqueMacAddressesTest: OK");52}5354public UniqueMacAddressesTest() {55log.println("UniqueMacAddressesTest: start");56}5758public void execute() throws Exception {59// build a list of NetworkInterface name address pairs60// to test MAC address uniqueness61List<NetIfPair> netIfList = createNetworkInterfaceList(NetworkConfiguration.probe());62if (!macAddressesAreUnique(netIfList))63throw new RuntimeException("mac address uniqueness test failed");64}6566private boolean macAddressesAreUnique(List<NetIfPair> netIfPairs) {67for (NetIfPair netIfPair : netIfPairs) {68for (NetIfPair compNetIfPair : netIfPairs) {69if (!netIfPair.interfaceName.equals(compNetIfPair.interfaceName) &&70testMacAddressesEqual(netIfPair, compNetIfPair))71return false;72}73}74return true;75}7677private boolean testMacAddressesEqual(NetIfPair if1, NetIfPair if2) {78log.println("Compare hardware addresses of " + if1.interfaceName + " ("79+ createMacAddressString(if1.address) + ")" + " and " + if2.interfaceName80+ " (" + createMacAddressString(if2.address) + ")");81return (Arrays.equals(if1.address, if2.address));82}8384private String createMacAddressString(byte[] macAddr) {85StringBuilder sb = new StringBuilder();86if (macAddr != null) {87for (int i = 0; i < macAddr.length; i++) {88sb.append(String.format("%02X%s", macAddr[i],89(i < macAddr.length - 1) ? "-" : ""));90}91}92return sb.toString();93}9495private byte[] getNetworkInterfaceHardwareAddress(NetworkInterface inf) {96try {97return inf.getHardwareAddress();98} catch (SocketException se) {99throw new UncheckedIOException(se);100}101}102103private List<NetIfPair> createNetworkInterfaceList(NetworkConfiguration netConf) {104return netConf.interfaces()105.map(netIf -> new NetIfPair(netIf.getName(), getNetworkInterfaceHardwareAddress(netIf)))106.collect(Collectors.filtering(netIfPair -> netIfPair.address != null,107Collectors.toCollection(ArrayList::new)));108}109}110111112