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_constants.h"
22 #include "bundle_installer_interface.h"
23 #include "iremote_broker.h"
24 #include "iremote_object.h"
25 #include "mock_bundle_installer_host.h"
26 #include "mock_bundle_mgr_host.h"
27
28 using namespace testing::ext;
29 using namespace OHOS::AAFwk;
30 using namespace OHOS::AppExecFwk;
31
32 namespace OHOS {
33 class BmCommandDumpTest : 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_ = "dump";
44 sptr<IBundleMgr> mgrProxyPtr_;
45 sptr<IBundleInstaller> installerProxyPtr_;
46 };
47
SetUpTestCase()48 void BmCommandDumpTest::SetUpTestCase()
49 {}
50
TearDownTestCase()51 void BmCommandDumpTest::TearDownTestCase()
52 {}
53
SetUp()54 void BmCommandDumpTest::SetUp()
55 {
56 // reset optind to 0
57 optind = 0;
58
59 // make mock objects
60 MakeMockObjects();
61 }
62
TearDown()63 void BmCommandDumpTest::TearDown()
64 {}
65
MakeMockObjects()66 void BmCommandDumpTest::MakeMockObjects()
67 {
68 // mock a mgr host
69 auto mgrHostPtr = sptr<IRemoteObject>(new MockBundleMgrHost());
70 // mock a mgr proxy
71 mgrProxyPtr_ = iface_cast<IBundleMgr>(mgrHostPtr);
72
73 // mock a installer host
74 auto installerHostPtr = sptr<IRemoteObject>(new MockBundleInstallerHost());
75 // mock a installer proxy
76 installerProxyPtr_ = iface_cast<IBundleInstaller>(installerHostPtr);
77 }
78
SetMockObjects(BundleManagerShellCommand & cmd) const79 void BmCommandDumpTest::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_Dump_0100
90 * @tc.name: ExecCommand
91 * @tc.desc: Verify the "bm dump" command.
92 */
93 HWTEST_F(BmCommandDumpTest, Bm_Command_Dump_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_DUMP);
108 }
109
110 /**
111 * @tc.number: Bm_Command_Dump_0200
112 * @tc.name: ExecCommand
113 * @tc.desc: Verify the "bm dump xxx" command.
114 */
115 HWTEST_F(BmCommandDumpTest, Bm_Command_Dump_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_DUMP);
131 }
132
133 /**
134 * @tc.number: Bm_Command_Dump_0300
135 * @tc.name: ExecCommand
136 * @tc.desc: Verify the "bm dump -x" command.
137 */
138 HWTEST_F(BmCommandDumpTest, Bm_Command_Dump_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_DUMP);
154 }
155
156 /**
157 * @tc.number: Bm_Command_Dump_0400
158 * @tc.name: ExecCommand
159 * @tc.desc: Verify the "bm dump -xxx" command.
160 */
161 HWTEST_F(BmCommandDumpTest, Bm_Command_Dump_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_DUMP);
177 }
178
179 /**
180 * @tc.number: Bm_Command_Dump_0500
181 * @tc.name: ExecCommand
182 * @tc.desc: Verify the "bm dump --x" command.
183 */
184 HWTEST_F(BmCommandDumpTest, Bm_Command_Dump_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_DUMP);
200 }
201
202 /**
203 * @tc.number: Bm_Command_Dump_0600
204 * @tc.name: ExecCommand
205 * @tc.desc: Verify the "bm dump --xxx" command.
206 */
207 HWTEST_F(BmCommandDumpTest, Bm_Command_Dump_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_DUMP);
223 }
224
225 /**
226 * @tc.number: Bm_Command_Dump_0700
227 * @tc.name: ExecCommand
228 * @tc.desc: Verify the "bm dump -h" command.
229 */
230 HWTEST_F(BmCommandDumpTest, Bm_Command_Dump_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_DUMP);
246 }
247
248 /**
249 * @tc.number: Bm_Command_Dump_0800
250 * @tc.name: ExecCommand
251 * @tc.desc: Verify the "bm dump --help" command.
252 */
253 HWTEST_F(BmCommandDumpTest, Bm_Command_Dump_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_DUMP);
269 }
270
271 /**
272 * @tc.number: Bm_Command_Dump_0900
273 * @tc.name: ExecCommand
274 * @tc.desc: Verify the "bm dump -a" command.
275 */
276 HWTEST_F(BmCommandDumpTest, Bm_Command_Dump_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*>("-a"),
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(), "OK");
292 }
293
294 /**
295 * @tc.number: Bm_Command_Dump_1000
296 * @tc.name: ExecCommand
297 * @tc.desc: Verify the "bm dump --all" command.
298 */
299 HWTEST_F(BmCommandDumpTest, Bm_Command_Dump_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*>("--all"),
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(), "OK");
315 }
316
317 /**
318 * @tc.number: Bm_Command_Dump_1100
319 * @tc.name: ExecCommand
320 * @tc.desc: Verify the "bm dump -n" command.
321 */
322 HWTEST_F(BmCommandDumpTest, Bm_Command_Dump_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*>(""),
329 };
330 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
331
332 BundleManagerShellCommand cmd(argc, argv);
333
334 // set the mock objects
335 SetMockObjects(cmd);
336
337 EXPECT_EQ(cmd.ExecCommand(), STRING_REQUIRE_CORRECT_VALUE + HELP_MSG_DUMP);
338 }
339
340 /**
341 * @tc.number: Bm_Command_Dump_1200
342 * @tc.name: ExecCommand
343 * @tc.desc: Verify the "bm dump -n <bundle-name>" command.
344 */
345 HWTEST_F(BmCommandDumpTest, Bm_Command_Dump_1200, Function | MediumTest | Level1)
346 {
347 char *argv[] = {
348 const_cast<char*>(TOOL_NAME.c_str()),
349 const_cast<char*>(cmd_.c_str()),
350 const_cast<char*>("-n"),
351 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
352 const_cast<char*>(""),
353 };
354 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
355
356 BundleManagerShellCommand cmd(argc, argv);
357
358 // set the mock objects
359 SetMockObjects(cmd);
360
361 EXPECT_EQ(cmd.ExecCommand(), STRING_BUNDLE_NAME + "\n");
362 }
363
364 /**
365 * @tc.number: Bm_Command_Dump_1300
366 * @tc.name: ExecCommand
367 * @tc.desc: Verify the "bm dump -s" command.
368 * @tc.require: AR000GJUII
369 */
370 HWTEST_F(BmCommandDumpTest, Bm_Command_Dump_1300, Function | MediumTest | Level1)
371 {
372 char *argv[] = {
373 const_cast<char*>(TOOL_NAME.c_str()),
374 const_cast<char*>(cmd_.c_str()),
375 const_cast<char*>("-s"),
376 const_cast<char*>(""),
377 };
378 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
379 BundleManagerShellCommand cmd(argc, argv);
380 // set the mock objects
381 SetMockObjects(cmd);
382 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n" + HELP_MSG_DUMP);
383 }
384
385 /**
386 * @tc.number: Bm_Command_Dump_1400
387 * @tc.name: ExecCommand
388 * @tc.desc: Verify the "bm dump -n <bundle-name> -s" command.
389 * @tc.require: AR000GJUII
390 */
391 HWTEST_F(BmCommandDumpTest, Bm_Command_Dump_1400, Function | MediumTest | Level1)
392 {
393 char *argv[] = {
394 const_cast<char*>(TOOL_NAME.c_str()),
395 const_cast<char*>(cmd_.c_str()),
396 const_cast<char*>("-n"),
397 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
398 const_cast<char*>("-s"),
399 const_cast<char*>(""),
400 };
401 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
402 BundleManagerShellCommand cmd(argc, argv);
403 // set the mock objects
404 SetMockObjects(cmd);
405 EXPECT_EQ(cmd.ExecCommand(), STRING_BUNDLE_NAME + "\n");
406 }
407
408 /**
409 * @tc.number: Bm_Command_Dump_1500
410 * @tc.name: ExecCommand
411 * @tc.desc: Verify the "bm dump -n <bundle-name> -u" command.
412 */
413 HWTEST_F(BmCommandDumpTest, Bm_Command_Dump_1500, Function | MediumTest | Level1)
414 {
415 char *argv[] = {
416 const_cast<char*>(TOOL_NAME.c_str()),
417 const_cast<char*>(cmd_.c_str()),
418 const_cast<char*>("-n"),
419 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
420 const_cast<char*>("-u"),
421 const_cast<char*>(""),
422 };
423 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
424 BundleManagerShellCommand cmd(argc, argv);
425 // set the mock objects
426 SetMockObjects(cmd);
427 EXPECT_EQ(cmd.ExecCommand(), STRING_REQUIRE_CORRECT_VALUE + HELP_MSG_DUMP);
428 }
429
430 /**
431 * @tc.number: Bm_Command_Dump_1600
432 * @tc.name: ExecCommand
433 * @tc.desc: Verify the "bm dump -n <bundle-name> -u <user-id>" command.
434 */
435 HWTEST_F(BmCommandDumpTest, Bm_Command_Dump_1600, Function | MediumTest | Level1)
436 {
437 char *argv[] = {
438 const_cast<char*>(TOOL_NAME.c_str()),
439 const_cast<char*>(cmd_.c_str()),
440 const_cast<char*>("-n"),
441 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
442 const_cast<char*>("-u"),
443 const_cast<char*>(DEFAULT_USER_ID.c_str()),
444 const_cast<char*>(""),
445 };
446 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
447 BundleManagerShellCommand cmd(argc, argv);
448 // set the mock objects
449 SetMockObjects(cmd);
450 EXPECT_EQ(cmd.ExecCommand(), STRING_BUNDLE_NAME + "\n");
451 }
452
453 /**
454 * @tc.number: Bm_Command_Dump_1700
455 * @tc.name: ExecCommand
456 * @tc.desc: Verify the "bm dump -n <bundle-name> -d" command.
457 */
458 HWTEST_F(BmCommandDumpTest, Bm_Command_Dump_1700, Function | MediumTest | Level1)
459 {
460 char *argv[] = {
461 const_cast<char*>(TOOL_NAME.c_str()),
462 const_cast<char*>(cmd_.c_str()),
463 const_cast<char*>("-n"),
464 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
465 const_cast<char*>("-d"),
466 const_cast<char*>(""),
467 };
468 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
469 BundleManagerShellCommand cmd(argc, argv);
470 // set the mock objects
471 SetMockObjects(cmd);
472 EXPECT_EQ(cmd.ExecCommand(), STRING_REQUIRE_CORRECT_VALUE + HELP_MSG_DUMP);
473 }
474
475 /**
476 * @tc.number: Bm_Command_Dump_1800
477 * @tc.name: ExecCommand
478 * @tc.desc: Verify the "bm dump -n <bundle-name> -d <device-id>" command.
479 */
480 HWTEST_F(BmCommandDumpTest, Bm_Command_Dump_1800, Function | MediumTest | Level1)
481 {
482 char *argv[] = {
483 const_cast<char*>(TOOL_NAME.c_str()),
484 const_cast<char*>(cmd_.c_str()),
485 const_cast<char*>("-n"),
486 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
487 const_cast<char*>("-d"),
488 const_cast<char*>(DEFAULT_DEVICE_TIME.c_str()),
489 const_cast<char*>(""),
490 };
491 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
492 BundleManagerShellCommand cmd(argc, argv);
493 // set the mock objects
494 SetMockObjects(cmd);
495 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_DUMP_FAILED + "\n");
496 }
497
498 /**
499 * @tc.number: Bm_Command_Dump_1900
500 * @tc.name: ExecCommand
501 * @tc.desc: Verify the "bm dump -n <bundle-name> -XXX <user-id>" command.
502 */
503 HWTEST_F(BmCommandDumpTest, Bm_Command_Dump_1900, Function | MediumTest | Level1)
504 {
505 char *argv[] = {
506 const_cast<char*>(TOOL_NAME.c_str()),
507 const_cast<char*>(cmd_.c_str()),
508 const_cast<char*>("-n"),
509 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
510 const_cast<char*>("-XXX"),
511 const_cast<char*>(DEFAULT_USER_ID.c_str()),
512 const_cast<char*>(""),
513 };
514 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
515 BundleManagerShellCommand cmd(argc, argv);
516 // set the mock objects
517 SetMockObjects(cmd);
518 EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_DUMP);
519 }
520
521 /**
522 * @tc.number: Bm_Command_Dump_2000
523 * @tc.name: ExecCommand
524 * @tc.desc: Verify the "bm dump -n <bundle-name> -u <user-id>" command.
525 */
526 HWTEST_F(BmCommandDumpTest, Bm_Command_Dump_2000, Function | MediumTest | Level1)
527 {
528 char *argv[] = {
529 const_cast<char*>(TOOL_NAME.c_str()),
530 const_cast<char*>(cmd_.c_str()),
531 const_cast<char*>("-n"),
532 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
533 const_cast<char*>("-u"),
534 const_cast<char*>(ERR_USER_ID.c_str()),
535 const_cast<char*>(""),
536 };
537 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
538 BundleManagerShellCommand cmd(argc, argv);
539 // set the mock objects
540 SetMockObjects(cmd);
541 EXPECT_EQ(cmd.ExecCommand(), "error: option requires a correct value.\n");
542 }
543
544 /**
545 * @tc.number: Bm_Command_Dump_2100
546 * @tc.name: ExecCommand
547 * @tc.desc: Verify the "bm dump -xx <bundle-name>" command.
548 */
549 HWTEST_F(BmCommandDumpTest, Bm_Command_Dump_2100, Function | MediumTest | Level1)
550 {
551 char *argv[] = {
552 const_cast<char*>(TOOL_NAME.c_str()),
553 const_cast<char*>(cmd_.c_str()),
554 const_cast<char*>("-xx"),
555 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
556 const_cast<char*>(""),
557 };
558 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
559 BundleManagerShellCommand cmd(argc, argv);
560 // set the mock objects
561 SetMockObjects(cmd);
562 EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_DUMP);
563 }
564 } // namespace OHOS