Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/NetworkInterface/Test.java
38811 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* @run main Test26* @run main/othervm -Djava.net.preferIPv4Stack=true Test27* @summary Basic tests for NetworkInterface28*/29import java.net.NetworkInterface;30import java.net.InetAddress;31import java.util.Enumeration;3233public class Test {3435public static void main(String args[]) throws Exception {3637Enumeration nifs = NetworkInterface.getNetworkInterfaces();3839while (nifs.hasMoreElements()) {40NetworkInterface ni = (NetworkInterface)nifs.nextElement();4142String name = ni.getName();43System.out.println("\n" + name);4445/*46* Enumeration the IP addresses on this interface47*/48Enumeration addrs = ni.getInetAddresses();49while (addrs.hasMoreElements()) {50InetAddress addr = (InetAddress)addrs.nextElement();51System.out.println(addr);52if (NetworkInterface.getByInetAddress(addr) == null) {53throw new Exception("getByInetAddress returned null");54}55}56System.out.println("getInetAddresses() test passed.");5758/*59* Check equals and hashCode contract60*/61NetworkInterface ni2 = NetworkInterface.getByName(name);62if (!ni2.equals(ni)) {63throw new Exception("getByName returned: " + ni2);64}65if (!ni.equals(ni2)) {66throw new Exception("equals specification broken");67}68System.out.println("equals() tests passed.");69if (ni2.hashCode() != ni.hashCode()) {70throw new Exception("hashCode contract broken");71}72System.out.println("hashCode() test passed.");7374byte[] ba = ni.getHardwareAddress();75if (ba != null && ba.length == 0) {76throw new Exception("getHardwareAddress returned 0 length byte array");77}78System.out.println("getHardwareAddress() test passed.");79}8081// misc tests :-82// getByXXX(null) should throw NPE83// getByXXX("garbage") should return null8485System.out.println("\nMiscellenous tests: ");8687try {88NetworkInterface.getByName(null);89} catch (NullPointerException npe) {90}91System.out.println("getByName(null) test passed.");9293try {94NetworkInterface.getByInetAddress(null);95} catch (NullPointerException npe) {96}97System.out.println("getByInetAddress(null) test passed.");9899if (NetworkInterface.getByName("not-a-valid-name") != null) {100throw new Exception101("getByName returned unexpected interface: null expected");102}103System.out.println("getByName(<unknown>) test passed.");104105InetAddress ia = InetAddress.getByName("255.255.255.255");106if (NetworkInterface.getByInetAddress(ia) != null) {107throw new Exception108("getByInetAddress returned unexpected interface: null expected");109}110System.out.println("getByName(getByInetAddress(<unknown>) test passed.");111112}113}114115116