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 <cstdio>
17 #include <cstdlib>
18 #include <cstring>
19 #include <gtest/gtest.h>
20 #include <securec.h>
21 
22 #include "common_list.h"
23 #include "softbus_conn_interface.h"
24 #include "softbus_conn_manager.h"
25 #include "softbus_def.h"
26 #include "softbus_errcode.h"
27 #include "softbus_feature_config.h"
28 #include "message_handler.h"
29 
30 static const uint32_t CONN_HEAD_SIZE = 24;
31 static const uint32_t SHIFT_BITS = 16;
32 
33 static ConnectCallback *g_mangerCb = 0;
34 static ConnectionInfo g_connInfo = {0};
35 static unsigned int g_connId = 0;
36 
37 using namespace testing::ext;
38 
39 namespace OHOS {
ObjectGetConnectionId(unsigned int type)40 unsigned int ObjectGetConnectionId(unsigned int type)
41 {
42     unsigned int ret = type << SHIFT_BITS;
43     ret++;
44     return ret;
45 }
46 
ObjectConnectDevice(const ConnectOption * option,unsigned int requestId,const ConnectResult * result)47 int ObjectConnectDevice(const ConnectOption *option, unsigned int requestId, const ConnectResult *result)
48 {
49     ConnectionInfo info = {0};
50     if (option == 0 || result == 0) {
51         return 1;
52     }
53     g_connInfo.isAvailable = 1;
54     g_connInfo.type = option->type;
55     result->OnConnectSuccessed(requestId, ObjectGetConnectionId(option->type), &info);
56     return 0;
57 }
58 
ObjectPostBytes(unsigned int connectionId,uint8_t * data,uint32_t len,int pid,int flag,int module,int64_t seq)59 int ObjectPostBytes(unsigned int connectionId, uint8_t *data, uint32_t len, int pid, int flag, int module, int64_t seq)
60 {
61     (void)connectionId;
62     (void)data;
63     (void)len;
64     (void)pid;
65     (void)flag;
66     (void)module;
67     (void)seq;
68     return 0;
69 }
70 
ObjectDisconnectDevice(unsigned int connectionId)71 int ObjectDisconnectDevice(unsigned int connectionId)
72 {
73     (void)connectionId;
74     return 0;
75 }
76 
ObjectGetConnectionInfo(unsigned int connectionId,ConnectionInfo * info)77 int ObjectGetConnectionInfo(unsigned int connectionId, ConnectionInfo *info)
78 {
79     (void)connectionId;
80     if (info == nullptr) {
81         return -1;
82     }
83     (void)memcpy_s(info, sizeof(ConnectionInfo), &g_connInfo, sizeof(ConnectionInfo));
84     return 0;
85 }
86 
ObjectStartLocalListening(const LocalListenerInfo * info)87 int ObjectStartLocalListening(const LocalListenerInfo *info)
88 {
89     if (info == nullptr) {
90         return 1;
91     }
92     if (g_mangerCb) {
93         g_mangerCb->OnConnected(ObjectGetConnectionId(info->type), &g_connInfo);
94     }
95     return 0;
96 }
97 
ObjectStopLocalListening(const LocalListenerInfo * info)98 int ObjectStopLocalListening(const LocalListenerInfo *info)
99 {
100     if (info == nullptr) {
101         return 1;
102     }
103     if (g_mangerCb) {
104         g_mangerCb->OnDisconnected(ObjectGetConnectionId(info->type), &g_connInfo);
105     }
106     return 0;
107 }
108 
ConnInitObject(const ConnectCallback * callback)109 ConnectFuncInterface *ConnInitObject(const ConnectCallback *callback)
110 {
111     if (callback == 0) {
112         return nullptr;
113     }
114     ConnectFuncInterface *inter = (ConnectFuncInterface*)calloc(1, sizeof(ConnectFuncInterface));
115     if (inter == nullptr) {
116         return nullptr;
117     }
118     g_mangerCb = (ConnectCallback*)callback;
119 
120     inter->ConnectDevice = ObjectConnectDevice;
121     inter->PostBytes = ObjectPostBytes;
122     inter->DisconnectDevice = ObjectDisconnectDevice;
123     inter->GetConnectionInfo = ObjectGetConnectionInfo;
124     inter->StartLocalListening = ObjectStartLocalListening;
125     inter->StopLocalListening = ObjectStopLocalListening;
126     return inter;
127 }
128 
ConnInitBr(const ConnectCallback * callback)129 extern "C" ConnectFuncInterface *ConnInitBr(const ConnectCallback *callback)
130 {
131     return ConnInitObject(callback);
132 }
133 
ConnInitTcp(const ConnectCallback * callback)134 extern "C" ConnectFuncInterface *ConnInitTcp(const ConnectCallback *callback)
135 {
136     return ConnInitObject(callback);
137 }
138 
ConnectedCB(unsigned int connectionId,const ConnectionInfo * info)139 void ConnectedCB(unsigned int connectionId, const ConnectionInfo *info)
140 {
141     printf("recv remote ConnectedCB %u\r\n", connectionId);
142     g_connId = connectionId;
143     return;
144 }
145 
DisConnectCB(unsigned int connectionId,const ConnectionInfo * info)146 void DisConnectCB(unsigned int connectionId, const ConnectionInfo *info)
147 {
148     printf("DconDisConnect %u\r\n", connectionId);
149     return;
150 }
151 
DataReceivedCB(unsigned int connectionId,ConnModule moduleId,int64_t seq,char * data,int len)152 void DataReceivedCB(unsigned int connectionId, ConnModule moduleId, int64_t seq, char *data, int len)
153 {
154     printf("DconDataReceived moduleId %d %s %d\r\n", moduleId, data, len);
155     return;
156 }
157 
ConnectSuccessedCB(unsigned int requestId,unsigned int connectionId,const ConnectionInfo * info)158 void ConnectSuccessedCB(unsigned int requestId, unsigned int connectionId, const ConnectionInfo *info)
159 {
160     printf("ConnectSuccessedCB %u\r\n", connectionId);
161     g_connId = connectionId;
162     return;
163 }
164 
ConnectFailedCB(unsigned int requestId,int reason)165 void ConnectFailedCB(unsigned int requestId, int reason)
166 {
167     (void)requestId;
168     (void)reason;
169     printf("DconConnectFailed\r\n");
170     return;
171 }
172 
173 class ConnectionManagerTest : public testing::Test {
174 public:
ConnectionManagerTest()175     ConnectionManagerTest()
176     {}
~ConnectionManagerTest()177     ~ConnectionManagerTest()
178     {}
179     static void SetUpTestCase(void);
180     static void TearDownTestCase(void);
181     void SetUp();
182     void TearDown();
183 };
184 
SetUpTestCase(void)185 void ConnectionManagerTest::SetUpTestCase(void)
186 {
187     SoftbusConfigInit();
188     LooperInit();
189     ConnServerInit();
190 }
191 
TearDownTestCase(void)192 void ConnectionManagerTest::TearDownTestCase(void)
193 {}
194 
SetUp(void)195 void ConnectionManagerTest::SetUp(void)
196 {}
197 
TearDown(void)198 void ConnectionManagerTest::TearDown(void)
199 {}
200 
201 /*
202 * @tc.name: testConnmanger001
203 * @tc.desc: test ConnTypeIsSupport
204 * @tc.type: FUNC
205 * @tc.require:
206 */
207 HWTEST_F(ConnectionManagerTest, testConnmanger001, TestSize.Level1)
208 {
209     int ret;
210     printf("testConnmanger001\r\n");
211 
212     ret = ConnTypeIsSupport(CONNECT_TCP);
213     EXPECT_EQ(SOFTBUS_OK, ret);
214 #ifdef connection_enable_br_test
215     ret = ConnTypeIsSupport(CONNECT_BR);
216     EXPECT_EQ(SOFTBUS_OK, ret);
217     GTEST_LOG_(INFO) << "BR Support";
218 #endif
219 
220 #ifdef connection_enable_ble_test
221     ret = ConnTypeIsSupport(CONNECT_BLE);
222     EXPECT_EQ(SOFTBUS_OK, ret);
223     GTEST_LOG_(INFO) << "BLE Support";
224 #endif
225 };
226 
227 /*
228 * @tc.name: testConnmanger002
229 * @tc.desc: test invalid param
230 * @tc.type: FUNC
231 * @tc.require:
232 */
233 HWTEST_F(ConnectionManagerTest, testConnmanger002, TestSize.Level1)
234 {
235     printf("test begin testConnmanger002 \r\n");
236     int32_t ret;
237     ret = ConnSetConnectCallback(static_cast<ConnModule>(0), nullptr);
238     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
239     ret = ConnConnectDevice(nullptr, 0, nullptr);
240     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
241     ret = ConnPostBytes(0, nullptr);
242     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
243     ret = ConnStartLocalListening(nullptr);
244     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
245     ret = ConnStopLocalListening(nullptr);
246     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
247 };
248 
249 /*
250 * @tc.name: testConnmanger003
251 * @tc.desc: test set unset callback and connect post disconnect
252 * @tc.type: FUNC
253 * @tc.require:
254 */
255 HWTEST_F(ConnectionManagerTest, testConnmanger003, TestSize.Level1)
256 {
257     int ret;
258     int reqId;
259     ConnectCallback connCb;
260     ConnectResult connRet;
261     ConnPostData data;
262     ConnectOption info;
263     const char *str = "send msg local2\r\n";
264     printf("test begin testConnmanger003 \r\n");
265 
266     connCb.OnConnected = ConnectedCB;
267     connCb.OnDisconnected = DisConnectCB;
268     connCb.OnDataReceived = DataReceivedCB;
269     ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
270     EXPECT_EQ(SOFTBUS_OK, ret);
271     ret = ConnSetConnectCallback(MODULE_AUTH_SDK, &connCb);
272     EXPECT_EQ(SOFTBUS_OK, ret);
273 
274     info.type = CONNECT_BR;
275     connRet.OnConnectFailed = ConnectFailedCB;
276     connRet.OnConnectSuccessed = ConnectSuccessedCB;
277     reqId = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
278     ret = ConnConnectDevice(&info, reqId, &connRet);
279     EXPECT_EQ(SOFTBUS_OK, ret);
280     if (g_connId) {
281         data.buf = (char *)calloc(1, CONN_HEAD_SIZE + 20);
282         ASSERT_TRUE(data.buf != nullptr);
283         (void)strcpy_s(data.buf + 1, strlen(str), str);
284         data.len = CONN_HEAD_SIZE + 20;
285         data.module = MODULE_TRUST_ENGINE;
286         data.pid = 0;
287         data.flag = 1;
288         data.seq = 1;
289         ret = ConnPostBytes(g_connId, &data);
290         EXPECT_EQ(SOFTBUS_OK, ret);
291         if (data.buf != nullptr) {
292             free(data.buf);
293         }
294     }
295     ret = ConnDisconnectDevice(g_connId);
296     EXPECT_EQ(SOFTBUS_OK, ret);
297     ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
298     ConnUnSetConnectCallback(MODULE_AUTH_SDK);
299     g_connId = 0;
300 };
301 
302 /*
303 * @tc.name: testConnmanger004
304 * @tc.desc: test set unset callback and post disconnect without connect
305 * @tc.type: FUNC
306 * @tc.require:
307 */
308 HWTEST_F(ConnectionManagerTest, testConnmanger004, TestSize.Level1)
309 {
310     printf("test begin ConnManagerTest004 \r\n");
311     int ret;
312     ConnectCallback connCb;
313     LocalListenerInfo info;
314     ConnPostData data;
315     const char *str = "send msg local2\r\n";
316 
317     connCb.OnConnected = ConnectedCB;
318     connCb.OnDisconnected = DisConnectCB;
319     connCb.OnDataReceived = DataReceivedCB;
320     ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
321     EXPECT_EQ(SOFTBUS_OK, ret);
322     info.type = CONNECT_BR;
323     ret = ConnStartLocalListening(&info);
324     EXPECT_EQ(SOFTBUS_OK, ret);
325 
326     if (g_connId) {
327         data.buf = (char*)calloc(1, CONN_HEAD_SIZE + 20);
328         (void)strcpy_s(data.buf + CONN_HEAD_SIZE, strlen(str), str);
329         ASSERT_TRUE(data.buf != NULL);
330         data.len = CONN_HEAD_SIZE + 20;
331         data.module = MODULE_TRUST_ENGINE;
332         data.pid = 0;
333         data.flag = 1;
334         data.seq = 1;
335         ret = ConnPostBytes(g_connId, &data);
336         EXPECT_EQ(SOFTBUS_OK, ret);
337         ret = ConnDisconnectDevice(g_connId);
338         EXPECT_EQ(SOFTBUS_OK, ret);
339         if (data.buf != nullptr) {
340             free(data.buf);
341         }
342     }
343 
344     ret = ConnStopLocalListening(&info);
345     EXPECT_EQ(SOFTBUS_OK, ret);
346     ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
347     g_connId = 0;
348 };
349 
350 /*
351 * @tc.name: testConnmanger005
352 * @tc.desc: test set unset callback multi times
353 * @tc.type: FUNC
354 * @tc.require:
355 */
356 HWTEST_F(ConnectionManagerTest, testConnmanger005, TestSize.Level1)
357 {
358     int ret;
359     ConnectCallback connCb;
360 
361     connCb.OnConnected = ConnectedCB;
362     connCb.OnDisconnected = DisConnectCB;
363     connCb.OnDataReceived = DataReceivedCB;
364     ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
365     EXPECT_EQ(SOFTBUS_OK, ret);
366     ret = ConnSetConnectCallback(MODULE_AUTH_SDK, &connCb);
367     EXPECT_EQ(SOFTBUS_OK, ret);
368     ret = ConnSetConnectCallback(MODULE_AUTH_SDK, &connCb);
369     EXPECT_EQ(SOFTBUS_CONN_INTERNAL_ERR, ret);
370 
371     ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
372     ConnUnSetConnectCallback(MODULE_AUTH_SDK);
373 };
374 
375 /*
376 * @tc.name: testConnmanger006
377 * @tc.desc: test set unset callback and connect post disconnect
378 * @tc.type: FUNC
379 * @tc.require:
380 */
381 HWTEST_F(ConnectionManagerTest, testConnmanger006, TestSize.Level1)
382 {
383     uint32_t reqId = 1;
384     int32_t ret;
385     ConnectCallback connCb;
386     ConnectOption optionInfo;
387     ConnectionInfo info;
388     ConnectResult connRet;
389 
390     connCb.OnConnected = ConnectedCB;
391     connCb.OnDisconnected = DisConnectCB;
392     connCb.OnDataReceived = DataReceivedCB;
393     ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
394     EXPECT_EQ(SOFTBUS_OK, ret);
395 
396     optionInfo.type = CONNECT_BR;
397     connRet.OnConnectFailed = ConnectFailedCB;
398     connRet.OnConnectSuccessed = ConnectSuccessedCB;
399     reqId = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
400     ret = ConnConnectDevice(&optionInfo, reqId, &connRet);
401     EXPECT_EQ(SOFTBUS_OK, ret);
402     if (g_connId) {
403         ret = ConnGetConnectionInfo(g_connId, &info);
404         EXPECT_EQ(SOFTBUS_OK, ret);
405         ret = ConnDisconnectDevice(g_connId);
406         g_connId = 0;
407         EXPECT_EQ(SOFTBUS_OK, ret);
408         printf("testConnmanger006 ConnDisconnectDevice\r\n");
409     }
410     printf("testConnmanger006 ConnUnSetConnectCallback\r\n");
411     ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
412     printf("testConnmanger006 ConnUnSetConnectCallback end 11\r\n");
413 };
414 
415 /*
416 * @tc.name: testConnmanger007
417 * @tc.desc: Test ConnSetConnectCallback moduleId out of max.
418 * @tc.in: Test module, Test number, Test levels.
419 * @tc.out: NonZero
420 * @tc.type: FUNC
421 * @tc.require: The ConnSetConnectCallback operates normally.
422 */
423 HWTEST_F(ConnectionManagerTest, testConnmanger007, TestSize.Level1)
424 {
425     ConnectCallback connCb;
426     connCb.OnConnected = ConnectedCB;
427     connCb.OnDisconnected = DisConnectCB;
428     connCb.OnDataReceived = DataReceivedCB;
429 
430     int moduleIdMin = 0;
431     int moduleIdMax = 200;
432 
433     int ret = ConnSetConnectCallback((ConnModule)moduleIdMin, &connCb);
434     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
435 
436     ret = ConnSetConnectCallback((ConnModule)moduleIdMax, &connCb);
437     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
438 };
439 
440 /*
441 * @tc.name: testConnmanger008
442 * @tc.desc: Test ConnConnectDevice info type out of max.
443 * @tc.in: Test module, Test number, Test levels.
444 * @tc.out: NonZero
445 * @tc.type: FUNC
446 * @tc.require: The ConnConnectDevice operates normally.
447 */
448 HWTEST_F(ConnectionManagerTest, testConnmanger008, TestSize.Level1)
449 {
450     const char *testBleMac = "11:22:33:44:55:66";
451     ConnectResult connRet;
452     ConnectOption info;
453     info.type = CONNECT_BLE;
454     (void)memcpy_s(info.bleOption.bleMac, BT_MAC_LEN, testBleMac, BT_MAC_LEN);
455     connRet.OnConnectFailed = ConnectFailedCB;
456     connRet.OnConnectSuccessed = ConnectSuccessedCB;
457     uint32_t reqId = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
458 
459     int ret = ConnConnectDevice(nullptr, reqId, &connRet);
460     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
461 
462     info.type = (ConnectType)(CONNECT_TYPE_MAX + 1);
463     ret = ConnConnectDevice(&info, reqId, &connRet);
464     EXPECT_EQ(SOFTBUS_CONN_MANAGER_TYPE_NOT_SUPPORT, ret);
465 
466     info.type = (ConnectType)(CONNECT_TCP -1);
467     ret = ConnConnectDevice(&info, reqId, &connRet);
468     EXPECT_EQ(SOFTBUS_CONN_MANAGER_TYPE_NOT_SUPPORT, ret);
469 };
470 
471 /*
472 * @tc.name: testConnmanger009
473 * @tc.desc: Test ConnStartLocalListening info type out of max.
474 * @tc.in: Test module, Test number, Test levels.
475 * @tc.out: NonZero
476 * @tc.type: FUNC
477 * @tc.require: The ConnStartLocalListening operates normally.
478 */
479 HWTEST_F(ConnectionManagerTest, testConnmanger009, TestSize.Level1)
480 {
481     LocalListenerInfo info;
482     info.type = (ConnectType)(CONNECT_TYPE_MAX + 1);
483     int ret = ConnStartLocalListening(&info);
484     EXPECT_EQ(SOFTBUS_CONN_MANAGER_TYPE_NOT_SUPPORT, ret);
485 };
486 
487 /*
488 * @tc.name: testConnmanger010
489 * @tc.desc: Test ConnStopLocalListening info type out of max.
490 * @tc.in: Test module, Test number, Test levels.
491 * @tc.out: NonZero
492 * @tc.type: FUNC
493 * @tc.require: The ConnStopLocalListening operates normally.
494 */
495 HWTEST_F(ConnectionManagerTest, testConnmanger010, TestSize.Level1)
496 {
497     LocalListenerInfo info;
498     info.type = (ConnectType)(CONNECT_TYPE_MAX + 1);
499     int ret = ConnStopLocalListening(&info);
500     EXPECT_EQ(SOFTBUS_CONN_MANAGER_TYPE_NOT_SUPPORT, ret);
501 };
502 
503 /*
504 * @tc.name: testConnmanger011
505 * @tc.desc: Test ConnTypeIsSupport type out of max.
506 * @tc.in: Test module, Test number, Test levels.
507 * @tc.out: NonZero
508 * @tc.type: FUNC
509 * @tc.require: The ConnTypeIsSupport operates normally.
510 */
511 HWTEST_F(ConnectionManagerTest, testConnmanger011, TestSize.Level1)
512 {
513     int ret = ConnTypeIsSupport(CONNECT_TYPE_MAX);
514     EXPECT_EQ(SOFTBUS_CONN_INVALID_CONN_TYPE, ret);
515 };
516 } // namespace OHOS