Fix name constant used in Inet6Address.LOOPBACK

In Android M, the name for the IPv6 loopback was set as
"ip6-localhost" to match the /etc/hosts file. The switch
to OpenJDK partly reverted this, but only the hardcoded
name on Inet6Address.LOOPBACK.

Looking up "localhost" and "ip6-localhost" by name
still yielded the correct result, i.e. the IPv4 and IPv6
address accordingly.

The inconsistency only appears to lie in the name
constant. This change adds an explicit check for the
names: previously it was only hinted at.

Also reinstitute some DNS lookup testing.

Test: Ran CTS/android.core.tests.libcore.package.okhttp
Test: Ran CTS/android.core.tests.libcore.package.libcore
Test: Ran CTS/android.core.tests.libcore.package.harmony_java_net

Bug: 29311351
(cherry picked from commit 21d479dc790dfe7b9f0d38b3820b49b563ce734a)

Change-Id: I4291827c5534236ccbe8e93ed16cb5942bf2d509
diff --git a/luni/src/test/java/libcore/java/net/InetAddressTest.java b/luni/src/test/java/libcore/java/net/InetAddressTest.java
index d496801..600196b 100644
--- a/luni/src/test/java/libcore/java/net/InetAddressTest.java
+++ b/luni/src/test/java/libcore/java/net/InetAddressTest.java
@@ -20,6 +20,7 @@
 import java.net.Inet4Address;
 import java.net.Inet6Address;
 import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.net.NetworkInterface;
 import java.net.SocketException;
 import java.net.UnknownHostException;
@@ -334,11 +335,15 @@
 
     public void test_getHostNameCaches() throws Exception {
         InetAddress inetAddress = InetAddress.getByAddress(LOOPBACK6_BYTES);
-        // TODO(narayan): Investigate why these tests are suppressed.
-        // assertEquals("::1", inetAddress.getHostString());
+
+        // There should be no cached name.
+        assertEquals("::1", getHostStringWithoutReverseDns(inetAddress));
+
+        // Force the reverse-DNS lookup.
         assertEquals("ip6-localhost", inetAddress.getHostName());
-        // getHostString() should now be different.
-        // assertEquals("ip6-localhost", inetAddress.getHostString());
+
+        // The cached name should now be different.
+        assertEquals("ip6-localhost", getHostStringWithoutReverseDns(inetAddress));
     }
 
     public void test_getByAddress_loopbackIpv4() throws Exception {
@@ -367,7 +372,7 @@
 
     public void test_getByName_empty() throws Exception {
         InetAddress inetAddress = InetAddress.getByName("");
-        assertEquals(LOOPBACK6_BYTES, "localhost", inetAddress);
+        assertEquals(LOOPBACK6_BYTES, "ip6-localhost", inetAddress);
         assertTrue(inetAddress.isLoopbackAddress());
     }
 
@@ -418,6 +423,13 @@
         assertEquals(expectedLoopbackAddresses, createSet(inetAddresses));
     }
 
+    // http://b/29311351
+    public void test_loopbackConstantsPreInitializedNames() {
+        // Note: Inet6Address / Inet4Address equals() does not check host name.
+        assertEquals("ip6-localhost", getHostStringWithoutReverseDns(Inet6Address.LOOPBACK));
+        assertEquals("localhost", getHostStringWithoutReverseDns(Inet4Address.LOOPBACK));
+    }
+
     private static void assertEquals(
         byte[] expectedAddressBytes, String expectedHostname, InetAddress actual) {
         assertArrayEquals(expectedAddressBytes, actual.getAddress());
@@ -433,4 +445,11 @@
     private static Set<InetAddress> createSet(InetAddress... members) {
         return new HashSet<InetAddress>(Arrays.asList(members));
     }
+
+    private static String getHostStringWithoutReverseDns(InetAddress inetAddress) {
+        // The InetAddress API provides no way of avoiding a DNS lookup, but InetSocketAddress
+        // does via InetSocketAddress.getHostString().
+        InetSocketAddress inetSocketAddress = new InetSocketAddress(inetAddress, 9999);
+        return inetSocketAddress.getHostString();
+    }
 }
diff --git a/ojluni/src/main/java/java/net/Inet6Address.java b/ojluni/src/main/java/java/net/Inet6Address.java
index 4b353e5..d55fab9 100755
--- a/ojluni/src/main/java/java/net/Inet6Address.java
+++ b/ojluni/src/main/java/java/net/Inet6Address.java
@@ -174,7 +174,7 @@
             new Inet6Address("::", new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0);
 
     /** @hide */
-    public static final InetAddress LOOPBACK = new Inet6Address("localhost",
+    public static final InetAddress LOOPBACK = new Inet6Address("ip6-localhost",
             new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 0);
 
     /**