• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "notification_shell_command.h"
20 #undef private
21 #include "ans_inner_errors.h"
22 #include "ans_manager_interface.h"
23 #include "mock_ans_manager_stub.h"
24 #include "singleton.h"
25 
26 using namespace testing::ext;
27 using namespace OHOS;
28 using namespace OHOS::Notification;
29 
30 namespace {
31 static char g_dumpHelpMsg[] =
32 "request a option 'A' or 'R' or 'D'\n"
33 "usage: anm dump [<options>]\n"
34 "options list:\n"
35 "  --help, -h                   help menu\n"
36 "  --active,  -A                 list all active notifications\n"
37 "  --recent,  -R                 list recent notifications\n"
38 "  --bundle,  -b  <name>         dump the info filter by the specified bundle name\n"
39 "  --user-id, -u  <userId>       dump the info filter by the specified userId\n";
40 
41 static char g_dumpActiveBound[] =
42 "error: option 'b' requires a value.\n"
43 "usage: anm dump [<options>]\noptions list:\n"
44 "  --help, -h                   help menu\n"
45 "  --active,  -A                 list all active notifications\n"
46 "  --recent,  -R                 list recent notifications\n"
47 "  --bundle,  -b  <name>         dump the info filter by the specified bundle name\n"
48 "  --user-id, -u  <userId>       dump the info filter by the specified userId\n";
49 
50 static char g_dumpActiveUser[] =
51 "error: option 'u' requires a value.\n"
52 "usage: anm dump [<options>]\n"
53 "options list:\n"
54 "  --help, -h                   help menu\n"
55 "  --active,  -A                 list all active notifications\n"
56 "  --recent,  -R                 list recent notifications\n"
57 "  --bundle,  -b  <name>         dump the info filter by the specified bundle name\n"
58 "  --user-id, -u  <userId>       dump the info filter by the specified userId\n";
59 
60 static char g_enableErrorInformation[] =
61 "error: option 'e' requires a value.\nusage: anm setting [<options>]\noptions list:\n"
62 "  --help, -h                   help menu\n"
63 "  --recent-count -c <number>   set the max count of recent notifications keeping in memory\n  --enable-notification"
64 " -e <bundleName:uid:enable> set notification enabled for the bundle, eg: -e com.example:10100:1\n";
65 
66 static char g_enableBundleNameNull[] =
67 "error: setting information error\n"
68 "usage: anm setting [<options>]\n"
69 "options list:\n  --help, -h                   help menu\n"
70 "  --recent-count -c <number>   set the max count of recent notifications keeping in memory\n  --enable-notification"
71 " -e <bundleName:uid:enable> set notification enabled for the bundle, eg: -e com.example:10100:1\n";
72 
73 static char g_enableObjectNull[] =
74 "error: object is null\n"
75 "error: object is null\n"
76 "usage: anm setting [<options>]\n"
77 "options list:\n  --help, -h                   help menu\n"
78 "  --recent-count -c <number>   set the max count of recent notifications keeping in memory\n  --enable-notification"
79 " -e <bundleName:uid:enable> set notification enabled for the bundle, eg: -e com.example:10100:1\n";
80 
81 static char g_bundleName[] = "example";
82 static char g_commandActive[] = "active";
83 static char g_commandRecent[] = "recent";
84 
85 class AnmManagerDumpTest : public testing::Test {
86 public:
87     static void SetUpTestCase();
88     static void TearDownTestCase();
89     void SetUp() override;
90     void TearDown() override;
91 
92     void MakeMockObjects();
93 
94     std::string cmd_ = "dump";
95     std::string enable_ = "setting";
96     std::string toolName_ = "anm";
97     sptr<AnsManagerInterface> proxyPtr_;
98     sptr<MockAnsManagerStub> stubPtr_;
99 };
100 
SetUpTestCase()101 void AnmManagerDumpTest::SetUpTestCase()
102 {}
103 
TearDownTestCase()104 void AnmManagerDumpTest::TearDownTestCase()
105 {}
106 
SetUp()107 void AnmManagerDumpTest::SetUp()
108 {
109     // reset optind to 0
110     optind = 0;
111 
112     // make mock objects
113     MakeMockObjects();
114 }
115 
TearDown()116 void AnmManagerDumpTest::TearDown()
117 {}
118 
MakeMockObjects()119 void AnmManagerDumpTest::MakeMockObjects()
120 {
121     // mock a stub
122     stubPtr_ = new (std::nothrow) MockAnsManagerStub();
123 
124     // mock a proxy
125     proxyPtr_ = iface_cast<AnsManagerInterface>(stubPtr_);
126 
127     // set the mock proxy
128     auto ansNotificationPtr = DelayedSingleton<AnsNotification>::GetInstance();
129     ansNotificationPtr->ansManagerProxy_ = proxyPtr_;
130 }
131 
132 /**
133  * @tc.number: Anm_Command_Dump_0100
134  * @tc.name: ExecCommand
135  * @tc.desc: Verify the "anm dump -h" command.
136  */
137 HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_0100, Function | MediumTest | Level1)
138 {
139     char *argv[] = {
140         (char *)toolName_.c_str(),
141         (char *)cmd_.c_str(),
142         (char *)"-h",
143         (char *)"",
144     };
145     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
146 
147     NotificationShellCommand cmd(argc, argv);
148 
149     EXPECT_EQ(cmd.ExecCommand(), g_dumpHelpMsg);
150 }
151 
152 /**
153  * @tc.number: Anm_Command_Dump_0200
154  * @tc.name: ExecCommand
155  * @tc.desc: Verify the "anm dump -A" command.
156  */
157 HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_0200, Function | MediumTest | Level1)
158 {
159     char *argv[] = {
160         (char *)toolName_.c_str(),
161         (char *)cmd_.c_str(),
162         (char *)"-A",
163         (char *)"",
164     };
165     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
166 
167     NotificationShellCommand cmd(argc, argv);
168 
169     cmd.ExecCommand();
170 
171     EXPECT_EQ(stubPtr_->GetCmd(), g_commandActive);
172 }
173 
174 /**
175  * @tc.number: Anm_Command_Dump_0300
176  * @tc.name: ExecCommand
177  * @tc.desc: Verify the "anm dump -R" command.
178  */
179 HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_0300, Function | MediumTest | Level1)
180 {
181     char *argv[] = {
182         (char *)toolName_.c_str(),
183         (char *)cmd_.c_str(),
184         (char *)"-R",
185         (char *)"",
186     };
187     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
188 
189     NotificationShellCommand cmd(argc, argv);
190 
191     cmd.ExecCommand();
192 
193     EXPECT_EQ(stubPtr_->GetCmd(), g_commandRecent);
194 }
195 
196 /**
197  * @tc.number: Anm_Command_Dump_0400
198  * @tc.name: ExecCommand
199  * @tc.desc: Verify the "anm dump -R -b" command.
200  */
201 HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_0400, Function | MediumTest | Level1)
202 {
203     char *argv[] = {
204         (char *)toolName_.c_str(),
205         (char *)cmd_.c_str(),
206         (char *)"-R",
207         (char *)"-b",
208         (char *)"",
209     };
210     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
211 
212     NotificationShellCommand cmd(argc, argv);
213 
214     EXPECT_EQ(cmd.ExecCommand(), g_dumpActiveBound);
215 }
216 
217 /**
218  * @tc.number: Anm_Command_Dump_0500
219  * @tc.name: ExecCommand
220  * @tc.desc: Verify the "anm dump -A -b example" command.
221  */
222 HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_0500, Function | MediumTest | Level1)
223 {
224     char *argv[] = {
225         (char *)toolName_.c_str(),
226         (char *)cmd_.c_str(),
227         (char *)"-A",
228         (char *)"-b",
229         (char *)"example",
230         (char *)"",
231     };
232     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
233 
234     NotificationShellCommand cmd(argc, argv);
235 
236     cmd.ExecCommand();
237 
238     EXPECT_EQ(stubPtr_->GetCmd(), g_commandActive);
239     EXPECT_EQ(stubPtr_->GetBundle(), g_bundleName);
240 }
241 
242 /**
243  * @tc.number: Anm_Command_Dump_0600
244  * @tc.name: ExecCommand
245  * @tc.desc: Verify the "anm dump -A -b" command.
246  */
247 HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_0600, Function | MediumTest | Level1)
248 {
249     char *argv[] = {
250         (char *)toolName_.c_str(),
251         (char *)cmd_.c_str(),
252         (char *)"-A",
253         (char *)"-b",
254         (char *)"",
255     };
256     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
257 
258     NotificationShellCommand cmd(argc, argv);
259 
260     EXPECT_EQ(cmd.ExecCommand(), g_dumpActiveBound);
261 }
262 
263 /**
264  * @tc.number: Anm_Command_Dump_0700
265  * @tc.name: ExecCommand
266  * @tc.desc: Verify the "anm dump -R -b example" command.
267  */
268 HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_0700, Function | MediumTest | Level1)
269 {
270     char *argv[] = {
271         (char *)toolName_.c_str(),
272         (char *)cmd_.c_str(),
273         (char *)"-R",
274         (char *)"-b",
275         (char *)"example",
276         (char *)"",
277     };
278     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
279 
280     NotificationShellCommand cmd(argc, argv);
281 
282     cmd.ExecCommand();
283 
284     EXPECT_EQ(stubPtr_->GetCmd(), g_commandRecent);
285     EXPECT_EQ(stubPtr_->GetBundle(), g_bundleName);
286 }
287 
288 /**
289  * @tc.number: Anm_Command_Dump_0800
290  * @tc.name: ExecCommand
291  * @tc.desc: Verify the "anm dump -R -u" command.
292  */
293 HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_0800, Function | MediumTest | Level1)
294 {
295     char *argv[] = {
296         (char *)toolName_.c_str(),
297         (char *)cmd_.c_str(),
298         (char *)"-R",
299         (char *)"-u",
300         (char *)"",
301     };
302     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
303 
304     NotificationShellCommand cmd(argc, argv);
305 
306     EXPECT_EQ(cmd.ExecCommand(), g_dumpActiveUser);
307 }
308 
309 /**
310  * @tc.number: Anm_Command_Dump_0900
311  * @tc.name: ExecCommand
312  * @tc.desc: Verify the "anm dump -A -u" command.
313  */
314 HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_0900, Function | MediumTest | Level1)
315 {
316     char *argv[] = {
317         (char *)toolName_.c_str(),
318         (char *)cmd_.c_str(),
319         (char *)"-A",
320         (char *)"-u",
321         (char *)"",
322     };
323     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
324 
325     NotificationShellCommand cmd(argc, argv);
326 
327     EXPECT_EQ(cmd.ExecCommand(), g_dumpActiveUser);
328 }
329 
330 /**
331  * @tc.number: Anm_Command_Dump_1000
332  * @tc.name: ExecCommand
333  * @tc.desc: Verify the "anm dump -A -u 33" command.
334  */
335 HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_1000, Function | MediumTest | Level1)
336 {
337     char *argv[] = {
338         (char *)toolName_.c_str(),
339         (char *)cmd_.c_str(),
340         (char *)"-A",
341         (char *)"-u",
342         (char *)"33",
343         (char *)"",
344     };
345     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
346 
347     NotificationShellCommand cmd(argc, argv);
348 
349     cmd.ExecCommand();
350 
351     EXPECT_EQ(stubPtr_->GetCmd(), g_commandActive);
352     EXPECT_EQ(stubPtr_->GetUserId(), 33);
353 }
354 
355 /**
356  * @tc.number: Anm_Command_Dump_1100
357  * @tc.name: ExecCommand
358  * @tc.desc: Verify the "anm dump -R -u 33" command.
359  */
360 HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_1100, Function | MediumTest | Level1)
361 {
362     char *argv[] = {
363         (char *)toolName_.c_str(),
364         (char *)cmd_.c_str(),
365         (char *)"-R",
366         (char *)"-u",
367         (char *)"33",
368         (char *)"",
369     };
370     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
371 
372     NotificationShellCommand cmd(argc, argv);
373 
374     cmd.ExecCommand();
375 
376     EXPECT_EQ(stubPtr_->GetCmd(), g_commandRecent);
377     EXPECT_EQ(stubPtr_->GetUserId(), 33);
378 }
379 
380 /**
381  * @tc.number: Anm_Command_Dump_1200
382  * @tc.name: RunAsSettingCommand
383  * @tc.desc: test RunAsSettingCommand function
384  */
385 HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_1200, Function | MediumTest | Level1)
386 {
387     char *argv[] = {
388         (char *)toolName_.c_str(),
389         (char *)cmd_.c_str(),
390         (char *)"-h",
391         (char *)"",
392     };
393     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
394 
395     NotificationShellCommand cmd(argc, argv);
396 
397     EXPECT_EQ(cmd.RunAsSettingCommand(), ERR_INVALID_VALUE);
398 }
399 
400 /**
401  * @tc.number: Anm_Command_Dump_1300
402  * @tc.name: RunSetEnableCmd
403  * @tc.desc: test RunSetEnableCmd function
404  */
405 HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_1300, Function | MediumTest | Level1)
406 {
407     char *argv[] = {
408         (char *)toolName_.c_str(),
409         (char *)cmd_.c_str(),
410         (char *)"-h",
411         (char *)"",
412     };
413     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
414 
415     NotificationShellCommand cmd(argc, argv);
416 
417     EXPECT_EQ(cmd.RunSetEnableCmd(), ERR_ANS_SERVICE_NOT_CONNECTED);
418 }
419 
420 /**
421  * @tc.number: Anm_Command_Dump_1400
422  * @tc.name: GetCommandErrorMsg
423  * @tc.desc: test GetCommandErrorMsg function
424  */
425 HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_1400, Function | MediumTest | Level1)
426 {
427     char *argv[] = {
428         (char *)toolName_.c_str(),
429         (char *)cmd_.c_str(),
430         (char *)"-h",
431         (char *)"",
432     };
433     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
434 
435     NotificationShellCommand cmd(argc, argv);
436 
437     EXPECT_EQ(cmd.GetCommandErrorMsg(), "anm_dump: 'dump' is not a valid anm_dump command. See 'anm_dump help'.\n");
438 }
439 
440 /**
441  * @tc.number: Anm_Command_Dump_1500
442  * @tc.name: GetUnknownOptionMsg
443  * @tc.desc: test GetUnknownOptionMsg function
444  */
445 HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_1500, Function | MediumTest | Level1)
446 {
447     char *argv[] = {
448         (char *)toolName_.c_str(),
449         (char *)cmd_.c_str(),
450         (char *)"-h",
451         (char *)"",
452     };
453     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
454 
455     NotificationShellCommand cmd(argc, argv);
456 
457     std::string unknownOption = "aa";
458 
459     EXPECT_EQ(cmd.GetUnknownOptionMsg(unknownOption), "error: unknown option.\n");
460 }
461 
462 /**
463  * @tc.number: Anm_Command_Dump_1600
464  * @tc.name: GetMessageFromCode
465  * @tc.desc: test GetMessageFromCode function
466  */
467 HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_1600, Function | MediumTest | Level1)
468 {
469     char *argv[] = {
470         (char *)toolName_.c_str(),
471         (char *)cmd_.c_str(),
472         (char *)"-h",
473         (char *)"",
474     };
475     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
476 
477     NotificationShellCommand cmd(argc, argv);
478 
479     int32_t code = 11;
480 
481     EXPECT_EQ(cmd.GetMessageFromCode(code), "");
482 }
483 
484 /**
485  * @tc.number: Anm_Command_Dump_1700
486  * @tc.name: RunAsSettingCommand
487  * @tc.desc: Verify the "anm setting -e bundleName:uid:enable" command.
488  */
489 HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_1700, Function | MediumTest | Level1)
490 {
491     char *argv[] = {
492         (char *)toolName_.c_str(),
493         (char *)enable_.c_str(),
494         (char *)"-e",
495         (char *)"dd:ss:aa",
496         (char *)"",
497     };
498     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
499 
500     NotificationShellCommand cmd(argc, argv);
501 
502     EXPECT_EQ(cmd.ExecCommand(), "set notification enabled failed\n");
503 }
504 
505 /**
506  * @tc.number: Anm_Command_Dump_1800
507  * @tc.name: RunAsSettingCommand
508  * @tc.desc: Verify the "anm setting -e" command.
509  */
510 HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_1800, Function | MediumTest | Level1)
511 {
512     char *argv[] = {
513         (char *)toolName_.c_str(),
514         (char *)enable_.c_str(),
515         (char *)"-e",
516         (char *)"",
517     };
518     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
519 
520     NotificationShellCommand cmd(argc, argv);
521 
522     EXPECT_EQ(cmd.ExecCommand(), g_enableErrorInformation);
523 }
524 
525 /**
526  * @tc.number: Anm_Command_Dump_1900
527  * @tc.name: RunAsSettingCommand
528  * @tc.desc: Verify the "anm setting -e bundleName:uid" command.
529  */
530 HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_1900, Function | MediumTest | Level1)
531 {
532     char *argv[] = {
533         (char *)toolName_.c_str(),
534         (char *)enable_.c_str(),
535         (char *)"-e",
536         (char *)"dd:ss",
537         (char *)"",
538     };
539     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
540 
541     NotificationShellCommand cmd(argc, argv);
542 
543     EXPECT_EQ(cmd.ExecCommand(), g_enableBundleNameNull);
544 }
545 
546 /**
547  * @tc.number: Anm_Command_Dump_2000
548  * @tc.name: RunAsSettingCommand
549  * @tc.desc: Verify the "anm setting -e bundleName:uid:enable" command.
550  */
551 HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_2000, Function | MediumTest | Level1)
552 {
553     char *argv[] = {
554         (char *)toolName_.c_str(),
555         (char *)enable_.c_str(),
556         (char *)"-e",
557         (char *)"22",
558         (char *)"",
559     };
560     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
561 
562     NotificationShellCommand cmd(argc, argv);
563 
564     EXPECT_EQ(cmd.RunSetEnableCmd(), cmd.RunAsSettingCommand());
565     EXPECT_EQ(cmd.ExecCommand(), g_enableObjectNull);
566 }
567 
568 /**
569  * @tc.number: Anm_Command_Dump_2100
570  * @tc.name: RunAsSettingCommand
571  * @tc.desc: Verify the "anm setting -e bundleName:uid:enable" command.
572  */
573 HWTEST_F(AnmManagerDumpTest, Anm_Notification_Shell_Dump_2100, Function | MediumTest | Level1)
574 {
575     char *argv[] = {
576         (char *)toolName_.c_str(),
577         (char *)enable_.c_str(),
578         (char *)"-e",
579         (char *)"gg:ss:aa",
580         (char *)"22",
581         (char *)"",
582     };
583     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
584 
585     NotificationShellCommand cmd(argc, argv);
586 
587     EXPECT_EQ(cmd.ExecCommand(), "set notification enabled success\n");
588 }
589 }  // namespace