diff --git a/services/backup_sa/BUILD.gn b/services/backup_sa/BUILD.gn index b7ba886e93253f812d44cef4599d0214624bccc8..8c877bed7bf7acf436005dcaf3b355ec3aa5ce41 100644 --- a/services/backup_sa/BUILD.gn +++ b/services/backup_sa/BUILD.gn @@ -28,6 +28,7 @@ ohos_shared_library("backup_sa") { sources = [ "src/module_app_gallery/app_gallery_dispose_proxy.cpp", + "src/module_app_gallery/app_gallery_service_connection.cpp", "src/module_external/bms_adapter.cpp", "src/module_external/sms_adapter.cpp", "src/module_ipc/sa_backup_connection.cpp", diff --git a/services/backup_sa/include/module_app_gallery/app_gallery_dispose_proxy.h b/services/backup_sa/include/module_app_gallery/app_gallery_dispose_proxy.h index e98a16dbe30c3a5cd73b07e9e12c22bc786bf7d0..fdd41a59dadadc3ff0266cc06b27b7691bbb52fc 100644 --- a/services/backup_sa/include/module_app_gallery/app_gallery_dispose_proxy.h +++ b/services/backup_sa/include/module_app_gallery/app_gallery_dispose_proxy.h @@ -17,10 +17,12 @@ #define OHOS_FILEMGMT_BACKUP_APP_GALLERY_DISPOSE_PROXY_H #include +#include #include #include +#include "app_gallery_service_connection.h" #include "i_appgallery_service.h" namespace OHOS::FileManagement::Backup { @@ -39,24 +41,24 @@ public: static sptr GetInstance(); - DisposeErr StartBackup(const std::string &bundleName); - DisposeErr EndBackup(const std::string &bundleName); - DisposeErr StartRestore(const std::string &bundleName); - DisposeErr EndRestore(const std::string &bundleName); + DisposeErr StartBackup(const std::string &bundleName, const int32_t userId); + DisposeErr EndBackup(const std::string &bundleName, const int32_t userId); + DisposeErr StartRestore(const std::string &bundleName, const int32_t userId); + DisposeErr EndRestore(const std::string &bundleName, const int32_t userId); + + sptr GetAppGalleryConnection(const int32_t userId); + sptr ConnectAppGallery(const int32_t userId); - static std::condition_variable conditionVal_; static std::string abilityName; static std::string pkgName; - static std::mutex appRemoteObjLock_; - static std::mutex connectMutex; - static sptr appRemoteObj_; + static std::map> appGalleryConnectionMap_; private: - DisposeErr DoDispose(const std::string &bundleName, DisposeOperation disposeOperation); + DisposeErr DoDispose(const std::string &bundleName, DisposeOperation disposeOperation, const int32_t userId); private: static std::mutex instanceLock_; - static std::mutex conditionMutex_; + static std::mutex appGalleryConnectionLock_; static sptr appGalleryDisposeProxyInstance_; }; } // namespace OHOS::FileManagement::Backup diff --git a/services/backup_sa/include/module_app_gallery/app_gallery_service_connection.h b/services/backup_sa/include/module_app_gallery/app_gallery_service_connection.h index c5a06b7fc6f6c89d60e8ab65c85a19a7afbc42f9..30a2982e6bc1ec3459f7e899662b708c6e3b848f 100644 --- a/services/backup_sa/include/module_app_gallery/app_gallery_service_connection.h +++ b/services/backup_sa/include/module_app_gallery/app_gallery_service_connection.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -18,67 +18,36 @@ #include #include -#include #include "ability_manager_client.h" #include "ability_connect_callback_stub.h" #include "want.h" #include "i_appgallery_service.h" -#include "module_external/bms_adapter.h" -#include "filemgmt_libhilog.h" namespace OHOS::FileManagement::Backup { using namespace OHOS::AppExecFwk; -const int32_t CONNECT_TIME = 3; -template class AppGralleryConnection : public AbilityConnectionStub { +class AppGalleryConnection : public AbilityConnectionStub { public: - void OnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr &remoteObject, - int resultCode) - { - std::string uri = element.GetURI(); - HILOGI("OnAbilityConnectDone, uri = %{public}s", uri.c_str()); - T::appRemoteObj_ = remoteObject; - T::conditionVal_.notify_one(); - } + AppGalleryConnection(int32_t userId) : userId(userId) {} - void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) - { - std::string uri = element.GetURI(); - HILOGI("OnAbilityDisconnectDone, uri = %{public}s", uri.c_str()); - T::appRemoteObj_ = nullptr; - } -}; + void OnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr &remoteObject, + int resultCode); -template bool ConnectExtAbility() -{ - HILOGI("ConnectExtAbility called"); - std::lock_guard autoLock(T::appRemoteObjLock_); - if (T::appRemoteObj_ != nullptr) { - return true; - } + void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode); - auto appGalleryBundleName = BundleMgrAdapter::GetAppGalleryBundleName(); - if (appGalleryBundleName.empty()) { - HILOGI("ConnectExtAbility GetAppGalleryBundleName failed"); - return false; - } + bool ConnectExtAbility(std::string abilityName); - Want want; - want.SetElementName(appGalleryBundleName.c_str(), T::abilityName); - sptr connect = new AppGralleryConnection(); - auto ret = AbilityManagerClient::GetInstance()->ConnectAbility(want, connect, -1); + sptr GetRemoteObj(); - std::unique_lock uniqueLock(T::connectMutex); - T::conditionVal_.wait_for(uniqueLock, std::chrono::seconds(CONNECT_TIME)); - if (ret != IAppGalleryService::ERR_OK || T::appRemoteObj_ == nullptr) { - HILOGI("ConnectExtAbility failed, ret=%{public}d", ret); - T::appRemoteObj_ = nullptr; - return false; - } - HILOGI("ConnectExtAbility success, ret=%{public}d", ret); - return true; +private: + int32_t userId; + std::mutex conditionMutex_; + std::condition_variable conditionVal_; + std::mutex connectMutex; + std::mutex appRemoteObjLock_; + sptr appRemoteObj_; }; } // namespace OHOS::FileManagement::Backup #endif // OHOS_FILEMGMT_BACKUP_APP_GALLERY_SERVICE_CONNECTION_H \ 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 5665c5b3920750089039dd1289a7d9fad2126483..740de8b6bcadbf1a7c61ee46b5f8fd1c649c6cb9 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -310,6 +310,10 @@ public: */ void RemoveExtensionMutex(const BundleName &bundleName); void StartRunningTimer(const std::string &bundleName); + string BundleNameWithUserId(const string& bundleName, const int32_t userId); + std::tuple SplitBundleName(const string& bundleNameWithId); + void AppendBundles(const std::vector &bundleNames); + void ReportOnBundleStarted(IServiceReverse::Scenario scenario, const std::string &bundleName); ErrCode AppIncrementalFileReady(const std::string &bundleName, const std::string &fileName, UniqueFd fd, UniqueFd manifestFd, int32_t errCode); public: diff --git a/services/backup_sa/include/module_ipc/svc_session_manager.h b/services/backup_sa/include/module_ipc/svc_session_manager.h index f8bfd92c9bac75f6cdff512dc9272938f94b634d..b2f6a3fa5eb60bc1394e5b745340c81a221e3529 100644 --- a/services/backup_sa/include/module_ipc/svc_session_manager.h +++ b/services/backup_sa/include/module_ipc/svc_session_manager.h @@ -68,6 +68,7 @@ struct BackupExtInfo { std::string backupParameters; int32_t backupPriority; std::string extInfo; + int32_t userId {BConstants::DEFAULT_USER_ID}; int32_t appendNum {1}; bool isClearData {true}; bool isInPublishFile {false}; @@ -302,12 +303,29 @@ public: */ std::string GetBackupExtInfo(const std::string &bundleName); + /** + * @brief 暂存应用用户id + * + * @param bundleName 应用名称 + * @param userId 用户id + */ + void SetBundleUserId(const std::string &bundleName, const int32_t userId); + + /** + * @brief 获取应用用户id + * + * @param bundleName 应用名称 + * @return int32_t userId + */ + int32_t GetBundleUserId(const std::string &bundleName); + /** * @brief 追加应用 * * @param bundleNames 应用名称 + * @param failedBundles 返回失败应用名称 */ - void AppendBundles(const std::vector &bundleNames); + void AppendBundles(const std::vector &bundleNames, std::vector &failedBundles); /** * @brief 添加指定应用 diff --git a/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp b/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp index e6817ebd96b9849a9298bf627dde230f210bd0b1..9a375e27ff3421d50e90284969e435a80b946221 100644 --- a/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp +++ b/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp @@ -15,6 +15,7 @@ #include +#include "b_error/b_error.h" #include "b_radar/b_radar.h" #include "b_sa/b_sa_utils.h" #include "b_jsonutil/b_jsonutil.h" @@ -32,13 +33,10 @@ namespace { const auto APP_FOUNDATION_SERVICE = u"appgalleryservice.openapi.privacymanager.AppFoundationService"; } mutex AppGalleryDisposeProxy::instanceLock_; -mutex AppGalleryDisposeProxy::conditionMutex_; +mutex AppGalleryDisposeProxy::appGalleryConnectionLock_; string AppGalleryDisposeProxy::abilityName = "AppFoundationService"; -sptr AppGalleryDisposeProxy::appRemoteObj_; -condition_variable AppGalleryDisposeProxy::conditionVal_; -mutex AppGalleryDisposeProxy::appRemoteObjLock_; -mutex AppGalleryDisposeProxy::connectMutex; +std::map> AppGalleryDisposeProxy::appGalleryConnectionMap_; sptr AppGalleryDisposeProxy::appGalleryDisposeProxyInstance_; AppGalleryDisposeProxy::AppGalleryDisposeProxy() @@ -63,81 +61,81 @@ sptr AppGalleryDisposeProxy::GetInstance() return appGalleryDisposeProxyInstance_; } -DisposeErr AppGalleryDisposeProxy::StartBackup(const std::string &bundleName) +DisposeErr AppGalleryDisposeProxy::StartBackup(const std::string &bundleName, const int32_t userId) { - HILOGI("StartBackup, app %{public}s", bundleName.c_str()); - DisposeErr res = DoDispose(bundleName, DisposeOperation::START_BACKUP); + HILOGI("StartBackup, app %{public}s, userId %{public}d", bundleName.c_str(), userId); + DisposeErr res = DoDispose(bundleName, DisposeOperation::START_BACKUP, userId); if (res != DisposeErr::REQUEST_FAIL) { AppRadar::Info info(bundleName, "", ""); - AppRadar::GetInstance().RecordBackupFuncRes(info, "StartBackup", AppRadar::GetInstance().GetUserId(), + AppRadar::GetInstance().RecordBackupFuncRes(info, "StartBackup", userId, BizStageBackup::BIZ_STAGE_START_DISPOSE, static_cast(res)); } return res; } -DisposeErr AppGalleryDisposeProxy::EndBackup(const std::string &bundleName) +DisposeErr AppGalleryDisposeProxy::EndBackup(const std::string &bundleName, const int32_t userId) { - HILOGI("EndBackup, app %{public}s", bundleName.c_str()); - DisposeErr res = DoDispose(bundleName, DisposeOperation::END_BACKUP); + HILOGI("EndBackup, app %{public}s, userId %{public}d", bundleName.c_str(), userId); + DisposeErr res = DoDispose(bundleName, DisposeOperation::END_BACKUP, userId); if (res != DisposeErr::REQUEST_FAIL) { AppRadar::Info info(bundleName, "", ""); - AppRadar::GetInstance().RecordBackupFuncRes(info, "EndBackup", AppRadar::GetInstance().GetUserId(), + AppRadar::GetInstance().RecordBackupFuncRes(info, "EndBackup", userId, BizStageBackup::BIZ_STAGE_END_DISPOSE, static_cast(res)); } return res; } -DisposeErr AppGalleryDisposeProxy::StartRestore(const std::string &bundleName) +DisposeErr AppGalleryDisposeProxy::StartRestore(const std::string &bundleName, const int32_t userId) { if (SAUtils::IsSABundleName(bundleName)) { HILOGI("SA does not need to StartRestore"); return DisposeErr::OK; } - HILOGI("StartRestore, app %{public}s", bundleName.c_str()); - DisposeErr res = DoDispose(bundleName, DisposeOperation::START_RESTORE); + HILOGI("StartRestore, app %{public}s, userId %{public}d", bundleName.c_str(), userId); + DisposeErr res = DoDispose(bundleName, DisposeOperation::START_RESTORE, userId); if (res != DisposeErr::REQUEST_FAIL) { AppRadar::Info info(bundleName, "", ""); - AppRadar::GetInstance().RecordRestoreFuncRes(info, "StartRestore", AppRadar::GetInstance().GetUserId(), + AppRadar::GetInstance().RecordRestoreFuncRes(info, "StartRestore", userId, BizStageRestore::BIZ_STAGE_START_DISPOSE, static_cast(res)); } return res; } -DisposeErr AppGalleryDisposeProxy::EndRestore(const std::string &bundleName) +DisposeErr AppGalleryDisposeProxy::EndRestore(const std::string &bundleName, const int32_t userId) { if (SAUtils::IsSABundleName(bundleName)) { HILOGI("SA does not need to EndRestore"); return DisposeErr::OK; } - HILOGI("EndRestore, app %{public}s", bundleName.c_str()); - DisposeErr res = DoDispose(bundleName, DisposeOperation::END_RESTORE); + HILOGI("EndRestore, app %{public}s, userId %{public}d", bundleName.c_str(), userId); + DisposeErr res = DoDispose(bundleName, DisposeOperation::END_RESTORE, userId); if (res != DisposeErr::REQUEST_FAIL) { AppRadar::Info info(bundleName, "", ""); - AppRadar::GetInstance().RecordRestoreFuncRes(info, "EndRestore", AppRadar::GetInstance().GetUserId(), + AppRadar::GetInstance().RecordRestoreFuncRes(info, "EndRestore", userId, BizStageRestore::BIZ_STAGE_END_DISPOSE, static_cast(res)); } return res; } void RecordDoDisposeRes(const std::string &bundleName, - AppGalleryDisposeProxy::DisposeOperation disposeOperation, int32_t err) + AppGalleryDisposeProxy::DisposeOperation disposeOperation, int32_t userId, int32_t err) { AppRadar::Info info (bundleName, "", "REQUEST FAIL"); switch (disposeOperation) { case AppGalleryDisposeProxy::DisposeOperation::START_BACKUP: - AppRadar::GetInstance().RecordBackupFuncRes(info, "StartBackup", AppRadar::GetInstance().GetUserId(), + AppRadar::GetInstance().RecordBackupFuncRes(info, "StartBackup", userId, BizStageBackup::BIZ_STAGE_START_DISPOSE, err); break; case AppGalleryDisposeProxy::DisposeOperation::END_BACKUP: - AppRadar::GetInstance().RecordBackupFuncRes(info, "EndBackup", AppRadar::GetInstance().GetUserId(), + AppRadar::GetInstance().RecordBackupFuncRes(info, "EndBackup", userId, BizStageBackup::BIZ_STAGE_END_DISPOSE, err); break; case AppGalleryDisposeProxy::DisposeOperation::START_RESTORE: - AppRadar::GetInstance().RecordRestoreFuncRes(info, "StartRestore", AppRadar::GetInstance().GetUserId(), + AppRadar::GetInstance().RecordRestoreFuncRes(info, "StartRestore", userId, BizStageRestore::BIZ_STAGE_START_DISPOSE, err); break; case AppGalleryDisposeProxy::DisposeOperation::END_RESTORE: - AppRadar::GetInstance().RecordRestoreFuncRes(info, "EndRestore", AppRadar::GetInstance().GetUserId(), + AppRadar::GetInstance().RecordRestoreFuncRes(info, "EndRestore", userId, BizStageRestore::BIZ_STAGE_END_DISPOSE, err); break; default: @@ -145,12 +143,44 @@ void RecordDoDisposeRes(const std::string &bundleName, } } -DisposeErr AppGalleryDisposeProxy::DoDispose(const std::string &bundleName, DisposeOperation disposeOperation) +sptr AppGalleryDisposeProxy::GetAppGalleryConnection(const int32_t userId) +{ + auto it = appGalleryConnectionMap_.find(userId); + if (it == appGalleryConnectionMap_.end()) { + HILOGI("appGalleryConnectionMap not contain %{public}d, will register", userId); + return nullptr; + } + HILOGI("appGalleryConnectionMap contain %{public}d", userId); + return appGalleryConnectionMap_[userId]; +} + +sptr AppGalleryDisposeProxy::ConnectAppGallery(const int32_t userId) +{ + std::unique_lock lock(appGalleryConnectionLock_); + sptr connection = GetAppGalleryConnection(userId); + if (connection == nullptr) { + connection = sptr(new AppGalleryConnection(userId)); + appGalleryConnectionMap_[userId] = connection; + } + if (connection->GetRemoteObj() == nullptr) { + HILOGI("AppGalleryConnection try to connect, userId = %{public}d", userId); + if (!connection->ConnectExtAbility(abilityName) || connection->GetRemoteObj() == nullptr) { + HILOGE("AppGalleryConnection failed to connect, userId = %{public}d", userId); + return nullptr; + } + } + return connection; +} + +DisposeErr AppGalleryDisposeProxy::DoDispose(const std::string &bundleName, DisposeOperation disposeOperation, + const int32_t userId) { try { - HILOGI("DoDispose, app %{public}s, operation %{public}d", bundleName.c_str(), disposeOperation); - if (!ConnectExtAbility() || appRemoteObj_ == nullptr) { - HILOGE("Can not connect to %{public}s", bundleName.c_str()); + HILOGI("DoDispose, app %{public}s, operation %{public}d, userId %{public}d", bundleName.c_str(), + disposeOperation, userId); + sptr connection = ConnectAppGallery(userId); + if (connection == nullptr) { + HILOGE("Get AppGalleryConnection failed, userId = %{public}d", userId); return DisposeErr::CONN_FAIL; } @@ -172,16 +202,23 @@ DisposeErr AppGalleryDisposeProxy::DoDispose(const std::string &bundleName, Disp MessageParcel reply; MessageOption option; - int32_t ret = appRemoteObj_->SendRequest(static_cast(disposeOperation), data, reply, option); + auto remoteObj = connection->GetRemoteObj(); + if (remoteObj == nullptr) { + HILOGE("remoteObj is nullptr, bundleName=%{public}s, appindex=%{public}d, userId=%{public}d", + bundleDetailInfo.bundleName.c_str(), bundleDetailInfo.bundleIndex, userId); + return DisposeErr::CONN_FAIL; + } + + int32_t ret = remoteObj->SendRequest(static_cast(disposeOperation), data, reply, option); if (ret != ERR_NONE) { - HILOGE("SendRequest error, code=%{public}d, bundleName=%{public}s , appindex =%{public}d", - ret, bundleDetailInfo.bundleName.c_str(), bundleDetailInfo.bundleIndex); - RecordDoDisposeRes(bundleName, disposeOperation, ret); + HILOGE("SendRequest error, code=%{public}d, bundleName=%{public}s , appindex=%{public}d, userId=%{public}d", + ret, bundleDetailInfo.bundleName.c_str(), bundleDetailInfo.bundleIndex, userId); + RecordDoDisposeRes(bundleName, disposeOperation, userId, ret); return DisposeErr::REQUEST_FAIL; } - HILOGI("SendRequest success, dispose=%{public}d, bundleName=%{public}s, appindex =%{public}d", - disposeOperation, bundleDetailInfo.bundleName.c_str(), bundleDetailInfo.bundleIndex); + HILOGI("SendRequest success, dispose=%{public}d, bundleName=%{public}s, appindex=%{public}d, userId=%{public}d", + disposeOperation, bundleDetailInfo.bundleName.c_str(), bundleDetailInfo.bundleIndex, userId); return DisposeErr::OK; } catch (const BError &e) { HILOGE("Catch exception, errCode = %{public}d", e.GetCode()); diff --git a/services/backup_sa/src/module_app_gallery/app_gallery_service_connection.cpp b/services/backup_sa/src/module_app_gallery/app_gallery_service_connection.cpp new file mode 100644 index 0000000000000000000000000000000000000000..67989821c59ee32b4b8ec3817c26cb7e224c1de7 --- /dev/null +++ b/services/backup_sa/src/module_app_gallery/app_gallery_service_connection.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "module_app_gallery/app_gallery_service_connection.h" + +#include +#include + +#include "module_external/bms_adapter.h" +#include "filemgmt_libhilog.h" + +namespace OHOS::FileManagement::Backup { +const int32_t CONNECT_TIME = 3; + +void AppGalleryConnection::OnAbilityConnectDone(const AppExecFwk::ElementName &element, + const sptr &remoteObject, int resultCode) +{ + std::string uri = element.GetURI(); + HILOGI("OnAbilityConnectDone, uri = %{public}s", uri.c_str()); + appRemoteObj_ = remoteObject; + conditionVal_.notify_one(); +} + +void AppGalleryConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) +{ + std::string uri = element.GetURI(); + HILOGI("OnAbilityDisconnectDone, uri = %{public}s", uri.c_str()); + appRemoteObj_ = nullptr; +} + +bool AppGalleryConnection::ConnectExtAbility(std::string abilityName) +{ + HILOGI("ConnectExtAbility called, userId = %{public}d", userId); + std::lock_guard autoLock(appRemoteObjLock_); + if (appRemoteObj_ != nullptr) { + return true; + } + + auto appGalleryBundleName = BundleMgrAdapter::GetAppGalleryBundleName(); + if (appGalleryBundleName.empty()) { + HILOGE("ConnectExtAbility GetAppGalleryBundleName failed, userId = %{public}d", userId); + return false; + } + + Want want; + want.SetElementName(appGalleryBundleName.c_str(), abilityName); + auto ret = AbilityManagerClient::GetInstance()->ConnectAbility(want, this, userId); + + std::unique_lock uniqueLock(connectMutex); + conditionVal_.wait_for(uniqueLock, std::chrono::seconds(CONNECT_TIME)); + if (ret != IAppGalleryService::ERR_OK || appRemoteObj_ == nullptr) { + HILOGE("ConnectExtAbility failed, ret=%{public}d, userId = %{public}d", ret, userId); + appRemoteObj_ = nullptr; + return false; + } + HILOGI("ConnectExtAbility success, ret=%{public}d, userId = %{public}d", ret, userId); + return true; +} + +sptr AppGalleryConnection::GetRemoteObj() +{ + return appRemoteObj_; +} +} // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index f76f5f29746519194aee4a80061e4586df44c64d..66749bce8448fb2f4117ff70ca3143e47b6dd4b5 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -651,7 +651,7 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, const vectorDecreaseSessionCnt(__PRETTY_FUNCTION__); return BError(BError::Codes::OK); } - session_->AppendBundles(restoreBundleNames); + AppendBundles(restoreBundleNames); SetCurrentSessProperties(restoreInfos, restoreBundleNames, bundleNameDetailMap, isClearDataFlags, restoreType, oldBackupVersion); OnStartSched(); @@ -702,6 +702,7 @@ void Service::SetCurrentSessProperties(std::vector if (BundleMgrAdapter::IsUser0BundleName(bundleNameIndexInfo, session_->GetSessionUserId())) { SendUserIdToApp(bundleNameIndexInfo, session_->GetSessionUserId()); } + session_->SetBundleUserId(bundleNameIndexInfo, session_->GetSessionUserId()); session_->SetBackupExtName(bundleNameIndexInfo, restoreInfo.extensionName); session_->SetIsReadyLaunch(bundleNameIndexInfo); } @@ -737,7 +738,7 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, HILOGW("RestoreBundleNames is empty."); return BError(BError::Codes::OK); } - session_->AppendBundles(restoreBundleNames); + AppendBundles(restoreBundleNames); SetCurrentSessProperties(restoreInfos, restoreBundleNames, restoreType, oldBackupVersion); OnStartSched(); session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); @@ -785,6 +786,7 @@ void Service::SetCurrentSessProperties(std::vector session_->SetBundleVersionCode(bundleNameIndexInfo, restoreInfo.versionCode); session_->SetBundleVersionName(bundleNameIndexInfo, restoreInfo.versionName); session_->SetBundleDataSize(bundleNameIndexInfo, restoreInfo.spaceOccupied); + session_->SetBundleUserId(bundleNameIndexInfo, session_->GetSessionUserId()); auto iter = isClearDataFlags.find(bundleNameIndexInfo); if (iter != isClearDataFlags.end()) { session_->SetClearDataFlag(bundleNameIndexInfo, iter->second); @@ -794,8 +796,7 @@ void Service::SetCurrentSessProperties(std::vector bool broadCastRet = BJsonUtil::FindBundleInfoByName(bundleNameDetailMap, bundleNameIndexInfo, BROADCAST_TYPE, broadCastInfo); if (broadCastRet) { - bool notifyRet = - DelayedSingleton::GetInstance()->NotifyBundleDetail(broadCastInfo); + bool notifyRet = DelayedSingleton::GetInstance()->NotifyBundleDetail(broadCastInfo); HILOGI("Publish event end, notify result is:%{public}d", notifyRet); } bool uniCastRet = BJsonUtil::FindBundleInfoByName(bundleNameDetailMap, bundleNameIndexInfo, UNICAST_TYPE, @@ -829,7 +830,7 @@ ErrCode Service::AppendBundlesBackupSession(const vector &bundleName auto bundleDetails = MakeDetailList(bundleNames); auto backupInfos = BundleMgrAdapter::GetBundleInfosForAppend(bundleDetails, session_->GetSessionUserId()); std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, false, bundleNames); - session_->AppendBundles(supportBackupNames); + AppendBundles(supportBackupNames); SetCurrentBackupSessProperties(supportBackupNames, session_->GetSessionUserId(), backupInfos, false); OnStartSched(); session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); @@ -872,7 +873,7 @@ ErrCode Service::AppendBundlesDetailsBackupSession(const vector &bun auto bundleDetails = MakeDetailList(bundleNames); auto backupInfos = BundleMgrAdapter::GetBundleInfosForAppend(bundleDetails, session_->GetSessionUserId()); std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, false, bundleNames); - session_->AppendBundles(supportBackupNames); + AppendBundles(supportBackupNames); HandleCurGroupBackupInfos(backupInfos, bundleNameDetailMap, isClearDataFlags); OnStartSched(); session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); @@ -905,6 +906,7 @@ void Service::HandleCurGroupBackupInfos(std::vector bundleNameIndexInfo.c_str(), GetAnonyString(uniCastInfo.detail).c_str(), uniCastInfo.detail.size()); session_->SetBackupExtInfo(bundleNameIndexInfo, uniCastInfo.detail); } + session_->SetBundleUserId(bundleNameIndexInfo, session_->GetSessionUserId()); session_->SetBackupExtName(bundleNameIndexInfo, info.extensionName); session_->SetIsReadyLaunch(bundleNameIndexInfo); } @@ -950,6 +952,7 @@ ErrCode Service::ServiceResultReport(const std::string restoreRetInfo, ErrCode Service::SAResultReport(const std::string bundleName, const std::string restoreRetInfo, const ErrCode errCode, const BackupRestoreScenario sennario) { + SADone(errCode, bundleName); if (sennario == BackupRestoreScenario::FULL_RESTORE) { session_->GetServiceReverseProxy()->RestoreOnResultReport(restoreRetInfo, bundleName); } else if (sennario == BackupRestoreScenario::INCREMENTAL_RESTORE) { @@ -960,13 +963,14 @@ ErrCode Service::SAResultReport(const std::string bundleName, const std::string } else if (sennario == BackupRestoreScenario::INCREMENTAL_BACKUP) { session_->GetServiceReverseProxy()->IncrementalBackupOnResultReport(restoreRetInfo, bundleName); } + OnAllBundlesFinished(BError(BError::Codes::OK)); if (sennario == BackupRestoreScenario::FULL_RESTORE || sennario == BackupRestoreScenario::INCREMENTAL_RESTORE) { BundleEndRadarReport(bundleName, errCode, IServiceReverse::Scenario::RESTORE); } else if (sennario == BackupRestoreScenario::FULL_BACKUP || sennario == BackupRestoreScenario::INCREMENTAL_BACKUP) { BundleEndRadarReport(bundleName, errCode, IServiceReverse::Scenario::BACKUP); } - return SADone(errCode, bundleName); + return BError(BError::Codes::OK); } void Service::HandleCurBundleEndWork(std::string bundleName, const BackupRestoreScenario sennario) @@ -1091,6 +1095,7 @@ void Service::ExtStart(const string &bundleName) StartCurBundleBackupOrRestore(bundleName); } catch (...) { HILOGE("Unexpected exception, bundleName: %{public}s", bundleName.c_str()); + SendEndAppGalleryNotify(bundleName); ClearSessionAndSchedInfo(bundleName); NoticeClientFinish(bundleName, BError(BError::Codes::SA_INVAL_ARG)); } @@ -1103,25 +1108,13 @@ void Service::StartCurBundleBackupOrRestore(const std::string &bundleName) auto backUpConnection = session_->GetExtConnection(bundleName); if (backUpConnection == nullptr) { HILOGE("Error, backUpConnection is empty, bundle:%{public}s", bundleName.c_str()); - if (scenario == IServiceReverse::Scenario::BACKUP) { - session_->GetServiceReverseProxy()->BackupOnBundleStarted(BError(BError::Codes::SA_INVAL_ARG), - bundleName); - } else if (scenario == IServiceReverse::Scenario::RESTORE) { - session_->GetServiceReverseProxy()->RestoreOnBundleStarted(BError(BError::Codes::SA_INVAL_ARG), - bundleName); - } + ReportOnBundleStarted(scenario, bundleName); return; } auto proxy = backUpConnection->GetBackupExtProxy(); if (proxy == nullptr) { HILOGE("Error, Extension backup Proxy is empty, bundle:%{public}s", bundleName.c_str()); - if (scenario == IServiceReverse::Scenario::BACKUP) { - session_->GetServiceReverseProxy()->BackupOnBundleStarted(BError(BError::Codes::SA_INVAL_ARG), - bundleName); - } else if (scenario == IServiceReverse::Scenario::RESTORE) { - session_->GetServiceReverseProxy()->RestoreOnBundleStarted(BError(BError::Codes::SA_INVAL_ARG), - bundleName); - } + ReportOnBundleStarted(scenario, bundleName); return; } if (scenario == IServiceReverse::Scenario::BACKUP) { @@ -1129,6 +1122,7 @@ void Service::StartCurBundleBackupOrRestore(const std::string &bundleName) session_->GetServiceReverseProxy()->BackupOnBundleStarted(ret, bundleName); BundleBeginRadarReport(bundleName, ret, scenario); if (ret) { + SendEndAppGalleryNotify(bundleName); ClearSessionAndSchedInfo(bundleName); NoticeClientFinish(bundleName, BError(BError::Codes::SA_INVAL_ARG)); } @@ -1175,7 +1169,8 @@ void Service::ReportOnExtConnectFailed(const IServiceReverse::Scenario scenario, session_->ValidRestoreDataType(RestoreTypeEnum::RESTORE_DATA_WAIT_SEND)) { session_->GetServiceReverseProxy()->IncrementalRestoreOnBundleStarted(ret, bundleName); BundleBeginRadarReport(bundleName, ret, scenario); - DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, + session_->GetBundleUserId(bundleName)); HILOGI("ExtConnectFailed EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); } else if (scenario == IServiceReverse::Scenario::BACKUP) { @@ -1184,7 +1179,8 @@ void Service::ReportOnExtConnectFailed(const IServiceReverse::Scenario scenario, } else if (scenario == IServiceReverse::Scenario::RESTORE) { session_->GetServiceReverseProxy()->RestoreOnBundleStarted(ret, bundleName); BundleBeginRadarReport(bundleName, ret, scenario); - DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, + session_->GetBundleUserId(bundleName)); HILOGI("ExtConnectFailed EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); } @@ -1201,6 +1197,7 @@ void Service::ExtConnectFailed(const string &bundleName, ErrCode ret) HILOGE("begin %{public}s", bundleName.data()); scenario = session_->GetScenario(); ReportOnExtConnectFailed(scenario, bundleName, ret); + SendEndAppGalleryNotify(bundleName); ClearSessionAndSchedInfo(bundleName); NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_DIED)); return; @@ -1232,6 +1229,7 @@ void Service::ExtConnectDone(string bundleName) BConstants::ServiceSchedAction curSchedAction = session_->GetServiceSchedAction(bundleName); if (curSchedAction == BConstants::ServiceSchedAction::UNKNOWN) { HILOGE("Can not find bundle from this session, bundleName:%{public}s", bundleName.c_str()); + SendEndAppGalleryNotify(bundleName); ClearSessionAndSchedInfo(bundleName); NoticeClientFinish(bundleName, BError(BError::Codes::SA_REFUSED_ACT)); return; @@ -1255,6 +1253,7 @@ void Service::ExtConnectDone(string bundleName) sched_->Sched(bundleName); } catch (...) { HILOGE("Unexpected exception, bundleName: %{public}s", bundleName.c_str()); + SendEndAppGalleryNotify(bundleName); ClearSessionAndSchedInfo(bundleName); NoticeClientFinish(bundleName, BError(BError::Codes::SDK_INVAL_ARG)); return; @@ -1308,7 +1307,7 @@ void Service::HandleRestoreDepsBundle(const string &bundleName) HILOGI("Start restore session, bundle: %{public}s", bundle.first.c_str()); restoreBundleNames.emplace_back(bundle.first); } - session_->AppendBundles(restoreBundleNames); + AppendBundles(restoreBundleNames); for (const auto &bundle : restoreBundleMap) { for (const auto &bundleInfo : SvcRestoreDepsManager::GetInstance().GetAllBundles()) { if (bundle.first != bundleInfo.name) { @@ -1350,14 +1349,16 @@ void Service::SendStartAppGalleryNotify(const BundleName &bundleName) if (scenario != IServiceReverse::Scenario::RESTORE) { return; } - if (!disposal_->IfBundleNameInDisposalConfigFile(bundleName)) { + int32_t userId = session_->GetBundleUserId(bundleName); + std::string bundleNameWithUserId = BundleNameWithUserId(bundleName, userId); + if (!disposal_->IfBundleNameInDisposalConfigFile(bundleNameWithUserId)) { HILOGE("WriteDisposalConfigFile Failed"); return; } - HILOGI("AppendIntoDisposalConfigFile OK, bundleName=%{public}s", bundleName.c_str()); - DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->StartRestore(bundleName); - HILOGI("StartRestore, code=%{public}d, bundleName=%{public}s", disposeErr, - bundleName.c_str()); + HILOGI("AppendIntoDisposalConfigFile OK, bundleName=%{public}s", bundleNameWithUserId.c_str()); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->StartRestore(bundleName, userId); + HILOGI("StartRestore, code=%{public}d, bundleName=%{public}s, userId=%{public}d", disposeErr, bundleName.c_str(), + userId); } void Service::SendEndAppGalleryNotify(const BundleName &bundleName) @@ -1371,27 +1372,38 @@ void Service::SendEndAppGalleryNotify(const BundleName &bundleName) if (scenario != IServiceReverse::Scenario::RESTORE) { return; } - DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName); - HILOGI("EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, - bundleName.c_str()); + int32_t userId = session_->GetBundleUserId(bundleName); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, userId); + HILOGI("EndRestore, code=%{public}d, bundleName=%{public}s, userId=%{public}d", disposeErr, bundleName.c_str(), + userId); if (disposeErr != DisposeErr::OK) { HILOGE("Error code=%{public}d, disposal will be clear in the end", disposeErr); return; } - if (!disposal_->DeleteFromDisposalConfigFile(bundleName)) { - HILOGE("DeleteFromDisposalConfigFile Failed, bundleName=%{public}s", bundleName.c_str()); + std::string bundleNameWithUserId = BundleNameWithUserId(bundleName, userId); + if (!disposal_->DeleteFromDisposalConfigFile(bundleNameWithUserId)) { + HILOGE("DeleteFromDisposalConfigFile Failed, bundleName=%{public}s", bundleNameWithUserId.c_str()); return; } - HILOGI("DeleteFromDisposalConfigFile OK, bundleName=%{public}s", bundleName.c_str()); + HILOGI("DeleteFromDisposalConfigFile OK, bundleName=%{public}s", bundleNameWithUserId.c_str()); } void Service::TryToClearDispose(const BundleName &bundleName) { + auto [bundle, userId] = SplitBundleName(bundleName); + if (bundle.empty() || userId == -1) { + HILOGE("BundleName from disposal config is invalid, bundleName = %{public}s", bundleName.c_str()); + if (!disposal_->DeleteFromDisposalConfigFile(bundleName)) { + HILOGE("DeleteFromDisposalConfigFile Failed, bundleName=%{public}s", bundleName.c_str()); + } + return; + } int32_t maxAtt = MAX_TRY_CLEAR_DISPOSE_NUM; int32_t att = 0; while (att < maxAtt) { - DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName); - HILOGI("EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundle, userId); + HILOGI("EndRestore, code=%{public}d, bundleName=%{public}s, userId=%{public}d", disposeErr, bundle.c_str(), + userId); if (disposeErr == DisposeErr::OK) { break; } @@ -1720,7 +1732,7 @@ ErrCode Service::AppendBundlesClearSession(const std::vector &bundle std::string bundleNameIndexStr = BJsonUtil::BuildBundleNameIndexInfo(info.name, info.appIndex); supportBundleNames.emplace_back(bundleNameIndexStr); } - session_->AppendBundles(supportBundleNames); + AppendBundles(supportBundleNames); for (const auto &info : backupInfos) { std::string bundleNameIndexInfo = BJsonUtil::BuildBundleNameIndexInfo(info.name, info.appIndex); session_->SetBackupExtName(bundleNameIndexInfo, info.extensionName); @@ -1910,7 +1922,6 @@ ErrCode Service::SADone(ErrCode errCode, std::string bundleName) saConnection->DisconnectBackupSAExt(); ClearSessionAndSchedInfo(bundleName); } - OnAllBundlesFinished(BError(BError::Codes::OK)); return BError(BError::Codes::OK); } catch (const BError &e) { ReleaseOnException(); @@ -1945,7 +1956,6 @@ void Service::NotifyCallerCurAppDone(ErrCode errCode, const std::string &callerN ); } else if (scenario == IServiceReverse::Scenario::RESTORE) { HILOGI("will notify clone data, scenario is Restore"); - SendEndAppGalleryNotify(callerName); session_->GetServiceReverseProxy()->RestoreOnBundleFinished(errCode, callerName); } BundleEndRadarReport(callerName, errCode, scenario); @@ -1993,6 +2003,7 @@ std::function Service::TimeOutCallback(wptr ptr, std::string bu DoTimeout(thisPtr, bundleName); } catch (...) { HILOGE("Unexpected exception, bundleName: %{public}s", bundleName.c_str()); + SendEndAppGalleryNotify(bundleName); thisPtr->ClearSessionAndSchedInfo(bundleName); thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_TIMEOUT)); } @@ -2052,10 +2063,12 @@ void Service::DoTimeout(wptr ptr, std::string bundleName) } sessionPtr->StopFwkTimer(bundleName); sessionPtr->StopExtTimer(bundleName); + SendEndAppGalleryNotify(bundleName); thisPtr->ClearSessionAndSchedInfo(bundleName); thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_TIMEOUT)); } catch (...) { HILOGE("Unexpected exception, bundleName: %{public}s", bundleName.c_str()); + SendEndAppGalleryNotify(bundleName); thisPtr->ClearSessionAndSchedInfo(bundleName); thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_TIMEOUT)); } diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp index a11949ab701d718a4ab2b548870f9ad87982084f..af75eb0c93a8719af134a179263d6346f082ddaa 100644 --- a/services/backup_sa/src/module_ipc/service_incremental.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental.cpp @@ -196,8 +196,8 @@ void Service::StartGetFdTask(std::string bundleName, wptr ptr) } // distinguish whether it is 0 user if (BundleMgrAdapter::IsUser0BundleName(bundleName, session_->GetSessionUserId())) { - auto ret = proxy->User0OnBackup(); - if (ret) { + if (proxy->User0OnBackup()) { + SendEndAppGalleryNotify(bundleName); thisPtr->ClearSessionAndSchedInfo(bundleName); thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_DIED)); } @@ -222,6 +222,7 @@ void Service::StartGetFdTask(std::string bundleName, wptr ptr) UniqueFd lastManifestFd(session->GetIncrementalManifestFd(bundleName)); auto ret = proxy->HandleIncrementalBackup(move(fdLocal), move(lastManifestFd)); if (ret) { + SendEndAppGalleryNotify(bundleName); thisPtr->ClearSessionAndSchedInfo(bundleName); thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_DIED)); } @@ -391,7 +392,7 @@ ErrCode Service::AppendBundlesIncrementalBackupSession(const std::vectorGetSessionUserId()); std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, true, bundleNames); - session_->AppendBundles(supportBackupNames); + AppendBundles(supportBackupNames); SetBundleIncDataInfo(bundlesToBackup, supportBackupNames); SetCurrentBackupSessProperties(supportBackupNames, session_->GetSessionUserId(), backupInfos, true); OnStartSched(); @@ -437,7 +438,7 @@ ErrCode Service::AppendBundlesIncrementalBackupSession(const std::vectorGetSessionUserId()); std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, true, bundleNames); - session_->AppendBundles(supportBackupNames); + AppendBundles(supportBackupNames); SetBundleIncDataInfo(bundlesToBackup, supportBackupNames); HandleCurGroupIncBackupInfos(backupInfos, bundleNameDetailMap, isClearDataFlags); OnStartSched(); @@ -707,12 +708,14 @@ bool Service::IncrementalBackup(const string &bundleName) auto backUpConnection = session_->GetExtConnection(bundleName); if (backUpConnection == nullptr) { HILOGE("backUpConnection is empty, bundle:%{public}s", bundleName.c_str()); + SendEndAppGalleryNotify(bundleName); NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_DIED)); return true; } auto proxy = backUpConnection->GetBackupExtProxy(); if (!proxy) { HILOGE("Increment backup error, extension proxy is empty, bundleName:%{public}s", bundleName.c_str()); + SendEndAppGalleryNotify(bundleName); NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_DIED)); return true; } @@ -721,6 +724,7 @@ bool Service::IncrementalBackup(const string &bundleName) session_->GetServiceReverseProxy()->IncrementalBackupOnBundleStarted(ret, bundleName); BundleBeginRadarReport(bundleName, ret, IServiceReverse::Scenario::BACKUP); if (ret) { + SendEndAppGalleryNotify(bundleName); ClearSessionAndSchedInfo(bundleName); NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_DIED)); } @@ -770,7 +774,6 @@ void Service::NotifyCallerCurAppIncrementDone(ErrCode errCode, const std::string ); } else if (scenario == IServiceReverse::Scenario::RESTORE) { HILOGI("will notify clone data, scenario is Restore"); - SendEndAppGalleryNotify(callerName); session_->GetServiceReverseProxy()->IncrementalRestoreOnBundleFinished(errCode, callerName); BundleEndRadarReport(callerName, errCode, scenario); } @@ -820,6 +823,7 @@ void Service::SetCurrentBackupSessProperties(const vector &bundleNames, } else { session_->SetBundleDataSize(bundleName, bundleInfo.spaceOccupied); } + session_->SetBundleUserId(bundleName, userId); session_->SetBackupExtName(bundleName, bundleInfo.extensionName); session_->SetIsReadyLaunch(bundleName); } diff --git a/services/backup_sa/src/module_ipc/sub_service.cpp b/services/backup_sa/src/module_ipc/sub_service.cpp index 9677dea80b54e46d1473efbb62f6bdfc4ada71ad..667debf8e337883fe692d96c32651e12207a8148 100644 --- a/services/backup_sa/src/module_ipc/sub_service.cpp +++ b/services/backup_sa/src/module_ipc/sub_service.cpp @@ -71,6 +71,42 @@ const int32_t WAIT_SCANNING_INFO_SEND_TIME = 5; const int ERR_SIZE = -1; } // namespace +void Service::AppendBundles(const std::vector &bundleNames) +{ + std::vector failedBundles; + session_->AppendBundles(bundleNames, failedBundles); + if (!failedBundles.empty()) { + HILOGE("Handle exception on failed bundles, size = %{public}zu", failedBundles.size()); + HandleExceptionOnAppendBundles(session_, failedBundles, {}); + } +} + +void Service::ReportOnBundleStarted(IServiceReverse::Scenario scenario, const std::string &bundleName) +{ + if (scenario == IServiceReverse::Scenario::BACKUP) { + session_->GetServiceReverseProxy()->BackupOnBundleStarted(BError(BError::Codes::SA_INVAL_ARG), bundleName); + } else if (scenario == IServiceReverse::Scenario::RESTORE) { + session_->GetServiceReverseProxy()->RestoreOnBundleStarted(BError(BError::Codes::SA_INVAL_ARG), bundleName); + } +} + +string Service::BundleNameWithUserId(const string& bundleName, const int32_t userId) +{ + return to_string(userId) + "-" + bundleName; +} + +std::tuple Service::SplitBundleName(const string& bundleNameWithId) +{ + size_t found = bundleNameWithId.find('-'); + if (found == std::string::npos) { + HILOGE("Can not split bundleName = %{public}s", bundleNameWithId.c_str()); + return { "", -1 }; + } + std::string bundleName = bundleNameWithId.substr(found + 1, bundleNameWithId.length()); + int32_t userId = std::atoi(bundleNameWithId.substr(0, found).c_str()); + return { bundleName, userId }; +} + vector Service::MakeDetailList(const vector &bundleNames) { vector bundleDetails {}; @@ -450,6 +486,7 @@ void Service::ExtConnectDied(const string &callName) bool needCleanData = session_->GetClearDataFlag(callName); if (!needCleanData) { HILOGE("Current extension is died, but not need clean data, bundleName:%{public}s", callName.c_str()); + SendEndAppGalleryNotify(callName); ClearSessionAndSchedInfo(callName); NoticeClientFinish(callName, BError(BError::Codes::EXT_ABILITY_DIED)); return; @@ -459,12 +496,14 @@ void Service::ExtConnectDied(const string &callName) if (ret) { /* Clear Session before notice client finish event */ HILOGE("Current bundle launch extension failed, bundleName:%{public}s", callName.c_str()); + SendEndAppGalleryNotify(callName); ClearSessionAndSchedInfo(callName); } /* Notice Client Ext Ability Process Died */ NoticeClientFinish(callName, BError(BError::Codes::EXT_ABILITY_DIED)); } catch (...) { HILOGE("Unexpected exception, bundleName: %{public}s", callName.c_str()); + SendEndAppGalleryNotify(callName); ClearSessionAndSchedInfo(callName); NoticeClientFinish(callName, BError(BError::Codes::EXT_ABILITY_DIED)); } @@ -505,7 +544,6 @@ void Service::NoticeClientFinish(const string &bundleName, ErrCode errCode) HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); HILOGI("begin %{public}s", bundleName.c_str()); try { - SendEndAppGalleryNotify(bundleName); auto scenario = session_->GetScenario(); if (scenario == IServiceReverse::Scenario::BACKUP && session_->GetIsIncrementalBackup()) { session_->GetServiceReverseProxy()->IncrementalBackupOnBundleFinished(errCode, bundleName); @@ -623,6 +661,7 @@ ErrCode Service::HandleCurAppDone(ErrCode errCode, const std::string &bundleName proxy->HandleClear(); session_->StopFwkTimer(bundleName); session_->StopExtTimer(bundleName); + SendEndAppGalleryNotify(bundleName); backUpConnection->DisconnectBackupExtAbility(); ClearSessionAndSchedInfo(bundleName); if (isIncBackup) { diff --git a/services/backup_sa/src/module_ipc/svc_session_manager.cpp b/services/backup_sa/src/module_ipc/svc_session_manager.cpp index 58ae38994223b4552403841b68474297e92faab5..ad0a274dd097d66980e67a976a6ccfe74c3f5ca6 100644 --- a/services/backup_sa/src/module_ipc/svc_session_manager.cpp +++ b/services/backup_sa/src/module_ipc/svc_session_manager.cpp @@ -613,7 +613,39 @@ std::string SvcSessionManager::GetBackupExtInfo(const string &bundleName) return it->second.extInfo; } -void SvcSessionManager::AppendBundles(const vector &bundleNames) +void SvcSessionManager::SetBundleUserId(const string &bundleName, const int32_t userId) +{ + unique_lock lock(lock_); + if (!impl_.clientToken) { + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return; + } + + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return; + } + it->second.userId = userId; +} + +int32_t SvcSessionManager::GetBundleUserId(const string &bundleName) +{ + shared_lock lock(lock_); + if (!impl_.clientToken) { + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return BConstants::DEFAULT_USER_ID; + } + + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return GetSessionUserId(); + } + return it->second.userId; +} + +void SvcSessionManager::AppendBundles(const vector &bundleNames, vector &failedBundles) { unique_lock lock(lock_); if (!impl_.clientToken) { @@ -626,11 +658,15 @@ void SvcSessionManager::AppendBundles(const vector &bundleNames) BackupExtInfo info {}; auto it = impl_.backupExtNameMap.find(bundleName); if (it != impl_.backupExtNameMap.end()) { - HILOGE("BackupExtNameMap already contain %{public}s", bundleName.c_str()); - info.backUpConnection = impl_.backupExtNameMap[bundleName].backUpConnection; - info.saBackupConnection = impl_.backupExtNameMap[bundleName].saBackupConnection; - info.appendNum = impl_.backupExtNameMap[bundleName].appendNum + 1; - impl_.backupExtNameMap[bundleName] = info; + if (impl_.backupExtNameMap[bundleName].userId == GetSessionUserId()) { + HILOGE("BackupExtNameMap already contain %{public}s", bundleName.c_str()); + info.backUpConnection = impl_.backupExtNameMap[bundleName].backUpConnection; + info.saBackupConnection = impl_.backupExtNameMap[bundleName].saBackupConnection; + info.appendNum = impl_.backupExtNameMap[bundleName].appendNum + 1; + impl_.backupExtNameMap[bundleName] = info; + } else { + failedBundles.push_back(bundleName); + } continue; } if (SAUtils::IsSABundleName(bundleName)) { diff --git a/tests/mock/module_ipc/app_gallery_dispose_proxy_mock.cpp b/tests/mock/module_ipc/app_gallery_dispose_proxy_mock.cpp index ca6ce6a2b7e11cfd5a49f7080a3dec499b23e8d4..28ba2d8243f3a719226295bbd6534d6f0ce0ceaf 100644 --- a/tests/mock/module_ipc/app_gallery_dispose_proxy_mock.cpp +++ b/tests/mock/module_ipc/app_gallery_dispose_proxy_mock.cpp @@ -41,27 +41,28 @@ sptr AppGalleryDisposeProxy::GetInstance() return new AppGalleryDisposeProxy; } -DisposeErr AppGalleryDisposeProxy::StartBackup(const std::string &bundleName) +DisposeErr AppGalleryDisposeProxy::StartBackup(const std::string &bundleName, const int32_t userId) { - return DoDispose(bundleName, DisposeOperation::START_BACKUP); + return DoDispose(bundleName, DisposeOperation::START_BACKUP, userId); } -DisposeErr AppGalleryDisposeProxy::EndBackup(const std::string &bundleName) +DisposeErr AppGalleryDisposeProxy::EndBackup(const std::string &bundleName, const int32_t userId) { - return DoDispose(bundleName, DisposeOperation::END_BACKUP); + return DoDispose(bundleName, DisposeOperation::END_BACKUP, userId); } -DisposeErr AppGalleryDisposeProxy::StartRestore(const std::string &bundleName) +DisposeErr AppGalleryDisposeProxy::StartRestore(const std::string &bundleName, const int32_t userId) { - return DoDispose(bundleName, DisposeOperation::START_RESTORE); + return DoDispose(bundleName, DisposeOperation::START_RESTORE, userId); } -DisposeErr AppGalleryDisposeProxy::EndRestore(const std::string &bundleName) +DisposeErr AppGalleryDisposeProxy::EndRestore(const std::string &bundleName, const int32_t userId) { - return DoDispose(bundleName, DisposeOperation::END_RESTORE); + return DoDispose(bundleName, DisposeOperation::END_RESTORE, userId); } -DisposeErr AppGalleryDisposeProxy::DoDispose(const std::string &bundleName, DisposeOperation disposeOperation) +DisposeErr AppGalleryDisposeProxy::DoDispose(const std::string &bundleName, DisposeOperation disposeOperation, + const int32_t userId) { return DisposeErr::OK; } diff --git a/tests/mock/module_ipc/include/app_gallery_dispose_proxy_mock.h b/tests/mock/module_ipc/include/app_gallery_dispose_proxy_mock.h index 190bea71a3a9ec24a884978f6f9401665095c488..4dae0f493551f67c8132c49025c9f4e05c2543f3 100644 --- a/tests/mock/module_ipc/include/app_gallery_dispose_proxy_mock.h +++ b/tests/mock/module_ipc/include/app_gallery_dispose_proxy_mock.h @@ -23,10 +23,10 @@ namespace OHOS::FileManagement::Backup { class BAppGalleryDisposeProxy { public: - virtual DisposeErr StartBackup(const std::string&) = 0; - virtual DisposeErr EndBackup(const std::string&) = 0; - virtual DisposeErr StartRestore(const std::string&) = 0; - virtual DisposeErr EndRestore(const std::string&) = 0; + virtual DisposeErr StartBackup(const std::string&, const int32_t) = 0; + virtual DisposeErr EndBackup(const std::string&, const int32_t) = 0; + virtual DisposeErr StartRestore(const std::string&, const int32_t) = 0; + virtual DisposeErr EndRestore(const std::string&, const int32_t) = 0; public: BAppGalleryDisposeProxy() = default; virtual ~BAppGalleryDisposeProxy() = default; @@ -36,10 +36,10 @@ public: class AppGalleryDisposeProxyMock : public BAppGalleryDisposeProxy { public: - MOCK_METHOD(DisposeErr, StartBackup, (const std::string&)); - MOCK_METHOD(DisposeErr, EndBackup, (const std::string&)); - MOCK_METHOD(DisposeErr, StartRestore, (const std::string&)); - MOCK_METHOD(DisposeErr, EndRestore, (const std::string&)); + MOCK_METHOD(DisposeErr, StartBackup, (const std::string&, const int32_t)); + MOCK_METHOD(DisposeErr, EndBackup, (const std::string&, const int32_t)); + MOCK_METHOD(DisposeErr, StartRestore, (const std::string&, const int32_t)); + MOCK_METHOD(DisposeErr, EndRestore, (const std::string&, const int32_t)); }; } // namespace OHOS::FileManagement::Backup #endif // OHOS_FILEMGMT_BACKUP_APP_GALLERY_DISPOSE_PROXY_MOCK_H \ No newline at end of file diff --git a/tests/mock/module_ipc/src/app_gallery_dispose_proxy_mock.cpp b/tests/mock/module_ipc/src/app_gallery_dispose_proxy_mock.cpp index 945676d7e161dd96521d7ce37de493b60c1a5895..eab1cfcaedea8667c9eca1fbf13b2596a0f9f6ef 100644 --- a/tests/mock/module_ipc/src/app_gallery_dispose_proxy_mock.cpp +++ b/tests/mock/module_ipc/src/app_gallery_dispose_proxy_mock.cpp @@ -42,23 +42,23 @@ sptr AppGalleryDisposeProxy::GetInstance() return appGalleryDisposeProxyInstance_; } -DisposeErr AppGalleryDisposeProxy::StartBackup(const string &bundleName) +DisposeErr AppGalleryDisposeProxy::StartBackup(const string &bundleName, int32_t userId) { - return BAppGalleryDisposeProxy::proxy->StartBackup(bundleName); + return BAppGalleryDisposeProxy::proxy->StartBackup(bundleName, userId); } -DisposeErr AppGalleryDisposeProxy::EndBackup(const string &bundleName) +DisposeErr AppGalleryDisposeProxy::EndBackup(const string &bundleName, int32_t userId) { - return BAppGalleryDisposeProxy::proxy->EndBackup(bundleName); + return BAppGalleryDisposeProxy::proxy->EndBackup(bundleName, userId); } -DisposeErr AppGalleryDisposeProxy::StartRestore(const string &bundleName) +DisposeErr AppGalleryDisposeProxy::StartRestore(const string &bundleName, int32_t userId) { - return BAppGalleryDisposeProxy::proxy->StartRestore(bundleName); + return BAppGalleryDisposeProxy::proxy->StartRestore(bundleName, userId); } -DisposeErr AppGalleryDisposeProxy::EndRestore(const string &bundleName) +DisposeErr AppGalleryDisposeProxy::EndRestore(const string &bundleName, int32_t userId) { - return BAppGalleryDisposeProxy::proxy->EndRestore(bundleName); + return BAppGalleryDisposeProxy::proxy->EndRestore(bundleName, userId); } } // namespace OHOS::FileManagement::Backup diff --git a/tests/mock/module_ipc/src/svc_session_manager_mock.cpp b/tests/mock/module_ipc/src/svc_session_manager_mock.cpp index 255d5ddab81a2e23ceba5ed26943a71aaf8e5700..aee54d0e8c32117ebd8cc86a492cdbc40617f27c 100644 --- a/tests/mock/module_ipc/src/svc_session_manager_mock.cpp +++ b/tests/mock/module_ipc/src/svc_session_manager_mock.cpp @@ -123,7 +123,14 @@ std::weak_ptr SvcSessionManager::GetSAExtConnection(const Bu return BSvcSessionManager::sessionManager->GetSAExtConnection(bundleName); } -void SvcSessionManager::AppendBundles(const vector &) {} +int32_t SvcSessionManager::GetBundleUserId(const string &bundleName) +{ + return 0; +} + +void SvcSessionManager::SetBundleUserId(const string &bundleName, const int32_t userId) {} + +void SvcSessionManager::AppendBundles(const vector &, vector &) {} sptr SvcSessionManager::CreateBackupConnection(BundleName &bundleName) { diff --git a/tests/mock/module_ipc/svc_session_manager_mock.cpp b/tests/mock/module_ipc/svc_session_manager_mock.cpp index 6cc240f17553016b18a938b290b6e778edcdfb3c..d63f0bf4fd21b6fdcd4a7f7f1793404a6c6d319b 100644 --- a/tests/mock/module_ipc/svc_session_manager_mock.cpp +++ b/tests/mock/module_ipc/svc_session_manager_mock.cpp @@ -249,7 +249,7 @@ std::weak_ptr SvcSessionManager::GetSAExtConnection(const Bu return std::weak_ptr(it->second.saBackupConnection); } -void SvcSessionManager::AppendBundles(const vector &bundleNames) +void SvcSessionManager::AppendBundles(const vector &bundleNames, vector &failedBundles) { GTEST_LOG_(INFO) << "AppendBundles"; BackupExtInfo info {}; @@ -369,6 +369,24 @@ std::string SvcSessionManager::GetBundleVersionName(const std::string &bundleNam return it->second.versionName; } +void SvcSessionManager::SetBundleUserId(const std::string &bundleName, const int32_t userId) +{ + auto it = impl_.backupExtNameMap.find(bundleName); + if (it == impl_.backupExtNameMap.end()) { + return; + } + it->second.userId = userId; +} + +int32_t SvcSessionManager::GetBundleUserId(const std::string &bundleName) +{ + auto it = impl_.backupExtNameMap.find(bundleName); + if (it == impl_.backupExtNameMap.end()) { + return 0; + } + return it->second.userId; +} + void SvcSessionManager::SetBundleDataSize(const std::string &bundleName, int64_t dataSize) { auto it = impl_.backupExtNameMap.find(bundleName); diff --git a/tests/mock/module_ipc/svc_session_manager_throw_mock.cpp b/tests/mock/module_ipc/svc_session_manager_throw_mock.cpp index 60d2b8eb25c8112dc3584d05f62b5c3fae9d836d..4ca98dc726638859f7d7905ac4e5019b83a7d17f 100644 --- a/tests/mock/module_ipc/svc_session_manager_throw_mock.cpp +++ b/tests/mock/module_ipc/svc_session_manager_throw_mock.cpp @@ -140,9 +140,19 @@ std::weak_ptr SvcSessionManager::GetSAExtConnection(const Bu return BackupSvcSessionManager::session->GetSAExtConnection(bundleName); } -void SvcSessionManager::AppendBundles(const vector &bundleNames) +int32_t SvcSessionManager::GetBundleUserId(const string &bundleName) { - BackupSvcSessionManager::session->AppendBundles(bundleNames); + return BackupSvcSessionManager::session->GetBundleUserId(bundleName); +} + +void SvcSessionManager::SetBundleUserId(const string &bundleName, const int32_t userId) +{ + BackupSvcSessionManager::session->SetBundleUserId(bundleName, userId); +} + +void SvcSessionManager::AppendBundles(const vector &bundleNames, vector &failedBundles) +{ + BackupSvcSessionManager::session->AppendBundles(bundleNames, failedBundles); } sptr SvcSessionManager::CreateBackupConnection(BundleName &bundleName) diff --git a/tests/mock/module_ipc/svc_session_manager_throw_mock.h b/tests/mock/module_ipc/svc_session_manager_throw_mock.h index 8e0c798c5bb30dc916a3c0a93faf09de452f8726..39ae9852de4eb3526b339acac788ddf072a49f21 100644 --- a/tests/mock/module_ipc/svc_session_manager_throw_mock.h +++ b/tests/mock/module_ipc/svc_session_manager_throw_mock.h @@ -52,7 +52,7 @@ public: virtual std::string GetBackupExtInfo(const std::string &) = 0; virtual void SetBackupExtName(const std::string &, const std::string &) = 0; virtual std::weak_ptr GetSAExtConnection(const BundleName &) = 0; - virtual void AppendBundles(const std::vector &) = 0; + virtual void AppendBundles(const std::vector &, std::vector &) = 0; virtual sptr CreateBackupConnection(BundleName &) = 0; virtual ErrCode Start() = 0; virtual ErrCode Finish() = 0; @@ -69,6 +69,8 @@ public: virtual int64_t GetBundleVersionCode(const std::string &) = 0; virtual void SetBundleVersionName(const std::string &, std::string) = 0; virtual std::string GetBundleVersionName(const std::string &) = 0; + virtual void SetBundleUserId(const std::string &, const int32_t) = 0; + virtual int32_t GetBundleUserId(const std::string &) = 0; virtual void SetBundleDataSize(const std::string &, int64_t) = 0; virtual bool StartFwkTimer(const std::string &, const Utils::Timer::TimerCallback &) = 0; virtual bool StopFwkTimer(const std::string &) = 0; @@ -126,7 +128,7 @@ public: MOCK_METHOD(std::string, GetBackupExtInfo, (const std::string &)); MOCK_METHOD(void, SetBackupExtName, (const std::string &, const std::string &)); MOCK_METHOD(std::weak_ptr, GetSAExtConnection, (const BundleName &)); - MOCK_METHOD(void, AppendBundles, (const std::vector &)); + MOCK_METHOD(void, AppendBundles, (const std::vector &, std::vector &)); MOCK_METHOD(sptr, CreateBackupConnection, (BundleName &)); MOCK_METHOD(ErrCode, Start, ()); MOCK_METHOD(ErrCode, Finish, ()); @@ -143,6 +145,8 @@ public: MOCK_METHOD(int64_t, GetBundleVersionCode, (const std::string &)); MOCK_METHOD(void, SetBundleVersionName, (const std::string &, std::string)); MOCK_METHOD(std::string, GetBundleVersionName, (const std::string &)); + MOCK_METHOD(void, SetBundleUserId, (const std::string &, const int32_t)); + MOCK_METHOD(int32_t, GetBundleUserId, (const std::string &)); MOCK_METHOD(void, SetBundleDataSize, (const std::string &, int64_t)); MOCK_METHOD(bool, StartFwkTimer, (const std::string &, const Utils::Timer::TimerCallback &)); MOCK_METHOD(bool, StopFwkTimer, (const std::string &)); diff --git a/tests/unittests/backup_sa/BUILD.gn b/tests/unittests/backup_sa/BUILD.gn index d4015e143a9df5630469033d44fe6840e4f7ed31..5351c84bdbf85a42b387bef2ee72b856a1145dc2 100644 --- a/tests/unittests/backup_sa/BUILD.gn +++ b/tests/unittests/backup_sa/BUILD.gn @@ -18,6 +18,7 @@ group("backup_sa_test") { testonly = true deps = [ + "module_app_gallery:app_gallery_test", "module_external:adapter_test", "module_ipc:backup_sa_ipc_test", "session:session_test", diff --git a/tests/unittests/backup_sa/module_app_gallery/BUILD.gn b/tests/unittests/backup_sa/module_app_gallery/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..24d57884cc146425f1aa3c41ee1cbf93c2c4c106 --- /dev/null +++ b/tests/unittests/backup_sa/module_app_gallery/BUILD.gn @@ -0,0 +1,73 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") +import("//foundation/filemanagement/app_file_service/backup.gni") + +ohos_unittest("app_gallery_dispose_proxy_test") { + branch_protector_ret = "pac_ret" + + module_out_path = path_module_out_tests + + sources = [ + "${path_backup_mock}/module_external/src/bms_adapter_mock.cpp", + "${path_backup}/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp", + "${path_backup}/services/backup_sa/src/module_app_gallery/app_gallery_service_connection.cpp", + "app_gallery_dispose_proxy_test.cpp", + ] + + include_dirs = [ + "${path_backup}/services/backup_sa/include", + "${path_backup}/interfaces/inner_api/native/backup_kit_inner/impl", + "${path_backup}/tests/unittests/backup_api/backup_impl/include", + "${path_backup_mock}/module_external/include", + ] + + deps = [ + "${path_backup}/interfaces/inner_api/native/backup_kit_inner:backup_kit_inner", + "${path_backup}/tests/utils:backup_test_utils", + "${path_backup}/utils:backup_utils", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:ability_manager", + "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "hisysevent:libhisysevent", + "hitrace:hitrace_meter", + "init:libbegetutil", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "storage_service:storage_manager_sa_proxy", + ] + + defines = [ + "LOG_TAG=\"app_file_service\"", + "LOG_DOMAIN=0xD004303", + "private = public", + ] + + use_exceptions = true +} + +group("app_gallery_test") { + testonly = true + deps = [ ":app_gallery_dispose_proxy_test" ] +} diff --git a/tests/unittests/backup_sa/module_app_gallery/app_gallery_dispose_proxy_test.cpp b/tests/unittests/backup_sa/module_app_gallery/app_gallery_dispose_proxy_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73a31317e816a0005b59b88a32248cf17537ff65 --- /dev/null +++ b/tests/unittests/backup_sa/module_app_gallery/app_gallery_dispose_proxy_test.cpp @@ -0,0 +1,246 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include + +#include "b_error/b_error.h" +#include "b_radar/b_radar.h" +#include "b_sa/b_sa_utils.h" +#include "b_jsonutil/b_jsonutil.h" +#include "filemgmt_libhilog.h" +#include "message_parcel.h" + +#include "bms_adapter_mock.h" +#include "module_app_gallery/app_gallery_dispose_proxy.h" +#include "module_app_gallery/app_gallery_service_connection.h" +#include "want.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; +using namespace testing; +using namespace testing::ext; + +class AppGalleryTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(); + void SetUp() {}; + void TearDown() {}; + + static inline shared_ptr bms = nullptr; +}; + +void AppGalleryTest::SetUpTestCase(void) +{ + bms = make_shared(); + BundleMgrAdapterMock::bms = bms; +} + +void AppGalleryTest::TearDownTestCase() +{ + BundleMgrAdapterMock::bms = nullptr; + bms = nullptr; +} + +/** + * @tc.number: App_Gallery_GetInstance_0100 + * @tc.name: App_Gallery_GetInstance_0100 + * @tc.desc: 测试 GetInstance + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issueIAKC3I + */ +HWTEST_F(AppGalleryTest, App_Gallery_GetInstance_0100, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin App_Gallery_GetInstance_0100"; + try { + auto instance = AppGalleryDisposeProxy::GetInstance(); + bool result = instance != nullptr; + EXPECT_TRUE(result); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by App_Gallery_GetInstance_0100."; + } + GTEST_LOG_(INFO) << "ServiceTest-end App_Gallery_GetInstance_0100"; +} + +/** + * @tc.number: App_Gallery_GetInstance_0200 + * @tc.name: App_Gallery_GetInstance_0200 + * @tc.desc: 测试 GetInstance + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issueIAKC3I + */ +HWTEST_F(AppGalleryTest, App_Gallery_GetInstance_0200, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin App_Gallery_GetInstance_0200"; + try { + auto instance1 = AppGalleryDisposeProxy::GetInstance(); + auto instance2 = AppGalleryDisposeProxy::GetInstance(); + bool result = instance1 == instance2; + EXPECT_TRUE(result); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by App_Gallery_GetInstance_0200."; + } + GTEST_LOG_(INFO) << "ServiceTest-end App_Gallery_GetInstance_0200"; +} + +/** + * @tc.number: App_Gallery_GetAppGalleryConnection_0100 + * @tc.name: App_Gallery_GetAppGalleryConnection_0100 + * @tc.desc: 测试 GetAppGalleryConnection + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issueIAKC3I + */ +HWTEST_F(AppGalleryTest, App_Gallery_GetAppGalleryConnection_0100, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin App_Gallery_GetAppGalleryConnection_0100"; + try { + auto instance = AppGalleryDisposeProxy::GetInstance(); + EXPECT_TRUE(instance != nullptr); + int32_t userId = 100; + auto result = instance->GetAppGalleryConnection(userId); + EXPECT_TRUE(result == nullptr); + instance->appGalleryConnectionMap_[userId] = sptr(new AppGalleryConnection(userId)); + result = instance->GetAppGalleryConnection(userId); + EXPECT_TRUE(result != nullptr); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by App_Gallery_GetAppGalleryConnection_0100."; + } + GTEST_LOG_(INFO) << "ServiceTest-end App_Gallery_GetAppGalleryConnection_0100"; +} + +/** + * @tc.number: App_Gallery_ConnectAppGallery_0100 + * @tc.name: App_Gallery_ConnectAppGallery_0100 + * @tc.desc: 测试 ConnectAppGallery + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issueIAKC3I + */ +HWTEST_F(AppGalleryTest, App_Gallery_ConnectAppGallery_0100, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin App_Gallery_ConnectAppGallery_0100"; + try { + auto instance = AppGalleryDisposeProxy::GetInstance(); + EXPECT_TRUE(instance != nullptr); + int32_t userId = 101; + EXPECT_CALL(*bms, GetAppGalleryBundleName()).WillOnce(Return("")); + auto result = instance->ConnectAppGallery(userId); + EXPECT_TRUE(result == nullptr); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by App_Gallery_ConnectAppGallery_0100."; + } + GTEST_LOG_(INFO) << "ServiceTest-end App_Gallery_ConnectAppGallery_0100"; +} + +/** + * @tc.number: App_Gallery_StartRestore_0100 + * @tc.name: App_Gallery_StartRestore_0100 + * @tc.desc: 测试 StartRestore + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issueIAKC3I + */ +HWTEST_F(AppGalleryTest, App_Gallery_StartRestore_0100, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin App_Gallery_StartRestore_0100"; + try { + auto instance = AppGalleryDisposeProxy::GetInstance(); + EXPECT_TRUE(instance != nullptr); + std::string bundleName = "1234"; + int32_t userId = 102; + auto result = instance->StartRestore(bundleName, userId); + EXPECT_EQ(result, DisposeErr::OK); + result = instance->EndRestore(bundleName, userId); + EXPECT_EQ(result, DisposeErr::OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by App_Gallery_StartRestore_0100."; + } + GTEST_LOG_(INFO) << "ServiceTest-end App_Gallery_StartRestore_0100"; +} + +/** + * @tc.number: App_Gallery_StartRestore_0200 + * @tc.name: App_Gallery_StartRestore_0200 + * @tc.desc: 测试 StartRestore + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issueIAKC3I + */ +HWTEST_F(AppGalleryTest, App_Gallery_StartRestore_0200, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin App_Gallery_StartRestore_0200"; + try { + auto instance = AppGalleryDisposeProxy::GetInstance(); + EXPECT_TRUE(instance != nullptr); + std::string bundleName = "testapp"; + int32_t userId = 102; + EXPECT_CALL(*bms, GetAppGalleryBundleName()).WillOnce(Return("")); + auto result = instance->StartRestore(bundleName, userId); + EXPECT_EQ(result, DisposeErr::CONN_FAIL); + EXPECT_CALL(*bms, GetAppGalleryBundleName()).WillOnce(Return("")); + result = instance->EndRestore(bundleName, userId); + EXPECT_EQ(result, DisposeErr::CONN_FAIL); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by App_Gallery_StartRestore_0200."; + } + GTEST_LOG_(INFO) << "ServiceTest-end App_Gallery_StartRestore_0200"; +} + +/** + * @tc.number: App_Gallery_StartBackup_0100 + * @tc.name: App_Gallery_StartBackup_0100 + * @tc.desc: 测试 StartBackup + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issueIAKC3I + */ +HWTEST_F(AppGalleryTest, App_Gallery_StartBackup_0100, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin App_Gallery_StartBackup_0100"; + try { + auto instance = AppGalleryDisposeProxy::GetInstance(); + EXPECT_TRUE(instance != nullptr); + std::string bundleName = "1234"; + int32_t userId = 102; + EXPECT_CALL(*bms, GetAppGalleryBundleName()).WillOnce(Return("")); + auto result = instance->StartBackup(bundleName, userId); + EXPECT_EQ(result, DisposeErr::CONN_FAIL); + EXPECT_CALL(*bms, GetAppGalleryBundleName()).WillOnce(Return("")); + result = instance->EndBackup(bundleName, userId); + EXPECT_EQ(result, DisposeErr::CONN_FAIL); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by App_Gallery_StartBackup_0100."; + } + GTEST_LOG_(INFO) << "ServiceTest-end App_Gallery_StartBackup_0100"; +} +} \ No newline at end of file diff --git a/tests/unittests/backup_sa/module_ipc/BUILD.gn b/tests/unittests/backup_sa/module_ipc/BUILD.gn index d6891417596a7b6efac3dbc69dfaa91b4bec65c2..6685f522e0465f166c8fb7d4fd65fab27b0c714b 100644 --- a/tests/unittests/backup_sa/module_ipc/BUILD.gn +++ b/tests/unittests/backup_sa/module_ipc/BUILD.gn @@ -358,6 +358,7 @@ ohos_unittest("backup_restore_deps_manager_test") { sources = [ "${path_backup_mock}/accesstoken/accesstoken_kit_mock.cpp", "${path_backup}/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp", + "${path_backup}/services/backup_sa/src/module_app_gallery/app_gallery_service_connection.cpp", "${path_backup}/services/backup_sa/src/module_ipc/sa_backup_connection.cpp", "${path_backup}/services/backup_sa/src/module_ipc/service.cpp", "${path_backup}/services/backup_sa/src/module_ipc/service_incremental.cpp", 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 833422e4ebb0d5dee7fa57851714eda00ffa3e34..4e0b7d1cf7d33b8b76e2a05f1093dca034b16365 100644 --- a/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp @@ -317,6 +317,7 @@ UniqueFd Service::GetLocalCapabilitiesForBundleInfos() return UniqueFd(-1); } +void Service::AppendBundles(const std::vector &bundleNames) {} ErrCode Service::GetBackupDataSize(bool isPreciseScan, vector bundleNameList) { return BError(BError::Codes::OK); 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 84e8abfd12692b5219cc1e70b2f63e2328451f2d..a34585797296e9e5e51e4955014520b6ab06909e 100644 --- a/tests/unittests/backup_sa/module_ipc/service_other_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_other_test.cpp @@ -936,6 +936,8 @@ HWTEST_F(ServiceTest, SUB_Service_SetCurrentSessProperties_0300, TestSize.Level1 RestoreTypeEnum restoreType = RestoreTypeEnum::RESTORE_DATA_WAIT_SEND; std::string backupVersion; + EXPECT_CALL(*session, GetSessionUserId()).WillOnce(Return(0)).WillOnce(Return(0)).WillOnce(Return(0)) + .WillOnce(Return(0)).WillOnce(Return(0)); EXPECT_CALL(*jsonUtil, BuildBundleNameIndexInfo(_, _)).WillOnce(Return("bundleName")) .WillOnce(Return("bundleName")); EXPECT_CALL(*saUtils, IsSABundleName(_)).WillOnce(Return(true)); diff --git a/tests/unittests/backup_sa/module_ipc/service_throw_test.cpp b/tests/unittests/backup_sa/module_ipc/service_throw_test.cpp index 0dd449a16f206c76abc4b5704c430edeb7439feb..05bd4ba713fd973746a164da5c08e4332ddb00bb 100644 --- a/tests/unittests/backup_sa/module_ipc/service_throw_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_throw_test.cpp @@ -675,41 +675,6 @@ HWTEST_F(ServiceThrowTest, SUB_Service_throw_OnBackupExtensionDied_0100, testing GTEST_LOG_(INFO) << "ServiceThrowTest-end SUB_Service_throw_OnBackupExtensionDied_0100"; } -/** - * @tc.number: SUB_Service_throw_ExtStart_0100 - * @tc.name: SUB_Service_throw_ExtStart_0100 - * @tc.desc: 测试 ExtStart 接口的 catch 分支 - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: issuesIAC04T - */ -HWTEST_F(ServiceThrowTest, SUB_Service_throw_ExtStart_0100, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "ServiceThrowTest-begin SUB_Service_throw_ExtStart_0100"; - try { - EXPECT_NE(service, nullptr); - string bundleName; - EXPECT_CALL(*sessionMock, GetScenario()).WillOnce(Invoke([]() { - throw BError(BError::Codes::EXT_THROW_EXCEPTION); - return IServiceReverse::Scenario::UNDEFINED; - })).WillOnce(Invoke([]() { - throw BError(BError::Codes::EXT_THROW_EXCEPTION); - return IServiceReverse::Scenario::UNDEFINED; - })); - EXPECT_CALL(*sessionMock, RemoveExtInfo(_)).WillOnce(Invoke([]() { - throw BError(BError::Codes::EXT_THROW_EXCEPTION); - })); - EXPECT_CALL(*sessionMock, IsOnAllBundlesFinished()).WillOnce(Return(false)); - service->ExtStart(bundleName); - EXPECT_TRUE(true); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "ServiceThrowTest-an exception occurred by ExtStart."; - } - GTEST_LOG_(INFO) << "ServiceThrowTest-end SUB_Service_throw_ExtStart_0100"; -} - /** * @tc.number: SUB_Service_throw_ExtConnectFailed_0100 * @tc.name: SUB_Service_throw_ExtConnectFailed_0100 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 8a4b9d978f05ff5783efb65e2f89f66be748f59c..1363c4a4e22ff1b8a3229ae4aa2d393379553c1b 100644 --- a/tests/unittests/backup_sa/module_ipc/sub_service_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/sub_service_test.cpp @@ -616,7 +616,7 @@ HWTEST_F(ServiceTest, SUB_Service_ReportOnExtConnectFailed_0000, TestSize.Level1 EXPECT_CALL(*session, ValidRestoreDataType(_)).WillOnce(Return(true)); EXPECT_CALL(*session, GetServiceReverseProxy()).WillOnce(Return(srProxy)); EXPECT_CALL(*srProxy, IncrementalRestoreOnBundleStarted(_, _)).WillOnce(Return()); - EXPECT_CALL(*gallery, EndRestore(_)).WillOnce(Return(DisposeErr::OK)); + EXPECT_CALL(*gallery, EndRestore(_, _)).WillOnce(Return(DisposeErr::OK)); service->ReportOnExtConnectFailed(IServiceReverse::Scenario::RESTORE, bundleName, 0); EXPECT_TRUE(true); @@ -624,14 +624,14 @@ HWTEST_F(ServiceTest, SUB_Service_ReportOnExtConnectFailed_0000, TestSize.Level1 EXPECT_CALL(*session, ValidRestoreDataType(_)).WillOnce(Return(false)); EXPECT_CALL(*session, GetServiceReverseProxy()).WillOnce(Return(srProxy)); EXPECT_CALL(*srProxy, RestoreOnBundleStarted(_, _)).WillOnce(Return()); - EXPECT_CALL(*gallery, EndRestore(_)).WillOnce(Return(DisposeErr::OK)); + EXPECT_CALL(*gallery, EndRestore(_, _)).WillOnce(Return(DisposeErr::OK)); service->ReportOnExtConnectFailed(IServiceReverse::Scenario::RESTORE, bundleName, 0); EXPECT_TRUE(true); EXPECT_CALL(*param, GetBackupOverrideIncrementalRestore()).WillOnce(Return(false)); EXPECT_CALL(*session, GetServiceReverseProxy()).WillOnce(Return(srProxy)); EXPECT_CALL(*srProxy, RestoreOnBundleStarted(_, _)).WillOnce(Return()); - EXPECT_CALL(*gallery, EndRestore(_)).WillOnce(Return(DisposeErr::OK)); + EXPECT_CALL(*gallery, EndRestore(_, _)).WillOnce(Return(DisposeErr::OK)); service->ReportOnExtConnectFailed(IServiceReverse::Scenario::RESTORE, bundleName, 0); EXPECT_TRUE(true); } catch (...) { @@ -747,6 +747,7 @@ HWTEST_F(ServiceTest, SUB_Service_ExtConnectDone_0000, TestSize.Level1) { GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_ExtConnectDone_0000"; try { + EXPECT_CALL(*session, GetClearDataFlag(_)).WillOnce(Return(false)).WillOnce(Return(false)); EXPECT_CALL(*session, GetServiceSchedAction(_)).WillOnce(Return(BConstants::ServiceSchedAction::WAIT)); EXPECT_CALL(*cdConfig, InsertClearBundleRecord(_)).WillOnce(Return(true)); service->ExtConnectDone(""); @@ -1066,7 +1067,7 @@ HWTEST_F(ServiceTest, SUB_Service_SendStartAppGalleryNotify_0000, TestSize.Level EXPECT_CALL(*saUtils, IsSABundleName(_)).WillOnce(Return(false)); EXPECT_CALL(*session, GetScenario()).WillOnce(Return(IServiceReverse::Scenario::RESTORE)); EXPECT_CALL(*jdConfig, IfBundleNameInDisposalConfigFile(_)).WillOnce(Return(true)); - EXPECT_CALL(*gallery, StartRestore(_)).WillOnce(Return(DisposeErr::OK)); + EXPECT_CALL(*gallery, StartRestore(_, _)).WillOnce(Return(DisposeErr::OK)); service->SendStartAppGalleryNotify(bundleName); EXPECT_TRUE(true); } catch (...) { @@ -1101,20 +1102,20 @@ HWTEST_F(ServiceTest, SUB_Service_SendEndAppGalleryNotify_0000, TestSize.Level1) EXPECT_CALL(*saUtils, IsSABundleName(_)).WillOnce(Return(false)); EXPECT_CALL(*session, GetScenario()).WillOnce(Return(IServiceReverse::Scenario::RESTORE)); - EXPECT_CALL(*gallery, EndRestore(_)).WillOnce(Return(DisposeErr::REQUEST_FAIL)); + EXPECT_CALL(*gallery, EndRestore(_, _)).WillOnce(Return(DisposeErr::REQUEST_FAIL)); service->SendEndAppGalleryNotify(bundleName); EXPECT_TRUE(true); EXPECT_CALL(*saUtils, IsSABundleName(_)).WillOnce(Return(false)); EXPECT_CALL(*session, GetScenario()).WillOnce(Return(IServiceReverse::Scenario::RESTORE)); - EXPECT_CALL(*gallery, EndRestore(_)).WillOnce(Return(DisposeErr::OK)); + EXPECT_CALL(*gallery, EndRestore(_, _)).WillOnce(Return(DisposeErr::OK)); EXPECT_CALL(*jdConfig, DeleteFromDisposalConfigFile(_)).WillOnce(Return(false)); service->SendEndAppGalleryNotify(bundleName); EXPECT_TRUE(true); EXPECT_CALL(*saUtils, IsSABundleName(_)).WillOnce(Return(false)); EXPECT_CALL(*session, GetScenario()).WillOnce(Return(IServiceReverse::Scenario::RESTORE)); - EXPECT_CALL(*gallery, EndRestore(_)).WillOnce(Return(DisposeErr::OK)); + EXPECT_CALL(*gallery, EndRestore(_, _)).WillOnce(Return(DisposeErr::OK)); EXPECT_CALL(*jdConfig, DeleteFromDisposalConfigFile(_)).WillOnce(Return(true)); service->SendEndAppGalleryNotify(bundleName); EXPECT_TRUE(true); @@ -1139,12 +1140,12 @@ HWTEST_F(ServiceTest, SUB_Service_TryToClearDispose_0000, TestSize.Level1) GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_TryToClearDispose_0000"; try { BundleName bundleName; - EXPECT_CALL(*gallery, EndRestore(_)).WillOnce(Return(DisposeErr::OK)); + EXPECT_CALL(*gallery, EndRestore(_, _)).WillOnce(Return(DisposeErr::OK)); EXPECT_CALL(*jdConfig, DeleteFromDisposalConfigFile(_)).WillOnce(Return(true)); service->TryToClearDispose(bundleName); EXPECT_TRUE(true); - EXPECT_CALL(*gallery, EndRestore(_)).WillOnce(Return(DisposeErr::REQUEST_FAIL)) + EXPECT_CALL(*gallery, EndRestore(_, _)).WillOnce(Return(DisposeErr::REQUEST_FAIL)) .WillOnce(Return(DisposeErr::OK)); EXPECT_CALL(*jdConfig, DeleteFromDisposalConfigFile(_)).WillOnce(Return(false)); service->TryToClearDispose(bundleName); @@ -1182,7 +1183,7 @@ HWTEST_F(ServiceTest, SUB_Service_SendErrAppGalleryNotify_0000, TestSize.Level1) bundleNameList.emplace_back("bundleName"); EXPECT_CALL(*session, GetScenario()).WillOnce(Return(IServiceReverse::Scenario::RESTORE)); EXPECT_CALL(*jdConfig, GetBundleNameFromConfigFile()).WillOnce(Return(bundleNameList)); - EXPECT_CALL(*gallery, EndRestore(_)).WillOnce(Return(DisposeErr::OK)); + EXPECT_CALL(*gallery, EndRestore(_, _)).WillOnce(Return(DisposeErr::OK)); EXPECT_CALL(*jdConfig, DeleteFromDisposalConfigFile(_)).WillOnce(Return(true)); service->SendErrAppGalleryNotify(); EXPECT_TRUE(true); @@ -1213,7 +1214,7 @@ HWTEST_F(ServiceTest, SUB_Service_ClearDisposalOnSaStart_0000, TestSize.Level1) bundleNameList.emplace_back("bundleName"); EXPECT_CALL(*jdConfig, GetBundleNameFromConfigFile()).WillOnce(Return(bundleNameList)); - EXPECT_CALL(*gallery, EndRestore(_)).WillOnce(Return(DisposeErr::OK)); + EXPECT_CALL(*gallery, EndRestore(_, _)).WillOnce(Return(DisposeErr::OK)); EXPECT_CALL(*jdConfig, DeleteFromDisposalConfigFile(_)).WillOnce(Return(true)); service->ClearDisposalOnSaStart(); EXPECT_TRUE(true); diff --git a/tests/unittests/backup_sa/module_ipc/svc_session_manager_test.cpp b/tests/unittests/backup_sa/module_ipc/svc_session_manager_test.cpp index a32ca5a1a95ae2497fa3c2f5ff5371f8eb54a8fc..54c6a506783b0c6ce680b06d19bedd59443689ce 100644 --- a/tests/unittests/backup_sa/module_ipc/svc_session_manager_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/svc_session_manager_test.cpp @@ -70,13 +70,14 @@ void SvcSessionManagerTest::TearDownTestCase(void) void SvcSessionManagerTest::Init(IServiceReverse::Scenario scenario) { vector bundleNames; + vector failedBundles; map backupExtNameMap; bundleNames.emplace_back(BUNDLE_NAME); EXPECT_TRUE(sessionManagerPtr_ != nullptr); sessionManagerPtr_->Active( {.clientToken = CLIENT_TOKEN_ID, .scenario = scenario, .backupExtNameMap = {}, .clientProxy = remote_}); sessionManagerPtr_->IsOnAllBundlesFinished(); - sessionManagerPtr_->AppendBundles(bundleNames); + sessionManagerPtr_->AppendBundles(bundleNames, failedBundles); sessionManagerPtr_->Finish(); sessionManagerPtr_->IsOnAllBundlesFinished(); } @@ -960,23 +961,24 @@ HWTEST_F(SvcSessionManagerTest, SUB_backup_sa_session_AppendBundles_0100, testin GTEST_LOG_(INFO) << "SvcSessionManagerTest-begin SUB_backup_sa_session_AppendBundles_0100"; try { vector bundleNames; + vector failedBundles; EXPECT_TRUE(sessionManagerPtr_ != nullptr); sessionManagerPtr_->impl_.clientToken = 0; - sessionManagerPtr_->AppendBundles(bundleNames); + sessionManagerPtr_->AppendBundles(bundleNames, failedBundles); EXPECT_TRUE(true); bundleNames.clear(); bundleNames.emplace_back("app1"); sessionManagerPtr_->impl_.clientToken = CLIENT_TOKEN_ID; sessionManagerPtr_->impl_.backupExtNameMap.clear(); - sessionManagerPtr_->AppendBundles(bundleNames); + sessionManagerPtr_->AppendBundles(bundleNames, failedBundles); EXPECT_EQ(sessionManagerPtr_->impl_.backupExtNameMap.size(), 1); bundleNames.clear(); bundleNames.emplace_back("123"); sessionManagerPtr_->impl_.clientToken = CLIENT_TOKEN_ID; sessionManagerPtr_->impl_.backupExtNameMap.clear(); - sessionManagerPtr_->AppendBundles(bundleNames); + sessionManagerPtr_->AppendBundles(bundleNames, failedBundles); EXPECT_EQ(sessionManagerPtr_->impl_.backupExtNameMap.size(), 1); } catch (...) { EXPECT_FALSE(false); @@ -1343,6 +1345,57 @@ HWTEST_F(SvcSessionManagerTest, SUB_backup_sa_session_GetBundleVersionName_0100, GTEST_LOG_(INFO) << "SvcSessionManagerTest-end SUB_backup_sa_session_GetBundleVersionName_0100"; } +/** + * @tc.number: SUB_backup_sa_session_SetBundleUserId_0100 + * @tc.name: SUB_backup_sa_session_SetBundleUserId_0100 + * @tc.desc: 测试 SetBundleUserId 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6VA38 + */ +HWTEST_F(SvcSessionManagerTest, SUB_backup_sa_session_SetBundleUserId_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SvcSessionManagerTest-begin SUB_backup_sa_session_SetBundleUserId_0100"; + try { + int32_t userId = 100; + sessionManagerPtr_->impl_.clientToken = CLIENT_TOKEN_ID; + sessionManagerPtr_->impl_.backupExtNameMap.clear(); + sessionManagerPtr_->impl_.backupExtNameMap[BUNDLE_NAME] = {}; + sessionManagerPtr_->SetBundleUserId(BUNDLE_NAME, userId); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "SvcSessionManagerTest-an exception occurred by SetBundleUserId."; + } + GTEST_LOG_(INFO) << "SvcSessionManagerTest-end SUB_backup_sa_session_SetBundleUserId_0100"; +} + +/** + * @tc.number: SUB_backup_sa_session_GetBundleUserId_0100 + * @tc.name: SUB_backup_sa_session_GetBundleUserId_0100 + * @tc.desc: 测试 GetBundleUserId 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6VA38 + */ +HWTEST_F(SvcSessionManagerTest, SUB_backup_sa_session_GetBundleUserId_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SvcSessionManagerTest-begin SUB_backup_sa_session_GetBundleUserId_0100"; + try { + sessionManagerPtr_->impl_.clientToken = CLIENT_TOKEN_ID; + sessionManagerPtr_->impl_.backupExtNameMap.clear(); + sessionManagerPtr_->impl_.backupExtNameMap[BUNDLE_NAME] = {}; + auto result = sessionManagerPtr_->GetBundleUserId(BUNDLE_NAME); + EXPECT_EQ(result, BConstants::DEFAULT_USER_ID); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "SvcSessionManagerTest-an exception occurred by GetBundleUserId."; + } + GTEST_LOG_(INFO) << "SvcSessionManagerTest-end SUB_backup_sa_session_GetBundleUserId_0100"; +} + /** * @tc.number: SUB_backup_sa_session_Start_0100 * @tc.name: SUB_backup_sa_session_Start_0100