1 /*
2  * Copyright (c) 2022-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 <cstddef>
17 #include <cstdint>
18 #include <string>
19 #include "i18n_calendar.h"
20 #include "calendar_fuzzer.h"
21 #include "unicode/ucal.h"
22 #include "unicode/utypes.h"
23 
24 namespace OHOS {
25     const size_t FIRST_PARAM_OFFSET_SIZE = 8;
26     const size_t SECOND_PARAM_OFFSET_SIZE = 16;
27 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)28     void DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
29     {
30         using namespace Global::I18n;
31 
32         uint32_t offset = 0;
33         std::string input(reinterpret_cast<const char*>(data), size);
34         I18nCalendar calendar(input, CalendarType::GREGORY);
35         if (size >= FIRST_PARAM_OFFSET_SIZE) {
36             double number = *(reinterpret_cast<const double*>(data + offset));
37             calendar.SetTime(number);
38         }
39 
40         calendar.Set(size, size, size);
41         calendar.Set(UCalendarDateFields::UCAL_YEAR, size);
42         calendar.GetTimeZone();
43         calendar.SetTimeZone(input);
44         calendar.GetTimeInMillis();
45         calendar.GetMinimalDaysInFirstWeek();
46         calendar.GetFirstDayOfWeek();
47         UErrorCode status = U_ZERO_ERROR;
48         calendar.IsWeekend(size, status);
49         calendar.IsWeekend();
50         calendar.GetDisplayName(input);
51         if (size < SECOND_PARAM_OFFSET_SIZE) {
52             return;
53         }
54         offset += sizeof(double);
55         double compareDay = *(reinterpret_cast<const double*>(data + offset));
56         calendar.CompareDays(compareDay);
57     }
58 }
59 
60 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)61 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
62 {
63     /* Run your code on data */
64     OHOS::DoSomethingInterestingWithMyAPI(data, size);
65     return 0;
66 }
67 
68