diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index 1b92cc342064fb7333e4bf19fb8b695165fa3214..06e25ba2064099d9a2a9c974b343dc5c36733a67 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -25,6 +25,7 @@ #include "b_json/b_json_service_disposal_config.h" #include "i_service_reverse.h" #include "iremote_stub.h" +#include "module_ipc/svc_backup_connection.h" #include "module_sched/sched_scheduler.h" #include "service_stub.h" #include "svc_session_manager.h" @@ -320,21 +321,21 @@ private: * @brief 验证调用者 * */ - void VerifyCaller(); + ErrCode VerifyCaller(); /** * @brief 验证调用者 * * @param scenario Scenario状态 */ - void VerifyCaller(IServiceReverse::Scenario scenario); + ErrCode VerifyCaller(IServiceReverse::Scenario scenario); /** * @brief 验证调用者并返回名称 * - * @return std::string + * @return ErrCode */ - std::string VerifyCallerAndGetCallerName(); + ErrCode VerifyCallerAndGetCallerName(std::string &bundleName); /** * @brief 清除Session Sched相关资源 @@ -559,6 +560,8 @@ private: void SetBundleIncDataInfo(const std::vector &bundlesToBackup, std::vector &supportBundleNames); + + sptr GetExtProxyByBundleName(const std::string &bundleName); private: static sptr instance_; static std::mutex instanceLock_; 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 a3656084e4c83d320f9b72f0154ecab5b842b2c9..84ae97cb290d7e5a58f77e7e3317bfd8b5bd5f7d 100644 --- a/services/backup_sa/include/module_ipc/svc_session_manager.h +++ b/services/backup_sa/include/module_ipc/svc_session_manager.h @@ -102,7 +102,7 @@ public: * @throw BError::Codes::SA_REFUSED_ACT 调用者不是会话所有者 * @throw BError::Codes::SDK_MIXED_SCENARIO 调用者在备份/恢复场景使用了不匹配的函数 */ - void VerifyCallerAndScenario(uint32_t clientToken, IServiceReverse::Scenario scenario) const; + ErrCode VerifyCallerAndScenario(uint32_t clientToken, IServiceReverse::Scenario scenario) const; /** * @brief 激活会话 @@ -118,7 +118,7 @@ public: * @param remoteInAction 尝试关闭会话的客户端代理。只有激活会话的客户端代理有权关闭会话 * @param force 强制关闭 */ - void Deactive(const wptr &remoteInAction, bool force = false); + ErrCode Deactive(const wptr &remoteInAction, bool force = false); /** * @brief 检验调用者给定的bundleName是否是有效的 @@ -126,7 +126,7 @@ public: * @param bundleName 调用者名称 * @throw BError::Codes::SA_REFUSED_ACT 调用者不是会话所有者 */ - void VerifyBundleName(std::string &bundleName); + ErrCode VerifyBundleName(std::string &bundleName); /** * @brief 获取IServiceReverse @@ -300,14 +300,14 @@ public: * * @return ErrCode */ - void Start(); + ErrCode Start(); /** * @brief 结束追加应用 * * @return ErrCode */ - void Finish(); + ErrCode Finish(); /** * @brief 整个备份恢复流程是否结束 @@ -539,7 +539,7 @@ private: * * @param newImpl */ - void InitClient(Impl &newImpl); + ErrCode InitClient(Impl &newImpl); /** * @brief 获取BackupExtNameMap iterator @@ -547,7 +547,7 @@ private: * @param bundleName 应用名称 * @return std::map::iterator */ - std::map::iterator GetBackupExtNameMap(const std::string &bundleName); + std::tuple::iterator> GetBackupExtNameMap(const std::string &bundleName); /** * @brief 计算出应用程序处理数据可能使用的时间 diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 3f9f76ef441666062b3b2564a43486d96ad39d6d..fcbe146dd3004969bc1e0fdb008771a21766147b 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -356,7 +356,7 @@ static inline void PermissionCheckFailRadar(const std::string &info, const std:: BError(BError::Codes::SA_REFUSED_ACT).GetCode()); } -string Service::VerifyCallerAndGetCallerName() +ErrCode Service::VerifyCallerAndGetCallerName(std::string &bundleName) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); uint32_t tokenCaller = IPCSkeleton::GetCallingTokenID(); @@ -365,81 +365,101 @@ string Service::VerifyCallerAndGetCallerName() Security::AccessToken::HapTokenInfo hapTokenInfo; if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenCaller, hapTokenInfo) != 0) { PermissionCheckFailRadar("Get hap token info failed", "VerifyCallerAndGetCallerName"); - throw BError(BError::Codes::SA_INVAL_ARG, "Get hap token info failed"); + HILOGE("Verify and get caller name failed, Get hap token info failed"); + return BError(BError::Codes::SA_INVAL_ARG); } std::string bundleNameIndexInfo = BJsonUtil::BuildBundleNameIndexInfo(hapTokenInfo.bundleName, hapTokenInfo.instIndex); - session_->VerifyBundleName(bundleNameIndexInfo); - return bundleNameIndexInfo; + ErrCode ret = session_->VerifyBundleName(bundleNameIndexInfo); + if (ret != ERR_OK) { + HILOGE("Verify bundle name failed, bundleNameIndexInfo:%{public}s", bundleNameIndexInfo.c_str()); + return ret; + } + bundleName = bundleNameIndexInfo; + return BError(BError::Codes::OK); } else { string str = to_string(tokenCaller); HILOGE("tokenID = %{private}s", GetAnonyString(str).c_str()); std::string info = string("Invalid token type").append(to_string(tokenType)).append(string("\"}")); PermissionCheckFailRadar(info, "VerifyCallerAndGetCallerName"); - throw BError(BError::Codes::SA_INVAL_ARG, string("Invalid token type ").append(to_string(tokenType))); + HILOGE("Verify and get caller name failed, Invalid token type"); + return BError(BError::Codes::SA_INVAL_ARG); } } -void Service::VerifyCaller() +ErrCode Service::VerifyCaller() { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); uint32_t tokenCaller = IPCSkeleton::GetCallingTokenID(); int tokenType = Security::AccessToken::AccessTokenKit::GetTokenType(tokenCaller); + ErrCode ret = BError(BError::Codes::OK); switch (tokenType) { case Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE: { /* Update Service */ if (Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, BACKUP_PERMISSION) != Security::AccessToken::PermissionState::PERMISSION_GRANTED) { + HILOGE("Permission denied, token type is token native"); std::string info = "Permission denied, token type is " + to_string(tokenType); PermissionCheckFailRadar(info, "VerifyCaller"); - throw BError(BError::Codes::SA_REFUSED_ACT, - string("Permission denied, token type is ").append(to_string(tokenType))); + ret = BError(BError::Codes::SA_REFUSED_ACT); } break; } case Security::AccessToken::ATokenTypeEnum::TOKEN_HAP: { if (Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, BACKUP_PERMISSION) != Security::AccessToken::PermissionState::PERMISSION_GRANTED) { + HILOGE("Permission denied, token type is token hap"); std::string info = "Permission denied, token type is " + to_string(tokenType); PermissionCheckFailRadar(info, "VerifyCaller"); - throw BError(BError::Codes::SA_REFUSED_ACT, - string("Permission denied, token type is ").append(to_string(tokenType))); + ret = BError(BError::Codes::SA_REFUSED_ACT); } uint64_t fullTokenId = OHOS::IPCSkeleton::GetCallingFullTokenID(); if (!Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId)) { + HILOGE("Permission denied, token type is token hap, full tokenId is error"); std::string info = "Permission denied, token type is " + to_string(tokenType); PermissionCheckFailRadar(info, "VerifyCaller"); - throw BError(BError::Codes::SA_REFUSED_ACT, - string("Permission denied, token type is ").append(to_string(tokenType))); + ret = BError(BError::Codes::SA_REFUSED_ACT); } break; } case Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL: if (IPCSkeleton::GetCallingUid() != BConstants::SYSTEM_UID) { + HILOGE("Permission denied, token type is token shell"); std::string info = "invalid calling uid"; PermissionCheckFailRadar(info, "VerifyCaller"); - throw BError(BError::Codes::SA_REFUSED_ACT, "Calling uid is invalid"); + ret = BError(BError::Codes::SA_REFUSED_ACT); } break; default: std::string info = "Permission denied, token type is " + to_string(tokenType); PermissionCheckFailRadar(info, "VerifyCaller"); - throw BError(BError::Codes::SA_REFUSED_ACT, string("Invalid token type ").append(to_string(tokenType))); + HILOGE("Permission denied, token type is default"); + ret = BError(BError::Codes::SA_REFUSED_ACT); break; } + return ret; } -void Service::VerifyCaller(IServiceReverse::Scenario scenario) +ErrCode Service::VerifyCaller(IServiceReverse::Scenario scenario) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - session_->VerifyCallerAndScenario(IPCSkeleton::GetCallingTokenID(), scenario); - VerifyCaller(); + ErrCode ret = session_->VerifyCallerAndScenario(IPCSkeleton::GetCallingTokenID(), scenario); + if (ret != ERR_OK) { + HILOGE("Verify bundle by scenario failed, ret:%{public}d", ret); + return ret; + } + ErrCode errCode = VerifyCaller(); + return errCode; } ErrCode Service::InitRestoreSession(sptr remote) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); try { - VerifyCaller(); + ErrCode ret = VerifyCaller(); + if (ret != ERR_OK) { + HILOGE("Init restore session failed, verify caller failed"); + return ret; + } ErrCode errCode = session_->Active({ .clientToken = IPCSkeleton::GetCallingTokenID(), .scenario = IServiceReverse::Scenario::RESTORE, @@ -882,7 +902,11 @@ ErrCode Service::ServiceResultReport(const std::string restoreRetInfo, string callerName = ""; HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); try { - callerName = VerifyCallerAndGetCallerName(); + ErrCode ret = VerifyCallerAndGetCallerName(callerName); + if (ret != ERR_OK) { + HILOGE("Result report fail, ret:%{public}d", ret); + return ret; + } SendEndAppGalleryNotify(callerName); if (sennario == BackupRestoreScenario::FULL_RESTORE) { session_->GetServiceReverseProxy()->RestoreOnResultReport(restoreRetInfo, callerName, errCode); @@ -946,11 +970,7 @@ void Service::NotifyCloneBundleFinish(std::string bundleName, const BackupRestor return; } std::lock_guard lock(mutexPtr->callbackMutex); - auto backUpConnection = session_->GetExtConnection(bundleName); - if (backUpConnection == nullptr) { - throw BError(BError::Codes::SA_INVAL_ARG, "backUpConnection is empty"); - } - auto proxy = backUpConnection->GetBackupExtProxy(); + auto proxy = GetExtProxyByBundleName(bundleName); if (!proxy) { throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); } @@ -1004,12 +1024,7 @@ ErrCode Service::GetFileHandle(const string &bundleName, const string &fileName) } auto action = session_->GetServiceSchedAction(bundleName); if (action == BConstants::ServiceSchedAction::RUNNING) { - auto backUpConnection = session_->GetExtConnection(bundleName); - if (backUpConnection == nullptr) { - HILOGE("GetFileHandle error, backUpConnection is empty"); - return BError(BError::Codes::SA_INVAL_ARG); - } - auto proxy = backUpConnection->GetBackupExtProxy(); + auto proxy = GetExtProxyByBundleName(bundleName); if (!proxy) { HILOGE("GetFileHandle error, Extension backup Proxy is empty"); return BError(BError::Codes::SA_INVAL_ARG); @@ -1023,7 +1038,7 @@ ErrCode Service::GetFileHandle(const string &bundleName, const string &fileName) } session_->GetServiceReverseProxy()->RestoreOnFileReady(bundleName, fileName, move(fd), errCode); FileReadyRadarReport(bundleName, fileName, errCode, IServiceReverse::Scenario::RESTORE); - } else { + } else if (action != BConstants::ServiceSchedAction::UNKNOWN) { session_->SetExtFileNameRequest(bundleName, fileName); } return BError(BError::Codes::OK); @@ -1050,7 +1065,12 @@ void Service::OnBackupExtensionDied(const string &&bundleName, bool isCleanCalle try { string callName = move(bundleName); HILOGE("Backup <%{public}s> Extension Process Died", callName.c_str()); - session_->VerifyBundleName(callName); + ErrCode ret = session_->VerifyBundleName(callName); + if (ret != ERR_OK) { + HILOGE("Backup Extension died error, verify bundleName failed, bundleName:%{public}s, ret:%{public}d", + bundleName.c_str(), ret); + ExtConnectDied(bundleName); + } // 重新连接清理缓存 HILOGI("Clear backup extension data, bundleName: %{public}s", callName.c_str()); ExtConnectDied(callName); @@ -1076,8 +1096,9 @@ void Service::ExtConnectDied(const string &callName) session_->StopFwkTimer(callName); session_->StopExtTimer(callName); auto backUpConnection = session_->GetExtConnection(callName); - if (backUpConnection != nullptr && backUpConnection->IsExtAbilityConnected()) { - backUpConnection->DisconnectBackupExtAbility(); + auto backupConn = backUpConnection.lock(); + if (backupConn != nullptr && backupConn->IsExtAbilityConnected()) { + backupConn->DisconnectBackupExtAbility(); } session_->SetServiceSchedAction(callName, BConstants::ServiceSchedAction::CLEAN); auto ret = LaunchBackupExtension(callName); @@ -1108,11 +1129,7 @@ void Service::ExtStart(const string &bundleName) return; } IServiceReverse::Scenario scenario = session_->GetScenario(); - auto backUpConnection = session_->GetExtConnection(bundleName); - if (backUpConnection == nullptr) { - throw BError(BError::Codes::SA_INVAL_ARG, "ExtStart bundle task error, backUpConnection is empty"); - } - auto proxy = backUpConnection->GetBackupExtProxy(); + auto proxy = GetExtProxyByBundleName(bundleName); if (!proxy) { throw BError(BError::Codes::SA_INVAL_ARG, "ExtStart bundle task error, Extension backup Proxy is empty"); } @@ -1268,7 +1285,7 @@ void Service::ExtConnectDone(string bundleName) if (curSchedAction == BConstants::ServiceSchedAction::START && clearRecorder_->FindClearBundleRecord(bundleName)) { session_->SetServiceSchedAction(bundleName, BConstants::ServiceSchedAction::CLEAN); - } else { + } else if (curSchedAction != BConstants::ServiceSchedAction::UNKNOWN) { session_->SetServiceSchedAction(bundleName, BConstants::ServiceSchedAction::RUNNING); AddClearBundleRecord(bundleName); } @@ -1589,11 +1606,7 @@ ErrCode Service::ClearResidualBundleData(const std::string &bundleName) if (session_ == nullptr) { return BError(BError::Codes::SA_INVAL_ARG); } - auto backUpConnection = session_->GetExtConnection(bundleName); - if (backUpConnection == nullptr) { - throw BError(BError::Codes::SA_INVAL_ARG, "backUpConnection is empty"); - } - auto proxy = backUpConnection->GetBackupExtProxy(); + auto proxy = GetExtProxyByBundleName(bundleName); if (!proxy) { throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); } @@ -1690,7 +1703,13 @@ ErrCode Service::StartExtTimer(bool &isExtStart) return BError(BError::Codes::SA_INVAL_ARG); } session_->IncreaseSessionCnt(__PRETTY_FUNCTION__); - string bundleName = VerifyCallerAndGetCallerName(); + string bundleName; + ErrCode ret = VerifyCallerAndGetCallerName(bundleName); + if (ret != ERR_OK) { + HILOGE("Start extension timer fail, Get bundleName failed, ret:%{public}d", ret); + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + return ret; + } auto timeoutCallback = TimeOutCallback(wptr(this), bundleName); session_->StopFwkTimer(bundleName); isExtStart = session_->StartExtTimer(bundleName, timeoutCallback); @@ -1714,7 +1733,14 @@ ErrCode Service::StartFwkTimer(bool &isFwkStart) return BError(BError::Codes::SA_INVAL_ARG); } session_->IncreaseSessionCnt(__PRETTY_FUNCTION__); - string bundleName = VerifyCallerAndGetCallerName(); + std::string bundleName; + ErrCode ret = VerifyCallerAndGetCallerName(bundleName); + if (ret != ERR_OK) { + HILOGE("Start fwk timer fail, Get bundleName failed, ret:%{public}d", ret); + isFwkStart = false; + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + return ret; + } auto timeoutCallback = TimeOutCallback(wptr(this), bundleName); session_->StopExtTimer(bundleName); isFwkStart = session_->StartFwkTimer(bundleName, timeoutCallback); @@ -1780,7 +1806,13 @@ ErrCode Service::UpdateTimer(BundleName &bundleName, uint32_t timeout, bool &res return BError(BError::Codes::SA_INVAL_ARG); } session_->IncreaseSessionCnt(__PRETTY_FUNCTION__); - VerifyCaller(); + ErrCode ret = VerifyCaller(); + if (ret != ERR_OK) { + HILOGE("Update timer failed, verify caller failed"); + result = false; + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + return ret; + } auto timeoutCallback = TimeOutCallback(wptr(this), bundleName); result = session_->UpdateTimer(bundleName, timeout, timeoutCallback); session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); @@ -1795,7 +1827,6 @@ ErrCode Service::UpdateTimer(BundleName &bundleName, uint32_t timeout, bool &res ErrCode Service::UpdateSendRate(std::string &bundleName, int32_t sendRate, bool &result) { - try { HILOGI("Begin, bundle name:%{public}s, sendRate is:%{public}d", bundleName.c_str(), sendRate); if (session_ == nullptr || isOccupyingSession_.load()) { HILOGE("Update Send Rate error, session is empty."); @@ -1803,7 +1834,11 @@ ErrCode Service::UpdateSendRate(std::string &bundleName, int32_t sendRate, bool return BError(BError::Codes::SA_INVAL_ARG); } session_->IncreaseSessionCnt(__PRETTY_FUNCTION__); - VerifyCaller(); + ErrCode errCode = VerifyCaller(); + if (errCode != ERR_OK) { + HILOGE("Update send rate fail, verify caller failed, errCode:%{public}d", errCode); + return ret; + } IServiceReverse::Scenario scenario = session_ -> GetScenario(); if (scenario != IServiceReverse::Scenario::BACKUP) { HILOGE("This method is applicable to the backup scenario"); @@ -1811,10 +1846,12 @@ ErrCode Service::UpdateSendRate(std::string &bundleName, int32_t sendRate, bool session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); return BError(BError::Codes::SA_INVAL_ARG); } - auto backupConnection = session_->GetExtConnection(bundleName); - auto proxy = backupConnection->GetBackupExtProxy(); + auto proxy = GetExtProxyByBundleName(bundleName); if (!proxy) { - throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); + HILOGE("Update send rate fail, extension proxy is empty"); + result = false; + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + return BError(BError::Codes::SA_INVAL_ARG); } auto ret = proxy->UpdateFdSendRate(bundleName, sendRate); if (ret != NO_ERROR) { @@ -1825,12 +1862,6 @@ ErrCode Service::UpdateSendRate(std::string &bundleName, int32_t sendRate, bool result = true; session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); return BError(BError::Codes::OK); - } catch (...) { - result = false; - session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); - HILOGI("Unexpected exception"); - return EPERM; - } } AAFwk::Want Service::CreateConnectWant (BundleName &bundleName) @@ -1968,7 +1999,12 @@ ErrCode Service::ReportAppProcessInfo(const std::string processInfo, BackupResto { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); try { - string bundleName = VerifyCallerAndGetCallerName(); + string bundleName; + ErrCode ret = VerifyCallerAndGetCallerName(bundleName); + if (ret != ERR_OK) { + HILOGE("Report app process info failed, Get bundle name failed, ret:%{public}d", ret); + return ret; + } if (sennario == BackupRestoreScenario::FULL_RESTORE) { session_->GetServiceReverseProxy()->RestoreOnProcessInfo(bundleName, processInfo); } else if (sennario == BackupRestoreScenario::INCREMENTAL_RESTORE) { @@ -2051,6 +2087,10 @@ void Service::DoTimeout(wptr ptr, std::string bundleName) saConnection->DisconnectBackupSAExt(); } else { auto sessionConnection = sessionPtr->GetExtConnection(bundleName); + if (sessionConnection == nullptr) { + HILOGE("backup connection is nullptr"); + return; + } sessionConnection->DisconnectBackupExtAbility(); } sessionPtr->StopFwkTimer(bundleName); diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp index 8583f06e2b92fad6e2c470a4d64942aa1566fcdd..1c3e585b475667b05fc163984ab6918d3105be27 100644 --- a/services/backup_sa/src/module_ipc/service_incremental.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental.cpp @@ -52,6 +52,7 @@ #include "parameter.h" #include "system_ability_definition.h" #include "hitrace_meter.h" +#include "service.h" namespace OHOS::FileManagement::Backup { using namespace std; @@ -199,7 +200,7 @@ void Service::StartGetFdTask(std::string bundleName, wptr ptr) throw BError(BError::Codes::SA_INVAL_ARG, "session is nullptr"); } auto backUpConnection = session->GetExtConnection(bundleName); - if (backUpConnection == nullptr) { + if (backUpConnection.lock() == nullptr) { throw BError(BError::Codes::SA_INVAL_ARG, "backUpConnection is empty"); } auto proxy = backUpConnection->GetBackupExtProxy(); @@ -271,7 +272,13 @@ ErrCode Service::GetAppLocalListAndDoIncrementalBackup() } session_->IncreaseSessionCnt(__PRETTY_FUNCTION__); session_->SetSessionUserId(GetUserIdDefault()); - std::string bundleName = VerifyCallerAndGetCallerName(); + std::string bundleName; + ErrCode ret = VerifyCallerAndGetCallerName(bundleName); + if (ret != ERR_OK) { + HILOGE("Get AppLocalList failed, Get bundle failed, ret:%{public}d", ret); + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + return ret; + } auto task = [this, bundleName]() { StartGetFdTask(bundleName, wptr(this)); }; @@ -439,14 +446,9 @@ ErrCode Service::PublishIncrementalFile(const BFileInfo &fileInfo) if (session_ != nullptr) { session_->SetPublishFlag(fileInfo.owner); } - auto backUpConnection = session_->GetExtConnection(fileInfo.owner); - if (backUpConnection == nullptr) { - HILOGE("PublishIncrementalFile error, backUpConnection is empty"); - return BError(BError::Codes::SA_INVAL_ARG); - } - auto proxy = backUpConnection->GetBackupExtProxy(); + auto proxy = GetExtProxyByBundleName(fileInfo.owner); if (!proxy) { - HILOGE("PublishIncrementalFile error, Extension backup Proxy is empty"); + HILOGE("Publish Incremental file failed, bundleName:%{public}s", fileInfo.owner.c_str()); return BError(BError::Codes::SA_INVAL_ARG); } ErrCode res = proxy->PublishIncrementalFile(fileInfo.fileName); @@ -486,7 +488,12 @@ ErrCode Service::AppIncrementalFileReady(const std::string &fileName, UniqueFd f { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); try { - string callerName = VerifyCallerAndGetCallerName(); + std::string callerName; + ErrCode ret = VerifyCallerAndGetCallerName(callerName); + if (ret != ERR_OK) { + HILOGE("App increment file ready failed, ret:%{public}d", ret); + return ret; + } if (session_->GetScenario() == IServiceReverse::Scenario::RESTORE) { session_->GetServiceReverseProxy()->IncrementalRestoreOnFileReady(callerName, fileName, move(fd), move(manifestFd), errCode); @@ -496,7 +503,6 @@ ErrCode Service::AppIncrementalFileReady(const std::string &fileName, UniqueFd f if (fileName == BConstants::EXT_BACKUP_MANAGE) { fd = session_->OnBundleExtManageInfo(callerName, move(fd)); } - HILOGD("reverse: Will notify IncrementalBackupOnFileReady"); session_->GetServiceReverseProxy()->IncrementalBackupOnFileReady(callerName, fileName, move(fd), move(manifestFd), errCode); FileReadyRadarReport(callerName, fileName, errCode, IServiceReverse::Scenario::BACKUP); @@ -504,10 +510,10 @@ ErrCode Service::AppIncrementalFileReady(const std::string &fileName, UniqueFd f callerName, GetAnonyPath(fileName) }; HiAudit::GetInstance(true).Write(auditLog); if (session_->OnBundleFileReady(callerName, fileName)) { - auto backUpConnection = session_->GetExtConnection(callerName); - auto proxy = backUpConnection->GetBackupExtProxy(); + auto proxy = GetExtProxyByBundleName(callerName); if (!proxy) { - throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); + HILOGE("AppIncrementalFileReady error, bundleName:%{public}s", callerName.c_str()); + return BError(BError::Codes::SA_INVAL_ARG);; } // 通知extension清空缓存 proxy->HandleClear(); @@ -544,7 +550,12 @@ ErrCode Service::AppIncrementalDone(ErrCode errCode) HILOGE("AppIncrementalDone error, session is null"); return BError(BError::Codes::SA_INVAL_ARG); } - string callerName = VerifyCallerAndGetCallerName(); + string callerName; + ErrCode ret = VerifyCallerAndGetCallerName(callerName); + if (ret != ERR_OK) { + HILOGE("App incremental done fail, ret:%{public}d", ret); + return ret; + } HILOGI("Service AppIncrementalDone start, callerName is %{public}s, errCode is: %{public}d", callerName.c_str(), errCode); if (session_->OnBundleFileReady(callerName) || errCode != BError(BError::Codes::OK)) { @@ -554,14 +565,10 @@ ErrCode Service::AppIncrementalDone(ErrCode errCode) return BError(BError::Codes::SA_INVAL_ARG, "Extension mutex ptr is null."); } std::lock_guard lock(mutexPtr->callbackMutex); - auto tempBackUpConnection = session_->GetExtConnection(callerName); - auto backUpConnection = tempBackUpConnection.promote(); - if (backUpConnection == nullptr) { - return BError(BError::Codes::SA_INVAL_ARG, "Promote backUpConnection ptr is null."); - } - auto proxy = backUpConnection->GetBackupExtProxy(); + auto proxy = GetExtProxyByBundleName(callerName); if (!proxy) { - return BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); + HILOGE("AppIncrementalDone failed, bundleName:%{public}s", callerName.c_str()); + return BError(BError::Codes::SA_INVAL_ARG); } proxy->HandleClear(); session_->StopFwkTimer(callerName); @@ -595,15 +602,10 @@ ErrCode Service::GetIncrementalFileHandle(const std::string &bundleName, const s VerifyCaller(IServiceReverse::Scenario::RESTORE); auto action = session_->GetServiceSchedAction(bundleName); if (action == BConstants::ServiceSchedAction::RUNNING) { - auto backUpConnection = session_->GetExtConnection(bundleName); - if (backUpConnection == nullptr) { - HILOGE("GetIncrementalFileHandle error, backUpConnection is empty"); - return BError(BError::Codes::SA_INVAL_ARG); - } - auto proxy = backUpConnection->GetBackupExtProxy(); + auto proxy = GetExtProxyByBundleName(bundleName); if (!proxy) { - HILOGE("GetIncrementalFileHandle error, Extension backup Proxy is empty"); - return BError(BError::Codes::SA_INVAL_ARG); + HILOGE("GetIncrementalFileHandle failed, bundleName:%{public}s", bundleName.c_str()); + return errCode; } ErrCode res = proxy->GetIncrementalFileHandle(fileName); if (res != ERR_OK) { @@ -612,7 +614,7 @@ ErrCode Service::GetIncrementalFileHandle(const std::string &bundleName, const s AppRadar::GetInstance().RecordRestoreFuncRes(info, "Service::GetIncrementalFileHandle", GetUserIdDefault(), BizStageRestore::BIZ_STAGE_GET_FILE_HANDLE_FAIL, res); } - } else { + } else if (action != BConstants::ServiceSchedAction::UNKNOWN) { SvcRestoreDepsManager::GetInstance().UpdateToRestoreBundleMap(bundleName, fileName); session_->SetExtFileNameRequest(bundleName, fileName); } @@ -632,11 +634,7 @@ bool Service::IncrementalBackup(const string &bundleName) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); IServiceReverse::Scenario scenario = session_->GetScenario(); - auto backUpConnection = session_->GetExtConnection(bundleName); - if (backUpConnection == nullptr) { - throw BError(BError::Codes::SA_INVAL_ARG, "backUpConnection is empty"); - } - auto proxy = backUpConnection->GetBackupExtProxy(); + auto proxy = GetExtProxyByBundleName(bundleName); if (!proxy) { throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); } diff --git a/services/backup_sa/src/module_ipc/sub_service.cpp b/services/backup_sa/src/module_ipc/sub_service.cpp index 8bbdf24822fd1c8958c992402912632f43990c8f..bf8727e212c7adcecffa7a3e12f0038ea6241a3c 100644 --- a/services/backup_sa/src/module_ipc/sub_service.cpp +++ b/services/backup_sa/src/module_ipc/sub_service.cpp @@ -102,12 +102,7 @@ ErrCode Service::PublishFile(const BFileInfo &fileInfo) HILOGE("Forbit to use publishFile with fileName for App"); return EPERM; } - auto backUpConnection = session_->GetExtConnection(fileInfo.owner); - if (backUpConnection == nullptr) { - HILOGE("PublishFile error, backUpConnection is empty"); - return BError(BError::Codes::SA_INVAL_ARG); - } - auto proxy = backUpConnection->GetBackupExtProxy(); + auto proxy = GetExtProxyByBundleName(fileInfo.owner); if (!proxy) { HILOGE("PublishFile error, Extension backup Proxy is empty"); return BError(BError::Codes::SA_INVAL_ARG); @@ -136,9 +131,15 @@ ErrCode Service::AppFileReady(const string &fileName, UniqueFd fd, int32_t errCo HILOGE("AppFileReady error, session is empty"); return BError(BError::Codes::SA_INVAL_ARG); } - string callerName = VerifyCallerAndGetCallerName(); + string callerName; + ErrCode ret = VerifyCallerAndGetCallerName(callerName); + if (ret != ERR_OK) { + HILOGE("AppFileReady error, Get bundle name failed, ret:%{public}d", ret); + return ret; + } if (fileName.find('/') != string::npos) { - throw BError(BError::Codes::SA_INVAL_ARG, "Filename is not valid"); + HILOGE("AppFileReady error, Filename is not valid, fileName:%{public}s", GetAnonyPath(fileName).c_str()); + return BError(BError::Codes::SA_INVAL_ARG); } if (fileName == BConstants::EXT_BACKUP_MANAGE) { fd = session_->OnBundleExtManageInfo(callerName, move(fd)); @@ -149,12 +150,7 @@ ErrCode Service::AppFileReady(const string &fileName, UniqueFd fd, int32_t errCo callerName, GetAnonyPath(fileName) }; HiAudit::GetInstance(true).Write(auditLog); if (session_->OnBundleFileReady(callerName, fileName)) { - auto backUpConnection = session_->GetExtConnection(callerName); - if (backUpConnection == nullptr) { - HILOGE("AppFileReady error, backUpConnection is empty"); - return BError(BError::Codes::SA_INVAL_ARG); - } - auto proxy = backUpConnection->GetBackupExtProxy(); + auto proxy = GetExtProxyByBundleName(callerName); if (!proxy) { HILOGE("AppFileReady error, Extension backup Proxy is empty"); return BError(BError::Codes::SA_INVAL_ARG); @@ -175,9 +171,6 @@ ErrCode Service::AppFileReady(const string &fileName, UniqueFd fd, int32_t errCo return BError(BError::Codes::OK); } catch (const BError &e) { return e.GetCode(); // 任意异常产生,终止监听该任务 - } catch (const exception &e) { - HILOGI("Catched an unexpected low-level exception %{public}s", e.what()); - return EPERM; } catch (...) { HILOGI("Unexpected exception"); return EPERM; @@ -192,7 +185,12 @@ ErrCode Service::AppDone(ErrCode errCode) HILOGE("App finish error, session info is empty"); return BError(BError::Codes::SA_INVAL_ARG); } - string callerName = VerifyCallerAndGetCallerName(); + string callerName; + ErrCode ret = VerifyCallerAndGetCallerName(callerName); + if (ret != ERR_OK) { + HILOGE("App done failed, Get bundle name failed, ret:%{public}d", ret); + return ret; + } HILOGI("Begin, callerName is: %{public}s, errCode: %{public}d", callerName.c_str(), errCode); if (session_->OnBundleFileReady(callerName) || errCode != BError(BError::Codes::OK)) { std::shared_ptr mutexPtr = GetExtensionMutex(callerName); @@ -364,4 +362,19 @@ void Service::HandleNotSupportBundleNames(const std::vector &srcBun } } } + +sptr Service::GetExtProxyByBundleName(const std::string &bundleName) +{ + HILOGI("Begin, bundleName:%{public}s", bundleName.c_str()); + if (session_ == nullptr) { + HILOGE("Get bundle extension proxy fail, session is empty, bundle:%{public}s", bundleName.c_str()); + } + auto tempBackUpConnection = session_->GetExtConnection(callerName); + auto backUpConnection = tempBackUpConnection.promote(); + if (backUpConnection == nullptr) { + HILOGE("Get bundle extension proxy fail, backUpConnection is empty, bundle:%{public}s", bundleName.c_str()); + return nullptr; + } + return backUpConnection->GetBackupExtProxy(); +} } \ No newline at end of file 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 f47d3e151d3389145ec075492394fc9591adb615..7a6f297be2e3cbe0b4c5df1cb1c1378b66266e24 100644 --- a/services/backup_sa/src/module_ipc/svc_session_manager.cpp +++ b/services/backup_sa/src/module_ipc/svc_session_manager.cpp @@ -39,7 +39,7 @@ namespace OHOS::FileManagement::Backup { using namespace std; -void SvcSessionManager::VerifyCallerAndScenario(uint32_t clientToken, IServiceReverse::Scenario scenario) const +ErrCode SvcSessionManager::VerifyCallerAndScenario(uint32_t clientToken, IServiceReverse::Scenario scenario) const { shared_lock lock(lock_); if (impl_.scenario != scenario) { @@ -48,16 +48,17 @@ void SvcSessionManager::VerifyCallerAndScenario(uint32_t clientToken, IServiceRe AppRadar::GetInstance().RecordDefaultFuncRes(info, "SvcSessionManager::VerifyCallerAndScenario", impl_.userId, BizStageBackup::BIZ_STAGE_PERMISSION_CHECK_FAIL, BError(BError::Codes::SDK_MIXED_SCENARIO).GetCode()); - throw BError(BError::Codes::SDK_MIXED_SCENARIO); + return BError(BError::Codes::SDK_MIXED_SCENARIO); } if (impl_.clientToken != clientToken) { AppRadar::Info info2("", "", "Caller mismatched"); AppRadar::GetInstance().RecordDefaultFuncRes(info2, "SvcSessionManager::VerifyCallerAndScenario", impl_.userId, BizStageBackup::BIZ_STAGE_PERMISSION_CHECK_FAIL, BError(BError::Codes::SDK_MIXED_SCENARIO).GetCode()); - throw BError(BError::Codes::SA_REFUSED_ACT, "Caller mismatched"); + return BError(BError::Codes::SA_REFUSED_ACT, "Caller mismatched"); } HILOGD("Succeed to verify the caller"); + return BError(BError::Codes::OK); } SvcSessionManager::Impl SvcSessionManager::GetImpl() @@ -80,32 +81,40 @@ ErrCode SvcSessionManager::Active(Impl newImpl, bool isOccupyingSession) } if (!isOccupyingSession && !newImpl.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("Active session fail, No caller token was specified"); + return BError(BError::Codes::SA_INVAL_ARG); } if (!isOccupyingSession && newImpl.scenario == IServiceReverse::Scenario::UNDEFINED) { - throw BError(BError::Codes::SA_INVAL_ARG, "No scenario was specified"); + HILOGE("Active session fail, No scenario was specified"); + return BError(BError::Codes::SA_INVAL_ARG); } if (!isOccupyingSession) { - InitClient(newImpl); + ErrCode ret = InitClient(newImpl); + if (ret != BError(BError::Codes::OK)) { + HILOGE("Active session failed, init client error"); + return ret; + } } impl_ = newImpl; IncreaseSessionCnt(__PRETTY_FUNCTION__); return BError(BError::Codes::OK); } -void SvcSessionManager::Deactive(const wptr &remoteInAction, bool force) +ErrCode SvcSessionManager::Deactive(const wptr &remoteInAction, bool force) { unique_lock lock(lock_); if (!impl_.clientToken) { - HILOGI("Empty session"); - return; + HILOGE("Deactive session fail, caller token is invalid"); + return BError(BError::Codes::SA_INVAL_ARG); } if (!force && (!impl_.clientToken || !impl_.clientProxy)) { - return; + HILOGE("Deactive session fail, client proxy is invalid"); + return BError(BError::Codes::SA_INVAL_ARG); } if (!force && (remoteInAction != impl_.clientProxy->AsObject())) { - throw BError(BError::Codes::SA_INVAL_ARG, "Only the client actived the session can deactive it"); + HILOGE("Deactive session fail, Only the client actived the session can deactive it"); + return BError(BError::Codes::SA_INVAL_ARG); } deathRecipient_ = nullptr; @@ -121,21 +130,23 @@ void SvcSessionManager::Deactive(const wptr &remoteInAction, bool impl_ = {}; extConnectNum_ = 0; DecreaseSessionCnt(__PRETTY_FUNCTION__); + return BError(BError::Codes::OK); } -void SvcSessionManager::VerifyBundleName(string &bundleName) +ErrCode SvcSessionManager::VerifyBundleName(string &bundleName) { shared_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("Verify bundle name failed, No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return BError(BError::Codes::SA_INVAL_ARG); } auto asVerify = [&bundleName](const auto &it) { return it.first == bundleName; }; if (none_of(impl_.backupExtNameMap.begin(), impl_.backupExtNameMap.end(), asVerify)) { - stringstream ss; - ss << "Could not find the " << bundleName << " from current session"; - throw BError(BError::Codes::SA_REFUSED_ACT, ss.str()); + HILOGE("Could not find the bundle from current session, bundleName:%{public}s", bundleName.c_str()); + return BError(BError::Codes::SA_REFUSED_ACT); } HILOGD("Succeed to verify the bundleName"); + return BError(BError::Codes::OK); } sptr SvcSessionManager::GetServiceReverseProxy() @@ -151,7 +162,8 @@ IServiceReverse::Scenario SvcSessionManager::GetScenario() { shared_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("Get scenario failed, No caller token was specified"); + return IServiceReverse::Scenario::UNDEFINED; } return impl_.scenario; } @@ -170,16 +182,15 @@ bool SvcSessionManager::OnBundleFileReady(const string &bundleName, const string { unique_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("OnBundleFileReady failed, bundleName:%{public}s, fileName:%{public}s", bundleName.c_str(), + GetAnonyPath(fileName).c_str()); + return false; } - HILOGD("Begin, bundleName name is:%{public}s", bundleName.c_str()); - auto it = impl_.backupExtNameMap.find(bundleName); - if (it == impl_.backupExtNameMap.end()) { - stringstream ss; - ss << "Could not find the " << bundleName << " from current session"; - throw BError(BError::Codes::SA_REFUSED_ACT, ss.str()); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find the bundle:%{public}s", bundleName.c_str()); + return false; } - // 判断是否结束 通知EXTENTION清理资源 TOOL应用完成备份 if (impl_.scenario == IServiceReverse::Scenario::RESTORE || SAUtils::IsSABundleName(bundleName)) { it->second.isBundleFinished = true; @@ -206,10 +217,12 @@ bool SvcSessionManager::OnBundleFileReady(const string &bundleName, const string UniqueFd SvcSessionManager::OnBundleExtManageInfo(const string &bundleName, UniqueFd fd) { if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return UniqueFd(-EPERM); } if (impl_.scenario != IServiceReverse::Scenario::BACKUP) { - throw BError(BError::Codes::SA_INVAL_ARG, "Invalid Scenario"); + HILOGE("Invalid Scenario, bundleName:%{public}s", bundleName.c_str()); + return UniqueFd(-EPERM); } BJsonCachedEntity cachedEntity(move(fd)); @@ -217,12 +230,16 @@ UniqueFd SvcSessionManager::OnBundleExtManageInfo(const string &bundleName, Uniq auto info = cache.GetExtManage(); for (auto &fileName : info) { - HILOGE("fileName %{public}s", GetAnonyString(fileName).data()); + HILOGE("fileName %{public}s", GetAnonyPath(fileName).data()); OnBundleFileReady(bundleName, fileName); } unique_lock lock(lock_); - auto it = GetBackupExtNameMap(bundleName); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find the bundle:%{public}s", bundleName.c_str()); + return UniqueFd(-EPERM); + } it->second.receExtManageJson = true; return move(cachedEntity.GetFd()); } @@ -233,7 +250,7 @@ void SvcSessionManager::RemoveExtInfo(const string &bundleName) unique_lock lock(lock_); auto it = impl_.backupExtNameMap.find(bundleName); if (it == impl_.backupExtNameMap.end()) { - HILOGI("BackupExtNameMap not contain %{public}s", bundleName.c_str()); + HILOGE("BackupExtNameMap not contain %{public}s", bundleName.c_str()); return; } if (extConnectNum_) { @@ -252,12 +269,18 @@ wptr SvcSessionManager::GetExtConnection(const BundleName & HILOGD("svcMrg:GetExt, bundleName:%{public}s", bundleName.c_str()); shared_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return wptr(); } - auto it = GetBackupExtNameMap(bundleName); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find current bundle, bundleName:%{public}s", bundleName.c_str()); + return wptr(); + } if (!it->second.backUpConnection) { - throw BError(BError::Codes::SA_INVAL_ARG, "Svc backup connection is empty"); + HILOGE("Svc backup connection is empty, bundleName:%{public}s", bundleName.c_str()); + return wptr(); } return wptr(it->second.backUpConnection); @@ -268,12 +291,18 @@ std::weak_ptr SvcSessionManager::GetSAExtConnection(const Bu HILOGD("svcMrg:GetExt, bundleName:%{public}s", bundleName.c_str()); shared_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return std::weak_ptr(); } - auto it = GetBackupExtNameMap(bundleName); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find current bundle, bundleName:%{public}s", bundleName.c_str()); + return std::weak_ptr(); + } if (!it->second.saBackupConnection) { - throw BError(BError::Codes::SA_INVAL_ARG, "SA backup connection is empty"); + HILOGE("SA backup connection is empty, bundleName:%{public}s", bundleName.c_str()); + return std::weak_ptr(); } return std::weak_ptr(it->second.saBackupConnection); @@ -357,16 +386,17 @@ void SvcSessionManager::DumpInfo(const int fd, const std::vector dprintf(fd, "Scenario: %d\n", impl_.scenario); } -void SvcSessionManager::InitClient(Impl &newImpl) +ErrCode SvcSessionManager::InitClient(Impl &newImpl) { if (!newImpl.clientProxy) { - throw BError(BError::Codes::SA_INVAL_ARG, "Invalid client"); + HILOGE("Init client error, Invalid client"); + return BError(BError::Codes::SA_INVAL_ARG); } auto remoteObj = newImpl.clientProxy->AsObject(); if (!remoteObj) { - throw BError(BError::Codes::SA_BROKEN_IPC, "Proxy's remote object can't be nullptr"); + HILOGE("Init client error, Proxy's remote object can't be nullptr"); + return BError(BError::Codes::SA_BROKEN_IPC); } - auto callback = [revPtr {reversePtr_}](const wptr &obj) { HILOGI("Client died."); @@ -389,16 +419,21 @@ void SvcSessionManager::InitClient(Impl &newImpl) BizStageBackup::BIZ_STAGE_ACTIVE_SESSION, ERR_OK); } HILOGI("Succeed to active a session"); + return BError(BError::Codes::OK); } void SvcSessionManager::SetExtFileNameRequest(const string &bundleName, const string &fileName) { unique_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + 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 current bundle, bundleName:%{public}s", bundleName.c_str()); + return; } - - auto it = GetBackupExtNameMap(bundleName); it->second.fileNameInfo.insert(fileName); } @@ -406,27 +441,33 @@ std::set SvcSessionManager::GetExtFileNameRequest(const std::string { unique_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return std::set(); } if (impl_.scenario != IServiceReverse::Scenario::RESTORE) { - throw BError(BError::Codes::SA_INVAL_ARG, "Invalid Scenario"); + HILOGE("Invalid Scenario, bundleName:%{public}s", bundleName.c_str()); + return std::set(); + } + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return std::set(); } - auto it = GetBackupExtNameMap(bundleName); set fileNameInfo = it->second.fileNameInfo; it->second.fileNameInfo.clear(); return fileNameInfo; } -map::iterator SvcSessionManager::GetBackupExtNameMap(const string &bundleName) +std::tuple::iterator> SvcSessionManager::GetBackupExtNameMap( + const string &bundleName) { auto it = impl_.backupExtNameMap.find(bundleName); if (it == impl_.backupExtNameMap.end()) { - stringstream ss; - ss << "Could not find the " << bundleName << " from current session"; - throw BError(BError::Codes::SA_REFUSED_ACT, ss.str()); + HILOGE("Could not find the bundle from current session, bundleName:%{public}s", bundleName.c_str()); + return {false, impl_.backupExtNameMap.end()}; } - return it; + return {true, it}; } bool SvcSessionManager::GetSchedBundleName(string &bundleName) @@ -456,9 +497,14 @@ BConstants::ServiceSchedAction SvcSessionManager::GetServiceSchedAction(const st { shared_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return BConstants::ServiceSchedAction::UNKNOWN; + } + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return BConstants::ServiceSchedAction::UNKNOWN;; } - auto it = GetBackupExtNameMap(bundleName); return it->second.schedAction; } @@ -466,10 +512,14 @@ void SvcSessionManager::SetServiceSchedAction(const string &bundleName, BConstan { unique_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + 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; } - - auto it = GetBackupExtNameMap(bundleName); it->second.schedAction = action; if (it->second.schedAction == BConstants::ServiceSchedAction::START) { extConnectNum_++; @@ -480,10 +530,15 @@ void SvcSessionManager::SetBackupExtName(const string &bundleName, const string { unique_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return; } - auto it = GetBackupExtNameMap(bundleName); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return; + } it->second.backupExtName = backupExtName; } @@ -491,10 +546,15 @@ string SvcSessionManager::GetBackupExtName(const string &bundleName) { shared_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return ""; } - auto it = GetBackupExtNameMap(bundleName); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return ""; + } return it->second.backupExtName; } @@ -502,10 +562,15 @@ void SvcSessionManager::SetBackupExtInfo(const string &bundleName, const string { unique_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return; } - auto it = GetBackupExtNameMap(bundleName); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return; + } it->second.extInfo = extInfo; } @@ -513,10 +578,15 @@ std::string SvcSessionManager::GetBackupExtInfo(const string &bundleName) { shared_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return ""; } - auto it = GetBackupExtNameMap(bundleName); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return ""; + } return it->second.extInfo; } @@ -524,7 +594,8 @@ void SvcSessionManager::AppendBundles(const vector &bundleNames) { unique_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("AppendBundles error, No caller token was specified"); + return; } for (auto &&bundleName : bundleNames) { @@ -556,29 +627,34 @@ sptr SvcSessionManager::CreateBackupConnection(BundleName & return GetBackupAbilityExt(bundleName); } -void SvcSessionManager::Start() +ErrCode SvcSessionManager::Start() { unique_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("Start error, No caller token was specified"); + return BError(BError::Codes::SA_INVAL_ARG); } impl_.isBackupStart = true; + return BError(BError::Codes::OK); } -void SvcSessionManager::Finish() +ErrCode SvcSessionManager::Finish() { unique_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("Finish error, No caller token was specified"); + return BError(BError::Codes::SA_INVAL_ARG); } impl_.isAppendFinish = true; + return BError(BError::Codes::OK); } bool SvcSessionManager::IsOnAllBundlesFinished() { shared_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("IsOnAllBundlesFinished error, No caller token was specified"); + return false; } bool isAllBundlesFinished = !impl_.backupExtNameMap.size(); if (impl_.scenario == IServiceReverse::Scenario::RESTORE) { @@ -594,7 +670,8 @@ bool SvcSessionManager::IsOnOnStartSched() HILOGI("Begin"); shared_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("IsOnOnStartSched error, No caller token was specified"); + return false; } if (impl_.isBackupStart && impl_.backupExtNameMap.size()) { return true; @@ -622,10 +699,14 @@ void SvcSessionManager::SetBundleRestoreType(const std::string &bundleName, Rest { unique_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return; } - auto it = GetBackupExtNameMap(bundleName); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + } it->second.restoreType = restoreType; } @@ -633,10 +714,15 @@ RestoreTypeEnum SvcSessionManager::GetBundleRestoreType(const std::string &bundl { shared_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return RestoreTypeEnum::RESTORE_DATA_WAIT_SEND; } - auto it = GetBackupExtNameMap(bundleName); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return RestoreTypeEnum::RESTORE_DATA_WAIT_SEND; + } return it->second.restoreType; } @@ -644,10 +730,15 @@ void SvcSessionManager::SetBundleVersionCode(const std::string &bundleName, int6 { unique_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return; } - auto it = GetBackupExtNameMap(bundleName); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return; + } it->second.versionCode = versionCode; } @@ -655,10 +746,15 @@ int64_t SvcSessionManager::GetBundleVersionCode(const std::string &bundleName) { shared_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return -1; } - auto it = GetBackupExtNameMap(bundleName); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return -1; + } return it->second.versionCode; } @@ -666,10 +762,15 @@ void SvcSessionManager::SetBundleVersionName(const std::string &bundleName, std: { unique_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return; } - auto it = GetBackupExtNameMap(bundleName); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return; + } it->second.versionName = versionName; } @@ -677,10 +778,15 @@ std::string SvcSessionManager::GetBundleVersionName(const std::string &bundleNam { shared_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return ""; } - auto it = GetBackupExtNameMap(bundleName); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return ""; + } return it->second.versionName; } @@ -688,10 +794,15 @@ void SvcSessionManager::SetBundleDataSize(const std::string &bundleName, int64_t { unique_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return; } - auto it = GetBackupExtNameMap(bundleName); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return; + } it->second.dataSize = dataSize; } @@ -705,14 +816,14 @@ uint32_t SvcSessionManager::CalAppProcessTime(const std::string &bundleName) int64_t timeout; uint32_t resTimeoutMs; - try { - auto it = GetBackupExtNameMap(bundleName); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("CalAppProcessTime failed, BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + timeout = defaultTimeout; + } else { int64_t appSize = it->second.dataSize; /* timeout = (AppSize / 3Ms) * 3 + 30 */ timeout = defaultTimeout + (appSize / processRate) * multiple; - } catch (const BError &e) { - HILOGE("Failed to get app<%{public}s> dataInfo, err=%{public}d", bundleName.c_str(), e.GetCode()); - timeout = defaultTimeout; } timeout = timeout < minTimeout ? minTimeout : timeout; resTimeoutMs = (uint32_t)(timeout * invertMillisecond % UINT_MAX); /* conver second to millisecond */ @@ -729,7 +840,11 @@ bool SvcSessionManager::StartFwkTimer(const std::string &bundleName, const Utils HILOGE("No caller token was specified"); return false; } - auto it = GetBackupExtNameMap(bundleName); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("Start fwk timer failed, BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return false; + } if (it->second.fwkTimerStatus == true) { HILOGE("FwkTimer is registered, unregister first."); return false; @@ -750,7 +865,11 @@ bool SvcSessionManager::StopFwkTimer(const std::string &bundleName) HILOGE("No caller token was specified"); return false; } - auto it = GetBackupExtNameMap(bundleName); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("Stop fwk timer failed, BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return false; + } if (it->second.fwkTimerStatus == false) { HILOGE("FwkTimer is unregistered, register first."); return true; @@ -770,7 +889,11 @@ bool SvcSessionManager::StartExtTimer(const std::string &bundleName, const Utils HILOGE("No caller token was specified"); return false; } - auto it = GetBackupExtNameMap(bundleName); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("Start extension timer failed, BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return false; + } if (it->second.extTimerStatus == true) { HILOGE("ExtTimer is registered, unregister first."); return false; @@ -791,7 +914,11 @@ bool SvcSessionManager::StopExtTimer(const std::string &bundleName) HILOGE("No caller token was specified"); return false; } - auto it = GetBackupExtNameMap(bundleName); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("Stop extension timer failed, BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return false; + } if (it->second.extTimerStatus == false) { HILOGE("ExtTimer is unregistered, register first."); return true; @@ -813,7 +940,11 @@ bool SvcSessionManager::UpdateTimer(const std::string &bundleName, uint32_t time HILOGE("No caller token was specified"); return false; } - auto it = GetBackupExtNameMap(bundleName); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("Update timer failed, BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return false; + } it->second.timeout = timeout; if (it->second.extTimerStatus == false) { HILOGI("ExtTimer is unregistered, just store timeout %{public}u(ms)", timeout); @@ -886,7 +1017,8 @@ bool SvcSessionManager::GetIsIncrementalBackup() { unique_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("GetIsIncrementalBackup error, No caller token was specified"); + return false; } return impl_.isIncrementalBackup; } @@ -895,9 +1027,14 @@ void SvcSessionManager::SetIncrementalData(const BIncrementalData &incrementalDa { unique_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("SetIncrementalData error, No caller token was specified"); + return; + } + auto [findBundleSuc, it] = GetBackupExtNameMap(incrementalData.bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find bundle %{public}s", incrementalData.bundleName.c_str()); + return; } - auto it = GetBackupExtNameMap(incrementalData.bundleName); it->second.lastIncrementalTime = incrementalData.lastIncrementalTime; it->second.manifestFd = incrementalData.manifestFd; it->second.backupParameters = incrementalData.backupParameters; @@ -908,9 +1045,14 @@ int32_t SvcSessionManager::GetIncrementalManifestFd(const string &bundleName) { unique_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return -1; + } + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return -1; } - auto it = GetBackupExtNameMap(bundleName); return it->second.manifestFd; } @@ -918,9 +1060,14 @@ int64_t SvcSessionManager::GetLastIncrementalTime(const string &bundleName) { unique_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return -1; + } + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return -1; } - auto it = GetBackupExtNameMap(bundleName); return it->second.lastIncrementalTime; } @@ -938,19 +1085,30 @@ void SvcSessionManager::SetClearDataFlag(const std::string &bundleName, bool isC { unique_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + 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; } - auto it = GetBackupExtNameMap(bundleName); it->second.isClearData = isClearData; HILOGI("bundleName:%{public}s, set clear data flag:%{public}d.", bundleName.c_str(), isClearData); } + bool SvcSessionManager::GetClearDataFlag(const std::string &bundleName) { unique_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return true; + } + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return true; } - auto it = GetBackupExtNameMap(bundleName); return it->second.isClearData; } @@ -1014,9 +1172,14 @@ void SvcSessionManager::SetPublishFlag(const std::string &bundleName) { unique_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + 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; } - auto it = GetBackupExtNameMap(bundleName); it->second.isInPublishFile = true; HILOGE("Set PublishFile success, bundleName = %{public}s", bundleName.c_str()); } @@ -1030,9 +1193,14 @@ void SvcSessionManager::SetIsReadyLaunch(const std::string &bundleName) { unique_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + 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; } - auto it = GetBackupExtNameMap(bundleName); it->second.isReadyLaunch = true; HILOGE("SetIsReadyLaunch success, bundleName = %{public}s", bundleName.c_str()); } diff --git a/tests/mock/module_ipc/service_mock.cpp b/tests/mock/module_ipc/service_mock.cpp index 4f6a1d0d1ac5a80aab55729dbec2450ed6320054..bfa111d6cf1bd2dfda8b59416e6768eb26a5af81 100644 --- a/tests/mock/module_ipc/service_mock.cpp +++ b/tests/mock/module_ipc/service_mock.cpp @@ -37,9 +37,9 @@ UniqueFd Service::GetLocalCapabilities() void Service::StopAll(const wptr &obj, bool force) {} -string Service::VerifyCallerAndGetCallerName() +ErrCode Service::VerifyCallerAndGetCallerName(std::string &bundleName) { - return ""; + return BError(BError::Codes::OK); } ErrCode Service::InitRestoreSession(sptr remote) 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 59c9e2a34f772dd4f54c74602c122162c993c5ba..d891689db0bc806f80807e328a368035c8db7e8e 100644 --- a/tests/mock/module_ipc/src/svc_session_manager_mock.cpp +++ b/tests/mock/module_ipc/src/svc_session_manager_mock.cpp @@ -20,7 +20,10 @@ namespace OHOS::FileManagement::Backup { using namespace std; -void SvcSessionManager::VerifyCallerAndScenario(uint32_t, IServiceReverse::Scenario) const {} +ErrCode SvcSessionManager::VerifyCallerAndScenario(uint32_t, IServiceReverse::Scenario) +{ + return BError(BError::Codes::OK); +} ErrCode SvcSessionManager::Active(Impl newImpl, bool force) { @@ -29,7 +32,10 @@ ErrCode SvcSessionManager::Active(Impl newImpl, bool force) void SvcSessionManager::Deactive(const wptr &, bool) {} -void SvcSessionManager::VerifyBundleName(string &bundleName) {} +ErrCode SvcSessionManager::VerifyBundleName(string &bundleName) +{ + return BError(BError::Codes::OK); +} sptr SvcSessionManager::GetServiceReverseProxy() { @@ -74,9 +80,9 @@ std::set SvcSessionManager::GetExtFileNameRequest(const std::string return BSvcSessionManager::sessionManager->GetExtFileNameRequest(bundleName); } -map::iterator SvcSessionManager::GetBackupExtNameMap(const string &) +std::tuple::iterator> SvcSessionManager::GetBackupExtNameMap(const string &) { - return {}; + return {false, {}}; } bool SvcSessionManager::GetSchedBundleName(string &bundleName) diff --git a/tests/mock/module_ipc/svc_session_manager_mock.cpp b/tests/mock/module_ipc/svc_session_manager_mock.cpp index 257ad09c2a3b0802335f4c2b5666684dabe7b210..b430f3537eb3251143665bf47cbe1cf577ca90ae 100644 --- a/tests/mock/module_ipc/svc_session_manager_mock.cpp +++ b/tests/mock/module_ipc/svc_session_manager_mock.cpp @@ -36,9 +36,10 @@ static int32_t g_nFileReadyNum = 0; static int32_t g_nAllBundlesFinished = 0; } // namespace -void SvcSessionManager::VerifyCallerAndScenario(uint32_t clientToken, IServiceReverse::Scenario scenario) const +ErrCode SvcSessionManager::VerifyCallerAndScenario(uint32_t clientToken, IServiceReverse::Scenario scenario) const { GTEST_LOG_(INFO) << "VerifyCallerAndScenario"; + return BError(BError::Codes::OK); } ErrCode SvcSessionManager::Active(Impl newImpl, bool force) @@ -54,9 +55,10 @@ void SvcSessionManager::Deactive(const wptr &remoteInAction, bool GTEST_LOG_(INFO) << "Deactive"; } -void SvcSessionManager::VerifyBundleName(string &bundleName) +ErrCode SvcSessionManager::VerifyBundleName(string &bundleName) { GTEST_LOG_(INFO) << "VerifyBundleName " << bundleName; + return BError(BError::Codes::OK); } sptr SvcSessionManager::GetServiceReverseProxy() @@ -123,7 +125,7 @@ void SvcSessionManager::DumpInfo(const int fd, const std::vector GTEST_LOG_(INFO) << "DumpInfo"; } -void SvcSessionManager::InitClient(Impl &newImpl) +ErrCode SvcSessionManager::InitClient(Impl &newImpl) { GTEST_LOG_(INFO) << "InitClient"; } @@ -142,11 +144,16 @@ std::set SvcSessionManager::GetExtFileNameRequest(const std::string return fileNameInfo; } -map::iterator SvcSessionManager::GetBackupExtNameMap(const string &bundleName) +std::tuple::iterator> SvcSessionManager::GetBackupExtNameMap( + const string &bundleName) { GTEST_LOG_(INFO) << "GetBackupExtNameMap"; auto it = impl_.backupExtNameMap.find(bundleName); - return it; + if (it == impl_.backupExtNameMap.end()) { + HILOGE("Could not find the bundle from current session, bundleName:%{public}s", bundleName.c_str()); + return {false, impl_.backupExtNameMap.end()}; + } + return {true, it}; } bool SvcSessionManager::GetSchedBundleName(string &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 8a0b29390d8c1c10d91c8847f9ae5db9059b1ebf..be3f0412c75f18720501f09dc35b9b7dc86a4579 100644 --- a/tests/mock/module_ipc/svc_session_manager_throw_mock.cpp +++ b/tests/mock/module_ipc/svc_session_manager_throw_mock.cpp @@ -19,7 +19,7 @@ namespace OHOS::FileManagement::Backup { using namespace std; -void SvcSessionManager::VerifyCallerAndScenario(uint32_t clientToken, IServiceReverse::Scenario scenario) const +ErrCode SvcSessionManager::VerifyCallerAndScenario(uint32_t clientToken, IServiceReverse::Scenario scenario) const { BackupSvcSessionManager::session->VerifyCallerAndScenario(clientToken, scenario); } @@ -34,7 +34,7 @@ void SvcSessionManager::Deactive(const wptr &remoteInAction, bool BackupSvcSessionManager::session->Deactive(remoteInAction, force); } -void SvcSessionManager::VerifyBundleName(string &bundleName) +ErrCode SvcSessionManager::VerifyBundleName(string &bundleName) { BackupSvcSessionManager::session->VerifyBundleName(bundleName); } @@ -94,7 +94,8 @@ std::set SvcSessionManager::GetExtFileNameRequest(const std::string return BackupSvcSessionManager::session->GetExtFileNameRequest(bundleName); } -map::iterator SvcSessionManager::GetBackupExtNameMap(const string &bundleName) +std::tuple::iterator> SvcSessionManager::GetBackupExtNameMap( + const string &bundleName) { return BackupSvcSessionManager::session->GetBackupExtNameMap(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 cb9904f7329a83785fa12f0decf9cf32b2871715..e1efbd2bfb04888ae97d4c9fd3304fa2ce1fff31 100644 --- a/tests/mock/module_ipc/svc_session_manager_throw_mock.h +++ b/tests/mock/module_ipc/svc_session_manager_throw_mock.h @@ -27,7 +27,7 @@ class BackupSvcSessionManager { public: virtual ~BackupSvcSessionManager() = default; public: - virtual void VerifyCallerAndScenario(uint32_t, IServiceReverse::Scenario) = 0; + virtual ErrCode VerifyCallerAndScenario(uint32_t, IServiceReverse::Scenario) = 0; virtual ErrCode Active(SvcSessionManager::Impl) = 0; virtual void Deactive(const wptr &, bool) = 0; virtual void VerifyBundleName(std::string &) = 0; @@ -42,7 +42,8 @@ public: virtual void InitClient(SvcSessionManager::Impl &) = 0; virtual void SetExtFileNameRequest(const std::string &, const std::string &) = 0; virtual std::set GetExtFileNameRequest(const std::string &) = 0; - virtual std::map::iterator GetBackupExtNameMap(const std::string &) = 0; + virtual std::tuple::iterator> GetBackupExtNameMap( + const std::string &) = 0; virtual bool GetSchedBundleName(std::string &) = 0; virtual BConstants::ServiceSchedAction GetServiceSchedAction(const std::string &) = 0; virtual void SetServiceSchedAction(const std::string &, BConstants::ServiceSchedAction) = 0; 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 9d43383b8e3e1b502305aba3dc479777e98a53a2..94ae18fa64730289d92383108dd63208f56d5e67 100644 --- a/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp @@ -42,9 +42,9 @@ UniqueFd Service::GetLocalCapabilities() void Service::StopAll(const wptr &obj, bool force) {} -string Service::VerifyCallerAndGetCallerName() +ErrCode Service::VerifyCallerAndGetCallerName(std::string &bundleName) { - return ""; + return BError(BError::Codes::OK); } ErrCode Service::InitRestoreSession(sptr remote) 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 e59fd30f24339c48c6c98f56986a78185b006325..3586dfcae96381fedd4d8786d0ea6d7bbcd4cfa9 100644 --- a/tests/unittests/backup_sa/module_ipc/service_other_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_other_test.cpp @@ -479,25 +479,26 @@ HWTEST_F(ServiceTest, SUB_Service_VerifyCallerAndGetCallerName_0100, TestSize.Le GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_VerifyCallerAndGetCallerName_0100"; try { ASSERT_TRUE(service != nullptr); + std::string callerName; EXPECT_CALL(*skeleton, GetCallingTokenID()).WillOnce(Return(0)); EXPECT_CALL(*token, GetTokenType(_)).WillOnce(Return(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP)); EXPECT_CALL(*token, GetHapTokenInfo(_, _)).WillOnce(Return(-1)); EXPECT_CALL(*param, GetBackupDebugOverrideAccount()) .WillOnce(Return(make_pair(true, DEBUG_ID + 1))); - EXPECT_THROW(service->VerifyCallerAndGetCallerName(), BError); + EXPECT_THROW(service->VerifyCallerAndGetCallerName(callerName), BError); EXPECT_CALL(*skeleton, GetCallingTokenID()).WillOnce(Return(0)); EXPECT_CALL(*token, GetTokenType(_)).WillOnce(Return(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP)); EXPECT_CALL(*token, GetHapTokenInfo(_, _)).WillOnce(Return(0)); EXPECT_CALL(*jsonUtil, BuildBundleNameIndexInfo(_, _)).WillOnce(Return("")); - auto ret = service->VerifyCallerAndGetCallerName(); + auto ret = service->VerifyCallerAndGetCallerName(callerName); EXPECT_TRUE(ret.empty()); EXPECT_CALL(*skeleton, GetCallingTokenID()).WillOnce(Return(0)); EXPECT_CALL(*token, GetTokenType(_)).WillOnce(Return(Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL)); EXPECT_CALL(*param, GetBackupDebugOverrideAccount()) .WillOnce(Return(make_pair(true, DEBUG_ID + 1))); - EXPECT_THROW(service->VerifyCallerAndGetCallerName(), BError); + EXPECT_THROW(service->VerifyCallerAndGetCallerName(callerName), BError); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by VerifyCallerAndGetCallerName."; diff --git a/utils/include/b_resources/b_constants.h b/utils/include/b_resources/b_constants.h index fe7934006af567336cd85b49f86762f2645b64ec..a99bb374228e12835ef514a92e6370c19d373d72 100644 --- a/utils/include/b_resources/b_constants.h +++ b/utils/include/b_resources/b_constants.h @@ -44,6 +44,7 @@ enum ServiceSchedAction { RUNNING = 2, FINISH = 3, CLEAN = 4, + UNKNOWN = 5, }; constexpr int SPAN_USERID_UID = 200000;