diff --git a/frameworks/native/backup_ext/include/ext_extension.h b/frameworks/native/backup_ext/include/ext_extension.h index 8caec03dfef4dc5b396d00df4af20bd841d76c8b..0f49608816b285497d50b9b138bcce0336f237b6 100644 --- a/frameworks/native/backup_ext/include/ext_extension.h +++ b/frameworks/native/backup_ext/include/ext_extension.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/native/backup_ext/include/tar_file.h b/frameworks/native/backup_ext/include/tar_file.h index 680401b99bf7a89e68e70faa2bb951c6ffe59460..ce8afbaa81e0fa92e5a49a1abfeff36efbd02e16 100644 --- a/frameworks/native/backup_ext/include/tar_file.h +++ b/frameworks/native/backup_ext/include/tar_file.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/native/backup_ext/include/untar_file.h b/frameworks/native/backup_ext/include/untar_file.h index bf8861282dec3449315361ac5a75cdc99d51bb50..49eb36e2620430df21d52040d89a495a2f609554 100644 --- a/frameworks/native/backup_ext/include/untar_file.h +++ b/frameworks/native/backup_ext/include/untar_file.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index b83bec5aa6c0980c357489cb0bfbc58775694a71..0fa51bd065e5d67a88f1feeab56cad0d868738d2 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/native/backup_ext/src/sub_ext_extension.cpp b/frameworks/native/backup_ext/src/sub_ext_extension.cpp index 08c511353e899727c37bdd2db1e01748b02a5546..afcaa1a108d2122a7e80efe1d6a460c1efaf1624 100644 --- a/frameworks/native/backup_ext/src/sub_ext_extension.cpp +++ b/frameworks/native/backup_ext/src/sub_ext_extension.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/native/backup_ext/src/untar_file.cpp b/frameworks/native/backup_ext/src/untar_file.cpp index e262175892a3886dbb0a01ef192b00317a16e4a5..f78ce3c9dc93679ea0ba7f08178d4e7912ff74e0 100644 --- a/frameworks/native/backup_ext/src/untar_file.cpp +++ b/frameworks/native/backup_ext/src/untar_file.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/tests/unittests/backup_ext/untar_file_sup_test.cpp b/tests/unittests/backup_ext/untar_file_sup_test.cpp index 94231441308e80b4cef3e85911aa00191e6957e3..7a6f0dfe2f6bf878776d20c77c86b80a1b171fec 100644 --- a/tests/unittests/backup_ext/untar_file_sup_test.cpp +++ b/tests/unittests/backup_ext/untar_file_sup_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/tests/unittests/backup_ext/untar_file_test.cpp b/tests/unittests/backup_ext/untar_file_test.cpp index 79082c8cdba6b99ff9b5979023ec924d7b0d6cb8..f1016f652bf89ce23e035e25ad7684c0baf4ade9 100644 --- a/tests/unittests/backup_ext/untar_file_test.cpp +++ b/tests/unittests/backup_ext/untar_file_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -21,6 +21,9 @@ #include "test_manager.h" #include "untar_file.h" +#include +#include + namespace OHOS::FileManagement::Backup { using namespace std; using namespace testing; @@ -31,6 +34,7 @@ public: static void TearDownTestCase(); void SetUp() override {}; void TearDown() override {}; + tuple TouchTestFile(int num, TestManager &tm); }; void UntarFileTest::SetUpTestCase() @@ -71,6 +75,50 @@ static void ClearCache() } } +tuple UntarFileTest::TouchTestFile(int num, TestManager &tm) +{ + string root = tm.GetRootDirCurTest(); + string testDir = root + "testdir/"; + if (mkdir(testDir.data(), S_IRWXU) && errno != EEXIST) { + GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno; + throw BError(errno); + } + string aFile = ""; + string bFile = ""; + for (int i = 0; i < num; i++) { + aFile += "test001/test002/test003/test004/test005/"; + bFile += "ab"; + } + bFile += ".txt"; + aFile += bFile; + + string pax = " path="; + size_t length = aFile.length() + pax.length(); + length += std::to_string(length).length(); + string str = std::to_string(length) + pax + aFile; + + double result = (static_cast(length) + OTHER_HEADER) / BLOCK_SIZE; + auto ret = static_cast(std::ceil(result)); + auto curSize = ret * BLOCK_SIZE; + + std::vector data(BLOCK_SIZE, '0'); + std::vector data2; + data2.reserve(curSize); + data2.insert(data2.end(), str.begin(), str.end()); + for (size_t i = str.size(); i < curSize; ++i) { + data2.push_back(0); + } + string path = testDir + "test.tar"; + std::ofstream file(path, std::ios::out | std::ios::trunc | std::ios::binary); + if (!file) { + throw BError(errno); + } + file.write(data.data(), data.size()); + file.write(data2.data(), data2.size()); + file.close(); + return {path, aFile}; +} + /** * @tc.number: SUB_Untar_File_GetInstance_0100 * @tc.name: SUB_Untar_File_GetInstance_0100 @@ -548,4 +596,171 @@ HWTEST_F(UntarFileTest, SUB_Untar_File_IncrementalUnPacket_0500, testing::ext::T } GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_IncrementalUnPacket_0500"; } +/** + * @tc.number: SUB_Untar_File_CheckLongName_0100 + * @tc.name: SUB_Untar_File_CheckLongName_0100 + * @tc.desc: 测试 CheckLongName 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(UntarFileTest, SUB_Untar_File_CheckLongName_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_CheckLongName_0100"; + try { + string longName = ""; + FileStatInfo info; + UntarFile::GetInstance().CheckLongName(longName, info); + EXPECT_TRUE(info.longName.empty()); + + longName = "test\n"; + UntarFile::GetInstance().CheckLongName(longName, info); + EXPECT_TRUE(info.longName == "test"); + + longName = "test\n"; + UntarFile::GetInstance().CheckLongName(longName, info); + EXPECT_TRUE(info.longName == "test"); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by CheckLongName."; + } + GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_CheckLongName_0100"; +} + +/** + * @tc.number: SUB_Untar_File_ParsePaxBlock_0100 + * @tc.name: SUB_Untar_File_ParsePaxBlock_0100 + * @tc.desc: 测试 ParsePaxBlock 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(UntarFileTest, SUB_Untar_File_ParsePaxBlock_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_ParsePaxBlock_0100"; + try { + TestManager tm("SUB_Untar_File_ParsePaxBlock_0100"); + string root = tm.GetRootDirCurTest(); + string testDir = root + "testdir/"; + if (mkdir(testDir.data(), S_IRWXU) && errno != EEXIST) { + GTEST_LOG_(INFO) << " invoked mkdir failure, errno :" << errno; + throw BError(errno); + } + std::vector data(BLOCK_SIZE, '0'); + std::vector data2; + data2.reserve(BLOCK_SIZE); + string str = "20 atime=1739170159\n"; + data2.insert(data2.end(), str.begin(), str.end()); + for (size_t i = str.size(); i < BLOCK_SIZE; ++i) { + data2.push_back(0); + } + string path = testDir + "test.tar"; + std::ofstream file(path, std::ios::out | std::ios::trunc | std::ios::binary); + if (!file) { + throw BError(errno); + } + file.write(data.data(), data.size()); + file.write(data2.data(), data2.size()); + file.close(); + UntarFile::GetInstance().tarFilePtr_ = fopen(path.c_str(), "rb"); + if (UntarFile::GetInstance().tarFilePtr_ == nullptr) { + throw BError(errno); + } + char buff[BLOCK_SIZE] = {0}; + auto readCnt = fread(buff, 1, BLOCK_SIZE, UntarFile::GetInstance().tarFilePtr_); + if (readCnt < BLOCK_SIZE) { + throw BError(errno); + } + auto res = UntarFile::GetInstance().ParsePaxBlock(); + fclose(UntarFile::GetInstance().tarFilePtr_); + UntarFile::GetInstance().tarFilePtr_ =nullptr; + EXPECT_EQ(std::get(res), 0); + EXPECT_TRUE(std::get(res) == ""); + ClearCache(); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by ParsePaxBlock."; + } + GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_ParsePaxBlock_0100"; +} + +/** + * @tc.number: SUB_Untar_File_ParsePaxBlock_0200 + * @tc.name: SUB_Untar_File_ParsePaxBlock_0200 + * @tc.desc: 测试 ParsePaxBlock 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(UntarFileTest, SUB_Untar_File_ParsePaxBlock_0200, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_ParsePaxBlock_0200"; + try { + TestManager tm("SUB_Untar_File_ParsePaxBlock_0200"); + int num = 30; + auto [path, fileName] = TouchTestFile(num, tm); + + UntarFile::GetInstance().tarFilePtr_ = fopen(path.c_str(), "rb"); + if (UntarFile::GetInstance().tarFilePtr_ == nullptr) { + throw BError(errno); + } + char buff[BLOCK_SIZE] = {0}; + auto readCnt = fread(buff, 1, BLOCK_SIZE, UntarFile::GetInstance().tarFilePtr_); + if (readCnt < BLOCK_SIZE) { + throw BError(errno); + } + auto [res, longName] = UntarFile::GetInstance().ParsePaxBlock(); + fclose(UntarFile::GetInstance().tarFilePtr_); + UntarFile::GetInstance().tarFilePtr_ =nullptr; + EXPECT_EQ(res, 0); + EXPECT_TRUE(longName == fileName); + ClearCache(); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by ParsePaxBlock."; + } + GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_ParsePaxBlock_0200"; +} + +/** + * @tc.number: SUB_Untar_File_ParsePaxBlock_0300 + * @tc.name: SUB_Untar_File_ParsePaxBlock_0300 + * @tc.desc: 测试 ParsePaxBlock 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(UntarFileTest, SUB_Untar_File_ParsePaxBlock_0300, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_ParsePaxBlock_0300"; + try { + TestManager tm("SUB_Untar_File_ParsePaxBlock_0300"); + int num = 1; + auto [path, fileName] = TouchTestFile(num, tm); + + UntarFile::GetInstance().tarFilePtr_ = fopen(path.c_str(), "rb"); + if (UntarFile::GetInstance().tarFilePtr_ == nullptr) { + throw BError(errno); + } + char buff[BLOCK_SIZE] = {0}; + auto readCnt = fread(buff, 1, BLOCK_SIZE, UntarFile::GetInstance().tarFilePtr_); + if (readCnt < BLOCK_SIZE) { + throw BError(errno); + } + auto [res, longName] = UntarFile::GetInstance().ParsePaxBlock(); + fclose(UntarFile::GetInstance().tarFilePtr_); + UntarFile::GetInstance().tarFilePtr_ =nullptr; + EXPECT_EQ(res, 0); + EXPECT_TRUE(longName == fileName); + ClearCache(); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by ParsePaxBlock."; + } + GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_ParsePaxBlock_0300"; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file