• 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_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 class BmCommandUninstallTest : 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 cmd_ = "uninstall";
43     sptr<IBundleMgr> mgrProxyPtr_;
44     sptr<IBundleInstaller> installerProxyPtr_;
45 };
46 
SetUpTestCase()47 void BmCommandUninstallTest::SetUpTestCase()
48 {}
49 
TearDownTestCase()50 void BmCommandUninstallTest::TearDownTestCase()
51 {}
52 
SetUp()53 void BmCommandUninstallTest::SetUp()
54 {
55     // reset optind to 0
56     optind = 0;
57 
58     // make mock objects
59     MakeMockObjects();
60 }
61 
TearDown()62 void BmCommandUninstallTest::TearDown()
63 {}
64 
MakeMockObjects()65 void BmCommandUninstallTest::MakeMockObjects()
66 {
67     // mock a mgr host
68     auto mgrHostPtr = sptr<IRemoteObject>(new MockBundleMgrHost());
69     // mock a mgr proxy
70     mgrProxyPtr_ = iface_cast<IBundleMgr>(mgrHostPtr);
71 
72     // mock a installer host
73     auto installerHostPtr = sptr<IRemoteObject>(new MockBundleInstallerHost());
74     // mock a installer proxy
75     installerProxyPtr_ = iface_cast<IBundleInstaller>(installerHostPtr);
76 }
77 
SetMockObjects(BundleManagerShellCommand & cmd) const78 void BmCommandUninstallTest::SetMockObjects(BundleManagerShellCommand &cmd) const
79 {
80     // set the mock mgr proxy
81     cmd.bundleMgrProxy_ = mgrProxyPtr_;
82 
83     // set the mock installer proxy
84     cmd.bundleInstallerProxy_ = installerProxyPtr_;
85 }
86 
87 /**
88  * @tc.number: Bm_Command_Uninstall_0100
89  * @tc.name: ExecCommand
90  * @tc.desc: Verify the "bm uninstall" command.
91  */
92 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_0100, Function | MediumTest | Level1)
93 {
94     char *argv[] = {
95         (char *)TOOL_NAME.c_str(),
96         (char *)cmd_.c_str(),
97         (char *)"",
98     };
99     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
100 
101     BundleManagerShellCommand cmd(argc, argv);
102 
103     // set the mock objects
104     SetMockObjects(cmd);
105 
106     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_UNINSTALL);
107 }
108 
109 /**
110  * @tc.number: Bm_Command_Uninstall_0200
111  * @tc.name: ExecCommand
112  * @tc.desc: Verify the "bm uninstall xxx" command.
113  */
114 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_0200, Function | MediumTest | Level1)
115 {
116     char *argv[] = {
117         (char *)TOOL_NAME.c_str(),
118         (char *)cmd_.c_str(),
119         (char *)"xxx",
120         (char *)"",
121     };
122     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
123 
124     BundleManagerShellCommand cmd(argc, argv);
125 
126     // set the mock objects
127     SetMockObjects(cmd);
128 
129     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_UNINSTALL);
130 }
131 
132 /**
133  * @tc.number: Bm_Command_Uninstall_0300
134  * @tc.name: ExecCommand
135  * @tc.desc: Verify the "bm uninstall -x" command.
136  */
137 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_0300, Function | MediumTest | Level1)
138 {
139     char *argv[] = {
140         (char *)TOOL_NAME.c_str(),
141         (char *)cmd_.c_str(),
142         (char *)"-x",
143         (char *)"",
144     };
145     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
146 
147     BundleManagerShellCommand cmd(argc, argv);
148 
149     // set the mock objects
150     SetMockObjects(cmd);
151 
152     EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_UNINSTALL);
153 }
154 
155 /**
156  * @tc.number: Bm_Command_Uninstall_0400
157  * @tc.name: ExecCommand
158  * @tc.desc: Verify the "bm uninstall -xxx" command.
159  */
160 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_0400, Function | MediumTest | Level1)
161 {
162     char *argv[] = {
163         (char *)TOOL_NAME.c_str(),
164         (char *)cmd_.c_str(),
165         (char *)"-xxx",
166         (char *)"",
167     };
168     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
169 
170     BundleManagerShellCommand cmd(argc, argv);
171 
172     // set the mock objects
173     SetMockObjects(cmd);
174 
175     EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_UNINSTALL);
176 }
177 
178 /**
179  * @tc.number: Bm_Command_Uninstall_0500
180  * @tc.name: ExecCommand
181  * @tc.desc: Verify the "bm uninstall --x" command.
182  */
183 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_0500, Function | MediumTest | Level1)
184 {
185     char *argv[] = {
186         (char *)TOOL_NAME.c_str(),
187         (char *)cmd_.c_str(),
188         (char *)"--x",
189         (char *)"",
190     };
191     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
192 
193     BundleManagerShellCommand cmd(argc, argv);
194 
195     // set the mock objects
196     SetMockObjects(cmd);
197 
198     EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_UNINSTALL);
199 }
200 
201 /**
202  * @tc.number: Bm_Command_Uninstall_0600
203  * @tc.name: ExecCommand
204  * @tc.desc: Verify the "bm uninstall --xxx" command.
205  */
206 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_0600, Function | MediumTest | Level1)
207 {
208     char *argv[] = {
209         (char *)TOOL_NAME.c_str(),
210         (char *)cmd_.c_str(),
211         (char *)"--xxx",
212         (char *)"",
213     };
214     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
215 
216     BundleManagerShellCommand cmd(argc, argv);
217 
218     // set the mock objects
219     SetMockObjects(cmd);
220 
221     EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_UNINSTALL);
222 }
223 
224 /**
225  * @tc.number: Bm_Command_Uninstall_0700
226  * @tc.name: ExecCommand
227  * @tc.desc: Verify the "bm uninstall --h" command.
228  */
229 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_0700, Function | MediumTest | Level1)
230 {
231     char *argv[] = {
232         (char *)TOOL_NAME.c_str(),
233         (char *)cmd_.c_str(),
234         (char *)"-h",
235         (char *)"",
236     };
237     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
238 
239     BundleManagerShellCommand cmd(argc, argv);
240 
241     // set the mock objects
242     SetMockObjects(cmd);
243 
244     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNINSTALL);
245 }
246 
247 /**
248  * @tc.number: Bm_Command_Uninstall_0800
249  * @tc.name: ExecCommand
250  * @tc.desc: Verify the "bm uninstall --help" command.
251  */
252 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_0800, Function | MediumTest | Level1)
253 {
254     char *argv[] = {
255         (char *)TOOL_NAME.c_str(),
256         (char *)cmd_.c_str(),
257         (char *)"--help",
258         (char *)"",
259     };
260     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
261 
262     BundleManagerShellCommand cmd(argc, argv);
263 
264     // set the mock objects
265     SetMockObjects(cmd);
266 
267     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_UNINSTALL);
268 }
269 
270 /**
271  * @tc.number: Bm_Command_Uninstall_0900
272  * @tc.name: ExecCommand
273  * @tc.desc: Verify the "bm uninstall -n" command.
274  */
275 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_0900, Function | MediumTest | Level1)
276 {
277     char *argv[] = {
278         (char *)TOOL_NAME.c_str(),
279         (char *)cmd_.c_str(),
280         (char *)"-n",
281         (char *)"",
282     };
283     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
284 
285     BundleManagerShellCommand cmd(argc, argv);
286 
287     // set the mock objects
288     SetMockObjects(cmd);
289 
290     EXPECT_EQ(cmd.ExecCommand(), "error: option requires a value.\n" + HELP_MSG_UNINSTALL);
291 }
292 
293 /**
294  * @tc.number: Bm_Command_Uninstall_1300
295  * @tc.name: ExecCommand
296  * @tc.desc: Verify the "bm uninstall -n <bundle-name>" command.
297  */
298 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_1000, Function | MediumTest | Level1)
299 {
300     char *argv[] = {
301         (char *)TOOL_NAME.c_str(),
302         (char *)cmd_.c_str(),
303         (char *)"-n",
304         (char *)STRING_BUNDLE_NAME.c_str(),
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(), STRING_UNINSTALL_BUNDLE_OK + "\n");
315 }
316 
317 /**
318  * @tc.number: Bm_Command_Uninstall_1100
319  * @tc.name: ExecCommand
320  * @tc.desc: Verify the "bm uninstall -n <bundle-name> -m" command.
321  */
322 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_1100, Function | MediumTest | Level1)
323 {
324     char *argv[] = {
325         (char *)TOOL_NAME.c_str(),
326         (char *)cmd_.c_str(),
327         (char *)"-n",
328         (char *)STRING_BUNDLE_NAME.c_str(),
329         (char *)"-m",
330         (char *)"",
331     };
332     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
333 
334     BundleManagerShellCommand cmd(argc, argv);
335 
336     // set the mock objects
337     SetMockObjects(cmd);
338 
339     EXPECT_EQ(cmd.ExecCommand(), "error: option requires a value.\n" + HELP_MSG_UNINSTALL);
340 }
341 
342 /**
343  * @tc.number: Bm_Command_Uninstall_1200
344  * @tc.name: ExecCommand
345  * @tc.desc: Verify the "bm uninstall -m <module-name>" command.
346  */
347 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_1200, Function | MediumTest | Level1)
348 {
349     char *argv[] = {
350         (char *)TOOL_NAME.c_str(),
351         (char *)cmd_.c_str(),
352         (char *)"-m",
353         (char *)STRING_MODULE_NAME.c_str(),
354         (char *)"",
355     };
356     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
357 
358     BundleManagerShellCommand cmd(argc, argv);
359 
360     // set the mock objects
361     SetMockObjects(cmd);
362 
363     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n" + HELP_MSG_UNINSTALL);
364 }
365 
366 /**
367  * @tc.number: Bm_Command_Uninstall_1300
368  * @tc.name: ExecCommand
369  * @tc.desc: Verify the "bm uninstall -n <bundle-name> -m <module-name>" command.
370  */
371 HWTEST_F(BmCommandUninstallTest, Bm_Command_Uninstall_1300, Function | MediumTest | Level1)
372 {
373     char *argv[] = {
374         (char *)TOOL_NAME.c_str(),
375         (char *)cmd_.c_str(),
376         (char *)"-n",
377         (char *)STRING_BUNDLE_NAME.c_str(),
378         (char *)"-m",
379         (char *)STRING_MODULE_NAME.c_str(),
380         (char *)"",
381     };
382     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
383 
384     BundleManagerShellCommand cmd(argc, argv);
385 
386     // set the mock objects
387     SetMockObjects(cmd);
388 
389     EXPECT_EQ(cmd.ExecCommand(), STRING_UNINSTALL_BUNDLE_OK + "\n");
390 }
391