test/jdk/java/net/NetworkInterface/Test.java
author chegar
Tue, 19 Jun 2018 09:13:58 +0100
branchhttp-client-branch
changeset 56771 73a6534bce94
parent 47216 71c04702a3d5
child 54770 62b6e7587b1f
permissions -rw-r--r--
http-client-branch: prepare tests for TLS1.3 Contributed-by: michaelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 17206
diff changeset
     2
 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
17206
3dfa000764e9 6594296: NetworkInterface.getHardwareAddress returns zero length byte array
chegar
parents: 5506
diff changeset
    24
/* @test
26595
1f96f8175e05 8058216: NetworkInterface.getHardwareAddress can return zero length byte array when run with preferIPv4Stack
chegar
parents: 23591
diff changeset
    25
 * @bug 4405354 6594296 8058216
1f96f8175e05 8058216: NetworkInterface.getHardwareAddress can return zero length byte array when run with preferIPv4Stack
chegar
parents: 23591
diff changeset
    26
 * @run main Test
1f96f8175e05 8058216: NetworkInterface.getHardwareAddress can return zero length byte array when run with preferIPv4Stack
chegar
parents: 23591
diff changeset
    27
 * @run main/othervm -Djava.net.preferIPv4Stack=true Test
17206
3dfa000764e9 6594296: NetworkInterface.getHardwareAddress returns zero length byte array
chegar
parents: 5506
diff changeset
    28
 * @summary Basic tests for NetworkInterface
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.NetworkInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class Test {
23591
1b50db37efd3 8038276: java/net/NetworkInterface/Test.java fails on Windows intermittently for Teredo Interface
chegar
parents: 23010
diff changeset
    35
    static final boolean isWindows = System.getProperty("os.name").startsWith("Windows");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        Enumeration nifs = NetworkInterface.getNetworkInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        while (nifs.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            NetworkInterface ni = (NetworkInterface)nifs.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
23591
1b50db37efd3 8038276: java/net/NetworkInterface/Test.java fails on Windows intermittently for Teredo Interface
chegar
parents: 23010
diff changeset
    44
            //JDK-8038276: Should not test on Windows with Teredo Tunneling Pseudo-Interface
1b50db37efd3 8038276: java/net/NetworkInterface/Test.java fails on Windows intermittently for Teredo Interface
chegar
parents: 23010
diff changeset
    45
            String dName = ni.getDisplayName();
1b50db37efd3 8038276: java/net/NetworkInterface/Test.java fails on Windows intermittently for Teredo Interface
chegar
parents: 23010
diff changeset
    46
            if (isWindows && dName != null && dName.contains("Teredo"))
1b50db37efd3 8038276: java/net/NetworkInterface/Test.java fails on Windows intermittently for Teredo Interface
chegar
parents: 23010
diff changeset
    47
                continue;
1b50db37efd3 8038276: java/net/NetworkInterface/Test.java fails on Windows intermittently for Teredo Interface
chegar
parents: 23010
diff changeset
    48
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            String name = ni.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            System.out.println("\n" + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
             * Enumeration the IP addresses on this interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            Enumeration addrs = ni.getInetAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            while (addrs.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                InetAddress addr = (InetAddress)addrs.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                System.out.println(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                if (NetworkInterface.getByInetAddress(addr) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                    throw new Exception("getByInetAddress returned null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            System.out.println("getInetAddresses() test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
             * Check equals and hashCode contract
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            NetworkInterface ni2 = NetworkInterface.getByName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            if (!ni2.equals(ni)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                throw new Exception("getByName returned: " + ni2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            if (!ni.equals(ni2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                throw new Exception("equals specification broken");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            System.out.println("equals() tests passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            if (ni2.hashCode() != ni.hashCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                throw new Exception("hashCode contract broken");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            System.out.println("hashCode() test passed.");
17206
3dfa000764e9 6594296: NetworkInterface.getHardwareAddress returns zero length byte array
chegar
parents: 5506
diff changeset
    80
3dfa000764e9 6594296: NetworkInterface.getHardwareAddress returns zero length byte array
chegar
parents: 5506
diff changeset
    81
            byte[] ba = ni.getHardwareAddress();
3dfa000764e9 6594296: NetworkInterface.getHardwareAddress returns zero length byte array
chegar
parents: 5506
diff changeset
    82
            if (ba != null && ba.length == 0) {
3dfa000764e9 6594296: NetworkInterface.getHardwareAddress returns zero length byte array
chegar
parents: 5506
diff changeset
    83
                throw new Exception("getHardwareAddress returned 0 length byte array");
3dfa000764e9 6594296: NetworkInterface.getHardwareAddress returns zero length byte array
chegar
parents: 5506
diff changeset
    84
            }
3dfa000764e9 6594296: NetworkInterface.getHardwareAddress returns zero length byte array
chegar
parents: 5506
diff changeset
    85
            System.out.println("getHardwareAddress() test passed.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // misc tests :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        //      getByXXX(null) should throw NPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        //      getByXXX("garbage") should return null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        System.out.println("\nMiscellenous tests: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            NetworkInterface.getByName(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } catch (NullPointerException npe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        System.out.println("getByName(null) test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            NetworkInterface.getByInetAddress(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        } catch (NullPointerException npe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        System.out.println("getByInetAddress(null) test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (NetworkInterface.getByName("not-a-valid-name") != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            throw new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                ("getByName returned unexpected interface: null expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        System.out.println("getByName(<unknown>) test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        InetAddress ia = InetAddress.getByName("255.255.255.255");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (NetworkInterface.getByInetAddress(ia) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            throw new Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                ("getByInetAddress returned unexpected interface: null expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        System.out.println("getByName(getByInetAddress(<unknown>) test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
}