• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 <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 
29 using namespace testing;
30 using namespace testing::ext;
31 
32 namespace OHOS {
33 namespace HiviewDFX {
34 class DumpCatcherInterfacesTest : public testing::Test {
35 public:
36     static void SetUpTestCase();
37     static void TearDownTestCase();
38     void SetUp();
39     void TearDown();
40 };
41 
42 static const int THREAD_ALIVE_TIME = 2;
43 
44 static const int CREATE_THREAD_TIMEOUT = 300000;
45 
46 static pid_t g_threadId = 0;
47 
48 static pid_t g_processId = 0;
49 
50 int g_testPid = 0;
51 
SetUpTestCase()52 void DumpCatcherInterfacesTest::SetUpTestCase()
53 {
54     InstallTestHap("/data/FaultloggerdJsTest.hap");
55     std::string testBundleName = TEST_BUNDLE_NAME;
56     std::string testAbiltyName = testBundleName + ".MainAbility";
57     g_testPid = LaunchTestHap(testAbiltyName, testBundleName);
58 }
59 
TearDownTestCase()60 void DumpCatcherInterfacesTest::TearDownTestCase()
61 {
62     StopTestHap(TEST_BUNDLE_NAME);
63     UninstallTestHap(TEST_BUNDLE_NAME);
64 }
65 
SetUp()66 void DumpCatcherInterfacesTest::SetUp()
67 {}
68 
TearDown()69 void DumpCatcherInterfacesTest::TearDown()
70 {}
71 
TestFunRecursive(int recursiveCount)72 static void TestFunRecursive(int recursiveCount)
73 {
74     GTEST_LOG_(INFO) << "Enter TestFunRecursive recursiveCount:" << recursiveCount;
75     if (recursiveCount <= 0) {
76         GTEST_LOG_(INFO) << "start enter sleep" << gettid();
77         sleep(THREAD_ALIVE_TIME);
78         GTEST_LOG_(INFO) << "sleep end.";
79     } else {
80         TestFunRecursive(recursiveCount - 1);
81     }
82 }
83 
CreateRecursiveThread(void * argv)84 static void* CreateRecursiveThread(void *argv)
85 {
86     g_threadId = gettid();
87     GTEST_LOG_(INFO) << "create Recursive MultiThread " << gettid();
88     TestFunRecursive(266); // 266: set recursive count to 266, used for dumpcatcher get stack info
89     GTEST_LOG_(INFO) << "Recursive MultiThread thread sleep end.";
90     return nullptr;
91 }
92 
RecursiveMultiThreadConstructor(void)93 static int RecursiveMultiThreadConstructor(void)
94 {
95     pthread_t thread;
96     pthread_create(&thread, nullptr, CreateRecursiveThread, nullptr);
97     pthread_detach(thread);
98     usleep(CREATE_THREAD_TIMEOUT);
99     return 0;
100 }
101 
CreateThread(void * argv)102 static void* CreateThread(void *argv)
103 {
104     g_threadId = gettid();
105     GTEST_LOG_(INFO) << "create MultiThread " << gettid();
106     sleep(THREAD_ALIVE_TIME);
107     GTEST_LOG_(INFO) << "create MultiThread thread sleep end.";
108     return nullptr;
109 }
110 
MultiThreadConstructor(void)111 static int MultiThreadConstructor(void)
112 {
113     pthread_t thread;
114     pthread_create(&thread, nullptr, CreateThread, nullptr);
115     pthread_detach(thread);
116     usleep(CREATE_THREAD_TIMEOUT);
117     return 0;
118 }
119 
ForkMultiThreadProcess(void)120 static void ForkMultiThreadProcess(void)
121 {
122     int pid = fork();
123     if (pid == 0) {
124         MultiThreadConstructor();
125         _exit(0);
126     } else if (pid < 0) {
127         GTEST_LOG_(INFO) << "ForkMultiThreadProcess fail. ";
128     } else {
129         g_processId = pid;
130         GTEST_LOG_(INFO) << "ForkMultiThreadProcess success, pid: " << pid;
131         usleep(CREATE_THREAD_TIMEOUT);
132         GTEST_LOG_(INFO) << "ForkMultiThreadProcess success, thread id: " << g_threadId;
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", "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" };
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     EXPECT_EQ(count, len) << msg << "DumpCatcherInterfacesTest015 Failed";
412     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest015: end.";
413 }
414 
415 /**
416  * @tc.name: DumpCatcherInterfacesTest016
417  * @tc.desc: test DumpCatchMix API: PID(test hap), TID(-1)
418  * @tc.type: FUNC
419  * @tc.require: issueI5PJ9O
420  */
421 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest016, TestSize.Level2)
422 {
423     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest016: start.";
424     if (g_testPid == 0) {
425         GTEST_LOG_(ERROR) << "Failed to launch target hap.";
426         return;
427     }
428     if (!CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME)) {
429         GTEST_LOG_(ERROR) << "Error process comm";
430         return;
431     }
432     DfxDumpCatcher dumplog;
433     std::string msg = "";
434     bool ret = dumplog.DumpCatchMix(g_testPid, -1, msg);
435     GTEST_LOG_(INFO) << ret;
436     EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest016 Failed";
437     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest016: end.";
438 }
439 
440 /**
441  * @tc.name: DumpCatcherInterfacesTest017
442  * @tc.desc: test DumpCatchMix API: PID(-1), TID(-1)
443  * @tc.type: FUNC
444  * @tc.require: issueI5PJ9O
445  */
446 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest017, TestSize.Level2)
447 {
448     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest017: start.";
449     DfxDumpCatcher dumplog;
450     std::string msg = "";
451     bool ret = dumplog.DumpCatchMix(-1, -1, msg);
452     GTEST_LOG_(INFO) << ret;
453     EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest017 Failed";
454     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest017: end.";
455 }
456 
457 /**
458  * @tc.name: DumpCatcherInterfacesTest018
459  * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(gettid())
460  * @tc.type: FUNC
461  */
462 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest018, TestSize.Level2)
463 {
464     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest018: start.";
465     DfxDumpCatcher dumplog;
466     std::string msg = "";
467     bool ret = dumplog.DumpCatchFd(getpid(), gettid(), msg, 1);
468     GTEST_LOG_(INFO) << ret;
469     EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest018 Failed";
470     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest018: end.";
471 }
472 
473 /**
474  * @tc.name: DumpCatcherInterfacesTest019
475  * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(0)
476  * @tc.type: FUNC
477  */
478 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest019, TestSize.Level2)
479 {
480     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest019: start.";
481     DfxDumpCatcher dumplog;
482     std::string msg = "";
483     bool ret = dumplog.DumpCatchFd(getpid(), 0, msg, 1);
484     GTEST_LOG_(INFO) << ret;
485     EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest019 Failed";
486     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest019: end.";
487 }
488 
489 /**
490  * @tc.name: DumpCatcherInterfacesTest020
491  * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(-1)
492  * @tc.type: FUNC
493  */
494 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest020, TestSize.Level2)
495 {
496     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest020: start.";
497     DfxDumpCatcher dumplog;
498     std::string msg = "";
499     bool ret = dumplog.DumpCatchFd(getpid(), -1, msg, 1);
500     GTEST_LOG_(INFO) << ret;
501     EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest020 Failed";
502     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest020: end.";
503 }
504 
505 
506 /**
507  * @tc.name: DumpCatcherInterfacesTest021
508  * @tc.desc: test DumpCatchFd API: PID(accountmgr), TID(0)
509  * @tc.type: FUNC
510  */
511 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest021, TestSize.Level2)
512 {
513     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest021: start.";
514     std::string apply = "accountmgr";
515     int applyPid = GetProcessPid(apply);
516     GTEST_LOG_(INFO) << "apply:" << apply << ", pid:" << applyPid;
517     DfxDumpCatcher dumplog;
518     std::string msg = "";
519     bool ret = dumplog.DumpCatchFd(applyPid, 0, msg, 1);
520     GTEST_LOG_(INFO) << ret;
521     EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest021 Failed";
522     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest021: end.";
523 }
524 
525 /**
526  * @tc.name: DumpCatcherInterfacesTest022
527  * @tc.desc: test DumpCatchFd API: PID(accountmgr), TID(accountmgr main thread)
528  * @tc.type: FUNC
529  */
530 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest022, TestSize.Level2)
531 {
532     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest022: start.";
533     std::string apply = "accountmgr";
534     int applyPid = GetProcessPid(apply);
535     GTEST_LOG_(INFO) << "apply:" << apply << ", pid:" << applyPid;
536     DfxDumpCatcher dumplog;
537     std::string msg = "";
538     bool ret = dumplog.DumpCatchFd(applyPid, applyPid, msg, 1);
539     GTEST_LOG_(INFO) << ret;
540     EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest022 Failed";
541     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest022: end.";
542 }
543 
544 /**
545  * @tc.name: DumpCatcherInterfacesTest023
546  * @tc.desc: test DumpCatchFd API: PID(accountmgr), TID(-1)
547  * @tc.type: FUNC
548  */
549 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest023, TestSize.Level2)
550 {
551     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest023: start.";
552     std::string apply = "accountmgr";
553     int applyPid = GetProcessPid(apply);
554     GTEST_LOG_(INFO) << "apply:" << apply << ", pid:" << applyPid;
555     DfxDumpCatcher dumplog;
556     std::string msg = "";
557     bool ret = dumplog.DumpCatchFd(applyPid, -1, msg, 1);
558     GTEST_LOG_(INFO) << ret;
559     EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest023 Failed";
560     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest023: end.";
561 }
562 
563 /**
564  * @tc.name: DumpCatcherInterfacesTest024
565  * @tc.desc: test DumpCatchFd API: PID(accountmgr), TID(9999)
566  * @tc.type: FUNC
567  */
568 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest024, TestSize.Level2)
569 {
570     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest024: start.";
571     std::string apply = "accountmgr";
572     int applyPid = GetProcessPid(apply);
573     GTEST_LOG_(INFO) << "apply:" << apply << ", pid:" << applyPid;
574     DfxDumpCatcher dumplog;
575     std::string msg = "";
576     bool ret = dumplog.DumpCatchFd(applyPid, 9999, msg, 1);
577     GTEST_LOG_(INFO) << ret;
578     EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest024 Failed";
579     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest024: end.";
580 }
581 
582 /**
583  * @tc.name: DumpCatcherInterfacesTest025
584  * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(9999)
585  * @tc.type: FUNC
586  */
587 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest025, TestSize.Level2)
588 {
589     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest025: start.";
590     DfxDumpCatcher dumplog;
591     std::string msg = "";
592     bool ret = dumplog.DumpCatchFd(getpid(), 9999, msg, 1);
593     GTEST_LOG_(INFO) << ret;
594     EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest025 Failed";
595     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest025: end.";
596 }
597 
598 /**
599  * @tc.name: DumpCatcherInterfacesTest026
600  * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(child thread)
601  * @tc.type: FUNC
602  */
603 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest026, TestSize.Level2)
604 {
605     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest026: start.";
606     MultiThreadConstructor();
607     DfxDumpCatcher dumplog;
608     std::string msg = "";
609     GTEST_LOG_(INFO) << "dump local process, "  << " tid:" << g_threadId;
610     bool ret = dumplog.DumpCatchFd(getpid(), g_threadId, msg, 1);
611     GTEST_LOG_(INFO) << ret;
612     EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest026 Failed";
613     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest026: end.";
614 }
615 
616 /**
617  * @tc.name: DumpCatcherInterfacesTest027
618  * @tc.desc: test DumpCatchFd API: PID(child process), TID(child thread of child process)
619  * @tc.type: FUNC
620  */
621 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest027, TestSize.Level2)
622 {
623     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest027: start.";
624     ForkMultiThreadProcess();
625     GTEST_LOG_(INFO) << "dump remote process, "  << " pid:" << g_processId << ", tid:" << g_threadId;
626     DfxDumpCatcher dumplog;
627     std::string msg = "";
628     bool ret = dumplog.DumpCatchFd(g_processId, g_threadId, msg, 1);
629     GTEST_LOG_(INFO) << ret;
630     EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest027 Failed";
631     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest027: end.";
632 }
633 
634 /**
635  * @tc.name: DumpCatcherInterfacesTest028
636  * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(child thread) and config FrameNum
637  * @tc.type: FUNC
638  */
639 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest028, TestSize.Level2)
640 {
641     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest028: start.";
642     RecursiveMultiThreadConstructor();
643     DfxDumpCatcher dumplog;
644     std::string msg = "";
645     GTEST_LOG_(INFO) << "dump local process, "  << " tid:" << g_threadId;
646     bool ret = dumplog.DumpCatchFd(getpid(), g_threadId, msg, 1, 10); // 10 means backtrace frames is 10
647     GTEST_LOG_(INFO) << "message:"  << msg;
648     GTEST_LOG_(INFO) << ret;
649     EXPECT_TRUE(msg.find("#10") != std::string::npos);
650     EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest028 Failed";
651     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest028: end.";
652 }
653 
654 /**
655  * @tc.name: DumpCatcherInterfacesTest029
656  * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(child thread) and DEFAULT_MAX_FRAME_NUM
657  * @tc.type: FUNC
658  */
659 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest029, TestSize.Level2)
660 {
661     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest029: start.";
662     RecursiveMultiThreadConstructor();
663     DfxDumpCatcher dumplog;
664     std::string msg = "";
665     GTEST_LOG_(INFO) << "dump local process, "  << " tid:" << g_threadId;
666     bool ret = dumplog.DumpCatchFd(getpid(), g_threadId, msg, 1);
667     GTEST_LOG_(INFO) << "message:"  << msg;
668     GTEST_LOG_(INFO) << ret;
669     int maxFrameNum = 31;
670     std::string stackKeyword = std::string("#") + std::to_string(maxFrameNum);
671     EXPECT_TRUE(msg.find(stackKeyword.c_str()) != std::string::npos);
672     EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest029 Failed";
673     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest029: end.";
674 }
675 
676 /**
677  * @tc.name: DumpCatcherInterfacesTest030
678  * @tc.desc: test DumpCatch remote API: PID(getpid()), TID(child thread)
679  *     and maxFrameNums(DEFAULT_MAX_FRAME_NUM), isJsom(true)
680  * @tc.type: FUNC
681  */
682 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest030, TestSize.Level2)
683 {
684     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest030: start.";
685     GTEST_LOG_(INFO) << "dump remote process, "  << " pid:" << 1 << ", tid:" << 0;
686     DfxDumpCatcher dumplog;
687     DfxJsonFormatter format;
688     string msg = "";
689     bool ret = dumplog.DumpCatch(1, 0, msg);
690     EXPECT_TRUE(ret) << "DumpCatch remote msg Failed.";
691     string jsonMsg = "";
692     bool jsonRet = dumplog.DumpCatch(1, 0, jsonMsg, DEFAULT_MAX_FRAME_NUM, true);
693     std::cout << jsonMsg << std::endl;
694     EXPECT_TRUE(jsonRet) << "DumpCatch remote json Failed.";
695     string stackMsg = "";
696     bool formatRet = format.FormatJsonStack(jsonMsg, stackMsg);
697     EXPECT_TRUE(formatRet) << "FormatJsonStack Failed.";
698     size_t pos = msg.find("Process name:");
699     if (pos != std::string::npos) {
700         msg = msg.erase(0, pos);
701         msg = msg.erase(0, msg.find("\n") + 1);
702     } else {
703         msg = msg.erase(0, msg.find("\n") + 1);
704     }
705     EXPECT_EQ(stackMsg == msg, true) << "stackMsg != msg";
706     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest030: end.";
707 }
708 
709 /**
710  * @tc.name: DumpCatcherInterfacesTest031
711  * @tc.desc: test DumpCatch local API: PID(getpid()), TID(child thread)
712  *     and maxFrameNums(DEFAULT_MAX_FRAME_NUM), isJsom(true)
713  * @tc.type: FUNC
714  */
715 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest031, TestSize.Level2)
716 {
717     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest031: start.";
718     GTEST_LOG_(INFO) << "dump remote process, "  << " pid:" << getpid() << ", tid:" << gettid();
719     DfxDumpCatcher dumplog;
720     DfxJsonFormatter format;
721     string msg = "";
722     int pid = getpid() ;
723     int tid = gettid();
724     bool ret = dumplog.DumpCatch(pid, tid, msg);
725     EXPECT_TRUE(ret) << "DumpCatch remote msg Failed.";
726     string jsonMsg = "";
727     bool jsonRet = dumplog.DumpCatch(pid, tid, jsonMsg, DEFAULT_MAX_FRAME_NUM, true);
728     EXPECT_TRUE(jsonRet) << "DumpCatch remote json Failed.";
729     string stackMsg = "";
730     jsonMsg = "[{\"frames\":" + jsonMsg + ",\"thread_name\":\"init\",\"tid\":1}]";
731     bool formatRet = format.FormatJsonStack(jsonMsg, stackMsg);
732     EXPECT_TRUE(formatRet) << "FormatJsonStack Failed.";
733     stackMsg = stackMsg.erase(0, stackMsg.find("\n") + 1);
734     stackMsg = stackMsg.erase(0, stackMsg.find("\n") + 1);
735     stackMsg = stackMsg.erase(0, stackMsg.find("\n") + 1);
736     msg = msg.erase(0, msg.find("\n") + 1);
737     msg = msg.erase(0, msg.find("\n") + 1);
738     EXPECT_EQ(stackMsg == msg, true) << "stackMsg != msg";
739     GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest031: end.";
740 }
741 } // namespace HiviewDFX
742 } // namepsace OHOS