1 /*
2 * Copyright (c) 2023-2025 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::NotificationSubscribeInfo> info = new Notification::NotificationSubscribeInfo();
104 bool allowed = fuzzData->ConsumeBool();
105 service->IsAllowedNotify(allowed);
106 service->IsAllowedNotifySelf(bundleOption, allowed);
107 service->IsAllowedNotifyForBundle(bundleOption, allowed);
108 service->IsSpecialBundleAllowedNotify(bundleOption, allowed);
109 service->CancelGroup(stringData, fuzzData->ConsumeRandomLengthString());
110 service->RemoveGroupByBundle(bundleOption, stringData);
111 sptr<Notification::NotificationDoNotDisturbDate> date = new Notification::NotificationDoNotDisturbDate();
112 service->SetDoNotDisturbDate(date);
113 service->GetDoNotDisturbDate(date);
114 bool doesSupport = fuzzData->ConsumeBool();
115 service->DoesSupportDoNotDisturbMode(doesSupport);
116 service->IsDistributedEnabled(enabled);
117 service->EnableDistributedByBundle(bundleOption, enabled);
118 service->EnableDistributedSelf(enabled);
119 service->EnableDistributed(enabled);
120 service->IsDistributedEnableByBundle(bundleOption, enabled);
121 int32_t remindType;
122 service->GetDeviceRemindType(remindType);
123 sptr<Notification::NotificationRequest> request = new Notification::NotificationRequest();
124 service->PublishContinuousTaskNotification(request);
125 service->CancelContinuousTaskNotification(stringData, notificationId);
126 bool support = fuzzData->ConsumeBool();
127 service->IsSupportTemplate(stringData, support);
128 service->IsSpecialUserAllowedNotify(userId, allowed);
129 int32_t deviceIds = fuzzData->ConsumeIntegral<int32_t>();
130 service->SetNotificationsEnabledByUser(deviceIds, enabled);
131 service->DeleteAllByUser(userId);
132 service->SetDoNotDisturbDate(date);
133 service->GetDoNotDisturbDate(date);
134 service->SetEnabledForBundleSlot(bundleOption, slotType, enabled, false);
135 service->GetEnabledForBundleSlot(bundleOption, slotType, enabled);
136 std::vector<std::string> dumpInfo;
137 service->ShellDump(stringData, stringData, userId, userId, dumpInfo);
138 service->SetSyncNotificationEnabledWithoutApp(userId, enabled);
139 service->GetSyncNotificationEnabledWithoutApp(userId, enabled);
140 int32_t badgeNum = fuzzData->ConsumeIntegral<int32_t>();
141 service->SetBadgeNumber(badgeNum, fuzzData->ConsumeRandomLengthString());
142 sptr<Notification::IAnsDialogCallback> dialogCallback = new Notification::AnsDialogCallbackProxy(nullptr);
143 std::shared_ptr<Notification::NotificationUnifiedGroupInfo> groupInfo;
144 bool enable = fuzzData->ConsumeBool();
145 std::string bundleName = fuzzData->ConsumeRandomLengthString();
146 std::string phoneNumber = fuzzData->ConsumeRandomLengthString();
147 std::string groupName = fuzzData->ConsumeRandomLengthString();
148 std::string deviceType = fuzzData->ConsumeRandomLengthString();
149 std::string localSwitch = fuzzData->ConsumeRandomLengthString();
150 std::vector<std::shared_ptr<Notification::NotificationRecord>> recordList;
151 bool isNative = fuzzData->ConsumeBool();
152 service->CanPopEnableNotificationDialog(nullptr, enable, bundleName);
153 std::vector<std::string> keys;
154 std::string key1 = fuzzData->ConsumeRandomLengthString();
155 keys.emplace_back(fuzzData->ConsumeRandomLengthString());
156 service->RemoveNotifications(keys, fuzzData->ConsumeIntegral<int32_t>());
157 service->SetBadgeNumberByBundle(bundleOption, fuzzData->ConsumeIntegral<int32_t>());
158 service->SetDistributedEnabledByBundle(bundleOption, fuzzData->ConsumeRandomLengthString(),
159 fuzzData->ConsumeBool());
160 service->IsDistributedEnableByBundle(bundleOption, enable);
161 service->SetDefaultNotificationEnabled(bundleOption, enabled);
162 service->ExcuteCancelAll(bundleOption, fuzzData->ConsumeIntegral<int32_t>());
163 service->ExcuteDelete(stringData, fuzzData->ConsumeIntegral<int32_t>());
164 service->HandleBadgeEnabledChanged(bundleOption, enabled);
165 service->RemoveSystemLiveViewNotifications(bundleName, fuzzData->ConsumeIntegral<int32_t>());
166 service->RemoveSystemLiveViewNotificationsOfSa(fuzzData->ConsumeIntegral<int32_t>());
167 service->TriggerLocalLiveView(bundleOption, fuzzData->ConsumeIntegral<int32_t>(), buttonOption);
168 service->RemoveNotificationBySlot(bundleOption, slot, fuzzData->ConsumeIntegral<int32_t>());
169 service->IsNeedSilentInDoNotDisturbMode(phoneNumber, fuzzData->ConsumeIntegral<int32_t>());
170 service->CheckNeedSilent(phoneNumber, fuzzData->ConsumeIntegral<int32_t>(),
171 fuzzData->ConsumeIntegral<int32_t>());
172 service->ExcuteCancelGroupCancel(bundleOption, groupName, fuzzData->ConsumeIntegral<int32_t>());
173 service->RemoveNotificationFromRecordList(recordList);
174 service->UpdateUnifiedGroupInfo(key1, groupInfo);
175 service->PublishNotificationBySa(request);
176 service->IsDistributedEnabledByBundle(bundleOption, deviceType, enabled);
177 service->DuplicateMsgControl(request);
178 service->DeleteDuplicateMsgs(bundleOption);
179 service->RemoveExpiredUniqueKey();
180 service->SetSmartReminderEnabled(deviceType, enabled);
181 service->IsSmartReminderEnabled(deviceType, enabled);
182 service->SetTargetDeviceStatus(deviceType, fuzzData->ConsumeIntegral<int32_t>(), "");
183 service->ClearAllNotificationGroupInfo(localSwitch);
184 service->IsDistributedEnabledBySlot(slotType, deviceType, enabled);
185
186 OHOS::DoTestForAdvancedNotificationUtils(service, fuzzData);
187 OHOS::DoTestForAdvancedNotificationService(service, fuzzData);
188 return true;
189 }
190
DoTestForAdvancedNotificationUtils(std::shared_ptr<Notification::AdvancedNotificationService> service,FuzzedDataProvider * fuzzData)191 bool DoTestForAdvancedNotificationUtils(std::shared_ptr<Notification::AdvancedNotificationService> service,
192 FuzzedDataProvider *fuzzData)
193 {
194 std::string randomString = fuzzData->ConsumeRandomLengthString();
195 int32_t randomInt32 = fuzzData->ConsumeIntegral<int32_t>();
196 sptr<Notification::NotificationBundleOption> bundleOption = new Notification::NotificationBundleOption();
197 bundleOption->SetBundleName(randomString);
198 bundleOption->SetUid(randomInt32);
199 sptr<Notification::NotificationBundleOption> targetBundleOption = nullptr;
200 sptr<Notification::NotificationRequest> request = new Notification::NotificationRequest();
201 request->SetSlotType(Notification::NotificationConstant::SlotType::LIVE_VIEW);
202 auto content = std::make_shared<Notification::NotificationLiveViewContent>();
203 request->SetContent(std::make_shared<Notification::NotificationContent>(content));
204 request->SetOwnerUid(randomInt32);
205 request->SetCreatorUid(randomInt32);
206 auto flag = std::make_shared<Notification::NotificationFlags>();
207 request->SetFlags(flag);
208 service->GetAppTargetBundle(bundleOption, targetBundleOption);
209 std::vector<std::string> infos;
210 infos.emplace_back(randomString);
211 service->SetAgentNotification(request, randomString);
212 service->ActiveNotificationDump(randomString, randomInt32, randomInt32, infos);
213 service->RecentNotificationDump(randomString, randomInt32, randomInt32, infos);
214 service->OnBundleRemoved(bundleOption);
215 service->OnBundleDataAdd(bundleOption);
216 service->OnBundleDataUpdate(bundleOption);
217 service->GetBundlesOfActiveUser();
218 service->InitNotificationEnableList();
219 std::shared_ptr<Notification::NotificationRecord> record =
220 service->MakeNotificationRecord(request, bundleOption);
221 record->slot = new Notification::NotificationSlot(Notification::NotificationConstant::SlotType::LIVE_VIEW);
222 service->PrePublishNotificationBySa(request, randomInt32, randomString);
223 service->SetRequestBundleInfo(request, randomInt32, randomString);
224 service->OnResourceRemove(randomInt32);
225 service->CheckApiCompatibility(bundleOption);
226 service->OnBundleDataCleared(bundleOption);
227 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
228 service->CheckPublishWithoutApp(randomInt32, request);
229 service->GetLocalNotificationKeys(bundleOption);
230 service->OnDistributedPublish(randomString, randomString, request);
231 service->OnDistributedUpdate(randomString, randomString, request);
232 service->OnDistributedDelete(randomString, randomString, randomString, randomInt32);
233 #endif
234 return true;
235 }
236
DoTestForAdvancedNotificationService(std::shared_ptr<Notification::AdvancedNotificationService> service,FuzzedDataProvider * fuzzData)237 bool DoTestForAdvancedNotificationService(std::shared_ptr<Notification::AdvancedNotificationService> service,
238 FuzzedDataProvider *fuzzData)
239 {
240 std::string randomString = fuzzData->ConsumeRandomLengthString();
241 int32_t randomInt32 = fuzzData->ConsumeIntegral<int32_t>();
242 sptr<Notification::NotificationBundleOption> bundleOption = new Notification::NotificationBundleOption();
243 bundleOption->SetBundleName(randomString);
244 bundleOption->SetUid(randomInt32);
245 sptr<Notification::NotificationBundleOption> targetBundleOption = nullptr;
246 sptr<Notification::NotificationRequest> request = new Notification::NotificationRequest();
247 request->SetSlotType(Notification::NotificationConstant::SlotType::LIVE_VIEW);
248 auto content = std::make_shared<Notification::NotificationLiveViewContent>();
249 request->SetContent(std::make_shared<Notification::NotificationContent>(content));
250 request->SetOwnerUid(randomInt32);
251 request->SetCreatorUid(randomInt32);
252 auto flag = std::make_shared<Notification::NotificationFlags>();
253 request->SetFlags(flag);
254 std::shared_ptr<Notification::NotificationRecord> record =
255 service->MakeNotificationRecord(request, bundleOption);
256 record->slot = new Notification::NotificationSlot(Notification::NotificationConstant::SlotType::LIVE_VIEW);
257 service->QueryDoNotDisturbProfile(randomInt32, randomString, randomString);
258 service->CheckDoNotDisturbProfile(record);
259 service->DoNotDisturbUpdataReminderFlags(record);
260 service->UpdateSlotAuthInfo(record);
261 service->Filter(record, fuzzData->ConsumeBool());
262 service->ChangeNotificationByControlFlags(record, fuzzData->ConsumeBool());
263 service->CheckPublishPreparedNotification(record, fuzzData->ConsumeBool());
264 service->UpdateInNotificationList(record);
265 service->PublishInNotificationList(record);
266 service->IsNeedPushCheck(request);
267 return true;
268 }
269 }
270
271 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)272 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
273 {
274 /* Run your code on data */
275 FuzzedDataProvider fdp(data, size);
276 std::vector<std::string> requestPermission = {
277 OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_CONTROLLER,
278 OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER,
279 OHOS::Notification::OHOS_PERMISSION_SET_UNREMOVABLE_NOTIFICATION
280 };
281 SystemHapTokenGet(requestPermission);
282 OHOS::DoSomethingInterestingWithMyAPI(&fdp);
283 return 0;
284 }
285