changeset 16228:ed04e8712261

8169115: java/net/InetAddress/ptr/lookup.sh failed intermittently Reviewed-by: dsamersoff
author xiaofeya
date Thu, 08 Dec 2016 07:21:50 -0800
parents 7e27a1e6e339
children 27ff083d0e07
files test/java/net/InetAddress/ptr/Lookup.java test/java/net/InetAddress/ptr/lookup.sh
diffstat 2 files changed, 64 insertions(+), 92 deletions(-) [+]
line wrap: on
line diff
--- a/test/java/net/InetAddress/ptr/Lookup.java	Thu Dec 08 15:35:56 2016 +0300
+++ b/test/java/net/InetAddress/ptr/Lookup.java	Thu Dec 08 07:21:50 2016 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -22,35 +22,79 @@
  */
 
 /*
+ * Lookup/reverse lookup class for regression test 4773521
+ * @test
+ * @bug 4773521
+ * @summary Test that reverse lookups of IPv4 addresses work when IPv6
+ *          is enabled
+ * @library /test/lib
+ * @build jdk.test.lib.JDKToolFinder
+ *        jdk.test.lib.process.OutputAnalyzer
+ *        Lookup
+ * @run main Lookup root
  *
- *
- * Lookup/reverse lookup class for regression test 4773521 - see
- * lookup.sh for details.
  */
+import java.io.IOException;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
+import java.util.List;
+
+import jdk.test.lib.JDKToolFinder;
+import jdk.test.lib.process.OutputAnalyzer;
 
 public class Lookup {
-    public static void main(String args[]) throws UnknownHostException {
+    private static final String HOST = "icann.org";
+    private static final String SKIP = "SKIP";
+    private static final String CLASS_PATH = System.getProperty(
+            "test.class.path");
+
+    public static void main(String args[]) throws IOException {
+        String addr = null;
+        String ipv4Name = null;
+        if (args.length == 0) {
+            // First check that host resolves to IPv4 address
+            try {
+                InetAddress ia = InetAddress.getByName(HOST);
+                addr = ia.getHostAddress();
+            } catch (UnknownHostException e) {
+                System.out.print(SKIP);
+                return;
+            }
+        } else {
+            String tmp = lookupWithIPv4Prefer();
+            System.out.println("IPv4 lookup results: [" + tmp + "]");
+            if (SKIP.equals(tmp)) {
+                System.out.println(HOST + " can't be resolved - test skipped.");
+                return;
+            }
+
+            String[] strs = tmp.split(":");
+            addr = strs[0];
+            ipv4Name = strs[1];
+        }
 
         // reverse lookup
-
-        if (args[0].equals("-q=PTR")) {
-            InetAddress ia = InetAddress.getByName(args[1]);
-            System.out.println(ia.getHostName());
+        InetAddress ia = InetAddress.getByName(addr);
+        String name = ia.getHostName();
+        if (args.length == 0) {
+            System.out.print(addr + ":" + name);
             return;
+        } else {
+            System.out.println("(default) " + addr + "--> " + name);
+            if (!ipv4Name.equals(name)) {
+                throw new RuntimeException("Mismatch between default"
+                        + " and java.net.preferIPv4Stack=true results");
+            }
         }
-
-        // lookup address
-
-        String addr;
-        if (args[0].equals("-q=A")) {
-            addr = args[1];
-        } else {
-            addr = args[0];
-        }
-        InetAddress ia = InetAddress.getByName(args[1]);
-        System.out.println(ia.getHostAddress());
     }
 
+    static String lookupWithIPv4Prefer() throws IOException {
+        String java = JDKToolFinder.getTestJDKTool("java");
+        String testClz = Lookup.class.getName();
+        List<String> cmd = List.of(java, "-Djava.net.preferIPv4Stack=true",
+                "-cp", CLASS_PATH, testClz);
+        System.out.println("Executing: " + cmd);
+        return new OutputAnalyzer(new ProcessBuilder(cmd).start()).getOutput();
+    }
 }
+
--- a/test/java/net/InetAddress/ptr/lookup.sh	Thu Dec 08 15:35:56 2016 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,72 +0,0 @@
-#!/bin/sh
-
-#
-# Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
-# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
-#
-# This code is free software; you can redistribute it and/or modify it
-# under the terms of the GNU General Public License version 2 only, as
-# published by the Free Software Foundation.
-#
-# This code is distributed in the hope that it will be useful, but WITHOUT
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-# version 2 for more details (a copy is included in the LICENSE file that
-# accompanied this code).
-#
-# You should have received a copy of the GNU General Public License version
-# 2 along with this work; if not, write to the Free Software Foundation,
-# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
-#
-# 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 4773521
-# @build Lookup
-# @run shell lookup.sh
-# @summary Test that reverse lookups of IPv4 addresses work when IPv6
-#          is enabled. 
-
-# The host that we try to resolve
-
-HOST=javaweb.sfbay.sun.com
-
-CLASSPATH=${TESTCLASSES}
-export CLASSPATH
-JAVA="${TESTJAVA}/bin/java"
-
-
-# First check that host resolves to IPv4 address.
-
-echo ''
-ADDR=`$JAVA -Djava.net.preferIPv4Stack=true Lookup -q=A $HOST`
-if [ $? != 0 ]; then
-    echo "$HOST can't be resolved - test skipped."
-    exit 0
-fi
-echo "$HOST --> $ADDR"
-
-
-# IPv4 reverse lookup
-echo ''
-OUT1=`$JAVA ${TESTVMOPTS} -Djava.net.preferIPv4Stack=true Lookup -q=PTR $ADDR`
-echo "(IPv4) $ADDR --> $OUT1"
-
-
-# reverse lookup (default)
-echo ''
-OUT2=`$JAVA ${TESTVMOPTS} Lookup -q=PTR $ADDR`
-echo "(default) $ADDR --> $OUT2"
-
-
-# Compare results
-if [ "$OUT1" != "$OUT2" ]; then
-    echo ''
-    echo "Mistmatch between default and java.net.preferIPv4Stack=true results"
-    exit 1
-fi
-