• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "notification_live_view_content.h"
17 #include "notification_record.h"
18 #include <cstdint>
19 #include <fuzzer/FuzzedDataProvider.h>
20 #include <memory>
21 #include <string>
22 #define private public
23 #define protected public
24 #include "advanced_notification_service.h"
25 #undef private
26 #undef protected
27 #include "advancednotificationservice_fuzzer.h"
28 #include "ans_dialog_callback_proxy.h"
29 #include "ans_subscriber_stub.h"
30 #include "ans_permission_def.h"
31 #include "notification_request.h"
32 
33 constexpr uint8_t SLOT_TYPE_NUM = 5;
34 
35 namespace OHOS {
36 
DoSomethingInterestingWithMyAPI(FuzzedDataProvider * fuzzData)37     bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider *fuzzData)
38     {
39         auto service = std::make_shared<Notification::AdvancedNotificationService>();
40         service->InitPublishProcess();
41         service->CreateDialogManager();
42         std::string stringData = fuzzData->ConsumeRandomLengthString();
43         sptr<Notification::NotificationRequest> notification = new Notification::NotificationRequest();
44         notification->SetOwnerUid(fuzzData->ConsumeIntegral<int32_t>());
45         notification->SetCreatorUid(fuzzData->ConsumeIntegral<int32_t>());
46         notification->SetSlotType(Notification::NotificationConstant::SlotType::LIVE_VIEW);
47         auto content = std::make_shared<Notification::NotificationLiveViewContent>();
48         notification->SetContent(std::make_shared<Notification::NotificationContent>(content));
49         service->Publish(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->CancelAsBundleWithAgent(bundleOption, fuzzData->ConsumeIntegral<int32_t>());
72         service->GetSlotNumAsBundle(bundleOption, num);
73         std::vector<sptr<Notification::NotificationRequest>> notifications;
74         service->GetActiveNotifications(notifications, fuzzData->ConsumeRandomLengthString());
75         service->GetActiveNotificationNums(num);
76         std::vector<sptr<Notification::Notification>> notificationss;
77         service->GetAllActiveNotifications(notificationss);
78         std::vector<std::string> key;
79         service->GetSpecialActiveNotifications(key, notificationss);
80         bool canPublish = fuzzData->ConsumeBool();
81         service->CanPublishAsBundle(stringData, canPublish);
82         service->PublishAsBundle(notification, stringData);
83         service->SetNotificationBadgeNum(num);
84         int importance = fuzzData->ConsumeIntegral<int32_t>();
85         service->GetBundleImportance(importance);
86         bool granted = fuzzData->ConsumeBool();
87         service->HasNotificationPolicyAccessPermission(granted);
88         int32_t removeReason = fuzzData->ConsumeIntegral<int32_t>();
89         service->RemoveNotification(bundleOption, notificationId, stringData, removeReason);
90         service->RemoveAllNotifications(bundleOption);
91         service->Delete(stringData, removeReason);
92         service->DeleteByBundle(bundleOption);
93         service->DeleteAll();
94         service->GetSlotsByBundle(bundleOption, slots);
95         service->UpdateSlots(bundleOption, slots);
96         bool enabled = fuzzData->ConsumeBool();
97         service->SetNotificationsEnabledForBundle(stringData, enabled);
98         service->SetNotificationsEnabledForAllBundles(stringData, enabled);
99         service->SetNotificationsEnabledForSpecialBundle(stringData, bundleOption, enabled);
100         service->SetShowBadgeEnabledForBundle(bundleOption, enabled);
101         service->GetShowBadgeEnabledForBundle(bundleOption, enabled);
102         service->GetShowBadgeEnabled(enabled);
103         sptr<Notification::AnsSubscriberStub> subscriber = new Notification::AnsSubscriberStub();
104         sptr<Notification::NotificationSubscribeInfo> info = new Notification::NotificationSubscribeInfo();
105         service->Subscribe(subscriber, info);
106         service->Unsubscribe(subscriber, info);
107         bool allowed = fuzzData->ConsumeBool();
108         service->IsAllowedNotify(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->SetDoNotDisturbDate(date);
116         service->GetDoNotDisturbDate(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         Notification::NotificationConstant::RemindType 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         sptr<Notification::AnsDialogCallback> dialogCallback = new Notification::AnsDialogCallbackProxy(nullptr);
146         sptr<IRemoteObject> callerToken = new Notification::AnsSubscriberStub();
147         std::shared_ptr<Notification::NotificationUnifiedGroupInfo> groupInfo;
148         service->RequestEnableNotification(stringData, dialogCallback, callerToken);
149         bool enable = fuzzData->ConsumeBool();
150         std::string bundleName = fuzzData->ConsumeRandomLengthString();
151         std::string phoneNumber = fuzzData->ConsumeRandomLengthString();
152         std::string groupName = fuzzData->ConsumeRandomLengthString();
153         std::string deviceType = fuzzData->ConsumeRandomLengthString();
154         std::string localSwitch = fuzzData->ConsumeRandomLengthString();
155         std::vector<std::shared_ptr<Notification::NotificationRecord>> recordList;
156         bool isNative = fuzzData->ConsumeBool();
157         service->CanPopEnableNotificationDialog(nullptr, enable, bundleName);
158         std::vector<std::string> keys;
159         std::string key1 = fuzzData->ConsumeRandomLengthString();
160         keys.emplace_back(fuzzData->ConsumeRandomLengthString());
161         service->RemoveNotifications(keys, fuzzData->ConsumeIntegral<int32_t>());
162         service->SetBadgeNumberByBundle(bundleOption, fuzzData->ConsumeIntegral<int32_t>());
163         service->SetDistributedEnabledByBundle(bundleOption, fuzzData->ConsumeRandomLengthString(),
164             fuzzData->ConsumeBool());
165         service->IsDistributedEnableByBundle(bundleOption, enable);
166         service->SetDefaultNotificationEnabled(bundleOption, enabled);
167         service->ExcuteCancelAll(bundleOption, fuzzData->ConsumeIntegral<int32_t>());
168         service->ExcuteDelete(stringData, fuzzData->ConsumeIntegral<int32_t>());
169         service->HandleBadgeEnabledChanged(bundleOption, enabled);
170         service->RemoveSystemLiveViewNotifications(bundleName, fuzzData->ConsumeIntegral<int32_t>());
171         service->RemoveSystemLiveViewNotificationsOfSa(fuzzData->ConsumeIntegral<int32_t>());
172         service->TriggerLocalLiveView(bundleOption, fuzzData->ConsumeIntegral<int32_t>(), buttonOption);
173         service->RemoveNotificationBySlot(bundleOption, slot, fuzzData->ConsumeIntegral<int32_t>());
174         service->IsNeedSilentInDoNotDisturbMode(phoneNumber, fuzzData->ConsumeIntegral<int32_t>());
175         service->CheckNeedSilent(phoneNumber, fuzzData->ConsumeIntegral<int32_t>(),
176             fuzzData->ConsumeIntegral<int32_t>());
177         service->ExcuteCancelGroupCancel(bundleOption, groupName, fuzzData->ConsumeIntegral<int32_t>());
178         service->RemoveNotificationFromRecordList(recordList);
179         service->UpdateUnifiedGroupInfo(key1, groupInfo);
180         service->PublishNotificationBySa(request);
181         service->IsDistributedEnabledByBundle(bundleOption, deviceType, enabled);
182         service->DuplicateMsgControl(request);
183         service->DeleteDuplicateMsgs(bundleOption);
184         service->RemoveExpiredUniqueKey();
185         service->SetSmartReminderEnabled(deviceType, enabled);
186         service->IsSmartReminderEnabled(deviceType, enabled);
187         service->SetTargetDeviceStatus(deviceType, fuzzData->ConsumeIntegral<int32_t>());
188         service->ClearAllNotificationGroupInfo(localSwitch);
189 
190         OHOS::DoTestForAdvancedNotificationUtils(service, fuzzData);
191         OHOS::DoTestForAdvancedNotificationService(service, fuzzData);
192         return true;
193     }
194 
DoTestForAdvancedNotificationUtils(std::shared_ptr<Notification::AdvancedNotificationService> service,FuzzedDataProvider * fuzzData)195     bool DoTestForAdvancedNotificationUtils(std::shared_ptr<Notification::AdvancedNotificationService> service,
196         FuzzedDataProvider *fuzzData)
197     {
198         std::string randomString = fuzzData->ConsumeRandomLengthString();
199         int32_t randomInt32 = fuzzData->ConsumeIntegral<int32_t>();
200         sptr<Notification::NotificationBundleOption> bundleOption = new Notification::NotificationBundleOption();
201         bundleOption->SetBundleName(randomString);
202         bundleOption->SetUid(randomInt32);
203         sptr<Notification::NotificationBundleOption> targetBundleOption = nullptr;
204         sptr<Notification::NotificationRequest> request = new Notification::NotificationRequest();
205         request->SetSlotType(Notification::NotificationConstant::SlotType::LIVE_VIEW);
206         auto content = std::make_shared<Notification::NotificationLiveViewContent>();
207         request->SetContent(std::make_shared<Notification::NotificationContent>(content));
208         request->SetOwnerUid(randomInt32);
209         request->SetCreatorUid(randomInt32);
210         auto flag = std::make_shared<Notification::NotificationFlags>();
211         request->SetFlags(flag);
212         service->GetAppTargetBundle(bundleOption, targetBundleOption);
213         std::vector<std::string> infos;
214         infos.emplace_back(randomString);
215         service->SetAgentNotification(request, randomString);
216         service->ActiveNotificationDump(randomString, randomInt32, randomInt32, infos);
217         service->RecentNotificationDump(randomString, randomInt32, randomInt32, infos);
218         service->OnBundleRemoved(bundleOption);
219         service->OnBundleDataAdd(bundleOption);
220         service->OnBundleDataUpdate(bundleOption);
221         service->GetBundlesOfActiveUser();
222         service->InitNotificationEnableList();
223         std::shared_ptr<Notification::NotificationRecord> record =
224             service->MakeNotificationRecord(request, bundleOption);
225         record->slot = new Notification::NotificationSlot(Notification::NotificationConstant::SlotType::LIVE_VIEW);
226         service->PrePublishNotificationBySa(request, randomInt32, randomString);
227         service->SetRequestBundleInfo(request, randomInt32, randomString);
228         service->OnResourceRemove(randomInt32);
229         service->CheckApiCompatibility(bundleOption);
230         service->OnBundleDataCleared(bundleOption);
231 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
232         service->CheckPublishWithoutApp(randomInt32, request);
233         service->GetLocalNotificationKeys(bundleOption);
234         service->OnDistributedPublish(randomString, randomString, request);
235         service->OnDistributedUpdate(randomString, randomString, request);
236         service->OnDistributedDelete(randomString, randomString, randomString, randomInt32);
237 #endif
238         return true;
239     }
240 
DoTestForAdvancedNotificationService(std::shared_ptr<Notification::AdvancedNotificationService> service,FuzzedDataProvider * fuzzData)241     bool DoTestForAdvancedNotificationService(std::shared_ptr<Notification::AdvancedNotificationService> service,
242         FuzzedDataProvider *fuzzData)
243     {
244         std::string randomString = fuzzData->ConsumeRandomLengthString();
245         int32_t randomInt32 = fuzzData->ConsumeIntegral<int32_t>();
246         sptr<Notification::NotificationBundleOption> bundleOption = new Notification::NotificationBundleOption();
247         bundleOption->SetBundleName(randomString);
248         bundleOption->SetUid(randomInt32);
249         sptr<Notification::NotificationBundleOption> targetBundleOption = nullptr;
250         sptr<Notification::NotificationRequest> request = new Notification::NotificationRequest();
251         request->SetSlotType(Notification::NotificationConstant::SlotType::LIVE_VIEW);
252         auto content = std::make_shared<Notification::NotificationLiveViewContent>();
253         request->SetContent(std::make_shared<Notification::NotificationContent>(content));
254         request->SetOwnerUid(randomInt32);
255         request->SetCreatorUid(randomInt32);
256         auto flag = std::make_shared<Notification::NotificationFlags>();
257         request->SetFlags(flag);
258         std::shared_ptr<Notification::NotificationRecord> record =
259             service->MakeNotificationRecord(request, bundleOption);
260         record->slot = new Notification::NotificationSlot(Notification::NotificationConstant::SlotType::LIVE_VIEW);
261         service->PublishPreparedNotification(request, bundleOption, fuzzData->ConsumeBool());
262         service->QueryDoNotDisturbProfile(randomInt32, randomString, randomString);
263         service->CheckDoNotDisturbProfile(record);
264         service->DoNotDisturbUpdataReminderFlags(record);
265         service->UpdateSlotAuthInfo(record);
266         service->Filter(record, fuzzData->ConsumeBool());
267         service->ChangeNotificationByControlFlags(record, fuzzData->ConsumeBool());
268         service->CheckPublishPreparedNotification(record, fuzzData->ConsumeBool());
269         service->UpdateInNotificationList(record);
270         service->PublishInNotificationList(record);
271         service->IsNeedPushCheck(request);
272         return true;
273     }
274 }
275 
276 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)277 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
278 {
279     /* Run your code on data */
280     FuzzedDataProvider fdp(data, size);
281     std::vector<std::string> requestPermission = {
282         OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_CONTROLLER,
283         OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER,
284         OHOS::Notification::OHOS_PERMISSION_SET_UNREMOVABLE_NOTIFICATION
285     };
286     SystemHapTokenGet(requestPermission);
287     OHOS::DoSomethingInterestingWithMyAPI(&fdp);
288     return 0;
289 }
290