test/jdk/java/net/Inet6Address/PreferIPv6AddressesTest.java
changeset 47216 71c04702a3d5
parent 38552 ca398af91529
child 54770 62b6e7587b1f
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2016, 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 
       
    24 /**
       
    25  * @test
       
    26  * @bug 8016521
       
    27  * @summary InetAddress should not always re-order addresses returned from name
       
    28  *          service
       
    29  * @run main/othervm -Djava.net.preferIPv6Addresses=false PreferIPv6AddressesTest
       
    30  * @run main/othervm -Djava.net.preferIPv6Addresses=true PreferIPv6AddressesTest
       
    31  * @run main/othervm -Djava.net.preferIPv6Addresses=system PreferIPv6AddressesTest
       
    32  * @run main/othervm PreferIPv6AddressesTest
       
    33  */
       
    34 
       
    35 import java.io.IOException;
       
    36 import java.net.*;
       
    37 import java.nio.channels.DatagramChannel;
       
    38 import java.util.Arrays;
       
    39 import java.util.stream.IntStream;
       
    40 import static java.lang.System.out;
       
    41 
       
    42 public class PreferIPv6AddressesTest {
       
    43 
       
    44     // A name, that if resolves, returns both IPv4 and IPv6 addresses.
       
    45     static final String HOST_NAME = "www.google.com";
       
    46 
       
    47     static final InetAddress LOOPBACK = InetAddress.getLoopbackAddress();
       
    48 
       
    49     static final String preferIPV6Address =
       
    50             System.getProperty("java.net.preferIPv6Addresses", "false");
       
    51 
       
    52     public static void main(String args[]) throws IOException {
       
    53 
       
    54         InetAddress addrs[];
       
    55         try {
       
    56             addrs = InetAddress.getAllByName(HOST_NAME);
       
    57         } catch (UnknownHostException e) {
       
    58             out.println("Unknown host " + HOST_NAME + ", cannot run test.");
       
    59             return;
       
    60         }
       
    61 
       
    62         int firstIPv4Address = IntStream.range(0, addrs.length)
       
    63                 .filter(x -> addrs[x] instanceof Inet4Address)
       
    64                 .findFirst().orElse(-1);
       
    65         int firstIPv6Address = IntStream.range(0, addrs.length)
       
    66                 .filter(x -> addrs[x] instanceof Inet6Address)
       
    67                 .findFirst().orElse(-1);
       
    68 
       
    69         out.println("IPv6 supported: " + IPv6Supported());
       
    70         out.println("Addresses: " + Arrays.asList(addrs));
       
    71 
       
    72         if (preferIPV6Address.equalsIgnoreCase("true") && firstIPv6Address != -1) {
       
    73             int off = firstIPv4Address != -1 ? firstIPv4Address : addrs.length;
       
    74             assertAllv6Addresses(addrs, 0, off);
       
    75             assertAllv4Addresses(addrs, off, addrs.length);
       
    76             assertLoopbackAddress(Inet6Address.class);
       
    77             assertAnyLocalAddress(Inet6Address.class);
       
    78         } else if (preferIPV6Address.equalsIgnoreCase("false") && firstIPv4Address != -1) {
       
    79             int off = firstIPv6Address != -1 ? firstIPv6Address : addrs.length;
       
    80             assertAllv4Addresses(addrs, 0, off);
       
    81             assertAllv6Addresses(addrs, off, addrs.length);
       
    82             assertLoopbackAddress(Inet4Address.class);
       
    83             assertAnyLocalAddress(Inet4Address.class);
       
    84         } else if (preferIPV6Address.equalsIgnoreCase("system") && IPv6Supported()) {
       
    85             assertLoopbackAddress(Inet6Address.class);
       
    86             assertAnyLocalAddress(Inet6Address.class);
       
    87         } else if (preferIPV6Address.equalsIgnoreCase("system") && !IPv6Supported()) {
       
    88             assertLoopbackAddress(Inet4Address.class);
       
    89             assertAnyLocalAddress(Inet4Address.class);
       
    90         }
       
    91     }
       
    92 
       
    93     static void assertAllv4Addresses(InetAddress[] addrs, int off, int len) {
       
    94         IntStream.range(off, len)
       
    95                  .mapToObj(x -> addrs[x])
       
    96                  .forEach(x -> {
       
    97                      if (!(x instanceof Inet4Address))
       
    98                          throw new RuntimeException("Expected IPv4, got " + x);
       
    99                  });
       
   100     }
       
   101 
       
   102     static void assertAllv6Addresses(InetAddress[] addrs, int off, int len) {
       
   103         IntStream.range(off, len)
       
   104                 .mapToObj(x -> addrs[x])
       
   105                 .forEach(x -> {
       
   106                     if (!(x instanceof Inet6Address))
       
   107                         throw new RuntimeException("Expected IPv6, got " + x);
       
   108                 });
       
   109     }
       
   110 
       
   111     static void assertLoopbackAddress(Class<?> expectedType) {
       
   112         if (!LOOPBACK.getClass().isAssignableFrom(expectedType))
       
   113             throw new RuntimeException("Expected " + expectedType
       
   114                     + ", got " + LOOPBACK.getClass());
       
   115     }
       
   116 
       
   117     static void assertAnyLocalAddress(Class<?> expectedType) {
       
   118         InetAddress anyAddr = (new InetSocketAddress(0)).getAddress();
       
   119         if (!anyAddr.getClass().isAssignableFrom(expectedType))
       
   120             throw new RuntimeException("Expected " + expectedType
       
   121                     + ", got " + anyAddr.getClass());
       
   122     }
       
   123 
       
   124     static boolean IPv6Supported() throws IOException {
       
   125         try {
       
   126             DatagramChannel.open(StandardProtocolFamily.INET6);
       
   127             return true;
       
   128         } catch (UnsupportedOperationException x) {
       
   129             return false;
       
   130         }
       
   131     }
       
   132 }