• 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 #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 constexpr uint8_t ENABLE = 2;
26 
27 namespace OHOS {
DoSomethingInterestingWithMyAPI(const char * data,size_t size)28     bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
29     {
30         std::string stringData(data, size);
31         Notification::AnsManagerStub ansManagerStub;
32         uint32_t code = GetU32Data(data);
33         MessageParcel datas;
34         MessageParcel reply;
35         MessageOption flags;
36         ansManagerStub.OnRemoteRequest(code, datas, reply, flags);
37         ansManagerStub.HandlePublish(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.HandleRemoveNotification(datas, reply);
65         ansManagerStub.HandleRemoveAllNotifications(datas, reply);
66         ansManagerStub.HandleDelete(datas, reply);
67         ansManagerStub.HandleDeleteByBundle(datas, reply);
68         ansManagerStub.HandleDeleteAll(datas, reply);
69         ansManagerStub.HandleGetSlotsByBundle(datas, reply);
70         ansManagerStub.HandleUpdateSlots(datas, reply);
71         ansManagerStub.HandleRequestEnableNotification(datas, reply);
72         ansManagerStub.HandleSetNotificationsEnabledForBundle(datas, reply);
73         ansManagerStub.HandleSetNotificationsEnabledForAllBundles(datas, reply);
74         ansManagerStub.HandleSetNotificationsEnabledForSpecialBundle(datas, reply);
75         ansManagerStub.HandleSetShowBadgeEnabledForBundle(datas, reply);
76         ansManagerStub.HandleGetShowBadgeEnabledForBundle(datas, reply);
77         ansManagerStub.HandleGetShowBadgeEnabled(datas, reply);
78         ansManagerStub.HandleSubscribe(datas, reply);
79         ansManagerStub.HandleUnsubscribe(datas, reply);
80         ansManagerStub.HandleIsAllowedNotify(datas, reply);
81         ansManagerStub.HandleIsAllowedNotifySelf(datas, reply);
82         ansManagerStub.HandleIsSpecialBundleAllowedNotify(datas, reply);
83         ansManagerStub.HandleCancelGroup(datas, reply);
84         ansManagerStub.HandleRemoveGroupByBundle(datas, reply);
85         ansManagerStub.HandleIsDistributedEnabled(datas, reply);
86         ansManagerStub.HandleEnableDistributed(datas, reply);
87         ansManagerStub.HandleEnableDistributedByBundle(datas, reply);
88         ansManagerStub.HandleEnableDistributedSelf(datas, reply);
89         ansManagerStub.HandleIsDistributedEnableByBundle(datas, reply);
90         ansManagerStub.HandleGetDeviceRemindType(datas, reply);
91         ansManagerStub.HandleShellDump(datas, reply);
92         ansManagerStub.HandlePublishReminder(datas, reply);
93         ansManagerStub.HandleCancelReminder(datas, reply);
94         ansManagerStub.HandleCancelAllReminders(datas, reply);
95         ansManagerStub.HandleGetValidReminders(datas, reply);
96         ansManagerStub.HandleIsSupportTemplate(datas, reply);
97         ansManagerStub.HandleIsSpecialUserAllowedNotifyByUser(datas, reply);
98         ansManagerStub.HandleSetNotificationsEnabledByUser(datas, reply);
99         ansManagerStub.HandleDeleteAllByUser(datas, reply);
100         ansManagerStub.HandleSetDoNotDisturbDateByUser(datas, reply);
101         ansManagerStub.HandleGetDoNotDisturbDateByUser(datas, reply);
102         ansManagerStub.HandleSetEnabledForBundleSlot(datas, reply);
103         ansManagerStub.HandleGetEnabledForBundleSlot(datas, reply);
104         ansManagerStub.HandleDistributedSetEnabledWithoutApp(datas, reply);
105         ansManagerStub.HandleDistributedGetEnabledWithoutApp(datas, reply);
106         sptr<Notification::NotificationRequest> notification = new Notification::NotificationRequest();
107         ansManagerStub.Publish(stringData, notification);
108         int notificationId = static_cast<int>(GetU32Data(data));
109         ansManagerStub.Cancel(notificationId, stringData);
110         ansManagerStub.CancelAll();
111         int32_t userId = static_cast<int32_t>(GetU32Data(data));
112         ansManagerStub.CancelAsBundle(notificationId, stringData, userId);
113         uint8_t type = *data % SLOT_TYPE_NUM;
114         Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType(type);
115         ansManagerStub.AddSlotByType(slotType);
116         std::vector<sptr<Notification::NotificationSlot>> slots;
117         ansManagerStub.AddSlots(slots);
118         ansManagerStub.RemoveSlotByType(slotType);
119         ansManagerStub.RemoveAllSlots();
120         sptr<Notification::NotificationSlot> slot = new Notification::NotificationSlot();
121         ansManagerStub.GetSlotByType(slotType, slot);
122         ansManagerStub.GetSlots(slots);
123         sptr<Notification::NotificationBundleOption> bundleOption = new Notification::NotificationBundleOption();
124         uint64_t num = static_cast<uint64_t>(GetU32Data(data));
125         ansManagerStub.GetSlotNumAsBundle(bundleOption, num);
126         std::vector<sptr<Notification::NotificationRequest>> notifications;
127         ansManagerStub.GetActiveNotifications(notifications);
128         ansManagerStub.GetActiveNotificationNums(num);
129         std::vector<sptr<Notification::Notification>> notificationss;
130         ansManagerStub.GetAllActiveNotifications(notificationss);
131         std::vector<std::string> key;
132         ansManagerStub.GetSpecialActiveNotifications(key, notificationss);
133         ansManagerStub.SetNotificationAgent(stringData);
134         ansManagerStub.GetNotificationAgent(stringData);
135         bool canPublish = *data % ENABLE;
136         ansManagerStub.CanPublishAsBundle(stringData, canPublish);
137         ansManagerStub.PublishAsBundle(notification, stringData);
138         ansManagerStub.SetNotificationBadgeNum(num);
139         int importance = static_cast<int>(GetU32Data(data));
140         ansManagerStub.GetBundleImportance(importance);
141         bool granted = *data % ENABLE;
142         ansManagerStub.HasNotificationPolicyAccessPermission(granted);
143         int32_t removeReason = static_cast<int32_t>(GetU32Data(data));
144         ansManagerStub.RemoveNotification(bundleOption, notificationId, stringData, removeReason);
145         ansManagerStub.RemoveAllNotifications(bundleOption);
146         ansManagerStub.Delete(stringData, removeReason);
147         ansManagerStub.DeleteByBundle(bundleOption);
148         ansManagerStub.DeleteAll();
149         ansManagerStub.GetSlotsByBundle(bundleOption, slots);
150         ansManagerStub.UpdateSlots(bundleOption, slots);
151         sptr<IRemoteObject> callerToken = nullptr;
152         ansManagerStub.RequestEnableNotification(stringData, callerToken);
153         bool enabled = *data % ENABLE;
154         ansManagerStub.SetNotificationsEnabledForBundle(stringData, enabled);
155         ansManagerStub.SetNotificationsEnabledForSpecialBundle(stringData, bundleOption, enabled);
156         ansManagerStub.SetShowBadgeEnabledForBundle(bundleOption, enabled);
157         ansManagerStub.GetShowBadgeEnabledForBundle(bundleOption, enabled);
158         ansManagerStub.GetShowBadgeEnabled(enabled);
159         bool allowed = *data % ENABLE;
160         ansManagerStub.IsAllowedNotify(allowed);
161         ansManagerStub.IsSpecialBundleAllowedNotify(bundleOption, allowed);
162         ansManagerStub.CancelGroup(stringData);
163         ansManagerStub.RemoveGroupByBundle(bundleOption, stringData);
164         sptr<Notification::NotificationDoNotDisturbDate> date = new Notification::NotificationDoNotDisturbDate();
165         ansManagerStub.SetDoNotDisturbDate(date);
166         ansManagerStub.GetDoNotDisturbDate(date);
167         bool doesSupport = *data % ENABLE;
168         ansManagerStub.DoesSupportDoNotDisturbMode(doesSupport);
169         ansManagerStub.IsDistributedEnabled(enabled);
170         ansManagerStub.EnableDistributedByBundle(bundleOption, enabled);
171         ansManagerStub.EnableDistributedSelf(enabled);
172         ansManagerStub.IsDistributedEnableByBundle(bundleOption, enabled);
173         Notification::NotificationConstant::RemindType remindType;
174         ansManagerStub.GetDeviceRemindType(remindType);
175         sptr<Notification::NotificationRequest> request = new Notification::NotificationRequest();
176         ansManagerStub.PublishContinuousTaskNotification(request);
177         ansManagerStub.CancelContinuousTaskNotification(stringData, notificationId);
178         sptr<Notification::ReminderRequest> reminder = new Notification::ReminderRequest();
179         ansManagerStub.PublishReminder(reminder);
180         int32_t reminderId = static_cast<int32_t>(GetU32Data(data));
181         ansManagerStub.CancelReminder(reminderId);
182         std::vector<sptr<Notification::ReminderRequest>> reminders;
183         ansManagerStub.GetValidReminders(reminders);
184         ansManagerStub.CancelAllReminders();
185         bool support = *data % ENABLE;
186         ansManagerStub.IsSupportTemplate(stringData, support);
187         ansManagerStub.IsSpecialUserAllowedNotify(userId, allowed);
188         int32_t deviceIds = static_cast<int32_t>(GetU32Data(data));
189         ansManagerStub.SetNotificationsEnabledByUser(deviceIds, enabled);
190         ansManagerStub.DeleteAllByUser(userId);
191         ansManagerStub.SetDoNotDisturbDate(date);
192         ansManagerStub.GetDoNotDisturbDate(date);
193         ansManagerStub.SetEnabledForBundleSlot(bundleOption, slotType, enabled);
194         ansManagerStub.GetEnabledForBundleSlot(bundleOption, slotType, enabled);
195         std::vector<std::string> dumpInfo;
196         ansManagerStub.ShellDump(stringData, stringData, userId, dumpInfo);
197         ansManagerStub.SetSyncNotificationEnabledWithoutApp(userId, enabled);
198         ansManagerStub.GetSyncNotificationEnabledWithoutApp(userId, enabled);
199         return true;
200     }
201 }
202 
203 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)204 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
205 {
206     /* Run your code on data */
207     char *ch = ParseData(data, size);
208     if (ch != nullptr && size >= GetU32Size()) {
209         OHOS::DoSomethingInterestingWithMyAPI(ch, size);
210         free(ch);
211         ch = nullptr;
212     }
213     return 0;
214 }
215