Explorar el Código

HADOOP-9095. Backport HADOOP-8372: NetUtils.normalizeHostName() incorrectly handles hostname starting with a numeric character. Contributed by Jing Zhao

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-1@1413969 13f79535-47bb-0310-9956-ffa450edef68
Tsz-wo Sze hace 12 años
padre
commit
5aa69172e5

+ 4 - 0
CHANGES.txt

@@ -312,6 +312,10 @@ Release 1.2.0 - unreleased
     HDFS-4207. All hadoop fs operations fail if the default fs is down even if 
     a different fs is specified in the command. (Jing Zhao via suresh)
 
+    HADOOP-9095. Backport HADOOP-8372: NetUtils.normalizeHostName() incorrectly
+    handles hostname starting with a numeric character.  (Jing Zhao via
+    szetszwo)
+
 Release 1.1.1 - Unreleased
 
   INCOMPATIBLE CHANGES

+ 4 - 8
src/core/org/apache/hadoop/net/NetUtils.java

@@ -536,15 +536,11 @@ public class NetUtils {
    * @return its IP address in the string format
    */
   public static String normalizeHostName(String name) {
-    if (Character.digit(name.charAt(0), 10) != -1) { //FIXME 
+    try {
+      InetAddress ipAddress = InetAddress.getByName(name);
+      return ipAddress.getHostAddress();
+    } catch (UnknownHostException e) {
       return name;
-    } else {
-      try {
-        InetAddress ipAddress = InetAddress.getByName(name);
-        return ipAddress.getHostAddress();
-      } catch (UnknownHostException e) {
-        return name;
-      }
     }
   }
   

+ 25 - 0
src/test/org/apache/hadoop/net/TestNetUtils.java

@@ -18,6 +18,7 @@
 package org.apache.hadoop.net;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
@@ -30,6 +31,8 @@ import java.net.Socket;
 import java.net.SocketException;
 import java.net.URI;
 import java.net.UnknownHostException;
+import java.util.Arrays;
+import java.util.List;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.hadoop.conf.Configuration;
@@ -321,4 +324,26 @@ public class TestNetUtils {
     String gotStr = StringUtils.join(got, ", ");
     assertEquals(expectStr, gotStr);
   }
+  
+  /**
+   * Test for {@link NetUtils#normalizeHostNames
+
+   */
+  @Test
+  public void testNormalizeHostName() {
+    List<String> hosts = Arrays.asList(new String[] { "127.0.0.1", "localhost",
+        "3w.org", "UnknownHost" });
+    List<String> normalizedHosts = NetUtils.normalizeHostNames(hosts);
+    // when ipaddress is normalized, same address is expected in return
+    assertEquals(normalizedHosts.get(0), hosts.get(0));
+    // for normalizing a resolvable hostname, resolved ipaddress is expected in
+    // return
+    assertFalse(normalizedHosts.get(1).equals(hosts.get(1)));
+    assertEquals(normalizedHosts.get(1), hosts.get(0));
+    // when normalizing a valid resolvable hostname start with numeric,
+    // its ipaddress is expected to return
+    assertFalse(normalizedHosts.get(2).equals(hosts.get(2)));
+    // return the same hostname after normalizing a irresolvable hostname.
+    assertEquals(normalizedHosts.get(3), hosts.get(3));
+  }
 }