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