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 61cf51fe11486be226db89a19c38e6e842ce03f8..605846600ec8ecd9f835356b538f665132bce425 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 { @@ -1202,6 +1203,9 @@ 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; }; diff --git a/services/bundlemgr/include/event_report.h b/services/bundlemgr/include/event_report.h index 6747087bd887df9e7d5299ebe2efd8c8a611dce8..00c70805e611926c348d14954ced49bbb0020014 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 42449b643dca77cd7348ba46cb4af718e40fc3f6..f0660c36b9e0b6cc33a141a916d8eaa9a9ee380c 100644 --- a/services/bundlemgr/src/bundle_mgr_host_impl.cpp +++ b/services/bundlemgr/src/bundle_mgr_host_impl.cpp @@ -88,6 +88,54 @@ 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; + +void ClearGlobalQueryEventInfo() +{ + std::unique_lock queryEventMutex(g_queryEventMutex); + g_queryEventList.clear(); +} + +void EraseQueryEventInfo(ErrCode error) +{ + std::unique_lock queryEventMutex(g_queryEventMutex); + g_queryEventList.erase(error); +} + +EventInfo GetQueryEventInfo(ErrCode error) +{ + EventInfo info; + std::unique_lock queryEventMutex(g_queryEventMutex); + if (g_queryEventList.find(error) != g_queryEventList.end()) { + return g_queryEventList[error]; + } + return info; +} + +EventInfo 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; + 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( @@ -220,6 +268,7 @@ bool BundleMgrHostImpl::GetBundleInfo( int32_t timerId = XCollieHelper::SetRecoveryTimer(FUNCTION_GET_BUNDLE_INFO); ScopeGuard cancelTimerIdGuard([timerId] { XCollieHelper::CancelTimer(timerId); }); // API9 need to be system app + int32_t intervalTime = ONE_DAY; if (!BundlePermissionMgr::IsSystemApp() && !BundlePermissionMgr::VerifyCallingBundleSdkVersion(ServiceConstants::API_VERSION_NINE)) { LOG_D(BMS_TAG_QUERY, "non-system app calling system api"); @@ -235,6 +284,9 @@ 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, + "GetBundleInfo", -1, userId, 0, flags); + SendQueryBundleInfoEvent(info, intervalTime, true); return false; } bool res = dataMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId); @@ -290,9 +342,13 @@ ErrCode BundleMgrHostImpl::GetBundleInfoV9( return ERR_BUNDLE_MANAGER_PERMISSION_DENIED; } LOG_D(BMS_TAG_QUERY, "verify permission success, begin to GetBundleInfoV9"); + int32_t intervalTime = ONE_DAY; auto dataMgr = GetDataMgrFromService(); if (dataMgr == nullptr) { LOG_E(BMS_TAG_QUERY, "DataMgr is nullptr"); + EventInfo info = PrepareQueryEvent(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, bundleName, + "GetBundleInfoV9", -1, userId, 0, flags); + SendQueryBundleInfoEvent(info, intervalTime, true); return ERR_BUNDLE_MANAGER_INTERNAL_ERROR; } auto res = dataMgr->GetBundleInfoV9(bundleName, flags, bundleInfo, userId); @@ -303,6 +359,8 @@ ErrCode BundleMgrHostImpl::GetBundleInfoV9( return ERR_OK; } } + EventInfo info = PrepareQueryEvent(res, bundleName, "GetBundleInfoV9", -1, userId, 0, flags); + SendQueryBundleInfoEvent(info, intervalTime, false); } return res; } @@ -519,6 +577,7 @@ ErrCode BundleMgrHostImpl::GetBundleInfosV9(int32_t flags, std::vectorGetBundleNameForUid(uid, bundleName); + 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", + "GetBundleNameForUid", uid, -1, 0, -1); + SendQueryBundleInfoEvent(info, intervalTime, false); + } + return res; } bool BundleMgrHostImpl::GetBundlesForUid(const int uid, std::vector &bundleNames) @@ -578,9 +647,13 @@ ErrCode BundleMgrHostImpl::GetNameForUid(const int uid, std::string &name) APP_LOGE("verify permission failed"); return ERR_BUNDLE_MANAGER_PERMISSION_DENIED; } + int32_t intervalTime = ONE_DAY; auto dataMgr = GetDataMgrFromService(); if (dataMgr == nullptr) { APP_LOGE("DataMgr is nullptr"); + 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); @@ -588,9 +661,17 @@ 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", + "GetNameForUid", uid, -1, 0, -1); + SendQueryBundleInfoEvent(info, intervalTime, false); return ERR_BUNDLE_MANAGER_INVALID_UID; } } + if (ret != ERR_OK) { + EventInfo info = PrepareQueryEvent(ret, "None", + "GetNameForUid", uid, -1, 0, -1); + SendQueryBundleInfoEvent(info, intervalTime, false); + } return ret; } @@ -3779,12 +3860,21 @@ ErrCode BundleMgrHostImpl::GetAppProvisionInfo(const std::string &bundleName, in APP_LOGE("verify permission failed"); return ERR_BUNDLE_MANAGER_PERMISSION_DENIED; } + int32_t intervalTime = ONE_DAY; auto dataMgr = GetDataMgrFromService(); if (dataMgr == nullptr) { APP_LOGE("DataMgr is nullptr"); + EventInfo info = PrepareQueryEvent(ERR_BUNDLE_MANAGER_INTERNAL_ERROR, bundleName, + "GetAppProvisionInfo", -1, userId, 0, -1); + 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) { + EventInfo info = PrepareQueryEvent(ret, bundleName, "GetAppProvisionInfo", -1, userId, 0, -1); + SendQueryBundleInfoEvent(info, intervalTime, false); + } + return ret; } ErrCode BundleMgrHostImpl::GetProvisionMetadata(const std::string &bundleName, int32_t userId, @@ -4596,7 +4686,6 @@ ErrCode BundleMgrHostImpl::CheckSandboxPath(std::vector &sourcePath return ERR_OK; } - ErrCode BundleMgrHostImpl::MigrateDataUserAuthentication() { #ifdef BMS_USER_AUTH_FRAMEWORK_ENABLED @@ -5832,6 +5921,99 @@ 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(); + 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 = "None"; + std::string callingAppId = "None"; + GetCallingInfo(callingUid, callingBundleName, 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 now"); + return true; + } + + 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; +} + ErrCode BundleMgrHostImpl::GetPluginInfo(const std::string &hostBundleName, const std::string &pluginBundleName, const int32_t userId, PluginBundleInfo &pluginBundleInfo) { diff --git a/services/bundlemgr/src/inner_event_report.cpp b/services/bundlemgr/src/inner_event_report.cpp index b21149381109616bef3afce26652013bb9aef2b9..9573252c22a29e8b893536945f4d0e78f84a9736 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) @@ -753,5 +768,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 d52f27bc24a46ee0378e76a160f256258860ca47..212c0bca97a4b683f663fe9fe36c4d0b9a7e8bbb 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 @@ -1449,6 +1449,22 @@ HWTEST_F(BmsBundleDataMgrNullptrTest, RdbDataManager_0004, Function | MediumTest EXPECT_TRUE(ret6); } +/** + * @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: GetRdbRestoreMutex_0010 * @tc.name: Test GetRdbRestoreMutex 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 2b3b237ea354abd0cbcf5092e3516d7a9e99205c..f647fbac53eb008e669d037dc21cef4a0a059fe6 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 @@ -61,6 +61,12 @@ #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; @@ -68,6 +74,11 @@ using OHOS::AAFwk::Want; namespace OHOS { namespace { +const int32_t TEST_QUERY_EVENT_UID = 20013998; +const int32_t TEST_QUERY_EVENT_BUNDLE_ID = 13998; +const int32_t TEST_QUERY_EVENT_UID2 = 20013999; +const int32_t TEST_QUERY_EVENT_BUNDLE_ID2 = 13999; +const int32_t MAX_QUERY_EVENT_REPORT_ONCE = 101; const std::string BUNDLE_NAME_TEST = "com.example.bundlekit.test"; const std::string MODULE_NAME_TEST = "com.example.bundlekit.test.entry"; const std::string MODULE_NAME_TEST1 = "com.example.bundlekit.test.entry1"; @@ -4284,6 +4295,207 @@ 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 + * @tc.desc: 1.Test the GetCallingInfo by BundleMgrHostImpl + */ +HWTEST_F(BmsBundleDataMgrTest, 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(BmsBundleDataMgrTest, 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}; + 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.lastReportEventTime = 0; + + // test reportNow is true + bool ret = bundleMgrHostImpl_->SendQueryBundleInfoEvent(test, 0, true); + EXPECT_EQ(ret, true); + + // test reportNow is false and CheckNeedAddEvent is true + ClearGlobalQueryEventInfo(); + ret = bundleMgrHostImpl_->SendQueryBundleInfoEvent(test, 0, 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 + ClearGlobalQueryEventInfo(); + test.bundleNameList = {"test.SendQueryBundleInfoEvent_0100"}; + ret = bundleMgrHostImpl_->SendQueryBundleInfoEvent(test, 0, false); + EXPECT_EQ(ret, false); + ret = bundleMgrHostImpl_->SendQueryBundleInfoEvent(test, 0, false); + EXPECT_EQ(ret, true); + + // test reportNow is false, not ready to report + ClearGlobalQueryEventInfo(); + test.bundleNameList = {"test.SendQueryBundleInfoEvent_0100"}; + ret = bundleMgrHostImpl_->SendQueryBundleInfoEvent(test, BundleUtil::GetCurrentTime(), false); + EXPECT_EQ(ret, false); + ret = bundleMgrHostImpl_->SendQueryBundleInfoEvent(test, BundleUtil::GetCurrentTime(), false); + EXPECT_EQ(ret, false); +} + +/** + * @tc.number: GetBundleNameForUid_0100 + * @tc.name: test GetBundleNameForUid + * @tc.desc: 1.Test the GetBundleNameForUid by BundleMgrHostImpl + */ +HWTEST_F(BmsBundleDataMgrTest, GetBundleNameForUid_0100, Function | SmallTest | Level1) +{ + ASSERT_NE(bundleMgrHostImpl_, nullptr); + auto dataMgr = bundleMgrHostImpl_->GetDataMgrFromService(); + ASSERT_NE(dataMgr, nullptr); + std::string testResult; + bool testRet = bundleMgrHostImpl_->GetBundleNameForUid(1, testResult); + EXPECT_FALSE(testRet); + + std::string bundleName = "com.GetBundleNameForUid_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_UID2; + 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_ID2; + dataMgr->bundleIdMap_.insert(std::pair(testBundleId, bundleName)); + + testRet = bundleMgrHostImpl_->GetBundleNameForUid(TEST_QUERY_EVENT_UID2, testResult); + EXPECT_TRUE(testRet); +} + /** * @tc.number: BundleMgrProxyBatchGetBundleStats_0100_0100 * @tc.name: BundleMgrProxyBatchGetBundleStats_0100_0100