• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 #include "notification_constant.h"
17 #include "notification_content.h"
18 #include "notification_normal_content.h"
19 #include "refbase.h"
20 #include <fuzzer/FuzzedDataProvider.h>
21 #include <memory>
22 #include <new>
23 #include <string>
24 #include <vector>
25 #define private public
26 #define protected public
27 #include "ans_manager_proxy.h"
28 #include "ans_manager_stub.h"
29 #include "ans_subscriber_stub.h"
30 #undef private
31 #undef protected
32 #include "ansmanagerstubannexthree_fuzzer.h"
33 #include "notification_request.h"
34 #include "ans_permission_def.h"
35 
36 constexpr uint8_t SLOT_TYPE_NUM = 5;
37 
38 namespace OHOS {
DoSomethingInterestingWithMyAPI(FuzzedDataProvider * fuzzData)39     bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider *fuzzData)
40     {
41         std::string stringData = fuzzData->ConsumeRandomLengthString();
42         int32_t intData = fuzzData->ConsumeIntegral<int32_t>();
43         bool boolData = fuzzData->ConsumeBool();
44         Notification::AnsManagerStub ansManagerStub;
45         MessageParcel datas;
46         MessageParcel reply;
47         MessageOption flags;
48         // test HandleIsNeedSilentInDoNotDisturbMode function
49         ansManagerStub.HandleIsNeedSilentInDoNotDisturbMode(datas, reply);
50         // test HandleRegisterSwingCallback function
51         #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
52         ansManagerStub.HandleRegisterSwingCallback(datas, reply);
53         #endif
54         ansManagerStub.OnRemoteRequest(0, datas, reply, flags);
55         ansManagerStub.OnRemoteRequest((int) Notification::NotificationInterfaceCode::PUBLISH_NOTIFICATION,
56             datas, reply, flags);
57         sptr<Notification::NotificationRequest> notification = new Notification::NotificationRequest();
58         notification->SetOwnerUid(fuzzData->ConsumeIntegral<int32_t>());
59         notification->SetCreatorUid(fuzzData->ConsumeIntegral<int32_t>());
60         notification->SetSlotType(Notification::NotificationConstant::SlotType::LIVE_VIEW);
61         auto content = std::make_shared<Notification::NotificationLiveViewContent>();
62         notification->SetContent(std::make_shared<Notification::NotificationContent>(content));
63 
64         sptr<Notification::NotificationBundleOption> bundleOption = new Notification::NotificationBundleOption();
65         bundleOption->SetBundleName(fuzzData->ConsumeRandomLengthString());
66         bundleOption->SetUid(fuzzData->ConsumeIntegral<int32_t>());
67 
68         sptr<Notification::NotificationButtonOption> buttonOption = new Notification::NotificationButtonOption();
69 
70         sptr<Notification::AnsSubscriberStub> subscriber = new Notification::AnsSubscriberStub();
71         sptr<Notification::NotificationSubscribeInfo> info = new Notification::NotificationSubscribeInfo();
72 
73         sptr<Notification::NotificationSlot> slot = new Notification::NotificationSlot();
74         std::vector<sptr<Notification::NotificationSlot>> slots {slot};
75 
76         uint8_t type = fuzzData->ConsumeIntegral<uint8_t>() % SLOT_TYPE_NUM;
77 
78         sptr<Notification::NotificationDoNotDisturbDate> distribuDate = new Notification::NotificationDoNotDisturbDate();
79 
80         datas.WriteString(stringData);
81         datas.WriteParcelable(notification);
82         ansManagerStub.HandlePublish(datas, reply);
83         datas.WriteParcelable(notification);
84         ansManagerStub.HandlePublishNotificationForIndirectProxy(datas, reply);
85         datas.WriteInt32(intData);
86         datas.WriteString(stringData);
87         datas.WriteInt32(intData);
88         ansManagerStub.HandleCancelAsBundle(datas, reply);
89         datas.WriteParcelable(bundleOption);
90         datas.WriteInt32(intData);
91         ansManagerStub.HandleCancelAsBundleOption(datas, reply);
92         datas.WriteInt32(intData);
93         ansManagerStub.HandleCancelAll(datas, reply);
94         datas.WriteInt32(intData);
95         datas.WriteString(stringData);
96         datas.WriteInt32(intData);
97         ansManagerStub.HandleCancel(datas, reply);
98         datas.WriteParcelable(bundleOption);
99         datas.WriteInt32(intData);
100         datas.WriteInt32(intData);
101         ansManagerStub.HandleCancelAsBundleAndUser(datas, reply);
102         datas.WriteInt32(type);
103         ansManagerStub.HandleAddSlotByType(datas, reply);
104         ansManagerStub.HandleAddSlots(datas, reply);
105         datas.WriteInt32(type);
106         ansManagerStub.HandleRemoveSlotByType(datas, reply);
107         ansManagerStub.HandleRemoveAllSlots(datas, reply);
108         ansManagerStub.HandleGetSlots(datas, reply);
109         datas.WriteInt32(type);
110         ansManagerStub.HandleGetSlotByType(datas, reply);
111         datas.WriteParcelable(bundleOption);
112         ansManagerStub.HandleGetSlotNumAsBundle(datas, reply);
113         datas.WriteParcelable(bundleOption);
114         datas.WriteInt32(intData);
115         ansManagerStub.HandleSetSlotFlagsAsBundle(datas, reply);
116         datas.WriteParcelable(bundleOption);
117         ansManagerStub.HandleGetSlotFlagsAsBundle(datas, reply);
118         datas.WriteInt32(intData);
119         ansManagerStub.HandleGetActiveNotifications(datas, reply);
120         ansManagerStub.HandleGetActiveNotificationNums(datas, reply);
121         ansManagerStub.HandleGetAllActiveNotifications(datas, reply);
122         std::vector<std::string> stringVector { stringData };
123         datas.WriteStringVector(stringVector);
124         ansManagerStub.HandleGetSpecialActiveNotifications(datas, reply);
125         datas.WriteParcelable(bundleOption);
126         datas.WriteInt32(intData);
127         datas.WriteString(stringData);
128         datas.WriteStringVector(stringVector);
129         ansManagerStub.HandleGetActiveNotificationByFilter(datas, reply);
130         datas.WriteString(stringData);
131         ansManagerStub.HandleCanPublishAsBundle(datas, reply);
132         datas.WriteParcelable(notification);
133         datas.WriteString(stringData);
134         ansManagerStub.HandlePublishAsBundle(datas, reply);
135         datas.WriteInt32(intData);
136         ansManagerStub.HandleSetNotificationBadgeNum(datas, reply);
137         ansManagerStub.HandleGetBundleImportance(datas, reply);
138         datas.WriteParcelable(distribuDate);
139         ansManagerStub.HandleSetDoNotDisturbDate(datas, reply);
140         ansManagerStub.HandleGetDoNotDisturbDate(datas, reply);
141         ansManagerStub.HandleDoesSupportDoNotDisturbMode(datas, reply);
142         datas.WriteParcelable(notification);
143         datas.WriteInt32(intData);
144         ansManagerStub.HandlePublishContinuousTaskNotification(datas, reply);
145         datas.WriteString(stringData);
146         datas.WriteInt32(intData);
147         ansManagerStub.HandleCancelContinuousTaskNotification(datas, reply);
148         ansManagerStub.HandleIsNotificationPolicyAccessGranted(datas, reply);
149         datas.WriteParcelable(bundleOption);
150         datas.WriteInt32(intData);
151         datas.WriteParcelable(buttonOption);
152         ansManagerStub.HandleTriggerLocalLiveView(datas, reply);
153         datas.WriteParcelable(bundleOption);
154         datas.WriteInt32(intData);
155         datas.WriteString(stringData);
156         datas.WriteInt32(intData);
157         ansManagerStub.HandleRemoveNotification(datas, reply);
158         ansManagerStub.HandleRemoveAllNotifications(datas, reply);
159         datas.WriteInt32(intData);
160         datas.WriteStringVector(stringVector);
161         datas.WriteInt32(intData);
162         ansManagerStub.HandleRemoveNotifications(datas, reply);
163         datas.WriteString(stringData);
164         datas.WriteInt32(intData);
165         ansManagerStub.HandleDelete(datas, reply);
166         datas.WriteParcelable(bundleOption);
167         ansManagerStub.HandleDeleteByBundle(datas, reply);
168         ansManagerStub.HandleDeleteAll(datas, reply);
169         datas.WriteParcelable(bundleOption);
170         datas.WriteInt32(intData);
171         ansManagerStub.HandleGetSlotByBundle(datas, reply);
172         ansManagerStub.HandleGetSlotsByBundle(datas, reply);
173         datas.WriteParcelable(bundleOption);
174         ansManagerStub.HandleUpdateSlots(datas, reply);
175         datas.WriteString(stringData);
176         datas.WriteRemoteObject(subscriber);
177         datas.WriteBool(boolData);
178         if (boolData) {
179             datas.WriteRemoteObject(subscriber);
180         }
181         ansManagerStub.HandleRequestEnableNotification(datas, reply);
182         datas.WriteString(stringData);
183         datas.WriteBool(boolData);
184         ansManagerStub.HandleSetNotificationsEnabledForBundle(datas, reply);
185         datas.WriteString(stringData);
186         datas.WriteBool(boolData);
187         ansManagerStub.HandleSetNotificationsEnabledForAllBundles(datas, reply);
188         datas.WriteString(stringData);
189         datas.WriteParcelable(bundleOption);
190         datas.WriteBool(boolData);
191         ansManagerStub.HandleSetNotificationsEnabledForSpecialBundle(datas, reply);
192         datas.WriteParcelable(bundleOption);
193         datas.WriteBool(boolData);
194         ansManagerStub.HandleSetShowBadgeEnabledForBundle(datas, reply);
195         datas.WriteParcelable(bundleOption);
196         ansManagerStub.HandleGetShowBadgeEnabledForBundle(datas, reply);
197         ansManagerStub.HandleGetShowBadgeEnabled(datas, reply);
198         datas.WriteRemoteObject(subscriber);
199         datas.WriteBool(boolData);
200         if (boolData) {
201             datas.WriteParcelable(info);
202         }
203         ansManagerStub.HandleSubscribe(datas, reply);
204         datas.WriteRemoteObject(subscriber);
205         ansManagerStub.HandleSubscribeSelf(datas, reply);
206         datas.WriteRemoteObject(subscriber);
207         datas.WriteBool(boolData);
208         if (boolData) {
209             datas.WriteParcelable(info);
210         }
211         datas.WriteBool(boolData);
212         ansManagerStub.HandleSubscribeLocalLiveView(datas, reply);
213         datas.WriteRemoteObject(subscriber);
214         datas.WriteBool(boolData);
215         if (boolData) {
216             datas.WriteParcelable(info);
217         }
218         ansManagerStub.HandleUnsubscribe(datas, reply);
219         ansManagerStub.HandleIsAllowedNotify(datas, reply);
220         ansManagerStub.HandleIsAllowedNotifySelf(datas, reply);
221         datas.WriteRemoteObject(subscriber);
222         ansManagerStub.HandleCanPopEnableNotificationDialog(datas, reply);
223         ansManagerStub.HandleRemoveEnableNotificationDialog(datas, reply);
224         datas.WriteParcelable(bundleOption);
225         ansManagerStub.HandleIsSpecialBundleAllowedNotify(datas, reply);
226         datas.WriteString(stringData);
227         datas.WriteInt32(intData);
228         ansManagerStub.HandleCancelGroup(datas, reply);
229         datas.WriteParcelable(bundleOption);
230         datas.WriteString(stringData);
231         ansManagerStub.HandleRemoveGroupByBundle(datas, reply);
232         ansManagerStub.HandleIsDistributedEnabled(datas, reply);
233         datas.WriteBool(boolData);
234         ansManagerStub.HandleEnableDistributed(datas, reply);
235         datas.WriteParcelable(bundleOption);
236         datas.WriteBool(boolData);
237         ansManagerStub.HandleEnableDistributedByBundle(datas, reply);
238         datas.WriteBool(boolData);
239         ansManagerStub.HandleEnableDistributedSelf(datas, reply);
240         datas.WriteParcelable(bundleOption);
241         ansManagerStub.HandleIsDistributedEnableByBundle(datas, reply);
242         ansManagerStub.HandleGetDeviceRemindType(datas, reply);
243         datas.WriteString(stringData);
244         datas.WriteString(stringData);
245         datas.WriteInt32(intData);
246         datas.WriteInt32(intData);
247         ansManagerStub.HandleShellDump(datas, reply);
248         datas.WriteString(stringData);
249         ansManagerStub.HandleIsSupportTemplate(datas, reply);
250         datas.WriteInt32(intData);
251         ansManagerStub.HandleIsSpecialUserAllowedNotifyByUser(datas, reply);
252         datas.WriteInt32(intData);
253         datas.WriteBool(boolData);
254         ansManagerStub.HandleSetNotificationsEnabledByUser(datas, reply);
255         datas.WriteInt32(intData);
256         ansManagerStub.HandleDeleteAllByUser(datas, reply);
257         datas.WriteInt32(intData);
258         datas.WriteParcelable(distribuDate);
259         ansManagerStub.HandleSetDoNotDisturbDateByUser(datas, reply);
260         datas.WriteInt32(intData);
261         ansManagerStub.HandleGetDoNotDisturbDateByUser(datas, reply);
262         datas.WriteInt32(intData);
263         ansManagerStub.HandleGetDoNotDisturbProfile(datas, reply);
264         datas.WriteParcelable(bundleOption);
265         datas.WriteInt32(type);
266         datas.WriteBool(boolData);
267         datas.WriteBool(boolData);
268         ansManagerStub.HandleSetEnabledForBundleSlot(datas, reply);
269         datas.WriteParcelable(bundleOption);
270         datas.WriteInt32(type);
271         ansManagerStub.HandleGetEnabledForBundleSlot(datas, reply);
272         datas.WriteInt32(type);
273         ansManagerStub.HandleGetEnabledForBundleSlotSelf(datas, reply);
274         datas.WriteInt32(intData);
275         datas.WriteBool(boolData);
276         ansManagerStub.HandleDistributedSetEnabledWithoutApp(datas, reply);
277         datas.WriteInt32(intData);
278         ansManagerStub.HandleDistributedGetEnabledWithoutApp(datas, reply);
279         datas.WriteInt32(intData);
280         datas.WriteInt32(intData);
281         ansManagerStub.HandleSetBadgeNumber(datas, reply);
282         datas.WriteParcelable(bundleOption);
283         datas.WriteInt32(intData);
284         ansManagerStub.HandleSetBadgeNumberByBundle(datas, reply);
285         ansManagerStub.HandleGetAllNotificationEnableStatus(datas, reply);
286         ansManagerStub.HandleRegisterPushCallback(datas, reply);
287         ansManagerStub.HandleUnregisterPushCallback(datas, reply);
288         ansManagerStub.HandleAddDoNotDisturbProfiles(datas, reply);
289         datas.WriteString(stringData);
290         datas.WriteBool(boolData);
291         ansManagerStub.HandleSetDistributedEnabledBySlot(datas, reply);
292         datas.WriteString(stringData);
293         ansManagerStub.HandleGetAllDistributedEnabledBundles(datas, reply);
294         datas.WriteInt32(intData);
295         ansManagerStub.HandleGetAllNotificationsBySlotType(datas, reply);
296         datas.WriteString(stringData);
297         ansManagerStub.HandleAllowUseReminder(datas, reply);
298         datas.WriteString(stringData);
299         datas.WriteInt32(intData);
300         datas.WriteInt32(intData);
301         ansManagerStub.HandleSetDeviceStatus(datas, reply);
302         datas.WriteParcelable(bundleOption);
303         datas.WriteString(stringData);
304         datas.WriteBool(boolData);
305         ansManagerStub.HandleSetDistributedEnabledByBundle(datas, reply);
306         ansManagerStub.HandleRemoveDoNotDisturbProfiles(datas, reply);
307         datas.WriteParcelable(bundleOption);
308         datas.WriteString(stringData);
309         ansManagerStub.HandleIsDistributedEnabledByBundle(datas, reply);
310         datas.WriteString(stringData);
311         datas.WriteBool(boolData);
312         ansManagerStub.HandleSetSmartReminderEnabled(datas, reply);
313         datas.WriteString(stringData);
314         ansManagerStub.HandleIsSmartReminderEnabled(datas, reply);
315         datas.WriteString(stringData);
316         datas.WriteString(stringData);
317         ansManagerStub.HandleSetAdditionConfig(datas, reply);
318         datas.WriteParcelable(bundleOption);
319         datas.WriteInt32(intData);
320         ansManagerStub.HandleCancelAsBundleWithAgent(datas, reply);
321         datas.WriteString(stringData);
322         datas.WriteInt32(intData);
323         ansManagerStub.HandleSetTargetDeviceStatus(datas, reply);
324         datas.WriteString(stringData);
325         ansManagerStub.HandleGetDeviceStatus(datas, reply);
326         sptr<Notification::NotificationRequest> notification2 = new Notification::NotificationRequest();
327         notification2->SetOwnerUid(fuzzData->ConsumeIntegral<int32_t>());
328         notification2->SetCreatorUid(fuzzData->ConsumeIntegral<int32_t>());
329         notification2->SetSlotType(Notification::NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
330         auto content2 = std::make_shared<Notification::NotificationNormalContent>();
331         notification2->SetContent(std::make_shared<Notification::NotificationContent>(content2));
332         ansManagerStub.Publish(stringData, notification2);
333         int notificationId = fuzzData->ConsumeIntegral<int>();
334         ansManagerStub.Cancel(notificationId, stringData, "");
335         ansManagerStub.CancelAll("");
336         int32_t userId = fuzzData->ConsumeIntegral<int32_t>();
337         ansManagerStub.CancelAsBundle(notificationId, stringData, userId);
338         Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType(type);
339         ansManagerStub.AddSlotByType(slotType);
340         ansManagerStub.AddSlots(slots);
341         ansManagerStub.RemoveSlotByType(slotType);
342         ansManagerStub.RemoveAllSlots();
343         ansManagerStub.GetSlotByType(slotType, slot);
344         ansManagerStub.GetSlots(slots);
345         uint64_t num = fuzzData->ConsumeIntegral<uint64_t>();
346         ansManagerStub.GetSlotNumAsBundle(bundleOption, num);
347         std::vector<sptr<Notification::NotificationRequest>> notifications;
348         ansManagerStub.GetActiveNotifications(notifications, "");
349         ansManagerStub.GetActiveNotificationNums(num);
350         std::vector<sptr<Notification::Notification>> notificationss;
351         ansManagerStub.GetAllActiveNotifications(notificationss);
352         std::vector<std::string> key;
353         ansManagerStub.GetSpecialActiveNotifications(key, notificationss);
354         bool canPublish = fuzzData->ConsumeBool();
355         ansManagerStub.CanPublishAsBundle(stringData, canPublish);
356         ansManagerStub.PublishAsBundle(notification, stringData);
357         ansManagerStub.SetNotificationBadgeNum(num);
358         int importance = fuzzData->ConsumeIntegral<int>();
359         ansManagerStub.GetBundleImportance(importance);
360         bool granted = fuzzData->ConsumeBool();
361         ansManagerStub.HasNotificationPolicyAccessPermission(granted);
362         int32_t removeReason = fuzzData->ConsumeIntegral<int32_t>();
363         ansManagerStub.RemoveNotification(bundleOption, notificationId, stringData, removeReason);
364         ansManagerStub.RemoveAllNotifications(bundleOption);
365         ansManagerStub.Delete(stringData, removeReason);
366         ansManagerStub.DeleteByBundle(bundleOption);
367         ansManagerStub.DeleteAll();
368         ansManagerStub.GetSlotsByBundle(bundleOption, slots);
369         ansManagerStub.UpdateSlots(bundleOption, slots);
370         sptr<Notification::AnsDialogCallback> dialogCallback = nullptr;
371         sptr<IRemoteObject> callerToken = nullptr;
372         ansManagerStub.RequestEnableNotification(stringData, dialogCallback, callerToken);
373         bool enabled = fuzzData->ConsumeBool();
374         ansManagerStub.SetNotificationsEnabledForBundle(stringData, enabled);
375         ansManagerStub.SetNotificationsEnabledForSpecialBundle(stringData, bundleOption, enabled);
376         ansManagerStub.SetShowBadgeEnabledForBundle(bundleOption, enabled);
377         ansManagerStub.GetShowBadgeEnabledForBundle(bundleOption, enabled);
378         ansManagerStub.GetShowBadgeEnabled(enabled);
379         bool allowed = fuzzData->ConsumeBool();
380         ansManagerStub.IsAllowedNotify(allowed);
381         ansManagerStub.IsSpecialBundleAllowedNotify(bundleOption, allowed);
382         ansManagerStub.CancelGroup(stringData, "");
383         ansManagerStub.RemoveGroupByBundle(bundleOption, stringData);
384         sptr<Notification::NotificationDoNotDisturbDate> date = new Notification::NotificationDoNotDisturbDate();
385         ansManagerStub.SetDoNotDisturbDate(date);
386         ansManagerStub.GetDoNotDisturbDate(date);
387         bool doesSupport = fuzzData->ConsumeBool();
388         ansManagerStub.DoesSupportDoNotDisturbMode(doesSupport);
389         ansManagerStub.IsDistributedEnabled(enabled);
390         ansManagerStub.EnableDistributedByBundle(bundleOption, enabled);
391         ansManagerStub.EnableDistributedSelf(enabled);
392         ansManagerStub.IsDistributedEnableByBundle(bundleOption, enabled);
393         Notification::NotificationConstant::RemindType remindType;
394         ansManagerStub.GetDeviceRemindType(remindType);
395         sptr<Notification::NotificationRequest> request = new Notification::NotificationRequest();
396         ansManagerStub.PublishContinuousTaskNotification(request);
397         ansManagerStub.CancelContinuousTaskNotification(stringData, notificationId);
398         bool support = fuzzData->ConsumeBool();
399         ansManagerStub.IsSupportTemplate(stringData, support);
400         ansManagerStub.IsSpecialUserAllowedNotify(userId, allowed);
401         int32_t deviceIds = fuzzData->ConsumeIntegral<int32_t>();
402         ansManagerStub.SetNotificationsEnabledByUser(deviceIds, enabled);
403         ansManagerStub.DeleteAllByUser(userId);
404         ansManagerStub.SetDoNotDisturbDate(date);
405         ansManagerStub.GetDoNotDisturbDate(date);
406         ansManagerStub.SetEnabledForBundleSlot(bundleOption, slotType, enabled, false);
407         ansManagerStub.GetEnabledForBundleSlot(bundleOption, slotType, enabled);
408         std::vector<std::string> dumpInfo;
409         ansManagerStub.ShellDump(stringData, stringData, userId, userId, dumpInfo);
410         ansManagerStub.SetSyncNotificationEnabledWithoutApp(userId, enabled);
411         ansManagerStub.GetSyncNotificationEnabledWithoutApp(userId, enabled);
412         return true;
413     }
414 }
415 
416 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)417 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
418 {
419     /* Run your code on data */
420     FuzzedDataProvider fdp(data, size);
421     std::vector<std::string> requestPermission = {
422         OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_CONTROLLER,
423         OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER,
424         OHOS::Notification::OHOS_PERMISSION_SET_UNREMOVABLE_NOTIFICATION
425     };
426     NativeTokenGet(requestPermission);
427     OHOS::DoSomethingInterestingWithMyAPI(&fdp);
428     return 0;
429 }
430