jdk/test/java/net/Inet4Address/PingThis.java
author jmasa
Wed, 04 Sep 2013 11:41:17 -0700
changeset 19730 4c38872d277e
parent 14856 92a1bcf46888
child 32266 e0a235a11254
permissions -rw-r--r--
8013938: Native OOME on fastdebug VM on Solaris Reviewed-by: azeemj, brutisso, kvn, tschatzl
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12688
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
     1
/*
14856
92a1bcf46888 8004982: JDK8 source with GPL header errors
katleman
parents: 12688
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
12688
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
     4
 *
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
     8
 *
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    13
 * accompanied this code).
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    14
 *
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    18
 *
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    21
 * questions.
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    22
 */
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    23
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    24
/*
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    25
 * Portions Copyright (c) 2012 IBM Corporation
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    26
 */
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    27
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    28
/* @test
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    29
 * @bug 7163874
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    30
 * @summary InetAddress.isReachable is returning false
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    31
 *          for InetAdress 0.0.0.0 and ::0
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    32
 * @run main PingThis
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    33
 * @run main/othervm -Djava.net.preferIPv4Stack=true PingThis
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    34
 */
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    35
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    36
import java.net.Inet6Address;
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    37
import java.net.InetAddress;
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    38
import java.net.NetworkInterface;
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    39
import java.util.ArrayList;
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    40
import java.util.Collections;
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    41
import java.util.Iterator;
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    42
import java.util.List;
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    43
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    44
public class PingThis {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    45
    private static boolean hasIPv6() throws Exception {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    46
        List<NetworkInterface> nics = Collections.list(NetworkInterface
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    47
                .getNetworkInterfaces());
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    48
        for (NetworkInterface nic : nics) {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    49
            List<InetAddress> addrs = Collections.list(nic.getInetAddresses());
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    50
            for (InetAddress addr : addrs) {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    51
                if (addr instanceof Inet6Address)
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    52
                    return true;
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    53
            }
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    54
        }
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    55
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    56
        return false;
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    57
    }
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    58
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    59
    public static void main(String args[]) throws Exception {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    60
        if (System.getProperty("os.name").startsWith("Windows")) {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    61
            return;
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    62
        }
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    63
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    64
        boolean preferIPv4Stack = "true".equals(System
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    65
                .getProperty("java.net.preferIPv4Stack"));
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    66
        List<String> addrs = new ArrayList<String>();
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    67
        InetAddress inetAddress = null;
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    68
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    69
        addrs.add("0.0.0.0");
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    70
        if (!preferIPv4Stack) {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    71
            if (hasIPv6()) {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    72
                addrs.add("::0");
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    73
            }
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    74
        }
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    75
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    76
        for (String addr : addrs) {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    77
            inetAddress = InetAddress.getByName(addr);
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    78
            System.out.println("The target ip is "
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    79
                    + inetAddress.getHostAddress());
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    80
            boolean isReachable = inetAddress.isReachable(3000);
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    81
            System.out.println("the target is reachable: " + isReachable);
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    82
            if (isReachable) {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    83
                System.out.println("Test passed ");
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    84
            } else {
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    85
                System.out.println("Test failed ");
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    86
                throw new Exception("address " + inetAddress.getHostAddress()
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    87
                        + " can not be reachable!");
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    88
            }
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    89
        }
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    90
    }
f15fd32c975e 7163874: InetAddress.isReachable should support pinging 0.0.0.0
youdwei
parents:
diff changeset
    91
}