From 5739f611e56ec15dadd416b3a02fe3f1197ccf5e Mon Sep 17 00:00:00 2001 From: huaqingsimeng Date: Sat, 23 Dec 2023 14:54:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8DRelease?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huaqingsimeng --- .../backup_kit_inner/src/b_session_backup.cpp | 10 +++++ .../src/b_session_restore.cpp | 10 +++++ .../backup_kit_inner/src/service_proxy.cpp | 20 ++++++++++ .../backup_kit_inner/impl/b_session_backup.h | 7 ++++ .../backup_kit_inner/impl/b_session_restore.h | 7 ++++ .../impl/b_session_restore_async.h | 25 ++++-------- .../native/backup_kit_inner/impl/i_service.h | 1 + .../impl/i_service_ipc_interface_code.h | 1 + .../backup_kit_inner/impl/service_proxy.h | 1 + .../js/backup/session_backup_n_exporter.cpp | 38 ++++++++++++++++++- .../js/backup/session_backup_n_exporter.h | 1 + .../js/backup/session_restore_n_exporter.cpp | 34 +++++++++++++++++ .../js/backup/session_restore_n_exporter.h | 1 + .../backup_sa/include/module_ipc/service.h | 1 + .../include/module_ipc/service_stub.h | 1 + .../include/module_sched/sched_scheduler.h | 3 +- services/backup_sa/src/module_ipc/service.cpp | 16 ++++++-- .../backup_sa/src/module_ipc/service_stub.cpp | 11 ++++++ .../src/module_ipc/svc_session_manager.cpp | 15 +++++--- .../src/module_sched/sched_scheduler.cpp | 4 +- .../b_session_backup_mock.cpp | 5 +++ .../b_session_restore_async_mock.cpp | 5 +++ .../b_session_restore_mock.cpp | 5 +++ .../backup_kit_inner/service_proxy_mock.cpp | 5 +++ tests/mock/module_ipc/service_mock.cpp | 5 +++ tests/mock/module_ipc/service_stub_mock.cpp | 8 ++++ .../module_sched/sched_scheduler_mock.cpp | 2 +- .../backup_impl/include/i_service_mock.h | 5 +++ .../backup_impl/service_proxy_test.cpp | 24 ++++++++++++ .../module_ipc/service_stub_test.cpp | 31 +++++++++++++++ tools/backup_tool/src/tools_op_backup.cpp | 1 + tools/backup_tool/src/tools_op_restore.cpp | 3 +- 32 files changed, 273 insertions(+), 33 deletions(-) diff --git a/frameworks/native/backup_kit_inner/src/b_session_backup.cpp b/frameworks/native/backup_kit_inner/src/b_session_backup.cpp index e89285aba..d96ef7847 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_backup.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_backup.cpp @@ -114,4 +114,14 @@ ErrCode BSessionBackup::Finish() return proxy->Finish(); } + +ErrCode BSessionBackup::Release() +{ + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + + return proxy->Release(); +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/frameworks/native/backup_kit_inner/src/b_session_restore.cpp b/frameworks/native/backup_kit_inner/src/b_session_restore.cpp index 59a6d67e4..f41ea469b 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_restore.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_restore.cpp @@ -113,6 +113,16 @@ ErrCode BSessionRestore::Finish() return proxy->Finish(); } +ErrCode BSessionRestore::Release() +{ + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + + return proxy->Release(); +} + void BSessionRestore::RegisterBackupServiceDied(std::function functor) { auto proxy = ServiceProxy::GetInstance(); diff --git a/frameworks/native/backup_kit_inner/src/service_proxy.cpp b/frameworks/native/backup_kit_inner/src/service_proxy.cpp index 43e01a994..16c524014 100644 --- a/frameworks/native/backup_kit_inner/src/service_proxy.cpp +++ b/frameworks/native/backup_kit_inner/src/service_proxy.cpp @@ -310,6 +310,26 @@ ErrCode ServiceProxy::Finish() return reply.ReadInt32(); } +ErrCode ServiceProxy::Release() +{ + HILOGI("Begin"); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode(); + } + + MessageParcel reply; + MessageOption option; + int32_t ret = + Remote()->SendRequest(static_cast(IServiceInterfaceCode::SERVICE_CMD_RELSEASE_SESSION), data, reply, option); + if (ret != NO_ERROR) { + string str = "Failed to send out the request because of " + to_string(ret); + return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode(); + } + return reply.ReadInt32(); +} + sptr ServiceProxy::GetInstance() { unique_lock lock(proxyMutex_); diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_backup.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_backup.h index 059e3b798..bed22250c 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_backup.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_backup.h @@ -67,6 +67,13 @@ public: */ ErrCode Start(); + /** + * @brief 用于结束服务 + * + * @return ErrCode 规范错误码 + */ + ErrCode Release(); + /** * @brief 注册备份服务意外死亡时执行的回调函数 * diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore.h index 4654a7bc5..14e730a21 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore.h @@ -85,6 +85,13 @@ public: */ ErrCode Start(); + /** + * @brief 用于结束服务 + * + * @return ErrCode 规范错误码 + */ + ErrCode Release(); + /** * @brief 注册备份服务意外死亡时执行的回调函数 * diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore_async.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore_async.h index 0d547909e..2b6e0ddcc 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore_async.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore_async.h @@ -86,6 +86,13 @@ public: RestoreTypeEnum restoreType = RestoreTypeEnum::RESTORE_DATA_WAIT_SEND, int32_t userId = DEFAULT_INVAL_VALUE); + /** + * @brief 用于结束服务 + * + * @return ErrCode 规范错误码 + */ + ErrCode Release(); + public: explicit BSessionRestoreAsync(Callbacks callbacks) : callbacks_(callbacks) {}; ~BSessionRestoreAsync(); @@ -94,24 +101,6 @@ private: /** @brief 注册备份服务意外死亡时执行的回调函数 */ void OnBackupServiceDied(); - /** @brief 从暂存队列中取出一次待恢复应用请求 */ - void PopBundleInfo(); - - /** - * @brief 执行待恢复应用请求 - * - * @param info 待恢复应用请求信息 - */ - void AppendBundlesImpl(AppendBundleInfo info); - - /** - * @brief IPC请求异常时通知回调 - * - * @param errCode - * @param bundlesToRestore - */ - void OnBundleStarted(ErrCode errCode, const std::vector &bundlesToRestore); - /** * @brief 注册备份服务意外死亡时执行的回调函数 * diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/i_service.h b/interfaces/inner_api/native/backup_kit_inner/impl/i_service.h index e8bb2202a..43e873298 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/i_service.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/i_service.h @@ -50,6 +50,7 @@ public: int32_t userId = DEFAULT_INVAL_VALUE) = 0; virtual ErrCode AppendBundlesBackupSession(const std::vector &bundleNames) = 0; virtual ErrCode Finish() = 0; + virtual ErrCode Release() = 0; DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Filemanagement.Backup.IService") }; diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/i_service_ipc_interface_code.h b/interfaces/inner_api/native/backup_kit_inner/impl/i_service_ipc_interface_code.h index 797921043..67c279496 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/i_service_ipc_interface_code.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/i_service_ipc_interface_code.h @@ -30,6 +30,7 @@ enum class IServiceInterfaceCode { SERVICE_CMD_APPEND_BUNDLES_RESTORE_SESSION, SERVICE_CMD_APPEND_BUNDLES_BACKUP_SESSION, SERVICE_CMD_FINISH, + SERVICE_CMD_RELSEASE_SESSION, }; } // namespace OHOS::FileManagement::Backup diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/service_proxy.h b/interfaces/inner_api/native/backup_kit_inner/impl/service_proxy.h index 5273921a2..134250f1f 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/service_proxy.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/service_proxy.h @@ -42,6 +42,7 @@ public: int32_t userId = DEFAULT_INVAL_VALUE) override; ErrCode AppendBundlesBackupSession(const std::vector &bundleNames) override; ErrCode Finish() override; + ErrCode Release() override; public: explicit ServiceProxy(const sptr &impl) : IRemoteProxy(impl) {} diff --git a/interfaces/kits/js/backup/session_backup_n_exporter.cpp b/interfaces/kits/js/backup/session_backup_n_exporter.cpp index 713527c7f..bc624935c 100644 --- a/interfaces/kits/js/backup/session_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_backup_n_exporter.cpp @@ -289,10 +289,46 @@ napi_value SessionBackupNExporter::AppendBundles(napi_env env, napi_callback_inf } } +napi_value SessionBackupNExporter::Release(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("called SessionBackup::Release begin"); + NFuncArg funcArg(env, cbinfo); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto backupEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!(backupEntity && backupEntity->session)) { + HILOGE("Failed to get backupSession entity."); + NError(EPERM).ThrowErr(env); + return nullptr; + } + + auto cbExec = [session {backupEntity->session.get()}]() -> NError { + if (!session) { + return NError(BError(BError::Codes::SDK_INVAL_ARG, "backup session is nullptr").GetCode()); + } + return NError(session->Release()); + }; + auto cbCompl = [](napi_env env, NError err) -> NVal { + return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUndefined(env); + }; + + HILOGE("Called SessionBackup::Release end."); + + NVal thisVar(env, funcArg.GetThisVar()); + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; +} + bool SessionBackupNExporter::Export() { HILOGI("called SessionBackupNExporter::Export begin"); - vector props = {NVal::DeclareNapiFunction("appendBundles", AppendBundles)}; + vector props = { + NVal::DeclareNapiFunction("appendBundles", AppendBundles), + NVal::DeclareNapiFunction("release", Release), + }; auto [succ, classValue] = NClass::DefineClass(exports_.env_, className, Constructor, std::move(props)); if (!succ) { diff --git a/interfaces/kits/js/backup/session_backup_n_exporter.h b/interfaces/kits/js/backup/session_backup_n_exporter.h index afa3c96b2..75dd410b7 100644 --- a/interfaces/kits/js/backup/session_backup_n_exporter.h +++ b/interfaces/kits/js/backup/session_backup_n_exporter.h @@ -27,6 +27,7 @@ public: static napi_value Constructor(napi_env env, napi_callback_info cbinfo); static napi_value AppendBundles(napi_env env, napi_callback_info cbinfo); + static napi_value Release(napi_env env, napi_callback_info cbinfo); SessionBackupNExporter(napi_env env, napi_value exports); ~SessionBackupNExporter() override; diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.cpp b/interfaces/kits/js/backup/session_restore_n_exporter.cpp index 53d9c0df0..4c1033e96 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_restore_n_exporter.cpp @@ -427,6 +427,39 @@ napi_value SessionRestoreNExporter::GetFileHandle(napi_env env, napi_callback_in } } +napi_value SessionRestoreNExporter::Release(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("called SessionRestore::Release begin"); + NFuncArg funcArg(env, cbinfo); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto restoreEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!(restoreEntity && restoreEntity->session)) { + HILOGE("Failed to get RestoreSession entity."); + NError(EPERM).ThrowErr(env); + return nullptr; + } + + auto cbExec = [session {restoreEntity->session.get()}]() -> NError { + if (!session) { + return NError(BError(BError::Codes::SDK_INVAL_ARG, "restore session is nullptr").GetCode()); + } + return NError(session->Release()); + }; + auto cbCompl = [](napi_env env, NError err) -> NVal { + return err ? NVal {env, err.GetNapiErr(env)} : NVal::CreateUndefined(env); + }; + + HILOGE("Called SessionRestore::Release end."); + + NVal thisVar(env, funcArg.GetThisVar()); + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; +} + bool SessionRestoreNExporter::Export() { HILOGI("called SessionRestoreNExporter::Export begin"); @@ -434,6 +467,7 @@ bool SessionRestoreNExporter::Export() NVal::DeclareNapiFunction("appendBundles", AppendBundles), NVal::DeclareNapiFunction("publishFile", PublishFile), NVal::DeclareNapiFunction("getFileHandle", GetFileHandle), + NVal::DeclareNapiFunction("release", Release), }; auto [succ, classValue] = NClass::DefineClass(exports_.env_, className, Constructor, std::move(props)); diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.h b/interfaces/kits/js/backup/session_restore_n_exporter.h index b389b55f5..21fc812a2 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.h +++ b/interfaces/kits/js/backup/session_restore_n_exporter.h @@ -32,6 +32,7 @@ public: static napi_value AppendBundles(napi_env env, napi_callback_info cbinfo); static napi_value PublishFile(napi_env env, napi_callback_info cbinfo); static napi_value GetFileHandle(napi_env env, napi_callback_info cbinfo); + static napi_value Release(napi_env env, napi_callback_info cbinfo); SessionRestoreNExporter(napi_env env, napi_value exports); ~SessionRestoreNExporter() override; diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index 2a2e639bc..b90d6709b 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -46,6 +46,7 @@ public: int32_t userId = DEFAULT_INVAL_VALUE) override; ErrCode AppendBundlesBackupSession(const std::vector &bundleNames) override; ErrCode Finish() override; + ErrCode Release() override; // 以下都是非IPC接口 public: diff --git a/services/backup_sa/include/module_ipc/service_stub.h b/services/backup_sa/include/module_ipc/service_stub.h index 1049e1574..7c29e0a40 100644 --- a/services/backup_sa/include/module_ipc/service_stub.h +++ b/services/backup_sa/include/module_ipc/service_stub.h @@ -45,6 +45,7 @@ private: int32_t CmdAppendBundlesRestoreSession(MessageParcel &data, MessageParcel &reply); int32_t CmdAppendBundlesBackupSession(MessageParcel &data, MessageParcel &reply); int32_t CmdFinish(MessageParcel &data, MessageParcel &reply); + int32_t CmdRelease(MessageParcel &data, MessageParcel &reply); }; } // namespace OHOS::FileManagement::Backup diff --git a/services/backup_sa/include/module_sched/sched_scheduler.h b/services/backup_sa/include/module_sched/sched_scheduler.h index 419c87782..abe1ca2e9 100644 --- a/services/backup_sa/include/module_sched/sched_scheduler.h +++ b/services/backup_sa/include/module_sched/sched_scheduler.h @@ -70,12 +70,11 @@ public: * @brief unload service * */ - void TryUnloadService(); + void UnloadService(); void StartTimer() { extTime_.Setup(); - TryUnloadServiceTimer(); } public: diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index d882ef63f..7a31cb97c 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -788,7 +788,6 @@ void Service::OnAllBundlesFinished(ErrCode errCode) } else if (scenario == IServiceReverse::Scenario::RESTORE) { session_->GetServiceReverseProxy()->RestoreOnAllBundlesFinished(errCode); } - sched_->TryUnloadServiceTimer(true); } } @@ -814,7 +813,7 @@ void Service::SendAppGalleryNotify(const BundleName &bundleName) void Service::SessionDeactive() { try { - HILOGI("Begin"); + HILOGE("Begin"); // 结束定时器 sched_->ClearSchedulerData(); // 清除缓存数据 @@ -822,10 +821,19 @@ void Service::SessionDeactive() // 清除session session_->Deactive(nullptr, true); // 卸载服务 - sched_->TryUnloadService(); + sched_->UnloadService(); } catch (...) { - HILOGI("Unexpected exception"); + HILOGE("Unexpected exception"); + sched_->UnloadService(); return; } } + +ErrCode Service::Release() +{ + HILOGI("KILL"); + VerifyCaller(session_->GetScenario()); + SessionDeactive(); + return BError(BError::Codes::OK); +} } // namespace OHOS::FileManagement::Backup diff --git a/services/backup_sa/src/module_ipc/service_stub.cpp b/services/backup_sa/src/module_ipc/service_stub.cpp index eb0f9470d..39d82e307 100644 --- a/services/backup_sa/src/module_ipc/service_stub.cpp +++ b/services/backup_sa/src/module_ipc/service_stub.cpp @@ -52,6 +52,7 @@ ServiceStub::ServiceStub() opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_BACKUP_SESSION)] = &ServiceStub::CmdAppendBundlesBackupSession; opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_FINISH)] = &ServiceStub::CmdFinish; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_RELSEASE_SESSION)] = &ServiceStub::CmdRelease; } int32_t ServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) @@ -257,4 +258,14 @@ int32_t ServiceStub::CmdFinish(MessageParcel &data, MessageParcel &reply) } return BError(BError::Codes::OK); } + +int32_t ServiceStub::CmdRelease(MessageParcel &data, MessageParcel &reply) +{ + HILOGI("Begin"); + int res = Release(); + if (!reply.WriteInt32(res)) { + return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res)); + } + return BError(BError::Codes::OK); +} } // namespace OHOS::FileManagement::Backup diff --git a/services/backup_sa/src/module_ipc/svc_session_manager.cpp b/services/backup_sa/src/module_ipc/svc_session_manager.cpp index b82bd8c8c..55d276455 100644 --- a/services/backup_sa/src/module_ipc/svc_session_manager.cpp +++ b/services/backup_sa/src/module_ipc/svc_session_manager.cpp @@ -612,17 +612,22 @@ void SvcSessionManager::DecreaseSessionCnt() void SvcSessionManager::ClearSessionData() { + HILOGE("Begin"); unique_lock lock(lock_); - for (auto it = impl_.backupExtNameMap.begin(); it != impl_.backupExtNameMap.end();) { + for (auto &&it : impl_.backupExtNameMap) { // clear timer - extBundleTimer.Unregister(it->second.extTimerId); + if (it.second.timerStatus == true) { + it.second.timerStatus = false; + extBundleTimer.Unregister(it.second.extTimerId); + } // disconnect extension - if (it->second.schedAction == BConstants::ServiceSchedAction::RUNNING) { - it->second.backUpConnection->DisconnectBackupExtAbility(); + if (it.second.schedAction == BConstants::ServiceSchedAction::RUNNING) { + it.second.backUpConnection->DisconnectBackupExtAbility(); } // clear data - it->second.schedAction = BConstants::ServiceSchedAction::FINISH; + it.second.schedAction = BConstants::ServiceSchedAction::FINISH; } impl_.backupExtNameMap.clear(); + HILOGE("End"); } } // namespace OHOS::FileManagement::Backup diff --git a/services/backup_sa/src/module_sched/sched_scheduler.cpp b/services/backup_sa/src/module_sched/sched_scheduler.cpp index 04e5db2bc..fbe93d92c 100644 --- a/services/backup_sa/src/module_sched/sched_scheduler.cpp +++ b/services/backup_sa/src/module_sched/sched_scheduler.cpp @@ -160,7 +160,7 @@ void SchedScheduler::TryUnloadServiceTimer(bool force) tryUnload(); } -void SchedScheduler::TryUnloadService() +void SchedScheduler::UnloadService() { HILOGI("Unload system ability"); sptr saManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); @@ -177,6 +177,7 @@ void SchedScheduler::TryUnloadService() void SchedScheduler::ClearSchedulerData() { + HILOGE("Begin"); unique_lock lock(lock_); for (auto &bundleTime : bundleTimeVec_) { auto &[bName, iTime] = bundleTime; @@ -184,5 +185,6 @@ void SchedScheduler::ClearSchedulerData() } bundleTimeVec_.clear(); threadPool_.Stop(); + HILOGE("End"); } }; // namespace OHOS::FileManagement::Backup diff --git a/tests/mock/backup_kit_inner/b_session_backup_mock.cpp b/tests/mock/backup_kit_inner/b_session_backup_mock.cpp index f3a8f5193..4f34a37d7 100644 --- a/tests/mock/backup_kit_inner/b_session_backup_mock.cpp +++ b/tests/mock/backup_kit_inner/b_session_backup_mock.cpp @@ -92,4 +92,9 @@ ErrCode BSessionBackup::Finish() { return BError(BError::Codes::OK); } + +ErrCode BSessionBackup::Release() +{ + return BError(BError::Codes::OK); +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/mock/backup_kit_inner/b_session_restore_async_mock.cpp b/tests/mock/backup_kit_inner/b_session_restore_async_mock.cpp index 4068bd936..739127482 100644 --- a/tests/mock/backup_kit_inner/b_session_restore_async_mock.cpp +++ b/tests/mock/backup_kit_inner/b_session_restore_async_mock.cpp @@ -96,5 +96,10 @@ ErrCode BSessionRestoreAsync::AppendBundles(UniqueFd remoteCap, void BSessionRestoreAsync::OnBackupServiceDied() {} +ErrCode BSessionRestoreAsync::Release() +{ + return BError(BError::Codes::OK); +} + void BSessionRestoreAsync::RegisterBackupServiceDied(function functor) {} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/mock/backup_kit_inner/b_session_restore_mock.cpp b/tests/mock/backup_kit_inner/b_session_restore_mock.cpp index 49d4126bc..3af358b95 100644 --- a/tests/mock/backup_kit_inner/b_session_restore_mock.cpp +++ b/tests/mock/backup_kit_inner/b_session_restore_mock.cpp @@ -96,5 +96,10 @@ ErrCode BSessionRestore::Finish() return BError(BError::Codes::OK); } +ErrCode BSessionRestore::Release() +{ + return BError(BError::Codes::OK); +} + void BSessionRestore::RegisterBackupServiceDied(function functor) {} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/mock/backup_kit_inner/service_proxy_mock.cpp b/tests/mock/backup_kit_inner/service_proxy_mock.cpp index cd0808e1a..8b01275ab 100644 --- a/tests/mock/backup_kit_inner/service_proxy_mock.cpp +++ b/tests/mock/backup_kit_inner/service_proxy_mock.cpp @@ -98,6 +98,11 @@ ErrCode ServiceProxy::Finish() return BError(BError::Codes::OK); } +ErrCode ServiceProxy::Release() +{ + return BError(BError::Codes::OK); +} + sptr ServiceProxy::GetInstance() { if (!GetMockGetInstance()) { diff --git a/tests/mock/module_ipc/service_mock.cpp b/tests/mock/module_ipc/service_mock.cpp index 898c0f835..afa22a435 100644 --- a/tests/mock/module_ipc/service_mock.cpp +++ b/tests/mock/module_ipc/service_mock.cpp @@ -129,4 +129,9 @@ void Service::OnStartSched() {} void Service::SendAppGalleryNotify(const BundleName &bundleName) {} void Service::SessionDeactive() {} + +ErrCode Service::Release() +{ + return BError(BError::Codes::OK); +} } // namespace OHOS::FileManagement::Backup diff --git a/tests/mock/module_ipc/service_stub_mock.cpp b/tests/mock/module_ipc/service_stub_mock.cpp index 07e131cac..c650f841e 100644 --- a/tests/mock/module_ipc/service_stub_mock.cpp +++ b/tests/mock/module_ipc/service_stub_mock.cpp @@ -44,6 +44,7 @@ ServiceStub::ServiceStub() opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_BACKUP_SESSION)] = &ServiceStub::CmdAppendBundlesBackupSession; opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_FINISH)] = &ServiceStub::CmdFinish; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_RELSEASE_SESSION)] = &ServiceStub::CmdRelease; } int32_t ServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) @@ -159,4 +160,11 @@ int32_t ServiceStub::CmdFinish(MessageParcel &data, MessageParcel &reply) reply.WriteInt32(res); return BError(BError::Codes::OK); } + +int32_t ServiceStub::CmdRelease(MessageParcel &data, MessageParcel &reply) +{ + int res = Release(); + reply.WriteInt32(res); + return BError(BError::Codes::OK); +} } // namespace OHOS::FileManagement::Backup diff --git a/tests/mock/module_sched/sched_scheduler_mock.cpp b/tests/mock/module_sched/sched_scheduler_mock.cpp index 82fdc4485..6ed858970 100644 --- a/tests/mock/module_sched/sched_scheduler_mock.cpp +++ b/tests/mock/module_sched/sched_scheduler_mock.cpp @@ -32,7 +32,7 @@ void SchedScheduler::RemoveExtConn(const string &bundleName) {} void SchedScheduler::TryUnloadServiceTimer(bool force) {} -void SchedScheduler::TryUnloadService() {} +void SchedScheduler::UnloadService() {} void SchedScheduler::ClearSchedulerData() {} }; // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/unittests/backup_api/backup_impl/include/i_service_mock.h b/tests/unittests/backup_api/backup_impl/include/i_service_mock.h index 2af0cb1e1..0f570e81e 100644 --- a/tests/unittests/backup_api/backup_impl/include/i_service_mock.h +++ b/tests/unittests/backup_api/backup_impl/include/i_service_mock.h @@ -110,6 +110,11 @@ public: { return BError(BError::Codes::OK); } + + ErrCode Release() override + { + return BError(BError::Codes::OK); + } }; } // namespace OHOS::FileManagement::Backup #endif // MOCK_I_SERVICE_MOCK_H \ No newline at end of file diff --git a/tests/unittests/backup_api/backup_impl/service_proxy_test.cpp b/tests/unittests/backup_api/backup_impl/service_proxy_test.cpp index ecc24d04a..6029a511f 100644 --- a/tests/unittests/backup_api/backup_impl/service_proxy_test.cpp +++ b/tests/unittests/backup_api/backup_impl/service_proxy_test.cpp @@ -346,6 +346,30 @@ HWTEST_F(ServiceProxyTest, SUB_Service_proxy_Finish_0100, testing::ext::TestSize GTEST_LOG_(INFO) << "ServiceProxyTest-end SUB_Service_proxy_Finish_0100"; } +/** + * @tc.number: SUB_Service_proxy_Release_0100 + * @tc.name: SUB_Service_proxy_Release_0100 + * @tc.desc: 测试 Finish 获取真实文件调用成功和失败 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6URNZ + */ +HWTEST_F(ServiceProxyTest, SUB_Service_proxy_Release_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceProxyTest-begin SUB_Service_proxy_Release_0100"; + EXPECT_CALL(*mock_, SendRequest(_, _, _, _)) + .Times(2) + .WillOnce(Invoke(mock_.GetRefPtr(), &IServiceMock::InvokeSendRequest)) + .WillOnce(Return(EPERM)); + + int32_t result = proxy_->Release(); + EXPECT_EQ(result, BError(BError::Codes::OK)); + result = proxy_->Release(); + EXPECT_NE(result, BError(BError::Codes::OK)); + GTEST_LOG_(INFO) << "ServiceProxyTest-end SUB_Service_proxy_Release_0100"; +} + /** * @tc.number: SUB_Service_proxy_OnLoadSystemAbilitySuccess_0100 * @tc.name: SUB_Service_proxy_OnLoadSystemAbilitySuccess_0100 diff --git a/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp b/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp index 8b06ed40c..c9d8b1c55 100644 --- a/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp @@ -53,6 +53,7 @@ public: 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()); UniqueFd InvokeGetLocalCapabilities() { if (bCapabilities_) { @@ -466,4 +467,34 @@ HWTEST_F(ServiceStubTest, SUB_backup_sa_ServiceStub_Finish_0100, testing::ext::T } GTEST_LOG_(INFO) << "ServiceStubTest-end SUB_backup_sa_ServiceStub_Finish_0100"; } + +/** + * @tc.number: SUB_backup_sa_ServiceStub_Release_0100 + * @tc.name: SUB_backup_sa_ServiceStub_Release_0100 + * @tc.desc: Test function of Release interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6URNZ + */ +HWTEST_F(ServiceStubTest, SUB_backup_sa_ServiceStub_Release_0100, testing::ext::TestSize.Level0) +{ + GTEST_LOG_(INFO) << "ServiceStubTest-begin SUB_backup_sa_ServiceStub_Release_0100"; + try { + MockService service; + EXPECT_CALL(service, Release()).WillOnce(Return(BError(BError::Codes::OK))); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + EXPECT_TRUE(data.WriteInterfaceToken(IService::GetDescriptor())); + EXPECT_EQ(BError(BError::Codes::OK), + service.OnRemoteRequest(static_cast(IServiceInterfaceCode::SERVICE_CMD_RELSEASE_SESSION), data, reply, + option)); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceStubTest-an exception occurred by Release."; + } + GTEST_LOG_(INFO) << "ServiceStubTest-end SUB_backup_sa_ServiceStub_Release_0100"; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tools/backup_tool/src/tools_op_backup.cpp b/tools/backup_tool/src/tools_op_backup.cpp index d79b1ba62..e9c1e97e6 100644 --- a/tools/backup_tool/src/tools_op_backup.cpp +++ b/tools/backup_tool/src/tools_op_backup.cpp @@ -258,6 +258,7 @@ static int32_t InitPathCapFile(const string &pathCapFile, vector bundleN ctx->SetBundleFinishedCount(bundleNames.size()); ctx->Wait(); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + ctx->session_->Release(); return 0; } diff --git a/tools/backup_tool/src/tools_op_restore.cpp b/tools/backup_tool/src/tools_op_restore.cpp index 333099dbd..9f21842b5 100644 --- a/tools/backup_tool/src/tools_op_restore.cpp +++ b/tools/backup_tool/src/tools_op_restore.cpp @@ -160,8 +160,8 @@ static void OnBundleFinished(shared_ptr ctx, ErrCode err, const BundleN ctx->UpdateBundleFinishedCount(); if (err != 0) { ctx->isAllBundelsFinished.store(true); - ctx->ClearBundleOfMap(name); } + ctx->ClearBundleOfMap(name); ctx->TryNotify(); } @@ -286,6 +286,7 @@ static int32_t InitPathCapFile(const string &pathCapFile, vector bundleN ctx->SetBundleFinishedCount(bundleNames.size()); RestoreApp(ctx, bundleNames); ctx->Wait(); + ctx->session_->Release(); return 0; } -- Gitee