1 /* 2 * Copyright (c) 2022, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package test.java.net.InetAddress; 24 25 /** 26 * @test 27 * @bug 8135305 28 * @key intermittent 29 * @summary ensure we can't ping external hosts via loopback if 30 */ 31 32 import java.io.*; 33 import java.net.*; 34 import java.util.*; 35 import org.testng.annotations.Test; 36 import static org.testng.Assert.fail; 37 38 public class IsReachableViaLoopbackTest { 39 @Test testReachableViaLoopback()40 public void testReachableViaLoopback() { 41 try { 42 InetAddress addr = InetAddress.getByName("localhost"); 43 // Android-changed: use alternative DNS address (b/255943063#comment49) 44 // and switch to literal address to avoid DNS lookup flakes. 45 // InetAddress remoteAddr = InetAddress.getByName("bugs.openjdk.java.net"); 46 InetAddress remoteAddr = InetAddress.getByAddress(new byte[]{8, 8, 8, 8}); 47 if (!addr.isReachable(10000)) 48 fail("Localhost should always be reachable"); 49 NetworkInterface inf = NetworkInterface.getByInetAddress(addr); 50 if (inf != null) { 51 if (!addr.isReachable(inf, 20, 10000)) { 52 fail("Localhost should always be reachable"); 53 } else { 54 } 55 if (remoteAddr.isReachable(inf, 20, 10000)) { 56 fail(remoteAddr + " is reachable"); 57 } else { 58 } 59 } 60 61 } catch (IOException e) { 62 fail("Unexpected exception:" + e); 63 } 64 } 65 }