• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
2  * Copyright (c) 2021 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 
22 #include <fcntl.h>
23 #include <sys/prctl.h>
24 #include <unistd.h>
25 
26 #include "securec.h"
27 #include "common_utils.h"
28 #include "file_util.h"
29 #define private public
30 #include "dmesg_catcher.h"
31 #include "event_log_task.h"
32 #include "open_stacktrace_catcher.h"
33 #include "shell_catcher.h"
34 #include "peer_binder_catcher.h"
35 #undef private
36 #include "binder_catcher.h"
37 #include "event_logger.h"
38 #include "event_log_catcher.h"
39 #include "sys_event.h"
40 
41 using namespace testing::ext;
42 using namespace OHOS::HiviewDFX;
43 
44 namespace OHOS {
45 namespace HiviewDFX {
46 constexpr mode_t DEFAULT_MODE = 0644;
SetUp()47 void EventloggerCatcherTest::SetUp()
48 {
49     /**
50      * @tc.setup: create an event loop and multiple event handlers
51      */
52     printf("SetUp.\n");
53     printf("path_ is %s\n", path_.c_str());
54 
55     sleep(1);
56     isSelinuxEnabled_ = false;
57     char buffer[BUF_SIZE_64] = {'\0'};
58     FILE* fp = popen("getenforce", "r");
59     if (fp != nullptr) {
60         fgets(buffer, sizeof(buffer), fp);
61         std::string str = buffer;
62         printf("buffer is %s\n", str.c_str());
63         if (str.find("Enforcing") != str.npos) {
64             printf("Enforcing %s\n", str.c_str());
65             isSelinuxEnabled_ = true;
66         } else {
67             printf("This isn't Enforcing %s\n", str.c_str());
68         }
69         pclose(fp);
70     } else {
71         printf("fp == nullptr\n");
72     }
73     system("setenforce 0");
74 
75     constexpr mode_t defaultLogDirMode = 0770;
76     if (!FileUtil::FileExists(path_)) {
77         FileUtil::ForceCreateDirectory(path_);
78         FileUtil::ChangeModeDirectory(path_, defaultLogDirMode);
79     }
80 }
81 
TearDown()82 void EventloggerCatcherTest::TearDown()
83 {
84     /**
85      * @tc.teardown: destroy the event loop we have created
86      */
87     if (isSelinuxEnabled_) {
88         system("setenforce 1");
89         isSelinuxEnabled_ = false;
90     }
91 
92     printf("TearDown.\n");
93 }
94 
GetFdSize(int32_t fd)95 int EventloggerCatcherTest::GetFdSize(int32_t fd)
96 {
97     struct stat fileStat;
98     if (fstat(fd, &fileStat) == -1) {
99         return 0;
100     }
101     return fileStat.st_size;
102 }
103 
GetFormatTime(unsigned long timestamp)104 std::string EventloggerCatcherTest::GetFormatTime(unsigned long timestamp)
105 {
106     struct tm tm;
107     time_t ts;
108     /* 20: the length of 'YYYYmmddHHMMSS' */
109     int strLen = 20;
110     ts = timestamp;
111     localtime_r(&ts, &tm);
112     char buf[strLen];
113 
114     (void)memset_s(buf, sizeof(buf), 0, strLen);
115     strftime(buf, strLen - 1, "%Y%m%d%H%M%S", &tm);
116     return std::string(buf, strlen(buf));
117 }
118 
JudgmentsFileSize(int minQuantity,const std::string sender)119 int EventloggerCatcherTest::JudgmentsFileSize(int minQuantity, const std::string sender)
120 {
121     constexpr mode_t defaultLogFileMode = 0644;
122     std::string logPath = path_ + "/" + logFile_;
123     auto fd2 = open(logPath.c_str(), O_RDONLY, defaultLogFileMode);
124     if (fd2 < 0) {
125         printf("second, Fail to create %s. fd2 == %d\n", logFile_.c_str(), fd2);
126         return RETURN_OPEN_FAIL2;
127     }
128     auto size = GetFdSize(fd2);
129     printf("%s, file size %d\n", sender.c_str(), size);
130     if (size < minQuantity) {
131         printf("error %s, less than size %d\n", sender.c_str(), minQuantity);
132         close(fd2);
133         return RETURN_LESS_THAN_SIZE;
134     }
135     return fd2;
136 }
137 
StartCreate(const std::string action,int pid,const std::string packageName,int interval,int minQuantity)138 int EventloggerCatcherTest::StartCreate(const std::string action, int pid,
139     const std::string packageName, int interval, int minQuantity)
140 {
141     auto eventlogger = std::make_unique<EventLogger>();
142     std::string name = "TEST" + packageName;
143     std::string jsonStr = R"~({"domain_":"demo","name_":")~" + name + R"~(","pid_":)~" +
144         std::to_string(pid) + R"~(,"tid_":6527,"PACKAGE_NAME":")~" + packageName + R"~("})~";
145     auto event = std::make_shared<SysEvent>("sender", nullptr, jsonStr);
146     std::time_t timeTmp = 0;
147     time(&timeTmp);
148     event->happenTime_ = timeTmp;
149     event->SetValue("eventLog_action", action);
150     event->SetValue("eventLog_interval", interval);
151 
152     printf("pid is %d, pid_ %d; packageName is %s, PACKAGE_NAME %s\n",
153         pid, event->GetPid(), packageName.c_str(), event->GetEventValue("PACKAGE_NAME").c_str());
154 
155     std::string idStr = event->eventName_;
156     logFile_ = idStr + "-" + GetFormatTime(event->happenTime_) + ".log";
157 
158     constexpr mode_t defaultLogFileMode = 0644;
159     std::string logPath = path_ + "/" + logFile_;
160     printf("logPath is %s\n", logPath.c_str());
161     auto fd = open(logPath.c_str(), O_CREAT | O_WRONLY | O_TRUNC, defaultLogFileMode);
162     if (fd < 0) {
163         printf("Fail to create %s. fd == %d\n", logFile_.c_str(), fd);
164         return RETURN_OPEN_FAIL;
165     }
166 
167     auto logTask = std::make_unique<EventLogTask>(fd, 1, event);
168     logTask->AddLog(event->GetValue("eventLog_action"));
169     auto ret = logTask->StartCompose();
170     if (ret != EventLogTask::TASK_SUCCESS) {
171         printf("capture fail %d", ret);
172         close(fd);
173         return RETURN_TASK_NO_SUCCESS;
174     }
175     close(fd);
176 
177     printf("logTask LogSize is %ld\n", logTask->GetLogSize());
178     if (logTask->GetLogSize() < minQuantity) {
179         printf("error LogSize less than %d\n", minQuantity);
180         return RETURN_TASK_LESS_THAN_SIZE;
181     }
182     return 0;
183 }
184 
185 /**
186  * @tc.name: EventloggerCatcherTest001
187  * @tc.desc: parse a correct config file and check result
188  * @tc.type: FUNC
189  * @tc.require: AR000FT62O
190  */
191 HWTEST_F(EventloggerCatcherTest, EventloggerCatcherTest001, TestSize.Level3)
192 {
193     /**
194      * @tc.steps: step1. create event handler and events
195      */
196     constexpr int minQuantity = 8000;
197     int pid = CommonUtils::GetPidByName("foundation");
198     auto ret = StartCreate("s", pid, "foundation", 0, minQuantity);
199     if (ret < 0) {
200         printf("EventloggerCatcherTest001 StartCreate is error ret == %d\n", ret);
201         FAIL();
202     }
203 
204     auto fd = JudgmentsFileSize(minQuantity, "EventloggerCatcherTest001");
205     if (fd < 0) {
206         printf("EventloggerCatcherTest001 JudgmentsFileSize is error ret == %d\n", fd);
207         FAIL();
208     }
209 
210     char readTmp[BUF_SIZE_256];
211     ret = -1;
212     while (read(fd, readTmp, BUF_SIZE_256)) {
213         std::string tmp = readTmp;
214         if (tmp.find("system/bin") != tmp.npos) {
215             ret = 0;
216             break;
217         }
218     }
219 
220     if (ret < 0) {
221         printf("not find Key Messages\n");
222         close(fd);
223         FAIL();
224     }
225     close(fd);
226 }
227 
228 /**
229  * @tc.name: EventloggerCatcherTest002
230  * @tc.desc: parse a correct config file and check result
231  * @tc.type: FUNC
232  * @tc.require: AR000FT62O
233  */
234 HWTEST_F(EventloggerCatcherTest, EventloggerCatcherTest002, TestSize.Level3)
235 {
236     /**
237      * @tc.steps: step1. create event handler and events
238      */
239     printf("EventloggerCatcherTest002 start\n");
240 
241     int pid = -1;
242     const int memSize = 1024 * 3;
243     if ((pid = fork()) < 0) {
244         printf("Fork error, err:%d", errno);
245         FAIL();
246     }
247 
248     // Creating a process with high usage
249     if (pid == 0) {
250         prctl(PR_SET_NAME, "EventlogTest02");
251         prctl(PR_SET_PDEATHSIG, SIGKILL);
252         printf("Fork pid == 0, child process\n");
253         volatile int temp[memSize] = {0};
254         while (true) {
255             int i = 0;
256             while (i < memSize) {
257                 temp[i] = i & 0x234567;
258                 i++;
259             }
260         }
261     }
262 
263     sleep(6);
264     constexpr int minQuantity = 500;
265     auto ret = StartCreate("cmd:c", pid, "EventlogTest02", 0, minQuantity);
266 
267     if (ret < 0) {
268         printf("EventloggerCatcherTest002 StartCreate is error ret == %d\n", ret);
269         FAIL();
270     }
271 
272     auto fd = JudgmentsFileSize(minQuantity, "EventloggerCatcherTest002");
273     if (fd < 0) {
274         printf("EventloggerCatcherTest002 JudgmentsFileSize is error ret == %d\n", fd);
275         FAIL();
276     }
277 
278     char readTmp[BUF_SIZE_256];
279     ret = -1;
280     while (read(fd, readTmp, BUF_SIZE_256)) {
281         std::string tmp = readTmp;
282         if (tmp.find("[cpuusage]") != tmp.npos) {
283             ret = 0;
284             break;
285         }
286     }
287 
288     if (ret < 0) {
289         printf("not find Key Messages\n");
290         close(fd);
291         FAIL();
292     }
293     close(fd);
294 }
295 
296 /**
297  * @tc.name: EventloggerCatcherTest003
298  * @tc.desc: parse a correct config file and check result
299  * @tc.type: FUNC
300  * @tc.require: AR000FT62O
301  */
302 HWTEST_F(EventloggerCatcherTest, EventloggerCatcherTest003, TestSize.Level3)
303 {
304     /**
305      * @tc.steps: step1. create event handler and events
306      */
307 
308     int pid = -1;
309     const int memSize = 1024 * 3;
310     if ((pid = fork()) < 0) {
311         printf("Fork error, err:%d", errno);
312         FAIL();
313     }
314 
315     // Creating a process with high memory
316     if (pid == 0) {
317         prctl(PR_SET_NAME, "EventlogTest03");
318         prctl(PR_SET_PDEATHSIG, SIGKILL);
319         printf("Fork pid == 0, child process\n");
320         volatile int temp[memSize] = {0};
321         while (true) {
322             int i = 0;
323             while (i < memSize) {
324                 temp[i] = i & 0x234567;
325                 i++;
326             }
327         }
328     }
329 
330     sleep(6);
331     constexpr int minQuantity = 500;
332     auto ret = StartCreate("cmd:m", pid, "EventlogTest03", 0, minQuantity);
333     if (ret < 0) {
334         printf("EventloggerCatcherTest003 is error ret == %d\n", ret);
335         FAIL();
336     }
337 
338     auto fd = JudgmentsFileSize(minQuantity, "EventloggerCatcherTest003");
339     if (fd < 0) {
340         printf("EventloggerCatcherTest003 JudgmentsFileSize is error ret == %d\n", fd);
341         FAIL();
342     }
343 
344     char readTmp[BUF_SIZE_256];
345     ret = -1;
346     while (read(fd, readTmp, BUF_SIZE_256)) {
347         std::string tmp = readTmp;
348         if (tmp.find("[memory]") != tmp.npos) {
349             ret = 0;
350             break;
351         }
352     }
353 
354     if (ret < 0) {
355         printf("not find Key Messages\n");
356         close(fd);
357         FAIL();
358     }
359     close(fd);
360 }
361 
362 /**
363  * @tc.name: EventLogCatcher
364  * @tc.desc: test EventLogCatcher
365  * @tc.type: FUNC
366  */
367 HWTEST_F(EventloggerCatcherTest, EventLogCatcherTest_001, TestSize.Level3)
368 {
369     auto eventLogCatcher = std::make_shared<EventLogCatcher>();
370     EXPECT_TRUE(eventLogCatcher->GetLogSize() == -1);
371     eventLogCatcher->SetLogSize(1);
372     EXPECT_TRUE(eventLogCatcher->GetLogSize() == 1);
373     EXPECT_TRUE(eventLogCatcher->AppendFile(-1, "") == 0);
374     auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
375     if (fd < 0) {
376         printf("Fail to create catcherFile. errno: %d\n", errno);
377         FAIL();
378     }
379     int res = eventLogCatcher->Catch(fd, 1);
380     EXPECT_TRUE(res == 0);
381     std::string fileName = "/data/test/testFile";
382     eventLogCatcher->AppendFile(fd, fileName);
383     EXPECT_TRUE(eventLogCatcher->AppendFile(fd, "") == 0);
384     close(fd);
385 }
386 
387 /**
388  * @tc.name: EventlogTask
389  * @tc.desc: test EventLogTask
390  * @tc.type: FUNC
391  */
392 HWTEST_F(EventloggerCatcherTest, EventlogTask_001, TestSize.Level3)
393 {
394     auto fd = open("/data/test/testFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
395     if (fd < 0) {
396         printf("Fail to create testFile. errno: %d\n", errno);
397         FAIL();
398     }
399     SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
400     std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
401     std::unique_ptr<EventLogTask> logTask = std::make_unique<EventLogTask>(fd, 1, sysEvent);
402     logTask->AddStopReason(fd, nullptr, "Test");
403     auto eventLogCatcher = std::make_shared<EventLogCatcher>();
404     logTask->AddStopReason(fd, eventLogCatcher, "Test");
405     bool ret = logTask->ShouldStopLogTask(fd, 1, -1, eventLogCatcher);
406     EXPECT_EQ(ret, false);
407     ret = logTask->ShouldStopLogTask(fd, 1, 20000, eventLogCatcher);
408     EXPECT_EQ(ret, false);
409     logTask->status_ = EventLogTask::Status::TASK_TIMEOUT;
410     ret = logTask->ShouldStopLogTask(fd, 0, 1, eventLogCatcher);
411     EXPECT_EQ(ret, true);
412     close(fd);
413 }
414 
415 /**
416  * @tc.name: EventlogTask
417  * @tc.desc: test EventlogTask
418  * @tc.type: FUNC
419  */
420 static HWTEST_F(EventloggerCatcherTest, EventlogTask_002, TestSize.Level3)
421 {
422     auto fd = open("/data/test/testFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
423     if (fd < 0) {
424         printf("Fail to create testFile. errno: %d\n", errno);
425         FAIL();
426     }
427     SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
428     std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
429     std::unique_ptr<EventLogTask> logTask = std::make_unique<EventLogTask>(fd, 1, sysEvent);
430     EXPECT_EQ(logTask->GetTaskStatus(), EventLogTask::Status::TASK_RUNNABLE);
431     auto ret = logTask->StartCompose();
432     EXPECT_EQ(ret, 2);
433     EXPECT_EQ(logTask->GetTaskStatus(), EventLogTask::Status::TASK_RUNNING);
434     ret = logTask->StartCompose();
435     EXPECT_EQ(ret, 1);
436     EXPECT_EQ(logTask->GetLogSize(), 0);
437     close(fd);
438 }
439 
440 /**
441  * @tc.name: EventlogTask
442  * @tc.desc: test EventlogTask
443  * @tc.type: FUNC
444  */
445 static HWTEST_F(EventloggerCatcherTest, EventlogTask_003, TestSize.Level3)
446 {
447     auto fd = open("/data/test/testFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
448     if (fd < 0) {
449         printf("Fail to create testFile. errno: %d\n", errno);
450         FAIL();
451     }
452     SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
453     std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
454     std::unique_ptr<EventLogTask> logTask = std::make_unique<EventLogTask>(fd, 1, sysEvent);
455     logTask->AppStackCapture();
456     logTask->SystemStackCapture();
457     logTask->BinderLogCapture();
458     logTask->WMSUsageCapture();
459     logTask->AMSUsageCapture();
460     logTask->PMSUsageCapture();
461     logTask->DPMSUsageCapture();
462     logTask->HilogCapture();
463     logTask->RSUsageCapture();
464     logTask->Screenshot();
465     logTask->DmesgCapture();
466     logTask->SCBSessionCapture();
467     logTask->SCBViewParamCapture();
468     logTask->LightHilogCapture();
469     printf("task size: %d\n", static_cast<int>(logTask->tasks_.size()));
470     EXPECT_EQ(logTask->PeerBinderCapture("Test"), false);
471     EXPECT_EQ(logTask->PeerBinderCapture("pb"), false);
472     EXPECT_EQ(logTask->PeerBinderCapture("pb:1:a"), true);
473     close(fd);
474 }
475 
476 /**
477  * @tc.name: BinderCatcherTest_001
478  * @tc.desc: add testcase code coverage
479  * @tc.type: FUNC
480  */
481 HWTEST_F(EventloggerCatcherTest, BinderCatcherTest_001, TestSize.Level1)
482 {
483     auto binderCatcher = std::make_shared<BinderCatcher>();
484     bool ret = binderCatcher->Initialize("test", 1, 2);
485     EXPECT_EQ(ret, true);
486     auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
487     if (fd < 0) {
488         printf("Fail to create catcherFile. errno: %d\n", errno);
489         FAIL();
490     }
491     int res = binderCatcher->Catch(fd, 1);
492     EXPECT_TRUE(res > 0);
493     close(fd);
494 }
495 
496 /**
497  * @tc.name: DmesgCatcherTest_001
498  * @tc.desc: add testcase code coverage
499  * @tc.type: FUNC
500  */
501 HWTEST_F(EventloggerCatcherTest, DmesgCatcherTest_001, TestSize.Level1)
502 {
503     auto dmesgCatcher = std::make_shared<DmesgCatcher>();
504     auto jsonStr = "{\"domain_\":\"KERNEL_VENDOR\"}";
505     std::shared_ptr<SysEvent> event = std::make_shared<SysEvent>("DmesgCatcherTest_001",
506         nullptr, jsonStr);
507     event->eventId_ = 0;
508     event->domain_ = "KERNEL_VENDOR";
509     event->eventName_ = "HUNGTASK";
510     event->SetEventValue("PID", 0);
511     EXPECT_TRUE(dmesgCatcher->Init(event));
512 }
513 
514 /**
515  * @tc.name: DmesgCatcherTest_002
516  * @tc.desc: add testcase code coverage
517  * @tc.type: FUNC
518  */
519 HWTEST_F(EventloggerCatcherTest, DmesgCatcherTest_002, TestSize.Level1)
520 {
521     auto dmesgCatcher = std::make_shared<DmesgCatcher>();
522     auto fd = open("/data/test/dmesgCatcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
523     if (fd < 0) {
524         printf("Fail to create dmesgCatcherFile. errno: %d\n", errno);
525         FAIL();
526     }
527     dmesgCatcher->Initialize("", 0, 0);
528     int jsonFd = 1;
529     EXPECT_TRUE(dmesgCatcher->Catch(fd, jsonFd) > 0);
530 
531     dmesgCatcher->Initialize("", 0, 1);
532     EXPECT_TRUE(dmesgCatcher->Catch(fd, jsonFd) > 0);
533 
534     dmesgCatcher->Initialize("", 1, 0);
535     printf("dmesgCatcher result: %d\n", dmesgCatcher->Catch(fd, jsonFd));
536 
537     dmesgCatcher->Initialize("", 1, 1);
538     printf("dmesgCatcher result: %d\n", dmesgCatcher->Catch(fd, jsonFd));
539 
540     close(fd);
541 }
542 
543 /**
544  * @tc.name: DmesgCatcherTest_003
545  * @tc.desc: add testcase code coverage
546  * @tc.type: FUNC
547  */
548 HWTEST_F(EventloggerCatcherTest, DmesgCatcherTest_003, TestSize.Level1)
549 {
550     auto dmesgCatcher = std::make_shared<DmesgCatcher>();
551     bool ret = dmesgCatcher->DumpDmesgLog(-1);
552     EXPECT_EQ(ret, false);
553     auto fd = open("/data/test/dmesgCatcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
554     if (fd < 0) {
555         printf("Fail to create dmesgCatcherFile. errno: %d\n", errno);
556         FAIL();
557     }
558     ret = dmesgCatcher->DumpDmesgLog(fd);
559     EXPECT_EQ(ret, true);
560     ret = dmesgCatcher->WriteSysrq();
561     EXPECT_EQ(ret, true);
562     std::string res = dmesgCatcher->DmesgSaveTofile();
563     printf("DmesgSaveTofile size: %zu\n", res.size());
564     close(fd);
565 }
566 
567 /**
568  * @tc.name: OpenStacktraceCatcherTest_001
569  * @tc.desc: add testcase code coverage
570  * @tc.type: FUNC
571  */
572 HWTEST_F(EventloggerCatcherTest, OpenStacktraceCatcherTest_001, TestSize.Level1)
573 {
574     auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
575     if (fd < 0) {
576         printf("Fail to create catcherFile. errno: %d\n", errno);
577         FAIL();
578     }
579 
580     auto openStackCatcher = std::make_shared<OpenStacktraceCatcher>();
581     ASSERT_EQ(openStackCatcher->Initialize("", 0, 0), false);
582 
583     int jsonFd = 1;
584     bool ret = openStackCatcher->Catch(fd, jsonFd);
585     EXPECT_TRUE(ret == 0);
586 
587     EXPECT_EQ(openStackCatcher->Initialize("test", 0, 0), false);
588     ret = openStackCatcher->Catch(fd, jsonFd);
589     EXPECT_TRUE(ret == 0);
590 
591     EXPECT_EQ(openStackCatcher->Initialize("", 1, 0), true);
592     EXPECT_EQ(openStackCatcher->Initialize("test", 1, 0), true);
593     ret = openStackCatcher->Catch(fd, jsonFd);
594     EXPECT_TRUE(ret > 0);
595     close(fd);
596 }
597 
598 /**
599  * @tc.name: OpenStacktraceCatcherTest_002
600  * @tc.desc: add testcase code coverage
601  * @tc.type: FUNC
602  */
603 HWTEST_F(EventloggerCatcherTest, OpenStacktraceCatcherTest_002, TestSize.Level1)
604 {
605     auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
606     if (fd < 0) {
607         printf("Fail to create catcherFile. errno: %d\n", errno);
608         FAIL();
609     }
610 
611     auto openStackCatcher = std::make_shared<OpenStacktraceCatcher>();
612     bool ret = openStackCatcher->Catch(fd, 1);
613     EXPECT_TRUE(ret == 0);
614     EXPECT_EQ(openStackCatcher->ForkAndDumpStackTrace(fd), 0);
615     close(fd);
616 }
617 
618 /**
619  * @tc.name: PeerBinderCatcherTest_001
620  * @tc.desc: add testcase code coverage
621  * @tc.type: FUNC
622  */
623 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_001, TestSize.Level1)
624 {
625     auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
626     if (fd < 0) {
627         printf("Fail to create catcherFile. errno: %d\n", errno);
628         FAIL();
629     }
630     auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
631     peerBinderCatcher->Initialize("PeerBinderCatcherTest", 0, 0);
632     int jsonFd = 1;
633     int res = peerBinderCatcher->Catch(fd, jsonFd);
634     EXPECT_TRUE(res < 0);
635 
636     peerBinderCatcher->Initialize("a", 0, 0);
637     peerBinderCatcher->Initialize("a", 1, 0);
638     res = peerBinderCatcher->Catch(fd, jsonFd);
639     EXPECT_TRUE(res < 0);
640 
641     peerBinderCatcher->Initialize("a", 1, 1);
642     res = peerBinderCatcher->Catch(fd, jsonFd);
643     EXPECT_TRUE(res > 0);
644 
645     int pid = CommonUtils::GetPidByName("foundation");
646 #ifdef HAS_HIPERF
647     std::set<int> pids;
648     pids.insert(pid);
649     peerBinderCatcher->DoExecHiperf("peerBinderCatcher", pids);
650 #endif
651     peerBinderCatcher->Initialize("", 0, pid);
652     peerBinderCatcher->Initialize("foundation", 0, pid);
653     peerBinderCatcher->Initialize("foundation", 1, pid);
654     peerBinderCatcher->CatcherStacktrace(fd, pid);
655     close(fd);
656 }
657 
658 /**
659  * @tc.name: PeerBinderCatcherTest_002
660  * @tc.desc: add testcase code coverage
661  * @tc.type: FUNC
662  */
663 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_002, TestSize.Level1)
664 {
665     auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
666     if (fd < 0) {
667         printf("Fail to create catcherFile. errno: %d\n", errno);
668         FAIL();
669     }
670     auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
671     peerBinderCatcher->Initialize("a", 1, 0);
672     auto jsonStr = "{\"domain_\":\"KERNEL_VENDOR\"}";
673     std::shared_ptr<SysEvent> event = std::make_shared<SysEvent>("PeerBinderCatcherTest_002",
674         nullptr, jsonStr);
675     event->eventId_ = 0;
676     event->domain_ = "KERNEL_VENDOR";
677     event->eventName_ = "HUNGTASK";
678     event->SetEventValue("PID", 0);
679     std::string filePath = "/data/test/catcherFile";
680     std::set<int> catchedPids;
681     catchedPids.insert(0);
682     catchedPids.insert(1);
683     int pid = CommonUtils::GetPidByName("foundation");
684     catchedPids.insert(pid);
685     peerBinderCatcher->Init(event, filePath, catchedPids);
686     peerBinderCatcher->Initialize("foundation", 1, pid);
687     int res = peerBinderCatcher->Catch(fd, 1);
688     EXPECT_GT(res, 0);
689     close(fd);
690 }
691 
692 /**
693  * @tc.name: PeerBinderCatcherTest_003
694  * @tc.desc: add testcase code coverage
695  * @tc.type: FUNC
696  */
697 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_003, TestSize.Level1)
698 {
699     std::set<int> pids;
700     pids.insert(0);
701     pids.insert(2);
702     auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
703     PeerBinderCatcher::BinderInfo info = {
704         .client = 1,
705         .server = 1,
706         .wait = 1
707     };
708     std::map<int, std::list<OHOS::HiviewDFX::PeerBinderCatcher::BinderInfo>> manager;
709     manager[info.client].push_back(info);
710     peerBinderCatcher->ParseBinderCallChain(manager, pids, 0);
711     PeerBinderCatcher::BinderInfo info1 = {
712         .client = 2,
713         .server = 2,
714         .wait = 0
715     };
716     manager[info1.client].push_back(info1);
717     peerBinderCatcher->ParseBinderCallChain(manager, pids, 1);
718 #ifdef HAS_HIPERF
719     pids.insert(3);
720     pids.insert(4);
721     peerBinderCatcher->DoExecHiperf("peerBinderCatcher", pids);
722     peerBinderCatcher->ForkToDumpHiperf(pids);
723 #endif
724 }
725 
726 /**
727  * @tc.name: PeerBinderCatcherTest_004
728  * @tc.desc: add testcase code coverage
729  * @tc.type: FUNC
730  */
731 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_004, TestSize.Level1)
732 {
733     auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
734     if (fd < 0) {
735         printf("Fail to create catcherFile. errno: %d\n", errno);
736         FAIL();
737     }
738     auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
739     std::set<int> pids = peerBinderCatcher->GetBinderPeerPids(fd, 1);
740     EXPECT_TRUE(pids.empty());
741 }
742 
743 /**
744  * @tc.name: PeerBinderCatcherTest_005
745  * @tc.desc: add testcase code coverage
746  * @tc.type: FUNC
747  */
748 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_005, TestSize.Level1)
749 {
750     auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
751     std::list<PeerBinderCatcher::OutputBinderInfo> infoList;
752     peerBinderCatcher->AddBinderJsonInfo(infoList, -1);
753     PeerBinderCatcher::OutputBinderInfo info = {
754         .info = "Test",
755         .pid = 0
756     };
757     infoList.push_back(info);
758     peerBinderCatcher->AddBinderJsonInfo(infoList, 1);
759     PeerBinderCatcher::OutputBinderInfo info1 = {
760         .info = "Test",
761         .pid = getpid()
762     };
763     infoList.push_back(info1);
764     peerBinderCatcher->AddBinderJsonInfo(infoList, 1);
765     std::string str = "/proc/" + std::to_string(getpid()) + "/cmdline";
766     printf("%s\n", str.c_str());
767 }
768 
769 /**
770  * @tc.name: PeerBinderCatcherTest_006
771  * @tc.desc: add testcase code coverage
772  * @tc.type: FUNC
773  */
774 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_006, TestSize.Level1)
775 {
776     auto fd = open("/data/test/peerFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
777     if (fd < 0) {
778         printf("Fail to create peerFile. errno: %d\n", errno);
779         FAIL();
780     }
781     std::ofstream testFile;
782     std::string path = "/data/test/peerFile";
783     testFile.open(path);
784     testFile << "0:pid\tcontext:binder\t0:request\t3:started\t"
785         "16:max\t4:ready\t521092:free_space\n";
786     testFile.close();
787 
788     auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
789     std::ifstream fin;
790     fin.open(path.c_str());
791     if (!fin.is_open()) {
792         printf("open binder file failed, %s\n.", path.c_str());
793         FAIL();
794     }
795     auto fd1 = open("/data/test/peerTestFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
796     if (fd1 < 0) {
797         printf("Fail to create peerTestFile. errno: %d\n", errno);
798         FAIL();
799     }
800     peerBinderCatcher->BinderInfoParser(fin, fd1, 1);
801     std::set<int> pids = peerBinderCatcher->GetBinderPeerPids(fd, 1);
802     EXPECT_TRUE(pids.empty());
803     pids = peerBinderCatcher->GetBinderPeerPids(-1, 1);
804     EXPECT_TRUE(pids.empty());
805     fin.close();
806 }
807 
808 /**
809  * @tc.name: ShellCatcherTest
810  * @tc.desc: add testcase code coverage
811  * @tc.type: FUNC
812  */
813 HWTEST_F(EventloggerCatcherTest, ShellCatcherTest_001, TestSize.Level1)
814 {
815     auto fd = open("/data/test/shellCatcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
816     if (fd < 0) {
817         printf("Fail to create shellCatcherFile. errno: %d\n", errno);
818         FAIL();
819     }
820 
821     auto shellCatcher = std::make_shared<ShellCatcher>();
822     int pid = CommonUtils::GetPidByName("foundation");
823 
824     bool res = shellCatcher->Initialize("", ShellCatcher::CATCHER_WMS, pid);
825     EXPECT_TRUE(res);
826 
827     int jsonFd = 1;
828     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) < 0);
829 
830     shellCatcher->Initialize("hidumper -s WindowManagerService -a -a", ShellCatcher::CATCHER_WMS, pid);
831     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
832 
833     shellCatcher->Initialize("hidumper -s AbilityManagerService -a -a", ShellCatcher::CATCHER_AMS, pid);
834     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
835 
836     shellCatcher->Initialize("hidumper --cpuusage", ShellCatcher::CATCHER_CPU, pid);
837     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
838 
839     shellCatcher->Initialize("hidumper --mem", ShellCatcher::CATCHER_MEM, pid);
840     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
841 
842     shellCatcher->Initialize("hidumper -s PowerManagerService -a -s", ShellCatcher::CATCHER_PMS, pid);
843     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
844 
845     shellCatcher->Initialize("hidumper -s DisplayPowerManagerService", ShellCatcher::CATCHER_DPMS, pid);
846     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
847 
848     shellCatcher->Initialize("hidumper -s RenderService -a allInfo", ShellCatcher::CATCHER_RS, pid);
849     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
850 
851     shellCatcher->Initialize("hilog -x", ShellCatcher::CATCHER_HILOG, 0);
852     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
853 
854     shellCatcher->Initialize("snapshot_display -f", ShellCatcher::CATCHER_SNAPSHOT, 0);
855     EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
856 
857     shellCatcher->Initialize("scb_debug SCBScenePanel getContainerSession", ShellCatcher::CATCHER_SCBSESSION, 0);
858     printf("CATCHER_SCBSESSION result: %s", shellCatcher->Catch(fd, jsonFd) > 0 ? "true" : "false");
859 
860     shellCatcher->Initialize("scb_debug SCBScenePanel getViewParam", ShellCatcher::CATCHER_SCBVIEWPARAM, 0);
861     printf("CATCHER_SCBVIEWPARAM result: %s", shellCatcher->Catch(fd, jsonFd) > 0 ? "true" : "false");
862 
863     shellCatcher->Initialize("default", -1, 0);
864     EXPECT_EQ(shellCatcher->Catch(fd, jsonFd), 0);
865 
866     close(fd);
867 }
868 } // namesapce HiviewDFX
869 } // namespace OHOS
870