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 56 const auto &jsonObjEnd = jsonObj.end(); 57 if (jsonObj.find(STRESS_TEST_AMS_KEY) != jsonObjEnd) { 58 jsonObj.at(STRESS_TEST_AMS_KEY).get_to(stlevel.AMSLevel); 59 if (stlevel.AMSLevel == 0) { 60 stlevel.AMSLevel = 1; 61 } 62 } 63 64 if (jsonObj.find(STRESS_TEST_BMS_KEY) != jsonObjEnd) { 65 jsonObj.at(STRESS_TEST_BMS_KEY).get_to(stlevel.BMSLevel); 66 if (stlevel.BMSLevel == 0) { 67 stlevel.BMSLevel = 1; 68 } 69 } 70 71 if (jsonObj.find(STRESS_TEST_CES_KEY) != jsonObjEnd) { 72 jsonObj.at(STRESS_TEST_CES_KEY).get_to(stlevel.CESLevel); 73 if (stlevel.CESLevel == 0) { 74 stlevel.CESLevel = 1; 75 } 76 } 77 } 78 }; 79 } // namespace OHOS 80 81 #endif // TEST_CONFIG_PARSER_H 82