• 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 <fstream>
17 #include <future>
18 #include <gtest/gtest.h>
19 
20 #include "bundle_mgr_service.h"
21 #include "directory_ex.h"
22 #include "installd/installd_service.h"
23 #include "installd_client.h"
24 #include "mock_status_receiver.h"
25 
26 using namespace OHOS;
27 using namespace testing::ext;
28 using namespace OHOS::AppExecFwk;
29 using namespace std::chrono_literals;
30 using OHOS::DelayedSingleton;
31 
32 namespace {
33 const std::string BUNDLE_CODE_PATH = "/data/app/el1/bundle/public/";
34 const std::string BUNDLE_DATA_PATH = "/data/accounts/account_0/appdata/";
35 const std::string THIRD_BUNDLE_PATH = "/data/test/bms_bundle/";
36 const std::string BUNDLE_NAME = "com.third.hiworld.example1";
37 const std::string MODULE_PACKAGE = "com.third.hiworld.example.h1";
38 const std::string SYSTEM_BUNDLE_NAME = "com.system.hiworld.examples2";
39 }  // namespace
40 
41 class BmsBundleUninstallerModuleTest : public testing::Test {
42 public:
43     static void SetUpTestCase();
44     static void TearDownTestCase();
45     void SetUp();
46     void TearDown();
47     ErrCode InstallBundle(const std::string &bundlePath) const;
48     ErrCode UninstallBundle(const std::string &bundleName) const;
49     ErrCode UninstallHap(const std::string &bundleName, const std::string &modulePackage) const;
50     void CheckFileExist(const std::string bundlename) const;
51     void CheckFileNonExist(const std::string bundlename) const;
52     void CheckBundleInfoExist(const std::string bundlename) const;
53     void CheckBundleInfoNonExist(const std::string bundlename) const;
54     void CheckHapDirNonExist(const std::string bundlename, const std::string &modulePackage) const;
55 
StartBundleMgrService()56     void StartBundleMgrService()
57     {
58         bms_ = DelayedSingleton<BundleMgrService>::GetInstance();
59         bms_->OnStart();
60     }
61 
StopBundleMgrService()62     void StopBundleMgrService()
63     {
64         bms_->OnStop();
65         DelayedSingleton<BundleMgrService>::DestroyInstance();
66     }
67 
StartInstalld()68     void StartInstalld()
69     {
70         installdService_->Start();
71     }
72 
StopInstalld()73     void StopInstalld()
74     {
75         installdService_->Stop();
76         InstalldClient::GetInstance()->ResetInstalldProxy();
77     }
78 
79 private:
80     std::shared_ptr<BundleMgrService> bms_ = nullptr;
81     std::shared_ptr<InstalldService> installdService_ = std::make_shared<InstalldService>();
82 };
83 
InstallBundle(const std::string & bundlePath) const84 ErrCode BmsBundleUninstallerModuleTest::InstallBundle(const std::string &bundlePath) const
85 {
86     auto installer = DelayedSingleton<BundleMgrService>::GetInstance()->GetBundleInstaller();
87     if (installer == nullptr) {
88         EXPECT_FALSE(true) << "the installer is nullptr";
89         return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
90     }
91     OHOS::sptr<MockStatusReceiver> receiver = new (std::nothrow) MockStatusReceiver();
92     if (receiver == nullptr) {
93         EXPECT_FALSE(true) << "the receiver is nullptr";
94         return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
95     }
96     InstallParam installParam;
97     installParam.installFlag = InstallFlag::NORMAL;
98     installer->Install(bundlePath, installParam, receiver);
99     return receiver->GetResultCode();
100 }
101 
UninstallBundle(const std::string & bundleName) const102 ErrCode BmsBundleUninstallerModuleTest::UninstallBundle(const std::string &bundleName) const
103 {
104     auto installer = DelayedSingleton<BundleMgrService>::GetInstance()->GetBundleInstaller();
105     if (installer == nullptr) {
106         EXPECT_FALSE(true) << "the installer is nullptr";
107         return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
108     }
109     OHOS::sptr<MockStatusReceiver> receiver = new (std::nothrow) MockStatusReceiver();
110     if (receiver == nullptr) {
111         EXPECT_FALSE(true) << "the receiver is nullptr";
112         return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
113     }
114     InstallParam installParam;
115     installParam.userId = Constants::DEFAULT_USERID;
116     installer->Uninstall(bundleName, installParam, receiver);
117     return receiver->GetResultCode();
118 }
119 
UninstallHap(const std::string & bundleName,const std::string & modulePackage) const120 ErrCode BmsBundleUninstallerModuleTest::UninstallHap(
121     const std::string &bundleName, const std::string &modulePackage) const
122 {
123     auto installer = DelayedSingleton<BundleMgrService>::GetInstance()->GetBundleInstaller();
124     if (installer == nullptr) {
125         EXPECT_FALSE(true) << "the installer is nullptr";
126         return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
127     }
128     OHOS::sptr<MockStatusReceiver> receiver = new (std::nothrow) MockStatusReceiver();
129     if (receiver == nullptr) {
130         EXPECT_FALSE(true) << "the receiver is nullptr";
131         return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
132     }
133     InstallParam installParam;
134     installParam.userId = Constants::DEFAULT_USERID;
135     installer->Uninstall(bundleName, modulePackage, installParam, receiver);
136     return receiver->GetResultCode();
137 }
138 
CheckFileExist(const std::string bundlename) const139 void BmsBundleUninstallerModuleTest::CheckFileExist(const std::string bundlename) const
140 {
141     int bundleDataExist = access((BUNDLE_DATA_PATH + bundlename).c_str(), F_OK);
142     EXPECT_EQ(bundleDataExist, 0) << "the bundle data dir does not exists: " << (BUNDLE_DATA_PATH + bundlename);
143     int codeExist = access((BUNDLE_CODE_PATH + bundlename).c_str(), F_OK);
144     EXPECT_EQ(codeExist, 0) << "the ability code file does not exist: " << (BUNDLE_CODE_PATH + bundlename);
145 }
146 
CheckFileNonExist(const std::string bundlename) const147 void BmsBundleUninstallerModuleTest::CheckFileNonExist(const std::string bundlename) const
148 {
149     int bundleDataExist = access((BUNDLE_DATA_PATH + bundlename).c_str(), F_OK);
150     EXPECT_NE(bundleDataExist, 0) << "the bundle data dir exists: " << (BUNDLE_DATA_PATH + bundlename);
151     int codeExist = access((BUNDLE_CODE_PATH + bundlename).c_str(), F_OK);
152     EXPECT_NE(codeExist, 0) << "the ability code exists: " << (BUNDLE_CODE_PATH + bundlename);
153 }
154 
CheckBundleInfoExist(const std::string bundlename) const155 void BmsBundleUninstallerModuleTest::CheckBundleInfoExist(const std::string bundlename) const
156 {
157     BundleInfo info;
158     std::shared_ptr<BundleMgrService> bms = DelayedSingleton<BundleMgrService>::GetInstance();
159     auto dataMgr = bms->GetDataMgr();
160     EXPECT_NE(dataMgr, nullptr);
161     bool isBundleExist = dataMgr->GetBundleInfo(bundlename, BundleFlag::GET_BUNDLE_DEFAULT, info);
162     EXPECT_TRUE(isBundleExist);
163 }
164 
CheckBundleInfoNonExist(const std::string bundlename) const165 void BmsBundleUninstallerModuleTest::CheckBundleInfoNonExist(const std::string bundlename) const
166 {
167     BundleInfo info;
168     std::shared_ptr<BundleMgrService> bms = DelayedSingleton<BundleMgrService>::GetInstance();
169     auto dataMgr = bms->GetDataMgr();
170     EXPECT_NE(dataMgr, nullptr);
171     bool isBundleExist = dataMgr->GetBundleInfo(bundlename, BundleFlag::GET_BUNDLE_DEFAULT, info);
172     EXPECT_FALSE(isBundleExist);
173 }
CheckHapDirNonExist(const std::string bundlename,const std::string & modulePackage) const174 void BmsBundleUninstallerModuleTest::CheckHapDirNonExist(
175     const std::string bundlename, const std::string &modulePackage) const
176 {
177     int bundleDataExist = access((BUNDLE_DATA_PATH + bundlename + modulePackage).c_str(), F_OK);
178     EXPECT_NE(bundleDataExist, 0) << "the bundle data dir exists: " << bundlename;
179     int codeExist = access((BUNDLE_CODE_PATH + bundlename + modulePackage).c_str(), F_OK);
180     EXPECT_NE(codeExist, 0) << "the ability code exists: " << bundlename;
181 }
182 
SetUpTestCase()183 void BmsBundleUninstallerModuleTest::SetUpTestCase()
184 {}
185 
TearDownTestCase()186 void BmsBundleUninstallerModuleTest::TearDownTestCase()
187 {}
188 
SetUp()189 void BmsBundleUninstallerModuleTest::SetUp()
190 {
191     StartInstalld();
192     StartBundleMgrService();
193 }
194 
TearDown()195 void BmsBundleUninstallerModuleTest::TearDown()
196 {
197     std::cout << "BmsBundleUninstallerModuleTest::TearDown begin" << std::endl;
198     if (access((BUNDLE_CODE_PATH + BUNDLE_NAME).c_str(), F_OK) == 0) {
199         ErrCode uninstallResult = UninstallBundle(BUNDLE_NAME);
200         EXPECT_EQ(uninstallResult, ERR_OK);
201     }
202 
203     StopInstalld();
204     StopBundleMgrService();
205     std::this_thread::sleep_for(50ms);
206 }
207 
208 /**
209  * @tc.number: Uninstall_0100
210  * @tc.name: test the third-party bundle can be uninstalled.
211  * @tc.desc: the installed bundle can be uninstalled successfully.
212  */
213 HWTEST_F(BmsBundleUninstallerModuleTest, Uninstall_0100, Function | MediumTest | Level1)
214 {
215     std::string bundlePath = THIRD_BUNDLE_PATH + "bmsThirdBundle1" + Constants::INSTALL_FILE_SUFFIX;
216     ErrCode installResult = InstallBundle(bundlePath);
217     EXPECT_EQ(installResult, ERR_OK);
218 
219     CheckFileExist(BUNDLE_NAME);
220     CheckBundleInfoExist(BUNDLE_NAME);
221 
222     ErrCode uninstallResult = UninstallBundle(BUNDLE_NAME);
223     EXPECT_EQ(uninstallResult, ERR_OK);
224     CheckFileNonExist(BUNDLE_NAME);
225     CheckBundleInfoNonExist(BUNDLE_NAME);
226 }
227 
228 /**
229  * @tc.number: Uninstall_0200
230  * @tc.name: test the third-party bundle can not be uninstalled by wrong bundle name.
231  * @tc.desc: the installed bundle can not be uninstalled successfully.
232  */
233 HWTEST_F(BmsBundleUninstallerModuleTest, Uninstall_0200, Function | MediumTest | Level1)
234 {
235     std::string bundlePath = THIRD_BUNDLE_PATH + "bmsThirdBundle1" + Constants::INSTALL_FILE_SUFFIX;
236     ErrCode installResult = InstallBundle(bundlePath);
237     EXPECT_EQ(installResult, ERR_OK);
238 
239     CheckFileExist(BUNDLE_NAME);
240     CheckBundleInfoExist(BUNDLE_NAME);
241 
242     std::string errorBundleName = "com.third.test0";
243     ErrCode uninstallResult = UninstallBundle(errorBundleName);
244     EXPECT_EQ(uninstallResult, ERR_APPEXECFWK_UNINSTALL_MISSING_INSTALLED_BUNDLE);
245 
246     CheckFileNonExist(errorBundleName);
247     CheckBundleInfoNonExist(errorBundleName);
248 }
249 
250 /**
251  * @tc.number: Uninstall_0300
252  * @tc.name: test the third-party bundle can't be uninstalled twice
253  * @tc.desc: The first is successful but second is failed
254  */
255 HWTEST_F(BmsBundleUninstallerModuleTest, Uninstall_0300, Function | MediumTest | Level2)
256 {
257     std::string bundlePath = THIRD_BUNDLE_PATH + "bmsThirdBundle1" + Constants::INSTALL_FILE_SUFFIX;
258     ErrCode installResult = InstallBundle(bundlePath);
259     EXPECT_EQ(installResult, ERR_OK);
260 
261     CheckFileExist(BUNDLE_NAME);
262     CheckBundleInfoExist(BUNDLE_NAME);
263 
264     ErrCode uninstallResult = UninstallBundle(BUNDLE_NAME);
265     EXPECT_EQ(uninstallResult, ERR_OK);
266     CheckFileNonExist(BUNDLE_NAME);
267     CheckBundleInfoNonExist(BUNDLE_NAME);
268 
269     uninstallResult = UninstallBundle(BUNDLE_NAME);
270     EXPECT_EQ(uninstallResult, ERR_APPEXECFWK_UNINSTALL_MISSING_INSTALLED_BUNDLE);
271 }
272 
273 /**
274  * @tc.number: Uninstall_0400
275  * @tc.name: test the third-party app with one hap can be uninstalled.
276  * @tc.desc: the installed app can be uninstalled successfully.
277  */
278 HWTEST_F(BmsBundleUninstallerModuleTest, Uninstall_0400, Function | MediumTest | Level1)
279 {
280     std::string bundlePath = THIRD_BUNDLE_PATH + "bmsThirdBundle1" + Constants::INSTALL_FILE_SUFFIX;
281     ErrCode installResult = InstallBundle(bundlePath);
282     EXPECT_EQ(installResult, ERR_OK);
283 
284     CheckFileExist(BUNDLE_NAME);
285     CheckBundleInfoExist(BUNDLE_NAME);
286 
287     ErrCode uninstallResult = UninstallHap(BUNDLE_NAME, MODULE_PACKAGE);
288     EXPECT_EQ(uninstallResult, ERR_OK);
289     CheckFileNonExist(BUNDLE_NAME);
290     CheckBundleInfoNonExist(BUNDLE_NAME);
291 }
292 
293 /**
294  * @tc.number: Uninstall_0500
295  * @tc.name: test the third-party app with two haps can be uninstalled
296  * @tc.desc: the installed bundle can not be uninstalled successfully.
297  */
298 HWTEST_F(BmsBundleUninstallerModuleTest, Uninstall_0500, Function | MediumTest | Level1)
299 {
300     std::string bundlePath = THIRD_BUNDLE_PATH + "bmsThirdBundle1" + Constants::INSTALL_FILE_SUFFIX;
301     ErrCode installResult = InstallBundle(bundlePath);
302     EXPECT_EQ(installResult, ERR_OK);
303 
304     bundlePath = THIRD_BUNDLE_PATH + "bmsThirdBundle4" + Constants::INSTALL_FILE_SUFFIX;
305     installResult = InstallBundle(bundlePath);
306     EXPECT_EQ(installResult, ERR_OK);
307 
308     CheckFileExist(BUNDLE_NAME);
309     CheckBundleInfoExist(BUNDLE_NAME);
310 
311     ErrCode uninstallResult = UninstallHap(BUNDLE_NAME, MODULE_PACKAGE);
312     EXPECT_EQ(uninstallResult, ERR_OK);
313     CheckHapDirNonExist(BUNDLE_NAME, MODULE_PACKAGE);
314     CheckFileExist(BUNDLE_NAME);
315     CheckBundleInfoExist(BUNDLE_NAME);
316 }
317 
318 /**
319  * @tc.number: Uninstall_0600
320  * @tc.name: test the big third-party app can be uninstalled
321  * @tc.desc: the installed bundle can not be uninstalled successfully.
322  */
323 HWTEST_F(BmsBundleUninstallerModuleTest, Uninstall_0600, Function | MediumTest | Level1)
324 {
325     BundleInfo info;
326     std::shared_ptr<BundleMgrService> bms = DelayedSingleton<BundleMgrService>::GetInstance();
327     auto dataMgr = bms->GetDataMgr();
328     EXPECT_NE(dataMgr, nullptr);
329     std::string bundlePath = THIRD_BUNDLE_PATH + "bmsThirdBundle13" + Constants::INSTALL_FILE_SUFFIX;
330     std::string bundleName = "com.third.hiworld.example5";
331     ErrCode installResult = InstallBundle(bundlePath);
332     EXPECT_EQ(installResult, ERR_OK);
333     bool isBundleExist = dataMgr->GetBundleInfo(bundleName, BundleFlag::GET_BUNDLE_DEFAULT, info);
334     EXPECT_TRUE(isBundleExist);
335     CheckFileExist(bundleName);
336     CheckBundleInfoExist(bundleName);
337 
338     auto installer = DelayedSingleton<BundleMgrService>::GetInstance()->GetBundleInstaller();
339     EXPECT_NE(installer, nullptr);
340     OHOS::sptr<MockStatusReceiver> receiver = new (std::nothrow) MockStatusReceiver();
341     EXPECT_NE(receiver, nullptr);
342     InstallParam installParam;
343     installParam.userId = Constants::DEFAULT_USERID;
344     installer->Uninstall(bundleName, installParam, receiver);
345     EXPECT_EQ(receiver->GetResultCode(), ERR_OK);
346     isBundleExist = dataMgr->GetBundleInfo(bundleName, BundleFlag::GET_BUNDLE_DEFAULT, info);
347     EXPECT_FALSE(isBundleExist);
348     CheckFileNonExist(bundleName);
349     CheckBundleInfoNonExist(bundleName);
350 }
351