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 sptr<IRemoteObject> callerToken = nullptr;
80 advancedNotificationService.RequestEnableNotification(stringData, callerToken);
81 bool enabled = *data % ENABLE;
82 advancedNotificationService.SetNotificationsEnabledForBundle(stringData, enabled);
83 advancedNotificationService.SetNotificationsEnabledForAllBundles(stringData, enabled);
84 advancedNotificationService.SetNotificationsEnabledForSpecialBundle(stringData, bundleOption, enabled);
85 advancedNotificationService.SetShowBadgeEnabledForBundle(bundleOption, enabled);
86 advancedNotificationService.GetShowBadgeEnabledForBundle(bundleOption, enabled);
87 advancedNotificationService.GetShowBadgeEnabled(enabled);
88 sptr<Notification::AnsSubscriberStub> subscriber = new Notification::AnsSubscriberStub();
89 sptr<Notification::NotificationSubscribeInfo> info = new Notification::NotificationSubscribeInfo();
90 advancedNotificationService.Subscribe(subscriber, info);
91 advancedNotificationService.Unsubscribe(subscriber, info);
92 bool allowed = *data % ENABLE;
93 advancedNotificationService.IsAllowedNotify(allowed);
94 advancedNotificationService.IsAllowedNotifySelf(allowed);
95 advancedNotificationService.IsSpecialBundleAllowedNotify(bundleOption, allowed);
96 advancedNotificationService.CancelGroup(stringData);
97 advancedNotificationService.RemoveGroupByBundle(bundleOption, stringData);
98 sptr<Notification::NotificationDoNotDisturbDate> date = new Notification::NotificationDoNotDisturbDate();
99 advancedNotificationService.SetDoNotDisturbDate(date);
100 advancedNotificationService.GetDoNotDisturbDate(date);
101 bool doesSupport = *data % ENABLE;
102 advancedNotificationService.DoesSupportDoNotDisturbMode(doesSupport);
103 advancedNotificationService.IsDistributedEnabled(enabled);
104 advancedNotificationService.EnableDistributedByBundle(bundleOption, enabled);
105 advancedNotificationService.EnableDistributedSelf(enabled);
106 advancedNotificationService.EnableDistributed(enabled);
107 advancedNotificationService.IsDistributedEnableByBundle(bundleOption, enabled);
108 Notification::NotificationConstant::RemindType remindType;
109 advancedNotificationService.GetDeviceRemindType(remindType);
110 sptr<Notification::NotificationRequest> request = new Notification::NotificationRequest();
111 advancedNotificationService.PublishContinuousTaskNotification(request);
112 advancedNotificationService.CancelContinuousTaskNotification(stringData, notificationId);
113 sptr<Notification::ReminderRequest> reminder = new Notification::ReminderRequest();
114 advancedNotificationService.PublishReminder(reminder);
115 int32_t reminderId = static_cast<int32_t>(GetU32Data(data));
116 advancedNotificationService.CancelReminder(reminderId);
117 std::vector<sptr<Notification::ReminderRequest>> reminders;
118 advancedNotificationService.GetValidReminders(reminders);
119 advancedNotificationService.CancelAllReminders();
120 bool support = *data % ENABLE;
121 advancedNotificationService.IsSupportTemplate(stringData, support);
122 advancedNotificationService.IsSpecialUserAllowedNotify(userId, allowed);
123 int32_t deviceIds = static_cast<int32_t>(GetU32Data(data));
124 advancedNotificationService.SetNotificationsEnabledByUser(deviceIds, enabled);
125 advancedNotificationService.DeleteAllByUser(userId);
126 advancedNotificationService.SetDoNotDisturbDate(date);
127 advancedNotificationService.GetDoNotDisturbDate(date);
128 advancedNotificationService.SetEnabledForBundleSlot(bundleOption, slotType, enabled);
129 advancedNotificationService.GetEnabledForBundleSlot(bundleOption, slotType, enabled);
130 std::vector<std::string> dumpInfo;
131 advancedNotificationService.ShellDump(stringData, stringData, userId, dumpInfo);
132 advancedNotificationService.SetSyncNotificationEnabledWithoutApp(userId, enabled);
133 advancedNotificationService.GetSyncNotificationEnabledWithoutApp(userId, enabled);
134 int32_t badgeNum = static_cast<int32_t>(GetU32Data(data));
135 advancedNotificationService.SetBadgeNumber(badgeNum);
136
137 return true;
138 }
139 }
140
141 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)142 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
143 {
144 /* Run your code on data */
145 char *ch = ParseData(data, size);
146 if (ch != nullptr && size >= GetU32Size()) {
147 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
148 free(ch);
149 ch = nullptr;
150 }
151 return 0;
152 }
153