Skip to content

Commit

Permalink
fix: fast_transfer_file API 分发文件,如果源文件中的文件名包含空格,会报错 #812
Browse files Browse the repository at this point in the history
  • Loading branch information
liuliaozhong committed May 17, 2023
1 parent f8e422b commit ba7323a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public static boolean validateFileSystemAbsolutePath(String path) {
if (!result) {
// 路径不合法
log.warn("The path {} is invalid and the verification fails", path);
// 路径校验失败输出日志,验证阶段不拦截返回true
result = true;
}
return result;
}
Expand All @@ -71,10 +73,11 @@ private static boolean isLinuxAbsolutePath(String path) {

/**
* 1 传统DOS路径
* 标准的DOS路径可由以下三部分组成:
* 1)卷号或驱动器号,后跟卷分隔符(:)。
* 2)目录名称。目录分隔符用来分隔嵌套目录层次结构中的子目录。
* 3)文件名。目录分隔符用来分隔文件路径和文件名。
* 标准的DOS路径可由以下三部分组成:
* 1)卷号或驱动器号,后跟卷分隔符(:)。
* 2)目录名称。目录分隔符用来分隔嵌套目录层次结构中的子目录。
* 3)文件名。目录分隔符用来分隔文件路径和文件名。
*
* @param path
* @return boolean
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,64 +1,60 @@
package com.tencent.bk.job.common.util;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

public class FilePathValidateUtilTest {
@Test
void testWindowsFilePath(){
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\Documents\\abc.txt")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("c:\\Documents\\abc.txt")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\Documents\\abc.txt")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath(":\\abc.txt")).isFalse();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:")).isFalse();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\\\")).isFalse();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\logs\\..\\access.log")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\.config\\conf")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\user\\abc>a")).isFalse();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\user\\abc:a")).isFalse();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\user\\abc|a")).isFalse();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\user\\abc?a")).isFalse();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\user\\abc<a")).isFalse();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\logs/logs")).isFalse();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\logs\\log*.log")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\tmp\\REGEX:myfile-[A-Za-z]{0,10}.tar.gz")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\tmp\\REGE:a|b")).isFalse();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\tmp\\REGEX:a|b")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\tmp\\REGEX:^[a-zA-Z0-9]")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("[FILESRCIP]\\logs")).isFalse();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\[FILESRCIP]\\logs")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\[DATE:yyyy-MM-dd]\\logs")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("${path}")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("${!#path}")).isFalse();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("${_path}")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("${path}\\test.txt")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\tmp\\${path}")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\${date}\\${path}")).isTrue();
}

@Test
void testLinuxFilePath(){
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/data/test_2022 04 12.apk")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/tmp/")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/tmp/.conf/abc")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/tmp/test/../test.log")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("data/test_2022-04-12.apk")).isFalse();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("///")).isTrue(); // 根目录
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/tmp////")).isTrue(); // /tmp/
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/tmp//test/")).isTrue();// /tmp/test/
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/logs/log*.log")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/tmp/REGEX:myfile-[A-Za-z]{0,10}.tar.gz")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/tmp/REGEX:aa|bb")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("[FILESRCIP]/logs")).isFalse();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/[FILESRCIP]/logs")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/[DATE:yyyy-MM-dd]/logs")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("${path}/test.txt")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("${path}/../test.txt")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/[FILESRCIP]/${path}")).isTrue();
assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/tmp/${path}")).isTrue();
}

// 验证阶段,路径只判断是否非空,测试用例先注释了
// @Test
// void testWindowsFilePath(){
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\Documents\\abc.txt")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("c:\\Documents\\abc.txt")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\Documents\\嘉 abc.txt")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath(":\\abc.txt")).isFalse();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:")).isFalse();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\\\")).isFalse();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\logs\\..\\access.log")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\.config\\conf")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\user\\abc>a")).isFalse();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\user\\abc:a")).isFalse();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\user\\abc|a")).isFalse();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\user\\abc?a")).isFalse();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\user\\abc<a")).isFalse();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\logs/logs")).isFalse();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\logs\\log*.log")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\tmp\\REGEX:myfile-[A-Za-z]{0,10}.tar.gz")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\tmp\\REGE:a|b")).isFalse();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\tmp\\REGEX:a|b")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\tmp\\REGEX:^[a-zA-Z0-9]")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("[FILESRCIP]\\logs")).isFalse();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\[FILESRCIP]\\logs")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\[DATE:yyyy-MM-dd]\\logs")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("${path}")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("${!#path}")).isFalse();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("${_path}")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("${path}\\test.txt")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\tmp\\${path}")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("C:\\${date}\\${path}")).isTrue();
// }
//
// @Test
// void testLinuxFilePath(){
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/data/test_2022 04 12.apk")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/tmp/")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/tmp/.conf/abc")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/tmp/test/../test.log")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("data/test_2022-04-12.apk")).isFalse();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("///")).isTrue(); // 根目录
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/tmp////")).isTrue(); // /tmp/
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/tmp//test/")).isTrue();// /tmp/test/
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/logs/log*.log")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/tmp/REGEX:myfile-[A-Za-z]{0,10}.tar.gz")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/tmp/REGEX:aa|bb")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("[FILESRCIP]/logs")).isFalse();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/[FILESRCIP]/logs")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/[DATE:yyyy-MM-dd]/logs")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("${path}/test.txt")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("${path}/../test.txt")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/[FILESRCIP]/${path}")).isTrue();
// assertThat(FilePathValidateUtil.validateFileSystemAbsolutePath("/tmp/${path}")).isTrue();
// }
}

0 comments on commit ba7323a

Please sign in to comment.