[PATCH] JDK-8201545 Clarify the return value of InetAddress.getByName/getAllName for empty host value

Jaikiran Pai jai.forums2013 at gmail.com
Fri Apr 20 10:08:19 UTC 2018


Hi,

The attached patch addresses the issue noted in [1], by updating the 
javadoc of InetAddress.getByName and InetAddress.getAllByName to clarify 
that these methods return a loopback address, if the host parameter is 
an empty string (same behaviour as host == null). The patch also updates 
an existing test case to test these methods for both null and empty 
parameter values.

After looking at existing tests for InetAddress, I felt the existing 
GetLoopbackAddress.java test case is closest to what we are testing 
here. One thing I need input on, for the GetLoopbackAddress.java test 
class, is the value of @summary. Should I update it to include a summary 
of this new test too, or should I remove it altogether? I have anyway 
updated the @bug to include the JIRA id of this issue. I am open to 
creating a fresh new test case class just for this issue, if that's better.

[1] https://bugs.openjdk.java.net/browse/JDK-8201545

-Jaikiran

-------------- next part --------------
# HG changeset patch
# User Jaikiran Pai <jaikiran.pai at gmail.com>
# Date 1524215697 -19800
#      Fri Apr 20 14:44:57 2018 +0530
# Node ID fbafd0fdcb0033f7d6b65b5686b26e8f23a46c5b
# Parent  dd5db907ab7e09c87b4dd246f960d97e33fe9c7a
JDK-8201545 Clarify the return value of InetAddress#getByName/getAllByName for empty host value

diff --git a/src/java.base/share/classes/java/net/InetAddress.java b/src/java.base/share/classes/java/net/InetAddress.java
--- a/src/java.base/share/classes/java/net/InetAddress.java
+++ b/src/java.base/share/classes/java/net/InetAddress.java
@@ -1222,8 +1222,9 @@
      * supported. See <a href="Inet6Address.html#scoped">here</a> for a description of IPv6
      * scoped addresses.
      *
-     * <p> If the host is {@code null} then an {@code InetAddress}
-     * representing an address of the loopback interface is returned.
+     * <p> If the host is {@code null} or {@code host.length()} is equal to zero,
+     * then an {@code InetAddress} representing an address of the
+     * loopback interface is returned.
      * See <a href="http://www.ietf.org/rfc/rfc3330.txt">RFC 3330</a>
      * section 2 and <a href="http://www.ietf.org/rfc/rfc2373.txt">RFC 2373</a>
      * section 2.5.3. </p>
@@ -1262,8 +1263,9 @@
      * also be qualified by appending a scoped zone identifier or scope_id.
      * The syntax and usage of scope_ids is described
      * <a href="Inet6Address.html#scoped">here</a>.
-     * <p> If the host is {@code null} then an {@code InetAddress}
-     * representing an address of the loopback interface is returned.
+     * <p> If the host is {@code null} or {@code host.length()} is equal to zero,
+     * then an {@code InetAddress} representing an address of the
+     * loopback interface is returned.
      * See <a href="http://www.ietf.org/rfc/rfc3330.txt">RFC 3330</a>
      * section 2 and <a href="http://www.ietf.org/rfc/rfc2373.txt">RFC 2373</a>
      * section 2.5.3. </p>
diff --git a/test/jdk/java/net/InetAddress/GetLoopbackAddress.java b/test/jdk/java/net/InetAddress/GetLoopbackAddress.java
--- a/test/jdk/java/net/InetAddress/GetLoopbackAddress.java
+++ b/test/jdk/java/net/InetAddress/GetLoopbackAddress.java
@@ -23,7 +23,7 @@
 
 /**
  * @test
- * @bug 6376404
+ * @bug 6376404 8201545
  * @summary InetAddress needs a getLoopbackAddress
  */
 import java.net.*;
@@ -45,17 +45,41 @@
         }
     }
 
-    public static void main(String[] args) {
+    public static void main(String[] args) throws Exception {
         InetAddress addr = InetAddress.getLoopbackAddress();
 
-        if (!(addr.equals(IPv4Loopback) || addr.equals(IPv6Loopback)))
+        if (!(addr.equals(IPv4Loopback) || addr.equals(IPv6Loopback))) {
             throw new RuntimeException("Failed: getLoopbackAddress" +
                  " not returning a valid loopback address");
+        }
 
         InetAddress addr2 = InetAddress.getLoopbackAddress();
 
-        if (addr != addr2)
+        if (addr != addr2) {
             throw new RuntimeException("Failed: getLoopbackAddress" +
                 " should return a reference to the same InetAddress loopback instance.");
+        }
+
+        InetAddress addrFromNullHost = InetAddress.getByName(null);
+        if (!addrFromNullHost.isLoopbackAddress()) {
+            throw new RuntimeException("getByName(null) did not return a" +
+            " loopback address");
+        }
+        InetAddress addrFromEmptyHost = InetAddress.getByName("");
+        if (!addrFromEmptyHost.isLoopbackAddress()) {
+            throw new RuntimeException("getByName with a host of length == 0," +
+                " did not return a loopback address");
+        }
+        
+        InetAddress[] adrsByNull = InetAddress.getAllByName(null);
+        if (!adrsByNull[0].isLoopbackAddress()) {
+            throw new RuntimeException("getAllByName(null) did not return" +
+            " a loopback address");
+        }
+        InetAddress[] adrsByEmpty = InetAddress.getAllByName("");
+        if (!adrsByEmpty[0].isLoopbackAddress()) {
+            throw new RuntimeException("getAllByName with a host of length" +
+            " == 0, did not return a loopback address");
+        }
     }
 }


More information about the net-dev mailing list