Path: blob/master/test/jdk/java/net/InterfaceAddress/Equals.java
41149 views
/*1* Copyright (c) 2008, 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 662857625* @modules java.base/java.net:open26* @summary InterfaceAddress.equals() NPE when broadcast field == null27*/2829import java.net.InterfaceAddress;30import java.net.InetAddress;31import java.net.UnknownHostException;32import java.lang.reflect.Constructor;33import java.lang.reflect.Field;34import java.lang.reflect.InvocationTargetException;3536public class Equals37{38public static void main(String[] args) {39InterfaceAddress ia1;40InterfaceAddress ia2;41InetAddress loopbackAddr = InetAddress.getLoopbackAddress();42InetAddress broadcast1 = null;43InetAddress broadcast2 = null;4445try {46broadcast1 = InetAddress.getByName("255.255.255.0");47broadcast2 = InetAddress.getByName("255.255.0.0");48} catch (UnknownHostException e) {49e.printStackTrace();50}5152ia1 = createInterfaceAddress(loopbackAddr, (InetAddress) null, (short)45);53ia2 = createInterfaceAddress(loopbackAddr, (InetAddress) null, (short)45);5455compare(ia1, ia2, true);5657ia2 = createInterfaceAddress(loopbackAddr, broadcast1, (short)45);58compare(ia1, ia2, false);5960ia2 = createInterfaceAddress((InetAddress)null, broadcast1, (short)45);61compare(ia1, ia2, false);6263ia1 = createInterfaceAddress(loopbackAddr, broadcast2, (short)45);64ia2 = createInterfaceAddress(loopbackAddr, broadcast2, (short)45);65compare(ia1, ia2, true);6667ia1.equals(null);68}6970static void compare(InterfaceAddress ia1, InterfaceAddress ia2, boolean equal) {71if (ia1.equals(ia2) != equal)72throw new RuntimeException("Failed: " + ia1 + " not equals to " + ia2);7374if (ia2.equals(ia1) != equal)75throw new RuntimeException("Failed: " + ia2 + " not equals to " + ia1);76}7778/**79* Returns an InterfaceAddress instance with its fields set the the values80* specificed.81*/82static InterfaceAddress createInterfaceAddress(83InetAddress address, InetAddress broadcast, short prefixlength) {84try {85Class<InterfaceAddress> IAClass = InterfaceAddress.class;86InterfaceAddress ia;87Constructor<InterfaceAddress> ctr = IAClass.getDeclaredConstructor();88ctr.setAccessible(true);8990Field addressField = IAClass.getDeclaredField("address");91addressField.setAccessible(true);9293Field broadcastField = IAClass.getDeclaredField("broadcast");94broadcastField.setAccessible(true);9596Field maskLengthField = IAClass.getDeclaredField("maskLength");97maskLengthField.setAccessible(true);9899ia = ctr.newInstance();100addressField.set(ia, address);101broadcastField.set(ia, broadcast);102maskLengthField.setShort(ia, prefixlength);103104return ia;105} catch (NoSuchFieldException nsfe) {106nsfe.printStackTrace();107} catch (NoSuchMethodException e) {108e.printStackTrace();109} catch (InstantiationException ie) {110ie.printStackTrace();111} catch (IllegalAccessException iae) {112iae.printStackTrace();113} catch (InvocationTargetException ite) {114ite.printStackTrace();115}116117return null;118}119}120121122