• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         (char *)TOOL_NAME.c_str(),
97         (char *)cmd_.c_str(),
98         (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         (char *)TOOL_NAME.c_str(),
119         (char *)cmd_.c_str(),
120         (char *)"xxx",
121         (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         (char *)TOOL_NAME.c_str(),
142         (char *)cmd_.c_str(),
143         (char *)"-x",
144         (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         (char *)TOOL_NAME.c_str(),
165         (char *)cmd_.c_str(),
166         (char *)"-xxx",
167         (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         (char *)TOOL_NAME.c_str(),
188         (char *)cmd_.c_str(),
189         (char *)"--x",
190         (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         (char *)TOOL_NAME.c_str(),
211         (char *)cmd_.c_str(),
212         (char *)"--xxx",
213         (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         (char *)TOOL_NAME.c_str(),
234         (char *)cmd_.c_str(),
235         (char *)"-h",
236         (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         (char *)TOOL_NAME.c_str(),
257         (char *)cmd_.c_str(),
258         (char *)"--help",
259         (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         (char *)TOOL_NAME.c_str(),
280         (char *)cmd_.c_str(),
281         (char *)"-a",
282         (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         (char *)TOOL_NAME.c_str(),
303         (char *)cmd_.c_str(),
304         (char *)"--all",
305         (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         (char *)TOOL_NAME.c_str(),
326         (char *)cmd_.c_str(),
327         (char *)"-n",
328         (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(), "error: option requires a value.\n" + 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         (char *)TOOL_NAME.c_str(),
349         (char *)cmd_.c_str(),
350         (char *)"-n",
351         (char *)STRING_BUNDLE_NAME.c_str(),
352         (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         (char *)TOOL_NAME.c_str(),
374         (char *)cmd_.c_str(),
375         (char *)"-s",
376         (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         (char *)TOOL_NAME.c_str(),
395         (char *)cmd_.c_str(),
396         (char *)"-n",
397         (char *)STRING_BUNDLE_NAME.c_str(),
398         (char *)"-s",
399         (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 } // namespace OHOS