• 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 <fuzzer/FuzzedDataProvider.h>
17 
18 #define private public
19 #define protected public
20 #include "advanced_notification_service.h"
21 #undef private
22 #undef protected
23 #include "ans_dialog_callback_proxy.h"
24 #include "ans_permission_def.h"
25 #include "ansmanagerstub_fuzzer.h"
26 #include "notification_record.h"
27 #include "notification_request.h"
28 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
29 #include "swing_call_back_proxy.h"
30 #endif
31 
32 constexpr uint8_t SLOT_TYPE_NUM = 5;
33 
34 namespace OHOS {
35 
DoSomethingInterestingWithMyAPI(FuzzedDataProvider * fuzzData)36     bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider *fuzzData)
37     {
38         auto service = std::make_shared<Notification::AdvancedNotificationService>();
39         service->InitPublishProcess();
40         service->CreateDialogManager();
41         std::string stringData = fuzzData->ConsumeRandomLengthString();
42         sptr<Notification::NotificationRequest> notification = new Notification::NotificationRequest();
43         notification->SetOwnerUid(fuzzData->ConsumeIntegral<int32_t>());
44         notification->SetCreatorUid(fuzzData->ConsumeIntegral<int32_t>());
45         notification->SetSlotType(Notification::NotificationConstant::SlotType::LIVE_VIEW);
46         auto content = std::make_shared<Notification::NotificationLiveViewContent>();
47         notification->SetContent(std::make_shared<Notification::NotificationContent>(content));
48         service->Publish(stringData, notification);
49         service->PublishWithMaxCapacity(stringData, notification);
50         int notificationId = fuzzData->ConsumeIntegral<int32_t>();
51         service->Cancel(notificationId, stringData, fuzzData->ConsumeRandomLengthString());
52         service->CancelAll(fuzzData->ConsumeRandomLengthString());
53         int32_t userId = fuzzData->ConsumeIntegral<int32_t>();
54         service->CancelAsBundle(notificationId, stringData, userId);
55         uint8_t type = fuzzData->ConsumeIntegral<uint8_t>() % SLOT_TYPE_NUM;
56         Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType(type);
57         service->AddSlotByType(slotType);
58         std::vector<sptr<Notification::NotificationSlot>> slots;
59         service->AddSlots(slots);
60         service->RemoveSlotByType(slotType);
61         service->RemoveAllSlots();
62         sptr<Notification::NotificationSlot> slot = new Notification::NotificationSlot();
63         service->GetSlotByType(slotType, slot);
64         service->GetSlots(slots);
65         sptr<Notification::NotificationBundleOption> bundleOption = new Notification::NotificationBundleOption();
66         sptr<Notification::NotificationButtonOption> buttonOption = new Notification::NotificationButtonOption();
67         bundleOption->SetBundleName(fuzzData->ConsumeRandomLengthString());
68         bundleOption->SetUid(fuzzData->ConsumeIntegral<int32_t>());
69         uint64_t num = fuzzData->ConsumeIntegral<uint64_t>();
70         service->CancelAsBundle(bundleOption, fuzzData->ConsumeIntegral<int32_t>());
71         service->CancelAsBundle(bundleOption, fuzzData->ConsumeIntegral<int32_t>(), userId);
72         service->CancelAsBundleWithAgent(bundleOption, fuzzData->ConsumeIntegral<int32_t>());
73         service->GetSlotNumAsBundle(bundleOption, num);
74         std::vector<sptr<Notification::NotificationRequest>> notifications;
75         service->GetActiveNotifications(notifications, fuzzData->ConsumeRandomLengthString());
76         service->GetActiveNotificationNums(num);
77         std::vector<sptr<Notification::Notification>> notificationss;
78         service->GetAllActiveNotifications(notificationss);
79         std::vector<std::string> key;
80         service->GetSpecialActiveNotifications(key, notificationss);
81         bool canPublish = fuzzData->ConsumeBool();
82         service->CanPublishAsBundle(stringData, canPublish);
83         service->PublishAsBundle(notification, stringData);
84         service->PublishAsBundleWithMaxCapacity(notification, stringData);
85         service->SetNotificationBadgeNum(num);
86         int importance = fuzzData->ConsumeIntegral<int32_t>();
87         service->GetBundleImportance(importance);
88         bool granted = fuzzData->ConsumeBool();
89         service->HasNotificationPolicyAccessPermission(granted);
90         int32_t removeReason = fuzzData->ConsumeIntegral<int32_t>();
91         service->RemoveNotification(bundleOption, notificationId, stringData, removeReason);
92         service->RemoveAllNotifications(bundleOption);
93         service->Delete(stringData, removeReason);
94         service->DeleteByBundle(bundleOption);
95         service->DeleteAll();
96         service->GetSlotsByBundle(bundleOption, slots);
97         service->UpdateSlots(bundleOption, slots);
98         bool enabled = fuzzData->ConsumeBool();
99         service->SetNotificationsEnabledForBundle(stringData, enabled);
100         service->SetNotificationsEnabledForAllBundles(stringData, enabled);
101         service->SetNotificationsEnabledForSpecialBundle(stringData, bundleOption, enabled);
102         service->SetShowBadgeEnabledForBundle(bundleOption, enabled);
103         service->GetShowBadgeEnabledForBundle(bundleOption, enabled);
104         service->GetShowBadgeEnabled(enabled);
105         sptr<Notification::NotificationSubscribeInfo> info = new Notification::NotificationSubscribeInfo();
106         bool allowed = fuzzData->ConsumeBool();
107         service->IsAllowedNotify(allowed);
108         service->IsAllowedNotifySelf(allowed);
109         service->IsAllowedNotifySelf(bundleOption, allowed);
110         service->IsAllowedNotifyForBundle(bundleOption, allowed);
111         service->IsSpecialBundleAllowedNotify(bundleOption, allowed);
112         service->CancelGroup(stringData, fuzzData->ConsumeRandomLengthString());
113         service->RemoveGroupByBundle(bundleOption, stringData);
114         sptr<Notification::NotificationDoNotDisturbDate> date = new Notification::NotificationDoNotDisturbDate();
115         service->SetDoNotDisturbDateByUser(userId, date);
116         service->GetDoNotDisturbDateByUser(userId, date);
117         bool doesSupport = fuzzData->ConsumeBool();
118         service->DoesSupportDoNotDisturbMode(doesSupport);
119         service->IsDistributedEnabled(enabled);
120         service->EnableDistributedByBundle(bundleOption, enabled);
121         service->EnableDistributedSelf(enabled);
122         service->EnableDistributed(enabled);
123         service->IsDistributedEnableByBundle(bundleOption, enabled);
124         int32_t remindType;
125         service->GetDeviceRemindType(remindType);
126         sptr<Notification::NotificationRequest> request = new Notification::NotificationRequest();
127         service->PublishContinuousTaskNotification(request);
128         service->CancelContinuousTaskNotification(stringData, notificationId);
129         bool support = fuzzData->ConsumeBool();
130         service->IsSupportTemplate(stringData, support);
131         service->IsSpecialUserAllowedNotify(userId, allowed);
132         int32_t deviceIds = fuzzData->ConsumeIntegral<int32_t>();
133         service->SetNotificationsEnabledByUser(deviceIds, enabled);
134         service->DeleteAllByUser(userId);
135         service->SetDoNotDisturbDate(date);
136         service->GetDoNotDisturbDate(date);
137         service->SetEnabledForBundleSlot(bundleOption, slotType, enabled, false);
138         service->GetEnabledForBundleSlot(bundleOption, slotType, enabled);
139         std::vector<std::string> dumpInfo;
140         service->ShellDump(stringData, stringData, userId, userId, dumpInfo);
141         service->SetSyncNotificationEnabledWithoutApp(userId, enabled);
142         service->GetSyncNotificationEnabledWithoutApp(userId, enabled);
143         int32_t badgeNum = fuzzData->ConsumeIntegral<int32_t>();
144         service->SetBadgeNumber(badgeNum, fuzzData->ConsumeRandomLengthString());
145         std::shared_ptr<Notification::NotificationUnifiedGroupInfo> groupInfo;
146         bool enable = fuzzData->ConsumeBool();
147         std::string bundleName = fuzzData->ConsumeRandomLengthString();
148         std::string phoneNumber = fuzzData->ConsumeRandomLengthString();
149         std::string groupName = fuzzData->ConsumeRandomLengthString();
150         std::string deviceType = fuzzData->ConsumeRandomLengthString();
151         std::string localSwitch = fuzzData->ConsumeRandomLengthString();
152         std::vector<std::shared_ptr<Notification::NotificationRecord>> recordList;
153         bool isNative = fuzzData->ConsumeBool();
154         service->CanPopEnableNotificationDialog(nullptr, enable, bundleName);
155         service->RemoveEnableNotificationDialog();
156         service->RemoveEnableNotificationDialog(bundleOption);
157         std::vector<std::string> keys;
158         std::string key1 = fuzzData->ConsumeRandomLengthString();
159         keys.emplace_back(fuzzData->ConsumeRandomLengthString());
160         service->RemoveNotifications(keys, fuzzData->ConsumeIntegral<int32_t>());
161         service->SetBadgeNumberByBundle(bundleOption, fuzzData->ConsumeIntegral<int32_t>());
162         service->SetDistributedEnabledByBundle(bundleOption, fuzzData->ConsumeRandomLengthString(),
163             fuzzData->ConsumeBool());
164         service->IsDistributedEnableByBundle(bundleOption, enable);
165         service->SetDefaultNotificationEnabled(bundleOption, enabled);
166         service->ExcuteCancelAll(bundleOption, fuzzData->ConsumeIntegral<int32_t>());
167         service->ExcuteDelete(stringData, fuzzData->ConsumeIntegral<int32_t>());
168         service->HandleBadgeEnabledChanged(bundleOption, enabled);
169         service->RemoveSystemLiveViewNotifications(bundleName, fuzzData->ConsumeIntegral<int32_t>());
170         service->RemoveSystemLiveViewNotificationsOfSa(fuzzData->ConsumeIntegral<int32_t>());
171         service->TriggerLocalLiveView(bundleOption, fuzzData->ConsumeIntegral<int32_t>(), buttonOption);
172         service->RemoveNotificationBySlot(bundleOption, slot, fuzzData->ConsumeIntegral<int32_t>());
173         service->IsNeedSilentInDoNotDisturbMode(phoneNumber, fuzzData->ConsumeIntegral<int32_t>());
174         service->CheckNeedSilent(phoneNumber, fuzzData->ConsumeIntegral<int32_t>(),
175             fuzzData->ConsumeIntegral<int32_t>());
176         service->ExcuteCancelGroupCancel(bundleOption, groupName, fuzzData->ConsumeIntegral<int32_t>());
177         service->RemoveNotificationFromRecordList(recordList);
178         service->UpdateUnifiedGroupInfo(key1, groupInfo);
179         service->PublishNotificationBySa(request);
180         service->IsDistributedEnabledByBundle(bundleOption, deviceType, enabled);
181         service->DuplicateMsgControl(request);
182         service->DeleteDuplicateMsgs(bundleOption);
183         service->RemoveExpiredUniqueKey();
184         service->SetSmartReminderEnabled(deviceType, enabled);
185         service->IsSmartReminderEnabled(deviceType, enabled);
186 
187         uint32_t status = fuzzData->ConsumeIntegral<uint32_t>();
188         uint32_t controlFlag = fuzzData->ConsumeIntegral<uint32_t>();
189         service->SetTargetDeviceStatus(deviceType, status, stringData);
190         service->SetTargetDeviceStatus(deviceType, status, controlFlag, stringData, userId);
191         service->ClearAllNotificationGroupInfo(localSwitch);
192 
193         service->SetSlotFlagsAsBundle(bundleOption, fuzzData->ConsumeIntegral<int32_t>());
194         uint32_t slotFlags;
195         service->GetSlotFlagsAsBundle(bundleOption, slotFlags);
196         service->GetActiveNotificationByFilter(bundleOption, notificationId, stringData, userId, keys, request);
197         service->GetSlotByBundle(bundleOption, slotType, slot);
198         std::vector<Notification::NotificationBundleOption> bundleOptions;
199         service->GetAllNotificationEnabledBundles(bundleOptions);
200         std::vector<sptr<Notification::NotificationDoNotDisturbProfile>> profiles;
201         sptr<Notification::NotificationDoNotDisturbProfile> profile =
202             new Notification::NotificationDoNotDisturbProfile();
203         profiles.emplace_back(profile);
204         service->AddDoNotDisturbProfiles(profiles);
205         service->RemoveDoNotDisturbProfiles(profiles);
206         std::string value = fuzzData->ConsumeRandomLengthString();
207 
208         sptr<Notification::NotificationCheckRequest> notificationCheckRequest =
209             new Notification::NotificationCheckRequest();
210         service->RegisterPushCallback(nullptr, notificationCheckRequest);
211         service->UnregisterPushCallback();
212         service->SetAdditionConfig(key1, value);
213         service->PublishNotificationForIndirectProxy(notification);
214         service->PublishNotificationForIndirectProxyWithMaxCapacity(notification);
215 
216         bool enabledByslot;
217         service->GetEnabledForBundleSlotSelf(slotType, enabledByslot);
218 
219         service->Subscribe(nullptr, info);
220         service->Subscribe(nullptr);
221         service->Unsubscribe(nullptr, info);
222         service->Unsubscribe(nullptr);
223         service->SubscribeSelf(nullptr);
224 
225         service->SubscribeLocalLiveView(nullptr, info, isNative);
226         service->SubscribeLocalLiveView(nullptr, isNative);
227 
228         int32_t uid = fuzzData->ConsumeIntegral<int32_t>();
229         sptr<Notification::IAnsDialogCallback> callback = new Notification::AnsDialogCallbackProxy(nullptr);
230         service->RequestEnableNotification(stringData, callback, nullptr);
231         service->RequestEnableNotification(stringData, callback);
232 
233         service->SetDistributedEnabledBySlot(slotType, deviceType, enabled);
234         service->GetAllDistribuedEnabledBundles(deviceType, bundleOptions);
235         std::vector<sptr<Notification::Notification>> notificationsVector;
236         service->GetAllNotificationsBySlotType(notificationsVector, slotType);
237         service->AllowUseReminder(bundleName, allowed);
238         int32_t deviceStatus;
239         service->GetTargetDeviceStatus(deviceType, deviceStatus);
240         bool isPaused = fuzzData->ConsumeBool();
241         service->UpdateNotificationTimerByUid(uid, isPaused);
242 
243         service->SetBadgeNumberForDhByBundle(bundleOption, badgeNum);
244         service->GetNotificationRequestByHashCode(stringData, notification);
245         sptr<Notification::NotificationOperationInfo> operationInfo = new Notification::NotificationOperationInfo();
246         operationInfo->SetActionName(fuzzData->ConsumeRandomLengthString());
247         operationInfo->SetUserInput(fuzzData->ConsumeRandomLengthString());
248         operationInfo->SetHashCode(fuzzData->ConsumeRandomLengthString());
249         operationInfo->SetEventId(fuzzData->ConsumeRandomLengthString());
250         service->DistributeOperation(operationInfo, nullptr);
251         service->SetHashCodeRule(fuzzData->ConsumeIntegral<uint32_t>());
252         service->GetAllLiveViewEnabledBundles(bundleOptions);
253         sptr<Notification::NotificationDisable> notificationDisable = new Notification::NotificationDisable();
254         service->DisableNotificationFeature(notificationDisable);
255         service->ReplyDistributeOperation(stringData, fuzzData->ConsumeIntegral<int32_t>());
256         service->UpdateNotificationTimerByUid(uid, isPaused);
257 
258 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
259         sptr<Notification::ISwingCallBack> swingCallBack = new Notification::SwingCallBackProxy(nullptr);
260         service->RegisterSwingCallback(swingCallBack->AsObject());
261 #endif
262 
263         return true;
264     }
265 }
266 
267 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)268 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
269 {
270     /* Run your code on data */
271     FuzzedDataProvider fdp(data, size);
272     std::vector<std::string> requestPermission = {
273         OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_CONTROLLER,
274         OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER,
275         OHOS::Notification::OHOS_PERMISSION_SET_UNREMOVABLE_NOTIFICATION
276     };
277     SystemHapTokenGet(requestPermission);
278     OHOS::DoSomethingInterestingWithMyAPI(&fdp);
279     return 0;
280 }
281