Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/NetworkInterface/UniqueMacAddressesTest.java
38811 views
/*1* Copyright (c) 2013, 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.net.InetAddress;24import java.net.NetworkInterface;25import java.net.SocketException;26import java.util.ArrayList;27import java.util.Arrays;28import java.util.Enumeration;29import java.util.List;303132/*33* @test34* @bug 802137235* @summary Tests that the MAC addresses returned by NetworkInterface.getNetworkInterfaces are unique for each adapter.36*37*/38public class UniqueMacAddressesTest {3940public static void main(String[] args) throws Exception {41new UniqueMacAddressesTest().execute();42System.out.println("UniqueMacAddressesTest: OK");43}4445public UniqueMacAddressesTest() {46System.out.println("UniqueMacAddressesTest: start ");47}4849public void execute() throws Exception {50Enumeration<NetworkInterface> networkInterfaces;51boolean areMacAddressesUnique = false;52List<NetworkInterface> networkInterfaceList = new ArrayList<NetworkInterface>();53networkInterfaces = NetworkInterface.getNetworkInterfaces();5455// build a list of NetworkInterface objects to test MAC address56// uniqueness57createNetworkInterfaceList(networkInterfaces, networkInterfaceList);58areMacAddressesUnique = checkMacAddressesAreUnique(networkInterfaceList);59if (!areMacAddressesUnique) {60throw new RuntimeException("mac address uniqueness test failed");61}62}6364private boolean checkMacAddressesAreUnique (65List<NetworkInterface> networkInterfaces) throws Exception {66boolean uniqueMacAddresses = true;67for (NetworkInterface networkInterface : networkInterfaces) {68for (NetworkInterface comparisonNetIf : networkInterfaces) {69System.out.println("Comparing netif "70+ networkInterface.getName() + " and netif "71+ comparisonNetIf.getName());72if (testMacAddressesEqual(networkInterface, comparisonNetIf)) {73uniqueMacAddresses = false;74break;75}76}77if (uniqueMacAddresses != true)78break;79}80return uniqueMacAddresses;81}8283private boolean testMacAddressesEqual(NetworkInterface netIf1,84NetworkInterface netIf2) throws Exception {8586byte[] rawMacAddress1 = null;87byte[] rawMacAddress2 = null;88boolean macAddressesEqual = false;89if (!netIf1.getName().equals(netIf2.getName())) {90System.out.println("compare hardware addresses "91+ createMacAddressString(netIf1) + " and " + createMacAddressString(netIf2));92rawMacAddress1 = netIf1.getHardwareAddress();93rawMacAddress2 = netIf2.getHardwareAddress();94macAddressesEqual = Arrays.equals(rawMacAddress1, rawMacAddress2);95} else {96// same interface97macAddressesEqual = false;98}99return macAddressesEqual;100}101102private String createMacAddressString (NetworkInterface netIf) throws Exception {103byte[] macAddr = netIf.getHardwareAddress();104StringBuilder sb = new StringBuilder();105if (macAddr != null) {106for (int i = 0; i < macAddr.length; i++) {107sb.append(String.format("%02X%s", macAddr[i],108(i < macAddr.length - 1) ? "-" : ""));109}110}111return sb.toString();112}113114private void createNetworkInterfaceList(Enumeration<NetworkInterface> nis,115List<NetworkInterface> networkInterfaceList) throws Exception {116byte[] macAddr = null;117NetworkInterface netIf = null;118while (nis.hasMoreElements()) {119netIf = (NetworkInterface) nis.nextElement();120if (netIf.isUp()) {121macAddr = netIf.getHardwareAddress();122if (macAddr != null) {123System.out.println("Adding NetworkInterface "124+ netIf.getName() + " with mac address "125+ createMacAddressString(netIf));126networkInterfaceList.add(netIf);127}128}129}130}131}132133134