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 #ifndef PARAMS_PARSE_FUZZER_H 17 #define PARAMS_PARSE_FUZZER_H 18 19 #include <string> 20 #include <vector> 21 #include <fstream> 22 #include <filesystem> 23 #include <gtest/gtest.h> 24 25 namespace fuzztest { 26 static std::string g_currDir = ""; 27 static std::string g_currFile = ""; 28 29 class Param { 30 public: Param(std::string name,std::vector<std::string> values)31 Param(std::string name, std::vector<std::string> values) : name(name), values(values) {} 32 std::string name; 33 std::vector<std::string> values; 34 }; 35 36 class ParamsParse { 37 public: 38 void ParamsParseFuzzTest(); 39 private: 40 void CallParamsParseFunc(const std::vector<std::string>& args); 41 void SetTestArgs(std::vector<std::string>& args); 42 std::vector<Param> paramList = { 43 Param("-j", { g_currDir }), 44 Param("-n", { "entry" }), 45 Param("-d", { "" }), 46 Param("-p", { "8888" }), 47 Param("-s", { "phone_1676450550023_1" }), 48 Param("-or", { "1080", "2340" }), 49 Param("-cr", { "1080", "2340" }), 50 Param("-f", { g_currFile }), 51 Param("-hs", { "102400" }), 52 Param("-hf", { "true" }), 53 Param("-shape", { "rect" }), 54 Param("-device", { "phone" }), 55 Param("-url", { "pages/Index" }), 56 Param("-refresh", { "region" }), 57 Param("-card", { "true" }), 58 Param("-projectID", { "985150866" }), 59 Param("-ts", { "trace_93488_commandPipe" }), 60 Param("-cm", { "light" }), 61 Param("-o", { "portrait" }), 62 Param("-lws", { "40000" }), 63 Param("-av", { "ACE_2_0" }), 64 Param("-l", { "zh_CN" }), 65 Param("-sd", { "480" }), 66 Param("-sm", { "dynamic" }), 67 Param("-arp", { g_currDir }), 68 Param("-pm", { "Stage" }), 69 Param("-pages", { "main_pages" }), 70 Param("-hsp", { g_currDir }), 71 Param("-cpm", { "true" }), 72 Param("-abp", { "ets/entryability/EntryAbility.abc" }), 73 Param("-abn", { "EntryAbility" }), 74 Param("-staticCard", { "true" }), 75 Param("-foldable", { "true" }), 76 Param("-foldStatus", { "unfold" }), 77 Param("-fr", { "1080", "2340" }), 78 Param("-ljPath", { g_currFile }), 79 Param("-sid", { "abcdef1111" }) 80 }; 81 }; 82 83 class ParamsParseFuzzTest : public testing::Test { 84 protected: 85 // 在整个测试夹具类执行前执行一次初始化操作 SetUpTestCase()86 static void SetUpTestCase() 87 { 88 // 使用空格分割字符串,并存入vector 89 char buffer[FILENAME_MAX]; 90 if (getcwd(buffer, FILENAME_MAX) != nullptr) { 91 g_currDir = std::string(buffer); 92 g_currDir += "/MyApplication"; 93 int status = mkdir(g_currDir.c_str(), 0777); // 0777 表示所有用户有读、写、执行权限 94 if (status != 0) { 95 printf("Error creating folder!\n"); 96 } 97 g_currFile = g_currDir + "/test.json"; 98 // 创建文件流对象并打开文件 99 std::ofstream file(g_currFile); 100 // 检查文件是否成功打开 101 if (file.is_open()) { 102 file.close(); 103 } else { 104 printf("Error creating file!\n"); 105 } 106 } else { 107 printf("error: getcwd failed\n"); 108 } 109 } 110 111 // 在整个测试夹具类执行后执行一次清理操作 TearDownTestCase()112 static void TearDownTestCase() 113 { 114 if (std::remove(g_currFile.c_str()) != 0) { 115 printf("Error deleting file!\n"); 116 } 117 std::filesystem::remove(g_currDir.c_str()); 118 } 119 SetUp()120 virtual void SetUp() 121 { 122 std::cout << "--> ParamsParseFuzzTest." << __func__ << " <--" <<std::endl; 123 } 124 TearDown()125 virtual void TearDown() 126 { 127 std::cout << "--> ParamsParseFuzzTest." << __func__ << " <--" <<std::endl; 128 } 129 130 ParamsParse parse; 131 }; 132 } 133 134 #endif