0N/A/*
2362N/A * Copyright (c) 2001, 2002, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test 01/02/14
0N/A * @bug 4405354
0N/A * @summary Exercise java.net.NetworkInterface
0N/A */
0N/Aimport java.net.NetworkInterface;
0N/Aimport java.net.InetAddress;
0N/Aimport java.util.Enumeration;
0N/A
0N/Apublic class Test {
0N/A
0N/A public static void main(String args[]) throws Exception {
0N/A
0N/A Enumeration nifs = NetworkInterface.getNetworkInterfaces();
0N/A
0N/A while (nifs.hasMoreElements()) {
0N/A NetworkInterface ni = (NetworkInterface)nifs.nextElement();
0N/A
0N/A String name = ni.getName();
0N/A System.out.println("\n" + name);
0N/A
0N/A /*
0N/A * Enumeration the IP addresses on this interface
0N/A */
0N/A Enumeration addrs = ni.getInetAddresses();
0N/A while (addrs.hasMoreElements()) {
0N/A InetAddress addr = (InetAddress)addrs.nextElement();
0N/A System.out.println(addr);
0N/A if (NetworkInterface.getByInetAddress(addr) == null) {
0N/A throw new Exception("getByInetAddress returned null");
0N/A }
0N/A }
0N/A System.out.println("getInetAddresses() test passed.");
0N/A
0N/A /*
0N/A * Check equals and hashCode contract
0N/A */
0N/A NetworkInterface ni2 = NetworkInterface.getByName(name);
0N/A if (!ni2.equals(ni)) {
0N/A throw new Exception("getByName returned: " + ni2);
0N/A }
0N/A if (!ni.equals(ni2)) {
0N/A throw new Exception("equals specification broken");
0N/A }
0N/A System.out.println("equals() tests passed.");
0N/A if (ni2.hashCode() != ni.hashCode()) {
0N/A throw new Exception("hashCode contract broken");
0N/A }
0N/A System.out.println("hashCode() test passed.");
0N/A }
0N/A
0N/A // misc tests :-
0N/A // getByXXX(null) should throw NPE
0N/A // getByXXX("garbage") should return null
0N/A
0N/A System.out.println("\nMiscellenous tests: ");
0N/A
0N/A try {
0N/A NetworkInterface.getByName(null);
0N/A } catch (NullPointerException npe) {
0N/A }
0N/A System.out.println("getByName(null) test passed.");
0N/A
0N/A try {
0N/A NetworkInterface.getByInetAddress(null);
0N/A } catch (NullPointerException npe) {
0N/A }
0N/A System.out.println("getByInetAddress(null) test passed.");
0N/A
0N/A if (NetworkInterface.getByName("not-a-valid-name") != null) {
0N/A throw new Exception
0N/A ("getByName returned unexpected interface: null expected");
0N/A }
0N/A System.out.println("getByName(<unknown>) test passed.");
0N/A
0N/A InetAddress ia = InetAddress.getByName("255.255.255.255");
0N/A if (NetworkInterface.getByInetAddress(ia) != null) {
0N/A throw new Exception
0N/A ("getByInetAddress returned unexpected interface: null expected");
0N/A }
0N/A System.out.println("getByName(getByInetAddress(<unknown>) test passed.");
0N/A
0N/A }
0N/A}