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