< prev index next >

test/java/net/Inet6Address/B6558853.java

Print this page

        

@@ -19,47 +19,46 @@
  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 
-/**
+/*
  * @test
  * @bug 6558853
  * @summary  getHostAddress() on connections using IPv6 link-local addrs should have zone id
+ * @library /lib/testlibrary
+ * @build jdk.testlibrary.NetworkConfiguration
+ * @run main B6558853
  */
+
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.*;
-import java.util.Enumeration;
+import java.util.Optional;
+import jdk.testlibrary.NetworkConfiguration;
 
 public class B6558853 implements Runnable {
     private InetAddress addr = null;
     private int port = 0;
 
     public static void main(String[] args) throws Exception {
-        ServerSocket ss = new ServerSocket(0);
-        int port = ss.getLocalPort();
-        Enumeration<NetworkInterface> l = NetworkInterface.getNetworkInterfaces();
-        InetAddress dest = null;
-        while (l.hasMoreElements() && dest == null) {
-            NetworkInterface nif = l.nextElement();
-            if (!nif.isUp())
-                continue;
-
-            for (InterfaceAddress a : nif.getInterfaceAddresses()) {
-                if (a.getAddress() instanceof Inet6Address) {
-                    Inet6Address a6 = (Inet6Address) a.getAddress();
-                    if (a6.isLinkLocalAddress()) {
-                        dest = a6;
-                    }
-                    break;
-                }
-            }
+        Optional<Inet6Address> oaddr = NetworkConfiguration.probe()
+                .ip6Addresses()
+                .filter(a -> a.isLinkLocalAddress())
+                .findFirst();
+
+        if (!oaddr.isPresent()) {
+            System.out.println("No suitable interface found. Exiting.");
+            return;
         }
+
+        Inet6Address dest = oaddr.get();
         System.out.println("Using " + dest);
-        if (dest != null) {
+
+        try (ServerSocket ss = new ServerSocket(0)) {
+            int port = ss.getLocalPort();
             B6558853 test = new B6558853(dest, port);
             Thread thread = new Thread(test);
             thread.start();
             Socket s = ss.accept();
             InetAddress a = s.getInetAddress();
< prev index next >