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 "memory_catcher.h"
38 #include "event_logger.h"
39 #include "event_log_catcher.h"
40 #include "sys_event.h"
41 #include "hisysevent.h"
42 #include "eventlogger_util_test.h"
43 #include "log_catcher_utils.h"
44 using namespace testing::ext;
45 using namespace OHOS::HiviewDFX;
46
47 namespace OHOS {
48 namespace HiviewDFX {
SetUp()49 void EventloggerCatcherTest::SetUp()
50 {
51 /**
52 * @tc.setup: create an event loop and multiple event handlers
53 */
54 printf("SetUp.\n");
55 InitSeLinuxEnabled();
56 }
57
TearDown()58 void EventloggerCatcherTest::TearDown()
59 {
60 /**
61 * @tc.teardown: destroy the event loop we have created
62 */
63 CancelSeLinuxEnabled();
64 printf("TearDown.\n");
65 }
66
67 /**
68 * @tc.name: EventLogCatcher
69 * @tc.desc: test EventLogCatcher
70 * @tc.type: FUNC
71 */
72 HWTEST_F(EventloggerCatcherTest, EventLogCatcherTest_001, TestSize.Level3)
73 {
74 auto eventLogCatcher = std::make_shared<EventLogCatcher>();
75 EXPECT_TRUE(eventLogCatcher->GetLogSize() == -1);
76 eventLogCatcher->SetLogSize(1);
77 EXPECT_TRUE(eventLogCatcher->GetLogSize() == 1);
78 EXPECT_TRUE(eventLogCatcher->AppendFile(-1, "") == 0);
79 auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
80 if (fd < 0) {
81 printf("Fail to create catcherFile. errno: %d\n", errno);
82 FAIL();
83 }
84 int res = eventLogCatcher->Catch(fd, 1);
85 EXPECT_TRUE(res == 0);
86 std::string fileName = "/data/test/catcherFile";
87 eventLogCatcher->AppendFile(fd, fileName);
88 EXPECT_TRUE(eventLogCatcher->AppendFile(fd, "") == 0);
89 close(fd);
90 }
91
92 /**
93 * @tc.name: EventlogTask
94 * @tc.desc: test EventLogTask
95 * @tc.type: FUNC
96 */
97 HWTEST_F(EventloggerCatcherTest, EventlogTask_001, TestSize.Level3)
98 {
99 auto fd = open("/data/test/testFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
100 if (fd < 0) {
101 printf("Fail to create testFile. errno: %d\n", errno);
102 FAIL();
103 }
104 SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
105 std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
106 std::unique_ptr<EventLogTask> logTask = std::make_unique<EventLogTask>(fd, 1, sysEvent);
107 logTask->AddStopReason(fd, nullptr, "Test");
108 auto eventLogCatcher = std::make_shared<EventLogCatcher>();
109 logTask->AddStopReason(fd, eventLogCatcher, "Test");
110 logTask->AddSeparator(fd, eventLogCatcher);
111 bool ret = logTask->ShouldStopLogTask(fd, 1, -1, eventLogCatcher);
112 EXPECT_EQ(ret, false);
113 ret = logTask->ShouldStopLogTask(fd, 1, 20000, eventLogCatcher);
114 EXPECT_EQ(ret, false);
115 logTask->status_ = EventLogTask::Status::TASK_TIMEOUT;
116 ret = logTask->ShouldStopLogTask(fd, 0, 1, eventLogCatcher);
117 EXPECT_EQ(ret, true);
118 close(fd);
119 }
120
121 /**
122 * @tc.name: EventlogTask
123 * @tc.desc: test EventlogTask
124 * @tc.type: FUNC
125 */
126 HWTEST_F(EventloggerCatcherTest, EventlogTask_002, TestSize.Level3)
127 {
128 auto fd = open("/data/test/testFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
129 if (fd < 0) {
130 printf("Fail to create testFile. errno: %d\n", errno);
131 FAIL();
132 }
133 SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
134 std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
135 std::unique_ptr<EventLogTask> logTask = std::make_unique<EventLogTask>(fd, 1, sysEvent);
136 EXPECT_EQ(logTask->GetTaskStatus(), EventLogTask::Status::TASK_RUNNABLE);
137 auto ret = logTask->StartCompose();
138 EXPECT_EQ(ret, 2);
139 EXPECT_EQ(logTask->GetTaskStatus(), EventLogTask::Status::TASK_RUNNING);
140 ret = logTask->StartCompose();
141 EXPECT_EQ(ret, 1);
142 EXPECT_EQ(logTask->GetLogSize(), 0);
143 logTask->GetThermalInfo(fd);
144 close(fd);
145 }
146
147 /**
148 * @tc.name: EventlogTask
149 * @tc.desc: test EventlogTask
150 * @tc.type: FUNC
151 */
152 HWTEST_F(EventloggerCatcherTest, EventlogTask_003, TestSize.Level3)
153 {
154 auto fd = open("/data/test/testFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
155 if (fd < 0) {
156 printf("Fail to create testFile. errno: %d\n", errno);
157 FAIL();
158 }
159 SysEventCreator sysEventCreator("HIVIEWDFX", "EventlogTask", SysEventCreator::FAULT);
160 std::shared_ptr<SysEvent> sysEvent = std::make_shared<SysEvent>("EventlogTask", nullptr, sysEventCreator);
161 std::unique_ptr<EventLogTask> logTask = std::make_unique<EventLogTask>(fd, 1, sysEvent);
162 logTask->AddLog("cmd:scbCS");
163 logTask->AppStackCapture();
164 logTask->SystemStackCapture();
165 logTask->BinderLogCapture();
166 logTask->WMSUsageCapture();
167 logTask->AMSUsageCapture();
168 logTask->PMSUsageCapture();
169 logTask->DPMSUsageCapture();
170 logTask->HilogCapture();
171 logTask->RSUsageCapture();
172 logTask->Screenshot();
173 logTask->DmesgCapture();
174 logTask->SCBSessionCapture();
175 logTask->SCBViewParamCapture();
176 logTask->LightHilogCapture();
177 logTask->SCBWMSCapture();
178 logTask->DumpAppMapCapture();
179 logTask->SCBWMSEVTCapture();
180 logTask->SysrqCapture(true);
181 logTask->MemoryUsageCapture();
182 logTask->DMSUsageCapture();
183 logTask->CpuUsageCapture();
184 logTask->MMIUsageCapture();
185 logTask->HitraceCapture();
186 logTask->SCBWMSEVTCapture();
187 logTask->InputHilogCapture();
188 logTask->RemoteStackCapture();
189 logTask->AddLog("Test");
190 logTask->AddLog("cmd:w");
191 logTask->status_ = EventLogTask::Status::TASK_RUNNING;
192 auto ret = logTask->StartCompose();
193 printf("task size: %d\n", static_cast<int>(logTask->tasks_.size()));
194 EXPECT_EQ(logTask->PeerBinderCapture("Test"), false);
195 EXPECT_EQ(logTask->PeerBinderCapture("pb"), false);
196 EXPECT_EQ(logTask->PeerBinderCapture("pb:1:a"), true);
197 close(fd);
198 }
199
200 /**
201 * @tc.name: BinderCatcherTest_001
202 * @tc.desc: add testcase code coverage
203 * @tc.type: FUNC
204 */
205 HWTEST_F(EventloggerCatcherTest, BinderCatcherTest_001, TestSize.Level1)
206 {
207 auto binderCatcher = std::make_shared<BinderCatcher>();
208 bool ret = binderCatcher->Initialize("test", 1, 2);
209 EXPECT_EQ(ret, true);
210 auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
211 if (fd < 0) {
212 printf("Fail to create catcherFile. errno: %d\n", errno);
213 FAIL();
214 }
215 int res = binderCatcher->Catch(fd, 1);
216 EXPECT_TRUE(res > 0);
217 close(fd);
218 }
219
220 /**
221 * @tc.name: MemoryCatcherTest_001
222 * @tc.desc: add testcase code coverage
223 * @tc.type: FUNC
224 */
225 HWTEST_F(EventloggerCatcherTest, MemoryCatcherTest_001, TestSize.Level1)
226 {
227 auto memoryCatcher = std::make_shared<MemoryCatcher>();
228 bool ret = memoryCatcher->Initialize("test", 1, 2);
229 EXPECT_EQ(ret, true);
230 auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
231 if (fd < 0) {
232 printf("Fail to create catcherFile. errno: %d\n", errno);
233 FAIL();
234 }
235 int res = memoryCatcher->Catch(fd, 1);
236 EXPECT_TRUE(res > 0);
237 res = memoryCatcher->Catch(0, 1);
238 EXPECT_EQ(res, 0);
239 printf("memoryCatcher result: %d\n", res);
240 close(fd);
241 }
242
243 /**
244 * @tc.name: DmesgCatcherTest_001
245 * @tc.desc: add testcase code coverage
246 * @tc.type: FUNC
247 */
248 HWTEST_F(EventloggerCatcherTest, DmesgCatcherTest_001, TestSize.Level1)
249 {
250 auto dmesgCatcher = std::make_shared<DmesgCatcher>();
251 auto jsonStr = "{\"domain_\":\"KERNEL_VENDOR\"}";
252 std::shared_ptr<SysEvent> event = std::make_shared<SysEvent>("DmesgCatcherTest_001",
253 nullptr, jsonStr);
254 event->eventId_ = 0;
255 event->domain_ = "KERNEL_VENDOR";
256 event->eventName_ = "HUNGTASK";
257 event->SetEventValue("PID", 0);
258 EXPECT_TRUE(dmesgCatcher->Init(event));
259 }
260
261 /**
262 * @tc.name: DmesgCatcherTest_002
263 * @tc.desc: add testcase code coverage
264 * @tc.type: FUNC
265 */
266 HWTEST_F(EventloggerCatcherTest, DmesgCatcherTest_002, TestSize.Level1)
267 {
268 auto dmesgCatcher = std::make_shared<DmesgCatcher>();
269 auto fd = open("/data/test/dmesgCatcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
270 if (fd < 0) {
271 printf("Fail to create dmesgCatcherFile. errno: %d\n", errno);
272 FAIL();
273 }
274 dmesgCatcher->Initialize("", 0, 0);
275 int jsonFd = 1;
276 EXPECT_TRUE(dmesgCatcher->Catch(fd, jsonFd) > 0);
277
278 dmesgCatcher->Initialize("", 0, 1);
279 EXPECT_TRUE(dmesgCatcher->Catch(fd, jsonFd) > 0);
280
281 dmesgCatcher->Initialize("", 1, 0);
282 printf("dmesgCatcher result: %d\n", dmesgCatcher->Catch(fd, jsonFd));
283
284 dmesgCatcher->Initialize("", 1, 1);
285 printf("dmesgCatcher result: %d\n", dmesgCatcher->Catch(fd, jsonFd));
286
287 close(fd);
288 }
289
290 /**
291 * @tc.name: DmesgCatcherTest_003
292 * @tc.desc: add testcase code coverage
293 * @tc.type: FUNC
294 */
295 HWTEST_F(EventloggerCatcherTest, DmesgCatcherTest_003, TestSize.Level1)
296 {
297 auto dmesgCatcher = std::make_shared<DmesgCatcher>();
298 bool ret = dmesgCatcher->DumpDmesgLog(-1);
299 EXPECT_EQ(ret, false);
300 auto fd = open("/data/test/dmesgCatcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
301 if (fd < 0) {
302 printf("Fail to create dmesgCatcherFile. errno: %d\n", errno);
303 FAIL();
304 }
305 ret = dmesgCatcher->DumpDmesgLog(fd);
306 EXPECT_EQ(ret, true);
307 ret = dmesgCatcher->WriteSysrq();
308 EXPECT_EQ(ret, true);
309 std::string res = dmesgCatcher->DmesgSaveTofile();
310 printf("DmesgSaveTofile size: %zu\n", res.size());
311 close(fd);
312 }
313
314 /**
315 * @tc.name: OpenStacktraceCatcherTest_001
316 * @tc.desc: add testcase code coverage
317 * @tc.type: FUNC
318 */
319 HWTEST_F(EventloggerCatcherTest, OpenStacktraceCatcherTest_001, TestSize.Level1)
320 {
321 auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
322 if (fd < 0) {
323 printf("Fail to create catcherFile. errno: %d\n", errno);
324 FAIL();
325 }
326
327 auto openStackCatcher = std::make_shared<OpenStacktraceCatcher>();
328 ASSERT_EQ(openStackCatcher->Initialize("", 0, 0), false);
329
330 int jsonFd = 1;
331 bool ret = openStackCatcher->Catch(fd, jsonFd);
332 EXPECT_TRUE(ret == 0);
333
334 EXPECT_EQ(openStackCatcher->Initialize("test", 0, 0), false);
335 ret = openStackCatcher->Catch(fd, jsonFd);
336 EXPECT_TRUE(ret == 0);
337
338 EXPECT_EQ(openStackCatcher->Initialize("", 1, 0), true);
339 EXPECT_EQ(openStackCatcher->Initialize("test", 1, 0), true);
340 ret = openStackCatcher->Catch(fd, jsonFd);
341 EXPECT_TRUE(ret > 0);
342 close(fd);
343 }
344
345 /**
346 * @tc.name: OpenStacktraceCatcherTest_002
347 * @tc.desc: add testcase code coverage
348 * @tc.type: FUNC
349 */
350 HWTEST_F(EventloggerCatcherTest, OpenStacktraceCatcherTest_002, TestSize.Level1)
351 {
352 auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
353 if (fd < 0) {
354 printf("Fail to create catcherFile. errno: %d\n", errno);
355 FAIL();
356 }
357
358 auto openStackCatcher = std::make_shared<OpenStacktraceCatcher>();
359 bool ret = openStackCatcher->Catch(fd, 1);
360 EXPECT_TRUE(ret == 0);
361 EXPECT_EQ(openStackCatcher->ForkAndDumpStackTrace(fd), 0);
362 close(fd);
363 }
364
365 /**
366 * @tc.name: PeerBinderCatcherTest_001
367 * @tc.desc: add testcase code coverage
368 * @tc.type: FUNC
369 */
370 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_001, TestSize.Level1)
371 {
372 auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
373 if (fd < 0) {
374 printf("Fail to create catcherFile. errno: %d\n", errno);
375 FAIL();
376 }
377 auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
378 peerBinderCatcher->Initialize("PeerBinderCatcherTest", 0, 0);
379 int jsonFd = 1;
380 int res = peerBinderCatcher->Catch(fd, jsonFd);
381 EXPECT_TRUE(res < 0);
382
383 peerBinderCatcher->Initialize("a", 0, 0);
384 peerBinderCatcher->Initialize("a", 1, 0);
385 res = peerBinderCatcher->Catch(fd, jsonFd);
386 EXPECT_TRUE(res < 0);
387
388 peerBinderCatcher->Initialize("a", 1, 1);
389 res = peerBinderCatcher->Catch(fd, jsonFd);
390 EXPECT_TRUE(res > 0);
391
392 int pid = CommonUtils::GetPidByName("foundation");
393 #ifdef HAS_HIPERF
394 std::set<int> pids;
395 pids.insert(pid);
396 peerBinderCatcher->DoExecHiperf("peerBinderCatcher", pids);
397 #endif
398 peerBinderCatcher->Initialize("", 0, pid);
399 peerBinderCatcher->Initialize("foundation", 0, pid);
400 peerBinderCatcher->Initialize("foundation", 1, pid);
401 peerBinderCatcher->CatcherStacktrace(fd, pid);
402 close(fd);
403 }
404
405 /**
406 * @tc.name: PeerBinderCatcherTest_002
407 * @tc.desc: add testcase code coverage
408 * @tc.type: FUNC
409 */
410 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_002, TestSize.Level1)
411 {
412 auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
413 if (fd < 0) {
414 printf("Fail to create catcherFile. errno: %d\n", errno);
415 FAIL();
416 }
417 auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
418 peerBinderCatcher->Initialize("a", 1, 0);
419 auto jsonStr = "{\"domain_\":\"KERNEL_VENDOR\"}";
420 std::shared_ptr<SysEvent> event = std::make_shared<SysEvent>("PeerBinderCatcherTest_002",
421 nullptr, jsonStr);
422 event->eventId_ = 0;
423 event->domain_ = "KERNEL_VENDOR";
424 event->eventName_ = "HUNGTASK";
425 event->SetEventValue("PID", 0);
426 std::string filePath = "/data/test/catcherFile";
427 std::set<int> catchedPids;
428 catchedPids.insert(0);
429 catchedPids.insert(1);
430 int pid = CommonUtils::GetPidByName("foundation");
431 catchedPids.insert(pid);
432 peerBinderCatcher->Init(event, filePath, catchedPids);
433 peerBinderCatcher->Initialize("foundation", 1, pid);
434 int res = peerBinderCatcher->Catch(fd, 1);
435 EXPECT_GT(res, 0);
436 close(fd);
437 }
438
439 /**
440 * @tc.name: PeerBinderCatcherTest_003
441 * @tc.desc: add testcase code coverage
442 * @tc.type: FUNC
443 */
444 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_003, TestSize.Level1)
445 {
446 std::set<int> pids;
447 pids.insert(0);
448 pids.insert(2);
449 auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
450 PeerBinderCatcher::BinderInfo info = {
451 .client = 1,
452 .server = 1,
453 .wait = 1
454 };
455 std::map<int, std::list<OHOS::HiviewDFX::PeerBinderCatcher::BinderInfo>> manager;
456 manager[info.client].push_back(info);
457 peerBinderCatcher->ParseBinderCallChain(manager, pids, 0);
458 PeerBinderCatcher::BinderInfo info1 = {
459 .client = 2,
460 .server = 2,
461 .wait = 0
462 };
463 manager[info1.client].push_back(info1);
464 peerBinderCatcher->ParseBinderCallChain(manager, pids, 1);
465 #ifdef HAS_HIPERF
466 pids.insert(3);
467 pids.insert(4);
468 peerBinderCatcher->DoExecHiperf("peerBinderCatcher", pids);
469 peerBinderCatcher->ForkToDumpHiperf(pids);
470 #endif
471 }
472
473 /**
474 * @tc.name: PeerBinderCatcherTest_004
475 * @tc.desc: add testcase code coverage
476 * @tc.type: FUNC
477 */
478 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_004, TestSize.Level1)
479 {
480 auto fd = open("/data/test/catcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
481 if (fd < 0) {
482 printf("Fail to create catcherFile. errno: %d\n", errno);
483 FAIL();
484 }
485 auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
486 std::set<int> asyncPids;
487 std::set<int> pids = peerBinderCatcher->GetBinderPeerPids(fd, 1, asyncPids);
488 EXPECT_TRUE(pids.empty());
489 }
490
491 /**
492 * @tc.name: PeerBinderCatcherTest_005
493 * @tc.desc: add testcase code coverage
494 * @tc.type: FUNC
495 */
496 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_005, TestSize.Level1)
497 {
498 auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
499 std::list<PeerBinderCatcher::OutputBinderInfo> infoList;
500 peerBinderCatcher->AddBinderJsonInfo(infoList, -1);
501 PeerBinderCatcher::OutputBinderInfo info = {
502 .info = "Test",
503 .pid = 0
504 };
505 infoList.push_back(info);
506 peerBinderCatcher->AddBinderJsonInfo(infoList, 1);
507 PeerBinderCatcher::OutputBinderInfo info1 = {
508 .info = "Test",
509 .pid = getpid()
510 };
511 infoList.push_back(info1);
512 peerBinderCatcher->AddBinderJsonInfo(infoList, 1);
513 std::string str = "/proc/" + std::to_string(getpid()) + "/cmdline";
514 printf("%s\n", str.c_str());
515 EXPECT_TRUE(!str.empty());
516 }
517
518 /**
519 * @tc.name: PeerBinderCatcherTest_006
520 * @tc.desc: add testcase code coverage
521 * @tc.type: FUNC
522 */
523 HWTEST_F(EventloggerCatcherTest, PeerBinderCatcherTest_006, TestSize.Level1)
524 {
525 auto fd = open("/data/test/peerFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
526 if (fd < 0) {
527 printf("Fail to create peerFile. errno: %d\n", errno);
528 FAIL();
529 }
530 std::ofstream testFile;
531 std::string path = "/data/test/peerFile";
532 testFile.open(path);
533 testFile << "0:pid\tcontext:binder\t0:request\t3:started\t"
534 "16:max\t4:ready\t521092:free_space\n";
535 testFile.close();
536
537 auto peerBinderCatcher = std::make_shared<PeerBinderCatcher>();
538 std::ifstream fin;
539 fin.open(path.c_str());
540 if (!fin.is_open()) {
541 printf("open binder file failed, %s\n.", path.c_str());
542 FAIL();
543 }
544 auto fd1 = open("/data/test/peerTestFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
545 if (fd1 < 0) {
546 printf("Fail to create peerTestFile. errno: %d\n", errno);
547 FAIL();
548 }
549 std::set<int> asyncPids;
550 peerBinderCatcher->BinderInfoParser(fin, fd1, 1, asyncPids);
551 std::set<int> pids = peerBinderCatcher->GetBinderPeerPids(fd, 1, asyncPids);
552 EXPECT_TRUE(pids.empty());
553 pids = peerBinderCatcher->GetBinderPeerPids(-1, 1, asyncPids);
554 EXPECT_TRUE(pids.empty());
555 fin.close();
556 }
557
558 /**
559 * @tc.name: ShellCatcherTest
560 * @tc.desc: add testcase code coverage
561 * @tc.type: FUNC
562 */
563 HWTEST_F(EventloggerCatcherTest, ShellCatcherTest_001, TestSize.Level1)
564 {
565 auto fd = open("/data/test/shellCatcherFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
566 if (fd < 0) {
567 printf("Fail to create shellCatcherFile. errno: %d\n", errno);
568 FAIL();
569 }
570
571 auto shellCatcher = std::make_shared<ShellCatcher>();
572 int pid = CommonUtils::GetPidByName("foundation");
573
574 bool res = shellCatcher->Initialize("", ShellCatcher::CATCHER_WMS, pid);
575 EXPECT_TRUE(res);
576
577 int jsonFd = 1;
578 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) < 0);
579
580 shellCatcher->Initialize("hidumper -s WindowManagerService -a -a", ShellCatcher::CATCHER_WMS, pid);
581 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
582
583 shellCatcher->Initialize("hidumper -s AbilityManagerService -a -a", ShellCatcher::CATCHER_AMS, pid);
584 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
585
586 shellCatcher->Initialize("hidumper --cpuusage", ShellCatcher::CATCHER_CPU, pid);
587 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
588
589 shellCatcher->Initialize("hidumper -s PowerManagerService -a -s", ShellCatcher::CATCHER_PMS, pid);
590 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
591
592 shellCatcher->Initialize("hidumper -s DisplayPowerManagerService", ShellCatcher::CATCHER_DPMS, pid);
593 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
594
595 shellCatcher->Initialize("hidumper -s RenderService -a allInfo", ShellCatcher::CATCHER_RS, pid);
596 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
597
598 shellCatcher->Initialize("hilog -x", ShellCatcher::CATCHER_HILOG, 0);
599 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
600
601 shellCatcher->Initialize("hilog -z", ShellCatcher::CATCHER_LIGHT_HILOG, 0);
602 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
603
604 shellCatcher->Initialize("snapshot_display -f", ShellCatcher::CATCHER_SNAPSHOT, 0);
605 EXPECT_TRUE(shellCatcher->Catch(fd, jsonFd) > 0);
606
607 shellCatcher->Initialize("scb_debug SCBScenePanel getContainerSession", ShellCatcher::CATCHER_SCBSESSION, 0);
608 printf("CATCHER_SCBSESSION result: %s\n", shellCatcher->Catch(fd, jsonFd) > 0 ? "true" : "false");
609
610 shellCatcher->Initialize("scb_debug SCBScenePanel getViewParam", ShellCatcher::CATCHER_SCBVIEWPARAM, 0);
611 printf("CATCHER_SCBVIEWPARAM result: %s\n", shellCatcher->Catch(fd, jsonFd) > 0 ? "true" : "false");
612
613 shellCatcher->Initialize("hidumper -s WindowManagerService -a -w -default", ShellCatcher::CATCHER_SCBWMS, 0);
614 EXPECT_EQ(shellCatcher->Catch(fd, jsonFd), 0);
615
616 auto jsonStr = "{\"domain_\":\"KERNEL_VENDOR\"}";
617 std::shared_ptr<SysEvent> event = std::make_shared<SysEvent>("ShellCatcherTest", nullptr, jsonStr);
618 event->SetValue("FOCUS_WINDOW", 4); // 4 test value
619 shellCatcher->SetEvent(event);
620 shellCatcher->Initialize("hidumper -s WindowManagerService -a -w -default", ShellCatcher::CATCHER_SCBWMS, 0);
621 printf("CATCHER_SCBWMS result: %s\n", shellCatcher->Catch(fd, jsonFd) > 0 ? "true" : "false");
622
623 shellCatcher->Initialize("hilog -T InputKeyFlow -z 1000", ShellCatcher::CATCHER_INPUT_HILOG, 0);
624 shellCatcher->Initialize("hilog -T InputKeyFlow -e eventId -z 1000",
625 ShellCatcher::CATCHER_INPUT_EVENT_HILOG, 0);
626
627 shellCatcher->Initialize("default", -1, 0);
628 EXPECT_EQ(shellCatcher->Catch(fd, jsonFd), 0);
629
630 close(fd);
631 }
632
633 /**
634 * @tc.name: ShellCatcherTest
635 * @tc.desc: GET_DISPLAY_SNAPSHOT test
636 * @tc.type: FUNC
637 */
638 HWTEST_F(EventloggerCatcherTest, ShellCatcherTest_002, TestSize.Level1)
639 {
640 int windowId = 4;
641 int ret = HiSysEventWrite(HiSysEvent::Domain::WINDOW_MANAGER,
642 "GET_DISPLAY_SNAPSHOT",
643 HiSysEvent::EventType::STATISTIC,
644 "FOCUS_WINDOW", windowId);
645 printf("HiSysEventWrite: %d\n", ret);
646 EXPECT_EQ(ret, 0);
647 }
648
649 /**
650 * @tc.name: ShellCatcherTest
651 * @tc.desc: add test
652 * @tc.type: FUNC
653 */
654 HWTEST_F(EventloggerCatcherTest, ShellCatcherTest_003, TestSize.Level1)
655 {
656 auto fd = open("/data/test/testFile", O_CREAT | O_WRONLY | O_TRUNC, DEFAULT_MODE);
657 if (fd < 0) {
658 printf("Fail to create testFile. errno: %d\n", errno);
659 FAIL();
660 }
661 auto shellCatcher = std::make_shared<ShellCatcher>();
662 shellCatcher->Initialize("hidumper -s 1910 -a DumpAppMap", ShellCatcher::CATCHER_DAM, 0);
663 EXPECT_TRUE(shellCatcher->Catch(fd, 1) >= 0);
664 printf("DumpAppMap result: %s\n", shellCatcher->Catch(fd, 1) > 0 ? "true" : "false");
665 close(fd);
666 }
667
668 /**
669 * @tc.name: LogCatcherUtilsTest_001
670 * @tc.desc: add test
671 * @tc.type: FUNC
672 */
673 HWTEST_F(EventloggerCatcherTest, LogCatcherUtilsTest_001, TestSize.Level1)
674 {
675 int pid = getpid();
676 int ret = LogCatcherUtils::DumpStacktrace(-1, pid);
677 EXPECT_EQ(ret, -1);
678 LogCatcherUtils::DumpStacktrace(1, pid);
679 LogCatcherUtils::DumpStacktrace(2, pid);
680 ret = LogCatcherUtils::WriteKernelStackToFd(2, "Test", -1);
681 EXPECT_EQ(ret, -1);
682 ret = LogCatcherUtils::WriteKernelStackToFd(200, "Test\n", getprocpid());
683 EXPECT_EQ(ret, 0);
684 ret = LogCatcherUtils::WriteKernelStackToFd(200, "Test\n", getprocpid());
685 EXPECT_EQ(ret, 0);
686 }
687 } // namespace HiviewDFX
688 } // namespace OHOS
689