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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18
19 #include "b_error/b_error.h"
20 #include "file_ex.h"
21 #include "test_manager.h"
22 #include "untar_file.h"
23
24 #include <cstdio>
25 #include <fstream>
26
27 namespace OHOS::FileManagement::Backup {
28 using namespace std;
29 using namespace testing;
30
31 class UntarFileTest : public testing::Test {
32 public:
33 static void SetUpTestCase(void);
34 static void TearDownTestCase();
SetUp()35 void SetUp() override {};
TearDown()36 void TearDown() override {};
37 tuple<string, string> TouchTestFile(int num, TestManager &tm);
38 };
39
SetUpTestCase()40 void UntarFileTest::SetUpTestCase()
41 {
42 GTEST_LOG_(INFO) << "SetUpTestCase enter";
43 }
44
TearDownTestCase()45 void UntarFileTest::TearDownTestCase()
46 {
47 GTEST_LOG_(INFO) << "TearDownTestCase enter";
48 }
49
ClearCache()50 static void ClearCache()
51 {
52 UntarFile::GetInstance().rootPath_.clear();
53 UntarFile::GetInstance().tarFileSize_ = 0;
54 UntarFile::GetInstance().tarFileBlockCnt_ = 0;
55 UntarFile::GetInstance().pos_ = 0;
56 UntarFile::GetInstance().readCnt_ = 0;
57 if (UntarFile::GetInstance().tarFilePtr_ != nullptr) {
58 fclose(UntarFile::GetInstance().tarFilePtr_);
59 UntarFile::GetInstance().tarFilePtr_ = nullptr;
60 }
61 TarFile::GetInstance().fileCount_ = 0;
62 TarFile::GetInstance().tarMap_.clear();
63 TarFile::GetInstance().rootPath_.clear();
64 TarFile::GetInstance().packagePath_.clear();
65 TarFile::GetInstance().baseTarName_.clear();
66 TarFile::GetInstance().tarFileName_.clear();
67 TarFile::GetInstance().ioBuffer_.clear();
68 TarFile::GetInstance().currentTarName_.clear();
69 TarFile::GetInstance().currentTarFileSize_ = 0;
70 TarFile::GetInstance().tarFileCount_ = 0;
71 TarFile::GetInstance().currentFileName_.clear();
72 if (TarFile::GetInstance().currentTarFile_ != nullptr) {
73 fclose(TarFile::GetInstance().currentTarFile_);
74 TarFile::GetInstance().currentTarFile_ = nullptr;
75 }
76 }
77
TouchTestFile(int num,TestManager & tm)78 tuple<string, string> UntarFileTest::TouchTestFile(int num, TestManager &tm)
79 {
80 string root = tm.GetRootDirCurTest();
81 string testDir = root + "testdir/";
82 if (mkdir(testDir.data(), S_IRWXU) && errno != EEXIST) {
83 GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno;
84 throw BError(errno);
85 }
86 string aFile = "";
87 string bFile = "";
88 for (int i = 0; i < num; i++) {
89 aFile += "test001/test002/test003/test004/test005/";
90 bFile += "ab";
91 }
92 bFile += ".txt";
93 aFile += bFile;
94
95 string pax = " path=";
96 size_t length = aFile.length() + pax.length();
97 length += std::to_string(length).length();
98 string str = std::to_string(length) + pax + aFile;
99
100 double result = (static_cast<double>(length) + OTHER_HEADER) / BLOCK_SIZE;
101 auto ret = static_cast<uint32_t>(std::ceil(result));
102 auto curSize = ret * BLOCK_SIZE;
103
104 std::vector<char> data(BLOCK_SIZE, '0');
105 std::vector<char> data2;
106 data2.reserve(curSize);
107 data2.insert(data2.end(), str.begin(), str.end());
108 for (size_t i = str.size(); i < curSize; ++i) {
109 data2.push_back(0);
110 }
111 string path = testDir + "test.tar";
112 std::ofstream file(path, std::ios::out | std::ios::trunc | std::ios::binary);
113 if (!file) {
114 throw BError(errno);
115 }
116 file.write(data.data(), data.size());
117 file.write(data2.data(), data2.size());
118 file.close();
119 return {path, aFile};
120 }
121
122 /**
123 * @tc.number: SUB_Untar_File_GetInstance_0100
124 * @tc.name: SUB_Untar_File_GetInstance_0100
125 * @tc.desc: 测试 GetInstance 接口
126 * @tc.size: MEDIUM
127 * @tc.type: FUNC
128 * @tc.level Level 1
129 * @tc.require: I6F3GV
130 */
131 HWTEST_F(UntarFileTest, SUB_Untar_File_GetInstance_0100, testing::ext::TestSize.Level1)
132 {
133 GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_GetInstance_0100";
134 try {
135 UntarFile::GetInstance();
136 EXPECT_TRUE(true);
137 } catch (...) {
138 EXPECT_TRUE(false);
139 GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by UntarFile.";
140 }
141 GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_GetInstance_0100";
142 }
143
144 /**
145 * @tc.number: SUB_Untar_File_UnPacket_0100
146 * @tc.name: SUB_Untar_File_UnPacket_0100
147 * @tc.desc: 测试 UnPacket 接口
148 * @tc.size: MEDIUM
149 * @tc.type: FUNC
150 * @tc.level Level 1
151 * @tc.require: I6F3GV
152 */
153 HWTEST_F(UntarFileTest, SUB_Untar_File_UnPacket_0100, testing::ext::TestSize.Level1)
154 {
155 GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_UnPacket_0100";
156 try {
157 string tarFile("");
158 string rootPath("");
159 auto [ret, fileInfos, errFileInfos] = UntarFile::GetInstance().UnPacket(tarFile, rootPath);
160 EXPECT_EQ(ret, ENOENT);
161 ClearCache();
162 } catch (...) {
163 EXPECT_TRUE(false);
164 GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by UntarFile.";
165 }
166 GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_UnPacket_0100";
167 }
168
169 /**
170 * @tc.number: SUB_Untar_File_UnPacket_0200
171 * @tc.name: SUB_Untar_File_UnPacket_0200
172 * @tc.desc: 测试 UnPacket 接口
173 * @tc.size: MEDIUM
174 * @tc.type: FUNC
175 * @tc.level Level 1
176 * @tc.require: I6F3GV
177 */
178 HWTEST_F(UntarFileTest, SUB_Untar_File_UnPacket_0200, testing::ext::TestSize.Level1)
179 {
180 GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_UnPacket_0200";
181 try {
182 // 预置文件和目录
183 TestManager tm("SUB_Untar_File_UnPacket_0200");
184 string root = tm.GetRootDirCurTest();
185 string testDir = root + "/testdir";
186 if (mkdir(testDir.data(), S_IRWXU) && errno != EEXIST) {
187 GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno;
188 throw BError(errno);
189 }
190 string aFile = testDir + "/a.txt";
191 string bFile = testDir + "/b.txt";
192 SaveStringToFile(aFile, "hello");
193 SaveStringToFile(bFile, "world");
194
195 string tarFile = root + "test.tar";
196 string cmd = "tar -cvf " + tarFile + " " + testDir;
197 string rootPath(root);
198 if (system(cmd.c_str()) != 0) {
199 GTEST_LOG_(INFO) << " execute tar failure, errno :" << errno;
200 throw BError(errno);
201 }
202 auto [ret, fileInfos, errFileInfos] = UntarFile::GetInstance().UnPacket(tarFile, rootPath);
203 EXPECT_EQ(ret, 0);
204 ClearCache();
205 } catch (...) {
206 EXPECT_TRUE(false);
207 GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by UntarFile.";
208 }
209 GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_UnPacket_0200";
210 }
211
212 /**
213 * @tc.number: SUB_Untar_File_UnPacket_0300
214 * @tc.name: SUB_Untar_File_UnPacket_0300
215 * @tc.desc: 测试 UnPacket 接口
216 * @tc.size: MEDIUM
217 * @tc.type: FUNC
218 * @tc.level Level 1
219 * @tc.require: I6F3GV
220 */
221 HWTEST_F(UntarFileTest, SUB_Untar_File_UnPacket_0300, testing::ext::TestSize.Level1)
222 {
223 GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_UnPacket_0300";
224 try {
225 // 预置文件和目录
226 TestManager tm("SUB_Untar_File_UnPacket_0300");
227 string root = tm.GetRootDirCurTest();
228 string testDir = root + "/testdir";
229 if (mkdir(testDir.data(), S_IRWXU) && errno != EEXIST) {
230 GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno;
231 throw BError(errno);
232 }
233 string aFile = testDir + "/a.txt";
234 SaveStringToFile(aFile, "hello");
235
236 string rootPath(root);
237 auto [ret, fileInfos, errFileInfos] = UntarFile::GetInstance().UnPacket(aFile, rootPath);
238 EXPECT_EQ(ret, 0);
239 ClearCache();
240 } catch (...) {
241 EXPECT_TRUE(false);
242 GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by UntarFile.";
243 }
244 GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_UnPacket_0300";
245 }
246
247 /**
248 * @tc.number: SUB_Untar_File_UnPacket_0400
249 * @tc.name: SUB_Untar_File_UnPacket_0400
250 * @tc.desc: 测试 UnPacket 接口
251 * @tc.size: MEDIUM
252 * @tc.type: FUNC
253 * @tc.level Level 1
254 * @tc.require: I6F3GV
255 */
256 HWTEST_F(UntarFileTest, SUB_Untar_File_UnPacket_0400, testing::ext::TestSize.Level1)
257 {
258 GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_UnPacket_0400";
259 try {
260 // 预置文件和目录
261 TestManager tm("SUB_Untar_File_UnPacket_0400");
262 string root = tm.GetRootDirCurTest();
263 string testDir = root + "/testdir";
264 if (mkdir(testDir.data(), S_IRWXU) && errno != EEXIST) {
265 GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno;
266 throw BError(errno);
267 }
268 string tarFile = root + "test.tar";
269 string cmd = "tar -cvf " + tarFile + " " + testDir;
270 if (system(cmd.c_str()) != 0) {
271 GTEST_LOG_(INFO) << " execute tar failure, errno :" << errno;
272 throw BError(errno);
273 }
274
275 string rootPath(root);
276 auto [ret, fileInfos, errFileInfos] = UntarFile::GetInstance().UnPacket(tarFile, rootPath);
277 EXPECT_EQ(ret, 0);
278 ClearCache();
279 } catch (...) {
280 EXPECT_TRUE(false);
281 GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by UntarFile.";
282 }
283 GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_UnPacket_0400";
284 }
285
286 /**
287 * @tc.number: SUB_Untar_File_UnPacket_0500
288 * @tc.name: SUB_Untar_File_UnPacket_0500
289 * @tc.desc: 测试 UnPacket 接口
290 * @tc.size: MEDIUM
291 * @tc.type: FUNC
292 * @tc.level Level 1
293 * @tc.require: I6F3GV
294 */
295 HWTEST_F(UntarFileTest, SUB_Untar_File_UnPacket_0500, testing::ext::TestSize.Level1)
296 {
297 GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_UnPacket_0500";
298 try {
299 // 预置文件和目录
300 TestManager tm("SUB_Untar_File_UnPacket_0500");
301 string root = tm.GetRootDirCurTest();
302 string testDir = root + "/testdir/";
303 if (mkdir(testDir.data(), S_IRWXU) && errno != EEXIST) {
304 GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno;
305 throw BError(errno);
306 }
307 string aFile = testDir;
308 string bFile = testDir;
309 // 循环100次,用来构造超长路径和超长文件名
310 for (int i = 0; i < 100; i++) {
311 aFile += "test001/test002/test003/test004/test005/";
312 bFile += "ab";
313 }
314 aFile += "a.txt";
315 bFile += ".txt";
316 SaveStringToFile(aFile, "hello");
317 SaveStringToFile(bFile, "world");
318
319 string tarFile = root + "/test.0.tar";
320 TarMap tarMap {};
321 vector<string> smallFiles;
322 smallFiles.emplace_back(aFile);
323 smallFiles.emplace_back(bFile);
__anon40c80aec0102(std::string msg, int err) 324 auto reportCb = [](std::string msg, int err) {
325 return;
326 };
327 TarFile::GetInstance().Packet(smallFiles, "test", root, tarMap, reportCb);
328
329 string rootPath(root);
330 auto [ret, fileInfos, errFileInfos] = UntarFile::GetInstance().UnPacket(tarFile, rootPath);
331 EXPECT_EQ(ret, 0);
332 ClearCache();
333 } catch (...) {
334 EXPECT_TRUE(false);
335 GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by UntarFile.";
336 }
337 GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_UnPacket_0500";
338 }
339
340 /**
341 * @tc.number: SUB_Untar_File_IncrementalUnPacket_0100
342 * @tc.name: SUB_Untar_File_IncrementalUnPacket_0100
343 * @tc.desc: 测试 IncrementalUnPacket 接口
344 * @tc.size: MEDIUM
345 * @tc.type: FUNC
346 * @tc.level Level 1
347 * @tc.require: I6F3GV
348 */
349 HWTEST_F(UntarFileTest, SUB_Untar_File_IncrementalUnPacket_0100, testing::ext::TestSize.Level1)
350 {
351 GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_IncrementalUnPacket_0100";
352 try {
353 // 预置文件和目录
354 TestManager tm("SUB_Untar_File_IncrementalUnPacket_0100");
355 string root = tm.GetRootDirCurTest();
356 string testDir = root + "/testdir/";
357 if (mkdir(testDir.data(), S_IRWXU) && errno != EEXIST) {
358 GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno;
359 throw BError(errno);
360 }
361 string aFile = testDir;
362 string bFile = testDir;
363 // 循环100次,用来构造超长路径和超长文件名
364 for (int i = 0; i < 100; i++) {
365 aFile += "test001/test002/test003/test004/test005/";
366 bFile += "ab";
367 }
368 aFile += "a.txt";
369 bFile += ".txt";
370 SaveStringToFile(aFile, "hello");
371 SaveStringToFile(bFile, "world");
372
373 TarMap tarMap {};
374 vector<string> smallFiles;
375 smallFiles.emplace_back(aFile);
376 smallFiles.emplace_back(bFile);
__anon40c80aec0202(std::string msg, int err) 377 auto reportCb = [](std::string msg, int err) {
378 return;
379 };
380 TarFile::GetInstance().Packet(smallFiles, "test", root, tarMap, reportCb);
381
382 string tarFile = root + "/test.0.tar";
383 string rootPath(root);
384 unordered_map<string, struct ReportFileInfo> cloudFiles;
385 auto [ret, fileInfos, errFileInfos] =
386 UntarFile::GetInstance().IncrementalUnPacket(tarFile, rootPath, cloudFiles);
387 EXPECT_EQ(ret, 0);
388 ClearCache();
389 } catch (...) {
390 EXPECT_TRUE(false);
391 GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by UntarFile.";
392 }
393 GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_IncrementalUnPacket_0100";
394 }
395
396 /**
397 * @tc.number: SUB_Untar_File_IncrementalUnPacket_0200
398 * @tc.name: SUB_Untar_File_IncrementalUnPacket_0200
399 * @tc.desc: 测试 IncrementalUnPacket 接口
400 * @tc.size: MEDIUM
401 * @tc.type: FUNC
402 * @tc.level Level 1
403 * @tc.require: I6F3GV
404 */
405 HWTEST_F(UntarFileTest, SUB_Untar_File_IncrementalUnPacket_0200, testing::ext::TestSize.Level1)
406 {
407 GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_IncrementalUnPacket_0200";
408 try {
409 // 预置文件和目录
410 TestManager tm("SUB_Untar_File_IncrementalUnPacket_0200");
411 string root = tm.GetRootDirCurTest();
412 // 构造tarFile不存在
413 string tarFile = root + "/empty.0.tar";
414 string rootPath(root);
415 unordered_map<string, struct ReportFileInfo> cloudFiles;
416 auto [ret, fileInfos, errFileInfos] =
417 UntarFile::GetInstance().IncrementalUnPacket(tarFile, rootPath, cloudFiles);
418 EXPECT_EQ(ret, 2); // 错误码2表示找不到文件或路径
419 ClearCache();
420 } catch (...) {
421 EXPECT_TRUE(false);
422 GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by UntarFile.";
423 }
424 GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_IncrementalUnPacket_0200";
425 }
426
427 /**
428 * @tc.number: SUB_Untar_File_IncrementalUnPacket_0300
429 * @tc.name: SUB_Untar_File_IncrementalUnPacket_0300
430 * @tc.desc: 测试 IncrementalUnPacket 接口
431 * @tc.size: MEDIUM
432 * @tc.type: FUNC
433 * @tc.level Level 1
434 * @tc.require: I6F3GV
435 */
436 HWTEST_F(UntarFileTest, SUB_Untar_File_IncrementalUnPacket_0300, testing::ext::TestSize.Level1)
437 {
438 GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_IncrementalUnPacket_0300";
439 try {
440 // 预置文件和目录
441 TestManager tm("SUB_Untar_File_IncrementalUnPacket_0300");
442 string root = tm.GetRootDirCurTest();
443 string testDir = root + "/testdir/";
444 if (mkdir(testDir.data(), S_IRWXU) && errno != EEXIST) {
445 GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno;
446 throw BError(errno);
447 }
448 string aFile = "a.txt";
449 SaveStringToFile(aFile, "hello");
450
451 TarMap tarMap {};
452 vector<string> smallFiles;
453 smallFiles.emplace_back(aFile);
__anon40c80aec0302(std::string msg, int err) 454 auto reportCb = [](std::string msg, int err) {
455 return;
456 };
457 TarFile::GetInstance().Packet(smallFiles, "test", root, tarMap, reportCb);
458
459 string tarFile = root + "/test.0.tar";
460 string rootPath(root);
461 unordered_map<string, struct ReportFileInfo> cloudFiles;
462 // 构造文件标头为空
463 FILE *currentTarFile = fopen(tarFile.c_str(), "wb+");
464 fwrite("\0", sizeof(uint8_t), 1, currentTarFile);
465 fclose(currentTarFile);
466 auto [ret, fileInfos, errFileInfos] =
467 UntarFile::GetInstance().IncrementalUnPacket(tarFile, rootPath, cloudFiles);
468 EXPECT_EQ(ret, 0);
469 ClearCache();
470 } catch (...) {
471 EXPECT_TRUE(false);
472 GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by UntarFile.";
473 }
474 GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_IncrementalUnPacket_0300";
475 }
476
477 /**
478 * @tc.number: SUB_Untar_File_IncrementalUnPacket_0400
479 * @tc.name: SUB_Untar_File_IncrementalUnPacket_0400
480 * @tc.desc: 测试 IncrementalUnPacket 接口
481 * @tc.size: MEDIUM
482 * @tc.type: FUNC
483 * @tc.level Level 1
484 * @tc.require: I6F3GV
485 */
486 HWTEST_F(UntarFileTest, SUB_Untar_File_IncrementalUnPacket_0400, testing::ext::TestSize.Level1)
487 {
488 GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_IncrementalUnPacket_0400";
489 try {
490 // 预置文件和目录
491 TestManager tm("SUB_Untar_File_IncrementalUnPacket_0400");
492 string root = tm.GetRootDirCurTest();
493 string testDir = root + "/testdir/";
494 if (mkdir(testDir.data(), S_IRWXU) && errno != EEXIST) {
495 GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno;
496 throw BError(errno);
497 }
498 string aFile = testDir;
499 string bFile = testDir;
500 // 循环100次,用来构造超长路径和超长文件名
501 for (int i = 0; i < 100; i++) {
502 aFile += "test001/test002/test003/test004/test005/";
503 bFile += "ab";
504 }
505 aFile += "a.txt";
506 bFile += ".txt";
507 SaveStringToFile(aFile, "hello");
508 SaveStringToFile(bFile, "world");
509
510 TarMap tarMap {};
511 vector<string> smallFiles;
512 smallFiles.emplace_back(aFile);
513 smallFiles.emplace_back(bFile);
__anon40c80aec0402(std::string msg, int err) 514 auto reportCb = [](std::string msg, int err) {
515 return;
516 };
517 TarFile::GetInstance().Packet(smallFiles, "test", root, tarMap, reportCb);
518
519 string tarFile = root + "/test.0.tar";
520 string rootPath(root);
521 unordered_map<string, struct ReportFileInfo> cloudFiles;
522 // 构造文件标头为空
523 FILE *currentTarFile = fopen(tarFile.c_str(), "wb+");
524 fseeko(currentTarFile, 1L, SEEK_SET);
525 fwrite("\0", sizeof(uint8_t), 1, currentTarFile);
526 fclose(currentTarFile);
527 auto [ret, fileInfos, errFileInfos] =
528 UntarFile::GetInstance().IncrementalUnPacket(tarFile, rootPath, cloudFiles);
529 EXPECT_EQ(ret, 0);
530 ClearCache();
531 } catch (...) {
532 EXPECT_TRUE(false);
533 GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by UntarFile.";
534 }
535 GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_IncrementalUnPacket_0400";
536 }
537
538
539 /**
540 * @tc.number: SUB_Untar_File_IncrementalUnPacket_0500
541 * @tc.name: SUB_Untar_File_IncrementalUnPacket_0500
542 * @tc.desc: 测试 IncrementalUnPacket 接口
543 * @tc.size: MEDIUM
544 * @tc.type: FUNC
545 * @tc.level Level 1
546 * @tc.require: I6F3GV
547 */
548 HWTEST_F(UntarFileTest, SUB_Untar_File_IncrementalUnPacket_0500, testing::ext::TestSize.Level1)
549 {
550 GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_IncrementalUnPacket_0500";
551 try {
552 // 预置文件和目录
553 TestManager tm("SUB_Untar_File_IncrementalUnPacket_0500");
554 string root = tm.GetRootDirCurTest();
555 string testDir = root + "/testdir/";
556 if (mkdir(testDir.data(), S_IRWXU) && errno != EEXIST) {
557 GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno;
558 throw BError(errno);
559 }
560 string aFile = testDir + "a.txt";
561 string bFile = testDir + "b.txt";
562 SaveStringToFile(aFile, "hello");
563 SaveStringToFile(bFile, "world");
564 // 打 tar
565 TarMap tarMap {};
566 vector<string> smallFiles;
567 smallFiles.emplace_back(aFile);
568 smallFiles.emplace_back(bFile);
__anon40c80aec0502(std::string msg, int err) 569 auto reportCb = [](std::string msg, int err) {
570 return;
571 };
572 TarFile::GetInstance().Packet(smallFiles, "test", root, tarMap, reportCb);
573 // 首次恢复
574 string tarFile = root + "/test.0.tar";
575 string rootPath(root);
576 unordered_map<string, struct ReportFileInfo> cloudFiles;
577 cloudFiles.emplace(make_pair(aFile.substr(1), ReportFileInfo())); // 1: the pos of /
578 cloudFiles.emplace(make_pair(bFile.substr(1), ReportFileInfo())); // 1: the pos of /
579 auto result = UntarFile::GetInstance().IncrementalUnPacket(tarFile, rootPath, cloudFiles);
580 EXPECT_EQ(std::get<1>(result).size(), 2); // 1: First ele, 2: 2 files, aFile and bFile
581 EXPECT_EQ(std::get<0>(result), 0);
582 // 增量恢复,aFile 有变化
583 cloudFiles.erase(aFile.substr(1)); // 1: the pos of /
584 result = UntarFile::GetInstance().IncrementalUnPacket(tarFile, rootPath, cloudFiles);
585 EXPECT_EQ(std::get<0>(result), 0);
586 EXPECT_EQ(std::get<1>(result).size(), 1); // 1: the first ele, 1: result size -> bFile
587 // 恢复归一后 clone等其他场景,rp文件没数据
588 cloudFiles.erase(bFile.substr(1)); // 1: the pos of /
589 result = UntarFile::GetInstance().IncrementalUnPacket(tarFile, rootPath, cloudFiles);
590 EXPECT_EQ(std::get<0>(result), 0);
591 EXPECT_EQ(std::get<1>(result).size(), 2); // 1: First ele, 2: 2 files, aFile and bFile
592 ClearCache();
593 } catch (...) {
594 EXPECT_TRUE(false);
595 GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by UntarFile.";
596 }
597 GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_IncrementalUnPacket_0500";
598 }
599 /**
600 * @tc.number: SUB_Untar_File_CheckLongName_0100
601 * @tc.name: SUB_Untar_File_CheckLongName_0100
602 * @tc.desc: 测试 CheckLongName 接口
603 * @tc.size: MEDIUM
604 * @tc.type: FUNC
605 * @tc.level Level 1
606 * @tc.require: I6F3GV
607 */
608 HWTEST_F(UntarFileTest, SUB_Untar_File_CheckLongName_0100, testing::ext::TestSize.Level1)
609 {
610 GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_CheckLongName_0100";
611 try {
612 string longName = "";
613 FileStatInfo info;
614 UntarFile::GetInstance().CheckLongName(longName, info);
615 EXPECT_TRUE(info.longName.empty());
616
617 longName = "test\n";
618 UntarFile::GetInstance().CheckLongName(longName, info);
619 EXPECT_TRUE(info.longName == "test");
620
621 longName = "test\n";
622 UntarFile::GetInstance().CheckLongName(longName, info);
623 EXPECT_TRUE(info.longName == "test");
624 } catch (...) {
625 EXPECT_TRUE(false);
626 GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by CheckLongName.";
627 }
628 GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_CheckLongName_0100";
629 }
630
631 /**
632 * @tc.number: SUB_Untar_File_ParsePaxBlock_0100
633 * @tc.name: SUB_Untar_File_ParsePaxBlock_0100
634 * @tc.desc: 测试 ParsePaxBlock 接口
635 * @tc.size: MEDIUM
636 * @tc.type: FUNC
637 * @tc.level Level 1
638 * @tc.require: I6F3GV
639 */
640 HWTEST_F(UntarFileTest, SUB_Untar_File_ParsePaxBlock_0100, testing::ext::TestSize.Level1)
641 {
642 GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_ParsePaxBlock_0100";
643 try {
644 TestManager tm("SUB_Untar_File_ParsePaxBlock_0100");
645 string root = tm.GetRootDirCurTest();
646 string testDir = root + "testdir/";
647 if (mkdir(testDir.data(), S_IRWXU) && errno != EEXIST) {
648 GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno;
649 throw BError(errno);
650 }
651 std::vector<char> data(BLOCK_SIZE, '0');
652 std::vector<char> data2;
653 data2.reserve(BLOCK_SIZE);
654 string str = "20 atime=1739170159\n";
655 data2.insert(data2.end(), str.begin(), str.end());
656 for (size_t i = str.size(); i < BLOCK_SIZE; ++i) {
657 data2.push_back(0);
658 }
659 string path = testDir + "test.tar";
660 std::ofstream file(path, std::ios::out | std::ios::trunc | std::ios::binary);
661 if (!file) {
662 throw BError(errno);
663 }
664 file.write(data.data(), data.size());
665 file.write(data2.data(), data2.size());
666 file.close();
667 UntarFile::GetInstance().tarFilePtr_ = fopen(path.c_str(), "rb");
668 if (UntarFile::GetInstance().tarFilePtr_ == nullptr) {
669 throw BError(errno);
670 }
671 char buff[BLOCK_SIZE] = {0};
672 auto readCnt = fread(buff, 1, BLOCK_SIZE, UntarFile::GetInstance().tarFilePtr_);
673 if (readCnt < BLOCK_SIZE) {
674 throw BError(errno);
675 }
676 auto res = UntarFile::GetInstance().ParsePaxBlock();
677 fclose(UntarFile::GetInstance().tarFilePtr_);
678 UntarFile::GetInstance().tarFilePtr_ =nullptr;
679 EXPECT_EQ(std::get<FIRST_PARAM>(res), 0);
680 EXPECT_TRUE(std::get<SECOND_PARAM>(res) == "");
681 ClearCache();
682 } catch (...) {
683 EXPECT_TRUE(false);
684 GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by ParsePaxBlock.";
685 }
686 GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_ParsePaxBlock_0100";
687 }
688
689 /**
690 * @tc.number: SUB_Untar_File_ParsePaxBlock_0200
691 * @tc.name: SUB_Untar_File_ParsePaxBlock_0200
692 * @tc.desc: 测试 ParsePaxBlock 接口
693 * @tc.size: MEDIUM
694 * @tc.type: FUNC
695 * @tc.level Level 1
696 * @tc.require: I6F3GV
697 */
698 HWTEST_F(UntarFileTest, SUB_Untar_File_ParsePaxBlock_0200, testing::ext::TestSize.Level1)
699 {
700 GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_ParsePaxBlock_0200";
701 try {
702 TestManager tm("SUB_Untar_File_ParsePaxBlock_0200");
703 int num = 30;
704 auto [path, fileName] = TouchTestFile(num, tm);
705
706 UntarFile::GetInstance().tarFilePtr_ = fopen(path.c_str(), "rb");
707 if (UntarFile::GetInstance().tarFilePtr_ == nullptr) {
708 throw BError(errno);
709 }
710 char buff[BLOCK_SIZE] = {0};
711 auto readCnt = fread(buff, 1, BLOCK_SIZE, UntarFile::GetInstance().tarFilePtr_);
712 if (readCnt < BLOCK_SIZE) {
713 throw BError(errno);
714 }
715 auto [res, longName] = UntarFile::GetInstance().ParsePaxBlock();
716 fclose(UntarFile::GetInstance().tarFilePtr_);
717 UntarFile::GetInstance().tarFilePtr_ =nullptr;
718 EXPECT_EQ(res, 0);
719 EXPECT_TRUE(longName == fileName);
720 ClearCache();
721 } catch (...) {
722 EXPECT_TRUE(false);
723 GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by ParsePaxBlock.";
724 }
725 GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_ParsePaxBlock_0200";
726 }
727
728 /**
729 * @tc.number: SUB_Untar_File_ParsePaxBlock_0300
730 * @tc.name: SUB_Untar_File_ParsePaxBlock_0300
731 * @tc.desc: 测试 ParsePaxBlock 接口
732 * @tc.size: MEDIUM
733 * @tc.type: FUNC
734 * @tc.level Level 1
735 * @tc.require: I6F3GV
736 */
737 HWTEST_F(UntarFileTest, SUB_Untar_File_ParsePaxBlock_0300, testing::ext::TestSize.Level1)
738 {
739 GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_ParsePaxBlock_0300";
740 try {
741 TestManager tm("SUB_Untar_File_ParsePaxBlock_0300");
742 int num = 1;
743 auto [path, fileName] = TouchTestFile(num, tm);
744
745 UntarFile::GetInstance().tarFilePtr_ = fopen(path.c_str(), "rb");
746 if (UntarFile::GetInstance().tarFilePtr_ == nullptr) {
747 throw BError(errno);
748 }
749 char buff[BLOCK_SIZE] = {0};
750 auto readCnt = fread(buff, 1, BLOCK_SIZE, UntarFile::GetInstance().tarFilePtr_);
751 if (readCnt < BLOCK_SIZE) {
752 throw BError(errno);
753 }
754 auto [res, longName] = UntarFile::GetInstance().ParsePaxBlock();
755 fclose(UntarFile::GetInstance().tarFilePtr_);
756 UntarFile::GetInstance().tarFilePtr_ =nullptr;
757 EXPECT_EQ(res, 0);
758 EXPECT_TRUE(longName == fileName);
759 ClearCache();
760 } catch (...) {
761 EXPECT_TRUE(false);
762 GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by ParsePaxBlock.";
763 }
764 GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_ParsePaxBlock_0300";
765 }
766 } // namespace OHOS::FileManagement::Backup