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