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 <string>
17#include <fstream>
18#include <map>
19#include <iostream>
20#include <filesystem>
21#include "cJSON.h"
22#include <gtest/gtest.h>
23#include "secodeFuzz.h"
24#include "StageContext.h"
25#include "common.h"
26#include "ChangeJsonUtil.h"
27using namespace fuzztest;
28
29namespace {
30std::string g_moduleJson1 = R"({
31    "modulePathMap" : {
32        "entry" : "C:\\MyApp\\entry",
33        "library" : "C:\\MyApp\\library"
34    },
35    "compileMode" : "esmodule",
36    "projectRootPath" : "C:\\MyApp",
37    "nodeModulesPath" : "C:\\MyApp\\entry\\.preview\\default\\intermediates\\loader_out\\default\\node_modules",
38    "moduleName" : "entry",
39    "hspNameOhmMap" : {
40        "library" : "@bundle:com.example.myapplication/library/Index"
41    },
42    "harNameOhmMap" : {
43        "library" : "@bundle:com.example.myapplication/library/Index"
44    },
45    "packageManagerType" : "ohpm",
46    "compileEntry" : [],
47    "dynamicImportLibInfo" : {},
48    "anBuildOutPut" : "C:\\MyApp\\entry\\.preview\\default\\intermediates\\loader_out\\default\\an\\arm64-v8a",
49    "anBuildMode" : "type",
50    "buildConfigPath" : ".preview\\config\\buildConfig.json"
51})";
52
53std::string g_moduleJson2 = R"({
54    "projectRootPath": "C:\\MyApp"
55})";
56
57TEST(GetModulePathMapFuzzTest, test_json)
58{
59    std::cout << "--> GetModulePathMapFuzzTest for common start <--" << std::endl;
60    DT_FUZZ_START(0, TEST_TIMES, (char*)"GetModulePathMapFuzzTest", 0)
61    {
62        uint64_t index = 0;
63        // 变化数据
64        cJSON* jsonArgs = cJSON_Parse(g_moduleJson1.c_str());
65        ChangeJsonUtil::ModifyObject(jsonArgs, index);
66        // 保存到文件
67        std::string filePath = "loader.json";
68        ChangeJsonUtil::WriteFile(filePath, jsonArgs);
69        cJSON_Delete(jsonArgs);
70        // 执行接口
71        OHOS::Ide::StageContext::GetInstance().SetLoaderJsonPath("");
72        OHOS::Ide::StageContext::GetInstance().GetModulePathMapFromLoaderJson();
73        OHOS::Ide::StageContext::GetInstance().SetLoaderJsonPath(filePath);
74        OHOS::Ide::StageContext::GetInstance().GetModulePathMapFromLoaderJson();
75        ChangeJsonUtil::WriteFile(filePath, g_moduleJson2);
76        OHOS::Ide::StageContext::GetInstance().GetModulePathMapFromLoaderJson();
77        ChangeJsonUtil::WriteFile(filePath, "aaa");
78        OHOS::Ide::StageContext::GetInstance().GetModulePathMapFromLoaderJson();
79    }
80    DT_FUZZ_END()
81    printf("end ---- GetModulePathMapFuzzTest\r\n");
82    if (DT_GetIsPass() == 0) {
83        printf("test GetModulePathMapFuzzTest is not ok\r\n");
84    } else {
85        printf("test GetModulePathMapFuzzTest is ok\r\n");
86    }
87    std::cout << "--> GetModulePathMapFuzzTest for common end <--" << std::endl;
88}
89}