Path: blob/master/test/jdk/java/net/NetworkInterface/Test.java
41149 views
/*1* Copyright (c) 2001, 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 4405354 6594296 805821625* @library /test/lib26* @run main/othervm -Xcheck:jni Test27* @run main/othervm -Xcheck:jni -Djava.net.preferIPv4Stack=true Test28* @summary Basic tests for NetworkInterface29*/30import java.net.NetworkInterface;31import java.net.InetAddress;32import java.util.Enumeration;33import jdk.test.lib.net.IPSupport;3435public class Test {36static final boolean isWindows = System.getProperty("os.name").startsWith("Windows");3738public static void main(String args[]) throws Exception {39IPSupport.throwSkippedExceptionIfNonOperational();4041Enumeration nifs = NetworkInterface.getNetworkInterfaces();4243while (nifs.hasMoreElements()) {44NetworkInterface ni = (NetworkInterface)nifs.nextElement();4546//JDK-8038276: Should not test on Windows with Teredo Tunneling Pseudo-Interface47String dName = ni.getDisplayName();48if (isWindows && dName != null && dName.contains("Teredo"))49continue;5051String name = ni.getName();52System.out.println("\n" + name);5354/*55* Enumeration the IP addresses on this interface56*/57Enumeration addrs = ni.getInetAddresses();58while (addrs.hasMoreElements()) {59InetAddress addr = (InetAddress)addrs.nextElement();60System.out.println(addr);61if (NetworkInterface.getByInetAddress(addr) == null) {62throw new Exception("getByInetAddress returned null");63}64}65System.out.println("getInetAddresses() test passed.");6667/*68* Check equals and hashCode contract69*/70NetworkInterface ni2 = NetworkInterface.getByName(name);71if (!ni2.equals(ni)) {72throw new Exception("getByName returned: " + ni2);73}74if (!ni.equals(ni2)) {75throw new Exception("equals specification broken");76}77System.out.println("equals() tests passed.");78if (ni2.hashCode() != ni.hashCode()) {79throw new Exception("hashCode contract broken");80}81System.out.println("hashCode() test passed.");8283byte[] ba = ni.getHardwareAddress();84if (ba != null && ba.length == 0) {85throw new Exception("getHardwareAddress returned 0 length byte array");86}87System.out.println("getHardwareAddress() test passed.");88}8990// misc tests :-91// getByXXX(null) should throw NPE92// getByXXX("garbage") should return null9394System.out.println("\nMiscellenous tests: ");9596try {97NetworkInterface.getByName(null);98} catch (NullPointerException npe) {99}100System.out.println("getByName(null) test passed.");101102try {103NetworkInterface.getByInetAddress(null);104} catch (NullPointerException npe) {105}106System.out.println("getByInetAddress(null) test passed.");107108if (NetworkInterface.getByName("not-a-valid-name") != null) {109throw new Exception110("getByName returned unexpected interface: null expected");111}112System.out.println("getByName(<unknown>) test passed.");113114InetAddress ia = InetAddress.getByName("255.255.255.255");115if (NetworkInterface.getByInetAddress(ia) != null) {116throw new Exception117("getByInetAddress returned unexpected interface: null expected");118}119System.out.println("getByName(getByInetAddress(<unknown>) test passed.");120121}122}123124125