1 /* 2 * Copyright (c) 2021 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 #ifndef FUZZ_CONFIG_PARSER_H 16 #define FUZZ_CONFIG_PARSER_H 17 18 #include <iostream> 19 #include <fstream> 20 #include <string> 21 #include <vector> 22 23 #include "nlohmann/json.hpp" 24 25 namespace OHOS { 26 const std::string FUZZ_TEST_CONFIG_FILE_PATH {"/data/fuzztestconfig/config.json"}; 27 28 const std::string FUZZ_TEST_MAIN_LOOP_KEY {"flag"}; 29 30 struct FuzzTestData { 31 int32_t mainLoopFlag {0}; 32 std::vector<std::string> methodVec {}; 33 }; 34 35 class FuzzConfigParser { 36 public: ParseFromFile4FuzzTest(const std::string & path,FuzzTestData & ftd)37 void ParseFromFile4FuzzTest(const std::string &path, FuzzTestData &ftd) 38 { 39 std::cout << __func__ << std::endl; 40 if (path.empty()) { 41 std::cout << __FUNCTION__ << " invalid file path, check!" << std::endl; 42 return; 43 } 44 45 nlohmann::json jsonObj; 46 std::ifstream(path) >> jsonObj; 47 std::cout << path; 48 for (auto it = jsonObj.begin(); it != jsonObj.end(); ++it) { 49 if (!it.key().compare(FUZZ_TEST_MAIN_LOOP_KEY)) { 50 ftd.mainLoopFlag = it.value(); 51 continue; 52 } 53 54 auto className = it.key(); 55 if (it->is_structured()) { 56 for (auto itm = it->begin(); itm != it->end(); ++itm) { 57 auto methodName = itm.key(); 58 59 if (!(it->is_structured() && (it->size() != 0))) { 60 ftd.methodVec.push_back(className + methodName); 61 continue; 62 } 63 64 std::string param {}; 65 for (auto itp = itm->begin(); itp != itm->end(); ++itp) { 66 auto tp = itp.value(); 67 param += tp; 68 } 69 ftd.methodVec.push_back(className + methodName + param); 70 } 71 } 72 } 73 } 74 }; 75 } // namespace OHOS 76 77 #endif // FUZZ_CONFIG_PARSER_H 78