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_request_alarm.h"
19 #undef private
20 #undef protected
21 #include "reminderrequestalarm_fuzzer.h"
22 
23 namespace OHOS {
24     namespace {
25         constexpr uint8_t HOUR = 24;
26         constexpr uint8_t MINUTE = 60;
27         constexpr uint8_t WEEK = 7;
28         constexpr uint8_t ENABLE = 2;
29     }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)30     bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
31     {
32         uint8_t hour = *data % HOUR;
33         uint8_t minute = *data % MINUTE;
34         uint8_t week = *data % WEEK;
35         std::vector<uint8_t> daysOfWeek;
36         daysOfWeek.push_back(week);
37         auto rrc = std::make_shared<Notification::ReminderRequestAlarm>(hour, minute, daysOfWeek);
38         // test SetRepeatDaysOfWeek function
39         bool enabled = *data % ENABLE;
40         rrc->SetRepeatDaysOfWeek(enabled, daysOfWeek);
41         // test GetDaysOfWeek function
42         rrc->GetDaysOfWeek();
43         // test CheckParamValid function
44         rrc->CheckParamValid();
45         // test IsRepeatReminder function
46         rrc->IsRepeatReminder();
47         // test PreGetNextTriggerTimeIgnoreSnooze function
48         rrc->PreGetNextTriggerTimeIgnoreSnooze(enabled, enabled);
49         // test GetNextTriggerTime function
50         rrc->GetNextTriggerTime(enabled);
51         // test GetNextDaysOfWeek function
52         time_t now;
53         (void)time(&now);  // unit is seconds.
54         time_t target = *data % ENABLE;
55         rrc->GetNextDaysOfWeek(now, target);
56         // test GetHour function
57         rrc->GetHour();
58         // test GetMinute function
59         rrc->GetMinute();
60         // test GetRepeatDaysOfWeek function
61         rrc->GetRepeatDaysOfWeek();
62         // test OnDateTimeChange function
63         rrc->OnDateTimeChange();
64         // test OnTimeZoneChange function
65         rrc->OnTimeZoneChange();
66         // test UpdateNextReminder function
67         rrc->UpdateNextReminder();
68         // test Unmarshalling function
69         Parcel parcel;
70         rrc->Unmarshalling(parcel);
71         rrc->ReadFromParcel(parcel);
72         return rrc->Marshalling(parcel);
73     }
74 }
75 
76 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)77 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
78 {
79     /* Run your code on data */
80     char *ch = ParseData(data, size);
81     if (ch != nullptr && size >= GetU32Size()) {
82         OHOS::DoSomethingInterestingWithMyAPI(ch, size);
83         free(ch);
84         ch = nullptr;
85     }
86     return 0;
87 }
88