Path: blob/master/test/jdk/java/net/NetworkInterface/NullMacAddress.java
41149 views
/*1* Copyright (c) 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*/2223/* @test24* @bug 805930925* @summary Test that querrying the mac address of the loopback interface26* returns null and doesn't throw a SocketException.27* @library /test/lib28* @run testng/othervm NullMacAddress29* @run testng/othervm -Djava.net.preferIPv6Addresses=true NullMacAddress30* @run testng/othervm -Djava.net.preferIPv4Stack=true NullMacAddress31*/3233import org.testng.annotations.BeforeTest;34import org.testng.annotations.Test;35import static org.testng.Assert.*;3637import java.io.UncheckedIOException;38import java.math.BigInteger;39import java.net.NetworkInterface;40import java.net.SocketException;41import java.util.Locale;4243import jdk.test.lib.net.IPSupport;4445public class NullMacAddress {4647@BeforeTest48void setup() {49IPSupport.throwSkippedExceptionIfNonOperational();50}5152@Test53public void testNetworkInterfaces() throws SocketException {54NetworkInterface.networkInterfaces().forEach(this::testMacAddress);55}5657private void testMacAddress(NetworkInterface ni) {58try {59var name = ni.getDisplayName();60System.out.println("Testing: " + name);61var loopback = ni.isLoopback();62var macAddress = ni.getHardwareAddress();63var hdr = macAddress == null ? "null"64: "0x" + new BigInteger(1, macAddress)65.toString(16).toUpperCase(Locale.ROOT);66System.out.println(" MAC address: " + hdr + (loopback ? " (loopback)" : ""));67if (loopback) {68assertNull(macAddress, "Loopback interface \""69+ name + "\" doesn't have a null MAC Address");70}71} catch (SocketException ex) {72throw new UncheckedIOException(ex);73}74}75}76777879