From 34248347528af9c415aa6400c702014d9ae8ff84 Mon Sep 17 00:00:00 2001 From: wangpggg Date: Mon, 29 Apr 2024 16:52:23 +0800 Subject: [PATCH] add ut to improve cover Signed-off-by: wangpeng --- tests/unittests/backup_sa/BUILD.gn | 5 +- tests/unittests/backup_sa/session/BUILD.gn | 66 ++ .../session/b_incremental_session_test.cpp | 592 ++++++++++++++++++ .../backup_sa/session/service_proxy_mock.cpp | 162 +++++ .../backup_sa/session/service_proxy_mock.h | 60 ++ 5 files changed, 884 insertions(+), 1 deletion(-) create mode 100644 tests/unittests/backup_sa/session/BUILD.gn create mode 100644 tests/unittests/backup_sa/session/b_incremental_session_test.cpp create mode 100644 tests/unittests/backup_sa/session/service_proxy_mock.cpp create mode 100644 tests/unittests/backup_sa/session/service_proxy_mock.h diff --git a/tests/unittests/backup_sa/BUILD.gn b/tests/unittests/backup_sa/BUILD.gn index 45559f172..370b91517 100644 --- a/tests/unittests/backup_sa/BUILD.gn +++ b/tests/unittests/backup_sa/BUILD.gn @@ -17,5 +17,8 @@ import("//foundation/filemanagement/app_file_service/backup.gni") group("backup_sa_test") { testonly = true - deps = [ "module_ipc:backup_sa_ipc_test" ] + deps = [ + "module_ipc:backup_sa_ipc_test", + "session:session_test", + ] } diff --git a/tests/unittests/backup_sa/session/BUILD.gn b/tests/unittests/backup_sa/session/BUILD.gn new file mode 100644 index 000000000..46b847da7 --- /dev/null +++ b/tests/unittests/backup_sa/session/BUILD.gn @@ -0,0 +1,66 @@ +# 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. + +import("//build/test.gni") +import("//foundation/filemanagement/app_file_service/backup.gni") + +ohos_unittest("b_incremental_session_test") { + module_out_path = path_module_out_tests + + include_dirs = [ + "${path_backup}/frameworks/native/backup_kit_inner/include", + "${path_backup}/interfaces/inner_api/native/backup_kit_inner/impl", + ] + + sources = [ + "${path_backup}/frameworks/native/backup_kit_inner/src/b_file_info.cpp", + "${path_backup}/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp", + "${path_backup}/frameworks/native/backup_kit_inner/src/b_incremental_data.cpp", + "${path_backup}/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp", + "${path_backup}/frameworks/native/backup_kit_inner/src/b_incremental_session_restore_async.cpp", + "${path_backup}/frameworks/native/backup_kit_inner/src/service_incremental_reverse.cpp", + "${path_backup}/frameworks/native/backup_kit_inner/src/service_reverse.cpp", + "${path_backup}/frameworks/native/backup_kit_inner/src/service_reverse_stub.cpp", + "b_incremental_session_test.cpp", + "service_proxy_mock.cpp", + ] + + deps = [ + "${path_backup}/utils:backup_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + defines = [ + "LOG_TAG=\"app_file_service\"", + "LOG_DOMAIN=0xD200000", + "private = public", + "protected = public", + ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "hitrace:hitrace_meter", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + use_exceptions = true +} + +group("session_test") { + testonly = true + deps = [ ":b_incremental_session_test" ] +} diff --git a/tests/unittests/backup_sa/session/b_incremental_session_test.cpp b/tests/unittests/backup_sa/session/b_incremental_session_test.cpp new file mode 100644 index 000000000..804567eea --- /dev/null +++ b/tests/unittests/backup_sa/session/b_incremental_session_test.cpp @@ -0,0 +1,592 @@ +/*) + * 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 "b_error/b_error.h" +#include "b_incremental_backup_session.h" +#include "b_incremental_restore_session.h" +#include "b_incremental_session_restore_async.h" +#include "service_proxy_mock.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; +using namespace testing; + +class IncrementalSessionTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(); + void SetUp() override {}; + void TearDown() override {}; +public: + static inline sptr proxy = nullptr; + static inline shared_ptr backupSession = nullptr; + static inline shared_ptr restoreSession = nullptr; + static inline shared_ptr restoreAsyncSession = nullptr; +}; + +void IncrementalSessionTest::SetUpTestCase() +{ + proxy = sptr(new ServiceProxyMock(nullptr)); + backupSession = make_shared(); + restoreSession = make_shared(); + BIncrementalSessionRestoreAsync::Callbacks callbacks; + restoreAsyncSession = make_shared(callbacks); + ServiceProxy::serviceProxy_ = proxy; +} + +void IncrementalSessionTest::TearDownTestCase() +{ +} + +/** + * @tc.number: SUB_b_incremental_session_test_0100 + * @tc.name: SUB_b_incremental_session_test_0100 + * @tc.desc: 测试 InitRestoreSession 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issuesI9KPRL + */ +HWTEST_F(IncrementalSessionTest, SUB_b_incremental_session_test_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IncrementalSessionTest-begin SUB_b_incremental_session_test_0100"; + try { + ServiceProxy::serviceProxy_ = nullptr; + BIncrementalBackupSession::Callbacks callbacks; + auto err = backupSession->Init(callbacks); + EXPECT_EQ(err, nullptr); + + EXPECT_CALL(*proxy, InitIncrementalBackupSession(_)).WillOnce(Return(-1)).WillOnce(Return(0)); + ServiceProxy::serviceProxy_ = proxy; + err = backupSession->Init(callbacks); + EXPECT_EQ(err, nullptr); + err = backupSession->Init(callbacks); + EXPECT_EQ(err, nullptr); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "IncrementalSessionTest-an exception occurred by RemoveExtConn."; + } + GTEST_LOG_(INFO) << "IncrementalSessionTest-end SUB_b_incremental_session_test_0100"; +} + +void BackupSeviceDied() {} +/** + * @tc.number: SUB_b_incremental_session_test_0200 + * @tc.name: SUB_b_incremental_session_test_0200 + * @tc.desc: 测试 RegisterBackupServiceDied 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issuesI9KPRL + */ +HWTEST_F(IncrementalSessionTest, SUB_b_incremental_session_test_0200, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IncrementalSessionTest-begin SUB_b_incremental_session_test_0200"; + try { + ServiceProxy::serviceProxy_ = nullptr; + backupSession->RegisterBackupServiceDied(nullptr); + + ServiceProxy::serviceProxy_ = proxy; + backupSession->RegisterBackupServiceDied(nullptr); + + EXPECT_CALL(*proxy, AsObject()).WillOnce(Return(nullptr)); + backupSession->RegisterBackupServiceDied(BackupSeviceDied); + } catch (...) { + EXPECT_TRUE(true); + GTEST_LOG_(INFO) << "IncrementalSessionTest-an exception occurred by RemoveExtConn."; + } + GTEST_LOG_(INFO) << "IncrementalSessionTest-end SUB_b_incremental_session_test_0200"; +} + +/** + * @tc.number: SUB_b_incremental_session_test_0300 + * @tc.name: SUB_b_incremental_session_test_0300 + * @tc.desc: 测试 AppendBundles 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issuesI9KPRL + */ +HWTEST_F(IncrementalSessionTest, SUB_b_incremental_session_test_0300, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IncrementalSessionTest-begin SUB_b_incremental_session_test_0300"; + try { + ServiceProxy::serviceProxy_ = nullptr; + vector bundlesToBackup; + auto err = backupSession->AppendBundles(bundlesToBackup); + EXPECT_EQ(err, BError(BError::Codes::SDK_BROKEN_IPC).GetCode()); + + EXPECT_CALL(*proxy, AppendBundlesIncrementalBackupSession(_)).WillOnce(Return(0)); + ServiceProxy::serviceProxy_ = proxy; + err = backupSession->AppendBundles(bundlesToBackup); + EXPECT_EQ(err, BError(BError::Codes::OK).GetCode()); + } catch (...) { + EXPECT_TRUE(true); + GTEST_LOG_(INFO) << "IncrementalSessionTest-an exception occurred by RemoveExtConn."; + } + GTEST_LOG_(INFO) << "IncrementalSessionTest-end SUB_b_incremental_session_test_0300"; +} + +/** + * @tc.number: SUB_b_incremental_session_test_0400 + * @tc.name: SUB_b_incremental_session_test_0400 + * @tc.desc: 测试 Release 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issuesI9KPRL + */ +HWTEST_F(IncrementalSessionTest, SUB_b_incremental_session_test_0400, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IncrementalSessionTest-begin SUB_b_incremental_session_test_0400"; + try { + ServiceProxy::serviceProxy_ = nullptr; + auto err = backupSession->Release(); + EXPECT_EQ(err, BError(BError::Codes::SDK_BROKEN_IPC).GetCode()); + + EXPECT_CALL(*proxy, Release()).WillOnce(Return(0)); + ServiceProxy::serviceProxy_ = proxy; + err = backupSession->Release(); + EXPECT_EQ(err, BError(BError::Codes::OK).GetCode()); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "IncrementalSessionTest-an exception occurred by RemoveExtConn."; + } + GTEST_LOG_(INFO) << "IncrementalSessionTest-end SUB_b_incremental_session_test_0400"; +} + +/** + * @tc.number: SUB_b_incremental_session_test_0500 + * @tc.name: SUB_b_incremental_session_test_0500 + * @tc.desc: 测试 Init 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issuesI9KPRL + */ +HWTEST_F(IncrementalSessionTest, SUB_b_incremental_session_test_0500, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IncrementalSessionTest-begin SUB_b_incremental_session_test_0500"; + try { + ServiceProxy::serviceProxy_ = nullptr; + BIncrementalRestoreSession::Callbacks callbacks; + auto err = restoreSession->Init(callbacks); + EXPECT_EQ(err, nullptr); + + EXPECT_CALL(*proxy, InitIncrementalBackupSession(_)).WillOnce(Return(-1)).WillOnce(Return(0)); + ServiceProxy::serviceProxy_ = proxy; + err = restoreSession->Init(callbacks); + EXPECT_EQ(err, nullptr); + err = restoreSession->Init(callbacks); + EXPECT_EQ(err, nullptr); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "IncrementalSessionTest-an exception occurred by RemoveExtConn."; + } + GTEST_LOG_(INFO) << "IncrementalSessionTest-end SUB_b_incremental_session_test_0500"; +} + +void RestoreSeviceDied() {} +/** + * @tc.number: SUB_b_incremental_session_test_0600 + * @tc.name: SUB_b_incremental_session_test_0600 + * @tc.desc: 测试 RegisterBackupServiceDied 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issuesI9KPRL + */ +HWTEST_F(IncrementalSessionTest, SUB_b_incremental_session_test_0600, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IncrementalSessionTest-begin SUB_b_incremental_session_test_0600"; + try { + ServiceProxy::serviceProxy_ = nullptr; + restoreSession->RegisterBackupServiceDied(nullptr); + + ServiceProxy::serviceProxy_ = proxy; + restoreSession->RegisterBackupServiceDied(nullptr); + + EXPECT_CALL(*proxy, AsObject()).WillOnce(Return(nullptr)); + restoreSession->RegisterBackupServiceDied(RestoreSeviceDied); + } catch (...) { + EXPECT_TRUE(true); + GTEST_LOG_(INFO) << "IncrementalSessionTest-an exception occurred by RemoveExtConn."; + } + GTEST_LOG_(INFO) << "IncrementalSessionTest-end SUB_b_incremental_session_test_0600"; +} + +/** + * @tc.number: SUB_b_incremental_session_test_0700 + * @tc.name: SUB_b_incremental_session_test_0700 + * @tc.desc: 测试 AppendBundles 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issuesI9KPRL + */ +HWTEST_F(IncrementalSessionTest, SUB_b_incremental_session_test_0700, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IncrementalSessionTest-begin SUB_b_incremental_session_test_0700"; + try { + ServiceProxy::serviceProxy_ = nullptr; + UniqueFd remoteCap1; + vector bundlesToRestore; + auto err = restoreSession->AppendBundles(move(remoteCap1), bundlesToRestore); + EXPECT_EQ(err, BError(BError::Codes::SDK_BROKEN_IPC).GetCode()); + + EXPECT_CALL(*proxy, AppendBundlesRestoreSession(_, _, _, _)).WillOnce(Return(0)); + UniqueFd remoteCap2; + ServiceProxy::serviceProxy_ = proxy; + err = restoreSession->AppendBundles(move(remoteCap2), bundlesToRestore); + EXPECT_EQ(err, BError(BError::Codes::OK).GetCode()); + } catch (...) { + EXPECT_TRUE(true); + GTEST_LOG_(INFO) << "IncrementalSessionTest-an exception occurred by RemoveExtConn."; + } + GTEST_LOG_(INFO) << "IncrementalSessionTest-end SUB_b_incremental_session_test_0700"; +} + +/** + * @tc.number: SUB_b_incremental_session_test_0800 + * @tc.name: SUB_b_incremental_session_test_0800 + * @tc.desc: 测试 Release 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issuesI9KPRL + */ +HWTEST_F(IncrementalSessionTest, SUB_b_incremental_session_test_0800, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IncrementalSessionTest-begin SUB_b_incremental_session_test_0800"; + try { + ServiceProxy::serviceProxy_ = nullptr; + auto err = restoreSession->Release(); + EXPECT_EQ(err, BError(BError::Codes::SDK_BROKEN_IPC).GetCode()); + + EXPECT_CALL(*proxy, Release()).WillOnce(Return(0)); + ServiceProxy::serviceProxy_ = proxy; + err = restoreSession->Release(); + EXPECT_EQ(err, BError(BError::Codes::OK).GetCode()); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "IncrementalSessionTest-an exception occurred by RemoveExtConn."; + } + GTEST_LOG_(INFO) << "IncrementalSessionTest-end SUB_b_incremental_session_test_0800"; +} + +/** + * @tc.number: SUB_b_incremental_session_test_0900 + * @tc.name: SUB_b_incremental_session_test_0900 + * @tc.desc: 测试 PublishFile 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issuesI9KPRL + */ +HWTEST_F(IncrementalSessionTest, SUB_b_incremental_session_test_0900, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IncrementalSessionTest-begin SUB_b_incremental_session_test_0900"; + try { + ServiceProxy::serviceProxy_ = nullptr; + BFileInfo fileInfo; + auto err = restoreSession->PublishFile(fileInfo); + EXPECT_EQ(err, BError(BError::Codes::SDK_BROKEN_IPC).GetCode()); + + EXPECT_CALL(*proxy, PublishIncrementalFile(_)).WillOnce(Return(0)); + ServiceProxy::serviceProxy_ = proxy; + err = restoreSession->PublishFile(fileInfo); + EXPECT_EQ(err, BError(BError::Codes::OK).GetCode()); + } catch (...) { + EXPECT_TRUE(true); + GTEST_LOG_(INFO) << "IncrementalSessionTest-an exception occurred by RemoveExtConn."; + } + GTEST_LOG_(INFO) << "IncrementalSessionTest-end SUB_b_incremental_session_test_0900"; +} + + +/** + * @tc.number: SUB_b_incremental_session_test_1000 + * @tc.name: SUB_b_incremental_session_test_1000 + * @tc.desc: 测试 GetFileHandle 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issuesI9KPRL + */ +HWTEST_F(IncrementalSessionTest, SUB_b_incremental_session_test_1000, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IncrementalSessionTest-begin SUB_b_incremental_session_test_1000"; + try { + ServiceProxy::serviceProxy_ = nullptr; + string bundleName; + string fileName; + auto err = restoreSession->GetFileHandle(bundleName, fileName); + EXPECT_EQ(err, BError(BError::Codes::SDK_BROKEN_IPC).GetCode()); + + EXPECT_CALL(*proxy, GetIncrementalFileHandle(_, _)).WillOnce(Return(0)); + ServiceProxy::serviceProxy_ = proxy; + err = restoreSession->GetFileHandle(bundleName, fileName); + EXPECT_EQ(err, BError(BError::Codes::OK).GetCode()); + } catch (...) { + EXPECT_TRUE(true); + GTEST_LOG_(INFO) << "IncrementalSessionTest-an exception occurred by RemoveExtConn."; + } + GTEST_LOG_(INFO) << "IncrementalSessionTest-end SUB_b_incremental_session_test_1000"; +} + +/** + * @tc.number: SUB_b_incremental_session_test_1100 + * @tc.name: SUB_b_incremental_session_test_1100 + * @tc.desc: 测试 AppendBundles 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issuesI9KPRL + */ +HWTEST_F(IncrementalSessionTest, SUB_b_incremental_session_test_1100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IncrementalSessionTest-begin SUB_b_incremental_session_test_1100"; + try { + ServiceProxy::serviceProxy_ = nullptr; + UniqueFd remoteCap; + vector bundlesToRestore; + vector detailInfos; + auto err = restoreSession->AppendBundles(move(remoteCap), bundlesToRestore, detailInfos); + EXPECT_EQ(err, BError(BError::Codes::SDK_BROKEN_IPC).GetCode()); + + EXPECT_CALL(*proxy, AppendBundlesRestoreSession(_, _, _, _, _)).WillOnce(Return(0)); + ServiceProxy::serviceProxy_ = proxy; + err = restoreSession->AppendBundles(move(remoteCap), bundlesToRestore, detailInfos); + EXPECT_EQ(err, BError(BError::Codes::OK).GetCode()); + } catch (...) { + EXPECT_TRUE(true); + GTEST_LOG_(INFO) << "IncrementalSessionTest-an exception occurred by RemoveExtConn."; + } + GTEST_LOG_(INFO) << "IncrementalSessionTest-end SUB_b_incremental_session_test_1100"; +} + +/** + * @tc.number: SUB_b_incremental_session_test_1200 + * @tc.name: SUB_b_incremental_session_test_1200 + * @tc.desc: 测试 Init 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issuesI9KPRL + */ +HWTEST_F(IncrementalSessionTest, SUB_b_incremental_session_test_1200, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IncrementalSessionTest-begin SUB_b_incremental_session_test_1200"; + try { + ServiceProxy::serviceProxy_ = nullptr; + BIncrementalSessionRestoreAsync::Callbacks callbacks; + auto err = restoreAsyncSession->Init(callbacks); + EXPECT_EQ(err, nullptr); + + EXPECT_CALL(*proxy, InitIncrementalBackupSession(_)).WillOnce(Return(-1)).WillOnce(Return(0)); + ServiceProxy::serviceProxy_ = proxy; + err = restoreAsyncSession->Init(callbacks); + EXPECT_EQ(err, nullptr); + err = restoreAsyncSession->Init(callbacks); + EXPECT_EQ(err, nullptr); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "IncrementalSessionTest-an exception occurred by RemoveExtConn."; + } + GTEST_LOG_(INFO) << "IncrementalSessionTest-end SUB_b_incremental_session_test_1200"; +} + +void RestoreAsyncSeviceDied() {} +/** + * @tc.number: SUB_b_incremental_session_test_1300 + * @tc.name: SUB_b_incremental_session_test_1300 + * @tc.desc: 测试 RegisterBackupServiceDied 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issuesI9KPRL + */ +HWTEST_F(IncrementalSessionTest, SUB_b_incremental_session_test_1300, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IncrementalSessionTest-begin SUB_b_incremental_session_test_1300"; + try { + ServiceProxy::serviceProxy_ = nullptr; + restoreAsyncSession->RegisterBackupServiceDied(nullptr); + + ServiceProxy::serviceProxy_ = proxy; + restoreAsyncSession->RegisterBackupServiceDied(nullptr); + + EXPECT_CALL(*proxy, AsObject()).WillOnce(Return(nullptr)); + restoreAsyncSession->RegisterBackupServiceDied(RestoreAsyncSeviceDied); + } catch (...) { + EXPECT_TRUE(true); + GTEST_LOG_(INFO) << "IncrementalSessionTest-an exception occurred by RemoveExtConn."; + } + GTEST_LOG_(INFO) << "IncrementalSessionTest-end SUB_b_incremental_session_test_1300"; +} + +/** + * @tc.number: SUB_b_incremental_session_test_1400 + * @tc.name: SUB_b_incremental_session_test_1400 + * @tc.desc: 测试 AppendBundles 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issuesI9KPRL + */ +HWTEST_F(IncrementalSessionTest, SUB_b_incremental_session_test_1400, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IncrementalSessionTest-begin SUB_b_incremental_session_test_1400"; + try { + ServiceProxy::serviceProxy_ = nullptr; + UniqueFd remoteCap1; + vector bundlesToRestore; + auto err = restoreAsyncSession->AppendBundles(move(remoteCap1), bundlesToRestore); + EXPECT_EQ(err, BError(BError::Codes::SDK_BROKEN_IPC).GetCode()); + + EXPECT_CALL(*proxy, AppendBundlesRestoreSession(_, _, _, _)).WillOnce(Return(0)); + UniqueFd remoteCap2; + ServiceProxy::serviceProxy_ = proxy; + err = restoreAsyncSession->AppendBundles(move(remoteCap2), bundlesToRestore); + EXPECT_EQ(err, BError(BError::Codes::OK).GetCode()); + } catch (...) { + EXPECT_TRUE(true); + GTEST_LOG_(INFO) << "IncrementalSessionTest-an exception occurred by RemoveExtConn."; + } + GTEST_LOG_(INFO) << "IncrementalSessionTest-end SUB_b_incremental_session_test_1400"; +} + +/** + * @tc.number: SUB_b_incremental_session_test_1500 + * @tc.name: SUB_b_incremental_session_test_1500 + * @tc.desc: 测试 PublishFile 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issuesI9KPRL + */ +HWTEST_F(IncrementalSessionTest, SUB_b_incremental_session_test_1500, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IncrementalSessionTest-begin SUB_b_incremental_session_test_1500"; + try { + ServiceProxy::serviceProxy_ = nullptr; + BFileInfo fileInfo; + auto err = restoreAsyncSession->PublishFile(fileInfo); + EXPECT_EQ(err, BError(BError::Codes::SDK_BROKEN_IPC).GetCode()); + + EXPECT_CALL(*proxy, PublishIncrementalFile(_)).WillOnce(Return(0)); + ServiceProxy::serviceProxy_ = proxy; + err = restoreAsyncSession->PublishFile(fileInfo); + EXPECT_EQ(err, BError(BError::Codes::OK).GetCode()); + } catch (...) { + EXPECT_TRUE(true); + GTEST_LOG_(INFO) << "IncrementalSessionTest-an exception occurred by RemoveExtConn."; + } + GTEST_LOG_(INFO) << "IncrementalSessionTest-end SUB_b_incremental_session_test_1500"; +} + + +/** + * @tc.number: SUB_b_incremental_session_test_1600 + * @tc.name: SUB_b_incremental_session_test_1600 + * @tc.desc: 测试 GetFileHandle 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issuesI9KPRL + */ +HWTEST_F(IncrementalSessionTest, SUB_b_incremental_session_test_1600, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IncrementalSessionTest-begin SUB_b_incremental_session_test_1600"; + try { + ServiceProxy::serviceProxy_ = nullptr; + string bundleName; + string fileName; + auto err = restoreAsyncSession->GetFileHandle(bundleName, fileName); + EXPECT_EQ(err, BError(BError::Codes::SDK_BROKEN_IPC).GetCode()); + + EXPECT_CALL(*proxy, GetIncrementalFileHandle(_, _)).WillOnce(Return(0)); + ServiceProxy::serviceProxy_ = proxy; + err = restoreAsyncSession->GetFileHandle(bundleName, fileName); + EXPECT_EQ(err, BError(BError::Codes::OK).GetCode()); + } catch (...) { + EXPECT_TRUE(true); + GTEST_LOG_(INFO) << "IncrementalSessionTest-an exception occurred by RemoveExtConn."; + } + GTEST_LOG_(INFO) << "IncrementalSessionTest-end SUB_b_incremental_session_test_1600"; +} + +/** + * @tc.number: SUB_b_incremental_session_test_1700 + * @tc.name: SUB_b_incremental_session_test_1700 + * @tc.desc: 测试 AppendBundles 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issuesI9KPRL + */ +HWTEST_F(IncrementalSessionTest, SUB_b_incremental_session_test_1700, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IncrementalSessionTest-begin SUB_b_incremental_session_test_1700"; + try { + ServiceProxy::serviceProxy_ = nullptr; + UniqueFd remoteCap; + vector bundlesToRestore; + vector detailInfos; + auto err = restoreAsyncSession->AppendBundles(move(remoteCap), bundlesToRestore, detailInfos); + EXPECT_EQ(err, BError(BError::Codes::SDK_BROKEN_IPC).GetCode()); + + EXPECT_CALL(*proxy, AppendBundlesRestoreSession(_, _, _, _, _)).WillOnce(Return(0)); + ServiceProxy::serviceProxy_ = proxy; + err = restoreAsyncSession->AppendBundles(move(remoteCap), bundlesToRestore, detailInfos); + EXPECT_EQ(err, BError(BError::Codes::OK).GetCode()); + } catch (...) { + EXPECT_TRUE(true); + GTEST_LOG_(INFO) << "IncrementalSessionTest-an exception occurred by RemoveExtConn."; + } + GTEST_LOG_(INFO) << "IncrementalSessionTest-end SUB_b_incremental_session_test_1700"; +} + +/** + * @tc.number: SUB_b_incremental_session_test_1800 + * @tc.name: SUB_b_incremental_session_test_1800 + * @tc.desc: 测试 Release 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issuesI9KPRL + */ +HWTEST_F(IncrementalSessionTest, SUB_b_incremental_session_test_1800, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IncrementalSessionTest-begin SUB_b_incremental_session_test_1800"; + try { + ServiceProxy::serviceProxy_ = nullptr; + auto err = restoreAsyncSession->Release(); + EXPECT_EQ(err, BError(BError::Codes::SDK_BROKEN_IPC).GetCode()); + + EXPECT_CALL(*proxy, Release()).WillOnce(Return(0)); + ServiceProxy::serviceProxy_ = proxy; + err = restoreAsyncSession->Release(); + EXPECT_EQ(err, BError(BError::Codes::OK).GetCode()); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "IncrementalSessionTest-an exception occurred by RemoveExtConn."; + } + GTEST_LOG_(INFO) << "IncrementalSessionTest-end SUB_b_incremental_session_test_1800"; +} + +} // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/unittests/backup_sa/session/service_proxy_mock.cpp b/tests/unittests/backup_sa/session/service_proxy_mock.cpp new file mode 100644 index 000000000..df936f746 --- /dev/null +++ b/tests/unittests/backup_sa/session/service_proxy_mock.cpp @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2022-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 "service_proxy.h" + +#include "b_error/b_error.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; + +int32_t ServiceProxy::InitRestoreSession(sptr remote) +{ + return 0; +} + +int32_t ServiceProxy::InitBackupSession(sptr remote) +{ + return 0; +} + +ErrCode ServiceProxy::Start() +{ + return BError(BError::Codes::OK); +} + +UniqueFd ServiceProxy::GetLocalCapabilities() +{ + return UniqueFd(-1); +} + +ErrCode ServiceProxy::PublishFile(const BFileInfo &fileInfo) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::AppFileReady(const string &fileName, UniqueFd fd) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::AppDone(ErrCode errCode) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::ServiceResultReport(const std::string restoreRetInfo, BackupRestoreScenario scenario) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::GetFileHandle(const string &bundleName, const string &fileName) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::AppendBundlesRestoreSession(UniqueFd fd, const vector &bundleNames, + const vector &detailInfos, RestoreTypeEnum restoreType, int32_t userId) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::AppendBundlesRestoreSession(UniqueFd fd, + const vector &bundleNames, + RestoreTypeEnum restoreType, + int32_t userId) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::AppendBundlesBackupSession(const vector &bundleNames) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::Finish() +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::Release() +{ + return BError(BError::Codes::OK); +} + +UniqueFd ServiceProxy::GetLocalCapabilitiesIncremental(const vector &bundleNames) +{ + return UniqueFd(-1); +} + +ErrCode ServiceProxy::InitIncrementalBackupSession(sptr remote) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::AppendBundlesIncrementalBackupSession(const vector &bundlesToBackup) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::PublishIncrementalFile(const BFileInfo &fileInfo) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::AppIncrementalFileReady(const string &fileName, UniqueFd fd, UniqueFd manifestFd) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::AppIncrementalDone(ErrCode errCode) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::GetIncrementalFileHandle(const std::string &bundleName, const std::string &fileName) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::GetBackupInfo(std::string &bundleName, std::string &result) +{ + return BError(BError::Codes::OK); +} + +ErrCode ServiceProxy::UpdateTimer(BundleName &bundleName, uint32_t timeOut, bool &result) +{ + return BError(BError::Codes::OK); +} + +sptr ServiceProxy::GetInstance() +{ + return serviceProxy_; +} + +void ServiceProxy::InvaildInstance() +{ + serviceProxy_ = nullptr; +} + +void ServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, + const OHOS::sptr &remoteObject) +{ + return; +} + +void ServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) +{ + return; +} +} // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/unittests/backup_sa/session/service_proxy_mock.h b/tests/unittests/backup_sa/session/service_proxy_mock.h new file mode 100644 index 000000000..c648e8783 --- /dev/null +++ b/tests/unittests/backup_sa/session/service_proxy_mock.h @@ -0,0 +1,60 @@ +/* + * 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. + */ + +#ifndef TEST_UNITTEST_SERVICE_PROXY_MOCK_H +#define TEST_UNITTEST_SERVICE_PROXY_MOCK_H + +#include + +#include "service_proxy.h" + +namespace OHOS::FileManagement::Backup { +class ServiceProxyMock : public ServiceProxy { +public: + explicit ServiceProxyMock(const sptr &impl) : ServiceProxy(impl) {} + ~ServiceProxyMock() override {} +public: + MOCK_METHOD1(InitRestoreSession, ErrCode(sptr remote)); + MOCK_METHOD1(InitBackupSession, ErrCode(sptr remote)); + MOCK_METHOD0(Start, ErrCode()); + MOCK_METHOD0(AsObject, sptr()); + MOCK_METHOD0(GetLocalCapabilities, UniqueFd()); + MOCK_METHOD1(PublishFile, ErrCode(const BFileInfo &fileInfo)); + MOCK_METHOD2(AppFileReady, ErrCode(const std::string &fileName, UniqueFd fd)); + MOCK_METHOD3(AppFileReady, ErrCode(const std::string &fileName, UniqueFd fd, int32_t errCode)); + MOCK_METHOD1(AppDone, ErrCode(ErrCode errCode)); + MOCK_METHOD2(ServiceResultReport, ErrCode(const std::string restoreRetInfo, BackupRestoreScenario scenario)); + MOCK_METHOD2(GetFileHandle, ErrCode(const std::string &bundleName, const std::string &fileName)); + MOCK_METHOD5(AppendBundlesRestoreSession, ErrCode(UniqueFd fd, const std::vector &bundleNames, + const std::vector &detailInfos, RestoreTypeEnum restoreType, int32_t userId)); + MOCK_METHOD4(AppendBundlesRestoreSession, ErrCode(UniqueFd fd, const std::vector &bundleNames, + RestoreTypeEnum restoreType, int32_t userId)); + MOCK_METHOD1(AppendBundlesBackupSession, ErrCode(const std::vector &bundleNames)); + MOCK_METHOD0(Finish, ErrCode()); + MOCK_METHOD0(Release, ErrCode()); + MOCK_METHOD1(GetLocalCapabilitiesIncremental, UniqueFd(const std::vector &bundleNames)); + MOCK_METHOD1(InitIncrementalBackupSession, ErrCode(sptr remote)); + MOCK_METHOD1(AppendBundlesIncrementalBackupSession, ErrCode(const std::vector &bundlesToBackup)); + MOCK_METHOD1(PublishIncrementalFile, ErrCode(const BFileInfo &fileInfo)); + MOCK_METHOD3(AppIncrementalFileReady, ErrCode(const std::string &fileName, UniqueFd fd, UniqueFd manifestFd)); + MOCK_METHOD4(AppIncrementalFileReady, ErrCode(const std::string &fileName, UniqueFd fd, UniqueFd manifestFd, + int32_t errCode)); + MOCK_METHOD1(AppIncrementalDone, ErrCode(ErrCode errCode)); + MOCK_METHOD2(GetIncrementalFileHandle, ErrCode(const std::string &bundleName, const std::string &fileName)); + MOCK_METHOD2(GetBackupInfo, ErrCode(BundleName &bundleName, std::string &result)); + MOCK_METHOD3(UpdateTimer, ErrCode(BundleName &bundleName, uint32_t timeOut, bool &result)); +}; +} // End of namespace OHOS::FileManagement::Backup +#endif // TEST_UNITTEST_SERVICE_PROXY_MOCK_H \ No newline at end of file -- Gitee