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