1 /*
2 * Copyright (c) 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 "usbd_device_status_test.h"
17
18 #include <iostream>
19 #include <vector>
20
21 #include "UsbSubscriberTest.h"
22 #include "hdf_log.h"
23 #include "v1_1/iusb_interface.h"
24
25 const int SLEEP_TIME = 3;
26 const uint8_t BUS_NUM_INVALID = 255;
27 const uint8_t DEV_ADDR_INVALID = 255;
28 const uint8_t INTERFACEID_OK = 1;
29 const uint8_t INTERFACEID_OK_NEW = 0;
30 const uint8_t INTERFACEID_INVALID = 255;
31
32 using namespace testing::ext;
33 using namespace OHOS;
34 using namespace OHOS::USB;
35 using namespace std;
36 using namespace OHOS::HDI::Usb::V1_1;
37 namespace OHOS {
38 namespace USB {
39 UsbDev UsbdDeviceStatusTest::dev_ = {0, 0};
40 sptr<UsbSubscriberTest> UsbdDeviceStatusTest::subscriber_ = nullptr;
41 sptr<OHOS::HDI::Usb::V1_1::IUsbInterface> g_usbInterface = nullptr;
42
SwitchErrCode(int32_t ret)43 int32_t SwitchErrCode(int32_t ret)
44 {
45 return ret == HDF_ERR_NOT_SUPPORT ? HDF_SUCCESS : ret;
46 }
47
SetUpTestCase(void)48 void UsbdDeviceStatusTest::SetUpTestCase(void)
49 {
50 g_usbInterface = OHOS::HDI::Usb::V1_1::IUsbInterface::Get();
51 if (g_usbInterface == nullptr) {
52 HDF_LOGE("%{public}s:IUsbInterface::Get() failed.", __func__);
53 exit(0);
54 }
55 auto ret = g_usbInterface->SetPortRole(1, 1, 1);
56 sleep(SLEEP_TIME);
57 HDF_LOGI("UsbdDeviceStatusTest::[Device] %{public}d SetPortRole=%{public}d", __LINE__, ret);
58 ret = SwitchErrCode(ret);
59 ASSERT_EQ(0, ret);
60 if (ret != 0) {
61 exit(0);
62 }
63
64 subscriber_ = new UsbSubscriberTest();
65 if (g_usbInterface->BindUsbdSubscriber(subscriber_) != HDF_SUCCESS) {
66 HDF_LOGE("%{public}s: bind usbd subscriber_ failed", __func__);
67 exit(0);
68 }
69
70 std::cout << "please connect device, press enter to continue" << std::endl;
71 int c;
72 while ((c = getchar()) != '\n' && c != EOF) {}
73 dev_ = {subscriber_->busNum_, subscriber_->devAddr_};
74
75 ret = g_usbInterface->OpenDevice(dev_);
76 HDF_LOGI("UsbdDeviceStatusTest::%{public}d OpenDevice=%{public}d", __LINE__, ret);
77 ASSERT_EQ(0, ret);
78 }
79
TearDownTestCase(void)80 void UsbdDeviceStatusTest::TearDownTestCase(void)
81 {
82 g_usbInterface->UnbindUsbdSubscriber(subscriber_);
83 dev_ = {subscriber_->busNum_, subscriber_->devAddr_};
84 auto ret = g_usbInterface->CloseDevice(dev_);
85 HDF_LOGI("UsbdDeviceStatusTest::%{public}d Close=%{public}d", __LINE__, ret);
86 ASSERT_EQ(0, ret);
87 }
88
SetUp(void)89 void UsbdDeviceStatusTest::SetUp(void) {}
90
TearDown(void)91 void UsbdDeviceStatusTest::TearDown(void) {}
92
93 /**
94 * @tc.name: SUB_USB_HostManager_HDI_Func_1000
95 * @tc.desc: Test functions to GetDeviceSpeed
96 * @tc.desc: int32_t GetDeviceSpeed(const UsbDev &dev, uint8_t interfaceId, uint8_t speed);
97 * @tc.desc: Positive test: parameters correctly
98 * @tc.type: FUNC
99 */
100 HWTEST_F(UsbdDeviceStatusTest, UsbdGetDeviceSpeed001, Function | MediumTest | Level1)
101 {
102 struct UsbDev dev = dev_;
103 int32_t ret = -1;
104 uint8_t speed = 0;
105 ret = g_usbInterface->GetDeviceSpeed(dev, speed);
106 HDF_LOGI("UsbdGetDeviceSpeed001 %{public}d GetDeviceSpeed=%{public}d, speed=%{public}d", __LINE__, ret, speed);
107 ASSERT_EQ(0, ret);
108 }
109
110 /**
111 * @tc.name: SUB_USB_HostManager_HDI_Compatibility_8000
112 * @tc.desc: Test functions to GetDeviceSpeed
113 * @tc.desc: int32_t GetDeviceSpeed(const UsbDev &dev, uint8_t interfaceId, uint8_t speed);
114 * @tc.desc: Negative test: parameters exception, busNum error
115 * @tc.type: FUNC
116 */
117 HWTEST_F(UsbdDeviceStatusTest, UsbdGetDeviceSpeed002, Function | MediumTest | Level1)
118 {
119 struct UsbDev dev = dev_;
120 dev.busNum = BUS_NUM_INVALID;
121 uint8_t speed = 0;
122 auto ret = g_usbInterface->GetDeviceSpeed(dev, speed);
123 HDF_LOGI("UsbdGetDeviceSpeed002 %{public}d ret=%{public}d, speed=%{public}d", __LINE__, ret, speed);
124 ASSERT_NE(ret, 0);
125 }
126
127
128 /**
129 * @tc.name: SUB_USB_HostManager_HDI_Compatibility_8100
130 * @tc.desc: Test functions to GetDeviceSpeed
131 * @tc.desc: int32_t GetDeviceSpeed(const UsbDev &dev, uint8_t interfaceId, uint8_t speed);
132 * @tc.desc: Negative test: parameters exception, devAddr error
133 * @tc.type: FUNC
134 */
135 HWTEST_F(UsbdDeviceStatusTest, UsbdGetDeviceSpeed003, Function | MediumTest | Level1)
136 {
137 struct UsbDev dev = {dev_.busNum, DEV_ADDR_INVALID};
138 uint8_t speed = 0;
139 auto ret = g_usbInterface->GetDeviceSpeed(dev, speed);
140 HDF_LOGI("UsbdGetDeviceSpeed003 %{public}d, ret=%{public}d, speed=%{public}d", __LINE__, ret, speed);
141 ASSERT_NE(ret, 0);
142 }
143
144 /**
145 * @tc.name: SUB_USB_HostManager_HDI_Compatibility_8200
146 * @tc.desc: Test functions to GetDeviceSpeed
147 * @tc.desc: int32_t GetDeviceSpeed(const UsbDev &dev, uint8_t interfaceId, uint8_t speed);
148 * @tc.desc: Negative test: parameters exception, busNum && devAddr error
149 * @tc.type: FUNC
150 */
151 HWTEST_F(UsbdDeviceStatusTest, UsbdGetDeviceSpeed004, Function | MediumTest | Level1)
152 {
153 uint8_t speed = 0;
154 struct UsbDev dev = {BUS_NUM_INVALID, DEV_ADDR_INVALID};
155 auto ret = g_usbInterface->GetDeviceSpeed(dev, speed);
156 HDF_LOGI("UsbdGetDeviceSpeed004 %{public}d, ret=%{public}d, speed=%{public}d", __LINE__, ret, speed);
157 ASSERT_NE(ret, 0);
158 }
159
160 /**
161 * @tc.name: SUB_USB_HostManager_HDI_Func_1100
162 * @tc.desc: Test functions to GetInterfaceActiveStatus
163 * @tc.desc: int32_t GetInterfaceActiveStatus(const UsbDev &dev, uint8_t interfaceId, bool unactived);
164 * @tc.desc: Positive test: parameters correctly
165 * @tc.type: FUNC
166 */
167 HWTEST_F(UsbdDeviceStatusTest, UsbdGetInterfaceActiveStatus001, Function | MediumTest | Level1)
168 {
169 uint8_t interfaceId = INTERFACEID_OK_NEW;
170 struct UsbDev dev = dev_;
171 int32_t ret = -1;
172 bool unactived = 1;
173 ret = g_usbInterface->ClaimInterface(dev, interfaceId, 1);
174 HDF_LOGI("UsbdDeviceStatusTest::UsbdGetInterfaceActiveStatus %{public}d ClaimInterface=%{public}d", __LINE__, ret);
175 ASSERT_EQ(0, ret);
176 for (; interfaceId < INTERFACEID_INVALID; interfaceId++) {
177 ret = g_usbInterface->GetInterfaceActiveStatus(dev, interfaceId, unactived);
178 if (ret == 0) {
179 break;
180 }
181 }
182 HDF_LOGI("UsbdGetInterfaceActiveStatus001 %{public}d GetInterfaceActiveStatus=%{public}d, unactived=%{public}d",
183 __LINE__, ret, unactived);
184 ASSERT_EQ(0, ret);
185 }
186
187 /**
188 * @tc.name: SUB_USB_HostManager_HDI_Compatibility_8300
189 * @tc.desc: Test functions to GetInterfaceActiveStatus
190 * @tc.desc: int32_t GetInterfaceActiveStatus(const UsbDev &dev, uint8_t interfaceId, bool unactived);
191 * @tc.desc: Negative test: parameters exception, busNum error
192 * @tc.type: FUNC
193 */
194 HWTEST_F(UsbdDeviceStatusTest, UsbdGetInterfaceActiveStatus002, Function | MediumTest | Level1)
195 {
196 uint8_t interfaceId = INTERFACEID_OK;
197 struct UsbDev dev = dev_;
198 dev.busNum = BUS_NUM_INVALID;
199 bool unactived = 1;
200 auto ret = g_usbInterface->GetInterfaceActiveStatus(dev, interfaceId, unactived);
201 HDF_LOGI("UsbdGetInterfaceActiveStatus002 %{public}d ret=%{public}d, unactived=%{public}d",
202 __LINE__, ret, unactived);
203 ASSERT_NE(ret, 0);
204 }
205
206 /**
207 * @tc.name: SUB_USB_HostManager_HDI_Compatibility_8400
208 * @tc.desc: Test functions to GetInterfaceActiveStatus
209 * @tc.desc: int32_t GetInterfaceActiveStatus(const UsbDev &dev, uint8_t interfaceId, bool unactived);
210 * @tc.desc: Negative test: parameters exception, devAddr error
211 * @tc.type: FUNC
212 */
213 HWTEST_F(UsbdDeviceStatusTest, UsbdGetInterfaceActiveStatus003, Function | MediumTest | Level1)
214 {
215 uint8_t interfaceId = INTERFACEID_OK;
216 struct UsbDev dev = {dev_.busNum, DEV_ADDR_INVALID};
217 bool unactived = 1;
218 auto ret = g_usbInterface->GetInterfaceActiveStatus(dev, interfaceId, unactived);
219 HDF_LOGI("UsbdGetInterfaceActiveStatus003 %{public}d, ret=%{public}d, unactived=%{public}d",
220 __LINE__, ret, unactived);
221 ASSERT_NE(ret, 0);
222 }
223
224 /**
225 * @tc.name: SUB_USB_HostManager_HDI_Compatibility_8500
226 * @tc.desc: Test functions to GetInterfaceActiveStatus
227 * @tc.desc: int32_t GetInterfaceActiveStatus(const UsbDev &dev, uint8_t interfaceId, bool unactived);
228 * @tc.desc: Negative test: parameters exception, interfaceid error
229 * @tc.type: FUNC
230 */
231 HWTEST_F(UsbdDeviceStatusTest, UsbdGetInterfaceActiveStatus004, Function | MediumTest | Level1)
232 {
233 uint8_t interfaceId = INTERFACEID_OK;
234 struct UsbDev dev = dev_;
235 interfaceId = INTERFACEID_INVALID;
236 bool unactived = 1;
237 auto ret = g_usbInterface->GetInterfaceActiveStatus(dev, interfaceId, unactived);
238 HDF_LOGI("UsbdGetInterfaceActiveStatus004 %{public}d, ret=%{public}d, unactived=%{public}d",
239 __LINE__, ret, unactived);
240 ASSERT_NE(ret, 0);
241 }
242
243 /**
244 * @tc.name: SUB_USB_HostManager_HDI_Compatibility_8600
245 * @tc.desc: Test functions to GetInterfaceActiveStatus
246 * @tc.desc: int32_t GetInterfaceActiveStatus(const UsbDev &dev, uint8_t interfaceId, bool unactived);
247 * @tc.desc: Negative test: parameters exception, busNum && devAddr error
248 * @tc.type: FUNC
249 */
250 HWTEST_F(UsbdDeviceStatusTest, UsbdGetInterfaceActiveStatus005, Function | MediumTest | Level1)
251 {
252 uint8_t interfaceId = INTERFACEID_OK;
253 bool unactived = 1;
254 struct UsbDev dev = {BUS_NUM_INVALID, DEV_ADDR_INVALID};
255 auto ret = g_usbInterface->GetInterfaceActiveStatus(dev, interfaceId, unactived);
256 HDF_LOGI("UsbdGetInterfaceActiveStatus005 %{public}d, ret=%{public}d, unactived=%{public}d",
257 __LINE__, ret, unactived);
258 ASSERT_NE(ret, 0);
259 }
260
261 /**
262 * @tc.name: SUB_USB_HostManager_HDI_Compatibility_8700
263 * @tc.desc: Test functions to GetInterfaceActiveStatus
264 * @tc.desc: int32_t GetInterfaceActiveStatus(const UsbDev &dev, uint8_t interfaceId, bool unactived);
265 * @tc.desc: Negative test: parameters exception, busNum && interfaceid error
266 * @tc.type: FUNC
267 */
268 HWTEST_F(UsbdDeviceStatusTest, UsbdGetInterfaceActiveStatus006, Function | MediumTest | Level1)
269 {
270 uint8_t interfaceId = INTERFACEID_INVALID;
271 bool unactived = 1;
272 struct UsbDev dev = {BUS_NUM_INVALID, dev_.devAddr};
273 auto ret = g_usbInterface->GetInterfaceActiveStatus(dev, interfaceId, unactived);
274 HDF_LOGI("UsbdGetInterfaceActiveStatus006 %{public}d, ret=%{public}d, unactived=%{public}d",
275 __LINE__, ret, unactived);
276 ASSERT_NE(ret, 0);
277 }
278
279 /**
280 * @tc.name: SUB_USB_HostManager_HDI_Compatibility_8800
281 * @tc.desc: Test functions to GetInterfaceActiveStatus
282 * @tc.desc: int32_t GetInterfaceActiveStatus(const UsbDev &dev, uint8_t interfaceId, bool unactived);
283 * @tc.desc: Negative test: parameters exception, devAddr && interfaceid error
284 * @tc.type: FUNC
285 */
286 HWTEST_F(UsbdDeviceStatusTest, UsbdGetInterfaceActiveStatus007, Function | MediumTest | Level1)
287 {
288 uint8_t interfaceId = INTERFACEID_INVALID;
289 bool unactived = 1;
290 struct UsbDev dev = {dev_.busNum, DEV_ADDR_INVALID};
291 auto ret = g_usbInterface->GetInterfaceActiveStatus(dev, interfaceId, unactived);
292 HDF_LOGI("UsbdGetInterfaceActiveStatus007 %{public}d, ret=%{public}d, unactived=%{public}d",
293 __LINE__, ret, unactived);
294 ASSERT_NE(ret, 0);
295 }
296
297 /**
298 * @tc.name: SUB_USB_HostManager_HDI_Compatibility_8900
299 * @tc.desc: Test functions to GetInterfaceActiveStatus
300 * @tc.desc: int32_t GetInterfaceActiveStatus(const UsbDev &dev, uint8_t interfaceId, bool unactived);
301 * @tc.desc: Negative test: parameters exception, busNum && devAddr && interfaceid error
302 * @tc.type: FUNC
303 */
304 HWTEST_F(UsbdDeviceStatusTest, UsbdGetInterfaceActiveStatus008, Function | MediumTest | Level1)
305 {
306 uint8_t interfaceId = INTERFACEID_INVALID;
307 bool unactived = 1;
308 struct UsbDev dev = {BUS_NUM_INVALID, DEV_ADDR_INVALID};
309 auto ret = g_usbInterface->GetInterfaceActiveStatus(dev, interfaceId, unactived);
310 HDF_LOGI("UsbdGetInterfaceActiveStatus008 %{public}d, ret=%{public}d, unactived=%{public}d",
311 __LINE__, ret, unactived);
312 ASSERT_NE(ret, 0);
313 }
314 } // USB
315 } // OHOS
316