• 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;
30 using namespace OHOS::AAFwk;
31 using namespace OHOS::AppExecFwk;
32 
33 class BmCommandDumpModuleTest : 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 BmCommandDumpModuleTest::SetUpTestCase()
49 {}
50 
TearDownTestCase()51 void BmCommandDumpModuleTest::TearDownTestCase()
52 {}
53 
SetUp()54 void BmCommandDumpModuleTest::SetUp()
55 {
56     // reset optind to 0
57     optind = 0;
58 
59     // make mock objects
60     MakeMockObjects();
61 }
62 
TearDown()63 void BmCommandDumpModuleTest::TearDown()
64 {}
65 
MakeMockObjects()66 void BmCommandDumpModuleTest::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 BmCommandDumpModuleTest::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_ModuleTest_0100
90  * @tc.name: ExecCommand
91  * @tc.desc: Verify the "bm dump -a" command.
92  */
93 HWTEST_F(BmCommandDumpModuleTest, Bm_Command_Dump_ModuleTest_0100, Function | MediumTest | Level1)
94 {
95     // install a bundle
96     char *argv[] = {
97         const_cast<char *>(TOOL_NAME.c_str()),
98         const_cast<char *>(cmd_.c_str()),
99         const_cast<char *>("-a"),
100         const_cast<char *>(""),
101     };
102     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
103 
104     BundleManagerShellCommand cmd(argc, argv);
105 
106     // set the mock objects
107     SetMockObjects(cmd);
108 
109     EXPECT_EQ(cmd.ExecCommand(), "OK");
110 }
111 
112 /**
113  * @tc.number: Bm_Command_Dump_ModuleTest_0200
114  * @tc.name: ExecCommand
115  * @tc.desc: Verify the "bm dump --all" command.
116  */
117 HWTEST_F(BmCommandDumpModuleTest, Bm_Command_Dump_ModuleTest_0200, Function | MediumTest | Level1)
118 {
119     // install a bundle
120     char *argv[] = {
121         const_cast<char *>(TOOL_NAME.c_str()),
122         const_cast<char *>(cmd_.c_str()),
123         const_cast<char *>("--all"),
124         const_cast<char *>(""),
125     };
126     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
127 
128     BundleManagerShellCommand cmd(argc, argv);
129 
130     // set the mock objects
131     SetMockObjects(cmd);
132 
133     EXPECT_EQ(cmd.ExecCommand(), "OK");
134 }
135