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 <string> 17#include <map> 18#include <fstream> 19#include <iostream> 20#include <filesystem> 21#include <gtest/gtest.h> 22#include "cJSON.h" 23#include "secodeFuzz.h" 24#include "common.h" 25#include "StageContext.h" 26#include "ChangeJsonUtil.h" 27#include "MockFile.h" 28using namespace fuzztest; 29using namespace testmock; 30 31namespace { 32std::string g_moduleJson = R"({ 33 "modulePathMap" : { 34 "entry" : "C:\\MyApp1\\entry", 35 "library" : "C:\\MyApp1\\library" 36 }, 37 "compileMode" : "esmodule", 38 "projectRootPath" : "C:\\MyApp1", 39 "nodeModulesPath" : "C:\\MyApp1\\entry\\.preview\\default\\intermediates\\loader_out\\default\\node_modules", 40 "moduleName" : "entry", 41 "hspNameOhmMap" : { 42 "library" : "@bundle:com.example.myapplication/library/Index" 43 }, 44 "harNameOhmMap" : { 45 "library" : "@bundle:com.example.myapplication/library/Index" 46 }, 47 "packageManagerType" : "ohpm", 48 "compileEntry" : [], 49 "dynamicImportLibInfo" : {}, 50 "anBuildOutPut" : "C:\\MyApp1\\entry\\.preview\\default\\intermediates\\loader_out\\default\\an\\arm64-v8a", 51 "anBuildMode" : "type", 52 "buildConfigPath" : ".preview\\config\\buildConfig.json" 53})"; 54 55TEST(ReadFileContentsFuzzTest, test_json) 56{ 57 std::cout << "--> ReadFileContentsFuzzTest for common start <--" << std::endl; 58 DT_FUZZ_START(0, TEST_TIMES, (char*)"ReadFileContentsFuzzTest", 0) 59 { 60 uint64_t index = 0; 61 // 变化数据 62 cJSON* jsonArgs = cJSON_Parse(g_moduleJson.c_str()); 63 ChangeJsonUtil::ModifyObject(jsonArgs, index); 64 // 保存到文件 65 std::string filePath = "testFile.json"; 66 ChangeJsonUtil::WriteFile(filePath, jsonArgs); 67 cJSON_Delete(jsonArgs); 68 // 执行接口 69 OHOS::Ide::StageContext::GetInstance().ReadFileContents("aaa"); 70 MockFile::SimulateFileLock(filePath); 71 OHOS::Ide::StageContext::GetInstance().ReadFileContents(filePath); 72 MockFile::ReleaseFileLock(filePath); 73 OHOS::Ide::StageContext::GetInstance().ReadFileContents(filePath); 74 } 75 DT_FUZZ_END() 76 printf("end ---- ReadFileContentsFuzzTest\r\n"); 77 if (DT_GetIsPass() == 0) { 78 printf("test ReadFileContentsFuzzTest is not ok\r\n"); 79 } else { 80 printf("test ReadFileContentsFuzzTest is ok\r\n"); 81 } 82 std::cout << "--> ReadFileContentsFuzzTest for common end <--" << std::endl; 83} 84}