1 /*
2 * Copyright (c) 2021-2023 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 <pthread.h>
18 #include <securec.h>
19
20 #include "common_list.h"
21 #include "softbus_base_listener.h"
22 #include "softbus_def.h"
23 #include "softbus_errcode.h"
24 #include "softbus_tcp_socket.h"
25 #include "softbus_utils.h"
26 #include "softbus_conn_manager.h"
27
28 using namespace testing::ext;
29
30 static const int INVALID_FD = -1;
31 static const int TEST_FD = 1;
32 static pthread_mutex_t g_isInitedLock;
33 static int g_count = 0;
34 static int g_port = 6666;
35
36 namespace OHOS {
37 class SoftbusConnCommonTest : public testing::Test {
38 public:
SoftbusConnCommonTest()39 SoftbusConnCommonTest()
40 {}
~SoftbusConnCommonTest()41 ~SoftbusConnCommonTest()
42 {}
43 static void SetUpTestCase(void);
44 static void TearDownTestCase(void);
45 void SetUp();
46 void TearDown();
47 };
48
ThreadPoolTask(void * arg)49 int ThreadPoolTask(void* arg)
50 {
51 pthread_mutex_lock(&g_isInitedLock);
52 g_count++;
53 pthread_mutex_unlock(&g_isInitedLock);
54 return SOFTBUS_OK;
55 }
56
SetUpTestCase(void)57 void SoftbusConnCommonTest::SetUpTestCase(void)
58 {
59 pthread_mutex_init(&g_isInitedLock, nullptr);
60 GTEST_LOG_(INFO) << "SoftbusConnCommonTestSetUp";
61 ConnServerInit();
62 }
63
TearDownTestCase(void)64 void SoftbusConnCommonTest::TearDownTestCase(void)
65 {
66 g_count = 0;
67 g_port++;
68 GTEST_LOG_(INFO) << "+-------------------------------------------+";
69 }
70
SetUp(void)71 void SoftbusConnCommonTest::SetUp(void)
72 {
73 g_count = 0;
74 }
75
TearDown(void)76 void SoftbusConnCommonTest::TearDown(void)
77 {
78 g_count = 0;
79 }
80
ConnectEvent(ListenerModule module,int32_t cfd,const ConnectOption * clientAddr)81 int32_t ConnectEvent(ListenerModule module, int32_t cfd, const ConnectOption *clientAddr)
82 {
83 return 0;
84 }
85
DataEvent(ListenerModule module,int32_t events,int32_t fd)86 int32_t DataEvent(ListenerModule module, int32_t events, int32_t fd)
87 {
88 return 0;
89 }
90
91 SocketAddr g_socketAddr = {
92 .addr = "127.0.0.1",
93 .port = g_port,
94 };
95
96 /*
97 * @tc.name: testBaseListener002
98 * @tc.desc: test GetSoftbusBaseListener and set
99 * @tc.type: FUNC
100 * @tc.require: I5HSOL
101 */
102 HWTEST_F(SoftbusConnCommonTest, testBaseListener002, TestSize.Level1)
103 {
104 int i;
105 int port = 6666;
106 SoftbusBaseListener *setListener = (SoftbusBaseListener *)malloc(sizeof(SoftbusBaseListener));
107 ASSERT_TRUE(setListener != nullptr);
108 setListener->onConnectEvent = ConnectEvent;
109 setListener->onDataEvent = DataEvent;
110 for (i = PROXY; i < LISTENER_MODULE_DYNAMIC_START; i++) {
111 LocalListenerInfo info = {
112 .type = CONNECT_TCP,
113 .socketOption = {
114 .addr = "127.0.0.1",
115 .port = port,
116 .moduleId = static_cast<ListenerModule>(i),
117 .protocol = LNN_PROTOCOL_IP
118 }
119 };
120 EXPECT_EQ(port, StartBaseListener(&info, setListener));
121 ASSERT_EQ(SOFTBUS_OK, StopBaseListener(static_cast<ListenerModule>(i)));
122 ++port;
123 }
124 free(setListener);
125 };
126
127 /*
128 * @tc.name: testBaseListener006
129 * @tc.desc: test Invalid trigger param
130 * @tc.type: FUNC
131 * @tc.require:
132 */
133 HWTEST_F(SoftbusConnCommonTest, testBaseListener006, TestSize.Level1)
134 {
135 int module;
136 int triggerType;
137 int fd = 1;
138 for (triggerType = READ_TRIGGER; triggerType <= RW_TRIGGER; triggerType++) {
139 EXPECT_EQ(SOFTBUS_INVALID_PARAM, AddTrigger(UNUSE_BUTT, fd, static_cast<TriggerType>(triggerType)));
140 EXPECT_EQ(SOFTBUS_INVALID_PARAM, DelTrigger(UNUSE_BUTT, fd, static_cast<TriggerType>(triggerType)));
141 }
142 for (module = PROXY; module < LISTENER_MODULE_DYNAMIC_START; module++) {
143 for (triggerType = READ_TRIGGER; triggerType <= RW_TRIGGER; triggerType++) {
144 EXPECT_EQ(SOFTBUS_INVALID_PARAM, AddTrigger(static_cast<ListenerModule>(module), INVALID_FD,
145 static_cast<TriggerType>(triggerType)));
146 EXPECT_EQ(SOFTBUS_INVALID_PARAM, DelTrigger(static_cast<ListenerModule>(module), INVALID_FD,
147 static_cast<TriggerType>(triggerType)));
148 }
149 }
150 };
151
152 /*
153 * @tc.name: testBaseListener007
154 * @tc.desc: test Not set baselistener
155 * @tc.type: FUNC
156 * @tc.require:
157 */
158 HWTEST_F(SoftbusConnCommonTest, testBaseListener007, TestSize.Level1)
159 {
160 int module;
161 int triggerType;
162 int fd = 1;
163 for (module = PROXY; module < LISTENER_MODULE_DYNAMIC_START; module++) {
164 for (triggerType = READ_TRIGGER; triggerType <= RW_TRIGGER; triggerType++) {
165 EXPECT_EQ(SOFTBUS_ERR, AddTrigger(static_cast<ListenerModule>(module),
166 fd, static_cast<TriggerType>(triggerType)));
167 EXPECT_EQ(SOFTBUS_OK, DelTrigger(static_cast<ListenerModule>(module),
168 fd, static_cast<TriggerType>(triggerType)));
169 }
170 }
171 };
172
173 /*
174 * @tc.name: testBaseListener008
175 * @tc.desc: test add del trigger
176 * @tc.type: FUNC
177 * @tc.require:
178 */
179 HWTEST_F(SoftbusConnCommonTest, testBaseListener008, TestSize.Level1)
180 {
181 int module;
182 int triggerType;
183 int fd = 1;
184 int port = 6666;
185
186 for (module = PROXY; module < LISTENER_MODULE_DYNAMIC_START; module++) {
187 SoftbusBaseListener* listener = (SoftbusBaseListener*)malloc(sizeof(SoftbusBaseListener));
188 ASSERT_TRUE(listener != nullptr);
189 listener->onConnectEvent = ConnectEvent;
190 listener->onDataEvent = DataEvent;
191
192 LocalListenerInfo info = {
193 .type = CONNECT_TCP,
194 .socketOption = {.addr = "127.0.0.1",
195 .port = port,
196 .moduleId = static_cast<ListenerModule>(module),
197 .protocol = LNN_PROTOCOL_IP}
198 };
199 EXPECT_EQ(port, StartBaseListener(&info, listener));
200 for (triggerType = READ_TRIGGER; triggerType <= RW_TRIGGER; triggerType++) {
201 EXPECT_EQ(SOFTBUS_OK, AddTrigger(static_cast<ListenerModule>(module),
202 fd, static_cast<TriggerType>(triggerType)));
203 EXPECT_EQ(SOFTBUS_OK, AddTrigger(static_cast<ListenerModule>(module),
204 fd, static_cast<TriggerType>(triggerType)));
205 EXPECT_EQ(SOFTBUS_OK, DelTrigger(static_cast<ListenerModule>(module),
206 fd, static_cast<TriggerType>(triggerType)));
207 EXPECT_EQ(SOFTBUS_OK, DelTrigger(static_cast<ListenerModule>(module),
208 fd, static_cast<TriggerType>(triggerType)));
209 }
210 EXPECT_EQ(SOFTBUS_OK, StopBaseListener(static_cast<ListenerModule>(module)));
211 free(listener);
212 }
213 };
214
215 /*
216 * @tc.name: testBaseListener016
217 * @tc.desc: Test StartBaseClient invalid input param ListenerModule module.
218 * @tc.in: Test module, Test number, Test Levels.
219 * @tc.out: NonZero
220 * @tc.type: FUNC
221 * @tc.require: The StartBaseClient operates normally.
222 */
223 HWTEST_F(SoftbusConnCommonTest, testBaseListener016, TestSize.Level1)
224 {
225 EXPECT_EQ(SOFTBUS_INVALID_PARAM, StartBaseClient(static_cast<ListenerModule>(PROXY - 1), NULL));
226 EXPECT_EQ(SOFTBUS_INVALID_PARAM,
227 StartBaseClient(static_cast<ListenerModule>(DIRECT_CHANNEL_SERVER_WIFI + 1), NULL));
228 };
229
230 /*
231 * @tc.name: testBaseListener017
232 * @tc.desc: Test StartBaseClient, BaseListener not set, start failed.
233 * @tc.in: Test module, Test number, Test Levels.
234 * @tc.out: NonZero
235 * @tc.type: FUNC
236 * @tc.require: The StartBaseClient operates normally.
237 */
238 HWTEST_F(SoftbusConnCommonTest, testBaseListener017, TestSize.Level1)
239 {
240 SoftbusBaseListener* listener = (SoftbusBaseListener*)malloc(sizeof(SoftbusBaseListener));
241 ASSERT_TRUE(listener != nullptr);
242 listener->onConnectEvent = ConnectEvent;
243 listener->onDataEvent = DataEvent;
244 int i;
245 for (i = PROXY; i < LISTENER_MODULE_DYNAMIC_START; i++) {
246 EXPECT_EQ(SOFTBUS_OK, StartBaseClient(static_cast<ListenerModule>(i), listener));
247 }
248 free(listener);
249 };
250
251 /*
252 * @tc.name: testBaseListener021
253 * @tc.desc: Test StartBaseListener invalid input param const char *ip.
254 * @tc.in: Test module, Test number, Test Levels.
255 * @tc.out: NonZero
256 * @tc.type: FUNC
257 * @tc.require: The StartBaseListener operates normally.
258 */
259 HWTEST_F(SoftbusConnCommonTest, testBaseListener021, TestSize.Level1)
260 {
261 SoftbusBaseListener* listener = (SoftbusBaseListener*)malloc(sizeof(SoftbusBaseListener));
262 ASSERT_TRUE(listener != nullptr);
263 listener->onConnectEvent = ConnectEvent;
264 listener->onDataEvent = DataEvent;
265 LocalListenerInfo info = {
266 .type = CONNECT_TCP,
267 .socketOption = {.addr = "",
268 .port = 666,
269 .moduleId = PROXY,
270 .protocol = LNN_PROTOCOL_IP}
271 };
272 int i;
273 for (i = PROXY; i < LISTENER_MODULE_DYNAMIC_START; i++) {
274 info.socketOption.moduleId = static_cast<ListenerModule>(i);
275 EXPECT_EQ(SOFTBUS_ERR, StartBaseListener(&info, listener));
276 }
277 free(listener);
278 };
279
280 /*
281 * @tc.name: testBaseListener022
282 * @tc.desc: Test StartBaseListener invalid input param int32_t port < 0.
283 * @tc.in: Test module, Test number, Test Levels.
284 * @tc.out: NonZero
285 * @tc.type: FUNC
286 * @tc.require: The StartBaseListener operates normally.
287 */
288 HWTEST_F(SoftbusConnCommonTest, testBaseListener022, TestSize.Level1)
289 {
290 SoftbusBaseListener* listener = (SoftbusBaseListener*)malloc(sizeof(SoftbusBaseListener));
291 ASSERT_TRUE(listener != nullptr);
292 listener->onConnectEvent = ConnectEvent;
293 listener->onDataEvent = DataEvent;
294 LocalListenerInfo info = {
295 .type = CONNECT_TCP,
296 .socketOption = {.addr = "127.0.0.1",
297 .port = -1,
298 .moduleId = PROXY,
299 .protocol = LNN_PROTOCOL_IP}
300 };
301 int i;
302 for (i = PROXY; i <= LISTENER_MODULE_DYNAMIC_START; i++) {
303 info.socketOption.moduleId = static_cast<ListenerModule>(i);
304 EXPECT_EQ(SOFTBUS_INVALID_PARAM, StartBaseListener(&info, listener));
305 }
306 free(listener);
307 };
308
309 /*
310 * @tc.name: testBaseListener026
311 * @tc.desc: Test StopBaseListener invalid input param ListenerModule module.
312 * @tc.in: Test module, Test number, Test Levels.
313 * @tc.out: NonZero
314 * @tc.type: FUNC
315 * @tc.require: The StopBaseListener operates normally.
316 */
317 HWTEST_F(SoftbusConnCommonTest, testBaseListener026, TestSize.Level1)
318 {
319 EXPECT_EQ(SOFTBUS_INVALID_PARAM, StopBaseListener(static_cast<ListenerModule>(PROXY - 1)));
320 EXPECT_EQ(SOFTBUS_INVALID_PARAM, StopBaseListener(static_cast<ListenerModule>(UNUSE_BUTT)));
321 };
322
323 /*
324 * @tc.name: testBaseListener027
325 * @tc.desc: Test StopBaseListener failed g_listenerList[module].info = NULL.
326 * @tc.in: Test module, Test number, Test Levels.
327 * @tc.out: NonZero
328 * @tc.type: FUNC
329 * @tc.require: The StopBaseListener operates normally.
330 */
331 HWTEST_F(SoftbusConnCommonTest, testBaseListener027, TestSize.Level1)
332 {
333 int i;
334 for (i = PROXY; i < LISTENER_MODULE_DYNAMIC_START; i++) {
335 EXPECT_EQ(SOFTBUS_OK, StopBaseListener(static_cast<ListenerModule>(i)));
336 }
337 EXPECT_EQ(SOFTBUS_INVALID_PARAM, StopBaseListener(UNUSE_BUTT));
338 };
339
340 /*
341 * @tc.name: testTcpSocket001
342 * @tc.desc: test OpenTcpServerSocket
343 * @tc.type: FUNC
344 * @tc.require:
345 */
346 HWTEST_F(SoftbusConnCommonTest, testTcpSocket001, TestSize.Level1)
347 {
348 const SocketInterface *tcp = GetTcpProtocol();
349 ASSERT_NE(tcp, nullptr);
350
351 LocalListenerInfo info = {
352 .type = CONNECT_TCP,
353 .socketOption = {
354 .addr = "127.0.0.1",
355 .port = g_port,
356 .moduleId = DIRECT_CHANNEL_SERVER_WIFI,
357 .protocol = LNN_PROTOCOL_IP
358 }
359 };
360
361 int fd = tcp->OpenServerSocket(&info);
362 int ret = (fd <= 0) ? SOFTBUS_ERR : SOFTBUS_OK;
363 ASSERT_TRUE(ret == SOFTBUS_OK);
364 int port = tcp->GetSockPort(fd);
365 EXPECT_EQ(port, g_port);
366 ConnCloseSocket(fd);
367
368 fd = tcp->OpenServerSocket(nullptr);
369 ret = (fd <= 0) ? SOFTBUS_ERR : SOFTBUS_OK;
370 EXPECT_EQ(ret, SOFTBUS_ERR);
371 ConnCloseSocket(fd);
372
373 info.socketOption.port = -1;
374 fd = tcp->OpenServerSocket(&info);
375 ret = (fd <= 0) ? SOFTBUS_ERR : SOFTBUS_OK;
376 EXPECT_EQ(ret, SOFTBUS_ERR);
377 ConnCloseSocket(fd);
378 };
379
380 /*
381 * @tc.name: testTcpSocket002
382 * @tc.desc: test OpenTcpClientSocket
383 * @tc.type: FUNC
384 * @tc.require:
385 */
386 HWTEST_F(SoftbusConnCommonTest, testTcpSocket002, TestSize.Level1)
387 {
388 const SocketInterface *tcp = GetTcpProtocol();
389 ASSERT_NE(tcp, nullptr);
390
391 ConnectOption option = {
392 .type = CONNECT_TCP,
393 .socketOption = {
394 .addr = "127.0.0.1",
395 .port = g_port,
396 .moduleId = DIRECT_CHANNEL_SERVER_WIFI,
397 .protocol = LNN_PROTOCOL_IP
398 }
399 };
400
401 int fd = tcp->OpenClientSocket(nullptr, "127.0.0.1", false);
402 int ret = (fd < 0) ? SOFTBUS_ERR : SOFTBUS_OK;
403 EXPECT_EQ(ret, SOFTBUS_ERR);
404 ConnCloseSocket(fd);
405 fd = tcp->OpenClientSocket(nullptr, nullptr, false);
406 ret = (fd < 0) ? SOFTBUS_ERR : SOFTBUS_OK;
407 EXPECT_EQ(ret, SOFTBUS_ERR);
408 ConnCloseSocket(fd);
409
410 option.socketOption.port = -1;
411 fd = tcp->OpenClientSocket(&option, "127.0.0.1", false);
412 ret = (fd < 0) ? SOFTBUS_ERR : SOFTBUS_OK;
413 EXPECT_EQ(ret, SOFTBUS_ERR);
414 ConnCloseSocket(fd);
415 };
416
417 /*
418 * @tc.name: testBaseListener003
419 * @tc.desc: test GetTcpSockPort invalid fd
420 * @tc.type: FUNC
421 * @tc.require:
422 */
423 HWTEST_F(SoftbusConnCommonTest, testTcpSocket003, TestSize.Level1)
424 {
425 const SocketInterface *tcp = GetTcpProtocol();
426 ASSERT_NE(tcp, nullptr);
427 int invalidFd = 1;
428 int port = tcp->GetSockPort(invalidFd);
429 int ret = (port <= 0) ? SOFTBUS_ERR : SOFTBUS_OK;
430 EXPECT_EQ(ret, SOFTBUS_ERR);
431 };
432
433 /*
434 * @tc.name: testTcpSocket004
435 * @tc.desc: test ConnSendSocketData invalid fd
436 * @tc.type: FUNC
437 * @tc.require:
438 */
439 HWTEST_F(SoftbusConnCommonTest, testTcpSocket004, TestSize.Level1)
440 {
441 const SocketInterface *tcp = GetTcpProtocol();
442 ASSERT_NE(tcp, nullptr);
443
444 int clientFd = tcp->OpenClientSocket(nullptr, "127.5.0.1", false);
445 int ret = (clientFd < 0) ? SOFTBUS_ERR : SOFTBUS_OK;
446 EXPECT_EQ(ret, SOFTBUS_ERR);
447 ssize_t bytes = ConnSendSocketData(clientFd, "Hello world", 11, 0);
448 EXPECT_EQ(bytes, -1);
449 ConnShutdownSocket(clientFd);
450 };
451
452 /*
453 * @tc.name: testSocket001
454 * @tc.desc: test ConnGetLocalSocketPort port
455 * @tc.type: FUNC
456 * @tc.require: I5PC1B
457 */
458 HWTEST_F(SoftbusConnCommonTest, testSocket001, TestSize.Level1)
459 {
460 int ret;
461 ret = ConnGetLocalSocketPort(INVALID_FD);
462 EXPECT_EQ(SOFTBUS_ADAPTER_ERR, ret);
463
464 ret = ConnGetLocalSocketPort(TEST_FD);
465 EXPECT_TRUE(ret < 0);
466 };
467
468 /*
469 * @tc.name: testSocket002
470 * @tc.desc: test ConnGetPeerSocketAddr param is invalid
471 * @tc.type: FUNC
472 * @tc.require: I5PC1B
473 */
474 HWTEST_F(SoftbusConnCommonTest, testSocket002, TestSize.Level1)
475 {
476 int ret;
477 ret = ConnGetPeerSocketAddr(INVALID_FD, &g_socketAddr);
478 EXPECT_EQ(SOFTBUS_ERR, ret);
479
480 ret = ConnGetPeerSocketAddr(TEST_FD, NULL);
481 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
482
483 ret = ConnGetPeerSocketAddr(TEST_FD, &g_socketAddr);
484 EXPECT_EQ(SOFTBUS_ERR, ret);
485 };
486
487 /*
488 * @tc.name: testConnSetTcpUserTimeOut001
489 * @tc.desc: Test ConnSetTcpUserTimeOut param is invalid
490 * @tc.in: test module, test number,test levels.
491 * @tc.out: Zero
492 * @tc.type: FUNC
493 * @tc.require: The ThreadPoolDestroy operates normally.
494 */
495 HWTEST_F(SoftbusConnCommonTest, testConnSetTcpUserTimeOut001, TestSize.Level1)
496 {
497 int32_t fd = -1;
498 uint32_t millSec= 1;
499 int ret = ConnSetTcpUserTimeOut(fd, millSec);
500 EXPECT_NE(SOFTBUS_OK, ret);
501 }
502
503 /*
504 * @tc.name: testConnSetTcpUserTimeOut002
505 * @tc.desc: Test ConnSetTcpUserTimeOut param is invalid
506 * @tc.in: test module, test number,test levels.
507 * @tc.out: Zero
508 * @tc.type: FUNC
509 * @tc.require: The ThreadPoolDestroy operates normally.
510 */
511 HWTEST_F(SoftbusConnCommonTest, testConnSetTcpUserTimeOut002, TestSize.Level1)
512 {
513 int32_t fd = 1;
514 uint32_t millSec= 321;
515 int ret = ConnSetTcpUserTimeOut(fd, millSec);
516 EXPECT_NE(SOFTBUS_OK, ret);
517 }
518
519 /*
520 * @tc.name: testSocket003
521 * @tc.desc: test ConnGetPeerSocketAddr param is invalid
522 * @tc.type: FUNC
523 * @tc.require:
524 */
525 HWTEST_F(SoftbusConnCommonTest, testSocket003, TestSize.Level1)
526 {
527 int ret;
528 SocketAddr socketAddr;
529 ret = ConnGetPeerSocketAddr(INVALID_FD, &socketAddr);
530 EXPECT_NE(SOFTBUS_OK, ret);
531 }
532
533 /*
534 * @tc.name: testSocket004
535 * @tc.desc: test ConnGetLocalSocketPort port
536 * @tc.type: FUNC
537 * @tc.require:
538 */
539 HWTEST_F(SoftbusConnCommonTest, testSocket004, TestSize.Level1)
540 {
541 int ret;
542 ret = ConnGetLocalSocketPort(INVALID_FD);
543 EXPECT_NE(SOFTBUS_OK, ret);
544 }
545 }
546