1 /*
2 * Copyright (c) 2022 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 <string>
17 #include <gtest/gtest.h>
18
19 #include "softbus_error_code.h"
20 #include "legacy/softbus_hisysevt_transreporter.h"
21 #include "softbus_hidumper_trans.c"
22
23 using namespace std;
24 using namespace testing::ext;
25
26 static const TransDumpLaneLinkType g_errLinkType = (TransDumpLaneLinkType)(-1);
27 static const int32_t TEST_TRANS_UID = 1;
28 static const int32_t TEST_TRANS_PID = 1;
29 static const int32_t ERR_TRANS_UID = 1;
30 static const int32_t ERR_TRANS_PID = 1;
31 static const int32_t TEST_TRANS_ARGV_MAX_NUM = 10;
32 static const int32_t TRANS_DUMP_PROCESS_TEST_NUM = 4;
33 static const int32_t ERR_FD = -1;
34 static const int32_t TEST_FD = 0;
35 static const int32_t ERR_ARGC = -1;
36 static const int32_t TEST_ARGC_ONE = 1;
37 static const int32_t TEST_ARGC_TWO = 2;
38 static const char *g_testSessionName = "testSessionName";
39 static const char *g_testPkgName = "testPkg";
40 static const char *g_testMsg = "test";
41
42 namespace OHOS {
43 class TransDfxTest : public testing::Test {
44 public:
45 static void SetUpTestCase();
46 static void TearDownTestCase();
47 void SetUp();
48 void TearDown();
49 };
50
SetUpTestCase(void)51 void TransDfxTest::SetUpTestCase(void) {}
52
TearDownTestCase(void)53 void TransDfxTest::TearDownTestCase(void) {}
54
SetUp(void)55 void TransDfxTest::SetUp(void) {}
56
TearDown(void)57 void TransDfxTest::TearDown(void) {}
58
59 typedef struct {
60 int32_t fd;
61 int32_t argc;
62 const char* argv[TEST_TRANS_ARGV_MAX_NUM];
63 } TransArgvNode;
64
65 static AppInfo g_testAppInfo = {
66 .groupId = "1234d575ad685798989adfe358aec45",
67 .sessionKey = "867f996ae4567df",
68 .reqId = "123456",
69 .peerNetWorkId = "1456df44568e758b545aad45875e",
70 .routeType = WIFI_STA,
71 .businessType = BUSINESS_TYPE_MESSAGE,
72 .streamType = COMMON_VIDEO_STREAM,
73 .udpConnType = UDP_CONN_TYPE_P2P,
74 .udpChannelOptType = TYPE_UDP_CHANNEL_CLOSE,
75 .fd = 2,
76 .appType = APP_TYPE_NORMAL,
77 .protocol = 1,
78 .encrypt = 1,
79 .algorithm = 2,
80 .crc = 101,
81 .fileProtocol = 1
82 };
83
TransRegisterSessionTest1(int32_t fd)84 static int32_t TransRegisterSessionTest1(int32_t fd)
85 {
86 SoftBusTransDumpRegisterSession(fd, g_testPkgName, g_testSessionName, TEST_TRANS_UID, TEST_TRANS_PID);
87 return SOFTBUS_OK;
88 }
TransRunningSessionInfo1(int32_t fd)89 static int32_t TransRunningSessionInfo1(int32_t fd)
90 {
91 SoftBusTransDumpRunningSession(fd, DUMPER_LANE_BR, &g_testAppInfo);
92 return SOFTBUS_OK;
93 }
94
TransRegisterSessionTest2(int32_t fd)95 static int32_t TransRegisterSessionTest2(int32_t fd)
96 {
97 SoftBusTransDumpRegisterSession(fd, nullptr, g_testSessionName, TEST_TRANS_UID, TEST_TRANS_PID);
98 return SOFTBUS_OK;
99 }
100
101
TransRunningSessionInfo2(int32_t fd)102 static int32_t TransRunningSessionInfo2(int32_t fd)
103 {
104 SoftBusTransDumpRunningSession(fd, DUMPER_LANE_BR, nullptr);
105 return SOFTBUS_OK;
106 }
107
108 TransArgvNode g_validTransCmdArray[TRANS_DUMP_PROCESS_TEST_NUM] = {
109 {TEST_FD, TEST_ARGC_ONE, {"-h"}},
110 {TEST_FD, TEST_ARGC_ONE, {"-l"}},
111 {TEST_FD, TEST_ARGC_TWO, {"-l", "registed_sessionlist"}},
112 {TEST_FD, TEST_ARGC_TWO, {"-l", "concurrent_sessionlist"}},
113 };
114
115 /**
116 * @tc.name: SoftbusReportTransInfoEvt001
117 * @tc.desc: Verify SoftbusReportTransInfoEvt function.
118 * @tc.type: FUNC
119 * @tc.require: I5NJEO
120 */
121 HWTEST_F(TransDfxTest, SoftbusReportTransInfoEvt001, TestSize.Level0)
122 {
123 SoftbusReportTransInfoEvt(g_testMsg);
124 EXPECT_EQ("test", g_testMsg);
125 SoftbusReportTransInfoEvt(nullptr);
126 }
127
128 /**
129 * @tc.name: SoftBusTransDumpHandler_001
130 * @tc.desc: Verify SoftBusTransDumpHandler function, use the normal parameter.
131 * @tc.type: FUNC
132 * @tc.require:
133 */
134 HWTEST_F(TransDfxTest, SoftBusTransDumpHandler_001, TestSize.Level1)
135 {
136 int32_t fd = 1;
137 int32_t argc = 1;
138 const char* argv = "aaa";
139 int32_t ret = SoftBusTransDumpHandler(fd, argc, &argv);
140 EXPECT_EQ(SOFTBUS_OK, ret);
141 }
142
143 /**
144 * @tc.name: SoftBusTransDumpHandler002
145 * @tc.desc: Verify SoftBusTransDumpHandler function, use valid param, return SOFTBUS_OK.
146 * @tc.type: FUNC
147 * @tc.require:
148 */
149 HWTEST_F(TransDfxTest, SoftBusTransDumpHandler002, TestSize.Level0)
150 {
151 int32_t ret = SoftBusRegTransVarDump("registed_sessionlist", TransRegisterSessionTest1);
152 EXPECT_EQ(SOFTBUS_OK, ret);
153 ret = SoftBusRegTransVarDump("concurrent_sessionlist", TransRunningSessionInfo1);
154 EXPECT_EQ(SOFTBUS_OK, ret);
155
156 for (int32_t i = 0; i < TRANS_DUMP_PROCESS_TEST_NUM; i++) {
157 ret = SoftBusTransDumpHandler(g_validTransCmdArray[i].fd, g_validTransCmdArray[i].argc,
158 g_validTransCmdArray[i].argv);
159 EXPECT_EQ(SOFTBUS_OK, ret);
160 }
161 SoftBusHiDumperTransDeInit();
162 }
163
164 /**
165 * @tc.name: SoftBusTransDumpHandler003
166 * @tc.desc: Verify SoftBusTransDumpHandler function, use hidumperHandler use valid param, return SOFTBUS_OK,
167 * but the TransRegisterSessionTest2() and TransRunningSessionInfo2 use invalid param, test error switch
168 * @tc.type: FUNC
169 * @tc.require:
170 */
171 HWTEST_F(TransDfxTest, SoftBusTransDumpHandler003, TestSize.Level0)
172 {
173 int32_t ret = SoftBusRegTransVarDump("registed_sessionlist", TransRegisterSessionTest2);
174 EXPECT_EQ(SOFTBUS_OK, ret);
175 ret = SoftBusRegTransVarDump("concurrent_sessionlist", TransRunningSessionInfo2);
176 EXPECT_EQ(SOFTBUS_OK, ret);
177 for (int32_t i = 0; i < TRANS_DUMP_PROCESS_TEST_NUM; i++) {
178 ret = SoftBusTransDumpHandler(g_validTransCmdArray[i].fd, g_validTransCmdArray[i].argc,
179 g_validTransCmdArray[i].argv);
180 EXPECT_EQ(SOFTBUS_OK, ret);
181 }
182 SoftBusHiDumperTransDeInit();
183 }
184
185 /**
186 * @tc.name: SoftBusTransDumpHandler004
187 * @tc.desc: Verify SoftBusTransDumpHandler function, use hidumperHandler use valid param, return SOFTBUS_OK,
188 * but the TransRegisterSessionTest2() and TransRunningSessionInfo2 use invalid param, test error switch
189 * @tc.type: FUNC
190 * @tc.require:
191 */
192 HWTEST_F(TransDfxTest, SoftBusTransDumpHandler004, TestSize.Level0)
193 {
194 TransArgvNode testInValidTransCmdArray[TRANS_DUMP_PROCESS_TEST_NUM] = {
195 {ERR_FD, TEST_ARGC_ONE, {"-h"}},
196 {TEST_FD, ERR_ARGC, {"-l"}},
197 {ERR_FD, TEST_ARGC_ONE, {"-l", "registed_sessionlist"}},
198 {ERR_FD, TEST_ARGC_TWO, {"-l", "concurrent_sessionlist"}},
199 };
200 int32_t ret;
201 for (int32_t i = 0; i < TRANS_DUMP_PROCESS_TEST_NUM; i++) {
202 ret = SoftBusTransDumpHandler(testInValidTransCmdArray[i].fd, testInValidTransCmdArray[i].argc,
203 testInValidTransCmdArray[i].argv);
204 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
205 }
206 ret = SoftBusTransDumpHandler(TEST_FD, TEST_ARGC_ONE, nullptr);
207 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
208 }
209
210 /**
211 * @tc.name: SoftBusRegTransVarDump001
212 * @tc.desc: Verify SoftBusRegTransVarDump function, use hidumperHandler use valid param, return SOFTBUS_INVALID_PARAM.
213 * @tc.type: FUNC
214 * @tc.require:
215 */
216 HWTEST_F(TransDfxTest, SoftBusRegTransVarDump001, TestSize.Level0)
217 {
218 int32_t ret = SOFTBUS_OK;
219 ret = SoftBusRegTransVarDump("registed_sessionlist", nullptr);
220 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
221 ret = SoftBusRegTransVarDump(nullptr, TransRegisterSessionTest1);
222 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
223
224 ret = SoftBusRegTransVarDump("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", TransRegisterSessionTest1);
225 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
226 }
227
228 /**
229 * @tc.name: SoftBusTransDumpRegisterSession001
230 * @tc.desc: Verify SoftBusTransDumpRegisterSession function, use hidumperHandler use valid param, return
231 * SOFTBUS_INVALID_PARAM.
232 * @tc.type: FUNC
233 * @tc.require:
234 */
235 HWTEST_F(TransDfxTest, SoftBusTransDumpRegisterSession001, TestSize.Level0)
236 {
237 SoftBusTransDumpRegisterSession(ERR_FD, g_testPkgName, g_testSessionName, TEST_TRANS_UID, TEST_TRANS_PID);
238 SoftBusTransDumpRunningSession(ERR_FD, DUMPER_LANE_BR, &g_testAppInfo);
239
240 int32_t ret = SoftBusTransDumpHandler(TEST_FD, TEST_ARGC_ONE, nullptr);
241 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
242
243 SoftBusTransDumpRegisterSession(TEST_FD, nullptr, g_testSessionName, TEST_TRANS_UID, TEST_TRANS_PID);
244 SoftBusTransDumpRunningSession(TEST_FD, g_errLinkType, &g_testAppInfo);
245
246 ret = SoftBusTransDumpHandler(TEST_FD, TEST_ARGC_ONE, nullptr);
247 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
248
249 SoftBusTransDumpRegisterSession(TEST_FD, g_testPkgName, nullptr, TEST_TRANS_UID, TEST_TRANS_PID);
250 SoftBusTransDumpRunningSession(TEST_FD, DUMPER_LANE_LINK_TYPE_BUTT, &g_testAppInfo);
251
252 ret = SoftBusTransDumpHandler(TEST_FD, TEST_ARGC_ONE, nullptr);
253 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
254
255 SoftBusTransDumpRegisterSession(TEST_FD, g_testPkgName, g_testSessionName, ERR_TRANS_UID, TEST_TRANS_PID);
256 SoftBusTransDumpRunningSession(TEST_FD, DUMPER_LANE_WLAN, nullptr);
257
258 ret = SoftBusTransDumpHandler(TEST_FD, TEST_ARGC_ONE, nullptr);
259 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
260
261 SoftBusTransDumpRegisterSession(TEST_FD, g_testPkgName, g_testSessionName, TEST_TRANS_UID, ERR_TRANS_PID);
262 SoftBusTransDumpRunningSession(TEST_FD, DUMPER_LANE_WLAN, &g_testAppInfo);
263
264 ret = SoftBusTransDumpHandler(TEST_FD, TEST_ARGC_ONE, nullptr);
265 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
266
267 SoftBusTransDumpRegisterSession(TEST_FD, g_testPkgName, g_testSessionName, TEST_TRANS_UID, TEST_TRANS_PID);
268 SoftBusTransDumpRunningSession(TEST_FD, DUMPER_LANE_WLAN, &g_testAppInfo);
269
270 const char* tmpArgv = "aaa";
271 ret = SoftBusTransDumpHandler(TEST_FD, TEST_ARGC_ONE, &tmpArgv);
272 EXPECT_EQ(SOFTBUS_OK, ret);
273 }
274 } // namespace OHOS
275