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 WLOG_FILE = "/data/acm_write_xts";
28 const string RLOG_FILE = "/data/acm_read_xts";
29
30 class UsbDeviceSerialFuncTest : public testing::Test {
31 protected:
SetUpTestCase(void)32 static void SetUpTestCase(void)
33 {
34 printf("------start UsbDeviceSerialFuncTest------\n");
35 system("cat /dev/null > /data/acm_write_xts");
36 system("cat /dev/null > /data/acm_read_xts");
37 }
TearDownTestCase(void)38 static void TearDownTestCase(void)
39 {
40 printf("------end UsbDeviceSerialFuncTest------\n");
41 }
42 };
43
44 /**
45 * @tc.number : H_Lx_H_Sub_usb_IO read_002,H_Lx_H_Sub_usb_IO read_008
46 * @tc.name : 验证device sdk的数据读写
47 * @tc.type : FUNC
48 * @tc.level : Level 1
49 */
HWTEST_F(UsbDeviceSerialFuncTest, DeviceIOTest_001, TestSize.Level1)50 HWTEST_F(UsbDeviceSerialFuncTest, DeviceIOTest_001, TestSize.Level1)
51 {
52 printf("------start DeviceIOTest_001------\n");
53 ASSERT_EQ(system("usb_dev_test -1 abc &"), 0) << "ErrInfo: failed to start acm read";
54 sleep(2);
55 const string data = "abc123";
56 double startTs = GetNowTs();
57 string wlog, rlog;
58 ASSERT_EQ(system(("usb_dev_test -2 '" + data + "'").c_str()), 0);
59 wlog = "send data[" + data + "] to host";
60 rlog = "recv data[" + data + "] from host";
61 sleep(2);
62 EXPECT_TRUE(HasLog(wlog, startTs, WLOG_FILE));
63 EXPECT_TRUE(HasLog(rlog, startTs, RLOG_FILE));
64 printf("------end DeviceIOTest_001------\n");
65 }
66
67 /**
68 * @tc.number : H_Lx_H_Sub_usb_IO read_002,H_Lx_H_Sub_usb_IO read_008
69 * @tc.name : 验证device sdk的数据读写
70 * @tc.type : FUNC
71 * @tc.level : Level 1
72 */
HWTEST_F(UsbDeviceSerialFuncTest, DeviceIOTest_002, TestSize.Level1)73 HWTEST_F(UsbDeviceSerialFuncTest, DeviceIOTest_002, TestSize.Level1)
74 {
75 printf("------start DeviceIOTest_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 ASSERT_EQ(system(("usb_dev_test -2 '" + data[i] + "'").c_str()), 0);
86 wlog = "send data[" + data[i] + "] to host";
87 rlog = "recv data[" + data[i] + "] from host";
88 sleep(2);
89 EXPECT_TRUE(HasLog(wlog, startTs, WLOG_FILE));
90 EXPECT_TRUE(HasLog(rlog, startTs, RLOG_FILE));
91 }
92 printf("------end DeviceIOTest_002------\n");
93 }
94
95 /**
96 * @tc.number : H_Lx_H_Sub_usb_IO read_002,H_Lx_H_Sub_usb_IO read_008
97 * @tc.name : 验证device sdk的数据读写
98 * @tc.type : FUNC
99 * @tc.level : Level 2
100 */
HWTEST_F(UsbDeviceSerialFuncTest, DeviceIOTest_003, TestSize.Level2)101 HWTEST_F(UsbDeviceSerialFuncTest, DeviceIOTest_003, TestSize.Level2)
102 {
103 printf("------start DeviceIOTest_003------\n");
104 for (int32_t i = 0; i < 30; i++) {
105 system("usb_dev_test -2 `date +%s%N | md5sum | cut -c 1-32`");
106 }
107 sleep(1);
108 double startTs = GetNowTs();
109 string wlog, rlog;
110 const string data = "abc";
111 ASSERT_EQ(system(("usb_dev_test -2 '" + data + "'").c_str()), 0);
112 wlog = "send data[" + data + "] to host";
113 rlog = "recv data[" + data + "] from host";
114 sleep(2);
115 EXPECT_TRUE(HasLog(wlog, startTs, WLOG_FILE));
116 EXPECT_TRUE(HasLog(rlog, startTs, RLOG_FILE));
117 printf("------end DeviceIOTest_003------\n");
118 }
119
120 /**
121 * @tc.number : H_Lx_D_Sub_usb_Instance_002, H_Lx_D_Sub_usb_Descriptor_002, H_Lx_D_Sub_usb_Descriptor_001,
122 * @tc.name : 验证HCS中配置的设备描述符、配置描述符、接口描述符和端点描述符解析是否正确
123 * @tc.type : FUNC
124 * @tc.level : Level 1
125 */
HWTEST_F(UsbDeviceSerialFuncTest, HcsConfigTest_001, TestSize.Level1)126 HWTEST_F(UsbDeviceSerialFuncTest, HcsConfigTest_001, TestSize.Level1)
127 {
128 printf("------start HcsConfigTest_001------\n");
129 const char *idVendor = "12d1";
130 const char *idPorduct = "5000";
131 const char *bcdDevice = "0223";
132 const char *configurationValue = "1";
133 const int32_t logMaxLen = 100;
134 char targetLog[logMaxLen] = {0};
135 const char *fmt = "recv data[%s %s %s %s] from host";
136 snprintf_s(targetLog, logMaxLen, logMaxLen - 1, fmt, idVendor, idPorduct, bcdDevice, configurationValue);
137 const char *cmd = "usb_dev_test -2 GET_DESCRIPTOR";
138 double startTs = GetNowTs();
139 ASSERT_EQ(system(cmd), 0);
140 sleep(1);
141 EXPECT_TRUE(HasLog(string(targetLog), startTs, RLOG_FILE));
142 ASSERT_EQ(system("killall usb_dev_test"), 0) << "ErrInfo: failed to kill usb_dev_test";
143 printf("------end HcsConfigTest_001------\n");
144 }
145
146 /**
147 * @tc.number : H_Lx_D_Sub_usb_Instance_002
148 * @tc.name : 验证HCS中配置的设备描述符解析是否正确
149 * @tc.type : FUNC
150 * @tc.level : Level 2
151 */
HWTEST_F(UsbDeviceSerialFuncTest, HcsConfigTest_002, TestSize.Level2)152 HWTEST_F(UsbDeviceSerialFuncTest, HcsConfigTest_002, TestSize.Level2)
153 {
154 printf("------start HcsConfigTest_002------\n");
155 const char *idVendor = "18d2";
156 const char *idProduct = "4ee8";
157 const char *bDeviceProtocol = "01";
158
159 ASSERT_EQ(system(("PropTest -s idVendor " + string(idVendor)).c_str()), 0);
160 ASSERT_EQ(system(("PropTest -s idProduct " + string(idProduct)).c_str()), 0);
161 ASSERT_EQ(system(("PropTest -s bDeviceProtocol " + string(bDeviceProtocol)).c_str()), 0);
162 ASSERT_EQ(system("PropTest -g idVendor"), 0);
163 ASSERT_EQ(system("PropTest -g idProduct"), 0);
164 ASSERT_EQ(system("PropTest -g bDeviceProtocol"), 0);
165 ASSERT_EQ(system("PropTest -s idVendor 12d1"), 0);
166 ASSERT_EQ(system("PropTest -s idProduct 5000"), 0);
167 ASSERT_EQ(system("PropTest -s bDeviceProtocol 00"), 0);
168 printf("------end HcsConfigTest_002------\n");
169 }
170 }