From eadfb4ccbf51d1002816a5937f29e54904b860a7 Mon Sep 17 00:00:00 2001 From: "Zhangyao(Maggie)" Date: Mon, 9 Dec 2024 19:34:43 +0800 Subject: [PATCH 1/2] restore session Signed-off-by: Zhangyao(Maggie) --- .../src/b_incremental_restore_session.cpp | 32 ++++++++ .../backup_kit_inner/src/service_proxy.cpp | 30 ++++++++ .../impl/b_incremental_restore_session.h | 11 +++ .../native/backup_kit_inner/impl/i_service.h | 1 + .../impl/i_service_ipc_interface_code.h | 1 + .../backup_kit_inner/impl/service_proxy.h | 1 + .../js/backup/session_restore_n_exporter.cpp | 9 ++- .../backup_sa/include/module_ipc/service.h | 19 +++++ .../include/module_ipc/service_stub.h | 1 + .../include/module_ipc/svc_session_manager.h | 16 ++++ services/backup_sa/src/module_ipc/service.cpp | 10 ++- .../src/module_ipc/service_incremental.cpp | 15 ---- .../backup_sa/src/module_ipc/service_stub.cpp | 25 +++++++ .../backup_sa/src/module_ipc/sub_service.cpp | 73 +++++++++++++++++++ .../src/module_ipc/svc_session_manager.cpp | 20 ++++- .../backup_kit_inner/service_proxy_mock.cpp | 8 ++ .../include/svc_session_manager_mock.h | 4 + tests/mock/module_ipc/service_mock.cpp | 19 +++++ tests/mock/module_ipc/service_stub_mock.cpp | 13 ++++ .../src/svc_session_manager_mock.cpp | 11 +++ .../module_ipc/svc_session_manager_mock.cpp | 10 +++ .../svc_session_manager_throw_mock.cpp | 10 +++ .../svc_session_manager_throw_mock.h | 4 + .../backup_impl/include/i_service_mock.h | 13 ++++ .../module_ipc/service_incremental_test.cpp | 65 +++++------------ .../module_ipc/service_other_test.cpp | 10 +-- .../module_ipc/service_stub_test.cpp | 1 + .../backup_sa/module_ipc/service_test.cpp | 3 + .../module_ipc/svc_session_manager_test.cpp | 2 +- .../backup_sa/session/service_proxy_mock.cpp | 5 ++ .../backup_sa/session/service_proxy_mock.h | 1 + utils/include/b_error/b_error.h | 6 ++ 32 files changed, 377 insertions(+), 72 deletions(-) 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 0999dac1d..2399c86a5 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 2e1ecb6bb..6bf48c5bd 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 3a2e2ec5f..be9f29b38 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 7e0684a6a..f722041a7 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 25cd58312..6d787fca0 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 4df70eea8..68eb9f4aa 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 6257637ef..74eab8291 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 43908f69a..570538b2c 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 验证调用者 * @@ -544,6 +559,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 6cabc844b..be78894b5 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 a3656084e..823f9fe25 100644 --- a/services/backup_sa/include/module_ipc/svc_session_manager.h +++ b/services/backup_sa/include/module_ipc/svc_session_manager.h @@ -91,6 +91,8 @@ public: int32_t userId {100}; RestoreTypeEnum restoreDataType {RESTORE_DATA_WAIT_SEND}; bool isIncrementalBackup {false}; + std::string callerName {}; + std::string activeTime {}; }; public: @@ -158,6 +160,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 3f9f76ef4..c46c52c05 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) { @@ -104,7 +104,7 @@ static inline int32_t GetUserIdDefault() return multiuser.userId; } -void OnStartResRadarReport(const std::vector &bundleNameList, int32_t stage) +void Service::OnStartResRadarReport(const std::vector &bundleNameList, int32_t stage) { std::stringstream ss; ss << "failedBundles:{"; @@ -232,6 +232,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"); @@ -347,7 +349,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); @@ -445,6 +447,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 079c59875..d152af437 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 bc5fb76a0..c16c25eda 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 8bbdf2482..8c8ad268c 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" @@ -364,4 +365,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 = R"({"sessionInfo": [{"userId" : ")" + to_string(session_->GetSessionUserId()) + + R"(", "name" : ")" + session_->GetSessionCallerName() + R"(", "activeTime" : ")" + + session_->GetSessionActiveTime() + R"("}]})"; + } + 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 f47d3e151..712db8077 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 2c1f80c3c..02bddcba6 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 680c2bea6..90b1ebb6e 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; @@ -71,6 +73,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 b4f761a70..2de9b697c 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 101f2fdfb..40f029b64 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; } int32_t ServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) @@ -108,6 +110,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 59c9e2a34..171962dc4 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 257ad09c2..9cbafc768 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 8a0b29390..83b32c43c 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 cb9904f73..5e0d1ecf5 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; @@ -129,6 +131,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/unittests/backup_api/backup_impl/include/i_service_mock.h b/tests/unittests/backup_api/backup_impl/include/i_service_mock.h index 3a62022b6..eac34116e 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 1bfc002e1..48a61a9ae 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() {} @@ -242,6 +252,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() {} @@ -325,51 +344,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 @@ -780,6 +754,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 fe7fe1918..b9cfc47c5 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 6288e19a7..5fb00c3d1 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 0835ba5e0..5b4c83e5b 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 6ddbbe2e6..30449bd62 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 b59d183da..f1263b321 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 c2a8c5571..cd4387fad 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 0566781f4..77b3b10bf 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: -- Gitee From b55442eae81e94f7da27e5680c487434c53b3707 Mon Sep 17 00:00:00 2001 From: "Zhangyao(Maggie)" Date: Mon, 9 Dec 2024 20:29:04 +0800 Subject: [PATCH 2/2] restore session Signed-off-by: Zhangyao(Maggie) --- .../src/b_incremental_restore_session.cpp | 32 ++++++++ .../backup_kit_inner/src/service_proxy.cpp | 30 ++++++++ .../impl/b_incremental_restore_session.h | 11 +++ .../native/backup_kit_inner/impl/i_service.h | 1 + .../impl/i_service_ipc_interface_code.h | 1 + .../backup_kit_inner/impl/service_proxy.h | 1 + .../js/backup/session_restore_n_exporter.cpp | 9 ++- .../backup_sa/include/module_ipc/service.h | 19 +++++ .../include/module_ipc/service_stub.h | 1 + .../include/module_ipc/svc_session_manager.h | 16 ++++ services/backup_sa/src/module_ipc/service.cpp | 10 ++- .../src/module_ipc/service_incremental.cpp | 15 ---- .../backup_sa/src/module_ipc/service_stub.cpp | 25 +++++++ .../backup_sa/src/module_ipc/sub_service.cpp | 73 +++++++++++++++++++ .../src/module_ipc/svc_session_manager.cpp | 20 ++++- .../backup_kit_inner/service_proxy_mock.cpp | 8 ++ .../include/svc_session_manager_mock.h | 4 + tests/mock/module_ipc/service_mock.cpp | 19 +++++ tests/mock/module_ipc/service_stub_mock.cpp | 13 ++++ .../src/svc_session_manager_mock.cpp | 11 +++ .../module_ipc/svc_session_manager_mock.cpp | 10 +++ .../svc_session_manager_throw_mock.cpp | 10 +++ .../svc_session_manager_throw_mock.h | 4 + .../backup_impl/include/i_service_mock.h | 13 ++++ .../module_ipc/service_incremental_test.cpp | 65 +++++------------ .../module_ipc/service_other_test.cpp | 10 +-- .../module_ipc/service_stub_test.cpp | 1 + .../backup_sa/module_ipc/service_test.cpp | 3 + .../module_ipc/svc_session_manager_test.cpp | 2 +- .../backup_sa/session/service_proxy_mock.cpp | 5 ++ .../backup_sa/session/service_proxy_mock.h | 1 + utils/include/b_error/b_error.h | 6 ++ 32 files changed, 377 insertions(+), 72 deletions(-) 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 0999dac1d..2399c86a5 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 2e1ecb6bb..6bf48c5bd 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 3a2e2ec5f..be9f29b38 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 7e0684a6a..f722041a7 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 25cd58312..6d787fca0 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 4df70eea8..68eb9f4aa 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 6257637ef..74eab8291 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 43908f69a..570538b2c 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 验证调用者 * @@ -544,6 +559,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 6cabc844b..be78894b5 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 a3656084e..823f9fe25 100644 --- a/services/backup_sa/include/module_ipc/svc_session_manager.h +++ b/services/backup_sa/include/module_ipc/svc_session_manager.h @@ -91,6 +91,8 @@ public: int32_t userId {100}; RestoreTypeEnum restoreDataType {RESTORE_DATA_WAIT_SEND}; bool isIncrementalBackup {false}; + std::string callerName {}; + std::string activeTime {}; }; public: @@ -158,6 +160,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 3f9f76ef4..c46c52c05 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) { @@ -104,7 +104,7 @@ static inline int32_t GetUserIdDefault() return multiuser.userId; } -void OnStartResRadarReport(const std::vector &bundleNameList, int32_t stage) +void Service::OnStartResRadarReport(const std::vector &bundleNameList, int32_t stage) { std::stringstream ss; ss << "failedBundles:{"; @@ -232,6 +232,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"); @@ -347,7 +349,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); @@ -445,6 +447,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 079c59875..d152af437 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 bc5fb76a0..c16c25eda 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 8bbdf2482..8c8ad268c 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" @@ -364,4 +365,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 = R"({"sessionInfo": [{"userId" : ")" + to_string(session_->GetSessionUserId()) + + R"(", "name" : ")" + session_->GetSessionCallerName() + R"(", "activeTime" : ")" + + session_->GetSessionActiveTime() + R"("}]})"; + } + 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 f47d3e151..712db8077 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 2c1f80c3c..02bddcba6 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 680c2bea6..90b1ebb6e 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; @@ -71,6 +73,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 b4f761a70..2de9b697c 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 101f2fdfb..40f029b64 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; } int32_t ServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) @@ -108,6 +110,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 59c9e2a34..171962dc4 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 257ad09c2..9cbafc768 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 8a0b29390..83b32c43c 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 cb9904f73..5e0d1ecf5 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; @@ -129,6 +131,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/unittests/backup_api/backup_impl/include/i_service_mock.h b/tests/unittests/backup_api/backup_impl/include/i_service_mock.h index 3a62022b6..eac34116e 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 1bfc002e1..48a61a9ae 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() {} @@ -242,6 +252,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() {} @@ -325,51 +344,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 @@ -780,6 +754,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 fe7fe1918..b9cfc47c5 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 6288e19a7..5fb00c3d1 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 0835ba5e0..5b4c83e5b 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 6ddbbe2e6..30449bd62 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 b59d183da..f1263b321 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 c2a8c5571..cd4387fad 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 0566781f4..77b3b10bf 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: -- Gitee