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 #include "securec.h"
21 #include "lib_acm_test.h"
22
23 using namespace std;
24 using namespace testing::ext;
25
26 namespace {
27 class UsbDeviceSerialFuncTest : public testing::Test {
28 public:
29 static void SetUpTestCase();
30 static void TearDownTestCase();
31 };
32
SetUpTestCase()33 void UsbDeviceSerialFuncTest::SetUpTestCase()
34 {
35 AcmOpen();
36 }
37
TearDownTestCase()38 void UsbDeviceSerialFuncTest::TearDownTestCase()
39 {
40 AcmClose();
41 }
42
43 /**
44 * @tc.number : H_Lx_D_Sub_usb_Control_msg_004,H_Lx_D_Sub_usb_Control_msg_006
45 * @tc.name : 验证device sdk的数据读写
46 * @tc.type : FUNC
47 * @tc.level : Level 1
48 */
49 HWTEST_F(UsbDeviceSerialFuncTest, DeviceIOTest_001, TestSize.Level1)
50 {
51 printf("------start DeviceIOTest_001------\n");
52 char writeBuf[] = "abc123";
53 char readBuf[256] = {0};
54 AcmWrite(writeBuf);
55 AcmRead(readBuf);
56 EXPECT_EQ(strcmp(writeBuf, readBuf), 0);
57 printf("------end DeviceIOTest_001------\n");
58 }
59
60 /**
61 * @tc.number : H_Lx_D_Sub_usb_Control_msg_004,H_Lx_D_Sub_usb_Control_msg_006
62 * @tc.name : 验证device sdk的数据读写
63 * @tc.type : FUNC
64 * @tc.level : Level 1
65 */
66 HWTEST_F(UsbDeviceSerialFuncTest, DeviceIOTest_002, TestSize.Level1)
67 {
68 printf("------start DeviceIOTest_002------\n");
69 const char * data[] = {
70 "0123456789",
71 "Z",
72 "0!a@1#b$2%c^3&D*4(E)5-F_",
73 ""
74 };
75 char readBuf[256] = {0};
76 for (int32_t i = 0; strlen(data[i]) > 0; i++) {
77 memset_s(readBuf, sizeof(readBuf), 0, sizeof(readBuf));
78 AcmWrite(const_cast<char *>(data[i]));
79 AcmRead(readBuf);
80 EXPECT_EQ(strcmp(const_cast<char *>(data[i]), readBuf), 0);
81 }
82 printf("------end DeviceIOTest_002------\n");
83 }
84
85 /**
86 * @tc.number : H_Lx_D_Sub_usb_Control_msg_004,H_Lx_D_Sub_usb_Control_msg_006
87 * @tc.name : 验证device sdk的数据读写
88 * @tc.type : FUNC
89 * @tc.level : Level 2
90 */
91 HWTEST_F(UsbDeviceSerialFuncTest, DeviceIOTest_003, TestSize.Level2)
92 {
93 printf("------start DeviceIOTest_003------\n");
94 char wdata[256] = "AAA1234567890";
95 char rdata[256] = {0};
96 for (int32_t i = 0; i < 30; i++) {
97 AcmWrite(wdata);
98 AcmRead(rdata);
99 memset_s(rdata, sizeof(rdata), 0, sizeof(rdata));
100 }
101 char writeBuf[] = "aaa111";
102 char readBuf[256] = {0};
103 AcmWrite(writeBuf);
104 AcmRead(readBuf);
105 EXPECT_EQ(strcmp(writeBuf, readBuf), 0);
106 printf("------end DeviceIOTest_003------\n");
107 }
108
109 /**
110 * @tc.number : H_Lx_D_Sub_usb_Control_msg_004,H_Lx_D_Sub_usb_Control_msg_006
111 * @tc.name : 验证device sdk的数据读写
112 * @tc.type : FUNC
113 * @tc.level : Level 2
114 */
115 HWTEST_F(UsbDeviceSerialFuncTest, DeviceIOTest_004, TestSize.Level2)
116 {
117 printf("------start DeviceIOTest_004------\n");
118 const string s = "0123456789abcdef";
119 string data;
120 int32_t totalSize = 1024;
121 int32_t writeCnt = 8;
122 unsigned int n = 0;
123 while (n < totalSize / writeCnt / s.size()) {
124 data += s;
125 n++;
126 }
127 char readBuf[256] = {0};
128 char writeBuf[256] = {0};
129 strcpy_s(writeBuf, sizeof(writeBuf), data.c_str());
130 for (int32_t i = 0; i < writeCnt; i++) {
131 memset_s(readBuf, sizeof(readBuf), 0, sizeof(readBuf));
132 AcmWrite(writeBuf);
133 AcmRead(readBuf);
134 EXPECT_EQ(strcmp(writeBuf, readBuf), 0);
135 }
136 char exitBuf[] = "q";
137 AcmWrite(exitBuf);
138 printf("------end DeviceIOTest_004------\n");
139 }
140
141 /**
142 * @tc.number : H_Lx_D_Sub_usb_Instance_002
143 * @tc.name : 支持应用添加自定义属性和添加Set/Get回调函数
144 * @tc.type : FUNC
145 * @tc.level : Level 2
146 */
147 HWTEST_F(UsbDeviceSerialFuncTest, HcsPropTest_001, TestSize.Level1)
148 {
149 printf("------start HcsPropTest_001------\n");
150 const char *propName1 = "idVendor";
151 const char *propName2 = "idProduct";
152 char propValue1[] = "12d1";
153 char propValue2[] = "5000";
154 char readBuf1[256] = {0};
155 char readBuf2[256] = {0};
156 acm_prop_read(propName1, readBuf1);
157 printf("readBuf1==>%s\n", readBuf1);
158 EXPECT_EQ(strcmp(propValue1, readBuf1), 0);
159 acm_prop_read(propName2, readBuf2);
160 printf("readBuf2==>%s\n", readBuf2);
161 EXPECT_EQ(strcmp(propValue2, readBuf2), 0);
162 printf("------end HcsPropTest_001------\n");
163 }
164
165 /**
166 * @tc.number : H_Lx_D_Sub_usb_Instance_002
167 * @tc.name : 支持应用添加自定义属性和添加Set/Get回调函数
168 * @tc.type : FUNC
169 * @tc.level : Level 2
170 */
171 HWTEST_F(UsbDeviceSerialFuncTest, HcsPropTest_002, TestSize.Level1)
172 {
173 printf("------start HcsPropTest_002------\n");
174 const char *propName = "idVendor";
175 const char *writeBuf = "1234";
176 char readBuf[256] = {0};
177 acm_prop_write(propName, writeBuf);
178 acm_prop_read(propName, readBuf);
179 printf("readBuf==>%s\n", readBuf);
180 EXPECT_EQ(strcmp(const_cast<char *>(writeBuf), readBuf), 0);
181 printf("------end HcsPropTest_002------\n");
182 }
183
184 /**
185 * @tc.number : H_Lx_D_Sub_usb_Instance_002
186 * @tc.name : 支持应用添加自定义属性和添加Set/Get回调函数
187 * @tc.type : FUNC
188 * @tc.level : Level 2
189 */
190 HWTEST_F(UsbDeviceSerialFuncTest, HcsPropTest_003, TestSize.Level1)
191 {
192 printf("------start HcsPropTest_003------\n");
193 const char *propName = "idVendor";
194 const char *writeBuf = "12d1";
195 char readBuf[256] = {0};
196 acm_prop_write(propName, writeBuf);
197 acm_prop_read(propName, readBuf);
198 printf("readBuf==>%s\n", readBuf);
199 EXPECT_EQ(strcmp(const_cast<char *>(writeBuf), readBuf), 0);
200 printf("------end HcsPropTest_003------\n");
201 }
202
203 /**
204 * @tc.number : H_Lx_D_Sub_usb_Instance_002
205 * @tc.name : 支持应用添加自定义属性和添加Set/Get回调函数
206 * @tc.type : FUNC
207 * @tc.level : Level 2
208 */
209 HWTEST_F(UsbDeviceSerialFuncTest, HcsPropTest_004, TestSize.Level1)
210 {
211 printf("------start HcsPropTest_004------\n");
212 const char *propName = "testp";
213 const char *propValue = "a123";
214 char readBuf[256] = {0};
215 acm_prop_regist(propName, propValue);
216 acm_prop_read(propName, readBuf);
217 printf("readBuf==>%s", readBuf);
218 EXPECT_EQ(strcmp(const_cast<char *>(propValue), readBuf), 0);
219 printf("------end HcsPropTest_004------\n");
220 }
221
222 /**
223 * @tc.number : H_Lx_D_Sub_usb_Instance_002
224 * @tc.name : 支持应用添加自定义属性和添加Set/Get回调函数
225 * @tc.type : FUNC
226 * @tc.level : Level 2
227 */
228 HWTEST_F(UsbDeviceSerialFuncTest, HcsPropTest_005, TestSize.Level1)
229 {
230 printf("------start HcsPropTest_005------\n");
231 const char *propName = "testp";
232 const char *writeBuf = "111";
233 char readBuf[256] = {0};
234 acm_prop_write(propName, writeBuf);
235 acm_prop_read(propName, readBuf);
236 printf("readBuf==>%s\n", readBuf);
237 EXPECT_EQ(strcmp(const_cast<char *>(writeBuf), readBuf), 0);
238 printf("------end HcsPropTest_005------\n");
239 }
240 }
241