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