From 5978931931a8fab330321710172de266610f052f Mon Sep 17 00:00:00 2001 From: dingdong Date: Wed, 14 Aug 2024 15:56:23 +0800 Subject: [PATCH] add UT for SA Signed-off-by: dingdong --- .../module_ipc/sa_backup_connection_test.cpp | 158 ++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 tests/unittests/backup_sa/module_ipc/sa_backup_connection_test.cpp diff --git a/tests/unittests/backup_sa/module_ipc/sa_backup_connection_test.cpp b/tests/unittests/backup_sa/module_ipc/sa_backup_connection_test.cpp new file mode 100644 index 000000000..a87210b8c --- /dev/null +++ b/tests/unittests/backup_sa/module_ipc/sa_backup_connection_test.cpp @@ -0,0 +1,158 @@ +/* + * 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. + */ + +#include "b_error/b_error.h" +#include "b_error/b_excep_utils.h" +#include "filemgmt_libhilog.h" +#include "module_ipc/sa_backup_connection.h" +#include "system_ability_definition.h" +#include + +namespace OHOS::FileManagement::Backup { + +class SABackupConnectionTest : public testing::test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp() override {}; + void TearDown() override {}; + +public: + static inline sptr backupCon_ = nullptr; + static inline shared_ptr castMock = nullptr; +}; + +void SABackupConnectionTest::SetUpTestCase(void) +{ + backupCon_ = sptr(new SABackupConnection(CallDied, CallDone, CallBackup, CallRestore)); + castMock = std::make_shared(); + IfaceCastMock::cast = castMock; +} + +void SABackupConnectionTest::TearDownTestCase(void) +{ + backupCon_ = nullptr; + IfaceCastMock::cast = nullptr; + castMock = nullptr; +} + +static void CallDied(const std::string &&name) +{ + GTEST_LOG_(INFO) << "SABackupConnectionTest-CallDied SUCCESS"; +} + +static void CallDone(const std::string &&name) +{ + GTEST_LOG_(INFO) << "SABackupConnectionTest-CallDone SUCCESS"; +} + +static void CallBackUp(const std::string &&bundleName, const int &&fd, const std::string result, const std::string ret) +{ + GTEST_LOG_(INFO) << "SABackupConnectionTest-CallBackUp SUCCESS"; +} + +static void CallRestore(const std::string &&bundleName, const int &&fd, const std::string result, const std::string ret) +{ + GTEST_LOG_(INFO) << "SABackupConnectionTest-CallRestore SUCCESS"; +} + +HWTEST_F(SABackupConnectionTest, SUB_SABackupConnection_ConnectBackupSAExt_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SABackupConnectionTest-begin SUB_SABackupConnection_ConnectBackupSAExt_001"; + try { + std::string extInfo = "info"; + std::string bundleName = "com.example.app"; + std::strint extension = BConstants::EXTENSION_BACKUP; + EXPECT_TRUE(backupCon_ != nullptr); + EXPECT_TRUE(castMock != nullptr); + backupCon_->ConnectBackupSAExt(NULL, extension, extInfo); + EXPECT_TRUE(true); + sptr remoteObject = sptr(new BackupExtExtensionMock); + EXPECT_CALL(*castMock, iface_cast(_)).WillOnce(Return(nullptr)); + backupCon_->ConnectBackupSAExt(bundleName, extension, extInfo); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "SABackupConnectionTest-an exception occurred by OnAbilityConnectDone."; + } + GTEST_LOG_(INFO) << "SABackupConnectionTest-end SUB_SABackupConnection_ConnectBackupSAExt_001"; +} + +HWTEST_F(SABackupConnectionTest, SUB_SABackupConnection_ConnectBackupSAExt_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SABackupConnectionTest-begin SUB_SABackupConnection_ConnectBackupSAExt_002"; + try { + std::string extInfo = "info"; + std::string bundleName = "com.example.app"; + std::strint extension = BConstants::EXTENSION_BACKUP; + EXPECT_TRUE(backupCon_ != nullptr); + EXPECT_TRUE(castMock != nullptr); + backupCon_->ConnectBackupSAExt(bundleName, NULL, extInfo); + EXPECT_TRUE(true); + sptr remoteObject = sptr(new BackupExtExtensionMock); + EXPECT_CALL(*castMock, iface_cast(_)).WillOnce(Return(nullptr)); + backupCon_->ConnectBackupSAExt(bundleName, extension, extInfo); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "SABackupConnectionTest-an exception occurred by OnAbilityConnectDone."; + } + GTEST_LOG_(INFO) << "SABackupConnectionTest-end SUB_SABackupConnection_ConnectBackupSAExt_002"; +} + +HWTEST_F(SABackupConnectionTest, SUB_SABackupConnection_ConnectBackupSAExt_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SABackupConnectionTest-begin SUB_SABackupConnection_ConnectBackupSAExt_003"; + try { + std::string extInfo = "info"; + std::string bundleName = "com.example.app"; + std::strint extension = BConstants::EXTENSION_RESTORE; + EXPECT_TRUE(backupCon_ != nullptr); + EXPECT_TRUE(castMock != nullptr); + backupCon_->ConnectBackupSAExt(NULL, extension, extInfo); + EXPECT_TRUE(true); + sptr remoteObject = sptr(new BackupExtExtensionMock); + EXPECT_CALL(*castMock, iface_cast(_)).WillOnce(Return(nullptr)); + backupCon_->ConnectBackupSAExt(bundleName, extension, extInfo); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "SABackupConnectionTest-an exception occurred by OnAbilityConnectDone."; + } + GTEST_LOG_(INFO) << "SABackupConnectionTest-end SUB_SABackupConnection_ConnectBackupSAExt_003"; +} + +HWTEST_F(SABackupConnectionTest, SUB_SABackupConnection_ConnectBackupSAExt_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SABackupConnectionTest-begin SUB_SABackupConnection_ConnectBackupSAExt_004"; + try { + std::string extInfo = "info"; + std::string bundleName = "com.example.app"; + std::strint extension = BConstants::EXTENSION_RESTORE; + EXPECT_TRUE(backupCon_ != nullptr); + EXPECT_TRUE(castMock != nullptr); + backupCon_->ConnectBackupSAExt(bundleName, NULL, extInfo); + EXPECT_TRUE(true); + sptr remoteObject = sptr(new BackupExtExtensionMock); + EXPECT_CALL(*castMock, iface_cast(_)).WillOnce(Return(nullptr)); + backupCon_->ConnectBackupSAExt(bundleName, extension, extInfo); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "SABackupConnectionTest-an exception occurred by OnAbilityConnectDone."; + } + GTEST_LOG_(INFO) << "SABackupConnectionTest-end SUB_SABackupConnection_ConnectBackupSAExt_004"; +} +} // namespace OHOS::FileManagement::Backup \ No newline at end of file -- Gitee