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