• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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 <fcntl.h>
17 #include <gtest/gtest.h>
18 
19 #include <cerrno>
20 #include <cstdio>
21 
22 #include "event_dump.h"
23 #include "event_log_helper.h"
24 #include "i_input_windows_manager.h"
25 #include "input_device_manager.h"
26 #include "input_event_handler.h"
27 #include "input_manager.h"
28 #include "input_manager_util.h"
29 #include "mouse_event_normalize.h"
30 #include "multimodal_event_handler.h"
31 #include "system_info.h"
32 #include "event_statistic.h"
33 
34 #undef MMI_LOG_TAG
35 #define MMI_LOG_TAG "EventDumpTest"
36 
37 namespace OHOS {
38 namespace MMI {
39 namespace {
40 using namespace testing::ext;
41 const std::string TEST_FILE_NAME = "/data/log.log";
42 constexpr int32_t MAX_COMMAND_COUNT { 32 };
43 } // namespace
44 
45 class EventDumpTest : public testing::Test {
46 public:
SetUp()47     void SetUp() override
48     {
49         fd_ = open(TEST_FILE_NAME.c_str(), O_WRONLY);
50         ASSERT_NE(MMIEventDump, nullptr) << "MMIEventDump is null, cannot run test case";
51     }
52 
TearDown()53     void TearDown() override
54     {
55         close(fd_);
56         fd_ = -1;
57     }
58 
59     int32_t fd_;
60 };
61 
62 /**
63  * @tc.name: EventDumpTest_CheckCount_001
64  * @tc.desc: Event dump CheckCount
65  * @tc.type: FUNC
66  * @tc.require:
67  */
68 HWTEST_F(EventDumpTest, EventDumpTest001, TestSize.Level1)
69 {
70     CALL_TEST_DEBUG;
71     std::vector<std::string> args;
72     int32_t count = 0;
73     MMIEventDump->CheckCount(fd_, args, count);
74     EXPECT_EQ(count, 0);
75 }
76 
77 /**
78  * @tc.name: EventDumpTest_CheckCount_002
79  * @tc.desc: Event dump CheckCount
80  * @tc.type: FUNC
81  * @tc.require:
82  */
83 HWTEST_F(EventDumpTest, EventDumpTest002, TestSize.Level1)
84 {
85     CALL_TEST_DEBUG;
86     std::vector<std::string> args = {"--help"};
87     int32_t count = 0;
88     MMIEventDump->CheckCount(fd_, args, count);
89     MMIEventDump->ParseCommand(fd_, args);
90     EXPECT_EQ(count, 1);
91 }
92 
93 /**
94  * @tc.name: EventDumpTest_CheckCount_003
95  * @tc.desc: Event dump CheckCount
96  * @tc.type: FUNC
97  * @tc.require:
98  */
99 HWTEST_F(EventDumpTest, EventDumpTest003, TestSize.Level1)
100 {
101     CALL_TEST_DEBUG;
102     std::vector<std::string> args = {"-h"};
103     int32_t count = 0;
104     MMIEventDump->CheckCount(fd_, args, count);
105     MMIEventDump->ParseCommand(fd_, args);
106     EXPECT_EQ(count, 1);
107 }
108 
109 /**
110  * @tc.name: EventDumpTest_CheckCount_004
111  * @tc.desc: Event dump CheckCount
112  * @tc.type: FUNC
113  * @tc.require:
114  */
115 HWTEST_F(EventDumpTest, EventDumpTest004, TestSize.Level1)
116 {
117     CALL_TEST_DEBUG;
118     std::vector<std::string> args = {"-abc"};
119     int32_t count = 0;
120     MMIEventDump->CheckCount(fd_, args, count);
121     MMIEventDump->ParseCommand(fd_, args);
122     EXPECT_EQ(count, 3);
123 }
124 
125 /**
126  * @tc.name: EventDumpTest_CheckCount_005
127  * @tc.desc: Event dump CheckCount
128  * @tc.type: FUNC
129  * @tc.require:
130  */
131 HWTEST_F(EventDumpTest, EventDumpTest005, TestSize.Level1)
132 {
133     CALL_TEST_DEBUG;
134     std::vector<std::string> args = {"-a", "--help", "foo", "-bc", "bar"};
135     int32_t count = 0;
136     MMIEventDump->CheckCount(fd_, args, count);
137     MMIEventDump->ParseCommand(fd_, args);
138     MMIEventDump->DumpEventHelp(fd_, args);
139     EXPECT_EQ(count, 4);
140 }
141 
142 /**
143  * @tc.name: EventDumpTest_006
144  * @tc.desc: Event dump InputDeviceManager
145  * @tc.type: FUNC
146  * @tc.require:
147  */
148 HWTEST_F(EventDumpTest, EventDumpTest006, TestSize.Level1)
149 {
150     CALL_TEST_DEBUG;
151     std::vector<std::string> args = {"-d"};
152     int32_t count = 0;
153     MMIEventDump->CheckCount(fd_, args, count);
154     MMIEventDump->ParseCommand(fd_, args);
155     ASSERT_NO_FATAL_FAILURE(INPUT_DEV_MGR->Dump(fd_, args));
156 }
157 
158 /**
159  * @tc.name: EventDumpTest_007
160  * @tc.desc: Event dump DeviceList
161  * @tc.type: FUNC
162  * @tc.require:
163  */
164 HWTEST_F(EventDumpTest, EventDumpTest007, TestSize.Level1)
165 {
166     CALL_TEST_DEBUG;
167     std::vector<std::string> args = {"-l"};
168     int32_t count = 0;
169     MMIEventDump->CheckCount(fd_, args, count);
170     MMIEventDump->ParseCommand(fd_, args);
171     ASSERT_NO_FATAL_FAILURE(INPUT_DEV_MGR->DumpDeviceList(fd_, args));
172 }
173 
174 /**
175  * @tc.name: EventDumpTest_008
176  * @tc.desc: Event dump WindowsManager
177  * @tc.type: FUNC
178  * @tc.require:
179  */
180 HWTEST_F(EventDumpTest, EventDumpTest008, TestSize.Level1)
181 {
182     CALL_TEST_DEBUG;
183     std::vector<std::string> args = {"-w"};
184     int32_t count = 0;
185     MMIEventDump->CheckCount(fd_, args, count);
186     MMIEventDump->ParseCommand(fd_, args);
187     ASSERT_NO_FATAL_FAILURE(WIN_MGR->Dump(fd_, args));
188 }
189 
190 /**
191  * @tc.name: EventDumpTest_009
192  * @tc.desc: Event dump UDSServer
193  * @tc.type: FUNC
194  * @tc.require:
195  */
196 HWTEST_F(EventDumpTest, EventDumpTest009, TestSize.Level1)
197 {
198     CALL_TEST_DEBUG;
199     std::vector<std::string> args = {"-u"};
200     int32_t count = 0;
201     auto udsServer = InputHandler->GetUDSServer();
202     CHKPV(udsServer);
203     MMIEventDump->CheckCount(fd_, args, count);
204     MMIEventDump->ParseCommand(fd_, args);
205     ASSERT_NO_FATAL_FAILURE(udsServer->Dump(fd_, args));
206 }
207 
208 /**
209  * @tc.name: EventDumpTest_010
210  * @tc.desc: Event dump SubscriberHandler
211  * @tc.type: FUNC
212  * @tc.require:
213  */
214 HWTEST_F(EventDumpTest, EventDumpTest010, TestSize.Level1)
215 {
216     CALL_TEST_DEBUG;
217     std::vector<std::string> args = {"-s"};
218     int32_t count = 0;
219     auto subscriberHandler = InputHandler->GetSubscriberHandler();
220     CHKPV(subscriberHandler);
221     MMIEventDump->CheckCount(fd_, args, count);
222     MMIEventDump->ParseCommand(fd_, args);
223     ASSERT_NO_FATAL_FAILURE(subscriberHandler->Dump(fd_, args));
224 }
225 
226 /**
227  * @tc.name: EventDumpTest_011
228  * @tc.desc: Event dump MonitorHandler
229  * @tc.type: FUNC
230  * @tc.require:
231  */
232 HWTEST_F(EventDumpTest, EventDumpTest011, TestSize.Level1)
233 {
234     CALL_TEST_DEBUG;
235     std::vector<std::string> args = {"-o"};
236     int32_t count = 0;
237     auto monitorHandler = InputHandler->GetMonitorHandler();
238     CHKPV(monitorHandler);
239     MMIEventDump->CheckCount(fd_, args, count);
240     MMIEventDump->ParseCommand(fd_, args);
241     ASSERT_NO_FATAL_FAILURE(monitorHandler->Dump(fd_, args));
242 }
243 
244 /**
245  * @tc.name: EventDumpTest_012
246  * @tc.desc: Event dump InterceptorHandler
247  * @tc.type: FUNC
248  * @tc.require:
249  */
250 HWTEST_F(EventDumpTest, EventDumpTest012, TestSize.Level1)
251 {
252     CALL_TEST_DEBUG;
253     std::vector<std::string> args = {"-i"};
254     int32_t count = 0;
255     auto interceptorHandler = InputHandler->GetInterceptorHandler();
256     CHKPV(interceptorHandler);
257     MMIEventDump->CheckCount(fd_, args, count);
258     MMIEventDump->ParseCommand(fd_, args);
259     ASSERT_NO_FATAL_FAILURE(interceptorHandler->Dump(fd_, args));
260 }
261 
262 /**
263  * @tc.name: EventDumpTest_013
264  * @tc.desc: Event dump FilterHandler
265  * @tc.type: FUNC
266  * @tc.require:
267  */
268 HWTEST_F(EventDumpTest, EventDumpTest013, TestSize.Level1)
269 {
270     CALL_TEST_DEBUG;
271     std::vector<std::string> args = {"-f"};
272     int32_t count = 0;
273     auto filterHandler = InputHandler->GetFilterHandler();
274     CHKPV(filterHandler);
275     MMIEventDump->CheckCount(fd_, args, count);
276     MMIEventDump->ParseCommand(fd_, args);
277     ASSERT_NO_FATAL_FAILURE(filterHandler->Dump(fd_, args));
278 }
279 
280 /**
281  * @tc.name: EventDumpTest_014
282  * @tc.desc: Event dump MouseEventHandler
283  * @tc.type: FUNC
284  * @tc.require:
285  */
286 HWTEST_F(EventDumpTest, EventDumpTest014, TestSize.Level1)
287 {
288     CALL_TEST_DEBUG;
289     std::vector<std::string> args = {"-m"};
290     int32_t count = 0;
291     MMIEventDump->CheckCount(fd_, args, count);
292     MMIEventDump->ParseCommand(fd_, args);
293     ASSERT_NO_FATAL_FAILURE(MouseEventHdr->Dump(fd_, args));
294 }
295 
296 /**
297  * @tc.name: EventDumpTest_015
298  * @tc.desc: Event dump event
299  * @tc.type: FUNC
300  * @tc.require:
301  */
302 HWTEST_F(EventDumpTest, EventDumpTest015, TestSize.Level1)
303 {
304     CALL_TEST_DEBUG;
305     std::vector<std::string> args = {"-e"};
306     int32_t count = 0;
307     MMIEventDump->CheckCount(fd_, args, count);
308     MMIEventDump->ParseCommand(fd_, args);
309     ASSERT_NO_FATAL_FAILURE(EventStatistic::Dump(fd_, args));
310 }
311 
312 /**
313  * @tc.name: EventDumpTest_CheckCount_001
314  * @tc.desc: Verify CheckCount with args containing options starting with "--"
315  * @tc.type: FUNC
316  * @tc.require:
317  */
318 HWTEST_F(EventDumpTest, EventDumpTest_CheckCount_001, TestSize.Level1)
319 {
320     CALL_TEST_DEBUG;
321     int32_t count = 0;
322     std::vector<std::string> args = {"--help", "--version"};
323     MMIEventDump->CheckCount(fd_, args, count);
324     EXPECT_EQ(count, 2);
325 }
326 
327 /**
328  * @tc.name: EventDumpTest_CheckCount_002
329  * @tc.desc: Verify CheckCount with args containing options starting with "-"
330  * @tc.type: FUNC
331  * @tc.require:
332  */
333 HWTEST_F(EventDumpTest, EventDumpTest_CheckCount_002, TestSize.Level1)
334 {
335     CALL_TEST_DEBUG;
336     int32_t count = 0;
337     std::vector<std::string> args = {"-abc", "-d"};
338     MMIEventDump->CheckCount(fd_, args, count);
339     EXPECT_EQ(count, 4);
340 }
341 
342 /**
343  * @tc.name: EventDumpTest_CheckCount_003
344  * @tc.desc: Verify CheckCount with mixed args, including normal strings
345  * @tc.type: FUNC
346  * @tc.require:
347  */
348 HWTEST_F(EventDumpTest, EventDumpTest_CheckCount_003, TestSize.Level1)
349 {
350     CALL_TEST_DEBUG;
351     int32_t count = 0;
352     std::vector<std::string> args = {"--opt", "-xy", "normal", "-z"};
353     MMIEventDump->CheckCount(fd_, args, count);
354     EXPECT_EQ(count, 4);
355 }
356 
357 /**
358  * @tc.name: EventDumpTest_CheckCount_004
359  * @tc.desc: Verify CheckCount with empty args
360  * @tc.type: FUNC
361  * @tc.require:
362  */
363 HWTEST_F(EventDumpTest, EventDumpTest_CheckCount_004, TestSize.Level1)
364 {
365     CALL_TEST_DEBUG;
366     int32_t count = 0;
367     std::vector<std::string> args = {};
368     MMIEventDump->CheckCount(fd_, args, count);
369     EXPECT_EQ(count, 0);
370 }
371 
372 /**
373  * @tc.name: EventDumpTest_ParseCommand_001
374  * @tc.desc: Verify ParseCommand when args is empty
375  * @tc.type: FUNC
376  * @tc.require:
377  */
378 HWTEST_F(EventDumpTest, EventDumpTest_ParseCommand_001, TestSize.Level1)
379 {
380     CALL_TEST_DEBUG;
381     std::vector<std::string> args;
382     EXPECT_NO_FATAL_FAILURE(MMIEventDump->ParseCommand(fd_, args));
383 }
384 
385 /**
386  * @tc.name: EventDumpTest_ParseCommand_002
387  * @tc.desc: Verify ParseCommand when count > MAX_COMMAND_COUNT
388  * @tc.type: FUNC
389  * @tc.require:
390  */
391 HWTEST_F(EventDumpTest, EventDumpTest_ParseCommand_002, TestSize.Level1)
392 {
393     CALL_TEST_DEBUG;
394     std::vector<std::string> args(MAX_COMMAND_COUNT + 5, "--test");
395     EXPECT_NO_FATAL_FAILURE(MMIEventDump->ParseCommand(fd_, args));
396 }
397 
398 /**
399  * @tc.name: EventDumpTest_ParseCommand_003
400  * @tc.desc: Verify ParseCommand with valid '-h' option
401  * @tc.type: FUNC
402  * @tc.require:
403  */
404 HWTEST_F(EventDumpTest, EventDumpTest_ParseCommand_003, TestSize.Level1)
405 {
406     CALL_TEST_DEBUG;
407     std::vector<std::string> args = {"-h"};
408     EXPECT_NO_FATAL_FAILURE(MMIEventDump->ParseCommand(fd_, args));
409 }
410 
411 /**
412  * @tc.name: EventDumpTest_ParseCommand_004
413  * @tc.desc: Verify ParseCommand with valid '-d' option
414  * @tc.type: FUNC
415  * @tc.require:
416  */
417 HWTEST_F(EventDumpTest, EventDumpTest_ParseCommand_004, TestSize.Level1)
418 {
419     CALL_TEST_DEBUG;
420     std::vector<std::string> args = {"-d"};
421     EXPECT_NO_FATAL_FAILURE(MMIEventDump->ParseCommand(fd_, args));
422 }
423 
424 /**
425  * @tc.name: EventDumpTest_ParseCommand_005
426  * @tc.desc: Verify ParseCommand with valid '-l' option
427  * @tc.type: FUNC
428  * @tc.require:
429  */
430 HWTEST_F(EventDumpTest, EventDumpTest_ParseCommand_005, TestSize.Level1)
431 {
432     CALL_TEST_DEBUG;
433     std::vector<std::string> args = {"-l"};
434     EXPECT_NO_FATAL_FAILURE(MMIEventDump->ParseCommand(fd_, args));
435 }
436 
437 /**
438  * @tc.name: EventDumpTest_ParseCommand_006
439  * @tc.desc: Verify ParseCommand with valid '-w' option
440  * @tc.type: FUNC
441  * @tc.require:
442  */
443 HWTEST_F(EventDumpTest, EventDumpTest_ParseCommand_006, TestSize.Level1)
444 {
445     CALL_TEST_DEBUG;
446     std::vector<std::string> args = {"-w"};
447     EXPECT_NO_FATAL_FAILURE(MMIEventDump->ParseCommand(fd_, args));
448 }
449 
450 /**
451  * @tc.name: EventDumpTest_ParseCommand_007
452  * @tc.desc: Verify ParseCommand with valid '-e' option
453  * @tc.type: FUNC
454  * @tc.require:
455  */
456 HWTEST_F(EventDumpTest, EventDumpTest_ParseCommand_007, TestSize.Level1)
457 {
458     CALL_TEST_DEBUG;
459     std::vector<std::string> args = {"-e"};
460     EXPECT_NO_FATAL_FAILURE(MMIEventDump->ParseCommand(fd_, args));
461 }
462 
463 /**
464  * @tc.name: EventDumpTest_ParseCommand_008
465  * @tc.desc: Verify ParseCommand with invalid option
466  * @tc.type: FUNC
467  * @tc.require:
468  */
469 HWTEST_F(EventDumpTest, EventDumpTest_ParseCommand_008, TestSize.Level1)
470 {
471     CALL_TEST_DEBUG;
472     std::vector<std::string> args = {"-z"};
473     EXPECT_NO_FATAL_FAILURE(MMIEventDump->ParseCommand(fd_, args));
474 }
475 
476 /**
477  * @tc.name: EventDumpTest_ParseCommand_009
478  * @tc.desc: Verify ParseCommand with mixed valid and invalid options
479  * @tc.type: FUNC
480  * @tc.require:
481  */
482 HWTEST_F(EventDumpTest, EventDumpTest_ParseCommand_009, TestSize.Level1)
483 {
484     CALL_TEST_DEBUG;
485     std::vector<std::string> args = {"-h", "-z", "-d"};
486     EXPECT_NO_FATAL_FAILURE(MMIEventDump->ParseCommand(fd_, args));
487 }
488 
489 /**
490  * @tc.name: EventDumpTest_DumpHelp_001
491  * @tc.desc: Verify DumpHelp prints all expected help messages.
492  * @tc.type: FUNC
493  * @tc.require:
494  */
495 HWTEST_F(EventDumpTest, EventDumpTest_DumpHelp_001, TestSize.Level1)
496 {
497     CALL_TEST_DEBUG;
498     const char *tmpFile = "/data/tmp_dumphelp.log";
499     int fd = open(tmpFile, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
500     ASSERT_GE(fd, 0);
501     ASSERT_NE(MMIEventDump, nullptr);
502     MMIEventDump->DumpHelp(fd);
503     close(fd);
504     std::string content;
505     {
506         int fdRead = open(tmpFile, O_RDONLY);
507         ASSERT_GE(fdRead, 0);
508         char buf[1024] = {};
509         ssize_t bytes = read(fdRead, buf, sizeof(buf) - 1);
510         ASSERT_GT(bytes, 0);
511         if (bytes < static_cast<ssize_t>(sizeof(buf))) {
512             buf[bytes] = '\0';
513         } else {
514             buf[sizeof(buf) - 1] = '\0';
515         }
516         content = buf;
517         close(fdRead);
518     }
519 
520     EXPECT_NE(content.find("-h, --help"), std::string::npos);
521     EXPECT_NE(content.find("-d, --device"), std::string::npos);
522     EXPECT_NE(content.find("-l, --devicelist"), std::string::npos);
523     EXPECT_NE(content.find("-w, --windows"), std::string::npos);
524     EXPECT_NE(content.find("-e, --event"), std::string::npos);
525     unlink(tmpFile);
526 }
527 
528 /**
529  * @tc.name: EventDumpTest_DumpEventHelp_001
530  * @tc.desc: Verify DumpEventHelp calls DumpHelp correctly.
531  * @tc.type: FUNC
532  * @tc.require:
533  */
534 HWTEST_F(EventDumpTest, EventDumpTest_DumpEventHelp_001, TestSize.Level1)
535 {
536     CALL_TEST_DEBUG;
537     const char *tmpFile = "/data/tmp_dumpeventhelp.log";
538     int32_t fd = open(tmpFile, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
539     ASSERT_GE(fd, 0);
540     ASSERT_NE(MMIEventDump, nullptr);
541     std::vector<std::string> args = {"-h"};
542     MMIEventDump->DumpEventHelp(fd, args);
543     close(fd);
544     std::string content;
545     {
546         int32_t fdRead = open(tmpFile, O_RDONLY);
547         ASSERT_GE(fdRead, 0);
548         char buf[1024] = {};
549         ssize_t bytes = read(fdRead, buf, sizeof(buf) - 1);
550         ASSERT_GT(bytes, 0);
551         if (bytes < static_cast<ssize_t>(sizeof(buf))) {
552             buf[bytes] = '\0';
553         } else {
554             buf[sizeof(buf) - 1] = '\0';
555         }
556         content = buf;
557         close(fdRead);
558     }
559     EXPECT_NE(content.find("Usage:"), std::string::npos);
560     unlink(tmpFile);
561 }
562 } // namespace MMI
563 } // namespace OHOS