From c8c11d055263d1e24bfc8794b9f338deea7471c5 Mon Sep 17 00:00:00 2001 From: small_leek Date: Wed, 25 Jun 2025 10:07:41 +0800 Subject: [PATCH 1/3] add query bundle info event Signed-off-by: small_leek --- bundle_hisysevent.yaml | 15 +- .../bundlemgr/include/bundle_mgr_host_impl.h | 8 +- services/bundlemgr/include/event_report.h | 20 ++ .../bundlemgr/include/inner_event_report.h | 1 + .../bundlemgr/src/bundle_mgr_host_impl.cpp | 196 +++++++++++++++++- services/bundlemgr/src/inner_event_report.cpp | 32 +++ .../bms_bundle_kit_service_test.cpp | 65 ++++++ 7 files changed, 326 insertions(+), 11 deletions(-) diff --git a/bundle_hisysevent.yaml b/bundle_hisysevent.yaml index 8dfee8cd0a..2a62db4169 100644 --- a/bundle_hisysevent.yaml +++ b/bundle_hisysevent.yaml @@ -21,4 +21,17 @@ DB_ERROR: DB_NAME: {type: STRING, desc: db name} OPERATION_TYPE: {type: INT32, desc: db operation type} ERROR_CODE: {type: INT32, desc: db error code} - REBUILD_TYPE: {type: INT32, desc: db rebuild type} \ No newline at end of file + REBUILD_TYPE: {type: INT32, desc: db rebuild type} + +QUERY_BUNDLE_INFO: + __BASE: {type: BEHAVIOR, level: MINOR, desc: query bundle info} + FUNC_ID_LIST: {type: INT32, desc: function of query} + USERID_LIST: {type: INT32, arrsize: 100, desc: userId of the bundle} + BUNDLE_NAME_LIST: {type: STRING, arrsize: 100, desc: bundleName} + UID_LIST: {type: INT32, arrsize: 100, desc: uid of bundlename} + APP_INDEX_LIST: {type: INT32, arrsize: 100, desc: app index} + CALLING_UID_LIST: {type: INT32, arrsize: 100, desc: calling uid} + CALLING_APP_ID_LIST: {type: STRING, arrsize: 100, desc: calling appId} + CALLING_BUNDLE_NAME_LIST: {type: STRING, arrsize: 100, desc: calling bundleName} + GET_BUNDLE_INFO_FLAG_LIST: {type: INT32, arrsize: 100, desc: flag of bundle info} + ERROR_CODE: {type: INT32, desc: error code} \ No newline at end of file diff --git a/services/bundlemgr/include/bundle_mgr_host_impl.h b/services/bundlemgr/include/bundle_mgr_host_impl.h index 8e98379d06..498efedb42 100644 --- a/services/bundlemgr/include/bundle_mgr_host_impl.h +++ b/services/bundlemgr/include/bundle_mgr_host_impl.h @@ -28,6 +28,7 @@ #ifdef DISTRIBUTED_BUNDLE_FRAMEWORK #include "distributed_bms_interface.h" #endif +#include "event_report.h" #include "inner_bundle_user_info.h" namespace OHOS { @@ -1195,8 +1196,13 @@ private: bool GetLabelFromCache(const std::string &cacheKey, const std::string &abilityName, const std::unordered_map> &resourceCache, std::string &label); - + bool SendQueryBundleInfoEvent(const std::string &funcName, int32_t userId, int32_t uid, + int32_t appIndex, int32_t flag, const std::string &bundleName, + ErrCode errCode, bool reportNow); + bool CheckNeedAddEvent(const EventInfo &query); + bool GetCallingInfo(int32_t callingUid, std::string &callingBundleName, std::string &callingAppId); std::atomic isBrokerServiceExisted_ = false; + // used to record system event infos }; } // namespace AppExecFwk } // namespace OHOS diff --git a/services/bundlemgr/include/event_report.h b/services/bundlemgr/include/event_report.h index d637b2373a..841df97018 100644 --- a/services/bundlemgr/include/event_report.h +++ b/services/bundlemgr/include/event_report.h @@ -54,6 +54,7 @@ enum class BMSEventType : uint8_t { DB_ERROR, DATA_PARTITION_USAGE_EVENT, DEFAULT_APP, + QUERY_BUNDLE_INFO }; enum class BundleEventType : uint8_t { @@ -166,6 +167,7 @@ struct EventInfo { int64_t freeSize = 0; int32_t errorCode = 0; int32_t rebuildType = 0; + int64_t lastReportEventTime = 0; std::string bundleName; std::string moduleName; std::string abilityName; @@ -193,6 +195,15 @@ struct EventInfo { bool isIntercepted = false; std::vector fileSize; std::vector partitionSize; + std::vector funcIdList; + std::vector uidList; + std::vector userIdList; + std::vector appIndexList; + std::vector callingUidList; + std::vector flagList; + std::vector bundleNameList; + std::vector callingAppIdList; + std::vector callingBundleNameList; std::string want; std::string utd; @@ -248,6 +259,15 @@ struct EventInfo { rebuildType = 0; want.clear(); utd.clear(); + funcIdList.clear(); + uidList.clear(); + userIdList.clear(); + appIndexList.clear(); + callingUidList.clear(); + flagList.clear(); + bundleNameList.clear(); + callingAppIdList.clear(); + callingBundleNameList.clear(); } }; diff --git a/services/bundlemgr/include/inner_event_report.h b/services/bundlemgr/include/inner_event_report.h index 94bdab6e3f..97e2a86900 100644 --- a/services/bundlemgr/include/inner_event_report.h +++ b/services/bundlemgr/include/inner_event_report.h @@ -63,6 +63,7 @@ private: static void InnerSendDbErrorEvent(const EventInfo& eventInfo); static void InnerSendDataPartitionUsageEvent(const EventInfo& eventInfo); static void InnerSendDefaultAppEvent(const EventInfo& eventInfo); + static void InnerSendQueryBundleInfoEvent(const EventInfo& eventInfo); template static void InnerEventWrite(const std::string &eventName, diff --git a/services/bundlemgr/src/bundle_mgr_host_impl.cpp b/services/bundlemgr/src/bundle_mgr_host_impl.cpp index b97019e379..bd2cb16c58 100644 --- a/services/bundlemgr/src/bundle_mgr_host_impl.cpp +++ b/services/bundlemgr/src/bundle_mgr_host_impl.cpp @@ -88,6 +88,45 @@ const std::string RESOURCE_NOT_SUPPORT = const uint8_t JSON_INDENTATION = 4; const uint64_t BAD_CONTEXT_ID = 0; const uint64_t VECTOR_SIZE_MAX = 200; +const uint64_t MAX_QUERY_EVENT_REPORT_ONCE = 3; +const int64_t ONE_DAY = 10; +const std::unordered_map QUERY_FUNC_MAP = { + {"GetNameForUid", 1}, + {"GetBundleNameForUid", 2}, + {"GetBundleInfoV9", 3}, + {"GetBundleInfo", 4}, + {"GetAppProvisionInfo", 5} +}; +} + +std::shared_mutex g_queryEventMutex; +std::unordered_map g_queryEventList; + +std::unordered_map GetGlobalQueryEventInfo() +{ + std::unique_lock queryEventMutex(g_queryEventMutex); + return g_queryEventList; +} + +void AddGlobalQueryEventInfo(const EventInfo &info) +{ + std::unique_lock queryEventMutex(g_queryEventMutex); + if (g_queryEventList.find(info.errorCode) == g_queryEventList.end()) { + g_queryEventList[info.errorCode] = info; + return; + } +} + +void EraseGlobalQueryEventInfo(ErrCode errCode) +{ + std::unique_lock queryEventMutex(g_queryEventMutex); + g_queryEventList.erase(errCode); +} + +void ClearGlobalQueryEventInfo() +{ + std::unique_lock queryEventMutex(g_queryEventMutex); + g_queryEventList.clear(); } bool BundleMgrHostImpl::GetApplicationInfo( @@ -235,13 +274,22 @@ bool BundleMgrHostImpl::GetBundleInfo( auto dataMgr = GetDataMgrFromService(); if (dataMgr == nullptr) { LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr"); + SendQueryBundleInfoEvent("GetBundleInfo", userId, 0, 0, flags, + bundleName, ERR_BUNDLE_MANAGER_INTERNAL_ERROR, true); return false; } bool res = dataMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId); if (!res) { if (isBrokerServiceExisted_) { auto bmsExtensionClient = std::make_shared(); - return bmsExtensionClient->GetBundleInfo(bundleName, flags, bundleInfo, userId) == ERR_OK; + ErrCode ret = bmsExtensionClient->GetBundleInfo(bundleName, flags, bundleInfo, userId) == ERR_OK; + if (ret != ERR_OK) { + SendQueryBundleInfoEvent("GetBundleInfo", userId, 0, 0, flags, + bundleName, ret, false); + } + } else { + SendQueryBundleInfoEvent("GetBundleInfo", userId, 0, 0, flags, + bundleName, ERR_BUNDLE_MANAGER_INVALID_USER_ID, false); } } return res; @@ -287,13 +335,16 @@ ErrCode BundleMgrHostImpl::GetBundleInfoV9( }(); if (!permissionVerify) { APP_LOGE("verify permission failed"); + SendQueryBundleInfoEvent("GetBundleInfoV9", userId, 0, 0, flags, + bundleName, ERR_BUNDLE_MANAGER_PERMISSION_DENIED, false); return ERR_BUNDLE_MANAGER_PERMISSION_DENIED; } LOG_D(BMS_TAG_QUERY, "verify permission success, begin to GetBundleInfoV9"); auto dataMgr = GetDataMgrFromService(); if (dataMgr == nullptr) { LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr"); - return ERR_BUNDLE_MANAGER_INTERNAL_ERROR; + SendQueryBundleInfoEvent("GetBundleInfoV9", userId, 0, 0, flags, + bundleName, ERR_BUNDLE_MANAGER_INTERNAL_ERROR, true); } auto res = dataMgr->GetBundleInfoV9(bundleName, flags, bundleInfo, userId); if (res != ERR_OK) { @@ -332,7 +383,10 @@ ErrCode BundleMgrHostImpl::BatchGetBundleInfo(const std::vector &bu auto bmsExtensionClient = std::make_shared(); bmsExtensionClient->BatchGetBundleInfo(bundleNames, flags, bundleInfos, userId, true); } - return bundleInfos.empty() ? ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST : ERR_OK; + if (bundleInfos.empty()) { + return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST; + } + return ERR_OK; } ErrCode BundleMgrHostImpl::GetBundleInfoForSelf(int32_t flags, BundleInfo &bundleInfo) @@ -532,6 +586,8 @@ bool BundleMgrHostImpl::GetBundleNameForUid(const int uid, std::string &bundleNa auto dataMgr = GetDataMgrFromService(); if (dataMgr == nullptr) { APP_LOGE("DataMgr is nullptr"); + SendQueryBundleInfoEvent("GetBundleNameForUid", -1, uid, 0, 0, + bundleName, ERR_BUNDLE_MANAGER_INTERNAL_ERROR, true); return false; } return dataMgr->GetBundleNameForUid(uid, bundleName); @@ -576,19 +632,28 @@ ErrCode BundleMgrHostImpl::GetNameForUid(const int uid, std::string &name) }(); if (!permissionVerify) { APP_LOGE("verify permission failed"); + SendQueryBundleInfoEvent("GetNameForUid", -1, uid, 0, 0, + "", ERR_BUNDLE_MANAGER_PERMISSION_DENIED, true); return ERR_BUNDLE_MANAGER_PERMISSION_DENIED; } auto dataMgr = GetDataMgrFromService(); if (dataMgr == nullptr) { APP_LOGE("DataMgr is nullptr"); + SendQueryBundleInfoEvent("GetNameForUid", -1, uid, 0, 0, + "", ERR_BUNDLE_MANAGER_INTERNAL_ERROR, true); return ERR_BUNDLE_MANAGER_INTERNAL_ERROR; } auto ret = dataMgr->GetNameForUid(uid, name); - if (ret != ERR_OK && isBrokerServiceExisted_) { - auto bmsExtensionClient = std::make_shared(); - ret = bmsExtensionClient->GetBundleNameByUid(uid, name); - if (ret != ERR_OK) { - return ERR_BUNDLE_MANAGER_INVALID_UID; + if (ret != ERR_OK) { + if (isBrokerServiceExisted_) { + auto bmsExtensionClient = std::make_shared(); + ret = bmsExtensionClient->GetBundleNameByUid(uid, name); + if (ret != ERR_OK) { + SendQueryBundleInfoEvent("GetNameForUid", -1, uid, 0, 0, "", ERR_BUNDLE_MANAGER_INVALID_UID, true); + return ERR_BUNDLE_MANAGER_INVALID_UID; + } + } else { + SendQueryBundleInfoEvent("GetNameForUid", -1, uid, 0, 0, "", ret, true); } } return ret; @@ -3722,6 +3787,8 @@ ErrCode BundleMgrHostImpl::GetAppProvisionInfo(const std::string &bundleName, in userId); if (!BundlePermissionMgr::IsSystemApp()) { APP_LOGE("non-system app calling system api"); + SendQueryBundleInfoEvent("GetAppProvisionInfo", userId, -1, 0, 0, + bundleName, ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED, true); return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED; } if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) && @@ -3732,9 +3799,16 @@ ErrCode BundleMgrHostImpl::GetAppProvisionInfo(const std::string &bundleName, in auto dataMgr = GetDataMgrFromService(); if (dataMgr == nullptr) { APP_LOGE("DataMgr is nullptr"); + SendQueryBundleInfoEvent("GetAppProvisionInfo", userId, -1, 0, 0, + bundleName, ERR_BUNDLE_MANAGER_INTERNAL_ERROR, true); return ERR_BUNDLE_MANAGER_INTERNAL_ERROR; } - return dataMgr->GetAppProvisionInfo(bundleName, userId, appProvisionInfo); + ErrCode ret = dataMgr->GetAppProvisionInfo(bundleName, userId, appProvisionInfo); + if (ret != ERR_OK) { + SendQueryBundleInfoEvent("GetAppProvisionInfo", userId, -1, 0, 0, + bundleName, ret, false); + } + return ret; } ErrCode BundleMgrHostImpl::GetProvisionMetadata(const std::string &bundleName, int32_t userId, @@ -5766,6 +5840,110 @@ ErrCode BundleMgrHostImpl::GetAllShortcutInfoForSelf(std::vector & return dataMgr->GetAllShortcutInfoForSelf(shortcutInfos); } +bool BundleMgrHostImpl::CheckNeedAddEvent(const EventInfo &query) +{ + if (g_queryEventList.find(query.errorCode) == g_queryEventList.end()) { + g_queryEventList[query.errorCode] = query; + return false; + } + EventInfo info = g_queryEventList[query.errCode]; + if (info.bundleNameList.size() >= MAX_QUERY_EVENT_REPORT_ONCE) { + return false; + } + if (std::find(info.funcIdList.begin(), info.funcIdList.end(), query.funcIdList[0]) == info.funcIdList.end()|| + std::find(info.userIdList.begin(), info.userIdList.end(), query.userIdList[0]) == info.userIdList.end() || + std::find(info.uidList.begin(), info.uidList.end(), query.uidList[0]) == info.uidList.end() || + std::find(info.appIndexList.begin(), info.appIndexList.end(), + query.appIndexList[0]) == info.appIndexList.end() || + std::find(info.flagList.begin(), info.flagList.end(), query.flagList[0]) == info.flagList.end() || + std::find(info.bundleNameList.begin(), info.bundleNameList.end(), query.bundleNameList[0]) == + info.bundleNameList.end() || + std::find(info.callingUidList.begin(), info.callingUidList.end(), query.callingUidList[0]) == + info.callingUidList.end()) { + return true; + } + return false; +} + +bool BundleMgrHostImpl::GetCallingInfo(int32_t callingUid, std::string &callingBundleName, std::string &callingAppId) +{ + auto dataMgr = GetDataMgrFromService(); + if (dataMgr == nullptr) { + LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr"); + return false; + } + if (!dataMgr->GetBundleNameForUid(callingUid, callingBundleName)) { + LOG_W(BMS_TAG_INSTALLER, "CallingUid %{public}d is not hap, no bundleName", callingUid); + callingBundleName = Constants::EMPTY_STRING; + } + BundleInfo bundleInfo; + if (!dataMgr->GetBundleInfo(callingBundleName, BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, + callingUid / Constants::BASE_USER_RANGE)) { + LOG_E(BMS_TAG_INSTALLER, "GetBundleInfo failed, bundleName: %{public}s", callingBundleName.c_str()); + } + callingAppId = bundleInfo.appId; + return true; +} + +bool BundleMgrHostImpl::SendQueryBundleInfoEvent(const std::string &funcName, int32_t userId, int32_t uid, + int32_t appIndex, int32_t flag, const std::string &bundleName, ErrCode errCode, bool reportNow) +{ + int32_t callingUid = IPCSkeleton::GetCallingUid(); + APP_LOGI("start SendQueryBundleInfoEvent, funcName:%{public}s, bundleName:%{public}s, flag:%{public}d, userId:%{public}d, uid:%{public}d, appindex:%{public}d, callinguid:%{public}d, errCode:%{public}d", funcName.c_str(), bundleName.c_str(), flag, userId, uid, appIndex, callingUid, errCode); + if (QUERY_FUNC_MAP.find(funcName) == QUERY_FUNC_MAP.end()) { + LOG_E(BMS_TAG_QUERY, "No Need to report for func: %{public}s", funcName.c_str()); + return false; + } + // get calling bundle info + std::string callingBundleName; + std::string callingAppId; + GetCallingInfo(callingUid, callingBundleName, callingAppId); + + EventInfo query; + query.errCode = errCode; + query.funcIdList.push_back(QUERY_FUNC_MAP.at(funcName)); + query.userIdList.push_back(userId); + query.uidList.push_back(uid); + query.appIndexList.push_back(appIndex); + query.flagList.push_back(flag); + query.bundleNameList.push_back(bundleName); + query.callingUidList.push_back(callingUid); + query.callingBundleNameList.push_back(callingBundleName); + query.callingAppIdList.push_back(callingAppId); + + // check report now + if (reportNow) { + EventReport::SendSystemEvent(BMSEventType::QUERY_BUNDLE_INFO, query); + APP_LOGI("SendSystemEvent:QUERY_BUNDLE_INFO begin1 for funcName:%{public}s, errCode:%{public}d", + funcName.c_str(), query.errCode); + return true; + } + + int32_t funcId = QUERY_FUNC_MAP.at(funcName); + std::unique_lock queryEventMutex(g_queryEventMutex); + bool needAddEvent = CheckNeedAddEvent(query); + if (needAddEvent) { + g_queryEventList[errCode].funcIdList.push_back(funcId); + g_queryEventList[errCode].userIdList.push_back(userId); + g_queryEventList[errCode].uidList.push_back(uid); + g_queryEventList[errCode].appIndexList.push_back(appIndex); + g_queryEventList[errCode].flagList.push_back(flag); + g_queryEventList[errCode].bundleNameList.push_back(bundleName); + g_queryEventList[errCode].callingUidList.push_back(callingUid); + g_queryEventList[errCode].callingBundleNameList.push_back(callingBundleName); + g_queryEventList[errCode].callingAppIdList.push_back(callingAppId); + } + if (g_queryEventList[errCode].lastReportEventTime == 0 || + (BundleUtil::GetCurrentTime() - g_queryEventList[errCode].lastReportEventTime) >= ONE_DAY) { + APP_LOGI("SendSystemEvent:QUERY_BUNDLE_INFO begin1 for funcName:%{public}s, errCode:%{public}d", + funcName.c_str(), g_queryEventList[errCode].errCode); + EventReport::SendSystemEvent(BMSEventType::QUERY_BUNDLE_INFO, g_queryEventList[errCode]); + g_queryEventList.erase(errCode); + return true; + } + return false; +} + // Internal interface. The application compares the API version number saved in the package management. //No permission control is required bool BundleMgrHostImpl::GreatOrEqualTargetAPIVersion(const int32_t platformVersion, const int32_t minorVersion, diff --git a/services/bundlemgr/src/inner_event_report.cpp b/services/bundlemgr/src/inner_event_report.cpp index d6f4cbe716..0e6781326a 100644 --- a/services/bundlemgr/src/inner_event_report.cpp +++ b/services/bundlemgr/src/inner_event_report.cpp @@ -49,6 +49,7 @@ constexpr const char* BMS_DISK_SPACE = "BMS_DISK_SPACE"; constexpr const char* APP_CONTROL_RULE = "APP_CONTROL_RULE"; constexpr const char* DB_ERROR = "DB_ERROR"; constexpr const char* DEFAULT_APP = "DEFAULT_APP"; +constexpr const char* QUERY_BUNDLE_INFO = "QUERY_BUNDLE_INFO"; // event params const char* EVENT_PARAM_PNAMEID = "PNAMEID"; @@ -135,6 +136,16 @@ const char* ERROR_CODE = "errorCode"; const char* REBUILD_TYPE = "rebuildType"; const char* COMPONENT_NAME = "hisevent"; const char* PARTITION_NAME = "/data"; +// query event +const char* EVENT_PARAM_FUNC_ID_LIST = "FUNC_ID_LIST"; +const char* EVENT_PARAM_USER_ID_LIST = "USER_ID_LIST"; +const char* EVENT_PARAM_UID_LIST = "UID_LIST"; +const char* EVENT_PARAM_APP_INDEX_LIST = "APP_INDEX_LIST"; +const char* EVENT_PARAM_FLAG_LIST = "FLAG_LIST"; +const char* EVENT_PARAM_BUNDLE_NAME_LIST = "BUNDLE_NAME_LIST"; +const char* EVENT_PARAM_CALLING_UID_LIST = "CALLING_UID_LIST"; +const char* EVENT_PARAM_CALLING_BUNDLE_NAME_LIST = "CALLING_BUNDLE_NAME_LIST"; +const char* EVENT_PARAM_CALLING_APP_ID_LIST = "CALLING_APP_ID_LIST"; const InstallScene INSTALL_SCENE_STR_MAP_KEY[] = { InstallScene::NORMAL, @@ -315,6 +326,10 @@ std::unordered_map [](const EventInfo& eventInfo) { InnerSendDefaultAppEvent(eventInfo); } }, + { BMSEventType::QUERY_BUNDLE_INFO, + [](const EventInfo& eventInfo) { + InnerSendQueryBundleInfoEvent(eventInfo); + } }, }; void InnerEventReport::SendSystemEvent(BMSEventType bmsEventType, const EventInfo& eventInfo) @@ -749,5 +764,22 @@ void InnerEventReport::InnerSystemEventWrite( static_cast(type), keyValues...); } + +void InnerEventReport::InnerSendQueryBundleInfoEvent(const EventInfo& eventInfo) +{ + InnerSystemEventWrite( + QUERY_BUNDLE_INFO, + HiSysEventType::BEHAVIOR, + EVENT_PARAM_FUNC_ID_LIST, eventInfo.funcIdList, + EVENT_PARAM_USER_ID_LIST, eventInfo.userIdList, + EVENT_PARAM_UID_LIST, eventInfo.appIndex, + EVENT_PARAM_APP_INDEX_LIST, eventInfo.appIndexList, + EVENT_PARAM_FLAG_LIST, eventInfo.flagList, + EVENT_PARAM_BUNDLE_NAME_LIST, eventInfo.bundleNameList, + EVENT_PARAM_CALLING_UID_LIST, eventInfo.callingUidList, + EVENT_PARAM_CALLING_BUNDLE_NAME_LIST, eventInfo.callingBundleNameList, + EVENT_PARAM_CALLING_APP_ID_LIST, eventInfo.callingAppIdList, + EVENT_PARAM_ERROR_CODE, eventInfo.errCode,); +} } // namespace AppExecFwk } // namespace OHOS \ No newline at end of file diff --git a/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_kit_service_test.cpp b/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_kit_service_test.cpp index 2569a31729..9f93df37de 100644 --- a/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_kit_service_test.cpp +++ b/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_kit_service_test.cpp @@ -62,6 +62,15 @@ #include "display_power_info.h" #include "battery_srv_client.h" +namespace OHOS { +namespace AppExecFwk { + std::unordered_map GetGlobalQueryEventInfo(); + void AddGlobalQueryEventInfo(const EventInfo &info); + void EraseGlobalQueryEventInfo(ErrCode errCode); + void ClearGlobalQueryEventInfo(); +} +} + using namespace testing::ext; using namespace OHOS; using namespace OHOS::AppExecFwk; @@ -14902,4 +14911,60 @@ HWTEST_F(BmsBundleKitServiceTest, Mgr_Proxy_GetAllShortcutInfoForSelf_0100, Func auto ret = bundleMgrProxy->GetAllShortcutInfoForSelf(shortcutInfos); EXPECT_NE(ret, ERR_OK); } + +/** + * @tc.number: CheckNeedAddEvent_0100 + * @tc.name: test CheckNeedAddEvent + * @tc.desc: 1.Test the CheckNeedAddEvent by BundleMgrHostImpl + */ +HWTEST_F(BmsBundleKitServiceTest, CheckNeedAddEvent_0100, Function | SmallTest | Level1) +{ + auto hostImpl = std::make_unique(); + ASSERT_NE(hostImpl, nullptr); + ClearGlobalQueryEventInfo(); + EventInfo test; + test.errorCode = ERR_BUNDLE_MANAGER_INTERNAL_ERROR; + test.funcIdList = {1}; + test.userIdList = {100}; + test.uidList = {1}; + test.appIndexList = {0}; + test.flagList = {BundleFlag::GET_BUNDLE_DEFAULT}; + test.bundleNameList = {"test.CheckNeedAddEvent_0100"}; + test.callingUidList = {1001}; + test.callingBundleNameList = {"com.test.CheckNeedAddEvent_0100"}; + test.callingAppIdList = {"123asdf"}; + uint64_t cacheSize = 1; + ErrCode ret = 0; + bool res = hostImpl->CheckNeedAddEvent(test); + EXPECT_EQ(res, false); + res = hostImpl->CheckNeedAddEvent(test); + EXPECT_EQ(res, false); +} + +/** + * @tc.number: CheckNeedAddEvent_0200 + * @tc.name: test CheckNeedAddEvent + * @tc.desc: 1.Test the CheckNeedAddEvent by BundleMgrHostImpl + */ +HWTEST_F(BmsBundleKitServiceTest, CheckNeedAddEvent_0200, Function | SmallTest | Level1) +{ + auto hostImpl = std::make_unique(); + ASSERT_NE(hostImpl, nullptr); + ClearGlobalQueryEventInfo(); + EventInfo test; + test.errorCode = ERR_BUNDLE_MANAGER_INTERNAL_ERROR; + test.funcIdList = {1}; + test.userIdList = {100}; + test.uidList = {1}; + test.appIndexList = {0}; + test.flagList = {BundleFlag::GET_BUNDLE_DEFAULT}; + test.bundleNameList = {"test.CheckNeedAddEvent_0100"}; + test.callingUidList = {1001}; + test.callingBundleNameList = {"com.test.CheckNeedAddEvent_0100"}; + test.callingAppIdList = {"123asdf"}; + uint64_t cacheSize = 1; + ErrCode ret = 0; + bool res = hostImpl->CheckNeedAddEvent(test); + EXPECT_EQ(res, false); +} } -- Gitee From 9a8bb698ebecfed00379e3a576ddb0b78fc580e8 Mon Sep 17 00:00:00 2001 From: small_leek Date: Wed, 9 Jul 2025 14:27:39 +0800 Subject: [PATCH 2/3] add ani for SetShortcutVisibleForSelf Signed-off-by: small_leek --- .../shortcut_manager/ani_shortcut_manager.cpp | 26 +++++++++++++++++++ .../ets/@ohos.bundle.shortcutManager.ets | 16 ++++++++++++ interfaces/kits/js/common/napi_constants.h | 2 ++ 3 files changed, 44 insertions(+) diff --git a/interfaces/kits/ani/shortcut_manager/ani_shortcut_manager.cpp b/interfaces/kits/ani/shortcut_manager/ani_shortcut_manager.cpp index 2ebcca46e7..123ed09550 100644 --- a/interfaces/kits/ani/shortcut_manager/ani_shortcut_manager.cpp +++ b/interfaces/kits/ani/shortcut_manager/ani_shortcut_manager.cpp @@ -31,6 +31,30 @@ constexpr const char* PARSE_SHORTCUT_INFO = "ParseShortCutInfo"; constexpr const char* NS_NAME_SHORTCUTMANAGER = "@ohos.bundle.shortcutManager.shortcutManager"; } +static void AniSetShortcutVisibleForSelf(ani_env* env, ani_object info, ani_string aniShortcutId, ani_boolean aniVisible) +{ + APP_LOGD("ani SetShortcutVisibleForSelf called"); + std::string shortcutId; + if (!CommonFunAni::ParseString(env, aniShortcutId, shortcutId)) { + APP_LOGE("shortcutId %{public}s invalid", shortcutId.c_str()); + BusinessErrorAni::ThrowCommonError(env, ERROR_PARAM_CHECK_ERROR, SHORT_CUT_ID, TYPE_STRING); + return nullptr; + } + bool visible = CommonFunAni::AniBooleanToBool(aniVisible); + auto iBundleMgr = CommonFunc::GetBundleMgr(); + if (iBundleMgr == nullptr) { + BusinessErrorAni::ThrowError(env, ERR_APPEXECFWK_SERVICE_NOT_READY, SET_SHORTCUT_VISIBLE_FOR_SELF); + return; + } + ErrCode ret = iBundleMgr->SetShortcutVisibleForSelf(shortcutId, visible); + if (ret != ERR_OK) { + APP_LOGE("SetShortcutVisibleForSelf failed ret:%{public}d, shortcutId:%{public}s, visible:%{public}d", + ret, shortcutId.c_str(), visible); + BusinessErrorAni::ThrowCommonError( + env, CommonFunc::ConvertErrCode(ret), SET_SHORTCUT_VISIBLE_FOR_SELF, Constants::PERMISSION_MANAGER_SHORTCUT); + } +} + static void AniAddDesktopShortcutInfo(ani_env* env, ani_object info, ani_double aniUserId) { APP_LOGD("ani AddDesktopShortcutInfo called"); @@ -155,6 +179,8 @@ ANI_EXPORT ani_status ANI_Constructor(ani_vm* vm, uint32_t* result) reinterpret_cast(AniDeleteDesktopShortcutInfo) }, ani_native_function { "getAllDesktopShortcutInfoNative", nullptr, reinterpret_cast(AniGetAllDesktopShortcutInfo) }, + ani_native_function { "setShortcutVisibleForSelfNative", nullptr, + reinterpret_cast(AniSetShortcutVisibleForSelf) }, }; status = env->Namespace_BindNativeFunctions(kitNs, methods.data(), methods.size()); diff --git a/interfaces/kits/ani/shortcut_manager/ets/@ohos.bundle.shortcutManager.ets b/interfaces/kits/ani/shortcut_manager/ets/@ohos.bundle.shortcutManager.ets index d5c64e1e71..70a68eaa0e 100644 --- a/interfaces/kits/ani/shortcut_manager/ets/@ohos.bundle.shortcutManager.ets +++ b/interfaces/kits/ani/shortcut_manager/ets/@ohos.bundle.shortcutManager.ets @@ -23,6 +23,7 @@ namespace shortcutManager { export native function addDesktopShortcutInfoNative(shortcutInfo: ShortcutInfo, userId: number): void; export native function deleteDesktopShortcutInfoNative(shortcutInfo: ShortcutInfo, userId: number): void; export native function getAllDesktopShortcutInfoNative(userId: number): Array; + export native function setShortcutVisibleForSelfNative(shortcutId: string, visible: boolean): void; function addDesktopShortcutInfo(shortcutInfo: ShortcutInfo, userId: number): Promise { let p = new Promise((resolve: (v: undefined) => void, reject: (error: BusinessError) => void): void => { @@ -70,6 +71,21 @@ namespace shortcutManager { return p; } + function setShortcutVisibleForSelf(shortcutId: string, visible: boolean): Promise { + let p = new Promise((resolve: (v: undefined) => void, reject: (error: BusinessError) => void): void => { + let cb = (): NullishType => { + return shortcutManager.setShortcutVisibleForSelfNative(shortcutId, visible); + } + let p1 = taskpool.execute(cb); + p1.then((): void => { + resolve(undefined); + }, (err: Error): void => { + reject(err as BusinessError); + }); + }); + return p; + } + export type ShortcutInfo = _ShortcutInfo; export type ShortcutWant = _ShortcutWant; export type ParameterItem = _ParameterItem; diff --git a/interfaces/kits/js/common/napi_constants.h b/interfaces/kits/js/common/napi_constants.h index 7428bb5fc1..343248ba82 100644 --- a/interfaces/kits/js/common/napi_constants.h +++ b/interfaces/kits/js/common/napi_constants.h @@ -43,6 +43,7 @@ constexpr const char* TYPE_ARRAY = "array"; constexpr const char* UID = "uid"; constexpr const char* USER_ID = "userId"; +constexpr const char* SHORT_CUT_ID = "shortCutId"; constexpr const char* BUNDLE_NAME = "bundleName"; constexpr const char* MODULE_NAME = "moduleName"; constexpr const char* ABILITY_NAME = "abilityName"; @@ -73,6 +74,7 @@ constexpr const char* RESOURCE_FLAGS = "resourceFlags"; constexpr const char* ADD_DESKTOP_SHORTCUT_INFO = "AddDesktopShortcutInfo"; constexpr const char* DELETE_DESKTOP_SHORTCUT_INFO = "DeleteDesktopShortcutInfo"; constexpr const char* GET_ALL_DESKTOP_SHORTCUT_INFO = "GetAllDesktopShortcutInfo"; +constexpr const char* SET_SHORTCUT_VISIBLE_FOR_SELF = "SetShortcutVisibleForSelf"; } } } -- Gitee From 118068f2c6e3ffde661814c1dc3cf104073c752c Mon Sep 17 00:00:00 2001 From: small_leek Date: Wed, 9 Jul 2025 14:29:47 +0800 Subject: [PATCH 3/3] remove ani Signed-off-by: small_leek --- .../shortcut_manager/ani_shortcut_manager.cpp | 26 -- .../ets/@ohos.bundle.shortcutManager.ets | 16 -- interfaces/kits/js/common/napi_constants.h | 2 - .../bundlemgr/include/bundle_mgr_host_impl.h | 6 +- .../bundlemgr/src/bundle_mgr_host_impl.cpp | 225 ++++++++++-------- services/bundlemgr/src/inner_event_report.cpp | 2 +- .../bms_bundle_data_mgr_nullptr_test.cpp | 16 ++ .../bms_bundle_data_mgr_test_three.cpp | 156 ++++++++++++ .../bms_bundle_kit_service_test.cpp | 65 ----- 9 files changed, 297 insertions(+), 217 deletions(-) diff --git a/interfaces/kits/ani/shortcut_manager/ani_shortcut_manager.cpp b/interfaces/kits/ani/shortcut_manager/ani_shortcut_manager.cpp index 123ed09550..2ebcca46e7 100644 --- a/interfaces/kits/ani/shortcut_manager/ani_shortcut_manager.cpp +++ b/interfaces/kits/ani/shortcut_manager/ani_shortcut_manager.cpp @@ -31,30 +31,6 @@ constexpr const char* PARSE_SHORTCUT_INFO = "ParseShortCutInfo"; constexpr const char* NS_NAME_SHORTCUTMANAGER = "@ohos.bundle.shortcutManager.shortcutManager"; } -static void AniSetShortcutVisibleForSelf(ani_env* env, ani_object info, ani_string aniShortcutId, ani_boolean aniVisible) -{ - APP_LOGD("ani SetShortcutVisibleForSelf called"); - std::string shortcutId; - if (!CommonFunAni::ParseString(env, aniShortcutId, shortcutId)) { - APP_LOGE("shortcutId %{public}s invalid", shortcutId.c_str()); - BusinessErrorAni::ThrowCommonError(env, ERROR_PARAM_CHECK_ERROR, SHORT_CUT_ID, TYPE_STRING); - return nullptr; - } - bool visible = CommonFunAni::AniBooleanToBool(aniVisible); - auto iBundleMgr = CommonFunc::GetBundleMgr(); - if (iBundleMgr == nullptr) { - BusinessErrorAni::ThrowError(env, ERR_APPEXECFWK_SERVICE_NOT_READY, SET_SHORTCUT_VISIBLE_FOR_SELF); - return; - } - ErrCode ret = iBundleMgr->SetShortcutVisibleForSelf(shortcutId, visible); - if (ret != ERR_OK) { - APP_LOGE("SetShortcutVisibleForSelf failed ret:%{public}d, shortcutId:%{public}s, visible:%{public}d", - ret, shortcutId.c_str(), visible); - BusinessErrorAni::ThrowCommonError( - env, CommonFunc::ConvertErrCode(ret), SET_SHORTCUT_VISIBLE_FOR_SELF, Constants::PERMISSION_MANAGER_SHORTCUT); - } -} - static void AniAddDesktopShortcutInfo(ani_env* env, ani_object info, ani_double aniUserId) { APP_LOGD("ani AddDesktopShortcutInfo called"); @@ -179,8 +155,6 @@ ANI_EXPORT ani_status ANI_Constructor(ani_vm* vm, uint32_t* result) reinterpret_cast(AniDeleteDesktopShortcutInfo) }, ani_native_function { "getAllDesktopShortcutInfoNative", nullptr, reinterpret_cast(AniGetAllDesktopShortcutInfo) }, - ani_native_function { "setShortcutVisibleForSelfNative", nullptr, - reinterpret_cast(AniSetShortcutVisibleForSelf) }, }; status = env->Namespace_BindNativeFunctions(kitNs, methods.data(), methods.size()); diff --git a/interfaces/kits/ani/shortcut_manager/ets/@ohos.bundle.shortcutManager.ets b/interfaces/kits/ani/shortcut_manager/ets/@ohos.bundle.shortcutManager.ets index 70a68eaa0e..d5c64e1e71 100644 --- a/interfaces/kits/ani/shortcut_manager/ets/@ohos.bundle.shortcutManager.ets +++ b/interfaces/kits/ani/shortcut_manager/ets/@ohos.bundle.shortcutManager.ets @@ -23,7 +23,6 @@ namespace shortcutManager { export native function addDesktopShortcutInfoNative(shortcutInfo: ShortcutInfo, userId: number): void; export native function deleteDesktopShortcutInfoNative(shortcutInfo: ShortcutInfo, userId: number): void; export native function getAllDesktopShortcutInfoNative(userId: number): Array; - export native function setShortcutVisibleForSelfNative(shortcutId: string, visible: boolean): void; function addDesktopShortcutInfo(shortcutInfo: ShortcutInfo, userId: number): Promise { let p = new Promise((resolve: (v: undefined) => void, reject: (error: BusinessError) => void): void => { @@ -71,21 +70,6 @@ namespace shortcutManager { return p; } - function setShortcutVisibleForSelf(shortcutId: string, visible: boolean): Promise { - let p = new Promise((resolve: (v: undefined) => void, reject: (error: BusinessError) => void): void => { - let cb = (): NullishType => { - return shortcutManager.setShortcutVisibleForSelfNative(shortcutId, visible); - } - let p1 = taskpool.execute(cb); - p1.then((): void => { - resolve(undefined); - }, (err: Error): void => { - reject(err as BusinessError); - }); - }); - return p; - } - export type ShortcutInfo = _ShortcutInfo; export type ShortcutWant = _ShortcutWant; export type ParameterItem = _ParameterItem; diff --git a/interfaces/kits/js/common/napi_constants.h b/interfaces/kits/js/common/napi_constants.h index 343248ba82..7428bb5fc1 100644 --- a/interfaces/kits/js/common/napi_constants.h +++ b/interfaces/kits/js/common/napi_constants.h @@ -43,7 +43,6 @@ constexpr const char* TYPE_ARRAY = "array"; constexpr const char* UID = "uid"; constexpr const char* USER_ID = "userId"; -constexpr const char* SHORT_CUT_ID = "shortCutId"; constexpr const char* BUNDLE_NAME = "bundleName"; constexpr const char* MODULE_NAME = "moduleName"; constexpr const char* ABILITY_NAME = "abilityName"; @@ -74,7 +73,6 @@ constexpr const char* RESOURCE_FLAGS = "resourceFlags"; constexpr const char* ADD_DESKTOP_SHORTCUT_INFO = "AddDesktopShortcutInfo"; constexpr const char* DELETE_DESKTOP_SHORTCUT_INFO = "DeleteDesktopShortcutInfo"; constexpr const char* GET_ALL_DESKTOP_SHORTCUT_INFO = "GetAllDesktopShortcutInfo"; -constexpr const char* SET_SHORTCUT_VISIBLE_FOR_SELF = "SetShortcutVisibleForSelf"; } } } diff --git a/services/bundlemgr/include/bundle_mgr_host_impl.h b/services/bundlemgr/include/bundle_mgr_host_impl.h index 498efedb42..37c7c6cbeb 100644 --- a/services/bundlemgr/include/bundle_mgr_host_impl.h +++ b/services/bundlemgr/include/bundle_mgr_host_impl.h @@ -1196,10 +1196,8 @@ private: bool GetLabelFromCache(const std::string &cacheKey, const std::string &abilityName, const std::unordered_map> &resourceCache, std::string &label); - bool SendQueryBundleInfoEvent(const std::string &funcName, int32_t userId, int32_t uid, - int32_t appIndex, int32_t flag, const std::string &bundleName, - ErrCode errCode, bool reportNow); - bool CheckNeedAddEvent(const EventInfo &query); + bool SendQueryBundleInfoEvent(EventInfo &query, int32_t intervalTime, bool reportNow); + bool CheckNeedAddEvent(const EventInfo &query, size_t maxEventSize); bool GetCallingInfo(int32_t callingUid, std::string &callingBundleName, std::string &callingAppId); std::atomic isBrokerServiceExisted_ = false; // used to record system event infos diff --git a/services/bundlemgr/src/bundle_mgr_host_impl.cpp b/services/bundlemgr/src/bundle_mgr_host_impl.cpp index bd2cb16c58..54bbeb4901 100644 --- a/services/bundlemgr/src/bundle_mgr_host_impl.cpp +++ b/services/bundlemgr/src/bundle_mgr_host_impl.cpp @@ -88,8 +88,8 @@ const std::string RESOURCE_NOT_SUPPORT = const uint8_t JSON_INDENTATION = 4; const uint64_t BAD_CONTEXT_ID = 0; const uint64_t VECTOR_SIZE_MAX = 200; -const uint64_t MAX_QUERY_EVENT_REPORT_ONCE = 3; -const int64_t ONE_DAY = 10; +const size_t MAX_QUERY_EVENT_REPORT_ONCE = 100; +const int64_t ONE_DAY = 86400; const std::unordered_map QUERY_FUNC_MAP = { {"GetNameForUid", 1}, {"GetBundleNameForUid", 2}, @@ -102,31 +102,40 @@ const std::unordered_map QUERY_FUNC_MAP = { std::shared_mutex g_queryEventMutex; std::unordered_map g_queryEventList; -std::unordered_map GetGlobalQueryEventInfo() +void ClearGlobalQueryEventInfo() { std::unique_lock queryEventMutex(g_queryEventMutex); - return g_queryEventList; + g_queryEventList.clear(); } -void AddGlobalQueryEventInfo(const EventInfo &info) +void EraseQueryEventInfo(ErrCode error) { std::unique_lock queryEventMutex(g_queryEventMutex); - if (g_queryEventList.find(info.errorCode) == g_queryEventList.end()) { - g_queryEventList[info.errorCode] = info; - return; - } + g_queryEventList.erase(error); } -void EraseGlobalQueryEventInfo(ErrCode errCode) +EventInfo GetQueryEventInfo(ErrCode error) { + EventInfo info; std::unique_lock queryEventMutex(g_queryEventMutex); - g_queryEventList.erase(errCode); + if (g_queryEventList.find(error) != g_queryEventList.end()) { + return g_queryEventList[error]; + } + return info; } -void ClearGlobalQueryEventInfo() +EventInfo PrepareQueryEvent(ErrCode errCode, const std::string &bundleName, const std::string &func, + int32_t uid, int32_t userId, int32_t appIndex, int32_t flags) { - std::unique_lock queryEventMutex(g_queryEventMutex); - g_queryEventList.clear(); + EventInfo info; + info.errCode = errCode; + info.funcIdList = {QUERY_FUNC_MAP.at(func)}; + info.uidList = {uid}; + info.userIdList = {userId}; + info.appIndexList = {appIndex}; + info.flagList = {flags}; + info.bundleNameList = {bundleName}; + return info; } bool BundleMgrHostImpl::GetApplicationInfo( @@ -256,40 +265,42 @@ bool BundleMgrHostImpl::GetBundleInfo( LOG_D(BMS_TAG_QUERY, "start GetBundleInfo, bundleName : %{public}s, flags : %{public}d, userId : %{public}d", bundleName.c_str(), flags, userId); - int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCTION_GET_BUNDLE_INFO); + HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__); + int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCATION_GET_BUNDLE_INFO); ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); }); + int32_t intervalTime = ONE_DAY; // API9 need to be system app if (!BundlePermissionMgr::IsSystemApp() && !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) { LOG_D(BMS_TAG_QUERY, "non-system app calling system api"); + EventInfo info = PrepareQueryEvent(ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED, bundleName, + "GetBundleInfo", -1, userId, 0, flags); + SendQueryBundleInfoEvent(info, intervalTime, false); return true; } if (!BundlePermissionMgr::VerifyCallingPermissionsForAll({Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED, Constants::PERMISSION_GET_BUNDLE_INFO}) && !BundlePermissionMgr::IsBundleSelfCalling(bundleName)) { LOG_E(BMS_TAG_QUERY, "verify permission failed"); + EventInfo info = PrepareQueryEvent(ERR_BUNDLE_MANAGER_PERMISSION_DENIED, bundleName, + "GetBundleInfo", -1, userId, 0, flags); + SendQueryBundleInfoEvent(info, intervalTime, false); return false; } LOG_D(BMS_TAG_QUERY, "verify permission success, begin to GetBundleInfo"); auto dataMgr = GetDataMgrFromService(); if (dataMgr == nullptr) { LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr"); - SendQueryBundleInfoEvent("GetBundleInfo", userId, 0, 0, flags, - bundleName, ERR_BUNDLE_MANAGER_INTERNAL_ERROR, true); + EventInfo info = PrepareQueryEvent(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, bundleName, + "GetBundleInfo", -1, userId, 0, flags); + SendQueryBundleInfoEvent(info, intervalTime, true); return false; } bool res = dataMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId); if (!res) { if (isBrokerServiceExisted_) { auto bmsExtensionClient = std::make_shared(); - ErrCode ret = bmsExtensionClient->GetBundleInfo(bundleName, flags, bundleInfo, userId) == ERR_OK; - if (ret != ERR_OK) { - SendQueryBundleInfoEvent("GetBundleInfo", userId, 0, 0, flags, - bundleName, ret, false); - } - } else { - SendQueryBundleInfoEvent("GetBundleInfo", userId, 0, 0, flags, - bundleName, ERR_BUNDLE_MANAGER_INVALID_USER_ID, false); + return bmsExtensionClient->GetBundleInfo(bundleName, flags, bundleInfo, userId) == ERR_OK; } } return res; @@ -316,10 +327,11 @@ ErrCode BundleMgrHostImpl::GetBundleInfoV9( const std::string &bundleName, int32_t flags, BundleInfo &bundleInfo, int32_t userId) { HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr); - int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCTION_GET_BUNDLE_INFO_V9); - ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); }); LOG_D(BMS_TAG_QUERY, "GetBundleInfoV9, bundleName:%{public}s, flags:%{public}d, userId:%{public}d", bundleName.c_str(), flags, userId); + HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__); + int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCATION_GET_BUNDLE_INFO_V9); + ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); }); bool permissionVerify = [bundleName]() { if (BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) { return true; @@ -333,27 +345,31 @@ ErrCode BundleMgrHostImpl::GetBundleInfoV9( } return false; }(); + // info.lastReportEventTime = BundleUtil::GetCurrentTime(); + int32_t intervalTime = ONE_DAY; if (!permissionVerify) { APP_LOGE("verify permission failed"); - SendQueryBundleInfoEvent("GetBundleInfoV9", userId, 0, 0, flags, - bundleName, ERR_BUNDLE_MANAGER_PERMISSION_DENIED, false); return ERR_BUNDLE_MANAGER_PERMISSION_DENIED; } LOG_D(BMS_TAG_QUERY, "verify permission success, begin to GetBundleInfoV9"); auto dataMgr = GetDataMgrFromService(); if (dataMgr == nullptr) { LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr"); - SendQueryBundleInfoEvent("GetBundleInfoV9", userId, 0, 0, flags, - bundleName, ERR_BUNDLE_MANAGER_INTERNAL_ERROR, true); + EventInfo info = PrepareQueryEvent(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, bundleName, + "GetBundleInfoV9", -1, userId, 0, flags); + SendQueryBundleInfoEvent(info, intervalTime, true); } auto res = dataMgr->GetBundleInfoV9(bundleName, flags, bundleInfo, userId); if (res != ERR_OK) { if (isBrokerServiceExisted_) { auto bmsExtensionClient = std::make_shared(); - if (bmsExtensionClient->GetBundleInfo(bundleName, flags, bundleInfo, userId, true) == ERR_OK) { + res = bmsExtensionClient->GetBundleInfo(bundleName, flags, bundleInfo, userId, true); + if (res == ERR_OK) { return ERR_OK; } } + EventInfo info = PrepareQueryEvent(res, bundleName, "GetBundleInfoV9", -1, userId, 0, flags); + SendQueryBundleInfoEvent(info, intervalTime, false); } return res; } @@ -573,24 +589,38 @@ ErrCode BundleMgrHostImpl::GetBundleInfosV9(int32_t flags, std::vectorGetBundleNameForUid(uid, bundleName); + bool res = dataMgr->GetBundleNameForUid(uid, bundleName); + if (!res) { + EventInfo info = PrepareQueryEvent(ERR_BUNDLE_MANAGER_INVALID_UID, "None", + "GetBundleNameForUid", uid, -1, 0, -1); + SendQueryBundleInfoEvent(info, intervalTime, false); + } + return res; } bool BundleMgrHostImpl::GetBundlesForUid(const int uid, std::vector &bundleNames) @@ -617,7 +647,7 @@ ErrCode BundleMgrHostImpl::GetNameForUid(const int uid, std::string &name) { HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr); APP_LOGD("start GetNameForUid, uid : %{public}d", uid); - int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCTION_GET_NAME_FOR_UID); + int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCATION_GET_NAME_FOR_UID); ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); }); bool permissionVerify = []() { if (BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) { @@ -630,17 +660,17 @@ ErrCode BundleMgrHostImpl::GetNameForUid(const int uid, std::string &name) } return false; }(); + int32_t intervalTime = ONE_DAY; if (!permissionVerify) { APP_LOGE("verify permission failed"); - SendQueryBundleInfoEvent("GetNameForUid", -1, uid, 0, 0, - "", ERR_BUNDLE_MANAGER_PERMISSION_DENIED, true); return ERR_BUNDLE_MANAGER_PERMISSION_DENIED; } auto dataMgr = GetDataMgrFromService(); if (dataMgr == nullptr) { APP_LOGE("DataMgr is nullptr"); - SendQueryBundleInfoEvent("GetNameForUid", -1, uid, 0, 0, - "", ERR_BUNDLE_MANAGER_INTERNAL_ERROR, true); + EventInfo info = PrepareQueryEvent(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, "None", + "GetNameForUid", uid, -1, 0, -1); + SendQueryBundleInfoEvent(info, intervalTime, true); return ERR_BUNDLE_MANAGER_INTERNAL_ERROR; } auto ret = dataMgr->GetNameForUid(uid, name); @@ -649,12 +679,12 @@ ErrCode BundleMgrHostImpl::GetNameForUid(const int uid, std::string &name) auto bmsExtensionClient = std::make_shared(); ret = bmsExtensionClient->GetBundleNameByUid(uid, name); if (ret != ERR_OK) { - SendQueryBundleInfoEvent("GetNameForUid", -1, uid, 0, 0, "", ERR_BUNDLE_MANAGER_INVALID_UID, true); - return ERR_BUNDLE_MANAGER_INVALID_UID; + ret = ERR_BUNDLE_MANAGER_INVALID_UID; } - } else { - SendQueryBundleInfoEvent("GetNameForUid", -1, uid, 0, 0, "", ret, true); } + EventInfo info = PrepareQueryEvent(ret, "None", + "GetNameForUid", uid, -1, 0, -1); + SendQueryBundleInfoEvent(info, intervalTime, false); } return ret; } @@ -3785,10 +3815,9 @@ ErrCode BundleMgrHostImpl::GetAppProvisionInfo(const std::string &bundleName, in { APP_LOGD("begin to GetAppProvisionInfo bundleName: %{public}s, userId: %{public}d", bundleName.c_str(), userId); + int32_t intervalTime = ONE_DAY; if (!BundlePermissionMgr::IsSystemApp()) { APP_LOGE("non-system app calling system api"); - SendQueryBundleInfoEvent("GetAppProvisionInfo", userId, -1, 0, 0, - bundleName, ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED, true); return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED; } if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED) && @@ -3799,14 +3828,15 @@ ErrCode BundleMgrHostImpl::GetAppProvisionInfo(const std::string &bundleName, in auto dataMgr = GetDataMgrFromService(); if (dataMgr == nullptr) { APP_LOGE("DataMgr is nullptr"); - SendQueryBundleInfoEvent("GetAppProvisionInfo", userId, -1, 0, 0, - bundleName, ERR_BUNDLE_MANAGER_INTERNAL_ERROR, true); + EventInfo info = PrepareQueryEvent(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, bundleName, + "GetAppProvisionInfo", -1, userId, 0, -1); + SendQueryBundleInfoEvent(info, intervalTime, true); return ERR_BUNDLE_MANAGER_INTERNAL_ERROR; } ErrCode ret = dataMgr->GetAppProvisionInfo(bundleName, userId, appProvisionInfo); if (ret != ERR_OK) { - SendQueryBundleInfoEvent("GetAppProvisionInfo", userId, -1, 0, 0, - bundleName, ret, false); + EventInfo info = PrepareQueryEvent(ret, bundleName, "GetAppProvisionInfo", -1, userId, 0, -1); + SendQueryBundleInfoEvent(info, intervalTime, false); } return ret; } @@ -5840,26 +5870,37 @@ ErrCode BundleMgrHostImpl::GetAllShortcutInfoForSelf(std::vector & return dataMgr->GetAllShortcutInfoForSelf(shortcutInfos); } -bool BundleMgrHostImpl::CheckNeedAddEvent(const EventInfo &query) +bool BundleMgrHostImpl::CheckNeedAddEvent(const EventInfo &query, size_t maxEventSize) { - if (g_queryEventList.find(query.errorCode) == g_queryEventList.end()) { - g_queryEventList[query.errorCode] = query; - return false; + std::unique_lock queryEventMutex(g_queryEventMutex); + if (g_queryEventList.find(query.errCode) == g_queryEventList.end()) { + g_queryEventList[query.errCode] = query; + g_queryEventList[query.errCode].lastReportEventTime = BundleUtil::GetCurrentTime(); + return true; } EventInfo info = g_queryEventList[query.errCode]; - if (info.bundleNameList.size() >= MAX_QUERY_EVENT_REPORT_ONCE) { + if (info.bundleNameList.size() >= maxEventSize) { return false; } - if (std::find(info.funcIdList.begin(), info.funcIdList.end(), query.funcIdList[0]) == info.funcIdList.end()|| + if (std::find(info.funcIdList.begin(), info.funcIdList.end(), query.funcIdList[0]) == info.funcIdList.end() || std::find(info.userIdList.begin(), info.userIdList.end(), query.userIdList[0]) == info.userIdList.end() || std::find(info.uidList.begin(), info.uidList.end(), query.uidList[0]) == info.uidList.end() || std::find(info.appIndexList.begin(), info.appIndexList.end(), query.appIndexList[0]) == info.appIndexList.end() || std::find(info.flagList.begin(), info.flagList.end(), query.flagList[0]) == info.flagList.end() || - std::find(info.bundleNameList.begin(), info.bundleNameList.end(), query.bundleNameList[0]) == - info.bundleNameList.end() || + std::find(info.bundleNameList.begin(), info.bundleNameList.end(), + query.bundleNameList[0]) == info.bundleNameList.end() || std::find(info.callingUidList.begin(), info.callingUidList.end(), query.callingUidList[0]) == info.callingUidList.end()) { + g_queryEventList[query.errCode].funcIdList.push_back(query.funcIdList.[0]); + g_queryEventList[query.errCode].userIdList.push_back(query.userIdList.[0]); + g_queryEventList[query.errCode].uidList.push_back(query.uidList.[0]); + g_queryEventList[query.errCode].appIndexList.push_back(query.appIndexList.[0]); + g_queryEventList[query.errCode].flagList.push_back(query.flagList.[0]); + g_queryEventList[query.errCode].bundleNameList.push_back(query.bundleNameList.[0]); + g_queryEventList[query.errCode].callingUidList.push_back(query.callingUidList.[0]); + g_queryEventList[query.errCode].callingBundleNameList.push_back(query.callingBundleNameList.[0]); + g_queryEventList[query.errCode].callingAppIdList.push_back(query.callingAppIdList.[0]); return true; } return false; @@ -5881,64 +5922,42 @@ bool BundleMgrHostImpl::GetCallingInfo(int32_t callingUid, std::string &callingB callingUid / Constants::BASE_USER_RANGE)) { LOG_E(BMS_TAG_INSTALLER, "GetBundleInfo failed, bundleName: %{public}s", callingBundleName.c_str()); } + LOG_D(BMS_TAG_INSTALLER, "get callingBundleName: %{public}s, callingAppId:%{public}s", + callingBundleName.c_str(), callingAppId.c_str()); callingAppId = bundleInfo.appId; return true; } -bool BundleMgrHostImpl::SendQueryBundleInfoEvent(const std::string &funcName, int32_t userId, int32_t uid, - int32_t appIndex, int32_t flag, const std::string &bundleName, ErrCode errCode, bool reportNow) +bool BundleMgrHostImpl::SendQueryBundleInfoEvent(EventInfo &query, int32_t intervalTime, bool reportNow) { int32_t callingUid = IPCSkeleton::GetCallingUid(); - APP_LOGI("start SendQueryBundleInfoEvent, funcName:%{public}s, bundleName:%{public}s, flag:%{public}d, userId:%{public}d, uid:%{public}d, appindex:%{public}d, callinguid:%{public}d, errCode:%{public}d", funcName.c_str(), bundleName.c_str(), flag, userId, uid, appIndex, callingUid, errCode); - if (QUERY_FUNC_MAP.find(funcName) == QUERY_FUNC_MAP.end()) { - LOG_E(BMS_TAG_QUERY, "No Need to report for func: %{public}s", funcName.c_str()); - return false; - } + APP_LOGI("start, -f:%{public}d, -c:%{public}d, -e:%{public}d", + query.funcIdList.front(), callingUid, query.errCode); // get calling bundle info - std::string callingBundleName; - std::string callingAppId; + std::string callingBundleName = "None"; + std::string callingAppId = "None"; GetCallingInfo(callingUid, callingBundleName, callingAppId); - - EventInfo query; - query.errCode = errCode; - query.funcIdList.push_back(QUERY_FUNC_MAP.at(funcName)); - query.userIdList.push_back(userId); - query.uidList.push_back(uid); - query.appIndexList.push_back(appIndex); - query.flagList.push_back(flag); - query.bundleNameList.push_back(bundleName); - query.callingUidList.push_back(callingUid); - query.callingBundleNameList.push_back(callingBundleName); - query.callingAppIdList.push_back(callingAppId); - + query.callingUidList = {callingUid} + query.callingBundleNameList = {callingBundleName}; + query.callingAppIdList = {callingAppId}; // check report now if (reportNow) { EventReport::SendSystemEvent(BMSEventType::QUERY_BUNDLE_INFO, query); - APP_LOGI("SendSystemEvent:QUERY_BUNDLE_INFO begin1 for funcName:%{public}s, errCode:%{public}d", - funcName.c_str(), query.errCode); + APP_LOGI("SendSystemEvent now"); return true; } - - int32_t funcId = QUERY_FUNC_MAP.at(funcName); - std::unique_lock queryEventMutex(g_queryEventMutex); - bool needAddEvent = CheckNeedAddEvent(query); - if (needAddEvent) { - g_queryEventList[errCode].funcIdList.push_back(funcId); - g_queryEventList[errCode].userIdList.push_back(userId); - g_queryEventList[errCode].uidList.push_back(uid); - g_queryEventList[errCode].appIndexList.push_back(appIndex); - g_queryEventList[errCode].flagList.push_back(flag); - g_queryEventList[errCode].bundleNameList.push_back(bundleName); - g_queryEventList[errCode].callingUidList.push_back(callingUid); - g_queryEventList[errCode].callingBundleNameList.push_back(callingBundleName); - g_queryEventList[errCode].callingAppIdList.push_back(callingAppId); - } - if (g_queryEventList[errCode].lastReportEventTime == 0 || - (BundleUtil::GetCurrentTime() - g_queryEventList[errCode].lastReportEventTime) >= ONE_DAY) { - APP_LOGI("SendSystemEvent:QUERY_BUNDLE_INFO begin1 for funcName:%{public}s, errCode:%{public}d", - funcName.c_str(), g_queryEventList[errCode].errCode); - EventReport::SendSystemEvent(BMSEventType::QUERY_BUNDLE_INFO, g_queryEventList[errCode]); - g_queryEventList.erase(errCode); + + if (CheckNeedAddEvent(query, MAX_QUERY_EVENT_REPORT_ONCE)) { + APP_LOGI("only add record for: %{public}d", query.errCode); + return false; + } + + EventInfo reportInfo = GetQueryEventInfo(query.errCode); + if (reportInfo.bundleNameList.size() >= MAX_QUERY_EVENT_REPORT_ONCE || + (BundleUtil::GetCurrentTime() - reportInfo.lastReportEventTime) >= intervalTime) { + APP_LOGI("SendSystemEvent for :%{public}d", reportInfo.errCode); + EventReport::SendSystemEvent(BMSEventType::QUERY_BUNDLE_INFO, reportInfo); + EraseQueryEventInfo(reportInfo.errCode); return true; } return false; diff --git a/services/bundlemgr/src/inner_event_report.cpp b/services/bundlemgr/src/inner_event_report.cpp index 0e6781326a..06b4044063 100644 --- a/services/bundlemgr/src/inner_event_report.cpp +++ b/services/bundlemgr/src/inner_event_report.cpp @@ -779,7 +779,7 @@ void InnerEventReport::InnerSendQueryBundleInfoEvent(const EventInfo& eventInfo) EVENT_PARAM_CALLING_UID_LIST, eventInfo.callingUidList, EVENT_PARAM_CALLING_BUNDLE_NAME_LIST, eventInfo.callingBundleNameList, EVENT_PARAM_CALLING_APP_ID_LIST, eventInfo.callingAppIdList, - EVENT_PARAM_ERROR_CODE, eventInfo.errCode,); + EVENT_PARAM_ERROR_CODE, eventInfo.errCode); } } // namespace AppExecFwk } // namespace OHOS \ No newline at end of file diff --git a/services/bundlemgr/test/unittest/bms_bundle_data_mgr_nullptr_test/bms_bundle_data_mgr_nullptr_test.cpp b/services/bundlemgr/test/unittest/bms_bundle_data_mgr_nullptr_test/bms_bundle_data_mgr_nullptr_test.cpp index 6b1b8aeeb4..16053c2274 100644 --- a/services/bundlemgr/test/unittest/bms_bundle_data_mgr_nullptr_test/bms_bundle_data_mgr_nullptr_test.cpp +++ b/services/bundlemgr/test/unittest/bms_bundle_data_mgr_nullptr_test/bms_bundle_data_mgr_nullptr_test.cpp @@ -1416,6 +1416,22 @@ HWTEST_F(BmsBundleDataMgrNullptrTest, RdbDataManager_0003, Function | MediumTest EXPECT_EQ(ret13, nullptr); } +/** + * @tc.number: GetCallingInfo_0001 + * @tc.name: GetCallingInfo + * @tc.desc: test GetCallingInfo of BundleMgrHostImpl + */ +HWTEST_F(BmsBundleDataMgrNullptrTest, GetCallingInfo_0001, Function | SmallTest | Level1) +{ + std::shared_ptr localBundleMgrHostImpl = std::make_shared(); + ASSERT_NE(localBundleMgrHostImpl, nullptr); + int32_t callingUid = 0; + std::string callingBundleName; + std::string callingAppId; + bool ret = localBundleMgrHostImpl->GetCallingInfo(callingUid, callingBundleName, callingAppId); + EXPECT_EQ(ret, false); +} + /** * @tc.number: RdbDataManager_0004 * @tc.name: test InsertData diff --git a/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_data_mgr_test_three.cpp b/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_data_mgr_test_three.cpp index 0c0b663018..f54583eb9a 100644 --- a/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_data_mgr_test_three.cpp +++ b/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_data_mgr_test_three.cpp @@ -60,6 +60,13 @@ #include "want.h" #include "user_unlocked_event_subscriber.h" + +namespace OHOS { +namespace AppExecFwk { + void ClearGlobalQueryEventInfo(); +} +} + using namespace testing::ext; using namespace OHOS; using namespace OHOS::AppExecFwk; @@ -157,6 +164,8 @@ const std::string TEST_URI_HTTP = "http://www.test.com"; const std::string META_DATA_SHORTCUTS_NAME = "ohos.ability.shortcuts"; constexpr int32_t MOCK_BUNDLE_MGR_EXT_FLAG = 10; const std::string BMS_EXTENSION_PATH = "/system/etc/app/bms-extensions.json"; +const int32_t TEST_QUERY_EVENT_UID = 20013999; +const int32_t TEST_QUERY_EVENT_BUNDLE_ID = 13999; const nlohmann::json APP_LIST0 = R"( { "app_list": [ @@ -3456,6 +3465,153 @@ HWTEST_F(BmsBundleDataMgrTest3, GetPluginBundleInfo_0001, Function | MediumTest EXPECT_EQ(result, false); } +/** + * @tc.number: CheckNeedAddEvent_0100 + * @tc.name: test CheckNeedAddEvent + * @tc.desc: 1.Test the CheckNeedAddEvent by BundleMgrHostImpl + */ +HWTEST_F(BmsBundleDataMgrTest3, CheckNeedAddEvent_0100, Function | SmallTest | Level1) +{ + ASSERT_NE(bundleMgrHostImpl_, nullptr); + ClearGlobalQueryEventInfo(); + size_t maxEvent = 7; + EventInfo test; + test.errCode = ERR_BUNDLE_MANAGER_INTERNAL_ERROR; + test.funcIdList = {1}; + test.userIdList = {100}; + test.uidList = {1}; + test.appIndexList = {0}; + test.flagList = {BundleFlag::GET_BUNDLE_DEFAULT}; + test.bundleNameList = {"test.CheckNeedAddEvent_0100"}; + test.callingUidList = {1001}; + test.callingBundleNameList = {"com.test.CheckNeedAddEvent_0100"}; + test.callingAppIdList = {"123asdf"}; + bool res = bundleMgrHostImpl_->CheckNeedAddEvent(test, maxEvent); + EXPECT_EQ(res, true); + + res = bundleMgrHostImpl_->CheckNeedAddEvent(test, maxEvent); + EXPECT_EQ(res, false); + + test.funcIdList = {2}; + res = bundleMgrHostImpl_->CheckNeedAddEvent(test, maxEvent); + EXPECT_EQ(res, true); + + test.userIdList = {2}; + res = bundleMgrHostImpl_->CheckNeedAddEvent(test, maxEvent); + EXPECT_EQ(res, true); + + test.appIndexList = {2}; + res = bundleMgrHostImpl_->CheckNeedAddEvent(test, maxEvent); + EXPECT_EQ(res, true); + + test.flagList = {BundleFlag::GET_BUNDLE_WITH_ABILITIES}; + res = bundleMgrHostImpl_->CheckNeedAddEvent(test, maxEvent); + EXPECT_EQ(res, true); + + test.bundleNameList = {"com.test.CheckNeedAddEvent_0100_2"}; + res = bundleMgrHostImpl_->CheckNeedAddEvent(test, maxEvent); + EXPECT_EQ(res, true); + + test.callingUidList = {1002}; + res = bundleMgrHostImpl_->CheckNeedAddEvent(test, maxEvent); + EXPECT_EQ(res, true); + + test.callingUidList = {1002}; + res = bundleMgrHostImpl_->CheckNeedAddEvent(test, maxEvent); + EXPECT_EQ(res, false); + ClearGlobalQueryEventInfo(); +} + +/** + * @tc.number: GetCallingInfo_0100 + * @tc.name: test GetCallingInfo + * @tc.desc: 1.Test the GetCallingInfo by BundleMgrHostImpl + */ +HWTEST_F(BmsBundleDataMgrTest3, GetCallingInfo_0100, Function | SmallTest | Level1) +{ + ASSERT_NE(bundleMgrHostImpl_, nullptr); + auto dataMgr = bundleMgrHostImpl_->GetDataMgrFromService(); + ASSERT_NE(dataMgr, nullptr); + int32_t callingUid = 0; + std::string callingBundleName; + std::string callingAppId; + bool ret = bundleMgrHostImpl_->GetCallingInfo(callingUid, callingBundleName, callingAppId); + EXPECT_EQ(ret, true); + callingUid = TEST_QUERY_EVENT_UID; + ret = bundleMgrHostImpl_->GetCallingInfo(callingUid, callingBundleName, callingAppId); + EXPECT_EQ(ret, true); + + std::string bundleName = "com.GetCallingInfo_0100.test"; + InnerBundleInfo info; + BundleInfo bundleInfo; + bundleInfo.name = bundleName; + bundleInfo.applicationInfo.name = bundleName; + ApplicationInfo applicationInfo; + applicationInfo.name = bundleName; + applicationInfo.bundleName = bundleName; + info.SetBaseBundleInfo(bundleInfo); + info.SetBaseApplicationInfo(applicationInfo); + InnerBundleUserInfo innerBundleUserInfo; + innerBundleUserInfo.uid = TEST_QUERY_EVENT_UID; + innerBundleUserInfo.bundleName = bundleName; + innerBundleUserInfo.bundleUserInfo.userId = 100; + info.AddInnerBundleUserInfo(innerBundleUserInfo); + + dataMgr->UpdateBundleInstallState(bundleName, InstallState::INSTALL_START); + dataMgr->AddInnerBundleInfo(bundleName, info); + + int32_t testBundleId = TEST_QUERY_EVENT_BUNDLE_ID; + dataMgr->bundleIdMap_.insert(std::pair(testBundleId, bundleName)); + ret = bundleMgrHostImpl_->GetCallingInfo(callingUid, callingBundleName, callingAppId); + EXPECT_EQ(ret, true); + EXPECT_EQ(callingBundleName, bundleName); +} + +/** + * @tc.number: SendQueryBundleInfoEvent_0100 + * @tc.name: test SendQueryBundleInfoEvent + * @tc.desc: 1.Test the SendQueryBundleInfoEvent by BundleMgrHostImpl + */ +HWTEST_F(BmsBundleDataMgrTest3, SendQueryBundleInfoEvent_0100, Function | SmallTest | Level1) +{ + ASSERT_NE(bundleMgrHostImpl_, nullptr); + auto dataMgr = bundleMgrHostImpl_->GetDataMgrFromService(); + ASSERT_NE(dataMgr, nullptr); + ClearGlobalQueryEventInfo(); + size_t maxEvent = 7; + EventInfo test; + test.errCode = ERR_BUNDLE_MANAGER_INTERNAL_ERROR; + test.funcIdList = {1}; + test.userIdList = {100}; + test.uidList = {1}; + test.appIndexList = {0}; + test.flagList = {BundleFlag::GET_BUNDLE_DEFAULT}; + test.bundleNameList = {"test.SendQueryBundleInfoEvent_0100"}; + test.callingUidList = {1001}; + test.callingBundleNameList = {"com.test.SendQueryBundleInfoEvent_0100"}; + test.callingAppIdList = {"123asdf"}; + test.lastReportEventTime = 0; + + // test reportNow is true + bool ret = bundleMgrHostImpl_->SendQueryBundleInfoEvent(test, 0, true); + EXPECT_EQ(ret, true); + + // test reportNow is false and lastReportEventTime is 0 + ClearGlobalQueryEventInfo(); + ret = bundleMgrHostImpl_->SendQueryBundleInfoEvent(test, 0, false); + EXPECT_EQ(ret, true); + + // test reportNow is false and wait for more than 24h + ClearGlobalQueryEventInfo(); + ret = bundleMgrHostImpl_->SendQueryBundleInfoEvent(test, 1, false); + EXPECT_EQ(ret, true); + + // test reportNow is false, not ready to report + ClearGlobalQueryEventInfo(); + ret = bundleMgrHostImpl_->SendQueryBundleInfoEvent(test, BundleUtil::GetCurrentTime(), false); + EXPECT_EQ(ret, false); +} + /** * @tc.number: FetchPluginBundleInfo_0001 * @tc.name: FetchPluginBundleInfo_0001 diff --git a/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_kit_service_test.cpp b/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_kit_service_test.cpp index 9f93df37de..2569a31729 100644 --- a/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_kit_service_test.cpp +++ b/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_kit_service_test.cpp @@ -62,15 +62,6 @@ #include "display_power_info.h" #include "battery_srv_client.h" -namespace OHOS { -namespace AppExecFwk { - std::unordered_map GetGlobalQueryEventInfo(); - void AddGlobalQueryEventInfo(const EventInfo &info); - void EraseGlobalQueryEventInfo(ErrCode errCode); - void ClearGlobalQueryEventInfo(); -} -} - using namespace testing::ext; using namespace OHOS; using namespace OHOS::AppExecFwk; @@ -14911,60 +14902,4 @@ HWTEST_F(BmsBundleKitServiceTest, Mgr_Proxy_GetAllShortcutInfoForSelf_0100, Func auto ret = bundleMgrProxy->GetAllShortcutInfoForSelf(shortcutInfos); EXPECT_NE(ret, ERR_OK); } - -/** - * @tc.number: CheckNeedAddEvent_0100 - * @tc.name: test CheckNeedAddEvent - * @tc.desc: 1.Test the CheckNeedAddEvent by BundleMgrHostImpl - */ -HWTEST_F(BmsBundleKitServiceTest, CheckNeedAddEvent_0100, Function | SmallTest | Level1) -{ - auto hostImpl = std::make_unique(); - ASSERT_NE(hostImpl, nullptr); - ClearGlobalQueryEventInfo(); - EventInfo test; - test.errorCode = ERR_BUNDLE_MANAGER_INTERNAL_ERROR; - test.funcIdList = {1}; - test.userIdList = {100}; - test.uidList = {1}; - test.appIndexList = {0}; - test.flagList = {BundleFlag::GET_BUNDLE_DEFAULT}; - test.bundleNameList = {"test.CheckNeedAddEvent_0100"}; - test.callingUidList = {1001}; - test.callingBundleNameList = {"com.test.CheckNeedAddEvent_0100"}; - test.callingAppIdList = {"123asdf"}; - uint64_t cacheSize = 1; - ErrCode ret = 0; - bool res = hostImpl->CheckNeedAddEvent(test); - EXPECT_EQ(res, false); - res = hostImpl->CheckNeedAddEvent(test); - EXPECT_EQ(res, false); -} - -/** - * @tc.number: CheckNeedAddEvent_0200 - * @tc.name: test CheckNeedAddEvent - * @tc.desc: 1.Test the CheckNeedAddEvent by BundleMgrHostImpl - */ -HWTEST_F(BmsBundleKitServiceTest, CheckNeedAddEvent_0200, Function | SmallTest | Level1) -{ - auto hostImpl = std::make_unique(); - ASSERT_NE(hostImpl, nullptr); - ClearGlobalQueryEventInfo(); - EventInfo test; - test.errorCode = ERR_BUNDLE_MANAGER_INTERNAL_ERROR; - test.funcIdList = {1}; - test.userIdList = {100}; - test.uidList = {1}; - test.appIndexList = {0}; - test.flagList = {BundleFlag::GET_BUNDLE_DEFAULT}; - test.bundleNameList = {"test.CheckNeedAddEvent_0100"}; - test.callingUidList = {1001}; - test.callingBundleNameList = {"com.test.CheckNeedAddEvent_0100"}; - test.callingAppIdList = {"123asdf"}; - uint64_t cacheSize = 1; - ErrCode ret = 0; - bool res = hostImpl->CheckNeedAddEvent(test); - EXPECT_EQ(res, false); -} } -- Gitee