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