From d54e211239433c4e25ae36efce51e83bd8f111df Mon Sep 17 00:00:00 2001 From: jaychao Date: Tue, 12 Dec 2023 12:31:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9ETDD-untar=5Ffile=5Ftest-2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: jaychao --- tests/unittests/backup_ext/BUILD.gn | 144 ++++++++++++++++++ .../unittests/backup_ext/untar_file_test.cpp | 98 ++++++++++++ 2 files changed, 242 insertions(+) create mode 100644 tests/unittests/backup_ext/BUILD.gn create mode 100644 tests/unittests/backup_ext/untar_file_test.cpp diff --git a/tests/unittests/backup_ext/BUILD.gn b/tests/unittests/backup_ext/BUILD.gn new file mode 100644 index 000000000..ae09f651f --- /dev/null +++ b/tests/unittests/backup_ext/BUILD.gn @@ -0,0 +1,144 @@ +# Copyright (c) 2022-2023 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. + +import("//build/test.gni") +import("//foundation/filemanagement/app_file_service/backup.gni") + +ohos_unittest("tar_file_test") { + module_out_path = path_module_out_tests + + sources = [ + "${path_backup}/frameworks/native/backup_ext/src/ext_backup.cpp", + "${path_backup}/frameworks/native/backup_ext/src/ext_backup_js.cpp", + "${path_backup}/frameworks/native/backup_ext/src/ext_backup_loader.cpp", + "${path_backup}/frameworks/native/backup_ext/src/ext_extension.cpp", + "${path_backup}/frameworks/native/backup_ext/src/ext_extension_stub.cpp", + "${path_backup}/frameworks/native/backup_ext/src/tar_file.cpp", + "${path_backup}/frameworks/native/backup_ext/src/untar_file.cpp", + "tar_file_test.cpp", + ] + sources += backup_mock_proxy_src + + include_dirs = [ + "${path_ability_runtime}/services/common/include", + "${path_ability_runtime}/interfaces/kits/napi/aafwk/inner/napi_common", + "${path_ability_runtime}/interfaces/kits/native/appkit/ability_runtime", + "${path_backup}/frameworks/native/backup_ext/include", + "${path_backup}/interfaces/inner_api/native/backup_kit_inner/", + "${path_backup}/interfaces/inner_api/native/backup_kit_inner/impl", + "${path_backup}/frameworks/native/backup_kit_inner/include", + "${path_backup}/utils/include", + "${path_napi}/native_engine", + ] + include_dirs += backup_mock_utils_include + + deps = [ + "${path_backup}/interfaces/inner_api/native/backup_kit_inner:backup_kit_inner", + "${path_backup}/tests/utils:backup_test_utils", + "${path_backup}/utils:backup_utils", + "${path_googletest}:gmock_main", + "${path_jsoncpp}:jsoncpp", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:ability_context_native", + "ability_runtime:abilitykit_native", + "ability_runtime:app_context", + "ability_runtime:appkit_native", + "ability_runtime:runtime", + "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "ipc:ipc_core", + "napi:ace_napi", + "samgr:samgr_proxy", + ] + + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + + use_exceptions = true +} + +ohos_unittest("untar_file_test") { + module_out_path = path_module_out_tests + + sources = [ + "${path_backup}/frameworks/native/backup_ext/src/ext_backup.cpp", + "${path_backup}/frameworks/native/backup_ext/src/ext_backup_js.cpp", + "${path_backup}/frameworks/native/backup_ext/src/ext_backup_loader.cpp", + "${path_backup}/frameworks/native/backup_ext/src/ext_extension.cpp", + "${path_backup}/frameworks/native/backup_ext/src/ext_extension_stub.cpp", + "${path_backup}/frameworks/native/backup_ext/src/tar_file.cpp", + "${path_backup}/frameworks/native/backup_ext/src/untar_file.cpp", + "untar_file_test.cpp", + ] + sources += backup_mock_proxy_src + + include_dirs = [ + "${path_ability_runtime}/services/common/include", + "${path_ability_runtime}/interfaces/kits/napi/aafwk/inner/napi_common", + "${path_ability_runtime}/interfaces/kits/native/appkit/ability_runtime", + "${path_backup}/frameworks/native/backup_ext/include", + "${path_backup}/interfaces/inner_api/native/backup_kit_inner/", + "${path_backup}/interfaces/inner_api/native/backup_kit_inner/impl", + "${path_backup}/frameworks/native/backup_kit_inner/include", + "${path_backup}/utils/include", + "${path_napi}/native_engine", + ] + include_dirs += backup_mock_utils_include + + deps = [ + "${path_backup}/interfaces/inner_api/native/backup_kit_inner:backup_kit_inner", + "${path_backup}/tests/utils:backup_test_utils", + "${path_backup}/utils:backup_utils", + "${path_googletest}:gmock_main", + "${path_jsoncpp}:jsoncpp", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:ability_context_native", + "ability_runtime:abilitykit_native", + "ability_runtime:app_context", + "ability_runtime:appkit_native", + "ability_runtime:runtime", + "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "ipc:ipc_core", + "napi:ace_napi", + "samgr:samgr_proxy", + ] + + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + + use_exceptions = true +} + +group("backup_ext_test") { + testonly = true + + deps = [ + ":tar_file_test", + ":untar_file_test", + ] +} diff --git a/tests/unittests/backup_ext/untar_file_test.cpp b/tests/unittests/backup_ext/untar_file_test.cpp new file mode 100644 index 000000000..dd46fdc64 --- /dev/null +++ b/tests/unittests/backup_ext/untar_file_test.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2022-2023 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. + */ + +/** + * @tc.number: SUB_Untar_File_HandleTarBuffer_0100 + * @tc.name: SUB_Untar_File_HandleTarBuffer_0100 + * @tc.desc: 测试 HandleTarBuffer 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(UntarFileTest, SUB_Untar_File_HandleTarBuffer_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_HandleTarBuffer_0100"; + try { + CreateTarFile("SUB_Untar_File_HandleTarBuffer_0100"); + string buff(""); + string name(""); + FileStatInfo info; + UntarFile::GetInstance().HandleTarBuffer(buff, name, info); + EXPECT_EQ(UntarFile::GetInstance().tarFileSize_, 0); + EXPECT_EQ(UntarFile::GetInstance().tarFileBlockCnt_, 0); + EXPECT_EQ(UntarFile::GetInstance().pos_, 0); + ClearCache(); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by UntarFile."; + } + GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_HandleTarBuffer_0100"; +} + +/** + * @tc.number: SUB_Untar_File_ParseFileByTypeFlag_0100 + * @tc.name: SUB_Untar_File_ParseFileByTypeFlag_0100 + * @tc.desc: 测试 ParseFileByTypeFlag 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(UntarFileTest, SUB_Untar_File_ParseFileByTypeFlag_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_ParseFileByTypeFlag_0100"; + try { + CreateTarFile("SUB_Untar_File_ParseFileByTypeFlag_0100"); + char typeFlag = REGTYPE; + bool isSkip = false; + FileStatInfo info; + UntarFile::GetInstance().ParseFileByTypeFlag(typeFlag, isSkip, info); + EXPECT_FALSE(isSkip); + ClearCache(); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by UntarFile."; + } + GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_ParseFileByTypeFlag_0100"; +} + +/** + * @tc.number: SUB_Untar_File_HandleFileProperties_0100 + * @tc.name: SUB_Untar_File_HandleFileProperties_0100 + * @tc.desc: 测试 HandleFileProperties 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(UntarFileTest, SUB_Untar_File_HandleFileProperties_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UntarFileTest-begin SUB_Untar_File_HandleFileProperties_0100"; + try { + bool isSkip = false; + FileStatInfo info; + int ret = UntarFile::GetInstance().HandleFileProperties(isSkip, info); + EXPECT_NE(ret, 0); + EXPECT_FALSE(isSkip); + ClearCache(); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "UntarFileTest-an exception occurred by UntarFile."; + } + GTEST_LOG_(INFO) << "UntarFileTest-end SUB_Untar_File_HandleFileProperties_0100"; +} + +} // namespace OHOS::FileManagement::Backup \ No newline at end of file -- Gitee