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 <unistd.h>
19 #include <gtest/gtest.h>
20 #include "securec.h"
21 #include "usbhost_ddk_test.h"
22 
23 using namespace std;
24 using namespace testing::ext;
25 
26 namespace {
27 const int32_t TEST_COUNT = 3;
28 class UsbHostRawApiFuncTest : public testing::Test {
29 public:
30     static void SetUpTestCase();
31     static void TearDownTestCase();
32 };
33 
SetUpTestCase()34 void UsbHostRawApiFuncTest::SetUpTestCase()
35 {
36     printf("------start UsbHostRawApiFuncTest------\n");
37     const char *apiType = "-RAW";
38     UsbHostDdkTestInit(const_cast<char*>(apiType));
39 }
40 
TearDownTestCase()41 void UsbHostRawApiFuncTest::TearDownTestCase()
42 {
43     char writeBuf[] = "q";
44     UsbHostDdkTestOpen(HOST_ACM_ASYNC_WRITE);
45     UsbHostDdkTestAsyncWrite(writeBuf);
46     UsbHostDdkTestClose(HOST_ACM_ASYNC_WRITE);
47     TestExit();
48     printf("------end UsbHostRawApiFuncTest------\n");
49 }
50 
51 /**
52  * @tc.number    : H_Lx_H_Sub_usb_IOread_write_001,H_Lx_H_Sub_usb_IOread_write_003
53  * @tc.name      : USB串口同步数据读写
54  * @tc.size      : MEDIUM
55  * @tc.type      : FUNC
56  * @tc.level     : Level 1
57  */
58 HWTEST_F(UsbHostRawApiFuncTest, UsbSerialReadSync_001, TestSize.Level1)
59 {
60     printf("------start UsbSerialReadSync_001------\n");
61     char writeBuf[512] = "abc";
62     char readBuf[512] = {0};
63     UsbHostDdkTestOpen(HOST_ACM_SYNC_WRITE);
64     UsbHostDdkTestSyncWrite(writeBuf);
65     UsbHostDdkTestClose(HOST_ACM_SYNC_WRITE);
66     UsbHostDdkTestOpen(HOST_ACM_SYNC_READ);
67     UsbHostDdkTestSyncRead(readBuf);
68     UsbHostDdkTestClose(HOST_ACM_SYNC_READ);
69     EXPECT_EQ(strcmp(writeBuf, readBuf), 0);
70     printf("------end UsbSerialReadSync_001------\n");
71 }
72 
73 /**
74  * @tc.number    : H_Lx_H_Sub_usb_IOread_write_001,H_Lx_H_Sub_usb_IOread_write_006
75  * @tc.name      : USB串口同步读+异步写
76  * @tc.size      : MEDIUM
77  * @tc.type      : FUNC
78  * @tc.level     : Level 1
79  */
80 HWTEST_F(UsbHostRawApiFuncTest, UsbSerialReadSync_002, TestSize.Level1)
81 {
82     printf("------start UsbSerialReadSync_002------\n");
83     char writeBuf[512] = "abc";
84     char readBuf[512] = {0};
85     UsbHostDdkTestOpen(HOST_ACM_ASYNC_WRITE);
86     UsbHostDdkTestAsyncWrite(writeBuf);
87     UsbHostDdkTestClose(HOST_ACM_ASYNC_WRITE);
88     UsbHostDdkTestOpen(HOST_ACM_SYNC_READ);
89     UsbHostDdkTestSyncRead(readBuf);
90     UsbHostDdkTestClose(HOST_ACM_SYNC_READ);
91     EXPECT_EQ(strcmp(writeBuf, readBuf), 0);
92     printf("------end UsbSerialReadSync_002------\n");
93 }
94 
95 /**
96  * @tc.number    : H_Lx_H_Sub_usb_IOread_write_001,H_Lx_H_Sub_usb_IOread_write_003
97  * @tc.name      : USB串口同步数据读写
98  * @tc.size      : MEDIUM
99  * @tc.type      : FUNC
100  * @tc.level     : Level 1
101  */
102 HWTEST_F(UsbHostRawApiFuncTest, UsbSerialReadSync_003, TestSize.Level1)
103 {
104     printf("------start UsbSerialReadSync_003------\n");
105     const char *data[] = {
106         "0123456789",
107         "Z",
108         "0!a@1#b$2%c^3&D*4(E)5-F_",
109         ""
110     };
111     char readBuf[512] = {0};
112     for (int32_t i = 0; strlen(data[i]) > 0; i++) {
113         memset_s(readBuf, sizeof(readBuf), 0, sizeof(readBuf));
114         UsbHostDdkTestOpen(HOST_ACM_SYNC_WRITE);
115         UsbHostDdkTestSyncWrite(const_cast<char *>(data[i]));
116         UsbHostDdkTestClose(HOST_ACM_SYNC_WRITE);
117         UsbHostDdkTestOpen(HOST_ACM_SYNC_READ);
118         UsbHostDdkTestSyncRead(readBuf);
119         UsbHostDdkTestClose(HOST_ACM_SYNC_READ);
120         EXPECT_EQ(strcmp(const_cast<char *>(data[i]), readBuf), 0);
121     }
122     printf("------end UsbSerialReadSync_003------\n");
123 }
124 
125 /**
126  * @tc.number    : H_Lx_H_Sub_usb_IOread_write_001,H_Lx_H_Sub_usb_IOread_write_006
127  * @tc.name      : USB串口同步读+异步写
128  * @tc.size      : MEDIUM
129  * @tc.type      : FUNC
130  * @tc.level     : Level 1
131  */
132 HWTEST_F(UsbHostRawApiFuncTest, UsbSerialReadSync_004, TestSize.Level1)
133 {
134     printf("------start UsbSerialReadSync_004------\n");
135     const char *data[] = {
136         "0123456789",
137         "Z",
138         "0!a@1#b$2%c^3&D*4(E)5-F_",
139         ""
140     };
141     char readBuf[512] = {0};
142     for (int32_t i = 0; strlen(data[i]) > 0; i++) {
143         memset_s(readBuf, sizeof(readBuf), 0, sizeof(readBuf));
144         UsbHostDdkTestOpen(HOST_ACM_ASYNC_WRITE);
145         UsbHostDdkTestAsyncWrite(const_cast<char *>(data[i]));
146         UsbHostDdkTestClose(HOST_ACM_ASYNC_WRITE);
147         UsbHostDdkTestOpen(HOST_ACM_SYNC_READ);
148         UsbHostDdkTestSyncRead(readBuf);
149         UsbHostDdkTestClose(HOST_ACM_SYNC_READ);
150         EXPECT_EQ(strcmp(const_cast<char *>(data[i]), readBuf), 0);
151     }
152     printf("------end UsbSerialReadSync_004------\n");
153 }
154 
155 /**
156  * @tc.number    : H_Lx_H_Sub_usb_IOread_write_001,H_Lx_H_Sub_usb_IOread_write_003
157  * @tc.name      : USB串口同步读写1KB数据
158  * @tc.size      : MEDIUM
159  * @tc.type      : FUNC
160  * @tc.level     : Level 2
161  */
162 HWTEST_F(UsbHostRawApiFuncTest, UsbSerialReadSync_005, TestSize.Level2)
163 {
164     printf("------start UsbSerialReadSync_005------\n");
165     const string s = "0123456789abcdef";
166     string data;
167     unsigned int totalSize = 1024;
168     unsigned int writeCnt = 8;
169     unsigned int n = 0;
170     while (n < totalSize / writeCnt / s.size()) {
171         data += s;
172         n++;
173     }
174     char readBuf[512] = {0};
175     char writeBuf[512] = {0};
176     strcpy_s(writeBuf, sizeof(writeBuf), data.c_str());
177     for (unsigned int i = 0; i < writeCnt; i++) {
178         memset_s(readBuf, sizeof(readBuf), 0, sizeof(readBuf));
179         UsbHostDdkTestOpen(HOST_ACM_SYNC_WRITE);
180         UsbHostDdkTestSyncWrite(writeBuf);
181         UsbHostDdkTestClose(HOST_ACM_SYNC_WRITE);
182         UsbHostDdkTestOpen(HOST_ACM_SYNC_READ);
183         UsbHostDdkTestSyncRead(readBuf);
184         UsbHostDdkTestClose(HOST_ACM_SYNC_READ);
185         EXPECT_EQ(strcmp(writeBuf, readBuf), 0);
186     }
187     printf("------end UsbSerialReadSync_005------\n");
188 }
189 
TestAsyncRead(char * readBuf,int32_t timeout)190 static void TestAsyncRead(char *readBuf, int32_t timeout)
191 {
192     printf("------TestAsyncRead start-----");
193     if (strlen(readBuf) > 0) {
194         memset_s(readBuf, strlen(readBuf), 0, strlen(readBuf));
195     }
196     timeout = timeout * 1000;
197     UsbHostDdkTestOpen(HOST_ACM_ASYNC_READ);
198     while (timeout-- > 0) {
199         UsbHostDdkTestAsyncRead(readBuf);
200         if (strlen(readBuf) > 0) {
201             break;
202         }
203         usleep(1000);
204     }
205     printf("------TestAsyncRead end-----");
206 }
207 
208 
209 /**
210  * @tc.number    : H_Lx_H_Sub_usb_IOread_write_005, H_Lx_H_Sub_usb_IOread_write_006
211  * @tc.name      : USB串口异步数据读写
212  * @tc.size      : MEDIUM
213  * @tc.type      : FUNC
214  * @tc.level     : Level 1
215  */
216 HWTEST_F(UsbHostRawApiFuncTest, UsbSerialReadAsync_001, TestSize.Level1)
217 {
218     printf("------start UsbSerialReadAsync_001------\n");
219     char writeBuf[512] = "abc";
220     char readBuf[512] = {0};
221     UsbHostDdkTestOpen(HOST_ACM_ASYNC_WRITE);
222     UsbHostDdkTestAsyncWrite(writeBuf);
223     UsbHostDdkTestClose(HOST_ACM_ASYNC_WRITE);
224     TestAsyncRead(readBuf, 5);
225     EXPECT_EQ(strcmp(writeBuf, readBuf), 0);
226     printf("------end UsbSerialReadAsync_001------\n");
227 }
228 
229 /**
230  * @tc.number    : H_Lx_H_Sub_usb_IOread_write_005, H_Lx_H_Sub_usb_IOread_write_003
231  * @tc.name      : USB串口异步读+同步写
232  * @tc.size      : MEDIUM
233  * @tc.type      : FUNC
234  * @tc.level     : Level 1
235  */
236 HWTEST_F(UsbHostRawApiFuncTest, UsbSerialReadAsync_002, TestSize.Level1)
237 {
238     printf("------start UsbSerialReadAsync_002------\n");
239     char writeBuf[512] = "abc";
240     char readBuf[512] = {0};
241     UsbHostDdkTestOpen(HOST_ACM_SYNC_WRITE);
242     UsbHostDdkTestSyncWrite(writeBuf);
243     UsbHostDdkTestClose(HOST_ACM_SYNC_WRITE);
244     TestAsyncRead(readBuf, 5);
245     EXPECT_EQ(strcmp(writeBuf, readBuf), 0);
246     printf("------end UsbSerialReadAsync_002------\n");
247 }
248 
249 /**
250  * @tc.number    : H_Lx_H_Sub_usb_IOread_write_005, H_Lx_H_Sub_usb_IOread_write_006
251  * @tc.name      : USB串口异步数据读写
252  * @tc.size      : MEDIUM
253  * @tc.type      : FUNC
254  * @tc.level     : Level 1
255  */
256 HWTEST_F(UsbHostRawApiFuncTest, UsbSerialReadAsync_003, TestSize.Level1)
257 {
258     printf("------start UsbSerialReadAsync_003------\n");
259     const char *data[] = {
260         "0123456789",
261         "Z",
262         "0!a@1#b$2%c^3&D*4(E)5-F_",
263         ""
264     };
265     char readBuf[512] = {0};
266     for (int32_t i = 0; strlen(data[i]) > 0; i++) {
267         memset_s(readBuf, sizeof(readBuf), 0, sizeof(readBuf));
268         UsbHostDdkTestOpen(HOST_ACM_ASYNC_WRITE);
269         UsbHostDdkTestAsyncWrite(const_cast<char *>(data[i]));
270         UsbHostDdkTestClose(HOST_ACM_ASYNC_WRITE);
271         TestAsyncRead(readBuf, 5);
272         EXPECT_EQ(strcmp(const_cast<char *>(data[i]), readBuf), 0);
273     }
274     printf("------end UsbSerialReadAsync_003------\n");
275 }
276 
277 /**
278  * @tc.number    : H_Lx_H_Sub_usb_IOread_write_005, H_Lx_H_Sub_usb_IOread_write_003
279  * @tc.name      : USB串口异步读+同步写
280  * @tc.size      : MEDIUM
281  * @tc.type      : FUNC
282  * @tc.level     : Level 1
283  */
284 HWTEST_F(UsbHostRawApiFuncTest, UsbSerialReadAsync_004, TestSize.Level1)
285 {
286     printf("------start UsbSerialReadAsync_004------\n");
287     const char *data[] = {
288         "0123456789",
289         "Z",
290         "0!a@1#b$2%c^3&D*4(E)5-F_",
291         ""
292     };
293     char readBuf[512] = {0};
294     for (int32_t i = 0; strlen(data[i]) > 0; i++) {
295         memset_s(readBuf, sizeof(readBuf), 0, sizeof(readBuf));
296         UsbHostDdkTestOpen(HOST_ACM_SYNC_WRITE);
297         UsbHostDdkTestSyncWrite(const_cast<char *>(data[i]));
298         UsbHostDdkTestClose(HOST_ACM_SYNC_WRITE);
299         TestAsyncRead(readBuf, 5);
300         EXPECT_EQ(strcmp(const_cast<char *>(data[i]), readBuf), 0);
301     }
302     printf("------end UsbSerialReadAsync_004------\n");
303 }
304 
305 /**
306  * @tc.number    : H_Lx_H_Sub_usb_IOread_write_005, H_Lx_H_Sub_usb_IOread_write_006
307  * @tc.name      : USB串口异步读写1KB数据
308  * @tc.size      : MEDIUM
309  * @tc.type      : FUNC
310  * @tc.level     : Level 2
311  */
312 HWTEST_F(UsbHostRawApiFuncTest, UsbSerialReadAsync_005, TestSize.Level2)
313 {
314     printf("------start UsbSerialReadAsync_005------\n");
315     const string s = "0123456789abcdef";
316     string data;
317     int32_t totalSize = 1024;
318     int32_t writeCnt = 8;
319     unsigned int n = 0;
320     while (n < totalSize / writeCnt / s.size()) {
321         data += s;
322         n++;
323     }
324     char readBuf[512] = {0};
325     char writeBuf[512] = {0};
326     strcpy_s(writeBuf, sizeof(writeBuf), data.c_str());
327     for (int32_t i = 0; i < writeCnt; i++) {
328         memset_s(readBuf, sizeof(readBuf), 0, sizeof(readBuf));
329         UsbHostDdkTestOpen(HOST_ACM_ASYNC_WRITE);
330         UsbHostDdkTestAsyncWrite(writeBuf);
331         UsbHostDdkTestClose(HOST_ACM_ASYNC_WRITE);
332         TestAsyncRead(readBuf, 5);
333         EXPECT_EQ(strcmp(writeBuf, readBuf), 0);
334     }
335     printf("------end UsbSerialReadAsync_005------\n");
336 }
337 }
338