Path: blob/master/test/jdk/java/net/NetworkInterface/Equals.java
41152 views
/*1* Copyright (c) 2011, 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 700339825* @run main/othervm -Djava.security.manager=allow Equals26*/2728import java.io.ByteArrayOutputStream;29import java.io.PrintStream;30import java.net.InetAddress;31import java.net.NetworkInterface;32import java.net.SocketException;33import java.util.Arrays;34import java.util.Collections;35import java.util.Enumeration;36import java.util.HashMap;3738public class Equals {3940static final boolean isWindows = System.getProperty("os.name").startsWith("Windows");4142public static void main(String args[]) throws Exception {43ByteArrayOutputStream baos = new ByteArrayOutputStream();44PrintStream bufferedOut = new PrintStream(baos);4546Enumeration<NetworkInterface> nifs1 = NetworkInterface.getNetworkInterfaces();47HashMap<String,Integer> hashes = new HashMap<>();48HashMap<String,NetworkInterface> nicMap = new HashMap<>();4950while (nifs1.hasMoreElements()) {51NetworkInterface ni = nifs1.nextElement();52hashes.put(ni.getName(),ni.hashCode());53nicMap.put(ni.getName(),ni);54displayInterfaceInformation(ni, bufferedOut);55bufferedOut.flush();56}5758System.setSecurityManager(new SecurityManager());5960Enumeration<NetworkInterface> nifs2 = NetworkInterface.getNetworkInterfaces();61while (nifs2.hasMoreElements()) {62NetworkInterface ni = nifs2.nextElement();6364// JDK-8022963, Skip (Windows)Teredo Tunneling Pseudo-Interface65String dName = ni.getDisplayName();66if (isWindows && dName != null && dName.contains("Teredo"))67continue;6869NetworkInterface niOrig = nicMap.get(ni.getName());7071int h = ni.hashCode();72if (h != hashes.get(ni.getName())) {73System.out.printf("%nSystem information:%n");74System.out.printf("%s", baos.toString("UTF8"));75System.out.printf("%nni.hashCode() returned %d, expected %d, for:%n",76h, hashes.get(ni.getName()));77displayInterfaceInformation(ni, System.out);78throw new RuntimeException("Hashcodes different for " +79ni.getName());80}81if (!ni.equals(niOrig)) {82System.out.printf("%nSystem information:%n");83System.out.printf("%s", baos.toString("UTF8"));84System.out.printf("%nExpected the following interfaces to be the same:%n");85displayInterfaceInformation(niOrig, System.out);86displayInterfaceInformation(ni, System.out);87throw new RuntimeException("equality different for " +88ni.getName());89}90}91}9293static void displayInterfaceInformation(NetworkInterface netint,94PrintStream out) throws SocketException {95out.printf("Display name: %s%n", netint.getDisplayName());96out.printf("Name: %s%n", netint.getName());97Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();9899for (InetAddress inetAddress : Collections.list(inetAddresses))100out.printf("InetAddress: %s%n", inetAddress);101102out.printf("Up? %s%n", netint.isUp());103out.printf("Loopback? %s%n", netint.isLoopback());104out.printf("PointToPoint? %s%n", netint.isPointToPoint());105out.printf("Supports multicast? %s%n", netint.supportsMulticast());106out.printf("Virtual? %s%n", netint.isVirtual());107out.printf("Hardware address: %s%n",108Arrays.toString(netint.getHardwareAddress()));109out.printf("MTU: %s%n", netint.getMTU());110out.printf("Index: %s%n", netint.getIndex());111out.printf("%n");112}113}114115116117