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 60648c65b7df2b4fda05859d89a0e6833dd1ff4f..a3ffb975456bed15bef1ba99113dc04ea071f8ee 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 42f9bea125e5fdbef2e5ccb35e32f8e806cbf1db..d7e87ce45e11e5e5cf3c75280ba1cd69f26e760f 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 48ec7869f331bbb9462349f37d92961da5ef79c6..4359053ee49f07d9d5617c7cb0bb5809b75f203a 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 133001e339da5028947452b87e648475695e1265..9c3db774d2f08f7a2115a236a354eba0f2729875 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 7845556880aca234475fd594fbd4603f61b2bda8..ef92a1c494ac38ef163631eac61a83b68a9a7aa5 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 0ab71f6fd4c3ab26cbff31114803b241e6843858..c6767a0bf3a23448423a5bf01b5058392efdf701 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 502fd1a9c39a382a51c9b6965c97a12216ab2ccb..ce2850071734ed354af73910bbd3ef44ee193b59 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 619c0afc2c2c4db543ba5a10c3c1f80ca559417a..ead1a5c00e5995e04a9bcae235e09933325db9a2 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 c831dead301507326356a5c3dedc3a7e42fec58d..082f0d9aaaf91c022d0e547756481b6709c9b4db 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 630867b95115dd1de4e9342bbcfb610a9860990f..ebcea3ccdae997661b78ec631cc2bb2a6d016f34 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 f6027fab4528e8568b4ccbb2eeb259b229ba3b5c..824d412aead272cef9cc2b7afcc0fd5e1db646a9 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 07071b41b773b71af3c7eac1f283f182bd343a5c..81ac33e93b354ae57899129349753fbd51a52854 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 c39d3196a61736db29f79715c798fa5e9a894663..6be68cc21b517abc9be7590484eb34237f7e824b 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 b8dd9f8a04cdf98db96e9ae4bd6f5554eb2101fd..49dafdff5a3e29d52f277340eeb47fbb3ff73451 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); static napi_value Cancel(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 b54fa973f97437563e532d4701e4817dd3bf7528..791c5baf46c12fcb70bafbd702e4b2831b431a88 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 2790b9400f6f69109cfddb5a158aa0a844b343ba..1464797e16bf6e41eac1b632ce1af2ee1e80a7c6 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); static napi_value Cancel(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 ddc36c4c0abbbb33b6754a5105ca9277b9603f56..24791772fc9d52c512bab45ee76d446adf8850fd 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 4222694477aa3f472e35105248c65e0a77537dc6..157f34b4a5ddff950585779220f4593e1a1a6dd7 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 5f2dc4497e4aff076e5ab2c43a89c1f16a23dded..651b6279a67ce8ab49f24155815092be4f7f6045 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 d877ad4a6550f60ccf892f1b47194fef650035f1..3d716cc6d9d8b1216b7228d0cfef21600917d07e 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 8ccaddf564edf79eb8112a9b9e540feefa3ac97b..f3c92babedeffd6e01540299cd68245be660adbf 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 e8322327c7c8df0e7560336007b8dd6c5d66e646..1a2450553360e05dc924bbf7425723a463783368 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 89eda115dca683703e171c0cd09cf68b83c55959..01e4ce512615b2ea8f549643e9638fc29b1c1fff 100644 --- a/services/backup_sa/src/module_ipc/service_stub.cpp +++ b/services/backup_sa/src/module_ipc/service_stub.cpp @@ -74,6 +74,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)] = @@ -196,6 +198,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 c6dc5dd4ee11a13f1c84b245e8947adbab3e3db4..aae59aaa0979630e6b09f038a06ca070091d67c8 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 {}; @@ -411,6 +429,51 @@ ErrCode Service::StopExtTimer(bool &isExtStop) } } +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); + } +} + void Service::ClearFailedBundles() { std::lock_guard lock(failedBundlesLock_); diff --git a/tests/mock/backup_kit_inner/service_proxy_mock.cpp b/tests/mock/backup_kit_inner/service_proxy_mock.cpp index 4ed01cd8f5dd919b6cf0f14fb7fa44ae95d3258c..e734f90c3ee224ccfc60481982f9babf0743989f 100644 --- a/tests/mock/backup_kit_inner/service_proxy_mock.cpp +++ b/tests/mock/backup_kit_inner/service_proxy_mock.cpp @@ -256,4 +256,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 71b8af8ef54fc6d7655b11f90b9c42d31d7cdc01..38416eb278302c20eae5cb02dfad8cd429d4e8fb 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 268cb69245e98676a859ea525fff255269693355..c35501e1e9eed0f003b425dfd00c2b11be9ee783 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 ee89521f1becb56da9ebb29a724ed9805623415d..8d71d531cdee1de4e9ec813fa5423faf9d7e875c 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 cd32d9e662c23b7546c10b9e633e9d4c30108b13..1892a24800bea69c6006447fc00445f00b845db2 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; @@ -70,6 +71,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 786a66bcce577947ff6adb8bd9a04859b914bde7..36e885a777113092ca88c2da788670048a6f9cd2 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() diff --git a/tests/mock/module_ipc/service_stub_mock.cpp b/tests/mock/module_ipc/service_stub_mock.cpp index 97452a3ebb3fea07c203afafc04f213fee20cfa8..8178d0a2d5c32f5653906c58f63922185cebc96d 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)] = @@ -345,4 +347,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 9c792ff93285e569cf169ea486d8d299ee3f99b3..973ff217d9ca90ba4b49fbf79d4a19f960a92a72 100644 --- a/tests/mock/module_ipc/src/service_stub_mock.cpp +++ b/tests/mock/module_ipc/src/service_stub_mock.cpp @@ -171,4 +171,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 d0600cbc6ce010d7712e3fefc463926d96a16d62..2cba7e5abd4389ae647ea7848ee6020147fa8cb9 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 b19ccd042ea254722e57707ac7ac8ebd17100f1a..0e592f8e5013079c7e9469973f54217743db48fb 100644 --- a/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp @@ -269,6 +269,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 ff2176f75f40ab5065ad42a9ce2cb07d711a236e..3a01f90c353ff0096bf3d269989dbba877e4ed9b 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 ce2c88e40223ff5484f89a794b28518a095c33a7..af64f3780d31a4e8201e056249a1808f97e6f36e 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 d41b0a5b8496a10d2e93ddc5d09551fd839b72f0..14fea57c874c24004d7d9f1786c4f344e4dd7dc4 100644 --- a/tests/unittests/backup_sa/session/service_proxy_mock.cpp +++ b/tests/unittests/backup_sa/session/service_proxy_mock.cpp @@ -213,4 +213,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 18df7af1a3d956b08ce7d511d1e37cedcd2afab4..380cf92c12971086298cd2bde9e31c05a18a48f3 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));