1 /* 2 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * @bug 6628576 26 * @modules java.base/java.net:open 27 * @summary InterfaceAddress.equals() NPE when broadcast field == null 28 */ 29 30 import java.net.InterfaceAddress; 31 import java.net.InetAddress; 32 import java.net.UnknownHostException; 33 import java.lang.reflect.Constructor; 34 import java.lang.reflect.Field; 35 import java.lang.reflect.InvocationTargetException; 36 37 public class Equals 38 { main(String[] args)39 public static void main(String[] args) { 40 InterfaceAddress ia1; 41 InterfaceAddress ia2; 42 InetAddress loopbackAddr = InetAddress.getLoopbackAddress(); 43 InetAddress broadcast1 = null; 44 InetAddress broadcast2 = null; 45 46 try { 47 broadcast1 = InetAddress.getByName("255.255.255.0"); 48 broadcast2 = InetAddress.getByName("255.255.0.0"); 49 } catch (UnknownHostException e) { 50 e.printStackTrace(); 51 } 52 53 ia1 = createInterfaceAddress(loopbackAddr, (InetAddress) null, (short)45); 54 ia2 = createInterfaceAddress(loopbackAddr, (InetAddress) null, (short)45); 55 56 compare(ia1, ia2, true); 57 58 ia2 = createInterfaceAddress(loopbackAddr, broadcast1, (short)45); 59 compare(ia1, ia2, false); 60 61 ia2 = createInterfaceAddress((InetAddress)null, broadcast1, (short)45); 62 compare(ia1, ia2, false); 63 64 ia1 = createInterfaceAddress(loopbackAddr, broadcast2, (short)45); 65 ia2 = createInterfaceAddress(loopbackAddr, broadcast2, (short)45); 66 compare(ia1, ia2, true); 67 68 ia1.equals(null); 69 } 70 compare(InterfaceAddress ia1, InterfaceAddress ia2, boolean equal)71 static void compare(InterfaceAddress ia1, InterfaceAddress ia2, boolean equal) { 72 if (ia1.equals(ia2) != equal) 73 throw new RuntimeException("Failed: " + ia1 + " not equals to " + ia2); 74 75 if (ia2.equals(ia1) != equal) 76 throw new RuntimeException("Failed: " + ia2 + " not equals to " + ia1); 77 } 78 79 /** 80 * Returns an InterfaceAddress instance with its fields set the the values 81 * specificed. 82 */ createInterfaceAddress( InetAddress address, InetAddress broadcast, short prefixlength)83 static InterfaceAddress createInterfaceAddress( 84 InetAddress address, InetAddress broadcast, short prefixlength) { 85 try { 86 Class<InterfaceAddress> IAClass = InterfaceAddress.class; 87 InterfaceAddress ia; 88 Constructor<InterfaceAddress> ctr = IAClass.getDeclaredConstructor(); 89 ctr.setAccessible(true); 90 91 Field addressField = IAClass.getDeclaredField("address"); 92 addressField.setAccessible(true); 93 94 Field broadcastField = IAClass.getDeclaredField("broadcast"); 95 broadcastField.setAccessible(true); 96 97 Field maskLengthField = IAClass.getDeclaredField("maskLength"); 98 maskLengthField.setAccessible(true); 99 100 ia = ctr.newInstance(); 101 addressField.set(ia, address); 102 broadcastField.set(ia, broadcast); 103 maskLengthField.setShort(ia, prefixlength); 104 105 return ia; 106 } catch (NoSuchFieldException nsfe) { 107 nsfe.printStackTrace(); 108 } catch (NoSuchMethodException e) { 109 e.printStackTrace(); 110 } catch (InstantiationException ie) { 111 ie.printStackTrace(); 112 } catch (IllegalAccessException iae) { 113 iae.printStackTrace(); 114 } catch (InvocationTargetException ite) { 115 ite.printStackTrace(); 116 } 117 118 return null; 119 } 120 } 121