8224014: Don't run test/jdk/java/net/NetworkInterface/IPv4Only.java in IPv6 only environment
authoraeubanks
Wed, 15 May 2019 16:21:51 -0700
changeset 54948 751a19168b11
parent 54947 69d1dff1bb70
child 54949 7bfb499b4f38
8224014: Don't run test/jdk/java/net/NetworkInterface/IPv4Only.java in IPv6 only environment Reviewed-by: chegar, dfuchs Contributed-by: aeubanks@google.com
test/jdk/java/net/NetworkInterface/IPv4Only.java
--- a/test/jdk/java/net/NetworkInterface/IPv4Only.java	Wed May 15 17:21:18 2019 -0700
+++ b/test/jdk/java/net/NetworkInterface/IPv4Only.java	Wed May 15 16:21:51 2019 -0700
@@ -23,6 +23,7 @@
 
 /* @test
  * @bug   6964714
+ * @library /test/lib
  * @run main/othervm -Djava.net.preferIPv4Stack=true IPv4Only
  * @summary Test the networkinterface listing with java.net.preferIPv4Stack=true.
  */
@@ -30,19 +31,36 @@
 
 import java.net.*;
 import java.util.*;
-
+import jdk.test.lib.net.IPSupport;
 
 public class IPv4Only {
     public static void main(String[] args) throws Exception {
-        Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
-        while (nifs.hasMoreElements()) {
-            NetworkInterface nif = nifs.nextElement();
-            Enumeration<InetAddress> addrs = nif.getInetAddresses();
-            while (addrs.hasMoreElements()) {
-               InetAddress hostAddr = addrs.nextElement();
-               if ( hostAddr instanceof Inet6Address ){
-                    throw new RuntimeException( "NetworkInterfaceV6List failed - found v6 address " + hostAddr.getHostAddress() );
-               }
+        if (IPSupport.hasIPv4()) {
+            System.out.println("Testing IPv4");
+            Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
+            while (nifs.hasMoreElements()) {
+                NetworkInterface nif = nifs.nextElement();
+                Enumeration<InetAddress> addrs = nif.getInetAddresses();
+                while (addrs.hasMoreElements()) {
+                   InetAddress hostAddr = addrs.nextElement();
+                   if ( hostAddr instanceof Inet6Address ){
+                        throw new RuntimeException( "NetworkInterfaceV6List failed - found v6 address " + hostAddr.getHostAddress() );
+                   }
+                }
+            }
+        } else {
+            try {
+                NetworkInterface.getNetworkInterfaces();
+                throw new RuntimeException("NetworkInterface.getNetworkInterfaces() should throw SocketException");
+            } catch (SocketException expected) {
+                System.out.println("caught expected exception: " + expected);
+            }
+
+            try {
+                NetworkInterface.networkInterfaces();
+                throw new RuntimeException("NetworkInterface.networkInterfaces() should throw SocketException");
+            } catch (SocketException expected) {
+                System.out.println("caught expected exception: " + expected);
             }
         }
     }