Path: blob/master/test/jdk/java/net/Inet6Address/PreferIPv6AddressesTest.java
41149 views
/*1* Copyright (c) 2016, 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 801652126* @library /test/lib27* @summary InetAddress should not always re-order addresses returned from name28* service29* @run main/othervm -Djava.net.preferIPv6Addresses=false PreferIPv6AddressesTest30* @run main/othervm -Djava.net.preferIPv6Addresses=true PreferIPv6AddressesTest31* @run main/othervm -Djava.net.preferIPv6Addresses=system PreferIPv6AddressesTest32* @run main/othervm PreferIPv6AddressesTest33*/3435import java.io.IOException;36import java.net.*;37import java.nio.channels.DatagramChannel;38import java.util.Arrays;39import java.util.stream.IntStream;40import static java.lang.System.out;41import jdk.test.lib.net.IPSupport;4243public class PreferIPv6AddressesTest {4445// A name, that if resolves, returns both IPv4 and IPv6 addresses.46static final String HOST_NAME = "www.google.com";4748static final InetAddress LOOPBACK = InetAddress.getLoopbackAddress();4950static final String preferIPV6Address =51System.getProperty("java.net.preferIPv6Addresses", "false");5253public static void main(String args[]) throws IOException {54InetAddress addrs[];55try {56addrs = InetAddress.getAllByName(HOST_NAME);57} catch (UnknownHostException e) {58out.println("Unknown host " + HOST_NAME + ", cannot run test.");59return;60}6162int firstIPv4Address = IntStream.range(0, addrs.length)63.filter(x -> addrs[x] instanceof Inet4Address)64.findFirst().orElse(-1);65int firstIPv6Address = IntStream.range(0, addrs.length)66.filter(x -> addrs[x] instanceof Inet6Address)67.findFirst().orElse(-1);6869out.println("IPv6 supported: " + IPSupport.hasIPv6());70out.println("Addresses: " + Arrays.asList(addrs));7172if (preferIPV6Address.equalsIgnoreCase("true") && firstIPv6Address != -1) {73int off = firstIPv4Address != -1 ? firstIPv4Address : addrs.length;74assertAllv6Addresses(addrs, 0, off);75assertAllv4Addresses(addrs, off, addrs.length);76assertLoopbackAddress(Inet6Address.class);77assertAnyLocalAddress(Inet6Address.class);78} else if (preferIPV6Address.equalsIgnoreCase("false") && firstIPv4Address != -1) {79int off = firstIPv6Address != -1 ? firstIPv6Address : addrs.length;80assertAllv4Addresses(addrs, 0, off);81assertAllv6Addresses(addrs, off, addrs.length);82assertLoopbackAddress(Inet4Address.class);83assertAnyLocalAddress(Inet4Address.class);84} else if (preferIPV6Address.equalsIgnoreCase("system") && IPSupport.hasIPv6()) {85assertLoopbackAddress(Inet6Address.class);86assertAnyLocalAddress(Inet6Address.class);87} else if (preferIPV6Address.equalsIgnoreCase("system") && !IPSupport.hasIPv6()) {88assertLoopbackAddress(Inet4Address.class);89assertAnyLocalAddress(Inet4Address.class);90}91}9293static void assertAllv4Addresses(InetAddress[] addrs, int off, int len) {94IntStream.range(off, len)95.mapToObj(x -> addrs[x])96.forEach(x -> {97if (!(x instanceof Inet4Address))98throw new RuntimeException("Expected IPv4, got " + x);99});100}101102static void assertAllv6Addresses(InetAddress[] addrs, int off, int len) {103IntStream.range(off, len)104.mapToObj(x -> addrs[x])105.forEach(x -> {106if (!(x instanceof Inet6Address))107throw new RuntimeException("Expected IPv6, got " + x);108});109}110111static void assertLoopbackAddress(Class<?> expectedType) {112if (!LOOPBACK.getClass().isAssignableFrom(expectedType))113throw new RuntimeException("Expected " + expectedType114+ ", got " + LOOPBACK.getClass());115}116117static void assertAnyLocalAddress(Class<?> expectedType) {118InetAddress anyAddr = (new InetSocketAddress(0)).getAddress();119if (!anyAddr.getClass().isAssignableFrom(expectedType))120throw new RuntimeException("Expected " + expectedType121+ ", got " + anyAddr.getClass());122}123}124125126