1 /*)
2 * Copyright (c) 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 #include <gmock/gmock.h>
16 #include <gtest/gtest.h>
17 #include "b_jsonutil_mock.h"
18 #include "b_sa_utils_mock.h"
19
20 #include <fstream>
21 #include <iostream>
22 #include <refbase.h>
23 #include "file_uri.h"
24 #include "sandbox_helper.h"
25 #include "module_external/storage_manager_service.h"
26 #include "module_external/storage_manager_service.cpp"
27
28 namespace OHOS {
29 const std::string CAMERA_BUNDLENAME = "file";
30 const std::string MMS_BUNDLENAME = "com.ohos.mms";
31 using namespace std;
32 using namespace testing;
33 using namespace FileManagement::Backup;
34 namespace fs = std::filesystem;
35 class StorageManagerServiceTest : public testing::Test {
36 public:
37 static void SetUpTestCase();
38 static void TearDownTestCase();
39 void SetUp() override;
40 void TearDown() override;
41 };
42
SetUpTestCase(void)43 void StorageManagerServiceTest::SetUpTestCase(void) {}
TearDownTestCase(void)44 void StorageManagerServiceTest::TearDownTestCase(void) {}
SetUp(void)45 void StorageManagerServiceTest::SetUp(void) {}
TearDown(void)46 void StorageManagerServiceTest::TearDown(void) {}
47
48 /**
49 * @tc.name: Storage_Manager_ServiceTest_GetBundleStatsForIncrease_001
50 * @tc.desc: check the GetBundleStatsForIncrease function
51 * @tc.type: FUNC
52 * @tc.require: AR000IGCR7
53 */
54 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetBundleStatsForIncrease_001,
55 testing::ext::TestSize.Level1)
56 {
57 uint32_t userId = 100;
58 std::vector<std::string> bundleNames = {"com.example.app1", "com.example.app2"};
59 std::vector<int64_t> incrementalBackTimes = {123456789};
60 std::vector<int64_t> pkgFileSizes;
61 std::vector<int64_t> incPkgFileSizes;
62 int32_t result = StorageManagerService::GetInstance().GetBundleStatsForIncrease(userId, bundleNames,
63 incrementalBackTimes, pkgFileSizes, incPkgFileSizes);
64 EXPECT_EQ(result, E_ERR);
65 }
66
67 /**
68 * @tc.name: Storage_Manager_ServiceTest_GetBundleStatsForIncrease_002
69 * @tc.desc: check the GetBundleStatsForIncrease function
70 * @tc.type: FUNC
71 * @tc.require: AR000IGCR7
72 */
73 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetBundleStatsForIncrease_002,
74 testing::ext::TestSize.Level1)
75 {
76 uint32_t userId = 100;
77 std::vector<std::string> bundleNames = {"com.example.app1", "com.example.app2"};
78 std::vector<int64_t> incrementalBackTimes = {123456789, 987654321};
79 std::vector<int64_t> pkgFileSizes;
80 std::vector<int64_t> incPkgFileSizes;
81 int32_t result = StorageManagerService::GetInstance().GetBundleStatsForIncrease(userId, bundleNames,
82 incrementalBackTimes, pkgFileSizes, incPkgFileSizes);
83 EXPECT_EQ(result, 0);
84 }
85
86 /**
87 * @tc.name: Storage_Manager_ServiceTest_GetBundleStatsForIncreaseEach_001
88 * @tc.desc: check the GetBundleStatsForIncreaseEach function
89 * @tc.type: FUNC
90 * @tc.require: AR000IGCR7
91 */
92 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetBundleStatsForIncreaseEach_001,
93 testing::ext::TestSize.Level1)
94 {
95 uint32_t userId = 100;
96 std::string bundleName = "testBundle";
97 int64_t lastBackupTime = 123456789;
98 std::vector<int64_t> pkgFileSizes;
99 std::vector<int64_t> incPkgFileSizes;
100 StorageManagerService::GetInstance().GetBundleStatsForIncreaseEach(userId, bundleName, lastBackupTime,
101 pkgFileSizes, incPkgFileSizes);
102 EXPECT_EQ(pkgFileSizes.size(), 1);
103 EXPECT_EQ(pkgFileSizes[0], 0);
104 EXPECT_EQ(incPkgFileSizes.size(), 1);
105 EXPECT_EQ(incPkgFileSizes[0], 0);
106 }
107
108 /**
109 * @tc.name: Storage_Manager_ServiceTest_ReadIncludesExcludesPath_001
110 * @tc.desc: check the ReadIncludesExcludesPath function
111 * @tc.type: FUNC
112 * @tc.require: AR000IGCR7
113 */
114 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ReadIncludesExcludesPath_001,
115 testing::ext::TestSize.Level1)
116 {
117 std::string bundleName = "";
118 int64_t lastBackupTime = 123456789;
119 uint32_t userId = 100;
120 auto result = StorageManagerService::GetInstance().ReadIncludesExcludesPath(bundleName, lastBackupTime, userId);
121 EXPECT_TRUE(std::get<0>(result).empty());
122 EXPECT_TRUE(std::get<1>(result).empty());
123 }
124
125 /**
126 * @tc.name: Storage_Manager_ServiceTest_ReadIncludesExcludesPath_002
127 * @tc.desc: check the ReadIncludesExcludesPath function
128 * @tc.type: FUNC
129 * @tc.require: AR000IGCR7
130 */
131 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ReadIncludesExcludesPath_002,
132 testing::ext::TestSize.Level1)
133 {
134 std::string bundleName = "testBundle";
135 int64_t lastBackupTime = 123456789;
136 uint32_t userId = 100;
137 // Assuming the file does not exist or cannot be opened
138 auto result = StorageManagerService::GetInstance().ReadIncludesExcludesPath(bundleName, lastBackupTime, userId);
139 EXPECT_TRUE(std::get<0>(result).empty());
140 EXPECT_TRUE(std::get<1>(result).empty());
141 }
142
143 /**
144 * @tc.name: Storage_Manager_ServiceTest_DealWithIncludeFiles_001
145 * @tc.desc: check the DealWithIncludeFiles function
146 * @tc.type: FUNC
147 * @tc.require: AR000IGCR7
148 */
149 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_DealWithIncludeFiles_001,
150 testing::ext::TestSize.Level1)
151 {
152 std::string bundleName = CAMERA_BUNDLENAME;
153 BundleStatsParas paras = {.userId = 100, .bundleName = bundleName,
154 .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0};
155 std::vector<std::string> includes = {"data/storage/el1/base/" + DEFAULT_PATH_WITH_WILDCARD};
156 std::vector<std::string> phyIncludes;
157 std::map<std::string, std::string> pathMap;
158 StorageManagerService::GetInstance().DealWithIncludeFiles(paras, includes, phyIncludes, pathMap);
159 EXPECT_TRUE(phyIncludes.empty());
160 }
161
162 /**
163 * @tc.name: Storage_Manager_ServiceTest_DealWithIncludeFiles_002
164 * @tc.desc: check the DealWithIncludeFiles function
165 * @tc.type: FUNC
166 * @tc.require: AR000IGCR7
167 */
168 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_DealWithIncludeFiles_002,
169 testing::ext::TestSize.Level1)
170 {
171 std::string bundleName = CAMERA_BUNDLENAME;
172 BundleStatsParas paras = {.userId = 100, .bundleName = bundleName,
173 .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0};
174 std::vector<std::string> includes = {NORMAL_SAND_PREFIX};
175 std::vector<std::string> phyIncludes;
176 std::map<std::string, std::string> pathMap;
177 StorageManagerService::GetInstance().DealWithIncludeFiles(paras, includes, phyIncludes, pathMap);
178 EXPECT_TRUE(phyIncludes.empty());
179 }
180
181 /**
182 * @tc.name: Storage_Manager_ServiceTest_DealWithIncludeFiles_003
183 * @tc.desc: check the DealWithIncludeFiles function
184 * @tc.type: FUNC
185 * @tc.require: AR000IGCR7
186 */
187 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_DealWithIncludeFiles_003,
188 testing::ext::TestSize.Level1)
189 {
190 std::string bundleName = "testBundle";
191 BundleStatsParas paras = {.userId = 100, .bundleName = bundleName,
192 .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0};
193 std::vector<std::string> includes = {};
194 std::vector<std::string> phyIncludes;
195 std::map<std::string, std::string> pathMap;
196 StorageManagerService::GetInstance().DealWithIncludeFiles(paras, includes, phyIncludes, pathMap);
197 EXPECT_TRUE(phyIncludes.empty());
198 }
199
200 /**
201 * @tc.name: Storage_Manager_ServiceTest_ConvertSandboxRealPath_001
202 * @tc.desc: check the ConvertSandboxRealPath function
203 * @tc.type: FUNC
204 * @tc.require: AR000IGCR7
205 */
206 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ConvertSandboxRealPath_001,
207 testing::ext::TestSize.Level1)
208 {
209 uint32_t userId = 100;
210 std::string bundleName = "com.example.app";
211 std::string sandboxPathStr = NORMAL_SAND_PREFIX + "/path/to/file";
212 std::vector<std::string> realPaths;
213 std::map<std::string, std::string> pathMap;
214 StorageManagerService::GetInstance().ConvertSandboxRealPath(userId, bundleName, sandboxPathStr, realPaths, pathMap);
215 EXPECT_TRUE(realPaths.empty());
216 EXPECT_TRUE(pathMap.empty());
217 }
218
219 /**
220 * @tc.name: Storage_Manager_ServiceTest_ConvertSandboxRealPath_002
221 * @tc.desc: check the ConvertSandboxRealPath function
222 * @tc.type: FUNC
223 * @tc.require: AR000IGCR7
224 */
225 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ConvertSandboxRealPath_002,
226 testing::ext::TestSize.Level1)
227 {
228 uint32_t userId = 100;
229 std::string bundleName = CAMERA_BUNDLENAME;
230 std::string sandboxPathStr = NORMAL_SAND_PREFIX + "/el2/database/";
231 std::vector<std::string> realPaths;
232 std::map<std::string, std::string> pathMap;
233 StorageManagerService::GetInstance().ConvertSandboxRealPath(userId, bundleName, sandboxPathStr, realPaths, pathMap);
234 std::string uriString = URI_PREFIX + bundleName + AppFileService::SandboxHelper::Encode(sandboxPathStr);
235 AppFileService::ModuleFileUri::FileUri uri(uriString);
236 std::string physicalPath;
237 AppFileService::SandboxHelper::GetBackupPhysicalPath(uri.ToString(), std::to_string(userId), physicalPath);
238 EXPECT_FALSE(realPaths.empty());
239 EXPECT_FALSE(pathMap.empty());
240 EXPECT_EQ(realPaths[0], physicalPath);
241 EXPECT_EQ(pathMap[realPaths[0]], sandboxPathStr);
242 }
243
244 /**
245 * @tc.name: Storage_Manager_ServiceTest_CheckIfDirForIncludes_001
246 * @tc.desc: check the CheckIfDirForIncludes function
247 * @tc.type: FUNC
248 * @tc.require: AR000IGCR7
249 */
250 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_CheckIfDirForIncludes_001,
251 testing::ext::TestSize.Level1)
252 {
253 ofstream closedStatFile;
254 std::string bundleName = "com.example.app";
255 BundleStatsParas paras = {.userId = 100, .bundleName = bundleName,
256 .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0};
257 std::map<std::string, std::string> pathMap;
258 std::map<std::string, bool> excludesMap;
259 auto result = StorageManagerService::GetInstance().CheckIfDirForIncludes("test_path", paras,
260 pathMap, closedStatFile, excludesMap);
261 EXPECT_EQ(result, std::make_tuple(false, false));
262 }
263
264 /**
265 * @tc.name: Storage_Manager_ServiceTest_CheckIfDirForIncludes_002
266 * @tc.desc: check the CheckIfDirForIncludes function
267 * @tc.type: FUNC
268 * @tc.require: AR000IGCR7
269 */
270 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_CheckIfDirForIncludes_002,
271 testing::ext::TestSize.Level1)
272 {
273 ofstream statFile;
274 std::string bundleName = "com.example.app";
275 BundleStatsParas paras = {.userId = 100, .bundleName = bundleName,
276 .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0};
277 std::map<std::string, std::string> pathMap;
278 std::map<std::string, bool> excludesMap;
279 auto result = StorageManagerService::GetInstance().CheckIfDirForIncludes("", paras, pathMap, statFile, excludesMap);
280 EXPECT_EQ(result, std::make_tuple(false, false));
281 }
282
283 /**
284 * @tc.name: Storage_Manager_ServiceTest_CheckIfDirForIncludes_003
285 * @tc.desc: check the CheckIfDirForIncludes function
286 * @tc.type: FUNC
287 * @tc.require: AR000IGCR7
288 */
289 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_CheckIfDirForIncludes_003,
290 testing::ext::TestSize.Level1)
291 {
292 std::ofstream statFile;
293 fs::path tempPath = "/data/temp.txt";
294
295 try {
296 fs::path dirPath = tempPath.parent_path();
297 if (!fs::exists(dirPath)) {
298 fs::create_directories(dirPath);
299 }
300 fs::path canonicalPath = fs::canonical(fs::absolute(tempPath));
301 statFile.open(canonicalPath, std::ios::out | std::ios::trunc);
302 EXPECT_FALSE(!statFile.is_open()) << "file can not open";
303 std::string bundleName = "testBundle";
304 BundleStatsParas paras = {.userId = 100, .bundleName = bundleName,
305 .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0};
306 std::map<std::string, std::string> pathMap;
307 std::map<std::string, bool> excludesMap;
308 auto result = StorageManagerService::GetInstance().CheckIfDirForIncludes("/data/service", paras, pathMap,
309 statFile, excludesMap);
310 EXPECT_EQ(result, std::make_tuple(true, true));
311 fs::remove(canonicalPath);
312 } catch (const fs::filesystem_error& e) {
313 std::cerr << "Filesystem error: " << e.what() << '\n';
314 }
315 }
316
317 /**
318 * @tc.name: Storage_Manager_ServiceTest_ExcludeFilter_001
319 * @tc.desc: check the ExcludeFilter function
320 * @tc.type: FUNC
321 * @tc.require: AR000IGCR7
322 */
323 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ExcludeFilter_001,
324 testing::ext::TestSize.Level1)
325 {
326 std::map<std::string, bool> excludesMap;
327 std::string path = "";
328 bool result = StorageManagerService::GetInstance().ExcludeFilter(excludesMap, path);
329 EXPECT_TRUE(result);
330 }
331
332 /**
333 * @tc.name: Storage_Manager_ServiceTest_ExcludeFilter_002
334 * @tc.desc: check the ExcludeFilter function
335 * @tc.type: FUNC
336 * @tc.require: AR000IGCR7
337 */
338 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ExcludeFilter_002,
339 testing::ext::TestSize.Level1)
340 {
341 std::map<std::string, bool> excludesMap;
342 std::string path = "/path/to/file";
343 bool result = StorageManagerService::GetInstance().ExcludeFilter(excludesMap, path);
344 EXPECT_FALSE(result);
345 }
346
347 /**
348 * @tc.name: Storage_Manager_ServiceTest_GetUserStorageStatsByType_001
349 * @tc.desc: check the GetUserStorageStatsByType function
350 * @tc.type: FUNC
351 * @tc.require: AR000IGCR7
352 */
353 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetUserStorageStatsByType_001,
354 testing::ext::TestSize.Level1)
355 {
356 StorageManager::StorageStats storageStats;
357 std::string type = "MEDIA_TYPE";
358 int32_t userId = 100;
359 int64_t result = StorageManagerService::GetInstance().GetUserStorageStatsByType(userId, storageStats, type);
360 EXPECT_EQ(result, E_ERR);
361 EXPECT_EQ(storageStats.video_, 0);
362 EXPECT_EQ(storageStats.image_, 0);
363 EXPECT_EQ(storageStats.file_, 0);
364 }
365
366 /**
367 * @tc.name: Storage_Manager_ServiceTest_GetUserStorageStatsByType_002
368 * @tc.desc: check the GetUserStorageStatsByType function
369 * @tc.type: FUNC
370 * @tc.require: AR000IGCR7
371 */
372 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetUserStorageStatsByType_002,
373 testing::ext::TestSize.Level1)
374 {
375 StorageManager::StorageStats storageStats;
376 std::string type = FILE_TYPE;
377 int32_t userId = 100;
378 int64_t result = StorageManagerService::GetInstance().GetUserStorageStatsByType(userId, storageStats, type);
379 EXPECT_EQ(result, 0);
380 EXPECT_GE(storageStats.video_, 0);
381 EXPECT_GE(storageStats.image_, 0);
382 EXPECT_GE(storageStats.file_, 0);
383 }
384
385 /**
386 * @tc.name: Storage_Manager_ServiceTest_GetUserStorageStatsByType_003
387 * @tc.desc: check the GetUserStorageStatsByType function
388 * @tc.type: FUNC
389 * @tc.require: AR000IGCR7
390 */
391 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetUserStorageStatsByType_003,
392 testing::ext::TestSize.Level1)
393 {
394 StorageManager::StorageStats storageStats;
395 std::string type = "other";
396 int32_t userId = 100;
397 int64_t result = StorageManagerService::GetInstance().GetUserStorageStatsByType(userId, storageStats, type);
398 EXPECT_EQ(result, E_ERR);
399 EXPECT_GE(storageStats.video_, 0);
400 EXPECT_GE(storageStats.image_, 0);
401 EXPECT_GE(storageStats.file_, 0);
402 }
403
404 /**
405 * @tc.name: Storage_Manager_ServiceTest_GetMediaTypeAndSize_001
406 * @tc.desc: check the GetMediaTypeAndSize function
407 * @tc.type: FUNC
408 * @tc.require: AR000IGCR7
409 */
410 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetMediaTypeAndSize_001,
411 testing::ext::TestSize.Level1)
412 {
413 StorageManager::StorageStats storageStats;
414 StorageManagerService::GetInstance().GetMediaTypeAndSize(nullptr, storageStats);
415 EXPECT_EQ(storageStats.image_, 0);
416 EXPECT_EQ(storageStats.audio_, 0);
417 EXPECT_EQ(storageStats.video_, 0);
418
419 auto resultSet = std::make_shared<DataShare::DataShareResultSet>();
420 StorageManagerService::GetInstance().GetMediaTypeAndSize(resultSet, storageStats);
421
422 EXPECT_EQ(storageStats.image_, 0);
423 EXPECT_EQ(storageStats.audio_, 0);
424 EXPECT_EQ(storageStats.video_, 0);
425 }
426
427 /**
428 * @tc.name: Storage_Manager_ServiceTest_GetQuotaSrcMountPath_001
429 * @tc.desc: check the GetQuotaSrcMountPath function
430 * @tc.type: FUNC
431 * @tc.require: AR000IGCR7
432 */
433 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetQuotaSrcMountPath_001,
434 testing::ext::TestSize.Level1)
435 {
436 mQuotaReverseMounts.insert(std::make_pair("/path1", "/mount1"));
437 mQuotaReverseMounts.insert(std::make_pair("/path2", "/mount2"));
438 std::string target = "/path1";
439 std::string expectedPath = "/mount1";
440 std::string actualPath = GetQuotaSrcMountPath(target);
441 EXPECT_EQ(expectedPath, actualPath);
442 mQuotaReverseMounts.clear();
443 }
444
445 /**
446 * @tc.name: Storage_Manager_ServiceTest_GetQuotaSrcMountPath_002
447 * @tc.desc: check the GetQuotaSrcMountPath function
448 * @tc.type: FUNC
449 * @tc.require: AR000IGCR7
450 */
451 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetQuotaSrcMountPath_002,
452 testing::ext::TestSize.Level1)
453 {
454 mQuotaReverseMounts.insert(std::make_pair("/path1", "/mount1"));
455 mQuotaReverseMounts.insert(std::make_pair("/path1", "/mount1"));
456 std::string target = "/path3";
457 std::string expectedPath = "";
458 std::string actualPath = GetQuotaSrcMountPath(target);
459 EXPECT_EQ(expectedPath, actualPath);
460 mQuotaReverseMounts.clear();
461 }
462
463 /**
464 * @tc.name: Storage_Manager_ServiceTest_PathSortFunc_001
465 * @tc.desc: check the PathSortFunc function
466 * @tc.type: FUNC
467 * @tc.require: AR000IGCR7
468 */
469 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_PathSortFunc_001,
470 testing::ext::TestSize.Level1)
471 {
472 std::string path1 = "AAA";
473 std::string path2 = "BBB";
474 EXPECT_TRUE(PathSortFunc(path1, path2));
475 }
476
477 /**
478 * @tc.name: Storage_Manager_ServiceTest_DeduplicationPath_001
479 * @tc.number: DeduplicationPathTest_001
480 * @tc.desc: 测试configPath为空时,不执行任何操作
481 */
482 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_DeduplicationPath_001,
483 testing::ext::TestSize.Level1)
484 {
485 std::vector<std::string> configPaths;
486 StorageManagerService::GetInstance().DeduplicationPath(configPaths);
487 EXPECT_TRUE(configPaths.size() == 0);
488 }
489
490 /**
491 * @tc.name: Storage_Manager_ServiceTest_DeduplicationPath_002
492 * @tc.number: DeduplicationPathTest_002
493 * @tc.desc: 测试configPath不为空时,执行去重
494 */
495 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_DeduplicationPath_002,
496 testing::ext::TestSize.Level1)
497 {
498 std::vector<std::string> configPaths = {"path1", "path2", "path2"};
499 StorageManagerService::GetInstance().DeduplicationPath(configPaths);
500 EXPECT_TRUE(configPaths.size() == 2);
501 }
502
503 /**
504 * @tc.name: Storage_Manager_ServiceTest_ScanExtensionPath_001
505 * @tc.number: ScanExtensionPathTest_001
506 * @tc.desc: ScanExtensionPath 有效路径正确扫描
507 */
508 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ScanExtensionPath_001,
509 testing::ext::TestSize.Level1)
510 {
511 std::string bundleName = MMS_BUNDLENAME;
512 BundleStatsParas paras = {.userId = 100, .bundleName = bundleName,
513 .lastBackupTime = 123456789, .fileSizeSum = 0, .incFileSizeSum = 0};
514 std::vector<std::string> includes = {"/path/to/include"};
515 std::vector<std::string> excludes = {"/path/to/exclude"};
516 std::map<std::string, std::string> pathMap;
517 std::ofstream statFile("statfile.txt");
518
519 StorageManagerService::GetInstance().ScanExtensionPath(paras, includes, excludes, pathMap, statFile);
520 EXPECT_TRUE(pathMap.empty());
521 EXPECT_TRUE(statFile.good());
522
523 statFile.close();
524 remove("statfile.txt");
525 }
526
527 /**
528 * @tc.name: Storage_Manager_ServiceTest_AddOuterDirIntoFileStat_001
529 * @tc.number: AddOuterDirIntoFileStat_001
530 * @tc.desc: AddOuterDirIntoFileStat 调用时正常返回
531 */
532 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_AddOuterDirIntoFileStat_001,
533 testing::ext::TestSize.Level1)
534 {
535 std::string bundleName = MMS_BUNDLENAME;
536 std::string dir = "/data/app/el1/100/base/" + bundleName + "/.backup";
537 BundleStatsParas paras = {.userId = 100, .bundleName = bundleName,
538 .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0};
539 std::string sandboxDir = "/path/to/sandboxDir";
540 std::ofstream statFile("statfile.txt");
541 std::map<std::string, bool> excludesMap;
542
543 StorageManagerService::GetInstance().AddOuterDirIntoFileStat(dir, paras, sandboxDir, statFile, excludesMap);
544 EXPECT_TRUE(excludesMap.empty());
545
546 dir = "";
547 StorageManagerService::GetInstance().AddOuterDirIntoFileStat(dir, paras, sandboxDir, statFile, excludesMap);
548 EXPECT_TRUE(excludesMap.empty());
549
550 statFile.close();
551 remove("statfile.txt");
552 }
553
554 /**
555 * @tc.name: Storage_Manager_ServiceTest_InsertStatFile_001
556 * @tc.number: InsertStatFile_001
557 * @tc.desc: 测试 InsertStatFile 函数在输入有效时是否正确插入文件信息
558 */
559 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_InsertStatFile_001, testing::ext::TestSize.Level1)
560 {
561 std::string bundleName = MMS_BUNDLENAME;
562 std::string path = "/data/app/el1/100/base/" + bundleName + "/.backup";
563 struct FileStat fileStat = {.isDir = true};
564 std::ofstream statFile("test_stat_file.txt");
565 std::map<std::string, bool> excludesMap;
566 BundleStatsParas paras = {.userId = 100, .bundleName = bundleName,
567 .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0};
568
569 StorageManagerService::GetInstance().InsertStatFile(path, fileStat, statFile, excludesMap, paras);
570 EXPECT_TRUE(excludesMap.empty());
571 statFile.close();
572 }
573
574 /**
575 * @tc.name: Storage_Manager_ServiceTest_InsertStatFile_002
576 * @tc.number: InsertStatFile_002
577 * @tc.desc: 测试 InsertStatFile 函数路径在excludesMap中时是否正确排除文件信息
578 */
579 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_InsertStatFile_002, testing::ext::TestSize.Level1)
580 {
581 std::string bundleName = MMS_BUNDLENAME;
582 std::string path = "/data/app/el1/100/base/" + bundleName + "/.backup";
583 struct FileStat fileStat = {};
584 std::ofstream statFile("test_stat_file.txt");
585 std::map<std::string, bool> excludesMap = {{path, true}};
586 BundleStatsParas paras = {.userId = 100, .bundleName = bundleName,
587 .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0};
588
589 StorageManagerService::GetInstance().InsertStatFile(path, fileStat, statFile, excludesMap, paras);
590 EXPECT_TRUE(excludesMap.find(path) != excludesMap.end());
591 statFile.close();
592 }
593
594 /**
595 * @tc.name: Storage_Manager_ServiceTest_InsertStatFile_003
596 * @tc.number: InsertStatFile_003
597 * @tc.desc: 测试 InsertStatFile 函数路径在输入无效文件状态信息时的处理
598 */
599 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_InsertStatFile_003, testing::ext::TestSize.Level1)
600 {
601 std::string bundleName = MMS_BUNDLENAME;
602 std::string path = "/invalid/path";
603 struct FileStat fileStat = {};
604 std::ofstream statFile("test_stat_file.txt");
605 std::map<std::string, bool> excludesMap;
606 BundleStatsParas paras = {.userId = 100, .bundleName = bundleName,
607 .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0};
608
609 StorageManagerService::GetInstance().InsertStatFile(path, fileStat, statFile, excludesMap, paras);
610 EXPECT_TRUE(excludesMap.empty());
611 statFile.close();
612 }
613
614 /**
615 * @tc.name: Storage_Manager_ServiceTest_AddPathMapForPathWildCard_001
616 * @tc.number: AddPathMapForPathWildCard_001
617 * @tc.desc: 测试 AddPathMapForPathWildCard 函数返回值正常
618 */
619 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_AddPathMapForPathWildCard_001,
620 testing::ext::TestSize.Level1)
621 {
622 uint32_t userId = 100;
623 std::string name = MMS_BUNDLENAME;
624 std::string phyPath = "/data/app/el1/100/base/com.ohos.mms/.backup";
625 std::map<std::string, std::string> pathMap;
626
627 bool result = StorageManagerService::GetInstance().AddPathMapForPathWildCard(userId, name, phyPath, pathMap);
628 EXPECT_EQ(result, true);
629
630 name = "com.example.app2";
631 phyPath = "/data/app/el2/100/base/com.example.app2/.backup";
632 pathMap.insert({phyPath, ".backup"});
633 result = StorageManagerService::GetInstance().AddPathMapForPathWildCard(userId, name, phyPath, pathMap);
634 EXPECT_EQ(result, true);
635
636 phyPath = "";
637 result = StorageManagerService::GetInstance().AddPathMapForPathWildCard(userId, name, phyPath, pathMap);
638 EXPECT_EQ(result, false);
639 }
640 }