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 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 class AaCommandStartTest : public ::testing::Test {
32 public:
33 static void SetUpTestCase();
34 static void TearDownTestCase();
35 void SetUp() override;
36 void TearDown() override;
37
38 void MakeMockObjects() const;
39
40 std::string cmd_ = "start";
41 };
42
SetUpTestCase()43 void AaCommandStartTest::SetUpTestCase()
44 {}
45
TearDownTestCase()46 void AaCommandStartTest::TearDownTestCase()
47 {}
48
SetUp()49 void AaCommandStartTest::SetUp()
50 {
51 // reset optind to 0
52 optind = 0;
53
54 // make mock objects
55 MakeMockObjects();
56 }
57
TearDown()58 void AaCommandStartTest::TearDown()
59 {}
60
MakeMockObjects() const61 void AaCommandStartTest::MakeMockObjects() const
62 {
63 // mock a stub
64 auto managerStubPtr = sptr<IRemoteObject>(new MockAbilityManagerStub());
65
66 // set the mock stub
67 auto managerClientPtr = AbilityManagerClient::GetInstance();
68 managerClientPtr->remoteObject_ = managerStubPtr;
69 }
70
71 /**
72 * @tc.number: Aa_Command_Start_0100
73 * @tc.name: ExecCommand
74 * @tc.desc: Verify the "aa start" command.
75 */
76 HWTEST_F(AaCommandStartTest, Aa_Command_Start_0100, Function | MediumTest | Level1)
77 {
78 char *argv[] = {
79 (char *)TOOL_NAME.c_str(),
80 (char *)cmd_.c_str(),
81 (char *)"",
82 };
83 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
84
85 AbilityManagerShellCommand cmd(argc, argv);
86 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_START);
87 }
88
89 /**
90 * @tc.number: Aa_Command_Start_0200
91 * @tc.name: ExecCommand
92 * @tc.desc: Verify the "aa start xxx" command.
93 */
94 HWTEST_F(AaCommandStartTest, Aa_Command_Start_0200, Function | MediumTest | Level1)
95 {
96 char *argv[] = {
97 (char *)TOOL_NAME.c_str(),
98 (char *)cmd_.c_str(),
99 (char *)"xxx",
100 (char *)"",
101 };
102 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
103
104 AbilityManagerShellCommand cmd(argc, argv);
105 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_START);
106 }
107
108 /**
109 * @tc.number: Aa_Command_Start_0300
110 * @tc.name: ExecCommand
111 * @tc.desc: Verify the "aa start -x" command.
112 */
113 HWTEST_F(AaCommandStartTest, Aa_Command_Start_0300, Function | MediumTest | Level1)
114 {
115 char *argv[] = {
116 (char *)TOOL_NAME.c_str(),
117 (char *)cmd_.c_str(),
118 (char *)"-x",
119 (char *)"",
120 };
121 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
122
123 AbilityManagerShellCommand cmd(argc, argv);
124 EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_START);
125 }
126
127 /**
128 * @tc.number: Aa_Command_Start_0400
129 * @tc.name: ExecCommand
130 * @tc.desc: Verify the "aa start -xxx" command.
131 */
132 HWTEST_F(AaCommandStartTest, Aa_Command_Start_0400, Function | MediumTest | Level1)
133 {
134 char *argv[] = {
135 (char *)TOOL_NAME.c_str(),
136 (char *)cmd_.c_str(),
137 (char *)"-xxx",
138 (char *)"",
139 };
140 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
141
142 AbilityManagerShellCommand cmd(argc, argv);
143 EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_START);
144 }
145
146 /**
147 * @tc.number: Aa_Command_Start_0500
148 * @tc.name: ExecCommand
149 * @tc.desc: Verify the "aa start --x" command.
150 */
151 HWTEST_F(AaCommandStartTest, Aa_Command_Start_0500, Function | MediumTest | Level1)
152 {
153 char *argv[] = {
154 (char *)TOOL_NAME.c_str(),
155 (char *)cmd_.c_str(),
156 (char *)"--x",
157 (char *)"",
158 };
159 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
160
161 AbilityManagerShellCommand cmd(argc, argv);
162 EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_START);
163 }
164
165 /**
166 * @tc.number: Aa_Command_Start_0600
167 * @tc.name: ExecCommand
168 * @tc.desc: Verify the "aa start --xxx" command.
169 */
170 HWTEST_F(AaCommandStartTest, Aa_Command_Start_0600, Function | MediumTest | Level1)
171 {
172 char *argv[] = {
173 (char *)TOOL_NAME.c_str(),
174 (char *)cmd_.c_str(),
175 (char *)"--xxx",
176 (char *)"",
177 };
178 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
179
180 AbilityManagerShellCommand cmd(argc, argv);
181 EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_START);
182 }
183
184 /**
185 * @tc.number: Aa_Command_Start_0700
186 * @tc.name: ExecCommand
187 * @tc.desc: Verify the "aa start -h" command.
188 */
189 HWTEST_F(AaCommandStartTest, Aa_Command_Start_0700, Function | MediumTest | Level1)
190 {
191 char *argv[] = {
192 (char *)TOOL_NAME.c_str(),
193 (char *)cmd_.c_str(),
194 (char *)"-h",
195 (char *)"",
196 };
197 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
198
199 AbilityManagerShellCommand cmd(argc, argv);
200 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_START);
201 }
202
203 /**
204 * @tc.number: Aa_Command_Start_0800
205 * @tc.name: ExecCommand
206 * @tc.desc: Verify the "aa start --help" command.
207 */
208 HWTEST_F(AaCommandStartTest, Aa_Command_Start_0800, Function | MediumTest | Level1)
209 {
210 char *argv[] = {
211 (char *)TOOL_NAME.c_str(),
212 (char *)cmd_.c_str(),
213 (char *)"--help",
214 (char *)"",
215 };
216 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
217
218 AbilityManagerShellCommand cmd(argc, argv);
219 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_START);
220 }
221
222 /**
223 * @tc.number: Aa_Command_Start_0900
224 * @tc.name: ExecCommand
225 * @tc.desc: Verify the "aa start -d" command.
226 */
227 HWTEST_F(AaCommandStartTest, Aa_Command_Start_0900, Function | MediumTest | Level1)
228 {
229 char *argv[] = {
230 (char *)TOOL_NAME.c_str(),
231 (char *)cmd_.c_str(),
232 (char *)"-d",
233 (char *)"",
234 };
235 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
236
237 AbilityManagerShellCommand cmd(argc, argv);
238 EXPECT_EQ(cmd.ExecCommand(), "error: option requires a value.\n" + HELP_MSG_START);
239 }
240
241 /**
242 * @tc.number: Aa_Command_Start_1000
243 * @tc.name: ExecCommand
244 * @tc.desc: Verify the "aa start -d <device-id>" command.
245 */
246 HWTEST_F(AaCommandStartTest, Aa_Command_Start_1000, Function | MediumTest | Level1)
247 {
248 char *argv[] = {
249 (char *)TOOL_NAME.c_str(),
250 (char *)cmd_.c_str(),
251 (char *)"-d",
252 (char *)STRING_DEVICE.c_str(),
253 (char *)"",
254 };
255 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
256
257 AbilityManagerShellCommand cmd(argc, argv);
258 EXPECT_EQ(cmd.ExecCommand(),
259 HELP_MSG_NO_ABILITY_NAME_OPTION + "\n" + HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n" + HELP_MSG_START);
260 }
261
262 /**
263 * @tc.number: Aa_Command_Start_1100
264 * @tc.name: ExecCommand
265 * @tc.desc: Verify the "aa start -d <device-id> -a" command.
266 */
267 HWTEST_F(AaCommandStartTest, Aa_Command_Start_1100, Function | MediumTest | Level1)
268 {
269 char *argv[] = {
270 (char *)TOOL_NAME.c_str(),
271 (char *)cmd_.c_str(),
272 (char *)"-d",
273 (char *)STRING_DEVICE.c_str(),
274 (char *)"-a",
275 (char *)"",
276 };
277 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
278
279 AbilityManagerShellCommand cmd(argc, argv);
280 EXPECT_EQ(cmd.ExecCommand(), "error: option requires a value.\n" + HELP_MSG_START);
281 }
282
283 /**
284 * @tc.number: Aa_Command_Start_1200
285 * @tc.name: ExecCommand
286 * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name>" command.
287 */
288 HWTEST_F(AaCommandStartTest, Aa_Command_Start_1200, Function | MediumTest | Level1)
289 {
290 char *argv[] = {
291 (char *)TOOL_NAME.c_str(),
292 (char *)cmd_.c_str(),
293 (char *)"-d",
294 (char *)STRING_DEVICE.c_str(),
295 (char *)"-a",
296 (char *)STRING_ABILITY_NAME.c_str(),
297 (char *)"",
298 };
299 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
300
301 AbilityManagerShellCommand cmd(argc, argv);
302 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n" + HELP_MSG_START);
303 }
304
305 /**
306 * @tc.number: Aa_Command_Start_1300
307 * @tc.name: ExecCommand
308 * @tc.desc: Verify the "aa start -d <device-id> -b" command.
309 */
310 HWTEST_F(AaCommandStartTest, Aa_Command_Start_1300, Function | MediumTest | Level1)
311 {
312 char *argv[] = {
313 (char *)TOOL_NAME.c_str(),
314 (char *)cmd_.c_str(),
315 (char *)"-d",
316 (char *)STRING_DEVICE.c_str(),
317 (char *)"-b",
318 (char *)"",
319 };
320 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
321
322 AbilityManagerShellCommand cmd(argc, argv);
323 EXPECT_EQ(cmd.ExecCommand(), "error: option requires a value.\n" + HELP_MSG_START);
324 }
325
326 /**
327 * @tc.number: Aa_Command_Start_1400
328 * @tc.name: ExecCommand
329 * @tc.desc: Verify the "aa start -d <device-id> -b <bundle-name>" command.
330 */
331 HWTEST_F(AaCommandStartTest, Aa_Command_Start_1400, Function | MediumTest | Level1)
332 {
333 char *argv[] = {
334 (char *)TOOL_NAME.c_str(),
335 (char *)cmd_.c_str(),
336 (char *)"-d",
337 (char *)STRING_DEVICE.c_str(),
338 (char *)"-b",
339 (char *)STRING_BUNDLE_NAME.c_str(),
340 (char *)"",
341 };
342 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
343
344 AbilityManagerShellCommand cmd(argc, argv);
345 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_ABILITY_NAME_OPTION + "\n" + HELP_MSG_START);
346 }
347
348 /**
349 * @tc.number: Aa_Command_Start_1500
350 * @tc.name: ExecCommand
351 * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b" command.
352 */
353 HWTEST_F(AaCommandStartTest, Aa_Command_Start_1500, Function | MediumTest | Level1)
354 {
355 char *argv[] = {
356 (char *)TOOL_NAME.c_str(),
357 (char *)cmd_.c_str(),
358 (char *)"-d",
359 (char *)STRING_DEVICE.c_str(),
360 (char *)"-a",
361 (char *)STRING_ABILITY_NAME.c_str(),
362 (char *)"-b",
363 (char *)"",
364 };
365 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
366
367 AbilityManagerShellCommand cmd(argc, argv);
368 EXPECT_EQ(cmd.ExecCommand(), "error: option requires a value.\n" + HELP_MSG_START);
369 }
370
371 /**
372 * @tc.number: Aa_Command_Start_1600
373 * @tc.name: ExecCommand
374 * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name>" command.
375 */
376 HWTEST_F(AaCommandStartTest, Aa_Command_Start_1600, Function | MediumTest | Level1)
377 {
378 char *argv[] = {
379 (char *)TOOL_NAME.c_str(),
380 (char *)cmd_.c_str(),
381 (char *)"-d",
382 (char *)STRING_DEVICE.c_str(),
383 (char *)"-a",
384 (char *)STRING_ABILITY_NAME.c_str(),
385 (char *)"-b",
386 (char *)STRING_BUNDLE_NAME.c_str(),
387 (char *)"",
388 };
389 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
390
391 AbilityManagerShellCommand cmd(argc, argv);
392 EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_OK + "\n");
393 }
394
395 /**
396 * @tc.number: Aa_Command_Start_1700
397 * @tc.name: ExecCommand
398 * @tc.desc: Verify the "aa start -a" command.
399 */
400 HWTEST_F(AaCommandStartTest, Aa_Command_Start_1700, Function | MediumTest | Level1)
401 {
402 char *argv[] = {
403 (char *)TOOL_NAME.c_str(),
404 (char *)cmd_.c_str(),
405 (char *)"-a",
406 (char *)"",
407 };
408 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
409
410 AbilityManagerShellCommand cmd(argc, argv);
411 EXPECT_EQ(cmd.ExecCommand(), "error: option requires a value.\n" + HELP_MSG_START);
412 }
413
414 /**
415 * @tc.number: Aa_Command_Start_1800
416 * @tc.name: ExecCommand
417 * @tc.desc: Verify the "aa start -a <ability-name> -b" command.
418 */
419 HWTEST_F(AaCommandStartTest, Aa_Command_Start_1800, Function | MediumTest | Level1)
420 {
421 char *argv[] = {
422 (char *)TOOL_NAME.c_str(),
423 (char *)cmd_.c_str(),
424 (char *)"-a",
425 (char *)STRING_ABILITY_NAME.c_str(),
426 (char *)"-b",
427 (char *)"",
428 };
429 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
430
431 AbilityManagerShellCommand cmd(argc, argv);
432 EXPECT_EQ(cmd.ExecCommand(), "error: option requires a value.\n" + HELP_MSG_START);
433 }
434
435 /**
436 * @tc.number: Aa_Command_Start_1900
437 * @tc.name: ExecCommand
438 * @tc.desc: Verify the "aa start -a <ability-name> -b <bundle-name>" command.
439 */
440 HWTEST_F(AaCommandStartTest, Aa_Command_Start_1900, Function | MediumTest | Level1)
441 {
442 char *argv[] = {
443 (char *)TOOL_NAME.c_str(),
444 (char *)cmd_.c_str(),
445 (char *)"-a",
446 (char *)STRING_ABILITY_NAME.c_str(),
447 (char *)"-b",
448 (char *)STRING_BUNDLE_NAME.c_str(),
449 (char *)"",
450 };
451 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
452
453 AbilityManagerShellCommand cmd(argc, argv);
454 EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_OK + "\n");
455 }
456
457 /**
458 * @tc.number: Aa_Command_Start_2000
459 * @tc.name: ExecCommand
460 * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name>" command.
461 */
462 HWTEST_F(AaCommandStartTest, Aa_Command_Start_2000, Function | MediumTest | Level1)
463 {
464 char *argv[] = {
465 (char *)TOOL_NAME.c_str(),
466 (char *)cmd_.c_str(),
467 (char *)"-d",
468 (char *)STRING_DEVICE.c_str(),
469 (char *)"-a",
470 (char *)STRING_ABILITY_NAME_INVALID.c_str(),
471 (char *)"-b",
472 (char *)STRING_BUNDLE_NAME.c_str(),
473 (char *)"",
474 };
475 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
476
477 AbilityManagerShellCommand cmd(argc, argv);
478 EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_NG + "\n" + cmd.messageMap_.at(RESOLVE_ABILITY_ERR) + "\n");
479 }
480
481 /**
482 * @tc.number: Aa_Command_Start_2100
483 * @tc.name: ExecCommand
484 * @tc.desc: Verify the "aa start -d <device-id> -a <ability-name> -b <bundle-name>" command.
485 */
486 HWTEST_F(AaCommandStartTest, Aa_Command_Start_2100, Function | MediumTest | Level1)
487 {
488 char *argv[] = {
489 (char *)TOOL_NAME.c_str(),
490 (char *)cmd_.c_str(),
491 (char *)"-d",
492 (char *)STRING_DEVICE.c_str(),
493 (char *)"-a",
494 (char *)STRING_ABILITY_NAME.c_str(),
495 (char *)"-b",
496 (char *)STRING_BUNDLE_NAME_INVALID.c_str(),
497 (char *)"",
498 };
499 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
500
501 AbilityManagerShellCommand cmd(argc, argv);
502 EXPECT_EQ(cmd.ExecCommand(), STRING_START_ABILITY_NG + "\n" + cmd.messageMap_.at(RESOLVE_APP_ERR) + "\n");
503 }
504