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 BmCommandUninstallModuleTest : 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_ = "uninstall";
44 sptr<IBundleMgr> mgrProxyPtr_;
45 sptr<IBundleInstaller> installerProxyPtr_;
46 };
47
SetUpTestCase()48 void BmCommandUninstallModuleTest::SetUpTestCase()
49 {}
50
TearDownTestCase()51 void BmCommandUninstallModuleTest::TearDownTestCase()
52 {}
53
SetUp()54 void BmCommandUninstallModuleTest::SetUp()
55 {
56 // reset optind to 0
57 optind = 0;
58
59 // make mock objects
60 MakeMockObjects();
61 }
62
TearDown()63 void BmCommandUninstallModuleTest::TearDown()
64 {}
65
MakeMockObjects()66 void BmCommandUninstallModuleTest::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 BmCommandUninstallModuleTest::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_Uninstall_ModuleTest_0100
90 * @tc.name: ExecCommand
91 * @tc.desc: Verify the "bm uninstall -n <bundle-name>" command.
92 */
93 HWTEST_F(BmCommandUninstallModuleTest, Bm_Command_Uninstall_ModuleTest_0100, Function | MediumTest | Level1)
94 {
95 char *argv[] = {
96 const_cast<char *>(TOOL_NAME.c_str()),
97 const_cast<char *>(cmd_.c_str()),
98 const_cast<char *>("-n"),
99 const_cast<char *>(STRING_BUNDLE_NAME.c_str()),
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(), STRING_UNINSTALL_BUNDLE_OK + "\n");
110 }
111
112 /**
113 * @tc.number: Bm_Command_Uninstall_ModuleTest_0200
114 * @tc.name: ExecCommand
115 * @tc.desc: Verify the "bm uninstall -n <bundle-name> -m <module-name>" command.
116 */
117 HWTEST_F(BmCommandUninstallModuleTest, Bm_Command_Uninstall_ModuleTest_0200, Function | MediumTest | Level1)
118 {
119 char *argv[] = {
120 const_cast<char *>(TOOL_NAME.c_str()),
121 const_cast<char *>(cmd_.c_str()),
122 const_cast<char *>("-n"),
123 const_cast<char *>(STRING_BUNDLE_NAME.c_str()),
124 const_cast<char *>("-m"),
125 const_cast<char *>(STRING_MODULE_NAME.c_str()),
126 const_cast<char *>(""),
127 };
128 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
129
130 BundleManagerShellCommand cmd(argc, argv);
131
132 // set the mock objects
133 SetMockObjects(cmd);
134
135 EXPECT_EQ(cmd.ExecCommand(), STRING_UNINSTALL_BUNDLE_OK + "\n");
136 }
137