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 #include "br_connection.c"
30
31 static const uint32_t CONN_HEAD_SIZE = 24;
32 #define TEST_BR_MAC "24:DA:33:6A:06:EC"
33
34 static unsigned int g_connId = 0;
35 static unsigned int g_secondConnId = 0;
36
37 #define WAIT_CONNECTION_COUNT 8
38 #define WAIT_CONNECTION_SLEEP_TIME 1
39
40 using namespace testing::ext;
41
42 namespace OHOS {
ConnectedCB(unsigned int connectionId,const ConnectionInfo * info)43 void ConnectedCB(unsigned int connectionId, const ConnectionInfo *info)
44 {
45 printf("recv remote ConnectedCB %u %d\r\n", connectionId, info->type);
46 if (info->type == CONNECT_BR) {
47 g_connId = connectionId;
48 }
49 return;
50 }
51
DisConnectCB(unsigned int connectionId,const ConnectionInfo * info)52 void DisConnectCB(unsigned int connectionId, const ConnectionInfo *info)
53 {
54 printf("DconDisConnect %u\r\n", connectionId);
55 return;
56 }
57
DataReceivedCB(unsigned int connectionId,ConnModule moduleId,int64_t seq,char * data,int len)58 void DataReceivedCB(unsigned int connectionId, ConnModule moduleId, int64_t seq, char *data, int len)
59 {
60 printf("DconDataReceived moduleId %d %s %d\r\n", moduleId, data, len);
61 return;
62 }
63
ConnectSuccessedCB(unsigned int requestId,unsigned int connectionId,const ConnectionInfo * info)64 void ConnectSuccessedCB(unsigned int requestId, unsigned int connectionId, const ConnectionInfo *info)
65 {
66 printf("ConnectSuccessedCB %u\r\n", connectionId);
67 g_connId = connectionId;
68 return;
69 }
70
SecondConnectSuccessedCB(unsigned int requestId,unsigned int connectionId,const ConnectionInfo * info)71 void SecondConnectSuccessedCB(unsigned int requestId, unsigned int connectionId, const ConnectionInfo *info)
72 {
73 g_secondConnId = connectionId;
74 return;
75 }
76
ConnectFailedCB(unsigned int requestId,int reason)77 void ConnectFailedCB(unsigned int requestId, int reason)
78 {
79 (void)requestId;
80 (void)reason;
81 printf("DconConnectFailed\r\n");
82 return;
83 }
84
85 class ConnectionBrTest : public testing::Test {
86 public:
ConnectionBrTest()87 ConnectionBrTest()
88 {}
~ConnectionBrTest()89 ~ConnectionBrTest()
90 {}
91 static void SetUpTestCase(void);
92 static void TearDownTestCase(void);
93 void SetUp();
94 void TearDown();
95 };
96
SetUpTestCase(void)97 void ConnectionBrTest::SetUpTestCase(void)
98 {
99 SoftbusConfigInit();
100 ConnServerInit();
101 }
102
TearDownTestCase(void)103 void ConnectionBrTest::TearDownTestCase(void)
104 {}
105
SetUp(void)106 void ConnectionBrTest::SetUp(void)
107 {}
108
TearDown(void)109 void ConnectionBrTest::TearDown(void)
110 {}
111
GetBrConnStateByConnectionId(uint32_t connectId)112 int32_t GetBrConnStateByConnectionId(uint32_t connectId)
113 {
114 (void)connectId;
115 return BR_CONNECTION_STATE_CLOSED;
116 }
117
118 /*
119 * @tc.name: testConnmanger001
120 * @tc.desc: test ConnTypeIsSupport
121 * @tc.type: FUNC
122 * @tc.require:
123 */
124 HWTEST_F(ConnectionBrTest, testConnmanger001, TestSize.Level1)
125 {
126 int ret;
127 printf("testConnmanger001\r\n");
128
129 ret = ConnTypeIsSupport(CONNECT_BR);
130 EXPECT_EQ(SOFTBUS_OK, ret);
131 };
132
133 /*
134 * @tc.name: testConnmanger002
135 * @tc.desc: test invalid param
136 * @tc.in: test module, test number, Test Levels.
137 * @tc.out: zero
138 * @tc.type: FUNC
139 * @tc.require:AR000GIIE9
140 */
141 HWTEST_F(ConnectionBrTest, testConnmanger002, TestSize.Level1)
142 {
143 printf("test begin testConnmanger002 \r\n");
144 int ret;
145 ret = ConnSetConnectCallback(static_cast<ConnModule>(0), nullptr);
146 ASSERT_TRUE(ret != SOFTBUS_OK);
147 ret = ConnConnectDevice(nullptr, 0, nullptr);
148 ASSERT_TRUE(ret != SOFTBUS_OK);
149 ret = ConnPostBytes(0, nullptr);
150 ASSERT_TRUE(ret != SOFTBUS_OK);
151 ret = ConnStartLocalListening(nullptr);
152 ASSERT_TRUE(ret != SOFTBUS_OK);
153 ret = ConnStopLocalListening(nullptr);
154 ASSERT_TRUE(ret != SOFTBUS_OK);
155 EXPECT_EQ(SOFTBUS_OK, SOFTBUS_OK);
156 };
157
158 /*
159 * @tc.name: testConnmanger003
160 * @tc.desc: test set unset callback and connect post disconnect and multiple disconnects.
161 * @tc.type: FUNC
162 * @tc.require:AR000GIRGE
163 */
164 HWTEST_F(ConnectionBrTest, testConnmanger003, TestSize.Level1)
165 {
166 int ret;
167 int reqId;
168 ConnectCallback connCb;
169 ConnectResult connRet;
170 ConnPostData data;
171 ConnectOption info;
172 const char *str = "send msg local2\r\n";
173 printf("test begin testConnmanger003 \r\n");
174
175 connCb.OnConnected = ConnectedCB;
176 connCb.OnDisconnected = DisConnectCB;
177 connCb.OnDataReceived = DataReceivedCB;
178 ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
179 EXPECT_EQ(SOFTBUS_OK, ret);
180 info.type = CONNECT_BR;
181 (void)memcpy_s(info.brOption.brMac, BT_MAC_LEN, TEST_BR_MAC, BT_MAC_LEN);
182 printf("brMac: %s\n", info.brOption.brMac);
183 connRet.OnConnectFailed = ConnectFailedCB;
184 connRet.OnConnectSuccessed = ConnectSuccessedCB;
185 reqId = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
186 ret = ConnConnectDevice(&info, reqId, &connRet);
187 EXPECT_EQ(SOFTBUS_OK, ret);
188 if (g_connId) {
189 data.buf = (char *)calloc(1, CONN_HEAD_SIZE + 20);
190 ASSERT_TRUE(data.buf != NULL);
191 (void)strcpy_s(data.buf + 1, strlen(str), str);
192 data.len = CONN_HEAD_SIZE + 20;
193 data.module = MODULE_TRUST_ENGINE;
194 data.pid = 0;
195 data.flag = 1;
196 data.seq = 1;
197 ret = ConnPostBytes(g_connId, &data);
198 EXPECT_EQ(SOFTBUS_OK, ret);
199 if (data.buf != nullptr) {
200 free(data.buf);
201 }
202 ret = ConnDisconnectDevice(g_connId);
203 EXPECT_EQ(SOFTBUS_OK, ret);
204 ret = ConnDisconnectDevice(g_connId);
205 EXPECT_EQ(SOFTBUS_CONN_MANAGER_TYPE_NOT_SUPPORT, ret);
206 }
207
208 ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
209 g_connId = 0;
210 };
211
212 /*
213 * @tc.name: testConnmanger004
214 * @tc.desc: test set unset callback and post disconnect without connect
215 * @tc.type: FUNC
216 * @tc.require:AR000GIIE9
217 */
218 HWTEST_F(ConnectionBrTest, testConnmanger004, TestSize.Level1)
219 {
220 printf("test begin ConnManagerTest004 \r\n");
221 int ret;
222 ConnectCallback connCb;
223 LocalListenerInfo info;
224
225 connCb.OnConnected = ConnectedCB;
226 connCb.OnDisconnected = DisConnectCB;
227 connCb.OnDataReceived = DataReceivedCB;
228 ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
229 EXPECT_EQ(SOFTBUS_OK, ret);
230 info.type = CONNECT_BR;
231 ret = ConnStartLocalListening(&info);
232 EXPECT_EQ(SOFTBUS_OK, ret);
233 ret = ConnStopLocalListening(&info);
234 EXPECT_EQ(SOFTBUS_OK, ret);
235 ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
236 g_connId = 0;
237 };
238
239 /*
240 * @tc.name: testConnmanger005
241 * @tc.desc: The test set unsets the return value of callback and disconnection after connection.
242 * @tc.type: FUNC
243 * @tc.require:AR000GIRGE
244 */
245 HWTEST_F(ConnectionBrTest, testConnmanger005, TestSize.Level1)
246 {
247 int reqId = 1;
248 int ret;
249 ConnectCallback connCb;
250 ConnectOption optionInfo;
251 ConnectionInfo info;
252 ConnectResult connRet;
253
254 connCb.OnConnected = ConnectedCB;
255 connCb.OnDisconnected = DisConnectCB;
256 connCb.OnDataReceived = DataReceivedCB;
257 ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
258 EXPECT_EQ(SOFTBUS_OK, ret);
259
260 optionInfo.type = CONNECT_BR;
261 (void)memcpy_s(optionInfo.brOption.brMac, BT_MAC_LEN, TEST_BR_MAC, BT_MAC_LEN);
262 printf("brMac: %s\n", optionInfo.brOption.brMac);
263 connRet.OnConnectFailed = ConnectFailedCB;
264 connRet.OnConnectSuccessed = ConnectSuccessedCB;
265 reqId = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
266 ret = ConnConnectDevice(&optionInfo, reqId, &connRet);
267 EXPECT_EQ(SOFTBUS_OK, ret);
268 if (g_connId) {
269 ret = ConnGetConnectionInfo(g_connId, &info);
270 EXPECT_EQ(SOFTBUS_OK, ret);
271 ret = ConnDisconnectDeviceAllConn(&optionInfo);
272 g_connId = 0;
273 EXPECT_EQ(SOFTBUS_OK, ret);
274 printf("testConnmanger005 ConnDisconnectDevice\r\n");
275 }
276 printf("testConnmanger005 ConnUnSetConnectCallback\r\n");
277 ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
278 printf("testConnmanger005 ConnUnSetConnectCallback end 11\r\n");
279 };
280
281 /*
282 * @tc.name: testConnmanger006
283 * @tc.desc: Test set unset callback.
284 * @tc.in: Test module, Test number, Test Levels.
285 * @tc.out: NA
286 * @tc.type: FUNC
287 * @tc.require: The ConnSetConnectCallback operates normally.
288 */
289 HWTEST_F(ConnectionBrTest, testConnmanger006, TestSize.Level1)
290 {
291 int ret;
292 ConnectCallback connCb;
293
294 connCb.OnConnected = ConnectedCB;
295 connCb.OnConnected = DisConnectCB;
296 connCb.OnDataReceived = DataReceivedCB;
297 ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
298 ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
299 g_connId = 0;
300 };
301
302 /*
303 * @tc.name: testConnmanger007
304 * @tc.desc: Test set unset callback and connect post disconnect.
305 * @tc.in: Test module, Test number, Test Levels.
306 * @tc.out: Zero
307 * @tc.type: FUNC
308 * @tc.require: The ConnSetConnectCallback and ConnDisconnectDevice
309 * and ConnDisconnectDevice and ConnPostBytes and operates normally.
310 */
311 HWTEST_F(ConnectionBrTest, testConnmanger007, TestSize.Level1)
312 {
313 int ret;
314 int reqId;
315 ConnectCallback connCb;
316 ConnectResult connRet;
317 ConnPostData data;
318 ConnectOption info;
319 const char *str = "send msg local2\r\n";
320
321 connCb.OnConnected = ConnectedCB;
322 connCb.OnDisconnected = DisConnectCB;
323 connCb.OnDataReceived = DataReceivedCB;
324 ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
325 EXPECT_EQ(SOFTBUS_OK, ret);
326 info.type = CONNECT_BR;
327 (void)memcpy_s(info.brOption.brMac, BT_MAC_LEN, TEST_BR_MAC, BT_MAC_LEN);
328 connRet.OnConnectFailed = ConnectFailedCB;
329 connRet.OnConnectSuccessed = ConnectSuccessedCB;
330 reqId = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
331 ret = ConnConnectDevice(&info, reqId, &connRet);
332 EXPECT_EQ(SOFTBUS_OK, ret);
333 if (g_connId != 0) {
334 data.buf = (char *)calloc(1, CONN_HEAD_SIZE + 20);
335 ASSERT_TRUE(data.buf != NULL);
336 (void)strcpy_s(data.buf + 1, strlen(str), str);
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 if (data.buf != nullptr) {
345 free(data.buf);
346 }
347 ret = ConnDisconnectDevice(g_connId);
348 EXPECT_EQ(SOFTBUS_OK, ret);
349 }
350
351 ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
352 g_connId = 0;
353 };
354
355 /*
356 * @tc.name: testConnmanger008
357 * @tc.desc: Test set unset callback and connect twice has same ConnectID.
358 * @tc.in: Test module, Test number, Test Levels.
359 * @tc.out: Zero
360 * @tc.type: FUNC
361 * @tc.require: The ConnSetConnectCallback and ConnConnectDevice operates normally.
362 */
363 HWTEST_F(ConnectionBrTest, testConnmanger008, TestSize.Level1)
364 {
365 int ret;
366 ConnectCallback connCb;
367 ConnectOption optionInfo;
368 ConnectionInfo info;
369 ConnectResult connRet;
370 ConnectResult connRet2;
371
372 connCb.OnConnected = ConnectedCB;
373 connCb.OnDisconnected = DisConnectCB;
374 connCb.OnDataReceived = DataReceivedCB;
375 ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
376 EXPECT_EQ(SOFTBUS_OK, ret);
377
378 optionInfo.type = CONNECT_BR;
379 (void)memcpy_s(optionInfo.brOption.brMac, BT_MAC_LEN, TEST_BR_MAC, BT_MAC_LEN);
380 connRet.OnConnectFailed = ConnectFailedCB;
381 connRet.OnConnectSuccessed = ConnectSuccessedCB;
382 int reqId = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
383 ret = ConnConnectDevice(&optionInfo, reqId, &connRet);
384
385 connRet2.OnConnectFailed = ConnectFailedCB;
386 connRet2.OnConnectSuccessed = SecondConnectSuccessedCB;
387 int reqId2 = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
388 ret = ConnConnectDevice(&optionInfo, reqId2, &connRet2);
389 EXPECT_EQ(SOFTBUS_OK, ret);
390 sleep(1);
391 if ((g_connId) && (g_secondConnId)) {
392 EXPECT_EQ(g_connId, g_secondConnId);
393 }
394
395 if (g_connId) {
396 ret = ConnGetConnectionInfo(g_connId, &info);
397 EXPECT_EQ(SOFTBUS_OK, ret);
398 ret = ConnDisconnectDeviceAllConn(&optionInfo);
399 g_connId = 0;
400 EXPECT_EQ(SOFTBUS_OK, ret);
401 }
402 ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
403 };
404
405 /*
406 * @tc.name: testConnmanger009
407 * @tc.desc: Test set unset callback and connect twice post disconnect post.
408 * @tc.in: Test module, Test number, Test Levels.
409 * @tc.out: Zero
410 * @tc.type: FUNC
411 * @tc.require: The ConnSetConnectCallback and ConnConnectDevice operates normally.
412 */
413 HWTEST_F(ConnectionBrTest, testConnmanger009, TestSize.Level1)
414 {
415 int ret;
416 ConnectCallback connCb;
417 ConnectResult connRet;
418 ConnPostData data;
419 ConnectOption info;
420 const char *str = "send msg local2\r\n";
421
422 connCb.OnConnected = ConnectedCB;
423 connCb.OnDisconnected = DisConnectCB;
424 connCb.OnDataReceived = DataReceivedCB;
425 ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
426 EXPECT_EQ(SOFTBUS_OK, ret);
427 info.type = CONNECT_BR;
428 (void)memcpy_s(info.brOption.brMac, BT_MAC_LEN, TEST_BR_MAC, BT_MAC_LEN);
429 printf("brMac: %s\n", info.brOption.brMac);
430 connRet.OnConnectFailed = ConnectFailedCB;
431 connRet.OnConnectSuccessed = ConnectSuccessedCB;
432 int reqId1 = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
433 ret = ConnConnectDevice(&info, reqId1, &connRet);
434 int reqId2 = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
435 ret = ConnConnectDevice(&info, reqId2, &connRet);
436 EXPECT_EQ(SOFTBUS_OK, ret);
437 if (g_connId != 0) {
438 data.buf = (char *)calloc(1, CONN_HEAD_SIZE + 20);
439 ASSERT_TRUE(data.buf != NULL);
440 (void)strcpy_s(data.buf + 1, strlen(str), str);
441 data.len = CONN_HEAD_SIZE + 20;
442 data.module = MODULE_TRUST_ENGINE;
443 data.pid = 0;
444 data.flag = 1;
445 data.seq = 1;
446 ret = ConnPostBytes(g_connId, &data);
447 EXPECT_EQ(SOFTBUS_OK, ret);
448
449 ret = ConnDisconnectDevice(g_connId);
450 EXPECT_EQ(SOFTBUS_OK, ret);
451
452 ret = ConnPostBytes(g_connId, &data);
453 ASSERT_EQ(SOFTBUS_OK, ret);
454 if (data.buf != nullptr) {
455 free(data.buf);
456 }
457 }
458 ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
459 g_connId = 0;
460 };
461
462 /*
463 * @tc.name: testConnmanger010
464 * @tc.desc: Test set unset callback and connect twice post disconnectAll post.
465 * @tc.in: Test module, Test number, Test Levels.
466 * @tc.out: Zero
467 * @tc.type: FUNC
468 * @tc.require: The ConnSetConnectCallback and ConnConnectDevice operates normally.
469 */
470 HWTEST_F(ConnectionBrTest, testConnmanger010, TestSize.Level1)
471 {
472 int ret;
473 ConnectCallback connCb;
474 ConnectResult connRet;
475 ConnPostData data;
476 ConnectOption info;
477 const char *str = "send msg local2\r\n";
478
479 connCb.OnConnected = ConnectedCB;
480 connCb.OnDisconnected = DisConnectCB;
481 connCb.OnDataReceived = DataReceivedCB;
482 ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
483 EXPECT_EQ(SOFTBUS_OK, ret);
484 info.type = CONNECT_BR;
485 (void)memcpy_s(info.brOption.brMac, BT_MAC_LEN, TEST_BR_MAC, BT_MAC_LEN);
486 connRet.OnConnectFailed = ConnectFailedCB;
487 connRet.OnConnectSuccessed = ConnectSuccessedCB;
488
489 int reqId1 = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
490 ret = ConnConnectDevice(&info, reqId1, &connRet);
491 EXPECT_EQ(SOFTBUS_OK, ret);
492 int reqId2 = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
493 ret = ConnConnectDevice(&info, reqId2, &connRet);
494 EXPECT_EQ(SOFTBUS_OK, ret);
495
496 if (g_connId != 0) {
497 data.buf = (char *)calloc(1, CONN_HEAD_SIZE + 20);
498 ASSERT_TRUE(data.buf != NULL);
499 (void)strcpy_s(data.buf + 1, strlen(str), str);
500 data.len = CONN_HEAD_SIZE + 20;
501 data.module = MODULE_TRUST_ENGINE;
502 data.pid = 0;
503 data.flag = 1;
504 data.seq = 1;
505
506 ret = ConnPostBytes(g_connId, &data);
507 ASSERT_EQ(SOFTBUS_OK, ret);
508 ret = ConnDisconnectDeviceAllConn(&info);
509 EXPECT_EQ(SOFTBUS_OK, ret);
510 ret = ConnPostBytes(g_connId, &data);
511 ASSERT_NE(SOFTBUS_OK, ret);
512
513 g_connId = 0;
514 if (data.buf != nullptr) {
515 free(data.buf);
516 }
517 }
518 ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
519 };
520
521 /*
522 * @tc.name: PostBytesInner
523 * @tc.desc: connect state != BR_CONNECTION_STATE_CLOSING && state != BR_CONNECTION_STATE_CONNECTED
524 * @tc.type: FUNC
525 * @tc.require:
526 */
527 HWTEST_F(ConnectionBrTest, PostBytesInner, TestSize.Level1)
528 {
529 uint32_t len = 16;
530 char *data = (char *) SoftBusMalloc(len);
531 ASSERT_TRUE(data != nullptr);
532 int32_t ret = PostBytesInner(0, 0, data, len);
533 EXPECT_EQ(ret, SOFTBUS_BRCONNECTION_POSTBYTES_ERROR);
534 }
535
536 /*
537 * @tc.name: PostBytes
538 * @tc.desc: connect state != BR_CONNECTION_STATE_CONNECTED
539 * @tc.type: FUNC
540 * @tc.require:
541 */
542 HWTEST_F(ConnectionBrTest, PostBytes, TestSize.Level1)
543 {
544 uint32_t len = 16;
545 char *data = (char *) SoftBusMalloc(len);
546 ASSERT_TRUE(data != nullptr);
547 int32_t ret = PostBytesInner(0, 0, data, len);
548 EXPECT_EQ(ret, SOFTBUS_BRCONNECTION_POSTBYTES_ERROR);
549 }
550 }