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