1 /*
2 * Copyright (c) 2022-2024 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(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(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_0700
401 * @tc.name: Ext_Extension_Test_0700
402 * @tc.desc: 测试file为空
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_0700, testing::ext::TestSize.Level1)
409 {
410 GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0700";
411 try {
412 vector<struct ReportFileInfo> files;
413 TarMap result = GetIncrmentBigInfos(files);
414 EXPECT_TRUE(result.empty());
415 } catch (...) {
416 EXPECT_TRUE(false);
417 GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
418 }
419 GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0700";
420 }
421
422 /**
423 * @tc.number: SUB_Ext_Extension_0701
424 * @tc.name: Ext_Extension_Test_0701
425 * @tc.desc: 测试file只有一个元素
426 * @tc.size: MEDIUM
427 * @tc.type: FUNC
428 * @tc.level Level 1
429 * @tc.require: I9P3Y3
430 */
431 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0701, testing::ext::TestSize.Level1)
432 {
433 GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0701";
434 try {
435 vector<struct ReportFileInfo> srcFiles;
436 struct ReportFileInfo info;
437 info.filePath = PATH + BUNDLE_NAME;
438 info.mode = "755";
439 info.isDir = 0;
440 info.size = 1024;
441 info.mtime = 123456789;
442 info.hash = "1234567890";
443 info.userTar = 1;
444 info.encodeFlag = false;
445 srcFiles.push_back(info);
446 TarMap result = GetIncrmentBigInfos(srcFiles);
447 EXPECT_EQ(result.size(), 1);
448 } catch (...) {
449 EXPECT_TRUE(false);
450 GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
451 }
452 GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0701";
453 }
454
455 /**
456 * @tc.number: SUB_Ext_Extension_0702
457 * @tc.name: Ext_Extension_Test_0702
458 * @tc.desc: 测试file有重复元素
459 * @tc.size: MEDIUM
460 * @tc.type: FUNC
461 * @tc.level Level 1
462 * @tc.require: I9P3Y3
463 */
464 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0702, testing::ext::TestSize.Level1)
465 {
466 GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0702";
467 try {
468 vector<struct ReportFileInfo> srcFiles;
469 struct ReportFileInfo info;
470 info.filePath = PATH + BUNDLE_NAME;
471 info.mode = "755";
472 info.isDir = 0;
473 info.size = 1024;
474 info.mtime = 123456789;
475 info.hash = "1234567890";
476 info.userTar = 1;
477 info.encodeFlag = false;
478 srcFiles.push_back(info);
479 srcFiles.push_back(info);
480 TarMap result = GetIncrmentBigInfos(srcFiles);
481 EXPECT_EQ(result.size(), 2);
482 } catch (...) {
483 EXPECT_TRUE(false);
484 GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
485 }
486 GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0702";
487 }
488
489 /**
490 * @tc.number: SUB_Ext_Extension_0800
491 * @tc.name: Ext_Extension_Test_0800
492 * @tc.desc: 测试 GetIndexFileRestorePath
493 * @tc.size: MEDIUM
494 * @tc.type: FUNC
495 * @tc.level Level 1
496 * @tc.require: I9P3Y3
497 */
498 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0800, testing::ext::TestSize.Level1)
499 {
500 GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0800";
501 try {
502 std::string bundleName = BConstants::BUNDLE_MEDIAL_DATA;
503 auto ret = GetIndexFileRestorePath(bundleName);
504 EXPECT_EQ(ret, string(BConstants::PATH_MEDIALDATA_BACKUP_HOME)
505 .append(BConstants::SA_BUNDLE_BACKUP_RESTORE)
506 .append(BConstants::EXT_BACKUP_MANAGE));
507
508 bundleName = "test";
509 ret = GetIndexFileRestorePath(bundleName);
510 EXPECT_EQ(ret, INDEX_FILE_RESTORE);
511 } catch (...) {
512 EXPECT_TRUE(false);
513 GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
514 }
515 GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0800";
516 }
517
518 /**
519 * @tc.number: SUB_Ext_Extension_0900
520 * @tc.name: Ext_Extension_Test_0900
521 * @tc.desc: 测试 GetRestoreTempPath
522 * @tc.size: MEDIUM
523 * @tc.type: FUNC
524 * @tc.level Level 1
525 * @tc.require: I9P3Y3
526 */
527 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0900, testing::ext::TestSize.Level1)
528 {
529 GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0900";
530 try {
531 std::string bundleName = BConstants::BUNDLE_MEDIAL_DATA;
532 auto ret = GetRestoreTempPath(bundleName);
533 EXPECT_EQ(ret, string(BConstants::PATH_MEDIALDATA_BACKUP_HOME)
534 .append(BConstants::SA_BUNDLE_BACKUP_RESTORE));
535
536 bundleName = "test";
537 ret = GetRestoreTempPath(bundleName);
538 EXPECT_EQ(ret, string(BConstants::PATH_BUNDLE_BACKUP_HOME)
539 .append(BConstants::SA_BUNDLE_BACKUP_RESTORE));
540 } catch (...) {
541 EXPECT_TRUE(false);
542 GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
543 }
544 GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0900";
545 }
546 } // namespace OHOS::FileManagement::Backup