Path: blob/master/test/jdk/java/net/NetworkInterface/NetworkInterfaceRetrievalTests.java
41149 views
/*1* Copyright (c) 2017, 2019, 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/**24* @test25* @bug 8179559 822523926* @library /test/lib27* @modules java.base/java.net:open28*/2930import java.net.InetAddress;31import java.net.NetworkInterface;32import java.util.Enumeration;33import java.lang.reflect.Method;34import jdk.test.lib.Platform;3536public class NetworkInterfaceRetrievalTests {37public static void main(String[] args) throws Exception {38int checkFailureCount = 0;3940Method isBound = NetworkInterface.class.getDeclaredMethod("isBoundInetAddress", InetAddress.class);41isBound.setAccessible(true);4243try {44Enumeration<NetworkInterface> en = NetworkInterface45.getNetworkInterfaces();46while (en.hasMoreElements()) {47NetworkInterface ni = en.nextElement();4849//JDK-8230132: Should not test on Windows with Teredo Tunneling Pseudo-Interface50String dName = ni.getDisplayName();51if (Platform.isWindows() && dName != null && dName.contains("Teredo"))52continue;5354Enumeration<InetAddress> addrs = ni.getInetAddresses();55System.out.println("############ Checking network interface + "56+ ni + " #############");57while (addrs.hasMoreElements()) {58InetAddress addr = addrs.nextElement();59System.out.println("************ Checking address + "60+ addr + " *************");61NetworkInterface addrNetIf = NetworkInterface62.getByInetAddress(addr);63if (addrNetIf.equals(ni)) {64System.out.println("Retreived net if " + addrNetIf65+ " equal to owning net if " + ni);66} else {67System.out.println("Retreived net if " + addrNetIf68+ "NOT equal to owning net if " + ni69+ "***********");70checkFailureCount++;71}7273// Any bound address should return true when calling isBoundInetAddress74if (!((boolean)isBound.invoke(null, addr))) {75System.out.println("Retreived net if bound addr " + addr76+ "NOT shown as bound using NetworkInterface.isBoundAddress "77+ "***********");78checkFailureCount++;79}80}81}8283} catch (Exception ex) {8485}8687if (checkFailureCount > 0) {88throw new RuntimeException(89"NetworkInterface lookup by address didn't match owner network interface");90}91}92}939495