1 /*
2 * Copyright (c) 2022-2024 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 <string>
19 #include <thread>
20 #include <vector>
21
22 #include <unistd.h>
23
24 #include "dfx_define.h"
25 #include "dfx_dump_catcher.h"
26 #include "dfx_json_formatter.h"
27 #include "dfx_test_util.h"
28 #include "faultloggerd_client.h"
29 #include "procinfo.h"
30
31 using namespace testing;
32 using namespace testing::ext;
33
34 namespace OHOS {
35 namespace HiviewDFX {
36 class DumpCatcherInterfacesTest : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 void SetUp();
41 void TearDown();
42 };
43
44 static const int THREAD_ALIVE_TIME = 2;
45
46 static const int CREATE_THREAD_TIMEOUT = 300000;
47
48 static pid_t g_threadId = 0;
49
50 static pid_t g_processId = 0;
51
52 int g_testPid = 0;
53
SetUpTestCase()54 void DumpCatcherInterfacesTest::SetUpTestCase()
55 {
56 InstallTestHap("/data/FaultloggerdJsTest.hap");
57 std::string testBundleName = TEST_BUNDLE_NAME;
58 std::string testAbiltyName = testBundleName + ".MainAbility";
59 g_testPid = LaunchTestHap(testAbiltyName, testBundleName);
60 }
61
TearDownTestCase()62 void DumpCatcherInterfacesTest::TearDownTestCase()
63 {
64 StopTestHap(TEST_BUNDLE_NAME);
65 UninstallTestHap(TEST_BUNDLE_NAME);
66 }
67
SetUp()68 void DumpCatcherInterfacesTest::SetUp()
69 {}
70
TearDown()71 void DumpCatcherInterfacesTest::TearDown()
72 {}
73
TestFunRecursive(int recursiveCount)74 static void TestFunRecursive(int recursiveCount)
75 {
76 GTEST_LOG_(INFO) << "Enter TestFunRecursive recursiveCount:" << recursiveCount;
77 if (recursiveCount <= 0) {
78 GTEST_LOG_(INFO) << "start enter sleep" << gettid();
79 sleep(THREAD_ALIVE_TIME);
80 GTEST_LOG_(INFO) << "sleep end.";
81 } else {
82 TestFunRecursive(recursiveCount - 1);
83 }
84 }
85
CreateRecursiveThread(void * argv)86 static void* CreateRecursiveThread(void *argv)
87 {
88 g_threadId = gettid();
89 GTEST_LOG_(INFO) << "create Recursive MultiThread " << gettid();
90 TestFunRecursive(266); // 266: set recursive count to 266, used for dumpcatcher get stack info
91 GTEST_LOG_(INFO) << "Recursive MultiThread thread sleep end.";
92 return nullptr;
93 }
94
RecursiveMultiThreadConstructor(void)95 static int RecursiveMultiThreadConstructor(void)
96 {
97 pthread_t thread;
98 pthread_create(&thread, nullptr, CreateRecursiveThread, nullptr);
99 pthread_detach(thread);
100 usleep(CREATE_THREAD_TIMEOUT);
101 return 0;
102 }
103
CreateThread(void * argv)104 static void* CreateThread(void *argv)
105 {
106 g_threadId = gettid();
107 GTEST_LOG_(INFO) << "create MultiThread " << gettid();
108 sleep(THREAD_ALIVE_TIME);
109 GTEST_LOG_(INFO) << "create MultiThread thread sleep end.";
110 return nullptr;
111 }
112
MultiThreadConstructor(void)113 static int MultiThreadConstructor(void)
114 {
115 pthread_t thread;
116 pthread_create(&thread, nullptr, CreateThread, nullptr);
117 pthread_detach(thread);
118 usleep(CREATE_THREAD_TIMEOUT);
119 return 0;
120 }
121
ForkMultiThreadProcess(void)122 static void ForkMultiThreadProcess(void)
123 {
124 int pid = fork();
125 if (pid == 0) {
126 MultiThreadConstructor();
127 _exit(0);
128 } else if (pid < 0) {
129 GTEST_LOG_(INFO) << "ForkMultiThreadProcess fail. ";
130 } else {
131 g_processId = pid;
132 GTEST_LOG_(INFO) << "ForkMultiThreadProcess success, pid: " << pid;
133 }
134 }
135
136 /**
137 * @tc.name: DumpCatcherInterfacesTest001
138 * @tc.desc: test DumpCatchMultiPid API: multiPid{PID(accountmgr), PID(foundation)}
139 * @tc.type: FUNC
140 */
141 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest001, TestSize.Level2)
142 {
143 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest001: start.";
144 std::string testProcess1 = "accountmgr";
145 int testPid1 = GetProcessPid(testProcess1);
146 GTEST_LOG_(INFO) << "testPid1:" << testPid1;
147 std::string testProcess2 = "foundation";
148 int testPid2 = GetProcessPid(testProcess2);
149 GTEST_LOG_(INFO) << "testPid2:" << testPid2;
150 std::vector<int> multiPid {testPid1, testPid2};
151 DfxDumpCatcher dumplog;
152 std::string msg = "";
153 bool ret = dumplog.DumpCatchMultiPid(multiPid, msg);
154 GTEST_LOG_(INFO) << ret;
155 string log[] = {"Tid:", "Name:", "Tid:", "Name:"};
156 log[0] = log[0] + std::to_string(testPid1);
157 log[1] = log[1] + testProcess1;
158 log[2] = log[2] + std::to_string(testPid2);
159 log[3] = log[3] + testProcess2;
160 int len = sizeof(log) / sizeof(log[0]);
161 int count = GetKeywordsNum(msg, log, len);
162 EXPECT_EQ(count, len) << msg << "DumpCatcherInterfacesTest001 Failed";
163 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest001: end.";
164 }
165
166 /**
167 * @tc.name: DumpCatcherInterfacesTest002
168 * @tc.desc: test DumpCatchMultiPid API: multiPid{0, 0}
169 * @tc.type: FUNC
170 */
171 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest002, TestSize.Level2)
172 {
173 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest002: start.";
174 int testPid1 = 0;
175 GTEST_LOG_(INFO) << "testPid1:" << testPid1;
176 int testPid2 = 0;
177 GTEST_LOG_(INFO) << "testPid2:" << testPid2;
178 std::vector<int> multiPid {testPid1, testPid2};
179 DfxDumpCatcher dumplog;
180 std::string msg = "";
181 bool ret = dumplog.DumpCatchMultiPid(multiPid, msg);
182 GTEST_LOG_(INFO) << ret;
183 EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest002 Failed";
184 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest002: end.";
185 }
186
187 /**
188 * @tc.name: DumpCatcherInterfacesTest003
189 * @tc.desc: test DumpCatchMultiPid API: multiPid{-11, -11}
190 * @tc.type: FUNC
191 */
192 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest003, TestSize.Level2)
193 {
194 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest003: start.";
195 int testPid1 = -11;
196 GTEST_LOG_(INFO) << "testPid1:" << testPid1;
197 int testPid2 = -11;
198 GTEST_LOG_(INFO) << "testPid2:" << testPid2;
199 std::vector<int> multiPid {testPid1, testPid2};
200 DfxDumpCatcher dumplog;
201 std::string msg = "";
202 bool ret = dumplog.DumpCatchMultiPid(multiPid, msg);
203 GTEST_LOG_(INFO) << ret;
204 EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest003 Failed";
205 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest003: end.";
206 }
207
208 /**
209 * @tc.name: DumpCatcherInterfacesTest004
210 * @tc.desc: test DumpCatchMultiPid API: multiPid{PID(accountmgr), 0}
211 * @tc.type: FUNC
212 */
213 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest004, TestSize.Level2)
214 {
215 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest004: start.";
216 std::string testProcess = "accountmgr";
217 int applyPid1 = GetProcessPid(testProcess);
218 GTEST_LOG_(INFO) << "applyPid1:" << applyPid1;
219 int applyPid2 = 0;
220 GTEST_LOG_(INFO) << "applyPid2:" << applyPid2;
221 std::vector<int> multiPid {applyPid1, applyPid2};
222 DfxDumpCatcher dumplog;
223 std::string msg = "";
224 bool ret = dumplog.DumpCatchMultiPid(multiPid, msg);
225 GTEST_LOG_(INFO) << ret;
226 string log[] = { "Tid:", "Name:", "Failed" };
227 log[0] = log[0] + std::to_string(applyPid1);
228 log[1] = log[1] + "accountmgr";
229 int len = sizeof(log) / sizeof(log[0]);
230 int count = GetKeywordsNum(msg, log, len);
231 EXPECT_EQ(count, len) << msg << "DumpCatcherInterfacesTest004 Failed";
232 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest004: end.";
233 }
234
235 /**
236 * @tc.name: DumpCatcherInterfacesTest005
237 * @tc.desc: test DumpCatchMultiPid API: multiPid{PID(accountmgr),PID(foundation),PID(systemui)}
238 * @tc.type: FUNC
239 */
240 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest005, TestSize.Level2)
241 {
242 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest005: start.";
243 std::vector<string> testProcessName = { "accountmgr", "foundation", "com.ohos.systemui" };
244 string matchProcessName[] = { "accountmgr", "foundation", "m.ohos.systemui" };
245 std::vector<int> multiPid;
246 std::vector<string> matchLog;
247 int index = 0;
248 for (string oneProcessName : testProcessName) {
249 int testPid = GetProcessPid(oneProcessName);
250 if (testPid == 0) {
251 GTEST_LOG_(INFO) << "process:" << oneProcessName << " pid is empty, skip";
252 index++;
253 continue;
254 }
255 multiPid.emplace_back(testPid);
256 matchLog.emplace_back("Tid:" + std::to_string(testPid));
257 matchLog.emplace_back("Name:" + matchProcessName[index]);
258 index++;
259 }
260
261 // It is recommended that the number of effective pids be greater than 1,
262 // otherwise the testing purpose will not be achieved
263 EXPECT_GT(multiPid.size(), 1) << "DumpCatcherInterfacesTest005 Failed";
264
265 DfxDumpCatcher dumplog;
266 std::string msg = "";
267 bool ret = dumplog.DumpCatchMultiPid(multiPid, msg);
268 GTEST_LOG_(INFO) << "ret:" << ret;
269
270 int matchLogCount = matchLog.size();
271 auto matchLogArray = std::make_unique<string[]>(matchLogCount);
272 index = 0;
273 for (string info : matchLog) {
274 matchLogArray[index] = info;
275 index++;
276 }
277 int count = GetKeywordsNum(msg, matchLogArray.get(), matchLogCount);
278 EXPECT_EQ(count, matchLogCount) << msg << "DumpCatcherInterfacesTest005 Failed";
279 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest005: end.";
280 }
281
282 /**
283 * @tc.name: DumpCatcherInterfacesTest006
284 * @tc.desc: test DumpCatchMultiPid API: multiPid{PID(accountmgr), -11}
285 * @tc.type: FUNC
286 */
287 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest006, TestSize.Level2)
288 {
289 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest006: start.";
290 std::string testProcess = "accountmgr";
291 int testPid1 = GetProcessPid(testProcess);
292 GTEST_LOG_(INFO) << "applyPid1:" << testPid1;
293 int testPid2 = -11;
294 GTEST_LOG_(INFO) << "applyPid2:" << testPid2;
295 std::vector<int> multiPid {testPid1, testPid2};
296 DfxDumpCatcher dumplog;
297 std::string msg = "";
298 bool ret = dumplog.DumpCatchMultiPid(multiPid, msg);
299 GTEST_LOG_(INFO) << ret;
300 string log[] = { "Tid:", "Name:", "Failed"};
301 log[0] = log[0] + std::to_string(testPid1);
302 log[1] = log[1] + "accountmgr";
303 int len = sizeof(log) / sizeof(log[0]);
304 int count = GetKeywordsNum(msg, log, len);
305 EXPECT_EQ(count, len) << msg << "DumpCatcherInterfacesTest006 Failed";
306 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest006: end.";
307 }
308
309 /**
310 * @tc.name: DumpCatcherInterfacesTest007
311 * @tc.desc: test DumpCatchMultiPid API: multiPid{9999, 9999}
312 * @tc.type: FUNC
313 */
314 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest007, TestSize.Level2)
315 {
316 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest007: start.";
317 int applyPid = 9999;
318 GTEST_LOG_(INFO) << "applyPid1:" << applyPid;
319 std::vector<int> multiPid {applyPid, applyPid};
320 DfxDumpCatcher dumplog;
321 std::string msg = "";
322 bool ret = dumplog.DumpCatchMultiPid(multiPid, msg);
323 GTEST_LOG_(INFO) << ret;
324 EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest007 Failed";
325 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest007: end.";
326 }
327
328 /**
329 * @tc.name: DumpCatcherInterfacesTest008
330 * @tc.desc: test DumpCatchMultiPid API: multiPid{PID(accountmgr), 9999}
331 * @tc.type: FUNC
332 */
333 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest008, TestSize.Level2)
334 {
335 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest008: start.";
336 std::string apply = "accountmgr";
337 int applyPid1 = GetProcessPid(apply);
338 GTEST_LOG_(INFO) << "applyPid1:" << applyPid1;
339 int applyPid2 = 9999;
340 GTEST_LOG_(INFO) << "applyPid2:" << applyPid2;
341 std::vector<int> multiPid {applyPid1, applyPid2};
342 DfxDumpCatcher dumplog;
343 std::string msg = "";
344 bool ret = dumplog.DumpCatchMultiPid(multiPid, msg);
345 GTEST_LOG_(INFO) << ret;
346 string log[] = { "Tid:", "Name:", "Failed"};
347 log[0] = log[0] + std::to_string(applyPid1);
348 log[1] = log[1] + apply;
349 int len = sizeof(log) / sizeof(log[0]);
350 int count = GetKeywordsNum(msg, log, len);
351 EXPECT_EQ(count, len) << msg << "DumpCatcherInterfacesTest008 Failed";
352 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest008: end.";
353 }
354
355 /**
356 * @tc.name: DumpCatcherInterfacesTest014
357 * @tc.desc: test DumpCatch API: PID(test hap), TID(0)
358 * @tc.type: FUNC
359 * @tc.require: issueI5PJ9O
360 */
361 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest014, TestSize.Level2)
362 {
363 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest014: start.";
364 bool isSuccess = g_testPid != 0;
365 if (!isSuccess) {
366 ASSERT_FALSE(isSuccess);
367 GTEST_LOG_(ERROR) << "Failed to launch target hap.";
368 return;
369 }
370 isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME);
371 if (!isSuccess) {
372 ASSERT_FALSE(isSuccess);
373 GTEST_LOG_(ERROR) << "Error process comm";
374 return;
375 }
376 DfxDumpCatcher dumplog;
377 std::string msg = "";
378 bool ret = dumplog.DumpCatch(g_testPid, 0, msg);
379 GTEST_LOG_(INFO) << ret;
380 #if defined(__aarch64__)
381 string log[] = { "Tid:", "Name:", "#00", "/system/bin/appspawn", "Name:OS_DfxWatchdog",
382 "at jsFunc", "index_.js"};
383 #else
384 string log[] = { "Tid:", "Name:", "#00", "/system/bin/appspawn", "Name:OS_DfxWatchdog"};
385 #endif
386 log[0] += std::to_string(g_testPid);
387 log[1] += TRUNCATE_TEST_BUNDLE_NAME;
388 int len = sizeof(log) / sizeof(log[0]);
389 int count = GetKeywordsNum(msg, log, len);
390 EXPECT_EQ(count, len) << msg << "DumpCatcherInterfacesTest014 Failed";
391 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest014: end.";
392 }
393
394 /**
395 * @tc.name: DumpCatcherInterfacesTest015
396 * @tc.desc: test DumpCatch API: PID(test hap), TID(test hap main thread)
397 * @tc.type: FUNC
398 * @tc.require: issueI5PJ9O
399 */
400 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest015, TestSize.Level2)
401 {
402 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest015: start.";
403 bool isSuccess = g_testPid != 0;
404 if (!isSuccess) {
405 ASSERT_FALSE(isSuccess);
406 GTEST_LOG_(ERROR) << "Failed to launch target hap.";
407 return;
408 }
409 isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME);
410 if (!isSuccess) {
411 ASSERT_FALSE(isSuccess);
412 GTEST_LOG_(ERROR) << "Error process comm";
413 return;
414 }
415 DfxDumpCatcher dumplog;
416 std::string msg = "";
417 bool ret = dumplog.DumpCatch(g_testPid, g_testPid, msg);
418 GTEST_LOG_(INFO) << ret;
419 string log[] = { "Tid:", "Name:", "#00", "/system/bin/appspawn"};
420 log[0] += std::to_string(g_testPid);
421 log[1] += TRUNCATE_TEST_BUNDLE_NAME;
422 int len = sizeof(log) / sizeof(log[0]);
423 int count = GetKeywordsNum(msg, log, len);
424 GTEST_LOG_(INFO) << msg;
425 EXPECT_EQ(count, len) << msg << "DumpCatcherInterfacesTest015 Failed";
426 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest015: end.";
427 }
428
429 /**
430 * @tc.name: DumpCatcherInterfacesTest016
431 * @tc.desc: test DumpCatch API: PID(test hap), TID(-1)
432 * @tc.type: FUNC
433 * @tc.require: issueI5PJ9O
434 */
435 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest016, TestSize.Level2)
436 {
437 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest016: start.";
438 bool isSuccess = g_testPid != 0;
439 if (!isSuccess) {
440 ASSERT_FALSE(isSuccess);
441 GTEST_LOG_(ERROR) << "Failed to launch target hap.";
442 return;
443 }
444 isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME);
445 if (!isSuccess) {
446 ASSERT_FALSE(isSuccess);
447 GTEST_LOG_(ERROR) << "Error process comm";
448 return;
449 }
450 DfxDumpCatcher dumplog;
451 std::string msg = "";
452 bool ret = dumplog.DumpCatch(g_testPid, -1, msg);
453 GTEST_LOG_(INFO) << ret;
454 EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest016 Failed";
455 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest016: end.";
456 }
457
458 /**
459 * @tc.name: DumpCatcherInterfacesTest017
460 * @tc.desc: test DumpCatch API: PID(-1), TID(-1)
461 * @tc.type: FUNC
462 * @tc.require: issueI5PJ9O
463 */
464 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest017, TestSize.Level2)
465 {
466 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest017: start.";
467 DfxDumpCatcher dumplog;
468 std::string msg = "";
469 bool ret = dumplog.DumpCatch(-1, -1, msg);
470 GTEST_LOG_(INFO) << ret;
471 EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest017 Failed";
472 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest017: end.";
473 }
474
475 /**
476 * @tc.name: DumpCatcherInterfacesTest018
477 * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(gettid())
478 * @tc.type: FUNC
479 */
480 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest018, TestSize.Level2)
481 {
482 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest018: start.";
483 DfxDumpCatcher dumplog;
484 std::string msg = "";
485 bool ret = dumplog.DumpCatchFd(getpid(), gettid(), msg, 1);
486 GTEST_LOG_(INFO) << ret;
487 EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest018 Failed";
488 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest018: end.";
489 }
490
491 /**
492 * @tc.name: DumpCatcherInterfacesTest019
493 * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(0)
494 * @tc.type: FUNC
495 */
496 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest019, TestSize.Level2)
497 {
498 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest019: start.";
499 DfxDumpCatcher dumplog;
500 std::string msg = "";
501 bool ret = dumplog.DumpCatchFd(getpid(), 0, msg, 1);
502 GTEST_LOG_(INFO) << ret;
503 EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest019 Failed";
504 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest019: end.";
505 }
506
507 /**
508 * @tc.name: DumpCatcherInterfacesTest020
509 * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(-1)
510 * @tc.type: FUNC
511 */
512 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest020, TestSize.Level2)
513 {
514 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest020: start.";
515 DfxDumpCatcher dumplog;
516 std::string msg = "";
517 bool ret = dumplog.DumpCatchFd(getpid(), -1, msg, 1);
518 GTEST_LOG_(INFO) << ret;
519 EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest020 Failed";
520 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest020: end.";
521 }
522
523
524 /**
525 * @tc.name: DumpCatcherInterfacesTest021
526 * @tc.desc: test DumpCatchFd API: PID(accountmgr), TID(0)
527 * @tc.type: FUNC
528 */
529 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest021, TestSize.Level2)
530 {
531 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest021: start.";
532 std::string apply = "accountmgr";
533 int applyPid = GetProcessPid(apply);
534 GTEST_LOG_(INFO) << "apply:" << apply << ", pid:" << applyPid;
535 DfxDumpCatcher dumplog;
536 std::string msg = "";
537 bool ret = dumplog.DumpCatchFd(applyPid, 0, msg, 1);
538 GTEST_LOG_(INFO) << ret;
539 EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest021 Failed";
540 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest021: end.";
541 }
542
543 /**
544 * @tc.name: DumpCatcherInterfacesTest022
545 * @tc.desc: test DumpCatchFd API: PID(accountmgr), TID(accountmgr main thread)
546 * @tc.type: FUNC
547 */
548 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest022, TestSize.Level2)
549 {
550 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest022: start.";
551 std::string apply = "accountmgr";
552 int applyPid = GetProcessPid(apply);
553 GTEST_LOG_(INFO) << "apply:" << apply << ", pid:" << applyPid;
554 DfxDumpCatcher dumplog;
555 std::string msg = "";
556 bool ret = dumplog.DumpCatchFd(applyPid, applyPid, msg, 1);
557 GTEST_LOG_(INFO) << ret;
558 EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest022 Failed";
559 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest022: end.";
560 }
561
562 /**
563 * @tc.name: DumpCatcherInterfacesTest023
564 * @tc.desc: test DumpCatchFd API: PID(accountmgr), TID(-1)
565 * @tc.type: FUNC
566 */
567 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest023, TestSize.Level2)
568 {
569 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest023: start.";
570 std::string apply = "accountmgr";
571 int applyPid = GetProcessPid(apply);
572 GTEST_LOG_(INFO) << "apply:" << apply << ", pid:" << applyPid;
573 DfxDumpCatcher dumplog;
574 std::string msg = "";
575 bool ret = dumplog.DumpCatchFd(applyPid, -1, msg, 1);
576 GTEST_LOG_(INFO) << ret;
577 EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest023 Failed";
578 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest023: end.";
579 }
580
581 /**
582 * @tc.name: DumpCatcherInterfacesTest024
583 * @tc.desc: test DumpCatchFd API: PID(accountmgr), TID(9999)
584 * @tc.type: FUNC
585 */
586 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest024, TestSize.Level2)
587 {
588 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest024: start.";
589 std::string apply = "accountmgr";
590 int applyPid = GetProcessPid(apply);
591 GTEST_LOG_(INFO) << "apply:" << apply << ", pid:" << applyPid;
592 DfxDumpCatcher dumplog;
593 std::string msg = "";
594 bool ret = dumplog.DumpCatchFd(applyPid, 9999, msg, 1);
595 GTEST_LOG_(INFO) << ret;
596 EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest024 Failed";
597 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest024: end.";
598 }
599
600 /**
601 * @tc.name: DumpCatcherInterfacesTest025
602 * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(9999)
603 * @tc.type: FUNC
604 */
605 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest025, TestSize.Level2)
606 {
607 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest025: start.";
608 DfxDumpCatcher dumplog;
609 std::string msg = "";
610 bool ret = dumplog.DumpCatchFd(getpid(), 9999, msg, 1);
611 GTEST_LOG_(INFO) << ret;
612 EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest025 Failed";
613 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest025: end.";
614 }
615
616 /**
617 * @tc.name: DumpCatcherInterfacesTest026
618 * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(child thread)
619 * @tc.type: FUNC
620 */
621 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest026, TestSize.Level2)
622 {
623 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest026: start.";
624 MultiThreadConstructor();
625 DfxDumpCatcher dumplog;
626 std::string msg = "";
627 GTEST_LOG_(INFO) << "dump local process, " << " tid:" << g_threadId;
628 bool ret = dumplog.DumpCatchFd(getpid(), g_threadId, msg, 1);
629 GTEST_LOG_(INFO) << ret;
630 EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest026 Failed";
631 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest026: end.";
632 }
633
634 /**
635 * @tc.name: DumpCatcherInterfacesTest027
636 * @tc.desc: test DumpCatchFd API: PID(child process), TID(child thread of child process)
637 * @tc.type: FUNC
638 */
639 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest027, TestSize.Level2)
640 {
641 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest027: start.";
642 ForkMultiThreadProcess();
643 std::vector<int> tids;
644 std::vector<int> nstids;
645 bool isSuccess = GetTidsByPid(g_processId, tids, nstids);
646 if (!isSuccess) {
647 ASSERT_FALSE(isSuccess);
648 return;
649 }
650 int childTid = tids[1]; // 1 : child thread
651 GTEST_LOG_(INFO) << "dump remote process, " << " pid:" << g_processId << ", tid:" << childTid;
652 DfxDumpCatcher dumplog;
653 std::string msg = "";
654 bool ret = dumplog.DumpCatchFd(g_processId, childTid, msg, 1);
655 GTEST_LOG_(INFO) << ret;
656 EXPECT_TRUE(ret) << "DumpCatcherInterfacesTest027 Failed";
657 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest027: end.";
658 }
659
660 /**
661 * @tc.name: DumpCatcherInterfacesTest028
662 * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(child thread) and config FrameNum
663 * @tc.type: FUNC
664 */
665 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest028, TestSize.Level2)
666 {
667 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest028: start.";
668 RecursiveMultiThreadConstructor();
669 DfxDumpCatcher dumplog;
670 std::string msg = "";
671 GTEST_LOG_(INFO) << "dump local process, " << " tid:" << g_threadId;
672 bool ret = dumplog.DumpCatchFd(getpid(), g_threadId, msg, 1, 10); // 10 means backtrace frames is 10
673 GTEST_LOG_(INFO) << "message:" << msg;
674 GTEST_LOG_(INFO) << ret;
675 EXPECT_TRUE(msg.find("#09") != std::string::npos);
676 EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest028 Failed";
677 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest028: end.";
678 }
679
680 /**
681 * @tc.name: DumpCatcherInterfacesTest029
682 * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(child thread) and DEFAULT_MAX_FRAME_NUM
683 * @tc.type: FUNC
684 */
685 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest029, TestSize.Level2)
686 {
687 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest029: start.";
688 RecursiveMultiThreadConstructor();
689 usleep(CREATE_THREAD_TIMEOUT);
690 DfxDumpCatcher dumplog;
691 std::string msg = "";
692 GTEST_LOG_(INFO) << "dump local process, " << " tid:" << g_threadId;
693 bool ret = dumplog.DumpCatchFd(getpid(), g_threadId, msg, 1);
694 GTEST_LOG_(INFO) << "message:" << msg;
695 GTEST_LOG_(INFO) << ret;
696 #if (defined(__aarch64__) || defined(__loongarch_lp64))
697 std::string stackKeyword = std::string("#") + std::to_string(DEFAULT_MAX_LOCAL_FRAME_NUM - 1);
698 #else
699 std::string stackKeyword = std::string("#") + std::to_string(DEFAULT_MAX_FRAME_NUM - 1);
700 #endif
701 GTEST_LOG_(INFO) << "stackKeyword:" << stackKeyword;
702 EXPECT_TRUE(msg.find(stackKeyword.c_str()) != std::string::npos);
703 EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest029 Failed";
704 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest029: end.";
705 }
706
707 #ifndef is_ohos_lite
708 /**
709 * @tc.name: DumpCatcherInterfacesTest030
710 * @tc.desc: test DumpCatch remote API: PID(getpid()), TID(child thread)
711 * and maxFrameNums(DEFAULT_MAX_FRAME_NUM), isJson(true)
712 * @tc.type: FUNC
713 */
714 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest030, TestSize.Level2)
715 {
716 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest030: start.";
717 int fd[2];
718 EXPECT_TRUE(CreatePipeFd(fd));
719 pid_t pid = fork();
720 if (pid == 0) {
721 NotifyProcStart(fd);
722 std::this_thread::sleep_for(std::chrono::seconds(10));
723 _exit(0);
724 }
725 WaitProcStart(fd);
726 GTEST_LOG_(INFO) << "dump remote process, " << " pid:" << pid << ", tid:" << 0;
727 DfxDumpCatcher dumplog;
728 DfxJsonFormatter format;
729 string msg = "";
730 bool ret = dumplog.DumpCatch(pid, 0, msg);
731 EXPECT_TRUE(ret) << "DumpCatch remote msg Failed.";
732 string jsonMsg = "";
733 bool jsonRet = dumplog.DumpCatch(pid, 0, jsonMsg, DEFAULT_MAX_FRAME_NUM, true);
734 std::cout << jsonMsg << std::endl;
735 EXPECT_TRUE(jsonRet) << "DumpCatch remote json Failed.";
736 string stackMsg = "";
737 bool formatRet = format.FormatJsonStack(jsonMsg, stackMsg);
738 EXPECT_TRUE(formatRet) << "FormatJsonStack Failed.";
739 size_t pos = msg.find("Process name:");
740 if (pos != std::string::npos) {
741 msg = msg.erase(0, pos);
742 msg = msg.erase(0, msg.find("\n") + 1);
743 } else {
744 msg = msg.erase(0, msg.find("\n") + 1);
745 }
746 EXPECT_EQ(stackMsg == msg, true) << "stackMsg: " << stackMsg << "msg: " << msg << "stackMsg != msg";
747 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest030: end.";
748 }
749
750 /**
751 * @tc.name: DumpCatcherInterfacesTest031
752 * @tc.desc: test DumpCatchProcess get kenerl stack
753 * @tc.type: FUNC
754 */
755 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest031, TestSize.Level2)
756 {
757 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest031: start.";
758 std::string res = ExecuteCommands("uname");
759 bool isSuccess = res.find("Linux") == std::string::npos;
760 if (!isSuccess) {
761 ASSERT_FALSE(isSuccess);
762 return;
763 }
764 isSuccess = g_testPid != 0;
765 if (!isSuccess) {
766 ASSERT_FALSE(isSuccess);
767 GTEST_LOG_(ERROR) << "Failed to launch target hap.";
768 return;
769 }
770 isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME);
771 if (!isSuccess) {
772 ASSERT_FALSE(isSuccess);
773 GTEST_LOG_(ERROR) << "Error process comm";
774 return;
775 }
776 std::string stopProcessCmd = "kill -s SIGSTOP $(pidof com.example.myapplication)";
777 ExecuteCommands(stopProcessCmd);
778 DfxDumpCatcher dumplog;
779 std::string msg = "";
780 ASSERT_EQ(dumplog.DumpCatchProcess(g_testPid, msg), 1); //kernel stack
781 GTEST_LOG_(INFO) << msg;
782 std::string continueProcessCmd = "kill -s SIGCONT $(pidof com.example.myapplication)";
783 ExecuteCommands(continueProcessCmd);
784 std::string formattedStack = "";
785 ASSERT_TRUE(DfxJsonFormatter::FormatKernelStack(msg, formattedStack, false));
786 ASSERT_GT(formattedStack.size(), 0);
787 GTEST_LOG_(INFO) << formattedStack;
788 string log[] = { "Tid:", "Name:", "#00", "/system/bin/appspawn", "Name:OS_DfxWatchdog" };
789 log[0] += std::to_string(g_testPid);
790 log[1] += TRUNCATE_TEST_BUNDLE_NAME;
791 int len = sizeof(log) / sizeof(log[0]);
792 int count = GetKeywordsNum(formattedStack, log, len);
793 EXPECT_EQ(count, len) << formattedStack << "DumpCatcherInterfacesTest031 Failed";
794 ASSERT_TRUE(DfxJsonFormatter::FormatKernelStack(msg, formattedStack, true));
795 string logJson[] = { "\"tid\":", TRUNCATE_TEST_BUNDLE_NAME, "/system/bin/appspawn", "OS_DfxWatchdog" };
796 logJson[0] += std::to_string(g_testPid);
797 len = sizeof(logJson) / sizeof(logJson[0]);
798 count = GetKeywordsNum(formattedStack, logJson, len);
799 EXPECT_EQ(count, len) << formattedStack << "DumpCatcherInterfacesTest031 Failed";
800 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest031: end.";
801 }
802 #endif
803
804 #ifndef is_ohos_lite
805 /**
806 * @tc.name: DumpCatcherInterfacesTest032
807 * @tc.desc: test DfxJsonFormatter
808 * @tc.type: FUNC
809 */
810 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest032, TestSize.Level2)
811 {
812 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest032: start.";
813 DfxJsonFormatter format;
814 string outStackStr = "";
815 string errorJsonMsg = "{\"test\"}";
816 bool formatRet = format.FormatJsonStack(errorJsonMsg, outStackStr);
817 EXPECT_FALSE(formatRet);
818
819 outStackStr = "";
820 string noThreadJsonMsg = "[{\"tid\" : \"1\"}]";
821 formatRet = format.FormatJsonStack(noThreadJsonMsg, outStackStr);
822 EXPECT_TRUE(formatRet);
823
824 outStackStr = "";
825 string noTidJsonMsg = "[{\"thread_name\" : \"test\"}]";
826 formatRet = format.FormatJsonStack(noTidJsonMsg, outStackStr);
827 EXPECT_TRUE(formatRet);
828
829 outStackStr = "";
830 string jsJsonMsg = R"~([{"frames":[{"buildId":"", "file":"/system/lib/ld-musl-arm.so.1",
831 "offset":0, "pc":"000fdf4c", "symbol":""}, {"line":"1", "file":"/system/lib/ld-musl-arm.so.1",
832 "offset":628, "pc":"000ff7f4", "symbol":"__pthread_cond_timedwait_time64"}],
833 "thread_name":"OS_SignalHandle", "tid":1608}])~";
834 formatRet = format.FormatJsonStack(jsJsonMsg, outStackStr);
835 EXPECT_TRUE(formatRet);
836 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest032: end.";
837 }
838 #endif
839
840 /**
841 @tc.name: DumpCatcherInterfacesTest033
842 @tc.desc: testDump after crashed
843 @tc.type: FUNC
844 */
845 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest033, TestSize.Level2)
846 {
847 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest033: start.";
848 int32_t fd = RequestFileDescriptor(FaultLoggerType::CPP_CRASH);
849 ASSERT_GT(fd, 0);
850 close(fd);
851 pid_t pid = fork();
852 if (pid == 0) {
853 GTEST_LOG_(INFO) << "dump remote process, " << "pid:" << getppid() << ", tid:" << 0;
854 DfxDumpCatcher dumplog;
855 string msg = "";
856 EXPECT_FALSE(dumplog.DumpCatch(getppid(), 0, msg));
857 constexpr int validTime = 1;
858 sleep(validTime);
859 msg = "";
860 EXPECT_TRUE(dumplog.DumpCatch(getppid(), 0, msg));
861 _exit(0);
862 } else if (pid < 0) {
863 GTEST_LOG_(INFO) << "Fail in fork.";
864 } else {
865 waitpid(pid, nullptr, 0);
866 }
867 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest033: end.";
868 }
869
870 /**
871 * @tc.name: DumpCatcherInterfacesTest034
872 * @tc.desc: test DumpCatchProcess get user stack
873 * @tc.type: FUNC
874 */
875 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest034, TestSize.Level2)
876 {
877 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest034: start.";
878 bool isSuccess = g_testPid != 0;
879 if (!isSuccess) {
880 ASSERT_FALSE(isSuccess);
881 GTEST_LOG_(ERROR) << "Failed to launch target hap.";
882 return;
883 }
884 isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME);
885 if (!isSuccess) {
886 ASSERT_FALSE(isSuccess);
887 GTEST_LOG_(ERROR) << "Error process comm";
888 return;
889 }
890 DfxDumpCatcher dumplog;
891 std::string msg = "";
892 ASSERT_EQ(dumplog.DumpCatchProcess(g_testPid, msg), 0); //user stack
893 string log[] = { "Tid:", "Name:", "#00", "/system/bin/appspawn", "Name:OS_DfxWatchdog" };
894 log[0] += std::to_string(g_testPid);
895 log[1] += TRUNCATE_TEST_BUNDLE_NAME;
896 int len = sizeof(log) / sizeof(log[0]);
897 int count = GetKeywordsNum(msg, log, len);
898 EXPECT_EQ(count, len) << msg << "DumpCatcherInterfacesTest034 Failed";
899 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest034: end.";
900 }
901
902 /**
903 * @tc.name: DumpCatcherInterfacesTest035
904 * @tc.desc: test DumpCatchWithTimeout API: PID(test hap)
905 * @tc.type: FUNC
906 * @tc.require: IB1XY4
907 */
908 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest035, TestSize.Level2)
909 {
910 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest035: start.";
911 bool isSuccess = g_testPid != 0;
912 if (!isSuccess) {
913 ASSERT_FALSE(isSuccess);
914 GTEST_LOG_(ERROR) << "Failed to launch target hap.";
915 return;
916 }
917 isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME);
918 if (!isSuccess) {
919 ASSERT_FALSE(isSuccess);
920 GTEST_LOG_(ERROR) << "Error process comm";
921 return;
922 }
923 DfxDumpCatcher dumplog;
924 std::string msg = "";
925 auto result = dumplog.DumpCatchWithTimeout(g_testPid, msg);
926 GTEST_LOG_(INFO) << result.second;
927 EXPECT_TRUE(result.first == 0) << "DumpCatcherInterfacesTest035 Failed";
928 string log[] = { "Tid:", "Name:", "#00", "/system/bin/appspawn", "Name:OS_DfxWatchdog" };
929 log[0] += std::to_string(g_testPid);
930 log[1] += TRUNCATE_TEST_BUNDLE_NAME;
931 int len = sizeof(log) / sizeof(log[0]);
932 int count = GetKeywordsNum(msg, log, len);
933 EXPECT_EQ(count, len) << "DumpCatcherInterfacesTest035 Failed";
934 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest035: end.";
935 }
936
937 /**
938 * @tc.name: DumpCatcherInterfacesTest036
939 * @tc.desc: test DumpCatchWithTimeout API: PID(test hap), TIMEOUT(1000)
940 * @tc.type: FUNC
941 * @tc.require: IB1XY4
942 */
943 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest036, TestSize.Level2)
944 {
945 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest036: start.";
946 bool isSuccess = g_testPid != 0;
947 if (!isSuccess) {
948 ASSERT_FALSE(isSuccess);
949 GTEST_LOG_(ERROR) << "Failed to launch target hap.";
950 return;
951 }
952 isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME);
953 if (!isSuccess) {
954 ASSERT_FALSE(isSuccess);
955 GTEST_LOG_(ERROR) << "Error process comm";
956 return;
957 }
958 DfxDumpCatcher dumplog;
959 std::string msg = "";
960 int timeout = 1000;
961 auto result = dumplog.DumpCatchWithTimeout(g_testPid, msg, timeout);
962 GTEST_LOG_(INFO) << result.second;
963 EXPECT_TRUE(result.first == -1) << "DumpCatcherInterfacesTest036 Failed";
964 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest036: end.";
965 }
966
967 /**
968 * @tc.name: DumpCatcherInterfacesTest037
969 * @tc.desc: test DumpCatchWithTimeout API: PID(nonexistent)
970 * @tc.type: FUNC
971 * @tc.require: IB1XY4
972 */
973 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest037, TestSize.Level2)
974 {
975 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest037: start.";
976 bool isSuccess = g_testPid != 0;
977 if (!isSuccess) {
978 ASSERT_FALSE(isSuccess);
979 GTEST_LOG_(ERROR) << "Failed to launch target hap.";
980 return;
981 }
982 isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME);
983 if (!isSuccess) {
984 ASSERT_FALSE(isSuccess);
985 GTEST_LOG_(ERROR) << "Error process comm";
986 return;
987 }
988 DfxDumpCatcher dumplog;
989 std::string msg = "";
990 int nonexistPid = 123456;
991 auto result = dumplog.DumpCatchWithTimeout(nonexistPid, msg);
992 GTEST_LOG_(INFO) << result.second;
993 EXPECT_TRUE(result.first == -1) << "DumpCatcherInterfacesTest037 Failed";
994 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest037: end.";
995 }
996
997 /**
998 * @tc.name: DumpCatcherInterfacesTest038
999 * @tc.desc: test DumpCatchWithTimeout API: PID(test hap), TIMEOUT(2000)
1000 * @tc.type: FUNC
1001 * @tc.require: IB1XY4
1002 */
1003 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest038, TestSize.Level2)
1004 {
1005 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest038: start.";
1006 bool isSuccess = g_testPid != 0;
1007 if (!isSuccess) {
1008 ASSERT_FALSE(isSuccess);
1009 GTEST_LOG_(ERROR) << "Failed to launch target hap.";
1010 return;
1011 }
1012 isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME);
1013 if (!isSuccess) {
1014 ASSERT_FALSE(isSuccess);
1015 GTEST_LOG_(ERROR) << "Error process comm";
1016 return;
1017 }
1018 DfxDumpCatcher dumplog;
1019 std::string msg = "";
1020 int timeout = 2000;
1021 auto result = dumplog.DumpCatchWithTimeout(g_testPid, msg, timeout);
1022 GTEST_LOG_(INFO) << result.second;
1023 EXPECT_TRUE(result.first == 0) << "DumpCatcherInterfacesTest038 Failed";
1024 string log[] = { "Tid:", "Name:", "#00", "/system/bin/appspawn", "Name:OS_DfxWatchdog" };
1025 log[0] += std::to_string(g_testPid);
1026 log[1] += TRUNCATE_TEST_BUNDLE_NAME;
1027 int len = sizeof(log) / sizeof(log[0]);
1028 int count = GetKeywordsNum(msg, log, len);
1029 EXPECT_EQ(count, len) << "DumpCatcherInterfacesTest038 Failed";
1030 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest038: end.";
1031 }
1032
1033 /**
1034 * @tc.name: DumpCatcherInterfacesTest039
1035 * @tc.desc: test DumpCatchWithTimeout API: PID(test hap) and SIGSTOP the process
1036 * @tc.type: FUNC
1037 * @tc.require: IB1XY4
1038 */
1039 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest039, TestSize.Level2)
1040 {
1041 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest039: start.";
1042 std::string res = ExecuteCommands("uname");
1043 bool isSuccess = res.find("Linux") == std::string::npos;
1044 if (!isSuccess) {
1045 ASSERT_FALSE(isSuccess);
1046 return;
1047 }
1048 isSuccess = g_testPid != 0;
1049 if (!isSuccess) {
1050 ASSERT_FALSE(isSuccess);
1051 GTEST_LOG_(ERROR) << "Failed to launch target hap.";
1052 return;
1053 }
1054 isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME);
1055 if (!isSuccess) {
1056 ASSERT_FALSE(isSuccess);
1057 GTEST_LOG_(ERROR) << "Error process comm";
1058 return;
1059 }
1060 std::string stopProcessCmd = "kill -s SIGSTOP $(pidof com.example.myapplication)";
1061 ExecuteCommands(stopProcessCmd);
1062 DfxDumpCatcher dumplog;
1063 std::string msg = "";
1064 auto result = dumplog.DumpCatchWithTimeout(g_testPid, msg);
1065 std::string startProcessCmd = "kill -s SIGCONT $(pidof com.example.myapplication)";
1066 ExecuteCommands(startProcessCmd);
1067 GTEST_LOG_(INFO) << result.second;
1068 ASSERT_TRUE(result.first == 1);
1069
1070 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest039: end.";
1071 }
1072
1073 /**
1074 * @tc.name: DumpCatcherInterfacesTest040
1075 * @tc.desc: test DumpCatchWithTimeout API: PID(test hap) and stop the faultloggerd
1076 * @tc.type: FUNC
1077 * @tc.require: IB1XY4
1078 */
1079 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest040, TestSize.Level2)
1080 {
1081 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest040: start.";
1082 std::string res = ExecuteCommands("uname");
1083 bool isSuccess = res.find("Linux") == std::string::npos;
1084 if (!isSuccess) {
1085 ASSERT_FALSE(isSuccess);
1086 return;
1087 }
1088 isSuccess = g_testPid != 0;
1089 if (!isSuccess) {
1090 ASSERT_FALSE(isSuccess);
1091 GTEST_LOG_(ERROR) << "Failed to launch target hap.";
1092 return;
1093 }
1094 isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME);
1095 if (!isSuccess) {
1096 ASSERT_FALSE(isSuccess);
1097 GTEST_LOG_(ERROR) << "Error process comm";
1098 return;
1099 }
1100 DfxDumpCatcher dumplog;
1101 std::string msg = "";
1102 std::string stopFaultloggerdCmd = "service_control stop faultloggerd";
1103 ExecuteCommands(stopFaultloggerdCmd);
1104 auto result = dumplog.DumpCatchWithTimeout(g_testPid, msg);
1105 std::string startFaultloggerdCmd = "service_control start faultloggerd";
1106 ExecuteCommands(startFaultloggerdCmd);
1107 GTEST_LOG_(INFO) << result.second;
1108 EXPECT_TRUE(result.first == -1);
1109 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest040: end.";
1110 }
1111 } // namespace HiviewDFX
1112 } // namepsace OHOS
1113