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