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 BmCommandDumpDependenciesTest : 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-dependencies";
44 sptr<IBundleMgr> mgrProxyPtr_;
45 sptr<IBundleInstaller> installerProxyPtr_;
46 };
47
SetUpTestCase()48 void BmCommandDumpDependenciesTest::SetUpTestCase()
49 {}
50
TearDownTestCase()51 void BmCommandDumpDependenciesTest::TearDownTestCase()
52 {}
53
SetUp()54 void BmCommandDumpDependenciesTest::SetUp()
55 {
56 // reset optind to 0
57 optind = 0;
58
59 // make mock objects
60 MakeMockObjects();
61 }
62
TearDown()63 void BmCommandDumpDependenciesTest::TearDown()
64 {}
65
MakeMockObjects()66 void BmCommandDumpDependenciesTest::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 BmCommandDumpDependenciesTest::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_DumpDependencies_0100
90 * @tc.name: ExecCommand
91 * @tc.desc: Verify the "bm dump-dependencies" command.
92 */
93 HWTEST_F(BmCommandDumpDependenciesTest, Bm_Command_Dump_DumpDependencies_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_SHARED_DEPENDENCIES);
108 }
109
110 /**
111 * @tc.number: Bm_Command_Dump_DumpDependencies_0200
112 * @tc.name: ExecCommand
113 * @tc.desc: Verify the "bm dump-dependencies xxx" command.
114 */
115 HWTEST_F(BmCommandDumpDependenciesTest, Bm_Command_Dump_DumpDependencies_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_SHARED_DEPENDENCIES);
131 }
132
133 /**
134 * @tc.number: Bm_Command_Dump_DumpDependencies_0300
135 * @tc.name: ExecCommand
136 * @tc.desc: Verify the "bm dump-dependencies -x" command.
137 */
138 HWTEST_F(BmCommandDumpDependenciesTest, Bm_Command_Dump_DumpDependencies_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_SHARED_DEPENDENCIES);
154 }
155
156 /**
157 * @tc.number: Bm_Command_Dump_DumpDependencies_0400
158 * @tc.name: ExecCommand
159 * @tc.desc: Verify the "bm dump-dependencies -xxx" command.
160 */
161 HWTEST_F(BmCommandDumpDependenciesTest, Bm_Command_Dump_DumpDependencies_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_SHARED_DEPENDENCIES);
177 }
178
179 /**
180 * @tc.number: Bm_Command_Dump_DumpDependencies_0500
181 * @tc.name: ExecCommand
182 * @tc.desc: Verify the "bm dump-dependencies --x" command.
183 */
184 HWTEST_F(BmCommandDumpDependenciesTest, Bm_Command_Dump_DumpDependencies_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_SHARED_DEPENDENCIES);
200 }
201
202 /**
203 * @tc.number: Bm_Command_Dump_DumpDependencies_0600
204 * @tc.name: ExecCommand
205 * @tc.desc: Verify the "bm dump-dependencies --xxx" command.
206 */
207 HWTEST_F(BmCommandDumpDependenciesTest, Bm_Command_Dump_DumpDependencies_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_SHARED_DEPENDENCIES);
223 }
224
225 /**
226 * @tc.number: Bm_Command_Dump_DumpDependencies_0700
227 * @tc.name: ExecCommand
228 * @tc.desc: Verify the "bm dump-dependencies -h" command.
229 */
230 HWTEST_F(BmCommandDumpDependenciesTest, Bm_Command_Dump_DumpDependencies_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_SHARED_DEPENDENCIES);
246 }
247
248 /**
249 * @tc.number: Bm_Command_Dump_DumpDependencies_0800
250 * @tc.name: ExecCommand
251 * @tc.desc: Verify the "bm dump-dependencies --help" command.
252 */
253 HWTEST_F(BmCommandDumpDependenciesTest, Bm_Command_Dump_DumpDependencies_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_SHARED_DEPENDENCIES);
269 }
270
271 /**
272 * @tc.number: Bm_Command_Dump_DumpDependencies_0900
273 * @tc.name: ExecCommand
274 * @tc.desc: Verify the "bm dump-dependencies -n" command.
275 */
276 HWTEST_F(BmCommandDumpDependenciesTest, Bm_Command_Dump_DumpDependencies_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_DUMP_SHARED_DEPENDENCIES);
292 }
293
294 /**
295 * @tc.number: Bm_Command_Dump_DumpDependencies_1000
296 * @tc.name: ExecCommand
297 * @tc.desc: Verify the "bm dump-dependencies -n <bundle-name>" command.
298 */
299 HWTEST_F(BmCommandDumpDependenciesTest, Bm_Command_Dump_DumpDependencies_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(), HELP_MSG_NO_REMOVABLE_OPTION + HELP_MSG_DUMP_SHARED_DEPENDENCIES);
316 }
317
318 /**
319 * @tc.number: Bm_Command_Dump_DumpDependencies_1100
320 * @tc.name: ExecCommand
321 * @tc.desc: Verify the "bm dump-dependencies -m" command.
322 */
323 HWTEST_F(BmCommandDumpDependenciesTest, Bm_Command_Dump_DumpDependencies_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*>("-m"),
329 const_cast<char*>(""),
330 };
331 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
332 BundleManagerShellCommand cmd(argc, argv);
333 // set the mock objects
334 SetMockObjects(cmd);
335 EXPECT_EQ(cmd.ExecCommand(), STRING_REQUIRE_CORRECT_VALUE + HELP_MSG_DUMP_SHARED_DEPENDENCIES);
336 }
337
338 /**
339 * @tc.number: Bm_Command_Dump_DumpDependencies_1200
340 * @tc.name: ExecCommand
341 * @tc.desc: Verify the "bm dump-dependencies -m <module-name>" command.
342 */
343 HWTEST_F(BmCommandDumpDependenciesTest, Bm_Command_Dump_DumpDependencies_1200, Function | MediumTest | Level1)
344 {
345 char *argv[] = {
346 const_cast<char*>(TOOL_NAME.c_str()),
347 const_cast<char*>(cmd_.c_str()),
348 const_cast<char*>("-m"),
349 const_cast<char*>(STRING_MODULE_NAME.c_str()),
350 const_cast<char*>(""),
351 };
352 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
353 BundleManagerShellCommand cmd(argc, argv);
354 // set the mock objects
355 SetMockObjects(cmd);
356 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_REMOVABLE_OPTION + HELP_MSG_DUMP_SHARED_DEPENDENCIES);
357 }
358
359 /**
360 * @tc.number: Bm_Command_Dump_DumpDependencies_1300
361 * @tc.name: ExecCommand
362 * @tc.desc: Verify the "bm dump-dependencies -n <bundle-name> -m <module-name>" command.
363 */
364 HWTEST_F(BmCommandDumpDependenciesTest, Bm_Command_Dump_DumpDependencies_1300, Function | MediumTest | Level1)
365 {
366 char *argv[] = {
367 const_cast<char*>(TOOL_NAME.c_str()),
368 const_cast<char*>(cmd_.c_str()),
369 const_cast<char*>("-n"),
370 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
371 const_cast<char*>("-m"),
372 const_cast<char*>(STRING_MODULE_NAME.c_str()),
373 const_cast<char*>(""),
374 };
375 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
376
377 BundleManagerShellCommand cmd(argc, argv);
378
379 // set the mock objects
380 SetMockObjects(cmd);
381
382 EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_DUMP_FAILED + "\n");
383 }
384
385 /**
386 * @tc.number: Bm_Command_Dump_DumpDependencies_1400
387 * @tc.name: ExecCommand
388 * @tc.desc: Verify the "bm dump-dependencies -n <bundle-name> -XXX <module-name>" command.
389 */
390 HWTEST_F(BmCommandDumpDependenciesTest, Bm_Command_Dump_DumpDependencies_1400, Function | MediumTest | Level1)
391 {
392 char *argv[] = {
393 const_cast<char*>(TOOL_NAME.c_str()),
394 const_cast<char*>(cmd_.c_str()),
395 const_cast<char*>("-n"),
396 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
397 const_cast<char*>("-XXX"),
398 const_cast<char*>(STRING_MODULE_NAME.c_str()),
399 const_cast<char*>(""),
400 };
401 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
402
403 BundleManagerShellCommand cmd(argc, argv);
404
405 // set the mock objects
406 SetMockObjects(cmd);
407
408 EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_DUMP_SHARED_DEPENDENCIES);
409 }
410
411 /**
412 * @tc.number: Bm_Command_Dump_DumpDependencies_1500
413 * @tc.name: ExecCommand
414 * @tc.desc: Verify the "bm dump-dependencies -xxx <bundle-name>" command.
415 */
416 HWTEST_F(BmCommandDumpDependenciesTest, Bm_Command_Dump_DumpDependencies_1500, Function | MediumTest | Level1)
417 {
418 char *argv[] = {
419 const_cast<char*>(TOOL_NAME.c_str()),
420 const_cast<char*>(cmd_.c_str()),
421 const_cast<char*>("-xxx"),
422 const_cast<char*>(STRING_BUNDLE_NAME.c_str()),
423 const_cast<char*>(""),
424 };
425 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
426
427 BundleManagerShellCommand cmd(argc, argv);
428
429 // set the mock objects
430 SetMockObjects(cmd);
431
432 EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_DUMP_SHARED_DEPENDENCIES);
433 }
434 } // namespace OHOS