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