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 TEST_CONFIG_PARSER_H 16 #define TEST_CONFIG_PARSER_H 17 18 #include <iostream> 19 #include <fstream> 20 #include <string> 21 22 #include "nlohmann/json.hpp" 23 24 namespace OHOS { 25 const std::string STRESS_TEST_CONFIG_FILE_PATH {"/data/testconfig/stressconfig.json"}; 26 27 const std::string STRESS_TEST_AMS_KEY {"AMS"}; 28 const std::string STRESS_TEST_BMS_KEY {"BMS"}; 29 const std::string STRESS_TEST_CES_KEY {"CES"}; 30 31 struct StressTestLevel { 32 int32_t AMSLevel {1}; 33 int32_t BMSLevel {1}; 34 int32_t CESLevel {1}; 35 }; 36 37 class TestConfigParser { 38 public: 39 /** 40 * Loads the fuzz test configuration file. 41 * 42 * @param path the path of configuration file 43 * @param stlevel test frequency 44 */ ParseFromFile4StressTest(const std::string & path,StressTestLevel & stlevel)45 void ParseFromFile4StressTest(const std::string &path, StressTestLevel &stlevel) 46 { 47 std::ifstream jf(path); 48 if (!jf.is_open()) { 49 std::cout << "json file can not open!" << std::endl; 50 return; 51 } 52 53 nlohmann::json jsonObj; 54 jf >> jsonObj; 55 if (jsonObj.is_null() || !jsonObj.is_object()) { 56 std::cout << "Invalid JSON object" << std::endl; 57 return; 58 } 59 const auto &jsonObjEnd = jsonObj.end(); 60 if (jsonObj.find(STRESS_TEST_AMS_KEY) != jsonObjEnd) { 61 jsonObj.at(STRESS_TEST_AMS_KEY).get_to(stlevel.AMSLevel); 62 if (stlevel.AMSLevel == 0) { 63 stlevel.AMSLevel = 1; 64 } 65 } 66 67 if (jsonObj.find(STRESS_TEST_BMS_KEY) != jsonObjEnd) { 68 jsonObj.at(STRESS_TEST_BMS_KEY).get_to(stlevel.BMSLevel); 69 if (stlevel.BMSLevel == 0) { 70 stlevel.BMSLevel = 1; 71 } 72 } 73 74 if (jsonObj.find(STRESS_TEST_CES_KEY) != jsonObjEnd) { 75 jsonObj.at(STRESS_TEST_CES_KEY).get_to(stlevel.CESLevel); 76 if (stlevel.CESLevel == 0) { 77 stlevel.CESLevel = 1; 78 } 79 } 80 } 81 }; 82 } // namespace OHOS 83 84 #endif // TEST_CONFIG_PARSER_H 85