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