1 /*
2 * Copyright (c) 2022-2024 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 <gtest/gtest.h>
17 #include "securec.h"
18 #include "client_trans_session_callback.h"
19 #include "client_trans_session_manager.h"
20 #include "client_trans_socket_manager.h"
21 #include "client_trans_udp_manager.h"
22 #include "session.h"
23 #include "softbus_def.h"
24 #include "softbus_errcode.h"
25 #include "trans_udp_channel_manager.h"
26
27 using namespace std;
28 using namespace testing::ext;
29
30 const char *g_sessionName = "ohos.distributedschedule.dms.test";
31 const char *g_networkid = "ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00";
32 const char *g_groupid = "TEST_GROUP_ID";
33
34 namespace OHOS {
35 #define TEST_ERR_PID (-1)
36 #define TEST_LEN 10
37 #define TEST_DATA_TYPE 2
38 #define TEST_PID 2
39 #define TEST_STATE 1
40 #define TEST_ERR_CODE 1
41 #define TEST_CHANNELID 5
42 #define TEST_CHANNELTYPE 2
43 #define TEST_REMOTE_TYPE 0
44 #define TEST_EVENT_ID 2
45 #define TEST_COUNT 2
46 #define TEST_ERR_COUNT (-2)
47 #define TEST_ERRCODE 0
48 #define TEST_FILE_NAME "test.filename.01"
49 #define STREAM_DATA_LENGTH 10
50 #define TEST_ERR_CHANNELID (-1)
51 #define FILE_PRIORITY_TEST 0x06
52
53 class ClientTransUdpManagerTest : public testing::Test {
54 public:
ClientTransUdpManagerTest()55 ClientTransUdpManagerTest() {}
~ClientTransUdpManagerTest()56 ~ClientTransUdpManagerTest() {}
57 static void SetUpTestCase(void);
58 static void TearDownTestCase(void);
SetUp()59 void SetUp() override {}
TearDown()60 void TearDown() override {}
61 };
62
OnSessionOpened(const char * sessionName,const ChannelInfo * channel,SessionType flag)63 static int32_t OnSessionOpened(const char *sessionName, const ChannelInfo *channel, SessionType flag)
64 {
65 return SOFTBUS_OK;
66 }
67
OnSessionClosed(int32_t channelId,int32_t channelType,ShutdownReason reason)68 static int32_t OnSessionClosed(int32_t channelId, int32_t channelType, ShutdownReason reason)
69 {
70 return SOFTBUS_OK;
71 }
72
OnSessionOpenFailed(int32_t channelId,int32_t channelType,int32_t errCode)73 static int32_t OnSessionOpenFailed(int32_t channelId, int32_t channelType, int32_t errCode)
74 {
75 return SOFTBUS_OK;
76 }
77
OnDataReceived(int32_t channelId,int32_t channelType,const void * data,uint32_t len,SessionPktType type)78 static int32_t OnDataReceived(int32_t channelId, int32_t channelType, const void *data,
79 uint32_t len, SessionPktType type)
80 {
81 return SOFTBUS_OK;
82 }
83 static IClientSessionCallBack g_sessionCb = {
84 .OnSessionOpened = OnSessionOpened,
85 .OnSessionClosed = OnSessionClosed,
86 .OnSessionOpenFailed = OnSessionOpenFailed,
87 .OnDataReceived = OnDataReceived,
88 };
89
SetUpTestCase(void)90 void ClientTransUdpManagerTest::SetUpTestCase(void)
91 {
92 int ret = ClientTransUdpMgrInit(&g_sessionCb);
93 EXPECT_EQ(SOFTBUS_OK, ret);
94 ret = TransClientInit();
95 EXPECT_EQ(ret, SOFTBUS_OK);
96 }
97
TearDownTestCase(void)98 void ClientTransUdpManagerTest::TearDownTestCase(void) {}
99
InitChannelInfo()100 static ChannelInfo InitChannelInfo()
101 {
102 ChannelInfo channel;
103 char strTmp[] = "ABCDEFG";
104 char strSessionName[] = "ohos.distributedschedule.dms.test";
105 channel.channelId = TEST_CHANNELID;
106 channel.businessType = BUSINESS_TYPE_STREAM;
107 channel.channelType = TEST_CHANNELTYPE;
108 channel.fd = TEST_DATA_TYPE;
109 channel.isServer = true;
110 channel.isEnabled = true;
111 channel.peerUid = TEST_CHANNELID;
112 channel.peerPid = TEST_CHANNELID;
113 channel.groupId = strTmp;
114 channel.sessionKey = strTmp;
115 channel.keyLen = sizeof(channel.sessionKey);
116 channel.peerSessionName = strSessionName;
117 channel.peerDeviceId = strTmp;
118 channel.myIp = strTmp;
119 channel.streamType = TEST_COUNT;
120 channel.isUdpFile = true;
121 channel.peerPort = TEST_COUNT;
122 channel.peerIp = strTmp;
123 channel.routeType = TEST_DATA_TYPE;
124 return channel;
125 }
126
127 /**
128 * @tc.name: TransOnUdpChannelOpenedTest001
129 * @tc.desc: trans on udp channel opened test, use the wrong parameter.
130 * @tc.type: FUNC
131 * @tc.require:
132 */
133 HWTEST_F(ClientTransUdpManagerTest, TransOnUdpChannelOpenedTest001, TestSize.Level0)
134 {
135 int32_t ret;
136 ChannelInfo channel = InitChannelInfo();
137 int32_t udpPort;
138
139 ret = TransOnUdpChannelOpened(NULL, &channel, &udpPort);
140 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
141
142 ret = TransOnUdpChannelOpened(g_sessionName, NULL, &udpPort);
143 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
144
145 ret = TransOnUdpChannelOpened(g_sessionName, &channel, NULL);
146 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
147 }
148
149 /**
150 * @tc.name: TransOnUdpChannelOpenedTest002
151 * @tc.desc: trans on udp channel opened test, use the wrong or normal parameter.
152 * @tc.type: FUNC
153 * @tc.require:
154 */
155 HWTEST_F(ClientTransUdpManagerTest, TransOnUdpChannelOpenedTest002, TestSize.Level0)
156 {
157 int32_t ret;
158 ChannelInfo channel = InitChannelInfo();
159 int32_t udpPort;
160 char strSessionName[] = "ohos.distributedschedule.dms.test";
161
162 ret = TransOnUdpChannelOpened(g_sessionName, &channel, &udpPort);
163 EXPECT_EQ(SOFTBUS_NO_INIT, ret);
164
165 ret = TransOnUdpChannelClosed(channel.channelId, SHUTDOWN_REASON_UNKNOWN);
166 EXPECT_EQ(SOFTBUS_TRANS_UDP_GET_CHANNEL_FAILED, ret);
167
168 channel.businessType = BUSINESS_TYPE_FILE;
169 ret = TransOnUdpChannelOpened(strSessionName, &channel, &udpPort);
170 EXPECT_EQ(SOFTBUS_TRANS_NODE_NOT_FOUND, ret);
171
172 channel.businessType = BUSINESS_TYPE_FILE;
173 channel.channelId = TEST_CHANNELID + 1;
174 ret = TransOnUdpChannelOpened(g_sessionName, &channel, &udpPort);
175 EXPECT_EQ(SOFTBUS_TRANS_NODE_NOT_FOUND, ret);
176
177 channel.businessType = TEST_COUNT;
178 ret = TransOnUdpChannelOpened(g_sessionName, &channel, &udpPort);
179 EXPECT_EQ(SOFTBUS_TRANS_BUSINESS_TYPE_NOT_MATCH, ret);
180 }
181
182 /**
183 * @tc.name: TransOnUdpChannelOpenedTest003
184 * @tc.desc: trans on udp channel opened test, use the wrong or normal parameter.
185 * @tc.type: FUNC
186 * @tc.require:
187 */
188 HWTEST_F(ClientTransUdpManagerTest, TransOnUdpChannelOpenedTest003, TestSize.Level0)
189 {
190 int32_t ret;
191 ChannelInfo channel = InitChannelInfo();
192 int32_t udpPort;
193 QosTv tvList;
194
195 ret = TransOnUdpChannelOpened(g_sessionName, &channel, &udpPort);
196 EXPECT_EQ(SOFTBUS_NO_INIT, ret);
197
198 ret = TransOnUdpChannelQosEvent(TEST_CHANNELID, TEST_EVENT_ID, TEST_COUNT, &tvList);
199 EXPECT_EQ(SOFTBUS_TRANS_UDP_GET_CHANNEL_FAILED, ret);
200
201 channel.businessType = BUSINESS_TYPE_BUTT;
202 ret = TransOnUdpChannelOpenFailed(TEST_CHANNELID, TEST_ERRCODE);
203 EXPECT_EQ(SOFTBUS_OK, ret);
204 }
205
206 /**
207 * @tc.name: TransOnUdpChannelOpenFailedTest001
208 * @tc.desc: trans on udp channel opened test, use the wrong or normal parameter.
209 * @tc.type: FUNC
210 * @tc.require:
211 */
212 HWTEST_F(ClientTransUdpManagerTest, TransOnUdpChannelOpenFailedTest001, TestSize.Level0)
213 {
214 int32_t ret;
215 ret = TransOnUdpChannelOpenFailed(TEST_CHANNELID, TEST_ERRCODE);
216 EXPECT_EQ(SOFTBUS_OK, ret);
217
218 ret = TransOnUdpChannelOpenFailed(0, TEST_ERRCODE);
219 EXPECT_EQ(SOFTBUS_OK, ret);
220 }
221
222 /**
223 * @tc.name: TransOnUdpChannelClosedTest001
224 * @tc.desc: trans on udp channel closed test, use the wrong or normal parameter.
225 * @tc.type: FUNC
226 * @tc.require:
227 */
228 HWTEST_F(ClientTransUdpManagerTest, TransOnUdpChannelClosedTest001, TestSize.Level0)
229 {
230 int32_t ret;
231 ret = TransOnUdpChannelClosed(TEST_CHANNELID, SHUTDOWN_REASON_UNKNOWN);
232 EXPECT_EQ(SOFTBUS_TRANS_UDP_GET_CHANNEL_FAILED, ret);
233 }
234
235 /**
236 * @tc.name: TransOnUdpChannelClosedTest002
237 * @tc.desc: trans on udp channel closed test, use the wrong or normal parameter.
238 * @tc.type: FUNC
239 * @tc.require:
240 */
241 HWTEST_F(ClientTransUdpManagerTest, TransOnUdpChannelClosedTest002, TestSize.Level0)
242 {
243 int32_t ret;
244 ChannelInfo channel = InitChannelInfo();
245 int32_t udpPort;
246 channel.businessType = BUSINESS_TYPE_FILE;
247
248 ret = TransOnUdpChannelOpened(g_sessionName, &channel, &udpPort);
249 EXPECT_EQ(SOFTBUS_TRANS_NODE_NOT_FOUND, ret);
250
251 ret = ClientTransUdpMgrInit(&g_sessionCb);
252 EXPECT_EQ(SOFTBUS_OK, ret);
253
254 ret = TransOnUdpChannelOpened(g_sessionName, &channel, &udpPort);
255 EXPECT_EQ(SOFTBUS_TRANS_NODE_NOT_FOUND, ret);
256
257 ret = TransOnUdpChannelClosed(channel.channelId, SHUTDOWN_REASON_UNKNOWN);
258 EXPECT_EQ(SOFTBUS_TRANS_UDP_GET_CHANNEL_FAILED, ret);
259
260 ret = TransOnUdpChannelClosed(channel.channelId, SHUTDOWN_REASON_PEER);
261 EXPECT_EQ(SOFTBUS_TRANS_UDP_GET_CHANNEL_FAILED, ret);
262
263 ret = TransOnUdpChannelClosed(channel.channelId, SHUTDOWN_REASON_SEND_FILE_ERR);
264 EXPECT_EQ(SOFTBUS_TRANS_UDP_GET_CHANNEL_FAILED, ret);
265
266 ret = TransOnUdpChannelClosed(TEST_CHANNELID + TEST_CHANNELID, SHUTDOWN_REASON_SEND_FILE_ERR);
267 EXPECT_EQ(SOFTBUS_TRANS_UDP_GET_CHANNEL_FAILED, ret);
268 }
269
270 /**
271 * @tc.name: TransOnUdpChannelQosEventTest001
272 * @tc.desc: trans on udp channel qos event test, use the wrong or normal parameter.
273 * @tc.type: FUNC
274 * @tc.require:
275 */
276 HWTEST_F(ClientTransUdpManagerTest, TransOnUdpChannelQosEventTest001, TestSize.Level0)
277 {
278 int32_t ret;
279 QosTv tvList;
280 ret = TransOnUdpChannelQosEvent(TEST_CHANNELID, TEST_EVENT_ID, TEST_COUNT, &tvList);
281 EXPECT_EQ(SOFTBUS_TRANS_UDP_GET_CHANNEL_FAILED, ret);
282 }
283
284 /**
285 * @tc.name: ClientTransCloseUdpChannelTest001
286 * @tc.desc: client trans close udp channel test, use the wrong or normal parameter.
287 * @tc.type: FUNC
288 * @tc.require:
289 */
290 HWTEST_F(ClientTransUdpManagerTest, ClientTransCloseUdpChannelTest001, TestSize.Level0)
291 {
292 int32_t ret;
293 ret = ClientTransCloseUdpChannel(TEST_CHANNELID, SHUTDOWN_REASON_UNKNOWN);
294 EXPECT_EQ(SOFTBUS_TRANS_UDP_GET_CHANNEL_FAILED, ret);
295 }
296
297 /**
298 * @tc.name: TransUdpChannelSendStreamTest001
299 * @tc.desc: trans udp channel send stream test, use the wrong or normal parameter.
300 * @tc.type: FUNC
301 * @tc.require:
302 */
303 HWTEST_F(ClientTransUdpManagerTest, TransUdpChannelSendStreamTest001, TestSize.Level0)
304 {
305 int32_t ret;
306 ChannelInfo channel = InitChannelInfo();
307 int32_t udpPort;
308
309 char sendStringData[STREAM_DATA_LENGTH] = "diudiudiu";
310 StreamData tmpData = {
311 sendStringData,
312 STREAM_DATA_LENGTH,
313 };
314 char str[STREAM_DATA_LENGTH] = "oohoohooh";
315 StreamData tmpData2 = {
316 str,
317 STREAM_DATA_LENGTH,
318 };
319
320 StreamFrameInfo tmpf = {};
321 ret = TransUdpChannelSendStream(TEST_CHANNELID, &tmpData, &tmpData2, &tmpf);
322 EXPECT_EQ(SOFTBUS_TRANS_UDP_GET_CHANNEL_FAILED, ret);
323
324 ret = TransOnUdpChannelOpened(g_sessionName, &channel, &udpPort);
325 EXPECT_EQ(SOFTBUS_NO_INIT, ret);
326
327 ret = TransUdpChannelSendStream(TEST_CHANNELID, &tmpData, &tmpData2, &tmpf);
328 EXPECT_EQ(SOFTBUS_TRANS_UDP_GET_CHANNEL_FAILED, ret);
329 }
330
331 /**
332 * @tc.name: TransUdpChannelSendFileTest001
333 * @tc.desc: trans udp channel send file test, use the wrong or normal parameter.
334 * @tc.type: FUNC
335 * @tc.require:
336 */
337 HWTEST_F(ClientTransUdpManagerTest, TransUdpChannelSendFileTest001, TestSize.Level0)
338 {
339 int32_t ret;
340 const char *sFileList[] = {
341 "/data/big.tar",
342 "/data/richu.jpg",
343 "/data/richu-002.jpg",
344 "/data/richu-003.jpg",
345 };
346 ret = TransUdpChannelSendFile(TEST_CHANNELID, NULL, NULL, 1);
347 EXPECT_EQ(SOFTBUS_TRANS_UDP_GET_CHANNEL_FAILED, ret);
348
349 ret = TransUdpChannelSendFile(TEST_CHANNELID, sFileList, NULL, 0);
350 EXPECT_EQ(SOFTBUS_TRANS_UDP_GET_CHANNEL_FAILED, ret);
351
352 ret = TransUdpChannelSendFile(TEST_CHANNELID, sFileList, NULL, 1);
353 EXPECT_EQ(SOFTBUS_TRANS_UDP_GET_CHANNEL_FAILED, ret);
354 }
355
356 /**
357 * @tc.name: TransGetUdpChannelByFileIdTest001
358 * @tc.desc: trans get udp channel by fileid test, use the wrong or normal parameter.
359 * @tc.type: FUNC
360 * @tc.require:
361 */
362 HWTEST_F(ClientTransUdpManagerTest, TransGetUdpChannelByFileIdTest001, TestSize.Level0)
363 {
364 int32_t ret;
365 UdpChannel udpChannel;
366 (void)memset_s(&udpChannel, sizeof(UdpChannel), 0, sizeof(UdpChannel));
367 ret = TransGetUdpChannelByFileId(TEST_DATA_TYPE, &udpChannel);
368 EXPECT_EQ(SOFTBUS_TRANS_UDP_CHANNEL_NOT_FOUND, ret);
369
370 ClientTransUdpMgrDeinit();
371 ret = TransGetUdpChannelByFileId(TEST_DATA_TYPE, &udpChannel);
372 EXPECT_EQ(SOFTBUS_NO_INIT, ret);
373 }
374
375 /**
376 * @tc.name: ClientTransAddUdpChannelTest001
377 * @tc.desc: client trans add udp channel test, use the wrong or normal parameter.
378 * @tc.type: FUNC
379 * @tc.require:
380 */
381 HWTEST_F(ClientTransUdpManagerTest, ClientTransAddUdpChannelTest001, TestSize.Level0)
382 {
383 int32_t ret;
384 ChannelInfo channel = InitChannelInfo();
385 int32_t udpPort;
386
387 ret = TransOnUdpChannelOpened(g_sessionName, &channel, &udpPort);
388 EXPECT_EQ(SOFTBUS_TRANS_UDP_CLIENT_ADD_CHANNEL_FAILED, ret);
389
390 ret = TransOnUdpChannelOpened(g_sessionName, &channel, &udpPort);
391 EXPECT_EQ(SOFTBUS_TRANS_UDP_CLIENT_ADD_CHANNEL_FAILED, ret);
392
393 ClientTransUdpMgrDeinit();
394 ret = TransOnUdpChannelOpened(g_sessionName, &channel, &udpPort);
395 EXPECT_EQ(SOFTBUS_TRANS_UDP_CLIENT_ADD_CHANNEL_FAILED, ret);
396 }
397
398 /**
399 * @tc.name: ClientTransUdpManagerTest001
400 * @tc.desc: client trans udp manager test, use the normal parameter.
401 * @tc.type: FUNC
402 * @tc.require:
403 */
404 HWTEST_F(ClientTransUdpManagerTest, ClientTransUdpManagerTest001, TestSize.Level0)
405 {
406 int32_t ret;
407
408 IClientSessionCallBack *cb = GetClientSessionCb();
409 ret = ClientTransUdpMgrInit(cb);
410 EXPECT_EQ(ret, SOFTBUS_OK);
411 }
412
413 /**
414 * @tc.name: ClientEmitFileEventTest001
415 * @tc.desc: client emit file event test, use the invalid parameter.
416 * @tc.type: FUNC
417 * @tc.require:
418 */
419 HWTEST_F(ClientTransUdpManagerTest, ClientEmitFileEventTest001, TestSize.Level0)
420 {
421 int32_t channelId = TEST_ERR_CHANNELID;
422 int32_t ret = ClientEmitFileEvent(channelId);
423 EXPECT_NE(ret, SOFTBUS_OK);
424 }
425
426 /**
427 * @tc.name: ClientEmitFileEventTest002
428 * @tc.desc: client emit file event test, use the invalid parameter.
429 * @tc.type: FUNC
430 * @tc.require:
431 */
432 HWTEST_F(ClientTransUdpManagerTest, ClientEmitFileEventTest002, TestSize.Level0)
433 {
434 int32_t channelId = TEST_CHANNELID;
435 int32_t ret = ClientEmitFileEvent(channelId);
436 EXPECT_NE(ret, SOFTBUS_OK);
437 }
438
439 /**
440 * @tc.name: TransLimitChangeTest
441 * @tc.desc: trans limit change test, use the invalid parameter.
442 * @tc.type: FUNC
443 * @tc.require:
444 */
445 HWTEST_F(ClientTransUdpManagerTest, TransLimitChangeTest, TestSize.Level0)
446 {
447 int32_t channelId = TEST_ERR_CHANNELID;
448 int32_t ret = TransLimitChange(channelId, FILE_PRIORITY_BK);
449 EXPECT_EQ(ret, SOFTBUS_TRANS_UDP_CHANNEL_NOT_FOUND);
450
451 channelId = TEST_CHANNELID;
452 ret = TransLimitChange(channelId, FILE_PRIORITY_BE);
453 EXPECT_EQ(ret, SOFTBUS_TRANS_UDP_CHANNEL_NOT_FOUND);
454
455 ret = TransLimitChange(channelId, FILE_PRIORITY_TEST);
456 EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
457 }
458 } // namespace OHOS