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 "usb_core_mock_test.h"
17
18 #include <csignal>
19 #include <cstring>
20 #include <iostream>
21 #include <vector>
22
23 #include "hilog_wrapper.h"
24 #include "ipc_skeleton.h"
25 #include "iservice_registry.h"
26 #include "string_ex.h"
27 #include "system_ability_definition.h"
28 #include "usb_common_test.h"
29 #include "usb_errors.h"
30 #include "usb_service.h"
31 #include "usb_srv_client.h"
32 #include "usb_srv_support.h"
33
34 using OHOS::HDI::Usb::V1_0::PortInfo;
35 using namespace OHOS;
36 using namespace OHOS::USB;
37 using namespace OHOS::USB::Common;
38 using namespace std;
39 using namespace testing::ext;
40 using ::testing::Eq;
41 using ::testing::Exactly;
42 using ::testing::Ge;
43 using ::testing::Le;
44 using ::testing::Ne;
45 using ::testing::Return;
46
47 namespace OHOS {
48 namespace USB {
49 constexpr int32_t USB_FUNCTION_INVALID = -1;
50 constexpr int32_t USB_PORT_ID_INVALID = 5;
51 constexpr int32_t USB_POWER_ROLE_INVALID = 5;
52 constexpr int32_t USB_DATA_ROLE_INVALID = 5;
53 sptr<MockUsbImpl> UsbCoreMockTest::mockUsbImpl_ = nullptr;
54 sptr<UsbService> UsbCoreMockTest::usbSrv_ = nullptr;
55
GetBundleName(std::string & bundleName)56 bool UsbCoreMockTest::GetBundleName(std::string &bundleName)
57 {
58 pid_t uid = IPCSkeleton::GetCallingUid();
59 sptr<ISystemAbilityManager> systemAbilityManager =
60 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
61 if (systemAbilityManager == nullptr) {
62 USB_HILOGE(MODULE_USB_SERVICE, "systemAbilityManager is nullptr");
63 return false;
64 }
65 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
66 if (remoteObject == nullptr) {
67 USB_HILOGE(MODULE_USB_SERVICE, "remoteObject is nullptr");
68 return false;
69 }
70
71 sptr<AppExecFwk::IBundleMgr> bundleMgr(new AppExecFwk::BundleMgrProxy(remoteObject));
72 if (bundleMgr == nullptr) {
73 USB_HILOGE(MODULE_USB_SERVICE, "bundleMgr is nullptr");
74 return false;
75 }
76 bundleMgr->GetNameForUid(uid, bundleName);
77 return true;
78 }
79
SetUpTestCase(void)80 void UsbCoreMockTest::SetUpTestCase(void)
81 {
82 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest UsbCoreMockTest");
83 UsbCommonTest::SetTestCaseHapApply();
84
85 usbSrv_ = DelayedSpSingleton<UsbService>::GetInstance();
86 EXPECT_NE(usbSrv_, nullptr);
87 mockUsbImpl_ = DelayedSpSingleton<MockUsbImpl>::GetInstance();
88 EXPECT_NE(mockUsbImpl_, nullptr);
89
90 usbSrv_->SetUsbd(mockUsbImpl_);
91
92 sptr<UsbServiceSubscriber> iSubscriber = new UsbServiceSubscriber();
93 EXPECT_NE(iSubscriber, nullptr);
94 mockUsbImpl_->BindUsbdSubscriber(iSubscriber);
95
96 EXPECT_CALL(*mockUsbImpl_, GetCurrentFunctions(testing::_)).WillRepeatedly(Return(0));
97 USBDeviceInfo info = {ACT_UPDEVICE, BUS_NUM_OK, DEV_ADDR_OK};
98 auto ret = mockUsbImpl_->SubscriberDeviceEvent(info);
99 EXPECT_EQ(0, ret);
100 }
101
TearDownTestCase(void)102 void UsbCoreMockTest::TearDownTestCase(void)
103 {
104 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest TearDownTestCase");
105 USBDeviceInfo info = {ACT_DOWNDEVICE, BUS_NUM_OK, DEV_ADDR_OK};
106 auto ret = mockUsbImpl_->SubscriberDeviceEvent(info);
107 EXPECT_EQ(0, ret);
108
109 mockUsbImpl_->UnbindUsbdSubscriber(nullptr);
110 sptr<IUsbInterface> usbd = IUsbInterface::Get();
111 usbSrv_->SetUsbd(usbd);
112
113 mockUsbImpl_ = nullptr;
114 usbSrv_ = nullptr;
115 DelayedSpSingleton<UsbService>::DestroyInstance();
116 DelayedSpSingleton<MockUsbImpl>::DestroyInstance();
117 }
118
SetUp(void)119 void UsbCoreMockTest::SetUp(void) {}
120
TearDown(void)121 void UsbCoreMockTest::TearDown(void) {}
122
123 /**
124 * @tc.name: GetCurrentFunctions001
125 * @tc.desc: Test functions to GetCurrentFunctions()
126 * @tc.desc: Positive test: parameters correctly
127 * @tc.type: FUNC
128 */
129 HWTEST_F(UsbCoreMockTest, GetCurrentFunctions001, TestSize.Level1)
130 {
131 USB_HILOGI(MODULE_USB_SERVICE, "Case Start : GetCurrentFunctions001 : SetConfig");
132 int32_t funcs = static_cast<int32_t>(UsbSrvSupport::FUNCTION_NONE);
133 EXPECT_CALL(*mockUsbImpl_, GetCurrentFunctions(testing::_)).WillRepeatedly(Return(0));
134 auto ret = usbSrv_->GetCurrentFunctions(funcs);
135 EXPECT_EQ(0, ret);
136 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::ret=%{public}d", ret);
137 }
138
139 /**
140 * @tc.name: SetCurrentFunctions001
141 * @tc.desc: Test functions to SetCurrentFunctions(int32_t funcs)
142 * @tc.desc: Positive test: parameters correctly
143 * @tc.type: FUNC
144 */
145 HWTEST_F(UsbCoreMockTest, SetCurrentFunctions001, TestSize.Level1)
146 {
147 EXPECT_CALL(*mockUsbImpl_, SetCurrentFunctions(testing::_)).WillRepeatedly(Return(0));
148 int32_t isok = usbSrv_->SetCurrentFunctions(UsbSrvSupport::FUNCTION_ACM);
149 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::SetCurrentFunctions=%{public}d", isok);
150 ASSERT_EQ(0, isok);
151 }
152
153 /**
154 * @tc.name: SetCurrentFunctions002
155 * @tc.desc: Test functions to SetCurrentFunctions(int32_t funcs)
156 * @tc.desc: Positive test: parameters correctly
157 * @tc.type: FUNC
158 */
159 HWTEST_F(UsbCoreMockTest, SetCurrentFunctions002, TestSize.Level1)
160 {
161 EXPECT_CALL(*mockUsbImpl_, SetCurrentFunctions(testing::_)).WillRepeatedly(Return(0));
162 int32_t isok = usbSrv_->SetCurrentFunctions(UsbSrvSupport::FUNCTION_ECM);
163 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::SetCurrentFunctions=%{public}d", isok);
164 ASSERT_EQ(0, isok);
165 }
166
167 /**
168 * @tc.name: SetCurrentFunctions003
169 * @tc.desc: Test functions to SetCurrentFunctions(int32_t funcs)
170 * @tc.desc: Positive test: parameters correctly
171 * @tc.type: FUNC
172 */
173 HWTEST_F(UsbCoreMockTest, SetCurrentFunctions003, TestSize.Level1)
174 {
175 int32_t funcs = UsbSrvSupport::FUNCTION_ACM | UsbSrvSupport::FUNCTION_ECM;
176 EXPECT_CALL(*mockUsbImpl_, SetCurrentFunctions(testing::_)).WillRepeatedly(Return(0));
177 int32_t isok = usbSrv_->SetCurrentFunctions(funcs);
178 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::SetCurrentFunctions=%{public}d", isok);
179 ASSERT_EQ(0, isok);
180 }
181
182 /**
183 * @tc.name: SetCurrentFunctions004
184 * @tc.desc: Test functions to SetCurrentFunctions(int32_t funcs)
185 * @tc.desc: Positive test: parameters correctly
186 * @tc.type: FUNC
187 */
188 HWTEST_F(UsbCoreMockTest, SetCurrentFunctions004, TestSize.Level1)
189 {
190 EXPECT_CALL(*mockUsbImpl_, SetCurrentFunctions(testing::_)).WillRepeatedly(Return(0));
191 int32_t isok = usbSrv_->SetCurrentFunctions(UsbSrvSupport::FUNCTION_HDC);
192 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::SetCurrentFunctions=%{public}d", isok);
193 ASSERT_EQ(0, isok);
194 }
195
196 /**
197 * @tc.name: SetCurrentFunctions005
198 * @tc.desc: Test functions to SetCurrentFunctions(int32_t funcs)
199 * @tc.desc: Positive test: parameters correctly
200 * @tc.type: FUNC
201 */
202 HWTEST_F(UsbCoreMockTest, SetCurrentFunctions005, TestSize.Level1)
203 {
204 int32_t funcs = UsbSrvSupport::FUNCTION_ACM | UsbSrvSupport::FUNCTION_HDC;
205 EXPECT_CALL(*mockUsbImpl_, SetCurrentFunctions(testing::_)).WillRepeatedly(Return(0));
206 int32_t isok = usbSrv_->SetCurrentFunctions(funcs);
207 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::SetCurrentFunctions=%{public}d", isok);
208 ASSERT_EQ(0, isok);
209 }
210
211 /**
212 * @tc.name: SetCurrentFunctions006
213 * @tc.desc: Test functions to SetCurrentFunctions(int32_t funcs)
214 * @tc.desc: Positive test: parameters correctly
215 * @tc.type: FUNC
216 */
217 HWTEST_F(UsbCoreMockTest, SetCurrentFunctions006, TestSize.Level1)
218 {
219 int32_t funcs = UsbSrvSupport::FUNCTION_ECM | UsbSrvSupport::FUNCTION_HDC;
220 EXPECT_CALL(*mockUsbImpl_, SetCurrentFunctions(testing::_)).WillRepeatedly(Return(0));
221 int32_t isok = usbSrv_->SetCurrentFunctions(funcs);
222 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::SetCurrentFunctions=%{public}d", isok);
223 ASSERT_EQ(0, isok);
224 }
225
226 /**
227 * @tc.name: SetCurrentFunctions007
228 * @tc.desc: Test functions to SetCurrentFunctions(int32_t funcs)
229 * @tc.desc: Negative test: parameters exception, funcs error
230 * @tc.type: FUNC
231 */
232 HWTEST_F(UsbCoreMockTest, SetCurrentFunctions007, TestSize.Level1)
233 {
234 EXPECT_CALL(*mockUsbImpl_, SetCurrentFunctions(testing::_)).WillRepeatedly(Return(RETVAL_INVALID));
235 int32_t isok = usbSrv_->SetCurrentFunctions(UsbSrvSupport::FUNCTION_MTP);
236 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::SetCurrentFunctions=%{public}d", isok);
237 ASSERT_NE(0, isok);
238 }
239
240 /**
241 * @tc.name: SetCurrentFunctions008
242 * @tc.desc: Test functions to SetCurrentFunctions(int32_t funcs)
243 * @tc.desc: Positive test: parameters correctly
244 * @tc.type: FUNC
245 */
246 HWTEST_F(UsbCoreMockTest, SetCurrentFunctions008, TestSize.Level1)
247 {
248 EXPECT_CALL(*mockUsbImpl_, SetCurrentFunctions(testing::_)).WillRepeatedly(Return(0));
249 int32_t isok = usbSrv_->SetCurrentFunctions(UsbSrvSupport::FUNCTION_NONE);
250 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::SetCurrentFunctions=%{public}d", isok);
251 ASSERT_EQ(0, isok);
252
253 EXPECT_CALL(*mockUsbImpl_, SetCurrentFunctions(testing::_)).WillRepeatedly(Return(0));
254 isok = usbSrv_->SetCurrentFunctions(UsbSrvSupport::FUNCTION_HDC);
255 ASSERT_EQ(0, isok);
256 }
257
258 /**
259 * @tc.name: UsbFunctionsFromString001
260 * @tc.desc: Test functions to UsbFunctionsFromString(string funcs)
261 * @tc.desc: Positive test: parameters correctly
262 * @tc.type: FUNC
263 */
264 HWTEST_F(UsbCoreMockTest, UsbFunctionsFromString001, TestSize.Level1)
265 {
266 int32_t funcCode = usbSrv_->UsbFunctionsFromString(UsbSrvSupport::FUNCTION_NAME_NONE);
267 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::functionsFromString=%{public}d", funcCode);
268 ASSERT_NE(UEC_SERVICE_INVALID_VALUE, funcCode);
269 }
270
271 /**
272 * @tc.name: UsbFunctionsFromString002
273 * @tc.desc: Test functions to UsbFunctionsFromString(string funcs)
274 * @tc.desc: Positive test: parameters correctly
275 * @tc.type: FUNC
276 */
277 HWTEST_F(UsbCoreMockTest, UsbFunctionsFromString002, TestSize.Level1)
278 {
279 int32_t funcCode = usbSrv_->UsbFunctionsFromString(UsbSrvSupport::FUNCTION_NAME_HDC);
280 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::functionsFromString=%{public}d", funcCode);
281 ASSERT_NE(UEC_SERVICE_INVALID_VALUE, funcCode);
282 }
283
284 /**
285 * @tc.name: UsbFunctionsFromString003
286 * @tc.desc: Test functions to UsbFunctionsFromString(string funcs)
287 * @tc.desc: Positive test: parameters correctly
288 * @tc.type: FUNC
289 */
290 HWTEST_F(UsbCoreMockTest, UsbFunctionsFromString003, TestSize.Level1)
291 {
292 int32_t funcCode = usbSrv_->UsbFunctionsFromString(UsbSrvSupport::FUNCTION_NAME_ACM);
293 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::functionsFromString=%{public}d", funcCode);
294 ASSERT_NE(UEC_SERVICE_INVALID_VALUE, funcCode);
295 }
296
297 /**
298 * @tc.name: UsbFunctionsFromString004
299 * @tc.desc: Test functions to UsbFunctionsFromString(string funcs)
300 * @tc.desc: Positive test: parameters correctly
301 * @tc.type: FUNC
302 */
303 HWTEST_F(UsbCoreMockTest, UsbFunctionsFromString004, TestSize.Level1)
304 {
305 int32_t funcCode = usbSrv_->UsbFunctionsFromString(UsbSrvSupport::FUNCTION_NAME_ECM);
306 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::functionsFromString=%{public}d", funcCode);
307 ASSERT_NE(UEC_SERVICE_INVALID_VALUE, funcCode);
308 }
309
310 /**
311 * @tc.name: UsbFunctionsFromString005
312 * @tc.desc: Test functions to UsbFunctionsFromString(string funcs)
313 * @tc.desc: Negative test: parameters exception, funcs error
314 * @tc.type: FUNC
315 */
316 HWTEST_F(UsbCoreMockTest, UsbFunctionsFromString005, TestSize.Level1)
317 {
318 std::string funcs = "qwerts";
319 int32_t funcCode = usbSrv_->UsbFunctionsFromString(funcs);
320 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::functionsFromString=%{public}d", funcCode);
321 ASSERT_EQ(UEC_SERVICE_INVALID_VALUE, funcCode);
322 }
323
324 /**
325 * @tc.name: UsbFunctionsFromString006
326 * @tc.desc: Test functions to UsbFunctionsFromString(string funcs)
327 * @tc.desc: Negative test: parameters exception, funcs error
328 * @tc.type: FUNC
329 */
330 HWTEST_F(UsbCoreMockTest, UsbFunctionsFromString006, TestSize.Level1)
331 {
332 std::string funcs = "zxcbvx";
333 int32_t funcCode = usbSrv_->UsbFunctionsFromString(funcs);
334 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::functionsFromString=%{public}d", funcCode);
335 ASSERT_EQ(UEC_SERVICE_INVALID_VALUE, funcCode);
336 }
337
338 /**
339 * @tc.name: UsbFunctionsToString001
340 * @tc.desc: Test functions to UsbFunctionsToString(int32_t funcs)
341 * @tc.desc: Positive test: parameters correctly
342 * @tc.type: FUNC
343 */
344 HWTEST_F(UsbCoreMockTest, UsbFunctionsToString001, TestSize.Level1)
345 {
346 std::string funcName = usbSrv_->UsbFunctionsToString(UsbSrvSupport::FUNCTION_NONE);
347 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::UsbFunctionsToString=%{public}s", funcName.c_str());
348 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::UsbFunctionsToString=%{public}zu", funcName.size());
349 ASSERT_TRUE(!(funcName.empty()));
350 }
351
352 /**
353 * @tc.name: UsbFunctionsToString002
354 * @tc.desc: Test functions to UsbFunctionsToString(int32_t funcs)
355 * @tc.desc: Positive test: parameters correctly
356 * @tc.type: FUNC
357 */
358 HWTEST_F(UsbCoreMockTest, UsbFunctionsToString002, TestSize.Level1)
359 {
360 std::string funcName = usbSrv_->UsbFunctionsToString(UsbSrvSupport::FUNCTION_HDC);
361 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::UsbFunctionsToString=%{public}s", funcName.c_str());
362 ASSERT_TRUE(!(funcName.empty()));
363 }
364
365 /**
366 * @tc.name: UsbFunctionsToString003
367 * @tc.desc: Test functions to UsbFunctionsToString(int32_t funcs)
368 * @tc.desc: Positive test: parameters correctly
369 * @tc.type: FUNC
370 */
371 HWTEST_F(UsbCoreMockTest, UsbFunctionsToString003, TestSize.Level1)
372 {
373 std::string funcName = usbSrv_->UsbFunctionsToString(UsbSrvSupport::FUNCTION_ACM);
374 USB_HILOGI(MODULE_USB_SERVICE, "UsbFunctionServiceTest::UsbFunctionsToString=%{public}s", funcName.c_str());
375 ASSERT_TRUE(!(funcName.empty()));
376 }
377
378 /**
379 * @tc.name: UsbFunctionsToString004
380 * @tc.desc: Test functions to UsbFunctionsToString(int32_t funcs)
381 * @tc.desc: Positive test: parameters correctly
382 * @tc.type: FUNC
383 */
384 HWTEST_F(UsbCoreMockTest, UsbFunctionsToString004, TestSize.Level1)
385 {
386 std::string funcName = usbSrv_->UsbFunctionsToString(UsbSrvSupport::FUNCTION_ECM);
387 USB_HILOGI(MODULE_USB_SERVICE, "UsbFunctionServiceTest::UsbFunctionsToString=%{public}s", funcName.c_str());
388 ASSERT_TRUE(!(funcName.empty()));
389 }
390
391 /**
392 * @tc.name: UsbFunctionsToString005
393 * @tc.desc: Test functions to UsbFunctionsToString(int32_t funcs)
394 * @tc.desc: Negative test: parameters exception, funcs error
395 * @tc.type: FUNC
396 */
397 HWTEST_F(UsbCoreMockTest, UsbFunctionsToString005, TestSize.Level1)
398 {
399 std::string funcName = usbSrv_->UsbFunctionsToString(USB_FUNCTION_INVALID);
400 USB_HILOGI(MODULE_USB_SERVICE, "UsbFunctionServiceTest::UsbFunctionsToString=%{public}s", funcName.c_str());
401 ASSERT_TRUE(!(funcName.empty()));
402 }
403
404 /**
405 * @tc.name: UsbHasRight001
406 * @tc.desc: bool HasRight(const std::string deviceName)
407 * @tc.desc: Negative test: first AddRight ,second HasRight
408 * @tc.type: FUNC
409 */
410 HWTEST_F(UsbCoreMockTest, UsbHasRight001, TestSize.Level1)
411 {
412 USB_HILOGI(MODULE_USB_SERVICE, "Case Start : UsbHasRight001: SetConfig");
413 std::string deviceName = "device_80";
414 bool result = usbSrv_->HasRight(deviceName);
415 ASSERT_TRUE(result);
416 }
417
418 /**
419 * @tc.name: UsbHasRight002
420 * @tc.desc: bool HasRight(const std::string deviceName)
421 * @tc.desc: Positive test: program correctly
422 * @tc.type: FUNC
423 */
424 HWTEST_F(UsbCoreMockTest, UsbHasRight002, TestSize.Level1)
425 {
426 USB_HILOGI(MODULE_USB_SERVICE, "Case Start : UsbHasRight002: SetConfig");
427 std::string deviceName = "device_80";
428 bool result = usbSrv_->HasRight(deviceName);
429 ASSERT_TRUE(result);
430
431 std::string bundleName;
432 UsbCoreMockTest::GetBundleName(bundleName);
433 // AddRight is called for system app,and third-party app should use RequestRight
434 usbSrv_->AddRight(bundleName, deviceName);
435
436 result = usbSrv_->HasRight(deviceName);
437 ASSERT_TRUE(result);
438 }
439
440 /**
441 * @tc.name: UsbHasRight003
442 * @tc.desc: bool HasRight(const std::string deviceName)
443 * @tc.desc: Negative test: first AddRight ,second HasRight
444 * @tc.type: FUNC
445 */
446 HWTEST_F(UsbCoreMockTest, UsbHasRight003, TestSize.Level1)
447 {
448 USB_HILOGI(MODULE_USB_SERVICE, "Case Start : UsbHasRight003: SetConfig");
449 std::string deviceName = "device_80";
450 bool result = usbSrv_->HasRight(deviceName);
451 ASSERT_TRUE(result);
452
453 int32_t ret = usbSrv_->RemoveRight(deviceName);
454 ASSERT_EQ(0, ret);
455
456 deviceName = "device_81";
457 result = usbSrv_->HasRight(deviceName);
458 ASSERT_TRUE(result);
459 }
460
461 /**
462 * @tc.name: UsbHasRight004
463 * @tc.desc: bool HasRight(const std::string deviceName)
464 * @tc.desc: Positive test: program correctly
465 * @tc.type: FUNC
466 */
467 HWTEST_F(UsbCoreMockTest, UsbHasRight004, TestSize.Level1)
468 {
469 USB_HILOGI(MODULE_USB_SERVICE, "Case Start : UsbHasRight004: SetConfig");
470 std::string deviceName = "device_82";
471 bool result = usbSrv_->HasRight(deviceName);
472 ASSERT_TRUE(result);
473
474 std::string bundleName;
475 UsbCoreMockTest::GetBundleName(bundleName);
476 // AddRight is called for system app,and third-party app should use RequestRight
477 usbSrv_->AddRight(bundleName, deviceName);
478
479 result = usbSrv_->HasRight(deviceName);
480 ASSERT_TRUE(result);
481
482 int32_t ret = usbSrv_->RemoveRight(deviceName);
483 ASSERT_EQ(0, ret);
484 }
485
486 /**
487 * @tc.name: UsbAddRight001
488 * @tc.desc: Test functions of requestright
489 * @tc.desc: int32_t requestRight(const std::string deviceName)
490 * @tc.desc: RequestRight then RemoveRight
491 * @tc.type: FUNC
492 */
493 HWTEST_F(UsbCoreMockTest, UsbAddRight001, TestSize.Level1)
494 {
495 USB_HILOGI(MODULE_USB_SERVICE, "Case Start : Usbrequestright001: SetConfig");
496 std::string deviceName = "device_83";
497 std::string bundleName;
498 UsbCoreMockTest::GetBundleName(bundleName);
499 // AddRight is called for system app,and third-party app should use RequestRight
500 usbSrv_->AddRight(bundleName, deviceName);
501
502 bool result = usbSrv_->HasRight(deviceName);
503 ASSERT_TRUE(result);
504
505 int32_t ret = usbSrv_->RemoveRight(deviceName);
506 ASSERT_EQ(0, ret);
507 }
508
509 /**
510 * @tc.name: UsbAddRight002
511 * @tc.desc: Test functions of requestright
512 * @tc.desc: int32_t requestRight(const std::string deviceName)
513 * @tc.type: FUNC
514 */
515 HWTEST_F(UsbCoreMockTest, UsbAddRight002, TestSize.Level1)
516 {
517 USB_HILOGI(MODULE_USB_SERVICE, "Case Start : UsbAddRight002: SetConfig");
518 std::string deviceName = "device_81";
519 bool result = usbSrv_->HasRight(deviceName);
520 ASSERT_TRUE(result);
521
522 std::string bundleName;
523 UsbCoreMockTest::GetBundleName(bundleName);
524 // AddRight is called for system app,and third-party app should use RequestRight
525 usbSrv_->AddRight(bundleName, deviceName);
526
527 result = usbSrv_->HasRight(deviceName);
528 ASSERT_TRUE(result);
529
530 int32_t ret = usbSrv_->RemoveRight(deviceName);
531 ASSERT_EQ(0, ret);
532 }
533
534 /**
535 * @tc.name: GetPorts001
536 * @tc.desc: Test functions to GetPorts
537 * @tc.desc: int32_t GetPorts(std::vector<UsbPort *> &usbports);
538 * @tc.desc: positive:the parameters are correct
539 * @tc.type: FUNC
540 */
541 HWTEST_F(UsbCoreMockTest, GetPorts001, TestSize.Level1)
542 {
543 std::vector<UsbPort> portList;
544 auto ports = usbSrv_->GetPorts(portList);
545 USB_HILOGD(MODULE_USB_SERVICE, "Get UsbPort size=%{public}zu", portList.size());
546 ASSERT_EQ(0, ports);
547 }
548
549 /**
550 * @tc.name: GetSupportedModes001
551 * @tc.desc: Test functions to GetSupportedModes
552 * @tc.desc: int32_t GetSupportedModes(int32_t portId, int32_t &result);
553 * @tc.desc: nagative: portid eroor
554 * @tc.type: FUNC
555 */
556 HWTEST_F(UsbCoreMockTest, GetSupportedModes001, TestSize.Level1)
557 {
558 int32_t result;
559 auto modes = usbSrv_->GetSupportedModes(UsbSrvSupport::PORT_MODE_NONE, result);
560 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::GetSupportedModes001 status=%{public}d", modes);
561 ASSERT_NE(modes, 0);
562 }
563
564 /**
565 * @tc.name: GetSupportedModes002
566 * @tc.desc: Test functions to GetSupportedModes
567 * @tc.desc: int32_t GetSupportedModes(int32_t portId, int32_t &result);
568 * @tc.desc: nagative:portid eroor
569 * @tc.type: FUNC
570 */
571 HWTEST_F(UsbCoreMockTest, GetSupportedModes002, TestSize.Level1)
572 {
573 int32_t result;
574 auto modes = usbSrv_->GetSupportedModes(USB_PORT_ID_INVALID, result);
575 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::GetSupportedModes004 status=%{public}d", modes);
576 ASSERT_NE(modes, 0);
577 }
578
579 /**
580 * @tc.name: GetSupportedModes003
581 * @tc.desc: Test functions to GetSupportedModes
582 * @tc.desc: int32_t GetSupportedModes(int32_t portId, int32_t &result);
583 * @tc.desc: nagative:portid eroor
584 * @tc.type: FUNC
585 */
586 HWTEST_F(UsbCoreMockTest, GetSupportedModes003, TestSize.Level1)
587 {
588 int32_t result = 0;
589 auto modes = usbSrv_->GetSupportedModes(0xFFFFFFFF, result);
590 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::GetSupportedModes004 status=%{public}d", modes);
591 ASSERT_NE(modes, 0);
592 }
593
594 /**
595 * @tc.name: GetSupportedModes004
596 * @tc.desc: Test functions to GetSupportedModes
597 * @tc.desc: int32_t GetSupportedModes(int32_t portId, int32_t &result);
598 * @tc.desc: positive:the parameters are correct
599 * @tc.type: FUNC
600 */
601 HWTEST_F(UsbCoreMockTest, GetSupportedModes004, TestSize.Level1)
602 {
603 int32_t result;
604 auto modes = usbSrv_->GetSupportedModes(UsbSrvSupport::PORT_MODE_DEVICE, result);
605 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::GetSupportedModes004 status=%{public}d", modes);
606 ASSERT_EQ(0, modes);
607 }
608
609 /**
610 * @tc.name: SetPortRole001
611 * @tc.desc: Test functions to SetPortRole
612 * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole);
613 * @tc.desc: positive:the parameters are correct
614 * @tc.type: FUNC
615 */
616 HWTEST_F(UsbCoreMockTest, SetPortRole001, TestSize.Level1)
617 {
618 auto ret = usbSrv_->SetPortRole(
619 UsbSrvSupport::PORT_MODE_DEVICE, UsbSrvSupport::POWER_ROLE_SOURCE, UsbSrvSupport::DATA_ROLE_HOST);
620 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::SetPortRole001 status=%{public}d", ret);
621 ASSERT_EQ(0, ret);
622 }
623
624 /**
625 * @tc.name: SetPortRole002
626 * @tc.desc: Test functions to SetPortRole
627 * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole);
628 * @tc.desc: nagative:portid error
629 * @tc.type: FUNC
630 */
631 HWTEST_F(UsbCoreMockTest, SetPortRole002, TestSize.Level1)
632 {
633 auto ret = usbSrv_->SetPortRole(
634 UsbSrvSupport::PORT_MODE_HOST, UsbSrvSupport::POWER_ROLE_SOURCE, UsbSrvSupport::DATA_ROLE_HOST);
635 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::SetPortRole002 status=%{public}d", ret);
636 ASSERT_NE(ret, 0);
637 }
638
639 /**
640 * @tc.name: SetPortRole003
641 * @tc.desc: Test functions to SetPortRole
642 * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole);
643 * @tc.desc: nagative:powerRole error
644 * @tc.type: FUNC
645 */
646 HWTEST_F(UsbCoreMockTest, SetPortRole003, TestSize.Level1)
647 {
648 auto ret = usbSrv_->SetPortRole(
649 UsbSrvSupport::PORT_MODE_DEVICE, USB_POWER_ROLE_INVALID, UsbSrvSupport::DATA_ROLE_DEVICE);
650 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::SetPortRole003 status=%{public}d", ret);
651 ASSERT_NE(ret, 0);
652 }
653
654 /**
655 * @tc.name: SetPortRole004
656 * @tc.desc: Test functions to SetPortRole
657 * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole);
658 * @tc.desc: nagative:dataRole error
659 * @tc.type: FUNC
660 */
661 HWTEST_F(UsbCoreMockTest, SetPortRole004, TestSize.Level1)
662 {
663 auto ret = usbSrv_->SetPortRole(
664 UsbSrvSupport::PORT_MODE_DEVICE, UsbSrvSupport::POWER_ROLE_SOURCE, USB_DATA_ROLE_INVALID);
665 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::SetPortRole004 status=%{public}d", ret);
666 ASSERT_NE(ret, 0);
667 }
668
669 /**
670 * @tc.name: SetPortRole005
671 * @tc.desc: Test functions to SetPortRole
672 * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole);
673 * @tc.desc: nagative:powerRole、dataRole error
674 * @tc.type: FUNC
675 */
676 HWTEST_F(UsbCoreMockTest, SetPortRole005, TestSize.Level1)
677 {
678 auto ret =
679 usbSrv_->SetPortRole(UsbSrvSupport::PORT_MODE_DEVICE, USB_POWER_ROLE_INVALID, USB_DATA_ROLE_INVALID);
680 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::SetPortRole005 status=%{public}d", ret);
681 ASSERT_NE(ret, 0);
682 }
683
684 /**
685 * @tc.name: SetPortRole006
686 * @tc.desc: Test functions to SetPortRole
687 * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole);
688 * @tc.desc: nagative:powerRole、dataRole error
689 * @tc.type: FUNC
690 */
691 HWTEST_F(UsbCoreMockTest, SetPortRole006, TestSize.Level1)
692 {
693 auto ret = usbSrv_->SetPortRole(USB_PORT_ID_INVALID, UsbSrvSupport::POWER_ROLE_SOURCE, USB_DATA_ROLE_INVALID);
694 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::SetPortRole006 status=%{public}d", ret);
695 ASSERT_NE(ret, 0);
696 }
697
698 /**
699 * @tc.name: SetPortRole007
700 * @tc.desc: Test functions to SetPortRole
701 * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole);
702 * @tc.desc: nagative:powerRole、dataRole error
703 * @tc.type: FUNC
704 */
705 HWTEST_F(UsbCoreMockTest, SetPortRole007, TestSize.Level1)
706 {
707 USB_HILOGI(MODULE_USB_SERVICE, "Case Start : SetPortRole007 : SetPortRole");
708 auto ret = usbSrv_->SetPortRole(UsbSrvSupport::PORT_MODE_HOST, USB_POWER_ROLE_INVALID, USB_DATA_ROLE_INVALID);
709 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::SetPortRole007 status=%{public}d", ret);
710 ASSERT_NE(ret, 0);
711 }
712
713 /**
714 * @tc.name: SetPortRole008
715 * @tc.desc: Test functions to SetPortRole
716 * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole);
717 * @tc.desc:
718 * @tc.type: FUNC
719 */
720 HWTEST_F(UsbCoreMockTest, SetPortRole008, TestSize.Level1)
721 {
722 auto ret = usbSrv_->SetPortRole(
723 UsbSrvSupport::PORT_MODE_DEVICE, UsbSrvSupport::DATA_ROLE_DEVICE, UsbSrvSupport::POWER_ROLE_SINK);
724 USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreMockTest::SetPortRole008 status=%{public}d", ret);
725 ASSERT_EQ(0, ret);
726 }
727 } // namespace USB
728 } // namespace OHOS
729