1 /*
2 * Copyright (c) 2023 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::AAFwk;
29 using namespace OHOS::AppExecFwk;
30
31 namespace OHOS {
32 class BmCommandOverlayTest : 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 overlay_ = "dump-overlay";
43 std::string tOverlay_ = "dump-target-overlay";
44 sptr<IBundleMgr> mgrProxyPtr_;
45 sptr<IBundleInstaller> installerProxyPtr_;
46 };
47
SetUpTestCase()48 void BmCommandOverlayTest::SetUpTestCase()
49 {}
50
TearDownTestCase()51 void BmCommandOverlayTest::TearDownTestCase()
52 {}
53
SetUp()54 void BmCommandOverlayTest::SetUp()
55 {
56 // reset optind to 0
57 optind = 0;
58
59 // make mock objects
60 MakeMockObjects();
61 }
62
TearDown()63 void BmCommandOverlayTest::TearDown()
64 {}
65
MakeMockObjects()66 void BmCommandOverlayTest::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 BmCommandOverlayTest::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_Overlay_0001
90 * @tc.name: ExecCommand
91 * @tc.desc: Verify the "bm dump-overlay" command.
92 */
93 HWTEST_F(BmCommandOverlayTest, Bm_Command_Overlay_0001, Function | MediumTest | Level1)
94 {
95 char *argv[] = {
96 const_cast<char*>(TOOL_NAME.c_str()),
97 const_cast<char*>(overlay_.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_OVERLAY);
108 }
109
110 /**
111 * @tc.number: Bm_Command_Overlay_0002
112 * @tc.name: ExecCommand
113 * @tc.desc: Verify the "bm dump-overlay -xxx" command.
114 */
115 HWTEST_F(BmCommandOverlayTest, Bm_Command_Overlay_0002, Function | MediumTest | Level1)
116 {
117 char *argv[] = {
118 const_cast<char*>(TOOL_NAME.c_str()),
119 const_cast<char*>(overlay_.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(), "error: unknown option.\n" + HELP_MSG_OVERLAY);
131 }
132
133 /**
134 * @tc.number: Bm_Command_Overlay_0003
135 * @tc.name: ExecCommand
136 * @tc.desc: Verify the "bm dump-overlay -b" command.
137 */
138 HWTEST_F(BmCommandOverlayTest, Bm_Command_Overlay_0003, Function | MediumTest | Level1)
139 {
140 char *argv[] = {
141 const_cast<char*>(TOOL_NAME.c_str()),
142 const_cast<char*>(overlay_.c_str()),
143 const_cast<char*>("-b"),
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(), STRING_REQUIRE_CORRECT_VALUE + HELP_MSG_OVERLAY);
154 }
155
156 /**
157 * @tc.number: Bm_Command_Overlay_0004
158 * @tc.name: ExecCommand
159 * @tc.desc: Verify the "bm dump-overlay -m" command.
160 */
161 HWTEST_F(BmCommandOverlayTest, Bm_Command_Overlay_0004, Function | MediumTest | Level1)
162 {
163 char *argv[] = {
164 const_cast<char*>(TOOL_NAME.c_str()),
165 const_cast<char*>(overlay_.c_str()),
166 const_cast<char*>("-m"),
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(), STRING_REQUIRE_CORRECT_VALUE + HELP_MSG_OVERLAY);
177 }
178
179 /**
180 * @tc.number: Bm_Command_Overlay_0005
181 * @tc.name: ExecCommand
182 * @tc.desc: Verify the "bm dump-overlay -t" command.
183 */
184 HWTEST_F(BmCommandOverlayTest, Bm_Command_Overlay_0005, Function | MediumTest | Level1)
185 {
186 char *argv[] = {
187 const_cast<char*>(TOOL_NAME.c_str()),
188 const_cast<char*>(overlay_.c_str()),
189 const_cast<char*>("-t"),
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(), STRING_REQUIRE_CORRECT_VALUE + HELP_MSG_OVERLAY);
200 }
201
202 /**
203 * @tc.number: Bm_Command_Overlay_0006
204 * @tc.name: ExecCommand
205 * @tc.desc: Verify the "bm dump-overlay -u" command.
206 */
207 HWTEST_F(BmCommandOverlayTest, Bm_Command_Overlay_0006, Function | MediumTest | Level1)
208 {
209 char *argv[] = {
210 const_cast<char*>(TOOL_NAME.c_str()),
211 const_cast<char*>(overlay_.c_str()),
212 const_cast<char*>("-u"),
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(), STRING_REQUIRE_CORRECT_VALUE + HELP_MSG_OVERLAY);
223 }
224
225 /**
226 * @tc.number: Bm_Command_Overlay_0007
227 * @tc.name: ExecCommand
228 * @tc.desc: Verify the "bm dump-overlay -h" command.
229 */
230 HWTEST_F(BmCommandOverlayTest, Bm_Command_Overlay_0007, Function | MediumTest | Level1)
231 {
232 char *argv[] = {
233 const_cast<char*>(TOOL_NAME.c_str()),
234 const_cast<char*>(overlay_.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_OVERLAY);
246 }
247
248 /**
249 * @tc.number: Bm_Command_Overlay_0008
250 * @tc.name: ExecCommand
251 * @tc.desc: Verify the "bm dump-overlay -b <bundle-name>" command.
252 */
253 HWTEST_F(BmCommandOverlayTest, Bm_Command_Overlay_0008, Function | MediumTest | Level1)
254 {
255 char *argv[] = {
256 const_cast<char*>(TOOL_NAME.c_str()),
257 const_cast<char*>(overlay_.c_str()),
258 const_cast<char*>("-b"),
259 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
260 const_cast<char*>(""),
261 };
262 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
263
264 BundleManagerShellCommand cmd(argc, argv);
265
266 // set the mock objects
267 SetMockObjects(cmd);
268
269 EXPECT_EQ(cmd.ExecCommand(), STRING_DUMP_OVERLAY_NG + "\n");
270 }
271
272 /**
273 * @tc.number: Bm_Command_Overlay_0009
274 * @tc.name: ExecCommand
275 * @tc.desc: Verify the "bm dump-overlay -m <module-name>" command.
276 */
277 HWTEST_F(BmCommandOverlayTest, Bm_Command_Overlay_0009, Function | MediumTest | Level1)
278 {
279 char *argv[] = {
280 const_cast<char*>(TOOL_NAME.c_str()),
281 const_cast<char*>(overlay_.c_str()),
282 const_cast<char*>("-m"),
283 const_cast<char*>(STRING_MODULE_NAME.c_str()),
284 const_cast<char*>(""),
285 };
286 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
287
288 BundleManagerShellCommand cmd(argc, argv);
289
290 // set the mock objects
291 SetMockObjects(cmd);
292
293 EXPECT_EQ(cmd.ExecCommand(), STRING_DUMP_OVERLAY_NG + "\n");
294 }
295
296 /**
297 * @tc.number: Bm_Command_Overlay_0010
298 * @tc.name: ExecCommand
299 * @tc.desc: Verify the "bm dump-overlay -t <target-module-name>" command.
300 */
301 HWTEST_F(BmCommandOverlayTest, Bm_Command_Overlay_0010, Function | MediumTest | Level1)
302 {
303 char *argv[] = {
304 const_cast<char*>(TOOL_NAME.c_str()),
305 const_cast<char*>(overlay_.c_str()),
306 const_cast<char*>("-t"),
307 const_cast<char*>(STRING_MODULE_NAME.c_str()),
308 const_cast<char*>(""),
309 };
310 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
311
312 BundleManagerShellCommand cmd(argc, argv);
313
314 // set the mock objects
315 SetMockObjects(cmd);
316
317 EXPECT_EQ(cmd.ExecCommand(), STRING_DUMP_OVERLAY_NG + "\n");
318 }
319
320 /**
321 * @tc.number: Bm_Command_Overlay_0017
322 * @tc.name: ExecCommand
323 * @tc.desc: Verify the "bm dump-overlay -b <bundle-name>
324 * -m <module-name> -t <target-module-name>" command.
325 */
326 HWTEST_F(BmCommandOverlayTest, Bm_Command_Overlay_0017, Function | MediumTest | Level1)
327 {
328 char *argv[] = {
329 const_cast<char*>(TOOL_NAME.c_str()),
330 const_cast<char*>(overlay_.c_str()),
331 const_cast<char*>("-b"),
332 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
333 const_cast<char*>("-m"),
334 const_cast<char*>(STRING_MODULE_NAME.c_str()),
335 const_cast<char*>("-t"),
336 const_cast<char*>(STRING_MODULE_NAME.c_str()),
337 const_cast<char*>("-u"),
338 const_cast<char*>(DEFAULT_USER_ID.c_str()),
339 const_cast<char*>(""),
340 };
341 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
342
343 BundleManagerShellCommand cmd(argc, argv);
344
345 // set the mock objects
346 SetMockObjects(cmd);
347
348 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_OVERLAY);
349 }
350
351 /**
352 * @tc.number: Bm_Command_Target_Overlay_0001
353 * @tc.name: ExecCommand
354 * @tc.desc: Verify the "bm dump-target-overlay" command.
355 */
356 HWTEST_F(BmCommandOverlayTest, Bm_Command_Target_Overlay_0001, Function | MediumTest | Level1)
357 {
358 char *argv[] = {
359 const_cast<char*>(TOOL_NAME.c_str()),
360 const_cast<char*>(tOverlay_.c_str()),
361 const_cast<char*>(""),
362 };
363 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
364
365 BundleManagerShellCommand cmd(argc, argv);
366
367 // set the mock objects
368 SetMockObjects(cmd);
369
370 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_OVERLAY_TARGET);
371 }
372
373 /**
374 * @tc.number: Bm_Command_Target_Overlay_0002
375 * @tc.name: ExecCommand
376 * @tc.desc: Verify the "bm dump-target-overlay -b" command.
377 */
378 HWTEST_F(BmCommandOverlayTest, Bm_Command_Target_Overlay_0002, Function | MediumTest | Level1)
379 {
380 char *argv[] = {
381 const_cast<char*>(TOOL_NAME.c_str()),
382 const_cast<char*>(tOverlay_.c_str()),
383 const_cast<char*>("-b"),
384 const_cast<char*>(""),
385 };
386 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
387
388 BundleManagerShellCommand cmd(argc, argv);
389
390 // set the mock objects
391 SetMockObjects(cmd);
392
393 EXPECT_EQ(cmd.ExecCommand(), STRING_REQUIRE_CORRECT_VALUE + HELP_MSG_OVERLAY_TARGET);
394 }
395
396 /**
397 * @tc.number: Bm_Command_Target_Overlay_0003
398 * @tc.name: ExecCommand
399 * @tc.desc: Verify the "bm dump-target-overlay -xxx" command.
400 */
401 HWTEST_F(BmCommandOverlayTest, Bm_Command_Target_Overlay_0003, Function | MediumTest | Level1)
402 {
403 char *argv[] = {
404 const_cast<char*>(TOOL_NAME.c_str()),
405 const_cast<char*>(tOverlay_.c_str()),
406 const_cast<char*>("-xxx"),
407 const_cast<char*>(""),
408 };
409 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
410
411 BundleManagerShellCommand cmd(argc, argv);
412
413 // set the mock objects
414 SetMockObjects(cmd);
415
416 EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_OVERLAY_TARGET);
417 }
418
419 /**
420 * @tc.number: Bm_Command_Target_Overlay_0004
421 * @tc.name: ExecCommand
422 * @tc.desc: Verify the "bm dump-target-overlay -m" command.
423 */
424 HWTEST_F(BmCommandOverlayTest, Bm_Command_Target_Overlay_0004, Function | MediumTest | Level1)
425 {
426 char *argv[] = {
427 const_cast<char*>(TOOL_NAME.c_str()),
428 const_cast<char*>(tOverlay_.c_str()),
429 const_cast<char*>("-m"),
430 const_cast<char*>(""),
431 };
432 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
433
434 BundleManagerShellCommand cmd(argc, argv);
435
436 // set the mock objects
437 SetMockObjects(cmd);
438
439 EXPECT_EQ(cmd.ExecCommand(), STRING_REQUIRE_CORRECT_VALUE + HELP_MSG_OVERLAY_TARGET);
440 }
441
442 /**
443 * @tc.number: Bm_Command_Target_Overlay_0005
444 * @tc.name: ExecCommand
445 * @tc.desc: Verify the "bm dump-target-overlay -u" command.
446 */
447 HWTEST_F(BmCommandOverlayTest, Bm_Command_Target_Overlay_0005, Function | MediumTest | Level1)
448 {
449 char *argv[] = {
450 const_cast<char*>(TOOL_NAME.c_str()),
451 const_cast<char*>(tOverlay_.c_str()),
452 const_cast<char*>("-u"),
453 const_cast<char*>(""),
454 };
455 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
456
457 BundleManagerShellCommand cmd(argc, argv);
458
459 // set the mock objects
460 SetMockObjects(cmd);
461
462 EXPECT_EQ(cmd.ExecCommand(), STRING_REQUIRE_CORRECT_VALUE + HELP_MSG_OVERLAY_TARGET);
463 }
464
465 /**
466 * @tc.number: Bm_Command_Target_Overlay_0006
467 * @tc.name: ExecCommand
468 * @tc.desc: Verify the "bm dump-target-overlay -h" command.
469 */
470 HWTEST_F(BmCommandOverlayTest, Bm_Command_Target_Overlay_0006, Function | MediumTest | Level1)
471 {
472 char *argv[] = {
473 const_cast<char*>(TOOL_NAME.c_str()),
474 const_cast<char*>(tOverlay_.c_str()),
475 const_cast<char*>("-h"),
476 const_cast<char*>(""),
477 };
478 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
479
480 BundleManagerShellCommand cmd(argc, argv);
481
482 // set the mock objects
483 SetMockObjects(cmd);
484
485 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_OVERLAY_TARGET);
486 }
487 } // namespace OHOS