1 /*
2 * Copyright (c) 2021-2023 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 <cerrno>
17 #include <fstream>
18 #include <gtest/gtest.h>
19 #include <map>
20 #include <securec.h>
21 #include <string>
22 #include <sys/mman.h>
23 #include <thread>
24 #include <unistd.h>
25 #include <vector>
26
27 #include "dfx_test_util.h"
28 #include "directory_ex.h"
29 #include "dfx_define.h"
30 #include "dfx_util.h"
31 #include "procinfo.h"
32
33 using namespace testing::ext;
34 using namespace std;
35
36 #define NSPID_PATH "/data/nspid"
37
38 namespace OHOS {
39 namespace HiviewDFX {
40 class FaultLoggerdSystemTest : public testing::Test {
41 public:
42 static void SetUpTestCase(void);
43 static void TearDownTestCase(void);
44 void SetUp();
45 void TearDown();
46 };
47
SetUpTestCase(void)48 void FaultLoggerdSystemTest::SetUpTestCase(void)
49 {
50 chmod("/data/crasher_c", 0755); // 0755 : -rwxr-xr-x
51 chmod("/data/crasher_cpp", 0755); // 0755 : -rwxr-xr-x
52 }
53
TearDownTestCase(void)54 void FaultLoggerdSystemTest::TearDownTestCase(void)
55 {
56 }
57
SetUp(void)58 void FaultLoggerdSystemTest::SetUp(void)
59 {
60 }
61
TearDown(void)62 void FaultLoggerdSystemTest::TearDown(void)
63 {
64 }
65
66 namespace {
67 static const int CPPCRASH_FILENAME_MIN_LENGTH = 36; // 36 : length of /data/log/faultlog/temp/cppcrash-x-x
68 static const int SIGNAL_TEST_NUM = 50;
69 }
70
ForkAndExecuteCrasher(const string & option,const CrasherType type)71 static pid_t ForkAndExecuteCrasher(const string& option, const CrasherType type)
72 {
73 pid_t pid = fork();
74 if (pid < 0) {
75 GTEST_LOG_(ERROR) << "Fork failed";
76 return pid;
77 } else if (pid == 0) {
78 if (type == CRASHER_C) {
79 execl("/data/crasher_c", "crasher_c", option.c_str(), nullptr);
80 } else {
81 execl("/data/crasher_cpp", "crasher_cpp", option.c_str(), nullptr);
82 }
83 }
84
85 GTEST_LOG_(INFO) << "forked pid:" << pid;
86 constexpr time_t maxWaitingTime = 60; // 60 : 60s timeout
87 time_t remainedTime = maxWaitingTime;
88 while (remainedTime > 0) {
89 time_t startTime = time(nullptr);
90 int status = 0;
91 waitpid(pid, &status, WNOHANG);
92 if (WIFEXITED(status)) {
93 break;
94 }
95 sleep(1);
96 time_t duration = time(nullptr) - startTime;
97 remainedTime = (remainedTime > duration) ? (remainedTime - duration) : 0;
98 }
99 return pid;
100 }
101
TriggerCrasherAndGetFileName(const string & option,const CrasherType type,string & crashFileName,int waitSec=1)102 static pid_t TriggerCrasherAndGetFileName(const string& option, const CrasherType type, string& crashFileName,
103 int waitSec = 1)
104 {
105 auto pid = ForkAndExecuteCrasher(option, type);
106 sleep(waitSec); // sleep for log generation
107 crashFileName = GetCppCrashFileName(pid);
108 return pid;
109 }
110
CheckCountNum(const string & filePath,const pid_t & pid,const string & option)111 static bool CheckCountNum(const string& filePath, const pid_t& pid, const string& option)
112 {
113 map<string, string> optionReasonMap = {
114 #if defined(__LP64__)
115 { string("triSIGTRAP"), string("SIGILL") },
116 #else
117 { string("triSIGTRAP"), string("SIGTRAP") },
118 #endif
119 { string("triSIGILL"), string("SIGILL") },
120 { string("triSIGSEGV"), string("SIGSEGV") },
121 { string("MaxStack"), string("SIGSEGV") },
122 { string("MaxMethod"), string("SIGSEGV") },
123 { string("STACKOF"), string("SIGSEGV") },
124 { string("OOM"), string("SIGABRT") },
125 };
126 string reason = option;
127 auto iter = optionReasonMap.find(option);
128 if (iter != optionReasonMap.end()) {
129 GTEST_LOG_(INFO) << "optionReasonMap matched";
130 reason = iter->second;
131 }
132 string log[] = {
133 "Pid:" + to_string(pid), "Uid", ":crasher", reason, "Tid:", "#00", "Registers:", REGISTERS, "FaultStack:",
134 "Maps:", "/crasher"
135 };
136 int minRegIdx = 6; // 6 : index of first REGISTERS - 1
137 int expectNum = sizeof(log) / sizeof(log[0]);
138 return CheckKeyWords(filePath, log, expectNum, minRegIdx) == expectNum;
139 }
140
CheckCountNumAbort(const string & filePath,const pid_t & pid)141 static bool CheckCountNumAbort(const string& filePath, const pid_t& pid)
142 {
143 string log[] = {
144 "Pid:" + to_string(pid), "Uid", ":crasher", "SIGABRT", "LastFatalMessage:", "ABORT!", "Tid:", "#00",
145 "Registers:", REGISTERS, "FaultStack:", "Maps:", "/crasher"
146 };
147 int minRegIdx = 8; // 8 : index of first REGISTERS - 1
148 int expectNum = sizeof(log) / sizeof(log[0]);
149 return CheckKeyWords(filePath, log, expectNum, minRegIdx) == expectNum;
150 }
151
CheckCountNumPCZero(const string & filePath,const pid_t & pid)152 static bool CheckCountNumPCZero(const string& filePath, const pid_t& pid)
153 {
154 string log[] = {
155 "Pid:" + to_string(pid), "Uid", ":crasher", "SIGSEGV", "Tid:", "#00", "Registers:", REGISTERS, "FaultStack:",
156 "Maps:", "/crasher"
157 };
158 int minRegIdx = 6; // 6 : index of first REGISTERS - 1
159 int expectNum = sizeof(log) / sizeof(log[0]);
160 return CheckKeyWords(filePath, log, expectNum, minRegIdx) == expectNum;
161 }
162
CheckCountNumOverStack(const string & filePath,const pid_t & pid)163 static bool CheckCountNumOverStack(const string& filePath, const pid_t& pid)
164 {
165 string log[] = {
166 "Pid:" + to_string(pid), "Uid", ":crasher", "SIGSEGV", "Tid:", "#56", "Registers:", REGISTERS, "FaultStack:",
167 "Maps:", "/crasher"
168 };
169 int minRegIdx = 6; // 6 : index of first REGISTERS - 1
170 int expectNum = sizeof(log) / sizeof(log[0]);
171 return CheckKeyWords(filePath, log, expectNum, minRegIdx) == expectNum;
172 }
173
CheckCountNumMultiThread(const string & filePath,const pid_t & pid)174 static bool CheckCountNumMultiThread(const string& filePath, const pid_t& pid)
175 {
176 string log[] = {
177 "Pid:" + to_string(pid), "Uid", ":crasher", "SIGSEGV", "Tid:", "#00",
178 "Registers:", REGISTERS, "FaultStack:", "Maps:",
179 "/crasher"
180 };
181 int minRegIdx = 6; // 6 : index of first REGISTERS - 1
182 int expectNum = sizeof(log) / sizeof(log[0]);
183 return CheckKeyWords(filePath, log, expectNum, minRegIdx) == expectNum;
184 }
185
GetStackTop(void)186 static string GetStackTop(void)
187 {
188 ifstream spFile;
189 spFile.open("/data/sp");
190 string sp;
191 spFile >> sp;
192 spFile.close();
193 int ret = remove("/data/sp");
194 if (ret != 0) {
195 printf("remove failed!");
196 }
197 int leftZero = REGISTER_FORMAT_LENGTH - sp.length();
198 while (leftZero > 0) {
199 sp = "0" + sp;
200 leftZero--;
201 }
202 GTEST_LOG_(INFO) << "sp:" << sp;
203 return sp;
204 }
205
WriteRealPid(int realPid)206 static void WriteRealPid(int realPid)
207 {
208 ofstream file;
209 file.open(NSPID_PATH);
210 file << std::to_string(realPid);
211 file.close();
212 }
213
ReadRealPid(void)214 static int ReadRealPid(void)
215 {
216 ifstream file;
217 file.open(NSPID_PATH);
218 string pid;
219 file >> pid;
220 file.close();
221 int ret = remove(NSPID_PATH);
222 if (ret != 0) {
223 printf("remove failed!");
224 }
225 int realPid = atoi(pid.c_str());
226 GTEST_LOG_(INFO) << "real pid:" << realPid;
227 return realPid;
228 }
229
CheckCountNumStackTop(const string & filePath,const pid_t & pid)230 static bool CheckCountNumStackTop(const string& filePath, const pid_t& pid)
231 {
232 string log[] = {
233 "Pid:" + to_string(pid), "Uid", ":crasher", "SIGSEGV", "Tid:", "#00", "Registers:", REGISTERS, "FaultStack:",
234 "Maps:", "/crasher"
235 };
236 string sp = GetStackTop();
237 for (auto& keyword : log) {
238 if (keyword == "sp:") {
239 keyword += sp;
240 }
241 }
242 int minRegIdx = 6; // 6 : index of first REGISTERS - 1
243 int expectNum = sizeof(log) / sizeof(log[0]);
244 return CheckKeyWords(filePath, log, expectNum, minRegIdx) == expectNum;
245 }
246
CheckCppCrashAllLabelKeywords(const string & filePath,const pid_t & pid)247 static bool CheckCppCrashAllLabelKeywords(const string& filePath, const pid_t& pid)
248 {
249 string log[] = {
250 "Timestamp:", "Pid:" + to_string(pid), "Uid:", "Process", "Reason:", "LastFatalMessage:", "Fault", "thread",
251 "Info:", "Tid:", "#00", "Registers:", REGISTERS, "Memory", "near", "registers:", "FaultStack:", "Maps:",
252 "/crasher"
253 };
254 int minRegIdx = 11; // 11 : index of first REGISTERS - 1
255 int expectNum = sizeof(log) / sizeof(log[0]);
256 return CheckKeyWords(filePath, log, expectNum, minRegIdx) == expectNum;
257 }
258
259 /**
260 * @tc.name: FaultLoggerdSystemTest001
261 * @tc.desc: test C crasher application: SIGFPE
262 * @tc.type: FUNC
263 */
264 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest001, TestSize.Level2)
265 {
266 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest001: start.";
267 string cmd = "SIGFPE";
268 string fileName;
269 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_C, fileName);
270 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
271 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
272 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
273 FAIL();
274 }
275 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "ProcessDfxRequestTest001 Failed";
276 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest001: end.";
277 }
278
279 /**
280 * @tc.name: FaultLoggerdSystemTest002
281 * @tc.desc: test CPP crasher application: SIGFPE
282 * @tc.type: FUNC
283 */
284 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest002, TestSize.Level2)
285 {
286 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest002: start.";
287 string cmd = "SIGFPE";
288 string fileName;
289 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_CPP, fileName);
290 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
291 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
292 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
293 FAIL();
294 }
295 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest002 Failed";
296 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest002: end.";
297 }
298
299 /**
300 * @tc.name: FaultLoggerdSystemTest003
301 * @tc.desc: test C crasher application: SIGILL
302 * @tc.type: FUNC
303 */
304 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest003, TestSize.Level2)
305 {
306
307 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest003: start.";
308 string cmd = "SIGILL";
309 string fileName;
310 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_C, fileName);
311 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
312 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
313 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
314 FAIL();
315 }
316 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest003 Failed";
317 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest003: end.";
318 }
319
320 /**
321 * @tc.name: FaultLoggerdSystemTest004
322 * @tc.desc: test CPP crasher application: SIGILL
323 * @tc.type: FUNC
324 */
325 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest004, TestSize.Level2)
326 {
327 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest004: start.";
328 string cmd = "SIGILL";
329 string fileName;
330 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_CPP, fileName);
331 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
332 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
333 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
334 FAIL();
335 }
336 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest004 Failed";
337 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest004: end.";
338 }
339
340 /**
341 * @tc.name: FaultLoggerdSystemTest005
342 * @tc.desc: test C crasher application: triSIGILL
343 * @tc.type: FUNC
344 */
345 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest005, TestSize.Level2)
346 {
347 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest005: start.";
348 string cmd = "triSIGILL";
349 string fileName;
350 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_C, fileName);
351 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
352 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
353 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
354 FAIL();
355 }
356 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest005 Failed";
357 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest005: end.";
358 }
359
360 /**
361 * @tc.name: FaultLoggerdSystemTest006
362 * @tc.desc: test CPP crasher application: triSIGILL
363 * @tc.type: FUNC
364 */
365 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest006, TestSize.Level2)
366 {
367 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest006: start.";
368 string cmd = "triSIGILL";
369 string fileName;
370 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_CPP, fileName);
371 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
372 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
373 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
374 FAIL();
375 }
376 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest006 Failed";
377 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest006: end.";
378 }
379
380 /**
381 * @tc.name: FaultLoggerdSystemTest007
382 * @tc.desc: test C crasher application: SIGSEGV
383 * @tc.type: FUNC
384 */
385 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest007, TestSize.Level2)
386 {
387
388 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest007: start.";
389 string cmd = "SIGSEGV";
390 string fileName;
391 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_C, fileName);
392 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
393 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
394 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
395 FAIL();
396 }
397 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest007 Failed";
398 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest007: end.";
399 }
400
401 /**
402 * @tc.name: FaultLoggerdSystemTest008
403 * @tc.desc: test CPP crasher application: SIGSEGV
404 * @tc.type: FUNC
405 */
406 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest008, TestSize.Level2)
407 {
408 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest008: start.";
409 string cmd = "SIGSEGV";
410 string fileName;
411 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_CPP, fileName);
412 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
413 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
414 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
415 FAIL();
416 }
417 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest008 Failed";
418 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest008: end.";
419 }
420
421 /**
422 * @tc.name: FaultLoggerdSystemTest009
423 * @tc.desc: test C crasher application: triSIGSEGV
424 * @tc.type: FUNC
425 */
426 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest009, TestSize.Level2)
427 {
428
429 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest009: start.";
430 string cmd = "triSIGSEGV";
431 string fileName;
432 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_C, fileName);
433 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
434 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
435 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
436 FAIL();
437 }
438 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest009 Failed";
439 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest009: end.";
440 }
441
442 /**
443 * @tc.name: FaultLoggerdSystemTest010
444 * @tc.desc: test CPP crasher application: triSIGSEGV
445 * @tc.type: FUNC
446 */
447 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest010, TestSize.Level2)
448 {
449 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest010: start.";
450 string cmd = "triSIGSEGV";
451 string fileName;
452 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_CPP, fileName);
453 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
454 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
455 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
456 FAIL();
457 }
458 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest010 Failed";
459 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest010: end.";
460 }
461
462 /**
463 * @tc.name: FaultLoggerdSystemTest011
464 * @tc.desc: test C crasher application: SIGTRAP
465 * @tc.type: FUNC
466 */
467 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest011, TestSize.Level2)
468 {
469 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest011: start.";
470 string cmd = "SIGTRAP";
471 string fileName;
472 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_C, fileName);
473 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
474 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
475 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
476 FAIL();
477 }
478 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest011 Failed";
479 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest011: end.";
480 }
481
482 /**
483 * @tc.name: FaultLoggerdSystemTest012
484 * @tc.desc: test CPP crasher application: SIGTRAP
485 * @tc.type: FUNC
486 */
487 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest012, TestSize.Level2)
488 {
489 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest012: start.";
490 string cmd = "SIGTRAP";
491 string fileName;
492 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_CPP, fileName);
493 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
494 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
495 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
496 FAIL();
497 }
498 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest012 Failed";
499 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest012: end.";
500 }
501
502 /**
503 * @tc.name: FaultLoggerdSystemTest013
504 * @tc.desc: test C crasher application: triSIGTRAP
505 * @tc.type: FUNC
506 */
507 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest013, TestSize.Level2)
508 {
509 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest013: start.";
510 string cmd = "triSIGTRAP";
511 string fileName;
512 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_C, fileName);
513 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
514 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
515 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
516 FAIL();
517 }
518 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest013 Failed";
519 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest013: end.";
520 }
521
522 /**
523 * @tc.name: FaultLoggerdSystemTest014
524 * @tc.desc: test CPP crasher application: triSIGTRAP
525 * @tc.type: FUNC
526 */
527 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest014, TestSize.Level2)
528 {
529 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest014: start.";
530 string cmd = "triSIGTRAP";
531 string fileName;
532 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_CPP, fileName);
533 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
534 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
535 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
536 FAIL();
537 }
538 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest014 Failed";
539 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest014: end.";
540 }
541
542 /**
543 * @tc.name: FaultLoggerdSystemTest015
544 * @tc.desc: test C crasher application: SIGABRT
545 * @tc.type: FUNC
546 */
547 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest015, TestSize.Level2)
548 {
549 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest015: start.";
550 string cmd = "SIGABRT";
551 string fileName;
552 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_C, fileName);
553 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
554 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
555 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
556 FAIL();
557 }
558 EXPECT_TRUE(CheckCountNumAbort(fileName, pid)) << "FaultLoggerdSystemTest015 Failed";
559 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest015: end.";
560 }
561
562 /**
563 * @tc.name: FaultLoggerdSystemTest016
564 * @tc.desc: test CPP crasher application: SIGABRT
565 * @tc.type: FUNC
566 */
567 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest016, TestSize.Level2)
568 {
569 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest016: start.";
570 string cmd = "SIGABRT";
571 string fileName;
572 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_CPP, fileName);
573 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
574 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
575 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
576 FAIL();
577 }
578 EXPECT_TRUE(CheckCountNumAbort(fileName, pid)) << "FaultLoggerdSystemTest016 Failed";
579 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest016: end.";
580 }
581
582 /**
583 * @tc.name: FaultLoggerdSystemTest017
584 * @tc.desc: test C crasher application: triSIGABRT
585 * @tc.type: FUNC
586 */
587 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest017, TestSize.Level2)
588 {
589 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest017: start.";
590 string cmd = "triSIGABRT";
591 string fileName;
592 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_C, fileName);
593 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
594 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
595 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
596 FAIL();
597 }
598 EXPECT_TRUE(CheckCountNumAbort(fileName, pid)) << "FaultLoggerdSystemTest017 Failed";
599 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest017: end.";
600 }
601
602 /**
603 * @tc.name: FaultLoggerdSystemTest018
604 * @tc.desc: test CPP crasher application: triSIGABRT
605 * @tc.type: FUNC
606 */
607 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest018, TestSize.Level2)
608 {
609 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest018: start.";
610 string cmd = "triSIGABRT";
611 string fileName;
612 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_C, fileName);
613 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
614 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
615 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
616 FAIL();
617 }
618 EXPECT_TRUE(CheckCountNumAbort(fileName, pid)) << "FaultLoggerdSystemTest018 Failed";
619 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest018: end.";
620 }
621
622 /**
623 * @tc.name: FaultLoggerdSystemTest019
624 * @tc.desc: test C crasher application: SIGBUS
625 * @tc.type: FUNC
626 */
627 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest019, TestSize.Level2)
628 {
629 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest019: start.";
630 string cmd = "SIGBUS";
631 string fileName;
632 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_C, fileName);
633 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
634 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
635 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
636 FAIL();
637 }
638 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest019 Failed";
639 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest019: end.";
640 }
641
642 /**
643 * @tc.name: FaultLoggerdSystemTest020
644 * @tc.desc: test CPP crasher application: SIGBUS
645 * @tc.type: FUNC
646 */
647 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest020, TestSize.Level2)
648 {
649 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest020: start.";
650 string cmd = "SIGBUS";
651 string fileName;
652 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_CPP, fileName);
653 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
654 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
655 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
656 FAIL();
657 }
658 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest020 Failed";
659 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest020: end.";
660 }
661
662 /**
663 * @tc.name: FaultLoggerdSystemTest021
664 * @tc.desc: test C crasher application: MaxStack
665 * @tc.type: FUNC
666 */
667 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest021, TestSize.Level2)
668 {
669 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest021: start.";
670 string cmd = "MaxStack";
671 string fileName;
672 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_C, fileName);
673 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
674 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
675 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
676 FAIL();
677 }
678 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest021 Failed";
679 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest021: end.";
680 }
681
682 /**
683 * @tc.name: FaultLoggerdSystemTest022
684 * @tc.desc: test CPPcrasher application: MaxStack
685 * @tc.type: FUNC
686 */
687 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest022, TestSize.Level2)
688 {
689 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest022: start.";
690 string cmd = "MaxStack";
691 string fileName;
692 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_CPP, fileName);
693 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
694 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
695 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
696 FAIL();
697 }
698 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest022 Failed";
699 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest022: end.";
700 }
701
702 /**
703 * @tc.name: FaultLoggerdSystemTest023
704 * @tc.desc: test C crasher application: MaxMethod
705 * @tc.type: FUNC
706 */
707 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest023, TestSize.Level2)
708 {
709 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest023: start.";
710 string cmd = "MaxMethod";
711 string fileName;
712 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_C, fileName);
713 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
714 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
715 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
716 FAIL();
717 }
718 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest023 Failed";
719 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest023: end.";
720 }
721
722 /**
723 * @tc.name: FaultLoggerdSystemTest024
724 * @tc.desc: test CPP crasher application: MaxMethod
725 * @tc.type: FUNC
726 */
727 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest024, TestSize.Level2)
728 {
729 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest024: start.";
730 string cmd = "MaxMethod";
731 string fileName;
732 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_CPP, fileName);
733 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
734 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
735 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
736 FAIL();
737 }
738 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest024 Failed";
739 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest024: end.";
740 }
741
742 /**
743 * @tc.name: FaultLoggerdSystemTest025
744 * @tc.desc: test C crasher application: STACKOF
745 * @tc.type: FUNC
746 */
747 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest025, TestSize.Level2)
748 {
749 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest025: start.";
750 string cmd = "STACKOF";
751 string fileName;
752 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_C, fileName);
753 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
754 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
755 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
756 FAIL();
757 }
758 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest025 Failed";
759 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest025: end.";
760 }
761
762 /**
763 * @tc.name: FaultLoggerdSystemTest026
764 * @tc.desc: test CPP crasher application: STACKOF
765 * @tc.type: FUNC
766 */
767 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest026, TestSize.Level2)
768 {
769 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest026: start.";
770 string cmd = "STACKOF";
771 string fileName;
772 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_CPP, fileName);
773 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
774 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
775 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
776 FAIL();
777 }
778 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest026 Failed";
779 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest026: end.";
780 }
781
782 /**
783 * @tc.name: FaultLoggerdSystemTest027
784 * @tc.desc: test CPP crasher application: OOM
785 * @tc.type: FUNC
786 */
787 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest027, TestSize.Level2)
788 {
789 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest027: start.";
790 string cmd = "OOM";
791 string fileName;
792 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_CPP, fileName);
793 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
794 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
795 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
796 FAIL();
797 }
798 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest027 Failed";
799 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest027: end.";
800 }
801
802 /**
803 * @tc.name: FaultLoggerdSystemTest028
804 * @tc.desc: test C crasher application: OOM
805 * @tc.type: FUNC
806 */
807 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest028, TestSize.Level2)
808 {
809 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest028: start.";
810 string cmd = "OOM";
811 string fileName;
812 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_C, fileName);
813 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
814 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
815 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
816 FAIL();
817 }
818 EXPECT_TRUE(CheckCountNum(fileName, pid, cmd)) << "FaultLoggerdSystemTest028 Failed";
819 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest028: end.";
820 }
821 /**
822 * @tc.name: FaultLoggerdSystemTest029
823 * @tc.desc: test CPP crasher application: PCZero
824 * @tc.type: FUNC
825 */
826 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest029, TestSize.Level2)
827 {
828 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest029: start.";
829 string cmd = "PCZero";
830 string fileName;
831 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_CPP, fileName);
832 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
833 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
834 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
835 FAIL();
836 }
837 EXPECT_TRUE(CheckCountNumPCZero(fileName, pid)) << "FaultLoggerdSystemTest029 Failed";
838 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest029: end.";
839 }
840
841 /**
842 * @tc.name: FaultLoggerdSystemTest030
843 * @tc.desc: test C crasher application: PCZero
844 * @tc.type: FUNC
845 */
846 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest030, TestSize.Level2)
847 {
848 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest030: start.";
849 string cmd = "PCZero";
850 string fileName;
851 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_C, fileName);
852 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
853 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
854 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
855 FAIL();
856 }
857 EXPECT_TRUE(CheckCountNumPCZero(fileName, pid)) << "FaultLoggerdSystemTest030 Failed";
858 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest030: end.";
859 }
860
861 /**
862 * @tc.name: FaultLoggerdSystemTest031
863 * @tc.desc: test C crasher application: MTCrash
864 * @tc.type: FUNC
865 */
866 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest031, TestSize.Level2)
867 {
868 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest031: start.";
869 string cmd = "MTCrash";
870 string fileName;
871 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_C, fileName, 2); // 2 : sleep 2s for waiting cppcrash file
872 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
873 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
874 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
875 FAIL();
876 }
877 EXPECT_TRUE(CheckCountNumMultiThread(fileName, pid)) << "FaultLoggerdSystemTest031 Failed";
878 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest031: end.";
879 }
880
881 /**
882 * @tc.name: FaultLoggerdSystemTest032
883 * @tc.desc: test CPP crasher application: MTCrash
884 * @tc.type: FUNC
885 */
886 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest032, TestSize.Level2)
887 {
888 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest032: start.";
889 string cmd = "MTCrash";
890 string fileName;
891 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_CPP, fileName, 2); // 2 : sleep 2s for waiting cppcrash file
892 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
893 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
894 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
895 FAIL();
896 }
897 EXPECT_TRUE(CheckCountNumMultiThread(fileName, pid)) << "FaultLoggerdSystemTest032 Failed";
898 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest032: end.";
899 }
900
901 /**
902 * @tc.name: FaultLoggerdSystemTest033
903 * @tc.desc: test CPP crasher application: StackOver64
904 * @tc.type: FUNC
905 */
906 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest033, TestSize.Level2)
907 {
908 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest033: start.";
909 string cmd = "StackOver64";
910 string fileName;
911 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_CPP, fileName);
912 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
913 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
914 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
915 FAIL();
916 }
917 EXPECT_TRUE(CheckCountNumOverStack(fileName, pid)) << "FaultLoggerdSystemTest033 Failed";
918 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest033: end.";
919 }
920
921 /**
922 * @tc.name: FaultLoggerdSystemTest034
923 * @tc.desc: test C crasher application: StackOver64
924 * @tc.type: FUNC
925 */
926 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest034, TestSize.Level2)
927 {
928 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest034: start.";
929 string cmd = "StackOver64";
930 string fileName;
931 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_C, fileName);
932 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
933 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
934 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
935 FAIL();
936 }
937 EXPECT_TRUE(CheckCountNumOverStack(fileName, pid)) << "FaultLoggerdSystemTest034 Failed";
938 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest034: end.";
939 }
940
941 /**
942 * @tc.name: FaultLoggerdSystemTest035
943 * @tc.desc: test C crasher application: StackTop
944 * @tc.type: FUNC
945 */
946 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest035, TestSize.Level2)
947 {
948 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest035: start.";
949 string cmd = "StackTop";
950 string fileName;
951 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_C, fileName);
952 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
953 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
954 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
955 FAIL();
956 }
957 EXPECT_TRUE(CheckCountNumStackTop(fileName, pid)) << "FaultLoggerdSystemTest035 Failed";
958 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest035: end.";
959 }
960
961 /**
962 * @tc.name: FaultLoggerdSystemTest036
963 * @tc.desc: test CPP crasher application: StackTop
964 * @tc.type: FUNC
965 */
966 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest036, TestSize.Level2)
967 {
968 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest036: start.";
969 string cmd = "StackTop";
970 string fileName;
971 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_CPP, fileName);
972 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
973 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
974 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
975 FAIL();
976 }
977 EXPECT_TRUE(CheckCountNumStackTop(fileName, pid)) << "FaultLoggerdSystemTest036 Failed";
978 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest036: end.";
979 }
980
981 /**
982 * @tc.name: FaultLoggerdSystemTest101
983 * @tc.desc: test C crasher application: 50 Abnormal signal
984 * @tc.type: FUNC
985 */
986 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest101, TestSize.Level2)
987 {
988 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest0009: start.";
989 string clearTempFilesCmd = "rm -rf /data/log/faultlog/temp/*";
990 system(clearTempFilesCmd.c_str());
991 for (int i = 0; i < SIGNAL_TEST_NUM; i++) {
992 system("/data/crasher_c CrashTest &");
993 }
994 sleep(10); // 10 : sleep for 10 seconds
995 vector<string> files;
996 OHOS::GetDirFiles("/data/log/faultlog/temp/", files);
997 GTEST_LOG_(INFO) << files.size();
998 EXPECT_EQ(files.size(), SIGNAL_TEST_NUM) << "FaultLoggerdSystemTest101 Failed";
999 }
1000
CrashInChildThread()1001 static void CrashInChildThread()
1002 {
1003 GTEST_LOG_(INFO) << "CrashInChildThread(): TID = " << gettid();
1004 raise(SIGSEGV);
1005 }
1006
RunInNewPidNs(void * arg)1007 static int RunInNewPidNs(void* arg)
1008 {
1009 (void)arg;
1010 struct ProcInfo g_nsProcInfo;
1011 GetProcStatus(g_nsProcInfo);
1012 WriteRealPid(g_nsProcInfo.pid);
1013 GTEST_LOG_(INFO) << "RunInNewPidNs(): real pid = " << g_nsProcInfo.pid;
1014 GTEST_LOG_(INFO) << "RunInNewPidNs(): PID = " << getpid();
1015 GTEST_LOG_(INFO) << "RunInNewPidNs(): TID = " << gettid();
1016 thread childThread(CrashInChildThread);
1017 childThread.join();
1018 _exit(0);
1019 }
1020
1021 /**
1022 * @tc.name: FaultLoggerdSystemTest102
1023 * @tc.desc: test crash in process with pid namespace
1024 * @tc.type: FUNC
1025 */
1026 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest102, TestSize.Level2)
1027 {
1028 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest102: start.";
1029 const int stackSz = 1024 * 1024 * 1024; // 1M
1030 void* cloneStack = mmap(nullptr, stackSz,
1031 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, 1, 0);
1032 if (cloneStack == nullptr) {
1033 FAIL();
1034 }
1035 cloneStack = static_cast<void *>(static_cast<uint8_t *>(cloneStack) + stackSz - 1);
1036 int childPid = clone(RunInNewPidNs, cloneStack, CLONE_NEWPID | SIGCHLD, nullptr);
1037 if (childPid <= 0) {
1038 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest102: Failed to clone new process. errno:" << errno;
1039 return;
1040 }
1041 // wait for log generation
1042 sleep(4); // 4 : sleep 4s
1043 int readPid = ReadRealPid();
1044 string fileName = GetCppCrashFileName(readPid);
1045 EXPECT_NE(0, fileName.size());
1046 printf("PidNs Crash File:%s\n", fileName.c_str());
1047 string log[] = {
1048 "Pid:", "Uid", "SIGSEGV", "Tid:", "#00",
1049 "Registers:", REGISTERS, "FaultStack:", "Maps:"
1050 };
1051 int minRegIdx = 5; // 5 : index of first REGISTERS - 1
1052 int expectNum = sizeof(log) / sizeof(log[0]);
1053 int count = CheckKeyWords(fileName, log, expectNum, minRegIdx);
1054 EXPECT_EQ(count, expectNum);
1055 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest102: end.";
1056 }
1057
1058 /**
1059 * @tc.name: FaultLoggerdSystemTest103
1060 * @tc.desc: test the aging mechanism of the temp directory
1061 * @tc.type: FUNC
1062 */
1063 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest103, TestSize.Level2)
1064 {
1065 string clearTempFilesCmd = "rm -rf /data/log/faultlog/temp/*";
1066 system(clearTempFilesCmd.c_str());
1067 for (int i = 0; i < 25; i++) { // 25 : the count of crash file
1068 system("/data/crasher_c SIGSEGV");
1069 }
1070 for (int i = 0; i < 25; i++) { // 25 : the count of crash file
1071 sleep(3); //3 : sleep for 3 seconds
1072 system("/data/crasher_c SIGSEGV");
1073 }
1074 vector<string> files;
1075 OHOS::GetDirFiles("/data/log/faultlog/temp/", files);
1076 int fileCount = files.size();
1077 GTEST_LOG_(INFO) << fileCount;
1078 system("/data/crasher_c SIGSEGV"); // trigger aging mechanism
1079 sleep(1); // 1 : sleep for 1 seconds
1080 files.clear();
1081 OHOS::GetDirFiles("/data/log/faultlog/temp/", files);
1082 GTEST_LOG_(INFO) << files.size();
1083 EXPECT_GT(fileCount, files.size()) << "FaultLoggerdSystemTest103 Failed";
1084 }
1085
1086 /**
1087 * @tc.name: FaultLoggerdSystemTest0104
1088 * @tc.desc: test crash log build-id
1089 * @tc.type: FUNC
1090 */
1091 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest104, TestSize.Level2)
1092 {
1093 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest104: start.";
1094 string cmd = "SIGSEGV";
1095 string fileName;
1096 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_C, fileName);
1097 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
1098 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
1099 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
1100 FAIL();
1101 }
1102 std::ifstream file;
1103 file.open(fileName.c_str(), std::ios::in);
1104 while (!file.eof()) {
1105 string s;
1106 file >> s;
1107 if (s.find("/data/crasher_c") != string::npos) {
1108 string buildId;
1109 size_t leftBraceIdx = s.find('(');
1110 size_t rightBraceIdx = s.find(')');
1111 if (leftBraceIdx != string::npos && rightBraceIdx != string::npos) {
1112 buildId = s.substr(leftBraceIdx + 1, rightBraceIdx - leftBraceIdx - 1);
1113 GTEST_LOG_(INFO) << "build-id = " << buildId;
1114 }
1115 EXPECT_FALSE(buildId.empty()) << "FaultLoggerdSystemTest104 Failed";
1116 break;
1117 }
1118
1119 }
1120 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest104: end.";
1121 }
1122
1123 /**
1124 * @tc.name: FaultLoggerdSystemTest105
1125 * @tc.desc: test C crasher application: SIGABRT, and check all label keywords
1126 * @tc.type: FUNC
1127 */
1128 HWTEST_F(FaultLoggerdSystemTest, FaultLoggerdSystemTest105, TestSize.Level2)
1129 {
1130 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest105: start.";
1131 string cmd = "SIGABRT";
1132 string fileName;
1133 pid_t pid = TriggerCrasherAndGetFileName(cmd, CRASHER_C, fileName);
1134 GTEST_LOG_(INFO) << "test pid(" << pid << ")" << " cppcrash file name : " << fileName;
1135 if (pid < 0 || fileName.size() < CPPCRASH_FILENAME_MIN_LENGTH) {
1136 GTEST_LOG_(ERROR) << "Trigger Crash Failed.";
1137 FAIL();
1138 }
1139 EXPECT_TRUE(CheckCppCrashAllLabelKeywords(fileName, pid)) << "FaultLoggerdSystemTest105 Failed";
1140 GTEST_LOG_(INFO) << "FaultLoggerdSystemTest105: end.";
1141 }
1142 } // namespace HiviewDFX
1143 } // namespace OHOS