1 /*
2 * Copyright (c) 2025 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 "usbd_serial_test.h"
17
18 #include "v1_0/iserial_interface.h"
19 #include "v1_0/serial_types.h"
20
21 using namespace testing::ext;
22 using namespace OHOS::HDI::Usb::Serial::V1_0;
23
24 #define ERR_CODE_IOEXCEPTION (-5)
25 #define ERR_CODE_DEVICENOTOPEN (-6)
26 #define ERR_CODE_TIMED_OUT (-7)
27
28 constexpr int32_t VALID_PORTID = 0;
29 constexpr int32_t INVALID_PORTID = -1;
30 constexpr int32_t FAILED = -1;
31 constexpr int32_t OK = 0;
32 constexpr int32_t ONE_KBYTE = 1024;
33 constexpr int32_t ONE_SECOND = 1000;
34 constexpr int32_t MAX_MEMORY = 8192;
35 constexpr uint8_t DATA_BITS_SIX = 6;
36 static std::vector<OHOS::HDI::Usb::Serial::V1_0::SerialPort> g_portList;
37
38 template<typename T>
make_shared_array(size_t size)39 std::shared_ptr<T> make_shared_array(size_t size)
40 {
41 if (size == 0) {
42 return NULL;
43 }
44 if (size > MAX_MEMORY) {
45 return NULL;
46 }
47 T* buffer = new (std::nothrow)T[size];
48 if (!buffer) {
49 return NULL;
50 }
51 return std::shared_ptr<T>(buffer, [] (T* p) { delete[] p; });
52 }
53
54 namespace OHOS {
55 namespace SERIAL {
56 sptr<OHOS::HDI::Usb::Serial::V1_0::ISerialInterface> g_serialInterface = nullptr;
57
SetUpTestCase(void)58 void SerialTest::SetUpTestCase(void)
59 {
60 g_serialInterface = OHOS::HDI::Usb::Serial::V1_0::ISerialInterface::Get("serial_interface_service", true);
61 if (g_serialInterface == nullptr) {
62 printf("NULL\n");
63 exit(0);
64 }
65 std::cout << "请先插拔串口线,输入回车继续" << std::endl;
66 int c;
67 do {
68 c = getchar();
69 } while (c != '\n' && c != EOF);
70 g_serialInterface->SerialGetPortList(g_portList);
71 }
72
TearDownTestCase(void)73 void SerialTest::TearDownTestCase(void) {}
74
SetUp(void)75 void SerialTest::SetUp(void) {}
76
TearDown(void)77 void SerialTest::TearDown(void) {}
78
79 /**
80 * @tc.name: SerialGetPortList_001
81 * @tc.desc: Test functions to int32_t SerialGetPortList(std::vector<OHOS::HDI::Usb::Serial::V1_0::SerialPort>&
82 * portList)
83 * @tc.type: FUNC
84 */
85 HWTEST_F(SerialTest, SerialGetPortList_001, TestSize.Level1)
86 {
87 EXPECT_NE(g_portList.size(), 0);
88 EXPECT_EQ(g_portList[0].portId, VALID_PORTID);
89 }
90
91 /**
92 * @tc.name: SerialOpen_001
93 * @tc.desc: Test functions to int32_t SerialOpen(int32_t portId)
94 * @tc.type: FUNC
95 */
96 HWTEST_F(SerialTest, SerialOpen_001, TestSize.Level1)
97 {
98 g_serialInterface->SerialClose(VALID_PORTID);
99 EXPECT_EQ(g_serialInterface->SerialOpen(VALID_PORTID), OK);
100 g_serialInterface->SerialClose(VALID_PORTID);
101 }
102
103 /**
104 * @tc.name: SerialOpen_002
105 * @tc.desc: Test functions to int32_t SerialOpen(int32_t portId)
106 * @tc.type: FUNC
107 */
108 HWTEST_F(SerialTest, SerialOpen_002, TestSize.Level1)
109 {
110 g_serialInterface->SerialClose(INVALID_PORTID);
111 EXPECT_NE(g_serialInterface->SerialOpen(INVALID_PORTID), OK);
112 g_serialInterface->SerialClose(INVALID_PORTID);
113 }
114
115 /**
116 * @tc.name: SerialWrite_001
117 * @tc.desc: Test functions to int32_t SerialWrite(int32_t portId, const std::vector<uint8_t> &data,
118 * uint32_t size, uint32_t timeout)
119 * @tc.type: FUNC
120 */
121 HWTEST_F(SerialTest, SerialWrite_001, TestSize.Level1)
122 {
123 g_serialInterface->SerialClose(VALID_PORTID);
124 g_serialInterface->SerialOpen(VALID_PORTID);
125 std::vector<uint8_t> data = { 't', 'e', 's', 't' };
126 int32_t ret = g_serialInterface->SerialWrite(VALID_PORTID, data, data.size(), ONE_SECOND);
127 EXPECT_EQ(ret, data.size());
128 g_serialInterface->SerialClose(VALID_PORTID);
129 }
130
131 /**
132 * @tc.name: SerialWrite_002
133 * @tc.desc: Test functions to int32_t SerialWrite(int32_t portId, const std::vector<uint8_t> &data,
134 * uint32_t size, uint32_t timeout)
135 * @tc.type: FUNC
136 */
137 HWTEST_F(SerialTest, SerialWrite_002, TestSize.Level1)
138 {
139 g_serialInterface->SerialClose(INVALID_PORTID);
140 g_serialInterface->SerialOpen(INVALID_PORTID);
141 std::vector<uint8_t> data = { 't', 'e', 's', 't' };
142 int32_t ret = g_serialInterface->SerialWrite(INVALID_PORTID, data, data.size(), ONE_SECOND);
143 EXPECT_EQ(ret, ERR_CODE_IOEXCEPTION);
144 g_serialInterface->SerialClose(INVALID_PORTID);
145 }
146
147 /**
148 * @tc.name: SerialRead_001
149 * @tc.desc: Test functions to int32_t SerialRead(int32_t portId, uint8_t *data,
150 * uint32_t size, uint32_t timeout)
151 * @tc.type: FUNC
152 */
153 HWTEST_F(SerialTest, SerialRead_001, TestSize.Level1)
154 {
155 g_serialInterface->SerialClose(VALID_PORTID);
156 g_serialInterface->SerialOpen(VALID_PORTID);
157 std::vector<uint8_t> data;
158 int32_t ret = g_serialInterface->SerialRead(VALID_PORTID, data, ONE_KBYTE, ONE_SECOND);
159 EXPECT_EQ(ret, ERR_CODE_TIMED_OUT);
160 g_serialInterface->SerialClose(VALID_PORTID);
161 }
162
163 /**
164 * @tc.name: SerialRead_002
165 * @tc.desc: Test functions to int32_t SerialRead(int32_t portId, uint8_t *data,
166 * uint32_t size, uint32_t timeout)
167 * @tc.type: FUNC
168 */
169 HWTEST_F(SerialTest, SerialRead_002, TestSize.Level1)
170 {
171 g_serialInterface->SerialClose(INVALID_PORTID);
172 g_serialInterface->SerialOpen(INVALID_PORTID);
173 std::vector<uint8_t> data;
174 int32_t ret = g_serialInterface->SerialRead(INVALID_PORTID, data, ONE_KBYTE, ONE_SECOND);
175 EXPECT_EQ(ret, ERR_CODE_IOEXCEPTION);
176 g_serialInterface->SerialClose(INVALID_PORTID);
177 }
178
179 /**
180 * @tc.name: SerialRead_003
181 * @tc.desc: Test functions to int32_t SerialRead(int32_t portId, uint8_t *data,
182 * uint32_t size, uint32_t timeout)
183 * @tc.type: FUNC
184 */
185 HWTEST_F(SerialTest, SerialRead_003, TestSize.Level1)
186 {
187 g_serialInterface->SerialClose(VALID_PORTID);
188 g_serialInterface->SerialOpen(VALID_PORTID);
189 std::cout << "请打开串口工具单次发送数据,输入回车继续" << std::endl;
190 int c;
191 do {
192 c = getchar();
193 } while (c != '\n' && c != EOF);
194 std::vector<uint8_t> data;
195 int32_t ret = g_serialInterface->SerialRead(VALID_PORTID, data, ONE_KBYTE, ONE_SECOND);
196 EXPECT_NE(ret, FAILED);
197 g_serialInterface->SerialClose(VALID_PORTID);
198 }
199
200 /**
201 * @tc.name: SerialGetAttribute_001
202 * @tc.desc: Test functions to int32_t SerialGetAttribute(int32_t portId,
203 * OHOS::HDI::Usb::Serial::V1_0::SerialAttribute& attributeInfo)
204 * @tc.type: FUNC
205 */
206 HWTEST_F(SerialTest, SerialGetAttribute_001, TestSize.Level1)
207 {
208 g_serialInterface->SerialClose(VALID_PORTID);
209 g_serialInterface->SerialOpen(VALID_PORTID);
210 OHOS::HDI::Usb::Serial::V1_0::SerialAttribute attributeInfo;
211 int32_t ret = g_serialInterface->SerialGetAttribute(VALID_PORTID, attributeInfo);
212 EXPECT_EQ(ret, OK);
213 g_serialInterface->SerialClose(VALID_PORTID);
214 }
215
216 /**
217 * @tc.name: SerialGetAttribute_002
218 * @tc.desc: Test functions to int32_t SerialGetAttribute(int32_t portId,
219 * OHOS::HDI::Usb::Serial::V1_0::SerialAttribute& attributeInfo)
220 * @tc.type: FUNC
221 */
222 HWTEST_F(SerialTest, SerialGetAttribute_002, TestSize.Level1)
223 {
224 g_serialInterface->SerialClose(INVALID_PORTID);
225 g_serialInterface->SerialOpen(INVALID_PORTID);
226 OHOS::HDI::Usb::Serial::V1_0::SerialAttribute attributeInfo;
227 int32_t ret = g_serialInterface->SerialGetAttribute(INVALID_PORTID, attributeInfo);
228 EXPECT_NE(ret, OK);
229 g_serialInterface->SerialClose(INVALID_PORTID);
230 }
231
232 /**
233 * @tc.name: SerialSetAttribute_001
234 * @tc.desc: Test functions to int32_t SerialSetAttribute(int32_t portId,
235 * const OHOS::HDI::Usb::Serial::V1_0::SerialAttribute& attributeInfo)
236 * @tc.type: FUNC
237 */
238 HWTEST_F(SerialTest, SerialSetAttribute_001, TestSize.Level1)
239 {
240 g_serialInterface->SerialClose(VALID_PORTID);
241 g_serialInterface->SerialOpen(VALID_PORTID);
242 OHOS::HDI::Usb::Serial::V1_0::SerialAttribute attributeInfo;
243 attributeInfo.baudrate = OHOS::HDI::Usb::Serial::V1_0::BAUDRATE_576000;
244 attributeInfo.dataBits = DATA_BITS_SIX;
245 attributeInfo.parity = OHOS::HDI::Usb::Serial::V1_0::USB_ATTR_PARITY_ODD;
246 attributeInfo.stopBits = OHOS::HDI::Usb::Serial::V1_0::USB_ATTR_STOPBIT_2;
247 int32_t ret = g_serialInterface->SerialSetAttribute(VALID_PORTID, attributeInfo);
248 EXPECT_EQ(ret, OK);
249 g_serialInterface->SerialClose(VALID_PORTID);
250 }
251
252 /**
253 * @tc.name: SerialSetAttribute_002
254 * @tc.desc: Test functions to int32_t SerialSetAttribute(int32_t portId,
255 * const OHOS::HDI::Usb::Serial::V1_0::SerialAttribute& attributeInfo)
256 * @tc.type: FUNC
257 */
258 HWTEST_F(SerialTest, SerialSetAttribute_002, TestSize.Level1)
259 {
260 g_serialInterface->SerialClose(INVALID_PORTID);
261 g_serialInterface->SerialOpen(INVALID_PORTID);
262 OHOS::HDI::Usb::Serial::V1_0::SerialAttribute attributeInfo;
263 attributeInfo.baudrate = OHOS::HDI::Usb::Serial::V1_0::BAUDRATE_576000;
264 attributeInfo.dataBits = DATA_BITS_SIX;
265 attributeInfo.parity = OHOS::HDI::Usb::Serial::V1_0::USB_ATTR_PARITY_ODD;
266 attributeInfo.stopBits = OHOS::HDI::Usb::Serial::V1_0::USB_ATTR_STOPBIT_2;
267 int32_t ret = g_serialInterface->SerialSetAttribute(INVALID_PORTID, attributeInfo);
268 EXPECT_NE(ret, OK);
269 g_serialInterface->SerialClose(INVALID_PORTID);
270 }
271
272 /**
273 * @tc.name: SerialClose_001
274 * @tc.desc: Test functions to int32_t SerialClose(int32_t portId)
275 * @tc.type: FUNC
276 */
277 HWTEST_F(SerialTest, SerialClose_001, TestSize.Level1)
278 {
279 g_serialInterface->SerialClose(VALID_PORTID);
280 g_serialInterface->SerialOpen(VALID_PORTID);
281 EXPECT_EQ(g_serialInterface->SerialClose(VALID_PORTID), OK);
282 g_serialInterface->SerialClose(VALID_PORTID);
283 }
284
285 }
286 }