• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #include <string>
18 #include <system_ability_definition.h>
19 #include <vector>
20 
21 #define private public
22 #include "bundle_manager_proxy.h"
23 #undef private
24 #include "edm_sys_manager_mock.h"
25 #include "enterprise_device_mgr_stub_mock.h"
26 #include "install_param.h"
27 #include "policy_type.h"
28 #include "func_code.h"
29 #include "utils.h"
30 
31 using namespace testing::ext;
32 using namespace testing;
33 
34 namespace OHOS {
35 namespace EDM {
36 namespace TEST {
37 const std::string ADMIN_PACKAGENAME = "com.edm.test.demo";
38 const std::string TEST_PACKAGE_PATH = "/data/test/resource/enterprise_device_management/hap/right.hap";
39 class BundleManagerProxyTest : public testing::Test {
40 protected:
41     void SetUp() override;
42 
43     void TearDown() override;
44 
45     static void TearDownTestSuite(void);
46     std::shared_ptr<BundleManagerProxy> bundleManagerProxy = nullptr;
47     std::shared_ptr<EdmSysManager> edmSysManager_ = nullptr;
48     sptr<EnterpriseDeviceMgrStubMock> object_ = nullptr;
49 };
50 
SetUp()51 void BundleManagerProxyTest::SetUp()
52 {
53     bundleManagerProxy = BundleManagerProxy::GetBundleManagerProxy();
54     edmSysManager_ = std::make_shared<EdmSysManager>();
55     object_ = new (std::nothrow) EnterpriseDeviceMgrStubMock();
56     edmSysManager_->RegisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID, object_);
57     Utils::SetEdmServiceEnable();
58 }
59 
TearDown()60 void BundleManagerProxyTest::TearDown()
61 {
62     bundleManagerProxy.reset();
63     edmSysManager_->UnregisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID);
64     object_ = nullptr;
65     Utils::SetEdmServiceDisable();
66 }
67 
TearDownTestSuite()68 void BundleManagerProxyTest::TearDownTestSuite()
69 {
70     ASSERT_FALSE(Utils::GetEdmServiceState());
71     std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl;
72 }
73 
74 /**
75  * @tc.name: TestAddAllowedInstallBundlesSuc
76  * @tc.desc: Test AddAllowedInstallBundles success func.
77  * @tc.type: FUNC
78  */
79 HWTEST_F(BundleManagerProxyTest, AddAllowedInstallBundlesSuc, TestSize.Level1)
80 {
81     OHOS::AppExecFwk::ElementName admin;
82     std::vector<std::string> bundles = {ADMIN_PACKAGENAME};
83     for (int32_t policyType = static_cast<int32_t>(PolicyType::ALLOW_INSTALL);
84         policyType <= static_cast<int32_t>(PolicyType::DISALLOW_UNINSTALL); policyType++) {
85         EXPECT_CALL(*object_, SendRequest(_, _, _, _))
86             .Times(1)
87             .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
88         ErrCode ret = bundleManagerProxy->AddBundlesByPolicyType(admin, bundles, DEFAULT_USER_ID, policyType);
89         ASSERT_TRUE(ret == ERR_OK);
90     }
91 }
92 
93 /**
94  * @tc.name: TestAddAllowedInstallBundlesFail
95  * @tc.desc: Test AddAllowedInstallBundles without enable edm service func.
96  * @tc.type: FUNC
97  */
98 HWTEST_F(BundleManagerProxyTest, AddAllowedInstallBundlesFail, TestSize.Level1)
99 {
100     Utils::SetEdmServiceDisable();
101     OHOS::AppExecFwk::ElementName admin;
102     std::vector<std::string> bundles = {ADMIN_PACKAGENAME};
103     for (int32_t policyType = static_cast<int32_t>(PolicyType::ALLOW_INSTALL);
104         policyType <= static_cast<int32_t>(PolicyType::DISALLOW_UNINSTALL); policyType++) {
105         ErrCode ret = bundleManagerProxy->AddBundlesByPolicyType(admin, bundles, DEFAULT_USER_ID, policyType);
106         ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
107     }
108 }
109 
110 /**
111  * @tc.name: TestRemoveAllowedInstallBundlesSuc
112  * @tc.desc: Test RemoveAllowedInstallBundles success func.
113  * @tc.type: FUNC
114  */
115 HWTEST_F(BundleManagerProxyTest, RemoveAllowedInstallBundlesSuc, TestSize.Level1)
116 {
117     OHOS::AppExecFwk::ElementName admin;
118     std::vector<std::string> bundles = {ADMIN_PACKAGENAME};
119     for (int32_t policyType = static_cast<int32_t>(PolicyType::ALLOW_INSTALL);
120         policyType <= static_cast<int32_t>(PolicyType::DISALLOW_UNINSTALL); policyType++) {
121         EXPECT_CALL(*object_, SendRequest(_, _, _, _))
122             .Times(1)
123             .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
124         ErrCode ret = bundleManagerProxy->RemoveBundlesByPolicyType(admin, bundles, DEFAULT_USER_ID, policyType);
125         ASSERT_TRUE(ret == ERR_OK);
126     }
127 }
128 
129 /**
130  * @tc.name: TestRemoveAllowedInstallBundlesFail
131  * @tc.desc: Test RemoveAllowedInstallBundles without enable edm service func.
132  * @tc.type: FUNC
133  */
134 HWTEST_F(BundleManagerProxyTest, RemoveAllowedInstallBundlesFail, TestSize.Level1)
135 {
136     Utils::SetEdmServiceDisable();
137     OHOS::AppExecFwk::ElementName admin;
138     std::vector<std::string> bundles = {ADMIN_PACKAGENAME};
139     for (int32_t policyType = static_cast<int32_t>(PolicyType::ALLOW_INSTALL);
140         policyType <= static_cast<int32_t>(PolicyType::DISALLOW_UNINSTALL); policyType++) {
141         ErrCode ret = bundleManagerProxy->RemoveBundlesByPolicyType(admin, bundles, DEFAULT_USER_ID, policyType);
142         ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
143     }
144 }
145 
146 /**
147  * @tc.name: TestGetAllowedInstallBundlesSuc
148  * @tc.desc: Test GetAllowedInstallBundles success func.
149  * @tc.type: FUNC
150  */
151 HWTEST_F(BundleManagerProxyTest, GetAllowedInstallBundlesSuc, TestSize.Level1)
152 {
153     OHOS::AppExecFwk::ElementName admin;
154     std::vector<std::string> bundles = {ADMIN_PACKAGENAME};
155     for (int32_t policyType = static_cast<int32_t>(PolicyType::ALLOW_INSTALL);
156         policyType <= static_cast<int32_t>(PolicyType::DISALLOW_UNINSTALL); policyType++) {
157         EXPECT_CALL(*object_, SendRequest(_, _, _, _))
158             .Times(1)
159             .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeArrayStringSendRequestGetPolicy));
160         ErrCode ret = bundleManagerProxy->GetBundlesByPolicyType(admin, DEFAULT_USER_ID, bundles, policyType);
161         ASSERT_TRUE(ret == ERR_OK);
162         ASSERT_TRUE(bundles.size() == 1);
163         ASSERT_TRUE(bundles[0] == RETURN_STRING);
164     }
165 }
166 
167 /**
168  * @tc.name: TestGetAllowedInstallBundlesFail
169  * @tc.desc: Test GetAllowedInstallBundles fail func.
170  * @tc.type: FUNC
171  */
172 HWTEST_F(BundleManagerProxyTest, TestGetAllowedInstallBundlesFail, TestSize.Level1)
173 {
174     OHOS::AppExecFwk::ElementName admin;
175     std::vector<std::string> bundles = {ADMIN_PACKAGENAME};
176     for (int32_t policyType = static_cast<int32_t>(PolicyType::ALLOW_INSTALL);
177         policyType <= static_cast<int32_t>(PolicyType::DISALLOW_UNINSTALL); policyType++) {
178         EXPECT_CALL(*object_, SendRequest(_, _, _, _))
179             .Times(1)
180             .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestGetErrPolicy));
181         ErrCode ret = bundleManagerProxy->GetBundlesByPolicyType(admin, DEFAULT_USER_ID, bundles, policyType);
182         ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
183     }
184 }
185 
186 /**
187  * @tc.name: TestGetAllowedInstallBundlesFailWithMax
188  * @tc.desc: Test GetAllowedInstallBundles fail func.
189  * @tc.type: FUNC
190  */
191 HWTEST_F(BundleManagerProxyTest, TestGetAllowedInstallBundlesFailWithMax, TestSize.Level1)
192 {
193     OHOS::AppExecFwk::ElementName admin;
194     std::vector<std::string> bundles = {ADMIN_PACKAGENAME};
195     for (int32_t policyType = static_cast<int32_t>(PolicyType::ALLOW_INSTALL);
196         policyType <= static_cast<int32_t>(PolicyType::DISALLOW_UNINSTALL); policyType++) {
197         EXPECT_CALL(*object_, SendRequest(_, _, _, _))
198             .Times(1)
199             .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestGetPolicyExceedsMax));
200         ErrCode ret = bundleManagerProxy->GetBundlesByPolicyType(admin, DEFAULT_USER_ID, bundles, policyType);
201         ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
202     }
203 }
204 
205 /**
206  * @tc.name: TestGetAllowedInstallBundlesFail
207  * @tc.desc: Test GetAllowedInstallBundles without enable edm service func.
208  * @tc.type: FUNC
209  */
210 HWTEST_F(BundleManagerProxyTest, GetAllowedInstallBundlesFail, TestSize.Level1)
211 {
212     Utils::SetEdmServiceDisable();
213     OHOS::AppExecFwk::ElementName admin;
214     std::vector<std::string> bundles = {ADMIN_PACKAGENAME};
215     for (int32_t policyType = static_cast<int32_t>(PolicyType::ALLOW_INSTALL);
216         policyType <= static_cast<int32_t>(PolicyType::DISALLOW_UNINSTALL); policyType++) {
217         ErrCode ret = bundleManagerProxy->GetBundlesByPolicyType(admin, DEFAULT_USER_ID, bundles, policyType);
218         ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
219     }
220 }
221 
222 /**
223  * @tc.name: TestInstallBundlesInvalidType
224  * @tc.desc: Test Add remove get BundlesByPolicyType with invalid policy type.
225  * @tc.type: FUNC
226  */
227 HWTEST_F(BundleManagerProxyTest, InstallBundlesInvalidType, TestSize.Level1)
228 {
229     Utils::SetEdmServiceDisable();
230     OHOS::AppExecFwk::ElementName admin;
231     std::vector<std::string> bundles = {ADMIN_PACKAGENAME};
232     int32_t policyType = static_cast<int32_t>(PolicyType::INVALID_TYPE);
233     ErrCode ret = bundleManagerProxy->AddBundlesByPolicyType(admin, bundles, DEFAULT_USER_ID, policyType);
234     ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
235 
236     ret = bundleManagerProxy->RemoveBundlesByPolicyType(admin, bundles, DEFAULT_USER_ID, policyType);
237     ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
238 
239     ret = bundleManagerProxy->GetBundlesByPolicyType(admin, DEFAULT_USER_ID, bundles, policyType);
240     ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
241 }
242 
243 /**
244  * @tc.name: TestUninstallParamErr
245  * @tc.desc: Test Uninsatll method with param error.
246  * @tc.type: FUNC
247  */
248 HWTEST_F(BundleManagerProxyTest, TestUninstallParamErr, TestSize.Level1)
249 {
250     OHOS::AppExecFwk::ElementName admin;
251     std::string retMsg;
252     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
253         .Times(1)
254         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestParamError));
255     ErrCode ret = bundleManagerProxy->Uninstall(admin, ADMIN_PACKAGENAME, DEFAULT_USER_ID, false, retMsg);
256     ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
257     ASSERT_TRUE(retMsg == RETURN_STRING);
258 }
259 
260 /**
261  * @tc.name: TestUninstallSuc
262  * @tc.desc: Test Uninsatll method success.
263  * @tc.type: FUNC
264  */
265 HWTEST_F(BundleManagerProxyTest, TestUninstallSuc, TestSize.Level1)
266 {
267     OHOS::AppExecFwk::ElementName admin;
268     std::string retMsg;
269     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
270         .Times(1)
271         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
272     ErrCode ret = bundleManagerProxy->Uninstall(admin, ADMIN_PACKAGENAME, DEFAULT_USER_ID, false, retMsg);
273     ASSERT_TRUE(ret == ERR_OK);
274 }
275 
276 /**
277  * @tc.name: TestWriteFileToStreamFailWithPathNull
278  * @tc.desc: Test WriteFileToStream method when file path is null.
279  * @tc.type: FUNC
280  */
281 HWTEST_F(BundleManagerProxyTest, TestWriteFileToStreamFailWithPathNull, TestSize.Level1)
282 {
283     OHOS::AppExecFwk::ElementName admin;
284     std::string hapFilePath;
285     std::vector<std::string> realPaths;
286     string errMessage;
287     ErrCode ret = bundleManagerProxy->WriteFileToStream(admin, hapFilePath, realPaths, errMessage);
288     ASSERT_TRUE(ret == EdmReturnErrCode::APPLICATION_INSTALL_FAILED);
289     ASSERT_TRUE(errMessage == "install failed due to invalid hapFilePaths");
290 }
291 
292 /**
293  * @tc.name: TestWriteFileToStreamSuc
294  * @tc.desc: Test WriteFileToStream method when file path is valid.
295  * @tc.type: FUNC
296  */
297 HWTEST_F(BundleManagerProxyTest, TestWriteFileToStreamSuc, TestSize.Level1)
298 {
299     OHOS::AppExecFwk::ElementName admin;
300     std::string hapFilePath  = TEST_PACKAGE_PATH;
301     std::vector<std::string> realPaths;
302     string errMessage;
303     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
304         .Times(1).WillOnce(Invoke(object_.GetRefPtr(),
305         &EnterpriseDeviceMgrStubMock::InvokeSendRequestGetPolicyForWriteFileToStream));
306     ErrCode ret = bundleManagerProxy->WriteFileToStream(admin, hapFilePath, realPaths, errMessage);
307     ASSERT_TRUE(ret == ERR_OK);
308 }
309 
310 /**
311  * @tc.name: TestWriteFileToStreamFailWithGetPolicyErr
312  * @tc.desc: Test WriteFileToStream method when file path is valid.
313  * @tc.type: FUNC
314  */
315 HWTEST_F(BundleManagerProxyTest, TestWriteFileToStreamFailWithGetPolicyErr, TestSize.Level1)
316 {
317     OHOS::AppExecFwk::ElementName admin;
318     std::string hapFilePath  = TEST_PACKAGE_PATH;
319     std::vector<std::string> realPaths;
320     string errMessage;
321     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
322         .Times(1)
323         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestGetErrPolicy));
324     ErrCode ret = bundleManagerProxy->WriteFileToStream(admin, hapFilePath, realPaths, errMessage);
325     ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
326 }
327 
328 /**
329  * @tc.name: TestInstallFailWithEmptyPath
330  * @tc.desc: Test Insatll method with empty hapFilePaths.
331  * @tc.type: FUNC
332  */
333 HWTEST_F(BundleManagerProxyTest, TestInstallFailWithEmptyPath, TestSize.Level1)
334 {
335     OHOS::AppExecFwk::ElementName admin;
336     std::vector<std::string> hapFilePaths;
337     AppExecFwk::InstallParam installParam;
338     std::string retMsg;
339     ErrCode ret = bundleManagerProxy->Install(admin, hapFilePaths, installParam, retMsg);
340     ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
341 }
342 
343 /**
344  * @tc.name: TestInstallSuc
345  * @tc.desc: Test Insatll method with one hapFilePaths.
346  * @tc.type: FUNC
347  */
348 HWTEST_F(BundleManagerProxyTest, TestInstallSuc, TestSize.Level1)
349 {
350     OHOS::AppExecFwk::ElementName admin;
351     std::vector<std::string> hapFilePaths = { TEST_PACKAGE_PATH };
352     AppExecFwk::InstallParam installParam;
353     std::string retMsg;
354     std::uint32_t funcCodeGet = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::GET, EdmInterfaceCode::INSTALL);
355     std::uint32_t funcCodeSet = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::INSTALL);
356     EXPECT_CALL(*object_, SendRequest(funcCodeGet, _, _, _))
357         .Times(1).WillOnce(Invoke(object_.GetRefPtr(),
358         &EnterpriseDeviceMgrStubMock::InvokeSendRequestGetPolicyForWriteFileToStream));
359     EXPECT_CALL(*object_, SendRequest(funcCodeSet, _, _, _))
360         .Times(1).WillOnce(Invoke(object_.GetRefPtr(),
361         &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
362     ErrCode ret = bundleManagerProxy->Install(admin, hapFilePaths, installParam, retMsg);
363     ASSERT_TRUE(ret == ERR_OK);
364 }
365 
366 /**
367  * @tc.name: TestInstallFail
368  * @tc.desc: Test Insatll method with one hapFilePaths.
369  * @tc.type: FUNC
370  */
371 HWTEST_F(BundleManagerProxyTest, TestInstallFail, TestSize.Level1)
372 {
373     OHOS::AppExecFwk::ElementName admin;
374     std::vector<std::string> hapFilePaths = { TEST_PACKAGE_PATH };
375     AppExecFwk::InstallParam installParam;
376     std::string retMsg;
377     std::uint32_t funcCodeGet = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::GET, EdmInterfaceCode::INSTALL);
378     std::uint32_t funcCodeSet = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::INSTALL);
379     EXPECT_CALL(*object_, SendRequest(funcCodeGet, _, _, _))
380         .Times(1).WillOnce(Invoke(object_.GetRefPtr(),
381         &EnterpriseDeviceMgrStubMock::InvokeSendRequestGetPolicyForWriteFileToStream));
382     EXPECT_CALL(*object_, SendRequest(funcCodeSet, _, _, _))
383         .Times(1).WillOnce(Invoke(object_.GetRefPtr(),
384         &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicyInstallFail));
385     ErrCode ret = bundleManagerProxy->Install(admin, hapFilePaths, installParam, retMsg);
386     ASSERT_TRUE(ret == EdmReturnErrCode::APPLICATION_INSTALL_FAILED);
387 }
388 
389 /**
390  * @tc.name: TestWriteFileToInnerFail
391  * @tc.desc: Test Insatll method with invalid hap file paths.
392  * @tc.type: FUNC
393  */
394 HWTEST_F(BundleManagerProxyTest, TestWriteFileToInnerFail, TestSize.Level1)
395 {
396     MessageParcel reply;
397     reply.WriteFileDescriptor(-1);
398     std::string hapFilePaths;
399     std::vector<std::string> realPaths;
400     std::string retMsg;
401     ErrCode ret = bundleManagerProxy->WriteFileToInner(reply, hapFilePaths, realPaths, retMsg);
402     ASSERT_TRUE(ret == EdmReturnErrCode::APPLICATION_INSTALL_FAILED);
403     ASSERT_TRUE(retMsg == "write file to stream failed due to invalid file descriptor");
404 }
405 
406 /**
407  * @tc.name: TestWriteFileToInnerSuc
408  * @tc.desc: Test Insatll method with hap file paths.
409  * @tc.type: FUNC
410  */
411 HWTEST_F(BundleManagerProxyTest, TestWriteFileToInnerSuc, TestSize.Level1)
412 {
413     MessageParcel reply;
414     reply.WriteFileDescriptor(1);
415     std::string hapFilePaths = { TEST_PACKAGE_PATH };
416     std::vector<std::string> realPaths;
417     std::string retMsg;
418     ErrCode ret = bundleManagerProxy->WriteFileToInner(reply, hapFilePaths, realPaths, retMsg);
419     ASSERT_TRUE(ret == ERR_OK);
420 }
421 } // namespace TEST
422 } // namespace EDM
423 } // namespace OHOS
424