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 SetMockObjects(const CommonEventCommand &cmd) const;
47 
48     std::string cmd_ = "dump";
49     std::string toolName_ = TOOL_NAME;
50 };
51 
SetUpTestCase()52 void CemCommandDumpTest::SetUpTestCase()
53 {}
54 
TearDownTestCase()55 void CemCommandDumpTest::TearDownTestCase()
56 {}
57 
SetUp()58 void CemCommandDumpTest::SetUp()
59 {
60     // reset optind to 0
61     optind = 0;
62 }
63 
TearDown()64 void CemCommandDumpTest::TearDown()
65 {}
66 
67 
SetMockObjects(const CommonEventCommand & cmd) const68 void CemCommandDumpTest::SetMockObjects(const CommonEventCommand &cmd) const
69 {}
70 
71 class CommonEventSubscriberTest : public CommonEventSubscriber {
72 public:
CommonEventSubscriberTest(const CommonEventSubscribeInfo & subscribeInfo)73     explicit CommonEventSubscriberTest(const CommonEventSubscribeInfo &subscribeInfo)
74         : CommonEventSubscriber(subscribeInfo)
75     {}
76 
~CommonEventSubscriberTest()77     ~CommonEventSubscriberTest()
78     {}
79 
OnReceiveEvent(const CommonEventData & data)80     void OnReceiveEvent(const CommonEventData &data)
81     {}
82 };
83 
84 /**
85  * @tc.number: Cem_Command_Dump_0100
86  * @tc.name: ExecCommand
87  * @tc.desc: Verify the "cem dump" command.
88  */
89 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_0100, Function | MediumTest | Level1)
90 {
91     char *argv[] = {
92         (char *)toolName_.c_str(),
93         (char *)cmd_.c_str(),
94         (char *)"",
95     };
96     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
97 
98     CommonEventCommand cmd(argc, argv);
99 
100     // set the mock objects
101     SetMockObjects(cmd);
102 
103     EXPECT_EQ(cmd.ExecCommand(), Concatenate(HELP_MSG_NO_OPTION, HELP_MSG_DUMP));
104 }
105 
106 /**
107  * @tc.number: Cem_Command_Dump_0200
108  * @tc.name: ExecCommand
109  * @tc.desc: Verify the "cem dump xxx" command.
110  */
111 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_0200, Function | MediumTest | Level1)
112 {
113     char *argv[] = {
114         (char *)toolName_.c_str(),
115         (char *)cmd_.c_str(),
116         (char *)"xxx",
117         (char *)"",
118     };
119     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
120 
121     CommonEventCommand cmd(argc, argv);
122     // set the mock objects
123     SetMockObjects(cmd);
124 
125     EXPECT_EQ(cmd.ExecCommand(), Concatenate(HELP_MSG_NO_OPTION, HELP_MSG_DUMP));
126 }
127 
128 /**
129  * @tc.number: Cem_Command_Dump_0300
130  * @tc.name: ExecCommand
131  * @tc.desc: Verify the "cem dump -x" command.
132  */
133 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_0300, Function | MediumTest | Level1)
134 {
135     char *argv[] = {
136         (char *)toolName_.c_str(),
137         (char *)cmd_.c_str(),
138         (char *)"-x",
139         (char *)"",
140     };
141     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
142 
143     CommonEventCommand cmd(argc, argv);
144 
145     // set the mock objects
146     SetMockObjects(cmd);
147 
148     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: unknown option.\n", HELP_MSG_DUMP));
149 }
150 
151 /**
152  * @tc.number: Cem_Command_Dump_0400
153  * @tc.name: ExecCommand
154  * @tc.desc: Verify the "cem dump -xxx" command.
155  */
156 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_0400, Function | MediumTest | Level1)
157 {
158     char *argv[] = {
159         (char *)toolName_.c_str(),
160         (char *)cmd_.c_str(),
161         (char *)"-xxx",
162         (char *)"",
163     };
164     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
165 
166     CommonEventCommand cmd(argc, argv);
167 
168     // set the mock objects
169     SetMockObjects(cmd);
170 
171     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: unknown option.\n", HELP_MSG_DUMP));
172 }
173 
174 /**
175  * @tc.number: Cem_Command_Dump_0500
176  * @tc.name: ExecCommand
177  * @tc.desc: Verify the "cem dump --x" command.
178  */
179 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_0500, Function | MediumTest | Level1)
180 {
181     char *argv[] = {
182         (char *)toolName_.c_str(),
183         (char *)cmd_.c_str(),
184         (char *)"--x",
185         (char *)"",
186     };
187     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
188 
189     CommonEventCommand cmd(argc, argv);
190 
191     // set the mock objects
192     SetMockObjects(cmd);
193     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: unknown option.\n", HELP_MSG_DUMP));
194 }
195 
196 /**
197  * @tc.number: Cem_Command_Dump_0600
198  * @tc.name: ExecCommand
199  * @tc.desc: Verify the "cem dump --xxx" command.
200  */
201 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_0600, Function | MediumTest | Level1)
202 {
203     char *argv[] = {
204         (char *)toolName_.c_str(),
205         (char *)cmd_.c_str(),
206         (char *)"--xxx",
207         (char *)"",
208     };
209     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
210 
211     CommonEventCommand cmd(argc, argv);
212 
213     // set the mock objects
214     SetMockObjects(cmd);
215     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: unknown option.\n", HELP_MSG_DUMP));
216 }
217 
218 /**
219  * @tc.number: Cem_Command_Dump_0700
220  * @tc.name: ExecCommand
221  * @tc.desc: Verify the "cem dump -h" command.
222  */
223 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_0700, Function | MediumTest | Level1)
224 {
225     char *argv[] = {
226         (char *)toolName_.c_str(),
227         (char *)cmd_.c_str(),
228         (char *)"-h",
229         (char *)"",
230     };
231     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
232 
233     CommonEventCommand cmd(argc, argv);
234 
235     // set the mock objects
236     SetMockObjects(cmd);
237 
238     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_DUMP);
239 }
240 
241 /**
242  * @tc.number: Cem_Command_Dump_0800
243  * @tc.name: ExecCommand
244  * @tc.desc: Verify the "cem dump --help" command.
245  */
246 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_0800, Function | MediumTest | Level1)
247 {
248     char *argv[] = {
249         (char *)toolName_.c_str(),
250         (char *)cmd_.c_str(),
251         (char *)"--help",
252         (char *)"",
253     };
254     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
255 
256     CommonEventCommand cmd(argc, argv);
257 
258     // set the mock objects
259     SetMockObjects(cmd);
260 
261     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_DUMP);
262 }
263 
264 /**
265  * @tc.number: Cem_Command_Dump_0900
266  * @tc.name: ExecCommand
267  * @tc.desc: Verify the "cem dump -a" command.
268  */
269 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_0900, Function | MediumTest | Level1)
270 {
271     char *argv[] = {
272         (char *)toolName_.c_str(),
273         (char *)cmd_.c_str(),
274         (char *)"-a",
275         (char *)"",
276     };
277     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
278 
279     CommonEventCommand cmd(argc, argv);
280 
281     EXPECT_EQ(cmd.ExecCommand(), "");
282 }
283 
284 /**
285  * @tc.number: Cem_Command_Dump_1000
286  * @tc.name: ExecCommand
287  * @tc.desc: Verify the "cem dump --all" command with no subscriber.
288  */
289 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_1000, Function | MediumTest | Level1)
290 {
291     char *argv[] = {
292         (char *)toolName_.c_str(),
293         (char *)cmd_.c_str(),
294         (char *)"--all",
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_1100
309  * @tc.name: ExecCommand
310  * @tc.desc: Verify the "cem dump -a" command with a subscriber.
311  */
312 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_1100, Function | MediumTest | Level1)
313 {
314     /* Subscribe */
315 
316     // make matching skills
317     MatchingSkills matchingSkills;
318     matchingSkills.AddEvent(STRING_EVENT);
319 
320     // make subscribe info
321     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
322 
323     // make a subscriber object
324     auto subscriberTestPtr = std::make_shared<CommonEventSubscriberTest>(subscribeInfo);
325     // subscribe a common event
326     CommonEventManager::SubscribeCommonEvent(subscriberTestPtr);
327 
328     char *argv[] = {
329         (char *)toolName_.c_str(),
330         (char *)cmd_.c_str(),
331         (char *)"-a",
332         (char *)"",
333     };
334     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
335 
336     CommonEventCommand cmd(argc, argv);
337 
338     // set the mock objects
339     SetMockObjects(cmd);
340     EXPECT_EQ(cmd.ExecCommand(), STRING_EVENT + "\n");
341 }
342 
343 /**
344  * @tc.number: Cem_Command_Dump_1200
345  * @tc.name: ExecCommand
346  * @tc.desc: Verify the "cem dump -e" command.
347  */
348 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_1200, Function | MediumTest | Level1)
349 {
350     char *argv[] = {
351         (char *)toolName_.c_str(),
352         (char *)cmd_.c_str(),
353         (char *)"-e",
354         (char *)"",
355     };
356     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
357 
358     CommonEventCommand cmd(argc, argv);
359 
360     // set the mock objects
361     SetMockObjects(cmd);
362 
363     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: option 'e' requires a value.\n", HELP_MSG_DUMP));
364 }
365 
366 /**
367  * @tc.number: Cem_Command_Dump_1300
368  * @tc.name: ExecCommand
369  * @tc.desc: Verify the "cem dump -e <name>" command with no subscriber.
370  */
371 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_1300, Function | MediumTest | Level1)
372 {
373     char *argv[] = {
374         (char *)toolName_.c_str(),
375         (char *)cmd_.c_str(),
376         (char *)"-e",
377         (char *)STRING_EVENT.c_str(),
378         (char *)"",
379     };
380     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
381 
382     CommonEventCommand cmd(argc, argv);
383 
384     // set the mock objects
385     SetMockObjects(cmd);
386 
387     EXPECT_EQ(cmd.ExecCommand(), "");
388 }
389 
390 /**
391  * @tc.number: Cem_Command_Dump_1400
392  * @tc.name: ExecCommand
393  * @tc.desc: Verify the "cem dump -e <event>" command with a subscriber.
394  */
395 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_1400, Function | MediumTest | Level1)
396 {
397     /* Subscribe */
398 
399     // make matching skills
400     MatchingSkills matchingSkills;
401     matchingSkills.AddEvent(STRING_EVENT);
402 
403     // make subscribe info
404     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
405 
406     // make a subscriber object
407     auto subscriberTestPtr = std::make_shared<CommonEventSubscriberTest>(subscribeInfo);
408     // subscribe a common event
409     CommonEventManager::SubscribeCommonEvent(subscriberTestPtr);
410 
411     char *argv[] = {
412         (char *)toolName_.c_str(),
413         (char *)cmd_.c_str(),
414         (char *)"-e",
415         (char *)STRING_EVENT.c_str(),
416         (char *)"",
417     };
418     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
419 
420     CommonEventCommand cmd(argc, argv);
421 
422     // set the mock objects
423     SetMockObjects(cmd);
424     EXPECT_EQ(cmd.ExecCommand(), STRING_EVENT + "\n");
425 }
426 
427 /**
428  * @tc.number: Cem_Command_Dump_1500
429  * @tc.name: ExecCommand
430  * @tc.desc: Verify the "cem dump -u" command.
431  */
432 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_1500, Function | MediumTest | Level1)
433 {
434     char *argv[] = {
435         (char *)toolName_.c_str(),
436         (char *)cmd_.c_str(),
437         (char *)"-u",
438         (char *)"",
439     };
440     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
441     CommonEventCommand cmd(argc, argv);
442     // set the mock objects
443     SetMockObjects(cmd);
444     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: option 'u' requires a value.\n", HELP_MSG_DUMP));
445 }
446 
447 /**
448  * @tc.number: Cem_Command_Dump_1600
449  * @tc.name: ExecCommand
450  * @tc.desc: Verify the "cem dump -u <user-id>" command with no subscriber.
451  */
452 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_1600, Function | MediumTest | Level1)
453 {
454     char *argv[] = {
455         (char *)toolName_.c_str(),
456         (char *)cmd_.c_str(),
457         (char *)"-u",
458         (char *)"100",
459         (char *)"",
460     };
461     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
462     CommonEventCommand cmd(argc, argv);
463     // set the mock objects
464     SetMockObjects(cmd);
465     EXPECT_EQ(cmd.ExecCommand(), "");
466 }
467 
468 /**
469  * @tc.number: Cem_Command_Dump_1700
470  * @tc.name: ExecCommand
471  * @tc.desc: Verify the "cem dump -e <name> -u <user-id>" command with no subscriber.
472  */
473 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_1700, Function | MediumTest | Level1)
474 {
475     char *argv[] = {
476         (char *)toolName_.c_str(),
477         (char *)cmd_.c_str(),
478         (char *)"-e",
479         (char *)STRING_EVENT.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_1800
493  * @tc.name: ExecCommand
494  * @tc.desc: Verify the "cem dump -e <name> -u <user-id>" command with a subscriber.
495  */
496 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_1800, Function | MediumTest | Level1)
497 {
498     // make matching skills
499     MatchingSkills matchingSkills;
500     matchingSkills.AddEvent(STRING_EVENT);
501     // make subscribe info
502     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
503     // make a subscriber object
504     auto subscriberTestPtr = std::make_shared<CommonEventSubscriberTest>(subscribeInfo);
505     // subscribe a common event
506     CommonEventManager::SubscribeCommonEvent(subscriberTestPtr);
507     char *argv[] = {
508         (char *)toolName_.c_str(),
509         (char *)cmd_.c_str(),
510         (char *)"-e",
511         (char *)STRING_EVENT.c_str(),
512         (char *)"-u",
513         (char *)"100",
514         (char *)"",
515     };
516     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
517     CommonEventCommand cmd(argc, argv);
518     // set the mock objects
519     SetMockObjects(cmd);
520     EXPECT_EQ(cmd.ExecCommand(), STRING_EVENT + "\n");
521 }
522 
523 /**
524  * @tc.number: Cem_Command_Dump_1900
525  * @tc.name: ExecCommand
526  * @tc.desc: Verify the "cem dump -p" command.
527  * @tc.require: issueI5UINZ
528  */
529 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_1900, Function | MediumTest | Level1)
530 {
531     char *argv[] = {
532         (char *)toolName_.c_str(),
533         (char *)cmd_.c_str(),
534         (char *)"-e",
535         (char *)STRING_EVENT.c_str(),
536         (char *)"-u",
537         (char *)"100",
538         (char *)STRING_EVENT.c_str(),
539         (char *)"-p",
540         (char *)"",
541     };
542     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
543     CommonEventCommand cmd(argc, argv);
544     // set the mock objects
545     SetMockObjects(cmd);
546     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: unknown option.\n", HELP_MSG_DUMP));
547 }
548 
549 /**
550  * @tc.number: Cem_Command_Dump_2000
551  * @tc.name: ExecCommand
552  * @tc.desc: Verify the "cem dump -p" command.
553  * @tc.require: issueI5UINZ
554  */
555 HWTEST_F(CemCommandDumpTest, Cem_Command_Dump_2000, Function | MediumTest | Level1)
556 {
557       /* Subscribe */
558 
559     // make matching skills
560     MatchingSkills matchingSkills;
561     matchingSkills.AddEvent(STRING_EVENT);
562 
563     // make subscribe info
564     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
565 
566     // make a subscriber object
567     auto subscriberTestPtr = std::make_shared<CommonEventSubscriberTest>(subscribeInfo);
568     // subscribe a common event
569     CommonEventManager::SubscribeCommonEvent(subscriberTestPtr);
570     char *argv[] = {
571         (char *)toolName_.c_str(),
572         (char *)cmd_.c_str(),
573         (char *)"-p",
574         (char *)"11",
575         (char *)"",
576     };
577     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
578     CommonEventCommand cmd(argc, argv);
579     // set the mock objects
580     SetMockObjects(cmd);
581     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: option 'p' requires a value.\n", HELP_MSG_DUMP));
582 }
583 }  // namespace
584