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 // 文件大小大于等于1GB(1024MB)的文件属于大文件,因此这里创建大小为1025MB和1026MB的大文件 137 auto [bFatalErr, ret] = 138 BProcess::ExecuteCmd({"dd", "if=/dev/urandom", ("of=" + filePath1).c_str(), "bs=1M", "count=1025"}); 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=1026"}); 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 * 1025); 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_PreDealExcludes_0100 390 * @tc.name: b_dir_PreDealExcludes_0100 391 * @tc.desc: Test function of PreDealExcludes 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__PreDealExcludes_0100, testing::ext::TestSize.Level1) 398 { 399 GTEST_LOG_(INFO) << "BDirTest-begin b_dir_PreDealExcludes_0100"; 400 try { 401 std::string firstEle = "test"; 402 std::string secEle = ""; 403 std::string thirdEle = "test/test1"; 404 std::string fourthEle = "/test/test1"; 405 std::string fifthEle = "/test/test1/"; 406 std::vector<std::string> excludes = { 407 firstEle, 408 secEle, 409 thirdEle, 410 fourthEle, 411 fifthEle 412 }; 413 PreDealExcludes(excludes); 414 EXPECT_EQ(excludes.size(), 4); // 4: the size of excludes after preDeal 415 EXPECT_EQ(excludes[0], firstEle); // 0: first idx 416 EXPECT_EQ(excludes[1], fourthEle); // 1: second idx 417 EXPECT_EQ(excludes[2], fourthEle); // 2: third idx 418 EXPECT_EQ(excludes[3], fifthEle + "*"); // 3: firth idx 419 } catch (...) { 420 GTEST_LOG_(INFO) << "BDirTest-an PreDealExcludes exception occurred."; 421 EXPECT_TRUE(false); 422 } 423 GTEST_LOG_(INFO) << "BDirTest-end b_dir_PreDealExcludes_0100"; 424 } 425 } // namespace OHOS::FileManagement::Backup