blob: 2835978129c09f53cc283ac68b909ac089331575 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
2 * Copyright 2004 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/**
25 * @test
26 * @bug 5087907
27 * @summary InetAddress.getAllByName does not obey setting of java.net.preferIPv6Addresses
28 * @run main/othervm -Djava.net.preferIPv6Addresses=false B5087907
29 * @run main/othervm -Djava.net.preferIPv6Addresses=true B5087907
30 * @run main/othervm B5087907
31 */
32
33import java.net.*;
34
35public class B5087907 {
36
37 public static void main(String args[]) {
38 InetAddress lh = null;
39 InetAddress addrs[] = null;
40 try {
41 lh = InetAddress.getByName("localhost");
42 addrs = InetAddress.getAllByName("localhost");
43 } catch (UnknownHostException e) {
44 System.out.println ("Cant lookup localhost. cant run test");
45 return;
46 }
47
48 boolean hasIPv4Address = false;
49 boolean hasIPv6Address = false;
50 for (InetAddress addr: addrs) {
51 if (addr instanceof Inet4Address) {
52 hasIPv4Address = true;
53 }
54 if (addr instanceof Inet6Address) {
55 hasIPv6Address = true;
56 }
57 if (hasIPv4Address && hasIPv6Address) {
58 break;
59 }
60 }
61
62 String prop = System.getProperty("java.net.preferIPv6Addresses");
63 boolean preferIPv6Addresses = (prop == null) ? false : prop.equals("true");
64
65 System.out.println("java.net.preferIPv6Addresses: " + preferIPv6Addresses);
66 System.out.println("localhost resolves to:");
67 for (InetAddress addr: addrs) {
68 System.out.println(" " + addr);
69 }
70 System.out.println("InetAddres.getByName returned: " + lh);
71
72 boolean failed = false;
73 if (preferIPv6Addresses && hasIPv6Address) {
74 if (!(lh instanceof Inet6Address)) failed = true;
75 }
76 if (!preferIPv6Addresses && hasIPv4Address) {
77 if (!(lh instanceof Inet4Address)) failed = true;
78 }
79 if (failed) {
80 throw new RuntimeException("Test failed!");
81 }
82 }
83
84}