1 /*
2 * Copyright (c) 2023-2024 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 #include "edm_sys_manager_mock.h"
22 #include "enterprise_device_mgr_stub_mock.h"
23 #include "usb_device_id.h"
24 #include "usb_interface_type.h"
25 #include "usb_manager_proxy.h"
26 #include "utils.h"
27
28 using namespace testing::ext;
29 using namespace testing;
30
31 namespace OHOS {
32 namespace EDM {
33 namespace TEST {
34 const std::string ADMIN_PACKAGENAME = "com.edm.test.demo";
35 class UsbManagerProxyTest : public testing::Test {
36 protected:
37 void SetUp() override;
38
39 void TearDown() override;
40
41 static void TearDownTestSuite(void);
42 std::shared_ptr<UsbManagerProxy> proxy_ = nullptr;
43 std::shared_ptr<EdmSysManager> edmSysManager_ = nullptr;
44 sptr<EnterpriseDeviceMgrStubMock> object_ = nullptr;
45 };
46
SetUp()47 void UsbManagerProxyTest::SetUp()
48 {
49 proxy_ = UsbManagerProxy::GetUsbManagerProxy();
50 edmSysManager_ = std::make_shared<EdmSysManager>();
51 object_ = new (std::nothrow) EnterpriseDeviceMgrStubMock();
52 edmSysManager_->RegisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID, object_);
53 Utils::SetEdmServiceEnable();
54 }
55
TearDown()56 void UsbManagerProxyTest::TearDown()
57 {
58 proxy_.reset();
59 edmSysManager_->UnregisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID);
60 object_ = nullptr;
61 Utils::SetEdmServiceDisable();
62 }
63
TearDownTestSuite()64 void UsbManagerProxyTest::TearDownTestSuite()
65 {
66 ASSERT_FALSE(Utils::GetEdmServiceState());
67 std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl;
68 }
69
70 /**
71 * @tc.name: TestSetUsbReadOnlySuc
72 * @tc.desc: Test SetUsbReadOnly success func.
73 * @tc.type: FUNC
74 */
75 HWTEST_F(UsbManagerProxyTest, TestSetUsbReadOnlySuc, TestSize.Level1)
76 {
77 OHOS::AppExecFwk::ElementName admin;
78 admin.SetBundleName(ADMIN_PACKAGENAME);
79 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
80 .Times(1)
81 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
82 int32_t ret = proxy_->SetUsbReadOnly(admin, true);
83 ASSERT_TRUE(ret == ERR_OK);
84 }
85
86 /**
87 * @tc.name: TestSetUsbReadOnlyFail
88 * @tc.desc: Test SetUsbReadOnly without enable edm service func.
89 * @tc.type: FUNC
90 */
91 HWTEST_F(UsbManagerProxyTest, TestSetUsbReadOnlyFail, TestSize.Level1)
92 {
93 Utils::SetEdmServiceDisable();
94 OHOS::AppExecFwk::ElementName admin;
95 admin.SetBundleName(ADMIN_PACKAGENAME);
96 int32_t ret = proxy_->SetUsbReadOnly(admin, true);
97 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
98 }
99
100 /**
101 * @tc.name: TestDisableUsbSuc
102 * @tc.desc: Test DisableUsb success func.
103 * @tc.type: FUNC
104 */
105 HWTEST_F(UsbManagerProxyTest, TestDisableUsbSuc, TestSize.Level1)
106 {
107 OHOS::AppExecFwk::ElementName admin;
108 admin.SetBundleName(ADMIN_PACKAGENAME);
109 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
110 .Times(1)
111 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
112 int32_t ret = proxy_->DisableUsb(admin, true);
113 ASSERT_TRUE(ret == ERR_OK);
114 }
115
116 /**
117 * @tc.name: TestDisableUsbFail
118 * @tc.desc: Test DisableUsb without enable edm service func.
119 * @tc.type: FUNC
120 */
121 HWTEST_F(UsbManagerProxyTest, TestDisableUsbFail, TestSize.Level1)
122 {
123 Utils::SetEdmServiceDisable();
124 OHOS::AppExecFwk::ElementName admin;
125 admin.SetBundleName(ADMIN_PACKAGENAME);
126 int32_t ret = proxy_->DisableUsb(admin, true);
127 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
128 }
129
130 /**
131 * @tc.name: TestIsUsbDisabledSuc
132 * @tc.desc: Test IsUsbDisabled func.
133 * @tc.type: FUNC
134 */
135 HWTEST_F(UsbManagerProxyTest, TestIsUsbDisabledSuc, TestSize.Level1)
136 {
137 OHOS::AppExecFwk::ElementName admin;
138 admin.SetBundleName(ADMIN_PACKAGENAME);
139 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
140 .Times(1)
141 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBoolSendRequestGetPolicy));
142
143 bool isDisable = false;
144 int32_t ret = proxy_->IsUsbDisabled(&admin, isDisable);
145 ASSERT_TRUE(ret == ERR_OK);
146 ASSERT_TRUE(isDisable);
147 }
148
149 /**
150 * @tc.name: TestIsUsbDisabledFail
151 * @tc.desc: Test IsUsbDisabled func without enable edm service.
152 * @tc.type: FUNC
153 */
154 HWTEST_F(UsbManagerProxyTest, TestIsUsbDisabledFail, TestSize.Level1)
155 {
156 Utils::SetEdmServiceDisable();
157 OHOS::AppExecFwk::ElementName admin;
158 admin.SetBundleName(ADMIN_PACKAGENAME);
159
160 bool isDisable = false;
161 int32_t ret = proxy_->IsUsbDisabled(&admin, isDisable);
162 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
163 ASSERT_FALSE(isDisable);
164 }
165
166 /**
167 * @tc.name: TestAddAllowedUsbDevicesSuc
168 * @tc.desc: Test AddAllowedUsbDevices success func.
169 * @tc.type: FUNC
170 */
171 HWTEST_F(UsbManagerProxyTest, TestAddAllowedUsbDevicesSuc, TestSize.Level1)
172 {
173 OHOS::AppExecFwk::ElementName admin;
174 admin.SetBundleName(ADMIN_PACKAGENAME);
175 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
176 .Times(1)
177 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
178 std::vector<UsbDeviceId> usbDeviceIds;
179 UsbDeviceId id1;
180 id1.SetVendorId(1);
181 id1.SetProductId(9);
182 usbDeviceIds.push_back(id1);
183 int32_t ret = proxy_->AddAllowedUsbDevices(admin, usbDeviceIds);
184 ASSERT_TRUE(ret == ERR_OK);
185 }
186
187 /**
188 * @tc.name: TestAddAllowedUsbDevicesFail
189 * @tc.desc: Test AddAllowedUsbDevices without enable edm service func.
190 * @tc.type: FUNC
191 */
192 HWTEST_F(UsbManagerProxyTest, TestAddAllowedUsbDevicesFail, TestSize.Level1)
193 {
194 Utils::SetEdmServiceDisable();
195 OHOS::AppExecFwk::ElementName admin;
196 admin.SetBundleName(ADMIN_PACKAGENAME);
197 std::vector<UsbDeviceId> usbDeviceIds;
198 UsbDeviceId id1;
199 id1.SetVendorId(1);
200 id1.SetProductId(9);
201 usbDeviceIds.push_back(id1);
202 int32_t ret = proxy_->AddAllowedUsbDevices(admin, usbDeviceIds);
203 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
204 }
205
206 /**
207 * @tc.name: TestRemoveAllowedUsbDevicesSuc
208 * @tc.desc: Test RemoveAllowedUsbDevices success func.
209 * @tc.type: FUNC
210 */
211 HWTEST_F(UsbManagerProxyTest, TestRemoveAllowedUsbDevicesSuc, TestSize.Level1)
212 {
213 OHOS::AppExecFwk::ElementName admin;
214 admin.SetBundleName(ADMIN_PACKAGENAME);
215 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
216 .Times(1)
217 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
218 std::vector<UsbDeviceId> usbDeviceIds;
219 UsbDeviceId id1;
220 id1.SetVendorId(1);
221 id1.SetProductId(9);
222 usbDeviceIds.push_back(id1);
223 int32_t ret = proxy_->RemoveAllowedUsbDevices(admin, usbDeviceIds);
224 ASSERT_TRUE(ret == ERR_OK);
225 }
226
227 /**
228 * @tc.name: TestRemoveAllowedUsbDevicesFail
229 * @tc.desc: Test RemoveAllowedUsbDevices without enable edm service func.
230 * @tc.type: FUNC
231 */
232 HWTEST_F(UsbManagerProxyTest, TestRemoveAllowedUsbDevicesFail, TestSize.Level1)
233 {
234 Utils::SetEdmServiceDisable();
235 OHOS::AppExecFwk::ElementName admin;
236 admin.SetBundleName(ADMIN_PACKAGENAME);
237 std::vector<UsbDeviceId> usbDeviceIds;
238 UsbDeviceId id1;
239 id1.SetVendorId(1);
240 id1.SetProductId(9);
241 usbDeviceIds.push_back(id1);
242 int32_t ret = proxy_->RemoveAllowedUsbDevices(admin, usbDeviceIds);
243 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
244 }
245
246 /**
247 * @tc.name: TestGetAllowedUsbDevicesSuc
248 * @tc.desc: Test GetAllowedUsbDevices func.
249 * @tc.type: FUNC
250 */
251 HWTEST_F(UsbManagerProxyTest, TestGetAllowedUsbDevicesSuc, TestSize.Level1)
252 {
253 OHOS::AppExecFwk::ElementName admin;
254 admin.SetBundleName(ADMIN_PACKAGENAME);
255 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
256 .Times(1)
257 .WillOnce(Invoke(object_.GetRefPtr(),
258 &EnterpriseDeviceMgrStubMock::InvokeAllowedUsbDevicesSendRequestGetPolicy));
259
260 std::vector<UsbDeviceId> usbDeviceIds;
261 int32_t ret = proxy_->GetAllowedUsbDevices(admin, usbDeviceIds);
262 ASSERT_TRUE(ret == ERR_OK);
263 ASSERT_TRUE(usbDeviceIds.size() == 1);
264 }
265
266 /**
267 * @tc.name: TestGetAllowedUsbDevicesFail
268 * @tc.desc: Test GetAllowedUsbDevices func without enable edm service.
269 * @tc.type: FUNC
270 */
271 HWTEST_F(UsbManagerProxyTest, TestGetAllowedUsbDevicesFail, TestSize.Level1)
272 {
273 Utils::SetEdmServiceDisable();
274 OHOS::AppExecFwk::ElementName admin;
275 admin.SetBundleName(ADMIN_PACKAGENAME);
276 std::vector<UsbDeviceId> usbDeviceIds;
277 int32_t ret = proxy_->GetAllowedUsbDevices(admin, usbDeviceIds);
278 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
279 ASSERT_TRUE(usbDeviceIds.empty());
280 }
281
282 /**
283 * @tc.name: TestSetUsbStorageDeviceAccessPolicySuc
284 * @tc.desc: Test SetUsbStorageDeviceAccessPolicy success func.
285 * @tc.type: FUNC
286 */
287 HWTEST_F(UsbManagerProxyTest, TestSetUsbStorageDeviceAccessPolicySuc, TestSize.Level1)
288 {
289 OHOS::AppExecFwk::ElementName admin;
290 admin.SetBundleName(ADMIN_PACKAGENAME);
291 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
292 .Times(1)
293 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
294 int32_t ret = proxy_->SetUsbStorageDeviceAccessPolicy(admin, 2);
295 ASSERT_TRUE(ret == ERR_OK);
296 }
297
298 /**
299 * @tc.name: TestSetUsbStorageDeviceAccessPolicyFail
300 * @tc.desc: Test SetUsbStorageDeviceAccessPolicy without enable edm service func.
301 * @tc.type: FUNC
302 */
303 HWTEST_F(UsbManagerProxyTest, TestSetUsbStorageDeviceAccessPolicyFail, TestSize.Level1)
304 {
305 Utils::SetEdmServiceDisable();
306 OHOS::AppExecFwk::ElementName admin;
307 admin.SetBundleName(ADMIN_PACKAGENAME);
308 int32_t ret = proxy_->SetUsbStorageDeviceAccessPolicy(admin, 2);
309 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
310 }
311
312 /**
313 * @tc.name: TestGetUsbStorageDeviceAccessPolicySuc
314 * @tc.desc: Test GetUsbStorageDeviceAccessPolicy func.
315 * @tc.type: FUNC
316 */
317 HWTEST_F(UsbManagerProxyTest, TestGetUsbStorageDeviceAccessPolicySuc, TestSize.Level1)
318 {
319 OHOS::AppExecFwk::ElementName admin;
320 admin.SetBundleName(ADMIN_PACKAGENAME);
321 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
322 .Times(1)
323 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeIntSendRequestGetPolicy));
324
325 int32_t policy = 0;
326 int32_t ret = proxy_->GetUsbStorageDeviceAccessPolicy(admin, policy);
327 ASSERT_TRUE(ret == ERR_OK);
328 ASSERT_TRUE(policy == 0);
329 }
330
331 /**
332 * @tc.name: TestGetUsbStorageDeviceAccessPolicyFail
333 * @tc.desc: Test GetUsbStorageDeviceAccessPolicy func without enable edm service.
334 * @tc.type: FUNC
335 */
336 HWTEST_F(UsbManagerProxyTest, TestGetUsbStorageDeviceAccessPolicyFail, TestSize.Level1)
337 {
338 Utils::SetEdmServiceDisable();
339 OHOS::AppExecFwk::ElementName admin;
340 admin.SetBundleName(ADMIN_PACKAGENAME);
341 int32_t policy = 0;
342 int32_t ret = proxy_->GetUsbStorageDeviceAccessPolicy(admin, policy);
343 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
344 }
345
346 /**
347 * @tc.name: TestAddOrRemoveDisallowedUsbDevicesAddSuc
348 * @tc.desc: Test AddOrRemoveDisallowedUsbDevices add success func.
349 * @tc.type: FUNC
350 */
351 HWTEST_F(UsbManagerProxyTest, TestAddOrRemoveDisallowedUsbDevicesAddSuc, TestSize.Level1)
352 {
353 OHOS::AppExecFwk::ElementName admin;
354 admin.SetBundleName(ADMIN_PACKAGENAME);
355 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
356 .Times(1)
357 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
358 std::vector<USB::UsbDeviceType> usbDeviceTypes;
359 USB::UsbDeviceType type;
360 type.baseClass = 3;
361 type.subClass = 1;
362 type.protocol = 2;
363 type.isDeviceType = false;
364 usbDeviceTypes.push_back(type);
365 int32_t ret = proxy_->AddOrRemoveDisallowedUsbDevices(admin, usbDeviceTypes, true);
366 ASSERT_TRUE(ret == ERR_OK);
367 }
368
369 /**
370 * @tc.name: TestAddOrRemoveDisallowedUsbDevicesAddFail
371 * @tc.desc: Test AddOrRemoveDisallowedUsbDevices for adding without enable edm service func.
372 * @tc.type: FUNC
373 */
374 HWTEST_F(UsbManagerProxyTest, TestAddOrRemoveDisallowedUsbDevicesAddFail, TestSize.Level1)
375 {
376 Utils::SetEdmServiceDisable();
377 OHOS::AppExecFwk::ElementName admin;
378 admin.SetBundleName(ADMIN_PACKAGENAME);
379 std::vector<USB::UsbDeviceType> usbDeviceTypes;
380 USB::UsbDeviceType type;
381 type.baseClass = 3;
382 type.subClass = 1;
383 type.protocol = 2;
384 type.isDeviceType = false;
385 usbDeviceTypes.push_back(type);
386 int32_t ret = proxy_->AddOrRemoveDisallowedUsbDevices(admin, usbDeviceTypes, true);
387 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
388 }
389
390 /**
391 * @tc.name: TestAddOrRemoveDisallowedUsbDevicesRemoveSuc
392 * @tc.desc: Test AddOrRemoveDisallowedUsbDevices remove success func.
393 * @tc.type: FUNC
394 */
395 HWTEST_F(UsbManagerProxyTest, TestAddOrRemoveDisallowedUsbDevicesRemoveSuc, TestSize.Level1)
396 {
397 OHOS::AppExecFwk::ElementName admin;
398 admin.SetBundleName(ADMIN_PACKAGENAME);
399 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
400 .Times(1)
401 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
402 std::vector<USB::UsbDeviceType> usbDeviceTypes;
403 USB::UsbDeviceType type;
404 type.baseClass = 3;
405 type.subClass = 1;
406 type.protocol = 2;
407 type.isDeviceType = false;
408 usbDeviceTypes.push_back(type);
409 int32_t ret = proxy_->AddOrRemoveDisallowedUsbDevices(admin, usbDeviceTypes, false);
410 ASSERT_TRUE(ret == ERR_OK);
411 }
412
413 /**
414 * @tc.name: TestAddOrRemoveDisallowedUsbDevicesRemoveFail
415 * @tc.desc: Test AddOrRemoveDisallowedUsbDevices for removing without enable edm service func.
416 * @tc.type: FUNC
417 */
418 HWTEST_F(UsbManagerProxyTest, TestAddOrRemoveDisallowedUsbDevicesRemoveFail, TestSize.Level1)
419 {
420 Utils::SetEdmServiceDisable();
421 OHOS::AppExecFwk::ElementName admin;
422 admin.SetBundleName(ADMIN_PACKAGENAME);
423 std::vector<USB::UsbDeviceType> usbDeviceTypes;
424 USB::UsbDeviceType type;
425 type.baseClass = 3;
426 type.subClass = 1;
427 type.protocol = 2;
428 type.isDeviceType = false;
429 usbDeviceTypes.push_back(type);
430 int32_t ret = proxy_->AddOrRemoveDisallowedUsbDevices(admin, usbDeviceTypes, false);
431 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
432 }
433
434 /**
435 * @tc.name: TestGetDisallowedUsbDevicesSuc
436 * @tc.desc: Test GetDisallowedUsbDevices func.
437 * @tc.type: FUNC
438 */
439 HWTEST_F(UsbManagerProxyTest, TestGetDisallowedUsbDevicesSuc, TestSize.Level1)
440 {
441 OHOS::AppExecFwk::ElementName admin;
442 admin.SetBundleName(ADMIN_PACKAGENAME);
443 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
444 .Times(1)
445 .WillOnce(Invoke(object_.GetRefPtr(),
446 &EnterpriseDeviceMgrStubMock::InvokeDisallowedUsbDevicesSendRequestGetPolicy));
447 std::vector<USB::UsbDeviceType> result;
448 int32_t ret = proxy_->GetDisallowedUsbDevices(admin, result);
449 ASSERT_TRUE(ret == ERR_OK);
450 ASSERT_TRUE(result.size() == 1);
451 }
452
453 /**
454 * @tc.name: TestGetDisallowedUsbDevicesFail
455 * @tc.desc: Test GetDisallowedUsbDevices func without enable edm service.
456 * @tc.type: FUNC
457 */
458 HWTEST_F(UsbManagerProxyTest, TestGetDisallowedUsbDevicesFail, TestSize.Level1)
459 {
460 Utils::SetEdmServiceDisable();
461 OHOS::AppExecFwk::ElementName admin;
462 admin.SetBundleName(ADMIN_PACKAGENAME);
463 std::vector<USB::UsbDeviceType> result;
464 int32_t ret = proxy_->GetDisallowedUsbDevices(admin, result);
465 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
466 ASSERT_TRUE(result.empty());
467 }
468 } // namespace TEST
469 } // namespace EDM
470 } // namespace OHOS
471