• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <map>
17 #include <string>
18 #include <iostream>
19 #include <fstream>
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"
27 #define private public
28 #include "CommandParser.h"
29 using namespace fuzztest;
30 
31 namespace {
32 std::string g_moduleJsonNoModule = R"({
33     "app" :
34     {
35         "apiReleaseType" : "Beta1"
36     }
37 })";
38 
39 std::string g_moduleJsonNoModuleName = R"({
40     "module" :
41     {
42         "packageName" : "entry"
43     }
44 })";
45 
46 std::string g_moduleJsonNoModulePackageName = R"({
47     "module" :
48     {
49         "name" : "entry"
50     }
51 })";
52 
53 std::string g_moduleJson = R"({
54     "module" :
55     {
56         "name" : "entry",
57         "type" : "entry",
58         "description" : "$string:module_desc",
59         "mainElement" : "EntryAbility",
60         "deviceTypes" :
61         [
62             "phone",
63             "tablet",
64             "2in1"
65         ],
66         "deliveryWithInstall" : true,
67         "installationFree" : false,
68         "pages" : "$profile:main_pages",
69         "extensionAbilities": [{"name": ""}],
70         "packageName":  "entry",
71         "virtualMachine" : "ark11.0.2.0",
72         "compileMode" : "esmodule",
73         "descriptionId" : 16777220
74     }
75 })";
76 
77 std::string g_pkgInfoJson = R"({
78     "entry": {
79         "packageName": "entry",
80         "bundleName": "",
81         "moduleName": "",
82         "version": "",
83         "entryPath": "src/main/",
84         "isSO": false,
85         "dependencyAlias": ""
86     }
87 })";
88 
TEST(SetPkgContextInfoFuzzTest,test_json)89 TEST(SetPkgContextInfoFuzzTest, test_json)
90 {
91     std::cout << "--> SetPkgContextInfoFuzzTest for common start <--" << std::endl;
92     DT_FUZZ_START(0, TEST_TIMES, (char*)"SetPkgContextInfoFuzzTest", 0)
93     {
94         std::map<std::string, std::string> pkgContextMap;
95         std::map<std::string, std::string> packageNameList;
96         CommandParser::GetInstance().appResourcePath = ".";
97         CommandParser::GetInstance().loaderJsonPath = "./loader.json";
98         // 变化数据
99         std::string filePath = "module.json";
100         ChangeJsonUtil::WriteFile(filePath, g_moduleJsonNoModule);
101         OHOS::Ide::StageContext::GetInstance().SetPkgContextInfo(pkgContextMap, packageNameList);
102         ChangeJsonUtil::WriteFile(filePath, g_moduleJsonNoModuleName);
103         OHOS::Ide::StageContext::GetInstance().SetPkgContextInfo(pkgContextMap, packageNameList);
104         ChangeJsonUtil::WriteFile(filePath, g_moduleJsonNoModulePackageName);
105         OHOS::Ide::StageContext::GetInstance().SetPkgContextInfo(pkgContextMap, packageNameList);
106         uint64_t moduleIndex = 0;
107         cJSON* moduleJsonArgs = cJSON_Parse(g_moduleJson.c_str());
108         ChangeJsonUtil::ModifyObject(moduleJsonArgs, moduleIndex);
109         ChangeJsonUtil::WriteFile(filePath, moduleJsonArgs);
110         cJSON_Delete(moduleJsonArgs);
111         std::string pkgContextFilePath = "pkgContextInfo.json";
112         uint64_t pkgContextIndex = 0;
113         cJSON* pkgContextJsonArgs = cJSON_Parse(g_pkgInfoJson.c_str());
114         ChangeJsonUtil::ModifyObject(pkgContextJsonArgs, pkgContextIndex);
115         ChangeJsonUtil::WriteFile(pkgContextFilePath, pkgContextJsonArgs);
116         cJSON_Delete(pkgContextJsonArgs);
117         // 执行接口
118         OHOS::Ide::StageContext::GetInstance().SetPkgContextInfo(pkgContextMap, packageNameList);
119     }
120     DT_FUZZ_END()
121     printf("end ---- SetPkgContextInfoFuzzTest\r\n");
122     if (DT_GetIsPass() == 0) {
123         printf("test SetPkgContextInfoFuzzTest is not ok\r\n");
124     } else {
125         printf("test SetPkgContextInfoFuzzTest is ok\r\n");
126     }
127     std::cout << "--> SetPkgContextInfoFuzzTest for common end <--" << std::endl;
128 }
129 }