8201545: InetAddress.getByName/getAllByName should clarify empty String behavior

Reviewed-by: chegar
This commit is contained in:
Jaikiran Pai 2018-04-30 16:13:30 +01:00 committed by Chris Hegarty
parent 0f478d2cfd
commit 3e47229057
2 changed files with 45 additions and 15 deletions

View File

@ -1222,11 +1222,17 @@ class InetAddress implements java.io.Serializable {
* 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 @@ class InetAddress implements java.io.Serializable {
* 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.

View File

@ -23,7 +23,7 @@
/**
* @test
* @bug 6376404
* @bug 6376404 8201545
* @summary InetAddress needs a getLoopbackAddress
*/
import java.net.*;
@ -45,17 +45,41 @@ public class GetLoopbackAddress
}
}
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]);
}
}
}