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 "reminder_helper.h"
19 #undef private
20 #undef protected
21 #include "reminderhelper_fuzzer.h"
22
23 namespace OHOS {
24 namespace {
25 constexpr uint8_t ENABLE = 2;
26 constexpr uint8_t SLOT_TYPE_NUM = 5;
27 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)28 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
29 {
30 std::string stringData(data);
31 Notification::ReminderRequest reminder;
32 reminder.SetContent(stringData);
33 reminder.SetExpiredContent(stringData);
34 Notification::ReminderHelper::PublishReminder(reminder);
35 int32_t reminderId = static_cast<int32_t>(*data);
36 Notification::ReminderHelper::CancelReminder(reminderId);
37 sptr<Notification::ReminderRequest> valid = new Notification::ReminderRequest();
38 std::vector<sptr<Notification::ReminderRequest>> validReminders;
39 validReminders.emplace_back(valid);
40 Notification::ReminderHelper::GetValidReminders(validReminders);
41 Notification::NotificationSlot notificationSlot;
42 bool enabled = *data % ENABLE;
43 notificationSlot.SetEnableLight(enabled);
44 notificationSlot.SetEnableVibration(enabled);
45 Notification::ReminderHelper::AddNotificationSlot(notificationSlot);
46 uint8_t type = *data % SLOT_TYPE_NUM;
47 Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType(type);
48 Notification::ReminderHelper::RemoveNotificationSlot(slotType);
49 return Notification::ReminderHelper::CancelAllReminders();
50 }
51 }
52
53 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)54 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
55 {
56 /* Run your code on data */
57 char *ch = ParseData(data, size);
58 if (ch != nullptr && size >= GetU32Size()) {
59 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
60 free(ch);
61 ch = nullptr;
62 }
63 return 0;
64 }
65