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 "usb_utils.h"
22 
23 using namespace std;
24 using namespace testing::ext;
25 
26 namespace {
27 const string RLOG_FILE = "/data/acm_read_xts";
28 
29 class UsbHostRawApiFuncTest : public testing::Test {
30 protected:
SetUpTestCase(void)31     static void SetUpTestCase(void)
32     {
33         printf("------start UsbHostRawApiFuncTest------\n");
34         system("cat /dev/null > /data/acm_write_xts");
35         system("cat /dev/null > /data/acm_read_xts");
36     }
TearDownTestCase(void)37     static void TearDownTestCase(void)
38     {
39         printf("------end UsbHostRawApiFuncTest------\n");
40     }
41 };
42 
43 /**
44  * @tc.number    : H_Lx_H_Sub_usb_IO read_001,H_Lx_H_Sub_usb_IO read_007
45  * @tc.name      : USB串口同步数据读写
46  * @tc.size      : MEDIUM
47  * @tc.type      : FUNC
48  * @tc.level     : Level 1
49  */
50 HWTEST_F(UsbHostRawApiFuncTest, CheckRawApiWriteSync_001, TestSize.Level1)
51 {
52     printf("------start CheckRawApiWriteSync_001------\n");
53     const string data = "abc";
54     double startTs = GetNowTs();
55     string wlog, rlog;
56     wlog = "send data[" + data + "] to device";
57     rlog = "recv data[" + data + "] from device";
58     ASSERT_EQ(system("usbhost_ddk_test -aR &"), 0);
59     ASSERT_EQ(system(("usbhost_ddk_test -aW '" + data + "'").c_str()), 0);
60     sleep(3);
61     EXPECT_TRUE(HasLog(wlog, startTs)) << "ErrInfo: cannot find sync write log";
62     EXPECT_TRUE(HasLog(rlog, startTs, RLOG_FILE)) << "ErrInfo: cannot find sync recv log";
63     printf("------end CheckRawApiWriteSync_001------\n");
64 }
65 
66 /**
67  * @tc.number    : H_Lx_H_Sub_usb_IO read_001,H_Lx_H_Sub_usb_IO read_007
68  * @tc.name      : USB串口同步数据读写
69  * @tc.size      : MEDIUM
70  * @tc.type      : FUNC
71  * @tc.level     : Level 1
72  */
73 HWTEST_F(UsbHostRawApiFuncTest, CheckRawApiWriteSync_002, TestSize.Level1)
74 {
75     printf("------start CheckRawApiWriteSync_002------\n");
76     const string data[] = {
77         "0123456789",
78         "Z",
79         "0!a@1#b$2%c^3&D*4(E)5-F_",
80         ""
81     };
82     double startTs = GetNowTs();
83     string wlog, rlog;
84     for (int32_t i = 0; data[i].size() > 0; i++) {
85         wlog = "send data[" + data[i] + "] to device";
86         rlog = "recv data[" + data[i] + "] from device";
87         ASSERT_EQ(system("usbhost_ddk_test -aR &"), 0);
88         ASSERT_EQ(system(("usbhost_ddk_test -aW '" + data[i] + "'").c_str()), 0);
89         sleep(3);
90         EXPECT_TRUE(HasLog(wlog, startTs)) << "ErrInfo: cannot find sync write log";
91         EXPECT_TRUE(HasLog(rlog, startTs, RLOG_FILE)) << "ErrInfo: cannot find sync recv log";
92     }
93     printf("------end CheckRawApiWriteSync_002------\n");
94 }
95 
96 /**
97  * @tc.number    : H_Lx_H_Sub_usb_IO read_003, H_Lx_H_Sub_usb_IO read_009
98  * @tc.name      : USB串口同步读写1KB数据
99  * @tc.size      : MEDIUM
100  * @tc.type      : FUNC
101  * @tc.level     : Level 2
102  */
103 HWTEST_F(UsbHostRawApiFuncTest, CheckRawApiWriteSync_003, TestSize.Level2)
104 {
105     printf("------start CheckRawApiWriteSync_003------\n");
106     const string s = "0123456789abcdef";
107     string data;
108     int32_t totalSize = 1024;
109     int32_t writeCnt = 8;
110     unsigned int n = 0;
111     while (n < totalSize / writeCnt / s.size()) {
112         data += s;
113         n++;
114     }
115     const string wlog = "send data[" + data + "] to device";
116     const string rlog = "recv data[" + data + "] from device";
117     double startTs;
118     for (int32_t i = 0; i < writeCnt; i++) {
119         startTs = GetNowTs();
120         ASSERT_EQ(system("usbhost_ddk_test -aR &"), 0);
121         ASSERT_EQ(system(("usbhost_ddk_test -aW '" + data + "'").c_str()), 0);
122         sleep(3);
123         EXPECT_TRUE(HasLog(wlog, startTs)) << "ErrInfo: cannot find sync write log";
124         EXPECT_TRUE(HasLog(rlog, startTs, RLOG_FILE)) << "ErrInfo: cannot find sync recv log";
125     }
126     printf("------end CheckRawApiWriteSync_003------\n");
127 }
128 
129 /**
130  * @tc.number    : H_Lx_H_Sub_usb_IO read_013, H_Lx_H_Sub_usb_IO read_017
131  * @tc.name      : USB串口异步数据读写
132  * @tc.size      : MEDIUM
133  * @tc.type      : FUNC
134  * @tc.level     : Level 1
135  */
136 HWTEST_F(UsbHostRawApiFuncTest, CheckRawApiWriteAsync_001, TestSize.Level1)
137 {
138     printf("------start CheckRawApiWriteAsync_001------\n");
139     ASSERT_EQ(system("usbhost_ddk_test -ar &"), 0) << \
140     "ErrInfo:  failed to start async read";
141     sleep(3);
142     const string data = "abc";
143     double startTs = GetNowTs();
144     string wlog, rlog;
145     wlog = "send data[" + data + "] to device";
146     rlog = "recv data[" + data + "] from device";
147     ASSERT_EQ(system(("usbhost_ddk_test -aw '" + data + "'").c_str()), 0);
148     sleep(3);
149     EXPECT_TRUE(HasLog(wlog, startTs)) << "ErrInfo: cannot find async write log";
150     EXPECT_TRUE(HasLog(rlog, startTs, RLOG_FILE)) << "ErrInfo: cannot find async recv log";
151     printf("------end CheckRawApiWriteAsync_001------\n");
152 }
153 
154 /**
155  * @tc.number    : H_Lx_H_Sub_usb_IO read_013, H_Lx_H_Sub_usb_IO read_017
156  * @tc.name      : USB串口异步数据读写
157  * @tc.size      : MEDIUM
158  * @tc.type      : FUNC
159  * @tc.level     : Level 1
160  */
161 HWTEST_F(UsbHostRawApiFuncTest, CheckRawApiWriteAsync_002, TestSize.Level1)
162 {
163     printf("------start CheckRawApiWriteAsync_002------\n");
164     const string data[] = {
165         "0123456789",
166         "Z",
167         "0!a@1#b$2%c^3&D*4(E)5-F_",
168         ""
169     };
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 -aw '" + data[i] + "'").c_str()), 0);
176         sleep(3);
177         EXPECT_TRUE(HasLog(wlog, startTs)) << "ErrInfo: cannot find async write log";
178         EXPECT_TRUE(HasLog(rlog, startTs, RLOG_FILE)) << "ErrInfo: cannot find async recv log";
179     }
180     printf("------end CheckRawApiWriteAsync_002------\n");
181 }
182 
183 /**
184  * @tc.number    : H_Lx_H_Sub_usb_IO read_019
185  * @tc.name      : USB串口异步写1KB数据
186  * @tc.size      : MEDIUM
187  * @tc.type      : FUNC
188  * @tc.level     : Level 2
189  */
190 HWTEST_F(UsbHostRawApiFuncTest, CheckRawApiWriteAsync_003, TestSize.Level2)
191 {
192     printf("------start CheckRawApiWriteAsync_003------\n");
193     const string s = "0123456789abcdef";
194     string data;
195     int32_t totalSize = 1024;
196     int32_t writeCnt = 8;
197     unsigned int n = 0;
198     while (n < totalSize / writeCnt / s.size()) {
199         data += s;
200         n++;
201     }
202     const string wlog = "send data[" + data + "] to device";
203     const string rlog = "recv data[" + data + "] from device";
204     double startTs;
205     for (int32_t i = 0; i < writeCnt; i++) {
206         startTs = GetNowTs();
207         ASSERT_EQ(system(("usbhost_ddk_test -aw '" + data + "'").c_str()), 0);
208         sleep(3);
209         EXPECT_TRUE(HasLog(wlog, startTs)) << "ErrInfo: cannot find async write log";
210         EXPECT_TRUE(HasLog(rlog, startTs, RLOG_FILE)) << "ErrInfo: cannot find async recv log";
211     }
212     ASSERT_EQ(system("killall usbhost_ddk_test"), 0) << "ErrInfo:  failed to kill async read";
213     printf("------end CheckRawApiWriteAsync_003------\n");
214 }
215 }