This is an automated email from the ASF dual-hosted git repository.

stevel pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/hadoop.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 342e6caba189 HDFS-17249. Fix TestDFSUtil.testIsValidName() unit test 
failure (#6249)
342e6caba189 is described below

commit 342e6caba189a5639a26ff3f27ac6af7ef5bd881
Author: LiuGuH <444506...@qq.com>
AuthorDate: Tue Nov 14 02:16:31 2023 +0800

    HDFS-17249. Fix TestDFSUtil.testIsValidName() unit test failure (#6249)
    
    
    Contributed by liuguanghua.
---
 .../java/org/apache/hadoop/hdfs/DFSUtilClient.java |  7 ++++--
 .../java/org/apache/hadoop/hdfs/TestDFSUtil.java   | 27 ++++++++++++++++------
 2 files changed, 25 insertions(+), 9 deletions(-)

diff --git 
a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSUtilClient.java
 
b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSUtilClient.java
index 71cff2e3915b..b2fc472aad83 100644
--- 
a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSUtilClient.java
+++ 
b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSUtilClient.java
@@ -661,9 +661,12 @@ public class DFSUtilClient {
     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;
       }
diff --git 
a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSUtil.java
 
b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSUtil.java
index f8e8e4120c43..5d7110d3d9a8 100644
--- 
a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSUtil.java
+++ 
b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSUtil.java
@@ -83,6 +83,7 @@ import 
org.apache.hadoop.security.alias.CredentialProviderFactory;
 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;
@@ -865,13 +866,25 @@ public class TestDFSUtil {
 
   @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)


---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscr...@hadoop.apache.org
For additional commands, e-mail: common-commits-h...@hadoop.apache.org

Reply via email to