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