Path: blob/master/test/jdk/java/net/InetAddress/B5087907.java
41149 views
/*1* Copyright (c) 2004, 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* @test25* @bug 508790726* @summary InetAddress.getAllByName does not obey setting of java.net.preferIPv6Addresses27* @run main/othervm -Djava.net.preferIPv6Addresses=false B508790728* @run main/othervm -Djava.net.preferIPv6Addresses=true B508790729* @run main/othervm B508790730*/3132import java.net.*;3334public class B5087907 {3536public static void main(String args[]) {37InetAddress lh = null;38InetAddress addrs[] = null;39try {40lh = InetAddress.getByName("localhost");41addrs = InetAddress.getAllByName("localhost");42} catch (UnknownHostException e) {43System.out.println ("Cant lookup localhost. cant run test");44return;45}4647boolean hasIPv4Address = false;48boolean hasIPv6Address = false;49for (InetAddress addr: addrs) {50if (addr instanceof Inet4Address) {51hasIPv4Address = true;52}53if (addr instanceof Inet6Address) {54hasIPv6Address = true;55}56if (hasIPv4Address && hasIPv6Address) {57break;58}59}6061String prop = System.getProperty("java.net.preferIPv6Addresses");62boolean preferIPv6Addresses = (prop == null) ? false : prop.equals("true");6364System.out.println("java.net.preferIPv6Addresses: " + preferIPv6Addresses);65System.out.println("localhost resolves to:");66for (InetAddress addr: addrs) {67System.out.println(" " + addr);68}69System.out.println("InetAddres.getByName returned: " + lh);7071boolean failed = false;72if (preferIPv6Addresses && hasIPv6Address) {73if (!(lh instanceof Inet6Address)) failed = true;74}75if (!preferIPv6Addresses && hasIPv4Address) {76if (!(lh instanceof Inet4Address)) failed = true;77}78if (failed) {79throw new RuntimeException("Test failed!");80}81}8283}848586