1 /*
2 * Copyright (c) 2022 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 "ans_manager_stub.h"
19 #undef private
20 #undef protected
21 #include "ansmanagerstub_fuzzer.h"
22 #include "notification_request.h"
23
24 constexpr uint8_t SLOT_TYPE_NUM = 5;
25
26 namespace OHOS {
DoSomethingInterestingWithMyAPI(const char * data,size_t size)27 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
28 {
29 std::string stringData(data);
30 Notification::AnsManagerStub ansManagerStub;
31 uint32_t code = GetU32Data(data);
32 MessageParcel datas;
33 MessageParcel reply;
34 MessageOption flags;
35 ansManagerStub.OnRemoteRequest(code, datas, reply, flags);
36 ansManagerStub.HandlePublish(datas, reply);
37 ansManagerStub.HandlePublishToDevice(datas, reply);
38 ansManagerStub.HandleCancel(datas, reply);
39 ansManagerStub.HandleCancelAll(datas, reply);
40 ansManagerStub.HandleCancelAsBundle(datas, reply);
41 ansManagerStub.HandleAddSlotByType(datas, reply);
42 ansManagerStub.HandleAddSlots(datas, reply);
43 ansManagerStub.HandleRemoveSlotByType(datas, reply);
44 ansManagerStub.HandleRemoveAllSlots(datas, reply);
45 ansManagerStub.HandleGetSlots(datas, reply);
46 ansManagerStub.HandleGetSlotByType(datas, reply);
47 ansManagerStub.HandleGetSlotNumAsBundle(datas, reply);
48 ansManagerStub.HandleGetActiveNotifications(datas, reply);
49 ansManagerStub.HandleGetActiveNotificationNums(datas, reply);
50 ansManagerStub.HandleGetAllActiveNotifications(datas, reply);
51 ansManagerStub.HandleGetSpecialActiveNotifications(datas, reply);
52 ansManagerStub.HandleSetNotificationAgent(datas, reply);
53 ansManagerStub.HandleGetNotificationAgent(datas, reply);
54 ansManagerStub.HandleCanPublishAsBundle(datas, reply);
55 ansManagerStub.HandlePublishAsBundle(datas, reply);
56 ansManagerStub.HandleSetNotificationBadgeNum(datas, reply);
57 ansManagerStub.HandleGetBundleImportance(datas, reply);
58 ansManagerStub.HandleSetDoNotDisturbDate(datas, reply);
59 ansManagerStub.HandleGetDoNotDisturbDate(datas, reply);
60 ansManagerStub.HandleDoesSupportDoNotDisturbMode(datas, reply);
61 ansManagerStub.HandlePublishContinuousTaskNotification(datas, reply);
62 ansManagerStub.HandleCancelContinuousTaskNotification(datas, reply);
63 ansManagerStub.HandleIsNotificationPolicyAccessGranted(datas, reply);
64 ansManagerStub.HandleSetPrivateNotificationsAllowed(datas, reply);
65 ansManagerStub.HandleGetPrivateNotificationsAllowed(datas, reply);
66 ansManagerStub.HandleRemoveNotification(datas, reply);
67 ansManagerStub.HandleRemoveAllNotifications(datas, reply);
68 ansManagerStub.HandleDelete(datas, reply);
69 ansManagerStub.HandleDeleteByBundle(datas, reply);
70 ansManagerStub.HandleDeleteAll(datas, reply);
71 ansManagerStub.HandleGetSlotsByBundle(datas, reply);
72 ansManagerStub.HandleUpdateSlots(datas, reply);
73 ansManagerStub.HandleRequestEnableNotification(datas, reply);
74 ansManagerStub.HandleSetNotificationsEnabledForBundle(datas, reply);
75 ansManagerStub.HandleSetNotificationsEnabledForAllBundles(datas, reply);
76 ansManagerStub.HandleSetNotificationsEnabledForSpecialBundle(datas, reply);
77 ansManagerStub.HandleSetShowBadgeEnabledForBundle(datas, reply);
78 ansManagerStub.HandleGetShowBadgeEnabledForBundle(datas, reply);
79 ansManagerStub.HandleGetShowBadgeEnabled(datas, reply);
80 ansManagerStub.HandleSubscribe(datas, reply);
81 ansManagerStub.HandleUnsubscribe(datas, reply);
82 ansManagerStub.HandleAreNotificationsSuspended(datas, reply);
83 ansManagerStub.HandleGetCurrentAppSorting(datas, reply);
84 ansManagerStub.HandleIsAllowedNotify(datas, reply);
85 ansManagerStub.HandleIsAllowedNotifySelf(datas, reply);
86 ansManagerStub.HandleIsSpecialBundleAllowedNotify(datas, reply);
87 ansManagerStub.HandleCancelGroup(datas, reply);
88 ansManagerStub.HandleRemoveGroupByBundle(datas, reply);
89 ansManagerStub.HandleIsDistributedEnabled(datas, reply);
90 ansManagerStub.HandleEnableDistributed(datas, reply);
91 ansManagerStub.HandleEnableDistributedByBundle(datas, reply);
92 ansManagerStub.HandleEnableDistributedSelf(datas, reply);
93 ansManagerStub.HandleIsDistributedEnableByBundle(datas, reply);
94 ansManagerStub.HandleGetDeviceRemindType(datas, reply);
95 ansManagerStub.HandleShellDump(datas, reply);
96 ansManagerStub.HandlePublishReminder(datas, reply);
97 ansManagerStub.HandleCancelReminder(datas, reply);
98 ansManagerStub.HandleCancelAllReminders(datas, reply);
99 ansManagerStub.HandleGetValidReminders(datas, reply);
100 ansManagerStub.HandleIsSupportTemplate(datas, reply);
101 ansManagerStub.HandleIsSpecialUserAllowedNotifyByUser(datas, reply);
102 ansManagerStub.HandleSetNotificationsEnabledByUser(datas, reply);
103 ansManagerStub.HandleDeleteAllByUser(datas, reply);
104 ansManagerStub.HandleSetDoNotDisturbDateByUser(datas, reply);
105 ansManagerStub.HandleGetDoNotDisturbDateByUser(datas, reply);
106 ansManagerStub.HandleSetEnabledForBundleSlot(datas, reply);
107 ansManagerStub.HandleGetEnabledForBundleSlot(datas, reply);
108 ansManagerStub.HandleDistributedSetEnabledWithoutApp(datas, reply);
109 ansManagerStub.HandleDistributedGetEnabledWithoutApp(datas, reply);
110 sptr<Notification::NotificationRequest> notification = new Notification::NotificationRequest();
111 const std::string label = "this is a notification label";
112 ansManagerStub.Publish(label, notification);
113 const std::string deviceId = "this is a notification deviceId";
114 ansManagerStub.PublishToDevice(notification, deviceId);
115 int notificationId = 1;
116 ansManagerStub.Cancel(notificationId, label);
117 ansManagerStub.CancelAll();
118 const std::string representativeBundle = "this is a notification representativeBundle";
119 int32_t userId = 1;
120 ansManagerStub.CancelAsBundle(notificationId, representativeBundle, userId);
121 uint8_t type = *data % SLOT_TYPE_NUM;
122 Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType(type);
123 ansManagerStub.AddSlotByType(slotType);
124 std::vector<sptr<Notification::NotificationSlot>> slots;
125 ansManagerStub.AddSlots(slots);
126 ansManagerStub.RemoveSlotByType(slotType);
127 ansManagerStub.RemoveAllSlots();
128 sptr<Notification::NotificationSlot> slot = new Notification::NotificationSlot();
129 ansManagerStub.GetSlotByType(slotType, slot);
130 ansManagerStub.GetSlots(slots);
131 sptr<Notification::NotificationBundleOption> bundleOption = new Notification::NotificationBundleOption();
132 uint64_t num = 1;
133 ansManagerStub.GetSlotNumAsBundle(bundleOption, num);
134 std::vector<sptr<Notification::NotificationRequest>> notifications;
135 ansManagerStub.GetActiveNotifications(notifications);
136 ansManagerStub.GetActiveNotificationNums(num);
137 std::vector<sptr<Notification::Notification>> notificationss;
138 ansManagerStub.GetAllActiveNotifications(notificationss);
139 std::vector<std::string> key;
140 ansManagerStub.GetSpecialActiveNotifications(key, notificationss);
141 std::string agent = "this is a notification agent";
142 ansManagerStub.SetNotificationAgent(agent);
143 ansManagerStub.GetNotificationAgent(agent);
144 bool canPublish = true;
145 ansManagerStub.CanPublishAsBundle(representativeBundle, canPublish);
146 ansManagerStub.PublishAsBundle(notification, representativeBundle);
147 ansManagerStub.SetNotificationBadgeNum(num);
148 int importance = 1;
149 ansManagerStub.GetBundleImportance(importance);
150 bool granted = true;
151 ansManagerStub.HasNotificationPolicyAccessPermission(granted);
152 bool allow = true;
153 ansManagerStub.SetPrivateNotificationsAllowed(allow);
154 int32_t removeReason = 1;
155 ansManagerStub.RemoveNotification(bundleOption, notificationId, label, removeReason);
156 ansManagerStub.RemoveAllNotifications(bundleOption);
157 const std::string keys = "this is a notification keys";
158 ansManagerStub.Delete(keys, removeReason);
159 ansManagerStub.DeleteByBundle(bundleOption);
160 ansManagerStub.DeleteAll();
161 ansManagerStub.GetSlotsByBundle(bundleOption, slots);
162 ansManagerStub.UpdateSlots(bundleOption, slots);
163 ansManagerStub.RequestEnableNotification(deviceId);
164 const std::string bundle = "this is a notification bundle";
165 bool enabled = true;
166 ansManagerStub.SetNotificationsEnabledForBundle(bundle, enabled);
167 ansManagerStub.SetNotificationsEnabledForSpecialBundle(deviceId, bundleOption, enabled);
168 ansManagerStub.SetShowBadgeEnabledForBundle(bundleOption, enabled);
169 ansManagerStub.GetShowBadgeEnabledForBundle(bundleOption, enabled);
170 ansManagerStub.GetShowBadgeEnabled(enabled);
171 bool suspended = true;
172 ansManagerStub.AreNotificationsSuspended(suspended);
173 sptr<Notification::NotificationSortingMap> sortingMap = new Notification::NotificationSortingMap();
174 ansManagerStub.GetCurrentAppSorting(sortingMap);
175 bool allowed = true;
176 ansManagerStub.IsAllowedNotify(allowed);
177 ansManagerStub.IsSpecialBundleAllowedNotify(bundleOption, allowed);
178 const std::string groupName = "this is a notification groupName";
179 ansManagerStub.CancelGroup(groupName);
180 ansManagerStub.RemoveGroupByBundle(bundleOption, groupName);
181 sptr<Notification::NotificationDoNotDisturbDate> date = new Notification::NotificationDoNotDisturbDate();
182 ansManagerStub.SetDoNotDisturbDate(date);
183 ansManagerStub.GetDoNotDisturbDate(date);
184 bool doesSupport = true;
185 ansManagerStub.DoesSupportDoNotDisturbMode(doesSupport);
186 ansManagerStub.IsDistributedEnabled(enabled);
187 ansManagerStub.EnableDistributedByBundle(bundleOption, enabled);
188 ansManagerStub.EnableDistributedSelf(enabled);
189 ansManagerStub.IsDistributedEnableByBundle(bundleOption, enabled);
190 Notification::NotificationConstant::RemindType remindType;
191 ansManagerStub.GetDeviceRemindType(remindType);
192 sptr<Notification::NotificationRequest> request = new Notification::NotificationRequest();
193 ansManagerStub.PublishContinuousTaskNotification(request);
194 ansManagerStub.CancelContinuousTaskNotification(label, notificationId);
195 sptr<Notification::ReminderRequest> reminder = new Notification::ReminderRequest();
196 ansManagerStub.PublishReminder(reminder);
197 const int32_t reminderId = 1;
198 ansManagerStub.CancelReminder(reminderId);
199 std::vector<sptr<Notification::ReminderRequest>> reminders;
200 ansManagerStub.GetValidReminders(reminders);
201 ansManagerStub.CancelAllReminders();
202 const std::string templateName = "this is a notification templateName";
203 bool support = true;
204 ansManagerStub.IsSupportTemplate(templateName, support);
205 ansManagerStub.IsSpecialUserAllowedNotify(userId, allowed);
206 const int32_t deviceIds = 1;
207 ansManagerStub.SetNotificationsEnabledByUser(deviceIds, enabled);
208 ansManagerStub.DeleteAllByUser(userId);
209 ansManagerStub.SetDoNotDisturbDate(date);
210 ansManagerStub.GetDoNotDisturbDate(date);
211 ansManagerStub.SetEnabledForBundleSlot(bundleOption, slotType, enabled);
212 ansManagerStub.GetEnabledForBundleSlot(bundleOption, slotType, enabled);
213 const std::string cmd = "this is a notification cmd";
214 std::vector<std::string> dumpInfo;
215 ansManagerStub.ShellDump(cmd, bundle, userId, dumpInfo);
216 ansManagerStub.SetSyncNotificationEnabledWithoutApp(userId, enabled);
217 ansManagerStub.GetSyncNotificationEnabledWithoutApp(userId, enabled);
218 return true;
219 }
220 }
221
222 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)223 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
224 {
225 /* Run your code on data */
226 char *ch = ParseData(data, size);
227 if (ch != nullptr && size >= GetU32Size()) {
228 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
229 free(ch);
230 ch = nullptr;
231 }
232 return 0;
233 }
234