17c804472Sopenharmony_ci/*
27c804472Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
37c804472Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
47c804472Sopenharmony_ci * you may not use this file except in compliance with the License.
57c804472Sopenharmony_ci * You may obtain a copy of the License at
67c804472Sopenharmony_ci *
77c804472Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
87c804472Sopenharmony_ci *
97c804472Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
107c804472Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
117c804472Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127c804472Sopenharmony_ci * See the License for the specific language governing permissions and
137c804472Sopenharmony_ci * limitations under the License.
147c804472Sopenharmony_ci */
157c804472Sopenharmony_ci
167c804472Sopenharmony_ci#include "CommandParse.h"
177c804472Sopenharmony_ci#include <random>
187c804472Sopenharmony_ci#include "secodeFuzz.h"
197c804472Sopenharmony_ci#include "cJSON.h"
207c804472Sopenharmony_ci#include "securec.h"
217c804472Sopenharmony_ci#include "CommandLineInterface.h"
227c804472Sopenharmony_ci#include "ChangeJsonUtil.h"
237c804472Sopenharmony_ciusing namespace fuzztest;
247c804472Sopenharmony_ci
257c804472Sopenharmony_civoid CommandParse::Execute(std::string& commond, std::string& jsonArgsStr,
267c804472Sopenharmony_ci    int& typeIndex, uint64_t& index, bool changeType)
277c804472Sopenharmony_ci{
287c804472Sopenharmony_ci    // 变化数据,不改变数据类型
297c804472Sopenharmony_ci    cJSON* jsonData = cJSON_CreateObject();
307c804472Sopenharmony_ci    if (jsonData == nullptr) {
317c804472Sopenharmony_ci        return;
327c804472Sopenharmony_ci    }
337c804472Sopenharmony_ci    cJSON_AddItemToObject(jsonData, "version", cJSON_CreateString("1.0.1"));
347c804472Sopenharmony_ci    cJSON_AddItemToObject(jsonData, "command", cJSON_CreateString(commond.c_str()));
357c804472Sopenharmony_ci    cJSON_AddItemToObject(jsonData, "type", cJSON_CreateString(types[typeIndex].c_str()));
367c804472Sopenharmony_ci    if (!jsonArgsStr.empty()) {
377c804472Sopenharmony_ci        cJSON* jsonArgs = cJSON_Parse(jsonArgsStr.c_str());
387c804472Sopenharmony_ci        if (!changeType) {
397c804472Sopenharmony_ci            ChangeJsonUtil::ModifyObject(jsonArgs, index);
407c804472Sopenharmony_ci        } else {
417c804472Sopenharmony_ci            ChangeJsonUtil::ModifyObject4ChangeType(jsonArgs, index);
427c804472Sopenharmony_ci        }
437c804472Sopenharmony_ci        cJSON_AddItemToObject(jsonData, "args", jsonArgs);
447c804472Sopenharmony_ci    }
457c804472Sopenharmony_ci    char* dataStr = cJSON_PrintUnformatted(jsonData);
467c804472Sopenharmony_ci    printf("============================command1:%s\r\n", dataStr);
477c804472Sopenharmony_ci    std::string dataString = std::string(dataStr);
487c804472Sopenharmony_ci    free(dataStr);
497c804472Sopenharmony_ci    CommandLineInterface::GetInstance().ProcessCommandMessage(dataString);
507c804472Sopenharmony_ci    cJSON_Delete(jsonData);
517c804472Sopenharmony_ci}
527c804472Sopenharmony_ci
537c804472Sopenharmony_civoid CommandParse::CreateAndExecuteCommand(std::map<std::string, std::string> dataMap)
547c804472Sopenharmony_ci{
557c804472Sopenharmony_ci    CommandLineInterface::GetInstance().Init("pipeName");
567c804472Sopenharmony_ci    uint64_t index = 0;
577c804472Sopenharmony_ci    for (std::map<std::string, std::string>::iterator iter = dataMap.begin(); iter != dataMap.end(); iter++) {
587c804472Sopenharmony_ci        for (int j = 0; j < types.size(); j++) {
597c804472Sopenharmony_ci            std::string key = iter->first;
607c804472Sopenharmony_ci            std::string val = iter->second;
617c804472Sopenharmony_ci            Execute(key, val, j, index, false);
627c804472Sopenharmony_ci            Execute(key, val, j, index, true);
637c804472Sopenharmony_ci        }
647c804472Sopenharmony_ci    }
657c804472Sopenharmony_ci}