HADOOP-8372. NetUtils.normalizeHostName() incorrectly handles hostname starting with a numeric character. Contributed by Junping Du.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1336446 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Suresh Srinivas 2012-05-09 23:18:15 +00:00
parent 0308423b9c
commit 1ceecf2aa2
4 changed files with 36 additions and 18 deletions

View File

@ -432,6 +432,9 @@ Release 2.0.0 - UNRELEASED
HADOOP-8359. Fix javadoc warnings in Configuration. (Anupam Seth via HADOOP-8359. Fix javadoc warnings in Configuration. (Anupam Seth via
szetszwo) szetszwo)
HADOOP-8372. NetUtils.normalizeHostName() incorrectly handles hostname
starting with a numeric character. (Junping Du via suresh)
BREAKDOWN OF HADOOP-7454 SUBTASKS BREAKDOWN OF HADOOP-7454 SUBTASKS
HADOOP-7455. HA: Introduce HA Service Protocol Interface. (suresh) HADOOP-7455. HA: Introduce HA Service Protocol Interface. (suresh)

View File

@ -494,7 +494,7 @@ public static void connect(Socket socket,
* also takes a local address and port to bind the socket to. * also takes a local address and port to bind the socket to.
* *
* @param socket * @param socket
* @param address the remote address * @param endpoint the remote address
* @param localAddr the local address to bind the socket to * @param localAddr the local address to bind the socket to
* @param timeout timeout in milliseconds * @param timeout timeout in milliseconds
*/ */
@ -549,16 +549,11 @@ public static void connect(Socket socket,
* @return its IP address in the string format * @return its IP address in the string format
*/ */
public static String normalizeHostName(String name) { public static String normalizeHostName(String name) {
if (Character.digit(name.charAt(0), 10) != -1) { // it is an IP try {
return InetAddress.getByName(name).getHostAddress();
} catch (UnknownHostException e) {
return name; return name;
} else { }
try {
InetAddress ipAddress = InetAddress.getByName(name);
return ipAddress.getHostAddress();
} catch (UnknownHostException e) {
return name;
}
}
} }
/** /**

View File

@ -31,7 +31,9 @@
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
import java.net.URI; import java.net.URI;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import junit.framework.AssertionFailedError; import junit.framework.AssertionFailedError;
@ -42,8 +44,6 @@
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.security.NetUtilsTestResolver; import org.apache.hadoop.security.NetUtilsTestResolver;
import org.apache.hadoop.test.MultithreadedTestUtil.TestContext;
import org.apache.hadoop.test.MultithreadedTestUtil.TestingThread;
import org.junit.Assume; import org.junit.Assume;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
@ -599,6 +599,26 @@ public void testCanonicalUriWithNoPortNoDefaultPort() {
assertEquals("scheme://host.a.b/path", uri.toString()); assertEquals("scheme://host.a.b/path", uri.toString());
} }
/**
* 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));
// this address HADOOP-8372: 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));
}
@Test @Test
public void testGetHostNameOfIP() { public void testGetHostNameOfIP() {
assertNull(NetUtils.getHostNameOfIP(null)); assertNull(NetUtils.getHostNameOfIP(null));

View File

@ -41,7 +41,7 @@ public class TestTableMapping {
public void setUp() throws IOException { public void setUp() throws IOException {
mappingFile = File.createTempFile(getClass().getSimpleName(), ".txt"); mappingFile = File.createTempFile(getClass().getSimpleName(), ".txt");
Files.write("a.b.c /rack1\n" + Files.write("a.b.c /rack1\n" +
"1.2.3\t/rack2\n", mappingFile, Charsets.UTF_8); "1.2.3.4\t/rack2\n", mappingFile, Charsets.UTF_8);
mappingFile.deleteOnExit(); mappingFile.deleteOnExit();
} }
@ -55,7 +55,7 @@ public void testResolve() throws IOException {
List<String> names = new ArrayList<String>(); List<String> names = new ArrayList<String>();
names.add("a.b.c"); names.add("a.b.c");
names.add("1.2.3"); names.add("1.2.3.4");
List<String> result = mapping.resolve(names); List<String> result = mapping.resolve(names);
assertEquals(names.size(), result.size()); assertEquals(names.size(), result.size());
@ -73,7 +73,7 @@ public void testTableCaching() throws IOException {
List<String> names = new ArrayList<String>(); List<String> names = new ArrayList<String>();
names.add("a.b.c"); names.add("a.b.c");
names.add("1.2.3"); names.add("1.2.3.4");
List<String> result1 = mapping.resolve(names); List<String> result1 = mapping.resolve(names);
assertEquals(names.size(), result1.size()); assertEquals(names.size(), result1.size());
@ -96,7 +96,7 @@ public void testNoFile() {
List<String> names = new ArrayList<String>(); List<String> names = new ArrayList<String>();
names.add("a.b.c"); names.add("a.b.c");
names.add("1.2.3"); names.add("1.2.3.4");
List<String> result = mapping.resolve(names); List<String> result = mapping.resolve(names);
assertEquals(names.size(), result.size()); assertEquals(names.size(), result.size());
@ -114,7 +114,7 @@ public void testFileDoesNotExist() {
List<String> names = new ArrayList<String>(); List<String> names = new ArrayList<String>();
names.add("a.b.c"); names.add("a.b.c");
names.add("1.2.3"); names.add("1.2.3.4");
List<String> result = mapping.resolve(names); List<String> result = mapping.resolve(names);
assertEquals(names.size(), result.size()); assertEquals(names.size(), result.size());
@ -134,7 +134,7 @@ public void testBadFile() throws IOException {
List<String> names = new ArrayList<String>(); List<String> names = new ArrayList<String>();
names.add("a.b.c"); names.add("a.b.c");
names.add("1.2.3"); names.add("1.2.3.4");
List<String> result = mapping.resolve(names); List<String> result = mapping.resolve(names);
assertEquals(names.size(), result.size()); assertEquals(names.size(), result.size());