1 /*
2  * Copyright (c) 2022-2023 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_request.h"
19 #undef private
20 #undef protected
21 #include "reminderrequest_fuzzer.h"
22 
23 namespace OHOS {
24     namespace {
25         constexpr uint8_t ENABLE = 2;
26         constexpr uint8_t ACTION_BUTTON_TYPE = 3;
27         constexpr uint8_t SLOT_TYPE_NUM = 5;
28     }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)29     bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
30     {
31         std::string stringData(data);
32         int32_t reminderId = static_cast<int32_t>(GetU32Data(data));
33         Notification::ReminderRequest reminderRequest(reminderId);
34         reminderRequest.CanRemove();
35         reminderRequest.CanShow();
36         reminderRequest.Dump();
37         uint8_t types = *data % ACTION_BUTTON_TYPE;
38         Notification::ReminderRequest::ActionButtonType type =
39             Notification::ReminderRequest::ActionButtonType(types);
40         reminderRequest.SetActionButton(stringData, type, stringData);
41         reminderRequest.SetContent(stringData);
42         reminderRequest.SetExpiredContent(stringData);
43         bool enabled = *data % ENABLE;
44         reminderRequest.SetExpired(enabled);
45         reminderRequest.InitReminderId();
46         reminderRequest.InitUserId(reminderId);
47         reminderRequest.InitUid(reminderId);
48         reminderRequest.IsExpired();
49         reminderRequest.IsShowing();
50         reminderRequest.OnClose(enabled);
51         reminderRequest.OnDateTimeChange();
52         uint64_t oriTriggerTime = static_cast<uint64_t>(GetU32Data(data));
53         uint64_t optTriggerTimes = static_cast<uint64_t>(GetU32Data(data));
54         reminderRequest.HandleSysTimeChange(oriTriggerTime, optTriggerTimes);
55         uint64_t oldZoneTriggerTime = static_cast<uint64_t>(GetU32Data(data));
56         uint64_t newZoneTriggerTime = static_cast<uint64_t>(GetU32Data(data));
57         uint64_t optTriggerTime = static_cast<uint64_t>(GetU32Data(data));
58         reminderRequest.HandleTimeZoneChange(oldZoneTriggerTime, newZoneTriggerTime, optTriggerTime);
59         reminderRequest.OnSameNotificationIdCovered();
60         reminderRequest.OnShow(enabled, enabled, enabled);
61         reminderRequest.OnShowFail();
62         reminderRequest.OnSnooze();
63         reminderRequest.OnStart();
64         reminderRequest.OnStop();
65         reminderRequest.OnTerminate();
66         reminderRequest.OnTimeZoneChange();
67         reminderRequest.StringSplit(stringData, stringData);
68         std::shared_ptr< Notification::ReminderRequest::MaxScreenAgentInfo> maxScreenWantAgentInfo =
69             std::make_shared< Notification::ReminderRequest::MaxScreenAgentInfo>();
70         reminderRequest.SetMaxScreenWantAgentInfo(maxScreenWantAgentInfo);
71         reminderRequest.SetNotificationId(reminderId);
72         uint8_t typed = *data % SLOT_TYPE_NUM;
73         Notification::NotificationConstant::SlotType slotType =
74             Notification::NotificationConstant::SlotType(typed);
75         reminderRequest.SetSlotType(slotType);
76         reminderRequest.SetSnoozeContent(stringData);
77         return reminderRequest.ShouldShowImmediately();
78     }
79 }
80 
81 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)82 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
83 {
84     /* Run your code on data */
85     char *ch = ParseData(data, size);
86     if (ch != nullptr && size >= GetU32Size()) {
87         OHOS::DoSomethingInterestingWithMyAPI(ch, size);
88         free(ch);
89         ch = nullptr;
90     }
91     return 0;
92 }
93