diff --git a/test/fuzztest/notificationhelper_fuzzer/BUILD.gn b/test/fuzztest/notificationhelper_fuzzer/BUILD.gn index 7f6fc5250db971940fcabfb3bd11ec32fc3f989b..913fc7539e71475daab867b5e1ba647cd1fe11a8 100644 --- a/test/fuzztest/notificationhelper_fuzzer/BUILD.gn +++ b/test/fuzztest/notificationhelper_fuzzer/BUILD.gn @@ -21,7 +21,10 @@ ohos_fuzztest("NotificationHelperFuzzTest") { module_out_path = service_fuzz_test_path fuzz_config_file = "${component_path}/test/fuzztest/notificationhelper_fuzzer" - include_dirs = [ "${component_path}/test/fuzztest/fuzz_common_base" ] + include_dirs = [ + "${component_path}/test/fuzztest/fuzz_common_base", + "${component_path}/test/fuzztest/fuzz_common_base/mock", + ] cflags = [ "-g", "-O0", diff --git a/test/fuzztest/notificationhelper_fuzzer/notificationhelper_fuzzer.cpp b/test/fuzztest/notificationhelper_fuzzer/notificationhelper_fuzzer.cpp index ff6f623f54e737fd5e96d0362168e66781813e49..45dfd1ea9859e68efa61bb87d3daede9f04e8496 100644 --- a/test/fuzztest/notificationhelper_fuzzer/notificationhelper_fuzzer.cpp +++ b/test/fuzztest/notificationhelper_fuzzer/notificationhelper_fuzzer.cpp @@ -20,42 +20,339 @@ #undef protected #include "notificationhelper_fuzzer.h" #include +#include "ans_permission_def.h" +#include "notification_subscriber.h" +#include "notification_button_option.h" +#include "mock_notification_subscribe_info.h" +#include "ans_dialog_host_client.h" +#include "mock_notification_donotdisturb_profile.h" +#include "notification_disable.h" +#include "mock_notification_operation_info.h" namespace OHOS { +namespace Notification { - bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider *fdp) +class FuzzNotificationSubscriber : public NotificationSubscriber { +public: + void OnDisconnected() override + {} + void OnDied() override + {} + void OnUpdate(const std::shared_ptr &sortingMap) override + {} + void OnDoNotDisturbDateChange(const std::shared_ptr &date) override + {} + void OnConnected() override + {} + void OnEnabledNotificationChanged(const std::shared_ptr &callbackData) override + {} + void OnCanceled(const std::shared_ptr &request, + const std::shared_ptr &sortingMap, int deleteReason) override + {} + void OnBadgeChanged(const std::shared_ptr &badgeData) override + {} + void OnBadgeEnabledChanged(const sptr &callbackData) override + {} + void OnConsumed(const std::shared_ptr &request, + const std::shared_ptr &sortingMap) override + {} + + void OnBatchCanceled(const std::vector> &requestList, + const std::shared_ptr &sortingMap, int32_t deleteReason) override + {} +}; + +class FuzzTestLocalLiveViewSubscriber : public NotificationLocalLiveViewSubscriber { +public: + void OnConnected() override + {} + void OnDisconnected() override + {} + void OnDied() override + {} + void OnResponse(int32_t notificationId, sptr buttonOption) override + {} +}; + + bool TestPublishAndRemove(FuzzedDataProvider* fdp, NotificationHelper& notificationHelper) { - Notification::NotificationHelper notificationHelper; std::string stringData = fdp->ConsumeRandomLengthString(); int32_t intData = fdp->ConsumeIntegral(); - // test IsSoundEnabled function + std::string representativeBundle = stringData; - Notification::NotificationRequest notification; + NotificationRequest notification; notification.SetOwnerUid(intData); notification.SetCreatorUid(intData); - notification.SetSlotType(Notification::NotificationConstant::SlotType::LIVE_VIEW); - auto content = std::make_shared(); - notification.SetContent(std::make_shared(content)); + notification.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW); + auto content = std::make_shared(); + notification.SetContent(std::make_shared(content)); notificationHelper.PublishNotificationAsBundle(representativeBundle, notification); notificationHelper.RemoveNotifications(); + std::vector hashCodes; + hashCodes.emplace_back(fdp->ConsumeRandomLengthString()); + notificationHelper.RemoveNotifications(hashCodes, fdp->ConsumeIntegral()); + + return true; + } + + bool TestBundleOperations(FuzzedDataProvider* fdp, NotificationHelper& notificationHelper) + { + std::string stringData = fdp->ConsumeRandomLengthString(); + int32_t intData = fdp->ConsumeIntegral(); bool enabled = fdp->ConsumeBool(); notificationHelper.SetNotificationsEnabledForAllBundles(stringData, enabled); - Notification::NotificationBundleOption bundleOption; + + NotificationBundleOption bundleOption; bundleOption.SetBundleName(stringData); bundleOption.SetUid(intData); + uint32_t flag = 0; notificationHelper.GetNotificationSlotFlagsAsBundle(bundleOption, flag); notificationHelper.SetNotificationSlotFlagsAsBundle(bundleOption, intData); notificationHelper.CancelAsBundle(bundleOption, intData); + NotificationButtonOption buttonOption; + notificationHelper.TriggerLocalLiveView(bundleOption, fdp->ConsumeIntegral(), buttonOption); + return true; + } + + bool TestNotificationSettings(FuzzedDataProvider* fdp, NotificationHelper& notificationHelper) + { + uint32_t slotFlags = fdp->ConsumeIntegral(); + notificationHelper.GetNotificationSettings(slotFlags); + + NotificationRequest notification; + notificationHelper.PublishNotificationForIndirectProxy(notification); + + bool canPop = fdp->ConsumeBool(); + + std::string stringData = fdp->ConsumeRandomLengthString(); + sptr client = nullptr; + AnsDialogHostClient::CreateIfNullptr(client); + client = AnsDialogHostClient::GetInstance(); + notificationHelper.CanPopEnableNotificationDialog(client, canPop, stringData); + notificationHelper.RemoveEnableNotificationDialog(); + int32_t uid = fdp->ConsumeIntegralInRange(0, 100); + notificationHelper.RequestEnableNotification(stringData, uid); + sptr callerToken = nullptr; + sptr notificationCheckRequest = + new NotificationCheckRequest(); + notificationHelper.RegisterPushCallback(callerToken, notificationCheckRequest); + notificationHelper.UnregisterPushCallback(); + return true; + } + + bool TestSubscriptionOperations(FuzzedDataProvider* fdp, NotificationHelper& notificationHelper) + { + FuzzNotificationSubscriber fuzzNotificationSub; + std::shared_ptr fuzzNotificationSubSptr = + std::make_shared(); + + notificationHelper.SubscribeNotification(fuzzNotificationSub); + notificationHelper.SubscribeNotification(fuzzNotificationSubSptr); + notificationHelper.SubscribeNotificationSelf(fuzzNotificationSub); + notificationHelper.SubscribeNotificationSelf(fuzzNotificationSubSptr); + + FuzzTestLocalLiveViewSubscriber fuzzLocalLiveViewSubscriber; + notificationHelper.SubscribeLocalLiveViewNotification(fuzzLocalLiveViewSubscriber, fdp->ConsumeBool()); + + sptr fuzzNotificationSubInfoSptr = + ObjectBuilder::Build(fdp); + notificationHelper.SubscribeNotification(fuzzNotificationSubSptr, fuzzNotificationSubInfoSptr); + + notificationHelper.UnSubscribeNotification(fuzzNotificationSub); + notificationHelper.UnSubscribeNotification(fuzzNotificationSubSptr); + notificationHelper.UnSubscribeNotification(fuzzNotificationSubSptr, fuzzNotificationSubInfoSptr); + return true; + } + + bool TestSlotConfiguration(FuzzedDataProvider* fdp, NotificationHelper& notificationHelper) + { + NotificationBundleOption bundleOption; + constexpr uint8_t SLOT_LEVEL_NUM = 6; + constexpr uint8_t SLOT_VISIBLENESS_TYPE_NUM = 4; + constexpr uint8_t SLOT_TYPE_NUM = 5; + + sptr slot = new NotificationSlot(); + slot->SetDescription(fdp->ConsumeRandomLengthString()); + slot->SetEnableLight(fdp->ConsumeBool()); + slot->SetEnableVibration(fdp->ConsumeBool()); + slot->SetLedLightColor(fdp->ConsumeIntegral()); + + uint8_t level = fdp->ConsumeIntegral() % SLOT_LEVEL_NUM; + slot->SetLevel(NotificationSlot::NotificationLevel(level)); + + uint8_t visibleness = fdp->ConsumeIntegral() % SLOT_VISIBLENESS_TYPE_NUM; + slot->SetLockscreenVisibleness(NotificationConstant::VisiblenessType(visibleness)); + + uint8_t type = fdp->ConsumeIntegral() % SLOT_TYPE_NUM; + NotificationConstant::SlotType slotType = NotificationConstant::SlotType(type); + slot->SetType(slotType); + + std::vector slots; + slots.emplace_back(*slot); + + notificationHelper.GetNotificationSlotForBundle(bundleOption, slotType, slot); + + std::vector> notifications; + notificationHelper.GetAllNotificationsBySlotType(notifications, slotType); return true; } + + bool TestDoNotDisturb(FuzzedDataProvider* fdp, NotificationHelper& notificationHelper) + { + std::vector> profiles; + size_t numProfiles = fdp->ConsumeIntegralInRange(1, 6); + for (size_t i = 0; i < numProfiles; ++i) { + sptr profile = + ObjectBuilder::Build(fdp); + profiles.push_back(profile); + } + notificationHelper.AddDoNotDisturbProfiles(profiles); + notificationHelper.RemoveDoNotDisturbProfiles(profiles); + + notificationHelper.IsNeedSilentInDoNotDisturbMode(fdp->ConsumeRandomLengthString(), + fdp->ConsumeIntegral()); + return true; + } + + bool TestBadgeOperations(FuzzedDataProvider* fdp, NotificationHelper& notificationHelper) + { + NotificationBundleOption bundleOption; + bool enabled = fdp->ConsumeBool(); + int32_t uid = fdp->ConsumeIntegralInRange(0, 100); + notificationHelper.SetNotificationsEnabledForAllBundles(uid, enabled); + + NotificationConstant::SlotType slotType = NotificationConstant::SlotType(fdp->ConsumeIntegral() % 5); + notificationHelper.GetEnabledForBundleSlotSelf(slotType, enabled); + + int32_t badgeNumber = fdp->ConsumeIntegralInRange(0, 100); + std::string bundleName = fdp->ConsumeRandomLengthString(); + notificationHelper.SetBadgeNumber(badgeNumber, bundleName); + notificationHelper.SetBadgeNumberByBundle(bundleOption, badgeNumber); + notificationHelper.SetBadgeNumberForDhByBundle(bundleOption, badgeNumber); + + std::vector bundleOptions; + bundleOptions.push_back(bundleOption); + notificationHelper.GetAllNotificationEnabledBundles(bundleOptions); + return true; + } + + bool TestDistributedOperations(FuzzedDataProvider* fdp, NotificationHelper& notificationHelper) + { + NotificationBundleOption bundleOption; + std::string deviceType = fdp->ConsumeRandomLengthString(); + std::string deviceId = fdp->ConsumeRandomLengthString(); + bool enabled = fdp->ConsumeBool(); + int32_t uid = fdp->ConsumeIntegralInRange(0, 100); + + notificationHelper.SetDistributedEnabledByBundle(bundleOption, deviceType, enabled); + notificationHelper.IsDistributedEnabledByBundle(bundleOption, deviceType, enabled); + notificationHelper.SetDistributedEnabled(deviceType, enabled); + notificationHelper.IsDistributedEnabled(deviceType, enabled); + + int32_t abilityId = fdp->ConsumeIntegralInRange(0, 100); + notificationHelper.GetDistributedAbility(abilityId); + + bool isAuth = fdp->ConsumeBool(); + notificationHelper.GetDistributedAuthStatus(deviceType, deviceId, uid, isAuth); + notificationHelper.SetDistributedAuthStatus(deviceType, deviceId, uid, isAuth); + + notificationHelper.SetSmartReminderEnabled(deviceType, enabled); + notificationHelper.IsSmartReminderEnabled(deviceType, enabled); + + NotificationConstant::SlotType slotType = NotificationConstant::SlotType(fdp->ConsumeIntegral() % 5); + notificationHelper.SetDistributedEnabledBySlot(slotType, deviceType, enabled); + notificationHelper.IsDistributedEnabledBySlot(slotType, deviceType, enabled); + notificationHelper.CancelAsBundleWithAgent(bundleOption, uid); + notificationHelper.SetHashCodeRule(fdp->ConsumeIntegral()); + return true; + } + + bool TestAdvancedOperations(FuzzedDataProvider* fdp, NotificationHelper& notificationHelper) + { + std::string key = fdp->ConsumeRandomLengthString(); + std::string val = fdp->ConsumeRandomLengthString(); + notificationHelper.SetAdditionConfig(key, val); + + std::string deviceType = fdp->ConsumeRandomLengthString(); + std::string deviceId = fdp->ConsumeRandomLengthString(); + uint32_t status = fdp->ConsumeIntegral(); + uint32_t controlFlag = fdp->ConsumeIntegral(); + int32_t uid = fdp->ConsumeIntegralInRange(0, 100); + + notificationHelper.SetTargetDeviceStatus(deviceType, status, deviceId); + notificationHelper.SetTargetDeviceStatus(deviceType, status, controlFlag, deviceId, uid); + + int32_t operateType = fdp->ConsumeIntegralInRange(0, 2); + std::vector bundleList; + bundleList.emplace_back(fdp->ConsumeRandomLengthString()); + notificationHelper.SetTargetDeviceBundleList(deviceType, deviceId, operateType, bundleList); + + notificationHelper.SetTargetDeviceSwitch(deviceType, deviceId, + fdp->ConsumeBool(), fdp->ConsumeBool()); + + sptr disturbProfile = + ObjectBuilder::Build(fdp); + notificationHelper.GetDoNotDisturbProfile(uid, disturbProfile); + + sptr operationInfo = ObjectBuilder::Build(fdp); + notificationHelper.DistributeOperation(operationInfo, nullptr); + + std::string hashCode = fdp->ConsumeRandomLengthString(); + sptr notificationRequest; + LiveViewFilter filter; + notificationHelper.GetNotificationRequestByHashCode(hashCode, notificationRequest); + notificationHelper.GetActiveNotificationByFilter(filter, notificationRequest); + + int32_t result = fdp->ConsumeIntegralInRange(0, 100); + notificationHelper.ReplyDistributeOperation(hashCode, result); + + bool isPaused = fdp->ConsumeBool(); + notificationHelper.UpdateNotificationTimerByUid(uid, isPaused); + + NotificationDisable notificationDisable; + notificationHelper.DisableNotificationFeature(notificationDisable); + + std::string bundleName = fdp->ConsumeRandomLengthString(); + bool isAllowUseReminder = fdp->ConsumeBool(); + notificationHelper.AllowUseReminder(bundleName, isAllowUseReminder); + + std::vector bundleOptions; + notificationHelper.GetAllLiveViewEnabledBundles(bundleOptions); + notificationHelper.GetAllDistribuedEnabledBundles(deviceType, bundleOptions); + return true; + } + + bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider *fdp) + { + NotificationHelper notificationHelper; + + TestPublishAndRemove(fdp, notificationHelper); + TestBundleOperations(fdp, notificationHelper); + TestNotificationSettings(fdp, notificationHelper); + TestSubscriptionOperations(fdp, notificationHelper); + TestSlotConfiguration(fdp, notificationHelper); + TestDoNotDisturb(fdp, notificationHelper); + TestBadgeOperations(fdp, notificationHelper); + TestDistributedOperations(fdp, notificationHelper); + TestAdvancedOperations(fdp, notificationHelper); + + return true; + } +} } /* Fuzzer entry point */ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { FuzzedDataProvider fdp(data, size); - OHOS::DoSomethingInterestingWithMyAPI(&fdp); + std::vector requestPermission = { + OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_CONTROLLER, + OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER, + OHOS::Notification::OHOS_PERMISSION_SET_UNREMOVABLE_NOTIFICATION + }; + MockRandomToken(&fdp, requestPermission); + OHOS::Notification::DoSomethingInterestingWithMyAPI(&fdp); return 0; }