Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/InterfaceAddress/Equals.java
38811 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* @summary InterfaceAddress.equals() NPE when broadcast field == null26*/2728import java.net.InterfaceAddress;29import java.net.InetAddress;30import java.net.UnknownHostException;31import java.lang.reflect.Constructor;32import java.lang.reflect.Field;33import java.lang.reflect.InvocationTargetException;3435public class Equals36{37public static void main(String[] args) {38InterfaceAddress ia1;39InterfaceAddress ia2;40InetAddress loopbackAddr = InetAddress.getLoopbackAddress();41InetAddress broadcast1 = null;42InetAddress broadcast2 = null;4344try {45broadcast1 = InetAddress.getByName("255.255.255.0");46broadcast2 = InetAddress.getByName("255.255.0.0");47} catch (UnknownHostException e) {48e.printStackTrace();49}5051ia1 = createInterfaceAddress(loopbackAddr, (InetAddress) null, (short)45);52ia2 = createInterfaceAddress(loopbackAddr, (InetAddress) null, (short)45);5354compare(ia1, ia2, true);5556ia2 = createInterfaceAddress(loopbackAddr, broadcast1, (short)45);57compare(ia1, ia2, false);5859ia2 = createInterfaceAddress((InetAddress)null, broadcast1, (short)45);60compare(ia1, ia2, false);6162ia1 = createInterfaceAddress(loopbackAddr, broadcast2, (short)45);63ia2 = createInterfaceAddress(loopbackAddr, broadcast2, (short)45);64compare(ia1, ia2, true);6566ia1.equals(null);67}6869static void compare(InterfaceAddress ia1, InterfaceAddress ia2, boolean equal) {70if (ia1.equals(ia2) != equal)71throw new RuntimeException("Failed: " + ia1 + " not equals to " + ia2);7273if (ia2.equals(ia1) != equal)74throw new RuntimeException("Failed: " + ia2 + " not equals to " + ia1);75}7677/**78* Returns an InterfaceAddress instance with its fields set the the values79* specificed.80*/81static InterfaceAddress createInterfaceAddress(82InetAddress address, InetAddress broadcast, short prefixlength) {83try {84Class<InterfaceAddress> IAClass = InterfaceAddress.class;85InterfaceAddress ia;86Constructor<InterfaceAddress> ctr = IAClass.getDeclaredConstructor();87ctr.setAccessible(true);8889Field addressField = IAClass.getDeclaredField("address");90addressField.setAccessible(true);9192Field broadcastField = IAClass.getDeclaredField("broadcast");93broadcastField.setAccessible(true);9495Field maskLengthField = IAClass.getDeclaredField("maskLength");96maskLengthField.setAccessible(true);9798ia = ctr.newInstance();99addressField.set(ia, address);100broadcastField.set(ia, broadcast);101maskLengthField.setShort(ia, prefixlength);102103return ia;104} catch (NoSuchFieldException nsfe) {105nsfe.printStackTrace();106} catch (NoSuchMethodException e) {107e.printStackTrace();108} catch (InstantiationException ie) {109ie.printStackTrace();110} catch (IllegalAccessException iae) {111iae.printStackTrace();112} catch (InvocationTargetException ite) {113ite.printStackTrace();114}115116return null;117}118}119120121