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