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 DumpCatchMix 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     if (g_testPid == 0) {
365         GTEST_LOG_(ERROR) << "Failed to launch target hap.";
366         return;
367     }
368     if (!CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME)) {
369         GTEST_LOG_(ERROR) << "Error process comm";
370         return;
371     }
372     DfxDumpCatcher dumplog;
373     std::string msg = "";
374     bool ret = dumplog.DumpCatchMix(g_testPid, 0, msg);
375     GTEST_LOG_(INFO) << ret;
376     string log[] = { "Tid:", "Name:", "#00", "/system/bin/appspawn", "Name:OS_DfxWatchdog" };
377     log[0] += std::to_string(g_testPid);
378     log[1] += TRUNCATE_TEST_BUNDLE_NAME;
379     int len = sizeof(log) / sizeof(log[0]);
380     int count = GetKeywordsNum(msg, log, len);
381     EXPECT_EQ(count, len) << msg << "DumpCatcherInterfacesTest014 Failed";
382     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest014: end.";
383 }
384 
385 /**
386  * @tc.name: DumpCatcherInterfacesTest015
387  * @tc.desc: test DumpCatchMix API: PID(test hap), TID(test hap main thread)
388  * @tc.type: FUNC
389  * @tc.require: issueI5PJ9O
390  */
391 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest015, TestSize.Level2)
392 {
393     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest015: start.";
394     if (g_testPid == 0) {
395         GTEST_LOG_(ERROR) << "Failed to launch target hap.";
396         return;
397     }
398     if (!CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME)) {
399         GTEST_LOG_(ERROR) << "Error process comm";
400         return;
401     }
402     DfxDumpCatcher dumplog;
403     std::string msg = "";
404     bool ret = dumplog.DumpCatchMix(g_testPid, g_testPid, msg);
405     GTEST_LOG_(INFO) << ret;
406     string log[] = { "Tid:", "Name:", "#00", "/system/bin/appspawn"};
407     log[0] += std::to_string(g_testPid);
408     log[1] += TRUNCATE_TEST_BUNDLE_NAME;
409     int len = sizeof(log) / sizeof(log[0]);
410     int count = GetKeywordsNum(msg, log, len);
411     GTEST_LOG_(INFO) << msg;
412     EXPECT_EQ(count, len) << msg << "DumpCatcherInterfacesTest015 Failed";
413     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest015: end.";
414 }
415 
416 /**
417  * @tc.name: DumpCatcherInterfacesTest016
418  * @tc.desc: test DumpCatchMix API: PID(test hap), TID(-1)
419  * @tc.type: FUNC
420  * @tc.require: issueI5PJ9O
421  */
422 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest016, TestSize.Level2)
423 {
424     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest016: start.";
425     if (g_testPid == 0) {
426         GTEST_LOG_(ERROR) << "Failed to launch target hap.";
427         return;
428     }
429     if (!CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME)) {
430         GTEST_LOG_(ERROR) << "Error process comm";
431         return;
432     }
433     DfxDumpCatcher dumplog;
434     std::string msg = "";
435     bool ret = dumplog.DumpCatchMix(g_testPid, -1, msg);
436     GTEST_LOG_(INFO) << ret;
437     EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest016 Failed";
438     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest016: end.";
439 }
440 
441 /**
442  * @tc.name: DumpCatcherInterfacesTest017
443  * @tc.desc: test DumpCatchMix API: PID(-1), TID(-1)
444  * @tc.type: FUNC
445  * @tc.require: issueI5PJ9O
446  */
447 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest017, TestSize.Level2)
448 {
449     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest017: start.";
450     DfxDumpCatcher dumplog;
451     std::string msg = "";
452     bool ret = dumplog.DumpCatchMix(-1, -1, msg);
453     GTEST_LOG_(INFO) << ret;
454     EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest017 Failed";
455     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest017: end.";
456 }
457 
458 /**
459  * @tc.name: DumpCatcherInterfacesTest018
460  * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(gettid())
461  * @tc.type: FUNC
462  */
463 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest018, TestSize.Level2)
464 {
465     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest018: start.";
466     DfxDumpCatcher dumplog;
467     std::string msg = "";
468     bool ret = dumplog.DumpCatchFd(getpid(), gettid(), msg, 1);
469     GTEST_LOG_(INFO) << ret;
470     EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest018 Failed";
471     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest018: end.";
472 }
473 
474 /**
475  * @tc.name: DumpCatcherInterfacesTest019
476  * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(0)
477  * @tc.type: FUNC
478  */
479 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest019, TestSize.Level2)
480 {
481     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest019: start.";
482     DfxDumpCatcher dumplog;
483     std::string msg = "";
484     bool ret = dumplog.DumpCatchFd(getpid(), 0, msg, 1);
485     GTEST_LOG_(INFO) << ret;
486     EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest019 Failed";
487     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest019: end.";
488 }
489 
490 /**
491  * @tc.name: DumpCatcherInterfacesTest020
492  * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(-1)
493  * @tc.type: FUNC
494  */
495 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest020, TestSize.Level2)
496 {
497     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest020: start.";
498     DfxDumpCatcher dumplog;
499     std::string msg = "";
500     bool ret = dumplog.DumpCatchFd(getpid(), -1, msg, 1);
501     GTEST_LOG_(INFO) << ret;
502     EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest020 Failed";
503     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest020: end.";
504 }
505 
506 
507 /**
508  * @tc.name: DumpCatcherInterfacesTest021
509  * @tc.desc: test DumpCatchFd API: PID(accountmgr), TID(0)
510  * @tc.type: FUNC
511  */
512 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest021, TestSize.Level2)
513 {
514     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest021: start.";
515     std::string apply = "accountmgr";
516     int applyPid = GetProcessPid(apply);
517     GTEST_LOG_(INFO) << "apply:" << apply << ", pid:" << applyPid;
518     DfxDumpCatcher dumplog;
519     std::string msg = "";
520     bool ret = dumplog.DumpCatchFd(applyPid, 0, msg, 1);
521     GTEST_LOG_(INFO) << ret;
522     EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest021 Failed";
523     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest021: end.";
524 }
525 
526 /**
527  * @tc.name: DumpCatcherInterfacesTest022
528  * @tc.desc: test DumpCatchFd API: PID(accountmgr), TID(accountmgr main thread)
529  * @tc.type: FUNC
530  */
531 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest022, TestSize.Level2)
532 {
533     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest022: start.";
534     std::string apply = "accountmgr";
535     int applyPid = GetProcessPid(apply);
536     GTEST_LOG_(INFO) << "apply:" << apply << ", pid:" << applyPid;
537     DfxDumpCatcher dumplog;
538     std::string msg = "";
539     bool ret = dumplog.DumpCatchFd(applyPid, applyPid, msg, 1);
540     GTEST_LOG_(INFO) << ret;
541     EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest022 Failed";
542     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest022: end.";
543 }
544 
545 /**
546  * @tc.name: DumpCatcherInterfacesTest023
547  * @tc.desc: test DumpCatchFd API: PID(accountmgr), TID(-1)
548  * @tc.type: FUNC
549  */
550 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest023, TestSize.Level2)
551 {
552     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest023: start.";
553     std::string apply = "accountmgr";
554     int applyPid = GetProcessPid(apply);
555     GTEST_LOG_(INFO) << "apply:" << apply << ", pid:" << applyPid;
556     DfxDumpCatcher dumplog;
557     std::string msg = "";
558     bool ret = dumplog.DumpCatchFd(applyPid, -1, msg, 1);
559     GTEST_LOG_(INFO) << ret;
560     EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest023 Failed";
561     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest023: end.";
562 }
563 
564 /**
565  * @tc.name: DumpCatcherInterfacesTest024
566  * @tc.desc: test DumpCatchFd API: PID(accountmgr), TID(9999)
567  * @tc.type: FUNC
568  */
569 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest024, TestSize.Level2)
570 {
571     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest024: start.";
572     std::string apply = "accountmgr";
573     int applyPid = GetProcessPid(apply);
574     GTEST_LOG_(INFO) << "apply:" << apply << ", pid:" << applyPid;
575     DfxDumpCatcher dumplog;
576     std::string msg = "";
577     bool ret = dumplog.DumpCatchFd(applyPid, 9999, msg, 1);
578     GTEST_LOG_(INFO) << ret;
579     EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest024 Failed";
580     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest024: end.";
581 }
582 
583 /**
584  * @tc.name: DumpCatcherInterfacesTest025
585  * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(9999)
586  * @tc.type: FUNC
587  */
588 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest025, TestSize.Level2)
589 {
590     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest025: start.";
591     DfxDumpCatcher dumplog;
592     std::string msg = "";
593     bool ret = dumplog.DumpCatchFd(getpid(), 9999, msg, 1);
594     GTEST_LOG_(INFO) << ret;
595     EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest025 Failed";
596     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest025: end.";
597 }
598 
599 /**
600  * @tc.name: DumpCatcherInterfacesTest026
601  * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(child thread)
602  * @tc.type: FUNC
603  */
604 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest026, TestSize.Level2)
605 {
606     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest026: start.";
607     MultiThreadConstructor();
608     DfxDumpCatcher dumplog;
609     std::string msg = "";
610     GTEST_LOG_(INFO) << "dump local process, "  << " tid:" << g_threadId;
611     bool ret = dumplog.DumpCatchFd(getpid(), g_threadId, msg, 1);
612     GTEST_LOG_(INFO) << ret;
613     EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest026 Failed";
614     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest026: end.";
615 }
616 
617 /**
618  * @tc.name: DumpCatcherInterfacesTest027
619  * @tc.desc: test DumpCatchFd API: PID(child process), TID(child thread of child process)
620  * @tc.type: FUNC
621  */
622 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest027, TestSize.Level2)
623 {
624     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest027: start.";
625     ForkMultiThreadProcess();
626     std::vector<int> tids;
627     std::vector<int> nstids;
628     if (!GetTidsByPid(g_processId, tids, nstids)) {
629         return;
630     }
631     int childTid = tids[1]; // 1 : child thread
632     GTEST_LOG_(INFO) << "dump remote process, "  << " pid:" << g_processId << ", tid:" << childTid;
633     DfxDumpCatcher dumplog;
634     std::string msg = "";
635     bool ret = dumplog.DumpCatchFd(g_processId, childTid, msg, 1);
636     GTEST_LOG_(INFO) << ret;
637     EXPECT_TRUE(ret) << "DumpCatcherInterfacesTest027 Failed";
638     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest027: end.";
639 }
640 
641 /**
642  * @tc.name: DumpCatcherInterfacesTest028
643  * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(child thread) and config FrameNum
644  * @tc.type: FUNC
645  */
646 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest028, TestSize.Level2)
647 {
648     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest028: start.";
649     RecursiveMultiThreadConstructor();
650     DfxDumpCatcher dumplog;
651     std::string msg = "";
652     GTEST_LOG_(INFO) << "dump local process, "  << " tid:" << g_threadId;
653     bool ret = dumplog.DumpCatchFd(getpid(), g_threadId, msg, 1, 10); // 10 means backtrace frames is 10
654     GTEST_LOG_(INFO) << "message:"  << msg;
655     GTEST_LOG_(INFO) << ret;
656     EXPECT_TRUE(msg.find("#09") != std::string::npos);
657     EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest028 Failed";
658     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest028: end.";
659 }
660 
661 /**
662  * @tc.name: DumpCatcherInterfacesTest029
663  * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(child thread) and DEFAULT_MAX_FRAME_NUM
664  * @tc.type: FUNC
665  */
666 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest029, TestSize.Level2)
667 {
668     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest029: start.";
669     RecursiveMultiThreadConstructor();
670     usleep(CREATE_THREAD_TIMEOUT);
671     DfxDumpCatcher dumplog;
672     std::string msg = "";
673     GTEST_LOG_(INFO) << "dump local process, "  << " tid:" << g_threadId;
674     bool ret = dumplog.DumpCatchFd(getpid(), g_threadId, msg, 1);
675     GTEST_LOG_(INFO) << "message:"  << msg;
676     GTEST_LOG_(INFO) << ret;
677 #if defined(__aarch64__)
678     std::string stackKeyword = std::string("#") + std::to_string(DEFAULT_MAX_LOCAL_FRAME_NUM - 1);
679 #else
680     std::string stackKeyword = std::string("#") + std::to_string(DEFAULT_MAX_FRAME_NUM - 1);
681 #endif
682     GTEST_LOG_(INFO) << "stackKeyword:"  << stackKeyword;
683     EXPECT_TRUE(msg.find(stackKeyword.c_str()) != std::string::npos);
684     EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest029 Failed";
685     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest029: end.";
686 }
687 
688 #ifndef is_ohos_lite
689 /**
690  * @tc.name: DumpCatcherInterfacesTest030
691  * @tc.desc: test DumpCatch remote API: PID(getpid()), TID(child thread)
692  *     and maxFrameNums(DEFAULT_MAX_FRAME_NUM), isJson(true)
693  * @tc.type: FUNC
694  */
695 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest030, TestSize.Level2)
696 {
697     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest030: start.";
698     pid_t pid = fork();
699     if (pid == 0) {
700         std::this_thread::sleep_for(std::chrono::seconds(10));
701         _exit(0);
702     }
703     GTEST_LOG_(INFO) << "dump remote process, "  << " pid:" << pid << ", tid:" << 0;
704     DfxDumpCatcher dumplog;
705     DfxJsonFormatter format;
706     string msg = "";
707     bool ret = dumplog.DumpCatch(pid, 0, msg);
708     EXPECT_TRUE(ret) << "DumpCatch remote msg Failed.";
709     string jsonMsg = "";
710     bool jsonRet = dumplog.DumpCatch(pid, 0, jsonMsg, DEFAULT_MAX_FRAME_NUM, true);
711     std::cout << jsonMsg << std::endl;
712     EXPECT_TRUE(jsonRet) << "DumpCatch remote json Failed.";
713     string stackMsg = "";
714     bool formatRet = format.FormatJsonStack(jsonMsg, stackMsg);
715     EXPECT_TRUE(formatRet) << "FormatJsonStack Failed.";
716     size_t pos = msg.find("Process name:");
717     if (pos != std::string::npos) {
718         msg = msg.erase(0, pos);
719         msg = msg.erase(0, msg.find("\n") + 1);
720     } else {
721         msg = msg.erase(0, msg.find("\n") + 1);
722     }
723     EXPECT_EQ(stackMsg == msg, true) << "stackMsg != msg";
724     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest030: end.";
725 }
726 
727 /**
728  * @tc.name: DumpCatcherInterfacesTest031
729  * @tc.desc: test DumpCatchProcess
730  * @tc.type: FUNC
731  */
732 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest031, TestSize.Level2)
733 {
734     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest031: start.";
735     std::string res = ExecuteCommands("uname");
736     if (res.find("Linux") != std::string::npos) {
737         return;
738     }
739     if (g_testPid == 0) {
740         GTEST_LOG_(ERROR) << "Failed to launch target hap.";
741         return;
742     }
743     if (!CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME)) {
744         GTEST_LOG_(ERROR) << "Error process comm";
745         return;
746     }
747     std::string stopProcessCmd = "kill -s SIGSTOP $(pidof com.example.myapplication)";
748     ExecuteCommands(stopProcessCmd);
749     DfxDumpCatcher dumplog;
750     std::string msg = "";
751     ASSERT_EQ(dumplog.DumpCatchProcess(g_testPid, msg), 1); //kernel stack
752     GTEST_LOG_(INFO) << msg;
753     std::string formattedStack = "";
754     ASSERT_TRUE(DfxJsonFormatter::FormatKernelStack(msg, formattedStack, false));
755     ASSERT_GT(formattedStack.size(), 0);
756     GTEST_LOG_(INFO) << formattedStack;
757     ASSERT_NE(formattedStack.find("#"), std::string::npos);
758     ASSERT_TRUE(DfxJsonFormatter::FormatKernelStack(msg, formattedStack, true));
759     GTEST_LOG_(INFO) << formattedStack;
760     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest031: end.";
761 }
762 #endif
763 
764 /**
765  * @tc.name: DumpCatcherInterfacesTest033
766  * @tc.desc: testDump after crashed
767  * @tc.type: FUNC
768  */
769 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest033, TestSize.Level2)
770 {
771     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest033: start.";
772     GTEST_LOG_(INFO) << "dump remote process, "  << " pid:" << getpid() << ", tid:" << gettid();
773     int32_t fd = RequestFileDescriptor(FaultLoggerType::CPP_CRASH);
774     ASSERT_GT(fd, 0);
775     close(fd);
776     DfxDumpCatcher dumplog;
777     string msg = "";
778     EXPECT_FALSE(dumplog.DumpCatchMix(getpid(), gettid(), msg));
779     constexpr int validTime = 8;
780     sleep(validTime);
781     msg = "";
782     EXPECT_TRUE(dumplog.DumpCatchMix(getpid(), gettid(), msg));
783 }
784 } // namespace HiviewDFX
785 } // namepsace OHOS