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_calendar.h"
19 #undef private
20 #undef protected
21 #include "reminderrequestcalendar_fuzzer.h"
22
23 namespace OHOS {
24 namespace {
25 constexpr uint8_t MONTHS = 12;
26 constexpr uint8_t DAYS = 31;
27 constexpr uint16_t YEAR = 365;
28 constexpr uint16_t HOURS = 24;
29 constexpr uint16_t MINUTES = 60;
30 constexpr uint16_t SECONDS = 60;
31 constexpr uint8_t ENABLE = 2;
32 constexpr uint8_t WEEK = 7;
33 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)34 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
35 {
36 struct tm nowTime;
37 uint8_t months = *data % MONTHS + 1;
38 uint8_t days = *data % DAYS + 1;
39 uint8_t weeks = *data % WEEK + 1;
40 std::vector<uint8_t> repeatMonths;
41 std::vector<uint8_t> repeatDays;
42 std::vector<uint8_t> daysOfWeek;
43 daysOfWeek.push_back(weeks);
44 repeatMonths.push_back(months);
45 repeatDays.push_back(days);
46 Notification::ReminderRequestCalendar reminderRequestCalendar(nowTime, repeatMonths, repeatDays, daysOfWeek);
47 // test SetNextTriggerTime function
48 reminderRequestCalendar.SetNextTriggerTime();
49 // test InitDateTime function
50 reminderRequestCalendar.InitDateTime();
51 // test InitDateTime function
52 reminderRequestCalendar.InitDateTime(nowTime);
53 // test SetDay function
54 bool enabled = *data % ENABLE;
55 reminderRequestCalendar.SetDay(days, enabled);
56 // test SetMonth function
57 reminderRequestCalendar.SetMonth(months, enabled);
58 // test SetRepeatDaysOfWeek function
59 reminderRequestCalendar.SetRepeatDaysOfWeek(enabled, daysOfWeek);
60 // test SetRepeatMonths function
61 reminderRequestCalendar.SetRepeatMonths(repeatMonths);
62 // test SetRepeatDaysOfMonth function
63 reminderRequestCalendar.SetRepeatDaysOfMonth(repeatDays);
64 // test GetRepeatMonths function
65 reminderRequestCalendar.GetRepeatMonths();
66 // test GetRepeatDays function
67 reminderRequestCalendar.GetRepeatDays();
68 // test GetDaysOfMonth function
69 uint16_t year = *data % YEAR;
70 reminderRequestCalendar.GetDaysOfMonth(year, months);
71 // test GetNextDay function
72 struct tm target;
73 reminderRequestCalendar.GetNextDay(year, months, nowTime, target);
74 // test GetNextTriggerTime function
75 reminderRequestCalendar.GetNextTriggerTime();
76 // test GetNextTriggerTimeAsRepeatReminder function
77 struct tm tarTime;
78 reminderRequestCalendar.GetNextTriggerTimeAsRepeatReminder(nowTime, tarTime);
79 // test GetTimeInstantMilli function
80 uint8_t hour = *data % HOURS;
81 uint8_t minute = *data % MINUTES;
82 uint8_t second = *data % SECONDS;
83 reminderRequestCalendar.GetTimeInstantMilli(year, months, days, hour, minute, second);
84 // test IsRepeatReminder function
85 reminderRequestCalendar.IsRepeatReminder();
86 // test IsRepeatMonth function
87 reminderRequestCalendar.IsRepeatMonth(months);
88 // test IsRepeatDay function
89 reminderRequestCalendar.IsRepeatDay(days);
90 // test OnDateTimeChange function
91 reminderRequestCalendar.OnDateTimeChange();
92 // test OnTimeZoneChange function
93 reminderRequestCalendar.OnTimeZoneChange();
94 // test UpdateNextReminder function
95 reminderRequestCalendar.UpdateNextReminder();
96 // test PreGetNextTriggerTimeIgnoreSnooze function
97 reminderRequestCalendar.PreGetNextTriggerTimeIgnoreSnooze(enabled, enabled);
98 // test Unmarshalling function
99 Parcel parcel;
100 reminderRequestCalendar.Unmarshalling(parcel);
101 reminderRequestCalendar.ReadFromParcel(parcel);
102 return reminderRequestCalendar.Marshalling(parcel);
103 }
104 }
105
106 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)107 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
108 {
109 /* Run your code on data */
110 char *ch = ParseData(data, size);
111 if (ch != nullptr && size >= GetU32Size()) {
112 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
113 free(ch);
114 ch = nullptr;
115 }
116 return 0;
117 }
118