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