• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 protected public
19 #include "bundle_command.h"
20 #include "quick_fix_command.h"
21 #undef protected
22 
23 using namespace testing::ext;
24 using namespace OHOS;
25 using namespace OHOS::AppExecFwk;
26 const int QUICK_FIX_COPY_FILES_FAILED = 4;
27 const int QUICK_FIX_GET_BUNDLE_INFO_FAILED = 8;
28 const int QUICK_FIX_INVALID_VALUE = 22;
29 const int QUICK_FIX_OK = 0;
30 
31 namespace OHOS {
32 namespace AppExecFwk {
33 class BmCommandQuickFixTest : public testing::Test {
34 public:
35     static void SetUpTestCase();
36     static void TearDownTestCase();
37     void SetUp();
38     void TearDown();
39 
40     std::string cmd_ = "quickfix";
41 };
42 
SetUpTestCase()43 void BmCommandQuickFixTest::SetUpTestCase()
44 {}
45 
TearDownTestCase()46 void BmCommandQuickFixTest::TearDownTestCase()
47 {}
48 
SetUp()49 void BmCommandQuickFixTest::SetUp()
50 {
51     // reset optind to 0
52     optind = 0;
53 }
54 
TearDown()55 void BmCommandQuickFixTest::TearDown()
56 {}
57 
58 /**
59  * @tc.name: Bm_Command_QuickFix_0100
60  * @tc.desc: "bm quickfix" test.
61  * @tc.type: FUNC
62  * @tc.require: issueI5OCZV
63  */
64 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_0100, TestSize.Level1)
65 {
66     char *argv[] = {
67         const_cast<char*>(TOOL_NAME.c_str()),
68         const_cast<char*>(cmd_.c_str()),
69         const_cast<char*>(""),
70     };
71     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
72 
73     BundleManagerShellCommand cmd(argc, argv);
74 
75     EXPECT_EQ(cmd.ExecCommand(), "error: parameter is not enough.\n" + HELP_MSG_QUICK_FIX);
76 }
77 
78 /**
79  * @tc.name: Bm_Command_QuickFix_0200
80  * @tc.desc: "bm quickfix --invalid" test.
81  * @tc.type: FUNC
82  * @tc.require: issueI5OCZV
83  */
84 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_0200, TestSize.Level1)
85 {
86     char *argv[] = {
87         const_cast<char*>(TOOL_NAME.c_str()),
88         const_cast<char*>(cmd_.c_str()),
89         const_cast<char*>("--invalid"),
90         const_cast<char*>(""),
91     };
92     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
93 
94     BundleManagerShellCommand cmd(argc, argv);
95 
96     EXPECT_EQ(cmd.ExecCommand(), "error: unknown option.\n" + HELP_MSG_QUICK_FIX);
97 }
98 
99 /**
100  * @tc.name: Bm_Command_QuickFix_Help_0100
101  * @tc.desc: "bm quickfix -h" test.
102  * @tc.type: FUNC
103  * @tc.require: issueI5OCZV
104  */
105 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_Help_0100, TestSize.Level1)
106 {
107     char *argv[] = {
108         const_cast<char*>(TOOL_NAME.c_str()),
109         const_cast<char*>(cmd_.c_str()),
110         const_cast<char*>("-h"),
111         const_cast<char*>(""),
112     };
113     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
114 
115     BundleManagerShellCommand cmd(argc, argv);
116 
117     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_QUICK_FIX);
118 }
119 
120 /**
121  * @tc.name: Bm_Command_QuickFix_Help_0200
122  * @tc.desc: "bm quickfix --help" test.
123  * @tc.type: FUNC
124  * @tc.require: issueI5OCZV
125  */
126 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_Help_0200, TestSize.Level1)
127 {
128     char *argv[] = {
129         const_cast<char*>(TOOL_NAME.c_str()),
130         const_cast<char*>(cmd_.c_str()),
131         const_cast<char*>("--help"),
132         const_cast<char*>(""),
133     };
134     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
135 
136     BundleManagerShellCommand cmd(argc, argv);
137 
138     EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_QUICK_FIX);
139 }
140 
141 /**
142  * @tc.name: Bm_Command_QuickFix_Apply_0100
143  * @tc.desc: "bm quickfix -a" test.
144  * @tc.type: FUNC
145  * @tc.require: issueI5OCZV
146  */
147 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_Apply_0100, TestSize.Level1)
148 {
149     char *argv[] = {
150         const_cast<char*>(TOOL_NAME.c_str()),
151         const_cast<char*>(cmd_.c_str()),
152         const_cast<char*>("-a"),
153         const_cast<char*>(""),
154     };
155     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
156 
157     BundleManagerShellCommand cmd(argc, argv);
158 
159     EXPECT_EQ(cmd.ExecCommand(), "error: option [--apply] is incorrect.\n" + HELP_MSG_QUICK_FIX);
160 }
161 
162 /**
163  * @tc.name: Bm_Command_QuickFix_Apply_0200
164  * @tc.desc: "bm quickfix --apply" test.
165  * @tc.type: FUNC
166  * @tc.require: issueI5OCZV
167  */
168 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_Apply_0200, TestSize.Level1)
169 {
170     char *argv[] = {
171         const_cast<char*>(TOOL_NAME.c_str()),
172         const_cast<char*>(cmd_.c_str()),
173         const_cast<char*>("--apply"),
174         const_cast<char*>(""),
175     };
176     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
177 
178     BundleManagerShellCommand cmd(argc, argv);
179 
180     EXPECT_EQ(cmd.ExecCommand(), "error: option [--apply] is incorrect.\n" + HELP_MSG_QUICK_FIX);
181 }
182 
183 /**
184  * @tc.name: Bm_Command_QuickFix_Apply_0300
185  * @tc.desc: "bm quickfix --apply --invalidKey" test.
186  * @tc.type: FUNC
187  * @tc.require: issueI5OCZV
188  */
189 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_Apply_0300, TestSize.Level1)
190 {
191     char *argv[] = {
192         const_cast<char*>(TOOL_NAME.c_str()),
193         const_cast<char*>(cmd_.c_str()),
194         const_cast<char*>("--apply"),
195         const_cast<char*>("--invalidKey"),
196         const_cast<char*>(""),
197     };
198     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
199 
200     BundleManagerShellCommand cmd(argc, argv);
201 
202     EXPECT_EQ(cmd.ExecCommand(), "error: option [--apply] is incorrect.\n" + HELP_MSG_QUICK_FIX);
203 }
204 
205 /**
206  * @tc.name: Bm_Command_QuickFix_Apply_0400
207  * @tc.desc: "bm quickfix --apply --invalidKey invalidValue" test.
208  * @tc.type: FUNC
209  * @tc.require: issueI5OCZV
210  */
211 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_Apply_0400, TestSize.Level1)
212 {
213     char *argv[] = {
214         const_cast<char*>(TOOL_NAME.c_str()),
215         const_cast<char*>(cmd_.c_str()),
216         const_cast<char*>("--apply"),
217         const_cast<char*>("--invalidKey"),
218         const_cast<char*>("invalidValue"),
219         const_cast<char*>(""),
220     };
221     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
222 
223     BundleManagerShellCommand cmd(argc, argv);
224 
225     EXPECT_EQ(cmd.ExecCommand(), "error: parameter is not enough.\n" + HELP_MSG_QUICK_FIX);
226 }
227 
228 /**
229  * @tc.name: Bm_Command_QuickFix_Apply_0500
230  * @tc.desc: "bm quickfix --apply -f <file-path>" test.
231  * @tc.type: FUNC
232  * @tc.require: issueI5OCZV
233  */
234 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_Apply_0500, TestSize.Level1)
235 {
236     char *argv[] = {
237         const_cast<char*>(TOOL_NAME.c_str()),
238         const_cast<char*>(cmd_.c_str()),
239         const_cast<char*>("--apply"),
240         const_cast<char*>("-f"),
241         const_cast<char*>("/data/storage/el1/aa.hqf"),
242         const_cast<char*>(""),
243     };
244     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
245 
246     BundleManagerShellCommand cmd(argc, argv);
247 
248     EXPECT_EQ(cmd.ExecCommand(), "apply quickfix failed with errno: 4.\n");
249 }
250 
251 /**
252  * @tc.name: Bm_Command_QuickFix_Apply_0600
253  * @tc.desc: "bm quickfix --apply --file-path <file-path> <file-path>" test.
254  * @tc.type: FUNC
255  * @tc.require: issueI5OCZV
256  */
257 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_Apply_0600, TestSize.Level1)
258 {
259     char *argv[] = {
260         const_cast<char*>(TOOL_NAME.c_str()),
261         const_cast<char*>(cmd_.c_str()),
262         const_cast<char*>("--apply"),
263         const_cast<char*>("--file-path"),
264         const_cast<char*>("/data/storage/el1/aa.hqf"),
265         const_cast<char*>("/data/storage/el2/bb.hqf"),
266         const_cast<char*>(""),
267     };
268     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
269 
270     BundleManagerShellCommand cmd(argc, argv);
271 
272     EXPECT_EQ(cmd.ExecCommand(), "apply quickfix failed with errno: 4.\n");
273 }
274 
275 /**
276  * @tc.name: Bm_Command_QuickFix_Apply_0700
277  * @tc.desc: "bm quickfix --apply --file-path <bundle-direction>" test.
278  * @tc.type: FUNC
279  * @tc.require: issueI5OCZV
280  */
281 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_Apply_0700, TestSize.Level1)
282 {
283     char *argv[] = {
284         const_cast<char*>(TOOL_NAME.c_str()),
285         const_cast<char*>(cmd_.c_str()),
286         const_cast<char*>("--apply"),
287         const_cast<char*>("--file-path"),
288         const_cast<char*>("/data/storage/el1"),
289         const_cast<char*>(""),
290     };
291     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
292 
293     BundleManagerShellCommand cmd(argc, argv);
294 
295     EXPECT_EQ(cmd.ExecCommand(), "apply quickfix failed with errno: 4.\n");
296 }
297 
298 /**
299  * @tc.name: Bm_Command_QuickFix_Query_0100
300  * @tc.desc: "bm quickfix -q" test.
301  * @tc.type: FUNC
302  * @tc.require: issueI5OCZV
303  */
304 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_Query_0100, TestSize.Level1)
305 {
306     char *argv[] = {
307         const_cast<char*>(TOOL_NAME.c_str()),
308         const_cast<char*>(cmd_.c_str()),
309         const_cast<char*>("-q"),
310         const_cast<char*>(""),
311     };
312     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
313 
314     BundleManagerShellCommand cmd(argc, argv);
315 
316     EXPECT_EQ(cmd.ExecCommand(), "error: option [--query] is incorrect.\n" + HELP_MSG_QUICK_FIX);
317 }
318 
319 /**
320  * @tc.name: Bm_Command_QuickFix_Query_0200
321  * @tc.desc: "bm quickfix --query" test.
322  * @tc.type: FUNC
323  * @tc.require: issueI5OCZV
324  */
325 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_Query_0200, TestSize.Level1)
326 {
327     char *argv[] = {
328         const_cast<char*>(TOOL_NAME.c_str()),
329         const_cast<char*>(cmd_.c_str()),
330         const_cast<char*>("--query"),
331         const_cast<char*>(""),
332     };
333     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
334 
335     BundleManagerShellCommand cmd(argc, argv);
336 
337     EXPECT_EQ(cmd.ExecCommand(), "error: option [--query] is incorrect.\n" + HELP_MSG_QUICK_FIX);
338 }
339 
340 /**
341  * @tc.name: Bm_Command_QuickFix_Query_0300
342  * @tc.desc: "bm quickfix -q --invalidKey" test.
343  * @tc.type: FUNC
344  * @tc.require: issueI5OCZV
345  */
346 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_Query_0300, TestSize.Level1)
347 {
348     char *argv[] = {
349         const_cast<char*>(TOOL_NAME.c_str()),
350         const_cast<char*>(cmd_.c_str()),
351         const_cast<char*>("-q"),
352         const_cast<char*>("--invalidKey"),
353         const_cast<char*>(""),
354     };
355     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
356 
357     BundleManagerShellCommand cmd(argc, argv);
358 
359     EXPECT_EQ(cmd.ExecCommand(), "error: option [--query] is incorrect.\n" + HELP_MSG_QUICK_FIX);
360 }
361 
362 /**
363  * @tc.name: Bm_Command_QuickFix_Query_0400
364  * @tc.desc: "bm quickfix --query --invalidKey invalidValue" test.
365  * @tc.type: FUNC
366  * @tc.require: issueI5OCZV
367  */
368 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_Query_0400, TestSize.Level1)
369 {
370     char *argv[] = {
371         const_cast<char*>(TOOL_NAME.c_str()),
372         const_cast<char*>(cmd_.c_str()),
373         const_cast<char*>("--query"),
374         const_cast<char*>("--invalidKey"),
375         const_cast<char*>("invalidValue"),
376         const_cast<char*>(""),
377     };
378     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
379 
380     BundleManagerShellCommand cmd(argc, argv);
381 
382     EXPECT_EQ(cmd.ExecCommand(), "bundle name is empty.\n");
383 }
384 
385 /**
386  * @tc.name: Bm_Command_QuickFix_Query_0500
387  * @tc.desc: "bm quickfix --query -b <bundle-name>" test.
388  * @tc.type: FUNC
389  * @tc.require: issueI5OCZV
390  */
391 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_Query_0500, TestSize.Level1)
392 {
393     char *argv[] = {
394         const_cast<char*>(TOOL_NAME.c_str()),
395         const_cast<char*>(cmd_.c_str()),
396         const_cast<char*>("--query"),
397         const_cast<char*>("-b"),
398         const_cast<char*>("bundleName1"),
399         const_cast<char*>(""),
400     };
401     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
402 
403     BundleManagerShellCommand cmd(argc, argv);
404 
405     EXPECT_EQ(cmd.ExecCommand(), "Get quick fix info failed with errno 8.\n");
406 }
407 
408 /**
409  * @tc.name: Bm_Command_QuickFix_Query_0600
410  * @tc.desc: "bm quickfix --query --bundle-name <bundle-name>" test.
411  * @tc.type: FUNC
412  * @tc.require: issueI5OCZV
413  */
414 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_Query_0600, TestSize.Level1)
415 {
416     char *argv[] = {
417         const_cast<char*>(TOOL_NAME.c_str()),
418         const_cast<char*>(cmd_.c_str()),
419         const_cast<char*>("--query"),
420         const_cast<char*>("--bundle-name"),
421         const_cast<char*>("bundleName1"),
422         const_cast<char*>(""),
423     };
424     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
425 
426     BundleManagerShellCommand cmd(argc, argv);
427 
428     EXPECT_EQ(cmd.ExecCommand(), "Get quick fix info failed with errno 8.\n");
429 }
430 
431 /**
432  * @tc.name: Bm_Command_QuickFix_Query_0700
433  * @tc.desc: Test quickFixFiles is empty
434  * @tc.type: FUNC
435  * @tc.require: issueI5OCZV
436  */
437 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_Query_0700, TestSize.Level1)
438 {
439     QuickFixCommand command;
440     std::vector<std::string> quickFixFiles;
441     std::string resultInfo;
442     auto ret = command.ApplyQuickFix(quickFixFiles, resultInfo);
443     EXPECT_EQ(resultInfo, "quick fix file is empty.\n");
444     EXPECT_EQ(ret, QUICK_FIX_INVALID_VALUE);
445 }
446 
447 /**
448  * @tc.name: Bm_Command_QuickFix_Query_0800
449  * @tc.desc: Test dir not have hqf file
450  * @tc.type: FUNC
451  * @tc.require: issueI5OCZV
452  */
453 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_Query_0800, TestSize.Level1)
454 {
455     QuickFixCommand command;
456     std::vector<std::string> quickFixFiles = { "/data/null" };
457     std::string resultInfo;
458     auto ret = command.ApplyQuickFix(quickFixFiles, resultInfo);
459     EXPECT_EQ(resultInfo, "apply quickfix failed with errno: 4.\n");
460     EXPECT_EQ(ret, QUICK_FIX_COPY_FILES_FAILED);
461 }
462 
463 /**
464  * @tc.name: Bm_Command_QuickFix_Query_0900
465  * @tc.desc: Test empty bundleName
466  * @tc.type: FUNC
467  * @tc.require: issueI5OCZV
468  */
469 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_Query_0900, TestSize.Level1)
470 {
471     QuickFixCommand command;
472     std::string bundleName;
473     std::string resultInfo;
474     auto ret = command.GetApplyedQuickFixInfo(bundleName, resultInfo);
475     EXPECT_EQ(resultInfo, "bundle name is empty.\n");
476     EXPECT_EQ(ret, QUICK_FIX_INVALID_VALUE);
477 }
478 
479 /**
480  * @tc.name: Bm_Command_QuickFix_Query_1000
481  * @tc.desc: Test right bundleName
482  * @tc.type: FUNC
483  * @tc.require: issueI5OCZV
484  */
485 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_Query_1000, TestSize.Level1)
486 {
487     QuickFixCommand command;
488     std::string bundleName = "ohos.global.systemres";
489     std::string resultInfo;
490     auto ret = command.GetApplyedQuickFixInfo(bundleName, resultInfo);
491     EXPECT_EQ(ret, QUICK_FIX_OK);
492 }
493 
494 /**
495  * @tc.name: Bm_Command_QuickFix_Query_1100
496  * @tc.desc: Test wrong bundleName
497  * @tc.type: FUNC
498  * @tc.require: issueI5OCZV
499  */
500 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_Query_1100, TestSize.Level1)
501 {
502     QuickFixCommand command;
503     std::string bundleName = "wrong";
504     std::string resultInfo;
505     auto ret = command.GetApplyedQuickFixInfo(bundleName, resultInfo);
506     EXPECT_EQ(ret, QUICK_FIX_GET_BUNDLE_INFO_FAILED);
507 }
508 
509 /**
510  * @tc.name: Bm_Command_QuickFix_Query_1200
511  * @tc.desc: Test GetQuickFixInfoString with different type
512  * @tc.type: FUNC
513  * @tc.require: issueI5OCZV
514  */
515 HWTEST_F(BmCommandQuickFixTest, Bm_Command_QuickFix_Query_1200, TestSize.Level1)
516 {
517     QuickFixCommand command;
518     AAFwk::ApplicationQuickFixInfo quickFixInfo;
519     std::vector<HqfInfo> hqfInfos;
520     HqfInfo hq1;
521     hq1.moduleName = "step1";
522     hq1.hapSha256 = "step2";
523     hq1.hqfFilePath = "step3";
524     hqfInfos.emplace_back(hq1);
525     quickFixInfo.appqfInfo.hqfInfos = hqfInfos;
526     quickFixInfo.appqfInfo.type = AppExecFwk::QuickFixType::PATCH;
527     auto ret = command.GetQuickFixInfoString(quickFixInfo);
528     EXPECT_EQ(ret, "ApplicationQuickFixInfo:\n  bundle name: \n  bundle version code: 0\n  "
529         "bundle version name: \n  patch version code: 0\n  patch version name: \n  "
530         "cpu abi: \n  native library path: \n  type: patch\n  ModuelQuickFixInfo:\n    "
531         "module name: step1\n    module sha256: step2\n    "
532         "file path: step3\n");
533     quickFixInfo.appqfInfo.type = AppExecFwk::QuickFixType::HOT_RELOAD;
534     ret = command.GetQuickFixInfoString(quickFixInfo);
535     EXPECT_EQ(ret, "ApplicationQuickFixInfo:\n  bundle name: \n  bundle version code: 0\n  "
536         "bundle version name: \n  patch version code: 0\n  patch version name: \n  "
537         "cpu abi: \n  native library path: \n  type: hotreload\n  ModuelQuickFixInfo:\n    "
538         "module name: step1\n    module sha256: step2\n    "
539         "file path: step3\n");
540 }
541 } // namespace AAFwk
542 } // namespace OHOS
543