From c87d469099581c396e994a853b18363c8d74fd39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=99=9F=E9=92=B0?= Date: Wed, 25 Dec 2024 09:43:43 +0800 Subject: [PATCH] =?UTF-8?q?getlocal=E6=8E=A5=E5=8F=A3=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张晟钰 --- .../src/b_incremental_backup_session.cpp | 16 ++++ .../src/b_incremental_restore_session.cpp | 16 ++++ .../backup_kit_inner/src/b_session_backup.cpp | 16 ++++ .../src/b_session_restore.cpp | 16 ++++ .../backup_kit_inner/src/service_proxy.cpp | 32 ++++++++ .../impl/b_incremental_backup_session.h | 7 ++ .../impl/b_incremental_restore_session.h | 7 ++ .../backup_kit_inner/impl/b_session_backup.h | 7 ++ .../backup_kit_inner/impl/b_session_restore.h | 7 ++ .../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 | 43 +++++++++++ .../js/backup/session_backup_n_exporter.h | 1 + .../session_incremental_backup_n_exporter.cpp | 43 +++++++++++ .../session_incremental_backup_n_exporter.h | 1 + .../js/backup/session_restore_n_exporter.cpp | 47 +++++++++++ .../js/backup/session_restore_n_exporter.h | 1 + .../include/module_external/bms_adapter.h | 5 ++ .../backup_sa/include/module_ipc/service.h | 1 + .../include/module_ipc/service_stub.h | 1 + .../src/module_external/bms_adapter.cpp | 77 +++++++++++++++++++ .../backup_sa/src/module_ipc/service_stub.cpp | 11 +++ .../backup_sa/src/module_ipc/sub_service.cpp | 63 +++++++++++++++ .../backup_kit_inner/service_proxy_mock.cpp | 5 ++ .../mock/module_external/bms_adapter_mock.cpp | 19 +++++ .../include/bms_adapter_mock.h | 4 + .../module_external/src/bms_adapter_mock.cpp | 11 +++ .../module_ipc/include/service_stub_mock.h | 2 + tests/mock/module_ipc/service_mock.cpp | 6 ++ tests/mock/module_ipc/service_stub_mock.cpp | 7 ++ .../mock/module_ipc/src/service_stub_mock.cpp | 5 ++ .../backup_impl/include/i_service_mock.h | 5 ++ .../module_ipc/service_incremental_test.cpp | 4 + .../module_ipc/service_other_test.cpp | 5 ++ .../module_ipc/service_stub_test.cpp | 1 + .../backup_sa/module_ipc/service_test.cpp | 10 ++- .../backup_sa/module_ipc/sub_service_test.cpp | 32 ++++++++ .../backup_sa/session/service_proxy_mock.cpp | 5 ++ .../backup_sa/session/service_proxy_mock.h | 1 + 40 files changed, 541 insertions(+), 2 deletions(-) diff --git a/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp b/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp index 0d69e9ba8..cf06c78bb 100644 --- a/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp +++ b/frameworks/native/backup_kit_inner/src/b_incremental_backup_session.cpp @@ -90,6 +90,22 @@ void BIncrementalBackupSession::RegisterBackupServiceDied(function funct remoteObj->AddDeathRecipient(deathRecipient_); } +UniqueFd BIncrementalBackupSession::GetLocalCapabilities() +{ + HILOGI(GetLocalCapabilities begin); + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + HILOGE("Failed to get backup service"); + return UniqueFd(-EPERM); + } + UniqueFd fd = proxy->GetLocalCapabilitiesForBdInfos(); + if (fd < 0) { + HILOGE("Failed to get local capabilities for bundleinfos"); + return UniqueFd(-EPERM); + } + return fd; +} + ErrCode BIncrementalBackupSession::AppendBundles(vector bundlesToBackup) { auto proxy = ServiceProxy::GetInstance(); diff --git a/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp b/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp index 0999dac1d..81c4cc51c 100644 --- a/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp +++ b/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp @@ -69,6 +69,22 @@ unique_ptr BIncrementalRestoreSession::Init(Callback return nullptr; } +UniqueFd BIncrementalRestoreSession::GetLocalCapabilities() +{ + HILOGI(GetLocalCapabilities begin); + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + HILOGE("Failed to get backup service"); + return UniqueFd(-EPERM); + } + UniqueFd fd = proxy->GetLocalCapabilitiesForBdInfos(); + if (fd < 0) { + HILOGE("Failed to get local capabilities for bundleinfos"); + return UniqueFd(-EPERM); + } + return fd; +} + ErrCode BIncrementalRestoreSession::PublishFile(BFileInfo fileInfo) { auto proxy = ServiceProxy::GetInstance(); 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 508626269..d62376003 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_backup.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_backup.cpp @@ -100,6 +100,22 @@ ErrCode BSessionBackup::Start() return proxy->Start(); } +UniqueFd BSessionBackup::GetLocalCapabilities() +{ + HILOGI(GetLocalCapabilities begin); + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + HILOGE("Failed to get backup service"); + return UniqueFd(-EPERM); + } + UniqueFd fd = proxy->GetLocalCapabilitiesForBdInfos(); + if (fd < 0) { + HILOGE("Failed to get local capabilities for bundleinfos"); + return UniqueFd(-EPERM); + } + return fd; +} + ErrCode BSessionBackup::AppendBundles(vector bundlesToBackup) { auto proxy = ServiceProxy::GetInstance(); 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 9247c39ff..d5b71bd7a 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_restore.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_restore.cpp @@ -69,6 +69,22 @@ unique_ptr BSessionRestore::Init(Callbacks callbacks) return nullptr; } +UniqueFd BSessionRestore::GetLocalCapabilities() +{ + HILOGI(GetLocalCapabilities begin); + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + HILOGE("Failed to get backup service"); + return UniqueFd(-EPERM); + } + UniqueFd fd = proxy->GetLocalCapabilitiesForBdInfos(); + if (fd < 0) { + HILOGE("Failed to get local capabilities for bundleinfos"); + return UniqueFd(-EPERM); + } + return fd; +} + ErrCode BSessionRestore::PublishFile(BFileInfo fileInfo) { 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 784555688..ef92a1c49 100644 --- a/frameworks/native/backup_kit_inner/src/service_proxy.cpp +++ b/frameworks/native/backup_kit_inner/src/service_proxy.cpp @@ -725,4 +725,36 @@ ErrCode ServiceProxy::RefreshDataSize(int64_t totalSize) return BError(BError::Codes::OK, "success"); } +UniqueFd ServiceProxy::GetLocalCapabilitiesForBdInfos() +{ + HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + HILOGI("ServiceProxy, start GetLocalCapabilitiesForBdInfos"); + if (Remote() == nullptr) { + HILOGE("Remote is nullptr"); + return UniqueFd(-EPERM); + } + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + HILOGE("Failed to write descriptor"); + return UniqueFd(-EPERM); + } + + MessageParcel reply; + MessageOption option; + option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME); + int32_t ret = Remote()->SendRequest( + static_cast(IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES_FOR_BDINFOS), data, reply, option); + if (ret != NO_ERROR) { + HILOGE("Received error %{public}d when doing IPC", ret); + AppRadar::Info resInfo("", "", ""); + AppRadar::GetInstance().RecordDefaultFuncRes(resInfo, "ServiceProxy::GetLocalCapabilities", + AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_GET_LOCAL_CAPABILITIES_FAIL, ret); + return UniqueFd(-ret); + } + UniqueFd fd(reply.ReadFileDescriptor()); + HILOGI("ServiceProxy, end GetLocalCapabilitiesForBdInfos"); + //return UniqueFd(fd.Release()); + return fd; +} + } // namespace OHOS::FileManagement::Backup diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_backup_session.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_backup_session.h index c7c40df0a..db31ed69a 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_backup_session.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_backup_session.h @@ -49,6 +49,13 @@ public: */ static std::unique_ptr Init(Callbacks callbacks); + /** + * @brief 获取支持备份的应用信息 + * + * @return ErrCode 规范错误码 + */ + UniqueFd GetLocalCapabilities(); + /** * @brief 用于追加应用,现阶段仅支持在Start之前调用 * diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_restore_session.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_restore_session.h index 3a2e2ec5f..b1c35fec1 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_restore_session.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_restore_session.h @@ -49,6 +49,13 @@ public: */ static std::unique_ptr Init(Callbacks callbacks); + /** + * @brief 获取支持备份的应用信息(不包含数据量大小) + * + * @return UniqueFd 包含应用信息的文件描述符 + */ + UniqueFd GetLocalCapabilities(); + /** * @brief 通知备份服务文件内容已就绪 * 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 f08c340be..171a67ae8 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 @@ -47,6 +47,13 @@ public: */ static std::unique_ptr Init(Callbacks callbacks); + /** + * @brief 获取支持备份的应用信息 + * + * @return ErrCode 规范错误码 + */ + UniqueFd GetLocalCapabilities(); + /** * @brief 用于追加应用,现阶段仅支持在Start之前调用 * 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 cdb72d326..373a648cb 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 @@ -47,6 +47,13 @@ public: */ static std::unique_ptr Init(Callbacks callbacks); + /** + * @brief 获取支持备份的应用信息 + * + * @return ErrCode 规范错误码 + */ + UniqueFd GetLocalCapabilities(); + /** * @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 16ddf4c62..23f943af8 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 @@ -49,6 +49,7 @@ public: virtual ErrCode InitBackupSession(sptr remote) = 0; virtual ErrCode Start() = 0; virtual UniqueFd GetLocalCapabilities() = 0; + virtual UniqueFd GetLocalCapabilitiesForBdInfos() = 0; virtual ErrCode PublishFile(const BFileInfo &fileInfo) = 0; virtual ErrCode AppFileReady(const std::string &fileName, UniqueFd fd, int32_t errCode) = 0; virtual ErrCode AppDone(ErrCode errCode) = 0; 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 c4ab2d359..403c2f12b 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 @@ -22,6 +22,7 @@ enum class IServiceInterfaceCode { SERVICE_CMD_INIT_RESTORE_SESSION, SERVICE_CMD_INIT_BACKUP_SESSION, SERVICE_CMD_GET_LOCAL_CAPABILITIES, + SERVICE_CMD_GET_LOCAL_CAPABILITIES_FOR_BDINFOS, SERVICE_CMD_PUBLISH_FILE, SERVICE_CMD_APP_FILE_READY, SERVICE_CMD_APP_DONE, 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 94271d4bc..5032ee1bb 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 @@ -32,6 +32,7 @@ public: ErrCode InitBackupSession(sptr remote) override; ErrCode Start() override; UniqueFd GetLocalCapabilities() override; + UniqueFd GetLocalCapabilitiesForBdInfos() override; ErrCode PublishFile(const BFileInfo &fileInfo) override; ErrCode AppFileReady(const std::string &fileName, UniqueFd fd, int32_t errCode) override; ErrCode AppDone(ErrCode errCode) override; diff --git a/interfaces/kits/js/backup/session_backup_n_exporter.cpp b/interfaces/kits/js/backup/session_backup_n_exporter.cpp index bfbb9c34a..33900165c 100644 --- a/interfaces/kits/js/backup/session_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_backup_n_exporter.cpp @@ -363,6 +363,49 @@ napi_value SessionBackupNExporter::Constructor(napi_env env, napi_callback_info return funcArg.GetThisVar(); } +napi_value SessionBackupNExporter::GetLocalCapabilities(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("called SessionBackup, GetLocalCapabilities Begin"); + if (!SAUtils::CheckBackupPermission()) { + HILOGE("Has not permission!"); + NError(E_PERMISSION).ThrowErr(env); + return nullptr; + } + if (!SAUtils::IsSystemApp()) { + HILOGE("System App check fail!"); + NError(E_PERMISSION_SYS).ThrowErr(env); + return nullptr; + } + NFuncArg funcArg(env, cbinfo); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched."); + NError(BError(BError::Codes::SDK_INVAL_ARG, "Number of arguments unmatched.").GetCode()).ThrowErr(env); + return false; + } + auto backupEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!(backupEntity && backupEntity->session)) { + HILOGE("Failed to get backupSession entity."); + NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to get backupSession entity.").GetCode()).ThrowErr(env); + return nullptr; + } + UniqueFd fd; + auto cbExec = [session {backupEntity->session.get()}, &fd]() -> NError { + if (!session) { + return NError(BError(BError::Codes::SDK_INVAL_ARG, "backup session is nullptr").GetCode()); + } + fd = session->GetLocalCapabilities(); + return fd < 0 ? NError(BError(BError::Codes::SA_INVAL_ARG, "Failed to get local capabilities.").GetCode()) : + NError(BError(BError::Codes::OK, "Success to get local capabilities.").GetCode()) + }; + auto cbCompl = [&fd](napi_env env, NError err) -> NVal { + NVal obj = NVal::CreateObject(env); + obj.AddProp({NVal::DeclareNapiProperty(BConstants::FD.c_str(), NVal::CreateInt32(env, fd.Release()).val_)}); + return err ? NVal {env, err.GetNapiErr(env)} : obj; + }; + NVal thisVar(env, funcArg.GetThisVar()); + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; +} + static bool VerifyParamSuccess(NFuncArg &funcArg, std::vector &bundleNames, std::vector &bundleInfos, napi_env env) { diff --git a/interfaces/kits/js/backup/session_backup_n_exporter.h b/interfaces/kits/js/backup/session_backup_n_exporter.h index 1c95b7df6..3892a76ba 100644 --- a/interfaces/kits/js/backup/session_backup_n_exporter.h +++ b/interfaces/kits/js/backup/session_backup_n_exporter.h @@ -26,6 +26,7 @@ public: std::string GetClassName() override; static napi_value Constructor(napi_env env, napi_callback_info cbinfo); + static napi_value GetLocalCapabilities(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); diff --git a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp index 7afafda7a..c5b360917 100644 --- a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp @@ -368,6 +368,49 @@ napi_value SessionIncrementalBackupNExporter::Constructor(napi_env env, napi_cal return funcArg.GetThisVar(); } +napi_value SessionIncrementalBackupNExporter::GetLocalCapabilities(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("called SessionBackup, GetLocalCapabilities Begin"); + if (!SAUtils::CheckBackupPermission()) { + HILOGE("Has not permission!"); + NError(E_PERMISSION).ThrowErr(env); + return nullptr; + } + if (!SAUtils::IsSystemApp()) { + HILOGE("System App check fail!"); + NError(E_PERMISSION_SYS).ThrowErr(env); + return nullptr; + } + NFuncArg funcArg(env, cbinfo); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched."); + NError(BError(BError::Codes::SDK_INVAL_ARG, "Number of arguments unmatched.").GetCode()).ThrowErr(env); + return false; + } + auto backupEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!(backupEntity && backupEntity->session)) { + HILOGE("Failed to get backupSession entity."); + NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to get backupSession entity.").GetCode()).ThrowErr(env); + return nullptr; + } + UniqueFd fd; + auto cbExec = [session {backupEntity->session.get()}, &fd]() -> NError { + if (!session) { + return NError(BError(BError::Codes::SDK_INVAL_ARG, "backup session is nullptr").GetCode()); + } + fd = session->GetLocalCapabilities(); + return fd < 0 ? NError(BError(BError::Codes::SA_INVAL_ARG, "Failed to get local capabilities.").GetCode()) : + NError(BError(BError::Codes::OK, "Success to get local capabilities.").GetCode()) + }; + auto cbCompl = [&fd](napi_env env, NError err) -> NVal { + NVal obj = NVal::CreateObject(env); + obj.AddProp({NVal::DeclareNapiProperty(BConstants::FD.c_str(), NVal::CreateInt32(env, fd.Release()).val_)}); + return err ? NVal {env, err.GetNapiErr(env)} : obj; + }; + NVal thisVar(env, funcArg.GetThisVar()); + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; +} + static bool CheckDataList(const LibN::NVal &data) { LibN::NVal name = data.GetProp(BConstants::BUNDLE_NAME); diff --git a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.h b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.h index 9508630b4..d36b10cb2 100644 --- a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.h +++ b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.h @@ -26,6 +26,7 @@ public: std::string GetClassName() override; static napi_value Constructor(napi_env env, napi_callback_info cbinfo); + static napi_value GetLocalCapabilities(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); diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.cpp b/interfaces/kits/js/backup/session_restore_n_exporter.cpp index cb4fb6de3..f4ec839a8 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_restore_n_exporter.cpp @@ -475,6 +475,53 @@ napi_value SessionRestoreNExporter::Constructor(napi_env env, napi_callback_info return funcArg.GetThisVar(); } +napi_value SessionRestoreNExporter::GetLocalCapabilities(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("called SessionBackup, GetLocalCapabilities Begin"); + if (!SAUtils::CheckBackupPermission()) { + HILOGE("Has not permission!"); + NError(E_PERMISSION).ThrowErr(env); + return nullptr; + } + if (!SAUtils::IsSystemApp()) { + HILOGE("System App check fail!"); + NError(E_PERMISSION_SYS).ThrowErr(env); + return nullptr; + } + NFuncArg funcArg(env, cbinfo); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + HILOGE("Number of arguments unmatched."); + NError(BError(BError::Codes::SDK_INVAL_ARG, "Number of arguments unmatched.").GetCode()).ThrowErr(env); + return false; + } + auto restoreEntity = NClass::GetEntityOf(env, funcArg.GetThisVar()); + if (!(restoreEntity && (restoreEntity->sessionWhole || restoreEntity->sessionSheet))) { + HILOGE("Failed to get restoreSession entity."); + NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to get restoreSession entity.").GetCode()).ThrowErr(env); + return nullptr; + } + UniqueFd fd; + auto cbExec = [session {restoreEntity}, &fd]() -> NError { + if (!session && (session->sessionWhole || session->sessionSheet)) { + return NError(BError(BError::Codes::SDK_INVAL_ARG, "backup session is nullptr").GetCode()); + } + if (session->sessionWhole) { + fd = session->sessionWhole->GetLocalCapabilities(); + } else if (session->sessionSheet) { + fd = session->sessionSheet->GetLocalCapabilities(); + } + return fd < 0 ? NError(BError(BError::Codes::SA_INVAL_ARG, "Failed to get local capabilities.").GetCode()) : + NError(BError(BError::Codes::OK, "Success to get local capabilities.").GetCode()) + }; + auto cbCompl = [&fd](napi_env env, NError err) -> NVal { + NVal obj = NVal::CreateObject(env); + obj.AddProp({NVal::DeclareNapiProperty(BConstants::FD.c_str(), NVal::CreateInt32(env, fd.Release()).val_)}); + return err ? NVal {env, err.GetNapiErr(env)} : obj; + }; + NVal thisVar(env, funcArg.GetThisVar()); + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; +} + static NContextCBExec GetAppendBundlesCBExec(napi_env env, NFuncArg &funcArg, const int32_t fdRestore, const std::vector &bundleNames, const std::vector &bundleInfos) { diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.h b/interfaces/kits/js/backup/session_restore_n_exporter.h index 49e1f6388..cfe3a4fe0 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.h +++ b/interfaces/kits/js/backup/session_restore_n_exporter.h @@ -29,6 +29,7 @@ public: std::string GetClassName() override; static napi_value Constructor(napi_env env, napi_callback_info cbinfo); + static napi_value GetLocalCapabilities(napi_env env, napi_callback_info cbinfo); 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); diff --git a/services/backup_sa/include/module_external/bms_adapter.h b/services/backup_sa/include/module_external/bms_adapter.h index 5f2dc4497..651b6279a 100644 --- a/services/backup_sa/include/module_external/bms_adapter.h +++ b/services/backup_sa/include/module_external/bms_adapter.h @@ -70,6 +70,11 @@ public: static std::vector GetBundleInfosForAppend( const std::vector &incrementalDataList, int32_t userId); + + static std::vector GetBundleInfosForLocalCapabilities(int32_t userId); + + static std::vector GetBundleInfosForIndex( + const vector &bundleNames, int32_t userId); private: static bool GetCurBundleExtenionInfo(AppExecFwk::BundleInfo &installedBundle, const std::string &bundleName, std::vector &extensionInfos, sptr bms, diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index 5319faf48..5e7567d73 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -52,6 +52,7 @@ public: ErrCode InitBackupSession(sptr remote) override; ErrCode Start() override; UniqueFd GetLocalCapabilities() override; + UniqueFd GetLocalCapabilitiesForBdInfos() override; ErrCode PublishFile(const BFileInfo &fileInfo) override; ErrCode AppFileReady(const std::string &fileName, UniqueFd fd, int32_t errCode) override; ErrCode AppDone(ErrCode errCode) override; diff --git a/services/backup_sa/include/module_ipc/service_stub.h b/services/backup_sa/include/module_ipc/service_stub.h index 09dc21490..4faa710b0 100644 --- a/services/backup_sa/include/module_ipc/service_stub.h +++ b/services/backup_sa/include/module_ipc/service_stub.h @@ -38,6 +38,7 @@ private: int32_t CmdInitBackupSession(MessageParcel &data, MessageParcel &reply); int32_t CmdStart(MessageParcel &data, MessageParcel &reply); int32_t CmdGetLocalCapabilities(MessageParcel &data, MessageParcel &reply); + int32_t CmdGetLocalCapabilitiesForBdInfos(MessageParcel &data, MessageParcel &reply); int32_t CmdPublishFile(MessageParcel &data, MessageParcel &reply); int32_t CmdAppFileReady(MessageParcel &data, MessageParcel &reply); int32_t CmdAppDone(MessageParcel &data, MessageParcel &reply); diff --git a/services/backup_sa/src/module_external/bms_adapter.cpp b/services/backup_sa/src/module_external/bms_adapter.cpp index e8322327c..1a2450553 100644 --- a/services/backup_sa/src/module_external/bms_adapter.cpp +++ b/services/backup_sa/src/module_external/bms_adapter.cpp @@ -561,4 +561,81 @@ vector BundleMgrAdapter::GetBundleInfosForAppend(co } return bundleInfos; } + +std::vector BundleMgrAdapter::GetBundleInfosForLocalCapabilities(int32_t userId) +{ + HILOGI("start GetFullBundleInfosForLocalCapabilities"); + vector installedBundles; + auto bms = GetBundleManager(); + if (!bms->GetBundleInfos(AppExecFwk::GET_BUNDLE_WITH_EXTENSION_INFO, installedBundles, userId)) { + HILOGI("Failed to get bundle infos"); + return {}; + } + vector bundleNames; + vector bundleInfos; + HILOGI("End get installedBundles count is:%{public}zu", installedBundles.size()); + for (auto const &installedBundle : installedBundles) { + if (installedBundle.applicationInfo.codePath == HMOS_HAP_CODE_PATH || + installedBundle.applicationInfo.codePath == LINUX_HAP_CODE_PATH) { + HILOGI("Unsupported applications, name : %{public}s", installedBundle.name.data()); + continue; + } + if (installedBundle.appIndex > 0) { + HILOGI("Current bundle %{public}s is a twin application, index = %{public}d", + installedBundle.name.c_str(), installedBundle.appIndex); + std::string bundleNameIndexInfo = BJsonUtil::BuildBundleNameIndexInfo(installedBundle.name, + installedBundle.appIndex); + bundleNames.emplace_back(bundleNameIndexInfo); + continue; + } + auto [allToBackup, fullBackupOnly, extName, restoreDeps, supportScene, extraInfo] = + GetAllowAndExtName(installedBundle.extensionInfos); + if (!allToBackup) { + HILOGI("Not allToBackup, bundleName = %{public}s, appIndex = %{public}d", + installedBundle.name.c_str(), installedBundle.appIndex); + continue; + } + bundleInfos.emplace_back(BJsonEntityCaps::BundleInfo { + installedBundle.name, installedBundle.appIndex, + installedBundle.versionCode, installedBundle.versionName, + 0, 0, allToBackup, fullBackupOnly, + extName, restoreDeps, supportScene, extraInfo}); + } + auto bundleInfosIndex = GetFullBundleInfosForIndex(bundleNames, userId); + auto bundleInfosSA = BundleMgrAdapter::GetBundleInfosForSA(); + copy(bundleInfosIndex.begin(), bundleInfosIndex.end(), back_inserter(bundleInfos)); + copy(bundleInfosSA.begin(), bundleInfosSA.end(), back_inserter(bundleInfos)); + HILOGI("End GetFullBundleInfosForLocalCapabilities, allowtobackup bundle count is : %{public}zu", bundleInfos.size()); + return bundleInfos; +} + +std::vector BundleMgrAdapter::GetBundleInfosForIndex(const vector &bundleNames, int32_t userId) +{ + HILOGI("start GetFullBundleInfosForIndex"); + vector bundleInfos; + auto bms = GetBundleManager(); + for (auto const &bundleName : bundleNames) { + AppExecFwk::BundleInfo installedBundle; + std::vector extensionInfos; + bool getBundleSuccess = GetCurBundleExtenionInfo(installedBundle, bundleName, extensionInfos, bms, userId); + if (!getBundleSuccess) { + HILOGE("Get current extension failed, bundleName:%{public}s", bundleName.c_str()); + continue; + } + auto [allToBackup, fullBackupOnly, extName, restoreDeps, supportScene, extraInfo] = + GetAllowAndExtName(extensionInfos); + if (!allToBackup) { + HILOGI("Not allToBackup, bundleName = %{public}s, appIndex = %{public}d", + installedBundle.name.c_str(), installedBundle.appIndex); + continue; + } + bundleInfos.emplace_back(BJsonEntityCaps::BundleInfo { + installedBundle.name, installedBundle.appIndex, + installedBundle.versionCode,installedBundle.versionName, + 0, 0, allToBackup, fullBackupOnly, + extName, restoreDeps, supportScene, extraInfo}); + } + HILOGI("End, bundleInfos size:%{public}zu", bundleInfos.size()); + return bundleInfos; +} } // 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 abf1a4563..e6978ef65 100644 --- a/services/backup_sa/src/module_ipc/service_stub.cpp +++ b/services/backup_sa/src/module_ipc/service_stub.cpp @@ -72,6 +72,8 @@ ServiceStub::ServiceStub() &ServiceStub::CmdInitBackupSession; opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES)] = &ServiceStub::CmdGetLocalCapabilities; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES_FOR_BDINFOS)] = + &ServiceStub::CmdGetLocalCapabilitiesForBdInfos; opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_FILE)] = &ServiceStub::CmdPublishFile; opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_APP_FILE_READY)] = @@ -194,6 +196,15 @@ int32_t ServiceStub::CmdGetLocalCapabilities(MessageParcel &data, MessageParcel return BError(BError::Codes::OK); } +int32_t ServiceStub::CmdGetLocalCapabilitiesForBdInfos(MessageParcel &data, MessageParcel &reply) +{ + UniqueFd fd(GetLocalCapabilities()); + if (!reply.WriteFileDescriptor(fd)) { + return BError(BError::Codes::SA_BROKEN_IPC, "Failed to send out the file"); + } + return BError(BError::Codes::OK); +} + int32_t ServiceStub::CmdPublishFile(MessageParcel &data, MessageParcel &reply) { unique_ptr fileInfo(data.ReadParcelable()); diff --git a/services/backup_sa/src/module_ipc/sub_service.cpp b/services/backup_sa/src/module_ipc/sub_service.cpp index 72903728a..52c102883 100644 --- a/services/backup_sa/src/module_ipc/sub_service.cpp +++ b/services/backup_sa/src/module_ipc/sub_service.cpp @@ -65,6 +65,24 @@ namespace OHOS::FileManagement::Backup { using namespace std; +namespace { +constexpr int32_t DEBUG_ID = 100; +} + +static inline int32_t GetUserIdDefault() +{ + auto [isDebug, debugId] = BackupPara().GetBackupDebugOverrideAccount(); + if (isDebug && debugId > DEBUG_ID) { + return debugId; + } + auto multiuser = BMultiuser::ParseUid(IPCSkeleton::GetCallingUid()); + HILOGI("GetUserIdDefault userId=%{public}d.", multiuser.userId); + if ((multiuser.userId == BConstants::SYSTEM_UID) || (multiuser.userId == BConstants::XTS_UID)) { + return BConstants::DEFAULT_USER_ID; + } + return multiuser.userId; +} + vector Service::MakeDetailList(const vector &bundleNames) { vector bundleDetails {}; @@ -405,4 +423,49 @@ ErrCode Service::StopExtTimer(bool &isExtStop) return EPERM; } } + +UniqueFd Service::GetLocalCapabilitiesForBdInfos() +{ + try { + HILOGI("start GetLocalCapabilitiesForBdInfos"); + if (session_ == nullptr) { + HILOGE("GetLocalCapabilitiesForBdInfos failed, session is nullptr"); + return UniqueFd(-EPERM); + } + session_->IncreaseSessionCnt(__PRETTY_FUNCTION__); + verifyCaller(); + string path = BConstants::GetSaBundleBackupRootDir(GetUserIdDefault()); + BExcepUltils::VerifyPath(path, false); + CreateDirIfNotExist(path); + UniqueFd fd(open(path.data(), O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR)); + if (fd < 0) { + HILOGE("Failed to open config file = %{private}s, err = %{public}d", path.c_str(), errno); + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + return UniqueFd(-EPERM); + } + BJsonCachedEntity cachedEntity(std::move(fd)); + auto cache = cachedEntity.Structuralize(); + cache.SetSystemFullName(GetOSFullName()); + cache.SetDeviceType(GetDeviceType()); + auto bundleInfos = BundleMgrAdapter::GetFullBundleInfosForLocalCapabilities(GetUserIdDefault()); + if (bundleInfos.size() == 0) { + HILOGE("getBundleInfos failed, size = 0"); + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + return UniqueFd(-EPERM); + } + cache.SetBundleInfos(bundleInfos); + cachedEntity.Persist(); + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + HILOGI("End"); + return move(cachedEntity.GetFd()); + } catch (const BError &e) { + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + HILOGE("GetLocalCapabilitiesForBdInfos failed, errCode = %{public}d", e.GetCode()); + return UniqueFd(-e.GetCode()); + } catch (...) { + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + HILOGI("Unexpected exception"); + return UniqueFd(-EPERM); + } +} } \ 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 7b5340c0b..a92a83861 100644 --- a/tests/mock/backup_kit_inner/service_proxy_mock.cpp +++ b/tests/mock/backup_kit_inner/service_proxy_mock.cpp @@ -250,4 +250,9 @@ void ServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilityFail(int32_t sys { return; } + +UniqueFd ServiceProxy::GetLocalCapabilitiesForBdInfos() +{ + return UniqueFd(-1); +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/mock/module_external/bms_adapter_mock.cpp b/tests/mock/module_external/bms_adapter_mock.cpp index 71b8af8ef..38416eb27 100644 --- a/tests/mock/module_external/bms_adapter_mock.cpp +++ b/tests/mock/module_external/bms_adapter_mock.cpp @@ -91,4 +91,23 @@ vector BundleMgrAdapter::GetBundleInfosForAppend( "com.example.app2backup"}); return bundleInfos; } + +std::vector BundleMgrAdapter::GetBundleInfosForLocalCapabilities(int32_t userId) +{ + vector bundleInfos; + bundleInfos.emplace_back( + BJsonEntityCaps::BundleInfo {"com.example.app2backup", 0, {}, {}, 0, 0, true, false, + "com.example.app2backup"}); + return bundleInfos; +} + +std::vector BundleMgrAdapter::GetBundleInfosForIndex( + const vector &bundleNames, int32_t userId) +{ + vector bundleInfos; + bundleInfos.emplace_back( + BJsonEntityCaps::BundleInfo {"com.example.app2backup", 0, {}, {}, 0, 0, true, false, + "com.example.app2backup"}); + return bundleInfos; +} } // namespace OHOS::FileManagement::Backup diff --git a/tests/mock/module_external/include/bms_adapter_mock.h b/tests/mock/module_external/include/bms_adapter_mock.h index 268cb6924..c35501e1e 100644 --- a/tests/mock/module_external/include/bms_adapter_mock.h +++ b/tests/mock/module_external/include/bms_adapter_mock.h @@ -36,6 +36,8 @@ public: virtual std::vector GetBundleInfosForAppend(const std::vector&, int32_t) = 0; virtual std::vector GetFullBundleInfos(int32_t) = 0; + virtual std::vector GetBundleInfosForLocalCapabilities(int32_t) = 0; + virtual std::vector GetBundleInfosForIndex(const vector&, int32_t) = 0; public: BBundleMgrAdapter() = default; virtual ~BBundleMgrAdapter() = default; @@ -59,6 +61,8 @@ public: MOCK_METHOD((std::vector), GetBundleInfosForAppend, ((const std::vector&), int32_t)); MOCK_METHOD((std::vector), GetFullBundleInfos, (int32_t)); + MOCK_METHOD((std::vector), GetBundleInfosForLocalCapabilities, (int32_t)); + MOCK_METHOD((std::vector), GetBundleInfosForIndex, (const vector&, int32_t)); }; } // namespace OHOS::FileManagement::Backup #endif // OHOS_FILEMGMT_BACKUP_BMS_ADAPTER_MOCK_H diff --git a/tests/mock/module_external/src/bms_adapter_mock.cpp b/tests/mock/module_external/src/bms_adapter_mock.cpp index ee89521f1..8d71d531c 100644 --- a/tests/mock/module_external/src/bms_adapter_mock.cpp +++ b/tests/mock/module_external/src/bms_adapter_mock.cpp @@ -70,4 +70,15 @@ vector BundleMgrAdapter::GetFullBundleInfos(int32_t { return BBundleMgrAdapter::bms->GetFullBundleInfos(userId); } + +std::vector BundleMgrAdapter::GetBundleInfosForLocalCapabilities(int32_t userId) +{ + return BundleMgrAdapter::bms->GetBundleInfosForLocalCapabilities(userId); +} + +std::vector BundleMgrAdapter::GetBundleInfosForIndex( + const vector &bundleNames, int32_t userId) +{ + BundleMgrAdapter::bms->GetBundleInfosForIndex(bundleNames, userId); +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/mock/module_ipc/include/service_stub_mock.h b/tests/mock/module_ipc/include/service_stub_mock.h index 0c3069b46..3643bd3f3 100644 --- a/tests/mock/module_ipc/include/service_stub_mock.h +++ b/tests/mock/module_ipc/include/service_stub_mock.h @@ -26,6 +26,7 @@ public: virtual int32_t CmdInitBackupSession(MessageParcel&, MessageParcel&) = 0; virtual int32_t CmdStart(MessageParcel&, MessageParcel&) = 0; virtual int32_t CmdGetLocalCapabilities(MessageParcel&, MessageParcel&) = 0; + virtual int32_t CmdGetLocalCapabilitiesForBdInfos(MessageParcel&, MessageParcel&) = 0; virtual int32_t CmdPublishFile(MessageParcel&, MessageParcel&) = 0; virtual int32_t CmdAppFileReady(MessageParcel&, MessageParcel&) = 0; virtual int32_t CmdAppDone(MessageParcel&, MessageParcel&) = 0; @@ -69,6 +70,7 @@ public: MOCK_METHOD(int32_t, CmdInitBackupSession, (MessageParcel&, MessageParcel&)); MOCK_METHOD(int32_t, CmdStart, (MessageParcel&, MessageParcel&)); MOCK_METHOD(int32_t, CmdGetLocalCapabilities, (MessageParcel&, MessageParcel&)); + MOCK_METHOD(int32_t, CmdGetLocalCapabilitiesForBdInfos, (MessageParcel&, MessageParcel&)); MOCK_METHOD(int32_t, CmdPublishFile, (MessageParcel&, MessageParcel&)); MOCK_METHOD(int32_t, CmdAppFileReady, (MessageParcel&, MessageParcel&)); MOCK_METHOD(int32_t, CmdAppDone, (MessageParcel&, MessageParcel&)); diff --git a/tests/mock/module_ipc/service_mock.cpp b/tests/mock/module_ipc/service_mock.cpp index 99bcabc99..30d73550a 100644 --- a/tests/mock/module_ipc/service_mock.cpp +++ b/tests/mock/module_ipc/service_mock.cpp @@ -35,6 +35,11 @@ UniqueFd Service::GetLocalCapabilities() return UniqueFd(-1); } +UniqueFd Service::GetLocalCapabilitiesForBdInfos() +{ + return UniqueFd(-1); +} + void Service::StopAll(const wptr &obj, bool force) {} string Service::VerifyCallerAndGetCallerName() @@ -310,4 +315,5 @@ std::vector Service::GetSupportBackupBundleNames(vector&, vector&, bool) {} void Service::SetBundleIncDataInfo(const std::vector&, std::vector&) {} + } // 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 cd36d6d44..17b4eed6a 100644 --- a/tests/mock/module_ipc/service_stub_mock.cpp +++ b/tests/mock/module_ipc/service_stub_mock.cpp @@ -35,6 +35,8 @@ ServiceStub::ServiceStub() &ServiceStub::CmdInitBackupSession; opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES)] = &ServiceStub::CmdGetLocalCapabilities; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES_FOR_BDINFOS)] = + &ServiceStub::CmdGetLocalCapabilitiesForBdInfos; opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_APP_FILE_READY)] = &ServiceStub::CmdAppFileReady; opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_FILE)] = @@ -330,4 +332,9 @@ int32_t ServiceStub::CmdRefreshDataSize(MessageParcel &data, MessageParcel &repl { return BError(BError::Codes::OK); } + +int32_t ServiceStub::CmdGetLocalCapabilitiesForBdInfos(MessageParcel &data, MessageParcel &reply) +{ + return BError(BError::Codes::OK); +} } // namespace OHOS::FileManagement::Backup diff --git a/tests/mock/module_ipc/src/service_stub_mock.cpp b/tests/mock/module_ipc/src/service_stub_mock.cpp index 59deec0f8..358e80de6 100644 --- a/tests/mock/module_ipc/src/service_stub_mock.cpp +++ b/tests/mock/module_ipc/src/service_stub_mock.cpp @@ -166,4 +166,9 @@ int32_t ServiceStub::CmdRefreshDataSize(MessageParcel &data, MessageParcel &repl { return BServiceStub::stub->CmdRefreshDataSize(data, reply); } + +int32_t ServiceStub::CmdGetLocalCapabilitiesForBdInfos(MessageParcel &data, MessageParcel &reply) +{ + return BServiceStub::stub->CmdGetLocalCapabilitiesForBdInfos(data, reply); +} } // namespace OHOS::FileManagement::Backup 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 20ebe6b6e..15063b181 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 @@ -74,6 +74,11 @@ public: return UniqueFd(-1); } + UniqueFd GetLocalCapabilitiesForBdInfos() override + { + return UniqueFd(-1); + } + ErrCode PublishFile(const BFileInfo &fileInfo) override { return BError(BError::Codes::OK); diff --git a/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp b/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp index bb1d1fb06..469317d83 100644 --- a/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp @@ -266,6 +266,10 @@ std::vector Service::GetSupportBackupBundleNames(vector remote)); MOCK_METHOD0(Start, ErrCode()); MOCK_METHOD0(GetLocalCapabilities, UniqueFd()); + MOCK_METHOD0(GetLocalCapabilitiesForBdInfos, UniqueFd()); MOCK_METHOD1(PublishFile, ErrCode(const BFileInfo &fileInfo)); MOCK_METHOD3(AppFileReady, ErrCode(const string &fileName, UniqueFd fd, int32_t errCode)); MOCK_METHOD1(AppDone, ErrCode(ErrCode errCode)); diff --git a/tests/unittests/backup_sa/module_ipc/service_test.cpp b/tests/unittests/backup_sa/module_ipc/service_test.cpp index 0835ba5e0..e74119b22 100644 --- a/tests/unittests/backup_sa/module_ipc/service_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_test.cpp @@ -20,11 +20,17 @@ #include #include "module_ipc/service.h" -#include "service.cpp" #include "service_reverse_mock.h" -#include "sub_service.cpp" #include "test_manager.h" +#include "service.cpp" +#define GetUserIdDefault GetUserIdDefault1 +#define DEBUG_ID DEBUG_ID1 + +#include "sub_service.cpp" +#define GetUserIdDefault1 GetUserIdDefault +#define DEBUG_ID1 DEBUG_ID + namespace OHOS::FileManagement::Backup { using namespace std; using namespace testing; diff --git a/tests/unittests/backup_sa/module_ipc/sub_service_test.cpp b/tests/unittests/backup_sa/module_ipc/sub_service_test.cpp index ce2c88e40..af64f3780 100644 --- a/tests/unittests/backup_sa/module_ipc/sub_service_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/sub_service_test.cpp @@ -1507,4 +1507,36 @@ HWTEST_F(ServiceTest, SUB_Service_TimeoutRadarReport_0000, TestSize.Level1) GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by TimeoutRadarReport."; } GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_TimeoutRadarReport_0000"; +} + +/** + * @tc.number: SUB_Service_GetLocalCapabilitiesForBdInfos_0000 + * @tc.name: SUB_Service_GetLocalCapabilitiesForBdInfos_0000 + * @tc.desc: 测试 GetLocalCapabilitiesForBdInfos 的正常/异常分支 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issueIAKC3I + */ +HWTEST_F(ServiceTest, SUB_Service_GetLocalCapabilitiesForBdInfos_0000, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_GetLocalCapabilitiesForBdInfos_0000"; + try { + ASSERT_TRUE(service != nullptr); + auto session_ = service->session_; + service->session_ = nullptr; + EXPECT_EQ(-EPERM, service->GetLocalCapabilitiesForBdInfos()); + + service->session_ = session_; + EXPECT_CALL(*skeleton, GetCallingTokenID()).WillOnce(Return(0)); + EXPECT_CALL(*token, GetTokenType(_)).WillOnce(Return(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP)); + EXPECT_CALL(*skeleton, GetCallingUid()).WillOnce(Return(BConstants::SYSTEM_UID)); + EXPECT_CALL(*param, GetBackupDebugOverrideAccount()) + .WillOnce(Return(make_pair(true, DEBUG_ID + 1))); + EXPECT_EQ(-EPERM, service->GetLocalCapabilitiesForBdInfos()); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by GetLocalCapabilitiesForBdInfos."; + } + GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_GetLocalCapabilitiesForBdInfos_0000"; } \ 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 index c27b68417..c37fd2e97 100644 --- a/tests/unittests/backup_sa/session/service_proxy_mock.cpp +++ b/tests/unittests/backup_sa/session/service_proxy_mock.cpp @@ -207,4 +207,9 @@ void ServiceProxy::InvaildInstance() {} void ServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilitySuccess(int32_t, const OHOS::sptr&) {} void ServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) {} + +UniqueFd ServiceProxy::GetLocalCapabilitiesForBdInfos() +{ + return UniqueFd(-1); +} } // 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 index 9d213bcd6..7dff5600a 100644 --- a/tests/unittests/backup_sa/session/service_proxy_mock.h +++ b/tests/unittests/backup_sa/session/service_proxy_mock.h @@ -31,6 +31,7 @@ public: MOCK_METHOD0(Start, ErrCode()); MOCK_METHOD0(AsObject, sptr()); MOCK_METHOD0(GetLocalCapabilities, UniqueFd()); + MOCK_METHOD0(GetLocalCapabilitiesForBdInfos, 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)); -- Gitee