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