blob: 5403bebe04c8866fd04f7fa6dba1c8c1a1cdc625 [file] [log] [blame]
chegard1684c92008-03-07 13:00:44 +00001/*
2 * Copyright 2008 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @bug 6628576
26 * @summary InterfaceAddress.equals() NPE when broadcast field == null
27 */
28
29import 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;
35
36public class Equals
37{
38 public static void main(String[] args) {
39 InterfaceAddress ia1;
40 InterfaceAddress ia2;
41 InetAddress loopbackAddr = InetAddress.getLoopbackAddress();
42 InetAddress broadcast1 = null;
43 InetAddress broadcast2 = null;
44
45 try {
46 broadcast1 = InetAddress.getByName("255.255.255.0");
47 broadcast2 = InetAddress.getByName("255.255.0.0");
48 } catch (UnknownHostException e) {
49 e.printStackTrace();
50 }
51
52 ia1 = createInterfaceAddress(loopbackAddr, (InetAddress) null, (short)45);
53 ia2 = createInterfaceAddress(loopbackAddr, (InetAddress) null, (short)45);
54
55 compare(ia1, ia2, true);
56
57 ia2 = createInterfaceAddress(loopbackAddr, broadcast1, (short)45);
58 compare(ia1, ia2, false);
59
60 ia2 = createInterfaceAddress((InetAddress)null, broadcast1, (short)45);
61 compare(ia1, ia2, false);
62
63 ia1 = createInterfaceAddress(loopbackAddr, broadcast2, (short)45);
64 ia2 = createInterfaceAddress(loopbackAddr, broadcast2, (short)45);
65 compare(ia1, ia2, true);
66
67 ia1.equals(null);
68 }
69
70 static void compare(InterfaceAddress ia1, InterfaceAddress ia2, boolean equal) {
71 if (ia1.equals(ia2) != equal)
72 throw new RuntimeException("Failed: " + ia1 + " not equals to " + ia2);
73
74 if (ia2.equals(ia1) != equal)
75 throw new RuntimeException("Failed: " + ia2 + " not equals to " + ia1);
76 }
77
78 /**
79 * Returns an InterfaceAddress instance with its fields set the the values
80 * specificed.
81 */
82 static InterfaceAddress createInterfaceAddress(
83 InetAddress address, InetAddress broadcast, short prefixlength) {
84 try {
85 Class<InterfaceAddress> IAClass = InterfaceAddress.class;
86 InterfaceAddress ia;
87 Constructor<InterfaceAddress> ctr = IAClass.getDeclaredConstructor();
88 ctr.setAccessible(true);
89
90 Field addressField = IAClass.getDeclaredField("address");
91 addressField.setAccessible(true);
92
93 Field broadcastField = IAClass.getDeclaredField("broadcast");
94 broadcastField.setAccessible(true);
95
96 Field maskLengthField = IAClass.getDeclaredField("maskLength");
97 maskLengthField.setAccessible(true);
98
99 ia = ctr.newInstance();
100 addressField.set(ia, address);
101 broadcastField.set(ia, broadcast);
102 maskLengthField.setShort(ia, prefixlength);
103
104 return ia;
105 } catch (NoSuchFieldException nsfe) {
106 nsfe.printStackTrace();
107 } catch (NoSuchMethodException e) {
108 e.printStackTrace();
109 } catch (InstantiationException ie) {
110 ie.printStackTrace();
111 } catch (IllegalAccessException iae) {
112 iae.printStackTrace();
113 } catch (InvocationTargetException ite) {
114 ite.printStackTrace();
115 }
116
117 return null;
118 }
119}