1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #define private public
17 #define protected public
18 #include "advanced_notification_service.h"
19 #undef private
20 #undef protected
21 #include "advancednotificationservice_fuzzer.h"
22 #include "ans_subscriber_stub.h"
23 #include "notification_request.h"
24
25 constexpr uint8_t SLOT_TYPE_NUM = 5;
26 constexpr uint8_t ENABLE = 2;
27
28 namespace OHOS {
29 Notification::AdvancedNotificationService advancedNotificationService;
30
DoSomethingInterestingWithMyAPI(const char * data,size_t size)31 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
32 {
33 std::string stringData(data, size);
34 sptr<Notification::NotificationRequest> notification = new Notification::NotificationRequest();
35 advancedNotificationService.Publish(stringData, notification);
36 int notificationId = static_cast<int>(GetU32Data(data));
37 advancedNotificationService.Cancel(notificationId, stringData);
38 advancedNotificationService.CancelAll();
39 int32_t userId = static_cast<int32_t>(GetU32Data(data));
40 advancedNotificationService.CancelAsBundle(notificationId, stringData, userId);
41 uint8_t type = *data % SLOT_TYPE_NUM;
42 Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType(type);
43 advancedNotificationService.AddSlotByType(slotType);
44 std::vector<sptr<Notification::NotificationSlot>> slots;
45 advancedNotificationService.AddSlots(slots);
46 advancedNotificationService.RemoveSlotByType(slotType);
47 advancedNotificationService.RemoveAllSlots();
48 sptr<Notification::NotificationSlot> slot = new Notification::NotificationSlot();
49 advancedNotificationService.GetSlotByType(slotType, slot);
50 advancedNotificationService.GetSlots(slots);
51 sptr<Notification::NotificationBundleOption> bundleOption = new Notification::NotificationBundleOption();
52 uint64_t num = static_cast<uint64_t>(GetU32Data(data));
53 advancedNotificationService.GetSlotNumAsBundle(bundleOption, num);
54 std::vector<sptr<Notification::NotificationRequest>> notifications;
55 advancedNotificationService.GetActiveNotifications(notifications);
56 advancedNotificationService.GetActiveNotificationNums(num);
57 std::vector<sptr<Notification::Notification>> notificationss;
58 advancedNotificationService.GetAllActiveNotifications(notificationss);
59 std::vector<std::string> key;
60 advancedNotificationService.GetSpecialActiveNotifications(key, notificationss);
61 advancedNotificationService.SetNotificationAgent(stringData);
62 advancedNotificationService.GetNotificationAgent(stringData);
63 bool canPublish = *data % ENABLE;
64 advancedNotificationService.CanPublishAsBundle(stringData, canPublish);
65 advancedNotificationService.PublishAsBundle(notification, stringData);
66 advancedNotificationService.SetNotificationBadgeNum(num);
67 int importance = static_cast<int>(GetU32Data(data));
68 advancedNotificationService.GetBundleImportance(importance);
69 bool granted = *data % ENABLE;
70 advancedNotificationService.HasNotificationPolicyAccessPermission(granted);
71 int32_t removeReason = static_cast<int32_t>(GetU32Data(data));
72 advancedNotificationService.RemoveNotification(bundleOption, notificationId, stringData, removeReason);
73 advancedNotificationService.RemoveAllNotifications(bundleOption);
74 advancedNotificationService.Delete(stringData, removeReason);
75 advancedNotificationService.DeleteByBundle(bundleOption);
76 advancedNotificationService.DeleteAll();
77 advancedNotificationService.GetSlotsByBundle(bundleOption, slots);
78 advancedNotificationService.UpdateSlots(bundleOption, slots);
79 bool enabled = *data % ENABLE;
80 advancedNotificationService.SetNotificationsEnabledForBundle(stringData, enabled);
81 advancedNotificationService.SetNotificationsEnabledForAllBundles(stringData, enabled);
82 advancedNotificationService.SetNotificationsEnabledForSpecialBundle(stringData, bundleOption, enabled);
83 advancedNotificationService.SetShowBadgeEnabledForBundle(bundleOption, enabled);
84 advancedNotificationService.GetShowBadgeEnabledForBundle(bundleOption, enabled);
85 advancedNotificationService.GetShowBadgeEnabled(enabled);
86 sptr<Notification::AnsSubscriberStub> subscriber = new Notification::AnsSubscriberStub();
87 sptr<Notification::NotificationSubscribeInfo> info = new Notification::NotificationSubscribeInfo();
88 advancedNotificationService.Subscribe(subscriber, info);
89 advancedNotificationService.Unsubscribe(subscriber, info);
90 bool allowed = *data % ENABLE;
91 advancedNotificationService.IsAllowedNotify(allowed);
92 advancedNotificationService.IsAllowedNotifySelf(allowed);
93 advancedNotificationService.IsSpecialBundleAllowedNotify(bundleOption, allowed);
94 advancedNotificationService.CancelGroup(stringData);
95 advancedNotificationService.RemoveGroupByBundle(bundleOption, stringData);
96 sptr<Notification::NotificationDoNotDisturbDate> date = new Notification::NotificationDoNotDisturbDate();
97 advancedNotificationService.SetDoNotDisturbDate(date);
98 advancedNotificationService.GetDoNotDisturbDate(date);
99 bool doesSupport = *data % ENABLE;
100 advancedNotificationService.DoesSupportDoNotDisturbMode(doesSupport);
101 advancedNotificationService.IsDistributedEnabled(enabled);
102 advancedNotificationService.EnableDistributedByBundle(bundleOption, enabled);
103 advancedNotificationService.EnableDistributedSelf(enabled);
104 advancedNotificationService.EnableDistributed(enabled);
105 advancedNotificationService.IsDistributedEnableByBundle(bundleOption, enabled);
106 Notification::NotificationConstant::RemindType remindType;
107 advancedNotificationService.GetDeviceRemindType(remindType);
108 sptr<Notification::NotificationRequest> request = new Notification::NotificationRequest();
109 advancedNotificationService.PublishContinuousTaskNotification(request);
110 advancedNotificationService.CancelContinuousTaskNotification(stringData, notificationId);
111 sptr<Notification::ReminderRequest> reminder = new Notification::ReminderRequest();
112 advancedNotificationService.PublishReminder(reminder);
113 int32_t reminderId = static_cast<int32_t>(GetU32Data(data));
114 advancedNotificationService.CancelReminder(reminderId);
115 std::vector<sptr<Notification::ReminderRequest>> reminders;
116 advancedNotificationService.GetValidReminders(reminders);
117 advancedNotificationService.CancelAllReminders();
118 uint64_t excludeDate = static_cast<uint64_t>(GetU32Data(data));
119 advancedNotificationService.AddExcludeDate(reminderId, excludeDate);
120 advancedNotificationService.DelExcludeDates(reminderId);
121 std::vector<uint64_t> excludeDates;
122 advancedNotificationService.GetExcludeDates(reminderId, excludeDates);
123 bool support = *data % ENABLE;
124 advancedNotificationService.IsSupportTemplate(stringData, support);
125 advancedNotificationService.IsSpecialUserAllowedNotify(userId, allowed);
126 int32_t deviceIds = static_cast<int32_t>(GetU32Data(data));
127 advancedNotificationService.SetNotificationsEnabledByUser(deviceIds, enabled);
128 advancedNotificationService.DeleteAllByUser(userId);
129 advancedNotificationService.SetDoNotDisturbDate(date);
130 advancedNotificationService.GetDoNotDisturbDate(date);
131 advancedNotificationService.SetEnabledForBundleSlot(bundleOption, slotType, enabled, false);
132 advancedNotificationService.GetEnabledForBundleSlot(bundleOption, slotType, enabled);
133 std::vector<std::string> dumpInfo;
134 advancedNotificationService.ShellDump(stringData, stringData, userId, userId, dumpInfo);
135 advancedNotificationService.SetSyncNotificationEnabledWithoutApp(userId, enabled);
136 advancedNotificationService.GetSyncNotificationEnabledWithoutApp(userId, enabled);
137 int32_t badgeNum = static_cast<int32_t>(GetU32Data(data));
138 advancedNotificationService.SetBadgeNumber(badgeNum);
139 sptr<Notification::AnsDialogCallback> dialogCallback = NULL;
140 sptr<IRemoteObject> callerToken = NULL;
141 advancedNotificationService.RequestEnableNotification(stringData, dialogCallback, callerToken);
142
143 return true;
144 }
145 }
146
147 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)148 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
149 {
150 /* Run your code on data */
151 char *ch = ParseData(data, size);
152 if (ch != nullptr && size >= GetU32Size()) {
153 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
154 free(ch);
155 ch = nullptr;
156 }
157 return 0;
158 }
159