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