Skip to content

Commit

Permalink
HDFS-17249. Fix TestDFSUtil.testIsValidName() unit test failure (apac…
Browse files Browse the repository at this point in the history
…he#6249)


Contributed by liuguanghua.
  • Loading branch information
LiuGuH authored and jiajunmao committed Feb 6, 2024
1 parent bfcecb5 commit 573ff7e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -661,9 +661,12 @@ public static boolean isValidName(String src) {
String[] components = StringUtils.split(src, '/');
for (int i = 0; i < components.length; i++) {
String element = components[i];
// For Windows, we must allow the : in the drive letter.
if (Shell.WINDOWS && i == 1 && element.endsWith(":")) {
continue;
}
if (element.equals(".") ||
// For Windows, we must allow the : in the drive letter.
(!Shell.WINDOWS && i == 1 && element.contains(":")) ||
(element.contains(":")) ||
(element.contains("/"))) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import org.apache.hadoop.security.alias.JavaKeyStoreProvider;
import org.apache.hadoop.test.GenericTestUtils;
import org.apache.hadoop.test.LambdaTestUtils;
import org.apache.hadoop.util.Shell;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -865,13 +866,25 @@ public void testLocalhostReverseLookup() {

@Test (timeout=15000)
public void testIsValidName() {
assertFalse(DFSUtil.isValidName("/foo/../bar"));
assertFalse(DFSUtil.isValidName("/foo/./bar"));
assertFalse(DFSUtil.isValidName("/foo//bar"));
assertTrue(DFSUtil.isValidName("/"));
assertTrue(DFSUtil.isValidName("/bar/"));
assertFalse(DFSUtil.isValidName("/foo/:/bar"));
assertFalse(DFSUtil.isValidName("/foo:bar"));
String validPaths[] = new String[]{"/", "/bar/"};
for (String path : validPaths) {
assertTrue("Should have been accepted '" + path + "'", DFSUtil.isValidName(path));
}

String invalidPaths[] =
new String[]{"/foo/../bar", "/foo/./bar", "/foo//bar", "/foo/:/bar", "/foo:bar"};
for (String path : invalidPaths) {
assertFalse("Should have been rejected '" + path + "'", DFSUtil.isValidName(path));
}

String windowsPath = "/C:/foo/bar";
if (Shell.WINDOWS) {
assertTrue("Should have been accepted '" + windowsPath + "' in windows os.",
DFSUtil.isValidName(windowsPath));
} else {
assertFalse("Should have been rejected '" + windowsPath + "' in unix os.",
DFSUtil.isValidName(windowsPath));
}
}

@Test(timeout=5000)
Expand Down

0 comments on commit 573ff7e

Please sign in to comment.