Parcourir la source

HDFS-2358. NPE when the default filesystem's uri has no authority. Contributed by Daryn Sharp.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-0.20-security-205@1179472 13f79535-47bb-0310-9956-ffa450edef68
Matthew Foley il y a 14 ans
Parent
commit
723d9d7ce6

+ 3 - 0
CHANGES.txt

@@ -56,6 +56,9 @@ Release 0.20.205.0 - 2011.09.28
 
   BUG FIXES
 
+    HDFS-2358. NPE when the default filesystem's uri has no authority.
+    (Daryn Sharp via mattf)
+
     MAPREDUCE-3112. Calling hadoop cli inside mapreduce job leads to errors.
     (Eric Yang via mattf)
 

+ 1 - 1
src/hdfs/org/apache/hadoop/hdfs/server/namenode/NameNode.java

@@ -223,7 +223,7 @@ public class NameNode implements ClientProtocol, DatanodeProtocol,
   }
 
   public static InetSocketAddress getAddress(Configuration conf) {
-    return getAddress(FileSystem.getDefaultUri(conf).getAuthority());
+    return getAddress(FileSystem.getDefaultUri(conf).toString());
   }
 
   public static URI getUri(InetSocketAddress namenode) {

+ 38 - 3
src/test/org/apache/hadoop/hdfs/TestDefaultNameNodePort.java

@@ -17,7 +17,9 @@
  */
 package org.apache.hadoop.hdfs;
 
-import junit.framework.TestCase;
+import org.junit.*;
+import static org.junit.Assert.*;
+
 import java.io.*;
 import java.net.*;
 import java.util.*;
@@ -27,8 +29,8 @@ import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.hdfs.server.namenode.NameNode;
 
 /** Test NameNode port defaulting code. */
-public class TestDefaultNameNodePort extends TestCase {
-
+public class TestDefaultNameNodePort {
+  @Test
   public void testGetAddressFromString() throws Exception {
     assertEquals(NameNode.getAddress("foo").getPort(),
                  NameNode.DEFAULT_PORT);
@@ -40,6 +42,7 @@ public class TestDefaultNameNodePort extends TestCase {
                  555);
   }
 
+  @Test
   public void testGetAddressFromConf() throws Exception {
     Configuration conf = new Configuration();
     FileSystem.setDefaultUri(conf, "hdfs://foo/");
@@ -57,4 +60,36 @@ public class TestDefaultNameNodePort extends TestCase {
                                                        NameNode.DEFAULT_PORT)),
                  URI.create("hdfs://foo"));
   }
+
+  @Test
+  public void testSlashAddress() throws Exception {
+    verifyBadAuthAddress("/junk");
+  } 
+
+  @Test
+  public void testSlashSlashAddress() throws Exception {
+    verifyBadAuthAddress("//junk");
+  } 
+
+  @Test
+  public void testNoAuthAddress() throws Exception {
+    // this is actually the default value if the default fs isn't configured!
+    verifyBadAuthAddress("file:///");
+  } 
+
+  public void verifyBadAuthAddress(String noAuth) throws Exception {
+    Configuration conf = new Configuration();
+    FileSystem.setDefaultUri(conf, noAuth);
+    try {
+      InetSocketAddress addr = NameNode.getAddress(conf);
+      // this will show what we got back in case the test fails
+      assertEquals(null, addr);
+    } catch (IllegalArgumentException e) {
+      assertEquals(
+          "Does not contain a valid host:port authority: " + noAuth,
+          e.getMessage());
+    }
+  } 
+
+
 }