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 protected public
19 #include "ability_command.h"
20 #undef protected
21 #include "mock_ability_manager_stub.h"
22 #define private public
23 #include "ability_manager_client.h"
24 #undef private
25 #include "ability_manager_interface.h"
26 
27 using namespace testing::ext;
28 using namespace OHOS;
29 using namespace OHOS::AAFwk;
30 
31 namespace {
32 const std::string STRING_USER_ID = "100";
33 const std::string UNKNOWN_OPTION_MSG = "fail: unknown option.\n";
34 }  // namespace
35 
36 class AaCommandDumpsysTest : public ::testing::Test {
37 public:
38     static void SetUpTestCase();
39     static void TearDownTestCase();
40     void SetUp() override;
41     void TearDown() override;
42 
43     void MakeMockObjects() const;
44 
45     std::string cmd_ = "dump";
46 };
47 
SetUpTestCase()48 void AaCommandDumpsysTest::SetUpTestCase()
49 {}
50 
TearDownTestCase()51 void AaCommandDumpsysTest::TearDownTestCase()
52 {}
53 
SetUp()54 void AaCommandDumpsysTest::SetUp()
55 {
56     // reset optind to 0
57     optind = 0;
58 
59     // make mock objects
60     MakeMockObjects();
61 }
62 
TearDown()63 void AaCommandDumpsysTest::TearDown()
64 {}
65 
MakeMockObjects() const66 void AaCommandDumpsysTest::MakeMockObjects() const
67 {
68     // mock a stub
69     auto managerStubPtr = sptr<IAbilityManager>(new MockAbilityManagerStub());
70 
71     // set the mock stub
72     auto managerClientPtr = AbilityManagerClient::GetInstance();
73     managerClientPtr->proxy_ = managerStubPtr;
74 }
75 
76 /**
77  * @tc.number: Aa_Command_Dumpsys_0100
78  * @tc.name: ExecCommand
79  * @tc.desc: Verify the "aa dump" command.
80  */
81 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_0100, Function | MediumTest | Level1)
82 {
83     char* argv[] = {
84         (char*)TOOL_NAME.c_str(),
85         (char*)cmd_.c_str(),
86         (char*)"",
87     };
88     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
89 
90     AbilityManagerShellCommand cmd(argc, argv);
91     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_DUMPSYS);
92 }
93 
94 /**
95  * @tc.number: Aa_Command_Dumpsys_0200
96  * @tc.name: ExecCommand
97  * @tc.desc: Verify the "aa dump xxx" command.
98  */
99 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_0200, Function | MediumTest | Level1)
100 {
101     char* argv[] = {
102         (char*)TOOL_NAME.c_str(),
103         (char*)cmd_.c_str(),
104         (char*)"xxx",
105         (char*)"",
106     };
107     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
108 
109     AbilityManagerShellCommand cmd(argc, argv);
110     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_DUMPSYS);
111 }
112 
113 /**
114  * @tc.number: Aa_Command_Dumpsys_0300
115  * @tc.name: ExecCommand
116  * @tc.desc: Verify the "aa dump -x" command.
117  */
118 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_0300, Function | MediumTest | Level1)
119 {
120     char* argv[] = {
121         (char*)TOOL_NAME.c_str(),
122         (char*)cmd_.c_str(),
123         (char*)"-x",
124         (char*)"",
125     };
126     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
127 
128     AbilityManagerShellCommand cmd(argc, argv);
129     EXPECT_EQ(cmd.ExecCommand(), UNKNOWN_OPTION_MSG + HELP_MSG_DUMPSYS);
130 }
131 
132 /**
133  * @tc.number: Aa_Command_Dumpsys_0400
134  * @tc.name: ExecCommand
135  * @tc.desc: Verify the "aa dump -xxx" command.
136  */
137 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_0400, Function | MediumTest | Level1)
138 {
139     char* argv[] = {
140         (char*)TOOL_NAME.c_str(),
141         (char*)cmd_.c_str(),
142         (char*)"-xxx",
143         (char*)"",
144     };
145     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
146 
147     AbilityManagerShellCommand cmd(argc, argv);
148     EXPECT_EQ(cmd.ExecCommand(), UNKNOWN_OPTION_MSG + HELP_MSG_DUMPSYS);
149 }
150 
151 /**
152  * @tc.number: Aa_Command_Dumpsys_0500
153  * @tc.name: ExecCommand
154  * @tc.desc: Verify the "aa dump --x" command.
155  */
156 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_0500, Function | MediumTest | Level1)
157 {
158     char* argv[] = {
159         (char*)TOOL_NAME.c_str(),
160         (char*)cmd_.c_str(),
161         (char*)"--x",
162         (char*)"",
163     };
164     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
165 
166     AbilityManagerShellCommand cmd(argc, argv);
167     EXPECT_EQ(cmd.ExecCommand(), UNKNOWN_OPTION_MSG + HELP_MSG_DUMPSYS);
168 }
169 
170 /**
171  * @tc.number: Aa_Command_Dumpsys_0600
172  * @tc.name: ExecCommand
173  * @tc.desc: Verify the "aa dump --xxx" command.
174  */
175 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_0600, Function | MediumTest | Level1)
176 {
177     char* argv[] = {
178         (char*)TOOL_NAME.c_str(),
179         (char*)cmd_.c_str(),
180         (char*)"--xxx",
181         (char*)"",
182     };
183     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
184 
185     AbilityManagerShellCommand cmd(argc, argv);
186     EXPECT_EQ(cmd.ExecCommand(), UNKNOWN_OPTION_MSG + HELP_MSG_DUMPSYS);
187 }
188 
189 /**
190  * @tc.number: Aa_Command_Dumpsys_0700
191  * @tc.name: ExecCommand
192  * @tc.desc: Verify the "aa dump -h" command.
193  */
194 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_0700, Function | MediumTest | Level1)
195 {
196     char* argv[] = {
197         (char*)TOOL_NAME.c_str(),
198         (char*)cmd_.c_str(),
199         (char*)"-h",
200         (char*)"",
201     };
202     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
203 
204     AbilityManagerShellCommand cmd(argc, argv);
205     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_DUMPSYS);
206 }
207 
208 /**
209  * @tc.number: Aa_Command_Dumpsys_0800
210  * @tc.name: ExecCommand
211  * @tc.desc: Verify the "aa dump --help" command.
212  */
213 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_0800, Function | MediumTest | Level1)
214 {
215     char* argv[] = {
216         (char*)TOOL_NAME.c_str(),
217         (char*)cmd_.c_str(),
218         (char*)"--help",
219         (char*)"",
220     };
221     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
222 
223     AbilityManagerShellCommand cmd(argc, argv);
224     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_DUMPSYS);
225 }
226 
227 /**
228  * @tc.number: Aa_Command_Dumpsys_0900
229  * @tc.name: ExecCommand
230  * @tc.desc: Verify the "aa dump -a" command.
231  */
232 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_0900, Function | MediumTest | Level1)
233 {
234     char* argv[] = {
235         (char*)TOOL_NAME.c_str(),
236         (char*)cmd_.c_str(),
237         (char*)"-a",
238         (char*)"",
239     };
240     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
241 
242     AbilityManagerShellCommand cmd(argc, argv);
243     EXPECT_EQ(cmd.ExecCommand(), "");
244 }
245 
246 /**
247  * @tc.number: Aa_Command_Dumpsys_1000
248  * @tc.name: ExecCommand
249  * @tc.desc: Verify the "aa dump --all" command.
250  */
251 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_1000, Function | MediumTest | Level1)
252 {
253     char* argv[] = {
254         (char*)TOOL_NAME.c_str(),
255         (char*)cmd_.c_str(),
256         (char*)"--all",
257         (char*)"",
258     };
259     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
260 
261     AbilityManagerShellCommand cmd(argc, argv);
262     EXPECT_EQ(cmd.ExecCommand(), "");
263 }
264 
265 /**
266  * @tc.number: Aa_Command_Dumpsys_1100
267  * @tc.name: ExecCommand
268  * @tc.desc: Verify the "aa dump -l" command.
269  */
270 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_1100, Function | MediumTest | Level1)
271 {
272     char* argv[] = {
273         (char*)TOOL_NAME.c_str(),
274         (char*)cmd_.c_str(),
275         (char*)"-l",
276         (char*)"",
277     };
278     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
279 
280     AbilityManagerShellCommand cmd(argc, argv);
281     EXPECT_EQ(cmd.ExecCommand(), "");
282 }
283 
284 /**
285  * @tc.number: Aa_Command_Dumpsys_1200
286  * @tc.name: ExecCommand
287  * @tc.desc: Verify the "aa dump --mission-list" command.
288  */
289 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_1200, Function | MediumTest | Level1)
290 {
291     char* argv[] = {
292         (char*)TOOL_NAME.c_str(),
293         (char*)cmd_.c_str(),
294         (char*)"--mission-list",
295         (char*)"",
296     };
297     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
298 
299     AbilityManagerShellCommand cmd(argc, argv);
300     EXPECT_EQ(cmd.ExecCommand(), "");
301 }
302 
303 /**
304  * @tc.number: Aa_Command_Dumpsys_1300
305  * @tc.name: ExecCommand
306  * @tc.desc: Verify the "aa dump -i" command.
307  */
308 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_1300, Function | MediumTest | Level1)
309 {
310     char* argv[] = {
311         (char*)TOOL_NAME.c_str(),
312         (char*)cmd_.c_str(),
313         (char*)"-i",
314         (char*)"",
315     };
316     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
317 
318     AbilityManagerShellCommand cmd(argc, argv);
319     EXPECT_EQ(cmd.ExecCommand(), UNKNOWN_OPTION_MSG + HELP_MSG_DUMPSYS);
320 }
321 
322 /**
323  * @tc.number: Aa_Command_Dumpsys_1400
324  * @tc.name: ExecCommand
325  * @tc.desc: Verify the "aa dump --ability" command.
326  */
327 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_1400, Function | MediumTest | Level1)
328 {
329     char* argv[] = {
330         (char*)TOOL_NAME.c_str(),
331         (char*)cmd_.c_str(),
332         (char*)"--ability",
333         (char*)"",
334     };
335     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
336 
337     AbilityManagerShellCommand cmd(argc, argv);
338     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_DUMPSYS);
339 }
340 
341 /**
342  * @tc.number: Aa_Command_Dumpsys_1500
343  * @tc.name: ExecCommand
344  * @tc.desc: Verify the "aa dump -e" command.
345  */
346 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_1500, Function | MediumTest | Level1)
347 {
348     char* argv[] = {
349         (char*)TOOL_NAME.c_str(),
350         (char*)cmd_.c_str(),
351         (char*)"-e",
352         (char*)"",
353     };
354     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
355 
356     AbilityManagerShellCommand cmd(argc, argv);
357     EXPECT_EQ(cmd.ExecCommand(), "");
358 }
359 
360 /**
361  * @tc.number: Aa_Command_Dumpsys_1600
362  * @tc.name: ExecCommand
363  * @tc.desc: Verify the "aa dump --extension" command.
364  */
365 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_1600, Function | MediumTest | Level1)
366 {
367     char* argv[] = {
368         (char*)TOOL_NAME.c_str(),
369         (char*)cmd_.c_str(),
370         (char*)"--extension",
371         (char*)"",
372     };
373     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
374 
375     AbilityManagerShellCommand cmd(argc, argv);
376     EXPECT_EQ(cmd.ExecCommand(), "");
377 }
378 
379 /**
380  * @tc.number: Aa_Command_Dumpsys_1700
381  * @tc.name: ExecCommand
382  * @tc.desc: Verify the "aa dump -p" command.
383  */
384 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_1700, Function | MediumTest | Level1)
385 {
386     char* argv[] = {
387         (char*)TOOL_NAME.c_str(),
388         (char*)cmd_.c_str(),
389         (char*)"-p",
390         (char*)"",
391     };
392     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
393 
394     AbilityManagerShellCommand cmd(argc, argv);
395     EXPECT_EQ(cmd.ExecCommand(), "");
396 }
397 
398 /**
399  * @tc.number: Aa_Command_Dumpsys_1800
400  * @tc.name: ExecCommand
401  * @tc.desc: Verify the "aa dump --pending" command.
402  */
403 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_1800, Function | MediumTest | Level1)
404 {
405     char* argv[] = {
406         (char*)TOOL_NAME.c_str(),
407         (char*)cmd_.c_str(),
408         (char*)"--pending",
409         (char*)"",
410     };
411     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
412 
413     AbilityManagerShellCommand cmd(argc, argv);
414     EXPECT_EQ(cmd.ExecCommand(), "");
415 }
416 
417 /**
418  * @tc.number: Aa_Command_Dumpsys_1900
419  * @tc.name: ExecCommand
420  * @tc.desc: Verify the "aa dump -r" command.
421  */
422 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_1900, Function | MediumTest | Level1)
423 {
424     char* argv[] = {
425         (char*)TOOL_NAME.c_str(),
426         (char*)cmd_.c_str(),
427         (char*)"-r",
428         (char*)"",
429     };
430     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
431 
432     AbilityManagerShellCommand cmd(argc, argv);
433     EXPECT_EQ(cmd.ExecCommand(), "");
434 }
435 
436 /**
437  * @tc.number: Aa_Command_Dumpsys_2000
438  * @tc.name: ExecCommand
439  * @tc.desc: Verify the "aa dump --process" command.
440  */
441 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_2000, Function | MediumTest | Level1)
442 {
443     char* argv[] = {
444         (char*)TOOL_NAME.c_str(),
445         (char*)cmd_.c_str(),
446         (char*)"--process",
447         (char*)"",
448     };
449     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
450 
451     AbilityManagerShellCommand cmd(argc, argv);
452     EXPECT_EQ(cmd.ExecCommand(), "");
453 }
454 
455 /**
456  * @tc.number: Aa_Command_Dumpsys_2100
457  * @tc.name: ExecCommand
458  * @tc.desc: Verify the "aa dump -d" command.
459  */
460 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_2100, Function | MediumTest | Level1)
461 {
462     char* argv[] = {
463         (char*)TOOL_NAME.c_str(),
464         (char*)cmd_.c_str(),
465         (char*)"-d",
466         (char*)"",
467     };
468     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
469 
470     AbilityManagerShellCommand cmd(argc, argv);
471     EXPECT_EQ(cmd.ExecCommand(), "");
472 }
473 
474 /**
475  * @tc.number: Aa_Command_Dumpsys_2200
476  * @tc.name: ExecCommand
477  * @tc.desc: Verify the "aa dump --data" command.
478  */
479 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_2200, Function | MediumTest | Level1)
480 {
481     char* argv[] = {
482         (char*)TOOL_NAME.c_str(),
483         (char*)cmd_.c_str(),
484         (char*)"--data",
485         (char*)"",
486     };
487     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
488 
489     AbilityManagerShellCommand cmd(argc, argv);
490     EXPECT_EQ(cmd.ExecCommand(), "");
491 }
492 
493 /**
494  * @tc.number: Aa_Command_Dumpsys_2300
495  * @tc.name: ExecCommand
496  * @tc.desc: Verify the "aa dump -u" command.
497  */
498 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_2300, Function | MediumTest | Level1)
499 {
500     char* argv[] = {
501         (char*)TOOL_NAME.c_str(),
502         (char*)cmd_.c_str(),
503         (char*)"-u",
504         (char*)"",
505     };
506     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
507 
508     AbilityManagerShellCommand cmd(argc, argv);
509     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_DUMPSYS);
510 }
511 
512 /**
513  * @tc.number: Aa_Command_Dumpsys_2400
514  * @tc.name: ExecCommand
515  * @tc.desc: Verify the "aa dump --userId" command.
516  */
517 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_2400, Function | MediumTest | Level1)
518 {
519     char* argv[] = {
520         (char*)TOOL_NAME.c_str(),
521         (char*)cmd_.c_str(),
522         (char*)"--userId",
523         (char*)"",
524     };
525     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
526 
527     AbilityManagerShellCommand cmd(argc, argv);
528     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_DUMPSYS);
529 }
530 
531 /**
532  * @tc.number: Aa_Command_Dumpsys_2500
533  * @tc.name: ExecCommand
534  * @tc.desc: Verify the "aa dump -c" command.
535  */
536 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_2500, Function | MediumTest | Level1)
537 {
538     char* argv[] = {
539         (char*)TOOL_NAME.c_str(),
540         (char*)cmd_.c_str(),
541         (char*)"-c",
542         (char*)"",
543     };
544     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
545 
546     AbilityManagerShellCommand cmd(argc, argv);
547     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_DUMPSYS);
548 }
549 
550 /**
551  * @tc.number: Aa_Command_Dumpsys_2600
552  * @tc.name: ExecCommand
553  * @tc.desc: Verify the "aa dump --client" command.
554  */
555 HWTEST_F(AaCommandDumpsysTest, Aa_Command_Dumpsys_2600, Function | MediumTest | Level1)
556 {
557     char* argv[] = {
558         (char*)TOOL_NAME.c_str(),
559         (char*)cmd_.c_str(),
560         (char*)"--client",
561         (char*)"",
562     };
563     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
564 
565     AbilityManagerShellCommand cmd(argc, argv);
566     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_DUMPSYS);
567 }
568