• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 
16 #include "smart_parser_module_test.h"
17 
18 #include <cstdio>
19 #include <ctime>
20 #include <errors.h>
21 #include <map>
22 #include <string>
23 
24 #include "compose_rule.h"
25 #include "extract_rule.h"
26 #include "feature_analysis.h"
27 #include "file_util.h"
28 #include "log_util.h"
29 #include "smart_parser.h"
30 #include "string_util.h"
31 
32 using namespace std;
33 namespace OHOS {
34 namespace HiviewDFX {
35 using namespace testing::ext;
36 static const std::string TEST_CONFIG = "/data/test/test_data/SmartParser/common/";
37 static const std::string TEST_COMPOSE_CONFIG = "test_compose_rule.json";
38 static const std::string TEST_EXTRACT_CONFIG = "test_extract_rule.json";
39 
SetUpTestCase(void)40 void SmartParserModuleTest::SetUpTestCase(void) {}
41 
TearDownTestCase(void)42 void SmartParserModuleTest::TearDownTestCase(void) {}
43 
SetUp(void)44 void SmartParserModuleTest::SetUp(void) {}
45 
TearDown(void)46 void SmartParserModuleTest::TearDown(void) {}
47 
48 /**
49  * @tc.name: SmartParserTest001
50  * @tc.desc: process cpp_crash fault, this case match compose_rule.json and extract_rule.json.
51  * @tc.type: FUNC
52  * @tc.require:
53  * @tc.author: liuwei
54  */
55 HWTEST_F(SmartParserModuleTest, SmartParserTest001, TestSize.Level1)
56 {
57     /**
58      * @tc.steps: step1. Set taskSheet fault log path and eventid.
59      */
60     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  +
61                             "/SmartParserTest001/cppcrash-com.ohos.launcher-20010025-19700324235211000.log";
62     std::string trustStack = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest001/trace.txt";
63 
64     ASSERT_EQ(FileUtil::FileExists(faultFile), true);
65     ASSERT_EQ(FileUtil::FileExists(trustStack), true);
66 
67     /**
68      * @tc.steps: step2. smart parser process fault log
69      */
70     auto eventInfos = SmartParser::Analysis(faultFile, TEST_CONFIG, "CPP_CRASH");
71 
72     /**
73      * @tc.steps: step3. check the result of eventinfo for fault.
74      * @tc.expected: step3. equal to correct answer.
75      */
76     EXPECT_STREQ(eventInfos["PNAME"].c_str(), "com.ohos.launcher");
77     EXPECT_EQ(eventInfos["END_STACK"].size() > 0, true);
78     std::stringstream buff;
79     LogUtil::ReadFileBuff(trustStack, buff);
80     std::vector<std::string> trace;
81     StringUtil::SplitStr(eventInfos["END_STACK"], LogUtil::SPLIT_PATTERN, trace, false, false);
82     std::string line;
83     size_t num = 0;
84     while (getline(buff, line) && num < trace.size()) {
85         EXPECT_STREQ(line.c_str(), trace[num++].c_str());
86     }
87 }
88 
89 /**
90  * @tc.name: SmartParserTest002
91  * @tc.desc: process JS_ERROR fault, this case match compose_rule.json and extract_rule.json.
92  * @tc.type: FUNC
93  * @tc.require:
94  * @tc.author: liuwei
95  */
96 HWTEST_F(SmartParserModuleTest, SmartParserTest002, TestSize.Level1)
97 {
98     /**
99      * @tc.steps: step1. Set taskSheet fault log path and eventid.
100      */
101     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  +
102                             "/SmartParserTest002/jscrash-com.example.jsinject-20010041-19700424183123000.log";
103     std::string trustStack = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest002/trace.txt";
104     ASSERT_EQ(FileUtil::FileExists(faultFile), true);
105     ASSERT_EQ(FileUtil::FileExists(trustStack), true);
106 
107     /**
108      * @tc.steps: step2. smart parser process fault log
109      */
110     auto eventInfos = SmartParser::Analysis(faultFile, TEST_CONFIG, "JS_ERROR");
111 
112     /**
113      * @tc.steps: step3. check the result of eventinfo for fault.
114      * @tc.expected: step3. equal to correct answer.
115      */
116     EXPECT_STREQ(eventInfos["PNAME"].c_str(), "com.example.jsinject");
117     EXPECT_EQ(eventInfos["END_STACK"].size() > 0, true);
118     std::stringstream buff;
119     LogUtil::ReadFileBuff(trustStack, buff);
120     std::vector<std::string> trace;
121     StringUtil::SplitStr(eventInfos["END_STACK"], LogUtil::SPLIT_PATTERN, trace, false, false);
122     std::string line;
123     size_t num = 0;
124     while (getline(buff, line) && num < trace.size()) {
125         EXPECT_STREQ(line.c_str(), trace[num++].c_str());
126     }
127 }
128 
129 /**
130  * @tc.name: SmartParserTest003
131  * @tc.desc: process freeze fault, this case match compose_rule.json and extract_rule.json.
132  * @tc.type: FUNC
133  * @tc.require:
134  * @tc.author: liuwei
135  */
136 HWTEST_F(SmartParserModuleTest, SmartParserTest003, TestSize.Level1)
137 {
138     /**
139      * @tc.steps: step1. Set taskSheet fault log path and eventid.
140      */
141     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  +
142                             "/SmartParserTest003/appfreeze-com.example.jsinject-20010039-19700326211815000.log";
143     std::string traceFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest003/trace.txt";
144     ASSERT_EQ(FileUtil::FileExists(faultFile), true);
145     ASSERT_EQ(FileUtil::FileExists(traceFile), true);
146 
147     /**
148      * @tc.steps: step2. smart parser process crash fault log
149      */
150     auto eventInfos = SmartParser::Analysis(faultFile, TEST_CONFIG, "APP_FREEZE");
151 
152     /**
153      * @tc.steps: step3. check the result of eventinfo for fault.
154      * @tc.expected: step3. equal to correct answer.
155      */
156     EXPECT_EQ(eventInfos["END_STACK"].size() > 0, true);
157     std::string content;
158     bool isSuccess = FileUtil::LoadStringFromFile(traceFile, content);
159     if (!isSuccess) {
160         ASSERT_FALSE(isSuccess);
161         printf("read logFile: %s failed", traceFile.c_str());
162     } else {
163         std::stringstream buff(content);
164         std::vector<std::string> trace;
165         StringUtil::SplitStr(eventInfos["END_STACK"], LogUtil::SPLIT_PATTERN, trace, false, false);
166         std::string line;
167         size_t num = 0;
168         while (getline(buff, line) && num < trace.size()) {
169             EXPECT_STREQ(trace[num++].c_str(), line.c_str());
170         }
171     }
172 }
173 
174 /**
175  * @tc.name: SmartParserTest004
176  * @tc.desc: process PANIC fault, this case match compose_rule.json and extract_rule.json.
177  *           1. fault log should can be read;
178  *           2. compose_rule.json and extract_rule.json. should match the json file in perforce.
179  * @tc.type: FUNC
180  * @tc.require:
181  * @tc.author: liuwei
182  */
183 HWTEST_F(SmartParserModuleTest, SmartParserTest004, TestSize.Level1)
184 {
185     /**
186      * @tc.steps: step1. Set taskSheet fault log path and eventid.
187      */
188     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest004/last_kmsg";
189     std::string traceFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest004/trace.txt";
190     ASSERT_EQ(FileUtil::FileExists(faultFile), true);
191     ASSERT_EQ(FileUtil::FileExists(traceFile), true);
192     std::stringstream buff;
193     LogUtil::ReadFileBuff(traceFile, buff);
194 
195     /**
196      * @tc.steps: step2. smart parser process crash fault log
197      */
198     auto eventInfos = SmartParser::Analysis(faultFile, TEST_CONFIG, "PANIC");
199 
200     std::vector<std::string> trace;
201     StringUtil::SplitStr(eventInfos["END_STACK"], LogUtil::SPLIT_PATTERN, trace, false, false);
202     std::string line;
203     size_t num = 0;
204     while (getline(buff, line) && num < trace.size()) {
205         EXPECT_STREQ(trace[num++].c_str(), line.c_str());
206     }
207 }
208 
209 /**
210  * @tc.name: SmartParserTest005
211  * @tc.desc: process HWWATCHDOG fault, this case match compose_rule.json and extract_rule.json.
212  *           1. fault log should can be read;
213  *           2. compose_rule.json and extract_rule.json. should match the json file in perforce.
214  * @tc.type: FUNC
215  * @tc.require:
216  * @tc.author: liuwei
217  */
218 HWTEST_F(SmartParserModuleTest, SmartParserTest005, TestSize.Level1)
219 {
220     /**
221      * @tc.steps: step1. Set taskSheet fault log path and eventid.
222      */
223     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest005/last_kmsg";
224     std::string traceFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest005/trace.txt";
225     ASSERT_EQ(FileUtil::FileExists(faultFile), true);
226     ASSERT_EQ(FileUtil::FileExists(traceFile), true);
227     std::stringstream buff;
228     LogUtil::ReadFileBuff(traceFile, buff);
229 
230     /**
231      * @tc.steps: step2. smart parser process crash fault log
232      */
233     auto eventInfos = SmartParser::Analysis(faultFile, TEST_CONFIG, "HWWATCHDOG");
234 
235     std::vector<std::string> trace;
236     StringUtil::SplitStr(eventInfos["END_STACK"], LogUtil::SPLIT_PATTERN, trace, false, false);
237     std::string line;
238     size_t num = 0;
239     while (getline(buff, line) && num < trace.size()) {
240         EXPECT_STREQ(trace[num++].c_str(), line.c_str());
241     }
242 }
243 
244 /**
245  * @tc.name: SmartParserTest006
246  * @tc.desc: process HWWATCHDOG fault, this case match compose_rule.json and extract_rule.json.
247  *           1. fault log should can be read;
248  *           2. compose_rule.json and extract_rule.json. should match the json file in perforce.
249  * @tc.type: FUNC
250  * @tc.require:
251  * @tc.author: liuwei
252  */
253 HWTEST_F(SmartParserModuleTest, SmartParserTest006, TestSize.Level1)
254 {
255     /**
256      * @tc.steps: step1. Set taskSheet fault log path and eventid.
257      */
258     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest005/last_kmsg-1";
259     ASSERT_EQ(FileUtil::FileExists(faultFile), false);
260 
261     /**
262      * @tc.steps: step2. smart parser process crash fault log
263      */
264     auto eventInfos = SmartParser::Analysis(faultFile, TEST_CONFIG, "HWWATCHDOG");
265     ASSERT_EQ(eventInfos.empty(), true);
266 }
267 
268 /**
269  * @tc.name: SmartParserTest007
270  * @tc.desc: process test fault, this case match compose_rule.json and extract_rule.json.
271  *           1. fault log should can be read;
272  *           2. compose_rule.json and extract_rule.json. should match the json file in perforce.
273  * @tc.type: FUNC
274  * @tc.require:
275  * @tc.author: liuwei
276  */
277 HWTEST_F(SmartParserModuleTest, SmartParserTest007, TestSize.Level1)
278 {
279     /**
280      * @tc.steps: step1. Set taskSheet fault log path and eventid.
281      */
282     auto eventInfos = SmartParser::Analysis("", "/system/etc/hiview/reliability", "TEST");
283     ASSERT_EQ(eventInfos.empty(), true);
284     eventInfos = SmartParser::Analysis("", "", "TEST");
285     ASSERT_EQ(eventInfos.empty(), true);
286     eventInfos = SmartParser::Analysis("", "", "");
287     ASSERT_EQ(eventInfos.empty(), true);
288     eventInfos = SmartParser::Analysis("test", "test", "test");
289     ASSERT_EQ(eventInfos.empty(), true);
290 }
291 
292 /**
293  * @tc.name: SmartParserTest008
294  * @tc.desc: process RUST_PANIC fault, this case match compose_rule.json and extract_rule.json.
295  *           1. fault log should can be read;
296  *           2. compose_rule.json and extract_rule.json. should match the json file in perforce.
297  * @tc.type: FUNC
298  * @tc.require:
299  * @tc.author: liuwei
300  */
301 HWTEST_F(SmartParserModuleTest, SmartParserTest008, TestSize.Level1)
302 {
303     /**
304      * @tc.steps: step1. Set taskSheet fault log path and eventid.
305      */
306     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  +
307         "/SmartParserTest006/rustpanic-rustpanic_maker-0-20230419222113000.log";
308     std::string traceFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest006/trace.txt";
309     ASSERT_EQ(FileUtil::FileExists(faultFile), true);
310     ASSERT_EQ(FileUtil::FileExists(traceFile), true);
311     std::stringstream buff;
312     LogUtil::ReadFileBuff(traceFile, buff);
313 
314     /**
315      * @tc.steps: step2. smart parser process crash fault log
316      */
317     auto eventInfos = SmartParser::Analysis(faultFile, TEST_CONFIG, "RUST_PANIC");
318     ASSERT_EQ(!eventInfos.empty(), true);
319 
320     std::vector<std::string> trace;
321     StringUtil::SplitStr(eventInfos["END_STACK"], LogUtil::SPLIT_PATTERN, trace, false, false);
322     std::string line;
323     size_t num = 0;
324     while (getline(buff, line) && num < trace.size()) {
325         EXPECT_STREQ(trace[num++].c_str(), line.c_str());
326     }
327 }
328 
329 /**
330  * @tc.name: SmartParserTest009
331  * @tc.desc: process PANIC fault, this case match test_compose_rule.json and test_extract_rule.json.
332  *           1. fault log should can be read;
333  *           2. test_compose_rule.json and test_extract_rule.json. should match the json file in perforce.
334  * @tc.type: FUNC
335  * @tc.require:
336  * @tc.author: liuwei
337  */
338 HWTEST_F(SmartParserModuleTest, SmartParserTest009, TestSize.Level1)
339 {
340     /**
341      * @tc.steps: step1. Set taskSheet fault log path and eventid.
342      */
343     std::map<std::string, FeatureSet> extract;
344     std::list<std::pair<std::string, std::map<std::string, std::string>>> compose;
345     std::map<std::string, std::vector<std::string>> segStatusCfg;
346     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest004/last_kmsg";
347     std::string traceFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest004/trace.txt";
348 
349     ASSERT_EQ(FileUtil::FileExists(faultFile), true);
350     ASSERT_EQ(FileUtil::FileExists(traceFile), true);
351     std::stringstream buff;
352     LogUtil::ReadFileBuff(traceFile, buff);
353 
354     /**
355      * @tc.steps: step2. smart parser process crash fault log
356      */
357     ExtractRule extractRule;
358     ComposeRule composeRule;
359     std::string extractConfig = TEST_CONFIG + TEST_EXTRACT_CONFIG;
360     extractRule.ParseExtractRule("PANIC", extractConfig, faultFile);
361     extract = extractRule.GetExtractRule();
362     segStatusCfg = extractRule.GetSegStatusCfg();
363     std::string composeConfig = TEST_CONFIG + TEST_COMPOSE_CONFIG;
364     composeRule.ParseComposeRule(composeConfig, "PANIC", extractRule.GetFeatureId());
365     compose = composeRule.GetComposeRule();
366 
367     ASSERT_EQ(!extract.empty(), true);
368     ASSERT_EQ(!segStatusCfg.empty(), true);
369     ASSERT_EQ(!compose.empty(), true);
370 }
371 
372 /**
373  * @tc.name: SmartParserTest010
374  * @tc.desc: process RUST_PANIC fault, this case match test_compose_rule.json and test_extract_rule.json.
375  *           1. fault log should can be read;
376  *           2. test_compose_rule.json and test_extract_rule.json. should match the json file in perforce.
377  * @tc.type: FUNC
378  * @tc.require:
379  * @tc.author: liuwei
380  */
381 HWTEST_F(SmartParserModuleTest, SmartParserTest010, TestSize.Level1)
382 {
383     /**
384      * @tc.steps: step1. Set taskSheet fault log path and eventid.
385      */
386     std::map<std::string, FeatureSet> extract;
387     std::list<std::pair<std::string, std::map<std::string, std::string>>> compose;
388     std::map<std::string, std::vector<std::string>> segStatusCfg;
389     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  +
390         "/SmartParserTest006/rustpanic-rustpanic_maker-0-20230419222113000.log";
391     std::string traceFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest006/trace.txt";
392 
393     ASSERT_EQ(FileUtil::FileExists(faultFile), true);
394     ASSERT_EQ(FileUtil::FileExists(traceFile), true);
395     std::stringstream buff;
396     LogUtil::ReadFileBuff(traceFile, buff);
397 
398     /**
399      * @tc.steps: step2. smart parser process crash fault log
400      */
401     ExtractRule extractRule;
402     ComposeRule composeRule;
403     std::string extractConfig = TEST_CONFIG + "/" + TEST_EXTRACT_CONFIG;
404     extractRule.ParseExtractRule("RUST_PANIC", extractConfig, faultFile);
405     extract = extractRule.GetExtractRule();
406     segStatusCfg = extractRule.GetSegStatusCfg();
407     std::string composeConfig = TEST_CONFIG + "/" + TEST_COMPOSE_CONFIG;
408     composeRule.ParseComposeRule(composeConfig, "RUST_PANIC", extractRule.GetFeatureId());
409     compose = composeRule.GetComposeRule();
410 
411     ASSERT_EQ(!extract.empty(), true);
412     ASSERT_EQ(!segStatusCfg.empty(), true);
413     ASSERT_EQ(!compose.empty(), true);
414 }
415 
416 /**
417  * @tc.name: SmartParserTest011
418  * @tc.desc: process RUST_PANIC fault, this case match test_compose_rule.json and test_extract_rule.json.
419  *           1. fault log should can be read;
420  *           2. test_compose_rule.json and test_extract_rule.json. should match the json file in perforce.
421  * @tc.type: FUNC
422  * @tc.require:
423  * @tc.author: liuwei
424  */
425 HWTEST_F(SmartParserModuleTest, SmartParserTest011, TestSize.Level1)
426 {
427     /**
428      * @tc.steps: step1. Set taskSheet fault log path and eventid.
429      */
430     std::map<std::string, FeatureSet> extract;
431     std::list<std::pair<std::string, std::map<std::string, std::string>>> compose;
432     std::map<std::string, std::vector<std::string>> segStatusCfg;
433     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  +
434         "/SmartParserTest007/rustpanic-rustpanic_maker-0-20230419222113000.log";
435     std::string traceFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest007/trace.txt";
436 
437     ASSERT_EQ(FileUtil::FileExists(faultFile), true);
438     ASSERT_EQ(FileUtil::FileExists(traceFile), true);
439     std::stringstream buff;
440     LogUtil::ReadFileBuff(traceFile, buff);
441 
442     /**
443      * @tc.steps: step2. smart parser process crash fault log
444      */
445     ExtractRule extractRule;
446     ComposeRule composeRule;
447     std::string extractConfig = "/data/test/test_data/SmartParser/SmartParserTest007/" + TEST_EXTRACT_CONFIG;
448     extractRule.ParseExtractRule("RUST_PANIC", extractConfig, faultFile);
449     extract = extractRule.GetExtractRule();
450     segStatusCfg = extractRule.GetSegStatusCfg();
451     std::string composeConfig = "/data/test/test_data/SmartParser/SmartParserTest007/" + TEST_COMPOSE_CONFIG;
452     composeRule.ParseComposeRule(composeConfig, "RUST_PANIC", extractRule.GetFeatureId());
453     compose = composeRule.GetComposeRule();
454 
455     std::map<std::string, std::string> eventInfoMap;
456     for (const auto& composeRules : compose) {
457         FeatureAnalysis feature(extract[composeRules.first], composeRules.second, "RUST_PANIC");
458         if (feature.AnalysisLog()) {
459             auto result = feature.GetReasult();
460             for (const auto& one : result) {
461                 eventInfoMap.emplace(one.first, one.second);
462             }
463         }
464     }
465 }
466 
467 /**
468  * @tc.name: SmartParserTest012
469  * @tc.desc: process PANIC fault, this case match test_compose_rule.json and test_extract_rule.json.
470  *           1. fault log should can be read;
471  *           2. test_compose_rule.json and test_extract_rule.json. should match the json file in perforce.
472  * @tc.type: FUNC
473  * @tc.require:
474  * @tc.author: liuwei
475  */
476 HWTEST_F(SmartParserModuleTest, SmartParserTest012, TestSize.Level1)
477 {
478     /**
479      * @tc.steps: step1. Set taskSheet fault log path and eventid.
480      */
481     std::map<std::string, FeatureSet> extract;
482     std::list<std::pair<std::string, std::map<std::string, std::string>>> compose;
483     std::map<std::string, std::vector<std::string>> segStatusCfg;
484     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest008/last_kmsg";
485     std::string traceFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest008/trace.txt";
486     ASSERT_EQ(FileUtil::FileExists(faultFile), true);
487     ASSERT_EQ(FileUtil::FileExists(traceFile), true);
488     std::stringstream buff;
489     LogUtil::ReadFileBuff(traceFile, buff);
490 
491     /**
492      * @tc.steps: step2. smart parser process crash fault log
493      */
494     ExtractRule extractRule;
495     ComposeRule composeRule;
496     std::string extractConfig = "/data/test/test_data/SmartParser/SmartParserTest008/" + TEST_EXTRACT_CONFIG;
497     extractRule.ParseExtractRule("PANIC", extractConfig, faultFile);
498     extract = extractRule.GetExtractRule();
499     segStatusCfg = extractRule.GetSegStatusCfg();
500     std::string composeConfig = "/data/test/test_data/SmartParser/SmartParserTest008/" + TEST_COMPOSE_CONFIG;
501     composeRule.ParseComposeRule(composeConfig, "PANIC", extractRule.GetFeatureId());
502     compose = composeRule.GetComposeRule();
503 
504     std::map<std::string, std::string> eventInfoMap;
505     for (const auto& composeRules : compose) {
506         FeatureAnalysis feature(extract[composeRules.first], composeRules.second, "PANIC");
507         if (feature.AnalysisLog()) {
508             auto result = feature.GetReasult();
509             for (const auto& one : result) {
510                 eventInfoMap.emplace(one.first, one.second);
511             }
512         }
513     }
514 }
515 
516 /**
517  * @tc.name: SmartParserTest013
518  * @tc.desc: process RUST_PANIC fault, this case match test_compose_rule.json and test_extract_rule.json.
519  *           1. fault log should can be read;
520  *           2. test_compose_rule.json and test_extract_rule.json. should match the json file in perforce.
521  * @tc.type: FUNC
522  * @tc.require:
523  * @tc.author: liuwei
524  */
525 HWTEST_F(SmartParserModuleTest, SmartParserTest013, TestSize.Level1)
526 {
527     /**
528      * @tc.steps: step1. Set taskSheet fault log path and eventid.
529      */
530     std::map<std::string, FeatureSet> extract;
531     std::list<std::pair<std::string, std::map<std::string, std::string>>> compose;
532     std::map<std::string, std::vector<std::string>> segStatusCfg;
533     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  +
534         "/SmartParserTest009/rustpanic-rustpanic_maker-0-20230419222113000.log";
535     std::string traceFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest009/trace.txt";
536     ASSERT_EQ(FileUtil::FileExists(faultFile), true);
537     ASSERT_EQ(FileUtil::FileExists(traceFile), true);
538     std::stringstream buff;
539     LogUtil::ReadFileBuff(traceFile, buff);
540 
541     /**
542      * @tc.steps: step2. smart parser process crash fault log
543      */
544     ExtractRule extractRule;
545     ComposeRule composeRule;
546     std::string extractConfig = "/data/test/test_data/SmartParser/SmartParserTest009/" + TEST_EXTRACT_CONFIG;
547     extractRule.ParseExtractRule("RUST_PANIC", extractConfig, faultFile);
548     extract = extractRule.GetExtractRule();
549     segStatusCfg = extractRule.GetSegStatusCfg();
550     std::string composeConfig = "/data/test/test_data/SmartParser/SmartParserTest009/" + TEST_COMPOSE_CONFIG;
551     composeRule.ParseComposeRule(composeConfig, "RUST_PANIC", extractRule.GetFeatureId());
552     compose = composeRule.GetComposeRule();
553 
554     std::map<std::string, std::string> eventInfoMap;
555     for (const auto& composeRules : compose) {
556         FeatureAnalysis feature(extract[composeRules.first], composeRules.second, "RUST_PANIC");
557         if (feature.AnalysisLog()) {
558             auto result = feature.GetReasult();
559             for (const auto& one : result) {
560                 eventInfoMap.emplace(one.first, one.second);
561             }
562         }
563     }
564 }
565 
566 /**
567  * @tc.name: SmartParserTest014
568  * @tc.desc: process APP_FREEZE fault, this case match test_compose_rule.json and test_extract_rule.json.
569  *           1. fault log should can be read;
570  *           2. test_compose_rule.json and test_extract_rule.json. should match the json file in perforce.
571  * @tc.type: FUNC
572  * @tc.require:
573  * @tc.author: liuwei
574  */
575 HWTEST_F(SmartParserModuleTest, SmartParserTest014, TestSize.Level1)
576 {
577     /**
578      * @tc.steps: step1. Set taskSheet fault log path and eventid.
579      */
580     std::map<std::string, FeatureSet> extract;
581     std::list<std::pair<std::string, std::map<std::string, std::string>>> compose;
582     std::map<std::string, std::vector<std::string>> segStatusCfg;
583     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  +
584         "/SmartParserTest010/appfreeze-com.example.jsinject-20010039-19700326211815000.log";
585     std::string traceFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest010/trace.txt";
586     ASSERT_EQ(FileUtil::FileExists(faultFile), true);
587     ASSERT_EQ(FileUtil::FileExists(traceFile), true);
588     std::stringstream buff;
589     LogUtil::ReadFileBuff(traceFile, buff);
590 
591     /**
592      * @tc.steps: step2. smart parser process crash fault log
593      */
594     ExtractRule extractRule;
595     ComposeRule composeRule;
596     std::string extractConfig = "/data/test/test_data/SmartParser/SmartParserTest010/" + TEST_EXTRACT_CONFIG;
597     extractRule.ParseExtractRule("APP_FREEZE", extractConfig, faultFile);
598     extract = extractRule.GetExtractRule();
599     segStatusCfg = extractRule.GetSegStatusCfg();
600     std::string composeConfig = "/data/test/test_data/SmartParser/SmartParserTest010/" + TEST_COMPOSE_CONFIG;
601     composeRule.ParseComposeRule(composeConfig, "APP_FREEZE", extractRule.GetFeatureId());
602     compose = composeRule.GetComposeRule();
603 
604     std::map<std::string, std::string> eventInfoMap;
605     for (const auto& composeRules : compose) {
606         FeatureAnalysis feature(extract[composeRules.first], composeRules.second, "APP_FREEZE");
607         if (feature.AnalysisLog()) {
608             auto result = feature.GetReasult();
609             for (const auto& one : result) {
610                 eventInfoMap.emplace(one.first, one.second);
611             }
612         }
613     }
614 }
615 
616 /**
617  * @tc.name: SmartParserTest015
618  * @tc.desc: process APP_FREEZE fault, this case match test_compose_rule.json and test_extract_rule.json.
619  *           1. fault log should can be read;
620  *           2. test_compose_rule.json and test_extract_rule.json. should match the json file in perforce.
621  * @tc.type: FUNC
622  * @tc.require:
623  * @tc.author: liuwei
624  */
625 HWTEST_F(SmartParserModuleTest, SmartParserTest015, TestSize.Level1)
626 {
627     /**
628      * @tc.steps: step1. Set taskSheet fault log path and eventid.
629      */
630     std::map<std::string, FeatureSet> extract;
631     std::list<std::pair<std::string, std::map<std::string, std::string>>> compose;
632     std::map<std::string, std::vector<std::string>> segStatusCfg;
633     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  +
634         "/SmartParserTest011/appfreeze-com.example.jsinject-20010039-19700326211815000.log";
635     std::string traceFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest011/trace.txt";
636     ASSERT_EQ(FileUtil::FileExists(faultFile), true);
637     ASSERT_EQ(FileUtil::FileExists(traceFile), true);
638 
639     /**
640      * @tc.steps: step2. smart parser process crash fault log
641      */
642     ExtractRule extractRule;
643     ComposeRule composeRule;
644     std::string extractConfig = "/data/test/test_data/SmartParser/SmartParserTest011/" + TEST_EXTRACT_CONFIG;
645     extractRule.ParseExtractRule("APP_FREEZE", extractConfig, faultFile);
646     extract = extractRule.GetExtractRule();
647     segStatusCfg = extractRule.GetSegStatusCfg();
648     std::string composeConfig = "/data/test/test_data/SmartParser/SmartParserTest011/" + TEST_COMPOSE_CONFIG;
649     composeRule.ParseComposeRule(composeConfig, "APP_FREEZE", extractRule.GetFeatureId());
650     compose = composeRule.GetComposeRule();
651 
652     std::map<std::string, std::string> eventInfoMap;
653     for (const auto &composeRules : compose)
654     {
655         FeatureAnalysis feature(extract[composeRules.first], composeRules.second, "APP_FREEZE");
656         if (feature.AnalysisLog())
657         {
658             auto result = feature.GetReasult();
659             for (const auto &one : result)
660             {
661                 eventInfoMap.emplace(one.first, one.second);
662             }
663             std::string writeLine(2049, 't');
664             stringstream buffer(writeLine);
665             feature.RawInfoPosition(buffer);
666 
667             stringstream bufferTwo(" test");
668             feature.RawInfoPosition(bufferTwo);
669 
670             stringstream bufferThree("\t");
671             feature.RawInfoPosition(bufferThree);
672 
673             bool segmentStart = true;
674             feature.CheckStartSegment(segmentStart);
675         }
676     }
677 }
678 
679 /**
680  * @tc.name: SmartParserTest016
681  * @tc.desc: process RUST_PANIC fault, this case match test_compose_rule.json and test_extract_rule.json.
682  *           1. fault log should can be read;
683  *           2. test_compose_rule.json and test_extract_rule.json. should match the json file in perforce.
684  * @tc.type: FUNC
685  * @tc.require:
686  * @tc.author: liuwei
687  */
688 HWTEST_F(SmartParserModuleTest, SmartParserTest016, TestSize.Level1)
689 {
690     /**
691      * @tc.steps: step1. Set taskSheet fault log path and eventid.
692      */
693     std::map<std::string, FeatureSet> extract;
694     std::list<std::pair<std::string, std::map<std::string, std::string>>> compose;
695     std::map<std::string, std::vector<std::string>> segStatusCfg;
696     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  +
697         "/SmartParserTest012/rustpanic-rustpanic_maker-0-20230419222113000.log";
698     std::string traceFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest012/trace.txt";
699 
700     ASSERT_EQ(FileUtil::FileExists(faultFile), true);
701     ASSERT_EQ(FileUtil::FileExists(traceFile), true);
702     std::stringstream buff;
703     LogUtil::ReadFileBuff(traceFile, buff);
704 
705     /**
706      * @tc.steps: step2. smart parser process crash fault log
707      */
708     ExtractRule extractRule;
709     ComposeRule composeRule;
710     std::string extractConfig = "/data/test/test_data/SmartParser/SmartParserTest012/" + TEST_EXTRACT_CONFIG;
711     extractRule.ParseExtractRule("RUST_PANIC", extractConfig, faultFile);
712     extract = extractRule.GetExtractRule();
713     segStatusCfg = extractRule.GetSegStatusCfg();
714     std::string composeConfig = "/data/test/test_data/SmartParser/SmartParserTest012/" + TEST_COMPOSE_CONFIG;
715     composeRule.ParseComposeRule(composeConfig, "RUST_PANIC", extractRule.GetFeatureId());
716     compose = composeRule.GetComposeRule();
717 
718     std::map<std::string, std::string> eventInfoMap;
719     for (const auto& composeRules : compose) {
720         FeatureAnalysis feature(extract[composeRules.first], composeRules.second, "RUST_PANIC");
721         if (feature.AnalysisLog()) {
722             auto result = feature.GetReasult();
723             for (const auto& one : result) {
724                 eventInfoMap.emplace(one.first, one.second);
725             }
726         }
727     }
728 }
729 
730 /**
731  * @tc.name: SmartParserTest017
732  * @tc.desc: process BOOTFAIL fault, this case match test_compose_rule.json and test_extract_rule.json.
733  *           1. fault log should can be read;
734  *           2. compose_rule.json and extract_rule.json. should match the json file in perforce.
735  * @tc.type: FUNC
736  * @tc.require:
737  * @tc.author: jincong
738  */
739 HWTEST_F(SmartParserModuleTest, SmartParserTest017, TestSize.Level1)
740 {
741     /**
742      * @tc.steps: step1. Set taskSheet fault log path and eventid.
743      */
744     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest013/bootfail_info_0";
745     std::string traceFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest013/trace.txt";
746     ASSERT_EQ(FileUtil::FileExists(faultFile), true);
747     ASSERT_EQ(FileUtil::FileExists(traceFile), true);
748     std::stringstream buff;
749     LogUtil::ReadFileBuff(traceFile, buff);
750 
751     /**
752      * @tc.steps: step2. smart parser process crash fault log
753      */
754     auto eventInfos = SmartParser::Analysis(faultFile, TEST_CONFIG, "BOOTFAIL");
755 
756     std::vector<std::string> trace;
757     StringUtil::SplitStr(eventInfos["END_STACK"], LogUtil::SPLIT_PATTERN, trace, false, false);
758     std::string line;
759     size_t num = 0;
760     while (getline(buff, line) && num < trace.size()) {
761         EXPECT_STREQ(trace[num++].c_str(), line.c_str());
762     }
763 }
764 
765 /**
766  * @tc.name: SmartParserTest018
767  * @tc.desc: process PANIC fault, this case match compose_rule.json and extract_rule.json.
768  *           1. fault log should can be read;
769  *           2. compose_rule.json and extract_rule.json. should match the json file in perforce.
770  * @tc.type: FUNC
771  * @tc.require:
772  * @tc.author: jincong
773  */
774 HWTEST_F(SmartParserModuleTest, SmartParserTest018, TestSize.Level1)
775 {
776     /**
777      * @tc.steps: step1. Set taskSheet fault log path and eventid.
778      */
779     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest014/last_kmsg";
780     std::string traceFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest014/trace.txt";
781     ASSERT_EQ(FileUtil::FileExists(faultFile), true);
782     ASSERT_EQ(FileUtil::FileExists(traceFile), true);
783     std::stringstream buff;
784     LogUtil::ReadFileBuff(traceFile, buff);
785 
786     /**
787      * @tc.steps: step2. smart parser process crash fault log
788      */
789     auto eventInfos = SmartParser::Analysis(faultFile, TEST_CONFIG, "PANIC");
790 
791     std::vector<std::string> trace;
792     StringUtil::SplitStr(eventInfos["END_STACK"], LogUtil::SPLIT_PATTERN, trace, false, false);
793     std::string line;
794     size_t num = 0;
795     while (getline(buff, line) && num < trace.size()) {
796         EXPECT_STREQ(trace[num++].c_str(), line.c_str());
797     }
798 }
799 
800 /**
801  * @tc.name: SmartParserTest019
802  * @tc.desc: process PANIC fault, this case match compose_rule.json and extract_rule.json.
803  *           1. fault log should can be read;
804  *           2. compose_rule.json and extract_rule.json. should match the json file in perforce.
805  * @tc.type: FUNC
806  * @tc.require:
807  * @tc.author: jincong
808  */
809 HWTEST_F(SmartParserModuleTest, SmartParserTest019, TestSize.Level1)
810 {
811     /**
812      * @tc.steps: step1. Set taskSheet fault log path and eventid.
813      */
814     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest015/last_kmsg";
815     std::string traceFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest015/trace.txt";
816     ASSERT_EQ(FileUtil::FileExists(faultFile), true);
817     ASSERT_EQ(FileUtil::FileExists(traceFile), true);
818     std::stringstream buff;
819     LogUtil::ReadFileBuff(traceFile, buff);
820 
821     /**
822      * @tc.steps: step2. smart parser process crash fault log
823      */
824     auto eventInfos = SmartParser::Analysis(faultFile, TEST_CONFIG, "PANIC");
825 
826     std::vector<std::string> trace;
827     StringUtil::SplitStr(eventInfos["END_STACK"], LogUtil::SPLIT_PATTERN, trace, false, false);
828     std::string line;
829     size_t num = 0;
830     while (getline(buff, line) && num < trace.size()) {
831         EXPECT_STREQ(trace[num++].c_str(), line.c_str());
832     }
833 }
834 
835 /**
836  * @tc.name: SmartParserTest020
837  * @tc.desc: process BOOTFAIL fault, this case match test_compose_rule.json and test_extract_rule.json.
838  *           1. fault log should can be read;
839  *           2. compose_rule.json and extract_rule.json. should match the json file in perforce.
840  * @tc.type: FUNC
841  * @tc.require:
842  * @tc.author: jincong
843  */
844 HWTEST_F(SmartParserModuleTest, SmartParserTest020, TestSize.Level1)
845 {
846     /**
847      * @tc.steps: step1. Set taskSheet fault log path and eventid.
848      */
849     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest013/bootfail_info_x";
850     ASSERT_EQ(FileUtil::FileExists(faultFile), false);
851 
852     /**
853      * @tc.steps: step2. smart parser process crash fault log
854      */
855     auto eventInfos = SmartParser::Analysis(faultFile, TEST_CONFIG, "BOOTFAIL");
856     ASSERT_EQ(eventInfos.empty(), true);
857 }
858 
859 /**
860  * @tc.name: SmartParserTest021
861  * @tc.desc: process SENSORHUBCRASH fault, this case match test_compose_rule.json and test_extract_rule.json.
862  *           1. fault log should can be read;
863  *           2. compose_rule.json and extract_rule.json. should match the json file in perforce.
864  * @tc.type: FUNC
865  * @tc.require:
866  * @tc.author: jincong
867  */
868 HWTEST_F(SmartParserModuleTest, SmartParserTest021, TestSize.Level1)
869 {
870     /**
871      * @tc.steps: step1. Set taskSheet fault log path and eventid.
872      */
873     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest016/history.log";
874     ASSERT_EQ(FileUtil::FileExists(faultFile), true);
875 
876     /**
877      * @tc.steps: step2. smart parser process crash fault log
878      */
879     auto eventInfos = SmartParser::Analysis(faultFile, TEST_CONFIG, "SENSORHUBCRASH");
880     ASSERT_EQ(eventInfos.empty(), false);
881 }
882 
883 /**
884  * @tc.name: SmartParserTest022
885  * @tc.desc: process MODEMCRASH fault, this case match test_compose_rule.json and test_extract_rule.json.
886  *           1. fault log should can be read;
887  *           2. compose_rule.json and extract_rule.json. should match the json file in perforce.
888  * @tc.type: FUNC
889  * @tc.require:
890  * @tc.author: jincong
891  */
892 HWTEST_F(SmartParserModuleTest, SmartParserTest022, TestSize.Level1)
893 {
894     /**
895      * @tc.steps: step1. Set taskSheet fault log path and eventid.
896      */
897     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest017/reset.log";
898     ASSERT_EQ(FileUtil::FileExists(faultFile), true);
899 
900     /**
901      * @tc.steps: step2. smart parser process crash fault log
902      */
903     auto eventInfos = SmartParser::Analysis(faultFile, TEST_CONFIG, "MODEMCRASH");
904     ASSERT_EQ(eventInfos.empty(), false);
905 }
906 
907 /**
908  * @tc.name: SmartParserTest023
909  * @tc.desc: process PANIC fault, this case match test_compose_rule.json and test_extract_rule.json.
910  *           1. fault log should can be read;
911  *           2. compose_rule.json and extract_rule.json. should match the json file in perforce.
912  * @tc.type: FUNC
913  * @tc.require:
914  * @tc.author: jincong
915  */
916 HWTEST_F(SmartParserModuleTest, SmartParserTest023, TestSize.Level1)
917 {
918     /**
919      * @tc.steps: step1. Set taskSheet fault log path and eventid.
920      */
921     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest018/last_kmsg";
922     ASSERT_EQ(FileUtil::FileExists(faultFile), true);
923 
924     /**
925      * @tc.steps: step2. smart parser process crash fault log
926      */
927     auto eventInfos = SmartParser::Analysis(faultFile, TEST_CONFIG, "PANIC");
928     ASSERT_EQ(eventInfos.empty(), false);
929 }
930 
931 /**
932  * @tc.name: SmartParserTest024
933  * @tc.desc: process DPACRASH fault, this case match test_compose_rule.json and test_extract_rule.json.
934  *           1. fault log should can be read;
935  *           2. compose_rule.json and extract_rule.json. should match the json file in perforce.
936  * @tc.type: FUNC
937  * @tc.require:
938  * @tc.author: jincong
939  */
940 HWTEST_F(SmartParserModuleTest, SmartParserTest024, TestSize.Level1)
941 {
942     /**
943      * @tc.steps: step1. Set taskSheet fault log path and eventid.
944      */
945     std::string faultFile = std::string{LogUtil::SMART_PARSER_TEST_DIR}  + "/SmartParserTest017/reset.log";
946     ASSERT_EQ(FileUtil::FileExists(faultFile), true);
947 
948     /**
949      * @tc.steps: step2. smart parser process crash fault log
950      */
951     auto eventInfos = SmartParser::Analysis(faultFile, TEST_CONFIG, "DPACRASH");
952     ASSERT_EQ(eventInfos.empty(), false);
953 }
954 }  // namespace HiviewDFX
955 }  // namespace OHOS
956