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 namespace OHOS {
34 class BmCommandUninstallModuleTest : public ::testing::Test {
35 public:
36     static void SetUpTestCase();
37     static void TearDownTestCase();
38     void SetUp() override;
39     void TearDown() override;
40 
41     void MakeMockObjects();
42     void SetMockObjects(BundleManagerShellCommand &cmd) const;
43 
44     std::string cmd_ = "uninstall";
45     sptr<IBundleMgr> mgrProxyPtr_;
46     sptr<IBundleInstaller> installerProxyPtr_;
47 };
48 
SetUpTestCase()49 void BmCommandUninstallModuleTest::SetUpTestCase()
50 {}
51 
TearDownTestCase()52 void BmCommandUninstallModuleTest::TearDownTestCase()
53 {}
54 
SetUp()55 void BmCommandUninstallModuleTest::SetUp()
56 {
57     // reset optind to 0
58     optind = 0;
59 
60     // make mock objects
61     MakeMockObjects();
62 }
63 
TearDown()64 void BmCommandUninstallModuleTest::TearDown()
65 {}
66 
MakeMockObjects()67 void BmCommandUninstallModuleTest::MakeMockObjects()
68 {
69     // mock a mgr host
70     auto mgrHostPtr = sptr<IRemoteObject>(new (std::nothrow) MockBundleMgrHost());
71     // mock a mgr proxy
72     mgrProxyPtr_ = iface_cast<IBundleMgr>(mgrHostPtr);
73 
74     // mock a installer host
75     auto installerHostPtr = sptr<IRemoteObject>(new (std::nothrow) MockBundleInstallerHost());
76     // mock a installer proxy
77     installerProxyPtr_ = iface_cast<IBundleInstaller>(installerHostPtr);
78 }
79 
SetMockObjects(BundleManagerShellCommand & cmd) const80 void BmCommandUninstallModuleTest::SetMockObjects(BundleManagerShellCommand &cmd) const
81 {
82     // set the mock mgr proxy
83     cmd.bundleMgrProxy_ = mgrProxyPtr_;
84 
85     // set the mock installer proxy
86     cmd.bundleInstallerProxy_ = installerProxyPtr_;
87 }
88 
89 /**
90  * @tc.number: Bm_Command_Uninstall_ModuleTest_0100
91  * @tc.name: ExecCommand
92  * @tc.desc: Verify the "bm uninstall -n <bundle-name>" command.
93  */
94 HWTEST_F(BmCommandUninstallModuleTest, Bm_Command_Uninstall_ModuleTest_0100, Function | MediumTest | Level1)
95 {
96     char *argv[] = {
97         const_cast<char *>(TOOL_NAME.c_str()),
98         const_cast<char *>(cmd_.c_str()),
99         const_cast<char *>("-n"),
100         const_cast<char *>(STRING_BUNDLE_NAME.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_UNINSTALL_BUNDLE_OK + "\n");
111 }
112 
113 /**
114  * @tc.number: Bm_Command_Uninstall_ModuleTest_0200
115  * @tc.name: ExecCommand
116  * @tc.desc: Verify the "bm uninstall -n <bundle-name> -m <module-name>" command.
117  */
118 HWTEST_F(BmCommandUninstallModuleTest, Bm_Command_Uninstall_ModuleTest_0200, Function | MediumTest | Level1)
119 {
120     char *argv[] = {
121         const_cast<char *>(TOOL_NAME.c_str()),
122         const_cast<char *>(cmd_.c_str()),
123         const_cast<char *>("-n"),
124         const_cast<char *>(STRING_BUNDLE_NAME.c_str()),
125         const_cast<char *>("-m"),
126         const_cast<char *>(STRING_MODULE_NAME.c_str()),
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_UNINSTALL_BUNDLE_OK + "\n");
137 }
138 } // OHOS
139