diff --git a/frameworks/native/backup_ext/include/ext_extension.h b/frameworks/native/backup_ext/include/ext_extension.h index 9d7ab287b50aa7165e13750bb09d0f615b673912..88ae32c43ebdaf1da145e686dd8b7366b19f35fb 100644 --- a/frameworks/native/backup_ext/include/ext_extension.h +++ b/frameworks/native/backup_ext/include/ext_extension.h @@ -63,6 +63,8 @@ public: void AsyncTaskIncrementalRestoreForUpgrade(void); ErrCode User0OnBackup() override; ErrCode UpdateDfxInfo(int64_t uniqId, uint32_t extConnectSpend, const std::string &bundleName) override; + ErrCode CleanBundleTempDir() override; + public: explicit BackupExtExtension(const std::shared_ptr &extension, const std::string &bundleName) : extension_(extension) @@ -121,6 +123,9 @@ private: /** @brief clear backup restore data */ void DoClear(); + /** @brief inner of doing clear backup restore data */ + void DoClearInner(); + /** * @brief extension backup restore is done * diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index 849fb2b594aeabc3650b6092685d86d965259fd8..073f35e7d6b3da961a4e4e226ed8fcab8b2ea7bb 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -1768,6 +1768,15 @@ void BackupExtExtension::DoClear() HILOGI("configured not clear data."); return; } + DoClearInner(); + } catch (...) { + HILOGE("Failed to clear"); + } +} + +void BackupExtExtension::DoClearInner() +{ + try { string backupCache = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_BACKUP); string restoreCache = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_RESTORE); string specialRestoreCache = GetRestoreTempPath(bundleName_); @@ -2207,4 +2216,25 @@ ErrCode BackupExtExtension::IncrementalAllFileReady(const TarMap &pkgInfo, } return ret; } + +ErrCode BackupExtExtension::CleanBundleTempDir() +{ + HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + HILOGI("BackupExtExtension::CleanBundleTempDir begin"); + if (extension_ == nullptr) { + HILOGE("Failed to CleanBundleTempDir, extension is nullptr"); + return BError(BError::Codes::EXT_INVAL_ARG, "Extension is nullptr").GetCode(); + } + if (extension_->GetExtensionAction() == BConstants::ExtensionAction::INVALID) { + return BError(BError::Codes::EXT_INVAL_ARG, "Action is invalid").GetCode(); + } + try { + VerifyCaller(); + DoClearInner(); + return ERR_OK; + } catch (...) { + HILOGE("Failed to CleanBundleTempDir"); + return BError(BError::Codes::EXT_BROKEN_IPC).GetCode(); + } +} } // namespace OHOS::FileManagement::Backup 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 731e23cdf17a063940fb63225dc28ac998d2e14d..69b84aa79b1770bb7d2b5d01b9a95be5bca748eb 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 @@ -209,4 +209,14 @@ ErrCode BIncrementalBackupSession::Cancel(std::string bundleName) } return result; } + +ErrCode BIncrementalBackupSession::CleanBundleTempDir(const std::string &bundleName) +{ + HILOGI("BIncrementalBackupSession::CleanBundleTempDir"); + auto proxy = ServiceClient::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + return proxy->CleanBundleTempDir(bundleName); +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file 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 25bcef8131ffaa18dd98ac7b02d30de472700afe..5c9864eef1ab014758d27dea2502d563eed794ed 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 @@ -231,4 +231,14 @@ ErrCode BIncrementalRestoreSession::Cancel(std::string bundleName) } return result; } + +ErrCode BIncrementalRestoreSession::CleanBundleTempDir(const std::string &bundleName) +{ + HILOGI("BIncrementalRestoreSession::CleanBundleTempDir"); + auto proxy = ServiceClient::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + return proxy->CleanBundleTempDir(bundleName); +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file 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 863d696d5098672c16def83003596ef783c5e349..8378cf28afece1d27ffe2629e4b4d302bca330e7 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_backup.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_backup.cpp @@ -228,4 +228,14 @@ ErrCode BSessionBackup::Cancel(std::string bundleName) } return result; } + +ErrCode BSessionBackup::CleanBundleTempDir(const std::string &bundleName) +{ + HILOGI("BSessionBackup::CleanBundleTempDir"); + auto proxy = ServiceClient::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + return proxy->CleanBundleTempDir(bundleName); +} } // 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 3daf19df33ddc3948cbd8824583dda21949283a9..703ac92ad28470c534c4ec85069edd53a7e86d87 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_restore.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_restore.cpp @@ -209,4 +209,14 @@ ErrCode BSessionRestore::Cancel(std::string bundleName) } return result; } + +ErrCode BSessionRestore::CleanBundleTempDir(const std::string &bundleName) +{ + HILOGI("BSessionRestore::CleanBundleTempDir"); + auto proxy = ServiceClient::GetInstance(); + if (proxy == nullptr) { + return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(); + } + return proxy->CleanBundleTempDir(bundleName); +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file 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 934cb55932ecd6126b7085ffc6841d1d4097775c..b32113789cdfe8600b667492ac01aec1179ba9e7 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 @@ -115,6 +115,14 @@ public: */ void RegisterBackupServiceDied(std::function functor); + /** + * @brief 备份或者恢复任务结束后,用于清理当前应用的临时目录(./backup目录下的backup和restore目录)的数据 + * + * @param bundleName 应用名称 + * @return ErrCode 规范错误码 + */ + ErrCode CleanBundleTempDir(const std::string &bundleName); + public: ~BIncrementalBackupSession(); 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 3b0e46a9c3498b43dd10d39241405d9080a2c92e..feaadb654955a5a2514c7932a86486a0d03931c8 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 @@ -137,6 +137,14 @@ public: */ void RegisterBackupServiceDied(std::function functor); + /** + * @brief 备份或者恢复任务结束后,用于清理当前应用的临时目录(./backup目录下的backup和restore目录)的数据 + * + * @param bundleName 应用名称 + * @return ErrCode 规范错误码 + */ + ErrCode CleanBundleTempDir(const std::string &bundleName); + public: ~BIncrementalRestoreSession(); 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 0fbcb4dc26fd04246e0c79551af32dcc6da08726..7f5cd583d69ebc0d36ed1e2d89734d14bda6e024 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 @@ -130,6 +130,14 @@ public: */ void RegisterBackupServiceDied(std::function functor); + /** + * @brief 备份或者恢复任务结束后,用于清理当前应用的临时目录(./backup目录下的backup和restore目录)的数据 + * + * @param bundleName 应用名称 + * @return ErrCode 规范错误码 + */ + ErrCode CleanBundleTempDir(const std::string &bundleName); + public: ~BSessionBackup(); 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 7aa5fdcd8fe50ad7082eaa3aefb0b0a3d3e0a4fd..4dd41dc361152df1629db1199a26bc6b545d07e5 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 @@ -137,6 +137,14 @@ public: */ void RegisterBackupServiceDied(std::function functor); + /** + * @brief 备份或者恢复任务结束后,用于清理当前应用的临时目录(./backup目录下的backup和restore目录)的数据 + * + * @param bundleName 应用名称 + * @return ErrCode 规范错误码 + */ + ErrCode CleanBundleTempDir(const std::string &bundleName); + public: ~BSessionRestore(); diff --git a/interfaces/kits/js/backup/session_backup_n_exporter.cpp b/interfaces/kits/js/backup/session_backup_n_exporter.cpp index c04ef6664b89560dfab6604181d2810a8634f504..bcd5846fc44b4275d0dac20243566eb6ef29214f 100644 --- a/interfaces/kits/js/backup/session_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_backup_n_exporter.cpp @@ -655,6 +655,63 @@ napi_value SessionBackupNExporter::Cancel(napi_env env, napi_callback_info info) return nResult; } +static NContextCBExec CleanBundleTempDirCBExec(napi_env env, + const NFuncArg &funcArg, std::unique_ptr bundleName) +{ + 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; + } + return [entity {backupEntity}, bundleName {std::string(bundleName.get())}]() -> NError { + if (!(entity && (entity->session))) { + return NError(BError(BError::Codes::SDK_INVAL_ARG, "Backup session is nullptr").GetCode()); + } + return NError(entity->session->CleanBundleTempDir(bundleName)); + }; +} + +napi_value SessionBackupNExporter::CleanBundleTempDir(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("Called SessionBackupNExporter::CleanBundleTempDir 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::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + NVal jsBundleStr(env, funcArg[NARG_POS::FIRST]); + auto [succ, bundleName, sizeStr] = jsBundleStr.ToUTF8String(); + if (!succ) { + HILOGE("First arguments is not string."); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + auto cbExec = CleanBundleTempDirCBExec(env, funcArg, std::move(bundleName)); + if (cbExec == nullptr) { + HILOGE("CleanBundleTempDirCBExec fail!"); + return nullptr; + } + auto cbCompl = [](napi_env env, NError err) -> NVal { + return err ? NVal::CreateBool(env, false) : NVal::CreateBool(env, true); + }; + + NVal thisVar(env, funcArg.GetThisVar()); + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; +} + bool SessionBackupNExporter::Export() { HILOGD("called SessionBackupNExporter::Export begin"); @@ -664,6 +721,7 @@ bool SessionBackupNExporter::Export() NVal::DeclareNapiFunction("appendBundles", AppendBundles), NVal::DeclareNapiFunction("release", Release), NVal::DeclareNapiFunction("cancel", Cancel), + NVal::DeclareNapiFunction("cleanBundleTempDir", CleanBundleTempDir), }; auto [succ, classValue] = NClass::DefineClass(exports_.env_, className, Constructor, std::move(props)); diff --git a/interfaces/kits/js/backup/session_backup_n_exporter.h b/interfaces/kits/js/backup/session_backup_n_exporter.h index 5fc20d610966df10727baef4b0d19cb89d5df590..708f3b9b714110e8d6bce8848788f324507405e2 100644 --- a/interfaces/kits/js/backup/session_backup_n_exporter.h +++ b/interfaces/kits/js/backup/session_backup_n_exporter.h @@ -31,6 +31,7 @@ public: 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); + static napi_value CleanBundleTempDir(napi_env env, napi_callback_info cbinfo); SessionBackupNExporter(napi_env env, napi_value exports); ~SessionBackupNExporter() override; 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 05912440e21fbcaa731e510b3b52876f3771fbda..6c7c377fd9611ed8416ba5f361d8b889ac7283d1 100644 --- a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.cpp @@ -654,6 +654,63 @@ napi_value SessionIncrementalBackupNExporter::Cancel(napi_env env, napi_callback return nResult; } +static NContextCBExec CleanBundleTempDirCBExec(napi_env env, const NFuncArg &funcArg, + std::unique_ptr bundleName) +{ + 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; + } + return [entity {backupEntity}, bundleName {std::string(bundleName.get())}]() -> NError { + if (!(entity && (entity->session))) { + return NError(BError(BError::Codes::SDK_INVAL_ARG, "Backup session is nullptr").GetCode()); + } + return NError(entity->session->CleanBundleTempDir(bundleName)); + }; +} + +napi_value SessionIncrementalBackupNExporter::CleanBundleTempDir(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("Called SessionIncrementalBackupNExporter::CleanBundleTempDir 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::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + NVal jsBundleStr(env, funcArg[NARG_POS::FIRST]); + auto [succ, bundleName, sizeStr] = jsBundleStr.ToUTF8String(); + if (!succ) { + HILOGE("First arguments is not string."); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + auto cbExec = CleanBundleTempDirCBExec(env, funcArg, std::move(bundleName)); + if (cbExec == nullptr) { + HILOGE("CleanBundleTempDirCBExec fail!"); + return nullptr; + } + auto cbCompl = [](napi_env env, NError err) -> NVal { + return err ? NVal::CreateBool(env, false) : NVal::CreateBool(env, true); + }; + + NVal thisVar(env, funcArg.GetThisVar()); + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; +} + bool SessionIncrementalBackupNExporter::Export() { HILOGD("called SessionIncrementalBackupNExporter::Export begin"); @@ -663,6 +720,7 @@ bool SessionIncrementalBackupNExporter::Export() NVal::DeclareNapiFunction("appendBundles", AppendBundles), NVal::DeclareNapiFunction("release", Release), NVal::DeclareNapiFunction("cancel", Cancel), + NVal::DeclareNapiFunction("cleanBundleTempDir", CleanBundleTempDir), }; auto [succ, classValue] = NClass::DefineClass(exports_.env_, className, Constructor, std::move(props)); 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 d2d5ba4758873d32992445c3a038390630f4f1e9..7f9547232f0fef20da12c0bdcb05b6fca8b11d1e 100644 --- a/interfaces/kits/js/backup/session_incremental_backup_n_exporter.h +++ b/interfaces/kits/js/backup/session_incremental_backup_n_exporter.h @@ -31,6 +31,7 @@ public: 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); + static napi_value CleanBundleTempDir(napi_env env, napi_callback_info cbinfo); SessionIncrementalBackupNExporter(napi_env env, napi_value exports); ~SessionIncrementalBackupNExporter() override; diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.cpp b/interfaces/kits/js/backup/session_restore_n_exporter.cpp index ef6ddf6df0e597aaaec74df1e61ffcd9fd0b2dfc..fc133f10422064741d3d3e4946fdd93ea54ffb9e 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_restore_n_exporter.cpp @@ -841,6 +841,66 @@ napi_value SessionRestoreNExporter::Cancel(napi_env env, napi_callback_info info return nResult; } +static NContextCBExec CleanBundleTempDirCBExec(napi_env env, const NFuncArg &funcArg, + std::unique_ptr bundleName) +{ + 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; + } + return [entity {restoreEntity}, bundleName {string(bundleName.get())}]() -> NError { + if (!(entity && (entity->sessionWhole || entity->sessionSheet))) { + return NError(BError(BError::Codes::SDK_INVAL_ARG, "restore session is nullptr").GetCode()); + } + if (entity->sessionWhole) { + return NError(entity->sessionWhole->CleanBundleTempDir(bundleName)); + } + return NError(entity->sessionSheet->CleanBundleTempDir(bundleName)); + }; +} + +napi_value SessionRestoreNExporter::CleanBundleTempDir(napi_env env, napi_callback_info cbinfo) +{ + HILOGI("Called SessionRestore::CleanBundleTempDir 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::ONE)) { + HILOGE("Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + NVal jsBundleStr(env, funcArg[NARG_POS::FIRST]); + auto [succ, bundleName, sizeStr] = jsBundleStr.ToUTF8String(); + if (!succ) { + HILOGE("First arguments is not string."); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + auto cbExec = CleanBundleTempDirCBExec(env, funcArg, std::move(bundleName)); + if (cbExec == nullptr) { + HILOGE("CleanBundleTempDirCBExec fail!"); + return nullptr; + } + auto cbCompl = [](napi_env env, NError err) -> NVal { + return err ? NVal::CreateBool(env, false) : NVal::CreateBool(env, true); + }; + + NVal thisVar(env, funcArg.GetThisVar()); + return NAsyncWorkPromise(env, thisVar).Schedule(className, cbExec, cbCompl).val_; +} + bool SessionRestoreNExporter::Export() { HILOGD("called SessionRestoreNExporter::Export begin"); @@ -851,6 +911,7 @@ bool SessionRestoreNExporter::Export() NVal::DeclareNapiFunction("getFileHandle", GetFileHandle), NVal::DeclareNapiFunction("release", Release), NVal::DeclareNapiFunction("cancel", Cancel), + NVal::DeclareNapiFunction("cleanBundleTempDir", CleanBundleTempDir), }; 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 157f34b4a5ddff950585779220f4593e1a1a6dd7..12a40fca8a98920d8b466a79b9331e036dbec53b 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.h +++ b/interfaces/kits/js/backup/session_restore_n_exporter.h @@ -35,6 +35,7 @@ public: static napi_value GetFileHandle(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); + static napi_value CleanBundleTempDir(napi_env env, napi_callback_info cbinfo); SessionRestoreNExporter(napi_env env, napi_value exports); ~SessionRestoreNExporter() override; diff --git a/services/backup_sa/IExtension.idl b/services/backup_sa/IExtension.idl index d6d37d3d1711bcb7e5ad7eae3ef2c69dd4a0fa43..4dcff5076053d1961f823cbd4ed59ca1295392f7 100644 --- a/services/backup_sa/IExtension.idl +++ b/services/backup_sa/IExtension.idl @@ -29,4 +29,5 @@ interface OHOS.FileManagement.Backup.IExtension{ [ipccode 12] void UpdateFdSendRate([in] String bundleName, [in] int sendRate); [ipccode 13] void User0OnBackup(); [ipccode 14] void UpdateDfxInfo([in] long uniqId, [in] unsigned int extConnectSpend, [in] String bundleName); + [ipccode 15] void CleanBundleTempDir(); } \ No newline at end of file diff --git a/services/backup_sa/IService.idl b/services/backup_sa/IService.idl index 5ecb32295e735de7c6268fb1dde21bbc7f62c6b9..d5cc82be5d7eeeee2875ae5f671cb53e2eb3a03c 100644 --- a/services/backup_sa/IService.idl +++ b/services/backup_sa/IService.idl @@ -69,4 +69,5 @@ interface OHOS.FileManagement.Backup.IService{ [ipccode 37] void ServiceResultReport([in]String restoreRetInfo, [in] BackupRestoreScenario sennario, [in] int serviceResultReportErrCode); [ipccode 38] void GetBackupDataSize([in] boolean isPreciseScan,[in] BIncrementalData[] bundleNameList); + [ipccode 40] void CleanBundleTempDir([in] String bundleName); } \ No newline at end of file diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index f68dd45d3752715b69e619f16faddf63b53e4b89..d3e961eba0af2f58ad854ee88e3e930fc7171be7 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -45,6 +45,7 @@ struct BundleTaskInfo { ErrCode errCode; }; const int INVALID_FD = -1; +constexpr const int32_t CONNECT_WAIT_TIME_S = 15; class Service : public SystemAbility, public ServiceStub, protected NoCopyable { DECLARE_SYSTEM_ABILITY(Service); @@ -110,6 +111,7 @@ public: void StartGetFdTask(std::string bundleName, wptr ptr); ErrCode GetBackupDataSize(bool isPreciseScan, const std::vector& bundleNameList) override; + ErrCode CleanBundleTempDir(const std::string &bundleName) override; // 以下都是非IPC接口 public: @@ -566,6 +568,15 @@ private: */ void SetOccupySession(bool isOccupyingSession); + /** + * @brief 尝试拉起某个应用的extension + * + * @param bundleName 目标应用 + * @param extConnection 框架和应用的连接 + * + */ + ErrCode TryToConnectExt(const std::string& bundleName, sptr& extConnection); + void ReportOnExtConnectFailed(const IServiceReverseType::Scenario scenario, const std::string &bundleName, const ErrCode ret); diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 6e421860207f5d8109c9c4a0e3708528ba204fa0..ecced7d84006be9436941820e4de4f4c8fe18cd7 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -81,7 +81,6 @@ constexpr int32_t MS_1000 = 1000; const static string BROADCAST_TYPE = "broadcast"; const std::string FILE_BACKUP_EVENTS = "FILE_BACKUP_EVENTS"; const static string UNICAST_TYPE = "unicast"; -const int32_t CONNECT_WAIT_TIME_S = 15; const std::string BACKUPSERVICE_WORK_STATUS_KEY = "persist.backupservice.workstatus"; const std::string BACKUPSERVICE_WORK_STATUS_ON = "true"; const std::string BACKUPSERVICE_WORK_STATUS_OFF = "false"; @@ -1471,7 +1470,7 @@ void Service::ClearDisposalOnSaStart() if (!bundleNameList.empty()) { for (vector::iterator it = bundleNameList.begin(); it != bundleNameList.end(); ++it) { string bundleName = *it; - HILOGE("dispose has residual, clear now, bundelName =%{public}s", bundleName.c_str()); + HILOGE("dispose has residual, clear now, bundleName =%{public}s", bundleName.c_str()); TryToClearDispose(bundleName); } } diff --git a/services/backup_sa/src/module_ipc/sub_service.cpp b/services/backup_sa/src/module_ipc/sub_service.cpp index af34f0b6f25a94f62f60a401b69e83dd2acdb2fc..3fe2a899d24dc6a60fcca7b5012e638f02d0adb3 100644 --- a/services/backup_sa/src/module_ipc/sub_service.cpp +++ b/services/backup_sa/src/module_ipc/sub_service.cpp @@ -1320,4 +1320,70 @@ ErrCode Service::StartFwkTimer(bool &isFwkStart) return EPERM; } } + +ErrCode Service::TryToConnectExt(const std::string& bundleName, sptr& extConnection) +{ + extConnection = session_->GetExtConnection(bundleName); + if (extConnection != nullptr && extConnection->IsExtAbilityConnected()) { + return BError(BError::Codes::OK); + } + if (extConnection == nullptr) { + extConnection = session_->CreateBackupConnection(bundleName); + if (extConnection == nullptr) { + HILOGE("backupConnection is null, bundleName: %{public}s", bundleName.c_str()); + return BError(BError::Codes::SA_INVAL_ARG); + } + } + auto callConnected = GetBackupInfoConnectDone(wptr(this), bundleName); + auto callDied = GetBackupInfoConnectDied(wptr(this), bundleName); + extConnection->SetCallback(callConnected); + extConnection->SetCallDied(callDied); + AAFwk::Want want = CreateConnectWant(bundleName); + ErrCode err = extConnection->ConnectBackupExtAbility(want, GetUserIdDefault(), false); + if (err != BError(BError::Codes::OK)) { + HILOGE("ConnectBackupExtAbility failed, bundleName:%{public}s, ret:%{public}d", bundleName.c_str(), err); + return BError(BError::Codes::SA_BOOT_EXT_FAIL); + } + return BError(BError::Codes::OK); +} + +ErrCode Service::CleanBundleTempDir(const string &bundleName) +{ + HILOGI("Service::CleanBundleTempDir"); + HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + + if (session_ == nullptr) { + HILOGE("session is empty."); + return BError(BError::Codes::SA_INVAL_ARG); + } + sptr backupConnection; + ErrCode err = TryToConnectExt(bundleName, backupConnection); + if (err != BError(BError::Codes::OK)) {return err;} + + std::unique_lock lock(getBackupInfoSyncLock_); + getBackupInfoCondition_.wait_for(lock, std::chrono::seconds(CONNECT_WAIT_TIME_S)); + if (isConnectDied_.load()) { + HILOGE("GetBackupInfoConnectDied, please check bundleName: %{public}s", bundleName.c_str()); + isConnectDied_.store(false); + return BError(BError::Codes::EXT_ABILITY_DIED); + } + + session_->IncreaseSessionCnt(__PRETTY_FUNCTION__); + if (backupConnection == nullptr) { + HILOGE("backupConnection is empty."); + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + return BError(BError::Codes::SA_INVAL_ARG); + } + auto proxy = backupConnection->GetBackupExtProxy(); + if (!proxy) { + HILOGE("Extension backup Proxy is empty."); + backupConnection->DisconnectBackupExtAbility(); + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + return BError(BError::Codes::SA_INVAL_ARG); + } + proxy->CleanBundleTempDir(); + backupConnection->DisconnectBackupExtAbility(); + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + return BError(BError::Codes::OK); +} } \ No newline at end of file diff --git a/tests/mock/backup_ext/include/ext_extension_mock.h b/tests/mock/backup_ext/include/ext_extension_mock.h index 65733f7ad2cf5122c7f49db72f9f7b05ed4b4086..c57e10ae2828e5cd3cde4fa500a5c30168d823f6 100644 --- a/tests/mock/backup_ext/include/ext_extension_mock.h +++ b/tests/mock/backup_ext/include/ext_extension_mock.h @@ -120,6 +120,7 @@ public: MOCK_METHOD((std::function), HandleTaskBackupEx, (wptr)); MOCK_METHOD(void, WaitToSendFd, ((std::chrono::system_clock::time_point&), int&)); MOCK_METHOD(void, RefreshTimeInfo, ((std::chrono::system_clock::time_point&), int&)); + MOCK_METHOD(ErrCode, CleanBundleTempDir, ()); }; } // namespace OHOS::FileManagement::Backup #endif // OHOS_FILEMGMT_BACKUP_EXT_EXTENSION_MOCK_H \ No newline at end of file diff --git a/tests/mock/backup_ext/src/ext_extension_mock.cpp b/tests/mock/backup_ext/src/ext_extension_mock.cpp index 4c54b88bfaf2a4414a1edb2d5da816a73aa8c335..f5bf98f3442f0b94de30bcc2d12ae6b22f8c7f4f 100644 --- a/tests/mock/backup_ext/src/ext_extension_mock.cpp +++ b/tests/mock/backup_ext/src/ext_extension_mock.cpp @@ -217,4 +217,9 @@ void BackupExtExtension::WaitToSendFd(std::chrono::system_clock::time_point &sta void BackupExtExtension::RefreshTimeInfo(std::chrono::system_clock::time_point &startTime, int &fdSendNum) { } + +ErrCode BackupExtExtension::CleanBundleTempDir() +{ + return BExtExtension::extExtension->CleanBundleTempDir(); +} } // namespace OHOS::FileManagement::Backup diff --git a/tests/mock/backup_kit_inner/service_proxy_mock.cpp b/tests/mock/backup_kit_inner/service_proxy_mock.cpp index 5abdac17723d31f59b1fd17e1b4d8113e05d6f52..d521ebec5a5b50ff13799758ed6201d78df17f9c 100644 --- a/tests/mock/backup_kit_inner/service_proxy_mock.cpp +++ b/tests/mock/backup_kit_inner/service_proxy_mock.cpp @@ -255,4 +255,9 @@ ErrCode ServiceProxy::GetBackupDataSize(bool isPreciseScan, const vector bundleNameList, string &scanning) {} void Service::GetPresumablySize(vector bundleNameList, string &scanning) {} diff --git a/tests/mock/module_ipc/svc_extension_proxy_mock.cpp b/tests/mock/module_ipc/svc_extension_proxy_mock.cpp index 63eacdcff70a7b73d6f80c54d837a0e68dabc57a..d9e0e8c94ea4c199c9c31ab40513705327b2875e 100644 --- a/tests/mock/module_ipc/svc_extension_proxy_mock.cpp +++ b/tests/mock/module_ipc/svc_extension_proxy_mock.cpp @@ -87,4 +87,9 @@ ErrCode ExtensionProxy::UpdateDfxInfo(int64_t uniqId, uint32_t extConnectSpend, { return 0; } + +ErrCode ExtensionProxy::CleanBundleTempDir() +{ + return 0; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/moduletests/backup_kit_inner/b_session_backup_test.cpp b/tests/moduletests/backup_kit_inner/b_session_backup_test.cpp index eae6c10edad5ce233f1dd0daf88f9f22cd908cb5..727593169b963269156dc0efc9194f812601b4ef 100644 --- a/tests/moduletests/backup_kit_inner/b_session_backup_test.cpp +++ b/tests/moduletests/backup_kit_inner/b_session_backup_test.cpp @@ -475,4 +475,38 @@ HWTEST_F(BSessionBackupTest, SUB_backup_b_session_backup_1200, testing::ext::Tes } GTEST_LOG_(INFO) << "BSessionBackupTest-end SUB_backup_b_session_backup_1200"; } + +/** + * @tc.number: SUB_backup_b_session_backup_1300 + * @tc.name: SUB_backup_b_session_backup_1300 + * @tc.desc: 测试 CleanBundleTempDir 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: IC7RHQ + */ +HWTEST_F(BSessionBackupTest, SUB_backup_b_session_backup_1300, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "BSessionBackupTest-begin SUB_backup_b_session_backup_1300"; + try { + if (backupPtr_ == nullptr) { + GTEST_LOG_(INFO) << "SUB_backup_b_session_backup_1300 backupPtr_ == nullptr"; + return; + } + GTEST_LOG_(INFO) << "GetInstance is false"; + std::string bundleName; + SetMockGetInstance(false); + auto err = backupPtr_->CleanBundleTempDir(bundleName); + EXPECT_EQ(err, BError(BError::Codes::SDK_BROKEN_IPC).GetCode()); + + GTEST_LOG_(INFO) << "GetInstance is true"; + SetMockGetInstance(true); + err = backupPtr_->CleanBundleTempDir(bundleName); + EXPECT_EQ(err, ERR_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "BSessionBackupTest-an exception occurred by CleanBundleTempDir."; + } + GTEST_LOG_(INFO) << "BSessionBackupTest-end SUB_backup_b_session_backup_1300"; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/unittests/backup_api/backup_impl/include/ext_extension_mock.h b/tests/unittests/backup_api/backup_impl/include/ext_extension_mock.h index be0b0d4c8c5e1173cb7e8705a28c3048e227d30f..34aaef2aeac080d3cb417d5fd53de0a7cc233163 100644 --- a/tests/unittests/backup_api/backup_impl/include/ext_extension_mock.h +++ b/tests/unittests/backup_api/backup_impl/include/ext_extension_mock.h @@ -168,6 +168,11 @@ public: { return ERR_OK; }; + + ErrCode CleanBundleTempDir() override + { + return BError(BError::Codes::OK); + }; private: int32_t nHandleBackupNum_ = 0; }; 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 8694c8f4d950adc9e443d6e14b1261196149dd8b..be238cc15966325658fdaf54069542a4e4bb5fb0 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 @@ -267,6 +267,11 @@ public: { return BError(BError::Codes::OK); } + + ErrCode CleanBundleTempDir(const std::string &bundleName) 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_sa/module_ipc/service_incremental_test.cpp b/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp index 4052238614604bb5983788bb95aaf8dc11bcbba4..70891991786660c56c79f0d040a4729460fde23c 100644 --- a/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp @@ -75,6 +75,7 @@ public: virtual ErrCode HandleCurAppDone(ErrCode, const std::string&, bool) = 0; virtual UniqueFd GetLocalCapabilitiesForBundleInfos() = 0; virtual ErrCode GetBackupDataSize(bool, const std::vector&) = 0; + virtual ErrCode CleanBundleTempDir(const std::string& bundleName) = 0; public: virtual bool UpdateToRestoreBundleMap(const string&, const string&) = 0; public: @@ -129,6 +130,7 @@ public: MOCK_METHOD(ErrCode, HandleCurAppDone, (ErrCode, const std::string&, bool)); MOCK_METHOD(UniqueFd, GetLocalCapabilitiesForBundleInfos, ()); MOCK_METHOD(ErrCode, GetBackupDataSize, (bool, const std::vector&)); + MOCK_METHOD(ErrCode, CleanBundleTempDir, (const std::string&)); public: MOCK_METHOD(bool, UpdateToRestoreBundleMap, (const string&, const string&)); }; @@ -433,6 +435,11 @@ ErrCode Service::GetBackupDataSize(bool isPreciseScan, const std::vectorGetBackupDataSize(isPreciseScan, bundleNameList); } + +ErrCode Service::CleanBundleTempDir(const std::string& bundleName) +{ + return BService::serviceMock->CleanBundleTempDir(bundleName); +} } // namespace OHOS::FileManagement::Backup namespace OHOS::FileManagement::Backup { diff --git a/tests/unittests/backup_sa/module_ipc/service_other_test.cpp b/tests/unittests/backup_sa/module_ipc/service_other_test.cpp index 5c80651bfe8ab224b4c00762099ff849795aa641..ef69344b38c216ef1f83c2962b6021f7b90b7774 100644 --- a/tests/unittests/backup_sa/module_ipc/service_other_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_other_test.cpp @@ -2322,4 +2322,195 @@ HWTEST_F(ServiceTest, SUB_Service_GetSupportBackupBundleNames_0100, testing::ext } GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_GetSupportBackupBundleNames_0100"; } + +/** + * @tc.number: SUB_Service_TryToConnectExt_0000 + * @tc.name: SUB_Service_TryToConnectExt_0000 + * @tc.desc: 测试 TryToConnectExt 的正常/异常分支 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: IC7RHQ + */ +HWTEST_F(ServiceTest, SUB_Service_TryToConnectExt_0000, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_TryToConnectExt_0000"; + try { + BundleName bundleName; + sptr extConnection; + EXPECT_CALL(*session, GetExtConnection(_)).WillOnce(Return(connect)); + EXPECT_CALL(*connect, GetBackupExtProxy()).WillOnce(Return(svcProxy)); + EXPECT_CALL(*connect, IsExtAbilityConnected()).WillOnce(Return(true)); + EXPECT_CALL(*connect, DisconnectBackupExtAbility()).WillOnce(Return(BError(BError::Codes::OK).GetCode())); + EXPECT_CALL(*session, GetScenario()).WillRepeatedly(Return(IServiceReverseType::Scenario::UNDEFINED)); + EXPECT_CALL(*connect, ConnectBackupExtAbility(_, _, _)).WillOnce(Return(BError(BError::Codes::OK).GetCode())); + auto res = service->TryToConnectExt(bundleName, extConnection); + EXPECT_EQ(res, BError(BError::Codes::OK).GetCode()); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by TryToConnectExt."; + } + GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_TryToConnectExt_0000"; +} + +/** + * @tc.number: SUB_Service_TryToConnectExt_0100 + * @tc.name: SUB_Service_TryToConnectExt_0100 + * @tc.desc: 测试 TryToConnectExt 的正常/异常分支 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: IC7RHQ + */ +HWTEST_F(ServiceTest, SUB_Service_TryToConnectExt_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_TryToConnectExt_0100"; + try { + BundleName bundleName; + sptr extConnection; + EXPECT_CALL(*session, GetExtConnection(_)).WillOnce(Return(nullptr)); + EXPECT_CALL(*connect, GetBackupExtProxy()).WillOnce(Return(svcProxy)); + EXPECT_CALL(*connect, IsExtAbilityConnected()).WillOnce(Return(true)); + EXPECT_CALL(*session, CreateBackupConnection(_)).WillRepeatedly(Return(nullptr)); + EXPECT_CALL(*connect, DisconnectBackupExtAbility()).WillOnce(Return(BError(BError::Codes::OK).GetCode())); + EXPECT_CALL(*session, GetScenario()).WillRepeatedly(Return(IServiceReverseType::Scenario::UNDEFINED)); + EXPECT_CALL(*connect, ConnectBackupExtAbility(_, _, _)) + .WillOnce(Return(BError(BError::Codes::EXT_INVAL_ARG).GetCode())); + auto res = service->TryToConnectExt(bundleName, extConnection); + EXPECT_EQ(res, BError(BError::Codes::SA_INVAL_ARG).GetCode()); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by TryToConnectExt."; + } + GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_TryToConnectExt_0100"; +} + +/** + * @tc.number: SUB_Service_TryToConnectExt_0200 + * @tc.name: SUB_Service_TryToConnectExt_0200 + * @tc.desc: 测试 TryToConnectExt 的正常/异常分支 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: IC7RHQ + */ +HWTEST_F(ServiceTest, SUB_Service_TryToConnectExt_0200, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_TryToConnectExt_0200"; + try { + BundleName bundleName; + sptr extConnection; + EXPECT_CALL(*session, GetExtConnection(_)).WillOnce(Return(connect)); + EXPECT_CALL(*connect, GetBackupExtProxy()).WillOnce(Return(svcProxy)); + EXPECT_CALL(*connect, IsExtAbilityConnected()).WillOnce(Return(false)); + EXPECT_CALL(*session, GetSessionUserId()).WillRepeatedly(Return(0)); + EXPECT_CALL(*param, GetBackupDebugOverrideAccount()) + .WillRepeatedly(Return(make_pair(true, DEBUG_ID + 1))); + EXPECT_CALL(*skeleton, GetCallingUid()).WillRepeatedly(Return(BConstants::XTS_UID)); + EXPECT_CALL(*connect, DisconnectBackupExtAbility()).WillOnce(Return(BError(BError::Codes::OK).GetCode())); + EXPECT_CALL(*session, GetScenario()).WillRepeatedly(Return(IServiceReverseType::Scenario::UNDEFINED)); + EXPECT_CALL(*connect, ConnectBackupExtAbility(_, _, _)).WillOnce(Return(BError(BError::Codes::OK).GetCode())); + auto res = service->TryToConnectExt(bundleName, extConnection); + EXPECT_EQ(res, BError(BError::Codes::OK).GetCode()); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by TryToConnectExt."; + } + GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_TryToConnectExt_0200"; +} + +/** + * @tc.number: SUB_Service_TryToConnectExt_0300 + * @tc.name: SUB_Service_TryToConnectExt_0300 + * @tc.desc: 测试 TryToConnectExt 的正常/异常分支 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: IC7RHQ + */ +HWTEST_F(ServiceTest, SUB_Service_TryToConnectExt_0300, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_TryToConnectExt_0300"; + try { + BundleName bundleName; + sptr extConnection; + EXPECT_CALL(*session, GetExtConnection(_)).WillOnce(Return(connect)); + EXPECT_CALL(*connect, GetBackupExtProxy()).WillOnce(Return(svcProxy)); + EXPECT_CALL(*connect, IsExtAbilityConnected()).WillOnce(Return(false)); + EXPECT_CALL(*session, GetSessionUserId()).WillRepeatedly(Return(0)); + EXPECT_CALL(*param, GetBackupDebugOverrideAccount()) + .WillRepeatedly(Return(make_pair(true, DEBUG_ID + 1))); + EXPECT_CALL(*skeleton, GetCallingUid()).WillRepeatedly(Return(BConstants::XTS_UID)); + EXPECT_CALL(*connect, DisconnectBackupExtAbility()).WillOnce(Return(BError(BError::Codes::OK).GetCode())); + EXPECT_CALL(*session, GetScenario()).WillRepeatedly(Return(IServiceReverseType::Scenario::UNDEFINED)); + EXPECT_CALL(*connect, ConnectBackupExtAbility(_, _, _)) + .WillOnce(Return(BError(BError::Codes::SA_BOOT_EXT_FAIL).GetCode())); + auto res = service->TryToConnectExt(bundleName, extConnection); + EXPECT_EQ(res, BError(BError::Codes::SA_BOOT_EXT_FAIL).GetCode()); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by TryToConnectExt."; + } + GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_TryToConnectExt_0300"; +} + +/** + * @tc.number: SUB_Service_CleanBundleTempDir_0000 + * @tc.name: SUB_Service_CleanBundleTempDir_0000 + * @tc.desc: 测试 CleanBundleTempDir 的正常/异常分支 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: IC7RHQ + */ +HWTEST_F(ServiceTest, SUB_Service_CleanBundleTempDir_0000, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_CleanBundleTempDir_0000"; + try { + BundleName bundleName; + sptr extConnection; + EXPECT_CALL(*session, GetExtConnection(_)).WillOnce(Return(connect)); + EXPECT_CALL(*connect, GetBackupExtProxy()).WillOnce(Return(svcProxy)); + EXPECT_CALL(*connect, IsExtAbilityConnected()).WillOnce(Return(true)); + EXPECT_CALL(*connect, DisconnectBackupExtAbility()).WillOnce(Return(BError(BError::Codes::OK).GetCode())); + EXPECT_CALL(*session, GetScenario()).WillRepeatedly(Return(IServiceReverseType::Scenario::UNDEFINED)); + EXPECT_CALL(*connect, ConnectBackupExtAbility(_, _, _)).WillOnce(Return(BError(BError::Codes::OK).GetCode())); + auto res = service->CleanBundleTempDir(bundleName); + EXPECT_EQ(res, BError(BError::Codes::OK).GetCode()); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by CleanBundleTempDir."; + } + GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_CleanBundleTempDir_0000"; +} + +/** + * @tc.number: SUB_Service_CleanBundleTempDir_0100 + * @tc.name: SUB_Service_CleanBundleTempDir_0100 + * @tc.desc: 测试 CleanBundleTempDir 的正常/异常分支 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: IC7RHQ + */ +HWTEST_F(ServiceTest, SUB_Service_CleanBundleTempDir_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_CleanBundleTempDir_0100"; + try { + BundleName bundleName; + sptr extConnection; + EXPECT_CALL(*session, GetExtConnection(_)).WillOnce(Return(connect)); + EXPECT_CALL(*connect, GetBackupExtProxy()).WillOnce(Return(nullptr)); + EXPECT_CALL(*connect, IsExtAbilityConnected()).WillOnce(Return(true)); + EXPECT_CALL(*connect, DisconnectBackupExtAbility()).WillOnce(Return(BError(BError::Codes::OK).GetCode())); + EXPECT_CALL(*session, GetScenario()).WillRepeatedly(Return(IServiceReverseType::Scenario::UNDEFINED)); + EXPECT_CALL(*connect, ConnectBackupExtAbility(_, _, _)).WillOnce(Return(BError(BError::Codes::OK).GetCode())); + auto res = service->CleanBundleTempDir(bundleName); + EXPECT_EQ(res, BError(BError::Codes::SA_INVAL_ARG).GetCode()); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by CleanBundleTempDir."; + } + GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_CleanBundleTempDir_0100"; +} } \ No newline at end of file diff --git a/tests/unittests/backup_sa/session/b_incremental_session_test.cpp b/tests/unittests/backup_sa/session/b_incremental_session_test.cpp index cd176ce501c4c90e8e46950d5973348fa656e7ac..47852f95022d2e4f31f29d0b3d44e9b9f7d5a1a3 100644 --- a/tests/unittests/backup_sa/session/b_incremental_session_test.cpp +++ b/tests/unittests/backup_sa/session/b_incremental_session_test.cpp @@ -935,4 +935,38 @@ HWTEST_F(IncrementalSessionTest, SUB_b_incremental_session_test_2800, testing::e } GTEST_LOG_(INFO) << "IncrementalSessionTest-end SUB_b_incremental_session_test_2800"; } + +/** + * @tc.number: SUB_b_incremental_session_test_2900 + * @tc.name: SUB_b_incremental_session_test_2900 + * @tc.desc: 测试 CleanBundleTempDir 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: IC7RHQ + */ +HWTEST_F(IncrementalSessionTest, SUB_b_incremental_session_test_2900, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IncrementalSessionTest-begin SUB_b_incremental_session_test_2900"; + try { + std::string bundleName; + ServiceClient::serviceProxy_ = nullptr; + ASSERT_TRUE(backupSession != nullptr); + auto err = backupSession->CleanBundleTempDir(bundleName); + EXPECT_EQ(err, BError(BError::Codes::SDK_BROKEN_IPC).GetCode()); + + ServiceClient::serviceProxy_ = proxy; + EXPECT_CALL(*proxy, CleanBundleTempDir(_)).WillOnce(Return(BError(BError::Codes::SDK_BROKEN_IPC).GetCode())); + err = backupSession->CleanBundleTempDir(bundleName); + EXPECT_EQ(err, BError(BError::Codes::SDK_BROKEN_IPC).GetCode()); + + EXPECT_CALL(*proxy, CleanBundleTempDir(_)).WillOnce(Return(BError(BError::Codes::OK).GetCode())); + err = backupSession->CleanBundleTempDir(bundleName); + EXPECT_EQ(err, ERR_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "IncrementalSessionTest-an exception occurred by CleanBundleTempDir."; + } + GTEST_LOG_(INFO) << "IncrementalSessionTest-end SUB_b_incremental_session_test_2900"; +} } // namespace OHOS::FileManagement::Backup \ 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 bfdd210affa055d615e1ec68477042d98040e7f1..2c0faa543e70b0aa935b5418f241a7bcffd1363b 100644 --- a/tests/unittests/backup_sa/session/service_proxy_mock.cpp +++ b/tests/unittests/backup_sa/session/service_proxy_mock.cpp @@ -229,4 +229,9 @@ ErrCode ServiceProxy::GetBackupDataSize(bool isPreciseScan, const vector &, int32_t, int32_t)); MOCK_METHOD2(GetBackupDataSize, ErrCode(bool isPreciseScan, const std::vector &bundleNameList)); + MOCK_METHOD1(CleanBundleTempDir, ErrCode(const std::string &bundleName)); }; } // End of namespace OHOS::FileManagement::Backup #endif // TEST_UNITTEST_SERVICE_PROXY_MOCK_H