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 "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,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 ConnServerInit();
189 }
190
TearDownTestCase(void)191 void ConnectionManagerTest::TearDownTestCase(void)
192 {}
193
SetUp(void)194 void ConnectionManagerTest::SetUp(void)
195 {}
196
TearDown(void)197 void ConnectionManagerTest::TearDown(void)
198 {}
199
200 /*
201 * @tc.name: testConnmanger001
202 * @tc.desc: test ConnTypeIsSupport
203 * @tc.type: FUNC
204 * @tc.require:
205 */
206 HWTEST_F(ConnectionManagerTest, testConnmanger001, TestSize.Level1)
207 {
208 int ret;
209 printf("testConnmanger001\r\n");
210
211 ret = ConnTypeIsSupport(CONNECT_TCP);
212 EXPECT_EQ(SOFTBUS_OK, ret);
213 #ifdef connection_enable_br_test
214 ret = ConnTypeIsSupport(CONNECT_BR);
215 EXPECT_EQ(SOFTBUS_OK, ret);
216 GTEST_LOG_(INFO) << "BR Support";
217 #endif
218
219 #ifdef connection_enable_ble_test
220 ret = ConnTypeIsSupport(CONNECT_BLE);
221 EXPECT_EQ(SOFTBUS_OK, ret);
222 GTEST_LOG_(INFO) << "BLE Support";
223 #endif
224 };
225
226 /*
227 * @tc.name: testConnmanger002
228 * @tc.desc: test invalid param
229 * @tc.type: FUNC
230 * @tc.require:
231 */
232 HWTEST_F(ConnectionManagerTest, testConnmanger002, TestSize.Level1)
233 {
234 printf("test begin testConnmanger002 \r\n");
235 int32_t ret;
236 ret = ConnSetConnectCallback(static_cast<ConnModule>(0), nullptr);
237 EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
238 ret = ConnConnectDevice(nullptr, 0, nullptr);
239 EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
240 ret = ConnPostBytes(0, nullptr);
241 EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
242 ret = ConnStartLocalListening(nullptr);
243 EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
244 ret = ConnStopLocalListening(nullptr);
245 EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
246 };
247
248 /*
249 * @tc.name: testConnmanger003
250 * @tc.desc: test set unset callback and connect post disconnect
251 * @tc.type: FUNC
252 * @tc.require:
253 */
254 HWTEST_F(ConnectionManagerTest, testConnmanger003, TestSize.Level1)
255 {
256 int ret;
257 int reqId;
258 ConnectCallback connCb;
259 ConnectResult connRet;
260 ConnPostData data;
261 ConnectOption info;
262 const char *str = "send msg local2\r\n";
263 printf("test begin testConnmanger003 \r\n");
264
265 connCb.OnConnected = ConnectedCB;
266 connCb.OnDisconnected = DisConnectCB;
267 connCb.OnDataReceived = DataReceivedCB;
268 ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
269 EXPECT_EQ(SOFTBUS_OK, ret);
270 ret = ConnSetConnectCallback(MODULE_AUTH_SDK, &connCb);
271 EXPECT_EQ(SOFTBUS_OK, ret);
272
273 info.type = CONNECT_BR;
274 connRet.OnConnectFailed = ConnectFailedCB;
275 connRet.OnConnectSuccessed = ConnectSuccessedCB;
276 reqId = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
277 ret = ConnConnectDevice(&info, reqId, &connRet);
278 EXPECT_EQ(SOFTBUS_OK, ret);
279 if (g_connId) {
280 data.buf = (char *)calloc(1, CONN_HEAD_SIZE + 20);
281 ASSERT_TRUE(data.buf != nullptr);
282 (void)strcpy_s(data.buf + 1, strlen(str), str);
283 data.len = CONN_HEAD_SIZE + 20;
284 data.module = MODULE_TRUST_ENGINE;
285 data.pid = 0;
286 data.flag = 1;
287 data.seq = 1;
288 ret = ConnPostBytes(g_connId, &data);
289 EXPECT_EQ(SOFTBUS_OK, ret);
290 if (data.buf != nullptr) {
291 free(data.buf);
292 }
293 }
294 ret = ConnDisconnectDevice(g_connId);
295 EXPECT_EQ(SOFTBUS_OK, ret);
296 ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
297 ConnUnSetConnectCallback(MODULE_AUTH_SDK);
298 g_connId = 0;
299 };
300
301 /*
302 * @tc.name: testConnmanger004
303 * @tc.desc: test set unset callback and post disconnect without connect
304 * @tc.type: FUNC
305 * @tc.require:
306 */
307 HWTEST_F(ConnectionManagerTest, testConnmanger004, TestSize.Level1)
308 {
309 printf("test begin ConnManagerTest004 \r\n");
310 int ret;
311 ConnectCallback connCb;
312 LocalListenerInfo info;
313 ConnPostData data;
314 const char *str = "send msg local2\r\n";
315
316 connCb.OnConnected = ConnectedCB;
317 connCb.OnDisconnected = DisConnectCB;
318 connCb.OnDataReceived = DataReceivedCB;
319 ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
320 EXPECT_EQ(SOFTBUS_OK, ret);
321 info.type = CONNECT_BR;
322 ret = ConnStartLocalListening(&info);
323 EXPECT_EQ(SOFTBUS_OK, ret);
324
325 if (g_connId) {
326 data.buf = (char*)calloc(1, CONN_HEAD_SIZE + 20);
327 (void)strcpy_s(data.buf + CONN_HEAD_SIZE, strlen(str), str);
328 ASSERT_TRUE(data.buf != NULL);
329 data.len = CONN_HEAD_SIZE + 20;
330 data.module = MODULE_TRUST_ENGINE;
331 data.pid = 0;
332 data.flag = 1;
333 data.seq = 1;
334 ret = ConnPostBytes(g_connId, &data);
335 EXPECT_EQ(SOFTBUS_OK, ret);
336 ret = ConnDisconnectDevice(g_connId);
337 EXPECT_EQ(SOFTBUS_OK, ret);
338 if (data.buf != nullptr) {
339 free(data.buf);
340 }
341 }
342
343 ret = ConnStopLocalListening(&info);
344 EXPECT_EQ(SOFTBUS_OK, ret);
345 ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
346 g_connId = 0;
347 };
348
349 /*
350 * @tc.name: testConnmanger005
351 * @tc.desc: test set unset callback multi times
352 * @tc.type: FUNC
353 * @tc.require:
354 */
355 HWTEST_F(ConnectionManagerTest, testConnmanger005, TestSize.Level1)
356 {
357 int ret;
358 ConnectCallback connCb;
359
360 connCb.OnConnected = ConnectedCB;
361 connCb.OnDisconnected = DisConnectCB;
362 connCb.OnDataReceived = DataReceivedCB;
363 ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
364 EXPECT_EQ(SOFTBUS_OK, ret);
365 ret = ConnSetConnectCallback(MODULE_AUTH_SDK, &connCb);
366 EXPECT_EQ(SOFTBUS_OK, ret);
367 ret = ConnSetConnectCallback(MODULE_AUTH_SDK, &connCb);
368 EXPECT_EQ(SOFTBUS_ERR, ret);
369
370 ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
371 ConnUnSetConnectCallback(MODULE_AUTH_SDK);
372 };
373
374 /*
375 * @tc.name: testConnmanger006
376 * @tc.desc: test set unset callback and connect post disconnect
377 * @tc.type: FUNC
378 * @tc.require:
379 */
380 HWTEST_F(ConnectionManagerTest, testConnmanger006, TestSize.Level1)
381 {
382 uint32_t reqId = 1;
383 int32_t ret;
384 ConnectCallback connCb;
385 ConnectOption optionInfo;
386 ConnectionInfo info;
387 ConnectResult connRet;
388
389 connCb.OnConnected = ConnectedCB;
390 connCb.OnDisconnected = DisConnectCB;
391 connCb.OnDataReceived = DataReceivedCB;
392 ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
393 EXPECT_EQ(SOFTBUS_OK, ret);
394
395 optionInfo.type = CONNECT_BR;
396 connRet.OnConnectFailed = ConnectFailedCB;
397 connRet.OnConnectSuccessed = ConnectSuccessedCB;
398 reqId = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
399 ret = ConnConnectDevice(&optionInfo, reqId, &connRet);
400 EXPECT_EQ(SOFTBUS_OK, ret);
401 if (g_connId) {
402 ret = ConnGetConnectionInfo(g_connId, &info);
403 EXPECT_EQ(SOFTBUS_OK, ret);
404 ret = ConnDisconnectDevice(g_connId);
405 g_connId = 0;
406 EXPECT_EQ(SOFTBUS_OK, ret);
407 printf("testConnmanger006 ConnDisconnectDevice\r\n");
408 }
409 printf("testConnmanger006 ConnUnSetConnectCallback\r\n");
410 ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
411 printf("testConnmanger006 ConnUnSetConnectCallback end 11\r\n");
412 };
413
414 /*
415 * @tc.name: testConnmanger007
416 * @tc.desc: Test ConnSetConnectCallback moduleId out of max.
417 * @tc.in: Test module, Test number, Test levels.
418 * @tc.out: NonZero
419 * @tc.type: FUNC
420 * @tc.require: The ConnSetConnectCallback operates normally.
421 */
422 HWTEST_F(ConnectionManagerTest, testConnmanger007, TestSize.Level1)
423 {
424 ConnectCallback connCb;
425 connCb.OnConnected = ConnectedCB;
426 connCb.OnDisconnected = DisConnectCB;
427 connCb.OnDataReceived = DataReceivedCB;
428
429 int moduleIdMin = 0;
430 int moduleIdMax = 200;
431
432 int ret = ConnSetConnectCallback((ConnModule)moduleIdMin, &connCb);
433 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
434
435 ret = ConnSetConnectCallback((ConnModule)moduleIdMax, &connCb);
436 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
437 };
438
439 /*
440 * @tc.name: testConnmanger008
441 * @tc.desc: Test ConnConnectDevice info type out of max.
442 * @tc.in: Test module, Test number, Test levels.
443 * @tc.out: NonZero
444 * @tc.type: FUNC
445 * @tc.require: The ConnConnectDevice operates normally.
446 */
447 HWTEST_F(ConnectionManagerTest, testConnmanger008, TestSize.Level1)
448 {
449 const char *testBleMac = "11:22:33:44:55:66";
450 ConnectResult connRet;
451 ConnectOption info;
452 info.type = CONNECT_BLE;
453 (void)memcpy_s(info.bleOption.bleMac, BT_MAC_LEN, testBleMac, BT_MAC_LEN);
454 connRet.OnConnectFailed = ConnectFailedCB;
455 connRet.OnConnectSuccessed = ConnectSuccessedCB;
456 uint32_t reqId = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
457
458 int ret = ConnConnectDevice(nullptr, reqId, &connRet);
459 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
460
461 info.type = (ConnectType)(CONNECT_TYPE_MAX + 1);
462 ret = ConnConnectDevice(&info, reqId, &connRet);
463 EXPECT_EQ(SOFTBUS_CONN_MANAGER_TYPE_NOT_SUPPORT, ret);
464
465 info.type = (ConnectType)(CONNECT_TCP -1);
466 ret = ConnConnectDevice(&info, reqId, &connRet);
467 EXPECT_EQ(SOFTBUS_CONN_MANAGER_TYPE_NOT_SUPPORT, ret);
468 };
469
470 /*
471 * @tc.name: testConnmanger009
472 * @tc.desc: Test ConnStartLocalListening info type out of max.
473 * @tc.in: Test module, Test number, Test levels.
474 * @tc.out: NonZero
475 * @tc.type: FUNC
476 * @tc.require: The ConnStartLocalListening operates normally.
477 */
478 HWTEST_F(ConnectionManagerTest, testConnmanger009, TestSize.Level1)
479 {
480 LocalListenerInfo info;
481 info.type = (ConnectType)(CONNECT_TYPE_MAX + 1);
482 int ret = ConnStartLocalListening(&info);
483 EXPECT_EQ(SOFTBUS_CONN_MANAGER_TYPE_NOT_SUPPORT, ret);
484 };
485
486 /*
487 * @tc.name: testConnmanger010
488 * @tc.desc: Test ConnStopLocalListening info type out of max.
489 * @tc.in: Test module, Test number, Test levels.
490 * @tc.out: NonZero
491 * @tc.type: FUNC
492 * @tc.require: The ConnStopLocalListening operates normally.
493 */
494 HWTEST_F(ConnectionManagerTest, testConnmanger010, TestSize.Level1)
495 {
496 LocalListenerInfo info;
497 info.type = (ConnectType)(CONNECT_TYPE_MAX + 1);
498 int ret = ConnStopLocalListening(&info);
499 EXPECT_EQ(SOFTBUS_CONN_MANAGER_TYPE_NOT_SUPPORT, ret);
500 };
501
502 /*
503 * @tc.name: testConnmanger011
504 * @tc.desc: Test ConnTypeIsSupport type out of max.
505 * @tc.in: Test module, Test number, Test levels.
506 * @tc.out: NonZero
507 * @tc.type: FUNC
508 * @tc.require: The ConnTypeIsSupport operates normally.
509 */
510 HWTEST_F(ConnectionManagerTest, testConnmanger011, TestSize.Level1)
511 {
512 int ret = ConnTypeIsSupport(CONNECT_TYPE_MAX);
513 EXPECT_EQ(SOFTBUS_CONN_INVALID_CONN_TYPE, ret);
514 };
515 } // namespace OHOS