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 "bundle_info.h"
17 #include "notification_live_view_content.h"
18 #include "notification_record.h"
19 #include <cstdint>
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 constexpr uint8_t ENABLE = 2;
35
36 namespace OHOS {
37
DoSomethingInterestingWithMyAPI(FuzzData fuzzData)38 bool DoSomethingInterestingWithMyAPI(FuzzData fuzzData)
39 {
40 auto service = std::make_shared<Notification::AdvancedNotificationService>();
41 service->InitPublishProcess();
42 service->CreateDialogManager();
43 std::string stringData = fuzzData.GenerateRandomString();
44 sptr<Notification::NotificationRequest> notification = new Notification::NotificationRequest();
45 notification->SetOwnerUid(fuzzData.GenerateRandomInt32());
46 notification->SetCreatorUid(fuzzData.GenerateRandomInt32());
47 notification->SetSlotType(Notification::NotificationConstant::SlotType::LIVE_VIEW);
48 auto content = std::make_shared<Notification::NotificationLiveViewContent>();
49 notification->SetContent(std::make_shared<Notification::NotificationContent>(content));
50 service->Publish(stringData, notification);
51 int notificationId = fuzzData.GenerateRandomInt32();
52 service->Cancel(notificationId, stringData, fuzzData.GenerateRandomInt32());
53 service->CancelAll(fuzzData.GenerateRandomInt32());
54 int32_t userId = fuzzData.GenerateRandomInt32();
55 service->CancelAsBundle(notificationId, stringData, userId);
56 uint8_t type = fuzzData.GetData<uint8_t>() % SLOT_TYPE_NUM;
57 Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType(type);
58 service->AddSlotByType(slotType);
59 std::vector<sptr<Notification::NotificationSlot>> slots;
60 service->AddSlots(slots);
61 service->RemoveSlotByType(slotType);
62 service->RemoveAllSlots();
63 sptr<Notification::NotificationSlot> slot = new Notification::NotificationSlot();
64 service->GetSlotByType(slotType, slot);
65 service->GetSlots(slots);
66 sptr<Notification::NotificationBundleOption> bundleOption = new Notification::NotificationBundleOption();
67 sptr<Notification::NotificationButtonOption> buttonOption = new Notification::NotificationButtonOption();
68 bundleOption->SetBundleName(fuzzData.GenerateRandomString());
69 bundleOption->SetUid(fuzzData.GenerateRandomInt32());
70 uint64_t num = fuzzData.GetData<uint64_t>();
71 service->CancelAsBundle(bundleOption, fuzzData.GenerateRandomInt32());
72 service->CancelAsBundleWithAgent(bundleOption, fuzzData.GenerateRandomInt32());
73 service->GetSlotNumAsBundle(bundleOption, num);
74 std::vector<sptr<Notification::NotificationRequest>> notifications;
75 service->GetActiveNotifications(notifications, fuzzData.GenerateRandomInt32());
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 service->SetNotificationAgent(stringData);
82 service->GetNotificationAgent(stringData);
83 bool canPublish = fuzzData.GenerateRandomBool();
84 service->CanPublishAsBundle(stringData, canPublish);
85 service->PublishAsBundle(notification, stringData);
86 service->SetNotificationBadgeNum(num);
87 int importance = fuzzData.GenerateRandomInt32();
88 service->GetBundleImportance(importance);
89 bool granted = fuzzData.GenerateRandomBool();
90 service->HasNotificationPolicyAccessPermission(granted);
91 int32_t removeReason = fuzzData.GenerateRandomInt32();
92 service->RemoveNotification(bundleOption, notificationId, stringData, removeReason);
93 service->RemoveAllNotifications(bundleOption);
94 service->Delete(stringData, removeReason);
95 service->DeleteByBundle(bundleOption);
96 service->DeleteAll();
97 service->GetSlotsByBundle(bundleOption, slots);
98 service->UpdateSlots(bundleOption, slots);
99 bool enabled = fuzzData.GenerateRandomBool();
100 service->SetNotificationsEnabledForBundle(stringData, enabled);
101 service->SetNotificationsEnabledForAllBundles(stringData, enabled);
102 service->SetNotificationsEnabledForSpecialBundle(stringData, bundleOption, enabled);
103 service->SetShowBadgeEnabledForBundle(bundleOption, enabled);
104 service->GetShowBadgeEnabledForBundle(bundleOption, enabled);
105 service->GetShowBadgeEnabled(enabled);
106 sptr<Notification::AnsSubscriberStub> subscriber = new Notification::AnsSubscriberStub();
107 sptr<Notification::NotificationSubscribeInfo> info = new Notification::NotificationSubscribeInfo();
108 service->Subscribe(subscriber, info);
109 service->Unsubscribe(subscriber, info);
110 bool allowed = fuzzData.GenerateRandomBool();
111 service->IsAllowedNotify(allowed);
112 service->IsAllowedNotifySelf(bundleOption, allowed);
113 service->IsAllowedNotifyForBundle(bundleOption, allowed);
114 service->IsSpecialBundleAllowedNotify(bundleOption, allowed);
115 service->CancelGroup(stringData, fuzzData.GenerateRandomInt32());
116 service->RemoveGroupByBundle(bundleOption, stringData);
117 sptr<Notification::NotificationDoNotDisturbDate> date = new Notification::NotificationDoNotDisturbDate();
118 service->SetDoNotDisturbDate(date);
119 service->GetDoNotDisturbDate(date);
120 bool doesSupport = fuzzData.GenerateRandomBool();
121 service->DoesSupportDoNotDisturbMode(doesSupport);
122 service->IsDistributedEnabled(enabled);
123 service->EnableDistributedByBundle(bundleOption, enabled);
124 service->EnableDistributedSelf(enabled);
125 service->EnableDistributed(enabled);
126 service->IsDistributedEnableByBundle(bundleOption, enabled);
127 Notification::NotificationConstant::RemindType remindType;
128 service->GetDeviceRemindType(remindType);
129 sptr<Notification::NotificationRequest> request = new Notification::NotificationRequest();
130 service->PublishContinuousTaskNotification(request);
131 service->CancelContinuousTaskNotification(stringData, notificationId);
132 sptr<Notification::ReminderRequest> reminder = new Notification::ReminderRequest();
133 service->PublishReminder(reminder);
134 int32_t reminderId = fuzzData.GenerateRandomInt32();
135 service->CancelReminder(reminderId);
136 std::vector<sptr<Notification::ReminderRequest>> reminders;
137 service->GetValidReminders(reminders);
138 service->CancelAllReminders();
139 uint64_t excludeDate = fuzzData.GetData<uint64_t>();
140 service->AddExcludeDate(reminderId, excludeDate);
141 service->DelExcludeDates(reminderId);
142 std::vector<uint64_t> excludeDates;
143 service->GetExcludeDates(reminderId, excludeDates);
144 bool support = fuzzData.GenerateRandomBool();
145 service->IsSupportTemplate(stringData, support);
146 service->IsSpecialUserAllowedNotify(userId, allowed);
147 int32_t deviceIds = fuzzData.GenerateRandomInt32();
148 service->SetNotificationsEnabledByUser(deviceIds, enabled);
149 service->DeleteAllByUser(userId);
150 service->SetDoNotDisturbDate(date);
151 service->GetDoNotDisturbDate(date);
152 service->SetEnabledForBundleSlot(bundleOption, slotType, enabled, false);
153 service->GetEnabledForBundleSlot(bundleOption, slotType, enabled);
154 std::vector<std::string> dumpInfo;
155 service->ShellDump(stringData, stringData, userId, userId, dumpInfo);
156 service->SetSyncNotificationEnabledWithoutApp(userId, enabled);
157 service->GetSyncNotificationEnabledWithoutApp(userId, enabled);
158 int32_t badgeNum = fuzzData.GenerateRandomInt32();
159 service->SetBadgeNumber(badgeNum, fuzzData.GenerateRandomInt32());
160 sptr<Notification::AnsDialogCallback> dialogCallback = new Notification::AnsDialogCallbackProxy(nullptr);
161 sptr<IRemoteObject> callerToken = new Notification::AnsSubscriberStub();
162 std::shared_ptr<Notification::NotificationUnifiedGroupInfo> groupInfo;
163 service->RequestEnableNotification(stringData, dialogCallback, callerToken);
164 bool enable = fuzzData.GenerateRandomBool();
165 std::string bundleName = fuzzData.GenerateRandomString();
166 std::string phoneNumber = fuzzData.GenerateRandomString();
167 std::string groupName = fuzzData.GenerateRandomString();
168 std::string deviceType = fuzzData.GenerateRandomString();
169 std::string localSwitch = fuzzData.GenerateRandomString();
170 std::vector<std::shared_ptr<Notification::NotificationRecord>> recordList;
171 bool isNative = fuzzData.GenerateRandomBool();
172 service->CanPopEnableNotificationDialog(nullptr, enable, bundleName);
173 std::vector<std::string> keys;
174 std::string key1 = fuzzData.GenerateRandomString();
175 keys.emplace_back(fuzzData.GenerateRandomString());
176 service->RemoveNotifications(keys, fuzzData.GenerateRandomInt32());
177 service->SetBadgeNumberByBundle(bundleOption, fuzzData.GenerateRandomInt32());
178 service->SetDistributedEnabledByBundle(bundleOption, fuzzData.GenerateRandomString(),
179 fuzzData.GenerateRandomBool());
180 service->IsDistributedEnableByBundle(bundleOption, enable);
181 service->SetDefaultNotificationEnabled(bundleOption, enabled);
182 service->ExcuteCancelAll(bundleOption, fuzzData.GenerateRandomInt32());
183 service->ExcuteDelete(stringData, fuzzData.GenerateRandomInt32());
184 service->HandleBadgeEnabledChanged(bundleOption, enabled);
185 service->RemoveSystemLiveViewNotifications(bundleName, fuzzData.GenerateRandomInt32());
186 service->RemoveSystemLiveViewNotificationsOfSa(fuzzData.GenerateRandomInt32());
187 service->TriggerLocalLiveView(bundleOption, fuzzData.GenerateRandomInt32(), buttonOption);
188 service->RemoveNotificationBySlot(bundleOption, slot, fuzzData.GenerateRandomInt32());
189 service->IsNeedSilentInDoNotDisturbMode(phoneNumber, fuzzData.GenerateRandomInt32());
190 service->CheckNeedSilent(phoneNumber, fuzzData.GenerateRandomInt32(), fuzzData.GenerateRandomInt32());
191 service->ExcuteCancelGroupCancel(bundleOption, groupName, fuzzData.GenerateRandomInt32());
192 service->RemoveNotificationFromRecordList(recordList);
193 service->UpdateUnifiedGroupInfo(key1, groupInfo);
194 service->PublishNotificationBySa(request);
195 service->IsDistributedEnabledByBundle(bundleOption, deviceType, enabled);
196 service->DuplicateMsgControl(request);
197 service->DeleteDuplicateMsgs(bundleOption);
198 service->RemoveExpiredUniqueKey();
199 service->SetSmartReminderEnabled(deviceType, enabled);
200 service->IsSmartReminderEnabled(deviceType, enabled);
201 service->SetTargetDeviceStatus(deviceType, fuzzData.GenerateRandomInt32());
202 service->ClearAllNotificationGroupInfo(localSwitch);
203
204 OHOS::DoTestForAdvancedNotificationUtils(service, fuzzData);
205 OHOS::DoTestForAdvancedNotificationService(service, fuzzData);
206 return true;
207 }
208
DoTestForAdvancedNotificationUtils(std::shared_ptr<Notification::AdvancedNotificationService> service, FuzzData fuzzData)209 bool DoTestForAdvancedNotificationUtils(std::shared_ptr<Notification::AdvancedNotificationService> service,
210 FuzzData fuzzData)
211 {
212 std::string randomString = fuzzData.GenerateRandomString();
213 int32_t randomInt32 = fuzzData.GenerateRandomInt32();
214 int64_t randomInt64 = fuzzData.GetData<int64_t>();
215 sptr<Notification::NotificationBundleOption> bundleOption = new Notification::NotificationBundleOption();
216 bundleOption->SetBundleName(randomString);
217 bundleOption->SetUid(randomInt32);
218 sptr<Notification::NotificationBundleOption> targetBundleOption = nullptr;
219 sptr<Notification::NotificationRequest> request = new Notification::NotificationRequest();
220 request->SetSlotType(Notification::NotificationConstant::SlotType::LIVE_VIEW);
221 auto content = std::make_shared<Notification::NotificationLiveViewContent>();
222 request->SetContent(std::make_shared<Notification::NotificationContent>(content));
223 request->SetOwnerUid(randomInt32);
224 request->SetCreatorUid(randomInt32);
225 auto flag = std::make_shared<Notification::NotificationFlags>();
226 request->SetFlags(flag);
227 service->GetAppTargetBundle(bundleOption, targetBundleOption);
228 std::vector<std::string> infos;
229 infos.emplace_back(randomString);
230 service->SetAgentNotification(request, randomString);
231 service->ActiveNotificationDump(randomString, randomInt32, randomInt32, infos);
232 service->RecentNotificationDump(randomString, randomInt32, randomInt32, infos);
233 service->OnBundleRemoved(bundleOption);
234 service->OnBundleDataAdd(bundleOption);
235 service->OnBundleDataUpdate(bundleOption);
236 service->GetBundlesOfActiveUser();
237 service->InitNotificationEnableList();
238 AppExecFwk::BundleInfo bundleInfo;
239 service->GetBundleInfoByNotificationBundleOption(bundleOption, bundleInfo);
240 std::shared_ptr<Notification::NotificationRecord> record =
241 service->MakeNotificationRecord(request, bundleOption);
242 record->slot = new Notification::NotificationSlot(Notification::NotificationConstant::SlotType::LIVE_VIEW);
243 service->PrePublishNotificationBySa(request, randomInt32, randomString);
244 service->SetRequestBundleInfo(request, randomInt32, randomString);
245 std::vector<std::u16string> args;
246 args.emplace_back(fuzzData.GetData<std::u16string>());
247 service->Dump(randomInt32, args);
248 service->OnResourceRemove(randomInt32);
249 service->CheckApiCompatibility(bundleOption);
250 service->OnBundleDataCleared(bundleOption);
251 service->CheckPublishWithoutApp(randomInt32, request);
252 service->GetLocalNotificationKeys(bundleOption);
253 service->OnDistributedPublish(randomString, randomString, request);
254 service->OnDistributedUpdate(randomString, randomString, request);
255 service->OnDistributedDelete(randomString, randomString, randomString, randomInt32);
256 service->AdjustDateForDndTypeOnce(randomInt64, randomInt64);
257 return true;
258 }
259
DoTestForAdvancedNotificationService(std::shared_ptr<Notification::AdvancedNotificationService> service, FuzzData fuzzData)260 bool DoTestForAdvancedNotificationService(std::shared_ptr<Notification::AdvancedNotificationService> service,
261 FuzzData fuzzData)
262 {
263 std::string randomString = fuzzData.GenerateRandomString();
264 int32_t randomInt32 = fuzzData.GenerateRandomInt32();
265 int64_t randomInt64 = fuzzData.GetData<int64_t>();
266 sptr<Notification::NotificationBundleOption> bundleOption = new Notification::NotificationBundleOption();
267 bundleOption->SetBundleName(randomString);
268 bundleOption->SetUid(randomInt32);
269 sptr<Notification::NotificationBundleOption> targetBundleOption = nullptr;
270 sptr<Notification::NotificationRequest> request = new Notification::NotificationRequest();
271 request->SetSlotType(Notification::NotificationConstant::SlotType::LIVE_VIEW);
272 auto content = std::make_shared<Notification::NotificationLiveViewContent>();
273 request->SetContent(std::make_shared<Notification::NotificationContent>(content));
274 request->SetOwnerUid(randomInt32);
275 request->SetCreatorUid(randomInt32);
276 auto flag = std::make_shared<Notification::NotificationFlags>();
277 request->SetFlags(flag);
278 std::shared_ptr<Notification::NotificationRecord> record =
279 service->MakeNotificationRecord(request, bundleOption);
280 record->slot = new Notification::NotificationSlot(Notification::NotificationConstant::SlotType::LIVE_VIEW);
281 service->StartFinishTimer(record, randomInt64, randomInt32);
282 service->StartUpdateTimer(record, randomInt64, randomInt32);
283 service->StartArchiveTimer(record);
284 service->PublishPreparedNotification(request, bundleOption, fuzzData.GenerateRandomBool());
285 service->QueryDoNotDisturbProfile(randomInt32, randomString, randomString);
286 service->CheckDoNotDisturbProfile(record);
287 service->DoNotDisturbUpdataReminderFlags(record);
288 service->UpdateSlotAuthInfo(record);
289 service->Filter(record, fuzzData.GenerateRandomBool());
290 service->ChangeNotificationByControlFlags(record, fuzzData.GenerateRandomBool());
291 service->CheckPublishPreparedNotification(record, fuzzData.GenerateRandomBool());
292 service->UpdateInNotificationList(record);
293 service->PublishFlowControl(record);
294 service->IsNeedPushCheck(request);
295 return true;
296 }
297 }
298
299 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)300 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
301 {
302 /* Run your code on data */
303 if (data != nullptr && size >= GetU32Size()) {
304 OHOS::FuzzData fuzzData(data, size);
305 std::vector<std::string> requestPermission = {
306 OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_CONTROLLER,
307 OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER,
308 OHOS::Notification::OHOS_PERMISSION_SET_UNREMOVABLE_NOTIFICATION
309 };
310 SystemHapTokenGet(requestPermission);
311 OHOS::DoSomethingInterestingWithMyAPI(fuzzData);
312 }
313 return 0;
314 }
315