diff --git a/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp b/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp index 42f9bea125e5fdbef2e5ccb35e32f8e806cbf1db..3e6a6f5fdd1f0d92b1de7142ac479d2d4bc50ebc 100644 --- a/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp +++ b/frameworks/native/backup_kit_inner/src/b_incremental_restore_session.cpp @@ -69,6 +69,38 @@ unique_ptr BIncrementalRestoreSession::Init(Callback return nullptr; } +unique_ptr BIncrementalRestoreSession::Init(Callbacks callbacks, + std::string &errMsg, ErrCode &errCode) +{ + try { + HILOGI("Init IncrementalRestoreSession Begin"); + auto restore = make_unique(); + ServiceProxy::InvaildInstance(); + auto proxy = ServiceProxy::GetInstance(); + if (proxy == nullptr) { + errMsg = "Failed to get backup service"; + errCode = BError(BError::Codes::SDK_BROKEN_IPC); + HILOGE("Init IncrementalRestoreSession failed, %{public}s", errMsg.c_str()); + return nullptr; + } + errCode = proxy->InitRestoreSession(sptr(new ServiceReverse(callbacks)), errMsg); + if (errCode != ERR_OK) { + HILOGE("Failed to Restore because of %{public}d", errCode); + AppRadar::Info info ("", "", "create restore session failed"); + AppRadar::GetInstance().RecordRestoreFuncRes(info, "BIncrementalRestoreSession::Init", + AppRadar::GetInstance().GetUserId(), BizStageRestore::BIZ_STAGE_CREATE_RESTORE_SESSION_FAIL, errCode); + return nullptr; + } + + restore->RegisterBackupServiceDied(callbacks.onBackupServiceDied); + return restore; + } catch (const exception &e) { + HILOGE("Failed to Restore because of %{public}s", e.what()); + errCode = BError(BError::Codes::SDK_INVAL_ARG); + } + return nullptr; +} + ErrCode BIncrementalRestoreSession::PublishFile(BFileInfo fileInfo) { auto proxy = ServiceProxy::GetInstance(); diff --git a/frameworks/native/backup_kit_inner/src/service_proxy.cpp b/frameworks/native/backup_kit_inner/src/service_proxy.cpp index 2e1ecb6bb653c539f26769bd315a904f895904f3..6bf48c5bd05e1ed49f9382aa4e74f2b35c1473fe 100644 --- a/frameworks/native/backup_kit_inner/src/service_proxy.cpp +++ b/frameworks/native/backup_kit_inner/src/service_proxy.cpp @@ -56,6 +56,36 @@ ErrCode ServiceProxy::InitRestoreSession(sptr remote) return reply.ReadInt32(); } +ErrCode ServiceProxy::InitRestoreSession(sptr remote, std::string &errMsg) +{ + HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr"); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode(); + } + MessageParcel reply; + MessageOption option; + + if (!remote) { + return BError(BError::Codes::SDK_INVAL_ARG, "Empty reverse stub").GetCode(); + } + if (!data.WriteRemoteObject(remote->AsObject().GetRefPtr())) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the reverse stub").GetCode(); + } + + int32_t ret = Remote()->SendRequest( + static_cast(IServiceInterfaceCode::SERVICE_CMD_INIT_RESTORE_SESSION_MSG), data, reply, option); + if (ret != NO_ERROR) { + string str = "Failed to send out the request because of " + to_string(ret); + return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode(); + } + if (!reply.ReadString(errMsg)) { + return BError(BError::Codes::SDK_INVAL_ARG, "Failed to receive the errMsg").GetCode(); + } + return reply.ReadInt32(); +} + ErrCode ServiceProxy::InitBackupSession(sptr remote) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_restore_session.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_restore_session.h index 502fd1a9c39a382a51c9b6965c97a12216ab2ccb..9f1b0b9fddfc8d8124a9cd58c16108b16b8fef24 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_restore_session.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_incremental_restore_session.h @@ -49,6 +49,17 @@ public: */ static std::unique_ptr Init(Callbacks callbacks); + /** + * @brief 获取一个用于控制恢复流程的会话 + * + * @param callbacks 注册的回调函数 + * @param errMsg 失败信息 + * @param errCode 错误码 + * @return std::unique_ptr 指向BRestoreSession的智能指针。失败时为空指针 + */ + static std::unique_ptr Init(Callbacks callbacks, + std::string &errMsg, ErrCode &errCode); + /** * @brief 通知备份服务文件内容已就绪 * diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/i_service.h b/interfaces/inner_api/native/backup_kit_inner/impl/i_service.h index 7e0684a6a723416ec55d128fcd7ce56935953b92..f722041a7f7314e2a8d1bcae0414b12bffae6cf8 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/i_service.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/i_service.h @@ -46,6 +46,7 @@ class IService : public IRemoteBroker { public: virtual ~IService() = default; virtual ErrCode InitRestoreSession(sptr remote) = 0; + virtual ErrCode InitRestoreSession(sptr remote, std::string &errMsg) = 0; virtual ErrCode InitBackupSession(sptr remote) = 0; virtual ErrCode Start() = 0; virtual UniqueFd GetLocalCapabilities() = 0; diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/i_service_ipc_interface_code.h b/interfaces/inner_api/native/backup_kit_inner/impl/i_service_ipc_interface_code.h index 25cd58312e7522f43885a380c54174b9c5ab5e46..6d787fca04a5eee4948bc8df3a8faca75ab96747 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/i_service_ipc_interface_code.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/i_service_ipc_interface_code.h @@ -20,6 +20,7 @@ namespace OHOS::FileManagement::Backup { enum class IServiceInterfaceCode { SERVICE_CMD_INIT_RESTORE_SESSION, + SERVICE_CMD_INIT_RESTORE_SESSION_MSG, SERVICE_CMD_INIT_BACKUP_SESSION, SERVICE_CMD_GET_LOCAL_CAPABILITIES, SERVICE_CMD_PUBLISH_FILE, diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/service_proxy.h b/interfaces/inner_api/native/backup_kit_inner/impl/service_proxy.h index 4df70eea8efc539cbf4da73f98be0de75fe63e02..68eb9f4aaaeb4c36711c1f4f2639841cde6040c6 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/service_proxy.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/service_proxy.h @@ -29,6 +29,7 @@ namespace OHOS::FileManagement::Backup { class ServiceProxy : public IRemoteProxy { public: ErrCode InitRestoreSession(sptr remote) override; + ErrCode InitRestoreSession(sptr remote, std::string &errMsg) override; ErrCode InitBackupSession(sptr remote) override; ErrCode Start() override; UniqueFd GetLocalCapabilities() override; diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.cpp b/interfaces/kits/js/backup/session_restore_n_exporter.cpp index 93fc3e6c9886ea16a7ffbf2f174f1836d9da3fa7..d876b46126ca43ce7c39760b776e74e7763dff73 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_restore_n_exporter.cpp @@ -446,6 +446,8 @@ napi_value SessionRestoreNExporter::Constructor(napi_env env, napi_callback_info auto restoreEntity = std::make_unique(); restoreEntity->callbacks = make_shared(env, ptr, callbacks); restoreEntity->sessionWhole = nullptr; + ErrCode errCode; + std::string errMsg; restoreEntity->sessionSheet = BIncrementalRestoreSession::Init(BIncrementalRestoreSession::Callbacks { .onFileReady = bind(OnFileReadySheet, restoreEntity->callbacks, placeholders::_1, placeholders::_2, placeholders::_3, placeholders::_4), @@ -454,9 +456,12 @@ napi_value SessionRestoreNExporter::Constructor(napi_env env, napi_callback_info .onAllBundlesFinished = bind(onAllBundlesEnd, restoreEntity->callbacks, placeholders::_1), .onResultReport = bind(OnResultReport, restoreEntity->callbacks, placeholders::_1, placeholders::_2), .onBackupServiceDied = bind(OnBackupServiceDied, restoreEntity->callbacks), - .onProcess = bind(OnProcess, restoreEntity->callbacks, placeholders::_1, placeholders::_2)}); + .onProcess = bind(OnProcess, restoreEntity->callbacks, placeholders::_1, placeholders::_2)}, errMsg, errCode); if (!restoreEntity->sessionSheet) { - NError(BError(BError::Codes::SDK_INVAL_ARG, "Failed to init restore").GetCode()).ThrowErr(env); + std::tuple errInfo = + std::make_tuple(errCode, BError::GetBackupMsgByErrno(errCode) + ", " + errMsg); + ErrParam errorParam = [ errInfo ]() { return errInfo;}; + NError(errorParam).ThrowErr(env); return nullptr; } if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(restoreEntity))) { diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index 118cf031e6d9a175fa4fa5f4bee826324129c4e9..10c5d20221d0bc2b082afaedb3f7a59455b6e3ff 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -49,6 +49,7 @@ class Service : public SystemAbility, public ServiceStub, protected NoCopyable { // 以下都是IPC接口 public: ErrCode InitRestoreSession(sptr remote) override; + ErrCode InitRestoreSession(sptr remote, std::string &errMsg) override; ErrCode InitBackupSession(sptr remote) override; ErrCode Start() override; UniqueFd GetLocalCapabilities() override; @@ -317,12 +318,26 @@ public: }; private: + /** + * @brief 获取用户id + * + * @return int32_t + */ + int32_t GetUserIdDefault(); + /** * @brief 验证调用者 * */ void VerifyCaller(); + /** + * @brief 获取调用者名称 + * + * @return std::string + */ + std::string GetCallerName(); + /** * @brief 验证调用者 * @@ -546,6 +561,10 @@ private: void ExtensionConnectFailRadarReport(const std::string &bundleName, const ErrCode errCode, const IServiceReverse::Scenario scenario); + void OnStartResRadarReport(const std::vector &bundleNameList, int32_t stage); + + void PermissionCheckFailRadar(const std::string &info, const std::string &func); + void UpdateFailedBundles(const std::string &bundleName, BundleTaskInfo taskInfo); void ClearFailedBundles(); diff --git a/services/backup_sa/include/module_ipc/service_stub.h b/services/backup_sa/include/module_ipc/service_stub.h index 6cabc844b59658ae68da14ec6e468a746c9dfc0b..be78894b5848a5b02567153e2f2d3569a6196e3c 100644 --- a/services/backup_sa/include/module_ipc/service_stub.h +++ b/services/backup_sa/include/module_ipc/service_stub.h @@ -35,6 +35,7 @@ private: std::map opToInterfaceMap_; int32_t CmdInitRestoreSession(MessageParcel &data, MessageParcel &reply); + int32_t CmdInitRestoreSessionMsg(MessageParcel &data, MessageParcel &reply); int32_t CmdInitBackupSession(MessageParcel &data, MessageParcel &reply); int32_t CmdStart(MessageParcel &data, MessageParcel &reply); int32_t CmdGetLocalCapabilities(MessageParcel &data, MessageParcel &reply); 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 1d63fdf2bdecd8e7b63a89adf5015e705c1acbcc..92584949efb1837cccb6bdb0a2c813b653645f45 100644 --- a/services/backup_sa/include/module_ipc/svc_session_manager.h +++ b/services/backup_sa/include/module_ipc/svc_session_manager.h @@ -92,6 +92,8 @@ public: RestoreTypeEnum restoreDataType {RESTORE_DATA_WAIT_SEND}; bool isIncrementalBackup {false}; std::string oldBackupVersion {""}; + std::string callerName {}; + std::string activeTime {}; }; public: @@ -159,6 +161,20 @@ public: */ void SetSessionUserId(int32_t userId); + /** + * @brief 获取当前处理事务会话对应的callerName + * + * @return string + */ + std::string GetSessionCallerName(); + + /** + * @brief 获取当前处理事务会话对应的激活时间 + * + * @return string + */ + std::string GetSessionActiveTime(); + /** * @brief 更新backupExtNameMap并判断是否完成分发 * diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 330ca702161e1f494712bc78c92ae38c34e6f150..142bc72b6c628bb8705179587b200f2217d8e69e 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -90,7 +90,7 @@ const int32_t MAX_TRY_CLEAR_DISPOSE_NUM = 3; } // namespace /* Shell/Xts user id equal to 0/1, we need set default 100 */ -static inline int32_t GetUserIdDefault() +int32_t Service::GetUserIdDefault() { auto [isDebug, debugId] = BackupPara().GetBackupDebugOverrideAccount(); if (isDebug && debugId > DEBUG_ID) { @@ -113,7 +113,7 @@ void Service::GetOldDeviceBackupVersion() HILOGI("backupVersion of old device = %{public}s", oldBackupVersion.c_str()); } -void OnStartResRadarReport(const std::vector &bundleNameList, int32_t stage) +void Service::OnStartResRadarReport(const std::vector &bundleNameList, int32_t stage) { std::stringstream ss; ss << "failedBundles:{"; @@ -241,6 +241,8 @@ void Service::OnStart() .scenario = IServiceReverse::Scenario::CLEAN, .clientProxy = nullptr, .userId = GetUserIdDefault(), + .callerName = "BackupSA", + .activeTime = TimeUtils::GetCurrentTime(), }, isOccupyingSession_.load()); HILOGE("SA OnStart, cleaning up backup data"); @@ -357,7 +359,7 @@ void Service::StopAll(const wptr &obj, bool force) session_->Deactive(obj, force); } -static inline void PermissionCheckFailRadar(const std::string &info, const std::string &func) +void Service::PermissionCheckFailRadar(const std::string &info, const std::string &func) { std::string funcPos = "Service::"; AppRadar::Info resInfo("", "", info); @@ -455,6 +457,8 @@ ErrCode Service::InitRestoreSession(sptr remote) .scenario = IServiceReverse::Scenario::RESTORE, .clientProxy = remote, .userId = GetUserIdDefault(), + .callerName = GetCallerName(), + .activeTime = TimeUtils::GetCurrentTime(), }); if (errCode == 0) { ClearFailedBundles(); diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp index 41a059d2e564bb7b3c1236de8d1f9a70d3b67d3a..519e3622a5a397a84819c24ae8408c4fdc0e7537 100644 --- a/services/backup_sa/src/module_ipc/service_incremental.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental.cpp @@ -58,26 +58,11 @@ using namespace std; const std::string FILE_BACKUP_EVENTS = "FILE_BACKUP_EVENTS"; namespace { -constexpr int32_t DEBUG_ID = 100; constexpr int32_t INDEX = 3; constexpr int32_t MS_1000 = 1000; const static string UNICAST_TYPE = "unicast"; } // namespace -static inline int32_t GetUserIdDefault() -{ - HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - auto [isDebug, debugId] = BackupPara().GetBackupDebugOverrideAccount(); - if (isDebug && debugId > DEBUG_ID) { - return debugId; - } - auto multiuser = BMultiuser::ParseUid(IPCSkeleton::GetCallingUid()); - if ((multiuser.userId == BConstants::SYSTEM_UID) || (multiuser.userId == BConstants::XTS_UID)) { - return BConstants::DEFAULT_USER_ID; - } - return multiuser.userId; -} - ErrCode Service::Release() { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); diff --git a/services/backup_sa/src/module_ipc/service_stub.cpp b/services/backup_sa/src/module_ipc/service_stub.cpp index bc5fb76a074ce6f7afeb6ec181a47eb87cc22928..c16c25eda340858fbdf4200fcdf6e82d758bcb86 100644 --- a/services/backup_sa/src/module_ipc/service_stub.cpp +++ b/services/backup_sa/src/module_ipc/service_stub.cpp @@ -52,6 +52,8 @@ void ServiceStub::ServiceStubSupplement() &ServiceStub::CmdStartFwkTimer; opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_CANCEL_BUNDLE)] = &ServiceStub::CmdCancel; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_INIT_RESTORE_SESSION_MSG)] = + &ServiceStub::CmdInitRestoreSessionMsg; } void ServiceStub::ServiceStubSuppAppendBundles() @@ -152,6 +154,29 @@ int32_t ServiceStub::CmdInitRestoreSession(MessageParcel &data, MessageParcel &r return BError(BError::Codes::OK); } +int32_t ServiceStub::CmdInitRestoreSessionMsg(MessageParcel &data, MessageParcel &reply) +{ + auto remote = data.ReadRemoteObject(); + std::string errMsg; + if (!remote) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the stub"); + } + auto iremote = iface_cast(remote); + if (!iremote) { + return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub"); + } + int32_t res = InitRestoreSession(iremote, errMsg); + if (!reply.WriteString(errMsg)) { + return BError(BError::Codes::SA_BROKEN_IPC, "Failed to send the errMsg"); + } + if (!reply.WriteInt32(res)) { + stringstream ss; + ss << "Failed to send the result " << res; + return BError(BError::Codes::SA_BROKEN_IPC, ss.str()); + } + return BError(BError::Codes::OK); +} + int32_t ServiceStub::CmdInitBackupSession(MessageParcel &data, MessageParcel &reply) { auto remote = data.ReadRemoteObject(); diff --git a/services/backup_sa/src/module_ipc/sub_service.cpp b/services/backup_sa/src/module_ipc/sub_service.cpp index be07397ba93f68b8f6037c422254bc4e2ed1b204..cf86805c57fe38135f291c834edf79b71df6bba7 100644 --- a/services/backup_sa/src/module_ipc/sub_service.cpp +++ b/services/backup_sa/src/module_ipc/sub_service.cpp @@ -45,6 +45,7 @@ #include "b_radar/b_radar.h" #include "b_resources/b_constants.h" #include "b_sa/b_sa_utils.h" +#include "b_utils/b_time.h" #include "bundle_mgr_client.h" #include "filemgmt_libhilog.h" #include "hisysevent.h" @@ -369,4 +370,76 @@ void Service::HandleNotSupportBundleNames(const std::vector &srcBun } } } + +std::string Service::GetCallerName() +{ + std::string callerName; + uint32_t tokenCaller = IPCSkeleton::GetCallingTokenID(); + int tokenType = Security::AccessToken::AccessTokenKit::GetTokenType(tokenCaller); + switch (tokenType) { + case Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE: { /* Update Service */ + Security::AccessToken::NativeTokenInfo nativeTokenInfo; + if (Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(tokenCaller, nativeTokenInfo) != 0) { + HILOGE("Failed to get native token info"); + break; + } + callerName = nativeTokenInfo.processName; + return callerName; + } + case Security::AccessToken::ATokenTypeEnum::TOKEN_HAP: { + Security::AccessToken::HapTokenInfo hapTokenInfo; + if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenCaller, hapTokenInfo) != 0) { + HILOGE("Failed to get hap token info"); + break; + } + callerName = hapTokenInfo.bundleName; + return callerName; + } + } + HILOGE("Invalid token type, %{public}s", to_string(tokenType).c_str()); + return callerName; +} + +ErrCode Service::InitRestoreSession(sptr remote, std::string &errMsg) +{ + HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + try { + VerifyCaller(); + if (session_ == nullptr) { + errMsg = "session is empty"; + HILOGE("Init RestoreSession session error, %{public}s", errMsg.c_str()); + return BError(BError::Codes::SA_INVAL_ARG); + } + ErrCode errCode = session_->Active({ + .clientToken = IPCSkeleton::GetCallingTokenID(), + .scenario = IServiceReverse::Scenario::RESTORE, + .clientProxy = remote, + .userId = GetUserIdDefault(), + .callerName = GetCallerName(), + .activeTime = TimeUtils::GetCurrentTime(), + }); + if (errCode == 0) { + ClearFailedBundles(); + successBundlesNum_ = 0; + } + if (errCode == BError(BError::Codes::SA_SESSION_CONFLICT)) { + errMsg = BJsonUtil::BuildInitSessionErrInfo(session_->GetSessionUserId(), + session_->GetSessionCallerName(), + session_->GetSessionActiveTime()); + } + return errCode; + } catch (const BError &e) { + StopAll(nullptr, true); + errMsg = e.what(); + return e.GetCode(); + } catch (const exception &e) { + errMsg = e.what(); + HILOGE("Catched an unexpected low-level exception %{public}s", errMsg.c_str()); + return EPERM; + } catch (...) { + errMsg = "Unexpected exception"; + HILOGE("%{public}s", errMsg.c_str()); + return EPERM; + } +} } \ 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 9fec17ab3df04c827c7748c08944d34e90277c0d..c45f5a0428ddd6ffe60dea2aa29974e4a011fcea 100644 --- a/services/backup_sa/src/module_ipc/svc_session_manager.cpp +++ b/services/backup_sa/src/module_ipc/svc_session_manager.cpp @@ -76,7 +76,7 @@ ErrCode SvcSessionManager::Active(Impl newImpl, bool isOccupyingSession) const Impl &oldImpl = impl_; if (oldImpl.clientToken) { HILOGE("Already have an active session"); - return BError(BError::Codes::SA_REFUSED_ACT); + return BError(BError::Codes::SA_SESSION_CONFLICT); } if (!isOccupyingSession && !newImpl.clientToken) { @@ -166,6 +166,24 @@ void SvcSessionManager::SetSessionUserId(int32_t userId) impl_.userId = userId; } +string SvcSessionManager::GetSessionCallerName() +{ + shared_lock lock(lock_); + if (!impl_.clientToken) { + throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + } + return impl_.callerName; +} + +string SvcSessionManager::GetSessionActiveTime() +{ + shared_lock lock(lock_); + if (!impl_.clientToken) { + throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + } + return impl_.activeTime; +} + bool SvcSessionManager::OnBundleFileReady(const string &bundleName, const string &fileName) { unique_lock lock(lock_); diff --git a/tests/mock/backup_kit_inner/service_proxy_mock.cpp b/tests/mock/backup_kit_inner/service_proxy_mock.cpp index 2c1f80c3c46c82c925d833fe00b36f1e14f85c91..02bddcba681fa9eb29eb2bd2a47cece23bf02dde 100644 --- a/tests/mock/backup_kit_inner/service_proxy_mock.cpp +++ b/tests/mock/backup_kit_inner/service_proxy_mock.cpp @@ -39,6 +39,14 @@ int32_t ServiceProxy::InitRestoreSession(sptr remote) return 0; } +int32_t ServiceProxy::InitRestoreSession(sptr remote, std::string &errMsg) +{ + if (!GetMockInitBackupOrRestoreSession()) { + return 1; + } + return 0; +} + int32_t ServiceProxy::InitBackupSession(sptr remote) { if (!GetMockInitBackupOrRestoreSession()) { diff --git a/tests/mock/module_ipc/include/svc_session_manager_mock.h b/tests/mock/module_ipc/include/svc_session_manager_mock.h index e8f74bb042c05c7418605739e68aa244abc043bc..fcfb87865130aefb9f55bc6e703c447c240a3a86 100644 --- a/tests/mock/module_ipc/include/svc_session_manager_mock.h +++ b/tests/mock/module_ipc/include/svc_session_manager_mock.h @@ -27,6 +27,8 @@ public: virtual sptr GetServiceReverseProxy() = 0; virtual IServiceReverse::Scenario GetScenario() = 0; virtual int32_t GetSessionUserId() = 0; + virtual std::string GetSessionCallerName() = 0; + virtual std::string GetSessionActiveTime() = 0; virtual bool OnBundleFileReady(const std::string&, const std::string&) = 0; virtual UniqueFd OnBundleExtManageInfo(const std::string&, UniqueFd) = 0; virtual wptr GetExtConnection(const BundleName&) = 0; @@ -72,6 +74,8 @@ public: MOCK_METHOD((sptr), GetServiceReverseProxy, ()); MOCK_METHOD(IServiceReverse::Scenario, GetScenario, ()); MOCK_METHOD(int32_t, GetSessionUserId, ()); + MOCK_METHOD(std::string, GetSessionCallerName, ()); + MOCK_METHOD(std::string, GetSessionActiveTime, ()); MOCK_METHOD(bool, OnBundleFileReady, (const std::string&, const std::string&)); MOCK_METHOD(UniqueFd, OnBundleExtManageInfo, (const std::string&, UniqueFd)); MOCK_METHOD((wptr), GetExtConnection, (const BundleName&)); diff --git a/tests/mock/module_ipc/service_mock.cpp b/tests/mock/module_ipc/service_mock.cpp index 2aa2cebb1ebea64665e21a2c4d1fd57f729a7591..fd9a1c151de2505ad0d62e49ad94fc56cd9f2b25 100644 --- a/tests/mock/module_ipc/service_mock.cpp +++ b/tests/mock/module_ipc/service_mock.cpp @@ -26,6 +26,11 @@ namespace OHOS::FileManagement::Backup { using namespace std; +int32_t Service::GetUserIdDefault() +{ + return 0; +} + void Service::OnStart() {} void Service::OnStop() {} @@ -47,6 +52,11 @@ ErrCode Service::InitRestoreSession(sptr remote) return BError(BError::Codes::OK); } +ErrCode Service::InitRestoreSession(sptr remote, std::string &errMsg) +{ + return BError(BError::Codes::OK); +} + ErrCode Service::InitBackupSession(sptr remote) { return BError(BError::Codes::OK); @@ -287,6 +297,15 @@ void Service::FileReadyRadarReport(const std::string &bundleName, const std::str void Service::ExtensionConnectFailRadarReport(const std::string &bundleName, const ErrCode errCode, const IServiceReverse::Scenario scenario) {} +void Service::PermissionCheckFailRadar(const std::string &info, const std::string &func) {} + +void Service::OnStartResRadarReport(const std::vector &bundleNameList, int32_t stage) {} + +std::string Service::GetCallerName() +{ + return ""; +} + void Service::UpdateFailedBundles(const std::string &bundleName, BundleTaskInfo taskInfo) {} void Service::ClearFailedBundles() {} diff --git a/tests/mock/module_ipc/service_stub_mock.cpp b/tests/mock/module_ipc/service_stub_mock.cpp index fb059d7bd8adcc0469609a8b3ecfdcb00bcab5cc..c5112cce56650eb1a34a01f17050dbefc61d77a2 100644 --- a/tests/mock/module_ipc/service_stub_mock.cpp +++ b/tests/mock/module_ipc/service_stub_mock.cpp @@ -81,6 +81,8 @@ void ServiceStub::ServiceStubSupplement() &ServiceStub::CmdUpdateSendRate; opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_REPORT_APP_PROCESS_INFO)] = &ServiceStub::CmdReportAppProcessInfo; + opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_INIT_RESTORE_SESSION_MSG)] = + &ServiceStub::CmdInitRestoreSessionMsg; opToInterfaceMap_[static_cast(IServiceInterfaceCode::SERVICE_CMD_CANCEL_BUNDLE)] = &ServiceStub::CmdCancel; } @@ -110,6 +112,17 @@ int32_t ServiceStub::CmdInitRestoreSession(MessageParcel &data, MessageParcel &r return BError(BError::Codes::OK); } +int32_t ServiceStub::CmdInitRestoreSessionMsg(MessageParcel &data, MessageParcel &reply) +{ + auto remote = data.ReadRemoteObject(); + auto iremote = iface_cast(remote); + std::string errMsg; + int32_t res = InitRestoreSession(iremote, errMsg); + reply.WriteString(errMsg); + reply.WriteInt32(res); + return BError(BError::Codes::OK); +} + int32_t ServiceStub::CmdInitBackupSession(MessageParcel &data, MessageParcel &reply) { auto remote = data.ReadRemoteObject(); 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 b4aa9f283e7133900b57bb5881dcd69dc52b4730..7c4cc6f061ce47b52bad93a8fe72e7c8d11888f8 100644 --- a/tests/mock/module_ipc/src/svc_session_manager_mock.cpp +++ b/tests/mock/module_ipc/src/svc_session_manager_mock.cpp @@ -143,6 +143,17 @@ int32_t SvcSessionManager::GetSessionUserId() void SvcSessionManager::SetSessionUserId(int32_t) {} +std::string SvcSessionManager::GetSessionCallerName() +{ + return BSvcSessionManager::sessionManager->GetSessionCallerName(); +} + +std::string SvcSessionManager::GetSessionActiveTime() +{ + return BSvcSessionManager::sessionManager->GetSessionActiveTime(); +} + + void SvcSessionManager::SetBundleRestoreType(const std::string &, RestoreTypeEnum) {} RestoreTypeEnum SvcSessionManager::GetBundleRestoreType(const std::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 1e9e265b8fb0cff65b5470aa8279313d9d5b6570..13c4c6ecb21188e697423c051ae625c6d721a95e 100644 --- a/tests/mock/module_ipc/svc_session_manager_mock.cpp +++ b/tests/mock/module_ipc/svc_session_manager_mock.cpp @@ -291,6 +291,16 @@ void SvcSessionManager::SetSessionUserId(int32_t userId) impl_.userId = userId; } +string SvcSessionManager::GetSessionCallerName() +{ + return impl_.callerName; +} + +string SvcSessionManager::GetSessionActiveTime() +{ + return impl_.activeTime; +} + void SvcSessionManager::SetBundleRestoreType(const std::string &bundleName, RestoreTypeEnum restoreType) { 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 c76234055b29952aa9042d0682b8dcf6f683ae45..afde882f6edadffcce8f7ccf5e876b7d2d3d5e8e 100644 --- a/tests/mock/module_ipc/svc_session_manager_throw_mock.cpp +++ b/tests/mock/module_ipc/svc_session_manager_throw_mock.cpp @@ -184,6 +184,16 @@ void SvcSessionManager::SetSessionUserId(int32_t userId) BackupSvcSessionManager::session->SetSessionUserId(userId); } +string SvcSessionManager::GetSessionCallerName() +{ + return BackupSvcSessionManager::session->GetSessionCallerName(); +} + +string SvcSessionManager::GetSessionActiveTime() +{ + return BackupSvcSessionManager::session->GetSessionActiveTime(); +} + void SvcSessionManager::SetBundleRestoreType(const std::string &bundleName, RestoreTypeEnum restoreType) { BackupSvcSessionManager::session->SetBundleRestoreType(bundleName, restoreType); 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 60113e1d789c02e9ce5b655fea4b9a2c365195e4..e7ee6a6b7a12051ebcfd174180d1feb23dedf8be 100644 --- a/tests/mock/module_ipc/svc_session_manager_throw_mock.h +++ b/tests/mock/module_ipc/svc_session_manager_throw_mock.h @@ -60,6 +60,8 @@ public: virtual bool NeedToUnloadService() = 0; virtual int32_t GetSessionUserId() = 0; virtual void SetSessionUserId(int32_t) = 0; + virtual std::string GetSessionCallerName() = 0; + virtual std::string GetSessionActiveTime() = 0; virtual void SetBundleRestoreType(const std::string &, RestoreTypeEnum) = 0; virtual RestoreTypeEnum GetBundleRestoreType(const std::string &) = 0; virtual void SetBundleVersionCode(const std::string &, int64_t) = 0; @@ -131,6 +133,8 @@ public: MOCK_METHOD(bool, NeedToUnloadService, ()); MOCK_METHOD(int32_t, GetSessionUserId, ()); MOCK_METHOD(void, SetSessionUserId, (int32_t)); + MOCK_METHOD(std::string, GetSessionCallerName, ()); + MOCK_METHOD(std::string, GetSessionActiveTime, ()); MOCK_METHOD(void, SetBundleRestoreType, (const std::string &, RestoreTypeEnum)); MOCK_METHOD(RestoreTypeEnum, GetBundleRestoreType, (const std::string &)); MOCK_METHOD(void, SetBundleVersionCode, (const std::string &, int64_t)); diff --git a/tests/mock/utils_mock/include/b_jsonutil_mock.h b/tests/mock/utils_mock/include/b_jsonutil_mock.h index 01cfaff0023c242918b69d5c6d543442caf5daf4..e0e0ad7c231b7a56a57386988ed95e68b6146e4c 100644 --- a/tests/mock/utils_mock/include/b_jsonutil_mock.h +++ b/tests/mock/utils_mock/include/b_jsonutil_mock.h @@ -38,6 +38,7 @@ public: virtual bool BuildOnProcessErrInfo(std::string&, std::string, int) = 0; virtual bool BuildBundleInfoJson(int32_t, std::string&) = 0; virtual bool HasUnicastInfo(std::string&) = 0; + virtual std::string BuildInitSessionErrInfo(int32_t, std::string, std::string) = 0; public: BBJsonUtil() = default; virtual ~BBJsonUtil() = default; @@ -62,6 +63,7 @@ public: MOCK_METHOD(bool, BuildOnProcessErrInfo, (std::string&, std::string, int)); MOCK_METHOD(bool, BuildBundleInfoJson, (int32_t, std::string&)); MOCK_METHOD(bool, HasUnicastInfo, (std::string&)); + MOCK_METHOD(std::string, BuildInitSessionErrInfo, (int32_t, std::string, std::string)); }; } // namespace OHOS::FileManagement::Backup #endif // OHOS_FILEMGMT_BACKUP_B_JSONUTIL_MOCK_MOCK_H diff --git a/tests/mock/utils_mock/src/b_jsonutil_mock.cpp b/tests/mock/utils_mock/src/b_jsonutil_mock.cpp index 8d802d4186c04b7c4b62d8a26745208ce491ebb1..eea1cfee0298163e3fc50b55bb9af39f5423472f 100644 --- a/tests/mock/utils_mock/src/b_jsonutil_mock.cpp +++ b/tests/mock/utils_mock/src/b_jsonutil_mock.cpp @@ -72,4 +72,9 @@ bool BJsonUtil::BuildBundleInfoJson(int32_t userId, string &detailInfo) { return BBJsonUtil::jsonUtil->BuildBundleInfoJson(userId, detailInfo); } + +std::string BJsonUtil::BuildInitSessionErrInfo(int32_t userId, string callerName, string activeTime) +{ + return BBJsonUtil::jsonUtil->BuildInitSessionErrInfo(userId, callerName, activeTime); +} } \ No newline at end of file diff --git a/tests/unittests/backup_api/backup_impl/include/i_service_mock.h b/tests/unittests/backup_api/backup_impl/include/i_service_mock.h index 3a62022b6c6ac34efda084a32ed02088aa0ff40a..eac34116ec9237e08ab809e89a2ebda01afcb591 100644 --- a/tests/unittests/backup_api/backup_impl/include/i_service_mock.h +++ b/tests/unittests/backup_api/backup_impl/include/i_service_mock.h @@ -43,6 +43,14 @@ public: return BError(BError::Codes::OK); } + int32_t InvokeMsgSendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) + { + code_ = code; + reply.WriteString(""); + reply.WriteInt32(BError(BError::Codes::OK)); + return BError(BError::Codes::OK); + } + int32_t InvokeGetLocalSendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { code_ = code; @@ -59,6 +67,11 @@ public: return BError(BError::Codes::OK); } + ErrCode InitRestoreSession(sptr remote, std::string &errMsg) override + { + return BError(BError::Codes::OK); + } + ErrCode InitBackupSession(sptr remote) override { return BError(BError::Codes::OK); diff --git a/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp b/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp index d7a2d42181aa13f2db37a4fdac9bac35ec84580b..5948fc9545c171f5b4eb00b19a70d915b74c3655 100644 --- a/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp @@ -52,6 +52,11 @@ ErrCode Service::InitRestoreSession(sptr remote) return BError(BError::Codes::OK); } +ErrCode Service::InitRestoreSession(sptr remote, std::string &errMsg) +{ + return BError(BError::Codes::OK); +} + ErrCode Service::InitBackupSession(sptr remote) { return BError(BError::Codes::OK); @@ -152,6 +157,11 @@ void Service::VerifyCaller() {} void Service::VerifyCaller(IServiceReverse::Scenario scenario) {} +int32_t Service::GetUserIdDefault() +{ + return 0; +} + void Service::OnAllBundlesFinished(ErrCode errCode) {} void Service::OnStartSched() {} @@ -243,6 +253,15 @@ void Service::FileReadyRadarReport(const std::string &bundleName, const std::str void Service::ExtensionConnectFailRadarReport(const std::string &bundleName, const ErrCode errCode, const IServiceReverse::Scenario scenario) {} +void Service::PermissionCheckFailRadar(const std::string &info, const std::string &func) {} + +void Service::OnStartResRadarReport(const std::vector &bundleNameList, int32_t stage) {} + +std::string Service::GetCallerName() +{ + return ""; +} + void Service::UpdateFailedBundles(const std::string &bundleName, BundleTaskInfo taskInfo) {} void Service::ClearFailedBundles() {} @@ -326,51 +345,6 @@ void ServiceIncrementalTest::TearDownTestCase() srProxy = nullptr; } -/** - * @tc.number: SUB_ServiceIncremental_GetUserIdDefault_0000 - * @tc.name: SUB_ServiceIncremental_GetUserIdDefault_0000 - * @tc.desc: 测试 GetUserIdDefault 的正常/异常分支 - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: issueIAKC3I - */ -HWTEST_F(ServiceIncrementalTest, SUB_ServiceIncremental_GetUserIdDefault_0000, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "ServiceIncrementalTest-begin SUB_ServiceIncremental_GetUserIdDefault_0000"; - try { - EXPECT_CALL(*param, GetBackupDebugOverrideAccount()) - .WillOnce(Return(make_pair(true, DEBUG_ID + 1))); - auto ret = GetUserIdDefault(); - EXPECT_EQ(ret, DEBUG_ID + 1); - - EXPECT_CALL(*param, GetBackupDebugOverrideAccount()).WillOnce(Return(make_pair(false, 0))); - EXPECT_CALL(*skeleton, GetCallingUid()).WillOnce(Return(BConstants::SYSTEM_UID)); - ret = GetUserIdDefault(); - EXPECT_EQ(ret, BConstants::DEFAULT_USER_ID); - - EXPECT_CALL(*param, GetBackupDebugOverrideAccount()).WillOnce(Return(make_pair(true, 0))); - EXPECT_CALL(*skeleton, GetCallingUid()).WillOnce(Return(BConstants::SYSTEM_UID)); - ret = GetUserIdDefault(); - EXPECT_EQ(ret, BConstants::DEFAULT_USER_ID); - - EXPECT_CALL(*param, GetBackupDebugOverrideAccount()).WillOnce(Return(make_pair(false, 0))); - EXPECT_CALL(*skeleton, GetCallingUid()).WillOnce(Return(BConstants::XTS_UID)); - ret = GetUserIdDefault(); - EXPECT_EQ(ret, BConstants::DEFAULT_USER_ID); - - EXPECT_CALL(*param, GetBackupDebugOverrideAccount()).WillOnce(Return(make_pair(false, 0))); - EXPECT_CALL(*skeleton, GetCallingUid()) - .WillOnce(Return(BConstants::SPAN_USERID_UID + BConstants::SPAN_USERID_UID)); - ret = GetUserIdDefault(); - EXPECT_EQ(ret, 2); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "ServiceIncrementalTest-an exception occurred by GetUserIdDefault."; - } - GTEST_LOG_(INFO) << "ServiceIncrementalTest-end SUB_ServiceIncremental_GetUserIdDefault_0000"; -} - /** * @tc.number: SUB_ServiceIncremental_GetLocalCapabilitiesIncremental_0000 * @tc.name: SUB_ServiceIncremental_GetLocalCapabilitiesIncremental_0000 @@ -781,6 +755,7 @@ HWTEST_F(ServiceIncrementalTest, SUB_ServiceIncremental_GetIncrementalFileHandle { GTEST_LOG_(INFO) << "ServiceIncrementalTest-begin SUB_ServiceIncremental_GetIncrementalFileHandle_0000"; try { + int32_t DEBUG_ID = 100; string bundleName; string fileName; auto session_ = service->session_; 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 f265061ed1f0049577c4cbea2791327dff4f5d25..e4715abf2b07a15477ca8c3b968e482715e7c934 100644 --- a/tests/unittests/backup_sa/module_ipc/service_other_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_other_test.cpp @@ -258,29 +258,29 @@ HWTEST_F(ServiceTest, SUB_Service_GetUserIdDefault_0000, TestSize.Level1) try { EXPECT_CALL(*param, GetBackupDebugOverrideAccount()) .WillOnce(Return(make_pair(true, DEBUG_ID + 1))); - auto ret = GetUserIdDefault(); + auto ret = service->GetUserIdDefault(); EXPECT_EQ(ret, DEBUG_ID + 1); EXPECT_CALL(*param, GetBackupDebugOverrideAccount()).WillOnce(Return(make_pair(false, 0))); EXPECT_CALL(*skeleton, GetCallingUid()).WillOnce(Return(BConstants::SYSTEM_UID)); - ret = GetUserIdDefault(); + ret = service->GetUserIdDefault(); EXPECT_EQ(ret, BConstants::DEFAULT_USER_ID); EXPECT_CALL(*param, GetBackupDebugOverrideAccount()).WillOnce(Return(make_pair(true, 0))); EXPECT_CALL(*skeleton, GetCallingUid()).WillOnce(Return(BConstants::SYSTEM_UID)); - ret = GetUserIdDefault(); + ret = service->GetUserIdDefault(); EXPECT_EQ(ret, BConstants::DEFAULT_USER_ID); EXPECT_CALL(*param, GetBackupDebugOverrideAccount()).WillOnce(Return(make_pair(false, 0))); EXPECT_CALL(*skeleton, GetCallingUid()).WillOnce(Return(BConstants::XTS_UID)); - ret = GetUserIdDefault(); + ret = service->GetUserIdDefault(); EXPECT_EQ(ret, BConstants::DEFAULT_USER_ID); EXPECT_CALL(*param, GetBackupDebugOverrideAccount()).WillOnce(Return(make_pair(false, 0))); EXPECT_CALL(*skeleton, GetCallingUid()) .WillOnce(Return(BConstants::SPAN_USERID_UID + BConstants::SPAN_USERID_UID)) .WillOnce(Return(BConstants::SPAN_USERID_UID + BConstants::SPAN_USERID_UID)); - ret = GetUserIdDefault(); + ret = service->GetUserIdDefault(); EXPECT_EQ(ret, 2); } catch (...) { EXPECT_TRUE(false); diff --git a/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp b/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp index 60aef6c36d6f5b9d3135deffec5f2d11f9672720..68b2fceeb29ba380dbdd4d4f766f35ff10ee9ec2 100644 --- a/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_stub_test.cpp @@ -44,6 +44,7 @@ const string FILE_NAME = "1.tar"; class ServiceMock final : public ServiceStub { public: MOCK_METHOD1(InitRestoreSession, ErrCode(sptr remote)); + MOCK_METHOD2(InitRestoreSession, ErrCode(sptr remote, std::string &errMsg)); MOCK_METHOD1(InitBackupSession, ErrCode(sptr remote)); MOCK_METHOD0(Start, ErrCode()); MOCK_METHOD0(GetLocalCapabilities, UniqueFd()); diff --git a/tests/unittests/backup_sa/module_ipc/service_test.cpp b/tests/unittests/backup_sa/module_ipc/service_test.cpp index ff2176f75f40ab5065ad42a9ce2cb07d711a236e..1ca6f3daad5da097cd2c2cb8ef50d0134e5bdf07 100644 --- a/tests/unittests/backup_sa/module_ipc/service_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_test.cpp @@ -80,6 +80,7 @@ ErrCode ServiceTest::Init(IServiceReverse::Scenario scenario) "}]"; bundleNames.emplace_back(BUNDLE_NAME); detailInfos.emplace_back(json); + string errMsg; ErrCode ret = 0; if (scenario == IServiceReverse::Scenario::RESTORE) { EXPECT_TRUE(servicePtr_ != nullptr); @@ -88,6 +89,8 @@ ErrCode ServiceTest::Init(IServiceReverse::Scenario scenario) EXPECT_GE(fd, BError(BError::Codes::OK)); ret = servicePtr_->InitRestoreSession(remote_); EXPECT_EQ(ret, BError(BError::Codes::OK)); + ret = servicePtr_->InitRestoreSession(remote_, errMsg); + EXPECT_EQ(ret, BError(BError::Codes::OK)); ret = servicePtr_->AppendBundlesRestoreSession(move(fd), bundleNames, detailInfos); EXPECT_EQ(ret, BError(BError::Codes::OK)); ret = servicePtr_->Finish(); 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 9370532d014da1db08e695406c8c62693008cbab..3b397ee64532c9c4ef54fba7d57722f0349d4d1d 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 @@ -141,7 +141,7 @@ HWTEST_F(SvcSessionManagerTest, SUB_backup_sa_session_Active_0100, testing::ext: EXPECT_TRUE(sessionManagerPtr_ != nullptr); sessionManagerPtr_->impl_.clientToken = CLIENT_TOKEN_ID; auto res = sessionManagerPtr_->Active(newImpl); - EXPECT_EQ(res, BError(BError::Codes::SA_REFUSED_ACT).GetCode()); + EXPECT_EQ(res, BError(BError::Codes::SA_SESSION_CONFLICT).GetCode()); } catch (BError &err) { EXPECT_TRUE(false); } diff --git a/tests/unittests/backup_sa/session/service_proxy_mock.cpp b/tests/unittests/backup_sa/session/service_proxy_mock.cpp index b59d183da8716a589a78a71ade804a3e5ef93a9f..f1263b3213f567148d45dd6246607cb6ab705da3 100644 --- a/tests/unittests/backup_sa/session/service_proxy_mock.cpp +++ b/tests/unittests/backup_sa/session/service_proxy_mock.cpp @@ -25,6 +25,11 @@ int32_t ServiceProxy::InitRestoreSession(sptr remote) return 0; } +int32_t ServiceProxy::InitRestoreSession(sptr remote, std::string &errMsg) +{ + return 0; +} + int32_t ServiceProxy::InitBackupSession(sptr remote) { return 0; diff --git a/tests/unittests/backup_sa/session/service_proxy_mock.h b/tests/unittests/backup_sa/session/service_proxy_mock.h index c2a8c5571074f398603868f8ca21350715e1d809..cd4387fad0886e721ca487c1108609f4e1b2acbc 100644 --- a/tests/unittests/backup_sa/session/service_proxy_mock.h +++ b/tests/unittests/backup_sa/session/service_proxy_mock.h @@ -27,6 +27,7 @@ public: ~ServiceProxyMock() override {} public: MOCK_METHOD1(InitRestoreSession, ErrCode(sptr remote)); + MOCK_METHOD2(InitRestoreSession, ErrCode(sptr remote, std::string &errMsg)); MOCK_METHOD1(InitBackupSession, ErrCode(sptr remote)); MOCK_METHOD0(Start, ErrCode()); MOCK_METHOD0(AsObject, sptr()); diff --git a/utils/include/b_error/b_error.h b/utils/include/b_error/b_error.h index 0566781f45f1df66afb4c58f834035645c0ad8ac..77b3b10bfd330c3bf9aac1621b3488e7576341d1 100644 --- a/utils/include/b_error/b_error.h +++ b/utils/include/b_error/b_error.h @@ -73,6 +73,7 @@ public: SA_BOOT_EXT_TIMEOUT = 0x3005, SA_BUNDLE_INFO_EMPTY = 0x3006, SA_BOOT_EXT_FAIL = 0x3007, + SA_SESSION_CONFLICT = 0x3008, // 0x4000~0x4999 backup_SDK错误 SDK_INVAL_ARG = 0x4000, @@ -119,6 +120,7 @@ public: E_TASKFAIL = 13500010, E_CANCEL_UNSTARTED_TASK = 13500011, E_CANCEL_NO_TASK = 13500012, + E_CONFLICT = 13500013, }; public: @@ -267,6 +269,7 @@ private: {Codes::SA_EXT_ERR_CALL, "SA Extension received invalid arguments"}, {Codes::SA_EXT_ERR_SAMGR, "SA Extension get samgr failed"}, {Codes::SA_EXT_RELOAD_FAIL, "SA Extension reload failed"}, + {Codes::SA_SESSION_CONFLICT, "Session Conflict"}, }; static inline const std::map errCodeTable_ { @@ -302,6 +305,7 @@ private: {static_cast(Codes::SA_EXT_ERR_CALL), BackupErrorCode::E_INVAL}, {static_cast(Codes::SA_EXT_ERR_SAMGR), BackupErrorCode::E_IPCSS}, {static_cast(Codes::SA_EXT_RELOAD_FAIL), BackupErrorCode::E_BEF}, + {static_cast(Codes::SA_SESSION_CONFLICT), BackupErrorCode::E_CONFLICT}, {BackupErrorCode::E_IPCSS, BackupErrorCode::E_IPCSS}, {BackupErrorCode::E_INVAL, BackupErrorCode::E_INVAL}, {BackupErrorCode::E_NOTEXIST, BackupErrorCode::E_NOTEXIST}, @@ -321,6 +325,7 @@ private: {BackupErrorCode::E_BEF, BackupErrorCode::E_BEF}, {BackupErrorCode::E_CANCEL_UNSTARTED_TASK, BackupErrorCode::E_CANCEL_UNSTARTED_TASK}, {BackupErrorCode::E_CANCEL_NO_TASK, BackupErrorCode::E_CANCEL_NO_TASK}, + {BackupErrorCode::E_CONFLICT, BackupErrorCode::E_CONFLICT}, }; static inline const std::map sysErrnoCodeTable_ { @@ -358,6 +363,7 @@ private: {BackupErrorCode::E_BEF, "SA failed to boot application extension"}, {BackupErrorCode::E_CANCEL_UNSTARTED_TASK, "Cancel unstarted backup or restore task "}, {BackupErrorCode::E_CANCEL_NO_TASK, "Cancel a backup or restore task that does not exist"}, + {BackupErrorCode::E_CONFLICT, "Session Conflict"}, }; private: diff --git a/utils/include/b_jsonutil/b_jsonutil.h b/utils/include/b_jsonutil/b_jsonutil.h index ebaadfbeec6011bd03ebfbbaea3176df3a30e1a1..22bf8050605d1764497db7490eca15ce5b4b3081 100644 --- a/utils/include/b_jsonutil/b_jsonutil.h +++ b/utils/include/b_jsonutil/b_jsonutil.h @@ -168,6 +168,17 @@ public: * */ static std::string ParseBackupVersion(); + + /** + * @brief 拼接session冲突时报错信息 + * + * @param userId 用户id + * @param callerName session创建方 + * @param activeTime session创建时间 + * + * @return 拼接结果 + */ + static std::string BuildInitSessionErrInfo(int32_t userId, std::string callerName, std::string activeTime); }; } // namespace OHOS::FileManagement::Backup diff --git a/utils/src/b_jsonutil/b_jsonutil.cpp b/utils/src/b_jsonutil/b_jsonutil.cpp index 432604604cf9eb55605d81c8dd82eeb964c67f3a..2ffd34ae1138b4f8edfa4be21a6c628924e357a6 100644 --- a/utils/src/b_jsonutil/b_jsonutil.cpp +++ b/utils/src/b_jsonutil/b_jsonutil.cpp @@ -456,4 +456,40 @@ std::string BJsonUtil::ParseBackupVersion() cJSON_Delete(root); return backupVersion; } + +std::string BJsonUtil::BuildInitSessionErrInfo(int32_t userId, std::string callerName, std::string activeTime) +{ + cJSON *info = cJSON_CreateObject(); + if (info == nullptr) { + HILOGE("Failed to create cJSON object info, update errMsg failed"); + return ""; + } + cJSON *sessionInfoArray = cJSON_CreateArray(); + if (sessionInfoArray == nullptr) { + HILOGE("Failed to create cJSON array sessionInfoArray, update errMsg failed"); + cJSON_Delete(info); + return ""; + } + cJSON_AddItemToObject(info, "sessionInfo", sessionInfoArray); + cJSON *sessionInfoObject = cJSON_CreateObject(); + if (sessionInfoObject == nullptr) { + HILOGE("Failed to create cJSON object sessionInfoObject, update errMsg failed"); + cJSON_Delete(info); + return ""; + } + cJSON_AddItemToArray(sessionInfoArray, sessionInfoObject); + cJSON_AddStringToObject(sessionInfoObject, "userId", to_string(userId).c_str()); + cJSON_AddStringToObject(sessionInfoObject, "name", callerName.c_str()); + cJSON_AddStringToObject(sessionInfoObject, "activeTime", activeTime.c_str()); + char *jsonStr = cJSON_Print(info); + if (jsonStr == nullptr) { + HILOGE("update errMsg failed"); + cJSON_Delete(info); + return ""; + } + std::string errMsg = jsonStr; + cJSON_Delete(info); + cJSON_free(jsonStr); + return errMsg; +} } \ No newline at end of file