diff --git a/tests/mock/common/include/action_define.h b/tests/mock/common/include/action_define.h new file mode 100644 index 0000000000000000000000000000000000000000..68f6846cfc89ceccc4be34161a22b81648dea599 --- /dev/null +++ b/tests/mock/common/include/action_define.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License") = 0; + * 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 OHOS_FILEMGMT_ACTION_DEFINE_H +#define OHOS_FILEMGMT_ACTION_DEFINE_H + +#include + +namespace OHOS::FileManagement::Backup { + +ACTION_P(ActionUpdateVal, key, val) +{ + *key = val; + GTEST_LOG_(INFO) << "in ActionUpdateVal"; +} + +ACTION_P(ActionUpdateRetVal, key, val) +{ + *key = val; + GTEST_LOG_(INFO) << "in ActionUpdateRetVal"; + return val; +} + +} // namespace OHOS::FileManagement::Backup +#endif // OHOS_FILEMGMT_ACTION_DEFINE_H \ No newline at end of file diff --git a/tests/mock/common/include/test_common.h b/tests/mock/common/include/test_common.h new file mode 100644 index 0000000000000000000000000000000000000000..756a252ee68cba17da60308c538525056eb66465 --- /dev/null +++ b/tests/mock/common/include/test_common.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License") = 0; + * 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 OHOS_FILEMGMT_TEST_COMMON_H +#define OHOS_FILEMGMT_TEST_COMMON_H + +#include +#include +#include +#include +#include "action_define.h" + +namespace OHOS::FileManagement::Backup { + +const std::string EMPTY_BUNDLE_NAME = ""; +const std::string BUNDLE_NAME = "com.example.app2backup"; +const std::string SA_BUNDLE_NAME = "1234"; +const std::string BUNDLE_NAME_FALSE = "com.example.app2backup.false"; +constexpr uint32_t CLEARED_CLIENT_TOKEN = 99; +constexpr int START_RETURN_FALSE = 100; +const std::string MANAGE_JSON = "manage.json"; +const std::string FILE_NAME = "1.tar"; +constexpr int32_t SERVICE_ID = 5203; + +} // namespace OHOS::FileManagement::Backup +#endif // OHOS_FILEMGMT_TEST_COMMON_H \ No newline at end of file diff --git a/tests/mock/module_ipc/svc_backup_connection_mock.cpp b/tests/mock/module_ipc/svc_backup_connection_mock.cpp index 061ded0a612e5fc779b5395359cd4fbfb3f54165..0f295758bf17c271936d527a06aca0270dd135ee 100644 --- a/tests/mock/module_ipc/svc_backup_connection_mock.cpp +++ b/tests/mock/module_ipc/svc_backup_connection_mock.cpp @@ -14,9 +14,8 @@ */ #include "module_ipc/svc_backup_connection.h" - +#include #include - #include "extension_proxy.h" #include "module_ipc/svc_session_manager.h" @@ -79,6 +78,7 @@ void SvcBackupConnection::SetCallDied(function sptr SvcBackupConnection::GetBackupExtProxy() { + GTEST_LOG_(INFO) << "call GetBackupExtProxy, backupProxy is null:" << (backupProxy_ == nullptr) ; return backupProxy_; } } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/mock/module_ipc/svc_session_manager_mock.cpp b/tests/mock/module_ipc/svc_session_manager_mock.cpp index 9fca8e48f9c96ad560ccd812f48f46baeacd57d2..ce966d25725f77a76c63f7b79ef4062f3ba8248e 100644 --- a/tests/mock/module_ipc/svc_session_manager_mock.cpp +++ b/tests/mock/module_ipc/svc_session_manager_mock.cpp @@ -18,13 +18,13 @@ #include #include #include -#include #include #include #include #include "b_resources/b_constants.h" #include "ext_extension_mock.h" +#include "../common/include/test_common.h" namespace OHOS::FileManagement::Backup { using namespace std; @@ -64,7 +64,7 @@ ErrCode SvcSessionManager::VerifyBundleName(string &bundleName) sptr SvcSessionManager::GetServiceReverseProxy() { - GTEST_LOG_(INFO) << "GetServiceReverseProxy"; + GTEST_LOG_(INFO) << "GetServiceReverseProxy, clientProxy is null:" << (impl_.clientProxy == nullptr); return impl_.clientProxy; } @@ -95,6 +95,7 @@ UniqueFd SvcSessionManager::OnBundleExtManageInfo(const string &bundleName, Uniq void SvcSessionManager::RemoveExtInfo(const string &bundleName) { GTEST_LOG_(INFO) << "RemoveExtInfo"; + impl_.clientToken = CLEARED_CLIENT_TOKEN; } wptr SvcSessionManager::GetExtConnection(const BundleName &bundleName) @@ -102,9 +103,11 @@ wptr SvcSessionManager::GetExtConnection(const BundleName & GTEST_LOG_(INFO) << "GetExtConnection"; auto it = impl_.backupExtNameMap.find(bundleName); if (it == impl_.backupExtNameMap.end()) { + GTEST_LOG_(INFO) << "GetExtConnection not find bundle"; return nullptr; } if (!it->second.backUpConnection) { + GTEST_LOG_(INFO) << "connection is null, init now!"; auto callDied = [](const string &&bundleName, bool isCleanCalled = false) {}; auto callConnected = [](const string &&bundleName) {}; it->second.backUpConnection = sptr(new SvcBackupConnection(callDied, callConnected, @@ -201,7 +204,7 @@ void SvcSessionManager::SetServiceSchedAction(const string &bundleName, BConstan string SvcSessionManager::GetBackupExtName(const string &bundleName) { GTEST_LOG_(INFO) << "GetBackupExtName " << bundleName; - return "com.example.app2backup"; + return BUNDLE_NAME; } void SvcSessionManager::SetBackupExtInfo(const string &bundleName, const string &extInfo) @@ -224,8 +227,10 @@ std::string SvcSessionManager::GetBackupExtInfo(const string &bundleName) void SvcSessionManager::SetBackupExtName(const string &bundleName, const string &backupExtName) { + GTEST_LOG_(INFO) << "call SetBackupExtName bundleName=" << bundleName << ",extName=" << backupExtName; auto it = impl_.backupExtNameMap.find(bundleName); if (it == impl_.backupExtNameMap.end()) { + GTEST_LOG_(INFO) << "not find"; return; } it->second.backupExtName = backupExtName; @@ -233,6 +238,7 @@ void SvcSessionManager::SetBackupExtName(const string &bundleName, const string std::weak_ptr SvcSessionManager::GetSAExtConnection(const BundleName &bundleName) { + GTEST_LOG_(INFO) << "call GetSAExtConnection"; auto it = impl_.backupExtNameMap.find(bundleName); if (it == impl_.backupExtNameMap.end()) { return std::weak_ptr(); @@ -253,8 +259,13 @@ void SvcSessionManager::AppendBundles(const vector &bundleNames, vec { GTEST_LOG_(INFO) << "AppendBundles"; BackupExtInfo info {}; - info.backupExtName = "com.example.app2backup"; - impl_.backupExtNameMap.insert(make_pair("com.example.app2backup", info)); + info.backupExtName = BUNDLE_NAME; + impl_.backupExtNameMap.insert(make_pair(BUNDLE_NAME, info)); + for (auto bundleName : bundleNames) { + if (bundleName == BUNDLE_NAME_FALSE) { + failedBundles.push_back(BUNDLE_NAME_FALSE); + } + } } sptr SvcSessionManager::CreateBackupConnection(const BundleName &bundleName) @@ -265,6 +276,12 @@ sptr SvcSessionManager::CreateBackupConnection(const Bundle ErrCode SvcSessionManager::Start() { + if (sessionCnt_.load() == START_RETURN_FALSE) { + GTEST_LOG_(INFO) << "call Start false"; + sessionCnt_.store(0); + return BError(BError::Codes::EXT_INVAL_ARG); + } + GTEST_LOG_(INFO) << "call Start ok"; return BError(BError::Codes::OK); } @@ -440,6 +457,10 @@ ErrCode SvcSessionManager::ClearSessionData() bool SvcSessionManager::GetIsIncrementalBackup() { + GTEST_LOG_(INFO) << "call GetIsIncrementalBackup, callerName: " << impl_.callerName; + if (impl_.callerName == BUNDLE_NAME_FALSE) { + return false; + } return true; } @@ -467,6 +488,9 @@ void SvcSessionManager::SetClearDataFlag(const std::string &bundleName, bool isN bool SvcSessionManager::GetClearDataFlag(const std::string &bundleName) { + if (bundleName == BUNDLE_NAME_FALSE) { + return false; + } return true; } diff --git a/tests/mock/utils_mock/include/b_json_clear_data_config_mock.h b/tests/mock/utils_mock/include/b_json_clear_data_config_mock.h index e530d0676d80c8234dd95e33e26cdfd2d0a7cd90..0a484c9287024ca7b4b58a65be834a9b65a5b54d 100644 --- a/tests/mock/utils_mock/include/b_json_clear_data_config_mock.h +++ b/tests/mock/utils_mock/include/b_json_clear_data_config_mock.h @@ -17,7 +17,6 @@ #define OHOS_FILEMGMT_BACKUP_B_JSON_CLEAR_DATA_CONFIG_MOCK_H #include - #include "b_json/b_json_clear_data_config.h" namespace OHOS::FileManagement::Backup { diff --git a/tests/unittests/backup_api/backup_impl/include/service_reverse_mock.h b/tests/unittests/backup_api/backup_impl/include/service_reverse_mock.h index 3a0474f0ccd6d56e99c1eb716da375decb08f02f..55b94b34deb6741a2dc34877990ee728f5926c0a 100644 --- a/tests/unittests/backup_api/backup_impl/include/service_reverse_mock.h +++ b/tests/unittests/backup_api/backup_impl/include/service_reverse_mock.h @@ -26,6 +26,9 @@ namespace OHOS::FileManagement::Backup { class ServiceReverseMock : public IRemoteStub { public: int code_ = 0; + bool restoreBundleStartCalled_ = false; + bool incRestoreBundleStartCalled_ = false; + bool backupOnBundleStartedCalled_ = false; ServiceReverseMock() : code_(0) {} virtual ~ServiceReverseMock() {} @@ -46,6 +49,7 @@ public: ErrCode BackupOnBundleStarted(int32_t errCode, const std::string &bundleName) override { + backupOnBundleStartedCalled_ = true; return BError(BError::Codes::OK); } @@ -76,6 +80,8 @@ public: ErrCode RestoreOnBundleStarted(int32_t errCode, const std::string &bundleName) override { + GTEST_LOG_(INFO) << "call RestoreOnBundleStarted"; + restoreBundleStartCalled_ = true; return BError(BError::Codes::OK); } @@ -156,6 +162,8 @@ public: ErrCode IncrementalRestoreOnBundleStarted(int32_t errCode, const std::string &bundleName) override { + GTEST_LOG_(INFO) << "call IncrementalRestoreOnBundleStarted"; + incRestoreBundleStartCalled_ = true; return BError(BError::Codes::OK); } diff --git a/tests/unittests/backup_sa/module_ipc/BUILD.gn b/tests/unittests/backup_sa/module_ipc/BUILD.gn index db61c87e36a8d757cdfc434b6432db02bf93fd57..676ad460b0ce4a6c041113e964b1cde72d0e01e8 100644 --- a/tests/unittests/backup_sa/module_ipc/BUILD.gn +++ b/tests/unittests/backup_sa/module_ipc/BUILD.gn @@ -85,6 +85,8 @@ ohos_unittest("backup_service_test") { "${path_backup_mock}/accesstoken/accesstoken_kit_mock.cpp", "${path_backup_mock}/module_ipc/app_gallery_dispose_proxy_mock.cpp", "${path_backup_mock}/timer/timer_mock.cpp", + "${path_backup_mock}/utils_mock/src/b_json_clear_data_config_mock.cpp", + "${path_backup_mock}/utils_mock/src/b_json_service_disposal_config_mock.cpp", "${path_backup}/services/backup_sa/src/module_ipc/sa_backup_connection.cpp", "${path_backup}/services/backup_sa/src/module_ipc/service_incremental.cpp", "${path_backup}/services/backup_sa/src/module_ipc/svc_restore_deps_manager.cpp", @@ -104,6 +106,10 @@ ohos_unittest("backup_service_test") { "${path_backup}/services/backup_sa/src/module_ipc", "${path_backup}/tests/unittests/backup_api/backup_impl/include", "${path_backup_mock}/b_process/", + "${path_backup_mock}/module_ipc/include", + "${path_backup_mock}/module_ipc", + "${path_backup_mock}/common/include", + "${path_backup_mock}/utils_mock/include", ] deps = [ diff --git a/tests/unittests/backup_sa/module_ipc/service_test.cpp b/tests/unittests/backup_sa/module_ipc/service_test.cpp index 43fc3037e35c55f8e65598cf98e1a9f297bf3886..d26fbe6076db72df5df5b8239d0a6b84eb66b941 100644 --- a/tests/unittests/backup_sa/module_ipc/service_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_test.cpp @@ -14,13 +14,17 @@ */ #include -#include +#include #include #include #include +#include +#include "b_json_clear_data_config_mock.h" +#include "b_json_service_disposal_config_mock.h" #include "module_ipc/service.h" #include "service_reverse_mock.h" +#include "test_common.h" #include "test_manager.h" #include "service.cpp" @@ -30,24 +34,45 @@ namespace OHOS::FileManagement::Backup { using namespace std; using namespace testing; -namespace { -const string BUNDLE_NAME = "com.example.app2backup"; -const string MANAGE_JSON = "manage.json"; -const string FILE_NAME = "1.tar"; -constexpr int32_t SERVICE_ID = 5203; -} // namespace - class ServiceTest : public testing::Test { public: static void SetUpTestCase(void); static void TearDownTestCase(); - void SetUp() {}; - void TearDown() {}; + void SetUp() + { + totalStat_ = std::make_shared(BizScene::BACKUP, "UT.caller", Mode::FULL); + session_ = sptr(new SvcSessionManager(servicePtr_)); + clearRecorder_ = std::make_shared(); + disposal_ = std::make_shared(); + clearRecorderMock_ = make_shared(); + BJsonClearDataConfigMock::config = clearRecorderMock_; + disposalMock_ = make_shared(); + BBJsonDisposalConfig::config = disposalMock_; + }; + void TearDown() + { + totalStat_ = nullptr; + session_ = nullptr; + clearRecorder_ = nullptr; + disposal_ = nullptr; + clearRecorderMock_ = nullptr; + BJsonClearDataConfigMock::config = nullptr; + disposalMock_ = nullptr; + BBJsonDisposalConfig::config = nullptr; + }; ErrCode Init(IServiceReverseType::Scenario scenario); static inline sptr servicePtr_ = nullptr; static inline sptr remote_ = nullptr; + static inline std::shared_ptr totalStat_ = nullptr; + static inline sptr session_ = nullptr; + static inline std::shared_ptr clearRecorder_ = nullptr; + static inline shared_ptr clearRecorderMock_ = nullptr; + static inline std::shared_ptr disposal_ = nullptr; + static inline shared_ptr disposalMock_ = nullptr; + static inline bool boolVal_ = false; + static inline int intVal_ = 0; }; void ServiceTest::SetUpTestCase(void) @@ -118,15 +143,20 @@ ErrCode ServiceTest::Init(IServiceReverseType::Scenario scenario) * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - * @tc.require: I6F3GV + * @tc.require: mark */ HWTEST_F(ServiceTest, SUB_Service_GetLocalCapabilities_0100, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_GetLocalCapabilities_0100"; try { EXPECT_TRUE(servicePtr_ != nullptr); + int fd1 = 0; + EXPECT_EQ(servicePtr_->GetLocalCapabilities(fd1), 0); UniqueFd fd = servicePtr_->GetLocalCapabilities(); EXPECT_GT(fd, BError(BError::Codes::OK)); + servicePtr_->session_ = nullptr; + EXPECT_EQ(servicePtr_->GetLocalCapabilities().Get(), UniqueFd(-EPERM).Get()); + servicePtr_->session_ = session_; } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by GetLocalCapabilities."; @@ -172,6 +202,16 @@ HWTEST_F(ServiceTest, SUB_Service_OnStart_0100, testing::ext::TestSize.Level1) try { EXPECT_TRUE(servicePtr_ != nullptr); servicePtr_->OnStart(); + servicePtr_->SetOccupySession(true); + EXPECT_TRUE(servicePtr_->isOccupyingSession_.load()); + servicePtr_->OnStart(); + EXPECT_FALSE(servicePtr_->isOccupyingSession_.load()); + servicePtr_->disposal_ = disposal_; + std::vector bundleNames = { BUNDLE_NAME }; + EXPECT_CALL(*disposalMock_, GetBundleNameFromConfigFile()).WillOnce(Return(bundleNames)) + .WillOnce(Return(bundleNames)); + servicePtr_->OnStart(); + EXPECT_FALSE(servicePtr_->isOccupyingSession_.load()); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by OnStart."; @@ -192,9 +232,15 @@ HWTEST_F(ServiceTest, SUB_Service_Start_0100, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_Start_0100"; try { + GTEST_LOG_(INFO) << "1. Start ok"; EXPECT_TRUE(servicePtr_ != nullptr); auto ret = servicePtr_->Start(); EXPECT_EQ(ret, BError(BError::Codes::OK)); + + GTEST_LOG_(INFO) << "2. Start if2"; + servicePtr_->session_ = session_; + session_->sessionCnt_.store(START_RETURN_FALSE); + EXPECT_NE(servicePtr_->Start(), BError(BError::Codes::OK)); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by Start."; @@ -512,6 +558,44 @@ HWTEST_F(ServiceTest, SUB_Service_ServiceResultReport_0000, testing::ext::TestSi GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_ServiceResultReport_0000"; } +/** + * @tc.number: SUB_Service_SAResultReport_0000 + * @tc.name: SUB_Service_SAResultReport_0000 + * @tc.desc: 测试 SAResultReport 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ServiceTest, SUB_Service_SAResultReport_0000, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_SAResultReport_0000"; + try { + GTEST_LOG_(INFO) << "SUB_Service_SAResultReport Branches Start"; + string bundleName = BUNDLE_NAME; + string restoreRetInfo; + int errCode = 0; + EXPECT_TRUE(servicePtr_ != nullptr); + auto ret = servicePtr_->SAResultReport(BUNDLE_NAME, restoreRetInfo, errCode, + BackupRestoreScenario::FULL_RESTORE); + EXPECT_EQ(ret, 0); + + ret = servicePtr_->SAResultReport(BUNDLE_NAME, restoreRetInfo, errCode, + BackupRestoreScenario::INCREMENTAL_RESTORE); + EXPECT_EQ(ret, 0); + + ret = servicePtr_->SAResultReport(BUNDLE_NAME, restoreRetInfo, errCode, BackupRestoreScenario::FULL_BACKUP); + EXPECT_EQ(ret, 0); + + ret = servicePtr_->SAResultReport(BUNDLE_NAME, restoreRetInfo, errCode, + BackupRestoreScenario::INCREMENTAL_BACKUP); + EXPECT_EQ(ret, 0); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by SAResultReport."; + } + GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_SAResultReport_0000"; +} + /** * @tc.number: SUB_Service_LaunchBackupExtension_0100 * @tc.name: SUB_Service_LaunchBackupExtension_0100 @@ -715,16 +799,32 @@ HWTEST_F(ServiceTest, SUB_Service_ExtStart_0100, testing::ext::TestSize.Level1) { GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_ExtStart_0100"; try { - GTEST_LOG_(INFO) << "SUB_Service_ExtStart_0100 BACKUP"; - ErrCode ret = Init(IServiceReverseType::Scenario::BACKUP); - EXPECT_EQ(ret, BError(BError::Codes::OK)); + GTEST_LOG_(INFO) << "1.ExtStart Incr BackupSA"; EXPECT_TRUE(servicePtr_ != nullptr); + servicePtr_->session_->impl_.callerName = BUNDLE_NAME; + servicePtr_->ExtStart(SA_BUNDLE_NAME); + + GTEST_LOG_(INFO) << "2.ExtStart BackupSA"; + servicePtr_->session_->impl_.callerName = BUNDLE_NAME_FALSE; + servicePtr_->session_->impl_.clientProxy = static_cast>(remote_); + servicePtr_->session_->impl_.scenario = IServiceReverseType::Scenario::RESTORE; + servicePtr_->ExtStart(SA_BUNDLE_NAME); + + GTEST_LOG_(INFO) << "3.ExtStart IncrementalBackup"; servicePtr_->ExtStart(BUNDLE_NAME); - GTEST_LOG_(INFO) << "ServiceTest-ExtStart BACKUP Branches"; - servicePtr_->ExtStart(BUNDLE_NAME); - GTEST_LOG_(INFO) << "SUB_Service_ExtStart_0100 RESTORE"; - ret = Init(IServiceReverseType::Scenario::RESTORE); + + GTEST_LOG_(INFO) << "4.ExtStart StartCurBundleBackupOrRestore"; + servicePtr_->session_->impl_.callerName = BUNDLE_NAME_FALSE; + servicePtr_->session_->impl_.scenario = IServiceReverseType::Scenario::BACKUP; + servicePtr_->session_->impl_.clientProxy = static_cast>(remote_); + remote_->backupOnBundleStartedCalled_ = false; + BackupExtInfo extInfo {}; + extInfo.backUpConnection = nullptr; + extInfo.versionName = "0.0.0.0-0.0.0.0"; + servicePtr_->session_->impl_.backupExtNameMap[BUNDLE_NAME] = extInfo; servicePtr_->ExtStart(BUNDLE_NAME); + EXPECT_TRUE(remote_->backupOnBundleStartedCalled_); + } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by ExtStart."; @@ -781,6 +881,55 @@ HWTEST_F(ServiceTest, SUB_Service_ExtStart_0101, testing::ext::TestSize.Level1) GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_ExtStart_0101"; } +/** + * @tc.number: SUB_Service_StartCurBundleBackupOrRestore_0100 + * @tc.name: SUB_Service_StartCurBundleBackupOrRestore_0100 + * @tc.desc: 测试 StartCurBundleBackupOrRestore 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(ServiceTest, SUB_Service_StartCurBundleBackupOrRestore_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_StartCurBundleBackupOrRestore_0100"; + try { + EXPECT_TRUE(servicePtr_ != nullptr); + GTEST_LOG_(INFO) << "1.StartCurBundleBackupOrRestore null1"; + servicePtr_->session_->impl_.backupExtNameMap.erase(BUNDLE_NAME); + servicePtr_->StartCurBundleBackupOrRestore(BUNDLE_NAME); + + GTEST_LOG_(INFO) << "2.StartCurBundleBackupOrRestore null2"; + BackupExtInfo extInfo {}; + auto callDied = [](const string &&bundleName, bool isCleanCalled) {}; + auto callConnected = [](const string &&bundleName) {}; + string bundleNameIndexInfo = "123456789"; + auto connection = sptr(new SvcBackupConnection(callDied, callConnected, bundleNameIndexInfo)); + extInfo.backUpConnection = connection; + connection->backupProxy_ = nullptr; + servicePtr_->session_->impl_.backupExtNameMap[BUNDLE_NAME] = extInfo; + servicePtr_->session_->impl_.clientProxy = static_cast>(remote_); + servicePtr_->StartCurBundleBackupOrRestore(BUNDLE_NAME); + + GTEST_LOG_(INFO) << "3.StartCurBundleBackupOrRestore ok backup"; + servicePtr_->session_->impl_.backupExtNameMap[BUNDLE_NAME].backUpConnection = nullptr; + servicePtr_->session_->impl_.scenario = IServiceReverseType::Scenario::BACKUP; + servicePtr_->StartCurBundleBackupOrRestore(BUNDLE_NAME); + + GTEST_LOG_(INFO) << "4.StartCurBundleBackupOrRestore ok restore"; + servicePtr_->session_->impl_.callerName = BUNDLE_NAME_FALSE; + servicePtr_->session_->impl_.scenario = IServiceReverseType::Scenario::RESTORE; + remote_->restoreBundleStartCalled_ = false; + servicePtr_->StartCurBundleBackupOrRestore(BUNDLE_NAME); + EXPECT_TRUE(remote_->restoreBundleStartCalled_); + + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by StartCurBundleBackupOrRestore."; + } + GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_StartCurBundleBackupOrRestore_0100"; +} + /** * @tc.number: SUB_Service_Dump_0100 * @tc.name: SUB_Service_Dump_0100 @@ -903,7 +1052,26 @@ HWTEST_F(ServiceTest, SUB_Service_ExtConnectDone_0100, testing::ext::TestSize.Le GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_ExtConnectDone_0100"; try { EXPECT_TRUE(servicePtr_ != nullptr); + std::vector bundles = { BUNDLE_NAME }; + session_->AppendBundles(bundles, bundles); + servicePtr_->session_ = session_; + session_->impl_.backupExtNameMap[BUNDLE_NAME].schedAction = BConstants::ServiceSchedAction::UNKNOWN; servicePtr_->ExtConnectDone(BUNDLE_NAME); + session_->impl_.backupExtNameMap[BUNDLE_NAME].schedAction = BConstants::ServiceSchedAction::CLEAN; + servicePtr_->ExtConnectDone(BUNDLE_NAME); + session_->impl_.backupExtNameMap[BUNDLE_NAME].schedAction = BConstants::ServiceSchedAction::START; + servicePtr_->clearRecorder_ = clearRecorder_; + boolVal_ = true; + EXPECT_CALL(*clearRecorderMock_, FindClearBundleRecord(_)).WillOnce(Return(true)) + .WillRepeatedly(ActionUpdateRetVal(&boolVal_, false)); + servicePtr_->ExtConnectDone(BUNDLE_NAME); + servicePtr_->totalStatistic_ = nullptr; + session_->impl_.backupExtNameMap[BUNDLE_NAME].schedAction = BConstants::ServiceSchedAction::START; + servicePtr_->ExtConnectDone(BUNDLE_NAME); + servicePtr_->totalStatistic_ = totalStat_; + servicePtr_->ExtConnectDone(BUNDLE_NAME); + servicePtr_->ExtConnectDone(BUNDLE_NAME_FALSE); + EXPECT_FALSE(boolVal_); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by ExtConnectDone."; @@ -1068,28 +1236,14 @@ HWTEST_F(ServiceTest, SUB_Service_SendStartAppGalleryNotify_0100, testing::ext:: GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_SendStartAppGalleryNotify_0100"; try { EXPECT_TRUE(servicePtr_ != nullptr); - BundleName bundleName = ""; - servicePtr_->SendStartAppGalleryNotify(bundleName); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by SendStartAppGalleryNotify."; - } - GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_SendStartAppGalleryNotify_0100"; -} + GTEST_LOG_(INFO) << "1. SendStartAppGalleryNotify null1"; + servicePtr_->SendStartAppGalleryNotify(SA_BUNDLE_NAME); -/** - * @tc.number: SUB_Service_SendStartAppGalleryNotify_0101 - * @tc.name: SUB_Service_SendStartAppGalleryNotify_0101 - * @tc.desc: 测试 SendStartAppGalleryNotify 接口 - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: I8ZIMJ - */ -HWTEST_F(ServiceTest, SUB_Service_SendStartAppGalleryNotify_0101, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_SendStartAppGalleryNotify_0101"; - try { + GTEST_LOG_(INFO) << "2. SendStartAppGalleryNotify not restore"; + servicePtr_->session_->impl_.scenario = IServiceReverseType::Scenario::BACKUP; + servicePtr_->SendStartAppGalleryNotify(BUNDLE_NAME); + + GTEST_LOG_(INFO) << "3. SendStartAppGalleryNotify ok"; ErrCode ret = Init(IServiceReverseType::Scenario::RESTORE); EXPECT_EQ(ret, BError(BError::Codes::OK)); EXPECT_TRUE(servicePtr_ != nullptr); @@ -1098,7 +1252,7 @@ HWTEST_F(ServiceTest, SUB_Service_SendStartAppGalleryNotify_0101, testing::ext:: EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by SendStartAppGalleryNotify."; } - GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_SendStartAppGalleryNotify_0101"; + GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_SendStartAppGalleryNotify_0100"; } /** @@ -1108,7 +1262,7 @@ HWTEST_F(ServiceTest, SUB_Service_SendStartAppGalleryNotify_0101, testing::ext:: * @tc.size: MEDIUM * @tc.type: FUNC * @tc.level Level 1 - * @tc.require: I8ZIMJ + * @tc.require: I8ZIMJ todo */ HWTEST_F(ServiceTest, SUB_Service_SessionDeactive_0100, testing::ext::TestSize.Level1) { @@ -1303,11 +1457,20 @@ HWTEST_F(ServiceTest, SUB_Service_OnBundleStarted_0100, testing::ext::TestSize.L wptr reversePtr(new Service(saID)); sptr session(new SvcSessionManager(reversePtr)); EXPECT_TRUE(servicePtr_ != nullptr); + session->impl_.clientProxy = static_cast>(remote_); + GTEST_LOG_(INFO) << "1. OnBundleStarted backup"; + session->impl_.scenario = IServiceReverseType::Scenario::BACKUP; servicePtr_->OnBundleStarted(BError(BError::Codes::SA_INVAL_ARG), session, BUNDLE_NAME); - SvcSessionManager::Impl impl_; - impl_.clientToken = 1; - impl_.scenario = IServiceReverseType::Scenario::RESTORE; + + GTEST_LOG_(INFO) << "2. OnBundleStarted inc restore"; + session->impl_.scenario = IServiceReverseType::Scenario::RESTORE; servicePtr_->OnBundleStarted(BError(BError::Codes::SA_INVAL_ARG), session, BUNDLE_NAME); + EXPECT_TRUE(remote_->incRestoreBundleStartCalled_); + + GTEST_LOG_(INFO) << "3. OnBundleStarted full restore"; + session->impl_.restoreDataType = RestoreTypeEnum::RESTORE_DATA_READDY; + servicePtr_->OnBundleStarted(BError(BError::Codes::SA_INVAL_ARG), session, BUNDLE_NAME); + EXPECT_TRUE(remote_->restoreBundleStartCalled_); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by OnBundleStarted."; @@ -1472,7 +1635,7 @@ HWTEST_F(ServiceTest, SUB_Service_SetCurrentSessProperties_0102, testing::ext::T std::vector restoreBundleInfos {aInfo}; std::vector restoreBundleNames {"123456"}; RestoreTypeEnum restoreType = RESTORE_DATA_READDY; - std::string backupVersion; + std::string backupVersion = ""; EXPECT_TRUE(servicePtr_ != nullptr); servicePtr_->SetCurrentSessProperties(restoreBundleInfos, restoreBundleNames, restoreType, backupVersion); @@ -1497,6 +1660,13 @@ HWTEST_F(ServiceTest, SUB_Service_SetCurrentSessProperties_0102, testing::ext::T aInfo.name = "123456"; restoreBundleInfos.push_back(aInfo); servicePtr_->SetCurrentSessProperties(restoreBundleInfos, restoreBundleNames, restoreType, backupVersion); + + restoreBundleInfos.clear(); + aInfo.name = "123456"; + aInfo.appIndex = 2; + restoreBundleInfos.push_back(aInfo); + servicePtr_->SetCurrentSessProperties(restoreBundleInfos, restoreBundleNames, restoreType, backupVersion); + EXPECT_EQ(servicePtr_->session_->impl_.oldBackupVersion, backupVersion); } catch (...) { EXPECT_TRUE(true); GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by SetCurrentSessProperties."; @@ -1517,21 +1687,199 @@ HWTEST_F(ServiceTest, SUB_Service_AppendBundlesRestoreSession_0100, testing::ext { GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_AppendBundlesRestoreSession_0100"; try { + servicePtr_->isOccupyingSession_.store(false); + servicePtr_->session_ = session_; UniqueFd fd = servicePtr_->GetLocalCapabilities(); EXPECT_GE(fd, BError(BError::Codes::OK)); vector bundleNames {}; RestoreTypeEnum restoreType = RESTORE_DATA_READDY; int32_t userId = 1; EXPECT_TRUE(servicePtr_ != nullptr); - auto ret = servicePtr_->AppendBundlesRestoreSession(move(fd), bundleNames, restoreType, userId); - EXPECT_EQ(ret, BError(BError::Codes::OK)); + GTEST_LOG_(INFO) << "1. AppendBundlesRestoreSession fail nullptr"; + servicePtr_->session_ = nullptr; + int ret = servicePtr_->AppendBundlesRestoreSession(move(fd), bundleNames, restoreType, userId); + EXPECT_EQ(ret, BError(BError::Codes::SA_INVAL_ARG).GetCode()); + + GTEST_LOG_(INFO) << "2. AppendBundlesRestoreSession fail false"; + servicePtr_->session_ = session_; + servicePtr_->isOccupyingSession_.store(true); + ret = servicePtr_->AppendBundlesRestoreSession(move(fd), bundleNames, restoreType, userId); + EXPECT_EQ(ret, BError(BError::Codes::SA_INVAL_ARG).GetCode()); + + GTEST_LOG_(INFO) << "3. AppendBundlesRestoreSession succ"; + servicePtr_->isOccupyingSession_.store(false); + UniqueFd fd2 = servicePtr_->GetLocalCapabilities(); + ret = servicePtr_->AppendBundlesRestoreSession(move(fd2), bundleNames, restoreType, userId); + EXPECT_EQ(ret, 0); + + GTEST_LOG_(INFO) << "4. AppendBundlesRestoreSessionData succ 1"; + UniqueFd fd3 = servicePtr_->GetLocalCapabilities(); + ret = servicePtr_->AppendBundlesRestoreSessionData(fd3.Get(), bundleNames, restoreType, userId); + EXPECT_EQ(ret, 0); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by AppendBundlesRestoreSession."; + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by AppendBundlesRestoreSession_01."; } GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_AppendBundlesRestoreSession_0100"; } +/** + * @tc.number: SUB_Service_AppendBundlesRestoreSession_0101 + * @tc.name: SUB_Service_AppendBundlesRestoreSession_0101 + * @tc.desc: 测试 AppendBundlesRestoreSession 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ServiceTest, SUB_Service_AppendBundlesRestoreSession_0101, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_AppendBundlesRestoreSession_0101"; + try { + servicePtr_->isOccupyingSession_.store(false); + servicePtr_->session_ = session_; + UniqueFd fd = servicePtr_->GetLocalCapabilities(); + EXPECT_GE(fd, BError(BError::Codes::OK)); + vector bundleNames {}; + RestoreTypeEnum restoreType = RESTORE_DATA_READDY; + int32_t userId = 1; + EXPECT_TRUE(servicePtr_ != nullptr); + vector detailInfos; + string json = "[{\"infos\":[{" + "\"details\":[{" + "\"detail\":[{" + "\"source\":\"com.app.demo001\"," + "\"target\":\"com.example.fileonrestoreex\"" + "}]," + "\"type\":\"app_mapping_relation\"" + "}]," + "\"type\":\"broadcast\"" + "}]" + "}]"; + detailInfos.emplace_back(json); + GTEST_LOG_(INFO) << "1. AppendBundlesRestoreSession fail nullptr"; + servicePtr_->session_ = nullptr; + int ret = servicePtr_->AppendBundlesRestoreSession(move(fd), bundleNames, detailInfos, restoreType, userId); + EXPECT_EQ(ret, BError(BError::Codes::SA_INVAL_ARG).GetCode()); + + GTEST_LOG_(INFO) << "2. AppendBundlesRestoreSession fail false"; + servicePtr_->session_ = session_; + servicePtr_->isOccupyingSession_.store(true); + ret = servicePtr_->AppendBundlesRestoreSession(move(fd), bundleNames, detailInfos, restoreType, userId); + EXPECT_EQ(ret, BError(BError::Codes::SA_INVAL_ARG).GetCode()); + + GTEST_LOG_(INFO) << "3. AppendBundlesRestoreSession succ"; + servicePtr_->isOccupyingSession_.store(false); + UniqueFd fd2 = servicePtr_->GetLocalCapabilities(); + ret = servicePtr_->AppendBundlesRestoreSession(move(fd2), bundleNames, detailInfos, restoreType, userId); + EXPECT_EQ(ret, 0); + + GTEST_LOG_(INFO) << "4. AppendBundlesRestoreSessionDataByDetail succ"; + UniqueFd fd3 = servicePtr_->GetLocalCapabilities(); + ret = servicePtr_->AppendBundlesRestoreSessionDataByDetail( + fd3.Get(), bundleNames, detailInfos, restoreType, userId + ); + EXPECT_EQ(ret, 0); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by SUB_Service_AppendBundlesRestoreSession_0101."; + } + GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_AppendBundlesRestoreSession_0101"; +} + +/** + * @tc.number: SUB_Service_AppendBundlesBackupSession_0100 + * @tc.name: SUB_Service_AppendBundlesBackupSession_0100 + * @tc.desc: 测试 AppendBundlesBackupSession 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ServiceTest, SUB_Service_AppendBundlesBackupSession_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_AppendBundlesBackupSession_0100"; + vector bundleNames { BUNDLE_NAME }; + EXPECT_TRUE(servicePtr_ != nullptr); + GTEST_LOG_(INFO) << "1. AppendBundlesBackupSession fail nullptr"; + servicePtr_->session_ = nullptr; + int ret = servicePtr_->AppendBundlesBackupSession(bundleNames); + EXPECT_EQ(ret, BError(BError::Codes::SA_INVAL_ARG).GetCode()); + + GTEST_LOG_(INFO) << "2. AppendBundlesBackupSession fail false"; + servicePtr_->session_ = session_; + servicePtr_->isOccupyingSession_.store(true); + ret = servicePtr_->AppendBundlesBackupSession(bundleNames); + EXPECT_EQ(ret, BError(BError::Codes::SA_INVAL_ARG).GetCode()); + + GTEST_LOG_(INFO) << "3. AppendBundlesBackupSession succ"; + servicePtr_->isOccupyingSession_.store(false); + ret = servicePtr_->AppendBundlesBackupSession(bundleNames); + EXPECT_EQ(ret, 0); + GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_AppendBundlesBackupSession_0100"; +} + +/** + * @tc.number: SUB_Service_AppendBundlesDetailsBackupSession_0100 + * @tc.name: SUB_Service_AppendBundlesDetailsBackupSession_0100 + * @tc.desc: 测试 AppendBundlesDetailsBackupSession 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ServiceTest, SUB_Service_AppendBundlesDetailsBackupSession_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_AppendBundlesDetailsBackupSession_0100"; + std::vector bundleNames { BUNDLE_NAME }; + std::vector bundleInfos; + bundleInfos.push_back(""); + EXPECT_TRUE(servicePtr_ != nullptr); + GTEST_LOG_(INFO) << "1. AppendBundlesDetailsBackupSession fail nullptr"; + servicePtr_->session_ = nullptr; + int ret = servicePtr_->AppendBundlesDetailsBackupSession(bundleNames, bundleInfos); + EXPECT_EQ(ret, 13900020); + + GTEST_LOG_(INFO) << "2. AppendBundlesDetailsBackupSession fail false"; + servicePtr_->session_ = session_; + servicePtr_->isOccupyingSession_.store(true); + ret = servicePtr_->AppendBundlesDetailsBackupSession(bundleNames, bundleInfos); + EXPECT_EQ(ret, 13900020); + + GTEST_LOG_(INFO) << "3. AppendBundlesDetailsBackupSession succ"; + servicePtr_->isOccupyingSession_.store(false); + ret = servicePtr_->AppendBundlesDetailsBackupSession(bundleNames, bundleInfos); + EXPECT_EQ(ret, 0); + GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_AppendBundlesDetailsBackupSession_0100"; +} + +/** + * @tc.number: SUB_Service_HandleCurGroupBackupInfos_0100 + * @tc.name: SUB_Service_HandleCurGroupBackupInfos_0100 + * @tc.desc: 测试 HandleCurGroupBackupInfos 接口 + * @tc.size: SMALL + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ServiceTest, SUB_Service_HandleCurGroupBackupInfos_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_HandleCurGroupBackupInfos_0100"; + std::vector backupInfos; + std::map> bundleNameDetailMap; + std::map isClearDataFlags; + BJsonEntityCaps::BundleInfo bundleInfo; + bundleInfo.name = BUNDLE_NAME; + bundleInfo.appIndex = 0; + bundleInfo.extensionName = "extensionNameTest"; + backupInfos.push_back(bundleInfo); + EXPECT_TRUE(servicePtr_ != nullptr); + GTEST_LOG_(INFO) << "1. HandleCurGroupBackupInfos"; + servicePtr_->session_ = session_; + BackupExtInfo extInfo; + session_->impl_.backupExtNameMap.emplace(BUNDLE_NAME, extInfo); + servicePtr_->HandleCurGroupBackupInfos(backupInfos, bundleNameDetailMap, isClearDataFlags); + EXPECT_EQ(session_->impl_.backupExtNameMap[BUNDLE_NAME].backupExtName, bundleInfo.extensionName); + + GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_HandleCurGroupBackupInfos_0100"; +} + /** * @tc.number: SUB_Service_HandleCurBundleEndWork_0100 * @tc.name: SUB_Service_HandleCurBundleEndWork_0100 @@ -1545,14 +1893,48 @@ HWTEST_F(ServiceTest, SUB_Service_HandleCurBundleEndWork_0100, testing::ext::Tes { GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_HandleCurBundleEndWork_0100"; try { - SvcSessionManager::Impl impl_; - impl_.clientToken = 1; - BackupExtInfo extInfo {}; - extInfo.backUpConnection = nullptr; - impl_.backupExtNameMap[BUNDLE_NAME] = extInfo; - impl_.scenario = IServiceReverseType::Scenario::RESTORE; EXPECT_TRUE(servicePtr_ != nullptr); + GTEST_LOG_(INFO) << "1. HandleCurBundleEndWork backup"; + servicePtr_->session_ = session_; + servicePtr_->HandleCurBundleEndWork(BUNDLE_NAME, BackupRestoreScenario::FULL_BACKUP); + + GTEST_LOG_(INFO) << "2. HandleCurBundleEndWork nullptr1"; + servicePtr_->backupExtMutexMap_[BUNDLE_NAME] = nullptr; + if (session_->OnBundleFileReady(BUNDLE_NAME)) { + session_->OnBundleFileReady(BUNDLE_NAME); + } + servicePtr_->HandleCurBundleEndWork(BUNDLE_NAME, BackupRestoreScenario::FULL_RESTORE); + + GTEST_LOG_(INFO) << "3. HandleCurBundleEndWork nullptr2"; + servicePtr_->backupExtMutexMap_[BUNDLE_NAME] = std::make_shared(BUNDLE_NAME); + session_->impl_.backupExtNameMap.erase(BUNDLE_NAME); + if (session_->OnBundleFileReady(BUNDLE_NAME)) { + session_->OnBundleFileReady(BUNDLE_NAME); + } + servicePtr_->HandleCurBundleEndWork(BUNDLE_NAME, BackupRestoreScenario::FULL_RESTORE); + + GTEST_LOG_(INFO) << "4. HandleCurBundleEndWork nullptr3"; + auto callDied = [](const string &&bundleName, bool isCleanCalled) {}; + auto callConnected = [](const string &&bundleName) {}; + string bundleNameIndexInfo = "123456789"; + auto connection = sptr(new SvcBackupConnection(callDied, callConnected, bundleNameIndexInfo)); + BackupExtInfo extInfo {}; + extInfo.backUpConnection = connection; + extInfo.backUpConnection->backupProxy_ = nullptr; + session_->impl_.backupExtNameMap[BUNDLE_NAME] = extInfo; + if (session_->OnBundleFileReady(BUNDLE_NAME)) { + session_->OnBundleFileReady(BUNDLE_NAME); + } + servicePtr_->HandleCurBundleEndWork(BUNDLE_NAME, BackupRestoreScenario::FULL_RESTORE); + + GTEST_LOG_(INFO) << "5. HandleCurBundleEndWork ok"; + servicePtr_->backupExtMutexMap_[BUNDLE_NAME] = std::make_shared(BUNDLE_NAME); + session_->impl_.backupExtNameMap[BUNDLE_NAME].backUpConnection = nullptr; + if (session_->OnBundleFileReady(BUNDLE_NAME)) { + session_->OnBundleFileReady(BUNDLE_NAME); + } servicePtr_->HandleCurBundleEndWork(BUNDLE_NAME, BackupRestoreScenario::FULL_RESTORE); + EXPECT_EQ(session_->impl_.clientToken, CLEARED_CLIENT_TOKEN); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by HandleCurBundleEndWork."; @@ -1596,11 +1978,21 @@ HWTEST_F(ServiceTest, SUB_Service_LaunchBackupSAExtension_0100, testing::ext::Te impl_.backupExtNameMap[BUNDLE_NAME] = extInfo; ret = servicePtr_->LaunchBackupSAExtension(bundleName); EXPECT_NE(ret, BError(BError::Codes::OK)); - + GTEST_LOG_(INFO) << "1. LaunchBackupSAExtension other"; ret = Init(IServiceReverseType::Scenario::BACKUP); EXPECT_EQ(ret, BError(BError::Codes::OK)); ret = servicePtr_->LaunchBackupSAExtension(bundleName); EXPECT_NE(ret, BError(BError::Codes::OK)); + + GTEST_LOG_(INFO) << "2. LaunchBackupSAExtension BACKUP"; + servicePtr_->session_->impl_.scenario = IServiceReverseType::Scenario::BACKUP; + ret = servicePtr_->LaunchBackupSAExtension(bundleName); + EXPECT_EQ(ret, BError(BError::Codes::SA_INVAL_ARG).GetCode()); + + GTEST_LOG_(INFO) << "3. LaunchBackupSAExtension RESTORE"; + servicePtr_->session_->impl_.scenario = IServiceReverseType::Scenario::RESTORE; + ret = servicePtr_->LaunchBackupSAExtension(bundleName); + EXPECT_EQ(ret, BError(BError::Codes::SA_INVAL_ARG).GetCode()); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by LaunchBackupSAExtension."; @@ -1721,14 +2113,23 @@ HWTEST_F(ServiceTest, SUB_Service_SendEndAppGalleryNotify_0100, testing::ext::Te { GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_SendEndAppGalleryNotify_0100"; try { + GTEST_LOG_(INFO) << "1. SendEndAppGalleryNotify backup"; ErrCode ret = Init(IServiceReverseType::Scenario::BACKUP); EXPECT_EQ(ret, BError(BError::Codes::OK)); EXPECT_TRUE(servicePtr_ != nullptr); servicePtr_->SendEndAppGalleryNotify(BUNDLE_NAME); + GTEST_LOG_(INFO) << "2. SendEndAppGalleryNotify restore"; ret = Init(IServiceReverseType::Scenario::RESTORE); - EXPECT_EQ(ret, BError(BError::Codes::OK)); servicePtr_->SendEndAppGalleryNotify(BUNDLE_NAME); + + GTEST_LOG_(INFO) << "3. SendEndAppGalleryNotify disposal false"; + servicePtr_->disposal_ = disposal_; + boolVal_ = true; + EXPECT_CALL(*disposalMock_, DeleteFromDisposalConfigFile(_)) + .WillRepeatedly(ActionUpdateRetVal(&boolVal_, false)); + servicePtr_->SendEndAppGalleryNotify(BUNDLE_NAME); + EXPECT_FALSE(boolVal_); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by SendEndAppGalleryNotify."; @@ -1736,6 +2137,39 @@ HWTEST_F(ServiceTest, SUB_Service_SendEndAppGalleryNotify_0100, testing::ext::Te GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_SendEndAppGalleryNotify_0100"; } +/** + * @tc.number: SUB_Service_TryToClearDispose_0100 + * @tc.name: SUB_Service_TryToClearDispose_0100 + * @tc.desc: 测试 TryToClearDispose 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(ServiceTest, SUB_Service_TryToClearDispose_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_TryToClearDispose_0100"; + try { + EXPECT_TRUE(servicePtr_ != nullptr); + GTEST_LOG_(INFO) << "1. TryToClearDispose empty"; + servicePtr_->TryToClearDispose(EMPTY_BUNDLE_NAME); + + GTEST_LOG_(INFO) << "2. TryToClearDispose ok"; + servicePtr_->TryToClearDispose(BUNDLE_NAME); + + GTEST_LOG_(INFO) << "3. TryToClearDispose disposal false"; + servicePtr_->disposal_ = disposal_; + boolVal_ = true; + EXPECT_CALL(*disposalMock_, DeleteFromDisposalConfigFile(_)) + .WillRepeatedly(ActionUpdateRetVal(&boolVal_, false)); + servicePtr_->TryToClearDispose(BUNDLE_NAME); + EXPECT_FALSE(boolVal_); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by TryToClearDispose."; + } + GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_TryToClearDispose_0100"; +} + /** * @tc.number: SUB_Service_SendErrAppGalleryNotify_0100 * @tc.name: SUB_Service_SendErrAppGalleryNotify_0100 @@ -1777,14 +2211,18 @@ HWTEST_F(ServiceTest, SUB_Service_ClearDisposalOnSaStart_0100, testing::ext::Tes { GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_ClearDisposalOnSaStart_0100"; try { + GTEST_LOG_(INFO) << "1. ClearDisposalOnSaStart backup"; ErrCode ret = Init(IServiceReverseType::Scenario::BACKUP); EXPECT_EQ(ret, BError(BError::Codes::OK)); EXPECT_TRUE(servicePtr_ != nullptr); servicePtr_->ClearDisposalOnSaStart(); - ret = Init(IServiceReverseType::Scenario::RESTORE); - EXPECT_EQ(ret, BError(BError::Codes::OK)); + GTEST_LOG_(INFO) << "2. ClearDisposalOnSaStart disposal mock"; + servicePtr_->disposal_ = disposal_; + std::vector bundleNames = { BUNDLE_NAME }; + EXPECT_CALL(*disposalMock_, GetBundleNameFromConfigFile()).WillRepeatedly(Return(bundleNames)); servicePtr_->ClearDisposalOnSaStart(); + EXPECT_TRUE(servicePtr_ != nullptr); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by ClearDisposalOnSaStart."; @@ -1805,14 +2243,28 @@ HWTEST_F(ServiceTest, SUB_Service_DeleteDisConfigFile_0100, testing::ext::TestSi { GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_DeleteDisConfigFile_0100"; try { - ErrCode ret = Init(IServiceReverseType::Scenario::BACKUP); - EXPECT_EQ(ret, BError(BError::Codes::OK)); - EXPECT_TRUE(servicePtr_ != nullptr); + GTEST_LOG_(INFO) << "1. DeleteDisConfigFile backup"; + servicePtr_->session_->impl_.scenario = IServiceReverseType::Scenario::BACKUP; servicePtr_->DeleteDisConfigFile(); - ret = Init(IServiceReverseType::Scenario::RESTORE); - EXPECT_EQ(ret, BError(BError::Codes::OK)); + GTEST_LOG_(INFO) << "2. DeleteDisConfigFile restore if1"; + servicePtr_->session_->impl_.scenario = IServiceReverseType::Scenario::RESTORE; + servicePtr_->disposal_ = disposal_; + std::vector bundleNames = { BUNDLE_NAME }; + std::vector empty = {}; + EXPECT_CALL(*disposalMock_, GetBundleNameFromConfigFile()).WillOnce(Return(bundleNames)) + .WillRepeatedly(Return(empty)); + boolVal_ = true; + servicePtr_->DeleteDisConfigFile(); + + GTEST_LOG_(INFO) << "3. DeleteDisConfigFile restore if2"; + EXPECT_CALL(*disposalMock_, DeleteConfigFile()).WillOnce(Return(true)) + .WillRepeatedly(ActionUpdateRetVal(&boolVal_, false)); servicePtr_->DeleteDisConfigFile(); + + GTEST_LOG_(INFO) << "4. DeleteDisConfigFile restore"; + servicePtr_->DeleteDisConfigFile(); + EXPECT_FALSE(boolVal_); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by DeleteDisConfigFile."; @@ -1838,13 +2290,16 @@ HWTEST_F(ServiceTest, SUB_Service_ExtensionConnectFailRadarReport_0100, testing: EXPECT_TRUE(servicePtr_ != nullptr); servicePtr_->ClearBundleRadarReport(); servicePtr_->ExtensionConnectFailRadarReport(BUNDLE_NAME, BError(BError::Codes::OK), - IServiceReverseType::Scenario::BACKUP); + IServiceReverseType::Scenario::BACKUP); EXPECT_TRUE(true); ret = Init(IServiceReverseType::Scenario::RESTORE); EXPECT_EQ(ret, BError(BError::Codes::OK)); servicePtr_->ExtensionConnectFailRadarReport(BUNDLE_NAME, BError(BError::Codes::OK), - IServiceReverseType::Scenario::RESTORE); + IServiceReverseType::Scenario::RESTORE); + servicePtr_->UpdateBundleRadarReport(BUNDLE_NAME); + servicePtr_->ExtensionConnectFailRadarReport(BUNDLE_NAME, BError(BError::Codes::OK), + IServiceReverseType::Scenario::RESTORE); EXPECT_TRUE(true); } catch (...) { EXPECT_TRUE(false); @@ -2088,4 +2543,27 @@ HWTEST_F(ServiceTest, SUB_Service_AppFileReady_0104, testing::ext::TestSize.Leve } GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_AppFileReady_0104"; } + +HWTEST_F(ServiceTest, Service_Total_Start, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin Service_Total_Start"; + servicePtr_->totalStatistic_ = nullptr; + servicePtr_->TotalStart(); + servicePtr_->totalStatistic_ = totalStat_; + servicePtr_->TotalStart(); + EXPECT_GT(totalStat_->totalSpendTime_.startMilli_, 0); + GTEST_LOG_(INFO) << "ServiceTest-end Service_Total_Start"; +} + +HWTEST_F(ServiceTest, Service_Total_Stat_Report, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin Service_Total_Stat_Report"; + servicePtr_->totalStatistic_ = nullptr; + servicePtr_->TotalStatReport(ERR_OK); + servicePtr_->totalStatistic_ = totalStat_; + servicePtr_->TotalStatReport(ERR_OK); + EXPECT_GT(totalStat_->totalSpendTime_.endMilli_, 0); + GTEST_LOG_(INFO) << "ServiceTest-end Service_Total_Stat_Report"; +} + } // namespace OHOS::FileManagement::Backup \ No newline at end of file