1 /* 2 * Copyright (c) 2022-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <cstdio> 17 #include <cstdlib> 18 19 #include <dirent.h> 20 #include <fcntl.h> 21 22 #include <errors.h> 23 #include <file_ex.h> 24 #include <gtest/gtest.h> 25 #include <memory> 26 27 #include "b_filesystem/b_dir.h" 28 #include "b_dir.cpp" 29 #include "b_process/b_process.h" 30 #include "test_manager.h" 31 32 namespace OHOS::FileManagement::Backup { 33 using namespace std; 34 35 class BDirTest : public testing::Test { 36 public: SetUpTestCase(void)37 static void SetUpTestCase(void) {}; TearDownTestCase()38 static void TearDownTestCase() {}; SetUp()39 void SetUp() {}; TearDown()40 void TearDown() {}; 41 }; 42 43 /** 44 * @tc.number: SUB_backup_b_dir_GetDirFiles_0100 45 * @tc.name: b_dir_GetDirFiles_0100 46 * @tc.desc: Test function of GetDirFiles interface for SUCCESS. 47 * @tc.size: MEDIUM 48 * @tc.type: FUNC 49 * @tc.level Level 0 50 * @tc.require: I6F3GV 51 */ 52 HWTEST_F(BDirTest, b_dir_GetDirFiles_0100, testing::ext::TestSize.Level1) 53 { 54 GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetDirFiles_0100"; 55 try { 56 TestManager tm("b_dir_GetDirFiles_0100"); 57 58 string preparedDir = tm.GetRootDirCurTest(); 59 string touchFilePrefix = string("touch ") + preparedDir; 60 system(touchFilePrefix.append("a.txt").c_str()); 61 system(touchFilePrefix.append("b.txt").c_str()); 62 system(touchFilePrefix.append("c.txt").c_str()); 63 64 vector<string> out; 65 bool bSucc; 66 tie(bSucc, out) = BDir::GetDirFiles(preparedDir); 67 68 vector<string> expectedRes = {preparedDir.append("a.txt"), preparedDir.append("b.txt"), 69 preparedDir.append("c.txt")}; 70 EXPECT_EQ(out, expectedRes); 71 72 tie(bSucc, out) = BDir::GetDirFiles("dev"); 73 EXPECT_EQ(bSucc, true); 74 } catch (...) { 75 EXPECT_TRUE(false); 76 GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; 77 } 78 GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetDirFiles_0100"; 79 } 80 81 /** 82 * @tc.number: SUB_backup_b_dir_GetDirFiles_0104 83 * @tc.name: b_dir_GetDirFiles_0104 84 * @tc.desc: Test function of GetDirFiles interface for SUCCESS. 85 * @tc.size: MEDIUM 86 * @tc.type: FUNC 87 * @tc.level Level 0 88 * @tc.require: I6F3GV 89 */ 90 HWTEST_F(BDirTest, b_dir_GetDirFiles_0104, testing::ext::TestSize.Level1) 91 { 92 GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetDirFiles_0104"; 93 try { 94 TestManager tm("b_dir_GetDirFiles_0104"); 95 96 string preparedDir = "/data/app/"; 97 string touchFilePrefix = string("touch ") + preparedDir; 98 system(touchFilePrefix.append("d.txt").c_str()); 99 system(touchFilePrefix.append("e.txt").c_str()); 100 system(touchFilePrefix.append("f.txt").c_str()); 101 102 bool bSucc; 103 vector<string> out; 104 tie(bSucc, out) = BDir::GetDirFiles(preparedDir); 105 106 vector<string> expectedRes = {preparedDir.append("d.txt"), preparedDir.append("e.txt"), 107 preparedDir.append("f.txt")}; 108 EXPECT_EQ(out, expectedRes); 109 110 tie(bSucc, out) = BDir::GetDirFiles("dev"); 111 EXPECT_EQ(bSucc, true); 112 } catch (...) { 113 EXPECT_TRUE(false); 114 GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; 115 } 116 GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetDirFiles_0104"; 117 } 118 119 120 /** 121 * @tc.number: SUB_backup_b_dir_GetBigFiles_0100 122 * @tc.name: b_dir_GetBigFiles_0100 123 * @tc.desc: 测试GetBigFiles接口是否能成功获取大文件 124 * @tc.size: MEDIUM 125 * @tc.type: FUNC 126 * @tc.level Level 1 127 * @tc.require: I6F3GV 128 */ 129 HWTEST_F(BDirTest, b_dir_GetBigFiles_0100, testing::ext::TestSize.Level1) 130 { 131 GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetBigFiles_0100"; 132 try { 133 TestManager tm("b_dir_GetBigFiles_0100"); 134 string rootDir = tm.GetRootDirCurTest(); 135 string filePath1 = rootDir + "a.txt"; 136 string filePath2 = rootDir + "b.txt"; 137 // 文件大小大于2M的文件属于大文件,因此这里创建大小为3MB文件和4MB的文件 138 auto [bFatalErr, ret] = 139 BProcess::ExecuteCmd({"dd", "if=/dev/urandom", ("of=" + filePath1).c_str(), "bs=1M", "count=3"}); 140 EXPECT_FALSE(bFatalErr); 141 EXPECT_EQ(ret, 0); 142 tie(bFatalErr, ret) = 143 BProcess::ExecuteCmd({"dd", "if=/dev/urandom", ("of=" + filePath2).c_str(), "bs=1M", "count=4"}); 144 EXPECT_FALSE(bFatalErr); 145 EXPECT_EQ(ret, 0); 146 vector<string> includes = {rootDir}; 147 vector<string> excludes = {filePath2}; 148 auto [errCode, mpNameToStat, smallFiles] = BDir::GetBigFiles(includes, excludes); 149 EXPECT_EQ(errCode, ERR_OK); 150 EXPECT_EQ(mpNameToStat.at(filePath1).st_size, 1024 * 1024 * 3); 151 EXPECT_EQ(mpNameToStat.find(filePath2), mpNameToStat.end()); 152 } catch (...) { 153 EXPECT_TRUE(false); 154 GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; 155 } 156 GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetBigFiles_0100"; 157 } 158 159 /** 160 * @tc.number: SUB_backup_b_dir_GetBigFiles_0200 161 * @tc.name: b_dir_GetBigFiles_0200 162 * @tc.desc: 测试GetBigFiles接口 分支逻辑 163 * @tc.size: MEDIUM 164 * @tc.type: FUNC 165 * @tc.level Level 1 166 * @tc.require: I6F3GV 167 */ 168 HWTEST_F(BDirTest, b_dir_GetBigFiles_0200, testing::ext::TestSize.Level1) 169 { 170 GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetBigFiles_0200"; 171 try { 172 vector<string> includes = {{}, {}}; 173 vector<string> excludes = {{}}; 174 auto [errCode, mpNameToStat, smallFiles] = BDir::GetBigFiles(includes, excludes); 175 EXPECT_EQ(errCode, ERR_OK); 176 } catch (...) { 177 EXPECT_TRUE(false); 178 GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; 179 } 180 GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetBigFiles_0200"; 181 } 182 183 /** 184 * @tc.number: SUB_backup_b_dir_GetBigFiles_0201 185 * @tc.name: b_dir_GetBigFiles_0201 186 * @tc.desc: 测试GetBigFiles接口 分支逻辑 187 * @tc.size: MEDIUM 188 * @tc.type: FUNC 189 * @tc.level Level 1 190 * @tc.require: I6F3GV 191 */ 192 HWTEST_F(BDirTest, b_dir_GetBigFiles_0201, testing::ext::TestSize.Level1) 193 { 194 GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetBigFiles_0201"; 195 try { 196 vector<string> includes = {"/data/"}; 197 vector<string> excludes; 198 auto [errCode, mpNameToStat, smallFiles] = BDir::GetBigFiles(includes, excludes); 199 EXPECT_EQ(errCode, ERR_OK); 200 } catch (...) { 201 EXPECT_TRUE(false); 202 GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; 203 } 204 GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetBigFiles_0201"; 205 } 206 207 /** 208 * @tc.number: SUB_backup_b_dir_GetBigFiles_0202 209 * @tc.name: b_dir_GetBigFiles_0202 210 * @tc.desc: 测试GetBigFiles接口 分支逻辑 211 * @tc.size: MEDIUM 212 * @tc.type: FUNC 213 * @tc.level Level 1 214 * @tc.require: I6F3GV 215 */ 216 HWTEST_F(BDirTest, b_dir_GetBigFiles_0202, testing::ext::TestSize.Level1) 217 { 218 GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetBigFiles_0202"; 219 try { 220 vector<string> includes = {"/data/app/"}; 221 vector<string> excludes; 222 auto [errCode, mpNameToStat, smallFiles] = BDir::GetBigFiles(includes, excludes); 223 EXPECT_EQ(errCode, ERR_OK); 224 } catch (...) { 225 EXPECT_TRUE(false); 226 GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; 227 } 228 GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetBigFiles_0202"; 229 } 230 231 /** 232 * @tc.number: SUB_backup_b_dir_GetBigFiles_0203 233 * @tc.name: b_dir_GetBigFiles_0203 234 * @tc.desc: 测试GetBigFiles接口 分支逻辑 235 * @tc.size: MEDIUM 236 * @tc.type: FUNC 237 * @tc.level Level 1 238 * @tc.require: I6F3GV 239 */ 240 HWTEST_F(BDirTest, b_dir_GetBigFiles_0203, testing::ext::TestSize.Level1) 241 { 242 GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetBigFiles_0203"; 243 try { 244 vector<string> includes; 245 vector<string> excludes; 246 const string str = ""; 247 auto [errCode, mpNameToStat, smallFiles] = BDir::GetBigFiles(includes, excludes); 248 EXPECT_EQ(errCode, ERR_OK); 249 } catch (...) { 250 EXPECT_TRUE(false); 251 GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; 252 } 253 GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetBigFiles_0203"; 254 } 255 256 /** 257 * @tc.number: SUB_backup_b_dir_GetBigFiles_0300 258 * @tc.name: b_dir_GetBigFiles_0300 259 * @tc.desc: 测试GetBigFiles接口 分支逻辑 260 * @tc.size: MEDIUM 261 * @tc.type: FUNC 262 * @tc.level Level 1 263 * @tc.require: I6F3GV 264 */ 265 HWTEST_F(BDirTest, b_dir_GetBigFiles_0300, testing::ext::TestSize.Level1) 266 { 267 GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetBigFiles_0300"; 268 try { 269 TestManager tm("b_dir_GetBigFiles_0300"); 270 string preparedDir = tm.GetRootDirCurTest(); 271 string cmdMkdir = string("mkdir -p ") + preparedDir + string("test/test1/test2"); 272 system(cmdMkdir.c_str()); 273 string touchFilePrefix = string("touch ") + preparedDir; 274 system(touchFilePrefix.append("a.txt").c_str()); 275 system(touchFilePrefix.append("b.txt").c_str()); 276 system(touchFilePrefix.append("c.txt").c_str()); 277 278 touchFilePrefix = string("touch ") + preparedDir + string("test/"); 279 system(touchFilePrefix.append("a.txt").c_str()); 280 system(touchFilePrefix.append("b.txt").c_str()); 281 system(touchFilePrefix.append("c.txt").c_str()); 282 touchFilePrefix = string("touch ") + preparedDir + string("test/test1/test2"); 283 system(touchFilePrefix.append("a.txt").c_str()); 284 system(touchFilePrefix.append("b.txt").c_str()); 285 system(touchFilePrefix.append("c.txt").c_str()); 286 vector<string> includes = {preparedDir + string("/*"), preparedDir + string("test")}; 287 vector<string> excludes = {preparedDir + string("/test/test1/test2"), {}}; 288 auto [errCode, mpNameToStat, smallFiles] = BDir::GetBigFiles(includes, excludes); 289 EXPECT_EQ(errCode, ERR_OK); 290 } catch (...) { 291 EXPECT_TRUE(false); 292 GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; 293 } 294 GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetBigFiles_0300"; 295 } 296 297 /** 298 * @tc.number: SUB_backup_b_dir_GetDirs_0100 299 * @tc.name: b_dir_GetDirs_0100 300 * @tc.desc: Test function of GetDirs interface for SUCCESS 301 * @tc.size: MEDIUM 302 * @tc.type: FUNC 303 * @tc.level Level 1 304 * @tc.require: I6F3GV 305 */ 306 HWTEST_F(BDirTest, b_dir_GetDirs_0100, testing::ext::TestSize.Level1) 307 { 308 GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetDirs_0100"; 309 try { 310 TestManager tm("b_dir_GetDirs_0100"); 311 vector<string_view> paths; 312 vector<string> res = BDir::GetDirs(paths); 313 set<string> inc(paths.begin(), paths.end()); 314 bool result = equal(inc.begin(), inc.end(), res.begin(), res.end()); 315 EXPECT_EQ(1, result); 316 EXPECT_EQ(inc.size(), res.size()); 317 } catch (...) { 318 GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; 319 } 320 GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetDirs_0100"; 321 } 322 323 /** 324 * @tc.number: SUB_backup_b_dir_IsFilePathValid_0100 325 * @tc.name: b_dir_IsFilePathValid_0100 326 * @tc.desc: Test function of IsFilePathValid interface for SUCCESS 327 * @tc.size: MEDIUM 328 * @tc.type: FUNC 329 * @tc.level Level 1 330 * @tc.require: I6F3GV 331 */ 332 HWTEST_F(BDirTest, b_dir_IsFilePathValid_0100, testing::ext::TestSize.Level1) 333 { 334 GTEST_LOG_(INFO) << "BDirTest-begin b_dir_IsFilePathValid_0100"; 335 try { 336 TestManager tm("b_dir_IsFilePathValid_0100"); 337 bool result = BDir::IsFilePathValid("../test../test1"); 338 EXPECT_FALSE(result); 339 result = BDir::IsFilePathValid("/../test../test1"); 340 EXPECT_FALSE(result); 341 result = BDir::IsFilePathValid("test../../test"); 342 EXPECT_FALSE(result); 343 result = BDir::IsFilePathValid("test../../"); 344 EXPECT_FALSE(result); 345 result = BDir::IsFilePathValid("test../test../.."); 346 EXPECT_FALSE(result); 347 result = BDir::IsFilePathValid("/test/..test/.."); 348 EXPECT_FALSE(result); 349 350 result = BDir::IsFilePathValid("test"); 351 EXPECT_TRUE(result); 352 result = BDir::IsFilePathValid("/test/test../test"); 353 EXPECT_TRUE(result); 354 result = BDir::IsFilePathValid("/test../test../test"); 355 EXPECT_TRUE(result); 356 result = BDir::IsFilePathValid("/test../test../test../"); 357 EXPECT_TRUE(result); 358 result = BDir::IsFilePathValid("/test../test../test../..test"); 359 EXPECT_TRUE(result); 360 } catch (...) { 361 GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; 362 } 363 GTEST_LOG_(INFO) << "BDirTest-end b_dir_IsFilePathValid_0100"; 364 } 365 366 /** 367 * @tc.number: SUB_backup_b_dir_GetFile_0100 368 * @tc.name: b_dir_GetFile_0100 369 * @tc.desc: Test function of GetFile interface for SUCCESS 370 * @tc.size: MEDIUM 371 * @tc.type: FUNC 372 * @tc.level Level 1 373 * @tc.require: I6F3GV 374 */ 375 HWTEST_F(BDirTest, b_dir_GetFile_0100, testing::ext::TestSize.Level1) 376 { 377 GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetFile_0100"; 378 try { 379 string path = "/"; 380 auto [errCode, subFiles, subSmallFiles] = GetFile(path); 381 string pathData = "/data"; 382 auto [errCode1, subFiles1, subSmallFiles1] = GetFile(pathData, PATH_MAX_LEN); 383 auto [errCode2, subFiles2, subSmallFiles2] = GetFile(pathData); 384 EXPECT_EQ(errCode, 0); 385 } catch (...) { 386 GTEST_LOG_(INFO) << "BDirTest-an exception occurred."; 387 } 388 GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetFile_0100"; 389 } 390 391 /** 392 * @tc.number: SUB_backup_b_dir_ExpandPathWildcard_0100 393 * @tc.name: b_dir_ExpandPathWildcard_0100 394 * @tc.desc: Test function of ExpandPathWildcard interface for SUCCESS 395 * @tc.size: MEDIUM 396 * @tc.type: FUNC 397 * @tc.level Level 1 398 * @tc.require: I6F3GV 399 */ 400 HWTEST_F(BDirTest, b_dir_ExpandPathWildcard_0100, testing::ext::TestSize.Level1) 401 { 402 GTEST_LOG_(INFO) << "BDirTest-begin b_dir_ExpandPathWildcard_0100"; 403 try { 404 TestManager tmCurrentUser("", MakeDirType::CURRENTUSER); 405 std::string dirCurrentUser = tmCurrentUser.GetRootDirCurTest(); 406 std::string cmdMkdirPre = std::string("mkdir -p ") + dirCurrentUser; 407 std::string dirAppData = "appdata/test"; 408 std::string dirHaps = "haps/test"; 409 std::string cmdMkdir = cmdMkdirPre + dirAppData; 410 system(cmdMkdir.c_str()); 411 cmdMkdir = cmdMkdirPre + dirHaps; 412 system(cmdMkdir.c_str()); 413 std::string cmdTouchFile = std::string("touch ") + dirCurrentUser + dirAppData + FILE_SEPARATOR_CHAR + "1.txt"; 414 system(cmdTouchFile.c_str()); 415 cmdTouchFile = string("touch ") + dirCurrentUser + "2.txt"; 416 system(cmdTouchFile.c_str()); 417 418 std::vector<std::string> include = { dirCurrentUser }; 419 std::set<std::string> res = ExpandPathWildcard(include, true); 420 EXPECT_EQ(res.size(), 2); // 2: valid path number 421 EXPECT_EQ(res.count(dirCurrentUser), 0); 422 423 std::string testDir = dirCurrentUser + "appdata"; 424 include = { testDir }; 425 res = ExpandPathWildcard(include, true); 426 EXPECT_EQ(res.size(), 0); 427 428 testDir = dirCurrentUser + "*.txt"; 429 include = { testDir }; 430 431 res = ExpandPathWildcard(include, true); 432 EXPECT_EQ(res.size(), 1); // 1: dirCurrentUser + "2.txt" 433 } catch (...) { 434 GTEST_LOG_(INFO) << "BDirTest-an ExpandPathWildcard_0100 exception occurred."; 435 EXPECT_TRUE(false); 436 } 437 GTEST_LOG_(INFO) << "BDirTest-end b_dir_ExpandPathWildcard_0100"; 438 } 439 440 /** 441 * @tc.number: SUB_backup_b_dir_ExpandPathWildcard_0200 442 * @tc.name: b_dir_ExpandPathWildcard_0200 443 * @tc.desc: Test function of ExpandPathWildcard interface for SUCCESS 444 * @tc.size: MEDIUM 445 * @tc.type: FUNC 446 * @tc.level Level 1 447 * @tc.require: I6F3GV 448 */ 449 HWTEST_F(BDirTest, b_dir_ExpandPathWildcard_0200, testing::ext::TestSize.Level1) 450 { 451 GTEST_LOG_(INFO) << "BDirTest-begin b_dir_ExpandPathWildcard_0200"; 452 try { 453 TestManager tmDefault("b_dir_ExpandPathWildcard_0200"); 454 std::string dirDefault = tmDefault.GetRootDirCurTest(); 455 std::string cmdMkdirPre = std::string("mkdir -p ") + dirDefault; 456 std::string dirAppData = "appdata/test"; 457 std::string dirHaps = "haps/test"; 458 std::string cmdMkdir = cmdMkdirPre + dirAppData; 459 system(cmdMkdir.c_str()); 460 cmdMkdir = cmdMkdirPre + dirHaps; 461 system(cmdMkdir.c_str()); 462 std::string cmdTouchFile = std::string("touch ") + dirDefault + dirAppData + FILE_SEPARATOR_CHAR + "1.txt"; 463 system(cmdTouchFile.c_str()); 464 cmdTouchFile = string("touch ") + dirDefault + "2.txt"; 465 system(cmdTouchFile.c_str()); 466 467 std::vector<std::string> include = { dirDefault }; 468 std::set<std::string> res = ExpandPathWildcard(include, true); 469 EXPECT_EQ(res.size(), 1); // 1: dirDefault 470 471 std::string testDir = dirDefault + "*.txt"; 472 include = { testDir }; 473 res = ExpandPathWildcard(include, true); 474 EXPECT_EQ(res.size(), 1); // 1: dirCurrentUser + "2.txt" 475 } catch (...) { 476 GTEST_LOG_(INFO) << "BDirTest-an ExpandPathWildcard_0200 exception occurred."; 477 EXPECT_TRUE(false); 478 } 479 GTEST_LOG_(INFO) << "BDirTest-end b_dir_ExpandPathWildcard_0200"; 480 } 481 482 /** 483 * @tc.number: SUB_backup_b_dir_RmForceExcludePath_0100 484 * @tc.name: b_dir_RmForceExcludePath_0100 485 * @tc.desc: Test function of RmForceExcludePath interface for SUCCESS 486 * @tc.size: MEDIUM 487 * @tc.type: FUNC 488 * @tc.level Level 1 489 * @tc.require: I6F3GV 490 */ 491 HWTEST_F(BDirTest, b_dir_RmForceExcludePath_0100, testing::ext::TestSize.Level1) 492 { 493 GTEST_LOG_(INFO) << "BDirTest-begin b_dir_RmForceExcludePath_0100"; 494 try { 495 TestManager tmCurrentUser("", MakeDirType::CURRENTUSER); 496 std::string dirCurrentUser = tmCurrentUser.GetRootDirCurTest(); 497 std::string cmdMkdirPre = std::string("mkdir -p ") + dirCurrentUser; 498 std::string dirAppData = "appdata/test"; 499 std::string dirHaps = "haps/test"; 500 std::string cmdMkdir = cmdMkdirPre + dirAppData; 501 system(cmdMkdir.c_str()); 502 cmdMkdir = cmdMkdirPre + dirHaps; 503 system(cmdMkdir.c_str()); 504 std::string cmdTouchFile = std::string("touch ") + dirCurrentUser + dirAppData + FILE_SEPARATOR_CHAR + "1.txt"; 505 system(cmdTouchFile.c_str()); 506 cmdTouchFile = string("touch ") + dirCurrentUser + "2.txt"; 507 system(cmdTouchFile.c_str()); 508 509 std::set<std::string> testPath = { 510 dirCurrentUser 511 }; 512 RmForceExcludePath(testPath); 513 EXPECT_EQ(testPath.size(), 2); // 2: valid path number 514 515 testPath = { 516 dirCurrentUser + "appdata/" 517 }; 518 RmForceExcludePath(testPath); 519 EXPECT_EQ(testPath.size(), 0); 520 521 testPath = { 522 dirCurrentUser + "haps" 523 }; 524 RmForceExcludePath(testPath); 525 EXPECT_EQ(testPath.size(), 1); // 1: dirCurrentUser + "haps" 526 } catch (...) { 527 GTEST_LOG_(INFO) << "BDirTest-an RmForceExcludePath exception occurred."; 528 EXPECT_TRUE(false); 529 } 530 GTEST_LOG_(INFO) << "BDirTest-end b_dir_RmForceExcludePath_0100"; 531 } 532 533 /** 534 * @tc.number: SUB_backup_b_dir_GetSubDir_0100 535 * @tc.name: b_dir_GetSubDir_0100 536 * @tc.desc: Test function of GetSubDir interface for SUCCESS 537 * @tc.size: MEDIUM 538 * @tc.type: FUNC 539 * @tc.level Level 1 540 * @tc.require: I6F3GV 541 */ 542 HWTEST_F(BDirTest, b_dir_GetSubDir_0100, testing::ext::TestSize.Level1) 543 { 544 GTEST_LOG_(INFO) << "BDirTest-begin b_dir_GetSubDir_0100"; 545 try { 546 TestManager tmCurrentUser("", MakeDirType::CURRENTUSER); 547 std::string dirCurrentUser = tmCurrentUser.GetRootDirCurTest(); 548 std::string cmdMkdirPre = std::string("mkdir -p ") + dirCurrentUser; 549 std::string dirAppData = "appdata/test"; 550 std::string dirHaps = "haps/test"; 551 std::string cmdMkdir = cmdMkdirPre + dirAppData; 552 system(cmdMkdir.c_str()); 553 cmdMkdir = cmdMkdirPre + dirHaps; 554 system(cmdMkdir.c_str()); 555 std::string cmdTouchFile = std::string("touch ") + dirCurrentUser + dirAppData + FILE_SEPARATOR_CHAR + "1.txt"; 556 system(cmdTouchFile.c_str()); 557 cmdTouchFile = string("touch ") + dirCurrentUser + "2.txt"; 558 system(cmdTouchFile.c_str()); 559 560 std::set<std::string> result = GetSubDir(""); 561 EXPECT_EQ(result.size(), 0); 562 563 result = GetSubDir("test"); 564 EXPECT_EQ(result.size(), 0); 565 566 result = GetSubDir(dirCurrentUser); 567 EXPECT_EQ(result.size(), 2); // 2: valid path number 568 } catch (...) { 569 GTEST_LOG_(INFO) << "BDirTest-an GetSubDir exception occurred."; 570 EXPECT_TRUE(false); 571 } 572 GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetSubDir_0100"; 573 } 574 575 /** 576 * @tc.number: SUB_backup_b_dir_PreDealExcludes_0100 577 * @tc.name: b_dir_PreDealExcludes_0100 578 * @tc.desc: Test function of PreDealExcludes interface for SUCCESS 579 * @tc.size: MEDIUM 580 * @tc.type: FUNC 581 * @tc.level Level 1 582 * @tc.require: I6F3GV 583 */ 584 HWTEST_F(BDirTest, b_dir__PreDealExcludes_0100, testing::ext::TestSize.Level1) 585 { 586 GTEST_LOG_(INFO) << "BDirTest-begin b_dir_PreDealExcludes_0100"; 587 try { 588 std::string firstEle = "test"; 589 std::string secEle = ""; 590 std::string thirdEle = "test/test1"; 591 std::string fourthEle = "/test/test1"; 592 std::string fifthEle = "/test/test1/"; 593 std::vector<std::string> excludes = { 594 firstEle, 595 secEle, 596 thirdEle, 597 fourthEle, 598 fifthEle 599 }; 600 PreDealExcludes(excludes); 601 EXPECT_EQ(excludes.size(), 4); // 4: the size of excludes after preDeal 602 EXPECT_EQ(excludes[0], firstEle); // 0: first idx 603 EXPECT_EQ(excludes[1], fourthEle); // 1: second idx 604 EXPECT_EQ(excludes[2], fourthEle); // 2: third idx 605 EXPECT_EQ(excludes[3], fifthEle + "*"); // 3: firth idx 606 } catch (...) { 607 GTEST_LOG_(INFO) << "BDirTest-an PreDealExcludes exception occurred."; 608 EXPECT_TRUE(false); 609 } 610 GTEST_LOG_(INFO) << "BDirTest-end b_dir_PreDealExcludes_0100"; 611 } 612 } // namespace OHOS::FileManagement::Backup