1 /*
2 * Copyright (c) 2021-2022 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
17 #include "hdf_log.h"
18 #include "if_system_ability_manager.h"
19 #include "system_ability_definition.h"
20 #include "usbd_function_test.h"
21 #include "v1_0/iusb_interface.h"
22 #include "v1_0/usb_types.h"
23
24 const int SLEEP_TIME = 3;
25 const int TEST_PORT_ID = 1;
26 const int TEST_POWER_ROLE = 2;
27 const int TEST_DATAR_ROLE = 2;
28 const int USB_FUNCTION_INVALID = -1;
29 const int USB_FUNCTION_ACM = 1;
30 const int USB_FUNCTION_ECM = 2;
31 const int USB_FUNCTION_HDC = 4;
32 const int USB_FUNCTION_RNDIS = 32;
33 const int USB_FUNCTION_STORAGE = 512;
34 const int USB_FUNCTION_UNSUPPORTED = 128;
35
36 using namespace testing::ext;
37 using namespace OHOS;
38 using namespace std;
39 using namespace OHOS::HDI::Usb::V1_0;
40
41 namespace {
42 sptr<IUsbInterface> g_usbInterface = nullptr;
43
SetUpTestCase(void)44 void UsbdFunctionTest::SetUpTestCase(void)
45 {
46 g_usbInterface = IUsbInterface::Get();
47 if (g_usbInterface == nullptr) {
48 HDF_LOGE("%{public}s:IUsbInterface::Get() failed.", __func__);
49 exit(0);
50 }
51 auto ret = g_usbInterface->SetPortRole(TEST_PORT_ID, TEST_POWER_ROLE, TEST_DATAR_ROLE);
52 sleep(SLEEP_TIME);
53 HDF_LOGI("UsbdFunctionTest::[Device] %{public}d SetPortRole=%{public}d", __LINE__, ret);
54 ASSERT_EQ(0, ret);
55 if (ret != 0) {
56 exit(0);
57 }
58 }
59
TearDownTestCase(void)60 void UsbdFunctionTest::TearDownTestCase(void) {}
61
SetUp(void)62 void UsbdFunctionTest::SetUp(void) {}
63
TearDown(void)64 void UsbdFunctionTest::TearDown(void) {}
65
66 /**
67 * @tc.name: SUB_USB_HDI_1250
68 * @tc.desc: Test functions to GetCurrentFunctions
69 * @tc.desc: int32_t GetCurrentFunctions(int32_t &funcs);
70 * @tc.desc: Positive test: parameters correctly
71 * @tc.type: FUNC
72 */
73 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_1250, Function | MediumTest | Level1)
74 {
75 int32_t funcs = 0;
76 auto ret = g_usbInterface->GetCurrentFunctions(funcs);
77 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_1250 %{public}d ret=%{public}d", __LINE__, ret);
78 ASSERT_EQ(0, ret);
79 }
80
81 /**
82 * @tc.name: SUB_USB_HDI_1260
83 * @tc.desc: Test functions to GetCurrentFunctions
84 * @tc.desc: int32_t GetCurrentFunctions(int32_t &funcs);
85 * @tc.desc: Positive test: parameters correctly
86 * @tc.type: FUNC
87 */
88 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_1260, Function | MediumTest | Level1)
89 {
90 auto ret = g_usbInterface->SetCurrentFunctions(1);
91 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_1270 %{public}d SetCurrentFunctions=%{public}d", __LINE__, ret);
92 ASSERT_EQ(0, ret);
93 int32_t funcs = 1;
94 ret = g_usbInterface->GetCurrentFunctions(funcs);
95 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_1260 %{public}d ret=%{public}d", __LINE__, ret);
96 ASSERT_EQ(0, ret);
97 }
98
99 /**********************************************************************************************************/
100
101 /**
102 * @tc.name: SUB_USB_HDI_1270
103 * @tc.desc: Test functions to SetCurrentFunctions
104 * @tc.desc: int32_t SetCurrentFunctions(int32_t funcs)
105 * @tc.desc: Positive test: parameters correctly
106 * @tc.type: FUNC
107 */
108 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_1270, Function | MediumTest | Level1)
109 {
110 int32_t funcs = USB_FUNCTION_ACM;
111 auto ret = g_usbInterface->SetCurrentFunctions(funcs);
112 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_1270 %{public}d SetCurrentFunctions=%{public}d", __LINE__, ret);
113 ASSERT_EQ(0, ret);
114 }
115
116 /**
117 * @tc.name: SUB_USB_HDI_1280
118 * @tc.desc: Test functions to SetCurrentFunctions
119 * @tc.desc: int32_t SetCurrentFunctions(int32_t funcs)
120 * @tc.desc: Negative test: parameters exception, Funcs error
121 * @tc.type: FUNC
122 */
123 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_1280, Function | MediumTest | Level1)
124 {
125 int32_t funcs = USB_FUNCTION_INVALID;
126 auto ret = g_usbInterface->SetCurrentFunctions(funcs);
127 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_1280 %{public}d, ret=%{public}d, funcs=%{public}d", __LINE__, ret, funcs);
128 ASSERT_NE(ret, 0);
129 }
130 /**
131 * @tc.name: SUB_USB_HDI_1290
132 * @tc.desc: Test functions to SetCurrentFunctions
133 * @tc.desc: int32_t SetCurrentFunctions(int32_t funcs)
134 * @tc.desc: Positive test: parameters correctly
135 * @tc.type: FUNC
136 */
137 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_1290, Function | MediumTest | Level1)
138 {
139 int32_t funcs = USB_FUNCTION_ECM;
140 auto ret = g_usbInterface->SetCurrentFunctions(funcs);
141 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_1290 %{public}d ret=%{public}d", __LINE__, ret);
142 ASSERT_EQ(0, ret);
143 }
144
145 /**
146 * @tc.name: SUB_USB_HDI_1300
147 * @tc.desc: Test functions to SetCurrentFunctions
148 * @tc.desc: int32_t SetCurrentFunctions(int32_t funcs)
149 * @tc.desc: Positive test: parameters correctly
150 * @tc.type: FUNC
151 */
152 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_1300, Function | MediumTest | Level1)
153 {
154 int32_t funcs = USB_FUNCTION_ACM | USB_FUNCTION_ECM;
155 auto ret = g_usbInterface->SetCurrentFunctions(funcs);
156 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_1300 %{public}d ret=%{public}d", __LINE__, ret);
157 ASSERT_EQ(0, ret);
158 }
159
160 /**
161 * @tc.name: SUB_USB_HDI_1310
162 * @tc.desc: Test functions to SetCurrentFunctions
163 * @tc.desc: int32_t SetCurrentFunctions(int32_t funcs)
164 * @tc.desc: Positive test: parameters correctly
165 * @tc.type: FUNC
166 */
167 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_1310, Function | MediumTest | Level1)
168 {
169 int32_t funcs = USB_FUNCTION_HDC;
170 auto ret = g_usbInterface->SetCurrentFunctions(funcs);
171 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_1310 %{public}d ret=%{public}d", __LINE__, ret);
172 ASSERT_EQ(0, ret);
173 }
174
175 /**
176 * @tc.name: SUB_USB_HDI_1320
177 * @tc.desc: Test functions to SetCurrentFunctions
178 * @tc.desc: int32_t SetCurrentFunctions(int32_t funcs)
179 * @tc.desc: Positive test: parameters correctly
180 * @tc.type: FUNC
181 */
182 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_1320, Function | MediumTest | Level1)
183 {
184 int32_t funcs = USB_FUNCTION_ACM | USB_FUNCTION_HDC;
185 auto ret = g_usbInterface->SetCurrentFunctions(funcs);
186 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_1320 %{public}d ret=%{public}d", __LINE__, ret);
187 ASSERT_EQ(0, ret);
188 }
189
190 /**
191 * @tc.name: SUB_USB_HDI_1330
192 * @tc.desc: Test functions to SetCurrentFunctions
193 * @tc.desc: int32_t SetCurrentFunctions(int32_t funcs)
194 * @tc.desc: Positive test: parameters correctly
195 * @tc.type: FUNC
196 */
197 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_1330, Function | MediumTest | Level1)
198 {
199 int32_t funcs = USB_FUNCTION_ECM | USB_FUNCTION_HDC;
200 auto ret = g_usbInterface->SetCurrentFunctions(funcs);
201 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_1330 %{public}d ret=%{public}d", __LINE__, ret);
202 ASSERT_EQ(0, ret);
203 }
204
205 /**
206 * @tc.name: SUB_USB_HDI_1340
207 * @tc.desc: Test functions to SetCurrentFunctions
208 * @tc.desc: int32_t SetCurrentFunctions(int32_t funcs)
209 * @tc.desc: Positive test: parameters correctly
210 * @tc.type: FUNC
211 */
212 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_1340, Function | MediumTest | Level1)
213 {
214 int32_t funcs = USB_FUNCTION_RNDIS;
215 auto ret = g_usbInterface->SetCurrentFunctions(funcs);
216 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_1340 %{public}d ret=%{public}d", __LINE__, ret);
217 ASSERT_EQ(0, ret);
218 }
219
220 /**
221 * @tc.name: SUB_USB_HDI_2340
222 * @tc.desc: Test functions to SetCurrentFunctions
223 * @tc.desc: int32_t SetCurrentFunctions(int32_t funcs)
224 * @tc.desc: Positive test: parameters correctly
225 * @tc.type: FUNC
226 */
227 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_2340, Function | MediumTest | Level1)
228 {
229 int32_t funcs = USB_FUNCTION_STORAGE;
230 auto ret = g_usbInterface->SetCurrentFunctions(funcs);
231 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_2340 %{public}d ret=%{public}d", __LINE__, ret);
232 ASSERT_EQ(0, ret);
233 }
234
235 /**
236 * @tc.name: SUB_USB_HDI_2350
237 * @tc.desc: Test functions to SetCurrentFunctions
238 * @tc.desc: int32_t SetCurrentFunctions(int32_t funcs)
239 * @tc.desc: Positive test: parameters correctly
240 * @tc.type: FUNC
241 */
242 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_2350, Function | MediumTest | Level1)
243 {
244 int32_t funcs = USB_FUNCTION_RNDIS | USB_FUNCTION_HDC;
245 auto ret = g_usbInterface->SetCurrentFunctions(funcs);
246 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_2350 %{public}d ret=%{public}d", __LINE__, ret);
247 ASSERT_EQ(0, ret);
248 }
249
250 /**
251 * @tc.name: SUB_USB_HDI_2360
252 * @tc.desc: Test functions to SetCurrentFunctions
253 * @tc.desc: int32_t SetCurrentFunctions(int32_t funcs)
254 * @tc.desc: Positive test: parameters correctly
255 * @tc.type: FUNC
256 */
257 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_2360, Function | MediumTest | Level1)
258 {
259 int32_t funcs = USB_FUNCTION_STORAGE | USB_FUNCTION_HDC;
260 auto ret = g_usbInterface->SetCurrentFunctions(funcs);
261 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_2360 %{public}d ret=%{public}d", __LINE__, ret);
262 ASSERT_EQ(0, ret);
263 }
264
265 /**
266 * @tc.name: SUB_USB_HDI_2370
267 * @tc.desc: Test functions to SetCurrentFunctions
268 * @tc.desc: int32_t SetCurrentFunctions(int32_t funcs)
269 * @tc.desc: Negative test: parameters exception, funcs error
270 * @tc.type: FUNC
271 */
272 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_2370, Function | MediumTest | Level1)
273 {
274 int32_t funcs = USB_FUNCTION_UNSUPPORTED;
275 auto ret = g_usbInterface->SetCurrentFunctions(funcs);
276 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_2370 %{public}d ret=%{public}d", __LINE__, ret);
277 ASSERT_NE(0, ret);
278 }
279
280 /**
281 * @tc.name: SUB_USB_HDI_1350
282 * @tc.desc: Test functions to SetPortRole
283 * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)
284 * @tc.desc: Positive test: parameters correctly
285 * @tc.type: FUNC
286 */
287 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_1350, Function | MediumTest | Level1)
288 {
289 auto ret = g_usbInterface->SetPortRole(1, 1, 1);
290 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_1350 %{public}d ret=%{public}d", __LINE__, ret);
291 ASSERT_EQ(0, ret);
292 }
293
294 /**
295 * @tc.name: SUB_USB_HDI_1360
296 * @tc.desc: Test functions to SetPortRole
297 * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)
298 * @tc.desc: Negative test: parameters exception, portId error
299 * @tc.type: FUNC
300 */
301 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_1360, Function | MediumTest | Level1)
302 {
303 auto ret = g_usbInterface->SetPortRole(2, 1, 1);
304 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_1360 %{public}d ret=%{public}d", __LINE__, ret);
305 ASSERT_NE(ret, 0);
306 }
307
308 /**
309 * @tc.name: SUB_USB_HDI_1370
310 * @tc.desc: Test functions to SetPortRole
311 * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)
312 * @tc.desc: Negative test: parameters exception, powerRole error
313 * @tc.type: FUNC
314 */
315 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_1370, Function | MediumTest | Level1)
316 {
317 auto ret = g_usbInterface->SetPortRole(1, 4, 2);
318 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_1370 %{public}d ret=%{public}d", __LINE__, ret);
319 ASSERT_NE(ret, 0);
320 }
321
322 /**
323 * @tc.name: SUB_USB_HDI_1380
324 * @tc.desc: Test functions to SetPortRole
325 * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)
326 * @tc.desc: Negative test: parameters exception, dataRole error
327 * @tc.type: FUNC
328 */
329 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_1380, Function | MediumTest | Level1)
330 {
331 auto ret = g_usbInterface->SetPortRole(1, 1, 5);
332 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_1380 %{public}d ret=%{public}d", __LINE__, ret);
333 ASSERT_NE(ret, 0);
334 }
335
336 /**
337 * @tc.name: SUB_USB_HDI_1390
338 * @tc.desc: Test functions to SetPortRole
339 * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)
340 * @tc.desc: Negative test: parameters exception, portId && powerRole error
341 * @tc.type: FUNC
342 */
343 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_1390, Function | MediumTest | Level1)
344 {
345 auto ret = g_usbInterface->SetPortRole(1, 5, 5);
346 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_1390 %{public}d ret=%{public}d", __LINE__, ret);
347 ASSERT_NE(ret, 0);
348 }
349
350 /**
351 * @tc.name: SUB_USB_HDI_1400
352 * @tc.desc: Test functions to SetPortRole
353 * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)
354 * @tc.desc: Negative test: parameters exception, portId && dataRole error
355 * @tc.type: FUNC
356 */
357 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_1400, Function | MediumTest | Level1)
358 {
359 auto ret = g_usbInterface->SetPortRole(5, 1, 5);
360 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_1400 %{public}d ret=%{public}d", __LINE__, ret);
361 ASSERT_NE(ret, 0);
362 }
363
364 /**
365 * @tc.name: SUB_USB_HDI_1410
366 * @tc.desc: Test functions to SetPortRole
367 * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)
368 * @tc.desc: Negative test: parameters exception, powerRole && dataRole error
369 * @tc.type: FUNC
370 */
371 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_1410, Function | MediumTest | Level1)
372 {
373 auto ret = g_usbInterface->SetPortRole(1, 5, 5);
374 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_1410 %{public}d ret=%{public}d", __LINE__, ret);
375 ASSERT_NE(ret, 0);
376 }
377
378 /**
379 * @tc.name: SUB_USB_HDI_1420
380 * @tc.desc: Test functions to SetPortRole
381 * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)
382 * @tc.desc: Negative test: parameters exception, portId && powerRole && dataRole error
383 * @tc.type: FUNC
384 */
385 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_1420, Function | MediumTest | Level1)
386 {
387 auto ret = g_usbInterface->SetPortRole(2, 5, 5);
388 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_1420 %{public}d ret=%{public}d", __LINE__, ret);
389 ASSERT_NE(ret, 0);
390 }
391
392 /**
393 * @tc.name: SUB_USB_HDI_1430
394 * @tc.desc: Test functions to SetPortRole
395 * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)
396 * @tc.desc: Positive test: parameters correctly
397 * @tc.type: FUNC
398 */
399 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_1430, Function | MediumTest | Level1)
400 {
401 auto ret = g_usbInterface->SetPortRole(1, 2, 2);
402 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_1430 %{public}d ret=%{public}d", __LINE__, ret);
403 ASSERT_EQ(0, ret);
404 }
405
406 /**
407 * @tc.name: SUB_USB_HDI_1700
408 * @tc.desc: Test functions to QueryPort
409 * @tc.desc: int32_t QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode);
410 * @tc.desc: Positive test: parameters correctly
411 * @tc.type: FUNC
412 */
413 HWTEST_F(UsbdFunctionTest, SUB_USB_HDI_1700, Function | MediumTest | Level1)
414 {
415 int32_t portId = 0;
416 int32_t powerRole = 0;
417 int32_t dataRole = 0;
418 int32_t mode = 0;
419 auto ret = g_usbInterface->QueryPort(portId, powerRole, dataRole, mode);
420 HDF_LOGI("UsbdFunctionTest::SUB_USB_HDI_1700 %{public}d ret=%{public}d", __LINE__, ret);
421 ASSERT_EQ(0, ret);
422 }
423 } // namespace
424