1 /*
2 * Copyright (c) 2022 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 <unistd.h>
20 #include <vector>
21
22 #include "dfx_dump_catcher.h"
23
24 using namespace testing;
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace HiviewDFX {
29 class DumpCatcherInterfacesTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 void SetUp();
34 void TearDown();
35 };
36
37 static const int THREAD_ALIVE_TIME = 2;
38
39 static const int CREATE_THREAD_TIMEOUT = 300000;
40
41 static pid_t g_threadId = 0;
42
43 static pid_t g_processId = 0;
44
SetUpTestCase()45 void DumpCatcherInterfacesTest::SetUpTestCase()
46 {}
47
TearDownTestCase()48 void DumpCatcherInterfacesTest::TearDownTestCase()
49 {}
50
SetUp()51 void DumpCatcherInterfacesTest::SetUp()
52 {}
53
TearDown()54 void DumpCatcherInterfacesTest::TearDown()
55 {}
56
CreateThread(void * argv)57 static void* CreateThread(void *argv)
58 {
59 g_threadId = gettid();
60 GTEST_LOG_(INFO) << "create MultiThread " << gettid();
61 sleep(THREAD_ALIVE_TIME);
62 GTEST_LOG_(INFO) << "create MultiThread thread sleep end.";
63 return nullptr;
64 }
65
MultiThreadConstructor(void)66 static int MultiThreadConstructor(void)
67 {
68 pthread_t thread;
69
70 pthread_create(&thread, nullptr, CreateThread, nullptr);
71 pthread_detach(thread);
72 usleep(CREATE_THREAD_TIMEOUT);
73 return 0;
74 }
75
ForkMultiThreadProcess(void)76 static void ForkMultiThreadProcess(void)
77 {
78 int pid = fork();
79 if (pid == 0) {
80 MultiThreadConstructor();
81 _exit(0);
82 } else if (pid < 0) {
83 GTEST_LOG_(INFO) << "ForkMultiThreadProcess fail. ";
84 } else {
85 g_processId = pid;
86 GTEST_LOG_(INFO) << "ForkMultiThreadProcess success, pid: " << pid;
87 usleep(CREATE_THREAD_TIMEOUT);
88 GTEST_LOG_(INFO) << "ForkMultiThreadProcess success, thread id: " << g_threadId;
89 }
90 }
91
GetProcessPid(std::string applyName)92 static int GetProcessPid(std::string applyName)
93 {
94 std::string procCMD = "pgrep '" + applyName + "'";
95 GTEST_LOG_(INFO) << "threadCMD = " << procCMD;
96 FILE *procFileInfo = nullptr;
97 procFileInfo = popen(procCMD.c_str(), "r");
98 if (procFileInfo == nullptr) {
99 perror("popen execute failed");
100 exit(1);
101 }
102 std::string applyPid;
103 char resultBufShell[100] = { 0, };
104 while (fgets(resultBufShell, sizeof(resultBufShell), procFileInfo) != nullptr) {
105 applyPid = resultBufShell;
106 GTEST_LOG_(INFO) << "applyPid: " << applyPid;
107 }
108 pclose(procFileInfo);
109 return std::atoi(applyPid.c_str());
110 }
111
GetCmdResultFromPopen(const std::string & cmd)112 static std::string GetCmdResultFromPopen(const std::string& cmd)
113 {
114 if (cmd.empty()) {
115 return "";
116 }
117 FILE* fp = popen(cmd.c_str(), "r");
118 if (fp == nullptr) {
119 return "";
120 }
121 const int bufSize = 128;
122 char buffer[bufSize];
123 std::string result = "";
124 while (!feof(fp)) {
125 if (fgets(buffer, bufSize - 1, fp) != nullptr) {
126 result += buffer;
127 }
128 }
129 pclose(fp);
130 return result;
131 }
132
GetServicePid(const std::string & serviceName)133 static int GetServicePid(const std::string& serviceName)
134 {
135 std::string cmd = "pidof " + serviceName;
136 std::string pidStr = GetCmdResultFromPopen(cmd);
137 int32_t pid = 0;
138 std::stringstream pidStream(pidStr);
139 pidStream >> pid;
140 printf("the pid of service(%s) is %s \n", serviceName.c_str(), pidStr.c_str());
141 return pid;
142 }
143
LaunchTestHap(const std::string & abilityName,const std::string & bundleName)144 static int LaunchTestHap(const std::string& abilityName, const std::string& bundleName)
145 {
146 std::string launchCmd = "/system/bin/aa start -a " + abilityName + " -b " + bundleName;
147 (void)GetCmdResultFromPopen(launchCmd);
148 sleep(2); // 2 : sleep 2s
149 return GetServicePid(bundleName);
150 }
151
152 /**
153 * @tc.name: DumpCatcherInterfacesTest001
154 * @tc.desc: test DumpCatchMultiPid API: multiPid{PID(app), PID(accountmgr)}
155 * @tc.type: FUNC
156 */
157 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest001, TestSize.Level2)
158 {
159 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest001: start.";
160 std::string testProcess1 = "accountmgr";
161 int testPid1 = GetProcessPid(testProcess1);
162 GTEST_LOG_(INFO) << "testPid1:" << testPid1;
163 std::string testProcess2 = "foundation";
164 int testPid2 = GetProcessPid(testProcess2);
165 GTEST_LOG_(INFO) << "testPid2:" << testPid2;
166 std::vector<int> multiPid {testPid1, testPid2};
167 DfxDumpCatcher dumplog;
168 std::string msg = "";
169 bool ret = dumplog.DumpCatchMultiPid(multiPid, msg);
170 GTEST_LOG_(INFO) << ret;
171 GTEST_LOG_(INFO) << msg;
172 string log[] = {"Tid:", "Name:", "Tid:", "Name:"};
173 log[0] = log[0] + std::to_string(testPid1);
174 log[1] = log[1] + testProcess1;
175 log[2] = log[2] + std::to_string(testPid2);
176 log[3] = log[3] + testProcess2;
177 string::size_type idx;
178 int expectNum = sizeof(log) / sizeof(log[0]);
179 int j = 0;
180 int count = 0;
181 for (int i = 0; i < expectNum; i++) {
182 idx = msg.find(log[j]);
183 GTEST_LOG_(INFO) << log[j];
184 if (idx != string::npos) {
185 count++;
186 }
187 j++;
188 }
189 EXPECT_EQ(count, expectNum) << "DumpCatcherInterfacesTest001 Failed";
190 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest001: end.";
191 }
192
193 /**
194 * @tc.name: DumpCatcherInterfacesTest002
195 * @tc.desc: test DumpCatchMultiPid API: multiPid{0, 0}
196 * @tc.type: FUNC
197 */
198 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest002, TestSize.Level2)
199 {
200 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest002: start.";
201 int testPid1 = 0;
202 GTEST_LOG_(INFO) << "testPid1:" << testPid1;
203 int testPid2 = 0;
204 GTEST_LOG_(INFO) << "testPid2:" << testPid2;
205 std::vector<int> multiPid {testPid1, testPid2};
206 DfxDumpCatcher dumplog;
207 std::string msg = "";
208 bool ret = dumplog.DumpCatchMultiPid(multiPid, msg);
209 GTEST_LOG_(INFO) << ret;
210 GTEST_LOG_(INFO) << msg;
211 EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest002 Failed";
212 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest002: end.";
213 }
214
215 /**
216 * @tc.name: DumpCatcherInterfacesTest003
217 * @tc.desc: test DumpCatchMultiPid API: multiPid{-11, -11}
218 * @tc.type: FUNC
219 */
220 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest003, TestSize.Level2)
221 {
222 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest003: start.";
223 int testPid1 = -11;
224 GTEST_LOG_(INFO) << "testPid1:" << testPid1;
225 int testPid2 = -11;
226 GTEST_LOG_(INFO) << "testPid2:" << testPid2;
227 std::vector<int> multiPid {testPid1, testPid2};
228 DfxDumpCatcher dumplog;
229 std::string msg = "";
230 bool ret = dumplog.DumpCatchMultiPid(multiPid, msg);
231 GTEST_LOG_(INFO) << ret;
232 GTEST_LOG_(INFO) << msg;
233 EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest003 Failed";
234 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest003: end.";
235 }
236
237 /**
238 * @tc.name: DumpCatcherInterfacesTest004
239 * @tc.desc: test DumpCatchMultiPid API: multiPid{PID(accountmgr), 0}
240 * @tc.type: FUNC
241 */
242 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest004, TestSize.Level2)
243 {
244 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest004: start.";
245 std::string testProcess = "accountmgr";
246 int applyPid1 = GetProcessPid(testProcess);
247 GTEST_LOG_(INFO) << "applyPid1:" << applyPid1;
248 int applyPid2 = 0;
249 GTEST_LOG_(INFO) << "applyPid2:" << applyPid2;
250 std::vector<int> multiPid {applyPid1, applyPid2};
251 DfxDumpCatcher dumplog;
252 std::string msg = "";
253 bool ret = dumplog.DumpCatchMultiPid(multiPid, msg);
254 GTEST_LOG_(INFO) << ret;
255 GTEST_LOG_(INFO) << msg;
256 string log[] = { "Tid:", "Name:", "Failed" };
257 log[0] = log[0] + std::to_string(applyPid1);
258 log[1] = log[1] + "accountmgr";
259 string::size_type idx;
260 int expectNum = sizeof(log) / sizeof(log[0]);
261 int j = 0;
262 int count = 0;
263 for (int i = 0; i < expectNum; i++) {
264 idx = msg.find(log[j]);
265 GTEST_LOG_(INFO) << log[j];
266 if (idx != string::npos) {
267 count++;
268 }
269 j++;
270 }
271 EXPECT_EQ(count, expectNum) << "DumpCatcherInterfacesTest004 Failed";
272 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest004: end.";
273 }
274
275 /**
276 * @tc.name: DumpCatcherInterfacesTest005
277 * @tc.desc: test DumpCatchMultiPid API: multiPid{PID(accountmgr),PID(app),PID(foundation)}
278 * @tc.type: FUNC
279 */
280 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest005, TestSize.Level2)
281 {
282 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest005: start.";
283 std::string testProcess1 = "accountmgr";
284 int testPid1 = GetProcessPid(testProcess1);
285 GTEST_LOG_(INFO) << "testPid1:" << testPid1;
286 std::string testProcess2 = "foundation";
287 int testPid2 = GetProcessPid(testProcess2);
288 GTEST_LOG_(INFO) << "testPid2:" << testPid2;
289 std::string testProcess3 = "com.ohos.systemui";
290 int testPid3 = GetServicePid(testProcess3);
291 GTEST_LOG_(INFO) << "testPid3:" << testPid3;
292 std::vector<int> multiPid {testPid1, testPid2, testPid3};
293 DfxDumpCatcher dumplog;
294 std::string msg = "";
295 bool ret = dumplog.DumpCatchMultiPid(multiPid, msg);
296 GTEST_LOG_(INFO) << ret;
297 GTEST_LOG_(INFO) << msg;
298 string log[] = { "Tid:", "Name:", "Tid:", "Name:", "Tid:", "Name:" };
299 log[0] = log[0] + std::to_string(testPid1);
300 log[1] = log[1] + testProcess1;
301 log[2] = log[2] + std::to_string(testPid2);
302 log[3] = log[3] + testProcess2;
303 log[4] = log[4] + std::to_string(testPid3);
304 log[5] = log[5] + "com.ohos.system";
305 string::size_type idx;
306 int expectNum = sizeof(log) / sizeof(log[0]);
307 int j = 0;
308 int count = 0;
309 for (int i = 0; i < expectNum; i++) {
310 idx = msg.find(log[j]);
311 GTEST_LOG_(INFO) << log[j];
312 if (idx != string::npos) {
313 count++;
314 }
315 j++;
316 }
317 EXPECT_EQ(count, expectNum) << "DumpCatcherInterfacesTest005 Failed";
318 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest005: end.";
319 }
320
321 /**
322 * @tc.name: DumpCatcherInterfacesTest006
323 * @tc.desc: test DumpCatchMultiPid API: multiPid{PID(accountmgr), -11}
324 * @tc.type: FUNC
325 */
326 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest006, TestSize.Level2)
327 {
328 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest006: start.";
329 std::string testProcess = "accountmgr";
330 int testPid1 = GetProcessPid(testProcess);
331 GTEST_LOG_(INFO) << "applyPid1:" << testPid1;
332 int testPid2 = -11;
333 GTEST_LOG_(INFO) << "applyPid2:" << testPid2;
334 std::vector<int> multiPid {testPid1, testPid2};
335 DfxDumpCatcher dumplog;
336 std::string msg = "";
337 bool ret = dumplog.DumpCatchMultiPid(multiPid, msg);
338 GTEST_LOG_(INFO) << ret;
339 GTEST_LOG_(INFO) << msg;
340 string log[] = { "Tid:", "Name:", "Failed"};
341 log[0] = log[0] + std::to_string(testPid1);
342 log[1] = log[1] + "accountmgr";
343 string::size_type idx;
344 int expectNum = sizeof(log) / sizeof(log[0]);
345 int j = 0;
346 int count = 0;
347 for (int i = 0; i < expectNum; i++) {
348 idx = msg.find(log[j]);
349 GTEST_LOG_(INFO) << log[j];
350 if (idx != string::npos) {
351 count++;
352 }
353 j++;
354 }
355 EXPECT_EQ(count, expectNum) << "DumpCatcherInterfacesTest006 Failed";
356 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest006: end.";
357 }
358
359 /**
360 * @tc.name: DumpCatcherInterfacesTest007
361 * @tc.desc: test DumpCatchMultiPid API: multiPid{9999, 9999}
362 * @tc.type: FUNC
363 */
364 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest007, TestSize.Level2)
365 {
366 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest007: start.";
367 int applyPid = 9999;
368 GTEST_LOG_(INFO) << "applyPid1:" << applyPid;
369 std::vector<int> multiPid {applyPid, applyPid};
370 DfxDumpCatcher dumplog;
371 std::string msg = "";
372 bool ret = dumplog.DumpCatchMultiPid(multiPid, msg);
373 GTEST_LOG_(INFO) << ret;
374 GTEST_LOG_(INFO) << msg;
375 EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest007 Failed";
376 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest007: end.";
377 }
378
379 /**
380 * @tc.name: DumpCatcherInterfacesTest008
381 * @tc.desc: test DumpCatchMultiPid API: multiPid{PID(accountmgr), 9999}
382 * @tc.type: FUNC
383 */
384 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest008, TestSize.Level2)
385 {
386 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest0019: start.";
387 std::string apply = "accountmgr";
388 int applyPid1 = GetProcessPid(apply);
389 GTEST_LOG_(INFO) << "applyPid1:" << applyPid1;
390 int applyPid2 = 9999;
391 GTEST_LOG_(INFO) << "applyPid2:" << applyPid2;
392 std::vector<int> multiPid {applyPid1, applyPid2};
393 DfxDumpCatcher dumplog;
394 std::string msg = "";
395 bool ret = dumplog.DumpCatchMultiPid(multiPid, msg);
396 GTEST_LOG_(INFO) << ret;
397 GTEST_LOG_(INFO) << msg;
398 string log[] = { "Tid:", "Name:", "Failed"};
399 log[0] = log[0] + std::to_string(applyPid1);
400 log[1] = log[1] + apply;
401 int expectNum = sizeof(log) / sizeof(log[0]);
402 string::size_type idx;
403 int j = 0;
404 int count = 0;
405 for (int i = 0; i < expectNum; i++) {
406 idx = msg.find(log[j]);
407 GTEST_LOG_(INFO) << log[j];
408 if (idx != string::npos) {
409 count++;
410 }
411 j++;
412 }
413 EXPECT_EQ(count, expectNum) << "DumpCatcherInterfacesTest008 Failed";
414 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest008: end.";
415 }
416
417 /**
418 * @tc.name: DumpCatcherInterfacesTest009
419 * @tc.desc: test CatchFrame API: PID(getpid()), TID(gettid())
420 * @tc.type: FUNC
421 */
422 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest009, TestSize.Level2)
423 {
424 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest009: start.";
425 DfxDumpCatcher dumplog(getpid());
426 std::vector<std::shared_ptr<DfxFrame>> frameV;
427 bool ret = dumplog.InitFrameCatcher();
428 EXPECT_EQ(ret, true);
429 ret = dumplog.RequestCatchFrame(gettid());
430 EXPECT_EQ(ret, true);
431 ret = dumplog.CatchFrame(gettid(), frameV);
432 EXPECT_EQ(ret, true);
433 dumplog.DestroyFrameCatcher();
434 EXPECT_GT(frameV.size(), 0) << "DumpCatcherInterfacesTest009 Failed";
435 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest009: end.";
436 }
437
438 /**
439 * @tc.name: DumpCatcherInterfacesTest010
440 * @tc.desc: test CatchFrame API: PID(com.ohos.systemui), TID(com.ohos.systemui)
441 * @tc.type: FUNC
442 */
443 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest010, TestSize.Level2)
444 {
445 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest010: start.";
446 std::string calcBundleName = "ohos.samples.distributedcalc";
447 std::string calcAbiltyName = calcBundleName + ".MainAbility";
448 int calcPid = LaunchTestHap(calcAbiltyName, calcBundleName);
449 DfxDumpCatcher dumplog(calcPid);
450 std::vector<std::shared_ptr<DfxFrame>> frameV;
451 bool ret = dumplog.InitFrameCatcher();
452 EXPECT_EQ(ret, true);
453 ret = dumplog.RequestCatchFrame(calcPid);
454 EXPECT_EQ(ret, false);
455 ret = dumplog.CatchFrame(calcPid, frameV);
456 EXPECT_EQ(ret, false);
457 dumplog.DestroyFrameCatcher();
458 EXPECT_EQ(frameV.size(), 0) << "DumpCatcherInterfacesTest010 Failed";
459 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest010: end.";
460 }
461
462 /**
463 * @tc.name: DumpCatcherInterfacesTest011
464 * @tc.desc: test CatchFrame API: TID(gettid())
465 * @tc.type: FUNC
466 */
467 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest011, TestSize.Level2)
468 {
469 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest011: start.";
470 DfxDumpCatcher dumplog;
471 std::vector<std::shared_ptr<DfxFrame>> frameV;
472 bool ret = dumplog.CatchFrame(gettid(), frameV);
473 GTEST_LOG_(INFO) << ret;
474 EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest011 Failed";
475 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest011: end.";
476 }
477
478 /**
479 * @tc.name: DumpCatcherInterfacesTest012
480 * @tc.desc: test CatchFrame API: app TID(-11)
481 * @tc.type: FUNC
482 */
483 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest012, TestSize.Level2)
484 {
485 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest012: start.";
486 DfxDumpCatcher dumplog;
487 std::vector<std::shared_ptr<DfxFrame>> frameV;
488 bool ret = dumplog.CatchFrame(-11, frameV);
489 GTEST_LOG_(INFO) << ret;
490 EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest012 Failed";
491 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest012: end.";
492 }
493
494 /**
495 * @tc.name: DumpCatcherInterfacesTest013
496 * @tc.desc: test CatchFrame API: TID(-1)
497 * @tc.type: FUNC
498 */
499 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest013, TestSize.Level2)
500 {
501 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest013: start.";
502 DfxDumpCatcher dumplog;
503 std::vector<std::shared_ptr<DfxFrame>> frameV;
504 bool ret = dumplog.CatchFrame(-1, frameV);
505 GTEST_LOG_(INFO) << ret;
506 EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest013 Failed";
507 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest013: end.";
508 }
509
510 /**
511 * @tc.name: DumpCatcherInterfacesTest014
512 * @tc.desc: test DumpCatchMix API: PID(systemui pid), TID(0)
513 * @tc.type: FUNC
514 * @tc.require: issueI5PJ9O
515 */
516 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest014, TestSize.Level2)
517 {
518 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest014: start.";
519 std::string calcBundleName = "ohos.samples.distributedcalc";
520 std::string calcAbiltyName = calcBundleName + ".MainAbility";
521 int calcPid = LaunchTestHap(calcAbiltyName, calcBundleName);
522 DfxDumpCatcher dumplog;
523 std::string msg = "";
524 bool ret = dumplog.DumpCatchMix(calcPid, 0, msg);
525 GTEST_LOG_(INFO) << ret;
526 GTEST_LOG_(INFO) << msg;
527 string log[] = { "Tid:", "comm:ohos.samples.di", "#00", "/system/bin/appspawn",
528 "comm:jsThread-", "comm:GC_WorkerThread", "comm:ace.bg.1"};
529 log[0] += std::to_string(calcPid);
530 string::size_type idx;
531 int j = 0;
532 int count = 0;
533 int expectNum = sizeof(log) / sizeof(log[0]);
534 for (int i = 0; i < expectNum; i++) {
535 idx = msg.find(log[j]);
536 GTEST_LOG_(INFO) << log[j];
537 if (idx != string::npos) {
538 count++;
539 }
540 j++;
541 }
542
543 EXPECT_EQ(count, expectNum) << "DumpCatcherInterfacesTest014 Failed";
544 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest014: end.";
545 }
546
547 /**
548 * @tc.name: DumpCatcherInterfacesTest015
549 * @tc.desc: test DumpCatchMix API: PID(systemui pid), TID(systemui pid)
550 * @tc.type: FUNC
551 * @tc.require: issueI5PJ9O
552 */
553 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest015, TestSize.Level2)
554 {
555 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest015: start.";
556 std::string calcBundleName = "ohos.samples.distributedcalc";
557 std::string calcAbiltyName = calcBundleName + ".MainAbility";
558 int calcPid = LaunchTestHap(calcAbiltyName, calcBundleName);
559 DfxDumpCatcher dumplog;
560 std::string msg = "";
561 bool ret = dumplog.DumpCatchMix(calcPid, calcPid, msg);
562 GTEST_LOG_(INFO) << ret;
563 GTEST_LOG_(INFO) << msg;
564 string log[] = { "Tid:", "comm:ohos.samples.di", "#00", "/system/bin/appspawn"};
565 log[0] += std::to_string(calcPid);
566 int expectNum = sizeof(log) / sizeof(log[0]);
567 string::size_type idx;
568 int j = 0;
569 int count = 0;
570 for (int i = 0; i < expectNum; i++) {
571 idx = msg.find(log[j]);
572 GTEST_LOG_(INFO) << log[j];
573 if (idx != string::npos) {
574 count++;
575 }
576 j++;
577 }
578 EXPECT_EQ(count, expectNum) << "DumpCatcherInterfacesTest015 Failed";
579 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest015: end.";
580 }
581
582 /**
583 * @tc.name: DumpCatcherInterfacesTest016
584 * @tc.desc: test DumpCatchMix API: PID(systemui pid), TID(-1)
585 * @tc.type: FUNC
586 * @tc.require: issueI5PJ9O
587 */
588 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest016, TestSize.Level2)
589 {
590 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest016: start.";
591 std::string calcBundleName = "ohos.samples.distributedcalc";
592 std::string calcAbiltyName = calcBundleName + ".MainAbility";
593 int calcPid = LaunchTestHap(calcAbiltyName, calcBundleName);
594 DfxDumpCatcher dumplog;
595 std::string msg = "";
596 bool ret = dumplog.DumpCatchMix(calcPid, -1, msg);
597 GTEST_LOG_(INFO) << ret;
598 GTEST_LOG_(INFO) << msg;
599 EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest016 Failed";
600 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest016: end.";
601 }
602
603 /**
604 * @tc.name: DumpCatcherInterfacesTest017
605 * @tc.desc: test DumpCatchMix API: PID(-1), TID(-1)
606 * @tc.type: FUNC
607 * @tc.require: issueI5PJ9O
608 */
609 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest017, TestSize.Level2)
610 {
611 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest017: start.";
612 DfxDumpCatcher dumplog;
613 std::string msg = "";
614 bool ret = dumplog.DumpCatchMix(-1, -1, msg);
615 GTEST_LOG_(INFO) << ret;
616 GTEST_LOG_(INFO) << msg;
617 EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest017 Failed";
618 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest017: end.";
619 }
620
621 /**
622 * @tc.name: DumpCatcherInterfacesTest018
623 * @tc.desc: test DumpCatchFd API
624 * @tc.type: FUNC
625 */
626 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest018, TestSize.Level2)
627 {
628 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest018: start.";
629 DfxDumpCatcher dumplog;
630 std::string msg = "";
631 bool ret = dumplog.DumpCatchFd(getpid(), gettid(), msg, 1);
632 GTEST_LOG_(INFO) << ret;
633 GTEST_LOG_(INFO) << msg;
634 EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest018 Failed";
635 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest018: end.";
636 }
637
638 /**
639 * @tc.name: DumpCatcherInterfacesTest019
640 * @tc.desc: test DumpCatchFd API
641 * @tc.type: FUNC
642 */
643 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest019, TestSize.Level2)
644 {
645 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest019: start.";
646 DfxDumpCatcher dumplog;
647 std::string msg = "";
648 bool ret = dumplog.DumpCatchFd(getpid(), 0, msg, 1);
649 GTEST_LOG_(INFO) << ret;
650 GTEST_LOG_(INFO) << msg;
651 EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest019 Failed";
652 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest019: end.";
653 }
654
655 /**
656 * @tc.name: DumpCatcherInterfacesTest020
657 * @tc.desc: test DumpCatchFd API
658 * @tc.type: FUNC
659 */
660 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest020, TestSize.Level2)
661 {
662 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest020: start.";
663 DfxDumpCatcher dumplog;
664 std::string msg = "";
665 bool ret = dumplog.DumpCatchFd(getpid(), -1, msg, 1);
666 GTEST_LOG_(INFO) << ret;
667 GTEST_LOG_(INFO) << msg;
668 EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest020 Failed";
669 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest020: end.";
670 }
671
672
673 /**
674 * @tc.name: DumpCatcherInterfacesTest021
675 * @tc.desc: test DumpCatchFd API
676 * @tc.type: FUNC
677 */
678 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest021, TestSize.Level2)
679 {
680 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest021: start.";
681 std::string apply = "accountmgr";
682 int applyPid = GetProcessPid(apply);
683 GTEST_LOG_(INFO) << "apply:" << apply << ", pid:" << applyPid;
684 DfxDumpCatcher dumplog;
685 std::string msg = "";
686 bool ret = dumplog.DumpCatchFd(applyPid, 0, msg, 1);
687 GTEST_LOG_(INFO) << ret;
688 GTEST_LOG_(INFO) << msg;
689 EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest021 Failed";
690 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest021: end.";
691 }
692
693 /**
694 * @tc.name: DumpCatcherInterfacesTest022
695 * @tc.desc: test DumpCatchFd API
696 * @tc.type: FUNC
697 */
698 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest022, TestSize.Level2)
699 {
700 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest022: start.";
701 std::string apply = "accountmgr";
702 int applyPid = GetProcessPid(apply);
703 GTEST_LOG_(INFO) << "apply:" << apply << ", pid:" << applyPid;
704 DfxDumpCatcher dumplog;
705 std::string msg = "";
706 bool ret = dumplog.DumpCatchFd(applyPid, applyPid, msg, 1);
707 GTEST_LOG_(INFO) << ret;
708 GTEST_LOG_(INFO) << msg;
709 EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest022 Failed";
710 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest022: end.";
711 }
712
713 /**
714 * @tc.name: DumpCatcherInterfacesTest023
715 * @tc.desc: test DumpCatchFd API
716 * @tc.type: FUNC
717 */
718 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest023, TestSize.Level2)
719 {
720 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest023: start.";
721 std::string apply = "accountmgr";
722 int applyPid = GetProcessPid(apply);
723 GTEST_LOG_(INFO) << "apply:" << apply << ", pid:" << applyPid;
724 DfxDumpCatcher dumplog;
725 std::string msg = "";
726 bool ret = dumplog.DumpCatchFd(applyPid, -1, msg, 1);
727 GTEST_LOG_(INFO) << ret;
728 GTEST_LOG_(INFO) << msg;
729 EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest023 Failed";
730 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest023: end.";
731 }
732
733 /**
734 * @tc.name: DumpCatcherInterfacesTest024
735 * @tc.desc: test DumpCatchFd API
736 * @tc.type: FUNC
737 */
738 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest024, TestSize.Level2)
739 {
740 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest024: start.";
741 std::string apply = "accountmgr";
742 int applyPid = GetProcessPid(apply);
743 GTEST_LOG_(INFO) << "apply:" << apply << ", pid:" << applyPid;
744 DfxDumpCatcher dumplog;
745 std::string msg = "";
746 bool ret = dumplog.DumpCatchFd(applyPid, 9999, msg, 1);
747 GTEST_LOG_(INFO) << ret;
748 GTEST_LOG_(INFO) << msg;
749 EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest024 Failed";
750 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest024: end.";
751 }
752
753 /**
754 * @tc.name: DumpCatcherInterfacesTest025
755 * @tc.desc: test DumpCatchFd API
756 * @tc.type: FUNC
757 */
758 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest025, TestSize.Level2)
759 {
760 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest025: start.";
761 DfxDumpCatcher dumplog;
762 std::string msg = "";
763 bool ret = dumplog.DumpCatchFd(getpid(), 9999, msg, 1);
764 GTEST_LOG_(INFO) << ret;
765 GTEST_LOG_(INFO) << msg;
766 EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest025 Failed";
767 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest025: end.";
768 }
769
770 /**
771 * @tc.name: DumpCatcherInterfacesTest026
772 * @tc.desc: test DumpCatchFd API
773 * @tc.type: FUNC
774 */
775 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest026, TestSize.Level2)
776 {
777 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest026: start.";
778 MultiThreadConstructor();
779
780 DfxDumpCatcher dumplog;
781 std::string msg = "";
782 GTEST_LOG_(INFO) << "dump local process, " << " tid:" << g_threadId;
783 bool ret = dumplog.DumpCatchFd(getpid(), g_threadId, msg, 1);
784 GTEST_LOG_(INFO) << ret;
785 GTEST_LOG_(INFO) << msg;
786 EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest026 Failed";
787 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest026: end.";
788 }
789
790 /**
791 * @tc.name: DumpCatcherInterfacesTest027
792 * @tc.desc: test DumpCatchFd API
793 * @tc.type: FUNC
794 */
795 HWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest027, TestSize.Level2)
796 {
797 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest027: start.";
798 ForkMultiThreadProcess();
799
800 GTEST_LOG_(INFO) << "dump remote process, " << " pid:" << g_processId << ", tid:" << g_threadId;
801 DfxDumpCatcher dumplog;
802 std::string msg = "";
803 bool ret = dumplog.DumpCatchFd(g_processId, g_threadId, msg, 1);
804 GTEST_LOG_(INFO) << ret;
805 GTEST_LOG_(INFO) << msg;
806 EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest027 Failed";
807 GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest027: end.";
808 }
809
810 } // namespace HiviewDFX
811 } // namepsace OHOS
812