1/*
2 * Copyright (c) 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 "parsefromtext_fuzzer.h"
17#include <fuzzer/FuzzedDataProvider.h>
18#include "ecmascript/pgo_profiler/pgo_profiler_info.h"
19
20using namespace panda;
21using namespace panda::ecmascript;
22using namespace panda::ecmascript::pgo;
23
24namespace OHOS {
25    void ParseFromTextFuzzTest(const uint8_t* data, size_t size)
26    {
27        // Use FuzzedDataProvider to parse input data
28        FuzzedDataProvider dataProvider(data, size);
29
30        std::string inputText = dataProvider.ConsumeRemainingBytesAsString();
31
32        std::string tempFileName = "temp_input_file.txt";
33        {
34            std::ofstream tempFile(tempFileName);
35            tempFile << inputText;
36        }
37
38        std::ifstream inputFile(tempFileName);
39        if (!inputFile.is_open()) {
40            return;
41        }
42
43        PGOPandaFileInfos pandaFileInfos;
44        PGORecordDetailInfos recordDetailInfos(0);
45
46        pandaFileInfos.ParseFromText(inputFile);
47
48        // reset
49        inputFile.clear();
50        inputFile.seekg(0, std::ios::beg);
51        recordDetailInfos.ParseFromText(inputFile);
52
53        // close and delete tmp
54        inputFile.close();
55        std::remove(tempFileName.c_str());
56    }
57}
58
59// Fuzzer entry point.
60extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
61{
62    // Run your code on data.
63    OHOS::ParseFromTextFuzzTest(data, size);
64    return 0;
65}