7032247: java/net/InetAddress/GetLocalHostWithSM.java fails if hostname resolves to loopback address
authorchegar
Fri, 07 Sep 2012 14:00:31 +0100
changeset 13787 19d54bca65d7
parent 13786 dfe9a95e1c30
child 13788 3f38e525f30a
7032247: java/net/InetAddress/GetLocalHostWithSM.java fails if hostname resolves to loopback address Summary: TESTBUG Reviewed-by: chegar, alanb Contributed-by: Eric Wang <yiming.wang@oracle.com>
jdk/test/java/net/InetAddress/GetLocalHostWithSM.java
--- a/jdk/test/java/net/InetAddress/GetLocalHostWithSM.java	Fri Sep 07 10:24:22 2012 +0800
+++ b/jdk/test/java/net/InetAddress/GetLocalHostWithSM.java	Fri Sep 07 14:00:31 2012 +0100
@@ -41,14 +41,13 @@
         public static void main(String[] args) throws Exception {
 
             // try setting the local hostname
-            try {
-                System.setProperty("host.name", InetAddress.
-                                                getLocalHost().
-                                                getHostName());
-            } catch (UnknownHostException e) {
-                System.out.println("Cannot find the local hostname, " +
-                        "no nameserver entry found");
+            InetAddress localHost = InetAddress.getLocalHost();
+            if (localHost.isLoopbackAddress()) {
+                System.err.println("Local host name is resolved into a loopback address. Quit now!");
+                return;
             }
+            System.setProperty("host.name", localHost.
+                                            getHostName());
             String policyFileName = System.getProperty("test.src", ".") +
                           "/" + "policy.file";
             System.setProperty("java.security.policy", policyFileName);
@@ -66,6 +65,7 @@
                                 new MyAction(), null);
 
             if (localHost1.equals(localHost2)) {
+                System.out.println("localHost1 = " + localHost1);
                 throw new RuntimeException("InetAddress.getLocalHost() test " +
                                            " fails. localHost2 should be " +
                                            " the real address instead of " +