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