61N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
61N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
61N/A *
61N/A * This code is free software; you can redistribute it and/or modify it
61N/A * under the terms of the GNU General Public License version 2 only, as
61N/A * published by the Free Software Foundation.
61N/A *
61N/A * This code is distributed in the hope that it will be useful, but WITHOUT
61N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
61N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
61N/A * version 2 for more details (a copy is included in the LICENSE file that
61N/A * accompanied this code).
61N/A *
61N/A * You should have received a copy of the GNU General Public License version
61N/A * 2 along with this work; if not, write to the Free Software Foundation,
61N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
61N/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.
61N/A */
61N/A
61N/A/* @test
61N/A * @bug 6628576
61N/A * @summary InterfaceAddress.equals() NPE when broadcast field == null
61N/A */
61N/A
61N/Aimport java.net.InterfaceAddress;
61N/Aimport java.net.InetAddress;
61N/Aimport java.net.UnknownHostException;
61N/Aimport java.lang.reflect.Constructor;
61N/Aimport java.lang.reflect.Field;
61N/Aimport java.lang.reflect.InvocationTargetException;
61N/A
61N/Apublic class Equals
61N/A{
61N/A public static void main(String[] args) {
61N/A InterfaceAddress ia1;
61N/A InterfaceAddress ia2;
61N/A InetAddress loopbackAddr = InetAddress.getLoopbackAddress();
61N/A InetAddress broadcast1 = null;
61N/A InetAddress broadcast2 = null;
61N/A
61N/A try {
61N/A broadcast1 = InetAddress.getByName("255.255.255.0");
61N/A broadcast2 = InetAddress.getByName("255.255.0.0");
61N/A } catch (UnknownHostException e) {
61N/A e.printStackTrace();
61N/A }
61N/A
61N/A ia1 = createInterfaceAddress(loopbackAddr, (InetAddress) null, (short)45);
61N/A ia2 = createInterfaceAddress(loopbackAddr, (InetAddress) null, (short)45);
61N/A
61N/A compare(ia1, ia2, true);
61N/A
61N/A ia2 = createInterfaceAddress(loopbackAddr, broadcast1, (short)45);
61N/A compare(ia1, ia2, false);
61N/A
61N/A ia2 = createInterfaceAddress((InetAddress)null, broadcast1, (short)45);
61N/A compare(ia1, ia2, false);
61N/A
61N/A ia1 = createInterfaceAddress(loopbackAddr, broadcast2, (short)45);
61N/A ia2 = createInterfaceAddress(loopbackAddr, broadcast2, (short)45);
61N/A compare(ia1, ia2, true);
61N/A
61N/A ia1.equals(null);
61N/A }
61N/A
61N/A static void compare(InterfaceAddress ia1, InterfaceAddress ia2, boolean equal) {
61N/A if (ia1.equals(ia2) != equal)
61N/A throw new RuntimeException("Failed: " + ia1 + " not equals to " + ia2);
61N/A
61N/A if (ia2.equals(ia1) != equal)
61N/A throw new RuntimeException("Failed: " + ia2 + " not equals to " + ia1);
61N/A }
61N/A
61N/A /**
61N/A * Returns an InterfaceAddress instance with its fields set the the values
61N/A * specificed.
61N/A */
61N/A static InterfaceAddress createInterfaceAddress(
61N/A InetAddress address, InetAddress broadcast, short prefixlength) {
61N/A try {
61N/A Class<InterfaceAddress> IAClass = InterfaceAddress.class;
61N/A InterfaceAddress ia;
61N/A Constructor<InterfaceAddress> ctr = IAClass.getDeclaredConstructor();
61N/A ctr.setAccessible(true);
61N/A
61N/A Field addressField = IAClass.getDeclaredField("address");
61N/A addressField.setAccessible(true);
61N/A
61N/A Field broadcastField = IAClass.getDeclaredField("broadcast");
61N/A broadcastField.setAccessible(true);
61N/A
61N/A Field maskLengthField = IAClass.getDeclaredField("maskLength");
61N/A maskLengthField.setAccessible(true);
61N/A
61N/A ia = ctr.newInstance();
61N/A addressField.set(ia, address);
61N/A broadcastField.set(ia, broadcast);
61N/A maskLengthField.setShort(ia, prefixlength);
61N/A
61N/A return ia;
61N/A } catch (NoSuchFieldException nsfe) {
61N/A nsfe.printStackTrace();
61N/A } catch (NoSuchMethodException e) {
61N/A e.printStackTrace();
61N/A } catch (InstantiationException ie) {
61N/A ie.printStackTrace();
61N/A } catch (IllegalAccessException iae) {
61N/A iae.printStackTrace();
61N/A } catch (InvocationTargetException ite) {
61N/A ite.printStackTrace();
61N/A }
61N/A
61N/A return null;
61N/A }
61N/A}