• 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 BmCommandInstallModuleTest : 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_ = "install";
44     sptr<IBundleMgr> mgrProxyPtr_;
45     sptr<IBundleInstaller> installerProxyPtr_;
46 };
47 
SetUpTestCase()48 void BmCommandInstallModuleTest::SetUpTestCase()
49 {}
50 
TearDownTestCase()51 void BmCommandInstallModuleTest::TearDownTestCase()
52 {}
53 
SetUp()54 void BmCommandInstallModuleTest::SetUp()
55 {
56     // reset optind to 0
57     optind = 0;
58 
59     // make mock objects
60     MakeMockObjects();
61 }
62 
TearDown()63 void BmCommandInstallModuleTest::TearDown()
64 {}
65 
MakeMockObjects()66 void BmCommandInstallModuleTest::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 BmCommandInstallModuleTest::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_Install_ModuleTest_0100
90  * @tc.name: ExecCommand
91  * @tc.desc: Verify the "bm install -p <bundle-path>" command.
92  */
93 HWTEST_F(BmCommandInstallModuleTest, Bm_Command_Install_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 *>("-p"),
100         const_cast<char *>(STRING_BUNDLE_PATH.c_str()),
101         const_cast<char *>(""),
102     };
103     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
104 
105     BundleManagerShellCommand cmd(argc, argv);
106 
107     // set the mock objects
108     SetMockObjects(cmd);
109 
110     EXPECT_EQ(cmd.ExecCommand(), STRING_INSTALL_BUNDLE_OK + "\n");
111 }
112 
113 /**
114  * @tc.number: Bm_Command_Install_ModuleTest_0200
115  * @tc.name: ExecCommand
116  * @tc.desc: Verify the "bm install -p <bundle-path> -r" command.
117  */
118 HWTEST_F(BmCommandInstallModuleTest, Bm_Command_Install_ModuleTest_0200, Function | MediumTest | Level1)
119 {
120     // install a bundle
121     char *argv[] = {
122         const_cast<char *>(TOOL_NAME.c_str()),
123         const_cast<char *>(cmd_.c_str()),
124         const_cast<char *>("-p"),
125         const_cast<char *>(STRING_BUNDLE_PATH.c_str()),
126         const_cast<char *>("-r"),
127         const_cast<char *>(""),
128     };
129     int argc = sizeof(argv) / sizeof(argv[0]) - 1;
130 
131     BundleManagerShellCommand cmd(argc, argv);
132 
133     // set the mock objects
134     SetMockObjects(cmd);
135 
136     EXPECT_EQ(cmd.ExecCommand(), STRING_INSTALL_BUNDLE_OK + "\n");
137 }
138