已合并
优化批量查询接口GetShowBadgeEnabledForBundles,改为真实批量查询 #5011
luxin创建于 8月25日
优化批量查询接口GetShowBadgeEnabledForBundles,改为真实批量查询 #5011
已合并
共 12 个文件变更+572-52
| @@ -1985,6 +1985,8 @@ public: | |||
| 1985 | static sptr<NotificationBundleOption> GenerateBundleOption(); | 1985 | static sptr<NotificationBundleOption> GenerateBundleOption(); |
| 1986 | static sptr<NotificationBundleOption> GenerateValidBundleOption( | 1986 | static sptr<NotificationBundleOption> GenerateValidBundleOption( |
| 1987 | const sptr<NotificationBundleOption> &bundleOption); | 1987 | const sptr<NotificationBundleOption> &bundleOption); |
| 1988 | + static std::vector<sptr<NotificationBundleOption>> ResolveBundleOptionUids( | ||
| 1989 | + const std::vector<sptr<NotificationBundleOption>> &bundleOptions); | ||
| 1988 | void UpdateCloneBundleInfoForRingtone(NotificationRingtoneInfo ringtoneInfo, int32_t userId, | 1990 | void UpdateCloneBundleInfoForRingtone(NotificationRingtoneInfo ringtoneInfo, int32_t userId, |
| 1989 | const sptr<NotificationBundleOption> bundle, const NotificationCloneBundleInfo cloneBundleInfo); | 1991 | const sptr<NotificationBundleOption> bundle, const NotificationCloneBundleInfo cloneBundleInfo); |
| 1990 | 1992 | ||
| @@ -156,6 +156,17 @@ public: | |||
| 156 | */ | 156 | */ |
| 157 | ErrCode IsShowBadge(const sptr<NotificationBundleOption> &bundleOption, bool &enable); | 157 | ErrCode IsShowBadge(const sptr<NotificationBundleOption> &bundleOption, bool &enable); |
| 158 | 158 | ||
| 159 | + /** | ||
| 160 | + * @brief Batch query show badge enabled state for multiple bundles. | ||
| 161 | + * | ||
| 162 | + * @param bundleOptions Indicates the bundle options to query. | ||
| 163 | + * @param bundleEnable Indicates the output map from bundle option to badge enabled state. | ||
| 164 | + * @param userId Indicates the user id for table routing. | ||
| 165 | + * @return Return true on success, false on failure. | ||
| 166 | + */ | ||
| 167 | + bool GetShowBadgeEnabledForBundles(const std::vector<sptr<NotificationBundleOption>> &bundleOptions, | ||
| 168 | + std::map<sptr<NotificationBundleOption>, bool> &bundleEnable, int32_t userId); | ||
| 169 | + | ||
| 159 | /** | 170 | /** |
| 160 | * @brief Set show badge in the of bunlde from DB. | 171 | * @brief Set show badge in the of bunlde from DB. |
| 161 | * | 172 | * |
| @@ -443,6 +443,17 @@ public: | |||
| 443 | std::map<sptr<NotificationBundleOption>, bool> &slotEnabled, | 443 | std::map<sptr<NotificationBundleOption>, bool> &slotEnabled, |
| 444 | int32_t userId); | 444 | int32_t userId); |
| 445 | 445 | ||
| 446 | + /** | ||
| 447 | + * @brief Batch query show badge enabled state for multiple bundles. | ||
| 448 | + * | ||
| 449 | + * @param bundleOptions Indicates the bundle options to query. | ||
| 450 | + * @param bundleEnable Indicates the output map from bundle option to badge enabled state. | ||
| 451 | + * @param userId Indicates the user id for table routing. | ||
| 452 | + * @return Return true on success, false on failure. | ||
| 453 | + */ | ||
| 454 | + bool GetShowBadgeEnabledForBundles(const std::vector<sptr<NotificationBundleOption>> &bundleOptions, | ||
| 455 | + std::map<sptr<NotificationBundleOption>, bool> &bundleEnable, int32_t userId); | ||
| 456 | + | ||
| 446 | /** | 457 | /** |
| 447 | * @brief Delete all slots in the of bundle from disturbe DB. | 458 | * @brief Delete all slots in the of bundle from disturbe DB. |
| 448 | * | 459 | * |
| @@ -684,6 +695,10 @@ private: | |||
| 684 | std::string GenerateSlotKey( | 695 | std::string GenerateSlotKey( |
| 685 | const std::string &bundleKey, const std::string &type = "", const std::string &subType = "") const; | 696 | const std::string &bundleKey, const std::string &type = "", const std::string &subType = "") const; |
| 686 | std::string GenerateBundleKey(const std::string &bundleKey, const std::string &type = "") const; | 697 | std::string GenerateBundleKey(const std::string &bundleKey, const std::string &type = "") const; |
| 698 | + bool GenerateBundleQueryKeyIndex(const std::vector<sptr<NotificationBundleOption>> &bundleOptions, | ||
| 699 | + const std::function<std::string(const std::string &)> &generateKey, | ||
| 700 | + std::vector<std::string> &keys, | ||
| 701 | + std::unordered_map<std::string, size_t> &keyToIndex) const; | ||
| 687 | int32_t GetUidFromGenerate(const std::string &generateBundleKey) const; | 702 | int32_t GetUidFromGenerate(const std::string &generateBundleKey) const; |
| 688 | 703 | ||
| 689 | void ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey, | 704 | void ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey, |
| @@ -104,23 +104,6 @@ namespace { | |||
| 104 | VOICE_BROADCAST_CONFIG_RULE_KEY, | 104 | VOICE_BROADCAST_CONFIG_RULE_KEY, |
| 105 | LIVE_VIEW_SHARE_FUNC_SWITCH_KEY, | 105 | LIVE_VIEW_SHARE_FUNC_SWITCH_KEY, |
| 106 | }; | 106 | }; |
| 107 | - | ||
| 108 | - std::vector<sptr<NotificationBundleOption>> ResolveBundleOptionUids( | ||
| 109 | - const std::vector<sptr<NotificationBundleOption>> &bundleOptions) | ||
| 110 | - { | ||
| 111 | - std::vector<sptr<NotificationBundleOption>> resolvedBundles; | ||
| 112 | - resolvedBundles.reserve(bundleOptions.size()); | ||
| 113 | - for (const auto &option : bundleOptions) { | ||
| 114 | - sptr<NotificationBundleOption> validBundle = | ||
| 115 | - AdvancedNotificationService::GenerateValidBundleOption(option); | ||
| 116 | - if (validBundle == nullptr) { | ||
| 117 | - ANS_LOGE("Invalid or unresolvable bundle option, skipped."); | ||
| 118 | - continue; | ||
| 119 | - } | ||
| 120 | - resolvedBundles.push_back(validBundle); | ||
| 121 | - } | ||
| 122 | - return resolvedBundles; | ||
| 123 | - } | ||
| 124 | } | 107 | } |
| 125 | 108 | ||
| 126 | ErrCode AdvancedNotificationService::AddSlots(const std::vector<sptr<NotificationSlot>> &slots) | 109 | ErrCode AdvancedNotificationService::AddSlots(const std::vector<sptr<NotificationSlot>> &slots) |
| @@ -174,6 +174,22 @@ sptr<NotificationBundleOption> AdvancedNotificationService::GenerateValidBundleO | |||
| 174 | return validBundleOption; | 174 | return validBundleOption; |
| 175 | } | 175 | } |
| 176 | 176 | ||
| 177 | +std::vector<sptr<NotificationBundleOption>> AdvancedNotificationService::ResolveBundleOptionUids( | ||
| 178 | + const std::vector<sptr<NotificationBundleOption>> &bundleOptions) | ||
| 179 | +{ | ||
| 180 | + std::vector<sptr<NotificationBundleOption>> resolvedBundles; | ||
| 181 | + resolvedBundles.reserve(bundleOptions.size()); | ||
| 182 | + for (const auto &option : bundleOptions) { | ||
| 183 | + sptr<NotificationBundleOption> validBundle = GenerateValidBundleOption(option); | ||
| 184 | + if (validBundle == nullptr) { | ||
| 185 | + ANS_LOGE("Invalid or unresolvable bundle option, skipped."); | ||
| 186 | + continue; | ||
| 187 | + } | ||
| 188 | + resolvedBundles.push_back(validBundle); | ||
| 189 | + } | ||
| 190 | + return resolvedBundles; | ||
| 191 | +} | ||
| 192 | + | ||
| 177 | sptr<NotificationSortingMap> AdvancedNotificationService::GenerateSortingMap() | 193 | sptr<NotificationSortingMap> AdvancedNotificationService::GenerateSortingMap() |
| 178 | { | 194 | { |
| 179 | std::vector<NotificationSorting> sortingList; | 195 | std::vector<NotificationSorting> sortingList; |
| @@ -256,36 +256,39 @@ ErrCode AdvancedNotificationService::GetShowBadgeEnabledForBundles( | |||
| 256 | message.ErrorCode(ERR_ANS_INNER_PERMISSION_DENIED).BranchId(BRANCH_1)); | 256 | message.ErrorCode(ERR_ANS_INNER_PERMISSION_DENIED).BranchId(BRANCH_1)); |
| 257 | return ERR_ANS_INNER_PERMISSION_DENIED; | 257 | return ERR_ANS_INNER_PERMISSION_DENIED; |
| 258 | } | 258 | } |
| 259 | - | 259 | + if (bundleOptions.empty()) { |
| 260 | + ANS_LOGE("Invalid bundle options."); | ||
| 261 | + return ERR_ANS_INNER_INVALID_PARAM; | ||
| 262 | + } | ||
| 263 | + auto resolvedBundles = ResolveBundleOptionUids(bundleOptions); | ||
| 264 | + int32_t userId = -1; | ||
| 265 | + if (OsAccountManagerHelper::GetInstance().GetCurrentCallingUserId(userId) != ERR_OK) { | ||
| 266 | + ANS_LOGW("GetCurrentCallingUserId failed, query shared table only."); | ||
| 267 | + } | ||
| 260 | ErrCode result = ERR_OK; | 268 | ErrCode result = ERR_OK; |
| 261 | auto submitResult = notificationSvrQueue_.SyncSubmit(std::bind([&]() { | 269 | auto submitResult = notificationSvrQueue_.SyncSubmit(std::bind([&]() { |
| 262 | - for (sptr<NotificationBundleOption> bundleOption : bundleOptions) { | 270 | + if (resolvedBundles.empty()) { |
| 263 | - sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption); | 271 | + ANS_LOGD("No valid bundle options after uid resolution."); |
| 264 | - if (bundle == nullptr) { | 272 | + return; |
| 265 | - continue; | ||
| 266 | - } | ||
| 267 | - bool enable = false; | ||
| 268 | - result = NotificationPreferences::GetInstance()->IsShowBadge(bundle, enable); | ||
| 269 | - if (result == ERR_ANS_INNER_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { | ||
| 270 | - result = ERR_OK; | ||
| 271 | - enable = true; | ||
| 272 | - } | ||
| 273 | - if (result != ERR_OK) { | ||
| 274 | - ANS_LOGE("%{public}s_%{public}d, get showbadge failed.", | ||
| 275 | - bundle->GetBundleName().c_str(), bundle->GetUid()); | ||
| 276 | - message.Message(bundle->GetBundleName() + "_" + std::to_string(bundle->GetUid()) + | ||
| 277 | - " get showbadge failed."); | ||
| 278 | - NotificationAnalyticsUtil::ReportModifyEvent(message.ErrorCode(result).BranchId(BRANCH_3)); | ||
| 279 | - continue; | ||
| 280 | - } | ||
| 281 | - bundleEnable.insert(std::make_pair(bundle, enable)); | ||
| 282 | } | 273 | } |
| 274 | + bool dbResult = NotificationPreferences::GetInstance()->GetShowBadgeEnabledForBundles( | ||
| 275 | + resolvedBundles, bundleEnable, userId); | ||
| 276 | + if (!dbResult) { | ||
| 277 | + result = ERR_ANS_INNER_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; | ||
| 278 | + return; | ||
| 279 | + } | ||
| 280 | + for (const auto &bundle : resolvedBundles) { | ||
| 281 | + if (bundleEnable.find(bundle) == bundleEnable.end()) { | ||
| 282 | + bundleEnable[bundle] = true; | ||
| 283 | + } | ||
| 284 | + } | ||
| 285 | + result = ERR_OK; | ||
| 283 | })); | 286 | })); |
| 284 | ANS_COND_DO_ERR(submitResult != ERR_OK, return submitResult, "Get badge enable for bundle."); | 287 | ANS_COND_DO_ERR(submitResult != ERR_OK, return submitResult, "Get badge enable for bundle."); |
| 285 | message.ErrorCode(result).Message("GetShowBadgeEnabledForBundles end").BranchId(BRANCH_4); | 288 | message.ErrorCode(result).Message("GetShowBadgeEnabledForBundles end").BranchId(BRANCH_4); |
| 286 | NotificationAnalyticsUtil::ReportModifyEvent(message); | 289 | NotificationAnalyticsUtil::ReportModifyEvent(message); |
| 287 | ANS_LOGD("GetShowBadgeEnabledForBundles end"); | 290 | ANS_LOGD("GetShowBadgeEnabledForBundles end"); |
| 288 | - return ERR_OK; | 291 | + return result; |
| 289 | } | 292 | } |
| 290 | 293 | ||
| 291 | ErrCode AdvancedNotificationService::GetShowBadgeEnabled(const sptr<IAnsResultDataSynchronizer> &synchronizer) | 294 | ErrCode AdvancedNotificationService::GetShowBadgeEnabled(const sptr<IAnsResultDataSynchronizer> &synchronizer) |
| @@ -1329,6 +1329,18 @@ bool __attribute__((weak)) NotificationPreferences::GetEnabledForBundleSlots( | |||
| 1329 | return preferncesDB_->GetEnabledForBundleSlots(bundleOptions, slotType, slotEnabled, userId); | 1329 | return preferncesDB_->GetEnabledForBundleSlots(bundleOptions, slotType, slotEnabled, userId); |
| 1330 | } | 1330 | } |
| 1331 | 1331 | ||
| 1332 | +bool __attribute__((weak)) NotificationPreferences::GetShowBadgeEnabledForBundles( | ||
| 1333 | + const std::vector<sptr<NotificationBundleOption>> &bundleOptions, | ||
| 1334 | + std::map<sptr<NotificationBundleOption>, bool> &bundleEnable, int32_t userId) | ||
| 1335 | +{ | ||
| 1336 | + ANS_LOGD("called"); | ||
| 1337 | + std::lock_guard<ffrt::mutex> lock(preferenceMutex_); | ||
| 1338 | + if (preferncesDB_ == nullptr) { | ||
| 1339 | + return false; | ||
| 1340 | + } | ||
| 1341 | + return preferncesDB_->GetShowBadgeEnabledForBundles(bundleOptions, bundleEnable, userId); | ||
| 1342 | +} | ||
| 1343 | + | ||
| 1332 | ErrCode NotificationPreferences::ClearNotificationInRestoreFactorySettings() | 1344 | ErrCode NotificationPreferences::ClearNotificationInRestoreFactorySettings() |
| 1333 | { | 1345 | { |
| 1334 | ErrCode result = ERR_OK; | 1346 | ErrCode result = ERR_OK; |
| @@ -1205,18 +1205,12 @@ bool NotificationPreferencesDatabase::GetEnabledForBundleSlots( | |||
| 1205 | } | 1205 | } |
| 1206 | 1206 | ||
| 1207 | std::vector<std::string> keys; | 1207 | std::vector<std::string> keys; |
| 1208 | - keys.reserve(bundleOptions.size()); | ||
| 1209 | std::unordered_map<std::string, size_t> keyToIndex; | 1208 | std::unordered_map<std::string, size_t> keyToIndex; |
| 1210 | - for (size_t i = 0; i < bundleOptions.size(); ++i) { | 1209 | + if (!GenerateBundleQueryKeyIndex(bundleOptions, |
| 1211 | - if (bundleOptions[i] == nullptr) { | 1210 | + [this, slotType](const std::string &bundleKey) { |
| 1212 | - continue; | 1211 | + return GenerateSlotKey(bundleKey, std::to_string(slotType), KEY_SLOT_ENABLED); |
| 1213 | - } | 1212 | + }, |
| 1214 | - std::string bundleKey = bundleOptions[i]->GetBundleName() + std::to_string(bundleOptions[i]->GetUid()); | 1213 | + keys, keyToIndex)) { |
| 1215 | - std::string slotKey = GenerateSlotKey(bundleKey, std::to_string(slotType), KEY_SLOT_ENABLED); | ||
| 1216 | - keys.push_back(slotKey); | ||
| 1217 | - keyToIndex[slotKey] = i; | ||
| 1218 | - } | ||
| 1219 | - if (keys.empty()) { | ||
| 1220 | return true; | 1214 | return true; |
| 1221 | } | 1215 | } |
| 1222 | 1216 | ||
| @@ -1236,6 +1230,43 @@ bool NotificationPreferencesDatabase::GetEnabledForBundleSlots( | |||
| 1236 | return true; | 1230 | return true; |
| 1237 | } | 1231 | } |
| 1238 | 1232 | ||
| 1233 | +bool NotificationPreferencesDatabase::GetShowBadgeEnabledForBundles( | ||
| 1234 | + const std::vector<sptr<NotificationBundleOption>> &bundleOptions, | ||
| 1235 | + std::map<sptr<NotificationBundleOption>, bool> &bundleEnable, | ||
| 1236 | + int32_t userId) | ||
| 1237 | +{ | ||
| 1238 | + ANS_LOGD("called"); | ||
| 1239 | + if (!CheckRdbStore()) { | ||
| 1240 | + return false; | ||
| 1241 | + } | ||
| 1242 | + if (bundleOptions.empty()) { | ||
| 1243 | + return false; | ||
| 1244 | + } | ||
| 1245 | + | ||
| 1246 | + std::vector<std::string> keys; | ||
| 1247 | + std::unordered_map<std::string, size_t> keyToIndex; | ||
| 1248 | + if (!GenerateBundleQueryKeyIndex(bundleOptions, | ||
| 1249 | + [this](const std::string &bundleKey) { return GenerateBundleKey(bundleKey, KEY_BUNDLE_SHOW_BADGE); }, | ||
| 1250 | + keys, keyToIndex)) { | ||
| 1251 | + return true; | ||
| 1252 | + } | ||
| 1253 | + | ||
| 1254 | + std::unordered_map<std::string, std::string> results; | ||
| 1255 | + if (rdbDataManager_->QueryDataInKeys(keys, results, userId) != NativeRdb::E_OK) { | ||
| 1256 | + return false; | ||
| 1257 | + } | ||
| 1258 | + | ||
| 1259 | + for (const auto &entry : results) { | ||
| 1260 | + auto it = keyToIndex.find(entry.first); | ||
| 1261 | + if (it == keyToIndex.end()) { | ||
| 1262 | + continue; | ||
| 1263 | + } | ||
| 1264 | + bool enabled = static_cast<bool>(AnsCommonUtils::StringToInt(entry.second)); | ||
| 1265 | + bundleEnable[bundleOptions[it->second]] = enabled; | ||
A | |||
| 1266 | + } | ||
| 1267 | + return true; | ||
| 1268 | +} | ||
| 1269 | + | ||
| 1239 | bool NotificationPreferencesDatabase::HandleDataBaseMapInner( | 1270 | bool NotificationPreferencesDatabase::HandleDataBaseMapInner( |
| 1240 | const std::unordered_map<std::string, std::string> &datas, | 1271 | const std::unordered_map<std::string, std::string> &datas, |
| 1241 | std::vector<NotificationBundleOption> &bundleOption, const int32_t currentUserId) | 1272 | std::vector<NotificationBundleOption> &bundleOption, const int32_t currentUserId) |
| @@ -1964,6 +1995,25 @@ std::string NotificationPreferencesDatabase::GenerateBundleKey( | |||
| 1964 | return key; | 1995 | return key; |
| 1965 | } | 1996 | } |
| 1966 | 1997 | ||
| 1998 | +bool NotificationPreferencesDatabase::GenerateBundleQueryKeyIndex( | ||
| 1999 | + const std::vector<sptr<NotificationBundleOption>> &bundleOptions, | ||
| 2000 | + const std::function<std::string(const std::string &)> &generateKey, | ||
| 2001 | + std::vector<std::string> &keys, | ||
| 2002 | + std::unordered_map<std::string, size_t> &keyToIndex) const | ||
| 2003 | +{ | ||
| 2004 | + keys.reserve(bundleOptions.size()); | ||
| 2005 | + for (size_t i = 0; i < bundleOptions.size(); ++i) { | ||
| 2006 | + if (bundleOptions[i] == nullptr) { | ||
| 2007 | + continue; | ||
| 2008 | + } | ||
| 2009 | + std::string bundleKey = bundleOptions[i]->GetBundleName() + std::to_string(bundleOptions[i]->GetUid()); | ||
| 2010 | + std::string queryKey = generateKey(bundleKey); | ||
| 2011 | + keys.push_back(queryKey); | ||
| 2012 | + keyToIndex[queryKey] = i; | ||
| 2013 | + } | ||
| 2014 | + return !keys.empty(); | ||
| 2015 | +} | ||
| 2016 | + | ||
| 1967 | int32_t NotificationPreferencesDatabase::GetUidFromGenerate(const std::string &generateBundleKey) const | 2017 | int32_t NotificationPreferencesDatabase::GetUidFromGenerate(const std::string &generateBundleKey) const |
| 1968 | { | 2018 | { |
| 1969 | auto pos = generateBundleKey.rfind(KEY_UNDER_LINE); | 2019 | auto pos = generateBundleKey.rfind(KEY_UNDER_LINE); |
| @@ -69,6 +69,7 @@ using namespace OHOS::Media; | |||
| 69 | 69 | ||
| 70 | extern void MockQueryForgroundOsAccountId(bool mockRet, uint8_t mockCase); | 70 | extern void MockQueryForgroundOsAccountId(bool mockRet, uint8_t mockCase); |
| 71 | extern void MockQueryAllCreatedOsAccounts(int32_t userId); | 71 | extern void MockQueryAllCreatedOsAccounts(int32_t userId); |
| 72 | +extern void MockGetOsAccountLocalIdFromUid(bool mockRet, uint8_t mockCase = 0); | ||
| 72 | 73 | ||
| 73 | namespace OHOS { | 74 | namespace OHOS { |
| 74 | namespace Notification { | 75 | namespace Notification { |
| @@ -6333,7 +6334,265 @@ HWTEST_F(AdvancedNotificationServiceTest, GetShowBadgeEnabledForBundles_100, Fun | |||
| 6333 | std::vector<sptr<NotificationBundleOption>> bundleOptions; | 6334 | std::vector<sptr<NotificationBundleOption>> bundleOptions; |
| 6334 | std::map<sptr<NotificationBundleOption>, bool> bundleEnable; | 6335 | std::map<sptr<NotificationBundleOption>, bool> bundleEnable; |
| 6335 | auto ret = advancedNotificationService_->GetShowBadgeEnabledForBundles(bundleOptions, bundleEnable); | 6336 | auto ret = advancedNotificationService_->GetShowBadgeEnabledForBundles(bundleOptions, bundleEnable); |
| 6336 | - EXPECT_EQ(ret, ERR_OK); | 6337 | + EXPECT_EQ(ret, ERR_ANS_INNER_INVALID_PARAM); |
| 6338 | +} | ||
| 6339 | + | ||
| 6340 | +/** | ||
| 6341 | + * @tc.name : GetShowBadgeEnabledForBundles_00001 | ||
| 6342 | + * @tc.desc : Test GetShowBadgeEnabledForBundles normal scenario: permission OK + batch query | ||
| 6343 | + * returns the persisted show badge values. | ||
| 6344 | + * @tc.type : FUNC | ||
| 6345 | + */ | ||
| 6346 | +HWTEST_F(AdvancedNotificationServiceTest, GetShowBadgeEnabledForBundles_00001, Function | SmallTest | Level1) | ||
| 6347 | +{ | ||
| 6348 | + MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE); | ||
| 6349 | + MockIsSystemApp(true); | ||
| 6350 | + MockIsVerfyPermisson(true); | ||
| 6351 | + MockGetOsAccountLocalIdFromUid(true); | ||
| 6352 | + sptr<NotificationBundleOption> bundleOn = | ||
| 6353 | + new NotificationBundleOption("BadgeEnabledBundle_00001", 21001); | ||
| 6354 | + sptr<NotificationBundleOption> bundleOff = | ||
| 6355 | + new NotificationBundleOption("BadgeDisabledBundle_00001", 21002); | ||
| 6356 | + std::map<sptr<NotificationBundleOption>, bool> setOptions; | ||
| 6357 | + setOptions[bundleOn] = true; | ||
| 6358 | + setOptions[bundleOff] = false; | ||
| 6359 | + ASSERT_EQ(advancedNotificationService_->SetShowBadgeEnabledForBundles(setOptions), (int)ERR_OK); | ||
| 6360 | + std::vector<sptr<NotificationBundleOption>> bundleOptions; | ||
| 6361 | + bundleOptions.push_back(bundleOn); | ||
| 6362 | + bundleOptions.push_back(bundleOff); | ||
| 6363 | + std::map<sptr<NotificationBundleOption>, bool> bundleEnable; | ||
| 6364 | + auto ret = advancedNotificationService_->GetShowBadgeEnabledForBundles(bundleOptions, bundleEnable); | ||
| 6365 | + ASSERT_EQ(ret, (int)ERR_OK); | ||
| 6366 | + ASSERT_EQ(bundleEnable.size(), 2u); | ||
| 6367 | + EXPECT_NE(bundleEnable.end(), bundleEnable.find(bundleOn)); | ||
| 6368 | + EXPECT_TRUE(bundleEnable[bundleOn]); | ||
| 6369 | + EXPECT_NE(bundleEnable.end(), bundleEnable.find(bundleOff)); | ||
| 6370 | + EXPECT_FALSE(bundleEnable[bundleOff]); | ||
| 6371 | +} | ||
| 6372 | + | ||
| 6373 | +/** | ||
| 6374 | + * @tc.name : GetShowBadgeEnabledForBundles_00002 | ||
| 6375 | + * @tc.desc : Test GetShowBadgeEnabledForBundles permission denied: non-system app returns | ||
| 6376 | + * ERR_ANS_INNER_NON_SYSTEM_APP. | ||
| 6377 | + * @tc.type : FUNC | ||
| 6378 | + */ | ||
| 6379 | +HWTEST_F(AdvancedNotificationServiceTest, GetShowBadgeEnabledForBundles_00002, Function | SmallTest | Level1) | ||
| 6380 | +{ | ||
| 6381 | + MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); | ||
| 6382 | + MockIsSystemApp(false); | ||
| 6383 | + MockIsVerfyPermisson(true); | ||
| 6384 | + std::vector<sptr<NotificationBundleOption>> bundleOptions; | ||
| 6385 | + bundleOptions.push_back(new NotificationBundleOption("BadgeNonSystemApp_00002", 22001)); | ||
| 6386 | + std::map<sptr<NotificationBundleOption>, bool> bundleEnable; | ||
| 6387 | + auto ret = advancedNotificationService_->GetShowBadgeEnabledForBundles(bundleOptions, bundleEnable); | ||
| 6388 | + ASSERT_EQ(ret, (int)ERR_ANS_INNER_NON_SYSTEM_APP); | ||
| 6389 | + EXPECT_EQ(bundleEnable.size(), 0u); | ||
| 6390 | +} | ||
| 6391 | + | ||
| 6392 | +/** | ||
| 6393 | + * @tc.name : GetShowBadgeEnabledForBundles_00003 | ||
| 6394 | + * @tc.desc : Test GetShowBadgeEnabledForBundles permission denied: missing | ||
| 6395 | + * NOTIFICATION_CONTROLLER returns ERR_ANS_INNER_PERMISSION_DENIED. | ||
| 6396 | + * @tc.type : FUNC | ||
| 6397 | + */ | ||
| 6398 | +HWTEST_F(AdvancedNotificationServiceTest, GetShowBadgeEnabledForBundles_00003, Function | SmallTest | Level1) | ||
| 6399 | +{ | ||
| 6400 | + MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); | ||
| 6401 | + MockIsSystemApp(true); | ||
| 6402 | + MockIsVerfyPermisson(false); | ||
| 6403 | + std::vector<sptr<NotificationBundleOption>> bundleOptions; | ||
| 6404 | + bundleOptions.push_back(new NotificationBundleOption("BadgeNoPermission_00003", 23001)); | ||
| 6405 | + std::map<sptr<NotificationBundleOption>, bool> bundleEnable; | ||
| 6406 | + auto ret = advancedNotificationService_->GetShowBadgeEnabledForBundles(bundleOptions, bundleEnable); | ||
| 6407 | + ASSERT_EQ(ret, (int)ERR_ANS_INNER_PERMISSION_DENIED); | ||
| 6408 | + EXPECT_EQ(bundleEnable.size(), 0u); | ||
| 6409 | +} | ||
| 6410 | + | ||
| 6411 | +/** | ||
| 6412 | + * @tc.name : GetShowBadgeEnabledForBundles_00004 | ||
| 6413 | + * @tc.desc : Test GetShowBadgeEnabledForBundles invalid param: empty bundleOptions returns | ||
| 6414 | + * ERR_ANS_INNER_INVALID_PARAM. | ||
| 6415 | + * @tc.type : FUNC | ||
| 6416 | + */ | ||
| 6417 | +HWTEST_F(AdvancedNotificationServiceTest, GetShowBadgeEnabledForBundles_00004, Function | SmallTest | Level1) | ||
| 6418 | +{ | ||
| 6419 | + MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE); | ||
| 6420 | + MockIsSystemApp(true); | ||
| 6421 | + MockIsVerfyPermisson(true); | ||
| 6422 | + std::vector<sptr<NotificationBundleOption>> bundleOptions; | ||
| 6423 | + std::map<sptr<NotificationBundleOption>, bool> bundleEnable; | ||
| 6424 | + auto ret = advancedNotificationService_->GetShowBadgeEnabledForBundles(bundleOptions, bundleEnable); | ||
| 6425 | + ASSERT_EQ(ret, (int)ERR_ANS_INNER_INVALID_PARAM); | ||
| 6426 | + EXPECT_EQ(bundleEnable.size(), 0u); | ||
| 6427 | +} | ||
| 6428 | + | ||
| 6429 | +/** | ||
| 6430 | + * @tc.name : GetShowBadgeEnabledForBundles_00005 | ||
| 6431 | + * @tc.desc : Test GetShowBadgeEnabledForBundles with all null bundle options: uid resolution | ||
| 6432 | + * skips them, returns ERR_OK with empty map. | ||
| 6433 | + * @tc.type : FUNC | ||
| 6434 | + */ | ||
| 6435 | +HWTEST_F(AdvancedNotificationServiceTest, GetShowBadgeEnabledForBundles_00005, Function | SmallTest | Level1) | ||
| 6436 | +{ | ||
| 6437 | + MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE); | ||
| 6438 | + MockIsSystemApp(true); | ||
| 6439 | + MockIsVerfyPermisson(true); | ||
| 6440 | + std::vector<sptr<NotificationBundleOption>> bundleOptions; | ||
| 6441 | + bundleOptions.push_back(nullptr); | ||
| 6442 | + bundleOptions.push_back(nullptr); | ||
| 6443 | + std::map<sptr<NotificationBundleOption>, bool> bundleEnable; | ||
| 6444 | + auto ret = advancedNotificationService_->GetShowBadgeEnabledForBundles(bundleOptions, bundleEnable); | ||
| 6445 | + ASSERT_EQ(ret, (int)ERR_OK); | ||
| 6446 | + EXPECT_EQ(bundleEnable.size(), 0u); | ||
| 6447 | +} | ||
| 6448 | + | ||
| 6449 | +/** | ||
| 6450 | + * @tc.name : GetShowBadgeEnabledForBundles_00006 | ||
| 6451 | + * @tc.desc : Test GetShowBadgeEnabledForBundles with bundle option whose uid is not filled | ||
| 6452 | + * (uid<=0). The bundle is resolved to NON_SYSTEM_APP_UID by mock bundle manager, | ||
| 6453 | + * and the no-record bundle is filled with default enabled true. | ||
| 6454 | + * @tc.type : FUNC | ||
| 6455 | + */ | ||
| 6456 | +HWTEST_F(AdvancedNotificationServiceTest, GetShowBadgeEnabledForBundles_00006, Function | SmallTest | Level1) | ||
| 6457 | +{ | ||
| 6458 | + MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE); | ||
| 6459 | + MockIsSystemApp(true); | ||
| 6460 | + MockIsVerfyPermisson(true); | ||
| 6461 | + MockQueryForgroundOsAccountId(true, 0); | ||
| 6462 | + MockGetOsAccountLocalIdFromUid(true); | ||
| 6463 | + std::vector<sptr<NotificationBundleOption>> bundleOptions; | ||
| 6464 | + bundleOptions.push_back(new NotificationBundleOption("BadgeUidNotFilled_00006", 0)); | ||
| 6465 | + std::map<sptr<NotificationBundleOption>, bool> bundleEnable; | ||
| 6466 | + auto ret = advancedNotificationService_->GetShowBadgeEnabledForBundles(bundleOptions, bundleEnable); | ||
| 6467 | + ASSERT_EQ(ret, (int)ERR_OK); | ||
| 6468 | + ASSERT_EQ(bundleEnable.size(), 1u); | ||
| 6469 | + const auto &entry = *bundleEnable.begin(); | ||
| 6470 | + EXPECT_EQ(entry.first->GetBundleName(), "BadgeUidNotFilled_00006"); | ||
| 6471 | + EXPECT_EQ(entry.first->GetUid(), 1000); // NON_SYSTEM_APP_UID returned by mock_bundle_manager_helper | ||
| 6472 | + EXPECT_TRUE(entry.second); | ||
| 6473 | +} | ||
| 6474 | + | ||
| 6475 | +/** | ||
| 6476 | + * @tc.name : GetShowBadgeEnabledForBundles_00007 | ||
| 6477 | + * @tc.desc : Test GetShowBadgeEnabledForBundles with recorded bundle + uid-not-filled bundle + | ||
| 6478 | + * null: recorded value returned, resolved bundle filled with default true, null skipped. | ||
| 6479 | + * @tc.type : FUNC | ||
| 6480 | + */ | ||
| 6481 | +HWTEST_F(AdvancedNotificationServiceTest, GetShowBadgeEnabledForBundles_00007, Function | SmallTest | Level1) | ||
| 6482 | +{ | ||
| 6483 | + MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE); | ||
| 6484 | + MockIsSystemApp(true); | ||
| 6485 | + MockIsVerfyPermisson(true); | ||
| 6486 | + MockQueryForgroundOsAccountId(true, 0); | ||
| 6487 | + MockGetOsAccountLocalIdFromUid(true); | ||
| 6488 | + sptr<NotificationBundleOption> bundleRecorded = | ||
| 6489 | + new NotificationBundleOption("BadgeMixedRecorded_00007", 24001); | ||
| 6490 | + std::map<sptr<NotificationBundleOption>, bool> setOptions; | ||
| 6491 | + setOptions[bundleRecorded] = false; | ||
| 6492 | + ASSERT_EQ(advancedNotificationService_->SetShowBadgeEnabledForBundles(setOptions), (int)ERR_OK); | ||
| 6493 | + std::vector<sptr<NotificationBundleOption>> bundleOptions; | ||
| 6494 | + bundleOptions.push_back(bundleRecorded); | ||
| 6495 | + bundleOptions.push_back(new NotificationBundleOption("BadgeMixedUidNotFilled_00007", 0)); | ||
| 6496 | + bundleOptions.push_back(nullptr); | ||
| 6497 | + std::map<sptr<NotificationBundleOption>, bool> bundleEnable; | ||
| 6498 | + auto ret = advancedNotificationService_->GetShowBadgeEnabledForBundles(bundleOptions, bundleEnable); | ||
| 6499 | + ASSERT_EQ(ret, (int)ERR_OK); | ||
| 6500 | + ASSERT_EQ(bundleEnable.size(), 2u); | ||
| 6501 | + EXPECT_NE(bundleEnable.end(), bundleEnable.find(bundleRecorded)); | ||
| 6502 | + EXPECT_FALSE(bundleEnable[bundleRecorded]); | ||
| 6503 | + bool foundResolved = false; | ||
| 6504 | + for (const auto &entry : bundleEnable) { | ||
| 6505 | + if (entry.first != nullptr && entry.first->GetBundleName() == "BadgeMixedUidNotFilled_00007") { | ||
| 6506 | + EXPECT_EQ(entry.first->GetUid(), 1000); // resolved uid from mock_bundle_manager_helper | ||
| 6507 | + EXPECT_TRUE(entry.second); | ||
| 6508 | + foundResolved = true; | ||
| 6509 | + } | ||
| 6510 | + } | ||
| 6511 | + EXPECT_TRUE(foundResolved); | ||
| 6512 | +} | ||
| 6513 | + | ||
| 6514 | +/** | ||
| 6515 | + * @tc.name : GetShowBadgeEnabledForBundles_00008 | ||
| 6516 | + * @tc.desc : Test GetShowBadgeEnabledForBundles when preferences database is null, returns | ||
| 6517 | + * ERR_ANS_INNER_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED. | ||
| 6518 | + * @tc.type : FUNC | ||
| 6519 | + */ | ||
| 6520 | +HWTEST_F(AdvancedNotificationServiceTest, GetShowBadgeEnabledForBundles_00008, Function | SmallTest | Level1) | ||
| 6521 | +{ | ||
| 6522 | + MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE); | ||
| 6523 | + MockIsSystemApp(true); | ||
| 6524 | + MockIsVerfyPermisson(true); | ||
| 6525 | + auto preferences = NotificationPreferences::GetInstance(); | ||
| 6526 | + auto originalDB = preferences->preferncesDB_; | ||
| 6527 | + preferences->preferncesDB_ = nullptr; | ||
| 6528 | + std::vector<sptr<NotificationBundleOption>> bundleOptions; | ||
| 6529 | + bundleOptions.push_back(new NotificationBundleOption("BadgeNullDB_00008", 25001)); | ||
| 6530 | + std::map<sptr<NotificationBundleOption>, bool> bundleEnable; | ||
| 6531 | + auto ret = advancedNotificationService_->GetShowBadgeEnabledForBundles(bundleOptions, bundleEnable); | ||
| 6532 | + preferences->preferncesDB_ = originalDB; | ||
| 6533 | + EXPECT_EQ(ret, (int)ERR_ANS_INNER_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED); | ||
| 6534 | + EXPECT_EQ(bundleEnable.size(), 0u); | ||
| 6535 | +} | ||
| 6536 | + | ||
| 6537 | +/** | ||
| 6538 | + * @tc.name : GetShowBadgeEnabledForBundles_00009 | ||
| 6539 | + * @tc.desc : Test GetShowBadgeEnabledForBundles with valid bundle options when | ||
| 6540 | + * notificationSvrQueue_ submit fails, returns ERR_ANS_INNER_INVALID_PARAM. | ||
| 6541 | + * @tc.type : FUNC | ||
| 6542 | + */ | ||
| 6543 | +HWTEST_F(AdvancedNotificationServiceTest, GetShowBadgeEnabledForBundles_00009, Function | SmallTest | Level1) | ||
| 6544 | +{ | ||
| 6545 | + MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE); | ||
| 6546 | + MockIsSystemApp(true); | ||
| 6547 | + MockIsVerfyPermisson(true); | ||
| 6548 | + std::vector<sptr<NotificationBundleOption>> bundleOptions; | ||
| 6549 | + bundleOptions.push_back(new NotificationBundleOption("BadgeQueueReset_00009", 26001)); | ||
| 6550 | + std::map<sptr<NotificationBundleOption>, bool> bundleEnable; | ||
| 6551 | + advancedNotificationService_->notificationSvrQueue_.Reset(); | ||
| 6552 | + auto ret = advancedNotificationService_->GetShowBadgeEnabledForBundles(bundleOptions, bundleEnable); | ||
| 6553 | + ASSERT_EQ(ret, (int)ERR_ANS_INNER_INVALID_PARAM); | ||
| 6554 | + EXPECT_EQ(bundleEnable.size(), 0u); | ||
| 6555 | +} | ||
| 6556 | + | ||
| 6557 | +/** | ||
| 6558 | + * @tc.name : GetShowBadgeEnabledForBundles_00010 | ||
| 6559 | + * @tc.desc : Test GetShowBadgeEnabledForBundles when GetCurrentCallingUserId fails: | ||
| 6560 | + * userId is invalid, only the shared table is queried: the shared-table | ||
| 6561 | + * record is returned, the user-table record is invisible and the | ||
| 6562 | + * missing bundle is filled with the default enabled true. | ||
| 6563 | + * @tc.type : FUNC | ||
| 6564 | + */ | ||
| 6565 | +HWTEST_F(AdvancedNotificationServiceTest, GetShowBadgeEnabledForBundles_00010, Function | SmallTest | Level1) | ||
| 6566 | +{ | ||
| 6567 | + MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE); | ||
| 6568 | + MockIsSystemApp(true); | ||
| 6569 | + MockIsVerfyPermisson(true); | ||
| 6570 | + MockGetOsAccountLocalIdFromUid(true, 0); | ||
| 6571 | + MockGetOsAccountLocalIdFromUid(true, 2); | ||
| 6572 | + sptr<NotificationBundleOption> bundleShared = | ||
| 6573 | + new NotificationBundleOption("BadgeSharedTable_00010", 27001); | ||
| 6574 | + std::map<sptr<NotificationBundleOption>, bool> setShared; | ||
| 6575 | + setShared[bundleShared] = false; | ||
| 6576 | + ASSERT_EQ(advancedNotificationService_->SetShowBadgeEnabledForBundles(setShared), (int)ERR_OK); | ||
| 6577 | + MockGetOsAccountLocalIdFromUid(true, 0); | ||
| 6578 | + sptr<NotificationBundleOption> bundleUserTable = | ||
| 6579 | + new NotificationBundleOption("BadgeUserTable_00010", 27002); | ||
| 6580 | + std::map<sptr<NotificationBundleOption>, bool> setUserTable; | ||
| 6581 | + setUserTable[bundleUserTable] = false; | ||
| 6582 | + ASSERT_EQ(advancedNotificationService_->SetShowBadgeEnabledForBundles(setUserTable), (int)ERR_OK); | ||
| 6583 | + MockGetOsAccountLocalIdFromUid(false, 1); | ||
| 6584 | + std::vector<sptr<NotificationBundleOption>> bundleOptions; | ||
| 6585 | + bundleOptions.push_back(bundleShared); | ||
| 6586 | + bundleOptions.push_back(bundleUserTable); | ||
| 6587 | + std::map<sptr<NotificationBundleOption>, bool> bundleEnable; | ||
| 6588 | + auto ret = advancedNotificationService_->GetShowBadgeEnabledForBundles(bundleOptions, bundleEnable); | ||
| 6589 | + MockGetOsAccountLocalIdFromUid(true, 0); | ||
| 6590 | + EXPECT_EQ(ret, (int)ERR_OK); | ||
| 6591 | + EXPECT_EQ(bundleEnable.size(), 2u); | ||
| 6592 | + EXPECT_NE(bundleEnable.end(), bundleEnable.find(bundleShared)); | ||
| 6593 | + EXPECT_FALSE(bundleEnable[bundleShared]); | ||
| 6594 | + EXPECT_NE(bundleEnable.end(), bundleEnable.find(bundleUserTable)); | ||
| 6595 | + EXPECT_TRUE(bundleEnable[bundleUserTable]); | ||
| 6337 | } | 6596 | } |
| 6338 | 6597 | ||
| 6339 | /** | 6598 | /** |
| @@ -178,7 +178,7 @@ HWTEST_F(AdvancedNotificationBadgeManagerTest, GetShowBadgeEnabledForBundles_010 | |||
| 178 | std::vector<sptr<NotificationBundleOption>> bundleOptions; | 178 | std::vector<sptr<NotificationBundleOption>> bundleOptions; |
| 179 | std::map<sptr<NotificationBundleOption>, bool> bundleEnable; | 179 | std::map<sptr<NotificationBundleOption>, bool> bundleEnable; |
| 180 | auto ret = advancedNotificationService_->GetShowBadgeEnabledForBundles(bundleOptions, bundleEnable); | 180 | auto ret = advancedNotificationService_->GetShowBadgeEnabledForBundles(bundleOptions, bundleEnable); |
| 181 | - EXPECT_EQ(ret, ERR_OK); | 181 | + EXPECT_EQ(ret, ERR_ANS_INNER_INVALID_PARAM); |
| 182 | } | 182 | } |
| 183 | 183 | ||
| 184 | /** | 184 | /** |
| @@ -3777,6 +3777,122 @@ HWTEST_F(NotificationPreferencesDatabaseTest, GetEnabledForBundleSlots_00400, Fu | |||
| 3777 | EXPECT_FALSE(foundMissing); | 3777 | EXPECT_FALSE(foundMissing); |
| 3778 | } | 3778 | } |
| 3779 | 3779 | ||
| 3780 | +/** | ||
| 3781 | + * @tc.name : GetShowBadgeEnabledForBundles_00100 | ||
| 3782 | + * @tc.number : GetShowBadgeEnabledForBundles_00100 | ||
| 3783 | + * @tc.desc : Test GetShowBadgeEnabledForBundles with empty bundleOptions, return false. | ||
| 3784 | + */ | ||
| 3785 | +HWTEST_F(NotificationPreferencesDatabaseTest, GetShowBadgeEnabledForBundles_00100, Function | SmallTest | Level1) | ||
| 3786 | +{ | ||
| 3787 | + std::vector<sptr<NotificationBundleOption>> bundleOptions; | ||
| 3788 | + std::map<sptr<NotificationBundleOption>, bool> bundleEnable; | ||
| 3789 | + EXPECT_FALSE(preferncesDB_->GetShowBadgeEnabledForBundles(bundleOptions, bundleEnable, 0)); | ||
| 3790 | + EXPECT_EQ(0u, bundleEnable.size()); | ||
| 3791 | +} | ||
| 3792 | + | ||
| 3793 | +/** | ||
| 3794 | + * @tc.name : GetShowBadgeEnabledForBundles_00200 | ||
| 3795 | + * @tc.number : GetShowBadgeEnabledForBundles_00200 | ||
| 3796 | + * @tc.desc : Test with null bundle option in vector, skipped and returns true. | ||
| 3797 | + */ | ||
| 3798 | +HWTEST_F(NotificationPreferencesDatabaseTest, GetShowBadgeEnabledForBundles_00200, Function | SmallTest | Level1) | ||
| 3799 | +{ | ||
| 3800 | + std::vector<sptr<NotificationBundleOption>> bundleOptions; | ||
| 3801 | + bundleOptions.push_back(nullptr); | ||
| 3802 | + std::map<sptr<NotificationBundleOption>, bool> bundleEnable; | ||
| 3803 | + EXPECT_TRUE(preferncesDB_->GetShowBadgeEnabledForBundles(bundleOptions, bundleEnable, 0)); | ||
| 3804 | + EXPECT_EQ(0u, bundleEnable.size()); | ||
| 3805 | +} | ||
| 3806 | + | ||
| 3807 | +/** | ||
| 3808 | + * @tc.name : GetShowBadgeEnabledForBundles_00300 | ||
| 3809 | + * @tc.number : GetShowBadgeEnabledForBundles_00300 | ||
| 3810 | + * @tc.desc : Test with null rdbDataManager, return false. | ||
| 3811 | + */ | ||
| 3812 | +HWTEST_F(NotificationPreferencesDatabaseTest, GetShowBadgeEnabledForBundles_00300, Function | SmallTest | Level1) | ||
| 3813 | +{ | ||
| 3814 | + preferncesDB_->rdbDataManager_ = nullptr; | ||
| 3815 | + std::vector<sptr<NotificationBundleOption>> bundleOptions; | ||
| 3816 | + bundleOptions.push_back(new NotificationBundleOption("badgeBatchBundleNullRdb", 1001)); | ||
| 3817 | + std::map<sptr<NotificationBundleOption>, bool> bundleEnable; | ||
| 3818 | + EXPECT_FALSE(preferncesDB_->GetShowBadgeEnabledForBundles(bundleOptions, bundleEnable, 0)); | ||
| 3819 | + EXPECT_EQ(0u, bundleEnable.size()); | ||
| 3820 | +} | ||
| 3821 | + | ||
| 3822 | +/** | ||
| 3823 | + * @tc.name : GetShowBadgeEnabledForBundles_00400 | ||
| 3824 | + * @tc.number : GetShowBadgeEnabledForBundles_00400 | ||
| 3825 | + * @tc.desc : Test end-to-end: explicit true/false records returned, no-record bundle excluded, | ||
| 3826 | + * duplicate bundle sptr keeps a single map entry (map dedup by pointer). | ||
| 3827 | + */ | ||
| 3828 | +HWTEST_F(NotificationPreferencesDatabaseTest, GetShowBadgeEnabledForBundles_00400, Function | SmallTest | Level1) | ||
| 3829 | +{ | ||
| 3830 | + const std::string bundleNameEnabled = "badgeBatchEnabled"; | ||
| 3831 | + const int32_t bundleUidEnabled = 1001; | ||
| 3832 | + const std::string bundleNameDisabled = "badgeBatchDisabled"; | ||
| 3833 | + const int32_t bundleUidDisabled = 1002; | ||
| 3834 | + const std::string bundleNameMissing = "badgeBatchMissing"; | ||
| 3835 | + const int32_t bundleUidMissing = 1003; | ||
| 3836 | + const int32_t userId = 0; | ||
| 3837 | + const std::string KEY_SHOW_BADGE = "showBadgeEnable"; | ||
| 3838 | + std::string keyEnabled = preferncesDB_->GenerateBundleKey( | ||
| 3839 | + bundleNameEnabled + std::to_string(bundleUidEnabled), KEY_SHOW_BADGE); | ||
| 3840 | + std::string keyDisabled = preferncesDB_->GenerateBundleKey( | ||
| 3841 | + bundleNameDisabled + std::to_string(bundleUidDisabled), KEY_SHOW_BADGE); | ||
| 3842 | + EXPECT_EQ(NativeRdb::E_OK, preferncesDB_->SetKvToDb(keyEnabled, "1", userId)); | ||
| 3843 | + EXPECT_EQ(NativeRdb::E_OK, preferncesDB_->SetKvToDb(keyDisabled, "0", userId)); | ||
| 3844 | + sptr<NotificationBundleOption> optionEnabled = new NotificationBundleOption(bundleNameEnabled, bundleUidEnabled); | ||
| 3845 | + sptr<NotificationBundleOption> optionDisabled = | ||
| 3846 | + new NotificationBundleOption(bundleNameDisabled, bundleUidDisabled); | ||
| 3847 | + sptr<NotificationBundleOption> optionMissing = new NotificationBundleOption(bundleNameMissing, bundleUidMissing); | ||
| 3848 | + std::vector<sptr<NotificationBundleOption>> bundleOptions; | ||
| 3849 | + bundleOptions.push_back(optionEnabled); | ||
| 3850 | + bundleOptions.push_back(optionDisabled); | ||
| 3851 | + bundleOptions.push_back(optionMissing); | ||
| 3852 | + bundleOptions.push_back(optionEnabled); | ||
| 3853 | + std::map<sptr<NotificationBundleOption>, bool> bundleEnable; | ||
| 3854 | + EXPECT_TRUE(preferncesDB_->GetShowBadgeEnabledForBundles(bundleOptions, bundleEnable, userId)); | ||
| 3855 | + EXPECT_EQ(2u, bundleEnable.size()); | ||
| 3856 | + EXPECT_NE(bundleEnable.end(), bundleEnable.find(optionEnabled)); | ||
| 3857 | + EXPECT_TRUE(bundleEnable[optionEnabled]); | ||
| 3858 | + EXPECT_NE(bundleEnable.end(), bundleEnable.find(optionDisabled)); | ||
| 3859 | + EXPECT_FALSE(bundleEnable[optionDisabled]); | ||
| 3860 | + EXPECT_EQ(bundleEnable.end(), bundleEnable.find(optionMissing)); | ||
| 3861 | +} | ||
| 3862 | + | ||
| 3863 | +/** | ||
| 3864 | + * @tc.name : GetShowBadgeEnabledForBundles_00500 | ||
| 3865 | + * @tc.number : GetShowBadgeEnabledForBundles_00500 | ||
| 3866 | + * @tc.desc : Test with mixed input: null option is skipped, recorded bundle is returned, | ||
| 3867 | + * unrecorded bundle is excluded from the result map. | ||
| 3868 | + */ | ||
| 3869 | +HWTEST_F(NotificationPreferencesDatabaseTest, GetShowBadgeEnabledForBundles_00500, Function | SmallTest | Level1) | ||
| 3870 | +{ | ||
| 3871 | + const std::string bundleNameRecorded = "badgeBatchMixedRecorded"; | ||
| 3872 | + const int32_t bundleUidRecorded = 1004; | ||
| 3873 | + const std::string bundleNameMissing = "badgeBatchMixedMissing"; | ||
| 3874 | + const int32_t bundleUidMissing = 1005; | ||
| 3875 | + const int32_t userId = 0; | ||
| 3876 | + const std::string KEY_SHOW_BADGE = "showBadgeEnable"; | ||
| 3877 | + std::string keyRecorded = preferncesDB_->GenerateBundleKey( | ||
| 3878 | + bundleNameRecorded + std::to_string(bundleUidRecorded), KEY_SHOW_BADGE); | ||
| 3879 | + EXPECT_EQ(NativeRdb::E_OK, preferncesDB_->SetKvToDb(keyRecorded, "1", userId)); | ||
| 3880 | + sptr<NotificationBundleOption> optionRecorded = | ||
| 3881 | + new NotificationBundleOption(bundleNameRecorded, bundleUidRecorded); | ||
| 3882 | + sptr<NotificationBundleOption> optionMissing = | ||
| 3883 | + new NotificationBundleOption(bundleNameMissing, bundleUidMissing); | ||
| 3884 | + std::vector<sptr<NotificationBundleOption>> bundleOptions; | ||
| 3885 | + bundleOptions.push_back(nullptr); | ||
| 3886 | + bundleOptions.push_back(optionRecorded); | ||
| 3887 | + bundleOptions.push_back(optionMissing); | ||
| 3888 | + std::map<sptr<NotificationBundleOption>, bool> bundleEnable; | ||
| 3889 | + EXPECT_TRUE(preferncesDB_->GetShowBadgeEnabledForBundles(bundleOptions, bundleEnable, userId)); | ||
| 3890 | + EXPECT_EQ(1u, bundleEnable.size()); | ||
| 3891 | + EXPECT_NE(bundleEnable.end(), bundleEnable.find(optionRecorded)); | ||
| 3892 | + EXPECT_TRUE(bundleEnable[optionRecorded]); | ||
| 3893 | + EXPECT_EQ(bundleEnable.end(), bundleEnable.find(optionMissing)); | ||
| 3894 | +} | ||
| 3895 | + | ||
| 3780 | /** | 3896 | /** |
| 3781 | * @tc.name : GetAllNotificationEnabledBundles_Optimization_00100 | 3897 | * @tc.name : GetAllNotificationEnabledBundles_Optimization_00100 |
| 3782 | * @tc.number : GetAllNotificationEnabledBundles_Optimization_00100 | 3898 | * @tc.number : GetAllNotificationEnabledBundles_Optimization_00100 |
| @@ -608,6 +608,59 @@ HWTEST_F(NotificationPreferencesTest, IsShowBadge_00300, Function | SmallTest | | |||
| 608 | (int)ERR_ANS_INNER_INVALID_PARAM); | 608 | (int)ERR_ANS_INNER_INVALID_PARAM); |
| 609 | } | 609 | } |
| 610 | 610 | ||
| 611 | +/** | ||
| 612 | + * @tc.number : GetShowBadgeEnabledForBundles_00100 | ||
| 613 | + * @tc.name : GetShowBadgeEnabledForBundles_00100 | ||
| 614 | + * @tc.desc : Test GetShowBadgeEnabledForBundles when preferncesDB_ is null, return false. | ||
| 615 | + */ | ||
| 616 | +HWTEST_F(NotificationPreferencesTest, GetShowBadgeEnabledForBundles_00100, Function | SmallTest | Level1) | ||
| 617 | +{ | ||
| 618 | + NotificationPreferences notificationPreferences; | ||
| 619 | + notificationPreferences.preferncesDB_ = nullptr; | ||
| 620 | + std::vector<sptr<NotificationBundleOption>> bundleOptions; | ||
| 621 | + bundleOptions.push_back(bundleOption_); | ||
| 622 | + std::map<sptr<NotificationBundleOption>, bool> bundleEnable; | ||
| 623 | + EXPECT_FALSE(notificationPreferences.GetShowBadgeEnabledForBundles(bundleOptions, bundleEnable, 100)); | ||
| 624 | + EXPECT_EQ(0u, bundleEnable.size()); | ||
| 625 | +} | ||
| 626 | + | ||
| 627 | +/** | ||
| 628 | + * @tc.number : GetShowBadgeEnabledForBundles_00200 | ||
| 629 | + * @tc.name : GetShowBadgeEnabledForBundles_00200 | ||
| 630 | + * @tc.desc : Test GetShowBadgeEnabledForBundles when rdb data manager is null, return false. | ||
| 631 | + */ | ||
| 632 | +HWTEST_F(NotificationPreferencesTest, GetShowBadgeEnabledForBundles_00200, Function | SmallTest | Level1) | ||
| 633 | +{ | ||
| 634 | + NotificationPreferences notificationPreferences; | ||
| 635 | + notificationPreferences.preferncesDB_ = std::make_shared<NotificationPreferencesDatabase>(); | ||
| 636 | + notificationPreferences.preferncesDB_->rdbDataManager_ = nullptr; | ||
| 637 | + std::vector<sptr<NotificationBundleOption>> bundleOptions; | ||
| 638 | + bundleOptions.push_back(bundleOption_); | ||
| 639 | + std::map<sptr<NotificationBundleOption>, bool> bundleEnable; | ||
| 640 | + EXPECT_FALSE(notificationPreferences.GetShowBadgeEnabledForBundles(bundleOptions, bundleEnable, 100)); | ||
| 641 | + EXPECT_EQ(0u, bundleEnable.size()); | ||
| 642 | +} | ||
| 643 | + | ||
| 644 | +/** | ||
| 645 | + * @tc.number : GetShowBadgeEnabledForBundles_00300 | ||
| 646 | + * @tc.name : GetShowBadgeEnabledForBundles_00300 | ||
| 647 | + * @tc.desc : Set show badge false for bundle, then batch query returns true with | ||
| 648 | + * the persisted false value. | ||
| 649 | + */ | ||
| 650 | +HWTEST_F(NotificationPreferencesTest, GetShowBadgeEnabledForBundles_00300, Function | SmallTest | Level1) | ||
| 651 | +{ | ||
| 652 | + MockGetOsAccountLocalIdFromUid(true); | ||
| 653 | + EXPECT_EQ((int)NotificationPreferences::GetInstance()->SetShowBadge(bundleOption_, false), (int)ERR_OK); | ||
| 654 | + std::vector<sptr<NotificationBundleOption>> bundleOptions; | ||
| 655 | + bundleOptions.push_back(bundleOption_); | ||
| 656 | + std::map<sptr<NotificationBundleOption>, bool> bundleEnable; | ||
| 657 | + EXPECT_TRUE(NotificationPreferences::GetInstance()->GetShowBadgeEnabledForBundles( | ||
| 658 | + bundleOptions, bundleEnable, 100)); | ||
| 659 | + EXPECT_EQ(1u, bundleEnable.size()); | ||
| 660 | + EXPECT_NE(bundleEnable.end(), bundleEnable.find(bundleOption_)); | ||
| 661 | + EXPECT_FALSE(bundleEnable[bundleOption_]); | ||
| 662 | +} | ||
| 663 | + | ||
| 611 | /** | 664 | /** |
| 612 | * @tc.number : SetImportance_00100 | 665 | * @tc.number : SetImportance_00100 |
| 613 | * @tc.name : | 666 | * @tc.name : |
🤖 AI 代码检视意见(回复本评论可解决检视意见,点击被检视代码行左侧的小头像可收起检视意见)
🟡 使用sptr作为std::map的键存在设计脆弱性
位置:
L1285| 严重程度: Medium❓ 问题描述
bundleEnable使用sptr<NotificationBundleOption>作为std::map的键。这会导致 map 按照指针地址进行排序和查找,而不是按照业务逻辑(如 bundle name 和 uid)。当前逻辑能工作仅仅是因为传递的是相同的sptr实例,如果调用方重新构造了相同业务属性的对象,将无法在 map 中找到,这属于脆弱的设计模式。💡 修复建议
修改建议:如果必须使用对象作为键,应确保
NotificationBundleOption重载了operator<以进行业务级别的比较;或者改用字符串类型的 bundle key(如bundleName + uid)作为 map 的键。1285: bundleEnable[bundleOptions[it->second]] = enabled;