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