• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <cstdio>
17 #include <cstring>
18 #include <gtest/gtest.h>
19 #include <unistd.h>
20 
21 #include "securec.h"
22 #include "usb_utils.h"
23 
24 using namespace std;
25 using namespace testing::ext;
26 
27 namespace {
28 const string RLOG_FILE = "/data/acm_read_xts";
29 const string PNP_LOG_FILE = "/data/usbhost_pnp_xts";
30 
31 class UsbHostSerialFuncTest : public testing::Test {
32 protected:
SetUpTestCase(void)33     static void SetUpTestCase(void)
34     {
35         printf("------start UsbHostSerialFuncTest------\n");
36         system("cat /dev/null > /data/acm_write_xts");
37         system("cat /dev/null > /data/acm_read_xts");
38         system("cat /dev/null > /data/usbhost_pnp_xts");
39     }
TearDownTestCase(void)40     static void TearDownTestCase(void)
41     {
42         printf("------end UsbHostSerialFuncTest------\n");
43     }
44 };
45 
46 /**
47  * @tc.number    : H_Lx_H_Sub_usb_insert_001
48  * @tc.name      : 验证插入USB串口设备后加载驱动
49  * @tc.size      : MEDIUM
50  * @tc.type      : FUNC
51  * @tc.level     : Level 1
52  */
53 HWTEST_F(UsbHostSerialFuncTest, UsbSerialInsertOnce, TestSize.Level1)
54 {
55     printf("------start UsbSerialInsertOnce------\n");
56     const string targetLog = "usb pnp sample device driver test add start";
57     double startTs = GetNowTs();
58     ASSERT_EQ(system("usbhost_pnp_test -add"), 0);
59     sleep(1);
60     EXPECT_TRUE(HasLog(targetLog, startTs, PNP_LOG_FILE)) << "ErrInfo: serial driver cannot be loaded in 1s";
61     printf("------end UsbSerialInsertOnce------\n");
62 }
63 
64 /**
65  * @tc.number    : H_Lx_H_Sub_usb_insert_002
66  * @tc.name      : 验证拔出USB串口设备后卸载驱动
67  * @tc.size      : MEDIUM
68  * @tc.type      : FUNC
69  * @tc.level     : Level 1
70  */
71 HWTEST_F(UsbHostSerialFuncTest, UsbSerialPullOutOnce, TestSize.Level1)
72 {
73     printf("------start UsbSerialPullOutOnce------\n");
74     const string targetLog = "usb pnp sample device driver test remove start";
75     double startTs = GetNowTs();
76     ASSERT_EQ(system("usbhost_pnp_test -remove"), 0);
77     sleep(1);
78     EXPECT_TRUE(HasLog(targetLog, startTs, PNP_LOG_FILE)) << "ErrInfo: serial driver cannot be unloaded in 1s";
79     printf("------end UsbSerialPullOutOnce------\n");
80 }
81 
82 /**
83  * @tc.number    : H_Lx_H_Sub_usb_insert_009
84  * @tc.name      : 验证20次插拔串口设备每次都能够正常识别
85  * @tc.size      : MEDIUM
86  * @tc.type      : FUNC
87  * @tc.level     : Level 2
88  */
89 HWTEST_F(UsbHostSerialFuncTest, UsbSerialInsertPullOut20, TestSize.Level2)
90 {
91     printf("------start UsbSerialInsertPullOut20------\n");
92     const string loadLog = "usb pnp sample device driver test add start";
93     const string unloadLog = "usb pnp sample device driver test remove start";
94     double startTs = GetNowTs();
95     for (int32_t i = 0; i < 20; i++) {
96         startTs = GetNowTs();
97         ASSERT_EQ(system("usbhost_pnp_test -add"), 0);
98         sleep(1);
99         EXPECT_TRUE(HasLog(loadLog, startTs, PNP_LOG_FILE)) << "ErrInfo: serial driver cannot be loaded in 1s";
100         ASSERT_EQ(system("usbhost_pnp_test -remove"), 0);
101         sleep(1);
102         EXPECT_TRUE(HasLog(unloadLog, startTs, PNP_LOG_FILE)) << "ErrInfo: serial driver cannot be unloaded in 1s";
103     }
104     printf("------end UsbSerialInsertPullOut20------\n");
105 }
106 
107 /**
108  * @tc.number    : H_Lx_H_Sub_usb_insert_011
109  * @tc.name      : 验证20次快速拔插串口后能否正常加载和卸载驱动
110  * @tc.size      : MEDIUM
111  * @tc.type      : FUNC
112  * @tc.level     : Level 2
113  */
114 HWTEST_F(UsbHostSerialFuncTest, UsbSerialInsertPullOutQuickly20, TestSize.Level2)
115 {
116     printf("------start UsbSerialInsertPullOutQuickly20------\n");
117     const string loadLog = "usb pnp sample device driver test add start";
118     const string unloadLog = "usb pnp sample device driver test remove start";
119     for (int32_t i = 0; i < 20; i++) {
120         ASSERT_EQ(system("usbhost_pnp_test -add"), 0);
121         usleep(100 * 1000);
122         ASSERT_EQ(system("usbhost_pnp_test -remove"), 0);
123     }
124     double startTs = GetNowTs();
125     ASSERT_EQ(system("usbhost_pnp_test -add"), 0);
126     sleep(1);
127     EXPECT_TRUE(HasLog(loadLog, startTs, PNP_LOG_FILE)) << "ErrInfo: serial driver cannot be loaded in 1s";
128     ASSERT_EQ(system("usbhost_pnp_test -remove"), 0);
129     sleep(1);
130     EXPECT_TRUE(HasLog(unloadLog, startTs, PNP_LOG_FILE)) << "ErrInfo: serial driver cannot be unloaded in 1s";
131     printf("------end UsbSerialInsertPullOutQuickly20------\n");
132 }
133 
134 /**
135  * @tc.number    : H_Lx_H_Sub_usb_IO read_001,H_Lx_H_Sub_usb_IO read_007
136  * @tc.name      : USB串口同步数据读写
137  * @tc.size      : MEDIUM
138  * @tc.type      : FUNC
139  * @tc.level     : Level 1
140  */
141 HWTEST_F(UsbHostSerialFuncTest, CheckUsbSerialIoWriteSync_001, TestSize.Level1)
142 {
143     printf("------start CheckUsbSerialIoWriteSync_001------\n");
144     const string data = "abc";
145     double startTs = GetNowTs();
146     string wlog, rlog;
147     wlog = "send data[" + data + "] to device";
148     rlog = "recv data[" + data + "] from device";
149     ASSERT_EQ(system("usbhost_ddk_test -AR &"), 0);
150     ASSERT_EQ(system(("usbhost_ddk_test -AW '" + data + "'").c_str()), 0);
151     sleep(3);
152     EXPECT_TRUE(HasLog(wlog, startTs)) << "ErrInfo: cannot find sync write log";
153     EXPECT_TRUE(HasLog(rlog, startTs, RLOG_FILE)) << "ErrInfo: cannot find sync recv log";
154     printf("------end CheckUsbSerialIoWriteSync_001------\n");
155 }
156 
157 /**
158  * @tc.number    : H_Lx_H_Sub_usb_IO read_001,H_Lx_H_Sub_usb_IO read_007
159  * @tc.name      : USB串口同步数据读写
160  * @tc.size      : MEDIUM
161  * @tc.type      : FUNC
162  * @tc.level     : Level 1
163  */
164 HWTEST_F(UsbHostSerialFuncTest, CheckUsbSerialIoWriteSync_002, TestSize.Level1)
165 {
166     printf("------start CheckUsbSerialIoWriteSync_002------\n");
167     const string data[] = {"0123456789", "Z", "0!a@1#b$2%c^3&D*4(E)5-F_", ""};
168     double startTs = GetNowTs();
169     string wlog, rlog;
170     for (int32_t i = 0; data[i].size() > 0; i++) {
171         wlog = "send data[" + data[i] + "] to device";
172         rlog = "recv data[" + data[i] + "] from device";
173         ASSERT_EQ(system("usbhost_ddk_test -AR &"), 0);
174         ASSERT_EQ(system(("usbhost_ddk_test -AW '" + data[i] + "'").c_str()), 0);
175         sleep(3);
176         EXPECT_TRUE(HasLog(wlog, startTs)) << "ErrInfo: cannot find sync write log";
177         EXPECT_TRUE(HasLog(rlog, startTs, RLOG_FILE)) << "ErrInfo: cannot find sync recv log";
178     }
179     printf("------end CheckUsbSerialIoWriteSync_002------\n");
180 }
181 
182 /**
183  * @tc.number    : H_Lx_H_Sub_usb_IO read_003, H_Lx_H_Sub_usb_IO read_009
184  * @tc.name      : USB串口同步读写1KB数据
185  * @tc.size      : MEDIUM
186  * @tc.type      : FUNC
187  * @tc.level     : Level 2
188  */
189 HWTEST_F(UsbHostSerialFuncTest, CheckUsbSerialIoWriteSync_003, TestSize.Level2)
190 {
191     printf("------start CheckUsbSerialIoWriteSync_003------\n");
192     const string s = "0123456789abcdef";
193     string data;
194     int32_t totalSize = 1024;
195     int32_t writeCnt = 8;
196     unsigned int n = 0;
197     while (n < totalSize / writeCnt / s.size()) {
198         data += s;
199         n++;
200     }
201     const string wlog = "send data[" + data + "] to device";
202     const string rlog = "recv data[" + data + "] from device";
203     double startTs;
204     for (int32_t i = 0; i < writeCnt; i++) {
205         startTs = GetNowTs();
206         ASSERT_EQ(system("usbhost_ddk_test -AR &"), 0);
207         ASSERT_EQ(system(("usbhost_ddk_test -AW '" + data + "'").c_str()), 0);
208         sleep(3);
209         EXPECT_TRUE(HasLog(wlog, startTs)) << "ErrInfo: cannot find sync write log";
210         EXPECT_TRUE(HasLog(rlog, startTs, RLOG_FILE)) << "ErrInfo: cannot find sync recv log";
211     }
212     printf("------end CheckUsbSerialIoWriteSync_003------\n");
213 }
214 
215 /**
216  * @tc.number    : H_Lx_H_Sub_usb_IO read_013, H_Lx_H_Sub_usb_IO read_017
217  * @tc.name      : USB串口异步数据读写
218  * @tc.size      : MEDIUM
219  * @tc.type      : FUNC
220  * @tc.level     : Level 1
221  */
222 HWTEST_F(UsbHostSerialFuncTest, CheckUsbSerialIoWriteAsync_001, TestSize.Level1)
223 {
224     printf("------start CheckUsbSerialIoWriteAsync_001------\n");
225     ASSERT_EQ(system("usbhost_ddk_test -Ar &"), 0) << "ErrInfo:  failed to start async read";
226     sleep(3);
227     const string data = "abc";
228     double startTs = GetNowTs();
229     string wlog, rlog;
230     wlog = "send data[" + data + "] to device";
231     rlog = "recv data[" + data + "] from device";
232     ASSERT_EQ(system(("usbhost_ddk_test -Aw '" + data + "'").c_str()), 0);
233     sleep(3);
234     EXPECT_TRUE(HasLog(wlog, startTs)) << "ErrInfo: cannot find async write log";
235     EXPECT_TRUE(HasLog(rlog, startTs, RLOG_FILE)) << "ErrInfo: cannot find async recv log";
236     printf("------end CheckUsbSerialIoWriteAsync_001------\n");
237 }
238 
239 /**
240  * @tc.number    : H_Lx_H_Sub_usb_IO read_013, H_Lx_H_Sub_usb_IO read_017
241  * @tc.name      : USB串口异步数据读写
242  * @tc.size      : MEDIUM
243  * @tc.type      : FUNC
244  * @tc.level     : Level 1
245  */
246 HWTEST_F(UsbHostSerialFuncTest, CheckUsbSerialIoWriteAsync_002, TestSize.Level1)
247 {
248     printf("------start CheckUsbSerialIoWriteAsync_002------\n");
249     const string data[] = {"0123456789", "Z", "0!a@1#b$2%c^3&D*4(E)5-F_", ""};
250     double startTs = GetNowTs();
251     string wlog, rlog;
252     for (int32_t i = 0; data[i].size() > 0; i++) {
253         wlog = "send data[" + data[i] + "] to device";
254         rlog = "recv data[" + data[i] + "] from device";
255         ASSERT_EQ(system(("usbhost_ddk_test -Aw '" + data[i] + "'").c_str()), 0);
256         sleep(3);
257         EXPECT_TRUE(HasLog(wlog, startTs)) << "ErrInfo: cannot find async write log";
258         EXPECT_TRUE(HasLog(rlog, startTs, RLOG_FILE)) << "ErrInfo: cannot find async recv log";
259     }
260     printf("------end CheckUsbSerialIoWriteAsync_002------\n");
261 }
262 
263 /**
264  * @tc.number    : H_Lx_H_Sub_usb_IO read_019
265  * @tc.name      : USB串口异步读写1KB数据
266  * @tc.size      : MEDIUM
267  * @tc.type      : FUNC
268  * @tc.level     : Level 2
269  */
270 HWTEST_F(UsbHostSerialFuncTest, CheckUsbSerialIoWriteAsync_003, TestSize.Level2)
271 {
272     printf("------start CheckUsbSerialIoWriteAsync_003------\n");
273     const string s = "0123456789abcdef";
274     string data;
275     int32_t totalSize = 1024;
276     int32_t writeCnt = 8;
277     unsigned int n = 0;
278     while (n < totalSize / writeCnt / s.size()) {
279         data += s;
280         n++;
281     }
282     const string wlog = "send data[" + data + "] to device";
283     const string rlog = "recv data[" + data + "] from device";
284     double startTs;
285     for (int32_t i = 0; i < writeCnt; i++) {
286         startTs = GetNowTs();
287         ASSERT_EQ(system(("usbhost_ddk_test -Aw '" + data + "'").c_str()), 0);
288         sleep(3);
289         EXPECT_TRUE(HasLog(wlog, startTs)) << "ErrInfo: cannot find async write log";
290         EXPECT_TRUE(HasLog(rlog, startTs, RLOG_FILE)) << "ErrInfo: cannot find async recv log";
291     }
292     ASSERT_EQ(system("killall usbhost_ddk_test"), 0) << "ErrInfo:  failed to kill async read";
293     printf("------end CheckUsbSerialIoWriteAsync_003------\n");
294 }
295 
296 /**
297  * @tc.number    : H_Lx_H_Sub_usb_Control_001
298  * @tc.name      : USB串口标准控制命令的同步处理
299  * @tc.size      : MEDIUM
300  * @tc.type      : FUNC
301  * @tc.level     : Level 1
302  */
303 HWTEST_F(UsbHostSerialFuncTest, UsbSerialStdCtrlCmdSync_001, TestSize.Level1)
304 {
305     printf("------start UsbSerialStdCtrlCmdSync_001------\n");
306     string targetLog;
307     double startTs = GetNowTs();
308     ASSERT_EQ(system("usbhost_ddk_test -AC"), 0);
309     targetLog = "usb serial control command[CMD_STD_CTRL_GET_DESCRIPTOR] done";
310     EXPECT_TRUE(HasLog(targetLog, startTs));
311     ASSERT_EQ(system("usbhost_ddk_test -Ai"), 0);
312     targetLog = "usb serial control command[CMD_STD_CTRL_GET_INTERFACE] done";
313     EXPECT_TRUE(HasLog(targetLog, startTs));
314     ASSERT_EQ(system("usbhost_ddk_test -Ag"), 0);
315     targetLog = "usb serial control command[CMD_STD_CTRL_GET_CONFIGURATION] done";
316     EXPECT_TRUE(HasLog(targetLog, startTs));
317     ASSERT_EQ(system("usbhost_ddk_test -As"), 0);
318     targetLog = "usb serial control command[CMD_STD_CTRL_GET_STATUS] done";
319     EXPECT_TRUE(HasLog(targetLog, startTs));
320     printf("------end UsbSerialStdCtrlCmdSync_001------\n");
321 }
322 
323 /**
324  * @tc.number    : H_Lx_H_Sub_usb_Control_002
325  * @tc.name      : USB串口标准控制命令的同步处理
326  * @tc.size      : MEDIUM
327  * @tc.type      : FUNC
328  * @tc.level     : Level 2
329  */
330 HWTEST_F(UsbHostSerialFuncTest, UsbSerialStdCtrlCmdSync_002, TestSize.Level2)
331 {
332     printf("------start UsbSerialStdCtrlCmdSync_002------\n");
333     string targetLog;
334     double startTs = GetNowTs();
335     ASSERT_EQ(system("usbhost_ddk_test -As"), 0);
336     ASSERT_EQ(system("usbhost_ddk_test -As"), 0);
337     targetLog = "usb serial control command[CMD_STD_CTRL_GET_STATUS] done";
338     EXPECT_TRUE(HasLog(targetLog, startTs));
339     printf("------end UsbSerialStdCtrlCmdSync_002------\n");
340 }
341 
342 /**
343  * @tc.number    : H_Lx_H_Sub_usb_Control_002
344  * @tc.name      : USB串口类控制命令的同步处理
345  * @tc.size      : MEDIUM
346  * @tc.type      : FUNC
347  * @tc.level     : Level 1
348  */
349 HWTEST_F(UsbHostSerialFuncTest, UsbSerialClsCtrlCmdSync, TestSize.Level1)
350 {
351     printf("------start UsbSerialClsCtrlCmdSync------\n");
352     string targetLog;
353     double startTs = GetNowTs();
354     ASSERT_EQ(system("usbhost_ddk_test -Ac"), 0);
355     targetLog = "usb serial control command[CMD_CLASS_CTRL] done";
356     EXPECT_TRUE(HasLog(targetLog, startTs));
357     printf("------end UsbSerialClsCtrlCmdSync------\n");
358 }
359 
360 /**
361  * @tc.number    : H_Lx_H_Sub_usb_Control_004
362  * @tc.name      : USB串口标准控制命令的异步处理
363  * @tc.size      : MEDIUM
364  * @tc.type      : FUNC
365  * @tc.level     : Level 1
366  */
367 HWTEST_F(UsbHostSerialFuncTest, UsbSerialStdCtrlCmdAsync, TestSize.Level1)
368 {
369     printf("------start UsbSerialStdCtrlCmdAsync------\n");
370     string targetLog;
371     double startTs = GetNowTs();
372     ASSERT_EQ(system("usbhost_ddk_test -Ad"), 0);
373     targetLog = "usb serial control command[CMD_STD_CTRL_GET_DESCRIPTOR_ASYNC] done";
374     EXPECT_TRUE(HasLog(targetLog, startTs));
375     printf("------end UsbSerialStdCtrlCmdAsync------\n");
376 }
377 
378 /**
379  * @tc.number    : H_Lx_D_Sub_usb_Instance_001, H_Lx_D_Sub_usb_Descriptor_002, H_Lx_D_Sub_usb_Descriptor_001
380  * @tc.name      : 动态实例化USB串口设备, 支持通用属性可配置, 从HCS导入属性及默认值
381  * @tc.type      : FUNC
382  * @tc.level     : Level 1
383  */
384 HWTEST_F(UsbHostSerialFuncTest, CheckUsbSerialDeviceInfo, TestSize.Level1)
385 {
386     printf("------start CheckUsbSerialDeviceInfo------\n");
387     const char *idVendor = "0x12d1";
388     const char *idProduct = "0x5000";
389     const char *bDeviceClass = "0x00";
390     const char *bDeviceSubClass = "0x00";
391     const char *bDeviceProtocol = "0x00";
392     const int32_t logMaxLen = 1000;
393     char targetLog[logMaxLen] = {0};
394     const char *fmt = "device descriptor info:[%s %s %s %s %s]";
395     snprintf_s(
396         targetLog, logMaxLen, logMaxLen - 1, fmt, idVendor, idProduct, bDeviceClass, bDeviceSubClass, bDeviceProtocol);
397     printf("targetLog==>%s\n", targetLog);
398     double startTs = GetNowTs();
399     ASSERT_EQ(system("usbhost_ddk_test -AC"), 0);
400     EXPECT_TRUE(HasLog(string(targetLog), startTs, RLOG_FILE));
401     printf("------end CheckUsbSerialDeviceInfo------\n");
402 }
403 
404 /**
405  * @tc.number    : H_Lx_H_Sub_usb_DFR_007
406  * @tc.name      : 验证进程被杀掉后SDK自启动功能
407  * @tc.size      : MEDIUM
408  * @tc.type      : FUNC
409  * @tc.level     : Level 1
410  */
411 HWTEST_F(UsbHostSerialFuncTest, KillHostSdkProcess, TestSize.Level1)
412 {
413     printf("------start KillHostSdkProcess------\n");
414     system("kill $(pidof pnp_host)");
415     printf("Please waiting for restarting sdk process...\n");
416     sleep(5);
417     ASSERT_EQ(system("usbhost_ddk_test -Ar &"), 0) << "ErrInfo:  failed to start async read";
418     sleep(3);
419     const string data = "abc";
420     double startTs = GetNowTs();
421     string wlog, rlog;
422     wlog = "send data[" + data + "] to device";
423     rlog = "recv data[" + data + "] from device";
424     ASSERT_EQ(system(("usbhost_ddk_test -Aw '" + data + "'").c_str()), 0);
425     sleep(3);
426     EXPECT_TRUE(HasLog(wlog, startTs)) << "ErrInfo: cannot find async write log";
427     EXPECT_TRUE(HasLog(rlog, startTs, RLOG_FILE)) << "ErrInfo: cannot find async recv log";
428     system("killall usbhost_ddk_test");
429     printf("------end KillHostSdkProcess------\n");
430 }
431 } // namespace