Skip to content

Commit 342e6ca

Browse files
authored
HDFS-17249. Fix TestDFSUtil.testIsValidName() unit test failure (#6249)
Contributed by liuguanghua.
1 parent a32097a commit 342e6ca

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSUtilClient.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,9 +661,12 @@ public static boolean isValidName(String src) {
661661
String[] components = StringUtils.split(src, '/');
662662
for (int i = 0; i < components.length; i++) {
663663
String element = components[i];
664+
// For Windows, we must allow the : in the drive letter.
665+
if (Shell.WINDOWS && i == 1 && element.endsWith(":")) {
666+
continue;
667+
}
664668
if (element.equals(".") ||
665-
// For Windows, we must allow the : in the drive letter.
666-
(!Shell.WINDOWS && i == 1 && element.contains(":")) ||
669+
(element.contains(":")) ||
667670
(element.contains("/"))) {
668671
return false;
669672
}

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSUtil.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
import org.apache.hadoop.security.alias.JavaKeyStoreProvider;
8484
import org.apache.hadoop.test.GenericTestUtils;
8585
import org.apache.hadoop.test.LambdaTestUtils;
86+
import org.apache.hadoop.util.Shell;
8687
import org.junit.Assert;
8788
import org.junit.Before;
8889
import org.junit.Test;
@@ -865,13 +866,25 @@ public void testLocalhostReverseLookup() {
865866

866867
@Test (timeout=15000)
867868
public void testIsValidName() {
868-
assertFalse(DFSUtil.isValidName("/foo/../bar"));
869-
assertFalse(DFSUtil.isValidName("/foo/./bar"));
870-
assertFalse(DFSUtil.isValidName("/foo//bar"));
871-
assertTrue(DFSUtil.isValidName("/"));
872-
assertTrue(DFSUtil.isValidName("/bar/"));
873-
assertFalse(DFSUtil.isValidName("/foo/:/bar"));
874-
assertFalse(DFSUtil.isValidName("/foo:bar"));
869+
String validPaths[] = new String[]{"/", "/bar/"};
870+
for (String path : validPaths) {
871+
assertTrue("Should have been accepted '" + path + "'", DFSUtil.isValidName(path));
872+
}
873+
874+
String invalidPaths[] =
875+
new String[]{"/foo/../bar", "/foo/./bar", "/foo//bar", "/foo/:/bar", "/foo:bar"};
876+
for (String path : invalidPaths) {
877+
assertFalse("Should have been rejected '" + path + "'", DFSUtil.isValidName(path));
878+
}
879+
880+
String windowsPath = "/C:/foo/bar";
881+
if (Shell.WINDOWS) {
882+
assertTrue("Should have been accepted '" + windowsPath + "' in windows os.",
883+
DFSUtil.isValidName(windowsPath));
884+
} else {
885+
assertFalse("Should have been rejected '" + windowsPath + "' in unix os.",
886+
DFSUtil.isValidName(windowsPath));
887+
}
875888
}
876889

877890
@Test(timeout=5000)

0 commit comments

Comments
 (0)