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 <iostream>
19 #include <fstream>
20 #include <filesystem>
21 #include "cJSON.h"
22 #include <gtest/gtest.h>
23 #include "secodeFuzz.h"
24 #include "common.h"
25 #include "ChangeJsonUtil.h"
26 #include "StageContext.h"
27 using namespace fuzztest;
28
29 namespace {
30 std::string g_mockConfigJson = R"({
31 "libentry.so": {
32 "source": "src/mock/libentry.mock.ets"
33 }
34 })";
35
TEST(ParseMockJsonFileFuzzTest,test_json)36 TEST(ParseMockJsonFileFuzzTest, test_json)
37 {
38 std::cout << "--> ParseMockJsonFileFuzzTest for common start <--" << std::endl;
39 DT_FUZZ_START(0, TEST_TIMES, (char*)"ParseMockJsonFileFuzzTest", 0)
40 {
41 uint64_t index = 0;
42 // 变化数据
43 cJSON* jsonArgs = cJSON_Parse(g_mockConfigJson.c_str());
44 ChangeJsonUtil::ModifyObject(jsonArgs, index);
45 // 保存到文件
46 std::string filePath = "mock-config.json";
47 ChangeJsonUtil::WriteFile(filePath, jsonArgs);
48 cJSON_Delete(jsonArgs);
49 // 执行接口
50 OHOS::Ide::StageContext::GetInstance().ParseMockJsonFile("aaa");
51 OHOS::Ide::StageContext::GetInstance().ParseMockJsonFile(filePath);
52 ChangeJsonUtil::WriteFile(filePath, "aaa");
53 OHOS::Ide::StageContext::GetInstance().ParseMockJsonFile(filePath);
54 }
55 DT_FUZZ_END()
56 printf("end ---- ParseMockJsonFileFuzzTest\r\n");
57 if (DT_GetIsPass() == 0) {
58 printf("test ParseMockJsonFileFuzzTest is not ok\r\n");
59 } else {
60 printf("test ParseMockJsonFileFuzzTest is ok\r\n");
61 }
62 std::cout << "--> ParseMockJsonFileFuzzTest for common end <--" << std::endl;
63 }
64 }