1 /*
2 * Copyright (c) 2022 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 "notification_helper.h"
19 #undef private
20 #undef protected
21 #include "notificationhelper_fuzzer.h"
22 #include <fuzzer/FuzzedDataProvider.h>
23 #include "ans_permission_def.h"
24 #include "notification_subscriber.h"
25 #include "notification_button_option.h"
26 #include "mock_notification_subscribe_info.h"
27 #include "ans_dialog_host_client.h"
28 #include "mock_notification_donotdisturb_profile.h"
29 #include "notification_disable.h"
30 #include "mock_notification_operation_info.h"
31
32 namespace OHOS {
33 namespace Notification {
34
35 class FuzzNotificationSubscriber : public NotificationSubscriber {
36 public:
OnDisconnected()37 void OnDisconnected() override
38 {}
OnDied()39 void OnDied() override
40 {}
OnUpdate(const std::shared_ptr<NotificationSortingMap> & sortingMap)41 void OnUpdate(const std::shared_ptr<NotificationSortingMap> &sortingMap) override
42 {}
OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> & date)43 void OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> &date) override
44 {}
OnConnected()45 void OnConnected() override
46 {}
OnEnabledNotificationChanged(const std::shared_ptr<EnabledNotificationCallbackData> & callbackData)47 void OnEnabledNotificationChanged(const std::shared_ptr<EnabledNotificationCallbackData> &callbackData) override
48 {}
OnCanceled(const std::shared_ptr<Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap,int deleteReason)49 void OnCanceled(const std::shared_ptr<Notification> &request,
50 const std::shared_ptr<NotificationSortingMap> &sortingMap, int deleteReason) override
51 {}
OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> & badgeData)52 void OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> &badgeData) override
53 {}
OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> & callbackData)54 void OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override
55 {}
OnConsumed(const std::shared_ptr<Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap)56 void OnConsumed(const std::shared_ptr<Notification> &request,
57 const std::shared_ptr<NotificationSortingMap> &sortingMap) override
58 {}
59
OnBatchCanceled(const std::vector<std::shared_ptr<Notification>> & requestList,const std::shared_ptr<NotificationSortingMap> & sortingMap,int32_t deleteReason)60 void OnBatchCanceled(const std::vector<std::shared_ptr<Notification>> &requestList,
61 const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) override
62 {}
63 };
64
65 class FuzzTestLocalLiveViewSubscriber : public NotificationLocalLiveViewSubscriber {
66 public:
OnConnected()67 void OnConnected() override
68 {}
OnDisconnected()69 void OnDisconnected() override
70 {}
OnDied()71 void OnDied() override
72 {}
OnResponse(int32_t notificationId,sptr<NotificationButtonOption> buttonOption)73 void OnResponse(int32_t notificationId, sptr<NotificationButtonOption> buttonOption) override
74 {}
75 };
76
TestPublishAndRemove(FuzzedDataProvider * fdp,NotificationHelper & notificationHelper)77 bool TestPublishAndRemove(FuzzedDataProvider* fdp, NotificationHelper& notificationHelper)
78 {
79 std::string stringData = fdp->ConsumeRandomLengthString();
80 int32_t intData = fdp->ConsumeIntegral<int32_t>();
81
82 std::string representativeBundle = stringData;
83 NotificationRequest notification;
84 notification.SetOwnerUid(intData);
85 notification.SetCreatorUid(intData);
86 notification.SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
87 auto content = std::make_shared<NotificationLiveViewContent>();
88 notification.SetContent(std::make_shared<NotificationContent>(content));
89 notificationHelper.PublishNotificationAsBundle(representativeBundle, notification);
90 notificationHelper.RemoveNotifications();
91 std::vector<std::string> hashCodes;
92 hashCodes.emplace_back(fdp->ConsumeRandomLengthString());
93 notificationHelper.RemoveNotifications(hashCodes, fdp->ConsumeIntegral<int32_t>());
94
95 return true;
96 }
97
TestBundleOperations(FuzzedDataProvider * fdp,NotificationHelper & notificationHelper)98 bool TestBundleOperations(FuzzedDataProvider* fdp, NotificationHelper& notificationHelper)
99 {
100 std::string stringData = fdp->ConsumeRandomLengthString();
101 int32_t intData = fdp->ConsumeIntegral<int32_t>();
102
103 bool enabled = fdp->ConsumeBool();
104 notificationHelper.SetNotificationsEnabledForAllBundles(stringData, enabled);
105
106 NotificationBundleOption bundleOption;
107 bundleOption.SetBundleName(stringData);
108 bundleOption.SetUid(intData);
109
110 uint32_t flag = 0;
111 notificationHelper.GetNotificationSlotFlagsAsBundle(bundleOption, flag);
112 notificationHelper.SetNotificationSlotFlagsAsBundle(bundleOption, intData);
113 notificationHelper.CancelAsBundle(bundleOption, intData);
114 NotificationButtonOption buttonOption;
115 notificationHelper.TriggerLocalLiveView(bundleOption, fdp->ConsumeIntegral<int32_t>(), buttonOption);
116 return true;
117 }
118
TestNotificationSettings(FuzzedDataProvider * fdp,NotificationHelper & notificationHelper)119 bool TestNotificationSettings(FuzzedDataProvider* fdp, NotificationHelper& notificationHelper)
120 {
121 uint32_t slotFlags = fdp->ConsumeIntegral<uint32_t>();
122 notificationHelper.GetNotificationSettings(slotFlags);
123
124 NotificationRequest notification;
125 notificationHelper.PublishNotificationForIndirectProxy(notification);
126
127 bool canPop = fdp->ConsumeBool();
128
129 std::string stringData = fdp->ConsumeRandomLengthString();
130 sptr<AnsDialogHostClient> client = nullptr;
131 AnsDialogHostClient::CreateIfNullptr(client);
132 client = AnsDialogHostClient::GetInstance();
133 notificationHelper.CanPopEnableNotificationDialog(client, canPop, stringData);
134 notificationHelper.RemoveEnableNotificationDialog();
135 int32_t uid = fdp->ConsumeIntegralInRange<int32_t>(0, 100);
136 notificationHelper.RequestEnableNotification(stringData, uid);
137 sptr<IRemoteObject> callerToken = nullptr;
138 sptr<NotificationCheckRequest> notificationCheckRequest =
139 new NotificationCheckRequest();
140 notificationHelper.RegisterPushCallback(callerToken, notificationCheckRequest);
141 notificationHelper.UnregisterPushCallback();
142 return true;
143 }
144
TestSubscriptionOperations(FuzzedDataProvider * fdp,NotificationHelper & notificationHelper)145 bool TestSubscriptionOperations(FuzzedDataProvider* fdp, NotificationHelper& notificationHelper)
146 {
147 FuzzNotificationSubscriber fuzzNotificationSub;
148 std::shared_ptr<NotificationSubscriber> fuzzNotificationSubSptr =
149 std::make_shared<FuzzNotificationSubscriber>();
150
151 notificationHelper.SubscribeNotification(fuzzNotificationSub);
152 notificationHelper.SubscribeNotification(fuzzNotificationSubSptr);
153 notificationHelper.SubscribeNotificationSelf(fuzzNotificationSub);
154 notificationHelper.SubscribeNotificationSelf(fuzzNotificationSubSptr);
155
156 FuzzTestLocalLiveViewSubscriber fuzzLocalLiveViewSubscriber;
157 notificationHelper.SubscribeLocalLiveViewNotification(fuzzLocalLiveViewSubscriber, fdp->ConsumeBool());
158
159 sptr<NotificationSubscribeInfo> fuzzNotificationSubInfoSptr =
160 ObjectBuilder<NotificationSubscribeInfo>::Build(fdp);
161 notificationHelper.SubscribeNotification(fuzzNotificationSubSptr, fuzzNotificationSubInfoSptr);
162
163 notificationHelper.UnSubscribeNotification(fuzzNotificationSub);
164 notificationHelper.UnSubscribeNotification(fuzzNotificationSubSptr);
165 notificationHelper.UnSubscribeNotification(fuzzNotificationSubSptr, fuzzNotificationSubInfoSptr);
166 return true;
167 }
168
TestSlotConfiguration(FuzzedDataProvider * fdp,NotificationHelper & notificationHelper)169 bool TestSlotConfiguration(FuzzedDataProvider* fdp, NotificationHelper& notificationHelper)
170 {
171 NotificationBundleOption bundleOption;
172 constexpr uint8_t SLOT_LEVEL_NUM = 6;
173 constexpr uint8_t SLOT_VISIBLENESS_TYPE_NUM = 4;
174 constexpr uint8_t SLOT_TYPE_NUM = 5;
175
176 sptr<NotificationSlot> slot = new NotificationSlot();
177 slot->SetDescription(fdp->ConsumeRandomLengthString());
178 slot->SetEnableLight(fdp->ConsumeBool());
179 slot->SetEnableVibration(fdp->ConsumeBool());
180 slot->SetLedLightColor(fdp->ConsumeIntegral<uint32_t>());
181
182 uint8_t level = fdp->ConsumeIntegral<uint8_t>() % SLOT_LEVEL_NUM;
183 slot->SetLevel(NotificationSlot::NotificationLevel(level));
184
185 uint8_t visibleness = fdp->ConsumeIntegral<uint8_t>() % SLOT_VISIBLENESS_TYPE_NUM;
186 slot->SetLockscreenVisibleness(NotificationConstant::VisiblenessType(visibleness));
187
188 uint8_t type = fdp->ConsumeIntegral<uint8_t>() % SLOT_TYPE_NUM;
189 NotificationConstant::SlotType slotType = NotificationConstant::SlotType(type);
190 slot->SetType(slotType);
191
192 std::vector<NotificationSlot> slots;
193 slots.emplace_back(*slot);
194
195 notificationHelper.GetNotificationSlotForBundle(bundleOption, slotType, slot);
196
197 std::vector<sptr<Notification>> notifications;
198 notificationHelper.GetAllNotificationsBySlotType(notifications, slotType);
199 return true;
200 }
201
TestDoNotDisturb(FuzzedDataProvider * fdp,NotificationHelper & notificationHelper)202 bool TestDoNotDisturb(FuzzedDataProvider* fdp, NotificationHelper& notificationHelper)
203 {
204 std::vector<sptr<NotificationDoNotDisturbProfile>> profiles;
205 size_t numProfiles = fdp->ConsumeIntegralInRange<size_t>(1, 6);
206 for (size_t i = 0; i < numProfiles; ++i) {
207 sptr<NotificationDoNotDisturbProfile> profile =
208 ObjectBuilder<NotificationDoNotDisturbProfile>::Build(fdp);
209 profiles.push_back(profile);
210 }
211 notificationHelper.AddDoNotDisturbProfiles(profiles);
212 notificationHelper.RemoveDoNotDisturbProfiles(profiles);
213
214 notificationHelper.IsNeedSilentInDoNotDisturbMode(fdp->ConsumeRandomLengthString(),
215 fdp->ConsumeIntegral<int32_t>());
216 return true;
217 }
218
TestBadgeOperations(FuzzedDataProvider * fdp,NotificationHelper & notificationHelper)219 bool TestBadgeOperations(FuzzedDataProvider* fdp, NotificationHelper& notificationHelper)
220 {
221 NotificationBundleOption bundleOption;
222 bool enabled = fdp->ConsumeBool();
223 int32_t uid = fdp->ConsumeIntegralInRange<int32_t>(0, 100);
224 notificationHelper.SetNotificationsEnabledForAllBundles(uid, enabled);
225
226 NotificationConstant::SlotType slotType = NotificationConstant::SlotType(fdp->ConsumeIntegral<uint8_t>() % 5);
227 notificationHelper.GetEnabledForBundleSlotSelf(slotType, enabled);
228
229 int32_t badgeNumber = fdp->ConsumeIntegralInRange<int32_t>(0, 100);
230 std::string bundleName = fdp->ConsumeRandomLengthString();
231 notificationHelper.SetBadgeNumber(badgeNumber, bundleName);
232 notificationHelper.SetBadgeNumberByBundle(bundleOption, badgeNumber);
233 notificationHelper.SetBadgeNumberForDhByBundle(bundleOption, badgeNumber);
234
235 std::vector<NotificationBundleOption> bundleOptions;
236 bundleOptions.push_back(bundleOption);
237 notificationHelper.GetAllNotificationEnabledBundles(bundleOptions);
238 return true;
239 }
240
TestDistributedOperations(FuzzedDataProvider * fdp,NotificationHelper & notificationHelper)241 bool TestDistributedOperations(FuzzedDataProvider* fdp, NotificationHelper& notificationHelper)
242 {
243 NotificationBundleOption bundleOption;
244 std::string deviceType = fdp->ConsumeRandomLengthString();
245 std::string deviceId = fdp->ConsumeRandomLengthString();
246 bool enabled = fdp->ConsumeBool();
247 int32_t uid = fdp->ConsumeIntegralInRange<int32_t>(0, 100);
248
249 notificationHelper.SetDistributedEnabledByBundle(bundleOption, deviceType, enabled);
250 notificationHelper.IsDistributedEnabledByBundle(bundleOption, deviceType, enabled);
251 notificationHelper.SetDistributedEnabled(deviceType, enabled);
252 notificationHelper.IsDistributedEnabled(deviceType, enabled);
253
254 int32_t abilityId = fdp->ConsumeIntegralInRange<int32_t>(0, 100);
255 notificationHelper.GetDistributedAbility(abilityId);
256
257 bool isAuth = fdp->ConsumeBool();
258 notificationHelper.GetDistributedAuthStatus(deviceType, deviceId, uid, isAuth);
259 notificationHelper.SetDistributedAuthStatus(deviceType, deviceId, uid, isAuth);
260
261 notificationHelper.SetSmartReminderEnabled(deviceType, enabled);
262 notificationHelper.IsSmartReminderEnabled(deviceType, enabled);
263
264 NotificationConstant::SlotType slotType = NotificationConstant::SlotType(fdp->ConsumeIntegral<uint8_t>() % 5);
265 notificationHelper.SetDistributedEnabledBySlot(slotType, deviceType, enabled);
266 notificationHelper.IsDistributedEnabledBySlot(slotType, deviceType, enabled);
267 notificationHelper.CancelAsBundleWithAgent(bundleOption, uid);
268 notificationHelper.SetHashCodeRule(fdp->ConsumeIntegral<uint32_t>());
269 return true;
270 }
271
TestAdvancedOperations(FuzzedDataProvider * fdp,NotificationHelper & notificationHelper)272 bool TestAdvancedOperations(FuzzedDataProvider* fdp, NotificationHelper& notificationHelper)
273 {
274 std::string key = fdp->ConsumeRandomLengthString();
275 std::string val = fdp->ConsumeRandomLengthString();
276 notificationHelper.SetAdditionConfig(key, val);
277
278 std::string deviceType = fdp->ConsumeRandomLengthString();
279 std::string deviceId = fdp->ConsumeRandomLengthString();
280 uint32_t status = fdp->ConsumeIntegral<uint32_t>();
281 uint32_t controlFlag = fdp->ConsumeIntegral<uint32_t>();
282 int32_t uid = fdp->ConsumeIntegralInRange<int32_t>(0, 100);
283
284 notificationHelper.SetTargetDeviceStatus(deviceType, status, deviceId);
285 notificationHelper.SetTargetDeviceStatus(deviceType, status, controlFlag, deviceId, uid);
286
287 int32_t operateType = fdp->ConsumeIntegralInRange<int32_t>(0, 2);
288 std::vector<std::string> bundleList;
289 bundleList.emplace_back(fdp->ConsumeRandomLengthString());
290 std::vector<std::string> labelList;
291 labelList.emplace_back(fdp->ConsumeRandomLengthString());
292 notificationHelper.SetTargetDeviceBundleList(deviceType, deviceId, operateType, bundleList, labelList);
293
294 notificationHelper.SetTargetDeviceSwitch(deviceType, deviceId,
295 fdp->ConsumeBool(), fdp->ConsumeBool());
296 notificationHelper.GetTargetDeviceBundleList(deviceType, deviceId, bundleList, labelList);
297
298 int32_t targetUserId;
299 std::string targetDeviceId;
300 notificationHelper.GetMutilDeviceStatus(deviceType, controlFlag, targetDeviceId, targetUserId);
301
302 sptr<NotificationDoNotDisturbProfile> disturbProfile =
303 ObjectBuilder<NotificationDoNotDisturbProfile>::Build(fdp);
304 notificationHelper.GetDoNotDisturbProfile(uid, disturbProfile);
305
306 sptr<NotificationOperationInfo> operationInfo = ObjectBuilder<NotificationOperationInfo>::Build(fdp);
307 notificationHelper.DistributeOperation(operationInfo, nullptr);
308
309 std::string hashCode = fdp->ConsumeRandomLengthString();
310 sptr<NotificationRequest> notificationRequest;
311 LiveViewFilter filter;
312 notificationHelper.GetNotificationRequestByHashCode(hashCode, notificationRequest);
313 notificationHelper.GetActiveNotificationByFilter(filter, notificationRequest);
314
315 int32_t result = fdp->ConsumeIntegralInRange<int32_t>(0, 100);
316 notificationHelper.ReplyDistributeOperation(hashCode, result);
317
318 bool isPaused = fdp->ConsumeBool();
319 notificationHelper.UpdateNotificationTimerByUid(uid, isPaused);
320
321 NotificationDisable notificationDisable;
322 notificationHelper.DisableNotificationFeature(notificationDisable);
323
324 std::string bundleName = fdp->ConsumeRandomLengthString();
325 bool isAllowUseReminder = fdp->ConsumeBool();
326 notificationHelper.AllowUseReminder(bundleName, isAllowUseReminder);
327
328 std::vector<NotificationBundleOption> bundleOptions;
329 notificationHelper.GetAllLiveViewEnabledBundles(bundleOptions);
330 notificationHelper.GetAllDistribuedEnabledBundles(deviceType, bundleOptions);
331 return true;
332 }
333
DoSomethingInterestingWithMyAPI(FuzzedDataProvider * fdp)334 bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider *fdp)
335 {
336 NotificationHelper notificationHelper;
337
338 TestPublishAndRemove(fdp, notificationHelper);
339 TestBundleOperations(fdp, notificationHelper);
340 TestNotificationSettings(fdp, notificationHelper);
341 TestSubscriptionOperations(fdp, notificationHelper);
342 TestSlotConfiguration(fdp, notificationHelper);
343 TestDoNotDisturb(fdp, notificationHelper);
344 TestBadgeOperations(fdp, notificationHelper);
345 TestDistributedOperations(fdp, notificationHelper);
346 TestAdvancedOperations(fdp, notificationHelper);
347
348 return true;
349 }
350 }
351 }
352
353 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)354 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
355 {
356 FuzzedDataProvider fdp(data, size);
357 std::vector<std::string> requestPermission = {
358 OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_CONTROLLER,
359 OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER,
360 OHOS::Notification::OHOS_PERMISSION_SET_UNREMOVABLE_NOTIFICATION
361 };
362 MockRandomToken(&fdp, requestPermission);
363 OHOS::Notification::DoSomethingInterestingWithMyAPI(&fdp);
364 return 0;
365 }
366