Path: blob/master/test/jdk/java/net/NetworkInterface/IndexTest.java
41149 views
/*1* Copyright (c) 2008, 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*/2223/* @test24* @bug 671787625* @summary Make java.net.NetworkInterface.getIndex() public26*/2728import java.net.*;29import java.util.Arrays;30import java.util.Collections;31import java.util.Enumeration;32import static java.lang.System.out;3334public class IndexTest {35static final boolean isWindows = System.getProperty("os.name").startsWith("Windows");3637public static void main(String[] args) throws Exception {38Enumeration<NetworkInterface> netifs = NetworkInterface.getNetworkInterfaces();39NetworkInterface nif;40while (netifs.hasMoreElements()) {41nif = netifs.nextElement();42// JDK-8022212, Skip (Windows) Teredo Tunneling Pseudo-Interface43String dName = nif.getDisplayName();44if (isWindows && dName != null && dName.contains("Teredo"))45continue;46int index = nif.getIndex();47if (index >= 0) {48NetworkInterface nif2 = NetworkInterface.getByIndex(index);49if (! nif.equals(nif2)) {50out.printf("%nExpected interfaces to be the same, but got:%n");51displayInterfaceInformation(nif);52displayInterfaceInformation(nif2);53throw new RuntimeException("both interfaces should be equal");54}55}56}57try {58nif = NetworkInterface.getByIndex(-1);59out.printf("%ngetByIndex(-1) should have thrown, but instead returned:%n");60displayInterfaceInformation(nif);61throw new RuntimeException("Should have thrown IllegalArgumentException");62} catch (IllegalArgumentException e) {63// OK64}65// In all likelyhood, this interface should not exist.66nif = NetworkInterface.getByIndex(Integer.MAX_VALUE - 1);67if (nif != null) {68out.printf("%ngetByIndex(MAX_VALUE - 1), expected null, got:%n");69displayInterfaceInformation(nif);70throw new RuntimeException("getByIndex() should have returned null");71}72}7374static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {75out.printf("Display name: %s%n", netint.getDisplayName());76out.printf("Name: %s%n", netint.getName());77Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();7879for (InetAddress inetAddress : Collections.list(inetAddresses))80out.printf("InetAddress: %s%n", inetAddress);8182out.printf("Up? %s%n", netint.isUp());83out.printf("Loopback? %s%n", netint.isLoopback());84out.printf("PointToPoint? %s%n", netint.isPointToPoint());85out.printf("Supports multicast? %s%n", netint.supportsMulticast());86out.printf("Virtual? %s%n", netint.isVirtual());87out.printf("Hardware address: %s%n",88Arrays.toString(netint.getHardwareAddress()));89out.printf("MTU: %s%n", netint.getMTU());90out.printf("Index: %s%n", netint.getIndex());91out.printf("%n");92}93}949596