Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Inet4Address/PingThis.java
47182 views
/*1* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* Portions Copyright (c) 2012 IBM Corporation25*/2627/* @test28* @bug 7163874 813301529* @summary InetAddress.isReachable is returning false30* for InetAdress 0.0.0.0 and ::031* @run main PingThis32* @run main/othervm -Djava.net.preferIPv4Stack=true PingThis33*/3435import java.net.Inet6Address;36import java.net.InetAddress;37import java.net.NetworkInterface;38import java.util.ArrayList;39import java.util.Collections;40import java.util.Iterator;41import java.util.List;4243public class PingThis {44private static boolean hasIPv6() throws Exception {45List<NetworkInterface> nics = Collections.list(NetworkInterface46.getNetworkInterfaces());47for (NetworkInterface nic : nics) {48List<InetAddress> addrs = Collections.list(nic.getInetAddresses());49for (InetAddress addr : addrs) {50if (addr instanceof Inet6Address)51return true;52}53}5455return false;56}5758public static void main(String args[]) throws Exception {59if (System.getProperty("os.name").startsWith("Windows")) {60return;61}6263boolean preferIPv4Stack = "true".equals(System64.getProperty("java.net.preferIPv4Stack"));65List<String> addrs = new ArrayList<String>();66InetAddress inetAddress = null;6768addrs.add("0.0.0.0");69if (!preferIPv4Stack) {70if (hasIPv6()) {71addrs.add("::0");72}73}7475for (String addr : addrs) {76inetAddress = InetAddress.getByName(addr);77System.out.println("The target ip is "78+ inetAddress.getHostAddress());79boolean isReachable = inetAddress.isReachable(3000);80System.out.println("the target is reachable: " + isReachable);81if (isReachable) {82System.out.println("Test passed ");83} else {84System.out.println("Test failed ");85throw new Exception("address " + inetAddress.getHostAddress()86+ " can not be reachable!");87}88}89}90}919293