From b58b660141036dacd88b00bc08ac6f9acfe4e93a Mon Sep 17 00:00:00 2001 From: zhangzezhong Date: Sun, 8 Jun 2025 14:49:06 +0800 Subject: [PATCH] support wantAgent multi user Signed-off-by: zhangzezhong --- .../js/napi/wantagent/napi_want_agent.cpp | 13 +- .../js/napi/wantagent/napi_want_agent.h | 1 + .../wantagent/include/pending_want.h | 2 +- .../wantagent/include/want_agent_info.h | 10 +- .../inner_api/wantagent/src/pending_want.cpp | 10 +- .../wantagent/src/want_agent_helper.cpp | 11 +- .../wantagent/src/want_agent_info.cpp | 8 +- .../include/ability_manager_service.h | 3 + .../src/ability_manager_service.cpp | 229 ++++++++++++++++-- .../BUILD.gn | 2 + .../ability_manager_service_eleven_test.cpp | 95 +++++++- 11 files changed, 348 insertions(+), 36 deletions(-) diff --git a/frameworks/js/napi/wantagent/napi_want_agent.cpp b/frameworks/js/napi/wantagent/napi_want_agent.cpp index 9631e1bc8b4..80d6b8865c9 100644 --- a/frameworks/js/napi/wantagent/napi_want_agent.cpp +++ b/frameworks/js/napi/wantagent/napi_want_agent.cpp @@ -951,6 +951,16 @@ int32_t JsWantAgent::GetWantAgentParam(napi_env env, napi_callback_info info, Wa return PARAMETER_ERROR; } + bool hasUserId = false; + napi_has_named_property(env, argv[0], "userId", &hasUserId); + if (hasUserId) { + napi_value jsUserId = nullptr; + napi_get_named_property(env, argv[0], "userId", &jsUserId); + if (!ConvertFromJsValue(env, jsUserId, paras.userId)) { + TAG_LOGE(AAFwkTag::WANTAGENT, "Convert userId failed"); + return PARAMETER_ERROR; + } + } bool hasActionFlags = false; napi_has_named_property(env, argv[0], "actionFlags", &hasActionFlags); if (hasActionFlags) { @@ -1314,7 +1324,8 @@ void JsWantAgent::SetOnNapiGetWantAgentCallback(std::shared_ptr(parasobj->operationType), parasobj->wantAgentFlags, parasobj->wants, - extraInfo); + extraInfo, + parasobj->userId); auto context = OHOS::AbilityRuntime::Context::GetApplicationContext(); std::shared_ptr wantAgent = nullptr; diff --git a/frameworks/js/napi/wantagent/napi_want_agent.h b/frameworks/js/napi/wantagent/napi_want_agent.h index fe34e7a2761..74dcb1b47da 100644 --- a/frameworks/js/napi/wantagent/napi_want_agent.h +++ b/frameworks/js/napi/wantagent/napi_want_agent.h @@ -72,6 +72,7 @@ struct WantAgentWantsParas { std::vector> wants = {}; int32_t operationType = -1; int32_t requestCode = -1; + int32_t userId = -1; std::vector wantAgentFlags = {}; AAFwk::WantParams extraInfo = {}; }; diff --git a/interfaces/inner_api/wantagent/include/pending_want.h b/interfaces/inner_api/wantagent/include/pending_want.h index 00ab6d4fe03..4cdebd5a0c5 100644 --- a/interfaces/inner_api/wantagent/include/pending_want.h +++ b/interfaces/inner_api/wantagent/include/pending_want.h @@ -74,7 +74,7 @@ public: const std::shared_ptr &context, int requestCode, const std::shared_ptr &want, unsigned int flags, const std::shared_ptr &options, - std::shared_ptr &pendingWant); + std::shared_ptr &pendingWant, int userId = -1); /** * Like GetAbility(Context, int, Want, int)}, but allows an diff --git a/interfaces/inner_api/wantagent/include/want_agent_info.h b/interfaces/inner_api/wantagent/include/want_agent_info.h index 326c73e7b68..03a718bc381 100644 --- a/interfaces/inner_api/wantagent/include/want_agent_info.h +++ b/interfaces/inner_api/wantagent/include/want_agent_info.h @@ -69,7 +69,7 @@ public: */ WantAgentInfo(int requestCode, const WantAgentConstant::OperationType &operationType, const std::vector &flags, std::vector> &Wants, - const std::shared_ptr &extraInfo); + const std::shared_ptr &extraInfo, int userId = -1); /** * A constructor used to create an WantAgentInfo instance by copying parameters from an existing one. @@ -113,8 +113,16 @@ public: */ std::shared_ptr GetExtraInfo() const; + /** + * Obtains the userId of the WantAgent object. + * + * @return Returns the userId of the WantAgent object. + */ + int GetUserId() const; + private: int requestCode_ = 0; + int userId_ = -1; WantAgentConstant::OperationType operationType_ = WantAgentConstant::OperationType::UNKNOWN_TYPE; std::vector flags_ = std::vector(); std::vector> wants_ = std::vector>(); diff --git a/interfaces/inner_api/wantagent/src/pending_want.cpp b/interfaces/inner_api/wantagent/src/pending_want.cpp index cccc8d57cd9..770d949341c 100644 --- a/interfaces/inner_api/wantagent/src/pending_want.cpp +++ b/interfaces/inner_api/wantagent/src/pending_want.cpp @@ -57,7 +57,7 @@ ErrCode PendingWant::GetAbility( const std::shared_ptr &context, int requestCode, const std::shared_ptr &want, unsigned int flags, const std::shared_ptr &options, - std::shared_ptr &pendingWant) + std::shared_ptr &pendingWant, int userId) { if (context == nullptr) { TAG_LOGE(AAFwkTag::WANTAGENT, "invalid input param"); @@ -74,9 +74,13 @@ ErrCode PendingWant::GetAbility( WantSenderInfo wantSenderInfo; wantSenderInfo.type = static_cast(WantAgentConstant::OperationType::START_ABILITY); wantSenderInfo.allWants.push_back(wantsInfo); - wantSenderInfo.bundleName = context->GetBundleName(); + if (userId >= 0) { + wantSenderInfo.bundleName = want->GetOperation().GetBundleName(); + } else { + wantSenderInfo.bundleName = context->GetBundleName(); + } wantSenderInfo.flags = flags; - wantSenderInfo.userId = -1; // -1 : invalid user id + wantSenderInfo.userId = userId; wantSenderInfo.requestCode = requestCode; sptr target = nullptr; ErrCode result = WantAgentClient::GetInstance().GetWantSender(wantSenderInfo, nullptr, target); diff --git a/interfaces/inner_api/wantagent/src/want_agent_helper.cpp b/interfaces/inner_api/wantagent/src/want_agent_helper.cpp index bf87450861e..fea31509008 100644 --- a/interfaces/inner_api/wantagent/src/want_agent_helper.cpp +++ b/interfaces/inner_api/wantagent/src/want_agent_helper.cpp @@ -111,11 +111,12 @@ ErrCode WantAgentHelper::GetWantAgent( std::shared_ptr extraInfo = paramsInfo.GetExtraInfo(); std::shared_ptr pendingWant = nullptr; int requestCode = paramsInfo.GetRequestCode(); + int userId = paramsInfo.GetUserId(); WantAgentConstant::OperationType operationType = paramsInfo.GetOperationType(); ErrCode result; switch (operationType) { case WantAgentConstant::OperationType::START_ABILITY: - result = PendingWant::GetAbility(context, requestCode, wants[0], flags, extraInfo, pendingWant); + result = PendingWant::GetAbility(context, requestCode, wants[0], flags, extraInfo, pendingWant, userId); break; case WantAgentConstant::OperationType::START_ABILITIES: result = PendingWant::GetAbilities(context, requestCode, wants, flags, extraInfo, pendingWant); @@ -370,6 +371,7 @@ std::string WantAgentHelper::ToString(const std::shared_ptr &agent) jsonObject["requestCode"] = (*info.get()).requestCode; jsonObject["operationType"] = (*info.get()).type; jsonObject["flags"] = (*info.get()).flags; + jsonObject["userId"] = (*info.get()).userId; nlohmann::json wants = nlohmann::json::array(); for (auto &wantInfo : (*info.get()).allWants) { @@ -402,6 +404,11 @@ std::shared_ptr WantAgentHelper::FromString(const std::string &jsonSt requestCode = jsonObject.at("requestCode").get(); } + int userId = -1; + if (jsonObject.contains("userId") && jsonObject["userId"].is_number_integer()) { + userId = jsonObject.at("userId").get(); + } + WantAgentConstant::OperationType operationType = WantAgentConstant::OperationType::UNKNOWN_TYPE; if (jsonObject.contains("operationType") && jsonObject["operationType"].is_number_integer()) { operationType = static_cast(jsonObject.at("operationType").get()); @@ -432,7 +439,7 @@ std::shared_ptr WantAgentHelper::FromString(const std::string &jsonSt } WantAgentInfo info(requestCode, operationType, flagsVec, wants, extraInfo); - return GetWantAgent(info, INVLID_WANT_AGENT_USER_ID, uid); + return GetWantAgent(info, userId, uid); } std::vector WantAgentHelper::ParseFlags(nlohmann::json jsonObject) diff --git a/interfaces/inner_api/wantagent/src/want_agent_info.cpp b/interfaces/inner_api/wantagent/src/want_agent_info.cpp index f2f62d14247..001b42fc72b 100644 --- a/interfaces/inner_api/wantagent/src/want_agent_info.cpp +++ b/interfaces/inner_api/wantagent/src/want_agent_info.cpp @@ -44,10 +44,11 @@ WantAgentInfo::WantAgentInfo(int requestCode, const WantAgentConstant::Operation WantAgentInfo::WantAgentInfo(int requestCode, const WantAgentConstant::OperationType &operationType, const std::vector &flags, std::vector> &wants, - const std::shared_ptr &extraInfo) + const std::shared_ptr &extraInfo, int userId) { requestCode_ = requestCode; operationType_ = operationType; + userId_ = userId; if (!flags.empty()) { flags_.insert(flags_.end(), flags.begin(), flags.end()); } @@ -105,4 +106,9 @@ std::shared_ptr WantAgentInfo::GetExtraInfo() const { return extraInfo_; } + +int WantAgentInfo::GetUserId() const +{ + return userId_; +} } // namespace OHOS::AbilityRuntime::WantAgent diff --git a/services/abilitymgr/include/ability_manager_service.h b/services/abilitymgr/include/ability_manager_service.h index 65a70a25454..2cae845f351 100644 --- a/services/abilitymgr/include/ability_manager_service.h +++ b/services/abilitymgr/include/ability_manager_service.h @@ -2313,6 +2313,9 @@ private: void PauseOldMissionListManager(int32_t userId); void PauseOldConnectManager(int32_t userId); bool IsSystemUI(const std::string &bundleName) const; + int32_t GetUidByCloneBundleInfo(std::string &bundleName, int32_t callerUid, int32_t appIndex, int32_t userId); + sptr GetWantSenderByUserId( + const WantSenderInfo &wantSenderInfo, const sptr &callerToken, int32_t uid, bool isSACall); bool VerificationAllToken(const sptr &token); std::shared_ptr GetCurrentDataAbilityManager(); diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index 1995b540057..5aa314d169d 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -5284,19 +5284,93 @@ int AbilityManagerService::UnRegisterMissionListener(const std::string &deviceId return dmsClient.UnRegisterMissionListener(Str8ToStr16(deviceId), listener->AsObject()); } +int32_t AbilityManagerService::GetUidByCloneBundleInfo( + std::string &bundleName, int32_t callerUid, int32_t appIndex, int32_t userId) +{ + auto bms = AbilityUtil::GetBundleManagerHelper(); + CHECK_POINTER_AND_RETURN(bms, -1); + AppExecFwk::BundleInfo bundleInfo; + MultiAppUtils::GetRunningMultiAppIndex(bundleName, callerUid, appIndex); + if (IN_PROCESS_CALL(bms->GetCloneBundleInfo( + bundleName, static_cast(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION), + appIndex, bundleInfo, userId)) != ERR_OK) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "failed get bundle info for %{public}s", bundleName.c_str()); + return -1; + } + return bundleInfo.uid; +} + +sptr AbilityManagerService::GetWantSenderByUserId( + const WantSenderInfo &wantSenderInfo, const sptr &callerToken, int32_t uid, bool isSACall) +{ + TAG_LOGD(AAFwkTag::ABILITYMGR, "called"); + bool isSpecifyUserId = wantSenderInfo.userId >= 0; + int32_t callerUid = IPCSkeleton::GetCallingUid(); + int32_t callerUserId = callerUid / BASE_USER_RANGE; + int32_t userId = -1, appUid = -1, appIndex = 0; + std::string bundleName = ""; + if (!wantSenderInfo.allWants.empty()) { + bundleName = wantSenderInfo.allWants.back().want.GetElement().GetBundleName(); + } + std::shared_ptr pendingWantManager; + bool isSystemApp = AAFwk::PermissionVerification::GetInstance()->IsSystemAppCall(); + if ((isSACall || callerUserId == U0_USER_ID) && isSpecifyUserId) { + userId = wantSenderInfo.userId; + if (uid >= 0) { + appUid = uid; + } else { + appUid = GetUidByCloneBundleInfo(bundleName, callerUid, appIndex, userId); + } + } else if ((isSACall || callerUserId == U0_USER_ID) && !isSpecifyUserId) { + if (uid >= 0) { + userId = callerUserId; + appUid = GetUidByCloneBundleInfo(bundleName, callerUid, appIndex, userId); + } else { + if (DelayedSingleton::GetInstance()-> + GetOsAccountLocalIdFromUid(callerUid, userId) != 0) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "getOsAccountLocalIdFromUid failed uid=%{public}d", callerUid); + return nullptr; + } + appUid = uid; + } + } else if (!isSystemApp) { + userId = callerUserId; + appUid = callerUid; + } else if (isSystemApp) { + if (isSpecifyUserId) { + userId = wantSenderInfo.userId; + } else { + userId = callerUserId; + } + appUid = GetUidByCloneBundleInfo(bundleName, callerUid, appIndex, userId); + } + pendingWantManager = GetPendingWantManagerByUserId(userId); + CHECK_POINTER_AND_RETURN(pendingWantManager, nullptr); + if (!CheckSenderWantInfo(appUid == -1 ? callerUid : appUid, wantSenderInfo)) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "check bundleName failed"); + return nullptr; + } + return pendingWantManager->GetWantSender(callerUid, appUid, isSystemApp, wantSenderInfo, callerToken, appIndex); +} + sptr AbilityManagerService::GetWantSender( const WantSenderInfo &wantSenderInfo, const sptr &callerToken, int32_t uid) { TAG_LOGD(AAFwkTag::ABILITYMGR, "called"); + int32_t userId = wantSenderInfo.userId; + int32_t callerUid = IPCSkeleton::GetCallingUid(); + int32_t callerUserId = callerUid / BASE_USER_RANGE; + bool isSACall = AAFwk::PermissionVerification::GetInstance()->IsSACall(); + if (userId >= 0 || isSACall || callerUserId == U0_USER_ID) { + return GetWantSenderByUserId(wantSenderInfo, callerToken, uid, isSACall); + } auto pendingWantManager = GetCurrentPendingWantManager(); CHECK_POINTER_AND_RETURN(pendingWantManager, nullptr); auto bms = AbilityUtil::GetBundleManagerHelper(); CHECK_POINTER_AND_RETURN(bms, nullptr); - int32_t callerUid = IPCSkeleton::GetCallingUid(); - int32_t userId = wantSenderInfo.userId; int32_t bundleMgrResult = 0; if (userId < 0) { @@ -5348,17 +5422,31 @@ sptr AbilityManagerService::GetWantSender( int AbilityManagerService::SendWantSender(sptr target, SenderInfo &senderInfo) { TAG_LOGI(AAFwkTag::ABILITYMGR, "call"); - auto pendingWantManager = GetCurrentPendingWantManager(); - CHECK_POINTER_AND_RETURN(pendingWantManager, ERR_INVALID_VALUE); CHECK_POINTER_AND_RETURN(target, ERR_INVALID_VALUE); + sptr obj = target->AsObject(); + if (!obj || obj->IsProxyObject()) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "obj null or proxy obj"); + return ERR_INVALID_VALUE; + } + sptr record = iface_cast(obj); + CHECK_POINTER_AND_RETURN(record, ERR_INVALID_VALUE); + int32_t userId = -1; + if (record->GetKey() != nullptr) { + userId = record->GetKey()->GetUserId(); + } + std::shared_ptr pendingWantManager; + if (userId > 0) { + pendingWantManager = GetPendingWantManagerByUserId(userId); + } else { + pendingWantManager = GetCurrentPendingWantManager(); + } + CHECK_POINTER_AND_RETURN(pendingWantManager, ERR_INVALID_VALUE); return pendingWantManager->SendWantSender(target, senderInfo); } void AbilityManagerService::CancelWantSender(const sptr &sender) { TAG_LOGD(AAFwkTag::ABILITYMGR, "called"); - auto pendingWantManager = GetCurrentPendingWantManager(); - CHECK_POINTER(pendingWantManager); CHECK_POINTER(sender); sptr obj = sender->AsObject(); @@ -5366,19 +5454,20 @@ void AbilityManagerService::CancelWantSender(const sptr &sender) TAG_LOGE(AAFwkTag::ABILITYMGR, "obj null or proxy obj"); return; } - - auto bms = AbilityUtil::GetBundleManagerHelper(); - CHECK_POINTER(bms); - - int32_t callerUid = IPCSkeleton::GetCallingUid(); sptr record = iface_cast(obj); + CHECK_POINTER(record); - int userId = -1; - if (DelayedSingleton::GetInstance()-> - GetOsAccountLocalIdFromUid(callerUid, userId) != 0) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "getOsAccountLocalIdFromUid failed uid=%{public}d", callerUid); - return; + int32_t userId = -1; + if (record->GetKey() != nullptr) { + userId = record->GetKey()->GetUserId(); + } + std::shared_ptr pendingWantManager; + if (userId > 0) { + pendingWantManager = GetPendingWantManagerByUserId(userId); + } else { + pendingWantManager = GetCurrentPendingWantManager(); } + CHECK_POINTER(pendingWantManager); TAG_LOGD(AAFwkTag::ABILITYMGR, "getOsAccountLocalIdFromUid userId: %{public}d", userId); bool isSystemAppCall = AAFwk::PermissionVerification::GetInstance()->IsSystemAppCall(); @@ -5420,12 +5509,29 @@ void AbilityManagerService::CancelWantSenderByFlags(const sptr &sen int AbilityManagerService::GetPendingWantUid(const sptr &target) { TAG_LOGI(AAFwkTag::ABILITYMGR, "%{public}s:begin", __func__); - auto pendingWantManager = GetCurrentPendingWantManager(); - CHECK_POINTER_AND_RETURN(pendingWantManager, -1); if (target == nullptr) { TAG_LOGE(AAFwkTag::ABILITYMGR, "%s, target null", __func__); return -1; } + sptr obj = target->AsObject(); + if (!obj || obj->IsProxyObject()) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "obj null or proxy obj"); + return -1; + } + sptr record = iface_cast(obj); + CHECK_POINTER_AND_RETURN(record, -1); + + int32_t userId = -1; + if (record->GetKey() != nullptr) { + userId = record->GetKey()->GetUserId(); + } + std::shared_ptr pendingWantManager; + if (userId > 0) { + pendingWantManager = GetPendingWantManagerByUserId(userId); + } else { + pendingWantManager = GetCurrentPendingWantManager(); + } + CHECK_POINTER_AND_RETURN(pendingWantManager, -1); return pendingWantManager->GetPendingWantUid(target); } @@ -5445,21 +5551,56 @@ std::string AbilityManagerService::GetPendingWantBundleName(const sptr obj = target->AsObject(); + if (!obj || obj->IsProxyObject()) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "obj null or proxy obj"); + return ""; + } + sptr record = iface_cast(obj); + CHECK_POINTER_AND_RETURN(record, ""); + + int32_t userId = -1; + if (record->GetKey() != nullptr) { + userId = record->GetKey()->GetUserId(); + } + std::shared_ptr pendingWantManager; + if (userId > 0) { + pendingWantManager = GetPendingWantManagerByUserId(userId); + } else { + pendingWantManager = GetCurrentPendingWantManager(); + } + CHECK_POINTER_AND_RETURN(pendingWantManager, ""); return pendingWantManager->GetPendingWantBundleName(target); } int AbilityManagerService::GetPendingWantCode(const sptr &target) { TAG_LOGI(AAFwkTag::ABILITYMGR, "%{public}s:begin", __func__); - auto pendingWantManager = GetCurrentPendingWantManager(); - CHECK_POINTER_AND_RETURN(pendingWantManager, -1); if (target == nullptr) { TAG_LOGE(AAFwkTag::ABILITYMGR, "%s, target null", __func__); return -1; } + sptr obj = target->AsObject(); + if (!obj || obj->IsProxyObject()) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "obj null or proxy obj"); + return -1; + } + sptr record = iface_cast(obj); + CHECK_POINTER_AND_RETURN(record, -1); + + int32_t userId = -1; + if (record->GetKey() != nullptr) { + userId = record->GetKey()->GetUserId(); + } + std::shared_ptr pendingWantManager; + if (userId > 0) { + pendingWantManager = GetPendingWantManagerByUserId(userId); + } else { + pendingWantManager = GetCurrentPendingWantManager(); + } + CHECK_POINTER_AND_RETURN(pendingWantManager, -1); return pendingWantManager->GetPendingWantCode(target); } @@ -5467,12 +5608,29 @@ int AbilityManagerService::GetPendingWantType(const sptr &target) { TAG_LOGD(AAFwkTag::ABILITYMGR, "call"); XCOLLIE_TIMER_DEFAULT(__PRETTY_FUNCTION__); - auto pendingWantManager = GetCurrentPendingWantManager(); - CHECK_POINTER_AND_RETURN(pendingWantManager, -1); if (target == nullptr) { TAG_LOGE(AAFwkTag::ABILITYMGR, "%s, target null", __func__); return -1; } + sptr obj = target->AsObject(); + if (!obj || obj->IsProxyObject()) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "obj null or proxy obj"); + return -1; + } + sptr record = iface_cast(obj); + CHECK_POINTER_AND_RETURN(record, -1); + + int32_t userId = -1; + if (record->GetKey() != nullptr) { + userId = record->GetKey()->GetUserId(); + } + std::shared_ptr pendingWantManager; + if (userId > 0) { + pendingWantManager = GetPendingWantManagerByUserId(userId); + } else { + pendingWantManager = GetCurrentPendingWantManager(); + } + CHECK_POINTER_AND_RETURN(pendingWantManager, -1); return pendingWantManager->GetPendingWantType(target); } @@ -5503,7 +5661,28 @@ int AbilityManagerService::GetPendingRequestWant(const sptr &target HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); TAG_LOGD(AAFwkTag::ABILITYMGR, "Get pending request want."); XCOLLIE_TIMER_DEFAULT(__PRETTY_FUNCTION__); - auto pendingWantManager = GetCurrentPendingWantManager(); + if (target == nullptr) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "%s, target null", __func__); + return -1; + } + sptr obj = target->AsObject(); + if (!obj || obj->IsProxyObject()) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "obj null or proxy obj"); + return -1; + } + sptr record = iface_cast(obj); + CHECK_POINTER_AND_RETURN(record, -1); + + int32_t userId = -1; + if (record->GetKey() != nullptr) { + userId = record->GetKey()->GetUserId(); + } + std::shared_ptr pendingWantManager; + if (userId > 0) { + pendingWantManager = GetPendingWantManagerByUserId(userId); + } else { + pendingWantManager = GetCurrentPendingWantManager(); + } CHECK_POINTER_AND_RETURN(pendingWantManager, ERR_INVALID_VALUE); CHECK_POINTER_AND_RETURN(target, ERR_INVALID_VALUE); CHECK_POINTER_AND_RETURN(want, ERR_INVALID_VALUE); diff --git a/test/unittest/ability_manager_service_eleven_test/BUILD.gn b/test/unittest/ability_manager_service_eleven_test/BUILD.gn index 6337c42c35a..dbbd641adc7 100644 --- a/test/unittest/ability_manager_service_eleven_test/BUILD.gn +++ b/test/unittest/ability_manager_service_eleven_test/BUILD.gn @@ -35,6 +35,7 @@ ohos_unittest("ability_manager_service_eleven_test") { "${ability_runtime_test_path}/mock/frameworks_kits_ability_native_test/include", "${ability_runtime_test_path}/mock/services_abilitymgr_test/libs/aakit/include", "${ability_runtime_test_path}/unittest/app_debug_listener_stub_test", + "${ability_runtime_test_path}/unittest/multi_app_utils_test/include", "${ability_runtime_services_path}/abilitymgr/include/mission", "${ability_runtime_services_path}/abilitymgr/include/scene_board", "mock/include", @@ -47,6 +48,7 @@ ohos_unittest("ability_manager_service_eleven_test") { "${ability_runtime_services_path}/abilitymgr/src/scene_board/ui_ability_lifecycle_manager.cpp", "${ability_runtime_services_path}/abilitymgr/src/utils/timeout_state_utils.cpp", "${ability_runtime_services_path}/abilitymgr/src/utils/window_options_utils.cpp", + "${ability_runtime_test_path}/unittest/multi_app_utils_test/src/mock_app_mgr_service.cpp", "ability_manager_service_eleven_test.cpp", "mock/src/mock_my_flag.cpp", "mock/src/mock_permission_verification.cpp", diff --git a/test/unittest/ability_manager_service_eleven_test/ability_manager_service_eleven_test.cpp b/test/unittest/ability_manager_service_eleven_test/ability_manager_service_eleven_test.cpp index 8a91e68bbd2..bb0446c963f 100644 --- a/test/unittest/ability_manager_service_eleven_test/ability_manager_service_eleven_test.cpp +++ b/test/unittest/ability_manager_service_eleven_test/ability_manager_service_eleven_test.cpp @@ -27,6 +27,7 @@ #include "mock_ability_controller.h" #include "mock_ability_manager_collaborator.h" #include "mock_ability_token.h" +#include "mock_app_mgr_service.h" #include "mock_mission_list_manager_interface.h" #include "mock_my_flag.h" #include "mock_permission_verification.h" @@ -254,8 +255,15 @@ HWTEST_F(AbilityManagerServiceElevenTest, GetWantSender_0002, TestSize.Level1) EXPECT_EQ(result, nullptr); MyFlag::flag_ = 1; - wantSenderInfo.userId = 1; - uid = 1; + wantSenderInfo.userId = 100; + bundleName = "com.ohos.settings"; + operation.SetBundleName(bundleName); + want.SetOperation(operation); + wantsInfo.want = want; + wantSenderInfo.allWants.clear(); + wantSenderInfo.allWants.emplace_back(wantsInfo); + abilityMs->subManagersHelper_->pendingWantManagers_.emplace( + wantSenderInfo.userId, std::make_shared(nullptr)); EXPECT_EQ(wantSenderInfo.allWants.size(), 1); result = abilityMs->GetWantSender(wantSenderInfo, nullptr, uid); EXPECT_NE(result, nullptr); @@ -263,6 +271,41 @@ HWTEST_F(AbilityManagerServiceElevenTest, GetWantSender_0002, TestSize.Level1) GTEST_LOG_(INFO) << "GetWantSender_0002 end"; } +/* + * Feature: GetWantSender_0003 + * Function: GetWantSender + * SubFunction: NA + * FunctionPoints: AbilityManagerService GetWantSender + */ +HWTEST_F(AbilityManagerServiceElevenTest, GetWantSender_0003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetWantSender_0003 start"; + auto abilityMs = std::make_shared(); + EXPECT_NE(abilityMs, nullptr); + abilityMs->subManagersHelper_ = std::make_shared(nullptr, nullptr); + EXPECT_NE(abilityMs->subManagersHelper_, nullptr); + + MyFlag::flag_ = 1; + WantSenderInfo wantSenderInfo; + wantSenderInfo.userId = 100; + AAFwk::Want want; + AAFwk::Operation operation; + std::string bundleName = "com.ohos.settings"; + operation.SetBundleName(bundleName); + want.SetOperation(operation); + WantsInfo wantsInfo; + wantsInfo.want = want; + wantsInfo.resolvedTypes = want.GetType(); + wantSenderInfo.allWants.clear(); + wantSenderInfo.allWants.emplace_back(wantsInfo); + abilityMs->subManagersHelper_->pendingWantManagers_.emplace( + wantSenderInfo.userId, std::make_shared(nullptr)); + EXPECT_EQ(wantSenderInfo.allWants.size(), 1); + auto result = abilityMs->GetWantSender(wantSenderInfo, nullptr, -1); + EXPECT_NE(result, nullptr); + GTEST_LOG_(INFO) << "GetWantSender_0003 end"; +} + /* * Feature: CancelWantSender_0003 * Function: CancelWantSender @@ -986,5 +1029,53 @@ HWTEST_F(AbilityManagerServiceElevenTest, VerificationToken_003, TestSize.Level1 EXPECT_FALSE(abilityMs->VerificationToken(token)); TAG_LOGI(AAFwkTag::TEST, "AbilityManagerServiceElevenTest VerificationToken_003 end"); } + +/* + * Feature: AbilityManagerService + * Name: GetUidByCloneBundleInfo_001 + * Function: GetUidByCloneBundleInfo + * SubFunction: NA + * FunctionPoints: AbilityManagerService GetUidByCloneBundleInfo + */ +HWTEST_F(AbilityManagerServiceElevenTest, GetUidByCloneBundleInfo_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetUidByCloneBundleInfo_001 start"; + auto abilityMs = std::make_shared(); + EXPECT_NE(abilityMs, nullptr); + abilityMs->subManagersHelper_ = std::make_shared(nullptr, nullptr); + EXPECT_NE(abilityMs->subManagersHelper_, nullptr); + + std::string bundleName = "com.ohos.settings"; + int mockRightUid = -1; + int userId = 100; + AppExecFwk::MockAppMgrService::retCode_ = mockRightUid; + auto result = abilityMs->GetUidByCloneBundleInfo(bundleName, mockRightUid, 0, userId); + EXPECT_NE(result, mockRightUid); + GTEST_LOG_(INFO) << "GetUidByCloneBundleInfo_001 end"; +} + +/* + * Feature: AbilityManagerService + * Name: GetUidByCloneBundleInfo_002 + * Function: GetUidByCloneBundleInfo + * SubFunction: NA + * FunctionPoints: AbilityManagerService GetUidByCloneBundleInfo + */ +HWTEST_F(AbilityManagerServiceElevenTest, GetUidByCloneBundleInfo_002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetUidByCloneBundleInfo_002 start"; + auto abilityMs = std::make_shared(); + EXPECT_NE(abilityMs, nullptr); + abilityMs->subManagersHelper_ = std::make_shared(nullptr, nullptr); + EXPECT_NE(abilityMs->subManagersHelper_, nullptr); + + std::string bundleName = "com.applicaion.test"; + int mockErrorUid = -1; + int userId = 101; + AppExecFwk::MockAppMgrService::retCode_ = mockErrorUid; + auto result = abilityMs->GetUidByCloneBundleInfo(bundleName, mockErrorUid, 0, userId); + EXPECT_EQ(result, mockErrorUid); + GTEST_LOG_(INFO) << "GetUidByCloneBundleInfo_002 end"; +} } // namespace AAFwk } // namespace OHOS -- Gitee