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_0200
121 * @tc.name: Ext_Extension_Test_0200
122 * @tc.desc: 测试成功
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_0200, testing::ext::TestSize.Level1)
129 {
130 GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0200";
131 try {
132 string fileName = "filename";
133 string reportName = GetReportFileName(fileName);
134 EXPECT_EQ(reportName, "filename.rp");
135 } catch (...) {
136 EXPECT_TRUE(false);
137 GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
138 }
139 GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0200";
140 }
141
142 /**
143 * @tc.number: SUB_Ext_Extension_0300
144 * @tc.name: Ext_Extension_Test_0300
145 * @tc.desc: 测试tarfile为空
146 * @tc.size: MEDIUM
147 * @tc.type: FUNC
148 * @tc.level Level 1
149 * @tc.require: I9P3Y3
150 */
151 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0300, testing::ext::TestSize.Level1)
152 {
153 GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0300";
154 try {
155 string tarFile = " ";
156 vector<ExtManageInfo> extManageInfo;
157 off_t tarFileSize = 0;
158 bool ret = IsUserTar(tarFile, extManageInfo, tarFileSize);
159 EXPECT_FALSE(ret);
160 EXPECT_TRUE(tarFileSize == 0);
161 } catch (...) {
162 EXPECT_TRUE(false);
163 GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
164 }
165 GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0300";
166 }
167
168 /**
169 * @tc.number: SUB_Ext_Extension_0301
170 * @tc.name: Ext_Extension_Test_0301
171 * @tc.desc: 测试tarfile不为空,extManageInfo不包含tarfile
172 * @tc.size: MEDIUM
173 * @tc.type: FUNC
174 * @tc.level Level 1
175 * @tc.require: I9P3Y3
176 */
177 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0301, testing::ext::TestSize.Level1)
178 {
179 GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0301";
180 try {
181 string tarFile = TAR_FILE;
182 vector<ExtManageInfo> extManageInfo;
183 off_t tarFileSize = 0;
184 bool ret = IsUserTar(tarFile, extManageInfo, tarFileSize);
185 EXPECT_FALSE(ret);
186 EXPECT_TRUE(tarFileSize == 0);
187 } catch (...) {
188 EXPECT_TRUE(false);
189 GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
190 }
191 GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0301";
192 }
193
194 /**
195 * @tc.number: SUB_Ext_Extension_0302
196 * @tc.name: Ext_Extension_Test_0302
197 * @tc.desc: 测试tarfile不为空,extManageInfo包含tarfile且isusertar =true
198 * @tc.size: MEDIUM
199 * @tc.type: FUNC
200 * @tc.level Level 1
201 * @tc.require: I9P3Y3
202 */
203 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0302, testing::ext::TestSize.Level1)
204 {
205 GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0302";
206 try {
207 string tarFile = TAR_FILE;
208 vector<ExtManageInfo> extManageInfo;
209 ExtManageInfo info;
210 info.hashName = TAR_FILE;
211 info.isUserTar = true;
212 info.sta.st_size = 1; // 1: test number;
213 extManageInfo.push_back(info);
214 off_t tarFileSize = 0;
215 bool ret = IsUserTar(tarFile, extManageInfo, tarFileSize);
216 EXPECT_TRUE(ret);
217 EXPECT_TRUE(tarFileSize == 1);
218 } catch (...) {
219 EXPECT_TRUE(false);
220 GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
221 }
222 GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0302";
223 }
224
225 /**
226 * @tc.number: SUB_Ext_Extension_0400
227 * @tc.name: Ext_Extension_Test_0400
228 * @tc.desc: 测试打开失败
229 * @tc.size: MEDIUM
230 * @tc.type: FUNC
231 * @tc.level Level 1
232 * @tc.require: I9P3Y3
233 */
234 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0400, testing::ext::TestSize.Level1)
235 {
236 GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0400";
237 try {
238 string tarName = " ";
239 auto ret = GetTarIncludes(tarName);
240 size_t size = ret.size();
241 EXPECT_EQ(size, 0);
242 } catch (...) {
243 EXPECT_TRUE(false);
244 GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
245 }
246 GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0400";
247 }
248
249 /**
250 * @tc.number: SUB_Ext_Extension_0500
251 * @tc.name: Ext_Extension_Test_0500
252 * @tc.desc: 测试filepath为空
253 * @tc.size: MEDIUM
254 * @tc.type: FUNC
255 * @tc.level Level 1
256 * @tc.require: I9P3Y3
257 */
258 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0500, testing::ext::TestSize.Level1)
259 {
260 GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0500";
261 try {
262 string filePath = " ";
263 string fileName = FILE_NAME;
264 string path = PATH;
265 string hashName = "test.txt";
266 bool ret = RestoreBigFilePrecheck(fileName, path, hashName, filePath);
267 EXPECT_FALSE(ret);
268 } catch (...) {
269 EXPECT_TRUE(false);
270 GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
271 }
272 GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0500";
273 }
274
275 /**
276 * @tc.number: SUB_Ext_Extension_0501
277 * @tc.name: Ext_Extension_Test_0501
278 * @tc.desc: 测试filename为空
279 * @tc.size: MEDIUM
280 * @tc.type: FUNC
281 * @tc.level Level 1
282 * @tc.require: I9P3Y3
283 */
284 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0501, testing::ext::TestSize.Level1)
285 {
286 GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0501";
287 try {
288 string filePath = PATH;
289 string fileName = " ";
290 string path = PATH;
291 string hashName = "test.txt";
292 bool ret = RestoreBigFilePrecheck(fileName, path, hashName, filePath);
293 EXPECT_FALSE(ret);
294 } catch (...) {
295 EXPECT_TRUE(false);
296 GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
297 }
298 GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0501";
299 }
300
301 /**
302 * @tc.number: SUB_Ext_Extension_0502
303 * @tc.name: Ext_Extension_Test_0502
304 * @tc.desc: 测试filename为空
305 * @tc.size: MEDIUM
306 * @tc.type: FUNC
307 * @tc.level Level 1
308 * @tc.require: I9P3Y3
309 */
310 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0502, testing::ext::TestSize.Level1)
311 {
312 GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0502";
313 try {
314 string filePath = "data/storage/el2/backup/test/";
315 string fileName = PATH + BUNDLE_NAME + FILE_NAME;
316 string path = PATH;
317 string hashName = "test.txt";
318 bool ret = RestoreBigFilePrecheck(fileName, path, hashName, filePath);
319 EXPECT_TRUE(ret);
320 } catch (...) {
321 EXPECT_TRUE(false);
322 GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
323 }
324 GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0502";
325 }
326
327 /**
328 * @tc.number: SUB_Ext_Extension_0601
329 * @tc.name: Ext_Extension_Test_0601
330 * @tc.desc: 测试写入成功
331 * @tc.size: MEDIUM
332 * @tc.type: FUNC
333 * @tc.level Level 1
334 * @tc.require: I9P3Y3
335 */
336 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0601, testing::ext::TestSize.Level1)
337 {
338 GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0601";
339 try {
340 string fileName = PATH + BUNDLE_NAME + "2.txt";
341 int fd = open(fileName.data(), O_RDWR | O_TRUNC, S_IRWXU);
342 EXPECT_GT(fd, 0);
343 close(fd);
344 vector<struct ReportFileInfo> srcFiles;
345 WriteFile(fileName, srcFiles);
346 ifstream f(fileName);
347 if (!f.is_open()) {
348 throw BError(BError::Codes::EXT_INVAL_ARG, "open failed");
349 }
350 string line;
351 getline(f, line);
352 string buf1 = line;
353 f.close();
354 EXPECT_EQ(buf1, "version=1.0&attrNum=8");
355 } catch (...) {
356 EXPECT_TRUE(false);
357 GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
358 }
359 GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0601";
360 }
361
362 /**
363 * @tc.number: SUB_Ext_Extension_0602
364 * @tc.name: Ext_Extension_Test_0602
365 * @tc.desc: 测试写入成功 info.encodeFlag = true
366 * @tc.size: MEDIUM
367 * @tc.type: FUNC
368 * @tc.level Level 1
369 * @tc.require: I9P3Y3
370 */
371 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0602, testing::ext::TestSize.Level1)
372 {
373 GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0602";
374 try {
375 string fileName = PATH + BUNDLE_NAME + "2.txt";
376 int fd = open(fileName.data(), O_RDWR | O_TRUNC, S_IRWXU);
377 EXPECT_GT(fd, 0);
378 close(fd);
379 vector<struct ReportFileInfo> srcFiles;
380 struct ReportFileInfo info;
381 info.mode = "755";
382 info.isDir = 0;
383 info.size = 1024;
384 info.mtime = 123456789;
385 info.hash = "1234567890";
386 info.userTar = 1;
387 info.encodeFlag = true;
388 srcFiles.push_back(info);
389 WriteFile(fileName, srcFiles);
390 ifstream f(fileName);
391 if (!f.is_open()) {
392 throw BError(BError::Codes::EXT_INVAL_ARG, "open failed");
393 }
394 f.seekg(-2, ios::end);
395 string lastChar = to_string(f.get());
396 f.close();
397 EXPECT_EQ(lastChar, "49");
398 } catch (...) {
399 EXPECT_TRUE(false);
400 GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
401 }
402 GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0602";
403 }
404
405 /**
406 * @tc.number: SUB_Ext_Extension_0603
407 * @tc.name: Ext_Extension_Test_0603
408 * @tc.desc: 测试写入成功 info.encodeFlag = false
409 * @tc.size: MEDIUM
410 * @tc.type: FUNC
411 * @tc.level Level 1
412 * @tc.require: I9P3Y3
413 */
414 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0603, testing::ext::TestSize.Level1)
415 {
416 GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0603";
417 try {
418 string fileName = PATH + BUNDLE_NAME + FILE_NAME;
419 vector<struct ReportFileInfo> srcFiles;
420 struct ReportFileInfo info;
421 info.mode = "755";
422 info.isDir = 0;
423 info.size = 1024;
424 info.mtime = 123456789;
425 info.hash = "1234567890";
426 info.userTar = 1;
427 info.encodeFlag = false;
428 srcFiles.push_back(info);
429 WriteFile(fileName, srcFiles);
430 ifstream f(fileName);
431 if (!f.is_open()) {
432 throw BError(BError::Codes::EXT_INVAL_ARG, "open failed");
433 }
434 f.seekg(-2, ios::end);
435 string lastChar = to_string(f.get());
436 f.close();
437 EXPECT_EQ(lastChar, "48");
438 } catch (...) {
439 EXPECT_TRUE(false);
440 GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
441 }
442 GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0603";
443 }
444
445 /**
446 * @tc.number: SUB_Ext_Extension_0700
447 * @tc.name: Ext_Extension_Test_0700
448 * @tc.desc: 测试file为空
449 * @tc.size: MEDIUM
450 * @tc.type: FUNC
451 * @tc.level Level 1
452 * @tc.require: I9P3Y3
453 */
454 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0700, testing::ext::TestSize.Level1)
455 {
456 GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0700";
457 try {
458 vector<struct ReportFileInfo> files;
459 TarMap result = GetIncrmentBigInfos(files);
460 EXPECT_TRUE(result.empty());
461 } catch (...) {
462 EXPECT_TRUE(false);
463 GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
464 }
465 GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0700";
466 }
467
468 /**
469 * @tc.number: SUB_Ext_Extension_0701
470 * @tc.name: Ext_Extension_Test_0701
471 * @tc.desc: 测试file只有一个元素
472 * @tc.size: MEDIUM
473 * @tc.type: FUNC
474 * @tc.level Level 1
475 * @tc.require: I9P3Y3
476 */
477 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0701, testing::ext::TestSize.Level1)
478 {
479 GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0701";
480 try {
481 vector<struct ReportFileInfo> srcFiles;
482 struct ReportFileInfo info;
483 info.filePath = PATH + BUNDLE_NAME;
484 info.mode = "755";
485 info.isDir = 0;
486 info.size = 1024;
487 info.mtime = 123456789;
488 info.hash = "1234567890";
489 info.userTar = 1;
490 info.encodeFlag = false;
491 srcFiles.push_back(info);
492 TarMap result = GetIncrmentBigInfos(srcFiles);
493 EXPECT_EQ(result.size(), 1);
494 } catch (...) {
495 EXPECT_TRUE(false);
496 GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
497 }
498 GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0701";
499 }
500
501 /**
502 * @tc.number: SUB_Ext_Extension_0702
503 * @tc.name: Ext_Extension_Test_0702
504 * @tc.desc: 测试file有重复元素
505 * @tc.size: MEDIUM
506 * @tc.type: FUNC
507 * @tc.level Level 1
508 * @tc.require: I9P3Y3
509 */
510 HWTEST_F(ExtExtensionTest, Ext_Extension_Test_0702, testing::ext::TestSize.Level1)
511 {
512 GTEST_LOG_(INFO) << "ExtExtensionTest-begin Ext_Extension_Test_0702";
513 try {
514 vector<struct ReportFileInfo> srcFiles;
515 struct ReportFileInfo info;
516 info.filePath = PATH + BUNDLE_NAME;
517 info.mode = "755";
518 info.isDir = 0;
519 info.size = 1024;
520 info.mtime = 123456789;
521 info.hash = "1234567890";
522 info.userTar = 1;
523 info.encodeFlag = false;
524 srcFiles.push_back(info);
525 srcFiles.push_back(info);
526 TarMap result = GetIncrmentBigInfos(srcFiles);
527 EXPECT_EQ(result.size(), 2);
528 } catch (...) {
529 EXPECT_TRUE(false);
530 GTEST_LOG_(INFO) << "ExtExtensionTest-an exception occurred by construction.";
531 }
532 GTEST_LOG_(INFO) << "ExtExtensionTest-end Ext_Extension_Test_0702";
533 }
534
535
536 } // namespace OHOS::FileManagement::Backup