14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#include "ecmascript/ohos/aot_runtime_info.h"
174514f5e3Sopenharmony_ci#include "setruntimeinfo_fuzzer.h"
184514f5e3Sopenharmony_ci#include <fuzzer/FuzzedDataProvider.h>
194514f5e3Sopenharmony_ci
204514f5e3Sopenharmony_ciusing namespace panda::ecmascript::ohos;
214514f5e3Sopenharmony_ci
224514f5e3Sopenharmony_cinamespace OHOS {
234514f5e3Sopenharmony_ci
244514f5e3Sopenharmony_ciclass AotRuntimeInfoTest : public AotRuntimeInfo {
254514f5e3Sopenharmony_cipublic:
264514f5e3Sopenharmony_ci    void TestSetRuntimeInfo(const char *realOutPath, char lines[][BUFFER_SIZE], int length) const
274514f5e3Sopenharmony_ci    {
284514f5e3Sopenharmony_ci        SetRuntimeInfo(realOutPath, lines, length);
294514f5e3Sopenharmony_ci    }
304514f5e3Sopenharmony_ci
314514f5e3Sopenharmony_ci    void TestGetRuntimeInfoByPath(char lines[][BUFFER_SIZE], const char *realOutPath, const char *soBuildId) const
324514f5e3Sopenharmony_ci    {
334514f5e3Sopenharmony_ci        GetRuntimeInfoByPath(lines, realOutPath, soBuildId);
344514f5e3Sopenharmony_ci    }
354514f5e3Sopenharmony_ci
364514f5e3Sopenharmony_ci    virtual bool TestGetRuntimeBuildId(char *buildId, int length) const
374514f5e3Sopenharmony_ci    {
384514f5e3Sopenharmony_ci        return GetRuntimeBuildId(buildId, length);
394514f5e3Sopenharmony_ci    }
404514f5e3Sopenharmony_ci};
414514f5e3Sopenharmony_ci
424514f5e3Sopenharmony_ci    void SetRuntimeInfoFuzzTest([[maybe_unused]] const uint8_t *data, size_t size)
434514f5e3Sopenharmony_ci    {
444514f5e3Sopenharmony_ci        FuzzedDataProvider dataProvider(data, size);
454514f5e3Sopenharmony_ci
464514f5e3Sopenharmony_ci        // Generate a random output file path
474514f5e3Sopenharmony_ci        std::string realOutPath = "/tmp/fuzz_test_" + std::to_string(dataProvider.ConsumeIntegral<uint32_t>());
484514f5e3Sopenharmony_ci
494514f5e3Sopenharmony_ci        // Generate random lines content
504514f5e3Sopenharmony_ci        constexpr int bufferSize = 4096;
514514f5e3Sopenharmony_ci        constexpr int maxLength = 255;
524514f5e3Sopenharmony_ci        char lines[maxLength][bufferSize] = {};
534514f5e3Sopenharmony_ci
544514f5e3Sopenharmony_ci        int lineCount = dataProvider.ConsumeIntegralInRange<int>(1, maxLength);
554514f5e3Sopenharmony_ci        for (int i = 0; i < lineCount; ++i) {
564514f5e3Sopenharmony_ci            std::string line = dataProvider.ConsumeRandomLengthString(bufferSize - 1);
574514f5e3Sopenharmony_ci        }
584514f5e3Sopenharmony_ci
594514f5e3Sopenharmony_ci        AotRuntimeInfoTest runtimeInfoTest;
604514f5e3Sopenharmony_ci        runtimeInfoTest.TestSetRuntimeInfo(realOutPath.c_str(), lines, maxLength);
614514f5e3Sopenharmony_ci
624514f5e3Sopenharmony_ci        // Clean up the created file
634514f5e3Sopenharmony_ci        unlink(realOutPath.c_str());
644514f5e3Sopenharmony_ci    };
654514f5e3Sopenharmony_ci
664514f5e3Sopenharmony_ci    void GetRuntimeInfoByPathFuzzTest(const uint8_t *data, size_t size)
674514f5e3Sopenharmony_ci    {
684514f5e3Sopenharmony_ci        FuzzedDataProvider dataProvider(data, size);
694514f5e3Sopenharmony_ci
704514f5e3Sopenharmony_ci        // Generate a random output file path
714514f5e3Sopenharmony_ci        std::string realOutPath = dataProvider.ConsumeRandomLengthString(AotRuntimeInfo::MAX_LENGTH);
724514f5e3Sopenharmony_ci
734514f5e3Sopenharmony_ci        // Generate a random Build ID
744514f5e3Sopenharmony_ci        std::string soBuildId = dataProvider.ConsumeRandomLengthString(AotRuntimeInfo::BUFFER_SIZE - 1);
754514f5e3Sopenharmony_ci
764514f5e3Sopenharmony_ci        // prepare lines
774514f5e3Sopenharmony_ci        char lines[AotRuntimeInfo::MAX_LENGTH][AotRuntimeInfo::BUFFER_SIZE] = {{0}};
784514f5e3Sopenharmony_ci
794514f5e3Sopenharmony_ci        AotRuntimeInfoTest runtimeInfoTest;
804514f5e3Sopenharmony_ci        runtimeInfoTest.TestGetRuntimeInfoByPath(lines, realOutPath.c_str(), soBuildId.c_str());
814514f5e3Sopenharmony_ci    };
824514f5e3Sopenharmony_ci
834514f5e3Sopenharmony_ci    void GetRuntimeBuildIdFuzzTest(const uint8_t *data, size_t size)
844514f5e3Sopenharmony_ci    {
854514f5e3Sopenharmony_ci        FuzzedDataProvider dataProvider(data, size);
864514f5e3Sopenharmony_ci
874514f5e3Sopenharmony_ci        // generate a random buildIdLength
884514f5e3Sopenharmony_ci        int buildIdLength = dataProvider.ConsumeIntegralInRange<int>(1, PATH_MAX);
894514f5e3Sopenharmony_ci
904514f5e3Sopenharmony_ci        // Generate a random Build ID
914514f5e3Sopenharmony_ci        char buildId[PATH_MAX] = {'\0'};
924514f5e3Sopenharmony_ci
934514f5e3Sopenharmony_ci        AotRuntimeInfoTest runtimeInfoTest;
944514f5e3Sopenharmony_ci        runtimeInfoTest.TestGetRuntimeBuildId(buildId, buildIdLength);
954514f5e3Sopenharmony_ci    }
964514f5e3Sopenharmony_ci}
974514f5e3Sopenharmony_ci
984514f5e3Sopenharmony_ci// Fuzzer entry point.
994514f5e3Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
1004514f5e3Sopenharmony_ci{
1014514f5e3Sopenharmony_ci    // Run your code on data.
1024514f5e3Sopenharmony_ci    OHOS::SetRuntimeInfoFuzzTest(data, size);
1034514f5e3Sopenharmony_ci    OHOS::GetRuntimeInfoByPathFuzzTest(data, size);
1044514f5e3Sopenharmony_ci    OHOS::GetRuntimeBuildIdFuzzTest(data, size);
1054514f5e3Sopenharmony_ci    return 0;
1064514f5e3Sopenharmony_ci}