From baff9870c9c32aa511243400691d61b899be47e1 Mon Sep 17 00:00:00 2001 From: "Zhangyao(Maggie)" Date: Fri, 13 Dec 2024 15:50:21 +0800 Subject: [PATCH] use cJson to build errMsg 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 + .../mock/utils_mock/include/b_jsonutil_mock.h | 2 + tests/mock/utils_mock/src/b_jsonutil_mock.cpp | 5 ++ .../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 ++ utils/include/b_jsonutil/b_jsonutil.h | 11 +++ utils/src/b_jsonutil/b_jsonutil.cpp | 36 +++++++++ 36 files changed, 431 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 42f9bea12..3e6a6f5fd 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 502fd1a9c..9f1b0b9fd 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 93fc3e6c9..d876b4612 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 118cf031e..10c5d2022 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 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 1d63fdf2b..92584949e 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 330ca7021..142bc72b6 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 41a059d2e..519e3622a 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 be07397ba..cf86805c5 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 9fec17ab3..c45f5a042 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 e8f74bb04..fcfb87865 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 2aa2cebb1..fd9a1c151 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 fb059d7bd..c5112cce5 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 b4aa9f283..7c4cc6f06 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 1e9e265b8..13c4c6ecb 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 c76234055..afde882f6 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 60113e1d7..e7ee6a6b7 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 01cfaff00..e0e0ad7c2 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 8d802d418..eea1cfee0 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 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 d7a2d4218..5948fc954 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 f265061ed..e4715abf2 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 60aef6c36..68b2fceeb 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 ff2176f75..1ca6f3daa 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 9370532d0..3b397ee64 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: diff --git a/utils/include/b_jsonutil/b_jsonutil.h b/utils/include/b_jsonutil/b_jsonutil.h index ebaadfbee..22bf80506 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 432604604..2ffd34ae1 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 -- Gitee