diff --git a/bundle_hisysevent.yaml b/bundle_hisysevent.yaml index 8dfee8cd0aaeb41387a9b77ab114a20728577a24..2a62db4169680dd8696169c680efee9fceb7c38f 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 8e98379d06f9555dded9fa0659015f4d236e2f7d..37c7c6cbebb73b1642618d9205cc224471cdb93d 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,11 @@ private: bool GetLabelFromCache(const std::string &cacheKey, const std::string &abilityName, const std::unordered_map> &resourceCache, std::string &label); - + 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 }; } // namespace AppExecFwk } // namespace OHOS diff --git a/services/bundlemgr/include/event_report.h b/services/bundlemgr/include/event_report.h index d637b2373a39384e3ab970803cd89f920e5b89d9..841df97018b8edcc4ad91c09616b115e1de5ffea 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 94bdab6e3fc1aeb554fc2b02e1a57e37e5e0e738..97e2a869003a55fa84f0743543ed4386b7e3c6de 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 b97019e379ccc88a35a547d4905450701a5de5dc..d44416bcddc82f82bc57e78d3b40b8d32fdccd93 100644 --- a/services/bundlemgr/src/bundle_mgr_host_impl.cpp +++ b/services/bundlemgr/src/bundle_mgr_host_impl.cpp @@ -88,6 +88,49 @@ 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 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}, + {"GetBundleInfoV9", 3}, + {"GetBundleInfo", 4}, + {"GetAppProvisionInfo", 5} +}; +} + +std::shared_mutex g_queryEventMutex; +std::unordered_map g_queryEventList; + +EventInfo GetGlobalQueryEventInfo(ErrCode errCode) +{ + EventInfo info; + std::unique_lock queryEventMutex(g_queryEventMutex); + if (g_queryEventList.find(errCode) == g_queryEventList.end()) { + return g_queryEventList[errCode]; + } + return info; +} + +void AddGlobalQueryEventInfo(const EventInfo &info) +{ + std::unique_lock queryEventMutex(g_queryEventMutex); + if (g_queryEventList.find(info.errCode) == g_queryEventList.end()) { + g_queryEventList[info.errCode] = 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( @@ -219,29 +262,48 @@ bool BundleMgrHostImpl::GetBundleInfo( bundleName.c_str(), flags, userId); int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCTION_GET_BUNDLE_INFO); ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); }); + EventInfo info; + info.userIdList.push_back(userId); + info.bundleNameList.push_back(bundleName); + info.flagList.push_back(flags); + info.funcIdList.push_back(QUERY_FUNC_MAP.at("GetBundleInfo")); + 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"); + info.errCode = ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED; + 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"); + info.errCode = ERR_BUNDLE_MANAGER_PERMISSION_DENIED; + 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"); + info.errCode = ERR_BUNDLE_MANAGER_INTERNAL_ERROR; + SendQueryBundleInfoEvent(info, intervalTime, 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) { + info.errCode = ret; + SendQueryBundleInfoEvent(info, intervalTime, false); + } + } else { + info.errCode = ERR_BUNDLE_MANAGER_INVALID_USER_ID; + SendQueryBundleInfoEvent(info, intervalTime, false); } } return res; @@ -285,6 +347,12 @@ ErrCode BundleMgrHostImpl::GetBundleInfoV9( } return false; }(); + EventInfo info; + info.userIdList.push_back(userId); + info.bundleNameList.push_back(bundleName); + info.flagList.push_back(flags); + info.funcIdList.push_back(QUERY_FUNC_MAP.at("GetBundleInfoV9")); + int32_t intervalTime = ONE_DAY; if (!permissionVerify) { APP_LOGE("verify permission failed"); return ERR_BUNDLE_MANAGER_PERMISSION_DENIED; @@ -293,16 +361,20 @@ ErrCode BundleMgrHostImpl::GetBundleInfoV9( auto dataMgr = GetDataMgrFromService(); if (dataMgr == nullptr) { LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr"); - return ERR_BUNDLE_MANAGER_INTERNAL_ERROR; + info.errCode = ERR_BUNDLE_MANAGER_INTERNAL_ERROR; + 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; } } + info.errCode = res; + SendQueryBundleInfoEvent(info, intervalTime, false); } return res; } @@ -332,7 +404,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) @@ -519,22 +594,37 @@ ErrCode BundleMgrHostImpl::GetBundleInfosV9(int32_t flags, std::vectorGetBundleNameForUid(uid, bundleName); + bool res = dataMgr->GetBundleNameForUid(uid, bundleName); + if (!res) { + info.errCode = ERR_BUNDLE_MANAGER_INVALID_UID; + SendQueryBundleInfoEvent(info, intervalTime, false); + } + return res; } bool BundleMgrHostImpl::GetBundlesForUid(const int uid, std::vector &bundleNames) @@ -574,6 +664,10 @@ ErrCode BundleMgrHostImpl::GetNameForUid(const int uid, std::string &name) } return false; }(); + EventInfo info; + info.uidList.push_back(uid); + info.funcIdList.push_back(QUERY_FUNC_MAP.at("GetNameForUid")); + int32_t intervalTime = ONE_DAY; if (!permissionVerify) { APP_LOGE("verify permission failed"); return ERR_BUNDLE_MANAGER_PERMISSION_DENIED; @@ -581,15 +675,22 @@ ErrCode BundleMgrHostImpl::GetNameForUid(const int uid, std::string &name) auto dataMgr = GetDataMgrFromService(); if (dataMgr == nullptr) { APP_LOGE("DataMgr is nullptr"); + info.errCode = ERR_BUNDLE_MANAGER_INTERNAL_ERROR; + SendQueryBundleInfoEvent(info, intervalTime, 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) { + ret = ERR_BUNDLE_MANAGER_INVALID_UID; + } } + info.errCode = ret; + SendQueryBundleInfoEvent(info, intervalTime, false); + } return ret; } @@ -3720,6 +3821,11 @@ ErrCode BundleMgrHostImpl::GetAppProvisionInfo(const std::string &bundleName, in { APP_LOGD("begin to GetAppProvisionInfo bundleName: %{public}s, userId: %{public}d", bundleName.c_str(), userId); + EventInfo info; + info.userIdList.push_back(userId); + info.bundleNameList.push_back(bundleName); + info.funcIdList.push_back(QUERY_FUNC_MAP.at("GetAppProvisionInfo")); + int32_t intervalTime = ONE_DAY; if (!BundlePermissionMgr::IsSystemApp()) { APP_LOGE("non-system app calling system api"); return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED; @@ -3732,9 +3838,16 @@ ErrCode BundleMgrHostImpl::GetAppProvisionInfo(const std::string &bundleName, in auto dataMgr = GetDataMgrFromService(); if (dataMgr == nullptr) { APP_LOGE("DataMgr is nullptr"); + info.errCode = ERR_BUNDLE_MANAGER_INTERNAL_ERROR; + SendQueryBundleInfoEvent(info, intervalTime, true); return ERR_BUNDLE_MANAGER_INTERNAL_ERROR; } - return dataMgr->GetAppProvisionInfo(bundleName, userId, appProvisionInfo); + ErrCode ret = dataMgr->GetAppProvisionInfo(bundleName, userId, appProvisionInfo); + if (ret != ERR_OK) { + info.errCode = ret; + SendQueryBundleInfoEvent(info, intervalTime, false); + } + return ret; } ErrCode BundleMgrHostImpl::GetProvisionMetadata(const std::string &bundleName, int32_t userId, @@ -5766,6 +5879,92 @@ ErrCode BundleMgrHostImpl::GetAllShortcutInfoForSelf(std::vector & return dataMgr->GetAllShortcutInfoForSelf(shortcutInfos); } +bool BundleMgrHostImpl::CheckNeedAddEvent(const EventInfo &query, size_t maxEventSize) +{ + if (g_queryEventList.find(query.errCode) == g_queryEventList.end()) { + g_queryEventList[query.errCode] = query; + return true; + } + EventInfo info = g_queryEventList[query.errCode]; + if (info.bundleNameList.size() >= maxEventSize) { + 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()) { + g_queryEventList[query.errCode].funcIdList.push_back(query.funcIdList.front()); + g_queryEventList[query.errCode].userIdList.push_back(query.userIdList.front()); + g_queryEventList[query.errCode].uidList.push_back(query.uidList.front()); + g_queryEventList[query.errCode].appIndexList.push_back(query.funcIdList.front()); + g_queryEventList[query.errCode].flagList.push_back(query.flagList.front()); + g_queryEventList[query.errCode].bundleNameList.push_back(query.bundleNameList.front()); + g_queryEventList[query.errCode].callingUidList.push_back(query.callingUidList.front()); + g_queryEventList[query.errCode].callingBundleNameList.push_back(query.callingBundleNameList.front()); + g_queryEventList[query.errCode].callingAppIdList.push_back(query.callingAppIdList.front()); + 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()); + } + 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(EventInfo &query, int32_t intervalTime, bool reportNow) +{ + int32_t callingUid = IPCSkeleton::GetCallingUid(); + 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; + GetCallingInfo(callingUid, callingBundleName, callingAppId); + 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 now"); + return true; + } + std::unique_lock queryEventMutex(g_queryEventMutex); + CheckNeedAddEvent(query, MAX_QUERY_EVENT_REPORT_ONCE); + if (g_queryEventList[query.errCode].lastReportEventTime == 0 || + (BundleUtil::GetCurrentTime() - g_queryEventList[query.errCode].lastReportEventTime) >= intervalTime) { + APP_LOGI("SendSystemEvent wait for :%{public}d", intervalTime); + EventReport::SendSystemEvent(BMSEventType::QUERY_BUNDLE_INFO, g_queryEventList[query.errCode]); + g_queryEventList.erase(query.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 d6f4cbe716cbc4e23e3be405e26b5f0a59c99a68..06b40440633677a7333d8c18d22239b52d2d3458 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_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 6b1b8aeeb44e6268930ee0c6fa437577983c1391..16053c227423241ba2c8591177f5e63e73b868b5 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 0c0b663018f213397d065934e6915a40a0026851..e56b5fea5080a6e2e46e0e50257277d72d57872d 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,16 @@ #include "want.h" #include "user_unlocked_event_subscriber.h" + +namespace OHOS { +namespace AppExecFwk { + EventInfo GetGlobalQueryEventInfo(ErrCode errCode); + void AddGlobalQueryEventInfo(const EventInfo &info); + void EraseGlobalQueryEventInfo(ErrCode errCode); + void ClearGlobalQueryEventInfo(); +} +} + using namespace testing::ext; using namespace OHOS; using namespace OHOS::AppExecFwk; @@ -157,6 +167,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 +3468,110 @@ 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"}; + uint64_t cacheSize = 1; + ErrCode ret = 0; + 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, false); + 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: FetchPluginBundleInfo_0001 * @tc.name: FetchPluginBundleInfo_0001