From 1b55514eed702715028bb9cad280fa096c601950 Mon Sep 17 00:00:00 2001 From: Ricky Date: Sun, 21 Jul 2024 14:13:21 +0800 Subject: [PATCH] add fuzz coverage Signed-off-by: dongqingran --- .../ansmanagerstub_fuzzer.cpp | 176 +++++++++++++++++- .../notificationhelper_fuzzer.cpp | 40 ++-- 2 files changed, 199 insertions(+), 17 deletions(-) diff --git a/test/fuzztest/ansmanagerstub_fuzzer/ansmanagerstub_fuzzer.cpp b/test/fuzztest/ansmanagerstub_fuzzer/ansmanagerstub_fuzzer.cpp index 84d5072e1..0aa76790c 100644 --- a/test/fuzztest/ansmanagerstub_fuzzer/ansmanagerstub_fuzzer.cpp +++ b/test/fuzztest/ansmanagerstub_fuzzer/ansmanagerstub_fuzzer.cpp @@ -23,7 +23,9 @@ #include #define private public #define protected public +#include "ans_manager_proxy.h" #include "ans_manager_stub.h" +#include "ans_subscriber_stub.h" #undef private #undef protected #include "ansmanagerstub_fuzzer.h" @@ -36,6 +38,8 @@ namespace OHOS { bool DoSomethingInterestingWithMyAPI(FuzzData fuzzData) { std::string stringData = fuzzData.GenerateRandomString(); + int32_t intData = fuzzData.GenerateRandomInt32(); + bool boolData = fuzzData.GenerateRandomBool(); Notification::AnsManagerStub ansManagerStub; MessageParcel datas; MessageParcel reply; @@ -43,101 +47,270 @@ namespace OHOS { ansManagerStub.OnRemoteRequest(0, datas, reply, flags); ansManagerStub.OnRemoteRequest((int) Notification::NotificationInterfaceCode::PUBLISH_NOTIFICATION, datas, reply, flags); + sptr notification = new Notification::NotificationRequest(); + notification->SetOwnerUid(fuzzData.GenerateRandomInt32()); + notification->SetCreatorUid(fuzzData.GenerateRandomInt32()); + notification->SetSlotType(Notification::NotificationConstant::SlotType::LIVE_VIEW); + auto content = std::make_shared(); + notification->SetContent(std::make_shared(content)); + + sptr bundleOption = new Notification::NotificationBundleOption(); + bundleOption->SetBundleName(fuzzData.GenerateRandomString()); + bundleOption->SetUid(fuzzData.GenerateRandomInt32()); + + sptr buttonOption = new Notification::NotificationButtonOption(); + + sptr subscriber = new Notification::AnsSubscriberStub(); + sptr info = new Notification::NotificationSubscribeInfo(); + + sptr slot = new Notification::NotificationSlot(); + std::vector> slots {slot}; + + uint8_t type = fuzzData.GetData() % SLOT_TYPE_NUM; + + sptr distribuDate = new Notification::NotificationDoNotDisturbDate(); + + datas.WriteString(stringData); + datas.WriteParcelable(notification); ansManagerStub.HandlePublish(datas, reply); + datas.WriteInt32(intData); + datas.WriteString(stringData); + datas.WriteInt32(intData); ansManagerStub.HandleCancelAsBundle(datas, reply); + datas.WriteParcelable(bundleOption); + datas.WriteInt32(intData); + ansManagerStub.HandleCancelAsBundleOption(datas, reply); + datas.WriteInt32(intData); ansManagerStub.HandleCancelAll(datas, reply); + datas.WriteInt32(intData); + datas.WriteString(stringData); + datas.WriteInt32(intData); ansManagerStub.HandleCancel(datas, reply); + datas.WriteParcelable(bundleOption); + datas.WriteInt32(intData); + datas.WriteInt32(intData); ansManagerStub.HandleCancelAsBundleAndUser(datas, reply); + datas.WriteInt32(type); ansManagerStub.HandleAddSlotByType(datas, reply); + WriteParcelableVector(slots, data); ansManagerStub.HandleAddSlots(datas, reply); + datas.WriteInt32(type); ansManagerStub.HandleRemoveSlotByType(datas, reply); ansManagerStub.HandleRemoveAllSlots(datas, reply); ansManagerStub.HandleGetSlots(datas, reply); + datas.WriteInt32(type); ansManagerStub.HandleGetSlotByType(datas, reply); + datas.WriteParcelable(bundleOption); ansManagerStub.HandleGetSlotNumAsBundle(datas, reply); + datas.WriteParcelable(bundleOption); + datas.WriteInt32(intData); ansManagerStub.HandleSetSlotFlagsAsBundle(datas, reply); + datas.WriteParcelable(bundleOption); ansManagerStub.HandleGetSlotFlagsAsBundle(datas, reply); + datas.WriteInt32(intData); ansManagerStub.HandleGetActiveNotifications(datas, reply); ansManagerStub.HandleGetActiveNotificationNums(datas, reply); ansManagerStub.HandleGetAllActiveNotifications(datas, reply); + std::vector stringVector { stringData }; + datas.WriteStringVector(stringVector); ansManagerStub.HandleGetSpecialActiveNotifications(datas, reply); + datas.WriteParcelable(bundleOption); + datas.WriteInt32(intData); + datas.WriteString(stringData); + datas.WriteStringVector(stringVector); ansManagerStub.HandleGetActiveNotificationByFilter(datas, reply); + datas.WriteString(stringData); ansManagerStub.HandleSetNotificationAgent(datas, reply); ansManagerStub.HandleGetNotificationAgent(datas, reply); + datas.WriteString(stringData); ansManagerStub.HandleCanPublishAsBundle(datas, reply); + datas.WriteParcelable(notification); + datas.WriteString(stringData); ansManagerStub.HandlePublishAsBundle(datas, reply); + datas.WriteInt32(intData); ansManagerStub.HandleSetNotificationBadgeNum(datas, reply); ansManagerStub.HandleGetBundleImportance(datas, reply); + data.WriteParcelable(distribuDate); ansManagerStub.HandleSetDoNotDisturbDate(datas, reply); ansManagerStub.HandleGetDoNotDisturbDate(datas, reply); ansManagerStub.HandleDoesSupportDoNotDisturbMode(datas, reply); + datas.WriteParcelable(notification); + datas.WriteInt32(intData); ansManagerStub.HandlePublishContinuousTaskNotification(datas, reply); + datas.WriteString(stringData); + datas.WriteInt32(intData); ansManagerStub.HandleCancelContinuousTaskNotification(datas, reply); ansManagerStub.HandleIsNotificationPolicyAccessGranted(datas, reply); + datas.WriteParcelable(bundleOption); + datas.WriteInt32(intData); + datas.WriteParcelable(buttonOption); ansManagerStub.HandleTriggerLocalLiveView(datas, reply); + datas.WriteParcelable(bundleOption); + datas.WriteInt32(intData); + datas.WriteString(stringData); + datas.WriteInt32(intData); ansManagerStub.HandleRemoveNotification(datas, reply); ansManagerStub.HandleRemoveAllNotifications(datas, reply); + datas.WriteInt32(intData); + datas.WriteStringVector(stringVector); + datas.WriteInt32(intData); ansManagerStub.HandleRemoveNotifications(datas, reply); + datas.WriteString(stringData); + datas.WriteInt32(intData); ansManagerStub.HandleDelete(datas, reply); + datas.WriteParcelable(bundleOption); ansManagerStub.HandleDeleteByBundle(datas, reply); ansManagerStub.HandleDeleteAll(datas, reply); + datas.WriteParcelable(bundleOption); + datas.WriteInt32(intData); ansManagerStub.HandleGetSlotByBundle(datas, reply); ansManagerStub.HandleGetSlotsByBundle(datas, reply); + datas.WriteParcelable(bundleOption); ansManagerStub.HandleUpdateSlots(datas, reply); + datas.WriteString(stringData); + datas.WriteRemoteObject(subscriber); + datas.WriteBool(boolData); + if (boolData) { + datas.WriteRemoteObject(subscriber); + } ansManagerStub.HandleRequestEnableNotification(datas, reply); + datas.WriteString(stringData); + datas.WriteBool(boolData); ansManagerStub.HandleSetNotificationsEnabledForBundle(datas, reply); + datas.WriteString(stringData); + datas.WriteBool(boolData); ansManagerStub.HandleSetNotificationsEnabledForAllBundles(datas, reply); + datas.WriteString(stringData); + datas.WriteParcelable(bundleOption); + datas.WriteBool(boolData); ansManagerStub.HandleSetNotificationsEnabledForSpecialBundle(datas, reply); + datas.WriteParcelable(bundleOption); + datas.WriteBool(boolData); ansManagerStub.HandleSetShowBadgeEnabledForBundle(datas, reply); + datas.WriteParcelable(bundleOption); ansManagerStub.HandleGetShowBadgeEnabledForBundle(datas, reply); ansManagerStub.HandleGetShowBadgeEnabled(datas, reply); + datas.WriteRemoteObject(subscriber); + datas.WriteBool(boolData); + if (boolData) { + datas.WriteRemoteObject(info); + } ansManagerStub.HandleSubscribe(datas, reply); + datas.WriteRemoteObject(subscriber); ansManagerStub.HandleSubscribeSelf(datas, reply); + datas.WriteRemoteObject(subscriber); + datas.WriteBool(boolData); + if (boolData) { + datas.WriteRemoteObject(info); + } + datas.WriteBool(boolData); ansManagerStub.HandleSubscribeLocalLiveView(datas, reply); + datas.WriteRemoteObject(subscriber); + datas.WriteBool(boolData); + if (boolData) { + datas.WriteRemoteObject(info); + } ansManagerStub.HandleUnsubscribe(datas, reply); ansManagerStub.HandleIsAllowedNotify(datas, reply); ansManagerStub.HandleIsAllowedNotifySelf(datas, reply); + datas.WriteRemoteObject(subscriber); + ansManagerStub.HandleCanPopEnableNotificationDialog(datas, reply); + datas.WriteParcelable(bundleOption); ansManagerStub.HandleIsSpecialBundleAllowedNotify(datas, reply); + datas.WriteString(stringData); + datas.WriteInt32(intData); ansManagerStub.HandleCancelGroup(datas, reply); + datas.WriteParcelable(bundleOption); + datas.WriteString(stringData); ansManagerStub.HandleRemoveGroupByBundle(datas, reply); ansManagerStub.HandleIsDistributedEnabled(datas, reply); + datas.WriteBool(boolData); ansManagerStub.HandleEnableDistributed(datas, reply); + datas.WriteParcelable(bundleOption); + datas.WriteBool(boolData); ansManagerStub.HandleEnableDistributedByBundle(datas, reply); + datas.WriteBool(boolData); ansManagerStub.HandleEnableDistributedSelf(datas, reply); + datas.WriteParcelable(bundleOption); ansManagerStub.HandleIsDistributedEnableByBundle(datas, reply); ansManagerStub.HandleGetDeviceRemindType(datas, reply); + datas.WriteString(stringData); + datas.WriteString(stringData); + datas.WriteInt32(intData); + datas.WriteInt32(intData); ansManagerStub.HandleShellDump(datas, reply); ansManagerStub.HandlePublishReminder(datas, reply); + datas.WriteInt32(intData); ansManagerStub.HandleCancelReminder(datas, reply); ansManagerStub.HandleCancelAllReminders(datas, reply); ansManagerStub.HandleGetValidReminders(datas, reply); + datas.WriteInt32(intData); + datas.WriteUint64(intData); ansManagerStub.HandleAddExcludeDate(datas, reply); + datas.WriteInt32(intData); ansManagerStub.HandleDelExcludeDates(datas, reply); + datas.WriteInt32(intData); ansManagerStub.HandleGetExcludeDates(datas, reply); + datas.WriteString(stringData); ansManagerStub.HandleIsSupportTemplate(datas, reply); + datas.WriteInt32(intData); ansManagerStub.HandleIsSpecialUserAllowedNotifyByUser(datas, reply); + datas.WriteInt32(intData); + datas.WriteBool(boolData); ansManagerStub.HandleSetNotificationsEnabledByUser(datas, reply); - ansManagerStub.HandleDeleteAllByUser(datas, reply); + datas.WriteInt32(intData); + ansManagerStub.HandleDeleteAllByUser(datas, reply) + datas.WriteInt32(intData); + data.WriteParcelable(distribuDate); ansManagerStub.HandleSetDoNotDisturbDateByUser(datas, reply); + datas.WriteInt32(intData); ansManagerStub.HandleGetDoNotDisturbDateByUser(datas, reply); + datas.WriteParcelable(bundleOption); + datas.WriteInt32(type); + datas.WriteBool(boolData); + datas.WriteBool(boolData); ansManagerStub.HandleSetEnabledForBundleSlot(datas, reply); + datas.WriteParcelable(bundleOption); + datas.WriteInt32(type); ansManagerStub.HandleGetEnabledForBundleSlot(datas, reply); + datas.WriteInt32(type); ansManagerStub.HandleGetEnabledForBundleSlotSelf(datas, reply); + datas.WriteInt32(intData); + datas.WriteBool(boolData); ansManagerStub.HandleDistributedSetEnabledWithoutApp(datas, reply); + datas.WriteInt32(intData); ansManagerStub.HandleDistributedGetEnabledWithoutApp(datas, reply); + datas.WriteInt32(intData); + datas.WriteInt32(intData); ansManagerStub.HandleSetBadgeNumber(datas, reply); + datas.WriteParcelable(bundleOption); + datas.WriteInt32(intData); ansManagerStub.HandleSetBadgeNumberByBundle(datas, reply); ansManagerStub.HandleGetAllNotificationEnableStatus(datas, reply); ansManagerStub.HandleRegisterPushCallback(datas, reply); ansManagerStub.HandleUnregisterPushCallback(datas, reply); ansManagerStub.HandleAddDoNotDisturbProfiles(datas, reply); + datas.WriteParcelable(bundleOption); + datas.WriteString(stringData); + datas.WriteBool(boolData); ansManagerStub.HandleSetDistributedEnabledByBundle(datas, reply); ansManagerStub.HandleRemoveDoNotDisturbProfiles(datas, reply); + datas.WriteParcelable(bundleOption); + datas.WriteString(stringData); ansManagerStub.HandleIsDistributedEnabledByBundle(datas, reply); + datas.WriteString(stringData); + datas.WriteBool(boolData); ansManagerStub.HandleSetSmartReminderEnabled(datas, reply); + datas.WriteString(stringData); ansManagerStub.HandleIsSmartReminderEnabled(datas, reply); + datas.WriteString(stringData); + datas.WriteString(stringData); ansManagerStub.HandleSetAdditionConfig(datas, reply); + datas.WriteParcelable(bundleOption); + datas.WriteInt32(intData); ansManagerStub.HandleCancelAsBundleWithAgent(datas, reply); + datas.WriteString(stringData); + datas.WriteInt32(intData); ansManagerStub.HandleSetTargetDeviceStatus(datas, reply); sptr notification = new Notification::NotificationRequest(); notification->SetOwnerUid(fuzzData.GenerateRandomInt32()); @@ -151,7 +324,6 @@ namespace OHOS { ansManagerStub.CancelAll(0); int32_t userId = fuzzData.GetData(); ansManagerStub.CancelAsBundle(notificationId, stringData, userId); - uint8_t type = fuzzData.GetData() % SLOT_TYPE_NUM; Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType(type); ansManagerStub.AddSlotByType(slotType); std::vector> slots; diff --git a/test/fuzztest/notificationhelper_fuzzer/notificationhelper_fuzzer.cpp b/test/fuzztest/notificationhelper_fuzzer/notificationhelper_fuzzer.cpp index ff1f5e769..31216d4cc 100644 --- a/test/fuzztest/notificationhelper_fuzzer/notificationhelper_fuzzer.cpp +++ b/test/fuzztest/notificationhelper_fuzzer/notificationhelper_fuzzer.cpp @@ -21,20 +21,33 @@ #include "notificationhelper_fuzzer.h" namespace OHOS { - namespace { - constexpr uint8_t ENABLE = 2; - } - bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) + + bool DoSomethingInterestingWithMyAPI(FuzzData fuzzData) { Notification::NotificationHelper notificationHelper; // test IsSoundEnabled function - std::string representativeBundle(data); - Notification::NotificationRequest request; - notificationHelper.PublishNotificationAsBundle(representativeBundle, request); + std::string representativeBundle = fuzzData.GenerateRandomString(); + sptr notification = new Notification::NotificationRequest(); + notification->SetOwnerUid(fuzzData.GenerateRandomInt32()); + notification->SetCreatorUid(fuzzData.GenerateRandomInt32()); + notification->SetSlotType(Notification::NotificationConstant::SlotType::LIVE_VIEW); + auto content = std::make_shared(); + notification->SetContent(std::make_shared(content)); + notificationHelper.PublishNotificationAsBundle(representativeBundle, *notification); notificationHelper.RemoveNotifications(); - int32_t userId = static_cast(GetU32Data(data)); - bool enabled = *data % ENABLE; - notificationHelper.SetNotificationsEnabledForAllBundles(userId, enabled); + int32_t intData = fuzzData.GenerateRandomInt32(); + bool enabled = fuzzData.GenerateRandomBool(); + notificationHelper.SetNotificationsEnabledForAllBundles(intData, enabled); + sptr bundleOption = new Notification::NotificationBundleOption(); + bundleOption->SetBundleName(fuzzData.GenerateRandomString()); + bundleOption->SetUid(fuzzData.GenerateRandomInt32()); + notificationHelper.GetNotificationSlotFlagsAsBundle(bundleOption, intData); + notificationHelper.SetNotificationSlotFlagsAsBundle(bundleOption, intData); + notificationHelper.CancelAsBundle(bundleOption, intData); + sptr subscriber = new Notification::AnsSubscriberStub(); + sptr info = new Notification::NotificationSubscribeInfo(); + notificationHelper.SubscribeNotification(subscriber); + notificationHelper.SubscribeNotificationSelf(subscriber); return true; } } @@ -42,12 +55,9 @@ namespace OHOS { /* Fuzzer entry point */ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { - /* Run your code on data */ - char *ch = ParseData(data, size); if (ch != nullptr && size >= GetU32Size()) { - OHOS::DoSomethingInterestingWithMyAPI(ch, size); - free(ch); - ch = nullptr; + OHOS::FuzzData fuzzData(data, size); + OHOS::DoSomethingInterestingWithMyAPI(fuzzData); } return 0; } -- Gitee