From 2d5f91ab3a3ce3477b5ae7c9fc03a1903207f2f1 Mon Sep 17 00:00:00 2001 From: hunili Date: Fri, 8 Nov 2024 10:22:04 +0800 Subject: [PATCH] add tdd for filter addtata issue: https://gitee.com/openharmony/filemanagement_app_file_service/issues/IB1X5W Signed-off-by: hunili --- .../module_ipc/svc_session_manager_test.cpp | 2 +- .../backup_utils/b_filesystem/b_dir_test.cpp | 184 ++++++++++++++++++ tests/utils/include/test_manager.h | 8 +- tests/utils/src/test_manager.cpp | 10 +- 4 files changed, 200 insertions(+), 4 deletions(-) diff --git a/tests/unittests/backup_sa/module_ipc/svc_session_manager_test.cpp b/tests/unittests/backup_sa/module_ipc/svc_session_manager_test.cpp index a0ec5ada5..3c25ef2fb 100644 --- a/tests/unittests/backup_sa/module_ipc/svc_session_manager_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/svc_session_manager_test.cpp @@ -863,7 +863,7 @@ HWTEST_F(SvcSessionManagerTest, SUB_backup_sa_session_GetSchedBundleName_0100, t sessionManagerPtr_->SetIsReadyLaunch(BUNDLE_NAME); bool condition = sessionManagerPtr_->GetSchedBundleName(bundleName); EXPECT_EQ(bundleName, BUNDLE_NAME); - EXPECT_FALSE(condition); + EXPECT_TRUE(condition); GTEST_LOG_(INFO) << "SvcSessionManagerTest-GetSchedBundleName Branches"; sessionManagerPtr_->SetServiceSchedAction(BUNDLE_NAME, BConstants::ServiceSchedAction::START); condition = sessionManagerPtr_->GetSchedBundleName(bundleName); diff --git a/tests/unittests/backup_utils/b_filesystem/b_dir_test.cpp b/tests/unittests/backup_utils/b_filesystem/b_dir_test.cpp index 42f8cde99..793168564 100644 --- a/tests/unittests/backup_utils/b_filesystem/b_dir_test.cpp +++ b/tests/unittests/backup_utils/b_filesystem/b_dir_test.cpp @@ -343,4 +343,188 @@ HWTEST_F(BDirTest, b_dir_GetFile_0100, testing::ext::TestSize.Level1) } GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetFile_0100"; } + +/** + * @tc.number: SUB_backup_b_dir_ExpandPathWildcard_0100 + * @tc.name: b_dir_ExpandPathWildcard_0100 + * @tc.desc: Test function of ExpandPathWildcard interface for SUCCESS + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(BDirTest, b_dir_ExpandPathWildcard_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "BDirTest-begin b_dir_ExpandPathWildcard_0100"; + try { + TestManager tmCurrentUser("", MakeDirType::CURRENTUSER); + std::string dirCurrentUser = tmCurrentUser.GetRootDirCurTest(); + std::string cmdMkdirPre = std::string("mkdir -p ") + dirCurrentUser; + std::string dirAppData = "appdata/test"; + std::string dirHaps = "haps/test"; + std::string cmdMkdir = cmdMkdirPre + dirAppData; + system(cmdMkdir.c_str()); + cmdMkdir = cmdMkdirPre + dirHaps; + system(cmdMkdir.c_str()); + std::string cmdTouchFile = std::string("touch ") + dirCurrentUser + dirAppData + FILE_SEPARATOR_CHAR + "1.txt"; + system(cmdTouchFile.c_str()); + cmdTouchFile = string("touch ") + dirCurrentUser + "2.txt"; + system(cmdTouchFile.c_str()); + + std::vector include = { dirCurrentUser }; + std::set res = ExpandPathWildcard(include, true); + EXPECT_EQ(res.size(), 2); // 2: valid path number + EXPECT_EQ(res.count(dirCurrentUser), 0); + + std::string testDir = dirCurrentUser + "appdata"; + include = { testDir }; + res = ExpandPathWildcard(include, true); + EXPECT_EQ(res.size(), 0); + + testDir = dirCurrentUser + "*.txt"; + include = { testDir }; + + res = ExpandPathWildcard(include, true); + EXPECT_EQ(res.size(), 1); // 1: dirCurrentUser + "2.txt" + } catch (...) { + GTEST_LOG_(INFO) << "BDirTest-an ExpandPathWildcard_0100 exception occurred."; + EXPECT_TRUE(false); + } + GTEST_LOG_(INFO) << "BDirTest-end b_dir_ExpandPathWildcard_0100"; +} + +/** + * @tc.number: SUB_backup_b_dir_ExpandPathWildcard_0200 + * @tc.name: b_dir_ExpandPathWildcard_0200 + * @tc.desc: Test function of ExpandPathWildcard interface for SUCCESS + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(BDirTest, b_dir_ExpandPathWildcard_0200, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "BDirTest-begin b_dir_ExpandPathWildcard_0200"; + try { + TestManager tmDefault("b_dir_ExpandPathWildcard_0200"); + std::string dirDefault = tmDefault.GetRootDirCurTest(); + std::string cmdMkdirPre = std::string("mkdir -p ") + dirDefault; + std::string dirAppData = "appdata/test"; + std::string dirHaps = "haps/test"; + std::string cmdMkdir = cmdMkdirPre + dirAppData; + system(cmdMkdir.c_str()); + cmdMkdir = cmdMkdirPre + dirHaps; + system(cmdMkdir.c_str()); + std::string cmdTouchFile = std::string("touch ") + dirDefault + dirAppData + FILE_SEPARATOR_CHAR + "1.txt"; + system(cmdTouchFile.c_str()); + cmdTouchFile = string("touch ") + dirDefault + "2.txt"; + system(cmdTouchFile.c_str()); + + std::vector include = { dirDefault }; + std::set res = ExpandPathWildcard(include, true); + EXPECT_EQ(res.size(), 1); // 1: dirDefault + + std::string testDir = dirDefault + "*.txt"; + include = { testDir }; + res = ExpandPathWildcard(include, true); + EXPECT_EQ(res.size(), 1); // 1: dirCurrentUser + "2.txt" + } catch (...) { + GTEST_LOG_(INFO) << "BDirTest-an ExpandPathWildcard_0200 exception occurred."; + EXPECT_TRUE(false); + } + GTEST_LOG_(INFO) << "BDirTest-end b_dir_ExpandPathWildcard_0200"; +} + +/** + * @tc.number: SUB_backup_b_dir_RmForceExcludePath_0100 + * @tc.name: b_dir_RmForceExcludePath_0100 + * @tc.desc: Test function of RmForceExcludePath interface for SUCCESS + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(BDirTest, b_dir_RmForceExcludePath_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "BDirTest-begin b_dir_RmForceExcludePath_0100"; + try { + TestManager tmCurrentUser("", MakeDirType::CURRENTUSER); + std::string dirCurrentUser = tmCurrentUser.GetRootDirCurTest(); + std::string cmdMkdirPre = std::string("mkdir -p ") + dirCurrentUser; + std::string dirAppData = "appdata/test"; + std::string dirHaps = "haps/test"; + std::string cmdMkdir = cmdMkdirPre + dirAppData; + system(cmdMkdir.c_str()); + cmdMkdir = cmdMkdirPre + dirHaps; + system(cmdMkdir.c_str()); + std::string cmdTouchFile = std::string("touch ") + dirCurrentUser + dirAppData + FILE_SEPARATOR_CHAR + "1.txt"; + system(cmdTouchFile.c_str()); + cmdTouchFile = string("touch ") + dirCurrentUser + "2.txt"; + system(cmdTouchFile.c_str()); + + std::set testPath = { + dirCurrentUser + }; + RmForceExcludePath(testPath); + EXPECT_EQ(testPath.size(), 2); // 2: valid path number + + testPath = { + dirCurrentUser + "appdata/" + }; + RmForceExcludePath(testPath); + EXPECT_EQ(testPath.size(), 0); + + testPath = { + dirCurrentUser + "haps" + }; + RmForceExcludePath(testPath); + EXPECT_EQ(testPath.size(), 1); // 1: dirCurrentUser + "haps" + } catch (...) { + GTEST_LOG_(INFO) << "BDirTest-an RmForceExcludePath exception occurred."; + EXPECT_TRUE(false); + } + GTEST_LOG_(INFO) << "BDirTest-end b_dir_RmForceExcludePath_0100"; +} + +/** + * @tc.number: SUB_backup_b_dir_GetSubDir_0100 + * @tc.name: b_dir_GetSubDir_0100 + * @tc.desc: Test function of GetSubDir interface for SUCCESS + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(BDirTest, b_dir_GetSubDir_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetSubDir_0100"; + try { + TestManager tmCurrentUser("", MakeDirType::CURRENTUSER); + std::string dirCurrentUser = tmCurrentUser.GetRootDirCurTest(); + std::string cmdMkdirPre = std::string("mkdir -p ") + dirCurrentUser; + std::string dirAppData = "appdata/test"; + std::string dirHaps = "haps/test"; + std::string cmdMkdir = cmdMkdirPre + dirAppData; + system(cmdMkdir.c_str()); + cmdMkdir = cmdMkdirPre + dirHaps; + system(cmdMkdir.c_str()); + std::string cmdTouchFile = std::string("touch ") + dirCurrentUser + dirAppData + FILE_SEPARATOR_CHAR + "1.txt"; + system(cmdTouchFile.c_str()); + cmdTouchFile = string("touch ") + dirCurrentUser + "2.txt"; + system(cmdTouchFile.c_str()); + + std::set result = GetSubDir(""); + EXPECT_EQ(result.size(), 0); + + result = GetSubDir("test"); + EXPECT_EQ(result.size(), 0); + + result = GetSubDir(dirCurrentUser); + EXPECT_EQ(result.size(), 2); // 2: valid path number + } catch (...) { + GTEST_LOG_(INFO) << "BDirTest-an GetSubDir exception occurred."; + EXPECT_TRUE(false); + } + GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetSubDir_0100"; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/utils/include/test_manager.h b/tests/utils/include/test_manager.h index 034658ba5..d24f5dd9c 100644 --- a/tests/utils/include/test_manager.h +++ b/tests/utils/include/test_manager.h @@ -19,9 +19,15 @@ #include namespace OHOS::FileManagement::Backup { +enum class MakeDirType { + DEFAULT, + CURRENTUSER +}; +const std::string PATH_CURRENT_USER = "/storage/Users/currentUser/"; +const char FILE_SEPARATOR_CHAR = '/'; class TestManager { public: - explicit TestManager(std::string functionName); + explicit TestManager(std::string functionName, MakeDirType dirType = MakeDirType::DEFAULT); ~TestManager(); /** diff --git a/tests/utils/src/test_manager.cpp b/tests/utils/src/test_manager.cpp index 7bf577ec4..125581cf7 100644 --- a/tests/utils/src/test_manager.cpp +++ b/tests/utils/src/test_manager.cpp @@ -21,9 +21,15 @@ namespace OHOS::FileManagement::Backup { using namespace std; -TestManager::TestManager(std::string functionName) +TestManager::TestManager(std::string functionName, MakeDirType dirType) { - rootDirCurTest_ = "/data/test/backup/" + functionName + "/"; + switch (dirType) { + case MakeDirType::CURRENTUSER : + rootDirCurTest_ = PATH_CURRENT_USER; + break; + default : + rootDirCurTest_ = "/data/test/backup/" + functionName + "/"; + } // REM:先删后创建 if (bool created = ForceCreateDirectory(rootDirCurTest_); !created) { throw std::system_error(errno, std::system_category()); -- Gitee