From eccf69289ca74c18e41f0618353ed7321db1ea71 Mon Sep 17 00:00:00 2001 From: small_leek Date: Thu, 31 Jul 2025 20:33:04 +0800 Subject: [PATCH] fix SendQueryBundleInfoEvent Signed-off-by: small_leek --- .../bundlemgr/include/bundle_mgr_host_impl.h | 29 ++- .../bundlemgr/src/bundle_mgr_host_impl.cpp | 167 +++++++------ services/bundlemgr/src/inner_event_report.cpp | 4 +- .../bms_bundle_data_mgr_test.cpp | 230 +++++++++++------- 4 files changed, 261 insertions(+), 169 deletions(-) diff --git a/services/bundlemgr/include/bundle_mgr_host_impl.h b/services/bundlemgr/include/bundle_mgr_host_impl.h index 4fbf164d9c..f8fafc57c0 100644 --- a/services/bundlemgr/include/bundle_mgr_host_impl.h +++ b/services/bundlemgr/include/bundle_mgr_host_impl.h @@ -33,6 +33,32 @@ namespace OHOS { namespace AppExecFwk { +struct QueryEventInfo { + int32_t errCode; + int32_t lastReportEventTime; + int32_t funcId; + int32_t uid; + int32_t userId; + int32_t appIndex; + int32_t callingUid; + int32_t flag; + std::string bundleName; + std::string callingAppId; + std::string callingBundleName; + + bool operator==(const QueryEventInfo& other) const + { + return funcId == other.funcId && + uid == other.uid && + userId == other.userId && + appIndex == other.appIndex && + flag == other.flag && + bundleName == other.bundleName && + callingUid == other.callingUid && + callingAppId == other.callingAppId && + callingBundleName == other.callingBundleName; + } +}; class BundleMgrHostImpl : public BundleMgrHost { public: BundleMgrHostImpl() = default; @@ -1205,8 +1231,7 @@ 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 SendQueryBundleInfoEvent(QueryEventInfo &query, int32_t intervalTime, bool reportNow); bool GetCallingInfo(int32_t callingUid, std::string &callingBundleName, std::string &callingAppId); std::atomic isBrokerServiceExisted_ = false; diff --git a/services/bundlemgr/src/bundle_mgr_host_impl.cpp b/services/bundlemgr/src/bundle_mgr_host_impl.cpp index 43b102d760..293a51fcfe 100644 --- a/services/bundlemgr/src/bundle_mgr_host_impl.cpp +++ b/services/bundlemgr/src/bundle_mgr_host_impl.cpp @@ -109,7 +109,48 @@ const std::vector QUERY_EXPECTED_ERR = { } std::shared_mutex g_queryEventMutex; -std::unordered_map g_queryEventList; +std::unordered_map> g_queryEventList; + +bool InsertQueryEventInfo(int32_t errCode, const QueryEventInfo& info) +{ + std::vector infos; + std::unique_lock lock(g_queryEventMutex); + if (g_queryEventList.find(errCode) == g_queryEventList.end()) { + g_queryEventList[errCode].push_back(info); + g_queryEventList[errCode][0].lastReportEventTime = BundleUtil::GetCurrentTime(); + APP_LOGD("init record for -e: %{public}d", errCode); + return true; + } + if (std::find(g_queryEventList[errCode].begin(), g_queryEventList[errCode].end(), info) == + g_queryEventList[errCode].end()) { + g_queryEventList[errCode].push_back(info); + APP_LOGD("add new record for -e:%{public}d", + errCode); + return true; + } + return true; +} + +bool TransQueryEventInfo(const std::vector &infos, EventInfo &report) +{ + if (infos.empty()) { + APP_LOGW("no query info to transform"); + return false; + } + report.errCode = infos[0].errCode; + for (auto queryInfo: infos) { + report.funcIdList.push_back(queryInfo.funcId); + report.userIdList.push_back(queryInfo.userId); + report.uidList.push_back(queryInfo.uid); + report.appIndexList.push_back(queryInfo.appIndex); + report.flagList.push_back(queryInfo.flag); + report.bundleNameList.push_back(queryInfo.bundleName); + report.callingUidList.push_back(queryInfo.callingUid); + report.callingBundleNameList.push_back(queryInfo.callingBundleName); + report.callingAppIdList.push_back(queryInfo.callingAppId); + } + return true; +} void ClearGlobalQueryEventInfo() { @@ -123,27 +164,27 @@ void EraseQueryEventInfo(ErrCode error) g_queryEventList.erase(error); } -EventInfo GetQueryEventInfo(ErrCode error) +std::vector GetQueryEventInfo(ErrCode error) { - EventInfo info; + std::vector infos; std::unique_lock queryEventMutex(g_queryEventMutex); if (g_queryEventList.find(error) != g_queryEventList.end()) { return g_queryEventList[error]; } - return info; + return infos; } -EventInfo PrepareQueryEvent(ErrCode errCode, const std::string &bundleName, const std::string &func, +QueryEventInfo PrepareQueryEvent(ErrCode errCode, const std::string &bundleName, const std::string &func, int32_t uid, int32_t userId, int32_t appIndex, int32_t flags) { - EventInfo info; + QueryEventInfo 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}; + info.funcId = QUERY_FUNC_MAP.at(func); + info.uid = uid; + info.userId = userId; + info.appIndex = appIndex; + info.flag = flags; + info.bundleName = bundleName; return info; } @@ -293,7 +334,7 @@ bool BundleMgrHostImpl::GetBundleInfo( auto dataMgr = GetDataMgrFromService(); if (dataMgr == nullptr) { LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr"); - EventInfo info = PrepareQueryEvent(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, bundleName, + QueryEventInfo info = PrepareQueryEvent(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, bundleName, "GetBundleInfo", -1, userId, 0, flags); SendQueryBundleInfoEvent(info, intervalTime, true); return false; @@ -355,7 +396,7 @@ ErrCode BundleMgrHostImpl::GetBundleInfoV9( auto dataMgr = GetDataMgrFromService(); if (dataMgr == nullptr) { LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr"); - EventInfo info = PrepareQueryEvent(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, bundleName, + QueryEventInfo info = PrepareQueryEvent(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, bundleName, "GetBundleInfoV9", -1, userId, 0, flags); SendQueryBundleInfoEvent(info, intervalTime, true); return ERR_BUNDLE_MANAGER_INTERNAL_ERROR; @@ -368,7 +409,7 @@ ErrCode BundleMgrHostImpl::GetBundleInfoV9( return ERR_OK; } } - EventInfo info = PrepareQueryEvent(res, bundleName, "GetBundleInfoV9", -1, userId, 0, flags); + QueryEventInfo info = PrepareQueryEvent(res, bundleName, "GetBundleInfoV9", -1, userId, 0, flags); SendQueryBundleInfoEvent(info, intervalTime, false); } return res; @@ -600,7 +641,7 @@ bool BundleMgrHostImpl::GetBundleNameForUid(const int uid, std::string &bundleNa auto dataMgr = GetDataMgrFromService(); if (dataMgr == nullptr) { APP_LOGE("DataMgr is nullptr"); - EventInfo info = PrepareQueryEvent(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, "None", + QueryEventInfo info = PrepareQueryEvent(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, "None", "GetBundleNameForUid", uid, -1, 0, -1); SendQueryBundleInfoEvent(info, intervalTime, true); return false; @@ -608,7 +649,7 @@ bool BundleMgrHostImpl::GetBundleNameForUid(const int uid, std::string &bundleNa bool res = dataMgr->GetBundleNameForUid(uid, bundleName); if (!res) { APP_LOGD("GetBundleNameForUid failed, -u: %{public}d", uid); - EventInfo info = PrepareQueryEvent(ERR_BUNDLE_MANAGER_INVALID_UID, "None", + QueryEventInfo info = PrepareQueryEvent(ERR_BUNDLE_MANAGER_INVALID_UID, "None", "GetBundleNameForUid", uid, -1, 0, -1); SendQueryBundleInfoEvent(info, intervalTime, false); } @@ -660,7 +701,7 @@ ErrCode BundleMgrHostImpl::GetNameForUid(const int uid, std::string &name) auto dataMgr = GetDataMgrFromService(); if (dataMgr == nullptr) { APP_LOGE("DataMgr is nullptr"); - EventInfo info = PrepareQueryEvent(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, "None", + QueryEventInfo info = PrepareQueryEvent(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, "None", "GetNameForUid", uid, -1, 0, -1); SendQueryBundleInfoEvent(info, intervalTime, true); return ERR_BUNDLE_MANAGER_INTERNAL_ERROR; @@ -670,14 +711,14 @@ ErrCode BundleMgrHostImpl::GetNameForUid(const int uid, std::string &name) auto bmsExtensionClient = std::make_shared(); ret = bmsExtensionClient->GetBundleNameByUid(uid, name); if (ret != ERR_OK) { - EventInfo info = PrepareQueryEvent(ret, "None", + QueryEventInfo info = PrepareQueryEvent(ret, "None", "GetNameForUid", uid, -1, 0, -1); SendQueryBundleInfoEvent(info, intervalTime, false); return ERR_BUNDLE_MANAGER_INVALID_UID; } } if (ret != ERR_OK) { - EventInfo info = PrepareQueryEvent(ret, "None", + QueryEventInfo info = PrepareQueryEvent(ret, "None", "GetNameForUid", uid, -1, 0, -1); SendQueryBundleInfoEvent(info, intervalTime, false); } @@ -3873,14 +3914,14 @@ ErrCode BundleMgrHostImpl::GetAppProvisionInfo(const std::string &bundleName, in auto dataMgr = GetDataMgrFromService(); if (dataMgr == nullptr) { APP_LOGE("DataMgr is nullptr"); - EventInfo info = PrepareQueryEvent(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, bundleName, + QueryEventInfo 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) { - EventInfo info = PrepareQueryEvent(ret, bundleName, "GetAppProvisionInfo", -1, userId, 0, -1); + QueryEventInfo info = PrepareQueryEvent(ret, bundleName, "GetAppProvisionInfo", -1, userId, 0, -1); SendQueryBundleInfoEvent(info, intervalTime, false); } return ret; @@ -5930,42 +5971,6 @@ bool BundleMgrHostImpl::GreatOrEqualTargetAPIVersion(const int32_t platformVersi return dataMgr->GreatOrEqualTargetAPIVersion(platformVersion, minorVersion, patchVersion); } -bool BundleMgrHostImpl::CheckNeedAddEvent(const EventInfo &query, size_t maxEventSize) -{ - 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() >= 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[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; -} - bool BundleMgrHostImpl::GetCallingInfo(int32_t callingUid, std::string &callingBundleName, std::string &callingAppId) { auto dataMgr = GetDataMgrFromService(); @@ -5989,40 +5994,46 @@ bool BundleMgrHostImpl::GetCallingInfo(int32_t callingUid, std::string &callingB return true; } -bool BundleMgrHostImpl::SendQueryBundleInfoEvent(EventInfo &query, int32_t intervalTime, bool reportNow) +bool BundleMgrHostImpl::SendQueryBundleInfoEvent( + QueryEventInfo &query, int64_t intervalTime, bool reportNow) { - if (std::find(QUERY_EXPECTED_ERR.begin(), QUERY_EXPECTED_ERR.end(), query.errCode) != QUERY_EXPECTED_ERR.end()) { - APP_LOGD("No need report for -e:%{public}d", query.errCode); + ErrCode errCode = query.errCode; + if (std::find(QUERY_EXPECTED_ERR.begin(), QUERY_EXPECTED_ERR.end(), errCode) != QUERY_EXPECTED_ERR.end()) { + APP_LOGD("No need report for -e:%{public}d", errCode); return false; } int32_t callingUid = IPCSkeleton::GetCallingUid(); APP_LOGD("start, -f:%{public}d, -c:%{public}d, -e:%{public}d", - query.funcIdList.front(), callingUid, query.errCode); + query.funcId, callingUid, errCode); // get calling bundle info std::string callingBundleName = Constants::EMPTY_STRING; std::string callingAppId = Constants::EMPTY_STRING; GetCallingInfo(callingUid, callingBundleName, callingAppId); - query.callingUidList = {callingUid}; - query.callingBundleNameList = {callingBundleName}; - query.callingAppIdList = {callingAppId}; + query.callingUid = callingUid; + query.callingBundleName = callingBundleName; + query.callingAppId = callingAppId; + + InsertQueryEventInfo(errCode, query); + auto infos = GetQueryEventInfo(errCode); // check report now if (reportNow) { - EventReport::SendSystemEvent(BMSEventType::QUERY_BUNDLE_INFO, query); + EventInfo report; + TransQueryEventInfo(infos, report); APP_LOGD("SendSystemEvent now"); + EventReport::SendSystemEvent(BMSEventType::QUERY_BUNDLE_INFO, report); + EraseQueryEventInfo(errCode); return true; } - - if (CheckNeedAddEvent(query, MAX_QUERY_EVENT_REPORT_ONCE)) { - APP_LOGD("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_LOGD("SendSystemEvent for :%{public}d", reportInfo.errCode); - EventReport::SendSystemEvent(BMSEventType::QUERY_BUNDLE_INFO, reportInfo); - EraseQueryEventInfo(reportInfo.errCode); + + size_t infoSize = infos.size(); + int32_t lastReportEventTime = infos[0].lastReportEventTime; + if (infoSize >= MAX_QUERY_EVENT_REPORT_ONCE || + (BundleUtil::GetCurrentTime() - lastReportEventTime) >= intervalTime) { + APP_LOGD("SendSystemEvent for :%{public}d", errCode); + EventInfo report; + TransQueryEventInfo(infos, report); + EventReport::SendSystemEvent(BMSEventType::QUERY_BUNDLE_INFO, report); + EraseQueryEventInfo(errCode); return true; } return false; diff --git a/services/bundlemgr/src/inner_event_report.cpp b/services/bundlemgr/src/inner_event_report.cpp index 9573252c22..024a689439 100644 --- a/services/bundlemgr/src/inner_event_report.cpp +++ b/services/bundlemgr/src/inner_event_report.cpp @@ -773,10 +773,10 @@ void InnerEventReport::InnerSendQueryBundleInfoEvent(const EventInfo& eventInfo) { InnerSystemEventWrite( QUERY_BUNDLE_INFO, - HiSysEventType::BEHAVIOR, + HiSysEventType::STATISTIC, EVENT_PARAM_FUNC_ID_LIST, eventInfo.funcIdList, EVENT_PARAM_USER_ID_LIST, eventInfo.userIdList, - EVENT_PARAM_UID_LIST, eventInfo.appIndex, + EVENT_PARAM_UID_LIST, eventInfo.uidList, EVENT_PARAM_APP_INDEX_LIST, eventInfo.appIndexList, EVENT_PARAM_FLAG_LIST, eventInfo.flagList, EVENT_PARAM_BUNDLE_NAME_LIST, eventInfo.bundleNameList, diff --git a/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_data_mgr_test.cpp b/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_data_mgr_test.cpp index 0b9681d861..6cc4b9301b 100644 --- a/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_data_mgr_test.cpp +++ b/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_data_mgr_test.cpp @@ -64,7 +64,8 @@ namespace OHOS { namespace AppExecFwk { void ClearGlobalQueryEventInfo(); - EventInfo GetQueryEventInfo(ErrCode error); + bool InsertQueryEventInfo(int32_t errCode, const QueryEventInfo& info); + bool TransQueryEventInfo(const std::vector &infos, EventInfo &report); } } @@ -4325,63 +4326,6 @@ HWTEST_F(BmsBundleDataMgrTest, BundleMgrHostHandleBatchGetBundleStats_0100, Func EXPECT_EQ(ret, ERR_OK); } -/** - * @tc.number: CheckNeedAddEvent_0100 - * @tc.name: test CheckNeedAddEvent - * @tc.desc: 1.Test the CheckNeedAddEvent by BundleMgrHostImpl - */ -HWTEST_F(BmsBundleDataMgrTest, 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 @@ -4441,19 +4385,17 @@ HWTEST_F(BmsBundleDataMgrTest, SendQueryBundleInfoEvent_0100, Function | SmallTe ASSERT_NE(dataMgr, nullptr); ClearGlobalQueryEventInfo(); size_t maxEvent = 7; - EventInfo test; + QueryEventInfo 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}; - for (int32_t i = 0; i <= MAX_QUERY_EVENT_REPORT_ONCE; i++) { - test.bundleNameList.push_back("test.SendQueryBundleInfoEvent_0100_" + std::to_string(i)); - } - test.callingUidList = {1001}; - test.callingBundleNameList = {"com.test.SendQueryBundleInfoEvent_0100"}; - test.callingAppIdList = {"123asdf"}; + test.funcId = 1; + test.userId = 100; + test.uid = 1; + test.appIndex = 0; + test.flag = BundleFlag::GET_BUNDLE_DEFAULT; + test.bundleName = "test.SendQueryBundleInfoEvent_0100"; + test.callingUid = 1001; + test.callingBundleName = "com.test.SendQueryBundleInfoEvent_0100"; + test.callingAppId = "123asdf"; test.lastReportEventTime = 0; // test no need report errcode @@ -4465,33 +4407,147 @@ HWTEST_F(BmsBundleDataMgrTest, SendQueryBundleInfoEvent_0100, Function | SmallTe test.errCode = ERR_BUNDLE_MANAGER_INTERNAL_ERROR; ret = bundleMgrHostImpl_->SendQueryBundleInfoEvent(test, 0, true); EXPECT_EQ(ret, true); + + // test infoSize >= MAX_QUERY_EVENT_REPORT_ONCE + ClearGlobalQueryEventInfo(); + for (int32_t i = 0; i <= MAX_QUERY_EVENT_REPORT_ONCE; i++) { + test.bundleName = "test.SendQueryBundleInfoEvent_0100_" + std::to_string(i); + InsertQueryEventInfo(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, test); + } + test.bundleName = "test.SendQueryBundleInfoEvent_0100_new"; + ret = bundleMgrHostImpl_->SendQueryBundleInfoEvent(test, 0, false); + EXPECT_EQ(ret, true); - // test reportNow is false and CheckNeedAddEvent is true + // test infoSize < MAX_QUERY_EVENT_REPORT_ONCE, but has wait for more than intervalTime ClearGlobalQueryEventInfo(); ret = bundleMgrHostImpl_->SendQueryBundleInfoEvent(test, 0, false); + EXPECT_EQ(ret, true); + + // test infoSize < MAX_QUERY_EVENT_REPORT_ONCE and need wait for more than intervalTime + ClearGlobalQueryEventInfo(); + int64_t oneDay = 86400; + test.bundleName = "test.SendQueryBundleInfoEvent_0100"; + ret = bundleMgrHostImpl_->SendQueryBundleInfoEvent(test, oneDay, false); EXPECT_EQ(ret, false); +} - // test reportNow is false and CheckNeedAddEvent is false - ret = bundleMgrHostImpl_->SendQueryBundleInfoEvent(test, 0, false); - EXPECT_EQ(ret, true); - EventInfo test2 = GetQueryEventInfo(ERR_BUNDLE_MANAGER_INTERNAL_ERROR); - EXPECT_EQ(test2.bundleNameList.empty(), true); - - // test reportNow is false and wait for more than intervalTime +/** + * @tc.number: TransQueryEventInfo_0100 + * @tc.name: test TransQueryEventInfo + * @tc.desc: 1.Test the TransQueryEventInfo by BundleMgrHostImpl + */ +HWTEST_F(BmsBundleDataMgrTest, TransQueryEventInfo_0100, Function | SmallTest | Level1) +{ + ASSERT_NE(bundleMgrHostImpl_, nullptr); + auto dataMgr = bundleMgrHostImpl_->GetDataMgrFromService(); + ASSERT_NE(dataMgr, nullptr); ClearGlobalQueryEventInfo(); - test.bundleNameList = {"test.SendQueryBundleInfoEvent_0100"}; - ret = bundleMgrHostImpl_->SendQueryBundleInfoEvent(test, 0, false); + size_t maxEvent = 7; + QueryEventInfo test; + test.errCode = ERR_BUNDLE_MANAGER_INTERNAL_ERROR; + test.funcId = 1; + test.userId = 100; + test.uid = 1; + test.appIndex = 0; + test.flag = BundleFlag::GET_BUNDLE_DEFAULT; + test.bundleName = "test.TransQueryEventInfo_0100"; + test.callingUid = 1001; + test.callingBundleName = "com.test.TransQueryEventInfo_0100"; + test.callingAppId = "123asdf"; + test.lastReportEventTime = 0; + + std::vector infos; + // test no need report errcode + EventInfo report; + ret = TransQueryEventInfo(infos, report); EXPECT_EQ(ret, false); - ret = bundleMgrHostImpl_->SendQueryBundleInfoEvent(test, 0, false); + + infos.push_back(test); + ret = TransQueryEventInfo(infos, report); EXPECT_EQ(ret, true); - - // test reportNow is false, not ready to report +} + +/** + * @tc.number: InsertQueryEventInfo_0100 + * @tc.name: test InsertQueryEventInfo + * @tc.desc: 1.Test the InsertQueryEventInfo by BundleMgrHostImpl + */ +HWTEST_F(BmsBundleDataMgrTest, InsertQueryEventInfo_0100, Function | SmallTest | Level1) +{ + ASSERT_NE(bundleMgrHostImpl_, nullptr); + auto dataMgr = bundleMgrHostImpl_->GetDataMgrFromService(); + ASSERT_NE(dataMgr, nullptr); ClearGlobalQueryEventInfo(); - test.bundleNameList = {"test.SendQueryBundleInfoEvent_0100"}; - ret = bundleMgrHostImpl_->SendQueryBundleInfoEvent(test, BundleUtil::GetCurrentTime(), false); - EXPECT_EQ(ret, false); - ret = bundleMgrHostImpl_->SendQueryBundleInfoEvent(test, BundleUtil::GetCurrentTime(), false); + size_t maxEvent = 7; + QueryEventInfo test; + test.errCode = ERR_BUNDLE_MANAGER_INTERNAL_ERROR; + test.funcId = 1; + test.userId = 100; + test.uid = 1; + test.appIndex = 0; + test.flag = BundleFlag::GET_BUNDLE_DEFAULT; + test.bundleName = "test.InsertQueryEventInfo_0100"; + test.callingUid = 1001; + test.callingBundleName = "com.test.InsertQueryEventInfo_0100"; + test.callingAppId = "123asdf"; + test.lastReportEventTime = 0; + + // test insert new info + bool ret = InsertQueryEventInfo(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, test); + EXPECT_EQ(ret, true); + + // test insert same info + ret = InsertQueryEventInfo(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, test); EXPECT_EQ(ret, false); + + // test insert different errcode + ret = InsertQueryEventInfo(ERR_OK, test); + EXPECT_EQ(ret, true); + + // test insert different funcid + test.funcId = 2; + ret = InsertQueryEventInfo(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, test); + EXPECT_EQ(ret, true); + + // test insert different uid + test.uid = 2; + ret = InsertQueryEventInfo(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, test); + EXPECT_EQ(ret, true); + + // test insert different userid + test.userId = 0; + ret = InsertQueryEventInfo(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, test); + EXPECT_EQ(ret, true); + + // test insert different appIndex + test.appIndex = 1; + ret = InsertQueryEventInfo(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, test); + EXPECT_EQ(ret, true); + + // test insert different flag + test.flag = BundleFlag::GET_BUNDLE_WITH_ABILITIES; + ret = InsertQueryEventInfo(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, test); + EXPECT_EQ(ret, true); + + // test insert different bundlename + test.bundleName = "com.test.InsertQueryEventInfo_0100_2"; + ret = InsertQueryEventInfo(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, test); + EXPECT_EQ(ret, true); + + // test insert different callinguid + test.callingUid = 200000; + ret = InsertQueryEventInfo(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, test); + EXPECT_EQ(ret, true); + + // test insert different callingBundleName + test.callingBundleName = "com.test.InsertQueryEventInfo_0100_3"; + ret = InsertQueryEventInfo(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, test); + EXPECT_EQ(ret, true); + + // test insert different callingAppId + test.callingAppId = "asdf123"; + ret = InsertQueryEventInfo(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, test); + EXPECT_EQ(ret, true); } /** -- Gitee