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