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 "ChangeJsonUtil.h" 177c804472Sopenharmony_ci#include <random> 187c804472Sopenharmony_ci#include <fstream> 197c804472Sopenharmony_ci#include <iostream> 207c804472Sopenharmony_ci#include "secodeFuzz.h" 217c804472Sopenharmony_ci#include "cJSON.h" 227c804472Sopenharmony_ci#include "securec.h" 237c804472Sopenharmony_ciusing namespace fuzztest; 247c804472Sopenharmony_ci 257c804472Sopenharmony_cinamespace { 267c804472Sopenharmony_ciconst int DEFAULT_LENGTH = 1000; 277c804472Sopenharmony_ciconst int DEFAULT_INT = 1000; 287c804472Sopenharmony_ciconst std::string DEFAULT_STRING = "aaaa"; 297c804472Sopenharmony_ci} 307c804472Sopenharmony_ci 317c804472Sopenharmony_civoid ChangeJsonUtil::ModifyObject(cJSON *object, uint64_t& idx) 327c804472Sopenharmony_ci{ 337c804472Sopenharmony_ci if (!object) { 347c804472Sopenharmony_ci return; 357c804472Sopenharmony_ci } 367c804472Sopenharmony_ci cJSON *item = nullptr; 377c804472Sopenharmony_ci cJSON_ArrayForEach(item, object) { 387c804472Sopenharmony_ci if (item->type == cJSON_String) { 397c804472Sopenharmony_ci std::string strVal = DT_SetGetString(&g_Element[idx], strlen(item->valuestring) + 1, DEFAULT_LENGTH, 407c804472Sopenharmony_ci (char*)item->valuestring); 417c804472Sopenharmony_ci idx++; 427c804472Sopenharmony_ci if (item->valuestring != nullptr) { 437c804472Sopenharmony_ci cJSON_free(item->valuestring); // 释放原字符串内存 447c804472Sopenharmony_ci } 457c804472Sopenharmony_ci item->valuestring = nullptr; 467c804472Sopenharmony_ci int length = strVal.length() + 1; 477c804472Sopenharmony_ci 487c804472Sopenharmony_ci void* ptr = malloc(length * sizeof(char)); 497c804472Sopenharmony_ci if (ptr != nullptr) { 507c804472Sopenharmony_ci item->valuestring = reinterpret_cast<char*>(ptr); 517c804472Sopenharmony_ci } 527c804472Sopenharmony_ci errno_t ret1 = strcpy_s(item->valuestring, length, strVal.c_str()); // 复制字符串内容 537c804472Sopenharmony_ci if (ret1 != EOK) { 547c804472Sopenharmony_ci printf("strcpy_s in modifyObject copy error"); 557c804472Sopenharmony_ci } 567c804472Sopenharmony_ci } else if (item->type == cJSON_Number) { 577c804472Sopenharmony_ci int intVal = *(s32 *)DT_SetGetS32(&g_Element[idx], item->valueint); 587c804472Sopenharmony_ci idx++; 597c804472Sopenharmony_ci item->valueint = intVal; 607c804472Sopenharmony_ci } else if (item->type == cJSON_True || item->type == cJSON_False) { 617c804472Sopenharmony_ci std::random_device rd; 627c804472Sopenharmony_ci std::mt19937 gen(rd()); 637c804472Sopenharmony_ci std::bernoulli_distribution distribution(0.5); // 0.5 is probability to generate true or false 647c804472Sopenharmony_ci bool ret = distribution(gen); 657c804472Sopenharmony_ci item->type = ret ? cJSON_True : cJSON_False; 667c804472Sopenharmony_ci } else if (item->type == cJSON_Object) { 677c804472Sopenharmony_ci cJSON *arrayItem = nullptr; 687c804472Sopenharmony_ci cJSON_ArrayForEach(arrayItem, item) { 697c804472Sopenharmony_ci ModifyObject(arrayItem, idx); 707c804472Sopenharmony_ci } 717c804472Sopenharmony_ci } else if (item->type == cJSON_Array) { 727c804472Sopenharmony_ci cJSON *objItem = nullptr; 737c804472Sopenharmony_ci cJSON_ArrayForEach(objItem, item) { 747c804472Sopenharmony_ci ModifyObject(objItem, idx); 757c804472Sopenharmony_ci } 767c804472Sopenharmony_ci } 777c804472Sopenharmony_ci } 787c804472Sopenharmony_ci} 797c804472Sopenharmony_ci 807c804472Sopenharmony_civoid ChangeJsonUtil::ModifyObject4ChangeType(cJSON *object, uint64_t& idx) 817c804472Sopenharmony_ci{ 827c804472Sopenharmony_ci if (!object) { 837c804472Sopenharmony_ci return; 847c804472Sopenharmony_ci } 857c804472Sopenharmony_ci cJSON *item = nullptr; 867c804472Sopenharmony_ci cJSON_ArrayForEach(item, object) { 877c804472Sopenharmony_ci if (item->type == cJSON_String) { 887c804472Sopenharmony_ci int32_t intValue = *(s32 *)DT_SetGetS32(&g_Element[idx], DEFAULT_INT); 897c804472Sopenharmony_ci idx++; 907c804472Sopenharmony_ci item->type = cJSON_Number; 917c804472Sopenharmony_ci item->valuedouble = static_cast<double>(intValue); 927c804472Sopenharmony_ci item->valueint = intValue; 937c804472Sopenharmony_ci if (item->valuestring != nullptr) { 947c804472Sopenharmony_ci cJSON_free(item->valuestring); // 释放原字符串内存 957c804472Sopenharmony_ci } 967c804472Sopenharmony_ci item->valuestring = nullptr; 977c804472Sopenharmony_ci } else if (item->type == cJSON_Number || item->type == cJSON_True || item->type == cJSON_False) { 987c804472Sopenharmony_ci std::string strVal = DT_SetGetString(&g_Element[idx], DEFAULT_STRING.size() + 1, DEFAULT_LENGTH, 997c804472Sopenharmony_ci (char*)DEFAULT_STRING.c_str()); 1007c804472Sopenharmony_ci idx++; 1017c804472Sopenharmony_ci item->type = cJSON_String; 1027c804472Sopenharmony_ci int length = strVal.length() + 1; 1037c804472Sopenharmony_ci void* ptr = malloc(length * sizeof(char)); 1047c804472Sopenharmony_ci if (ptr != nullptr) { 1057c804472Sopenharmony_ci item->valuestring = reinterpret_cast<char*>(ptr); 1067c804472Sopenharmony_ci } 1077c804472Sopenharmony_ci errno_t ret2 = strcpy_s(item->valuestring, length, strVal.c_str()); // 复制字符串内容 1087c804472Sopenharmony_ci if (ret2 != EOK) { 1097c804472Sopenharmony_ci printf("strcpy_s in modifyObject4ChangeType copy error"); 1107c804472Sopenharmony_ci } 1117c804472Sopenharmony_ci } else if (item->type == cJSON_Object) { 1127c804472Sopenharmony_ci cJSON *arrayItem = nullptr; 1137c804472Sopenharmony_ci cJSON_ArrayForEach(arrayItem, item) { 1147c804472Sopenharmony_ci ModifyObject4ChangeType(arrayItem, idx); 1157c804472Sopenharmony_ci } 1167c804472Sopenharmony_ci } else if (item->type == cJSON_Array) { 1177c804472Sopenharmony_ci cJSON *objItem = nullptr; 1187c804472Sopenharmony_ci cJSON_ArrayForEach(objItem, item) { 1197c804472Sopenharmony_ci ModifyObject4ChangeType(objItem, idx); 1207c804472Sopenharmony_ci } 1217c804472Sopenharmony_ci } 1227c804472Sopenharmony_ci } 1237c804472Sopenharmony_ci} 1247c804472Sopenharmony_ci 1257c804472Sopenharmony_civoid ChangeJsonUtil::WriteFile(std::string filePath, cJSON *object) 1267c804472Sopenharmony_ci{ 1277c804472Sopenharmony_ci // Check if cJSON object is valid 1287c804472Sopenharmony_ci if (object == nullptr) { 1297c804472Sopenharmony_ci std::cerr << "Error: cJSON object is null." << std::endl; 1307c804472Sopenharmony_ci return; 1317c804472Sopenharmony_ci } 1327c804472Sopenharmony_ci 1337c804472Sopenharmony_ci // Open file stream 1347c804472Sopenharmony_ci std::ofstream outFile(filePath); 1357c804472Sopenharmony_ci if (!outFile.is_open()) { 1367c804472Sopenharmony_ci std::cerr << "Error opening file: " << filePath << std::endl; 1377c804472Sopenharmony_ci return; 1387c804472Sopenharmony_ci } 1397c804472Sopenharmony_ci 1407c804472Sopenharmony_ci // Convert cJSON object to string 1417c804472Sopenharmony_ci char *jsonStr = cJSON_Print(object); 1427c804472Sopenharmony_ci if (jsonStr == nullptr) { 1437c804472Sopenharmony_ci std::cerr << "Error converting cJSON object to string." << std::endl; 1447c804472Sopenharmony_ci outFile.close(); 1457c804472Sopenharmony_ci return; 1467c804472Sopenharmony_ci } 1477c804472Sopenharmony_ci 1487c804472Sopenharmony_ci // Write JSON string to file 1497c804472Sopenharmony_ci outFile << jsonStr << std::endl; 1507c804472Sopenharmony_ci 1517c804472Sopenharmony_ci // Clean up 1527c804472Sopenharmony_ci cJSON_free(jsonStr); 1537c804472Sopenharmony_ci outFile.close(); 1547c804472Sopenharmony_ci 1557c804472Sopenharmony_ci std::cout << "JSON content successfully written to file: " << filePath << std::endl; 1567c804472Sopenharmony_ci} 1577c804472Sopenharmony_ci 1587c804472Sopenharmony_civoid ChangeJsonUtil::WriteFile(std::string filePath, std::string str) 1597c804472Sopenharmony_ci{ 1607c804472Sopenharmony_ci std::ofstream outFile(filePath); 1617c804472Sopenharmony_ci if (!outFile.is_open()) { 1627c804472Sopenharmony_ci std::cerr << "Error opening file: " << filePath << std::endl; 1637c804472Sopenharmony_ci return; 1647c804472Sopenharmony_ci } 1657c804472Sopenharmony_ci outFile << str << std::endl; 1667c804472Sopenharmony_ci std::cout << "Str content successfully written to file: " << filePath << std::endl; 1677c804472Sopenharmony_ci}