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 <map>
18#include <gtest/gtest.h>
19#include "cJSON.h"
20#include "secodeFuzz.h"
21#include "common.h"
22#include "CommandLineInterface.h"
23#include "ChangeJsonUtil.h"
24#define private public
25#include "CommandParser.h"
26using namespace fuzztest;
27
28namespace {
29std::string g_deviceConfigJson = R"({
30    "setting" : {
31        "1.0.1" : {
32            "Language" : {
33                "args" : {
34                    "Language" : "zh-CN"
35                }
36            }
37        }
38    },
39    "frontend" : {
40        "1.0.0" : {
41            "Resolution" : {
42                "args" : {
43                    "Resolution" : "360*780"
44                }
45            },
46            "DeviceType" : {
47                "args" : {
48                    "DeviceType" : "phone"
49                }
50            }
51        }
52    }
53})";
54
55TEST(DeviceConfigParseFuzzTest, test_json)
56{
57    std::cout << "--> DeviceConfigParseFuzzTest for common start <--" << std::endl;
58    DT_FUZZ_START(0, TEST_TIMES, (char*)"DeviceConfigParseFuzzTest", 0)
59    {
60        CommandLineInterface::GetInstance().Init("pipeName");
61        CommandParser::GetInstance().deviceType = "phone";
62        uint64_t index = 0;
63        // 变化数据
64        cJSON* jsonArgs = cJSON_Parse(g_deviceConfigJson.c_str());
65        ChangeJsonUtil::ModifyObject(jsonArgs, index);
66        // 保存到文件
67        std::string filePath = "phoneSettingConfig_Phone.json";
68        ChangeJsonUtil::WriteFile(filePath, jsonArgs);
69        cJSON_Delete(jsonArgs);
70        // 执行接口
71        CommandLineInterface::GetInstance().ReadAndApplyConfig(filePath);
72        CommandLineInterface::GetInstance().ReadAndApplyConfig("");
73    }
74    DT_FUZZ_END()
75    printf("end ---- DeviceConfigParseFuzzTest\r\n");
76    if (DT_GetIsPass() == 0) {
77        printf("test DeviceConfigParseFuzzTest is not ok\r\n");
78    } else {
79        printf("test DeviceConfigParseFuzzTest is ok\r\n");
80    }
81    std::cout << "--> DeviceConfigParseFuzzTest for common end <--" << std::endl;
82}
83}