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