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 private public
19 #include "bundle_command.h"
20 #undef private
21 #include "bundle_installer_interface.h"
22 #include "iremote_broker.h"
23 #include "iremote_object.h"
24 #include "mock_bundle_installer_host.h"
25 #include "mock_bundle_mgr_host.h"
26
27 using namespace testing::ext;
28 using namespace OHOS;
29 using namespace OHOS::AAFwk;
30 using namespace OHOS::AppExecFwk;
31
32 class BmCommandUninstallTest : public ::testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36 void SetUp() override;
37 void TearDown() override;
38
39 void MakeMockObjects();
40 void SetMockObjects(BundleManagerShellCommand &cmd) const;
41
42 std::string cmd_ = "uninstall";
43 sptr<IBundleMgr> mgrProxyPtr_;
44 sptr<IBundleInstaller> installerProxyPtr_;
45 };
46
SetUpTestCase()47 void BmCommandUninstallTest::SetUpTestCase()
48 {}
49
TearDownTestCase()50 void BmCommandUninstallTest::TearDownTestCase()
51 {}
52
SetUp()53 void BmCommandUninstallTest::SetUp()
54 {
55 // reset optind to 0
56 optind = 0;
57
58 // make mock objects
59 MakeMockObjects();
60 }
61
TearDown()62 void BmCommandUninstallTest::TearDown()
63 {}
64
MakeMockObjects()65 void BmCommandUninstallTest::MakeMockObjects()
66 {
67 // mock a mgr host
68 auto mgrHostPtr = sptr<IRemoteObject>(new MockBundleMgrHost());
69 // mock a mgr proxy
70 mgrProxyPtr_ = iface_cast<IBundleMgr>(mgrHostPtr);
71
72 // mock a installer host
73 auto installerHostPtr = sptr<IRemoteObject>(new MockBundleInstallerHost());
74 // mock a installer proxy
75 installerProxyPtr_ = iface_cast<IBundleInstaller>(installerHostPtr);
76 }
77
SetMockObjects(BundleManagerShellCommand & cmd) const78 void BmCommandUninstallTest::SetMockObjects(BundleManagerShellCommand &cmd) const
79 {
80 // set the mock mgr proxy
81 cmd.bundleMgrProxy_ = mgrProxyPtr_;
82
83 // set the mock installer proxy
84 cmd.bundleInstallerProxy_ = installerProxyPtr_;
85 }
86
87 /**
88 * @tc.number: Bm_Command_Uninstall_0100
89 * @tc.name: ExecCommand
90 * @tc.desc: Verify the "bm uninstall" command.
91 */
92 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_0100, Function | MediumTest | Level1)
93 {
94 char *argv[] = {
95 const_cast<char*>(TOOL_NAME.c_str()),
96 const_cast<char*>(cmd_.c_str()),
97 const_cast<char*>(""),
98 };
99 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
100
101 BundleManagerShellCommand cmd(argc, argv);
102
103 // set the mock objects
104 SetMockObjects(cmd);
105
106 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_UNINSTALL);
107 }
108
109 /**
110 * @tc.number: Bm_Command_Uninstall_0200
111 * @tc.name: ExecCommand
112 * @tc.desc: Verify the "bm uninstall xxx" command.
113 */
114 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_0200, Function | MediumTest | Level1)
115 {
116 char *argv[] = {
117 const_cast<char*>(TOOL_NAME.c_str()),
118 const_cast<char*>(cmd_.c_str()),
119 const_cast<char*>("xxx"),
120 const_cast<char*>(""),
121 };
122 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
123
124 BundleManagerShellCommand cmd(argc, argv);
125
126 // set the mock objects
127 SetMockObjects(cmd);
128
129 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_UNINSTALL);
130 }
131
132 /**
133 * @tc.number: Bm_Command_Uninstall_0300
134 * @tc.name: ExecCommand
135 * @tc.desc: Verify the "bm uninstall -x" command.
136 */
137 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_0300, Function | MediumTest | Level1)
138 {
139 char *argv[] = {
140 const_cast<char*>(TOOL_NAME.c_str()),
141 const_cast<char*>(cmd_.c_str()),
142 const_cast<char*>("-x"),
143 const_cast<char*>(""),
144 };
145 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
146
147 BundleManagerShellCommand cmd(argc, argv);
148
149 // set the mock objects
150 SetMockObjects(cmd);
151
152 EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_UNINSTALL);
153 }
154
155 /**
156 * @tc.number: Bm_Command_Uninstall_0400
157 * @tc.name: ExecCommand
158 * @tc.desc: Verify the "bm uninstall -xxx" command.
159 */
160 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_0400, Function | MediumTest | Level1)
161 {
162 char *argv[] = {
163 const_cast<char*>(TOOL_NAME.c_str()),
164 const_cast<char*>(cmd_.c_str()),
165 const_cast<char*>("-xxx"),
166 const_cast<char*>(""),
167 };
168 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
169
170 BundleManagerShellCommand cmd(argc, argv);
171
172 // set the mock objects
173 SetMockObjects(cmd);
174
175 EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_UNINSTALL);
176 }
177
178 /**
179 * @tc.number: Bm_Command_Uninstall_0500
180 * @tc.name: ExecCommand
181 * @tc.desc: Verify the "bm uninstall --x" command.
182 */
183 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_0500, Function | MediumTest | Level1)
184 {
185 char *argv[] = {
186 const_cast<char*>(TOOL_NAME.c_str()),
187 const_cast<char*>(cmd_.c_str()),
188 const_cast<char*>("--x"),
189 const_cast<char*>(""),
190 };
191 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
192
193 BundleManagerShellCommand cmd(argc, argv);
194
195 // set the mock objects
196 SetMockObjects(cmd);
197
198 EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_UNINSTALL);
199 }
200
201 /**
202 * @tc.number: Bm_Command_Uninstall_0600
203 * @tc.name: ExecCommand
204 * @tc.desc: Verify the "bm uninstall --xxx" command.
205 */
206 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_0600, Function | MediumTest | Level1)
207 {
208 char *argv[] = {
209 const_cast<char*>(TOOL_NAME.c_str()),
210 const_cast<char*>(cmd_.c_str()),
211 const_cast<char*>("--xxx"),
212 const_cast<char*>(""),
213 };
214 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
215
216 BundleManagerShellCommand cmd(argc, argv);
217
218 // set the mock objects
219 SetMockObjects(cmd);
220
221 EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_UNINSTALL);
222 }
223
224 /**
225 * @tc.number: Bm_Command_Uninstall_0700
226 * @tc.name: ExecCommand
227 * @tc.desc: Verify the "bm uninstall --h" command.
228 */
229 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_0700, Function | MediumTest | Level1)
230 {
231 char *argv[] = {
232 const_cast<char*>(TOOL_NAME.c_str()),
233 const_cast<char*>(cmd_.c_str()),
234 const_cast<char*>("-h"),
235 const_cast<char*>(""),
236 };
237 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
238
239 BundleManagerShellCommand cmd(argc, argv);
240
241 // set the mock objects
242 SetMockObjects(cmd);
243
244 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNINSTALL);
245 }
246
247 /**
248 * @tc.number: Bm_Command_Uninstall_0800
249 * @tc.name: ExecCommand
250 * @tc.desc: Verify the "bm uninstall --help" command.
251 */
252 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_0800, Function | MediumTest | Level1)
253 {
254 char *argv[] = {
255 const_cast<char*>(TOOL_NAME.c_str()),
256 const_cast<char*>(cmd_.c_str()),
257 const_cast<char*>("--help"),
258 const_cast<char*>(""),
259 };
260 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
261
262 BundleManagerShellCommand cmd(argc, argv);
263
264 // set the mock objects
265 SetMockObjects(cmd);
266
267 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNINSTALL);
268 }
269
270 /**
271 * @tc.number: Bm_Command_Uninstall_0900
272 * @tc.name: ExecCommand
273 * @tc.desc: Verify the "bm uninstall -n" command.
274 */
275 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_0900, Function | MediumTest | Level1)
276 {
277 char *argv[] = {
278 const_cast<char*>(TOOL_NAME.c_str()),
279 const_cast<char*>(cmd_.c_str()),
280 const_cast<char*>("-n"),
281 const_cast<char*>(""),
282 };
283 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
284
285 BundleManagerShellCommand cmd(argc, argv);
286
287 // set the mock objects
288 SetMockObjects(cmd);
289
290 EXPECT_EQ(cmd.ExecCommand(), STRING_REQUIRE_CORRECT_VALUE + HELP_MSG_UNINSTALL);
291 }
292
293 /**
294 * @tc.number: Bm_Command_Uninstall_1300
295 * @tc.name: ExecCommand
296 * @tc.desc: Verify the "bm uninstall -n <bundle-name>" command.
297 */
298 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_1000, Function | MediumTest | Level1)
299 {
300 char *argv[] = {
301 const_cast<char*>(TOOL_NAME.c_str()),
302 const_cast<char*>(cmd_.c_str()),
303 const_cast<char*>("-n"),
304 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
305 const_cast<char*>(""),
306 };
307 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
308
309 BundleManagerShellCommand cmd(argc, argv);
310
311 // set the mock objects
312 SetMockObjects(cmd);
313
314 EXPECT_EQ(cmd.ExecCommand(), STRING_UNINSTALL_BUNDLE_OK + "\n");
315 }
316
317 /**
318 * @tc.number: Bm_Command_Uninstall_1100
319 * @tc.name: ExecCommand
320 * @tc.desc: Verify the "bm uninstall -n <bundle-name> -m" command.
321 */
322 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_1100, Function | MediumTest | Level1)
323 {
324 char *argv[] = {
325 const_cast<char*>(TOOL_NAME.c_str()),
326 const_cast<char*>(cmd_.c_str()),
327 const_cast<char*>("-n"),
328 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
329 const_cast<char*>("-m"),
330 const_cast<char*>(""),
331 };
332 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
333
334 BundleManagerShellCommand cmd(argc, argv);
335
336 // set the mock objects
337 SetMockObjects(cmd);
338
339 EXPECT_EQ(cmd.ExecCommand(), STRING_REQUIRE_CORRECT_VALUE + HELP_MSG_UNINSTALL);
340 }
341
342 /**
343 * @tc.number: Bm_Command_Uninstall_1200
344 * @tc.name: ExecCommand
345 * @tc.desc: Verify the "bm uninstall -m <module-name>" command.
346 */
347 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_1200, Function | MediumTest | Level1)
348 {
349 char *argv[] = {
350 const_cast<char*>(TOOL_NAME.c_str()),
351 const_cast<char*>(cmd_.c_str()),
352 const_cast<char*>("-m"),
353 const_cast<char*>(STRING_MODULE_NAME.c_str()),
354 const_cast<char*>(""),
355 };
356 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
357
358 BundleManagerShellCommand cmd(argc, argv);
359
360 // set the mock objects
361 SetMockObjects(cmd);
362
363 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n" + HELP_MSG_UNINSTALL);
364 }
365
366 /**
367 * @tc.number: Bm_Command_Uninstall_1300
368 * @tc.name: ExecCommand
369 * @tc.desc: Verify the "bm uninstall -n <bundle-name> -m <module-name>" command.
370 */
371 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_1300, Function | MediumTest | Level1)
372 {
373 char *argv[] = {
374 const_cast<char*>(TOOL_NAME.c_str()),
375 const_cast<char*>(cmd_.c_str()),
376 const_cast<char*>("-n"),
377 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
378 const_cast<char*>("-m"),
379 const_cast<char*>(STRING_MODULE_NAME.c_str()),
380 const_cast<char*>(""),
381 };
382 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
383
384 BundleManagerShellCommand cmd(argc, argv);
385
386 // set the mock objects
387 SetMockObjects(cmd);
388
389 EXPECT_EQ(cmd.ExecCommand(), STRING_UNINSTALL_BUNDLE_OK + "\n");
390 }
391
392 /**
393 * @tc.number: Bm_Command_Uninstall_1400
394 * @tc.name: ExecCommand
395 * @tc.desc: Verify the "bm uninstall -n <bundle-name> -u" command.
396 */
397 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_1400, Function | MediumTest | Level1)
398 {
399 char *argv[] = {
400 const_cast<char*>(TOOL_NAME.c_str()),
401 const_cast<char*>(cmd_.c_str()),
402 const_cast<char*>("-n"),
403 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
404 const_cast<char*>("-u"),
405 const_cast<char*>(""),
406 };
407 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
408
409 BundleManagerShellCommand cmd(argc, argv);
410
411 // set the mock objects
412 SetMockObjects(cmd);
413
414 EXPECT_EQ(cmd.ExecCommand(), STRING_REQUIRE_CORRECT_VALUE + HELP_MSG_UNINSTALL);
415 }
416
417 /**
418 * @tc.number: Bm_Command_Uninstall_1500
419 * @tc.name: ExecCommand
420 * @tc.desc: Verify the "bm uninstall -n <bundle-name> -u <user-id>" command.
421 */
422 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_1500, Function | MediumTest | Level1)
423 {
424 char *argv[] = {
425 const_cast<char*>(TOOL_NAME.c_str()),
426 const_cast<char*>(cmd_.c_str()),
427 const_cast<char*>("-n"),
428 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
429 const_cast<char*>("-u"),
430 const_cast<char*>(DEFAULT_USER_ID.c_str()),
431 const_cast<char*>(""),
432 };
433 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
434
435 BundleManagerShellCommand cmd(argc, argv);
436
437 // set the mock objects
438 SetMockObjects(cmd);
439
440 EXPECT_EQ(cmd.ExecCommand(), STRING_UNINSTALL_BUNDLE_OK + "\n");
441 }
442
443 /**
444 * @tc.number: Bm_Command_Uninstall_1600
445 * @tc.name: ExecCommand
446 * @tc.desc: Verify the "bm uninstall -n <bundle-name> -k" command.
447 */
448 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_1600, Function | MediumTest | Level1)
449 {
450 char *argv[] = {
451 const_cast<char*>(TOOL_NAME.c_str()),
452 const_cast<char*>(cmd_.c_str()),
453 const_cast<char*>("-n"),
454 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
455 const_cast<char*>("-k"),
456 const_cast<char*>(""),
457 };
458 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
459
460 BundleManagerShellCommand cmd(argc, argv);
461
462 // set the mock objects
463 SetMockObjects(cmd);
464
465 EXPECT_EQ(cmd.ExecCommand(), STRING_UNINSTALL_BUNDLE_OK + "\n");
466 }
467
468 /**
469 * @tc.number: Bm_Command_Uninstall_1700
470 * @tc.name: ExecCommand
471 * @tc.desc: Verify the "bm uninstall -n <bundle-name> -k xxx" command.
472 */
473 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_1700, Function | MediumTest | Level1)
474 {
475 char *argv[] = {
476 const_cast<char*>(TOOL_NAME.c_str()),
477 const_cast<char*>(cmd_.c_str()),
478 const_cast<char*>("-n"),
479 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
480 const_cast<char*>("-k"),
481 const_cast<char*>("XXX"),
482 const_cast<char*>(""),
483 };
484 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
485
486 BundleManagerShellCommand cmd(argc, argv);
487
488 // set the mock objects
489 SetMockObjects(cmd);
490
491 EXPECT_EQ(cmd.ExecCommand(), STRING_UNINSTALL_BUNDLE_OK + "\n");
492 }
493
494 /**
495 * @tc.number: Bm_Command_Uninstall_1800
496 * @tc.name: ExecCommand
497 * @tc.desc: Verify the "bm uninstall -n xxx" command.
498 */
499 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_1800, Function | MediumTest | Level1)
500 {
501 char *argv[] = {
502 const_cast<char*>(TOOL_NAME.c_str()),
503 const_cast<char*>(cmd_.c_str()),
504 const_cast<char*>("-n"),
505 const_cast<char*>("XXX"),
506 const_cast<char*>(""),
507 };
508 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
509
510 BundleManagerShellCommand cmd(argc, argv);
511
512 // set the mock objects
513 SetMockObjects(cmd);
514
515 EXPECT_EQ(cmd.ExecCommand(), STRING_UNINSTALL_BUNDLE_OK + "\n");
516 }
517
518 /**
519 * @tc.number: Bm_Command_Uninstall_1900
520 * @tc.name: ExecCommand
521 * @tc.desc: Verify the "bm uninstall -xxx <bundle-name>" command.
522 */
523 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_1900, Function | MediumTest | Level1)
524 {
525 char *argv[] = {
526 const_cast<char*>(TOOL_NAME.c_str()),
527 const_cast<char*>(cmd_.c_str()),
528 const_cast<char*>("-XXX"),
529 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
530 const_cast<char*>(""),
531 };
532 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
533
534 BundleManagerShellCommand cmd(argc, argv);
535
536 // set the mock objects
537 SetMockObjects(cmd);
538
539 EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_UNINSTALL);
540 }
541
542 /**
543 * @tc.number: Bm_Command_Uninstall_2000
544 * @tc.name: ExecCommand
545 * @tc.desc: Verify the "bm uninstall -n <bundle-name> -u <user-id>" command.
546 */
547 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_2000, Function | MediumTest | Level1)
548 {
549 char *argv[] = {
550 const_cast<char*>(TOOL_NAME.c_str()),
551 const_cast<char*>(cmd_.c_str()),
552 const_cast<char*>("-n"),
553 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
554 const_cast<char*>("-u"),
555 const_cast<char*>(ERR_USER_ID.c_str()),
556 const_cast<char*>(""),
557 };
558 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
559
560 BundleManagerShellCommand cmd(argc, argv);
561
562 // set the mock objects
563 SetMockObjects(cmd);
564
565 EXPECT_EQ(cmd.ExecCommand(), "error: option requires a correct value.\n");
566 }
567
568 /**
569 * @tc.number: Bm_Command_Uninstall_2100
570 * @tc.name: ExecCommand
571 * @tc.desc: Verify the "bm uninstall -n <bundle-name> -k 1" command.
572 */
573 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_2100, Function | MediumTest | Level1)
574 {
575 char *argv[] = {
576 const_cast<char*>(TOOL_NAME.c_str()),
577 const_cast<char*>(cmd_.c_str()),
578 const_cast<char*>("-n"),
579 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
580 const_cast<char*>("-k"),
581 const_cast<char*>("1"),
582 const_cast<char*>(""),
583 };
584 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
585
586 BundleManagerShellCommand cmd(argc, argv);
587
588 // set the mock objects
589 SetMockObjects(cmd);
590
591 EXPECT_EQ(cmd.ExecCommand(), STRING_UNINSTALL_BUNDLE_OK + "\n");
592 }
593
594 /**
595 * @tc.number: Bm_Command_Uninstall_2200
596 * @tc.name: ExecCommand
597 * @tc.desc: Verify the "bm uninstall -cc <bundle-name>" command.
598 */
599 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_2200, Function | MediumTest | Level1)
600 {
601 char *argv[] = {
602 const_cast<char*>(TOOL_NAME.c_str()),
603 const_cast<char*>(cmd_.c_str()),
604 const_cast<char*>("-ccc"),
605 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
606 const_cast<char*>(""),
607 };
608 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
609
610 BundleManagerShellCommand cmd(argc, argv);
611
612 // set the mock objects
613 SetMockObjects(cmd);
614
615 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNINSTALL);
616 }