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