• 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 protected public
19 #include "ability_tool_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 OHOS {
32 namespace AAFwk {
33 namespace {
34 const std::string ABILITY_TOOL_HELP_MSG =
35     "usage: ability_tool <command> <options>\n"
36     "ability_tool commands list:\n"
37     "  help                        list available commands\n"
38     "  start                       start ability with options\n"
39     "  stop-service                stop service with options\n"
40     "  force-stop                  force stop the process with bundle name\n"
41     "  test                        start the test framework with options\n";
42 
43 const std::string ABILITY_TOOL_HELP_MSG_START =
44     "usage: ability_tool start <options>\n"
45     "ability_tool start options list:\n"
46     "  --help                      list available options\n"
47     "  --device <device-id>        device Id\n"
48     "  --ability <ability-name>    ability name, mandatory\n"
49     "  --bundle <bundle-name>      bundle name, mandatory\n"
50     "  --options <key> <value>     start options, such as windowMode 102\n"
51     "  --flags <flag>              flags in a want\n"
52     "  -C                          cold start\n"
53     "  -D                          start with debug mode\n";
54 
55 const std::string ABILITY_TOOL_HELP_MSG_STOP_SERVICE =
56     "usage: ability_tool stop-service <options>\n"
57     "ability_tool stop-service options list:\n"
58     "  --help                      list available options\n"
59     "  --device <device-id>        device Id\n"
60     "  --ability <ability-name>    ability name, mandatory\n"
61     "  --bundle <bundle-name>      bundle name, mandatory\n";
62 
63 const std::string ABILITY_TOOL_HELP_MSG_FORCE_STOP =
64     "usage: ability_tool force-stop <options>\n"
65     "ability_tool force-stop options list:\n"
66     "  --help                      list available options\n"
67     "  <bundle-name>               bundle name, mandatory\n";
68 
69 const std::string ABILITY_TOOL_HELP_MSG_TEST =
70     "usage: ability_tool test <options>\n"
71     "ability_tool test options list:\n"
72     "  --help                              list available options\n"
73     "  --bundle <bundle-name>              bundle name, mandatory\n"
74     "  --options unittest <test-runner>    test runner need to start, mandatory\n"
75     "  --package-name <package-name>       package name, required for the FA model\n"
76     "  --module-name <module-name>         module name, required for the STAGE model\n"
77     "  --options <key> <value>             test options, such as testcase test_001\n"
78     "  --watchdog <wait-time>              max execute time for this test\n"
79     "  -D                                  test with debug mode\n";
80 
81 const std::string ABILITY_TOOL_HELP_MSG_NO_ABILITY_NAME_OPTION = "error: --ability <ability-name> is expected";
82 const std::string ABILITY_TOOL_HELP_MSG_NO_BUNDLE_NAME_OPTION = "error: --bundle <bundle-name> is expected";
83 const std::string ABILITY_TOOL_HELP_MSG_WINDOW_MODE_INVALID = "error: --options windowMode <value> with invalid param";
84 const std::string ABILITY_TOOL_HELP_MSG_LACK_VALUE = "error: lack of value of key";
85 const std::string ABILITY_TOOL_HELP_MSG_ONLY_NUM = "error: current option only support number";
86 const std::string ABILITY_TOOL_HELP_LACK_OPTIONS = "error: lack of essential args";
87 } // namespace
88 
89 class AbilityToolTest : public testing::Test {
90 public:
91     static void SetUpTestCase();
92     static void TearDownTestCase();
93     void SetUp();
94     void TearDown();
95 };
96 
SetUpTestCase()97 void AbilityToolTest::SetUpTestCase()
98 {}
99 
TearDownTestCase()100 void AbilityToolTest::TearDownTestCase()
101 {}
102 
SetUp()103 void AbilityToolTest::SetUp()
104 {
105     // reset optind to 0
106     optind = 0;
107 }
108 
TearDown()109 void AbilityToolTest::TearDown()
110 {}
111 
112 /**
113  * @tc.name: AbilityTool_Cmd_0100
114  * @tc.desc: "ability_tool" test.
115  * @tc.type: FUNC
116  */
117 HWTEST_F(AbilityToolTest, AbilityTool_Cmd_0100, TestSize.Level1)
118 {
119     char *argv[] = {
120         const_cast<char *>("ability_tool"),
121     };
122     int argc = sizeof(argv) / sizeof(argv[0]);
123     AbilityToolCommand cmd(argc, argv);
124     EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG);
125 }
126 
127 /**
128  * @tc.name: AbilityTool_Cmd_0200
129  * @tc.desc: invalid options "ability_tool xxx" test.
130  * @tc.type: FUNC
131  */
132 HWTEST_F(AbilityToolTest, AbilityTool_Cmd_0200, TestSize.Level1)
133 {
134     // "ability_tool"
135     char *argv[] = {
136         const_cast<char *>("ability_tool"),
137         const_cast<char *>("xxx"),
138     };
139     int argc = sizeof(argv) / sizeof(argv[0]);
140     AbilityToolCommand cmd(argc, argv);
141     EXPECT_EQ(cmd.ExecCommand(), cmd.GetCommandErrorMsg() + ABILITY_TOOL_HELP_MSG);
142 }
143 
144 /**
145  * @tc.name: AbilityTool_Cmd_0300
146  * @tc.desc: "ability_tool help" test.
147  * @tc.type: FUNC
148  */
149 HWTEST_F(AbilityToolTest, AbilityTool_Cmd_0300, TestSize.Level1)
150 {
151     char *argv[] = {
152         const_cast<char *>("ability_tool"),
153         const_cast<char *>("help"),
154     };
155     int argc = sizeof(argv) / sizeof(argv[0]);
156     AbilityToolCommand cmd(argc, argv);
157     EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG);
158 }
159 
160 /**
161  * @tc.name: AbilityTool_Start_0100
162  * @tc.desc: "ability_tool start" test.
163  * @tc.type: FUNC
164  */
165 HWTEST_F(AbilityToolTest, AbilityTool_Start_0100, TestSize.Level1)
166 {
167     char *argv[] = {
168         const_cast<char *>("ability_tool"),
169         const_cast<char *>("start"),
170     };
171     int argc = sizeof(argv) / sizeof(argv[0]);
172     AbilityToolCommand cmd(argc, argv);
173     EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_NO_ABILITY_NAME_OPTION + "\n" +
174                                  ABILITY_TOOL_HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n" +
175                                  ABILITY_TOOL_HELP_MSG_START);
176 }
177 
178 /**
179  * @tc.name: AbilityTool_Start_0200
180  * @tc.desc: "ability_tool start --help" test.
181  * @tc.type: FUNC
182  */
183 HWTEST_F(AbilityToolTest, AbilityTool_Start_0200, TestSize.Level1)
184 {
185     char *argv[] = {
186         const_cast<char *>("ability_tool"),
187         const_cast<char *>("start"),
188         const_cast<char *>("--help"),
189     };
190     int argc = sizeof(argv) / sizeof(argv[0]);
191     AbilityToolCommand cmd(argc, argv);
192     EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_NO_ABILITY_NAME_OPTION + "\n" +
193                                  ABILITY_TOOL_HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n" +
194                                  ABILITY_TOOL_HELP_MSG_START);
195 }
196 
197 /**
198  * @tc.name: AbilityTool_Start_0300
199  * @tc.desc: without bundleName "ability_tool start --ability TestAbility" test.
200  * @tc.type: FUNC
201  */
202 HWTEST_F(AbilityToolTest, AbilityTool_Start_0300, TestSize.Level1)
203 {
204     char *argv[] = {
205         const_cast<char *>("ability_tool"),
206         const_cast<char *>("start"),
207         const_cast<char *>("--ability"),
208         const_cast<char *>("TestAbility"),
209     };
210     int argc = sizeof(argv) / sizeof(argv[0]);
211     AbilityToolCommand cmd(argc, argv);
212     EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n" +
213                                  ABILITY_TOOL_HELP_MSG_START);
214 }
215 
216 /**
217  * @tc.name: AbilityTool_Start_0400
218  * @tc.desc: without abilityName "ability_tool start --bundle com.example.abilitytooltest" test.
219  * @tc.type: FUNC
220  */
221 HWTEST_F(AbilityToolTest, AbilityTool_Start_0400, TestSize.Level1)
222 {
223     char *argv[] = {
224         const_cast<char *>("ability_tool"),
225         const_cast<char *>("start"),
226         const_cast<char *>("--bundle"),
227         const_cast<char *>("com.example.abilitytooltest"),
228     };
229     int argc = sizeof(argv) / sizeof(argv[0]);
230     AbilityToolCommand cmd(argc, argv);
231     EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_NO_ABILITY_NAME_OPTION + "\n" +
232                                  ABILITY_TOOL_HELP_MSG_START);
233 }
234 
235 /**
236  * @tc.name: AbilityTool_Start_0500
237  * @tc.desc: lack of windowMode test.
238  * "ability_tool start --ability TestAbility --bundle com.example.abilitytooltest --options windowMode"
239  * @tc.type: FUNC
240  */
241 HWTEST_F(AbilityToolTest, AbilityTool_Start_0500, TestSize.Level1)
242 {
243     char *argv[] = {
244         const_cast<char *>("ability_tool"),
245         const_cast<char *>("start"),
246         const_cast<char *>("--ability"),
247         const_cast<char *>("TestAbility"),
248         const_cast<char *>("--bundle"),
249         const_cast<char *>("com.example.abilitytooltest"),
250         const_cast<char *>("--options"),
251         const_cast<char *>("windowMode"),
252     };
253     int argc = sizeof(argv) / sizeof(argv[0]);
254     AbilityToolCommand cmd(argc, argv);
255     EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_LACK_VALUE + "\n" +
256                                  ABILITY_TOOL_HELP_MSG_START);
257 }
258 
259 /**
260  * @tc.name: AbilityTool_Start_0600
261  * @tc.desc: invalid windowMode test.
262  * "ability_tool start --ability TestAbility --bundle com.example.abilitytooltest --options windowMode 20"
263  * @tc.type: FUNC
264  */
265 HWTEST_F(AbilityToolTest, AbilityTool_Start_0600, TestSize.Level1)
266 {
267     char *argv[] = {
268         const_cast<char *>("ability_tool"),
269         const_cast<char *>("start"),
270         const_cast<char *>("--ability"),
271         const_cast<char *>("TestAbility"),
272         const_cast<char *>("--bundle"),
273         const_cast<char *>("com.example.abilitytooltest"),
274         const_cast<char *>("--options"),
275         const_cast<char *>("windowMode"),
276         const_cast<char *>("20"),
277     };
278     int argc = sizeof(argv) / sizeof(argv[0]);
279     AbilityToolCommand cmd(argc, argv);
280     EXPECT_NE(cmd.ExecCommand().find(ABILITY_TOOL_HELP_MSG_START), string::npos);
281 }
282 
283 /**
284  * @tc.name: AbilityTool_Start_0700
285  * @tc.desc: flag isn't num test. flag is not a number, didn't parse.
286  * "ability_tool start --ability TestAbility --bundle com.example.abilitytooltest --flags abc"
287  * @tc.type: FUNC
288  */
289 HWTEST_F(AbilityToolTest, AbilityTool_Start_0700, TestSize.Level1)
290 {
291     char *argv[] = {
292         const_cast<char *>("ability_tool"),
293         const_cast<char *>("start"),
294         const_cast<char *>("--ability"),
295         const_cast<char *>("TestAbility"),
296         const_cast<char *>("--bundle"),
297         const_cast<char *>("com.example.abilitytooltest"),
298         const_cast<char *>("--flags"),
299         const_cast<char *>("abc"),
300     };
301     int argc = sizeof(argv) / sizeof(argv[0]);
302     AbilityToolCommand cmd(argc, argv);
303     EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_NG + "\n");
304 }
305 
306 /**
307  * @tc.name: AbilityTool_StopService_0100
308  * @tc.desc: "ability_tool stop-service" test.
309  * @tc.type: FUNC
310  */
311 HWTEST_F(AbilityToolTest, AbilityTool_StopService_0100, TestSize.Level1)
312 {
313     char *argv[] = {
314         const_cast<char *>("ability_tool"),
315         const_cast<char *>("stop-service"),
316     };
317     int argc = sizeof(argv) / sizeof(argv[0]);
318     AbilityToolCommand cmd(argc, argv);
319     EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_NO_ABILITY_NAME_OPTION + "\n" +
320                                  ABILITY_TOOL_HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n" +
321                                  ABILITY_TOOL_HELP_MSG_STOP_SERVICE);
322 }
323 
324 /**
325  * @tc.name: AbilityTool_StopService_0200
326  * @tc.desc: "ability_tool stop-service --help" test.
327  * @tc.type: FUNC
328  */
329 HWTEST_F(AbilityToolTest, AbilityTool_StopService_0200, TestSize.Level1)
330 {
331     char *argv[] = {
332         const_cast<char *>("ability_tool"),
333         const_cast<char *>("stop-service"),
334         const_cast<char *>("--help"),
335     };
336     int argc = sizeof(argv) / sizeof(argv[0]);
337     AbilityToolCommand cmd(argc, argv);
338     EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_NO_ABILITY_NAME_OPTION + "\n" +
339                                  ABILITY_TOOL_HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n" +
340                                  ABILITY_TOOL_HELP_MSG_STOP_SERVICE);
341 }
342 
343 /**
344  * @tc.name: AbilityTool_StopService_0300
345  * @tc.desc: without bundleName "ability_tool stop-service --ability TestAbility" test.
346  * @tc.type: FUNC
347  */
348 HWTEST_F(AbilityToolTest, AbilityTool_StopService_0300, TestSize.Level1)
349 {
350     char *argv[] = {
351         const_cast<char *>("ability_tool"),
352         const_cast<char *>("stop-service"),
353         const_cast<char *>("--ability"),
354         const_cast<char *>("TestAbility"),
355     };
356     int argc = sizeof(argv) / sizeof(argv[0]);
357     AbilityToolCommand cmd(argc, argv);
358     EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n" +
359                                  ABILITY_TOOL_HELP_MSG_STOP_SERVICE);
360 }
361 
362 /**
363  * @tc.name: AbilityTool_StopService_0400
364  * @tc.desc: without abilityName "ability_tool stop-service --ability TestAbility" test.
365  * @tc.type: FUNC
366  */
367 HWTEST_F(AbilityToolTest, AbilityTool_StopService_0400, TestSize.Level1)
368 {
369     char *argv[] = {
370         const_cast<char *>("ability_tool"),
371         const_cast<char *>("stop-service"),
372         const_cast<char *>("--bundle"),
373         const_cast<char *>("com.example.abilitytooltest"),
374     };
375     int argc = sizeof(argv) / sizeof(argv[0]);
376     AbilityToolCommand cmd(argc, argv);
377     EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_NO_ABILITY_NAME_OPTION + "\n" +
378                                  ABILITY_TOOL_HELP_MSG_STOP_SERVICE);
379 }
380 
381 /**
382  * @tc.name: AbilityTool_StopService_0500
383  * @tc.desc: stop-service "ability_tool stop-service --ability TestAbility" test.
384  * @tc.type: FUNC
385  */
386 HWTEST_F(AbilityToolTest, AbilityTool_StopService_0500, TestSize.Level1)
387 {
388     char *argv[] = {
389         const_cast<char *>("ability_tool"),
390         const_cast<char *>("stop-service"),
391         const_cast<char *>("--ability"),
392         const_cast<char *>("com.ohos.screenshot.ServiceExtAbility"),
393         const_cast<char *>("--bundle"),
394         const_cast<char *>("com.ohos.screenshot"),
395     };
396     int argc = sizeof(argv) / sizeof(argv[0]);
397     AbilityToolCommand cmd(argc, argv);
398     EXPECT_EQ(cmd.ExecCommand(), STRING_STOP_SERVICE_ABILITY_NG + "\n");
399 }
400 
401 /**
402  * @tc.name: AbilityTool_ForceStop_0100
403  * @tc.desc: "ability_tool force-stop" test.
404  * @tc.type: FUNC
405  */
406 HWTEST_F(AbilityToolTest, AbilityTool_ForceStop_0100, TestSize.Level1)
407 {
408     char *argv[] = {
409         const_cast<char *>("ability_tool"),
410         const_cast<char *>("force-stop"),
411     };
412     int argc = sizeof(argv) / sizeof(argv[0]);
413     AbilityToolCommand cmd(argc, argv);
414     EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_FORCE_STOP);
415 }
416 
417 /**
418  * @tc.name: AbilityTool_Test_0100
419  * @tc.desc: "ability_tool test" test.
420  * @tc.type: FUNC
421  */
422 HWTEST_F(AbilityToolTest, AbilityTool_Test_0100, TestSize.Level1)
423 {
424     char *argv[] = {
425         const_cast<char *>("ability_tool"),
426         const_cast<char *>("test"),
427     };
428     int argc = sizeof(argv) / sizeof(argv[0]);
429     AbilityToolCommand cmd(argc, argv);
430     EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_LACK_OPTIONS + "\n" + ABILITY_TOOL_HELP_MSG_TEST);
431 }
432 
433 /**
434  * @tc.name: AbilityTool_Test_0200
435  * @tc.desc: "ability_tool test --help" test.
436  * @tc.type: FUNC
437  */
438 HWTEST_F(AbilityToolTest, AbilityTool_Test_0200, TestSize.Level1)
439 {
440     char *argv[] = {
441         const_cast<char *>("ability_tool"),
442         const_cast<char *>("test"),
443         const_cast<char *>("--help"),
444     };
445     int argc = sizeof(argv) / sizeof(argv[0]);
446     AbilityToolCommand cmd(argc, argv);
447     EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_LACK_OPTIONS + "\n" + ABILITY_TOOL_HELP_MSG_TEST);
448 }
449 
450 /**
451  * @tc.name: AbilityTool_Test_0300
452  * @tc.desc: without bundleName "ability_tool test --options unittest testRunner" test.
453  * @tc.type: FUNC
454  */
455 HWTEST_F(AbilityToolTest, AbilityTool_Test_0300, TestSize.Level1)
456 {
457     char *argv[] = {
458         const_cast<char *>("ability_tool"),
459         const_cast<char *>("test"),
460         const_cast<char *>("--options"),
461         const_cast<char *>("unittest"),
462         const_cast<char *>("testRunner"),
463     };
464     int argc = sizeof(argv) / sizeof(argv[0]);
465     AbilityToolCommand cmd(argc, argv);
466     EXPECT_NE(cmd.ExecCommand().find(ABILITY_TOOL_HELP_MSG_TEST), string::npos);
467 }
468 
469 /**
470  * @tc.name: AbilityTool_Test_0400
471  * @tc.desc: without unittest "ability_tool test --bundle com.example.abilitytooltest" test.
472  * @tc.type: FUNC
473  */
474 HWTEST_F(AbilityToolTest, AbilityTool_Test_0400, TestSize.Level1)
475 {
476     char *argv[] = {
477         const_cast<char *>("ability_tool"),
478         const_cast<char *>("test"),
479         const_cast<char *>("--bundle"),
480         const_cast<char *>("com.example.abilitytooltest"),
481     };
482     int argc = sizeof(argv) / sizeof(argv[0]);
483     AbilityToolCommand cmd(argc, argv);
484     EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_LACK_OPTIONS + "\n" + ABILITY_TOOL_HELP_MSG_TEST);
485 }
486 
487 /**
488  * @tc.name: AbilityTool_Test_0500
489  * @tc.desc: without value of unittest "ability_tool test --bundle com.example.abilitytooltest --options unittest" test.
490  * @tc.type: FUNC
491  */
492 HWTEST_F(AbilityToolTest, AbilityTool_Test_0500, TestSize.Level1)
493 {
494     char *argv[] = {
495         const_cast<char *>("ability_tool"),
496         const_cast<char *>("test"),
497         const_cast<char *>("--bundle"),
498         const_cast<char *>("com.example.abilitytooltest"),
499         const_cast<char *>("--options"),
500         const_cast<char *>("unittest"),
501     };
502     int argc = sizeof(argv) / sizeof(argv[0]);
503     AbilityToolCommand cmd(argc, argv);
504     EXPECT_EQ(cmd.ExecCommand(), ABILITY_TOOL_HELP_MSG_LACK_VALUE + "\n" + ABILITY_TOOL_HELP_MSG_TEST);
505 }
506 } // namespace AAFwk
507 } // namespace OHOS
508