From 3f34ad4626b79fa167ec0b59664b780a789a69f2 Mon Sep 17 00:00:00 2001 From: zhuruigan Date: Sun, 4 Feb 2024 16:16:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0utils=E7=9A=84TDD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhuruigan Change-Id: I634bfecafab48a1389525e9e9c6a03a17d61c655 --- tests/unittests/backup_utils/BUILD.gn | 2 + .../b_filesystem/b_file_hash_test.cpp | 77 +++++++++++++ .../b_json/b_report_entity_test.cpp | 102 ++++++++++++++++++ 3 files changed, 181 insertions(+) create mode 100644 tests/unittests/backup_utils/b_filesystem/b_file_hash_test.cpp create mode 100644 tests/unittests/backup_utils/b_json/b_report_entity_test.cpp diff --git a/tests/unittests/backup_utils/BUILD.gn b/tests/unittests/backup_utils/BUILD.gn index 7e2e8eb4c..5061c14f5 100644 --- a/tests/unittests/backup_utils/BUILD.gn +++ b/tests/unittests/backup_utils/BUILD.gn @@ -34,6 +34,7 @@ ohos_unittest("b_file_test") { sources = [ "b_filesystem/b_dir_test.cpp", + "b_filesystem/b_file_hash_test.cpp", "b_filesystem/b_file_test.cpp", ] @@ -59,6 +60,7 @@ ohos_unittest("b_json_test") { "b_json/b_json_cached_entity_test.cpp", "b_json/b_json_entity_ext_manage_test.cpp", "b_json/b_json_entity_extension_config_test.cpp", + "b_json/b_report_entity_test.cpp", ] sources += backup_mock_parameter_src diff --git a/tests/unittests/backup_utils/b_filesystem/b_file_hash_test.cpp b/tests/unittests/backup_utils/b_filesystem/b_file_hash_test.cpp new file mode 100644 index 000000000..7e369bd3a --- /dev/null +++ b/tests/unittests/backup_utils/b_filesystem/b_file_hash_test.cpp @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2024 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include +#include +#include + +#include "b_filesystem/b_file_hash.h" +#include "test_manager.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; + +class BFileHashTest : public testing::Test { +public: + static void SetUpTestCase(void) {}; + static void TearDownTestCase() {}; + void SetUp() {}; + void TearDown() {}; +}; + +/** + * @brief 创建测试文件 + * + * @return tuple 创建结果、文件路径、文件内容 + */ +static tuple GetTestFile(const TestManager &tm) +{ + string path = tm.GetRootDirCurTest(); + string filePath = path + "temp.txt"; + string content = "backup test"; + if (bool contentCreate = SaveStringToFile(filePath, content, true); !contentCreate) { + throw system_error(errno, system_category()); + } + return {filePath, content}; +} + +/** + * @tc.number: SUB_backup_b_file_hash_HashWithSHA256_0100 + * @tc.name: b_file_hash_HashWithSHA256_0100 + * @tc.desc: Test function of HashWithSHA256 interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(BFileTest, b_file_hash_HashWithSHA256_0100, testing::ext::TestSize.Level0) +{ + GTEST_LOG_(INFO) << "BFileHashTest-begin b_file_hash_HashWithSHA256_0100"; + try { + TestManager tm(__func__); + const auto [filePath, content] = GetTestFile(tm); + + auto [res, fileHash] = BFileHash::HashWithSHA256(filePath); + + EXPECT_EQ(res, 0); + } catch (const exception &e) { + GTEST_LOG_(INFO) << "BFileHashTest-an exception occurred by HashWithSHA256."; + e.what(); + } + GTEST_LOG_(INFO) << "BFileHashTest-end b_file_hash_HashWithSHA256_0100"; +} +} // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/unittests/backup_utils/b_json/b_report_entity_test.cpp b/tests/unittests/backup_utils/b_json/b_report_entity_test.cpp new file mode 100644 index 000000000..d5aae32a8 --- /dev/null +++ b/tests/unittests/backup_utils/b_json/b_report_entity_test.cpp @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2024 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "b_json/b_report_entity.h" +#include "test_manager.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; + +class BReportEntityTest : public testing::Test { +public: + static void SetUpTestCase(void) {}; + static void TearDownTestCase() {}; + void SetUp() {}; + void TearDown() {}; +}; + +/** + * @brief 创建测试文件 + * + * @return tuple 创建结果、文件路径、文件内容 + */ +static tuple GetTestFile(const TestManager &tm, const string content) +{ + string path = tm.GetRootDirCurTest(); + string filePath = path + "temp.txt"; + + if (bool contentCreate = SaveStringToFile(filePath, content, true); !contentCreate) { + throw system_error(errno, system_category()); + } + return {filePath, content}; +} + +/** + * @tc.number: SUB_backup_b_report_entity_GetReportInfos_0100 + * @tc.name: b_report_entity_GetReportInfos_0100 + * @tc.desc: Test function of GetReportInfos interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(BFileTest, b_report_entity_GetReportInfos_0100, testing::ext::TestSize.Level0) +{ + GTEST_LOG_(INFO) << "BReportEntityTest-begin b_report_entity_GetReportInfos_0100"; + try { + string fileName = "/a.txt"; + string mode = "0644"; + string isDir = "0"; + string size = "1"; + string mtime = "1501927260"; + string hash = "ASDasadSDASDA"; + + string content = "version=1.0&attrNum=6\r\npath;mode;dir;size;mtime;hash\r\n"; + content += fileName + ";" + mode + ";" + isDir + ";" + size + ";" + mtime + ";" + hash; + TestManager tm(__func__); + const auto [filePath, content] = GetTestFile(tm, content); + + BReportEntity cloudRp(UniqueFd(open(filePath.data(), O_RDONLY, 0))); + map cloudFiles = cloudRp.GetReportInfos(); + + EXPECT_EQ(cloudFiles.size(), 1); + auto item = cloudFiles.find(fileName); + if (l_it == cloudFiles.end()) { + EXPECT_TRUE(false); + } + + string path = item.first; + EXPECT_EQ(item.first, fileName); + EXPECT_EQ(item.second.mode, mode); + EXPECT_EQ(item.second.isDir, isDir); + EXPECT_EQ(item.second.size, size); + EXPECT_EQ(item.second.mtime, mtime); + EXPECT_EQ(item.second.hash, hash); + } catch (const exception &e) { + GTEST_LOG_(INFO) << "BReportEntityTest-an exception occurred by GetReportInfos."; + e.what(); + } + GTEST_LOG_(INFO) << "BReportEntityTest-end b_report_entity_GetReportInfos_0100"; +} +} // namespace OHOS::FileManagement::Backup \ No newline at end of file -- Gitee