• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <gtest/gtest.h>
17 
18 #define private public
19 #include "common_event_command.h"
20 #undef private
21 #include "common_event_manager.h"
22 #include "common_event_subscriber.h"
23 #include "mock_common_event_stub.h"
24 #include "singleton.h"
25 
26 using namespace testing::ext;
27 using namespace OHOS;
28 using namespace OHOS::AAFwk;
29 using namespace OHOS::EventFwk;
30 
31 namespace {
32 const std::string STRING_EVENT = "com.ces.event";
33 
Concatenate(const std::string & first,const std::string & second)34 static std::string Concatenate(const std::string &first,  const std::string &second)
35 {
36     return first + second;
37 }
38 
39 class CemCommandDumpTest : public testing::Test {
40 public:
41     static void SetUpTestCase();
42     static void TearDownTestCase();
43     void SetUp() override;
44     void TearDown() override;
45 
46     void MakeMockObjects();
47     void SetMockObjects(const CommonEventCommand &cmd) const;
48 
49     std::string cmd_ = "dump";
50     std::string toolName_ = TOOL_NAME;
51     sptr<ICommonEvent> proxyPtr_;
52 };
53 
SetUpTestCase()54 void CemCommandDumpTest::SetUpTestCase()
55 {}
56 
TearDownTestCase()57 void CemCommandDumpTest::TearDownTestCase()
58 {}
59 
SetUp()60 void CemCommandDumpTest::SetUp()
61 {
62     // reset optind to 0
63     optind = 0;
64 
65     // make mock objects
66     MakeMockObjects();
67 }
68 
TearDown()69 void CemCommandDumpTest::TearDown()
70 {}
71 
MakeMockObjects()72 void CemCommandDumpTest::MakeMockObjects()
73 {
74     // mock a stub
75     auto stubPtr = sptr<IRemoteObject>(new MockCommonEventStub());
76 
77     // mock a proxy
78     proxyPtr_ = iface_cast<ICommonEvent>(stubPtr);
79 
80     // set the mock proxy
81     auto commonEventPtr = DelayedSingleton<CommonEvent>::GetInstance();
82     commonEventPtr->isProxyValid_ = true;
83     commonEventPtr->commonEventProxy_ = proxyPtr_;
84 }
85 
SetMockObjects(const CommonEventCommand & cmd) const86 void CemCommandDumpTest::SetMockObjects(const CommonEventCommand &cmd) const
87 {}
88 
89 class CommonEventSubscriberTest : public CommonEventSubscriber {
90 public:
CommonEventSubscriberTest(const CommonEventSubscribeInfo & subscribeInfo)91     explicit CommonEventSubscriberTest(const CommonEventSubscribeInfo &subscribeInfo)
92         : CommonEventSubscriber(subscribeInfo)
93     {}
94 
~CommonEventSubscriberTest()95     ~CommonEventSubscriberTest()
96     {}
97 
OnReceiveEvent(const CommonEventData & data)98     void OnReceiveEvent(const CommonEventData &data)
99     {}
100 };
101 
102 /**
103  * @tc.number: Cem_Command_Dump_0100
104  * @tc.name: ExecCommand
105  * @tc.desc: Verify the "cem dump" command.
106  */
107 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_0100, Function | MediumTest | Level1)
108 {
109     char *argv[] = {
110         (char *)toolName_.c_str(),
111         (char *)cmd_.c_str(),
112         (char *)"",
113     };
114     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
115 
116     CommonEventCommand cmd(argc, argv);
117 
118     // set the mock objects
119     SetMockObjects(cmd);
120 
121     EXPECT_EQ(cmd.ExecCommand(), Concatenate(HELP_MSG_NO_OPTION, HELP_MSG_DUMP));
122 }
123 
124 /**
125  * @tc.number: Cem_Command_Dump_0200
126  * @tc.name: ExecCommand
127  * @tc.desc: Verify the "cem dump xxx" command.
128  */
129 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_0200, Function | MediumTest | Level1)
130 {
131     char *argv[] = {
132         (char *)toolName_.c_str(),
133         (char *)cmd_.c_str(),
134         (char *)"xxx",
135         (char *)"",
136     };
137     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
138 
139     CommonEventCommand cmd(argc, argv);
140     // set the mock objects
141     SetMockObjects(cmd);
142 
143     EXPECT_EQ(cmd.ExecCommand(), Concatenate(HELP_MSG_NO_OPTION, HELP_MSG_DUMP));
144 }
145 
146 /**
147  * @tc.number: Cem_Command_Dump_0300
148  * @tc.name: ExecCommand
149  * @tc.desc: Verify the "cem dump -x" command.
150  */
151 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_0300, Function | MediumTest | Level1)
152 {
153     char *argv[] = {
154         (char *)toolName_.c_str(),
155         (char *)cmd_.c_str(),
156         (char *)"-x",
157         (char *)"",
158     };
159     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
160 
161     CommonEventCommand cmd(argc, argv);
162 
163     // set the mock objects
164     SetMockObjects(cmd);
165 
166     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: unknown option.\n", HELP_MSG_DUMP));
167 }
168 
169 /**
170  * @tc.number: Cem_Command_Dump_0400
171  * @tc.name: ExecCommand
172  * @tc.desc: Verify the "cem dump -xxx" command.
173  */
174 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_0400, Function | MediumTest | Level1)
175 {
176     char *argv[] = {
177         (char *)toolName_.c_str(),
178         (char *)cmd_.c_str(),
179         (char *)"-xxx",
180         (char *)"",
181     };
182     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
183 
184     CommonEventCommand cmd(argc, argv);
185 
186     // set the mock objects
187     SetMockObjects(cmd);
188 
189     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: unknown option.\n", HELP_MSG_DUMP));
190 }
191 
192 /**
193  * @tc.number: Cem_Command_Dump_0500
194  * @tc.name: ExecCommand
195  * @tc.desc: Verify the "cem dump --x" command.
196  */
197 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_0500, Function | MediumTest | Level1)
198 {
199     char *argv[] = {
200         (char *)toolName_.c_str(),
201         (char *)cmd_.c_str(),
202         (char *)"--x",
203         (char *)"",
204     };
205     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
206 
207     CommonEventCommand cmd(argc, argv);
208 
209     // set the mock objects
210     SetMockObjects(cmd);
211 
212     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: unknown option.\n", HELP_MSG_DUMP));
213 }
214 
215 /**
216  * @tc.number: Cem_Command_Dump_0600
217  * @tc.name: ExecCommand
218  * @tc.desc: Verify the "cem dump --xxx" command.
219  */
220 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_0600, Function | MediumTest | Level1)
221 {
222     char *argv[] = {
223         (char *)toolName_.c_str(),
224         (char *)cmd_.c_str(),
225         (char *)"--xxx",
226         (char *)"",
227     };
228     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
229 
230     CommonEventCommand cmd(argc, argv);
231 
232     // set the mock objects
233     SetMockObjects(cmd);
234 
235     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: unknown option.\n", HELP_MSG_DUMP));
236 }
237 
238 /**
239  * @tc.number: Cem_Command_Dump_0700
240  * @tc.name: ExecCommand
241  * @tc.desc: Verify the "cem dump -h" command.
242  */
243 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_0700, Function | MediumTest | Level1)
244 {
245     char *argv[] = {
246         (char *)toolName_.c_str(),
247         (char *)cmd_.c_str(),
248         (char *)"-h",
249         (char *)"",
250     };
251     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
252 
253     CommonEventCommand cmd(argc, argv);
254 
255     // set the mock objects
256     SetMockObjects(cmd);
257 
258     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_DUMP);
259 }
260 
261 /**
262  * @tc.number: Cem_Command_Dump_0800
263  * @tc.name: ExecCommand
264  * @tc.desc: Verify the "cem dump --help" command.
265  */
266 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_0800, Function | MediumTest | Level1)
267 {
268     char *argv[] = {
269         (char *)toolName_.c_str(),
270         (char *)cmd_.c_str(),
271         (char *)"--help",
272         (char *)"",
273     };
274     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
275 
276     CommonEventCommand cmd(argc, argv);
277 
278     // set the mock objects
279     SetMockObjects(cmd);
280 
281     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_DUMP);
282 }
283 
284 /**
285  * @tc.number: Cem_Command_Dump_0900
286  * @tc.name: ExecCommand
287  * @tc.desc: Verify the "cem dump -a" command.
288  */
289 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_0900, Function | MediumTest | Level1)
290 {
291     char *argv[] = {
292         (char *)toolName_.c_str(),
293         (char *)cmd_.c_str(),
294         (char *)"-a",
295         (char *)"",
296     };
297     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
298 
299     CommonEventCommand cmd(argc, argv);
300 
301     // set the mock objects
302     SetMockObjects(cmd);
303 
304     EXPECT_EQ(cmd.ExecCommand(), "");
305 }
306 
307 /**
308  * @tc.number: Cem_Command_Dump_1000
309  * @tc.name: ExecCommand
310  * @tc.desc: Verify the "cem dump --all" command with no subscriber.
311  */
312 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_1000, Function | MediumTest | Level1)
313 {
314     char *argv[] = {
315         (char *)toolName_.c_str(),
316         (char *)cmd_.c_str(),
317         (char *)"--all",
318         (char *)"",
319     };
320     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
321 
322     CommonEventCommand cmd(argc, argv);
323 
324     // set the mock objects
325     SetMockObjects(cmd);
326 
327     EXPECT_EQ(cmd.ExecCommand(), "");
328 }
329 
330 /**
331  * @tc.number: Cem_Command_Dump_1100
332  * @tc.name: ExecCommand
333  * @tc.desc: Verify the "cem dump -a" command with a subscriber.
334  */
335 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_1100, Function | MediumTest | Level1)
336 {
337     /* Subscribe */
338 
339     // make matching skills
340     MatchingSkills matchingSkills;
341     matchingSkills.AddEvent(STRING_EVENT);
342 
343     // make subscribe info
344     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
345 
346     // make a subscriber object
347     auto subscriberTestPtr = std::make_shared<CommonEventSubscriberTest>(subscribeInfo);
348     // subscribe a common event
349     CommonEventManager::SubscribeCommonEvent(subscriberTestPtr);
350 
351     char *argv[] = {
352         (char *)toolName_.c_str(),
353         (char *)cmd_.c_str(),
354         (char *)"-a",
355         (char *)"",
356     };
357     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
358 
359     CommonEventCommand cmd(argc, argv);
360 
361     // set the mock objects
362     SetMockObjects(cmd);
363     EXPECT_EQ(cmd.ExecCommand(), STRING_EVENT + "\n");
364 }
365 
366 /**
367  * @tc.number: Cem_Command_Dump_1200
368  * @tc.name: ExecCommand
369  * @tc.desc: Verify the "cem dump -e" command.
370  */
371 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_1200, Function | MediumTest | Level1)
372 {
373     char *argv[] = {
374         (char *)toolName_.c_str(),
375         (char *)cmd_.c_str(),
376         (char *)"-e",
377         (char *)"",
378     };
379     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
380 
381     CommonEventCommand cmd(argc, argv);
382 
383     // set the mock objects
384     SetMockObjects(cmd);
385 
386     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: option 'e' requires a value.\n", HELP_MSG_DUMP));
387 }
388 
389 /**
390  * @tc.number: Cem_Command_Dump_1300
391  * @tc.name: ExecCommand
392  * @tc.desc: Verify the "cem dump -e <name>" command with no subscriber.
393  */
394 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_1300, Function | MediumTest | Level1)
395 {
396     char *argv[] = {
397         (char *)toolName_.c_str(),
398         (char *)cmd_.c_str(),
399         (char *)"-e",
400         (char *)STRING_EVENT.c_str(),
401         (char *)"",
402     };
403     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
404 
405     CommonEventCommand cmd(argc, argv);
406 
407     // set the mock objects
408     SetMockObjects(cmd);
409 
410     EXPECT_EQ(cmd.ExecCommand(), "");
411 }
412 
413 /**
414  * @tc.number: Cem_Command_Dump_1400
415  * @tc.name: ExecCommand
416  * @tc.desc: Verify the "cem dump -e <event>" command with a subscriber.
417  */
418 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_1400, Function | MediumTest | Level1)
419 {
420     /* Subscribe */
421 
422     // make matching skills
423     MatchingSkills matchingSkills;
424     matchingSkills.AddEvent(STRING_EVENT);
425 
426     // make subscribe info
427     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
428 
429     // make a subscriber object
430     auto subscriberTestPtr = std::make_shared<CommonEventSubscriberTest>(subscribeInfo);
431     // subscribe a common event
432     CommonEventManager::SubscribeCommonEvent(subscriberTestPtr);
433 
434     char *argv[] = {
435         (char *)toolName_.c_str(),
436         (char *)cmd_.c_str(),
437         (char *)"-e",
438         (char *)STRING_EVENT.c_str(),
439         (char *)"",
440     };
441     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
442 
443     CommonEventCommand cmd(argc, argv);
444 
445     // set the mock objects
446     SetMockObjects(cmd);
447     EXPECT_EQ(cmd.ExecCommand(), STRING_EVENT + "\n");
448 }
449 
450 /**
451  * @tc.number: Cem_Command_Dump_1500
452  * @tc.name: ExecCommand
453  * @tc.desc: Verify the "cem dump -u" command.
454  */
455 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_1500, Function | MediumTest | Level1)
456 {
457     char *argv[] = {
458         (char *)toolName_.c_str(),
459         (char *)cmd_.c_str(),
460         (char *)"-u",
461         (char *)"",
462     };
463     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
464     CommonEventCommand cmd(argc, argv);
465     // set the mock objects
466     SetMockObjects(cmd);
467     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: option 'u' requires a value.\n", HELP_MSG_DUMP));
468 }
469 
470 /**
471  * @tc.number: Cem_Command_Dump_1600
472  * @tc.name: ExecCommand
473  * @tc.desc: Verify the "cem dump -u <user-id>" command with no subscriber.
474  */
475 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_1600, Function | MediumTest | Level1)
476 {
477     char *argv[] = {
478         (char *)toolName_.c_str(),
479         (char *)cmd_.c_str(),
480         (char *)"-u",
481         (char *)"100",
482         (char *)"",
483     };
484     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
485     CommonEventCommand cmd(argc, argv);
486     // set the mock objects
487     SetMockObjects(cmd);
488     EXPECT_EQ(cmd.ExecCommand(), "");
489 }
490 
491 /**
492  * @tc.number: Cem_Command_Dump_1700
493  * @tc.name: ExecCommand
494  * @tc.desc: Verify the "cem dump -e <name> -u <user-id>" command with no subscriber.
495  */
496 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_1700, Function | MediumTest | Level1)
497 {
498     char *argv[] = {
499         (char *)toolName_.c_str(),
500         (char *)cmd_.c_str(),
501         (char *)"-e",
502         (char *)STRING_EVENT.c_str(),
503         (char *)"-u",
504         (char *)"100",
505         (char *)"",
506     };
507     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
508     CommonEventCommand cmd(argc, argv);
509     // set the mock objects
510     SetMockObjects(cmd);
511     EXPECT_EQ(cmd.ExecCommand(), "");
512 }
513 
514 /**
515  * @tc.number: Cem_Command_Dump_1800
516  * @tc.name: ExecCommand
517  * @tc.desc: Verify the "cem dump -e <name> -u <user-id>" command with a subscriber.
518  */
519 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_1800, Function | MediumTest | Level1)
520 {
521     // make matching skills
522     MatchingSkills matchingSkills;
523     matchingSkills.AddEvent(STRING_EVENT);
524     // make subscribe info
525     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
526     // make a subscriber object
527     auto subscriberTestPtr = std::make_shared<CommonEventSubscriberTest>(subscribeInfo);
528     // subscribe a common event
529     CommonEventManager::SubscribeCommonEvent(subscriberTestPtr);
530     char *argv[] = {
531         (char *)toolName_.c_str(),
532         (char *)cmd_.c_str(),
533         (char *)"-e",
534         (char *)STRING_EVENT.c_str(),
535         (char *)"-u",
536         (char *)"100",
537         (char *)"",
538     };
539     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
540     CommonEventCommand cmd(argc, argv);
541     // set the mock objects
542     SetMockObjects(cmd);
543     EXPECT_EQ(cmd.ExecCommand(), STRING_EVENT + "\n");
544 }
545 
546 /**
547  * @tc.number: Cem_Command_Dump_1900
548  * @tc.name: ExecCommand
549  * @tc.desc: Verify the "cem dump -p" command.
550  * @tc.require: issueI5UINZ
551  */
552 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_1900, Function | MediumTest | Level1)
553 {
554     char *argv[] = {
555         (char *)toolName_.c_str(),
556         (char *)cmd_.c_str(),
557         (char *)"-e",
558         (char *)STRING_EVENT.c_str(),
559         (char *)"-u",
560         (char *)"100",
561         (char *)STRING_EVENT.c_str(),
562         (char *)"-p",
563         (char *)"",
564     };
565     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
566     CommonEventCommand cmd(argc, argv);
567     // set the mock objects
568     SetMockObjects(cmd);
569     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: unknown option.\n", HELP_MSG_DUMP));
570 }
571 
572 /**
573  * @tc.number: Cem_Command_Dump_2000
574  * @tc.name: ExecCommand
575  * @tc.desc: Verify the "cem dump -p" command.
576  * @tc.require: issueI5UINZ
577  */
578 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_2000, Function | MediumTest | Level1)
579 {
580       /* Subscribe */
581 
582     // make matching skills
583     MatchingSkills matchingSkills;
584     matchingSkills.AddEvent(STRING_EVENT);
585 
586     // make subscribe info
587     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
588 
589     // make a subscriber object
590     auto subscriberTestPtr = std::make_shared<CommonEventSubscriberTest>(subscribeInfo);
591     // subscribe a common event
592     CommonEventManager::SubscribeCommonEvent(subscriberTestPtr);
593     char *argv[] = {
594         (char *)toolName_.c_str(),
595         (char *)cmd_.c_str(),
596         (char *)"-p",
597         (char *)"11",
598         (char *)"",
599     };
600     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
601     CommonEventCommand cmd(argc, argv);
602     // set the mock objects
603     SetMockObjects(cmd);
604     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: option 'p' requires a value.\n", HELP_MSG_DUMP));
605 }
606 }  // namespace
607