• 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 "CommandParse.h"
17 #include <random>
18 #include "secodeFuzz.h"
19 #include "cJSON.h"
20 #include "securec.h"
21 #include "CommandLineInterface.h"
22 #include "ChangeJsonUtil.h"
23 using namespace std;
24 using namespace fuzztest;
25 
Execute(std::string & commond,std::string & jsonArgsStr,int & typeIndex,uint64_t & index,bool changeType)26 void CommandParse::Execute(std::string& commond, std::string& jsonArgsStr,
27     int& typeIndex, uint64_t& index, bool changeType)
28 {
29     // 变化数据,不改变数据类型
30     cJSON* jsonData = cJSON_CreateObject();
31     if (jsonData == nullptr) {
32         return;
33     }
34     cJSON_AddItemToObject(jsonData, "version", cJSON_CreateString("1.0.1"));
35     cJSON_AddItemToObject(jsonData, "command", cJSON_CreateString(commond.c_str()));
36     cJSON_AddItemToObject(jsonData, "type", cJSON_CreateString(types[typeIndex].c_str()));
37     if (!jsonArgsStr.empty()) {
38         cJSON* jsonArgs = cJSON_Parse(jsonArgsStr.c_str());
39         if (!changeType) {
40             ChangeJsonUtil::ModifyObject(jsonArgs, index);
41         } else {
42             ChangeJsonUtil::ModifyObject4ChangeType(jsonArgs, index);
43         }
44         cJSON_AddItemToObject(jsonData, "args", jsonArgs);
45     }
46     char* dataStr = cJSON_PrintUnformatted(jsonData);
47     printf("============================command1:%s\r\n", dataStr);
48     std::string dataString = std::string(dataStr);
49     free(dataStr);
50     CommandLineInterface::GetInstance().ProcessCommandMessage(dataString);
51     cJSON_Delete(jsonData);
52 }
53 
CreateAndExecuteCommand(std::map<std::string,std::string> dataMap)54 void CommandParse::CreateAndExecuteCommand(std::map<std::string, std::string> dataMap)
55 {
56     CommandLineInterface::GetInstance().Init("pipeName");
57     uint64_t index = 0;
58     for (map<string, string>::iterator iter = dataMap.begin(); iter != dataMap.end(); iter++) {
59         for (int j = 0; j < types.size(); j++) {
60             std::string key = iter->first;
61             std::string val = iter->second;
62             Execute(key, val, j, index, false);
63             Execute(key, val, j, index, true);
64         }
65     }
66 }