1/*
2 * Copyright (c) 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#include "daterefnew_fuzzer.h"
17#include "ecmascript/napi/include/jsnapi.h"
18#include "ecmascript/ecma_string-inl.h"
19
20using namespace panda;
21using namespace panda::ecmascript;
22
23#define MAXBYTELEN sizeof(double)
24
25namespace OHOS {
26    void DateRefNewFuzzTest(const uint8_t* data, size_t size)
27    {
28        RuntimeOption option;
29        option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
30        auto vm = JSNApi::CreateJSVM(option);
31        if (data == nullptr || size <= 0) {
32            LOG_ECMA(ERROR) << "illegal input!";
33            return;
34        }
35        double input = 0;
36        if (size > MAXBYTELEN) {
37            size = MAXBYTELEN;
38        }
39        if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) {
40            std::cout << "memcpy_s failed";
41            UNREACHABLE();
42        }
43        if (std::isnan(input)) {
44            input = ecmascript::base::NAN_VALUE;
45        }
46        DateRef::New(vm, input);
47        JSNApi::DestroyJSVM(vm);
48    }
49
50    void DateRefGetTimeFuzzTest(const uint8_t* data, size_t size)
51    {
52        RuntimeOption option;
53        option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
54        auto vm = JSNApi::CreateJSVM(option);
55        if (data == nullptr || size <= 0) {
56            LOG_ECMA(ERROR) << "illegal input!";
57            return;
58        }
59        double input = 0;
60        if (size > MAXBYTELEN) {
61            size = MAXBYTELEN;
62        }
63        if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) {
64            std::cout << "memcpy_s failed";
65            UNREACHABLE();
66        }
67        if (std::isnan(input)) {
68            input = ecmascript::base::NAN_VALUE;
69        }
70        Local<DateRef> date = DateRef::New(vm, input);
71        date->GetTime(vm);
72        JSNApi::DestroyJSVM(vm);
73    }
74
75    void DateRefToStringFuzzTest(const uint8_t* data, size_t size)
76    {
77        RuntimeOption option;
78        option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
79        auto vm = JSNApi::CreateJSVM(option);
80        if (data == nullptr || size <= 0) {
81            LOG_ECMA(ERROR) << "illegal input!";
82            return;
83        }
84        double input = 0;
85        if (size > MAXBYTELEN) {
86            size = MAXBYTELEN;
87        }
88        if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) {
89            std::cout << "memcpy_s failed";
90            UNREACHABLE();
91        }
92        if (std::isnan(input)) {
93            input = ecmascript::base::NAN_VALUE;
94        }
95        Local<DateRef> date = DateRef::New(vm, input);
96        date->ToString(vm);
97        JSNApi::DestroyJSVM(vm);
98    }
99}
100
101// Fuzzer entry point.
102extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
103{
104    // Run your code on data.
105    OHOS::DateRefNewFuzzTest(data, size);
106    OHOS::DateRefGetTimeFuzzTest(data, size);
107    OHOS::DateRefToStringFuzzTest(data, size);
108    return 0;
109}