8201545: InetAddress.getByName/getAllByName should clarify empty String behavior
authorchegar
Mon, 30 Apr 2018 16:13:30 +0100
changeset 49926 b7c2996d690b
parent 49925 3deb300f0e55
child 49927 891132345d43
8201545: InetAddress.getByName/getAllByName should clarify empty String behavior Reviewed-by: chegar Contributed-by: Jaikiran Pai <jai.forums2013@gmail.com>
src/java.base/share/classes/java/net/InetAddress.java
test/jdk/java/net/InetAddress/GetLoopbackAddress.java
--- a/src/java.base/share/classes/java/net/InetAddress.java	Mon Apr 30 16:27:23 2018 +0200
+++ b/src/java.base/share/classes/java/net/InetAddress.java	Mon Apr 30 16:13:30 2018 +0100
@@ -1222,11 +1222,17 @@
      * 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&nbsp;3330</a>
      * section&nbsp;2 and <a href="http://www.ietf.org/rfc/rfc2373.txt">RFC&nbsp;2373</a>
-     * section&nbsp;2.5.3. </p>
+     * section&nbsp;2.5.3.
+     *
+     * <p> If there is a security manager, and {@code host} is not {@code null}
+     * or {@code host.length() } is not equal to zero, the security manager's
+     * {@code checkConnect} method is called with the hostname and {@code -1}
+     * as its arguments to determine if the operation is allowed.
      *
      * @param      host   the specified host, or {@code null}.
      * @return     an IP address for the given host name.
@@ -1262,18 +1268,18 @@
      * 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&nbsp;3330</a>
      * section&nbsp;2 and <a href="http://www.ietf.org/rfc/rfc2373.txt">RFC&nbsp;2373</a>
      * section&nbsp;2.5.3. </p>
      *
-     * <p> If there is a security manager and {@code host} is not
-     * null and {@code host.length() } is not equal to zero, the
-     * security manager's
-     * {@code checkConnect} method is called
-     * with the hostname and {@code -1}
-     * as its arguments to see if the operation is allowed.
+     * <p> If there is a security manager, and {@code host} is not {@code null}
+     * or {@code host.length() } is not equal to zero, the security manager's
+     * {@code checkConnect} method is called with the hostname and {@code -1}
+     * as its arguments to determine if the operation is allowed.
      *
      * @param      host   the name of the host, or {@code null}.
      * @return     an array of all the IP addresses for a given host name.
--- a/test/jdk/java/net/InetAddress/GetLoopbackAddress.java	Mon Apr 30 16:27:23 2018 +0200
+++ b/test/jdk/java/net/InetAddress/GetLoopbackAddress.java	Mon Apr 30 16:13:30 2018 +0100
@@ -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, but " + addrFromNullHost);
+        }
+        InetAddress addrFromEmptyHost = InetAddress.getByName("");
+        if (!addrFromEmptyHost.isLoopbackAddress()) {
+            throw new RuntimeException("getByName with a host of length == 0," +
+                " did not return a loopback address, but " + addrFromEmptyHost);
+        }
+
+        InetAddress[] addrsByNull = InetAddress.getAllByName(null);
+        if (!addrsByNull[0].isLoopbackAddress()) {
+            throw new RuntimeException("getAllByName(null) did not return" +
+            " a loopback address, but " + addrsByNull[0]);
+        }
+        InetAddress[] addrsByEmpty = InetAddress.getAllByName("");
+        if (!addrsByEmpty[0].isLoopbackAddress()) {
+            throw new RuntimeException("getAllByName with a host of length" +
+            " == 0, did not return a loopback address, but " + addrsByEmpty[0]);
+        }
     }
 }