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