< prev index next >

test/lib/jdk/test/lib/net/IPSupport.java

Print this page

  1 /*
  2  * Copyright (c) 2019, 2020, 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 package jdk.test.lib.net;
 25 
 26 import java.io.ByteArrayOutputStream;
 27 import java.io.IOException;
 28 import java.io.PrintStream;
 29 import java.io.UncheckedIOException;
 30 import java.net.InetAddress;
 31 import java.net.InetSocketAddress;

 32 import java.net.Socket;
 33 import java.net.SocketException;
 34 import java.net.UnknownHostException;
 35 import java.security.AccessController;
 36 import java.security.PrivilegedActionException;
 37 import java.security.PrivilegedExceptionAction;
 38 import java.util.concurrent.Callable;
 39 import jtreg.SkippedException;
 40 
 41 /**
 42  * Determines Internet Protocol version support at the TCP socket level.
 43  */
 44 public class IPSupport {
 45 
 46     private static final boolean hasIPv4;
 47     private static final boolean hasIPv6;
 48     private static final boolean preferIPv4Stack;
 49     private static final boolean preferIPv6Addresses;
 50 
 51     static {

 59 
 60             hasIPv4 = runPrivilegedAction(() -> hasAddress(loopbackIPv4));
 61             hasIPv6 = runPrivilegedAction(() -> hasAddress(loopbackIPv6));
 62         } catch (UnknownHostException e) {
 63             throw new AssertionError(e);
 64         }
 65         preferIPv4Stack = runPrivilegedAction(() -> Boolean.parseBoolean(
 66             System.getProperty("java.net.preferIPv4Stack")));
 67         preferIPv6Addresses = runPrivilegedAction(() -> Boolean.parseBoolean(
 68             System.getProperty("java.net.preferIPv6Addresses")));
 69         if (!preferIPv4Stack && !hasIPv4 && !hasIPv6) {
 70             throw new AssertionError("IPv4 and IPv6 both not available and java.net.preferIPv4Stack is not true");
 71         }
 72     }
 73 
 74     private static boolean hasAddress(InetAddress address) {
 75         try (Socket socket = new Socket()) {
 76             socket.bind(new InetSocketAddress(address, 0));
 77             return true;
 78         } catch (SocketException se) {
 79             return false;








 80         } catch (IOException e) {
 81             throw new UncheckedIOException(e);
 82         }
 83     }
 84 
 85     private static <T> T runPrivilegedAction(Callable<T> callable) {
 86         try {
 87             PrivilegedExceptionAction<T> pa = () -> callable.call();
 88             return AccessController.doPrivileged(pa);
 89         } catch (PrivilegedActionException pae) {
 90             throw new UncheckedIOException((IOException) pae.getCause());
 91         }
 92     }
 93 
 94     private IPSupport() { }
 95 
 96     /**
 97      * Whether or not IPv4 is supported.
 98      */
 99     public static final boolean hasIPv4() {

  1 /*
  2  * Copyright (c) 2019, 2021, 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 package jdk.test.lib.net;
 25 
 26 import java.io.ByteArrayOutputStream;
 27 import java.io.IOException;
 28 import java.io.PrintStream;
 29 import java.io.UncheckedIOException;
 30 import java.net.InetAddress;
 31 import java.net.InetSocketAddress;
 32 import java.net.NetworkInterface;
 33 import java.net.Socket;
 34 import java.net.SocketException;
 35 import java.net.UnknownHostException;
 36 import java.security.AccessController;
 37 import java.security.PrivilegedActionException;
 38 import java.security.PrivilegedExceptionAction;
 39 import java.util.concurrent.Callable;
 40 import jtreg.SkippedException;
 41 
 42 /**
 43  * Determines Internet Protocol version support at the TCP socket level.
 44  */
 45 public class IPSupport {
 46 
 47     private static final boolean hasIPv4;
 48     private static final boolean hasIPv6;
 49     private static final boolean preferIPv4Stack;
 50     private static final boolean preferIPv6Addresses;
 51 
 52     static {

 60 
 61             hasIPv4 = runPrivilegedAction(() -> hasAddress(loopbackIPv4));
 62             hasIPv6 = runPrivilegedAction(() -> hasAddress(loopbackIPv6));
 63         } catch (UnknownHostException e) {
 64             throw new AssertionError(e);
 65         }
 66         preferIPv4Stack = runPrivilegedAction(() -> Boolean.parseBoolean(
 67             System.getProperty("java.net.preferIPv4Stack")));
 68         preferIPv6Addresses = runPrivilegedAction(() -> Boolean.parseBoolean(
 69             System.getProperty("java.net.preferIPv6Addresses")));
 70         if (!preferIPv4Stack && !hasIPv4 && !hasIPv6) {
 71             throw new AssertionError("IPv4 and IPv6 both not available and java.net.preferIPv4Stack is not true");
 72         }
 73     }
 74 
 75     private static boolean hasAddress(InetAddress address) {
 76         try (Socket socket = new Socket()) {
 77             socket.bind(new InetSocketAddress(address, 0));
 78             return true;
 79         } catch (SocketException se) {
 80             try {
 81                 return NetworkInterface.networkInterfaces()
 82                         .flatMap(NetworkInterface::inetAddresses)
 83                         .map(InetAddress::getClass)
 84                         .filter(clz -> clz.equals(address.getClass()))
 85                         .findAny().isPresent();
 86             } catch (SocketException se2) {
 87                 return false;
 88             }
 89         } catch (IOException e) {
 90             throw new UncheckedIOException(e);
 91         }
 92     }
 93 
 94     private static <T> T runPrivilegedAction(Callable<T> callable) {
 95         try {
 96             PrivilegedExceptionAction<T> pa = () -> callable.call();
 97             return AccessController.doPrivileged(pa);
 98         } catch (PrivilegedActionException pae) {
 99             throw new UncheckedIOException((IOException) pae.getCause());
100         }
101     }
102 
103     private IPSupport() { }
104 
105     /**
106      * Whether or not IPv4 is supported.
107      */
108     public static final boolean hasIPv4() {
< prev index next >