1 /*
2 * Copyright (c) 2025 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 <gtest/gtest.h>
17
18 #include <fstream>
19 #include <iostream>
20 #include <string>
21 #include <unistd.h>
22
23 #include "dfx_test_util.h"
24 #include "kernel_snapshot_content_builder.h"
25 #include "kernel_snapshot_parser.h"
26 #include "kernel_snapshot_printer.h"
27 #include "kernel_snapshot_processor_impl.h"
28 #include "kernel_snapshot_reporter.h"
29 #include "kernel_snapshot_trie.h"
30
31 using namespace testing::ext;
32 using namespace std;
33
34 namespace OHOS {
35 namespace HiviewDFX {
36 namespace {
37 const char * const KERNEL_SNAPSHOT_EXECPTION_FILE = "/data/test/resource/testdata/kernel_snapshot_execption.txt";
38 const char * const KERNEL_SNAPSHOT_2_EXECPTION_FILE = "/data/test/resource/testdata/kernel_snapshot_2_execption.txt";
39 const char * const KERNEL_SNAPSHOT_ABORT_FILE = "/data/test/resource/testdata/kernel_snapshot_abort.txt";
40 const char * const STOP_FAULTLOGGERD = "service_control stop faultloggerd";
41 const char * const START_FAULTLOGGERD = "service_control start faultloggerd";
42 }
43
44 class KernelSnapshotTest : public testing::Test {
45 public:
46 static void SetUpTestCase();
47 static void TearDownTestCase();
48 void SetUp();
49 void TearDown();
50 };
51
SetUpTestCase()52 void KernelSnapshotTest::SetUpTestCase()
53 {
54 if (ExecuteCommands("uname").find("Linux") != std::string::npos) {
55 return;
56 }
57 system(STOP_FAULTLOGGERD);
58 system("param set kernel_snapshot_check_interval 3");
59 system(START_FAULTLOGGERD);
60 }
61
TearDownTestCase()62 void KernelSnapshotTest::TearDownTestCase()
63 {
64 if (ExecuteCommands("uname").find("Linux") != std::string::npos) {
65 return;
66 }
67 system(STOP_FAULTLOGGERD);
68 system("param set kernel_snapshot_check_interval 60");
69 system(START_FAULTLOGGERD);
70 }
71
SetUp()72 void KernelSnapshotTest::SetUp()
73 {}
74
TearDown()75 void KernelSnapshotTest::TearDown()
76 {}
77
78 namespace {
79 #if defined(__aarch64__)
BlockSignals()80 void BlockSignals()
81 {
82 sigset_t set;
83 sigemptyset(&set);
84 sigaddset(&set, SIGSEGV);
85 sigaddset(&set, SIGBUS);
86 sigaddset(&set, SIGFPE);
87 sigaddset(&set, SIGILL);
88 sigaddset(&set, SIGABRT);
89 sigprocmask(SIG_BLOCK, &set, nullptr);
90 }
91 #endif
92
93 std::string g_snapshotCont;
94
95 class TestKernelSnapshotProcessor : public IKernelSnapshotProcessor {
96 public:
Process(const std::string & snapshot)97 void Process(const std::string& snapshot) override
98 {
99 g_snapshotCont = snapshot;
100 GTEST_LOG_(INFO) << "update snapshot: " << snapshot;
101 }
102 };
103 } // namespace
104
105 /**
106 * @tc.name: KernelSnapshotTest002
107 * @tc.desc: test KernelSnapshotProcessorImpl
108 * @tc.type: FUNC
109 */
110 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest002, TestSize.Level0)
111 {
112 GTEST_LOG_(INFO) << "KernelSnapshotTest002: start.";
113 std::shared_ptr<IKernelSnapshotProcessor> processor = std::make_shared<KernelSnapshotProcessorImpl>();
114 std::ifstream file(KERNEL_SNAPSHOT_EXECPTION_FILE);
115 if (file.is_open()) {
116 std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
117 file.close();
118
119 processor->Process(content);
120 std::string filePath = "/data/log/faultlog/temp/cppcrash-799-1736169993223";
121 auto ret = access(filePath.c_str(), F_OK);
122 EXPECT_EQ(ret, 0);
123
124 std::string cmd = "rm -rf " + filePath;
125 system(cmd.c_str());
126 } else {
127 FAIL() << "Failed to open file: " << KERNEL_SNAPSHOT_EXECPTION_FILE;
128 }
129
130 GTEST_LOG_(INFO) << "KernelSnapshotTest002: end.";
131 }
132
133 #if defined(__aarch64__)
134 /**
135 * @tc.name: KernelSnapshotTest003
136 * @tc.desc: test snapshot generation
137 * @tc.type: FUNC
138 */
139 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest003, TestSize.Level2)
140 {
141 GTEST_LOG_(INFO) << "KernelSnapshotTest003: start.";
142
143 pid_t pid = fork();
144 if (pid == 0) {
145 BlockSignals();
146 int* p = (int*)55;
147 *p = 0;
148 }
149 waitpid(pid, 0, 0);
150 std::string snapshotFilePath = WaitCreateCrashFile("cppcrash", pid, 5);
151
152 std::ifstream snapshotFile(snapshotFilePath);
153 if (snapshotFile.is_open()) {
154 std::string content((std::istreambuf_iterator<char>(snapshotFile)), std::istreambuf_iterator<char>());
155 EXPECT_TRUE(content.find("Build info") != std::string::npos) << "KernelSnapshotTest003 Failed";
156 EXPECT_TRUE(content.find("Timestamp") != std::string::npos) << "KernelSnapshotTest003 Failed";
157 EXPECT_TRUE(content.find("Pid") != std::string::npos) << "KernelSnapshotTest003 Failed";
158 EXPECT_TRUE(content.find("Process name:") != std::string::npos) << "KernelSnapshotTest003 Failed";
159 EXPECT_TRUE(content.find("Reason:") != std::string::npos) << "KernelSnapshotTest003 Failed";
160 EXPECT_TRUE(content.find("Exception registers") != std::string::npos) << "KernelSnapshotTest003 Failed";
161 EXPECT_TRUE(content.find("#01") != std::string::npos) << "KernelSnapshotTest003 Failed";
162 EXPECT_TRUE(content.find("Registers:") != std::string::npos) << "KernelSnapshotTest003 Failed";
163 EXPECT_TRUE(content.find("Memory near registers:") != std::string::npos) << "KernelSnapshotTest003 Failed";
164 EXPECT_TRUE(content.find("FaultStack:") != std::string::npos) << "KernelSnapshotTest003 Failed";
165 EXPECT_TRUE(content.find("Elfs:") != std::string::npos) << "KernelSnapshotTest003 Failed";
166 } else {
167 EXPECT_TRUE(false) << "KernelSnapshotTest003 Failed";
168 }
169
170 GTEST_LOG_(INFO) << "KernelSnapshotTest003: end.";
171 }
172
173 /**
174 * @tc.name: KernelSnapshotTest004
175 * @tc.desc: test snapshot generation
176 * @tc.type: FUNC
177 */
178 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest004, TestSize.Level2)
179 {
180 GTEST_LOG_(INFO) << "KernelSnapshotTest004: start.";
181
182 std::string removeProcessdump = "mv /system/bin/processdump /system/bin/processdump.bak";
183 system(removeProcessdump.c_str());
184
185 pid_t pid = fork();
186 if (pid == 0) {
187 int* p = (int*)55;
188 *p = 0;
189 }
190 waitpid(pid, 0, 0);
191
192 std::string snapshotFilePath = WaitCreateCrashFile("cppcrash", pid, 5);
193 std::ifstream snapshotFile(snapshotFilePath);
194 if (snapshotFile.is_open()) {
195 std::string content((std::istreambuf_iterator<char>(snapshotFile)), std::istreambuf_iterator<char>());
196 EXPECT_TRUE(content.find("Build info") != std::string::npos) << "KernelSnapshotTest004 Failed";
197 EXPECT_TRUE(content.find("Timestamp") != std::string::npos) << "KernelSnapshotTest004 Failed";
198 EXPECT_TRUE(content.find("Pid") != std::string::npos) << "KernelSnapshotTest004 Failed";
199 EXPECT_TRUE(content.find("Process name:") != std::string::npos) << "KernelSnapshotTest004 Failed";
200 EXPECT_TRUE(content.find("Reason:") != std::string::npos) << "KernelSnapshotTest004 Failed";
201 EXPECT_TRUE(content.find("#01") != std::string::npos) << "KernelSnapshotTest004 Failed";
202 EXPECT_TRUE(content.find("Registers:") != std::string::npos) << "KernelSnapshotTest004 Failed";
203 EXPECT_TRUE(content.find("Elfs:") != std::string::npos) << "KernelSnapshotTest004 Failed";
204 } else {
205 EXPECT_TRUE(false) << "KernelSnapshotTest004 Failed";
206 }
207
208 std::string recoverProcessdump = "mv /system/bin/processdump.bak /system/bin/processdump";
209 system(recoverProcessdump.c_str());
210
211 GTEST_LOG_(INFO) << "KernelSnapshotTest004: end.";
212 }
213 #endif
214
215 /**
216 * @tc.name: KernelSnapshotTest014
217 * @tc.desc: test PreProcessLine
218 * @tc.type: FUNC
219 */
220 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest014, TestSize.Level2)
221 {
222 GTEST_LOG_(INFO) << "KernelSnapshotTest014: start.";
223
224 KernelSnapshotParser parser;
225 std::string srcLine = "abcd";
226 ASSERT_FALSE(parser.PreProcessLine(srcLine));
227
228 srcLine = "\tabcdefg";
229 ASSERT_FALSE(parser.PreProcessLine(srcLine));
230
231 srcLine = "[1733329272.590140][transaction start] now mono_time is [45.006871]";
232 ASSERT_TRUE(parser.PreProcessLine(srcLine));
233 std::string dstLine = "[transaction start] now mono_time is [45.006871][1733329272.590140]";
234 ASSERT_EQ(srcLine, dstLine);
235
236 GTEST_LOG_(INFO) << "KernelSnapshotTest014: end.";
237 }
238
239 /**
240 * @tc.name: KernelSnapshotTest015
241 * @tc.desc: test convertThreadInfoToPairs
242 * @tc.type: FUNC
243 */
244 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest015, TestSize.Level2)
245 {
246 GTEST_LOG_(INFO) << "KernelSnapshotTest015: start.";
247
248 KernelSnapshotParser parser;
249 std::string line = "pid=1, tid=2, name=main, key=val";
250 std::unordered_map<std::string, std::string> pairs = parser.ConvertThreadInfoToPairs(line);
251 ASSERT_EQ(pairs["pid"], "1");
252 ASSERT_EQ(pairs["tid"], "2");
253 ASSERT_EQ(pairs["name"], "main");
254
255 GTEST_LOG_(INFO) << "KernelSnapshotTest015: end.";
256 }
257
258 /**
259 * @tc.name: KernelSnapshotTest016
260 * @tc.desc: test ParseTransStart
261 * @tc.type: FUNC
262 */
263 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest016, TestSize.Level2)
264 {
265 GTEST_LOG_(INFO) << "KernelSnapshotTest016: start.";
266
267 KernelSnapshotParser parser;
268 std::string line = "abcd";
269 CrashMap output;
270 SnapshotCell cell {SnapshotSection::TRANSACTION_START, std::vector<std::string>{line}, 0, 0};
271 parser.ParseTransStart(cell, output);
272 ASSERT_EQ(output[CrashSection::TIME_STAMP], "");
273
274 line = "[AB_00][transaction start] now mono_time is [45.006871][1733329272.590140]";
275 parser.ParseTransStart({SnapshotSection::TRANSACTION_START, std::vector<std::string>{line}, 0, 0}, output);
276 ASSERT_EQ(output[CrashSection::TIME_STAMP], "1733329272590");
277
278 GTEST_LOG_(INFO) << "KernelSnapshotTest016: end.";
279 }
280
281 /**
282 * @tc.name: KernelSnapshotTest017
283 * @tc.desc: test ParseThreadInfo
284 * @tc.type: FUNC
285 */
286 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest017, TestSize.Level2)
287 {
288 GTEST_LOG_(INFO) << "KernelSnapshotTest017: start.";
289
290 KernelSnapshotParser parser;
291 std::vector<std::string> lines = {
292 "Thread info:",
293 "name=ei.hmsapp.music, tid=5601, state=RUNNING, sctime=40.362389062, tcb_cref=502520008108a, pid=5601, \
294 ppid=656, pgid=1, uid=20020048, cpu=7, cur_rq=7"
295 };
296 CrashMap output;
297 SnapshotCell cell {SnapshotSection::THREAD_INFO, lines, 0, 1};
298 parser.ParseThreadInfo(cell, output);
299 ASSERT_EQ(output[CrashSection::PID], "5601");
300 ASSERT_EQ(output[CrashSection::UID], "20020048");
301 ASSERT_EQ(output[CrashSection::PROCESS_NAME], "ei.hmsapp.music");
302
303 GTEST_LOG_(INFO) << "KernelSnapshotTest017: end.";
304 }
305
306 /**
307 * @tc.name: KernelSnapshotTest018
308 * @tc.desc: test ParseStackBacktrace
309 * @tc.type: FUNC
310 */
311 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest018, TestSize.Level2)
312 {
313 GTEST_LOG_(INFO) << "KernelSnapshotTest018: start.";
314 std::vector<std::string> lines = {
315 "Stack backtrace (pname=/system/bin/appspawn, pid=7296):",
316 " [0000000000047af8][FP: 0000007e8d68c330]<???+0x0/0x0> (/system/lib64/platformsdk/libace_napi.z.so)",
317 " [000000000057c33c][FP: 0000007e8d68c3f0]<???+0x0/0x0> (/system/lib64/platformsdk/libark_jsruntime.so)"
318 };
319 KernelSnapshotParser parser;
320 CrashMap output;
321
322 std::string frame = "#01 pc 000000000057c33c /system/lib64/platformsdk/libark_jsruntime.so";
323 parser.ParseStackBacktrace({SnapshotSection::STACK_BACKTRACE, lines, 0, 2}, output);
324 EXPECT_TRUE(output[CrashSection::FAULT_THREAD_INFO].find(frame) != std::string::npos);
325 GTEST_LOG_(INFO) << "KernelSnapshotTest018: end.";
326 }
327
328 /**
329 * @tc.name: KernelSnapshotTest019
330 * @tc.desc: test ParseProcessRealName
331 * @tc.type: FUNC
332 */
333 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest019, TestSize.Level2)
334 {
335 GTEST_LOG_(INFO) << "KernelSnapshotTest019: start.";
336 std::vector<std::string> lines = {
337 "Process statistics:",
338 " name tid state tcb_cref sched_cnt cpu_cur rq_cur cls rtprio ni pri pid ppid pgid",
339 " SaInit1 1012 RUNNING 5022a0008106b 7 7 6 TS - 0 20 799 1 1",
340 " audio_server 799 BLOCKED 5022a0008108a 325 4 6 TS - 0 20 799 1 1"
341 };
342 KernelSnapshotParser parser;
343 CrashMap output;
344 output[CrashSection::PID] = "799";
345 parser.ParseProcessRealName({SnapshotSection::PROCESS_STATISTICS, lines, 0, 3}, output);
346 ASSERT_EQ(output[CrashSection::PROCESS_NAME], "audio_server");
347 GTEST_LOG_(INFO) << "KernelSnapshotTest019: end.";
348 }
349
350 /**
351 * @tc.name: KernelSnapshotTest020
352 * @tc.desc: test ProcessSnapshotSection
353 * @tc.type: FUNC
354 */
355 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest020, TestSize.Level2)
356 {
357 GTEST_LOG_(INFO) << "KernelSnapshotTest020: start.";
358 KernelSnapshotParser parser;
359 CrashMap output;
360
361 std::vector<std::string> lines = {
362 "[AB_00][transaction start] now mono_time is [45.006871][1733329272.590140]"
363 };
364
365 parser.ProcessSnapshotSection({SnapshotSection::TRANSACTION_START, lines, 0, 0}, output);
366 ASSERT_EQ(output[CrashSection::TIME_STAMP], "1733329272590");
367 GTEST_LOG_(INFO) << "KernelSnapshotTest020: end.";
368 }
369
370 /**
371 * @tc.name: KernelSnapshotTest021
372 * @tc.desc: test ParseSnapshotUnit
373 * @tc.type: FUNC
374 */
375 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest021, TestSize.Level2)
376 {
377 GTEST_LOG_(INFO) << "KernelSnapshotTest021: start.";
378 KernelSnapshotParser parser;
379
380 std::ifstream file(KERNEL_SNAPSHOT_EXECPTION_FILE);
381 std::vector<std::string> lines;
382 if (file.is_open()) {
383 std::string line;
384 while (std::getline(file, line)) {
385 parser.PreProcessLine(line);
386 lines.push_back(line.substr(7));
387 }
388 file.close();
389 } else {
390 FAIL() << "Failed to open file: " << KERNEL_SNAPSHOT_EXECPTION_FILE;
391 }
392
393 size_t index = 0;
394 CrashMap output = parser.ParseSnapshotUnit(lines, index);
395 EXPECT_EQ(output[CrashSection::TIME_STAMP], "1736169993223");
396 EXPECT_EQ(output[CrashSection::PID], "799");
397 EXPECT_EQ(output[CrashSection::PROCESS_NAME], "audio_server");
398 GTEST_LOG_(INFO) << "KernelSnapshotTest021: end.";
399 }
400
401 /**
402 * @tc.name: KernelSnapshotTest022
403 * @tc.desc: test ParseSameSeqSnapshot
404 * @tc.type: FUNC
405 */
406 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest022, TestSize.Level2)
407 {
408 GTEST_LOG_(INFO) << "KernelSnapshotTest022: start.";
409 std::ifstream file(KERNEL_SNAPSHOT_2_EXECPTION_FILE);
410 KernelSnapshotParser parser;
411 std::vector<std::string> lines;
412 if (file.is_open()) {
413 std::string line;
414 while (std::getline(file, line)) {
415 parser.PreProcessLine(line);
416 lines.push_back(line.substr(7));
417 }
418 file.close();
419 } else {
420 FAIL() << "Failed to open file: " << KERNEL_SNAPSHOT_2_EXECPTION_FILE;
421 }
422 std::vector<CrashMap> crashMaps;
423 parser.ParseSameSeqSnapshot(lines, crashMaps);
424
425 EXPECT_EQ(crashMaps.size(), 2);
426 GTEST_LOG_(INFO) << "KernelSnapshotTest022: end.";
427 }
428
429 /**
430 * @tc.name: KernelSnapshotTest026
431 * @tc.desc: test ParseSnapshot
432 * @tc.type: FUNC
433 */
434 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest026, TestSize.Level2)
435 {
436 GTEST_LOG_(INFO) << "KernelSnapshotTest026: start.";
437 KernelSnapshotParser parser;
438 std::string cont = "adba";
439 std::vector<std::string> lines;
440 auto output = parser.ParseSnapshot(lines);
441 ASSERT_EQ(output.size(), 0);
442
443 std::ifstream file(KERNEL_SNAPSHOT_ABORT_FILE);
444 if (file.is_open()) {
445 std::string line;
446 while (std::getline(file, line)) {
447 lines.push_back(line);
448 }
449 file.close();
450 } else {
451 FAIL() << "Failed to open file: " << KERNEL_SNAPSHOT_ABORT_FILE;
452 }
453
454 output = parser.ParseSnapshot(lines);
455 ASSERT_EQ(output.size(), 3);
456 GTEST_LOG_(INFO) << "KernelSnapshotTest026: end.";
457 }
458
459 /**
460 * @tc.name: KernelSnapshotTest023
461 * @tc.desc: test SplitByNewLine
462 * @tc.type: FUNC
463 */
464 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest023, TestSize.Level2)
465 {
466 GTEST_LOG_(INFO) << "KernelSnapshotTest023: start.";
467 KernelSnapshotParser parser;
468 std::vector<std::string> lines;
469 std::string str = "a\nb\nc";
470 parser.SplitByNewLine(str, lines);
471 ASSERT_EQ(lines.size(), 3);
472 GTEST_LOG_(INFO) << "KernelSnapshotTest023: end.";
473 }
474
475 /**
476 * @tc.name: KernelSnapshotTest024
477 * @tc.desc: test ProcessTransStart
478 * @tc.type: FUNC
479 */
480 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest024, TestSize.Level2)
481 {
482 GTEST_LOG_(INFO) << "KernelSnapshotTest024: start.";
483 KernelSnapshotParser parser;
484 std::vector<std::string> lines = {
485 "[transaction start] now mono_time is [45.006871][1733329272.590140]"
486 };
487
488 CrashMap output;
489 size_t index = 0;
490 parser.ProcessTransStart(lines, index, SNAPSHOT_SECTION_KEYWORDS[0].key, output);
491 ASSERT_EQ(output[CrashSection::TIME_STAMP], "1733329272590");
492 GTEST_LOG_(INFO) << "KernelSnapshotTest024: end.";
493 }
494
495 /**
496 * @tc.name: KernelSnapshotTest025
497 * @tc.desc: test ParseDefaultAction
498 * @tc.type: FUNC
499 */
500 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest025, TestSize.Level2)
501 {
502 GTEST_LOG_(INFO) << "KernelSnapshotTest025: start.";
503 KernelSnapshotParser parser;
504 std::vector<std::string> lines = {
505 "default"
506 };
507 CrashMap output;
508 parser.ParseDefaultAction({SnapshotSection::BASE_ACTV_DUMPED, lines, 0, lines.size()}, output);
509 ASSERT_EQ(output[CrashSection::FAULT_STACK], "");
510 GTEST_LOG_(INFO) << "KernelSnapshotTest025: end.";
511 }
512
513 /**
514 * @tc.name: KernelSnapshotTest041
515 * @tc.desc: test SaveSnapshot
516 * @tc.type: FUNC
517 */
518 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest041, TestSize.Level2)
519 {
520 GTEST_LOG_(INFO) << "KernelSnapshotTest041: start.";
521 KernelSnapshotPrinter printer;
522 CrashMap output;
523
524 printer.SaveSnapshot(output);
525 auto ret = access("/data/log/faultlog/temp/cppcrash-1", F_OK);
526 ASSERT_FALSE(ret == 0);
527 GTEST_LOG_(INFO) << "KernelSnapshotTest041: end.";
528 }
529
530 /**
531 * @tc.name: KernelSnapshotTest042
532 * @tc.desc: test OutputToFile
533 * @tc.type: FUNC
534 */
535 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest042, TestSize.Level2)
536 {
537 GTEST_LOG_(INFO) << "KernelSnapshotTest042: start.";
538 KernelSnapshotPrinter printer;
539 CrashMap output;
540 std::string filePath = "/data/log/faultlog/temp/cppcrash.txt";
541 printer.OutputToFile(filePath, output);
542
543 auto ret = access(filePath.c_str(), F_OK);
544 ASSERT_EQ(ret, 0);
545 GTEST_LOG_(INFO) << "KernelSnapshotTest042: end.";
546 }
547
548 /**
549 * @tc.name: KernelSnapshotTest043
550 * @tc.desc: test SaveSnapshots
551 * @tc.type: FUNC
552 */
553 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest043, TestSize.Level2)
554 {
555 GTEST_LOG_(INFO) << "KernelSnapshotTest043: start.";
556 KernelSnapshotPrinter printer;
557 std::vector<CrashMap> outputs;
558 printer.SaveSnapshots(outputs);
559
560 for (auto &output : outputs) {
561 output[CrashSection::PID] = "1";
562 auto ret = access("/data/log/faultlog/temp/cppcrash-1", F_OK);
563 ASSERT_EQ(ret, 0);
564 }
565 GTEST_LOG_(INFO) << "KernelSnapshotTest043: end.";
566 }
567
568 /**
569 * @tc.name: KernelSnapshotTest051
570 * @tc.desc: test ReportCrashnoLogEvent
571 * @tc.type: FUNC
572 */
573 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest051, TestSize.Level2)
574 {
575 GTEST_LOG_(INFO) << "KernelSnapshotTest051: start.";
576 KernelSnapshotReporter reporter;
577
578 CrashMap output;
579 output[CrashSection::UID] = "123";
580 output[CrashSection::PID] = "156";
581 output[CrashSection::TIME_STAMP] = "152451571481";
582 output[CrashSection::PROCESS_NAME] = "testProcess";
583 auto ret = reporter.ReportCrashNoLogEvent(output);
584 ASSERT_TRUE(ret);
585 GTEST_LOG_(INFO) << "KernelSnapshotTest051: end.";
586 }
587
588 /**
589 * @tc.name: KernelSnapshotReporter002
590 * @tc.desc: test ReportRawMsg
591 * @tc.type: FUNC
592 */
593 HWTEST_F(KernelSnapshotTest, KernelSnapshotReporter002, TestSize.Level2)
594 {
595 GTEST_LOG_(INFO) << "KernelSnapshotReporter002: start.";
596 KernelSnapshotReporter reporter;
597
598 std::string snapshot;
599 std::vector<CrashMap> outputs;
600 CrashMap output;
601 output[CrashSection::UID] = "";
602 output[CrashSection::PID] = "156";
603 output[CrashSection::TIME_STAMP] = "152451571481";
604 output[CrashSection::PROCESS_NAME] = "testProcess";
605 outputs.push_back(output);
606 reporter.ReportEvents(outputs, snapshot);
607
608 auto ret = reporter.ReportRawMsg(snapshot);
609 EXPECT_FALSE(ret);
610 snapshot = "kernel_snapshot";
611 ret = reporter.ReportRawMsg(snapshot);
612 EXPECT_TRUE(ret);
613 GTEST_LOG_(INFO) << "KernelSnapshotReporter002: end.";
614 }
615
616 /**
617 * @tc.name: KernelSnapshotReporter003
618 * @tc.desc: test GetSnapshotPid
619 * @tc.type: FUNC
620 */
621 HWTEST_F(KernelSnapshotTest, KernelSnapshotReporter003, TestSize.Level2)
622 {
623 GTEST_LOG_(INFO) << "KernelSnapshotReporter003: start.";
624 KernelSnapshotReporter reporter;
625 EXPECT_EQ(reporter.GetSnapshotPid("12)"), 0);
626 EXPECT_EQ(reporter.GetSnapshotPid("abcpid=123"), 0);
627 EXPECT_EQ(reporter.GetSnapshotPid("abcpid=abc)"), 0);
628 EXPECT_EQ(reporter.GetSnapshotPid("abcpid=123)"), 123);
629 GTEST_LOG_(INFO) << "KernelSnapshotReporter003: end.";
630 }
631
632 /**
633 * @tc.name: KernelSnapshotTest052
634 * @tc.desc: test kernel snapshot trie insert
635 * @tc.type: FUNC
636 */
637 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest052, TestSize.Level2)
638 {
639 GTEST_LOG_(INFO) << "KernelSnapshotTest052: start.";
640 KernelSnapshotTrie trie;
641
642 bool ret = trie.Insert("", SnapshotSection::TRANSACTION_START);
643 EXPECT_EQ(ret, false);
644
645 ret = trie.Insert("start", SnapshotSection::TRANSACTION_START);
646 EXPECT_EQ(ret, true);
647
648 GTEST_LOG_(INFO) << "KernelSnapshotTest052: end.";
649 }
650 /**
651 * @tc.name: KernelSnapshotTest053
652 * @tc.desc: test kernel snapshot trie match prefix
653 * @tc.type: FUNC
654 */
655 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest053, TestSize.Level2)
656 {
657 GTEST_LOG_(INFO) << "KernelSnapshotTest053: start.";
658 KernelSnapshotTrie trie;
659
660 for (const auto& item : SNAPSHOT_SECTION_KEYWORDS) {
661 trie.Insert(item.key, item.type);
662 }
663
664 SnapshotSection type;
665 bool ret = trie.MatchPrefix(std::string(SNAPSHOT_SECTION_KEYWORDS[0].key), type);
666 EXPECT_EQ(ret, true);
667 EXPECT_EQ(type, SNAPSHOT_SECTION_KEYWORDS[0].type);
668
669 trie.MatchPrefix(std::string(SNAPSHOT_SECTION_KEYWORDS[1].key) + "abc", type);
670 EXPECT_EQ(ret, true);
671 EXPECT_EQ(type, SNAPSHOT_SECTION_KEYWORDS[1].type);
672
673 ret = trie.MatchPrefix(std::string("abc"), type);
674 EXPECT_EQ(ret, false);
675
676 GTEST_LOG_(INFO) << "KernelSnapshotTest053: end.";
677 }
678
679 /**
680 * @tc.name: KernelSnapshotTest054
681 * @tc.desc: test generate summary local is true
682 * @tc.type: FUNC
683 */
684 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest054, TestSize.Level2)
685 {
686 GTEST_LOG_(INFO) << "KernelSnapshotTest054: start.";
687 CrashMap output;
688 output[CrashSection::PID] = "";
689 output[CrashSection::UID] = "1000";
690 output[CrashSection::FAULT_STACK] = "[0000005b36f41a70][FP: 0000005b36fafbd0]";
691 auto summary = KernelSnapshotContentBuilder(output, true).GenerateSummary();
692
693 EXPECT_TRUE(summary.find("Pid:") == std::string::npos);
694 EXPECT_TRUE(summary.find("Uid:") != std::string::npos);
695 EXPECT_TRUE(summary.find("FaultStack:") != std::string::npos);
696
697 output[CrashSection::PID] = "123";
698 summary = KernelSnapshotContentBuilder(output, true).GenerateSummary();
699 EXPECT_TRUE(summary.find("Pid:") != std::string::npos);
700
701 GTEST_LOG_(INFO) << "KernelSnapshotTest054: end.";
702 }
703
704 /**
705 * @tc.name: KernelSnapshotTest055
706 * @tc.desc: test test generate summary local is false
707 * @tc.type: FUNC
708 */
709 HWTEST_F(KernelSnapshotTest, KernelSnapshotTest055, TestSize.Level2)
710 {
711 GTEST_LOG_(INFO) << "KernelSnapshotTest055: start.";
712 CrashMap output = {
713 {CrashSection::UID, "1000"},
714 {CrashSection::FAULT_STACK, "[0000005b36f41a70][FP: 0000005b36fafbd0]"}
715 };
716
717 auto summary = KernelSnapshotContentBuilder(output, false).GenerateSummary();
718
719 EXPECT_TRUE(summary.find("Uid:") != std::string::npos);
720 EXPECT_TRUE(summary.find("FaultStack:") == std::string::npos);
721
722 GTEST_LOG_(INFO) << "KernelSnapshotTest055: end.";
723 }
724 } // namespace HiviewDFX
725 } // namepsace OHOS
726