• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
2  * Copyright (c) 2021-2025 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 #include "event_logger_catcher_test.h"
16 
17 #include <ctime>
18 #include <fstream>
19 #include <iostream>
20 #include <memory>
21 #include <regex>
22 
23 #include <fcntl.h>
24 #include <sys/prctl.h>
25 #include <unistd.h>
26 #include <string>
27 
28 #include "securec.h"
29 #include "common_utils.h"
30 #include "file_util.h"
31 #define private public
32 #ifdef DMESG_CATCHER_ENABLE
33 #include "dmesg_catcher.h"
34 #endif // DMESG_CATCHER_ENABLE
35 #include "event_log_task.h"
36 #ifdef STACKTRACE_CATCHER_ENABLE
37 #include "open_stacktrace_catcher.h"
38 #endif // STACKTRACE_CATCHER_ENABLE
39 #include "shell_catcher.h"
40 #ifdef BINDER_CATCHER_ENABLE
41 #include "peer_binder_catcher.h"
42 #endif // BINDER_CATCHER_ENABLE
43 #ifdef USAGE_CATCHER_ENABLE
44 #include "memory_catcher.h"
45 #endif // USAGE_CATCHER_ENABLE
46 #undef private
47 #ifdef BINDER_CATCHER_ENABLE
48 #include "binder_catcher.h"
49 #endif // BINDER_CATCHER_ENABLE
50 #ifdef OTHER_CATCHER_ENABLE
51 #include "ffrt_catcher.h"
52 #endif // OTHER_CATCHER_ENABLE
53 #include "event_logger.h"
54 #include "event_log_catcher.h"
55 #include "sys_event.h"
56 #include "hisysevent.h"
57 #include "eventlogger_util_test.h"
58 #include "log_catcher_utils.h"
59 using namespace testing::ext;
60 using namespace OHOS::HiviewDFX;
61 
62 namespace OHOS {
63 namespace HiviewDFX {
SetUp()64 void EventloggerCatcherTest::SetUp()
65 {
66     /**
67      * @tc.setup: create an event loop and multiple event handlers
68      */
69     printf("SetUp.\n");
70     InitSeLinuxEnabled();
71 }
72 
TearDown()73 void EventloggerCatcherTest::TearDown()
74 {
75     /**
76      * @tc.teardown: destroy the event loop we have created
77      */
78     CancelSeLinuxEnabled();
79     printf("TearDown.\n");
80 }
81 
82 /**
83  * @tc.name: EventLogCatcher
84  * @tc.desc: test EventLogCatcher
85  * @tc.type: FUNC
86  */
87 HWTEST_F(EventloggerCatcherTest, EventLogCatcherTest_001, TestSize.Level3)
88 {
89     auto eventLogCatcher = std::make_shared<EventLogCatcher>();
90     EXPECT_TRUE(eventLogCatcher->GetLogSize() == -1);
91     eventLogCatcher->SetLogSize(1);
92     EXPECT_TRUE(eventLogCatcher->GetLogSize() == 1);
93     EXPECT_TRUE(eventLogCatcher->AppendFile(-1, "") == 0);
94     auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
95     if (fd < 0) {
96         printf("Fail to create catcherFile. errno: %d\n", errno);
97         FAIL();
98     }
99     int res = eventLogCatcher->Catch(fd, 1);
100     EXPECT_TRUE(res == 0);
101     std::string fileName = "/data/test/catcherFile";
102     eventLogCatcher->AppendFile(fd, fileName);
103     EXPECT_TRUE(eventLogCatcher->AppendFile(fd, "") == 0);
104     close(fd);
105 }
106 
107 /**
108  * @tc.name: EventlogTask
109  * @tc.desc: test EventLogTask
110  * @tc.type: FUNC
111  */
112 HWTEST_F(EventloggerCatcherTest, EventlogTask_001, TestSize.Level3)
113 {
114     auto fd = open("/data/test/testFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
115     if (fd < 0) {
116         printf("Fail to create testFile. errno: %d\n", errno);
117         FAIL();
118     }
119     SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
120     std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
121     std::unique_ptr<EventLogTask> logTask = std::make_unique<EventLogTask>(fd, 1, sysEvent);
122     logTask->AddStopReason(fd, nullptr, "Test");
123     auto eventLogCatcher = std::make_shared<EventLogCatcher>();
124     logTask->AddStopReason(fd, eventLogCatcher, "Test");
125     logTask->AddSeparator(fd, eventLogCatcher);
126     bool ret = logTask->ShouldStopLogTask(fd, 1, -1, eventLogCatcher);
127     EXPECT_EQ(ret, false);
128     ret = logTask->ShouldStopLogTask(fd, 1, 20000, eventLogCatcher);
129     EXPECT_EQ(ret, false);
130     logTask->status_ = EventLogTask::Status::TASK_TIMEOUT;
131     ret = logTask->ShouldStopLogTask(fd, 0, 1, eventLogCatcher);
132     EXPECT_EQ(ret, true);
133     close(fd);
134 }
135 
136 /**
137  * @tc.name: EventlogTask
138  * @tc.desc: test EventlogTask
139  * @tc.type: FUNC
140  */
141 HWTEST_F(EventloggerCatcherTest, EventlogTask_002, TestSize.Level3)
142 {
143     auto fd = open("/data/test/testFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
144     if (fd < 0) {
145         printf("Fail to create testFile. errno: %d\n", errno);
146         FAIL();
147     }
148     SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
149     std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
150     std::unique_ptr<EventLogTask> logTask = std::make_unique<EventLogTask>(fd, 1, sysEvent);
151     EXPECT_EQ(logTask->GetTaskStatus(), EventLogTask::Status::TASK_RUNNABLE);
152     auto ret = logTask->StartCompose();
153     EXPECT_EQ(ret, 2);
154     EXPECT_EQ(logTask->GetTaskStatus(), EventLogTask::Status::TASK_RUNNING);
155     ret = logTask->StartCompose();
156     EXPECT_EQ(ret, 1);
157     EXPECT_EQ(logTask->GetLogSize(), 0);
158     logTask->GetThermalInfo(fd);
159     close(fd);
160 }
161 
162 /**
163  * @tc.name: EventlogTask
164  * @tc.desc: test EventlogTask
165  * @tc.type: FUNC
166  */
167 HWTEST_F(EventloggerCatcherTest, EventlogTask_003, TestSize.Level3)
168 {
169     auto fd = open("/data/test/testFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
170     if (fd < 0) {
171         printf("Fail to create testFile. errno: %d\n", errno);
172         FAIL();
173     }
174     SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
175     std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
176     std::unique_ptr<EventLogTask> logTask = std::make_unique<EventLogTask>(fd, 1, sysEvent);
177     logTask->AddLog("cmd:scbCS");
178 #ifdef STACKTRACE_CATCHER_ENABLE
179     logTask->AppStackCapture();
180     logTask->SystemStackCapture();
181     logTask->RemoteStackCapture();
182     logTask->GetGPUProcessStack();
183 #endif // STACKTRACE_CATCHER_ENABLE
184 
185 #ifdef BINDER_CATCHER_ENABLE
186     logTask->BinderLogCapture();
187     EXPECT_EQ(logTask->PeerBinderCapture("Test"), false);
188     EXPECT_EQ(logTask->PeerBinderCapture("pb"), false);
189     EXPECT_EQ(logTask->PeerBinderCapture("pb:1:a"), true);
190 #endif // BINDER_CATCHER_ENABLE
191 
192 #ifdef HILOG_CATCHER_ENABLE
193     logTask->HilogCapture();
194     logTask->LightHilogCapture();
195     logTask->InputHilogCapture();
196 #endif // HILOG_CATCHER_ENABLE
197 
198 #ifdef SCB_CATCHER_ENABLE
199     logTask->SCBSessionCapture();
200     logTask->SCBViewParamCapture();
201     logTask->SCBWMSCapture();
202     logTask->SCBWMSEVTCapture();
203 #endif // SCB_CATCHER_ENABLE
204 
205 #ifdef USAGE_CATCHER_ENABLE
206     logTask->DumpAppMapCapture();
207     logTask->WMSUsageCapture();
208     logTask->AMSUsageCapture();
209     logTask->PMSUsageCapture();
210     logTask->DPMSUsageCapture();
211     logTask->RSUsageCapture();
212     logTask->MemoryUsageCapture();
213     logTask->CpuUsageCapture();
214 #endif // USAGE_CATCHER_ENABLE
215 
216 #ifdef DMESG_CATCHER_ENABLE
217     logTask->DmesgCapture();
218     logTask->SysrqCapture(true);
219 #endif // DMESG_CATCHER_ENABLE
220 
221 #ifdef OTHER_CATCHER_ENABLE
222     logTask->Screenshot();
223     logTask->FfrtCapture();
224     logTask->DMSUsageCapture();
225     logTask->MMIUsageCapture();
226     logTask->EECStateCapture();
227     logTask->GECStateCapture();
228     logTask->UIStateCapture();
229 #endif // OTHER_CATCHER_ENABLE
230 
231 #ifdef HITRACE_CATCHER_ENABLE
232     logTask->HitraceCapture();
233 #endif // HITRACE_CATCHER_ENABLE
234 
235     logTask->AddLog("Test");
236     logTask->AddLog("cmd:w");
237     logTask->status_ = EventLogTask::Status::TASK_RUNNING;
238     auto ret = logTask->StartCompose();
239     printf("task size: %d\n", static_cast<int>(logTask->tasks_.size()));
240 
241     close(fd);
242 }
243 
244 #ifdef STACKTRACE_CATCHER_ENABLE
245 /**
246  * @tc.name: EventlogTask
247  * @tc.desc: test EventlogTask
248  * @tc.type: FUNC
249  */
250 HWTEST_F(EventloggerCatcherTest, EventlogTask_004, TestSize.Level3)
251 {
252     auto fd = open("/data/test/testFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
253     if (fd < 0) {
254         printf("Fail to create testFile. errno: %d\n", errno);
255         FAIL();
256     }
257     SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
258     std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
259     std::unique_ptr<EventLogTask> logTask = std::make_unique<EventLogTask>(fd, 1, sysEvent);
260     logTask->GetSpecificProcessStack();
261     sysEvent->SetEventValue("SPECIFICSTACK_NAME", "EventloggerCatcherTest");
262     logTask->GetSpecificProcessStack();
263     EXPECT_TRUE(logTask != nullptr);
264 }
265 #endif // STACKTRACE_CATCHER_ENABLE
266 
267 #ifdef BINDER_CATCHER_ENABLE
268 /**
269  * @tc.name: BinderCatcherTest_001
270  * @tc.desc: add testcase code coverage
271  * @tc.type: FUNC
272  */
273 HWTEST_F(EventloggerCatcherTest, BinderCatcherTest_001, TestSize.Level1)
274 {
275     auto binderCatcher = std::make_shared<BinderCatcher>();
276     bool ret = binderCatcher->Initialize("test", 1, 2);
277     EXPECT_EQ(ret, true);
278     auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
279     if (fd < 0) {
280         printf("Fail to create catcherFile. errno: %d\n", errno);
281         FAIL();
282     }
283     int res = binderCatcher->Catch(fd, 1);
284     EXPECT_TRUE(res > 0);
285     close(fd);
286 }
287 #endif // BINDER_CATCHER_ENABLE
288 
289 #ifdef USAGE_CATCHER_ENABLE
290 /**
291  * @tc.name: MemoryCatcherTest_001
292  * @tc.desc: add testcase code coverage
293  * @tc.type: FUNC
294  */
295 HWTEST_F(EventloggerCatcherTest, MemoryCatcherTest_001, TestSize.Level1)
296 {
297     auto memoryCatcher = std::make_shared<MemoryCatcher>();
298     bool ret = memoryCatcher->Initialize("test", 1, 2);
299     EXPECT_EQ(ret, true);
300     auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
301     if (fd < 0) {
302         printf("Fail to create catcherFile. errno: %d\n", errno);
303         FAIL();
304     }
305     int res = memoryCatcher->Catch(fd, 1);
306     EXPECT_TRUE(res > 0);
307     res = memoryCatcher->Catch(0, 1);
308     EXPECT_EQ(res, 0);
309     printf("memoryCatcher result: %d\n", res);
310     close(fd);
311 }
312 
313 /**
314  * @tc.name: MemoryCatcherTest_002
315  * @tc.desc: EventloggerCatcherTest
316  * @tc.type: FUNC
317  */
318 HWTEST_F(EventloggerCatcherTest, MemoryCatcherTest_002, TestSize.Level3)
319 {
320     auto memoryCatcher = std::make_shared<MemoryCatcher>();
321     EXPECT_EQ(memoryCatcher->GetStringFromFile("/data/log/test"), "");
322 }
323 
324 /**
325  * @tc.name: MemoryCatcherTest_003
326  * @tc.desc: EventloggerCatcherTest
327  * @tc.type: FUNC
328  */
329 HWTEST_F(EventloggerCatcherTest, MemoryCatcherTest_003, TestSize.Level3)
330 {
331     auto memoryCatcher = std::make_shared<MemoryCatcher>();
332     int ret = memoryCatcher->GetNumFromString("abc");
333     EXPECT_EQ(ret, 0);
334     ret = memoryCatcher->GetNumFromString("100");
335     EXPECT_EQ(ret, 100);
336 }
337 
338 /**
339  * @tc.name: MemoryCatcherTest_004
340  * @tc.desc: EventloggerCatcherTest
341  * @tc.type: FUNC
342  */
343 HWTEST_F(EventloggerCatcherTest, MemoryCatcherTest_004, TestSize.Level3)
344 {
345     auto memoryCatcher = std::make_shared<MemoryCatcher>();
346     std::string data;
347     memoryCatcher->CheckString(0, "abc: 100", data, "abc", "/data/log/test");
348     EXPECT_TRUE(data.empty());
349 }
350 
351 /**
352  * @tc.name: MemoryCatcherTest_004
353  * @tc.desc: EventloggerCatcherTest
354  * @tc.type: FUNC
355  */
356 HWTEST_F(EventloggerCatcherTest, MemoryCatcherTest_005, TestSize.Level3)
357 {
358     auto memoryCatcher = std::make_shared<MemoryCatcher>();
359     std::string memInfo = memoryCatcher->CollectFreezeSysMemory();
360     EXPECT_TRUE(!memInfo.empty());
361 }
362 
363 /**
364  * @tc.name: MemoryCatcherTest_006
365  * @tc.desc: EventloggerCatcherTest
366  * @tc.type: FUNC
367  */
368 HWTEST_F(EventloggerCatcherTest, MemoryCatcherTest_006, TestSize.Level3)
369 {
370     auto fd = open("/data/test/testFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
371     if (fd < 0) {
372         printf("Fail to create testFile. errno: %d\n", errno);
373         FAIL();
374     }
375     SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
376     std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
377     sysEvent->SetEventValue("FREEZE_MEMORY", "test\\ntest");
378     std::unique_ptr<EventLogTask> logTask = std::make_unique<EventLogTask>(fd, 1, sysEvent);
379     logTask->MemoryUsageCapture();
380     EXPECT_TRUE(logTask != nullptr);
381 }
382 #endif // USAGE_CATCHER_ENABLE
383 
384 #ifdef OTHER_CATCHER_ENABLE
385 /**
386  * @tc.name: FfrtCatcherTest_001
387  * @tc.desc: add testcase code coverage
388  * @tc.type: FUNC
389  */
390 HWTEST_F(EventloggerCatcherTest, FfrtCatcherTest_001, TestSize.Level1)
391 {
392     auto fd = open("/data/test/FfrtCatcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
393     if (fd < 0) {
394         printf("Fail to create FfrtCatcherFile. errno: %d\n", errno);
395         FAIL();
396     }
397 
398     auto ffrtCatcher = std::make_shared<FfrtCatcher>();
399     int pid = CommonUtils::GetPidByName("foundation");
400     if (pid > 0) {
401         bool res = ffrtCatcher->Initialize("", pid, 0);
402         EXPECT_TRUE(res);
403 
404         int jsonFd = 1;
405         EXPECT_TRUE(ffrtCatcher->Catch(fd, jsonFd) > 0);
406     }
407     EXPECT_TRUE(true);
408     close(fd);
409 }
410 
411 /**
412  * @tc.name: FfrtCatcherTest_002
413  * @tc.desc: add testcase code coverage
414  * @tc.type: FUNC
415  */
416 HWTEST_F(EventloggerCatcherTest, FfrtCatcherTest_002, TestSize.Level1)
417 {
418     auto ffrtCatcher = std::make_shared<FfrtCatcher>();
419     bool ret = ffrtCatcher->Initialize("test", 1, 2);
420     EXPECT_EQ(ret, true);
421     auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
422     if (fd < 0) {
423         printf("Fail to create catcherFile. errno: %d\n", errno);
424         FAIL();
425     }
426     int res = ffrtCatcher->Catch(fd, 1);
427     EXPECT_TRUE(res > 0);
428     res = ffrtCatcher->Catch(0, 1);
429     EXPECT_EQ(res, 0);
430     printf("ffrtCatcher result: %d\n", res);
431     close(fd);
432 }
433 #endif // OTHER_CATCHER_ENABLE
434 
435 #ifdef DMESG_CATCHER_ENABLE
436 /**
437  * @tc.name: DmesgCatcherTest_001
438  * @tc.desc: add testcase code coverage
439  * @tc.type: FUNC
440  */
441 HWTEST_F(EventloggerCatcherTest, DmesgCatcherTest_001, TestSize.Level1)
442 {
443     auto dmesgCatcher = std::make_shared<DmesgCatcher>();
444     auto jsonStr = "{\"domain_\":\"KERNEL_VENDOR\"}";
445     std::shared_ptr<SysEvent> event = std::make_shared<SysEvent>("DmesgCatcherTest_001",
446         nullptr, jsonStr);
447     event->eventId_ = 0;
448     event->domain_ = "KERNEL_VENDOR";
449     event->eventName_ = "HUNGTASK";
450     event->SetEventValue("PID", 0);
451     EXPECT_TRUE(dmesgCatcher->Init(event));
452 }
453 
454 /**
455  * @tc.name: DmesgCatcherTest_002
456  * @tc.desc: add testcase code coverage
457  * @tc.type: FUNC
458  */
459 HWTEST_F(EventloggerCatcherTest, DmesgCatcherTest_002, TestSize.Level1)
460 {
461     auto dmesgCatcher = std::make_shared<DmesgCatcher>();
462     auto fd = open("/data/test/dmesgCatcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
463     if (fd < 0) {
464         printf("Fail to create dmesgCatcherFile. errno: %d\n", errno);
465         FAIL();
466     }
467     dmesgCatcher->Initialize("", 0, 0);
468     int jsonFd = 1;
469     EXPECT_TRUE(dmesgCatcher->Catch(fd, jsonFd) > 0);
470 
471     dmesgCatcher->Initialize("", 0, 1);
472     EXPECT_TRUE(dmesgCatcher->Catch(fd, jsonFd) > 0);
473 
474     dmesgCatcher->Initialize("", 1, 0);
475     printf("dmesgCatcher result: %d\n", dmesgCatcher->Catch(fd, jsonFd));
476 
477     dmesgCatcher->Initialize("", 1, 1);
478     printf("dmesgCatcher result: %d\n", dmesgCatcher->Catch(fd, jsonFd));
479 
480     close(fd);
481 }
482 
483 /**
484  * @tc.name: DmesgCatcherTest_003
485  * @tc.desc: add testcase code coverage
486  * @tc.type: FUNC
487  */
488 HWTEST_F(EventloggerCatcherTest, DmesgCatcherTest_003, TestSize.Level1)
489 {
490     auto jsonStr = "{\"domain_\":\"KERNEL_VENDOR\"}";
491     std::shared_ptr<SysEvent> event = std::make_shared<SysEvent>("DmesgCatcherTest_003",
492         nullptr, jsonStr);
493     event->SetEventValue("SYSRQ_TIME", "20250124");
494     auto dmesgCatcher = std::make_shared<DmesgCatcher>();
495     dmesgCatcher->Init(event);
496     bool ret = dmesgCatcher->DumpDmesgLog(-1);
497     EXPECT_EQ(ret, false);
498     auto fd = open("/data/test/dmesgCatcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
499     if (fd < 0) {
500         printf("Fail to create dmesgCatcherFile. errno: %d\n", errno);
501         FAIL();
502     }
503     ret = dmesgCatcher->DumpDmesgLog(fd);
504     EXPECT_EQ(ret, true);
505     ret = dmesgCatcher->WriteSysrq();
506     EXPECT_EQ(ret, true);
507     close(fd);
508 }
509 #endif // DMESG_CATCHER_ENABLE
510 
511 #ifdef STACKTRACE_CATCHER_ENABLE
512 /**
513  * @tc.name: OpenStacktraceCatcherTest_001
514  * @tc.desc: add testcase code coverage
515  * @tc.type: FUNC
516  */
517 HWTEST_F(EventloggerCatcherTest, OpenStacktraceCatcherTest_001, TestSize.Level1)
518 {
519     auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
520     if (fd < 0) {
521         printf("Fail to create catcherFile. errno: %d\n", errno);
522         FAIL();
523     }
524 
525     auto openStackCatcher = std::make_shared<OpenStacktraceCatcher>();
526     ASSERT_EQ(openStackCatcher->Initialize("", 0, 0), false);
527 
528     int jsonFd = 1;
529     bool ret = openStackCatcher->Catch(fd, jsonFd);
530     EXPECT_TRUE(ret == 0);
531 
532     EXPECT_EQ(openStackCatcher->Initialize("test", 0, 0), false);
533     ret = openStackCatcher->Catch(fd, jsonFd);
534     EXPECT_TRUE(ret == 0);
535 
536     EXPECT_EQ(openStackCatcher->Initialize("", 1, 0), true);
537     EXPECT_EQ(openStackCatcher->Initialize("test", 1, 0), true);
538     ret = openStackCatcher->Catch(fd, jsonFd);
539     EXPECT_TRUE(ret > 0);
540     close(fd);
541 }
542 
543 /**
544  * @tc.name: OpenStacktraceCatcherTest_002
545  * @tc.desc: add testcase code coverage
546  * @tc.type: FUNC
547  */
548 HWTEST_F(EventloggerCatcherTest, OpenStacktraceCatcherTest_002, TestSize.Level1)
549 {
550     auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
551     if (fd < 0) {
552         printf("Fail to create catcherFile. errno: %d\n", errno);
553         FAIL();
554     }
555 
556     auto openStackCatcher = std::make_shared<OpenStacktraceCatcher>();
557     bool ret = openStackCatcher->Catch(fd, 1);
558     EXPECT_TRUE(ret == 0);
559     EXPECT_EQ(openStackCatcher->ForkAndDumpStackTrace(fd), 0);
560     close(fd);
561 }
562 #endif // STACKTRACE_CATCHER_ENABLE
563 
564 #ifdef BINDER_CATCHER_ENABLE
565 /**
566  * @tc.name: PeerBinderCatcherTest_001
567  * @tc.desc: add testcase code coverage
568  * @tc.type: FUNC
569  */
570 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_001, TestSize.Level1)
571 {
572     auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
573     if (fd < 0) {
574         printf("Fail to create catcherFile. errno: %d\n", errno);
575         FAIL();
576     }
577     auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
578     peerBinderCatcher->Initialize("PeerBinderCatcherTest", 0, 0);
579     int jsonFd = 1;
580     int res = peerBinderCatcher->Catch(fd, jsonFd);
581     EXPECT_TRUE(res < 0);
582 
583     peerBinderCatcher->Initialize("a", 0, 0);
584     peerBinderCatcher->Initialize("a", 1, 0);
585     res = peerBinderCatcher->Catch(fd, jsonFd);
586     EXPECT_TRUE(res < 0);
587 
588     peerBinderCatcher->Initialize("a", 1, 1);
589     res = peerBinderCatcher->Catch(fd, jsonFd);
590     EXPECT_TRUE(res > 0);
591 
592     int pid = CommonUtils::GetPidByName("foundation");
593 #ifdef HAS_HIPERF
594     std::set<int> pids;
595     pids.insert(pid);
596     peerBinderCatcher->DoExecHiperf("peerBinderCatcher", pids, pid, "r");
597 #endif
598     peerBinderCatcher->Initialize("", 0, pid);
599     peerBinderCatcher->Initialize("foundation", 0, pid);
600     peerBinderCatcher->Initialize("foundation", 1, pid);
601     peerBinderCatcher->CatcherFfrtStack(fd, pid);
602     peerBinderCatcher->CatcherStacktrace(fd, pid);
603     close(fd);
604 }
605 
606 /**
607  * @tc.name: PeerBinderCatcherTest_002
608  * @tc.desc: add testcase code coverage
609  * @tc.type: FUNC
610  */
611 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_002, TestSize.Level1)
612 {
613     auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
614     if (fd < 0) {
615         printf("Fail to create catcherFile. errno: %d\n", errno);
616         FAIL();
617     }
618     auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
619     peerBinderCatcher->Initialize("a", 1, 0);
620     auto jsonStr = "{\"domain_\":\"KERNEL_VENDOR\"}";
621     std::shared_ptr<SysEvent> event = std::make_shared<SysEvent>("PeerBinderCatcherTest_002",
622         nullptr, jsonStr);
623     event->eventId_ = 0;
624     event->domain_ = "KERNEL_VENDOR";
625     event->eventName_ = "HUNGTASK";
626     event->SetEventValue("PID", 0);
627     std::string filePath = "/data/test/catcherFile";
628     std::set<int> catchedPids;
629     catchedPids.insert(0);
630     catchedPids.insert(1);
631     int pid = CommonUtils::GetPidByName("foundation");
632     catchedPids.insert(pid);
633     peerBinderCatcher->Init(event, filePath, catchedPids);
634     peerBinderCatcher->Initialize("foundation", 1, pid);
635     int res = peerBinderCatcher->Catch(fd, 1);
636     EXPECT_GT(res, 0);
637     close(fd);
638 }
639 
640 /**
641  * @tc.name: PeerBinderCatcherTest_003
642  * @tc.desc: add testcase code coverage
643  * @tc.type: FUNC
644  */
645 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_003, TestSize.Level1)
646 {
647     std::set<int> pids;
648     pids.insert(0);
649     pids.insert(2);
650     auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
651     PeerBinderCatcher::BinderInfo info = {
652         .clientPid = 1,
653         .clientTid = 0,
654         .serverPid = 1,
655         .serverTid = 0,
656         .wait = 1
657     };
658     std::map<int, std::list<OHOS::HiviewDFX::PeerBinderCatcher::BinderInfo>> manager;
659     manager[info.clientPid].push_back(info);
660     peerBinderCatcher->ParseBinderCallChain(manager, pids, 0);
661     PeerBinderCatcher::BinderInfo info1 = {
662         .clientPid = 2,
663         .clientTid = 0,
664         .serverPid = 2,
665         .serverTid = 0,
666         .wait = 0
667     };
668     manager[info1.clientPid].push_back(info1);
669     peerBinderCatcher->ParseBinderCallChain(manager, pids, 1);
670     EXPECT_TRUE(!pids.empty());
671 #ifdef HAS_HIPERF
672     pids.insert(3);
673     pids.insert(4);
674     int processId = getpid();
675     std::string perfCmd = "r";
676     peerBinderCatcher->DoExecHiperf("peerBinderCatcher", pids, processId, perfCmd);
677     peerBinderCatcher->DumpHiperf(pids, processId, perfCmd);
678 #endif
679 }
680 
681 /**
682  * @tc.name: PeerBinderCatcherTest_004
683  * @tc.desc: add testcase code coverage
684  * @tc.type: FUNC
685  */
686 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_004, TestSize.Level1)
687 {
688     auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
689     if (fd < 0) {
690         printf("Fail to create catcherFile. errno: %d\n", errno);
691         FAIL();
692     }
693     auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
694     std::set<int> asyncPids;
695     std::set<int> pids = peerBinderCatcher->GetBinderPeerPids(fd, 1, asyncPids);
696     EXPECT_TRUE(pids.empty());
697 }
698 
699 /**
700  * @tc.name: PeerBinderCatcherTest_005
701  * @tc.desc: add testcase code coverage
702  * @tc.type: FUNC
703  */
704 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_005, TestSize.Level1)
705 {
706     auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
707     std::list<PeerBinderCatcher::OutputBinderInfo> infoList;
708     peerBinderCatcher->AddBinderJsonInfo(infoList, -1);
709     PeerBinderCatcher::OutputBinderInfo info = {
710         .info = "Test",
711         .pid = 0
712     };
713     infoList.push_back(info);
714     peerBinderCatcher->AddBinderJsonInfo(infoList, 1);
715     PeerBinderCatcher::OutputBinderInfo info1 = {
716         .info = "Test",
717         .pid = getpid()
718     };
719     infoList.push_back(info1);
720     peerBinderCatcher->AddBinderJsonInfo(infoList, 1);
721     std::string str = "/proc/" + std::to_string(getpid()) + "/cmdline";
722     printf("%s\n", str.c_str());
723     EXPECT_TRUE(!str.empty());
724 }
725 
726 /**
727  * @tc.name: PeerBinderCatcherTest_006
728  * @tc.desc: add testcase code coverage
729  * @tc.type: FUNC
730  */
731 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_006, TestSize.Level1)
732 {
733     auto fd = open("/data/test/peerFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
734     if (fd < 0) {
735         printf("Fail to create peerFile. errno: %d\n", errno);
736         FAIL();
737     }
738     std::ofstream testFile;
739     std::string path = "/data/test/peerFile";
740     testFile.open(path);
741     testFile << "0:pid\tcontext:binder\t0:request\t3:started\t"
742         "16:max\t4:ready\t521092:free_space\n";
743     testFile.close();
744 
745     auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
746     std::ifstream fin;
747     fin.open(path.c_str());
748     if (!fin.is_open()) {
749         printf("open binder file failed, %s\n.", path.c_str());
750         FAIL();
751     }
752     auto fd1 = open("/data/test/peerTestFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
753     if (fd1 < 0) {
754         printf("Fail to create peerTestFile. errno: %d\n", errno);
755         FAIL();
756     }
757     std::set<int> asyncPids;
758     peerBinderCatcher->BinderInfoParser(fin, fd1, 1, asyncPids);
759     std::set<int> pids = peerBinderCatcher->GetBinderPeerPids(fd, 1, asyncPids);
760     EXPECT_TRUE(pids.empty());
761     pids = peerBinderCatcher->GetBinderPeerPids(-1, 1, asyncPids);
762     EXPECT_TRUE(pids.empty());
763     fin.close();
764 }
765 
766 /**
767  * @tc.name: PeerBinderCatcherTest_007
768  * @tc.desc: add testcase code coverage
769  * @tc.type: FUNC
770  */
771 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_007, TestSize.Level1)
772 {
773     auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
774     bool ret = peerBinderCatcher->IsAncoProc(getpid());
775     EXPECT_TRUE(!ret);
776 }
777 #endif // BINDER_CATCHER_ENABLE
778 
779 /**
780  * @tc.name: ShellCatcherTest
781  * @tc.desc: add testcase code coverage
782  * @tc.type: FUNC
783  */
784 HWTEST_F(EventloggerCatcherTest, ShellCatcherTest_001, TestSize.Level1)
785 {
786     auto fd = open("/data/test/shellCatcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
787     if (fd < 0) {
788         printf("Fail to create shellCatcherFile. errno: %d\n", errno);
789         FAIL();
790     }
791 
792     auto shellCatcher = std::make_shared<ShellCatcher>();
793     int pid = CommonUtils::GetPidByName("foundation");
794 
795 #ifdef USAGE_CATCHER_ENABLE
796     bool res = shellCatcher->Initialize("", ShellCatcher::CATCHER_WMS, pid);
797     EXPECT_TRUE(res);
798 #endif // USAGE_CATCHER_ENABLE
799 
800     int jsonFd = 1;
801     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) < 0);
802 
803     std::string cmd = "ShellCatcherTest_001";
804 #ifdef USAGE_CATCHER_ENABLE
805     shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_WMS, pid);
806     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
807 
808     shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_AMS, pid);
809     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
810 
811     shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_CPU, pid);
812     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
813 
814     shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_PMS, pid);
815     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
816     shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_DPMS, pid);
817     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
818 
819     shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_RS, pid);
820     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
821 #endif // USAGE_CATCHER_ENABLE
822 
823 #ifdef HILOG_CATCHER_ENABLE
824     shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_HILOG, 0);
825     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
826 
827     shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_LIGHT_HILOG, 0);
828     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
829 #endif // HILOG_CATCHER_ENABLE
830 
831     shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_SNAPSHOT, 0);
832     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
833     close(fd);
834 }
835 
836 /**
837  * @tc.name: ShellCatcherTest
838  * @tc.desc: GET_DISPLAY_SNAPSHOT test
839  * @tc.type: FUNC
840  */
841 HWTEST_F(EventloggerCatcherTest, ShellCatcherTest_002, TestSize.Level1)
842 {
843     auto fd = open("/data/test/shellCatcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
844     if (fd < 0) {
845         printf("Fail to create shellCatcherFile. errno: %d\n", errno);
846         FAIL();
847     }
848     auto shellCatcher = std::make_shared<ShellCatcher>();
849     int jsonFd = 1;
850     std::string cmd = "ShellCatcherTest_002";
851 #ifdef HILOG_CATCHER_ENABLE
852     shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_INPUT_HILOG, 0);
853     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) >= 0);
854     shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_INPUT_EVENT_HILOG, 0);
855     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) >= 0);
856 #endif // HILOG_CATCHER_ENABLE
857 
858 #ifdef OTHER_CATCHER_ENABLE
859     shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_EEC, 0);
860     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) >= 0);
861     shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_GEC, 0);
862     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) >= 0);
863     shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_UI, 0);
864     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) >= 0);
865     shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_SNAPSHOT, 0);
866     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
867     shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_MMI, 0);
868     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) >= 0);
869     shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_DMS, 0);
870     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) >= 0);
871 #endif // OTHER_CATCHER_ENABLE
872     auto jsonStr = "{\"domain_\":\"KERNEL_VENDOR\"}";
873     std::shared_ptr<SysEvent> event = std::make_shared<SysEvent>("ShellCatcherTest", nullptr, jsonStr);
874     event->SetValue("FOCUS_WINDOW", 4); // 4 test value
875     shellCatcher->SetEvent(event);
876 #ifdef SCB_CATCHER_ENABLE
877     shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_SCBWMSEVT, 0);
878     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) >= 0);
879     shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_SCBVIEWPARAM, 0);
880     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) >= 0);
881     shellCatcher->Initialize(cmd, ShellCatcher::CATCHER_SCBWMS, 0);
882     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) >= 0);
883 #endif // SCB_CATCHER_ENABLE
884     close(fd);
885 }
886 
887 /**
888  * @tc.name: ShellCatcherTest
889  * @tc.desc: add test
890  * @tc.type: FUNC
891  */
892 HWTEST_F(EventloggerCatcherTest, ShellCatcherTest_003, TestSize.Level1)
893 {
894     auto shellCatcher = std::make_shared<ShellCatcher>();
895     shellCatcher->SetFocusWindowId("ShellCatcherTest_003");
896     EXPECT_TRUE(!shellCatcher->focusWindowId_.empty());
897 }
898 
899 /**
900  * @tc.name: LogCatcherUtilsTest_001
901  * @tc.desc: add test
902  * @tc.type: FUNC
903  */
904 HWTEST_F(EventloggerCatcherTest, LogCatcherUtilsTest_001, TestSize.Level1)
905 {
906     int pid = getpid();
907     std::string threadStack;
908     int ret = LogCatcherUtils::DumpStacktrace(-1, pid, threadStack);
909     EXPECT_EQ(ret, -1);
910     LogCatcherUtils::DumpStacktrace(1, pid, threadStack);
911     EXPECT_TRUE(threadStack.empty());
912     LogCatcherUtils::DumpStacktrace(2, pid, threadStack);
913     ret = LogCatcherUtils::WriteKernelStackToFd(200, "Test\n", getprocpid());
914     EXPECT_EQ(ret, 0);
915     ret = LogCatcherUtils::WriteKernelStackToFd(200, "Test\n", getprocpid());
916     EXPECT_EQ(ret, 0);
917     ret = LogCatcherUtils::WriteKernelStackToFd(2, "Test", -1);
918     EXPECT_EQ(ret, -1);
919     ret = LogCatcherUtils::WriteKernelStackToFd(300, "Test\n", getprocpid());
920     EXPECT_EQ(ret, 0);
921     ret = LogCatcherUtils::WriteKernelStackToFd(3, "Test", -1);
922     EXPECT_EQ(ret, -1);
923 }
924 
925 /**
926  * @tc.name: LogCatcherUtilsTest_002
927  * @tc.desc: add test
928  * @tc.type: FUNC
929  */
930 HWTEST_F(EventloggerCatcherTest, LogCatcherUtilsTest_002, TestSize.Level1)
931 {
932     std::string processStack = "LogCatcherUtilsTest_002";
933     std::string stack = "";
934     LogCatcherUtils::GetThreadStack(processStack, stack, 0);
935     EXPECT_TRUE(stack.empty());
936     int tid = gettid();
937     processStack = "Tid:1234, Name: TestThread\n#00 pc 0017888c /system/lib/libark_jsruntime.so\n"
938         "#01 pc 00025779 /system/lib/platformsdk/libipc_core.z.so";
939     LogCatcherUtils::GetThreadStack(processStack, stack, tid);
940 }
941 
942 /**
943  * @tc.name: LogCatcherUtilsTest_003
944  * @tc.desc: add test
945  * @tc.type: FUNC
946  */
947 HWTEST_F(EventloggerCatcherTest, LogCatcherUtilsTest_003, TestSize.Level1)
948 {
949     auto fd = open("/data/test/logCatcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
950     if (fd < 0) {
951         printf("Fail to create logCatcherFile. errno: %d\n", errno);
952         FAIL();
953     }
954     int ret = LogCatcherUtils::DumpStackFfrt(fd, "");
955     EXPECT_EQ(ret, 0);
956 }
957 
958 /**
959  * @tc.name: LogCatcherUtilsTest_004
960  * @tc.desc: add test
961  * @tc.type: FUNC
962  */
963 HWTEST_F(EventloggerCatcherTest, LogCatcherUtilsTest_004, TestSize.Level1)
964 {
965     auto fd = open("/data/test/logCatcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
966     if (fd < 0) {
967         printf("Fail to create logCatcherFile. errno: %d\n", errno);
968         FAIL();
969     }
970     std::string serviceName = "ApplicationManagerService";
971     std::string cmd = "Test";
972     int count = 0;
973     LogCatcherUtils::ReadShellToFile(fd, serviceName, cmd, count);
974     EXPECT_EQ(count, 0);
975 }
976 } // namespace HiviewDFX
977 } // namespace OHOS
978