• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
37     /**
38      * Loads interface list from the configuration file.
39      *
40      * @param path the path of configuration file
41      * @param ftd record the configuration file
42      */
ParseFromFile4FuzzTest(const std::string & path,FuzzTestData & ftd)43     void ParseFromFile4FuzzTest(const std::string &path, FuzzTestData &ftd)
44     {
45         std::cout << __func__ << std::endl;
46         if (path.empty()) {
47             std::cout << __FUNCTION__ << " invalid file path, check!" << std::endl;
48             return;
49         }
50 
51         nlohmann::json jsonObj;
52         std::ifstream(path) >> jsonObj;
53         std::cout << path;
54         for (auto it = jsonObj.begin(); it != jsonObj.end(); ++it) {
55             if (!it.key().compare(FUZZ_TEST_MAIN_LOOP_KEY)) {
56                 ftd.mainLoopFlag = it.value();
57                 continue;
58             }
59 
60             auto className = it.key();
61             if (it->is_structured()) {
62                 for (auto itm = it->begin(); itm != it->end(); ++itm) {
63                     auto methodName = itm.key();
64 
65                     if (!(it->is_structured() && (it->size() != 0))) {
66                         ftd.methodVec.push_back(className + methodName);
67                         continue;
68                     }
69 
70                     std::string param {};
71                     for (auto itp = itm->begin(); itp != itm->end(); ++itp) {
72                         auto tp = itp.value();
73                         param += tp;
74                     }
75                     ftd.methodVec.push_back(className + methodName + param);
76                 }
77             }
78         }
79     }
80 };
81 }  // namespace OHOS
82 
83 #endif  // FUZZ_CONFIG_PARSER_H
84