From 858b9d134252009536106e741d2e13a151043cc7 Mon Sep 17 00:00:00 2001 From: gaojiaqi Date: Mon, 16 Jun 2025 11:32:23 +0800 Subject: [PATCH 1/2] refactor event manager Signed-off-by: gaojiaqi --- services/reminder/BUILD.gn | 1 - .../reminder/include/reminder_data_manager.h | 1 - .../reminder/include/reminder_event_manager.h | 115 +++--- .../src/reminder_config_change_observer.cpp | 41 --- .../reminder/src/reminder_data_manager.cpp | 3 +- .../reminder/src/reminder_event_manager.cpp | 345 +++++++----------- services/reminder/test/unittest/BUILD.gn | 66 ++++ .../mock/include/mock_notification_helper.h | 2 + .../mock/mock_notification_helper.cpp | 31 +- .../mock/mock_reminder_data_manager.cpp | 52 +++ .../unittest/reminder_event_manager_test.cpp | 263 +++++++++++++ .../remindereventmanager_fuzzer/BUILD.gn | 66 ++++ .../remindereventmanager_fuzzer/corpus/init | 13 + .../remindereventmanager_fuzzer/project.xml | 25 ++ .../remindereventmanager_fuzzer.cpp | 241 ++++++++++++ .../remindereventmanager_fuzzer.h | 31 +- 16 files changed, 960 insertions(+), 336 deletions(-) delete mode 100644 services/reminder/src/reminder_config_change_observer.cpp create mode 100644 services/reminder/test/unittest/reminder_event_manager_test.cpp create mode 100644 test/fuzztest/remindereventmanager_fuzzer/BUILD.gn create mode 100644 test/fuzztest/remindereventmanager_fuzzer/corpus/init create mode 100644 test/fuzztest/remindereventmanager_fuzzer/project.xml create mode 100644 test/fuzztest/remindereventmanager_fuzzer/remindereventmanager_fuzzer.cpp rename services/reminder/include/reminder_config_change_observer.h => test/fuzztest/remindereventmanager_fuzzer/remindereventmanager_fuzzer.h (39%) diff --git a/services/reminder/BUILD.gn b/services/reminder/BUILD.gn index 182e3404d..146d6e1bf 100644 --- a/services/reminder/BUILD.gn +++ b/services/reminder/BUILD.gn @@ -54,7 +54,6 @@ ohos_source_set("reminder_service_sources") { "src/reminder_agent_service.cpp", "src/reminder_agent_service_ability.cpp", "src/reminder_bundle_manager_helper.cpp", - "src/reminder_config_change_observer.cpp", "src/reminder_data_manager.cpp", "src/reminder_data_manager_inner.cpp", "src/reminder_datashare_helper.cpp", diff --git a/services/reminder/include/reminder_data_manager.h b/services/reminder/include/reminder_data_manager.h index a16a21f4e..d68a2c15f 100644 --- a/services/reminder/include/reminder_data_manager.h +++ b/services/reminder/include/reminder_data_manager.h @@ -30,7 +30,6 @@ #include "reminder_request_adaptation.h" #include "reminder_store.h" #include "reminder_timer_info.h" -#include "reminder_config_change_observer.h" #include "datashare_predicates.h" #include "datashare_values_bucket.h" #include "app_mgr_interface.h" diff --git a/services/reminder/include/reminder_event_manager.h b/services/reminder/include/reminder_event_manager.h index 4f1cae627..040213846 100644 --- a/services/reminder/include/reminder_event_manager.h +++ b/services/reminder/include/reminder_event_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -17,85 +17,84 @@ #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_EVENT_MANAGER_H #include "common_event_subscriber.h" -#include "reminder_data_manager.h" -#include "system_ability_status_change_stub.h" #include "notification_subscriber.h" +#include "configuration_observer_stub.h" +#include "system_ability_status_change_stub.h" -#include - -namespace OHOS { -namespace Notification { +namespace OHOS::Notification { class ReminderEventManager { public: - explicit ReminderEventManager(std::shared_ptr &reminderDataManager); - virtual ~ReminderEventManager() {}; - ReminderEventManager(ReminderEventManager &other) = delete; - ReminderEventManager& operator = (const ReminderEventManager &other) = delete; + ReminderEventManager() = default; + ~ReminderEventManager() = default; + ReminderEventManager(ReminderEventManager& other) = delete; + ReminderEventManager& operator = (const ReminderEventManager& other) = delete; + +public: + void Init(); private: - void init(std::shared_ptr &reminderDataManager) const; - void SubscribeSystemAbility(std::shared_ptr &reminderDataManager) const; + void SubscribeEvent(); + void SubscribeSystemAbility(const int32_t systemAbilityId); class ReminderEventSubscriber : public EventFwk::CommonEventSubscriber { public: - ReminderEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscriberInfo, - std::shared_ptr &reminderDataManager); - virtual void OnReceiveEvent(const EventFwk::CommonEventData &data); - -private: - sptr GetBundleOption(const OHOS::EventFwk::Want &want) const; - int32_t GetUid(const OHOS::EventFwk::Want &want) const; - void HandlePackageRemove(const EventFwk::Want &want) const; - void HandleProcessDied(const EventFwk::Want &want) const; - std::shared_ptr reminderDataManager_ = nullptr; + ReminderEventSubscriber(const EventFwk::CommonEventSubscribeInfo& subscriberInfo); + void OnReceiveEvent(const EventFwk::CommonEventData& data) override; }; -class ReminderEventCustomSubscriber : public EventFwk::CommonEventSubscriber { +class ReminderCustomEventSubscriber : public EventFwk::CommonEventSubscriber { public: - ReminderEventCustomSubscriber(const EventFwk::CommonEventSubscribeInfo &subscriberInfo, - std::shared_ptr &reminderDataManager); - virtual void OnReceiveEvent(const EventFwk::CommonEventData &data); - -private: - std::shared_ptr reminderDataManager_ = nullptr; + ReminderCustomEventSubscriber(const EventFwk::CommonEventSubscribeInfo& subscriberInfo); + void OnReceiveEvent(const EventFwk::CommonEventData& data) override; }; -class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub { +class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub { public: - explicit SystemAbilityStatusChangeListener(std::shared_ptr &reminderDataManager); - ~SystemAbilityStatusChangeListener() {}; - virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; - virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; -private: - std::shared_ptr reminderDataManager_ = nullptr; + SystemAbilityStatusChangeListener() = default; + ~SystemAbilityStatusChangeListener() = default; + + void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; + void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; }; class ReminderNotificationSubscriber : public NotificationSubscriber { public: - explicit ReminderNotificationSubscriber(std::shared_ptr &reminderDataManager); - ~ReminderNotificationSubscriber() override; - void OnConnected() override; - void OnDisconnected() override; - void OnCanceled(const std::shared_ptr &request, - const std::shared_ptr &sortingMap, int deleteReason) override; - void OnConsumed(const std::shared_ptr &request, - const std::shared_ptr &sortingMap) override; - void OnUpdate(const std::shared_ptr &sortingMap) override; - void OnDied() override; + ReminderNotificationSubscriber() = default; + ~ReminderNotificationSubscriber() override = default; + + void OnConnected() override {} + void OnDisconnected() override {} + void OnCanceled(const std::shared_ptr& request, + const std::shared_ptr& sortingMap, int deleteReason) override; + void OnConsumed(const std::shared_ptr& request, + const std::shared_ptr& sortingMap) override {} + void OnUpdate(const std::shared_ptr& sortingMap) override {} + void OnDied() override {} void OnDoNotDisturbDateChange( - const std::shared_ptr &date) override; + const std::shared_ptr& date) override {} void OnEnabledNotificationChanged( - const std::shared_ptr &callbackData) override; - void OnBadgeChanged(const std::shared_ptr &badgeData) override; - void OnBadgeEnabledChanged(const sptr &callbackData) override; - void OnBatchCanceled(const std::vector> &requestList, - const std::shared_ptr &sortingMap, int32_t deleteReason) override; -private: - std::shared_ptr reminderDataManager_ = nullptr; + const std::shared_ptr& callbackData) override {} + void OnBadgeChanged(const std::shared_ptr& badgeData) override {} + void OnBadgeEnabledChanged(const sptr& callbackData) override {} + void OnBatchCanceled(const std::vector>& requestList, + const std::shared_ptr& sortingMap, int32_t deleteReason) override {} }; +}; + +/** + * @brief Listening system language change, when the system language changes, + * notify ReminderDataManager. + */ +class ReminderConfigChangeObserver final : public AppExecFwk::ConfigurationObserverStub { +public: + ReminderConfigChangeObserver() = default; + ~ReminderConfigChangeObserver() = default; - static std::shared_ptr subscriber_; +public: + void OnConfigurationUpdated(const AppExecFwk::Configuration& configuration) override; + +private: + std::string languageInfo_; }; -} // namespace OHOS -} // namespace Notification +} // namespace OHOS::Notification #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_EVENT_MANAGER_H diff --git a/services/reminder/src/reminder_config_change_observer.cpp b/services/reminder/src/reminder_config_change_observer.cpp deleted file mode 100644 index a19712fb0..000000000 --- a/services/reminder/src/reminder_config_change_observer.cpp +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "reminder_config_change_observer.h" - -#include "ans_log_wrapper.h" -#include "reminder_data_manager.h" - -namespace OHOS { -namespace Notification { - -void ReminderConfigChangeObserver::OnConfigurationUpdated(const AppExecFwk::Configuration &configuration) -{ - ANSR_LOGD("OnConfigurationUpdated."); - auto reminderDataMgr = ReminderDataManager::GetInstance(); - if (reminderDataMgr == nullptr) { - ANSR_LOGE("Reminder data manager is nullptr"); - return; - } - std::string newLanguageInfo = configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE); - if (!newLanguageInfo.empty() && newLanguageInfo != languageInfo_) { - ANSR_LOGD("language change: %{public}s -> %{public}s", languageInfo_.c_str(), newLanguageInfo.c_str()); - reminderDataMgr->OnLanguageChanged(); - languageInfo_ = newLanguageInfo; - } -} - -} // namespace Notification -} // namespace OHOS diff --git a/services/reminder/src/reminder_data_manager.cpp b/services/reminder/src/reminder_data_manager.cpp index aa3498344..59f5a7459 100644 --- a/services/reminder/src/reminder_data_manager.cpp +++ b/services/reminder/src/reminder_data_manager.cpp @@ -621,7 +621,8 @@ std::shared_ptr ReminderDataManager::InitInstance() if (REMINDER_DATA_MANAGER == nullptr) { REMINDER_DATA_MANAGER = std::make_shared(); REMINDER_DATA_MANAGER->Init(); - ReminderEventManager reminderEventManager(REMINDER_DATA_MANAGER); + ReminderEventManager reminderEventManager; + reminderEventManager.Init(); } return REMINDER_DATA_MANAGER; } diff --git a/services/reminder/src/reminder_event_manager.cpp b/services/reminder/src/reminder_event_manager.cpp index ec486dc45..8d54b9091 100644 --- a/services/reminder/src/reminder_event_manager.cpp +++ b/services/reminder/src/reminder_event_manager.cpp @@ -16,34 +16,30 @@ #include "reminder_event_manager.h" #include "ans_log_wrapper.h" -#include "bundle_constants.h" -#include "bundle_mgr_interface.h" -#include "common_event_manager.h" -#include "common_event_support.h" +#include "reminder_data_manager.h" #include "reminder_bundle_manager_helper.h" -#include "if_system_ability_manager.h" + #include "ipc_skeleton.h" -#include "iservice_registry.h" -#include "system_ability_definition.h" #include "notification_helper.h" -#include "string_ex.h" +#include "common_event_manager.h" +#include "common_event_support.h" +#include "system_ability_definition.h" +#include "if_system_ability_manager.h" -using namespace OHOS::EventFwk; -namespace OHOS { -namespace Notification { -static const std::string NOTIFICATION_LABEL = "REMINDER_AGENT"; +namespace OHOS::Notification { static constexpr const char* UNLOCK_SCREEN_EVENT = "common.event.UNLOCK_SCREEN"; -std::shared_ptr ReminderEventManager::subscriber_ - = nullptr; -ReminderEventManager::ReminderEventManager(std::shared_ptr &reminderDataManager) +void ReminderEventManager::Init() { - init(reminderDataManager); + SubscribeEvent(); + SubscribeSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + SubscribeSystemAbility(APP_MGR_SERVICE_ID); + SubscribeSystemAbility(ABILITY_MGR_SERVICE_ID); } -void ReminderEventManager::init(std::shared_ptr &reminderDataManager) const +void ReminderEventManager::SubscribeEvent() { - MatchingSkills customMatchingSkills; + EventFwk::MatchingSkills customMatchingSkills; customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_ALARM_ALERT); customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT); customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CLOSE_ALERT); @@ -51,223 +47,177 @@ void ReminderEventManager::init(std::shared_ptr &reminderDa customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION); customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CUSTOM_ALERT); customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CLICK_ALERT); - CommonEventSubscribeInfo customSubscriberInfo(customMatchingSkills); + EventFwk::CommonEventSubscribeInfo customSubscriberInfo(customMatchingSkills); customSubscriberInfo.SetPermission("ohos.permission.GRANT_SENSITIVE_PERMISSIONS"); customSubscriberInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON); - auto customSubscriber = std::make_shared(customSubscriberInfo, reminderDataManager); - - MatchingSkills matchingSkills; - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIME_CHANGED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED); - CommonEventSubscribeInfo subscriberInfo(matchingSkills); + auto customSubscriber = std::make_shared(customSubscriberInfo); + + EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_TIME_CHANGED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED); + EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); subscriberInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON); - auto subscriber = std::make_shared(subscriberInfo, reminderDataManager); + auto subscriber = std::make_shared(subscriberInfo); - MatchingSkills screenMatchingSkills; + EventFwk::MatchingSkills screenMatchingSkills; screenMatchingSkills.AddEvent(UNLOCK_SCREEN_EVENT); - CommonEventSubscribeInfo screenSubscriberInfo(screenMatchingSkills); + EventFwk::CommonEventSubscribeInfo screenSubscriberInfo(screenMatchingSkills); screenSubscriberInfo.SetPublisherBundleName(AppExecFwk::Constants::SCENE_BOARD_BUNDLE_NAME); - auto screenSubscriber = std::make_shared(screenSubscriberInfo, reminderDataManager); + auto screenSubscriber = std::make_shared(screenSubscriberInfo); std::string identity = IPCSkeleton::ResetCallingIdentity(); - if (CommonEventManager::SubscribeCommonEvent(subscriber) && - CommonEventManager::SubscribeCommonEvent(customSubscriber) && - CommonEventManager::SubscribeCommonEvent(screenSubscriber)) { - ANSR_LOGD("SubscribeCommonEvent ok"); + if (EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber) && + EventFwk::CommonEventManager::SubscribeCommonEvent(customSubscriber) && + EventFwk::CommonEventManager::SubscribeCommonEvent(screenSubscriber)) { + ANSR_LOGD("SubscribeCommonEvent ok."); } else { - ANSR_LOGD("SubscribeCommonEvent fail"); + ANSR_LOGW("SubscribeCommonEvent failed."); } IPCSkeleton::SetCallingIdentity(identity); - subscriber_ = std::make_shared(reminderDataManager); - if (NotificationHelper::SubscribeNotification(*subscriber_) != ERR_OK) { - ANSR_LOGD("SubscribeNotification failed"); + auto notificationSubscriber = std::make_shared(); + if (NotificationHelper::SubscribeNotification(*notificationSubscriber) != ERR_OK) { + ANSR_LOGW("SubscribeNotification failed."); } - - SubscribeSystemAbility(reminderDataManager); } -void ReminderEventManager::SubscribeSystemAbility(std::shared_ptr &reminderDataManager) const +void ReminderEventManager::SubscribeSystemAbility(const int32_t systemAbilityId) { sptr statusChangeListener - = new (std::nothrow) SystemAbilityStatusChangeListener(reminderDataManager); + = new (std::nothrow) SystemAbilityStatusChangeListener(); if (statusChangeListener == nullptr) { - ANSR_LOGE("Failed to create statusChangeListener due to no memory."); + ANSR_LOGW("Failed to create SystemAbilityStatusChangeListener."); return; } - // app mgr - sptr appMgrStatusChangeListener - = new (std::nothrow) SystemAbilityStatusChangeListener(reminderDataManager); - if (appMgrStatusChangeListener == nullptr) { - ANSR_LOGE("Failed to create appMgrStatusChangeListener due to no memory."); - return; - } - // ability mgr - sptr abilityMgrStatusListener - = new (std::nothrow) SystemAbilityStatusChangeListener(reminderDataManager); - if (abilityMgrStatusListener == nullptr) { - ANSR_LOGE("Failed to create abilityMgrStatusListener due to no memory."); - return; - } - sptr samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (samgrProxy == nullptr) { - ANSR_LOGD("samgrProxy is null"); + ANSR_LOGW("GetSystemAbilityManager failed."); return; } - int32_t ret = samgrProxy->SubscribeSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, statusChangeListener); + int32_t ret = samgrProxy->SubscribeSystemAbility(systemAbilityId, statusChangeListener); if (ret != ERR_OK) { - ANSR_LOGE("subscribe system ability id: %{public}d failed", BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - } - ret = samgrProxy->SubscribeSystemAbility(APP_MGR_SERVICE_ID, appMgrStatusChangeListener); - if (ret != ERR_OK) { - ANSR_LOGE("subscribe system ability id: %{public}d failed", APP_MGR_SERVICE_ID); - } - ret = samgrProxy->SubscribeSystemAbility(ABILITY_MGR_SERVICE_ID, abilityMgrStatusListener); - if (ret != ERR_OK) { - ANSR_LOGE("subscribe system ability id: %{public}d failed", ABILITY_MGR_SERVICE_ID); + ANSR_LOGW("SubscribeSystemAbility id: %{public}d failed.", systemAbilityId); } } ReminderEventManager::ReminderEventSubscriber::ReminderEventSubscriber( - const CommonEventSubscribeInfo &subscriberInfo, - std::shared_ptr &reminderDataManager) : CommonEventSubscriber(subscriberInfo) -{ - reminderDataManager_ = reminderDataManager; -} + const EventFwk::CommonEventSubscribeInfo& subscriberInfo) : CommonEventSubscriber(subscriberInfo) +{} -ReminderEventManager::ReminderEventCustomSubscriber::ReminderEventCustomSubscriber( - const CommonEventSubscribeInfo &subscriberInfo, - std::shared_ptr &reminderDataManager) : CommonEventSubscriber(subscriberInfo) +void ReminderEventManager::ReminderEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData& data) { - reminderDataManager_ = reminderDataManager; -} - -void ReminderEventManager::ReminderEventCustomSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data) -{ - Want want = data.GetWant(); + auto manager = ReminderDataManager::GetInstance(); + if (manager == nullptr) { + return; + } + EventFwk::Want want = data.GetWant(); std::string action = want.GetAction(); - ANSR_LOGI("Recieved common event:%{public}s", action.c_str()); - if (action == ReminderRequest::REMINDER_EVENT_ALARM_ALERT) { - reminderDataManager_->ShowActiveReminder(want); + ANSR_LOGD("Recieved common event:%{public}s.", action.c_str()); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED || + action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) { + AppExecFwk::ElementName ele = want.GetElement(); + std::string bundleName = ele.GetBundleName(); + int32_t userId = want.GetIntParam(AppExecFwk::Constants::USER_ID, -1); + int32_t uid = want.GetIntParam(AppExecFwk::Constants::UID, -1); + manager->CancelAllReminders(bundleName, userId, uid); return; } - if (action == ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT) { - reminderDataManager_->TerminateAlerting(want); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED) { + AppExecFwk::ElementName ele = want.GetElement(); + std::string bundleName = ele.GetBundleName(); + int32_t userId = want.GetIntParam(AppExecFwk::Constants::USER_ID, -1); + int32_t uid = ReminderBundleManagerHelper::GetInstance().GetDefaultUidByBundleName(bundleName, userId); + manager->OnProcessDiedLocked(uid); return; } - if (action == ReminderRequest::REMINDER_EVENT_CLOSE_ALERT) { - reminderDataManager_->CloseReminder(want, true); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED) { + manager->RefreshRemindersDueToSysTimeChange(ReminderDataManager::TIME_ZONE_CHANGE); return; } - if (action == ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT) { - reminderDataManager_->SnoozeReminder(want); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_TIME_CHANGED) { + manager->RefreshRemindersDueToSysTimeChange(ReminderDataManager::DATE_TIME_CHANGE); return; } - if (action == ReminderRequest::REMINDER_EVENT_CUSTOM_ALERT) { - reminderDataManager_->HandleCustomButtonClick(want); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { + manager->OnUserSwitch(data.GetCode()); return; } - if (action == ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION) { - reminderDataManager_->CloseReminder(want, false, false); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) { + manager->OnUserRemove(data.GetCode()); return; } - if (action == ReminderRequest::REMINDER_EVENT_CLICK_ALERT) { - reminderDataManager_->ClickReminder(want); + if (action.compare(UNLOCK_SCREEN_EVENT) == 0) { + manager->OnUnlockScreen(); return; } } -void ReminderEventManager::ReminderEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data) +ReminderEventManager::ReminderCustomEventSubscriber::ReminderCustomEventSubscriber( + const EventFwk::CommonEventSubscribeInfo& subscriberInfo) : CommonEventSubscriber(subscriberInfo) +{} + +void ReminderEventManager::ReminderCustomEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData& data) { - Want want = data.GetWant(); - std::string action = want.GetAction(); - ANSR_LOGD("Recieved common event:%{public}s", action.c_str()); - if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) { - HandlePackageRemove(want); + auto manager = ReminderDataManager::GetInstance(); + if (manager == nullptr) { return; } - if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) { - HandlePackageRemove(want); + EventFwk::Want want = data.GetWant(); + std::string action = want.GetAction(); + ANSR_LOGI("Recieved common event:%{public}s.", action.c_str()); + if (action == ReminderRequest::REMINDER_EVENT_ALARM_ALERT) { + manager->ShowActiveReminder(want); return; } - if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED) { - HandleProcessDied(want); + if (action == ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT) { + manager->TerminateAlerting(want); return; } - if (action == CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED) { - reminderDataManager_->RefreshRemindersDueToSysTimeChange(ReminderDataManager::TIME_ZONE_CHANGE); + if (action == ReminderRequest::REMINDER_EVENT_CLOSE_ALERT) { + manager->CloseReminder(want, true); return; } - if (action == CommonEventSupport::COMMON_EVENT_TIME_CHANGED) { - reminderDataManager_->RefreshRemindersDueToSysTimeChange(ReminderDataManager::DATE_TIME_CHANGE); + if (action == ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT) { + manager->SnoozeReminder(want); return; } - if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { - reminderDataManager_->OnUserSwitch(data.GetCode()); + if (action == ReminderRequest::REMINDER_EVENT_CUSTOM_ALERT) { + manager->HandleCustomButtonClick(want); return; } - if (action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) { - reminderDataManager_->OnUserRemove(data.GetCode()); + if (action == ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION) { + manager->CloseReminder(want, false, false); return; } - if (action.compare(UNLOCK_SCREEN_EVENT) == 0) { - reminderDataManager_->OnUnlockScreen(); + if (action == ReminderRequest::REMINDER_EVENT_CLICK_ALERT) { + manager->ClickReminder(want); + return; } } -void ReminderEventManager::ReminderEventSubscriber::HandlePackageRemove(const EventFwk::Want &want) const -{ - OHOS::AppExecFwk::ElementName ele = want.GetElement(); - std::string bundleName = ele.GetBundleName(); - int32_t userId = want.GetIntParam(OHOS::AppExecFwk::Constants::USER_ID, -1); - int32_t uid = want.GetIntParam(OHOS::AppExecFwk::Constants::UID, -1); - reminderDataManager_->CancelAllReminders(bundleName, userId, uid); -} - -void ReminderEventManager::ReminderEventSubscriber::HandleProcessDied(const EventFwk::Want &want) const -{ - int32_t uid = GetUid(want); - reminderDataManager_->OnProcessDiedLocked(uid); -} - -int32_t ReminderEventManager::ReminderEventSubscriber::GetUid( - const OHOS::EventFwk::Want &want) const -{ - OHOS::AppExecFwk::ElementName ele = want.GetElement(); - std::string bundleName = ele.GetBundleName(); - int32_t userId = want.GetIntParam(OHOS::AppExecFwk::Constants::USER_ID, -1); - int32_t uid = ReminderBundleManagerHelper::GetInstance().GetDefaultUidByBundleName(bundleName, userId); - ANSR_LOGD("bundleName=%{public}s, userId=%{private}d, uid=%{public}d", bundleName.c_str(), userId, uid); - return uid; -} - -ReminderEventManager::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener( - std::shared_ptr &reminderDataManager) -{ - reminderDataManager_ = reminderDataManager; -} - void ReminderEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility( int32_t systemAbilityId, const std::string& deviceId) { - ANSR_LOGD("OnAddSystemAbilityInner"); + auto manager = ReminderDataManager::GetInstance(); + if (manager == nullptr) { + return; + } switch (systemAbilityId) { case BUNDLE_MGR_SERVICE_SYS_ABILITY_ID: - ANSR_LOGD("OnAddSystemAbilityInner: BUNDLE_MGR_SERVICE_SYS_ABILITY"); - reminderDataManager_->OnBundleMgrServiceStart(); + ANSR_LOGD("AddSystemAbility: BUNDLE_MGR_SERVICE_SYS_ABILITY_ID."); + manager->OnBundleMgrServiceStart(); break; case APP_MGR_SERVICE_ID: - ANSR_LOGD("OnAddSystemAbilityInner: APP_MGR_SERVICE"); + ANSR_LOGD("AddSystemAbility: APP_MGR_SERVICE_ID."); break; case ABILITY_MGR_SERVICE_ID: - ANSR_LOGD("OnAddSystemAbilityInner ABILITY_MGR_SERVICE_ID"); - reminderDataManager_->OnAbilityMgrServiceStart(); + ANSR_LOGD("AddSystemAbility: ABILITY_MGR_SERVICE_ID."); + manager->OnAbilityMgrServiceStart(); break; default: break; @@ -277,38 +227,29 @@ void ReminderEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility void ReminderEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility( int32_t systemAbilityId, const std::string& deviceId) { - ANSR_LOGD("OnRemoveSystemAbilityInner"); + auto manager = ReminderDataManager::GetInstance(); + if (manager == nullptr) { + return; + } switch (systemAbilityId) { case BUNDLE_MGR_SERVICE_SYS_ABILITY_ID: - ANSR_LOGD("OnRemoveSystemAbilityInner: BUNDLE_MGR_SERVICE_SYS_ABILITY"); + ANSR_LOGD("RemoveSystemAbility: BUNDLE_MGR_SERVICE_SYS_ABILITY_ID."); break; case APP_MGR_SERVICE_ID: - ANSR_LOGD("OnRemoveSystemAbilityInner: APP_MGR_SERVICE"); - reminderDataManager_->OnRemoveAppMgr(); + ANSR_LOGD("RemoveSystemAbility: APP_MGR_SERVICE_ID."); + manager->OnRemoveAppMgr(); break; case ABILITY_MGR_SERVICE_ID: - ANSR_LOGD("OnRemoveSystemAbilityInner ABILITY_MGR_SERVICE_ID"); + ANSR_LOGD("RemoveSystemAbility: ABILITY_MGR_SERVICE_ID."); break; default: break; } } -ReminderEventManager::ReminderNotificationSubscriber::ReminderNotificationSubscriber( - std::shared_ptr &reminderDataManager) -{ - reminderDataManager_ = reminderDataManager; -} - -ReminderEventManager::ReminderNotificationSubscriber::~ReminderNotificationSubscriber() {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnConnected() {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnDisconnected() {} - void ReminderEventManager::ReminderNotificationSubscriber::OnCanceled( - const std::shared_ptr ¬ification, - const std::shared_ptr &sortingMap, int deleteReason) + const std::shared_ptr& notification, + const std::shared_ptr& sortingMap, int deleteReason) { // Note: Don't modify param notification if (deleteReason != NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE) { @@ -320,40 +261,30 @@ void ReminderEventManager::ReminderNotificationSubscriber::OnCanceled( NotificationRequest request = notification->GetNotificationRequest(); std::string label = request.GetLabel(); int64_t autoDeletedTime = request.GetAutoDeletedTime(); - if (autoDeletedTime <= 0 || label != NOTIFICATION_LABEL) { + if (autoDeletedTime <= 0 || label.compare(ReminderRequest::NOTIFICATION_LABEL) != 0) { return; } - if (reminderDataManager_ == nullptr) { + auto manager = ReminderDataManager::GetInstance(); + if (manager == nullptr) { return; } int32_t notificationId = request.GetNotificationId(); int32_t uid = request.GetOwnerUid() == 0 ? request.GetCreatorUid() : request.GetOwnerUid(); - reminderDataManager_->HandleAutoDeleteReminder(notificationId, uid, autoDeletedTime); + manager->HandleAutoDeleteReminder(notificationId, uid, autoDeletedTime); } -void ReminderEventManager::ReminderNotificationSubscriber::OnConsumed(const std::shared_ptr ¬ification, - const std::shared_ptr &sortingMap) {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnUpdate( - const std::shared_ptr &sortingMap) {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnDied() {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnDoNotDisturbDateChange( - const std::shared_ptr &date) {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnEnabledNotificationChanged( - const std::shared_ptr &callbackData) {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnBadgeChanged( - const std::shared_ptr &badgeData) {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnBadgeEnabledChanged( - const sptr &callbackData) {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnBatchCanceled( - const std::vector> &requestList, - const std::shared_ptr &sortingMap, int32_t deleteReason) {} -} // namespace OHOS -} // namespace Notification +void ReminderConfigChangeObserver::OnConfigurationUpdated(const AppExecFwk::Configuration& configuration) +{ + auto manager = ReminderDataManager::GetInstance(); + if (manager == nullptr) { + return; + } + std::string newLanguageInfo = configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE); + if (!newLanguageInfo.empty() && newLanguageInfo != languageInfo_) { + ANSR_LOGD("Language change: %{public}s -> %{public}s.", languageInfo_.c_str(), newLanguageInfo.c_str()); + manager->OnLanguageChanged(); + languageInfo_ = newLanguageInfo; + } +} +} // namespace OHOS::Notification diff --git a/services/reminder/test/unittest/BUILD.gn b/services/reminder/test/unittest/BUILD.gn index bf5ca1c19..dd4ea247d 100644 --- a/services/reminder/test/unittest/BUILD.gn +++ b/services/reminder/test/unittest/BUILD.gn @@ -402,12 +402,78 @@ ohos_unittest("reminder_agent_service_ability_test") { part_name = "${component_name}" } +ohos_unittest("reminder_event_manager_test") { + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + module_out_path = module_output_path + include_dirs = [ + ".", + "include", + "/${services_path}/reminder/include", + "${services_path}/reminder/test/unittest/mock/include", + ] + sources = [ + "${services_path}/reminder/src/reminder_event_manager.cpp", + "mock/mock_notification_helper.cpp", + "mock/mock_reminder_bundle_manager_helper.cpp", + "mock/mock_reminder_data_manager.cpp", + "reminder_event_manager_test.cpp", + ] + deps = [ + "${frameworks_module_ans_path}:ans_innerkits", + "${frameworks_module_reminder_path}:reminder_innerkits", + "${services_path}/reminder:libreminder", + ] + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:abilitykit_native", + "ability_runtime:app_manager", + "ability_runtime:wantagent_innerkits", + "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "data_share:datashare_common", + "eventhandler:libeventhandler", + "ffrt:libffrt", + "hilog:libhilog", + "hitrace:hitrace_meter", + "hitrace:libhitracechain", + "image_framework:image_native", + "init:libbegetutil", + "ipc:ipc_core", + "kv_store:distributeddata_inner", + "os_account:os_account_innerkits", + "relational_store:native_rdb", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "time_service:time_client", + ] + + cflags = [ + "-Dprivate=public", + "-Dprotected=public", + ] + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} + group("unittest") { testonly = true deps = [ ":reminder_agent_service_ability_test", ":reminder_agent_service_test", ":reminder_bundle_manager_helper_test", + ":reminder_event_manager_test", ":reminder_service_test", ":reminder_unit_test", ] diff --git a/services/reminder/test/unittest/mock/include/mock_notification_helper.h b/services/reminder/test/unittest/mock/include/mock_notification_helper.h index 802fe8c38..bb1e992d1 100644 --- a/services/reminder/test/unittest/mock/include/mock_notification_helper.h +++ b/services/reminder/test/unittest/mock/include/mock_notification_helper.h @@ -23,6 +23,8 @@ class MockNotificationHelper { public: static void MockIsAllowUseReminder(const bool isAllowUseReminder); static void MockIsAllowedNotify(const bool isAllowedNotify, const int32_t ret); + static void MockSubscribeNotification(const int32_t ret); + static void MockSubscribeCommonEvent(const bool ret); }; } // namespace OHOS::Notification diff --git a/services/reminder/test/unittest/mock/mock_notification_helper.cpp b/services/reminder/test/unittest/mock/mock_notification_helper.cpp index 3058d8f26..754fcc297 100644 --- a/services/reminder/test/unittest/mock/mock_notification_helper.cpp +++ b/services/reminder/test/unittest/mock/mock_notification_helper.cpp @@ -16,14 +16,18 @@ #include "mock_notification_helper.h" #include "notification_helper.h" +#include "common_event_manager.h" -namespace OHOS::Notification { +namespace OHOS { namespace { bool g_mockIsAllowUseReminder = true; bool g_mockIsAllowedNotify = true; +bool g_mockSubscribeCommonEventRet = true; int32_t g_mockIsAllowedNotifyRet = 0; +int32_t g_mockSubscribeNotificationRet = 0; } +namespace Notification { void MockNotificationHelper::MockIsAllowUseReminder(const bool isAllowUseReminder) { g_mockIsAllowUseReminder = isAllowUseReminder; @@ -35,6 +39,16 @@ void MockNotificationHelper::MockIsAllowedNotify(const bool isAllowedNotify, con g_mockIsAllowedNotifyRet = ret; } +void MockNotificationHelper::MockSubscribeNotification(const int32_t ret) +{ + g_mockSubscribeNotificationRet = ret; +} + +void MockNotificationHelper::MockSubscribeCommonEvent(const bool ret) +{ + g_mockSubscribeCommonEventRet = ret; +} + ErrCode NotificationHelper::AllowUseReminder(const std::string& bundleName, bool& isAllowUseReminder) { isAllowUseReminder = g_mockIsAllowUseReminder; @@ -46,4 +60,17 @@ ErrCode NotificationHelper::IsAllowedNotify(const NotificationBundleOption& bund allowed = g_mockIsAllowedNotify; return g_mockIsAllowedNotifyRet; } -} // namespace OHOS::Notification \ No newline at end of file + +ErrCode NotificationHelper::SubscribeNotification(const NotificationSubscriber& subscriber) +{ + return g_mockSubscribeNotificationRet; +} +} + +namespace EventFwk { +bool CommonEventManager::SubscribeCommonEvent(const std::shared_ptr& subscriber) +{ + return g_mockSubscribeCommonEventRet; +} +} +} // namespace OHOS \ No newline at end of file diff --git a/services/reminder/test/unittest/mock/mock_reminder_data_manager.cpp b/services/reminder/test/unittest/mock/mock_reminder_data_manager.cpp index 490b4c737..186f2fdce 100644 --- a/services/reminder/test/unittest/mock/mock_reminder_data_manager.cpp +++ b/services/reminder/test/unittest/mock/mock_reminder_data_manager.cpp @@ -137,4 +137,56 @@ int32_t ReminderDataManager::QueryActiveReminderCount() { return g_mockQueryActiveReminderCountRet; } + +void ReminderDataManager::OnProcessDiedLocked(const int32_t callingUid) +{} + +void ReminderDataManager::RefreshRemindersDueToSysTimeChange(uint8_t type) +{} + +void ReminderDataManager::OnUserSwitch(const int32_t& userId) +{} + +void ReminderDataManager::OnUserRemove(const int32_t& userId) +{} + +void ReminderDataManager::OnUnlockScreen() +{} + +void ReminderDataManager::ShowActiveReminder(const EventFwk::Want& want) +{} + +void ReminderDataManager::TerminateAlerting(const EventFwk::Want& want) +{} + +void ReminderDataManager::CloseReminder(const sptr& reminder, bool cancelNotification) +{} + +void ReminderDataManager::SnoozeReminder(const EventFwk::Want& want) +{} + +void ReminderDataManager::HandleCustomButtonClick(const EventFwk::Want& want) +{} + +void ReminderDataManager::CloseReminder(const EventFwk::Want& want, bool cancelNotification, bool isButtonClick) +{} + +void ReminderDataManager::ClickReminder(const EventFwk::Want& want) +{} + +void ReminderDataManager::OnBundleMgrServiceStart() +{} + +void ReminderDataManager::OnAbilityMgrServiceStart() +{} + +void ReminderDataManager::OnRemoveAppMgr() +{} + +void ReminderDataManager::HandleAutoDeleteReminder(const int32_t notificationId, const int32_t uid, + const int64_t autoDeletedTime) +{} + +void ReminderDataManager::OnLanguageChanged() +{} } // namespace OHOS::Notification \ No newline at end of file diff --git a/services/reminder/test/unittest/reminder_event_manager_test.cpp b/services/reminder/test/unittest/reminder_event_manager_test.cpp new file mode 100644 index 000000000..c57a61990 --- /dev/null +++ b/services/reminder/test/unittest/reminder_event_manager_test.cpp @@ -0,0 +1,263 @@ +/* + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "reminder_data_manager.h" +#include "reminder_event_manager.h" + +#include "common_event_support.h" +#include "mock_notification_helper.h" +#include "mock_reminder_data_manager.h" + +using namespace testing::ext; +namespace OHOS::Notification { +class ReminderEventManagerTest : public testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + void SetUp() {} + void TearDown() {} +}; + +/** + * @tc.name: ReminderEventManagerTest_001 + * @tc.desc: test ReminderEventManager::Init function + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_001, Level1) +{ + ReminderDataManager::InitInstance(); + auto manager = std::make_shared(); + manager->Init(); + EXPECT_TRUE(manager != nullptr); +} + +/** + * @tc.name: ReminderEventManagerTest_002 + * @tc.desc: test ReminderEventManager::SubscribeEvent function + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_002, Level1) +{ + auto manager = std::make_shared(); + MockNotificationHelper::MockSubscribeCommonEvent(false); + manager->SubscribeEvent(); + MockNotificationHelper::MockSubscribeCommonEvent(true); + manager->SubscribeEvent(); + MockNotificationHelper::MockSubscribeNotification(-1); + manager->SubscribeEvent(); + MockNotificationHelper::MockSubscribeNotification(ERR_OK); + manager->SubscribeEvent(); + EXPECT_TRUE(manager != nullptr); +} + +/** + * @tc.name: ReminderEventManagerTest_003 + * @tc.desc: test ReminderEventManager::SubscribeSystemAbility function + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_003, Level1) +{ + auto manager = std::make_shared(); + manager->SubscribeSystemAbility(APP_MGR_SERVICE_ID); + EXPECT_TRUE(manager != nullptr); +} + +/** + * @tc.name: ReminderEventManagerTest_004 + * @tc.desc: test ReminderEventSubscriber::OnReceiveEvent function + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_004, Level1) +{ + EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED); + EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); + auto subscriber = std::make_shared(subscriberInfo); + + EventFwk::CommonEventData data; + EventFwk::Want want; + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED); + data.SetWant(want); + auto manager = std::move(ReminderDataManager::REMINDER_DATA_MANAGER); + subscriber->OnReceiveEvent(data); + + ReminderDataManager::REMINDER_DATA_MANAGER = std::move(manager); + subscriber->OnReceiveEvent(data); + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_TIME_CHANGED); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + want.SetAction("common.event.UNLOCK_SCREEN"); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + want.SetAction(ReminderRequest::REMINDER_EVENT_ALARM_ALERT); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(subscriber != nullptr); +} + +/** + * @tc.name: ReminderEventManagerTest_005 + * @tc.desc: test ReminderCustomEventSubscriber::OnReceiveEvent function + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_005, Level1) +{ + EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_ALARM_ALERT); + EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); + auto subscriber = std::make_shared(subscriberInfo); + + EventFwk::CommonEventData data; + EventFwk::Want want; + want.SetAction(ReminderRequest::REMINDER_EVENT_ALARM_ALERT); + data.SetWant(want); + auto manager = std::move(ReminderDataManager::REMINDER_DATA_MANAGER); + subscriber->OnReceiveEvent(data); + + ReminderDataManager::REMINDER_DATA_MANAGER = std::move(manager); + subscriber->OnReceiveEvent(data); + want.SetAction(ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + want.SetAction(ReminderRequest::REMINDER_EVENT_CLOSE_ALERT); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + want.SetAction(ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + want.SetAction(ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + want.SetAction(ReminderRequest::REMINDER_EVENT_CUSTOM_ALERT); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + want.SetAction(ReminderRequest::REMINDER_EVENT_CLICK_ALERT); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(subscriber != nullptr); +} + +/** + * @tc.name: ReminderEventManagerTest_006 + * @tc.desc: test SystemAbilityStatusChangeListener + * @tc.type: FUNC + * @tc.require: issue#I9IIDE + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_006, Level1) +{ + auto listener = std::make_shared(); + // test OnAddSystemAbility + auto manager = std::move(ReminderDataManager::REMINDER_DATA_MANAGER); + listener->OnAddSystemAbility(-1, ""); + ReminderDataManager::REMINDER_DATA_MANAGER = std::move(manager); + listener->OnAddSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, ""); + listener->OnAddSystemAbility(APP_MGR_SERVICE_ID, ""); + listener->OnAddSystemAbility(ABILITY_MGR_SERVICE_ID, ""); + listener->OnAddSystemAbility(-1, ""); + + // test OnRemoveSystemAbility + manager = std::move(ReminderDataManager::REMINDER_DATA_MANAGER); + listener->OnRemoveSystemAbility(-1, ""); + ReminderDataManager::REMINDER_DATA_MANAGER = std::move(manager); + listener->OnRemoveSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, ""); + listener->OnRemoveSystemAbility(APP_MGR_SERVICE_ID, ""); + listener->OnRemoveSystemAbility(ABILITY_MGR_SERVICE_ID, ""); + listener->OnRemoveSystemAbility(-1, ""); + EXPECT_TRUE(listener != nullptr); +} + +/** + * @tc.name: ReminderEventManagerTest_007 + * @tc.desc: test ReminderNotificationSubscriber::OnCanceled test + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_007, Level1) +{ + ReminderEventManager::ReminderNotificationSubscriber subscriber; + // test deleteReason + subscriber.OnCanceled(nullptr, nullptr, NotificationConstant::PACKAGE_REMOVE_REASON_DELETE); + // test notification + subscriber.OnCanceled(nullptr, nullptr, NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE); + // test autoDeletedTime + sptr request = new NotificationRequest(); + std::shared_ptr notification = std::make_shared(request); + subscriber.OnCanceled(notification, nullptr, NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE); + request->SetAutoDeletedTime(100); + // test label + subscriber.OnCanceled(notification, nullptr, NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE); + request->SetLabel("REMINDER_AGENT"); + // test manager + auto manager = std::move(ReminderDataManager::REMINDER_DATA_MANAGER); + subscriber.OnCanceled(notification, nullptr, NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE); + ReminderDataManager::REMINDER_DATA_MANAGER = std::move(manager); + subscriber.OnCanceled(notification, nullptr, NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE); + EXPECT_TRUE(manager == nullptr); +} + +/** + * @tc.name: ReminderEventManagerTest_008 + * @tc.desc: test ReminderConfigChangeObserver::OnConfigurationUpdated function + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_008, Level1) +{ + ReminderConfigChangeObserver observer; + AppExecFwk::Configuration config; + // test manager == nullptr + auto manager = std::move(ReminderDataManager::REMINDER_DATA_MANAGER); + observer.OnConfigurationUpdated(config); + ReminderDataManager::REMINDER_DATA_MANAGER = std::move(manager); + // test languageInfo + config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, ""); + observer.OnConfigurationUpdated(config); + config.RemoveItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE); + config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, "test"); + observer.languageInfo_ = "test"; + observer.OnConfigurationUpdated(config); + observer.languageInfo_ = "1111"; + observer.OnConfigurationUpdated(config); + EXPECT_TRUE(manager == nullptr); +} +} // namespace OHOS::Notification \ No newline at end of file diff --git a/test/fuzztest/remindereventmanager_fuzzer/BUILD.gn b/test/fuzztest/remindereventmanager_fuzzer/BUILD.gn new file mode 100644 index 000000000..e70423a59 --- /dev/null +++ b/test/fuzztest/remindereventmanager_fuzzer/BUILD.gn @@ -0,0 +1,66 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//base/notification/distributed_notification_service/notification.gni") +import("//build/config/features.gni") +import("//build/test.gni") + +##############################fuzztest########################################## +ohos_fuzztest("ReminderEventManagerFuzzTest") { + module_out_path = service_fuzz_test_path + fuzz_config_file = + "${component_path}/test/fuzztest/remindereventmanager_fuzzer" + + include_dirs = [ + "${component_path}/test/fuzztest/fuzz_common_base", + "${component_path}/services/reminder/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + "-Dprivate=public", + "-Dprotected=public", + ] + sources = [ "remindereventmanager_fuzzer.cpp" ] + + deps = [ + "${component_path}/test/fuzztest/fuzz_common_base:fuzz_common_base", + "${frameworks_module_reminder_path}:reminder_innerkits", + "${services_path}/reminder:libreminder_static", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "data_share:datashare_common", + "ffrt:libffrt", + "hilog:libhilog", + "image_framework:image_native", + "kv_store:distributeddata_inner", + "relational_store:native_rdb", + "time_service:time_client", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [ ":ReminderEventManagerFuzzTest" ] +} +############################################################################### diff --git a/test/fuzztest/remindereventmanager_fuzzer/corpus/init b/test/fuzztest/remindereventmanager_fuzzer/corpus/init new file mode 100644 index 000000000..f7880ef1a --- /dev/null +++ b/test/fuzztest/remindereventmanager_fuzzer/corpus/init @@ -0,0 +1,13 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +FUZZ \ No newline at end of file diff --git a/test/fuzztest/remindereventmanager_fuzzer/project.xml b/test/fuzztest/remindereventmanager_fuzzer/project.xml new file mode 100644 index 000000000..7133b2b92 --- /dev/null +++ b/test/fuzztest/remindereventmanager_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/remindereventmanager_fuzzer/remindereventmanager_fuzzer.cpp b/test/fuzztest/remindereventmanager_fuzzer/remindereventmanager_fuzzer.cpp new file mode 100644 index 000000000..b3feed457 --- /dev/null +++ b/test/fuzztest/remindereventmanager_fuzzer/remindereventmanager_fuzzer.cpp @@ -0,0 +1,241 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "reminder_data_manager.h" +#include "reminder_request_timer.h" +#include "reminderdatamanager_fuzzer.h" +#include + +namespace OHOS { + bool DoSomethingInterestingWithManager(FuzzedDataProvider* fdp) + { + std::string bundleName = fdp->ConsumeRandomLengthString(); + int32_t userId = fdp->ConsumeIntegral(); + int32_t uid = fdp->ConsumeIntegral(); + int32_t reminderId = fdp->ConsumeIntegral(); + uint64_t date = fdp->ConsumeIntegral(); + bool value = fdp->ConsumeBool(); + uint8_t type = fdp->ConsumeIntegral(); + int32_t callingUid = fdp->ConsumeIntegral(); + EventFwk::Want want; + constexpr uint64_t seconds = 1200; + sptr reminder = new Notification::ReminderRequestTimer(seconds); + + Notification::ReminderDataManager::InitInstance(); + auto manager = Notification::ReminderDataManager::GetInstance(); + manager->RegisterConfigurationObserver(); + manager->Init(); + manager->Dump(); + manager->CancelAllReminders(bundleName, userId, uid); + manager->CancelReminder(reminderId, callingUid); + manager->CheckExcludeDateParam(reminderId, callingUid); + manager->AddExcludeDate(reminderId, date, callingUid); + manager->DelExcludeDates(reminderId, callingUid); + + std::vector dates; + manager->GetExcludeDates(reminderId, callingUid, dates); + manager->CloseReminder(want, value); + std::vector reminders; + manager->GetValidReminders(callingUid, reminders); + manager->Init(); + manager->InitUserId(); + std::vector> immediatelyReminders; + std::vector> extensionReminders; + manager->CheckReminderTime(immediatelyReminders, extensionReminders); + + manager->RegisterConfigurationObserver(); + manager->OnUserRemove(userId); + manager->OnBundleMgrServiceStart(); + manager->OnAbilityMgrServiceStart(); + manager->OnUserSwitch(userId); + manager->OnProcessDiedLocked(callingUid); + manager->RefreshRemindersDueToSysTimeChange(type); + manager->ShouldAlert(reminder); + manager->ShowActiveReminder(want); + manager->SnoozeReminder(want); + + manager->HandleCustomButtonClick(want); + manager->ClickReminder(want); + manager->TerminateAlerting(want); + return true; + } + + bool DoSomethingInterestingWithReminder(FuzzedDataProvider* fdp) + { + std::string bundleName = fdp->ConsumeRandomLengthString(); + int32_t userId = fdp->ConsumeIntegral(); + int32_t uid = fdp->ConsumeIntegral(); + int32_t reminderId = fdp->ConsumeIntegral(); + bool value = fdp->ConsumeBool(); + int32_t callingUid = fdp->ConsumeIntegral(); + constexpr uint64_t seconds = 1200; + sptr reminder = new Notification::ReminderRequestTimer(seconds); + auto manager = Notification::ReminderDataManager::GetInstance(); + + manager->OnLanguageChanged(); + manager->OnRemoveAppMgr(); + manager->CancelAllReminders(userId); + manager->CheckUpdateConditions(reminder, Notification::ReminderRequest::ActionButtonType::INVALID, + reminder->GetActionButtons()); + manager->CancelRemindersImplLocked(bundleName, userId, uid, value); + manager->CloseRemindersByGroupId(reminderId, bundleName, bundleName); + manager->CancelNotification(reminder); + manager->CheckReminderLimitExceededLocked(callingUid, reminder); + std::vector> reminders; + manager->GetImmediatelyShowRemindersLocked(reminders); + manager->AddToShowedReminders(reminder); + + manager->IsAllowedNotify(reminder); + manager->PlaySoundAndVibrationLocked(reminder); + manager->PlaySoundAndVibration(reminder); + manager->StopSoundAndVibrationLocked(reminder); + manager->StopSoundAndVibration(reminder); + manager->RemoveFromShowedReminders(reminder); + manager->RemoveReminderLocked(reminderId, false); + manager->SetActiveReminder(reminder); + manager->SetAlertingReminder(reminder); + manager->ShowActiveReminderExtendLocked(reminder, reminders); + + std::vector> extensionReminders; + std::vector> immediatelyReminders; + manager->PublishReminder(reminder, callingUid); + manager->FindReminderRequestLocked(reminderId, false); + manager->StartRecentReminder(); + manager->HandleImmediatelyShow(immediatelyReminders, value); + int8_t type = fdp->ConsumeIntegral(); + manager->HandleExtensionReminder(extensionReminders, type); + manager->HandleSameNotificationIdShowing(reminder); + manager->IsBelongToSameApp(uid, uid); + manager->CheckIsSameApp(reminder, uid); + manager->ShowReminder(reminder, value, value, value, value); + return true; + } + + bool DoSomethingInteresting1(FuzzedDataProvider* fdp) + { + std::string bundleName = fdp->ConsumeRandomLengthString(); + int32_t id = fdp->ConsumeIntegral(); + int64_t ts = fdp->ConsumeIntegral(); + int32_t reminderId = fdp->ConsumeIntegral(); + bool value = fdp->ConsumeBool(); + int32_t callingUid = fdp->ConsumeIntegral(); + constexpr uint64_t seconds = 1200; + sptr reminder = new Notification::ReminderRequestTimer(seconds); + auto manager = Notification::ReminderDataManager::GetInstance(); + manager->OnUnlockScreen(); + manager->OnLoadReminderEvent(); + manager->OnLoadReminderInFfrt(); + manager->OnDataShareInsertOrDelete(); + std::map> remindersMap; + manager->OnDataShareUpdate(remindersMap); + manager->HandleAutoDeleteReminder(id, id, ts); + + std::vector> reminders; + manager->UpdateReminderLanguageLocked(id, reminders); + manager->IsSystemReady(); + manager->QueryActiveReminderCount(); + manager->StartLoadTimer(); + manager->InitShareReminders(value); + manager->UpdateAppDatabase(reminder, Notification::ReminderRequest::ActionButtonType::CLOSE); + DataShare::DataSharePredicates predicates; + std::vector equalToVector; + manager->GenPredicates(predicates, equalToVector); + DataShare::DataShareValuesBucket valuesBucket; + manager->GenValuesBucket(valuesBucket, equalToVector); + manager->CloseReminder(reminder, value); + Notification::ReminderDataManager::TimerType t = Notification::ReminderDataManager::TimerType::ALERTING_TIMER; + manager->CreateTimerInfo(t, reminder); + auto sharedTimerInfo = std::make_shared(); + manager->InitTimerInfo(sharedTimerInfo, reminder, t); + manager->GetRecentReminder(); + manager->HandleSysTimeChange(reminder); + manager->IsMatched(reminder, id, id, value); + manager->ResetStates(t); + return true; + } + + bool DoSomethingInteresting2(FuzzedDataProvider* fdp) + { + constexpr uint16_t MAX_SECOND = 5; + std::string bundleName = fdp->ConsumeRandomLengthString(); + std::string uri = fdp->ConsumeRandomLengthString(); + int32_t id = fdp->ConsumeIntegral(); + int8_t val = fdp->ConsumeIntegral(); + uint16_t second = fdp->ConsumeIntegral() % MAX_SECOND + 1; + int64_t ts = fdp->ConsumeIntegral(); + int32_t reminderId = fdp->ConsumeIntegral(); + bool value = fdp->ConsumeBool(); + int32_t callingUid = fdp->ConsumeIntegral(); + constexpr uint64_t seconds = 1200; + sptr reminder = new Notification::ReminderRequestTimer(seconds); + auto manager = Notification::ReminderDataManager::GetInstance(); + manager->IsMatchedForGroupIdAndPkgName(reminder, bundleName, uri); + manager->IsReminderAgentReady(); + manager->StartExtensionAbility(reminder, 0); + int32_t count = 0; + manager->AsyncStartExtensionAbility(reminder, id, val, count); + manager->InitServiceHandler(); + manager->SnoozeReminderImpl(reminder); + Notification::ReminderDataManager::TimerType t = Notification::ReminderDataManager::TimerType::ALERTING_TIMER; + manager->StartTimerLocked(reminder, t); + manager->StartTimer(reminder, t); + manager->StopAlertingReminder(reminder); + manager->StopTimer(t); + manager->StopTimerLocked(t); + manager->TerminateAlerting(reminder, uri); + manager->TerminateAlerting(second, reminder); + manager->UpdateAndSaveReminderLocked(reminder); + Notification::ReminderDataManager::cmp(reminder, reminder); + manager->ConnectAppMgr(); + manager->CheckNeedNotifyStatus(reminder, Notification::ReminderRequest::ActionButtonType::INVALID); + manager->GetFullPath(uri); + manager->IsActionButtonDataShareValid(reminder, static_cast(id)); + manager->GetResourceMgr(bundleName, id); + Global::Resource::ResourceManager::RawFileDescriptor desc; + manager->GetCustomRingFileDesc(reminder, desc); + manager->CloseCustomRingFileDesc(id, uri); + manager->ReportSysEvent(reminder); + std::map> reminders; + manager->UpdateShareReminders(reminders); + std::unordered_map limits; + int32_t totalCount = 0; + manager->CheckShowLimit(limits, totalCount, reminder); + manager->LoadShareReminders(); + manager->LoadReminderFromDb(); + return true; + } + + bool Clear() + { + auto manager = Notification::ReminderDataManager::GetInstance(); + if (manager->queue_ != nullptr) { + auto handler = manager->queue_->submit_h(std::bind([]() {})); + manager->queue_->wait(handler); + } + return true; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + FuzzedDataProvider fdp(data, size); + OHOS::DoSomethingInterestingWithManager(&fdp); + OHOS::DoSomethingInterestingWithReminder(&fdp); + OHOS::DoSomethingInteresting1(&fdp); + OHOS::DoSomethingInteresting2(&fdp); + return 0; +} diff --git a/services/reminder/include/reminder_config_change_observer.h b/test/fuzztest/remindereventmanager_fuzzer/remindereventmanager_fuzzer.h similarity index 39% rename from services/reminder/include/reminder_config_change_observer.h rename to test/fuzztest/remindereventmanager_fuzzer/remindereventmanager_fuzzer.h index 982b7ddb1..8c3569942 100644 --- a/services/reminder/include/reminder_config_change_observer.h +++ b/test/fuzztest/remindereventmanager_fuzzer/remindereventmanager_fuzzer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -13,30 +13,11 @@ * limitations under the License. */ -#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_CONFIG_CHANGE_OBSERVER_H -#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_CONFIG_CHANGE_OBSERVER_H +#ifndef TEST_FUZZTEST_REMINDERDATAMANAGER_FUZZER_REMINDEREVENTMANAGER_FUZZER_H +#define TEST_FUZZTEST_REMINDERDATAMANAGER_FUZZER_REMINDEREVENTMANAGER_FUZZER_H -#include "configuration_observer_stub.h" +#include "fuzz_common_base.h" -namespace OHOS { -namespace Notification { - -/** - * @brief Listening system language change, when the system language changes, - * notify ReminderDataManager. -*/ -class ReminderConfigChangeObserver final : public AppExecFwk::ConfigurationObserverStub { -public: - ReminderConfigChangeObserver() = default; - ~ReminderConfigChangeObserver() = default; +#define FUZZ_PROJECT_NAME "remindereventmanager_fuzzer" -public: - void OnConfigurationUpdated(const AppExecFwk::Configuration &configuration) override; - -private: - std::string languageInfo_; -}; -} // namespace Notification -} // namespace OHOS - -#endif \ No newline at end of file +#endif // TEST_FUZZTEST_REMINDERDATAMANAGER_FUZZER_REMINDEREVENTMANAGER_FUZZER_H \ No newline at end of file -- Gitee From b212b595d45c3955c58a11c4ac2274b4b3923faa Mon Sep 17 00:00:00 2001 From: gaojiaqi Date: Mon, 16 Jun 2025 11:32:23 +0800 Subject: [PATCH 2/2] refactor event manager Signed-off-by: gaojiaqi --- services/reminder/BUILD.gn | 1 - .../reminder/include/reminder_data_manager.h | 1 - .../reminder/include/reminder_event_manager.h | 115 +++--- .../src/reminder_config_change_observer.cpp | 41 -- .../reminder/src/reminder_data_manager.cpp | 3 +- .../reminder/src/reminder_event_manager.cpp | 347 +++++++--------- services/reminder/test/unittest/BUILD.gn | 65 +++ .../mock/include/mock_notification_helper.h | 2 + .../mock/include/mock_reminder_data_manager.h | 20 + .../mock/mock_notification_helper.cpp | 31 +- .../mock/mock_reminder_data_manager.cpp | 121 ++++++ .../unittest/reminder_agent_service_test.cpp | 4 +- .../unittest/reminder_event_manager_test.cpp | 370 ++++++++++++++++++ .../remindereventmanager_fuzzer/BUILD.gn | 66 ++++ .../remindereventmanager_fuzzer/corpus/init | 13 + .../remindereventmanager_fuzzer/project.xml | 25 ++ .../remindereventmanager_fuzzer.cpp | 241 ++++++++++++ .../remindereventmanager_fuzzer.h | 31 +- 18 files changed, 1158 insertions(+), 339 deletions(-) delete mode 100644 services/reminder/src/reminder_config_change_observer.cpp create mode 100644 services/reminder/test/unittest/reminder_event_manager_test.cpp create mode 100644 test/fuzztest/remindereventmanager_fuzzer/BUILD.gn create mode 100644 test/fuzztest/remindereventmanager_fuzzer/corpus/init create mode 100644 test/fuzztest/remindereventmanager_fuzzer/project.xml create mode 100644 test/fuzztest/remindereventmanager_fuzzer/remindereventmanager_fuzzer.cpp rename services/reminder/include/reminder_config_change_observer.h => test/fuzztest/remindereventmanager_fuzzer/remindereventmanager_fuzzer.h (39%) diff --git a/services/reminder/BUILD.gn b/services/reminder/BUILD.gn index 182e3404d..146d6e1bf 100644 --- a/services/reminder/BUILD.gn +++ b/services/reminder/BUILD.gn @@ -54,7 +54,6 @@ ohos_source_set("reminder_service_sources") { "src/reminder_agent_service.cpp", "src/reminder_agent_service_ability.cpp", "src/reminder_bundle_manager_helper.cpp", - "src/reminder_config_change_observer.cpp", "src/reminder_data_manager.cpp", "src/reminder_data_manager_inner.cpp", "src/reminder_datashare_helper.cpp", diff --git a/services/reminder/include/reminder_data_manager.h b/services/reminder/include/reminder_data_manager.h index a16a21f4e..d68a2c15f 100644 --- a/services/reminder/include/reminder_data_manager.h +++ b/services/reminder/include/reminder_data_manager.h @@ -30,7 +30,6 @@ #include "reminder_request_adaptation.h" #include "reminder_store.h" #include "reminder_timer_info.h" -#include "reminder_config_change_observer.h" #include "datashare_predicates.h" #include "datashare_values_bucket.h" #include "app_mgr_interface.h" diff --git a/services/reminder/include/reminder_event_manager.h b/services/reminder/include/reminder_event_manager.h index 4f1cae627..040213846 100644 --- a/services/reminder/include/reminder_event_manager.h +++ b/services/reminder/include/reminder_event_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -17,85 +17,84 @@ #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_EVENT_MANAGER_H #include "common_event_subscriber.h" -#include "reminder_data_manager.h" -#include "system_ability_status_change_stub.h" #include "notification_subscriber.h" +#include "configuration_observer_stub.h" +#include "system_ability_status_change_stub.h" -#include - -namespace OHOS { -namespace Notification { +namespace OHOS::Notification { class ReminderEventManager { public: - explicit ReminderEventManager(std::shared_ptr &reminderDataManager); - virtual ~ReminderEventManager() {}; - ReminderEventManager(ReminderEventManager &other) = delete; - ReminderEventManager& operator = (const ReminderEventManager &other) = delete; + ReminderEventManager() = default; + ~ReminderEventManager() = default; + ReminderEventManager(ReminderEventManager& other) = delete; + ReminderEventManager& operator = (const ReminderEventManager& other) = delete; + +public: + void Init(); private: - void init(std::shared_ptr &reminderDataManager) const; - void SubscribeSystemAbility(std::shared_ptr &reminderDataManager) const; + void SubscribeEvent(); + void SubscribeSystemAbility(const int32_t systemAbilityId); class ReminderEventSubscriber : public EventFwk::CommonEventSubscriber { public: - ReminderEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscriberInfo, - std::shared_ptr &reminderDataManager); - virtual void OnReceiveEvent(const EventFwk::CommonEventData &data); - -private: - sptr GetBundleOption(const OHOS::EventFwk::Want &want) const; - int32_t GetUid(const OHOS::EventFwk::Want &want) const; - void HandlePackageRemove(const EventFwk::Want &want) const; - void HandleProcessDied(const EventFwk::Want &want) const; - std::shared_ptr reminderDataManager_ = nullptr; + ReminderEventSubscriber(const EventFwk::CommonEventSubscribeInfo& subscriberInfo); + void OnReceiveEvent(const EventFwk::CommonEventData& data) override; }; -class ReminderEventCustomSubscriber : public EventFwk::CommonEventSubscriber { +class ReminderCustomEventSubscriber : public EventFwk::CommonEventSubscriber { public: - ReminderEventCustomSubscriber(const EventFwk::CommonEventSubscribeInfo &subscriberInfo, - std::shared_ptr &reminderDataManager); - virtual void OnReceiveEvent(const EventFwk::CommonEventData &data); - -private: - std::shared_ptr reminderDataManager_ = nullptr; + ReminderCustomEventSubscriber(const EventFwk::CommonEventSubscribeInfo& subscriberInfo); + void OnReceiveEvent(const EventFwk::CommonEventData& data) override; }; -class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub { +class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub { public: - explicit SystemAbilityStatusChangeListener(std::shared_ptr &reminderDataManager); - ~SystemAbilityStatusChangeListener() {}; - virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; - virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; -private: - std::shared_ptr reminderDataManager_ = nullptr; + SystemAbilityStatusChangeListener() = default; + ~SystemAbilityStatusChangeListener() = default; + + void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; + void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; }; class ReminderNotificationSubscriber : public NotificationSubscriber { public: - explicit ReminderNotificationSubscriber(std::shared_ptr &reminderDataManager); - ~ReminderNotificationSubscriber() override; - void OnConnected() override; - void OnDisconnected() override; - void OnCanceled(const std::shared_ptr &request, - const std::shared_ptr &sortingMap, int deleteReason) override; - void OnConsumed(const std::shared_ptr &request, - const std::shared_ptr &sortingMap) override; - void OnUpdate(const std::shared_ptr &sortingMap) override; - void OnDied() override; + ReminderNotificationSubscriber() = default; + ~ReminderNotificationSubscriber() override = default; + + void OnConnected() override {} + void OnDisconnected() override {} + void OnCanceled(const std::shared_ptr& request, + const std::shared_ptr& sortingMap, int deleteReason) override; + void OnConsumed(const std::shared_ptr& request, + const std::shared_ptr& sortingMap) override {} + void OnUpdate(const std::shared_ptr& sortingMap) override {} + void OnDied() override {} void OnDoNotDisturbDateChange( - const std::shared_ptr &date) override; + const std::shared_ptr& date) override {} void OnEnabledNotificationChanged( - const std::shared_ptr &callbackData) override; - void OnBadgeChanged(const std::shared_ptr &badgeData) override; - void OnBadgeEnabledChanged(const sptr &callbackData) override; - void OnBatchCanceled(const std::vector> &requestList, - const std::shared_ptr &sortingMap, int32_t deleteReason) override; -private: - std::shared_ptr reminderDataManager_ = nullptr; + const std::shared_ptr& callbackData) override {} + void OnBadgeChanged(const std::shared_ptr& badgeData) override {} + void OnBadgeEnabledChanged(const sptr& callbackData) override {} + void OnBatchCanceled(const std::vector>& requestList, + const std::shared_ptr& sortingMap, int32_t deleteReason) override {} }; +}; + +/** + * @brief Listening system language change, when the system language changes, + * notify ReminderDataManager. + */ +class ReminderConfigChangeObserver final : public AppExecFwk::ConfigurationObserverStub { +public: + ReminderConfigChangeObserver() = default; + ~ReminderConfigChangeObserver() = default; - static std::shared_ptr subscriber_; +public: + void OnConfigurationUpdated(const AppExecFwk::Configuration& configuration) override; + +private: + std::string languageInfo_; }; -} // namespace OHOS -} // namespace Notification +} // namespace OHOS::Notification #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_EVENT_MANAGER_H diff --git a/services/reminder/src/reminder_config_change_observer.cpp b/services/reminder/src/reminder_config_change_observer.cpp deleted file mode 100644 index a19712fb0..000000000 --- a/services/reminder/src/reminder_config_change_observer.cpp +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "reminder_config_change_observer.h" - -#include "ans_log_wrapper.h" -#include "reminder_data_manager.h" - -namespace OHOS { -namespace Notification { - -void ReminderConfigChangeObserver::OnConfigurationUpdated(const AppExecFwk::Configuration &configuration) -{ - ANSR_LOGD("OnConfigurationUpdated."); - auto reminderDataMgr = ReminderDataManager::GetInstance(); - if (reminderDataMgr == nullptr) { - ANSR_LOGE("Reminder data manager is nullptr"); - return; - } - std::string newLanguageInfo = configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE); - if (!newLanguageInfo.empty() && newLanguageInfo != languageInfo_) { - ANSR_LOGD("language change: %{public}s -> %{public}s", languageInfo_.c_str(), newLanguageInfo.c_str()); - reminderDataMgr->OnLanguageChanged(); - languageInfo_ = newLanguageInfo; - } -} - -} // namespace Notification -} // namespace OHOS diff --git a/services/reminder/src/reminder_data_manager.cpp b/services/reminder/src/reminder_data_manager.cpp index aa3498344..59f5a7459 100644 --- a/services/reminder/src/reminder_data_manager.cpp +++ b/services/reminder/src/reminder_data_manager.cpp @@ -621,7 +621,8 @@ std::shared_ptr ReminderDataManager::InitInstance() if (REMINDER_DATA_MANAGER == nullptr) { REMINDER_DATA_MANAGER = std::make_shared(); REMINDER_DATA_MANAGER->Init(); - ReminderEventManager reminderEventManager(REMINDER_DATA_MANAGER); + ReminderEventManager reminderEventManager; + reminderEventManager.Init(); } return REMINDER_DATA_MANAGER; } diff --git a/services/reminder/src/reminder_event_manager.cpp b/services/reminder/src/reminder_event_manager.cpp index ec486dc45..a261de8f4 100644 --- a/services/reminder/src/reminder_event_manager.cpp +++ b/services/reminder/src/reminder_event_manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,34 +16,30 @@ #include "reminder_event_manager.h" #include "ans_log_wrapper.h" -#include "bundle_constants.h" -#include "bundle_mgr_interface.h" -#include "common_event_manager.h" -#include "common_event_support.h" +#include "reminder_data_manager.h" #include "reminder_bundle_manager_helper.h" -#include "if_system_ability_manager.h" + #include "ipc_skeleton.h" -#include "iservice_registry.h" -#include "system_ability_definition.h" #include "notification_helper.h" -#include "string_ex.h" +#include "common_event_manager.h" +#include "common_event_support.h" +#include "system_ability_definition.h" +#include "if_system_ability_manager.h" -using namespace OHOS::EventFwk; -namespace OHOS { -namespace Notification { -static const std::string NOTIFICATION_LABEL = "REMINDER_AGENT"; +namespace OHOS::Notification { static constexpr const char* UNLOCK_SCREEN_EVENT = "common.event.UNLOCK_SCREEN"; -std::shared_ptr ReminderEventManager::subscriber_ - = nullptr; -ReminderEventManager::ReminderEventManager(std::shared_ptr &reminderDataManager) +void ReminderEventManager::Init() { - init(reminderDataManager); + SubscribeEvent(); + SubscribeSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + SubscribeSystemAbility(APP_MGR_SERVICE_ID); + SubscribeSystemAbility(ABILITY_MGR_SERVICE_ID); } -void ReminderEventManager::init(std::shared_ptr &reminderDataManager) const +void ReminderEventManager::SubscribeEvent() { - MatchingSkills customMatchingSkills; + EventFwk::MatchingSkills customMatchingSkills; customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_ALARM_ALERT); customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT); customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CLOSE_ALERT); @@ -51,223 +47,177 @@ void ReminderEventManager::init(std::shared_ptr &reminderDa customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION); customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CUSTOM_ALERT); customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CLICK_ALERT); - CommonEventSubscribeInfo customSubscriberInfo(customMatchingSkills); + EventFwk::CommonEventSubscribeInfo customSubscriberInfo(customMatchingSkills); customSubscriberInfo.SetPermission("ohos.permission.GRANT_SENSITIVE_PERMISSIONS"); customSubscriberInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON); - auto customSubscriber = std::make_shared(customSubscriberInfo, reminderDataManager); - - MatchingSkills matchingSkills; - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIME_CHANGED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED); - CommonEventSubscribeInfo subscriberInfo(matchingSkills); + auto customSubscriber = std::make_shared(customSubscriberInfo); + + EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_TIME_CHANGED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED); + EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); subscriberInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON); - auto subscriber = std::make_shared(subscriberInfo, reminderDataManager); + auto subscriber = std::make_shared(subscriberInfo); - MatchingSkills screenMatchingSkills; + EventFwk::MatchingSkills screenMatchingSkills; screenMatchingSkills.AddEvent(UNLOCK_SCREEN_EVENT); - CommonEventSubscribeInfo screenSubscriberInfo(screenMatchingSkills); + EventFwk::CommonEventSubscribeInfo screenSubscriberInfo(screenMatchingSkills); screenSubscriberInfo.SetPublisherBundleName(AppExecFwk::Constants::SCENE_BOARD_BUNDLE_NAME); - auto screenSubscriber = std::make_shared(screenSubscriberInfo, reminderDataManager); + auto screenSubscriber = std::make_shared(screenSubscriberInfo); std::string identity = IPCSkeleton::ResetCallingIdentity(); - if (CommonEventManager::SubscribeCommonEvent(subscriber) && - CommonEventManager::SubscribeCommonEvent(customSubscriber) && - CommonEventManager::SubscribeCommonEvent(screenSubscriber)) { - ANSR_LOGD("SubscribeCommonEvent ok"); + if (EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber) && + EventFwk::CommonEventManager::SubscribeCommonEvent(customSubscriber) && + EventFwk::CommonEventManager::SubscribeCommonEvent(screenSubscriber)) { + ANSR_LOGD("SubscribeCommonEvent ok."); } else { - ANSR_LOGD("SubscribeCommonEvent fail"); + ANSR_LOGW("SubscribeCommonEvent failed."); } IPCSkeleton::SetCallingIdentity(identity); - subscriber_ = std::make_shared(reminderDataManager); - if (NotificationHelper::SubscribeNotification(*subscriber_) != ERR_OK) { - ANSR_LOGD("SubscribeNotification failed"); + auto notificationSubscriber = std::make_shared(); + if (NotificationHelper::SubscribeNotification(*notificationSubscriber) != ERR_OK) { + ANSR_LOGW("SubscribeNotification failed."); } - - SubscribeSystemAbility(reminderDataManager); } -void ReminderEventManager::SubscribeSystemAbility(std::shared_ptr &reminderDataManager) const +void ReminderEventManager::SubscribeSystemAbility(const int32_t systemAbilityId) { sptr statusChangeListener - = new (std::nothrow) SystemAbilityStatusChangeListener(reminderDataManager); + = new (std::nothrow) SystemAbilityStatusChangeListener(); if (statusChangeListener == nullptr) { - ANSR_LOGE("Failed to create statusChangeListener due to no memory."); + ANSR_LOGW("Failed to create SystemAbilityStatusChangeListener."); return; } - // app mgr - sptr appMgrStatusChangeListener - = new (std::nothrow) SystemAbilityStatusChangeListener(reminderDataManager); - if (appMgrStatusChangeListener == nullptr) { - ANSR_LOGE("Failed to create appMgrStatusChangeListener due to no memory."); - return; - } - // ability mgr - sptr abilityMgrStatusListener - = new (std::nothrow) SystemAbilityStatusChangeListener(reminderDataManager); - if (abilityMgrStatusListener == nullptr) { - ANSR_LOGE("Failed to create abilityMgrStatusListener due to no memory."); - return; - } - sptr samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (samgrProxy == nullptr) { - ANSR_LOGD("samgrProxy is null"); + ANSR_LOGW("GetSystemAbilityManager failed."); return; } - int32_t ret = samgrProxy->SubscribeSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, statusChangeListener); + int32_t ret = samgrProxy->SubscribeSystemAbility(systemAbilityId, statusChangeListener); if (ret != ERR_OK) { - ANSR_LOGE("subscribe system ability id: %{public}d failed", BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - } - ret = samgrProxy->SubscribeSystemAbility(APP_MGR_SERVICE_ID, appMgrStatusChangeListener); - if (ret != ERR_OK) { - ANSR_LOGE("subscribe system ability id: %{public}d failed", APP_MGR_SERVICE_ID); - } - ret = samgrProxy->SubscribeSystemAbility(ABILITY_MGR_SERVICE_ID, abilityMgrStatusListener); - if (ret != ERR_OK) { - ANSR_LOGE("subscribe system ability id: %{public}d failed", ABILITY_MGR_SERVICE_ID); + ANSR_LOGW("SubscribeSystemAbility id: %{public}d failed.", systemAbilityId); } } ReminderEventManager::ReminderEventSubscriber::ReminderEventSubscriber( - const CommonEventSubscribeInfo &subscriberInfo, - std::shared_ptr &reminderDataManager) : CommonEventSubscriber(subscriberInfo) -{ - reminderDataManager_ = reminderDataManager; -} + const EventFwk::CommonEventSubscribeInfo& subscriberInfo) : CommonEventSubscriber(subscriberInfo) +{} -ReminderEventManager::ReminderEventCustomSubscriber::ReminderEventCustomSubscriber( - const CommonEventSubscribeInfo &subscriberInfo, - std::shared_ptr &reminderDataManager) : CommonEventSubscriber(subscriberInfo) +void ReminderEventManager::ReminderEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData& data) { - reminderDataManager_ = reminderDataManager; -} - -void ReminderEventManager::ReminderEventCustomSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data) -{ - Want want = data.GetWant(); + auto manager = ReminderDataManager::GetInstance(); + if (manager == nullptr) { + return; + } + EventFwk::Want want = data.GetWant(); std::string action = want.GetAction(); - ANSR_LOGI("Recieved common event:%{public}s", action.c_str()); - if (action == ReminderRequest::REMINDER_EVENT_ALARM_ALERT) { - reminderDataManager_->ShowActiveReminder(want); + ANSR_LOGD("Recieved common event:%{public}s.", action.c_str()); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED || + action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) { + AppExecFwk::ElementName ele = want.GetElement(); + std::string bundleName = ele.GetBundleName(); + int32_t userId = want.GetIntParam(AppExecFwk::Constants::USER_ID, -1); + int32_t uid = want.GetIntParam(AppExecFwk::Constants::UID, -1); + manager->CancelAllReminders(bundleName, userId, uid); return; } - if (action == ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT) { - reminderDataManager_->TerminateAlerting(want); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED) { + AppExecFwk::ElementName ele = want.GetElement(); + std::string bundleName = ele.GetBundleName(); + int32_t userId = want.GetIntParam(AppExecFwk::Constants::USER_ID, -1); + int32_t uid = ReminderBundleManagerHelper::GetInstance().GetDefaultUidByBundleName(bundleName, userId); + manager->OnProcessDiedLocked(uid); return; } - if (action == ReminderRequest::REMINDER_EVENT_CLOSE_ALERT) { - reminderDataManager_->CloseReminder(want, true); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED) { + manager->RefreshRemindersDueToSysTimeChange(ReminderDataManager::TIME_ZONE_CHANGE); return; } - if (action == ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT) { - reminderDataManager_->SnoozeReminder(want); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_TIME_CHANGED) { + manager->RefreshRemindersDueToSysTimeChange(ReminderDataManager::DATE_TIME_CHANGE); return; } - if (action == ReminderRequest::REMINDER_EVENT_CUSTOM_ALERT) { - reminderDataManager_->HandleCustomButtonClick(want); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { + manager->OnUserSwitch(data.GetCode()); return; } - if (action == ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION) { - reminderDataManager_->CloseReminder(want, false, false); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) { + manager->OnUserRemove(data.GetCode()); return; } - if (action == ReminderRequest::REMINDER_EVENT_CLICK_ALERT) { - reminderDataManager_->ClickReminder(want); + if (action.compare(UNLOCK_SCREEN_EVENT) == 0) { + manager->OnUnlockScreen(); return; } } -void ReminderEventManager::ReminderEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data) +ReminderEventManager::ReminderCustomEventSubscriber::ReminderCustomEventSubscriber( + const EventFwk::CommonEventSubscribeInfo& subscriberInfo) : CommonEventSubscriber(subscriberInfo) +{} + +void ReminderEventManager::ReminderCustomEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData& data) { - Want want = data.GetWant(); - std::string action = want.GetAction(); - ANSR_LOGD("Recieved common event:%{public}s", action.c_str()); - if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) { - HandlePackageRemove(want); + auto manager = ReminderDataManager::GetInstance(); + if (manager == nullptr) { return; } - if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) { - HandlePackageRemove(want); + EventFwk::Want want = data.GetWant(); + std::string action = want.GetAction(); + ANSR_LOGI("Recieved common event:%{public}s.", action.c_str()); + if (action == ReminderRequest::REMINDER_EVENT_ALARM_ALERT) { + manager->ShowActiveReminder(want); return; } - if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED) { - HandleProcessDied(want); + if (action == ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT) { + manager->TerminateAlerting(want); return; } - if (action == CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED) { - reminderDataManager_->RefreshRemindersDueToSysTimeChange(ReminderDataManager::TIME_ZONE_CHANGE); + if (action == ReminderRequest::REMINDER_EVENT_CLOSE_ALERT) { + manager->CloseReminder(want, true); return; } - if (action == CommonEventSupport::COMMON_EVENT_TIME_CHANGED) { - reminderDataManager_->RefreshRemindersDueToSysTimeChange(ReminderDataManager::DATE_TIME_CHANGE); + if (action == ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT) { + manager->SnoozeReminder(want); return; } - if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { - reminderDataManager_->OnUserSwitch(data.GetCode()); + if (action == ReminderRequest::REMINDER_EVENT_CUSTOM_ALERT) { + manager->HandleCustomButtonClick(want); return; } - if (action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) { - reminderDataManager_->OnUserRemove(data.GetCode()); + if (action == ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION) { + manager->CloseReminder(want, false, false); return; } - if (action.compare(UNLOCK_SCREEN_EVENT) == 0) { - reminderDataManager_->OnUnlockScreen(); + if (action == ReminderRequest::REMINDER_EVENT_CLICK_ALERT) { + manager->ClickReminder(want); + return; } } -void ReminderEventManager::ReminderEventSubscriber::HandlePackageRemove(const EventFwk::Want &want) const -{ - OHOS::AppExecFwk::ElementName ele = want.GetElement(); - std::string bundleName = ele.GetBundleName(); - int32_t userId = want.GetIntParam(OHOS::AppExecFwk::Constants::USER_ID, -1); - int32_t uid = want.GetIntParam(OHOS::AppExecFwk::Constants::UID, -1); - reminderDataManager_->CancelAllReminders(bundleName, userId, uid); -} - -void ReminderEventManager::ReminderEventSubscriber::HandleProcessDied(const EventFwk::Want &want) const -{ - int32_t uid = GetUid(want); - reminderDataManager_->OnProcessDiedLocked(uid); -} - -int32_t ReminderEventManager::ReminderEventSubscriber::GetUid( - const OHOS::EventFwk::Want &want) const -{ - OHOS::AppExecFwk::ElementName ele = want.GetElement(); - std::string bundleName = ele.GetBundleName(); - int32_t userId = want.GetIntParam(OHOS::AppExecFwk::Constants::USER_ID, -1); - int32_t uid = ReminderBundleManagerHelper::GetInstance().GetDefaultUidByBundleName(bundleName, userId); - ANSR_LOGD("bundleName=%{public}s, userId=%{private}d, uid=%{public}d", bundleName.c_str(), userId, uid); - return uid; -} - -ReminderEventManager::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener( - std::shared_ptr &reminderDataManager) -{ - reminderDataManager_ = reminderDataManager; -} - void ReminderEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility( int32_t systemAbilityId, const std::string& deviceId) { - ANSR_LOGD("OnAddSystemAbilityInner"); + auto manager = ReminderDataManager::GetInstance(); + if (manager == nullptr) { + return; + } switch (systemAbilityId) { case BUNDLE_MGR_SERVICE_SYS_ABILITY_ID: - ANSR_LOGD("OnAddSystemAbilityInner: BUNDLE_MGR_SERVICE_SYS_ABILITY"); - reminderDataManager_->OnBundleMgrServiceStart(); + ANSR_LOGD("AddSystemAbility: BUNDLE_MGR_SERVICE_SYS_ABILITY_ID."); + manager->OnBundleMgrServiceStart(); break; case APP_MGR_SERVICE_ID: - ANSR_LOGD("OnAddSystemAbilityInner: APP_MGR_SERVICE"); + ANSR_LOGD("AddSystemAbility: APP_MGR_SERVICE_ID."); break; case ABILITY_MGR_SERVICE_ID: - ANSR_LOGD("OnAddSystemAbilityInner ABILITY_MGR_SERVICE_ID"); - reminderDataManager_->OnAbilityMgrServiceStart(); + ANSR_LOGD("AddSystemAbility: ABILITY_MGR_SERVICE_ID."); + manager->OnAbilityMgrServiceStart(); break; default: break; @@ -277,38 +227,29 @@ void ReminderEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility void ReminderEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility( int32_t systemAbilityId, const std::string& deviceId) { - ANSR_LOGD("OnRemoveSystemAbilityInner"); + auto manager = ReminderDataManager::GetInstance(); + if (manager == nullptr) { + return; + } switch (systemAbilityId) { case BUNDLE_MGR_SERVICE_SYS_ABILITY_ID: - ANSR_LOGD("OnRemoveSystemAbilityInner: BUNDLE_MGR_SERVICE_SYS_ABILITY"); + ANSR_LOGD("RemoveSystemAbility: BUNDLE_MGR_SERVICE_SYS_ABILITY_ID."); break; case APP_MGR_SERVICE_ID: - ANSR_LOGD("OnRemoveSystemAbilityInner: APP_MGR_SERVICE"); - reminderDataManager_->OnRemoveAppMgr(); + ANSR_LOGD("RemoveSystemAbility: APP_MGR_SERVICE_ID."); + manager->OnRemoveAppMgr(); break; case ABILITY_MGR_SERVICE_ID: - ANSR_LOGD("OnRemoveSystemAbilityInner ABILITY_MGR_SERVICE_ID"); + ANSR_LOGD("RemoveSystemAbility: ABILITY_MGR_SERVICE_ID."); break; default: break; } } -ReminderEventManager::ReminderNotificationSubscriber::ReminderNotificationSubscriber( - std::shared_ptr &reminderDataManager) -{ - reminderDataManager_ = reminderDataManager; -} - -ReminderEventManager::ReminderNotificationSubscriber::~ReminderNotificationSubscriber() {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnConnected() {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnDisconnected() {} - void ReminderEventManager::ReminderNotificationSubscriber::OnCanceled( - const std::shared_ptr ¬ification, - const std::shared_ptr &sortingMap, int deleteReason) + const std::shared_ptr& notification, + const std::shared_ptr& sortingMap, int deleteReason) { // Note: Don't modify param notification if (deleteReason != NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE) { @@ -320,40 +261,30 @@ void ReminderEventManager::ReminderNotificationSubscriber::OnCanceled( NotificationRequest request = notification->GetNotificationRequest(); std::string label = request.GetLabel(); int64_t autoDeletedTime = request.GetAutoDeletedTime(); - if (autoDeletedTime <= 0 || label != NOTIFICATION_LABEL) { + if (autoDeletedTime <= 0 || label.compare(ReminderRequest::NOTIFICATION_LABEL) != 0) { return; } - if (reminderDataManager_ == nullptr) { + auto manager = ReminderDataManager::GetInstance(); + if (manager == nullptr) { return; } int32_t notificationId = request.GetNotificationId(); int32_t uid = request.GetOwnerUid() == 0 ? request.GetCreatorUid() : request.GetOwnerUid(); - reminderDataManager_->HandleAutoDeleteReminder(notificationId, uid, autoDeletedTime); + manager->HandleAutoDeleteReminder(notificationId, uid, autoDeletedTime); } -void ReminderEventManager::ReminderNotificationSubscriber::OnConsumed(const std::shared_ptr ¬ification, - const std::shared_ptr &sortingMap) {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnUpdate( - const std::shared_ptr &sortingMap) {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnDied() {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnDoNotDisturbDateChange( - const std::shared_ptr &date) {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnEnabledNotificationChanged( - const std::shared_ptr &callbackData) {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnBadgeChanged( - const std::shared_ptr &badgeData) {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnBadgeEnabledChanged( - const sptr &callbackData) {} - -void ReminderEventManager::ReminderNotificationSubscriber::OnBatchCanceled( - const std::vector> &requestList, - const std::shared_ptr &sortingMap, int32_t deleteReason) {} -} // namespace OHOS -} // namespace Notification +void ReminderConfigChangeObserver::OnConfigurationUpdated(const AppExecFwk::Configuration& configuration) +{ + auto manager = ReminderDataManager::GetInstance(); + if (manager == nullptr) { + return; + } + std::string newLanguageInfo = configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE); + if (!newLanguageInfo.empty() && newLanguageInfo != languageInfo_) { + ANSR_LOGD("Language change: %{public}s -> %{public}s.", languageInfo_.c_str(), newLanguageInfo.c_str()); + manager->OnLanguageChanged(); + languageInfo_ = newLanguageInfo; + } +} +} // namespace OHOS::Notification diff --git a/services/reminder/test/unittest/BUILD.gn b/services/reminder/test/unittest/BUILD.gn index bf5ca1c19..087353932 100644 --- a/services/reminder/test/unittest/BUILD.gn +++ b/services/reminder/test/unittest/BUILD.gn @@ -402,12 +402,77 @@ ohos_unittest("reminder_agent_service_ability_test") { part_name = "${component_name}" } +ohos_unittest("reminder_event_manager_test") { + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + module_out_path = module_output_path + include_dirs = [ + ".", + "include", + "/${services_path}/reminder/include", + "${services_path}/reminder/test/unittest/mock/include", + ] + sources = [ + "${services_path}/reminder/src/reminder_event_manager.cpp", + "mock/mock_notification_helper.cpp", + "mock/mock_reminder_bundle_manager_helper.cpp", + "mock/mock_reminder_data_manager.cpp", + "reminder_event_manager_test.cpp", + ] + deps = [ + "${frameworks_module_ans_path}:ans_innerkits", + "${frameworks_module_reminder_path}:reminder_innerkits", + ] + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:abilitykit_native", + "ability_runtime:app_manager", + "ability_runtime:wantagent_innerkits", + "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "data_share:datashare_common", + "eventhandler:libeventhandler", + "ffrt:libffrt", + "hilog:libhilog", + "hitrace:hitrace_meter", + "hitrace:libhitracechain", + "image_framework:image_native", + "init:libbegetutil", + "ipc:ipc_core", + "kv_store:distributeddata_inner", + "os_account:os_account_innerkits", + "relational_store:native_rdb", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "time_service:time_client", + ] + + cflags = [ + "-Dprivate=public", + "-Dprotected=public", + ] + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} + group("unittest") { testonly = true deps = [ ":reminder_agent_service_ability_test", ":reminder_agent_service_test", ":reminder_bundle_manager_helper_test", + ":reminder_event_manager_test", ":reminder_service_test", ":reminder_unit_test", ] diff --git a/services/reminder/test/unittest/mock/include/mock_notification_helper.h b/services/reminder/test/unittest/mock/include/mock_notification_helper.h index 802fe8c38..bb1e992d1 100644 --- a/services/reminder/test/unittest/mock/include/mock_notification_helper.h +++ b/services/reminder/test/unittest/mock/include/mock_notification_helper.h @@ -23,6 +23,8 @@ class MockNotificationHelper { public: static void MockIsAllowUseReminder(const bool isAllowUseReminder); static void MockIsAllowedNotify(const bool isAllowedNotify, const int32_t ret); + static void MockSubscribeNotification(const int32_t ret); + static void MockSubscribeCommonEvent(const bool ret); }; } // namespace OHOS::Notification diff --git a/services/reminder/test/unittest/mock/include/mock_reminder_data_manager.h b/services/reminder/test/unittest/mock/include/mock_reminder_data_manager.h index 73a0742e1..5cab50679 100644 --- a/services/reminder/test/unittest/mock/include/mock_reminder_data_manager.h +++ b/services/reminder/test/unittest/mock/include/mock_reminder_data_manager.h @@ -29,6 +29,26 @@ public: static void MockDelExcludeDates(const int32_t ret); static void MockGetExcludeDates(const int32_t ret); static void MockQueryActiveReminderCount(const int32_t ret); + + static void ResetFlag(); + + static bool callCancelAllReminders_; + static bool callOnProcessDiedLocked_; + static bool callRefreshRemindersDueToSysTimeChange_; + static bool callOnUserSwitch_; + static bool callOnUserRemove_; + static bool callOnUnlockScreen_; + static bool callShowActiveReminder_; + static bool callTerminateAlerting_; + static bool callCloseReminder_; + static bool callSnoozeReminder_; + static bool callHandleCustomButtonClick_; + static bool callClickReminder_; + static bool callOnBundleMgrServiceStart_; + static bool callOnAbilityMgrServiceStart_; + static bool callOnRemoveAppMgr_; + static bool callHandleAutoDeleteReminder_; + static bool callOnLanguageChanged_; }; } // namespace OHOS::Notification diff --git a/services/reminder/test/unittest/mock/mock_notification_helper.cpp b/services/reminder/test/unittest/mock/mock_notification_helper.cpp index 3058d8f26..754fcc297 100644 --- a/services/reminder/test/unittest/mock/mock_notification_helper.cpp +++ b/services/reminder/test/unittest/mock/mock_notification_helper.cpp @@ -16,14 +16,18 @@ #include "mock_notification_helper.h" #include "notification_helper.h" +#include "common_event_manager.h" -namespace OHOS::Notification { +namespace OHOS { namespace { bool g_mockIsAllowUseReminder = true; bool g_mockIsAllowedNotify = true; +bool g_mockSubscribeCommonEventRet = true; int32_t g_mockIsAllowedNotifyRet = 0; +int32_t g_mockSubscribeNotificationRet = 0; } +namespace Notification { void MockNotificationHelper::MockIsAllowUseReminder(const bool isAllowUseReminder) { g_mockIsAllowUseReminder = isAllowUseReminder; @@ -35,6 +39,16 @@ void MockNotificationHelper::MockIsAllowedNotify(const bool isAllowedNotify, con g_mockIsAllowedNotifyRet = ret; } +void MockNotificationHelper::MockSubscribeNotification(const int32_t ret) +{ + g_mockSubscribeNotificationRet = ret; +} + +void MockNotificationHelper::MockSubscribeCommonEvent(const bool ret) +{ + g_mockSubscribeCommonEventRet = ret; +} + ErrCode NotificationHelper::AllowUseReminder(const std::string& bundleName, bool& isAllowUseReminder) { isAllowUseReminder = g_mockIsAllowUseReminder; @@ -46,4 +60,17 @@ ErrCode NotificationHelper::IsAllowedNotify(const NotificationBundleOption& bund allowed = g_mockIsAllowedNotify; return g_mockIsAllowedNotifyRet; } -} // namespace OHOS::Notification \ No newline at end of file + +ErrCode NotificationHelper::SubscribeNotification(const NotificationSubscriber& subscriber) +{ + return g_mockSubscribeNotificationRet; +} +} + +namespace EventFwk { +bool CommonEventManager::SubscribeCommonEvent(const std::shared_ptr& subscriber) +{ + return g_mockSubscribeCommonEventRet; +} +} +} // namespace OHOS \ No newline at end of file diff --git a/services/reminder/test/unittest/mock/mock_reminder_data_manager.cpp b/services/reminder/test/unittest/mock/mock_reminder_data_manager.cpp index 490b4c737..f83c9f04f 100644 --- a/services/reminder/test/unittest/mock/mock_reminder_data_manager.cpp +++ b/services/reminder/test/unittest/mock/mock_reminder_data_manager.cpp @@ -29,6 +29,24 @@ int32_t g_mockGetExcludeDatesRet = 0; int32_t g_mockQueryActiveReminderCountRet = 0; } +bool MockReminderDataManager::callCancelAllReminders_ = false; +bool MockReminderDataManager::callOnProcessDiedLocked_ = false; +bool MockReminderDataManager::callRefreshRemindersDueToSysTimeChange_ = false; +bool MockReminderDataManager::callOnUserSwitch_ = false; +bool MockReminderDataManager::callOnUserRemove_ = false; +bool MockReminderDataManager::callOnUnlockScreen_ = false; +bool MockReminderDataManager::callShowActiveReminder_ = false; +bool MockReminderDataManager::callTerminateAlerting_ = false; +bool MockReminderDataManager::callCloseReminder_ = false; +bool MockReminderDataManager::callSnoozeReminder_ = false; +bool MockReminderDataManager::callHandleCustomButtonClick_ = false; +bool MockReminderDataManager::callClickReminder_ = false; +bool MockReminderDataManager::callOnBundleMgrServiceStart_ = false; +bool MockReminderDataManager::callOnAbilityMgrServiceStart_ = false; +bool MockReminderDataManager::callOnRemoveAppMgr_ = false; +bool MockReminderDataManager::callHandleAutoDeleteReminder_ = false; +bool MockReminderDataManager::callOnLanguageChanged_ = false; + void MockReminderDataManager::MockPublishReminder(const int32_t ret) { g_mockPublishReminderRet = ret; @@ -69,6 +87,27 @@ void MockReminderDataManager::MockQueryActiveReminderCount(const int32_t ret) g_mockQueryActiveReminderCountRet = ret; } +void MockReminderDataManager::ResetFlag() +{ + callCancelAllReminders_ = false; + callOnProcessDiedLocked_ = false; + callRefreshRemindersDueToSysTimeChange_ = false; + callOnUserSwitch_ = false; + callOnUserRemove_ = false; + callOnUnlockScreen_ = false; + callShowActiveReminder_ = false; + callTerminateAlerting_ = false; + callCloseReminder_ = false; + callSnoozeReminder_ = false; + callHandleCustomButtonClick_ = false; + callClickReminder_ = false; + callOnBundleMgrServiceStart_ = false; + callOnAbilityMgrServiceStart_ = false; + callOnRemoveAppMgr_ = false; + callHandleAutoDeleteReminder_ = false; + callOnLanguageChanged_ = false; +} + std::shared_ptr ReminderDataManager::REMINDER_DATA_MANAGER = nullptr; ReminderDataManager::ReminderDataManager() {} @@ -109,6 +148,7 @@ ErrCode ReminderDataManager::CancelReminder(const int32_t& reminderId, const int ErrCode ReminderDataManager::CancelAllReminders(const std::string& packageName, const int32_t userId, const int32_t uid) { + MockReminderDataManager::callCancelAllReminders_ = true; return g_mockCancelAllRemindersRet; } @@ -137,4 +177,85 @@ int32_t ReminderDataManager::QueryActiveReminderCount() { return g_mockQueryActiveReminderCountRet; } + +void ReminderDataManager::OnProcessDiedLocked(const int32_t callingUid) +{ + MockReminderDataManager::callOnProcessDiedLocked_ = true; +} + +void ReminderDataManager::RefreshRemindersDueToSysTimeChange(uint8_t type) +{ + MockReminderDataManager::callRefreshRemindersDueToSysTimeChange_ = true; +} + +void ReminderDataManager::OnUserSwitch(const int32_t& userId) +{ + MockReminderDataManager::callOnUserSwitch_ = true; +} + +void ReminderDataManager::OnUserRemove(const int32_t& userId) +{ + MockReminderDataManager::callOnUserRemove_ = true; +} + +void ReminderDataManager::OnUnlockScreen() +{ + MockReminderDataManager::callOnUnlockScreen_ = true; +} + +void ReminderDataManager::ShowActiveReminder(const EventFwk::Want& want) +{ + MockReminderDataManager::callShowActiveReminder_ = true; +} + +void ReminderDataManager::TerminateAlerting(const EventFwk::Want& want) +{ + MockReminderDataManager::callTerminateAlerting_ = true; +} + +void ReminderDataManager::CloseReminder(const EventFwk::Want& want, bool cancelNotification, bool isButtonClick) +{ + MockReminderDataManager::callCloseReminder_ = true; +} + +void ReminderDataManager::SnoozeReminder(const EventFwk::Want& want) +{ + MockReminderDataManager::callSnoozeReminder_ = true; +} + +void ReminderDataManager::HandleCustomButtonClick(const EventFwk::Want& want) +{ + MockReminderDataManager::callHandleCustomButtonClick_ = true; +} + +void ReminderDataManager::ClickReminder(const EventFwk::Want& want) +{ + MockReminderDataManager::callClickReminder_ = true; +} + +void ReminderDataManager::OnBundleMgrServiceStart() +{ + MockReminderDataManager::callOnBundleMgrServiceStart_ = true; +} + +void ReminderDataManager::OnAbilityMgrServiceStart() +{ + MockReminderDataManager::callOnAbilityMgrServiceStart_ = true; +} + +void ReminderDataManager::OnRemoveAppMgr() +{ + MockReminderDataManager::callOnRemoveAppMgr_ = true; +} + +void ReminderDataManager::HandleAutoDeleteReminder(const int32_t notificationId, const int32_t uid, + const int64_t autoDeletedTime) +{ + MockReminderDataManager::callHandleAutoDeleteReminder_ = true; +} + +void ReminderDataManager::OnLanguageChanged() +{ + MockReminderDataManager::callOnLanguageChanged_ = true; +} } // namespace OHOS::Notification \ No newline at end of file diff --git a/services/reminder/test/unittest/reminder_agent_service_test.cpp b/services/reminder/test/unittest/reminder_agent_service_test.cpp index d0323d7a1..77fa313a0 100644 --- a/services/reminder/test/unittest/reminder_agent_service_test.cpp +++ b/services/reminder/test/unittest/reminder_agent_service_test.cpp @@ -425,12 +425,12 @@ HWTEST_F(ReminderAgentServiceTest, ReminderAgentServiceTest_015, Function | Smal } /** - * @tc.name: ReminderAgentServiceTest_017 + * @tc.name: ReminderAgentServiceTest_016 * @tc.desc: Test UpdateReminder function * @tc.type: FUNC * @tc.require: issueI5S4VP */ -HWTEST_F(ReminderAgentServiceTest, ReminderAgentServiceTest_017, Function | SmallTest | Level1) +HWTEST_F(ReminderAgentServiceTest, ReminderAgentServiceTest_016, Function | SmallTest | Level1) { // test CreateReminderRequest int32_t reminderId = 0; diff --git a/services/reminder/test/unittest/reminder_event_manager_test.cpp b/services/reminder/test/unittest/reminder_event_manager_test.cpp new file mode 100644 index 000000000..b03a4dc00 --- /dev/null +++ b/services/reminder/test/unittest/reminder_event_manager_test.cpp @@ -0,0 +1,370 @@ +/* + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "reminder_data_manager.h" +#include "reminder_event_manager.h" + +#include "common_event_support.h" +#include "mock_service_registry.h" +#include "mock_notification_helper.h" +#include "mock_reminder_data_manager.h" + +using namespace testing::ext; +namespace OHOS::Notification { +class ReminderEventManagerTest : public testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + void SetUp() {} + void TearDown() {} +}; + +/** + * @tc.name: ReminderEventManagerTest_001 + * @tc.desc: test ReminderEventManager::Init function + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_001, Level1) +{ + ReminderDataManager::InitInstance(); + auto manager = std::make_shared(); + manager->Init(); + EXPECT_TRUE(manager != nullptr); +} + +/** + * @tc.name: ReminderEventManagerTest_002 + * @tc.desc: test ReminderEventManager::SubscribeEvent function + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_002, Level1) +{ + auto manager = std::make_shared(); + MockNotificationHelper::MockSubscribeCommonEvent(false); + manager->SubscribeEvent(); + MockNotificationHelper::MockSubscribeCommonEvent(true); + manager->SubscribeEvent(); + MockNotificationHelper::MockSubscribeNotification(-1); + manager->SubscribeEvent(); + MockNotificationHelper::MockSubscribeNotification(ERR_OK); + manager->SubscribeEvent(); + EXPECT_TRUE(manager != nullptr); +} + +/** + * @tc.name: ReminderEventManagerTest_003 + * @tc.desc: test ReminderEventManager::SubscribeSystemAbility function + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_003, Level1) +{ + auto manager = std::make_shared(); + MockServiceRegistry::MockGetSystemAbilityManager(true); + manager->SubscribeSystemAbility(APP_MGR_SERVICE_ID); + MockServiceRegistry::MockGetSystemAbilityManager(false); + manager->SubscribeSystemAbility(APP_MGR_SERVICE_ID); + EXPECT_TRUE(manager != nullptr); +} + +/** + * @tc.name: ReminderEventManagerTest_004 + * @tc.desc: test ReminderEventSubscriber::OnReceiveEvent function + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_004, Level1) +{ + MockReminderDataManager::ResetFlag(); + EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_TIME_CHANGED); + EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); + auto subscriber = std::make_shared(subscriberInfo); + + EventFwk::CommonEventData data; + EventFwk::Want want; + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); + data.SetWant(want); + auto manager = std::move(ReminderDataManager::REMINDER_DATA_MANAGER); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callCancelAllReminders_ == false); + + ReminderDataManager::REMINDER_DATA_MANAGER = std::move(manager); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callCancelAllReminders_ == true); + + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callRefreshRemindersDueToSysTimeChange_ == true); + + MockReminderDataManager::ResetFlag(); + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callCancelAllReminders_ == true); + + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callOnProcessDiedLocked_ == true); + + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_TIME_CHANGED); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callRefreshRemindersDueToSysTimeChange_ == true); +} + +/** + * @tc.name: ReminderEventManagerTest_005 + * @tc.desc: test ReminderEventSubscriber::OnReceiveEvent function + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_005, Level1) +{ + MockReminderDataManager::ResetFlag(); + EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED); + matchingSkills.AddEvent("common.event.UNLOCK_SCREEN"); + EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); + auto subscriber = std::make_shared(subscriberInfo); + + EventFwk::CommonEventData data; + EventFwk::Want want; + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callOnUserSwitch_ == true); + + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callOnUserRemove_ == true); + + want.SetAction("common.event.UNLOCK_SCREEN"); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callOnUnlockScreen_ == true); + + want.SetAction(ReminderRequest::REMINDER_EVENT_ALARM_ALERT); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callCancelAllReminders_ == false); +} + +/** + * @tc.name: ReminderEventManagerTest_006 + * @tc.desc: test ReminderCustomEventSubscriber::OnReceiveEvent function + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_006, Level1) +{ + MockReminderDataManager::ResetFlag(); + EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_ALARM_ALERT); + matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT); + matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CLOSE_ALERT); + matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT); + EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); + auto subscriber = std::make_shared(subscriberInfo); + + EventFwk::CommonEventData data; + EventFwk::Want want; + want.SetAction(ReminderRequest::REMINDER_EVENT_ALARM_ALERT); + data.SetWant(want); + auto manager = std::move(ReminderDataManager::REMINDER_DATA_MANAGER); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callShowActiveReminder_ == false); + + ReminderDataManager::REMINDER_DATA_MANAGER = std::move(manager); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callShowActiveReminder_ == true); + + want.SetAction(ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callTerminateAlerting_ == true); + + want.SetAction(ReminderRequest::REMINDER_EVENT_CLOSE_ALERT); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callCloseReminder_ == true); + + want.SetAction(ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callSnoozeReminder_ == true); +} + +/** + * @tc.name: ReminderEventManagerTest_007 + * @tc.desc: test ReminderCustomEventSubscriber::OnReceiveEvent function + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_007, Level1) +{ + MockReminderDataManager::ResetFlag(); + EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CUSTOM_ALERT); + matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION); + matchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CLICK_ALERT); + EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); + auto subscriber = std::make_shared(subscriberInfo); + + EventFwk::CommonEventData data; + EventFwk::Want want; + want.SetAction(ReminderRequest::REMINDER_EVENT_CUSTOM_ALERT); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callHandleCustomButtonClick_ == false); + + want.SetAction(ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callCloseReminder_ == true); + + want.SetAction(ReminderRequest::REMINDER_EVENT_CLICK_ALERT); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callClickReminder_ == true); + + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED); + data.SetWant(want); + subscriber->OnReceiveEvent(data); + EXPECT_TRUE(MockReminderDataManager::callShowActiveReminder_ == false); +} + +/** + * @tc.name: ReminderEventManagerTest_008 + * @tc.desc: test SystemAbilityStatusChangeListener + * @tc.type: FUNC + * @tc.require: issue#I9IIDE + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_008, Level1) +{ + MockReminderDataManager::ResetFlag(); + auto listener = std::make_shared(); + // test OnAddSystemAbility + auto manager = std::move(ReminderDataManager::REMINDER_DATA_MANAGER); + listener->OnAddSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, ""); + EXPECT_TRUE(MockReminderDataManager::callOnBundleMgrServiceStart_ == false); + + ReminderDataManager::REMINDER_DATA_MANAGER = std::move(manager); + listener->OnAddSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, ""); + EXPECT_TRUE(MockReminderDataManager::callOnBundleMgrServiceStart_ == true); + + listener->OnAddSystemAbility(APP_MGR_SERVICE_ID, ""); + listener->OnAddSystemAbility(ABILITY_MGR_SERVICE_ID, ""); + listener->OnAddSystemAbility(-1, ""); + EXPECT_TRUE(MockReminderDataManager::callOnAbilityMgrServiceStart_ == true); + + // test OnRemoveSystemAbility + manager = std::move(ReminderDataManager::REMINDER_DATA_MANAGER); + listener->OnRemoveSystemAbility(APP_MGR_SERVICE_ID, ""); + EXPECT_TRUE(MockReminderDataManager::callOnRemoveAppMgr_ == false); + + ReminderDataManager::REMINDER_DATA_MANAGER = std::move(manager); + listener->OnRemoveSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, ""); + listener->OnRemoveSystemAbility(ABILITY_MGR_SERVICE_ID, ""); + listener->OnRemoveSystemAbility(APP_MGR_SERVICE_ID, ""); + listener->OnRemoveSystemAbility(-1, ""); + EXPECT_TRUE(MockReminderDataManager::callOnRemoveAppMgr_ == true); +} + +/** + * @tc.name: ReminderEventManagerTest_009 + * @tc.desc: test ReminderNotificationSubscriber::OnCanceled test + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_009, Level1) +{ + MockReminderDataManager::ResetFlag(); + ReminderEventManager::ReminderNotificationSubscriber subscriber; + // test deleteReason + subscriber.OnCanceled(nullptr, nullptr, NotificationConstant::PACKAGE_REMOVE_REASON_DELETE); + EXPECT_TRUE(MockReminderDataManager::callHandleAutoDeleteReminder_ == false); + + // test notification + subscriber.OnCanceled(nullptr, nullptr, NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE); + EXPECT_TRUE(MockReminderDataManager::callHandleAutoDeleteReminder_ == false); + + // test autoDeletedTime + sptr request = new NotificationRequest(); + std::shared_ptr notification = std::make_shared(request); + subscriber.OnCanceled(notification, nullptr, NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE); + EXPECT_TRUE(MockReminderDataManager::callHandleAutoDeleteReminder_ == false); + + request->SetAutoDeletedTime(100); + // test label + subscriber.OnCanceled(notification, nullptr, NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE); + EXPECT_TRUE(MockReminderDataManager::callHandleAutoDeleteReminder_ == false); + + request->SetLabel("REMINDER_AGENT"); + // test manager + auto manager = std::move(ReminderDataManager::REMINDER_DATA_MANAGER); + subscriber.OnCanceled(notification, nullptr, NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE); + EXPECT_TRUE(MockReminderDataManager::callHandleAutoDeleteReminder_ == false); + + ReminderDataManager::REMINDER_DATA_MANAGER = std::move(manager); + subscriber.OnCanceled(notification, nullptr, NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE); + EXPECT_TRUE(MockReminderDataManager::callHandleAutoDeleteReminder_ == true); +} + +/** + * @tc.name: ReminderEventManagerTest_010 + * @tc.desc: test ReminderConfigChangeObserver::OnConfigurationUpdated function + * @tc.type: FUNC + * @tc.require: issueI5YTF3 + */ +HWTEST_F(ReminderEventManagerTest, ReminderEventManagerTest_010, Level1) +{ + MockReminderDataManager::ResetFlag(); + ReminderConfigChangeObserver observer; + AppExecFwk::Configuration config; + // test manager == nullptr + auto manager = std::move(ReminderDataManager::REMINDER_DATA_MANAGER); + observer.OnConfigurationUpdated(config); + EXPECT_TRUE(MockReminderDataManager::callOnLanguageChanged_ == false); + + ReminderDataManager::REMINDER_DATA_MANAGER = std::move(manager); + // test languageInfo + config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, ""); + observer.OnConfigurationUpdated(config); + EXPECT_TRUE(MockReminderDataManager::callOnLanguageChanged_ == false); + + config.RemoveItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE); + config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, "test"); + observer.languageInfo_ = "test"; + observer.OnConfigurationUpdated(config); + EXPECT_TRUE(MockReminderDataManager::callOnLanguageChanged_ == false); + + observer.languageInfo_ = "1111"; + observer.OnConfigurationUpdated(config); + EXPECT_TRUE(MockReminderDataManager::callOnLanguageChanged_ == true); +} +} // namespace OHOS::Notification \ No newline at end of file diff --git a/test/fuzztest/remindereventmanager_fuzzer/BUILD.gn b/test/fuzztest/remindereventmanager_fuzzer/BUILD.gn new file mode 100644 index 000000000..e70423a59 --- /dev/null +++ b/test/fuzztest/remindereventmanager_fuzzer/BUILD.gn @@ -0,0 +1,66 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#####################hydra-fuzz################### +import("//base/notification/distributed_notification_service/notification.gni") +import("//build/config/features.gni") +import("//build/test.gni") + +##############################fuzztest########################################## +ohos_fuzztest("ReminderEventManagerFuzzTest") { + module_out_path = service_fuzz_test_path + fuzz_config_file = + "${component_path}/test/fuzztest/remindereventmanager_fuzzer" + + include_dirs = [ + "${component_path}/test/fuzztest/fuzz_common_base", + "${component_path}/services/reminder/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + "-Dprivate=public", + "-Dprotected=public", + ] + sources = [ "remindereventmanager_fuzzer.cpp" ] + + deps = [ + "${component_path}/test/fuzztest/fuzz_common_base:fuzz_common_base", + "${frameworks_module_reminder_path}:reminder_innerkits", + "${services_path}/reminder:libreminder_static", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "data_share:datashare_common", + "ffrt:libffrt", + "hilog:libhilog", + "image_framework:image_native", + "kv_store:distributeddata_inner", + "relational_store:native_rdb", + "time_service:time_client", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [ ":ReminderEventManagerFuzzTest" ] +} +############################################################################### diff --git a/test/fuzztest/remindereventmanager_fuzzer/corpus/init b/test/fuzztest/remindereventmanager_fuzzer/corpus/init new file mode 100644 index 000000000..f7880ef1a --- /dev/null +++ b/test/fuzztest/remindereventmanager_fuzzer/corpus/init @@ -0,0 +1,13 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +FUZZ \ No newline at end of file diff --git a/test/fuzztest/remindereventmanager_fuzzer/project.xml b/test/fuzztest/remindereventmanager_fuzzer/project.xml new file mode 100644 index 000000000..7133b2b92 --- /dev/null +++ b/test/fuzztest/remindereventmanager_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/remindereventmanager_fuzzer/remindereventmanager_fuzzer.cpp b/test/fuzztest/remindereventmanager_fuzzer/remindereventmanager_fuzzer.cpp new file mode 100644 index 000000000..b3feed457 --- /dev/null +++ b/test/fuzztest/remindereventmanager_fuzzer/remindereventmanager_fuzzer.cpp @@ -0,0 +1,241 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "reminder_data_manager.h" +#include "reminder_request_timer.h" +#include "reminderdatamanager_fuzzer.h" +#include + +namespace OHOS { + bool DoSomethingInterestingWithManager(FuzzedDataProvider* fdp) + { + std::string bundleName = fdp->ConsumeRandomLengthString(); + int32_t userId = fdp->ConsumeIntegral(); + int32_t uid = fdp->ConsumeIntegral(); + int32_t reminderId = fdp->ConsumeIntegral(); + uint64_t date = fdp->ConsumeIntegral(); + bool value = fdp->ConsumeBool(); + uint8_t type = fdp->ConsumeIntegral(); + int32_t callingUid = fdp->ConsumeIntegral(); + EventFwk::Want want; + constexpr uint64_t seconds = 1200; + sptr reminder = new Notification::ReminderRequestTimer(seconds); + + Notification::ReminderDataManager::InitInstance(); + auto manager = Notification::ReminderDataManager::GetInstance(); + manager->RegisterConfigurationObserver(); + manager->Init(); + manager->Dump(); + manager->CancelAllReminders(bundleName, userId, uid); + manager->CancelReminder(reminderId, callingUid); + manager->CheckExcludeDateParam(reminderId, callingUid); + manager->AddExcludeDate(reminderId, date, callingUid); + manager->DelExcludeDates(reminderId, callingUid); + + std::vector dates; + manager->GetExcludeDates(reminderId, callingUid, dates); + manager->CloseReminder(want, value); + std::vector reminders; + manager->GetValidReminders(callingUid, reminders); + manager->Init(); + manager->InitUserId(); + std::vector> immediatelyReminders; + std::vector> extensionReminders; + manager->CheckReminderTime(immediatelyReminders, extensionReminders); + + manager->RegisterConfigurationObserver(); + manager->OnUserRemove(userId); + manager->OnBundleMgrServiceStart(); + manager->OnAbilityMgrServiceStart(); + manager->OnUserSwitch(userId); + manager->OnProcessDiedLocked(callingUid); + manager->RefreshRemindersDueToSysTimeChange(type); + manager->ShouldAlert(reminder); + manager->ShowActiveReminder(want); + manager->SnoozeReminder(want); + + manager->HandleCustomButtonClick(want); + manager->ClickReminder(want); + manager->TerminateAlerting(want); + return true; + } + + bool DoSomethingInterestingWithReminder(FuzzedDataProvider* fdp) + { + std::string bundleName = fdp->ConsumeRandomLengthString(); + int32_t userId = fdp->ConsumeIntegral(); + int32_t uid = fdp->ConsumeIntegral(); + int32_t reminderId = fdp->ConsumeIntegral(); + bool value = fdp->ConsumeBool(); + int32_t callingUid = fdp->ConsumeIntegral(); + constexpr uint64_t seconds = 1200; + sptr reminder = new Notification::ReminderRequestTimer(seconds); + auto manager = Notification::ReminderDataManager::GetInstance(); + + manager->OnLanguageChanged(); + manager->OnRemoveAppMgr(); + manager->CancelAllReminders(userId); + manager->CheckUpdateConditions(reminder, Notification::ReminderRequest::ActionButtonType::INVALID, + reminder->GetActionButtons()); + manager->CancelRemindersImplLocked(bundleName, userId, uid, value); + manager->CloseRemindersByGroupId(reminderId, bundleName, bundleName); + manager->CancelNotification(reminder); + manager->CheckReminderLimitExceededLocked(callingUid, reminder); + std::vector> reminders; + manager->GetImmediatelyShowRemindersLocked(reminders); + manager->AddToShowedReminders(reminder); + + manager->IsAllowedNotify(reminder); + manager->PlaySoundAndVibrationLocked(reminder); + manager->PlaySoundAndVibration(reminder); + manager->StopSoundAndVibrationLocked(reminder); + manager->StopSoundAndVibration(reminder); + manager->RemoveFromShowedReminders(reminder); + manager->RemoveReminderLocked(reminderId, false); + manager->SetActiveReminder(reminder); + manager->SetAlertingReminder(reminder); + manager->ShowActiveReminderExtendLocked(reminder, reminders); + + std::vector> extensionReminders; + std::vector> immediatelyReminders; + manager->PublishReminder(reminder, callingUid); + manager->FindReminderRequestLocked(reminderId, false); + manager->StartRecentReminder(); + manager->HandleImmediatelyShow(immediatelyReminders, value); + int8_t type = fdp->ConsumeIntegral(); + manager->HandleExtensionReminder(extensionReminders, type); + manager->HandleSameNotificationIdShowing(reminder); + manager->IsBelongToSameApp(uid, uid); + manager->CheckIsSameApp(reminder, uid); + manager->ShowReminder(reminder, value, value, value, value); + return true; + } + + bool DoSomethingInteresting1(FuzzedDataProvider* fdp) + { + std::string bundleName = fdp->ConsumeRandomLengthString(); + int32_t id = fdp->ConsumeIntegral(); + int64_t ts = fdp->ConsumeIntegral(); + int32_t reminderId = fdp->ConsumeIntegral(); + bool value = fdp->ConsumeBool(); + int32_t callingUid = fdp->ConsumeIntegral(); + constexpr uint64_t seconds = 1200; + sptr reminder = new Notification::ReminderRequestTimer(seconds); + auto manager = Notification::ReminderDataManager::GetInstance(); + manager->OnUnlockScreen(); + manager->OnLoadReminderEvent(); + manager->OnLoadReminderInFfrt(); + manager->OnDataShareInsertOrDelete(); + std::map> remindersMap; + manager->OnDataShareUpdate(remindersMap); + manager->HandleAutoDeleteReminder(id, id, ts); + + std::vector> reminders; + manager->UpdateReminderLanguageLocked(id, reminders); + manager->IsSystemReady(); + manager->QueryActiveReminderCount(); + manager->StartLoadTimer(); + manager->InitShareReminders(value); + manager->UpdateAppDatabase(reminder, Notification::ReminderRequest::ActionButtonType::CLOSE); + DataShare::DataSharePredicates predicates; + std::vector equalToVector; + manager->GenPredicates(predicates, equalToVector); + DataShare::DataShareValuesBucket valuesBucket; + manager->GenValuesBucket(valuesBucket, equalToVector); + manager->CloseReminder(reminder, value); + Notification::ReminderDataManager::TimerType t = Notification::ReminderDataManager::TimerType::ALERTING_TIMER; + manager->CreateTimerInfo(t, reminder); + auto sharedTimerInfo = std::make_shared(); + manager->InitTimerInfo(sharedTimerInfo, reminder, t); + manager->GetRecentReminder(); + manager->HandleSysTimeChange(reminder); + manager->IsMatched(reminder, id, id, value); + manager->ResetStates(t); + return true; + } + + bool DoSomethingInteresting2(FuzzedDataProvider* fdp) + { + constexpr uint16_t MAX_SECOND = 5; + std::string bundleName = fdp->ConsumeRandomLengthString(); + std::string uri = fdp->ConsumeRandomLengthString(); + int32_t id = fdp->ConsumeIntegral(); + int8_t val = fdp->ConsumeIntegral(); + uint16_t second = fdp->ConsumeIntegral() % MAX_SECOND + 1; + int64_t ts = fdp->ConsumeIntegral(); + int32_t reminderId = fdp->ConsumeIntegral(); + bool value = fdp->ConsumeBool(); + int32_t callingUid = fdp->ConsumeIntegral(); + constexpr uint64_t seconds = 1200; + sptr reminder = new Notification::ReminderRequestTimer(seconds); + auto manager = Notification::ReminderDataManager::GetInstance(); + manager->IsMatchedForGroupIdAndPkgName(reminder, bundleName, uri); + manager->IsReminderAgentReady(); + manager->StartExtensionAbility(reminder, 0); + int32_t count = 0; + manager->AsyncStartExtensionAbility(reminder, id, val, count); + manager->InitServiceHandler(); + manager->SnoozeReminderImpl(reminder); + Notification::ReminderDataManager::TimerType t = Notification::ReminderDataManager::TimerType::ALERTING_TIMER; + manager->StartTimerLocked(reminder, t); + manager->StartTimer(reminder, t); + manager->StopAlertingReminder(reminder); + manager->StopTimer(t); + manager->StopTimerLocked(t); + manager->TerminateAlerting(reminder, uri); + manager->TerminateAlerting(second, reminder); + manager->UpdateAndSaveReminderLocked(reminder); + Notification::ReminderDataManager::cmp(reminder, reminder); + manager->ConnectAppMgr(); + manager->CheckNeedNotifyStatus(reminder, Notification::ReminderRequest::ActionButtonType::INVALID); + manager->GetFullPath(uri); + manager->IsActionButtonDataShareValid(reminder, static_cast(id)); + manager->GetResourceMgr(bundleName, id); + Global::Resource::ResourceManager::RawFileDescriptor desc; + manager->GetCustomRingFileDesc(reminder, desc); + manager->CloseCustomRingFileDesc(id, uri); + manager->ReportSysEvent(reminder); + std::map> reminders; + manager->UpdateShareReminders(reminders); + std::unordered_map limits; + int32_t totalCount = 0; + manager->CheckShowLimit(limits, totalCount, reminder); + manager->LoadShareReminders(); + manager->LoadReminderFromDb(); + return true; + } + + bool Clear() + { + auto manager = Notification::ReminderDataManager::GetInstance(); + if (manager->queue_ != nullptr) { + auto handler = manager->queue_->submit_h(std::bind([]() {})); + manager->queue_->wait(handler); + } + return true; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + FuzzedDataProvider fdp(data, size); + OHOS::DoSomethingInterestingWithManager(&fdp); + OHOS::DoSomethingInterestingWithReminder(&fdp); + OHOS::DoSomethingInteresting1(&fdp); + OHOS::DoSomethingInteresting2(&fdp); + return 0; +} diff --git a/services/reminder/include/reminder_config_change_observer.h b/test/fuzztest/remindereventmanager_fuzzer/remindereventmanager_fuzzer.h similarity index 39% rename from services/reminder/include/reminder_config_change_observer.h rename to test/fuzztest/remindereventmanager_fuzzer/remindereventmanager_fuzzer.h index 982b7ddb1..8c3569942 100644 --- a/services/reminder/include/reminder_config_change_observer.h +++ b/test/fuzztest/remindereventmanager_fuzzer/remindereventmanager_fuzzer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -13,30 +13,11 @@ * limitations under the License. */ -#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_CONFIG_CHANGE_OBSERVER_H -#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_REMINDER_INCLUDE_REMINDER_CONFIG_CHANGE_OBSERVER_H +#ifndef TEST_FUZZTEST_REMINDERDATAMANAGER_FUZZER_REMINDEREVENTMANAGER_FUZZER_H +#define TEST_FUZZTEST_REMINDERDATAMANAGER_FUZZER_REMINDEREVENTMANAGER_FUZZER_H -#include "configuration_observer_stub.h" +#include "fuzz_common_base.h" -namespace OHOS { -namespace Notification { - -/** - * @brief Listening system language change, when the system language changes, - * notify ReminderDataManager. -*/ -class ReminderConfigChangeObserver final : public AppExecFwk::ConfigurationObserverStub { -public: - ReminderConfigChangeObserver() = default; - ~ReminderConfigChangeObserver() = default; +#define FUZZ_PROJECT_NAME "remindereventmanager_fuzzer" -public: - void OnConfigurationUpdated(const AppExecFwk::Configuration &configuration) override; - -private: - std::string languageInfo_; -}; -} // namespace Notification -} // namespace OHOS - -#endif \ No newline at end of file +#endif // TEST_FUZZTEST_REMINDERDATAMANAGER_FUZZER_REMINDEREVENTMANAGER_FUZZER_H \ No newline at end of file -- Gitee