• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <iostream>
17 #include <map>
18 #include <sstream>
19 #include <string>
20 #include <sys/stat.h>
21 #include <unistd.h>
22 #include <vector>
23 #include <fstream>
24 
25 #include <gtest/gtest.h>
26 
27 #include "b_error/b_error.h"
28 #include "b_error/b_excep_utils.h"
29 #include "ext_extension.cpp"
30 #include "sub_ext_extension.cpp"
31 
32 namespace OHOS::FileManagement::Backup {
33 using namespace std;
34 using namespace testing;
35 
36 namespace {
37 const string FILE_NAME = "1.txt";
38 const string BUNDLE_NAME = "com.example.app2backup/";
39 const string MANAGE_JSON = "manage.json";
40 const string PATH = "/data/storage/el2/backup/test/";
41 const string TAR_FILE = "1.tar";
42 } // namespace
43 
44 class ExtExtensionTest : public testing::Test {
45 public:
46     //所有测试用例执行之前执行
47     static void SetUpTestCase(void);
48     //所有测试用例执行之后执行
49     static void TearDownTestCase(void);
50     //每次测试用例执行之前执行
SetUp()51     void SetUp() {};
52     //每次测试用例执行之后执行
TearDown()53     void TearDown() {};
54 };
55 
SetUpTestCase(void)56 void ExtExtensionTest::SetUpTestCase(void)
57 {
58     //创建测试路径
59     string cmdMkdir = string("mkdir -p ") + PATH + BUNDLE_NAME;
60     system(cmdMkdir.c_str());
61     //创建测试文件
62     string touchFile = string("touch ") + PATH + BUNDLE_NAME + FILE_NAME;
63     system(touchFile.c_str());
64     string touchFile2 = string("touch ") + PATH + BUNDLE_NAME + "2.txt";
65     system(touchFile2.c_str());
66 };
67 
TearDownTestCase(void)68 void ExtExtensionTest::TearDownTestCase(void)
69 {
70     //删除测试文件夹和文件
71     string rmDir = string("rm -r") + PATH + BUNDLE_NAME;
72     system(rmDir.c_str());
73 };
74 
75 /**
76  * @tc.number: SUB_Ext_Extension_0100
77  * @tc.name: Ext_Extension_Test_0100
78  * @tc.desc: 测试路径为空返回TRUE
79  * @tc.size: MEDIUM
80  * @tc.type: FUNC
81  * @tc.level Level 1
82  * @tc.require: I9P3Y3
83  */
84 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0100, testing::ext::TestSize.Level1)
85 {
86     GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0100";
87     try {
88         string filePath = " ";
89         EXPECT_TRUE(BDir::CheckAndCreateDirectory(filePath));
90     } catch (...) {
91         EXPECT_TRUE(false);
92         GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
93     }
94     GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0100";
95 }
96 
97 /**
98  * @tc.number: SUB_Ext_Extension_0101
99  * @tc.name: Ext_Extension_Test_0101
100  * @tc.desc: 测试路径非法
101  * @tc.size: MEDIUM
102  * @tc.type: FUNC
103  * @tc.level Level 1
104  * @tc.require: I9P3Y3
105  */
106 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0101, testing::ext::TestSize.Level1)
107 {
108     GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0101";
109     try {
110         string filePath = PATH + "/tmp";
111         EXPECT_TRUE(BDir::CheckAndCreateDirectory(filePath));
112     } catch (...) {
113         EXPECT_TRUE(false);
114         GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
115     }
116     GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0101";
117 }
118 
119 /**
120  * @tc.number: SUB_Ext_Extension_0300
121  * @tc.name: Ext_Extension_Test_0300
122  * @tc.desc: 测试tarfile为空
123  * @tc.size: MEDIUM
124  * @tc.type: FUNC
125  * @tc.level Level 1
126  * @tc.require: I9P3Y3
127  */
128 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0300, testing::ext::TestSize.Level1)
129 {
130     GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0300";
131     try {
132         string tarFile = " ";
133         vector<ExtManageInfo> extManageInfo;
134         off_t tarFileSize = 0;
135         bool ret = IsUserTar(tarFile, extManageInfo, tarFileSize);
136         EXPECT_FALSE(ret);
137         EXPECT_TRUE(tarFileSize == 0);
138     } catch (...) {
139         EXPECT_TRUE(false);
140         GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
141     }
142     GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0300";
143 }
144 
145 /**
146  * @tc.number: SUB_Ext_Extension_0301
147  * @tc.name: Ext_Extension_Test_0301
148  * @tc.desc: 测试tarfile不为空,extManageInfo不包含tarfile
149  * @tc.size: MEDIUM
150  * @tc.type: FUNC
151  * @tc.level Level 1
152  * @tc.require: I9P3Y3
153  */
154 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0301, testing::ext::TestSize.Level1)
155 {
156     GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0301";
157     try {
158         string tarFile = TAR_FILE;
159         vector<ExtManageInfo> extManageInfo;
160         off_t tarFileSize = 0;
161         bool ret = IsUserTar(tarFile, extManageInfo, tarFileSize);
162         EXPECT_FALSE(ret);
163         EXPECT_TRUE(tarFileSize == 0);
164     } catch (...) {
165         EXPECT_TRUE(false);
166         GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
167     }
168     GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0301";
169 }
170 
171 /**
172  * @tc.number: SUB_Ext_Extension_0302
173  * @tc.name: Ext_Extension_Test_0302
174  * @tc.desc: 测试tarfile不为空,extManageInfo包含tarfile且isusertar =true
175  * @tc.size: MEDIUM
176  * @tc.type: FUNC
177  * @tc.level Level 1
178  * @tc.require: I9P3Y3
179  */
180 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0302, testing::ext::TestSize.Level1)
181 {
182     GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0302";
183     try {
184         string tarFile = TAR_FILE;
185         vector<ExtManageInfo> extManageInfo;
186         ExtManageInfo info;
187         info.hashName = TAR_FILE;
188         info.isUserTar = true;
189         info.sta.st_size = 1; // 1: test number;
190         extManageInfo.push_back(info);
191         off_t tarFileSize = 0;
192         bool ret = IsUserTar(tarFile, extManageInfo, tarFileSize);
193         EXPECT_TRUE(ret);
194         EXPECT_TRUE(tarFileSize == 1);
195     } catch (...) {
196         EXPECT_TRUE(false);
197         GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
198     }
199     GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0302";
200 }
201 
202 
203 /**
204  * @tc.number: SUB_Ext_Extension_0500
205  * @tc.name: Ext_Extension_Test_0500
206  * @tc.desc: 测试filepath为空
207  * @tc.size: MEDIUM
208  * @tc.type: FUNC
209  * @tc.level Level 1
210  * @tc.require: I9P3Y3
211  */
212 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0500, testing::ext::TestSize.Level1)
213 {
214     GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0500";
215     try {
216         string filePath = " ";
217         string fileName = FILE_NAME;
218         string path = PATH;
219         string hashName = "test.txt";
220         bool ret = RestoreBigFilePrecheck(fileName, path, hashName, filePath);
221         EXPECT_FALSE(ret);
222     } catch (...) {
223         EXPECT_TRUE(false);
224         GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
225     }
226     GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0500";
227 }
228 
229 /**
230  * @tc.number: SUB_Ext_Extension_0501
231  * @tc.name: Ext_Extension_Test_0501
232  * @tc.desc: 测试filename为空
233  * @tc.size: MEDIUM
234  * @tc.type: FUNC
235  * @tc.level Level 1
236  * @tc.require: I9P3Y3
237  */
238 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0501, testing::ext::TestSize.Level1)
239 {
240     GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0501";
241     try {
242         string filePath = PATH;
243         string fileName = " ";
244         string path = PATH;
245         string hashName = "test.txt";
246         bool ret = RestoreBigFilePrecheck(fileName, path, hashName, filePath);
247         EXPECT_FALSE(ret);
248     } catch (...) {
249         EXPECT_TRUE(false);
250         GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
251     }
252     GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0501";
253 }
254 
255 /**
256  * @tc.number: SUB_Ext_Extension_0502
257  * @tc.name: Ext_Extension_Test_0502
258  * @tc.desc: 测试filename为空
259  * @tc.size: MEDIUM
260  * @tc.type: FUNC
261  * @tc.level Level 1
262  * @tc.require: I9P3Y3
263  */
264 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0502, testing::ext::TestSize.Level1)
265 {
266     GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0502";
267     try {
268         string filePath = "data/storage/el2/backup/test/";
269         string fileName = PATH + BUNDLE_NAME + FILE_NAME;
270         string path = PATH;
271         string hashName = "test.txt";
272         bool ret = RestoreBigFilePrecheck(fileName, path, hashName, filePath);
273         EXPECT_TRUE(ret);
274     } catch (...) {
275         EXPECT_TRUE(false);
276         GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
277     }
278     GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0502";
279 }
280 
281 /**
282  * @tc.number: SUB_Ext_Extension_0601
283  * @tc.name: Ext_Extension_Test_0601
284  * @tc.desc: 测试写入成功
285  * @tc.size: MEDIUM
286  * @tc.type: FUNC
287  * @tc.level Level 1
288  * @tc.require: I9P3Y3
289  */
290 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0601, testing::ext::TestSize.Level1)
291 {
292     GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0601";
293     try {
294         string fileName = PATH + BUNDLE_NAME + "2.txt";
295         int fd = open(fileName.data(), O_RDWR | O_TRUNC, S_IRWXU);
296         EXPECT_GT(fd, 0);
297         close(fd);
298         vector<struct ReportFileInfo> srcFiles;
299         WriteFile(fileName, srcFiles);
300         ifstream f(fileName);
301         if (!f.is_open()) {
302             throw BError(BError::Codes::EXT_INVAL_ARG, "open failed");
303         }
304         string line;
305         getline(f, line);
306         string buf1 = line;
307         f.close();
308         EXPECT_EQ(buf1, "version=1.0&attrNum=8");
309     } catch (...) {
310         EXPECT_TRUE(false);
311         GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
312     }
313     GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0601";
314 }
315 
316 /**
317  * @tc.number: SUB_Ext_Extension_0602
318  * @tc.name: Ext_Extension_Test_0602
319  * @tc.desc: 测试写入成功 info.encodeFlag = true
320  * @tc.size: MEDIUM
321  * @tc.type: FUNC
322  * @tc.level Level 1
323  * @tc.require: I9P3Y3
324  */
325 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0602, testing::ext::TestSize.Level1)
326 {
327     GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0602";
328     try {
329         string fileName = PATH + BUNDLE_NAME + "2.txt";
330         int fd = open(fileName.data(), O_RDWR | O_TRUNC, S_IRWXU);
331         EXPECT_GT(fd, 0);
332         close(fd);
333         vector<struct ReportFileInfo> srcFiles;
334         struct ReportFileInfo info;
335         info.mode = "755";
336         info.isDir = 0;
337         info.size = 1024;
338         info.mtime = 123456789;
339         info.hash = "1234567890";
340         info.userTar = 1;
341         info.encodeFlag = true;
342         srcFiles.push_back(info);
343         WriteFile(fileName, srcFiles);
344         ifstream f(fileName);
345         if (!f.is_open()) {
346             throw BError(BError::Codes::EXT_INVAL_ARG, "open failed");
347         }
348         f.seekg(-2, ios::end);
349         string lastChar = to_string(f.get());
350         f.close();
351         EXPECT_EQ(lastChar, "49");
352     } catch (...) {
353         EXPECT_TRUE(false);
354         GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
355     }
356     GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0602";
357 }
358 
359 /**
360  * @tc.number: SUB_Ext_Extension_0603
361  * @tc.name: Ext_Extension_Test_0603
362  * @tc.desc: 测试写入成功 info.encodeFlag = false
363  * @tc.size: MEDIUM
364  * @tc.type: FUNC
365  * @tc.level Level 1
366  * @tc.require: I9P3Y3
367  */
368 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0603, testing::ext::TestSize.Level1)
369 {
370     GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0603";
371     try {
372         string fileName = PATH + BUNDLE_NAME + FILE_NAME;
373         vector<struct ReportFileInfo> srcFiles;
374         struct ReportFileInfo info;
375         info.mode = "755";
376         info.isDir = 0;
377         info.size = 1024;
378         info.mtime = 123456789;
379         info.hash = "1234567890";
380         info.userTar = 1;
381         info.encodeFlag = false;
382         srcFiles.push_back(info);
383         WriteFile(fileName, srcFiles);
384         ifstream f(fileName);
385         if (!f.is_open()) {
386             throw BError(BError::Codes::EXT_INVAL_ARG, "open failed");
387         }
388         f.seekg(-2, ios::end);
389         string lastChar = to_string(f.get());
390         f.close();
391         EXPECT_EQ(lastChar, "48");
392     } catch (...) {
393         EXPECT_TRUE(false);
394         GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
395     }
396     GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0603";
397 }
398 
399 /**
400  * @tc.number: SUB_Ext_Extension_0800
401  * @tc.name: Ext_Extension_Test_0800
402  * @tc.desc: 测试 GetIndexFileRestorePath
403  * @tc.size: MEDIUM
404  * @tc.type: FUNC
405  * @tc.level Level 1
406  * @tc.require: I9P3Y3
407  */
408 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0800, testing::ext::TestSize.Level1)
409 {
410     GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0800";
411     try {
412         std::string bundleName = BConstants::BUNDLE_MEDIAL_DATA;
413         auto ret = GetIndexFileRestorePath(bundleName);
414         EXPECT_EQ(ret, string(BConstants::PATH_MEDIALDATA_BACKUP_HOME)
415             .append(BConstants::SA_BUNDLE_BACKUP_RESTORE)
416             .append(BConstants::EXT_BACKUP_MANAGE));
417 
418         bundleName = "test";
419         ret = GetIndexFileRestorePath(bundleName);
420         EXPECT_EQ(ret, INDEX_FILE_RESTORE);
421     } catch (...) {
422         EXPECT_TRUE(false);
423         GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
424     }
425     GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0800";
426 }
427 
428 /**
429  * @tc.number: SUB_Ext_Extension_0900
430  * @tc.name: Ext_Extension_Test_0900
431  * @tc.desc: 测试 GetRestoreTempPath
432  * @tc.size: MEDIUM
433  * @tc.type: FUNC
434  * @tc.level Level 1
435  * @tc.require: I9P3Y3
436  */
437 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0900, testing::ext::TestSize.Level1)
438 {
439     GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0900";
440     try {
441         std::string bundleName = BConstants::BUNDLE_MEDIAL_DATA;
442         auto ret = GetRestoreTempPath(bundleName);
443         EXPECT_EQ(ret, string(BConstants::PATH_MEDIALDATA_BACKUP_HOME)
444             .append(BConstants::SA_BUNDLE_BACKUP_RESTORE));
445 
446         bundleName = "test";
447         ret = GetRestoreTempPath(bundleName);
448         EXPECT_EQ(ret, string(BConstants::PATH_BUNDLE_BACKUP_HOME)
449             .append(BConstants::SA_BUNDLE_BACKUP_RESTORE));
450     } catch (...) {
451         EXPECT_TRUE(false);
452         GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
453     }
454     GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0900";
455 }
456 
457 /**
458  * @tc.number: SUB_Ext_Extension_1100
459  * @tc.name: Ext_Extension_Test_1100
460  * @tc.desc: 测试 GetFileHandleForSpecialCloneCloud
461  * @tc.size: MEDIUM
462  * @tc.type: FUNC
463  * @tc.level Level 1
464  * @tc.require: I9P3Y3
465  */
466 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_1100, testing::ext::TestSize.Level1)
467 {
468     GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_1100";
469     try {
470         ErrCode errCode = ERR_OK;
471         int64_t cost = 0;
472         uint32_t includeNum = 0;
473         uint32_t excludeNum = 0;
474         AppRadar::DoBackupInfo backupInfo = {cost, 0, 0, 0, includeNum, excludeNum};
475         RecordDoBackupRes(BUNDLE_NAME, errCode, backupInfo);
476 
477         cost = BConstants::MAX_TIME_COST;
478         includeNum = BConstants::MAX_INEXCLUDE_SIZE;
479         backupInfo = {cost, 0, 0, 0, includeNum, excludeNum};
480         RecordDoBackupRes(BUNDLE_NAME, errCode, backupInfo);
481 
482         errCode = BError(BError::Codes::EXT_BROKEN_IPC);
483         RecordDoBackupRes(BUNDLE_NAME, errCode, backupInfo);
484     } catch (...) {
485         EXPECT_TRUE(false);
486         GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
487     }
488     GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_1100";
489 }
490 
491 /**
492  * @tc.number: SUB_Ext_Extension_1200
493  * @tc.name: Ext_Extension_Test_1200
494  * @tc.desc: 测试 GetFileHandleForSpecialCloneCloud
495  * @tc.size: MEDIUM
496  * @tc.type: FUNC
497  * @tc.level Level 1
498  * @tc.require: I9P3Y3
499  */
500 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_1200, testing::ext::TestSize.Level1)
501 {
502     GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_1200";
503     try {
504         const string fileName = "1.txt";
505         string tarName = "2.tar";
506         auto ret = GetIncrementalFileHandlePath(fileName, BUNDLE_NAME, tarName);
507         EXPECT_NE(ret, ERR_OK);
508         ret = GetIncrementalFileHandlePath(fileName, BConstants::BUNDLE_FILE_MANAGER, tarName);
509         EXPECT_NE(ret, ERR_OK);
510         ret = GetIncrementalFileHandlePath(fileName, BConstants::BUNDLE_MEDIAL_DATA, tarName);
511         EXPECT_NE(ret, ERR_OK);
512     } catch (...) {
513         EXPECT_TRUE(false);
514         GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
515     }
516     GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_1200";
517 }
518 } // namespace OHOS::FileManagement::Backup