1 /*
2 * Copyright (c) 2024 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 "reminder_data_manager.h"
17 #include "reminder_request_timer.h"
18 #include "reminderdatamanager_fuzzer.h"
19 #include <fuzzer/FuzzedDataProvider.h>
20
21 namespace OHOS {
DoSomethingInterestingWithManager(FuzzedDataProvider * fdp)22 bool DoSomethingInterestingWithManager(FuzzedDataProvider* fdp)
23 {
24 std::string bundleName = fdp->ConsumeRandomLengthString();
25 int32_t userId = fdp->ConsumeIntegral<int32_t>();
26 int32_t uid = fdp->ConsumeIntegral<int32_t>();
27 int32_t reminderId = fdp->ConsumeIntegral<int32_t>();
28 uint64_t date = fdp->ConsumeIntegral<uint64_t>();
29 bool value = fdp->ConsumeBool();
30 uint8_t type = fdp->ConsumeIntegral<uint8_t>();
31 int32_t callingUid = fdp->ConsumeIntegral<int32_t>();
32 EventFwk::Want want;
33 constexpr uint64_t seconds = 1200;
34 sptr<Notification::ReminderRequest> reminder = new Notification::ReminderRequestTimer(seconds);
35
36 Notification::ReminderDataManager::InitInstance();
37 auto manager = Notification::ReminderDataManager::GetInstance();
38 manager->RegisterConfigurationObserver();
39 manager->Init();
40 manager->Dump();
41 manager->CancelAllReminders(bundleName, userId, uid);
42 manager->CancelReminder(reminderId, callingUid);
43 manager->CheckExcludeDateParam(reminderId, callingUid);
44 manager->AddExcludeDate(reminderId, date, callingUid);
45 manager->DelExcludeDates(reminderId, callingUid);
46
47 std::vector<int64_t> dates;
48 manager->GetExcludeDates(reminderId, callingUid, dates);
49 manager->CloseReminder(want, value);
50 std::vector<Notification::ReminderRequestAdaptation> reminders;
51 manager->GetValidReminders(callingUid, reminders);
52 manager->Init();
53 manager->InitUserId();
54 std::vector<sptr<Notification::ReminderRequest>> immediatelyReminders;
55 std::vector<sptr<Notification::ReminderRequest>> extensionReminders;
56 manager->CheckReminderTime(immediatelyReminders, extensionReminders);
57
58 manager->RegisterConfigurationObserver();
59 manager->OnUserRemove(userId);
60 manager->OnBundleMgrServiceStart();
61 manager->OnAbilityMgrServiceStart();
62 manager->OnUserSwitch(userId);
63 manager->OnProcessDiedLocked(callingUid);
64 manager->RefreshRemindersDueToSysTimeChange(type);
65 manager->ShouldAlert(reminder);
66 manager->ShowActiveReminder(want);
67 manager->SnoozeReminder(want);
68
69 manager->HandleCustomButtonClick(want);
70 manager->ClickReminder(want);
71 manager->TerminateAlerting(want);
72 return true;
73 }
74
DoSomethingInterestingWithReminder(FuzzedDataProvider * fdp)75 bool DoSomethingInterestingWithReminder(FuzzedDataProvider* fdp)
76 {
77 std::string bundleName = fdp->ConsumeRandomLengthString();
78 int32_t userId = fdp->ConsumeIntegral<int32_t>();
79 int32_t uid = fdp->ConsumeIntegral<int32_t>();
80 int32_t reminderId = fdp->ConsumeIntegral<int32_t>();
81 bool value = fdp->ConsumeBool();
82 int32_t callingUid = fdp->ConsumeIntegral<int32_t>();
83 constexpr uint64_t seconds = 1200;
84 sptr<Notification::ReminderRequest> reminder = new Notification::ReminderRequestTimer(seconds);
85 auto manager = Notification::ReminderDataManager::GetInstance();
86
87 manager->OnLanguageChanged();
88 manager->OnRemoveAppMgr();
89 manager->CancelAllReminders(userId);
90 manager->CheckUpdateConditions(reminder, Notification::ReminderRequest::ActionButtonType::INVALID,
91 reminder->GetActionButtons());
92 manager->CancelRemindersImplLocked(bundleName, userId, uid, value);
93 manager->CloseRemindersByGroupId(reminderId, bundleName, bundleName);
94 manager->CancelNotification(reminder);
95 manager->CheckReminderLimitExceededLocked(callingUid, reminder);
96 std::vector<sptr<Notification::ReminderRequest>> reminders;
97 manager->GetImmediatelyShowRemindersLocked(reminders);
98 manager->AddToShowedReminders(reminder);
99
100 manager->IsAllowedNotify(reminder);
101 manager->PlaySoundAndVibrationLocked(reminder);
102 manager->PlaySoundAndVibration(reminder);
103 manager->StopSoundAndVibrationLocked(reminder);
104 manager->StopSoundAndVibration(reminder);
105 manager->RemoveFromShowedReminders(reminder);
106 manager->RemoveReminderLocked(reminderId, false);
107 manager->SetActiveReminder(reminder);
108 manager->SetAlertingReminder(reminder);
109 manager->ShowActiveReminderExtendLocked(reminder, reminders);
110
111 std::vector<sptr<Notification::ReminderRequest>> extensionReminders;
112 std::vector<sptr<Notification::ReminderRequest>> immediatelyReminders;
113 manager->PublishReminder(reminder, callingUid);
114 manager->FindReminderRequestLocked(reminderId, false);
115 manager->StartRecentReminder();
116 manager->HandleImmediatelyShow(immediatelyReminders, value);
117 int8_t type = fdp->ConsumeIntegral<int8_t>();
118 manager->HandleExtensionReminder(extensionReminders, type);
119 manager->HandleSameNotificationIdShowing(reminder);
120 manager->IsBelongToSameApp(uid, uid);
121 manager->CheckIsSameApp(reminder, uid);
122 manager->ShowReminder(reminder, value, value, value, value);
123 return true;
124 }
125
Clear()126 bool Clear()
127 {
128 auto manager = Notification::ReminderDataManager::GetInstance();
129 if (manager->queue_ != nullptr) {
130 auto handler = manager->queue_->submit_h(std::bind([]() {}));
131 manager->queue_->wait(handler);
132 }
133 return true;
134 }
135 }
136
137 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)138 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
139 {
140 /* Run your code on data */
141 FuzzedDataProvider fdp(data, size);
142 OHOS::DoSomethingInterestingWithManager(&fdp);
143 OHOS::DoSomethingInterestingWithReminder(&fdp);
144 return 0;
145 }
146