< 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 {
 52         try {
 53             InetAddress loopbackIPv4 = InetAddress.getByAddress(
 54                     new byte[] {0x7F, 0x00, 0x00, 0x01});
 55 
 56             InetAddress loopbackIPv6 = InetAddress.getByAddress(
 57                     new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 58                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01});
 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() {
100         return hasIPv4;
101     }

  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.Inet4Address;
 31 import java.net.Inet6Address;
 32 import java.net.InetAddress;
 33 import java.net.ProtocolFamily;
 34 import java.net.StandardProtocolFamily;
 35 import java.nio.channels.SocketChannel;

 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 {
 53         hasIPv4 = runPrivilegedAction(() -> isSupported(Inet4Address.class));
 54         hasIPv6 = runPrivilegedAction(() -> isSupported(Inet6Address.class));











 55         preferIPv4Stack = runPrivilegedAction(() -> Boolean.parseBoolean(
 56             System.getProperty("java.net.preferIPv4Stack")));
 57         preferIPv6Addresses = runPrivilegedAction(() -> Boolean.parseBoolean(
 58             System.getProperty("java.net.preferIPv6Addresses")));
 59         if (!preferIPv4Stack && !hasIPv4 && !hasIPv6) {
 60             throw new AssertionError("IPv4 and IPv6 both not available and java.net.preferIPv4Stack is not true");
 61         }
 62     }
 63 
 64     private static boolean isSupported(Class<? extends InetAddress> addressType) {
 65         ProtocolFamily family = addressType == Inet4Address.class ?
 66                 StandardProtocolFamily.INET : StandardProtocolFamily.INET6;
 67         try (var sc = SocketChannel.open(family)) {
 68             return true;
 69         } catch (IOException | UnsupportedOperationException ex) {
 70             return false;


 71         }
 72     }
 73 
 74     private static <T> T runPrivilegedAction(Callable<T> callable) {
 75         try {
 76             PrivilegedExceptionAction<T> pa = () -> callable.call();
 77             return AccessController.doPrivileged(pa);
 78         } catch (PrivilegedActionException pae) {
 79             throw new UncheckedIOException((IOException) pae.getCause());
 80         }
 81     }
 82 
 83     private IPSupport() { }
 84 
 85     /**
 86      * Whether or not IPv4 is supported.
 87      */
 88     public static final boolean hasIPv4() {
 89         return hasIPv4;
 90     }
< prev index next >