• 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 
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 const std::string STRING_CODE = "1024";
34 const std::string STRING_DATA = "data";
35 }  // namespace
36 
Concatenate(const std::string & first,const std::string & second)37 static std::string Concatenate(const std::string &first,  const std::string &second)
38 {
39     return first + second;
40 }
41 
42 class CemCommandPublishTest : public testing::Test {
43 public:
44     static void SetUpTestCase();
45     static void TearDownTestCase();
46     void SetUp() override;
47     void TearDown() override;
48 
49     void MakeMockObjects();
50     void SetMockObjects(const CommonEventCommand &cmd) const;
51 
52     std::string cmd_ = "publish";
53     std::string toolName_ = TOOL_NAME;
54     sptr<ICommonEvent> proxyPtr_;
55 };
56 
SetUpTestCase()57 void CemCommandPublishTest::SetUpTestCase()
58 {}
59 
TearDownTestCase()60 void CemCommandPublishTest::TearDownTestCase()
61 {}
62 
SetUp()63 void CemCommandPublishTest::SetUp()
64 {
65     // reset optind to 0
66     optind = 0;
67 
68     // make mock objects
69     MakeMockObjects();
70 }
71 
TearDown()72 void CemCommandPublishTest::TearDown()
73 {}
74 
MakeMockObjects()75 void CemCommandPublishTest::MakeMockObjects()
76 {
77     // mock a stub
78     auto stubPtr = sptr<IRemoteObject>(new MockCommonEventStub());
79 
80     // mock a proxy
81     proxyPtr_ = iface_cast<ICommonEvent>(stubPtr);
82 
83     // set the mock proxy
84     auto commonEventPtr = DelayedSingleton<CommonEvent>::GetInstance();
85     commonEventPtr->isProxyValid_ = true;
86     commonEventPtr->commonEventProxy_ = proxyPtr_;
87 }
88 
SetMockObjects(const CommonEventCommand & cmd) const89 void CemCommandPublishTest::SetMockObjects(const CommonEventCommand &cmd) const
90 {}
91 
92 /**
93  * @tc.number: Cem_Command_Publish_0100
94  * @tc.name: ExecCommand
95  * @tc.desc: Verify the "cem publish" command.
96  */
97 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_0100, Function | MediumTest | Level1)
98 {
99     char *argv[] = {
100         (char *)toolName_.c_str(),
101         (char *)cmd_.c_str(),
102         (char *)"",
103     };
104     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
105 
106     CommonEventCommand cmd(argc, argv);
107     EXPECT_EQ(cmd.ExecCommand(), Concatenate(HELP_MSG_NO_OPTION, HELP_MSG_PUBLISH));
108 }
109 
110 /**
111  * @tc.number: Cem_Command_Publish_0200
112  * @tc.name: ExecCommand
113  * @tc.desc: Verify the "cem publish xxx" command.
114  */
115 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_0200, Function | MediumTest | Level1)
116 {
117     char *argv[] = {
118         (char *)toolName_.c_str(),
119         (char *)cmd_.c_str(),
120         (char *)"xxx",
121         (char *)"",
122     };
123     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
124 
125     CommonEventCommand cmd(argc, argv);
126     EXPECT_EQ(cmd.ExecCommand(), Concatenate(HELP_MSG_NO_OPTION, HELP_MSG_PUBLISH));
127 }
128 
129 /**
130  * @tc.number: Cem_Command_Publish_0300
131  * @tc.name: ExecCommand
132  * @tc.desc: Verify the "cem publish -x" command.
133  */
134 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_0300, Function | MediumTest | Level1)
135 {
136     char *argv[] = {
137         (char *)toolName_.c_str(),
138         (char *)cmd_.c_str(),
139         (char *)"-x",
140         (char *)"",
141     };
142     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
143 
144     CommonEventCommand cmd(argc, argv);
145     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: unknown option.\n", HELP_MSG_PUBLISH));
146 }
147 
148 /**
149  * @tc.number: Cem_Command_Publish_0400
150  * @tc.name: ExecCommand
151  * @tc.desc: Verify the "cem publish -xxx" command.
152  */
153 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_0400, Function | MediumTest | Level1)
154 {
155     char *argv[] = {
156         (char *)toolName_.c_str(),
157         (char *)cmd_.c_str(),
158         (char *)"-xxx",
159         (char *)"",
160     };
161     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
162 
163     CommonEventCommand cmd(argc, argv);
164     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: unknown option.\n", HELP_MSG_PUBLISH));
165 }
166 
167 /**
168  * @tc.number: Cem_Command_Publish_0500
169  * @tc.name: ExecCommand
170  * @tc.desc: Verify the "cem publish --x" command.
171  */
172 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_0500, Function | MediumTest | Level1)
173 {
174     char *argv[] = {
175         (char *)toolName_.c_str(),
176         (char *)cmd_.c_str(),
177         (char *)"--x",
178         (char *)"",
179     };
180     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
181 
182     CommonEventCommand cmd(argc, argv);
183     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: unknown option.\n", HELP_MSG_PUBLISH));
184 }
185 
186 /**
187  * @tc.number: Cem_Command_Publish_0600
188  * @tc.name: ExecCommand
189  * @tc.desc: Verify the "cem publish --xxx" command.
190  */
191 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_0600, Function | MediumTest | Level1)
192 {
193     char *argv[] = {
194         (char *)toolName_.c_str(),
195         (char *)cmd_.c_str(),
196         (char *)"--xxx",
197         (char *)"",
198     };
199     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
200 
201     CommonEventCommand cmd(argc, argv);
202     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: unknown option.\n", HELP_MSG_PUBLISH));
203 }
204 
205 /**
206  * @tc.number: Cem_Command_Publish_0700
207  * @tc.name: ExecCommand
208  * @tc.desc: Verify the "cem publish -h" command.
209  */
210 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_0700, Function | MediumTest | Level1)
211 {
212     char *argv[] = {
213         (char *)toolName_.c_str(),
214         (char *)cmd_.c_str(),
215         (char *)"-h",
216         (char *)"",
217     };
218     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
219 
220     CommonEventCommand cmd(argc, argv);
221     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_PUBLISH);
222 }
223 
224 /**
225  * @tc.number: Cem_Command_Publish_0800
226  * @tc.name: ExecCommand
227  * @tc.desc: Verify the "cem publish --help" command.
228  */
229 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_0800, Function | MediumTest | Level1)
230 {
231     char *argv[] = {
232         (char *)toolName_.c_str(),
233         (char *)cmd_.c_str(),
234         (char *)"--help",
235         (char *)"",
236     };
237     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
238 
239     CommonEventCommand cmd(argc, argv);
240     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_PUBLISH);
241 }
242 
243 /**
244  * @tc.number: Cem_Command_Publish_0900
245  * @tc.name: ExecCommand
246  * @tc.desc: Verify the "cem publish -e" command.
247  */
248 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_0900, Function | MediumTest | Level1)
249 {
250     char *argv[] = {
251         (char *)toolName_.c_str(),
252         (char *)cmd_.c_str(),
253         (char *)"-e",
254         (char *)"",
255     };
256     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
257 
258     CommonEventCommand cmd(argc, argv);
259     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: option 'e' requires a value.\n", HELP_MSG_PUBLISH));
260 }
261 
262 /**
263  * @tc.number: Cem_Command_Publish_1000
264  * @tc.name: ExecCommand
265  * @tc.desc: Verify the "cem publish -e <name>" command.
266  */
267 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_1000, Function | MediumTest | Level1)
268 {
269     char *argv[] = {
270         (char *)toolName_.c_str(),
271         (char *)cmd_.c_str(),
272         (char *)"-e",
273         (char *)STRING_EVENT.c_str(),
274         (char *)"",
275     };
276     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
277 
278     CommonEventCommand cmd(argc, argv);
279     EXPECT_EQ(cmd.ExecCommand(), STRING_PUBLISH_COMMON_EVENT_OK);
280 }
281 
282 /**
283  * @tc.number: Cem_Command_Publish_1100
284  * @tc.name: ExecCommand
285  * @tc.desc: Verify the "cem publish -c" command.
286  */
287 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_1100, Function | MediumTest | Level1)
288 {
289     char *argv[] = {
290         (char *)toolName_.c_str(),
291         (char *)cmd_.c_str(),
292         (char *)"-c",
293         (char *)"",
294     };
295     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
296 
297     CommonEventCommand cmd(argc, argv);
298     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: option 'c' requires a value.\n", HELP_MSG_PUBLISH));
299 }
300 
301 /**
302  * @tc.number: Cem_Command_Publish_1200
303  * @tc.name: ExecCommand
304  * @tc.desc: Verify the "cem publish -e <name> -c" command.
305  */
306 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_1200, Function | MediumTest | Level1)
307 {
308     char *argv[] = {
309         (char *)toolName_.c_str(),
310         (char *)cmd_.c_str(),
311         (char *)"-e",
312         (char *)STRING_EVENT.c_str(),
313         (char *)"-c",
314         (char *)"",
315     };
316     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
317 
318     CommonEventCommand cmd(argc, argv);
319     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: option 'c' requires a value.\n", HELP_MSG_PUBLISH));
320 }
321 
322 /**
323  * @tc.number: Cem_Command_Publish_1300
324  * @tc.name: ExecCommand
325  * @tc.desc: Verify the "cem publish -c <code>" command.
326  */
327 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_1300, Function | MediumTest | Level1)
328 {
329     char *argv[] = {
330         (char *)toolName_.c_str(),
331         (char *)cmd_.c_str(),
332         (char *)"-c",
333         (char *)STRING_CODE.c_str(),
334         (char *)"",
335     };
336     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
337 
338     CommonEventCommand cmd(argc, argv);
339     EXPECT_EQ(cmd.ExecCommand(), Concatenate(HELP_MSG_NO_EVENT_OPTION, HELP_MSG_PUBLISH));
340 }
341 
342 /**
343  * @tc.number: Cem_Command_Publish_1400
344  * @tc.name: ExecCommand
345  * @tc.desc: Verify the "cem publish -e <name> -c <code>" command.
346  */
347 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_1400, Function | MediumTest | Level1)
348 {
349     char *argv[] = {
350         (char *)toolName_.c_str(),
351         (char *)cmd_.c_str(),
352         (char *)"-e",
353         (char *)STRING_EVENT.c_str(),
354         (char *)"-c",
355         (char *)STRING_CODE.c_str(),
356         (char *)"",
357     };
358     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
359 
360     CommonEventCommand cmd(argc, argv);
361     EXPECT_EQ(cmd.ExecCommand(), STRING_PUBLISH_COMMON_EVENT_OK);
362 }
363 
364 /**
365  * @tc.number: Cem_Command_Publish_1500
366  * @tc.name: ExecCommand
367  * @tc.desc: Verify the "cem publish -d" command.
368  */
369 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_1500, Function | MediumTest | Level1)
370 {
371     char *argv[] = {
372         (char *)toolName_.c_str(),
373         (char *)cmd_.c_str(),
374         (char *)"-d",
375         (char *)"",
376     };
377     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
378 
379     CommonEventCommand cmd(argc, argv);
380     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: option 'd' requires a value.\n", HELP_MSG_PUBLISH));
381 }
382 
383 /**
384  * @tc.number: Cem_Command_Publish_1600
385  * @tc.name: ExecCommand
386  * @tc.desc: Verify the "cem publish -e <name> -d" command.
387  */
388 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_1600, Function | MediumTest | Level1)
389 {
390     char *argv[] = {
391         (char *)toolName_.c_str(),
392         (char *)cmd_.c_str(),
393         (char *)"-e",
394         (char *)STRING_EVENT.c_str(),
395         (char *)"-d",
396         (char *)"",
397     };
398     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
399 
400     CommonEventCommand cmd(argc, argv);
401     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: option 'd' requires a value.\n", HELP_MSG_PUBLISH));
402 }
403 
404 /**
405  * @tc.number: Cem_Command_Publish_1700
406  * @tc.name: ExecCommand
407  * @tc.desc: Verify the "cem publish -d <data>" command.
408  */
409 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_1700, Function | MediumTest | Level1)
410 {
411     char *argv[] = {
412         (char *)toolName_.c_str(),
413         (char *)cmd_.c_str(),
414         (char *)"-d",
415         (char *)STRING_DATA.c_str(),
416         (char *)"",
417     };
418     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
419 
420     CommonEventCommand cmd(argc, argv);
421     EXPECT_EQ(cmd.ExecCommand(), Concatenate(HELP_MSG_NO_EVENT_OPTION, HELP_MSG_PUBLISH));
422 }
423 
424 /**
425  * @tc.number: Cem_Command_Publish_1800
426  * @tc.name: ExecCommand
427  * @tc.desc: Verify the "cem publish -e <name> -d <data>" command.
428  */
429 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_1800, Function | MediumTest | Level1)
430 {
431     char *argv[] = {
432         (char *)toolName_.c_str(),
433         (char *)cmd_.c_str(),
434         (char *)"-e",
435         (char *)STRING_EVENT.c_str(),
436         (char *)"-d",
437         (char *)STRING_DATA.c_str(),
438         (char *)"",
439     };
440     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
441 
442     CommonEventCommand cmd(argc, argv);
443     EXPECT_EQ(cmd.ExecCommand(), STRING_PUBLISH_COMMON_EVENT_OK);
444 }
445 
446 /**
447  * @tc.number: Cem_Command_Publish_1900
448  * @tc.name: ExecCommand
449  * @tc.desc: Verify the "cem publish -e <name> -c <code> -d <data>" command.
450  */
451 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_1900, Function | MediumTest | Level1)
452 {
453     char *argv[] = {
454         (char *)toolName_.c_str(),
455         (char *)cmd_.c_str(),
456         (char *)"-e",
457         (char *)STRING_EVENT.c_str(),
458         (char *)"-c",
459         (char *)STRING_CODE.c_str(),
460         (char *)"-d",
461         (char *)STRING_DATA.c_str(),
462         (char *)"",
463     };
464     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
465 
466     CommonEventCommand cmd(argc, argv);
467     EXPECT_EQ(cmd.ExecCommand(), STRING_PUBLISH_COMMON_EVENT_OK);
468 }
469 
470 /**
471  * @tc.number: Cem_Command_Publish_2000
472  * @tc.name: ExecCommand
473  * @tc.desc: Verify the "cem publish -d" command.
474  */
475 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_2000, Function | MediumTest | Level1)
476 {
477     char *argv[] = {
478         (char *)toolName_.c_str(),
479         (char *)cmd_.c_str(),
480         (char *)"-d",
481         (char *)"",
482     };
483     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
484     CommonEventCommand cmd(argc, argv);
485     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: option 'd' requires a value.\n", HELP_MSG_PUBLISH));
486 }
487 
488 /**
489  * @tc.number: Cem_Command_Publish_2100
490  * @tc.name: ExecCommand
491  * @tc.desc: Verify the "cem publish -e <name> -u <user-id>" command.
492  */
493 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_2100, Function | MediumTest | Level1)
494 {
495     char *argv[] = {
496         (char *)toolName_.c_str(),
497         (char *)cmd_.c_str(),
498         (char *)"-e",
499         (char *)STRING_EVENT.c_str(),
500         (char *)"-u",
501         (char *)"100",
502         (char *)"",
503     };
504     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
505     CommonEventCommand cmd(argc, argv);
506     EXPECT_EQ(cmd.ExecCommand(), STRING_PUBLISH_COMMON_EVENT_OK);
507 }
508 
509 /**
510  * @tc.number: Cem_Command_Publish_2200
511  * @tc.name: ExecCommand
512  * @tc.desc: Verify the "cem publish -e <name> -c <code> -d <data> -u <user-id>" command.
513  */
514 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_2200, Function | MediumTest | Level1)
515 {
516     char *argv[] = {
517         (char *)toolName_.c_str(),
518         (char *)cmd_.c_str(),
519         (char *)"-e",
520         (char *)STRING_EVENT.c_str(),
521         (char *)"-c",
522         (char *)STRING_CODE.c_str(),
523         (char *)"-d",
524         (char *)STRING_DATA.c_str(),
525         (char *)"-u",
526         (char *)"100",
527         (char *)"",
528     };
529     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
530     CommonEventCommand cmd(argc, argv);
531     EXPECT_EQ(cmd.ExecCommand(), STRING_PUBLISH_COMMON_EVENT_OK);
532 }
533 
534 /**
535  * @tc.number: Cem_Command_Publish_2300
536  * @tc.name: ExecCommand
537  * @tc.desc: Verify the "cem publish -s" command.
538  * @tc.require: issueI5UINZ
539  */
540 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_2300, Function | MediumTest | Level1)
541 {
542     char *argv[] = {
543         (char *)toolName_.c_str(),
544         (char *)cmd_.c_str(),
545         (char *)"-e",
546         (char *)STRING_EVENT.c_str(),
547         (char *)"-s",
548         (char *)"",
549     };
550     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
551     CommonEventCommand cmd(argc, argv);
552     EXPECT_EQ(cmd.ExecCommand(), "publish the common event successfully.\n");
553 }
554 
555 /**
556  * @tc.number: Cem_Command_Publish_2400
557  * @tc.name: ExecCommand
558  * @tc.desc: Verify the "cem publish -o" command.
559  * @tc.require: issueI5UINZ
560  */
561 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_2400, Function | MediumTest | Level1)
562 {
563     char *argv[] = {
564         (char *)toolName_.c_str(),
565         (char *)cmd_.c_str(),
566         (char *)"-e",
567         (char *)STRING_EVENT.c_str(),
568         (char *)"-o",
569         (char *)"",
570     };
571     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
572     CommonEventCommand cmd(argc, argv);
573     EXPECT_EQ(cmd.ExecCommand(), "publish the common event successfully.\n");
574 }
575 
576 /**
577  * @tc.number: Cem_Command_Publish_2500
578  * @tc.name: ExecCommand
579  * @tc.desc: Verify the "cem publish -e <name> -u <user-id>" command.
580  * @tc.require: issueI5UINZ
581  */
582 HWTEST_F(CemCommandPublishTest, Cem_Command_Publish_2500, Function | MediumTest | Level1)
583 {
584     char *argv[] = {
585         (char *)toolName_.c_str(),
586         (char *)cmd_.c_str(),
587         (char *)"-e",
588         (char *)STRING_EVENT.c_str(),
589         (char *)"-u",
590         (char *)"",
591     };
592     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
593     CommonEventCommand cmd(argc, argv);
594     EXPECT_EQ(cmd.ExecCommand(), Concatenate("error: option 'u' requires a value.\n", HELP_MSG_PUBLISH));
595 }