1 /* 2 * Copyright (c) 2004, 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 5087907 28 * @summary InetAddress.getAllByName does not obey setting of java.net.preferIPv6Addresses 29 * @run main/othervm -Djava.net.preferIPv6Addresses=false B5087907 30 * @run main/othervm -Djava.net.preferIPv6Addresses=true B5087907 31 * @run main/othervm B5087907 32 */ 33 34 import java.net.*; 35 import org.testng.annotations.Test; 36 import static org.testng.Assert.assertTrue; 37 38 public class B5087907 { 39 40 @Test testB5087907()41 public void testB5087907() { 42 InetAddress lh = null; 43 InetAddress addrs[] = null; 44 try { 45 lh = InetAddress.getByName("localhost"); 46 addrs = InetAddress.getAllByName("localhost"); 47 } catch (UnknownHostException e) { 48 System.out.println ("Cant lookup localhost. cant run test"); 49 return; 50 } 51 52 boolean hasIPv4Address = false; 53 boolean hasIPv6Address = false; 54 for (InetAddress addr: addrs) { 55 if (addr instanceof Inet4Address) { 56 hasIPv4Address = true; 57 } 58 if (addr instanceof Inet6Address) { 59 hasIPv6Address = true; 60 } 61 if (hasIPv4Address && hasIPv6Address) { 62 break; 63 } 64 } 65 66 String prop = System.getProperty("java.net.preferIPv6Addresses"); 67 boolean preferIPv6Addresses = (prop == null) ? false : prop.equals("true"); 68 69 boolean failed = false; 70 if (preferIPv6Addresses && hasIPv6Address) { 71 assertTrue(lh instanceof Inet6Address); 72 } 73 if (!preferIPv6Addresses && hasIPv4Address) { 74 assertTrue(lh instanceof Inet4Address); 75 } 76 } 77 78 }