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 "connection_br_mock.h"
23 #include "softbus_conn_br_trans.h"
24 #include "common_list.h"
25 #include "softbus_adapter_mem.h"
26 #include "softbus_conn_br_connection.h"
27 #include "softbus_conn_br_pending_packet.h"
28 #include "softbus_conn_interface.h"
29 #include "softbus_conn_manager.h"
30 #include "softbus_def.h"
31 #include "softbus_errcode.h"
32 #include "softbus_feature_config.h"
33 #include "softbus_conn_br_send_queue.h"
34
35 static const uint32_t CONN_HEAD_SIZE = 24;
36 #define TEST_BR_MAC "24:DA:33:6A:06:EC"
37
38 static unsigned int g_connId = 0;
39 static unsigned int g_secondConnId = 0;
40
41 #define WAIT_CONNECTION_COUNT 8
42 #define WAIT_CONNECTION_SLEEP_TIME 1
43
44 using namespace testing::ext;
45 using namespace testing;
46
47 namespace OHOS {
ConnectedCB(unsigned int connectionId,const ConnectionInfo * info)48 void ConnectedCB(unsigned int connectionId, const ConnectionInfo *info)
49 {
50 printf("recv remote ConnectedCB %u %d\r\n", connectionId, info->type);
51 if (info->type == CONNECT_BR) {
52 g_connId = connectionId;
53 }
54 return;
55 }
56
DisConnectCB(unsigned int connectionId,const ConnectionInfo * info)57 void DisConnectCB(unsigned int connectionId, const ConnectionInfo *info)
58 {
59 printf("DconDisConnect %u\r\n", connectionId);
60 return;
61 }
62
DataReceivedCB(unsigned int connectionId,ConnModule moduleId,int64_t seq,char * data,int len)63 void DataReceivedCB(unsigned int connectionId, ConnModule moduleId, int64_t seq, char *data, int len)
64 {
65 printf("DconDataReceived moduleId %d %s %d\r\n", moduleId, data, len);
66 return;
67 }
68
ConnectSuccessedCB(unsigned int requestId,unsigned int connectionId,const ConnectionInfo * info)69 void ConnectSuccessedCB(unsigned int requestId, unsigned int connectionId, const ConnectionInfo *info)
70 {
71 printf("ConnectSuccessedCB %u\r\n", connectionId);
72 g_connId = connectionId;
73 return;
74 }
75
SecondConnectSuccessedCB(unsigned int requestId,unsigned int connectionId,const ConnectionInfo * info)76 void SecondConnectSuccessedCB(unsigned int requestId, unsigned int connectionId, const ConnectionInfo *info)
77 {
78 g_secondConnId = connectionId;
79 return;
80 }
81
ConnectFailedCB(unsigned int requestId,int reason)82 void ConnectFailedCB(unsigned int requestId, int reason)
83 {
84 (void)requestId;
85 (void)reason;
86 printf("DconConnectFailed\r\n");
87 return;
88 }
89
90 class ConnectionBrTest : public testing::Test {
91 public:
ConnectionBrTest()92 ConnectionBrTest()
93 {}
~ConnectionBrTest()94 ~ConnectionBrTest()
95 {}
96 static void SetUpTestCase(void);
97 static void TearDownTestCase(void);
98 void SetUp();
99 void TearDown();
100 };
101
SetUpTestCase(void)102 void ConnectionBrTest::SetUpTestCase(void)
103 {
104 LooperInit();
105 SoftbusConfigInit();
106 ConnServerInit();
107 }
108
TearDownTestCase(void)109 void ConnectionBrTest::TearDownTestCase(void)
110 {
111 LooperDeinit();
112 }
113
SetUp(void)114 void ConnectionBrTest::SetUp(void)
115 {}
116
TearDown(void)117 void ConnectionBrTest::TearDown(void)
118 {}
119
GetBrConnStateByConnectionId(uint32_t connectId)120 int32_t GetBrConnStateByConnectionId(uint32_t connectId)
121 {
122 (void)connectId;
123 return BR_CONNECTION_STATE_CLOSED;
124 }
125
126 /*
127 * @tc.name: testConnmanger001
128 * @tc.desc: test ConnTypeIsSupport
129 * @tc.type: FUNC
130 * @tc.require:
131 */
132 HWTEST_F(ConnectionBrTest, testConnmanger001, TestSize.Level1)
133 {
134 int ret;
135 printf("testConnmanger001\r\n");
136
137 ret = ConnTypeIsSupport(CONNECT_BR);
138 EXPECT_EQ(SOFTBUS_OK, ret);
139 };
140
141 /*
142 * @tc.name: testConnmanger002
143 * @tc.desc: test invalid param
144 * @tc.in: test module, test number, Test Levels.
145 * @tc.out: zero
146 * @tc.type: FUNC
147 * @tc.require:AR000GIIE9
148 */
149 HWTEST_F(ConnectionBrTest, testConnmanger002, TestSize.Level1)
150 {
151 printf("test begin testConnmanger002 \r\n");
152 int ret;
153 ret = ConnSetConnectCallback(static_cast<ConnModule>(0), nullptr);
154 ASSERT_TRUE(ret != SOFTBUS_OK);
155 ret = ConnConnectDevice(nullptr, 0, nullptr);
156 ASSERT_TRUE(ret != SOFTBUS_OK);
157 ret = ConnPostBytes(0, nullptr);
158 ASSERT_TRUE(ret != SOFTBUS_OK);
159 ret = ConnStartLocalListening(nullptr);
160 ASSERT_TRUE(ret != SOFTBUS_OK);
161 ret = ConnStopLocalListening(nullptr);
162 ASSERT_TRUE(ret != SOFTBUS_OK);
163 };
164
165 /*
166 * @tc.name: testConnmanger003
167 * @tc.desc: test set unset callback and connect post disconnect and multiple disconnects.
168 * @tc.type: FUNC
169 * @tc.require:AR000GIRGE
170 */
171 HWTEST_F(ConnectionBrTest, testConnmanger003, TestSize.Level1)
172 {
173 int ret;
174 int reqId;
175 ConnectCallback connCb;
176 ConnectResult connRet;
177 ConnPostData data;
178 ConnectOption info;
179 const char *str = "send msg local2\r\n";
180 printf("test begin testConnmanger003 \r\n");
181
182 connCb.OnConnected = ConnectedCB;
183 connCb.OnDisconnected = DisConnectCB;
184 connCb.OnDataReceived = DataReceivedCB;
185 ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
186 EXPECT_EQ(SOFTBUS_OK, ret);
187 info.type = CONNECT_BR;
188 (void)memcpy_s(info.brOption.brMac, BT_MAC_LEN, TEST_BR_MAC, BT_MAC_LEN);
189 printf("brMac: %s\n", info.brOption.brMac);
190 connRet.OnConnectFailed = ConnectFailedCB;
191 connRet.OnConnectSuccessed = ConnectSuccessedCB;
192 reqId = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
193 ret = ConnConnectDevice(&info, reqId, &connRet);
194 EXPECT_EQ(SOFTBUS_OK, ret);
195 if (g_connId) {
196 data.buf = (char *)calloc(1, CONN_HEAD_SIZE + 20);
197 ASSERT_TRUE(data.buf != NULL);
198 (void)strcpy_s(data.buf + 1, strlen(str), str);
199 data.len = CONN_HEAD_SIZE + 20;
200 data.module = MODULE_TRUST_ENGINE;
201 data.pid = 0;
202 data.flag = 1;
203 data.seq = 1;
204 ret = ConnPostBytes(g_connId, &data);
205 EXPECT_EQ(SOFTBUS_OK, ret);
206 if (data.buf != nullptr) {
207 free(data.buf);
208 }
209 ret = ConnDisconnectDevice(g_connId);
210 EXPECT_EQ(SOFTBUS_OK, ret);
211 ret = ConnDisconnectDevice(g_connId);
212 EXPECT_EQ(SOFTBUS_CONN_MANAGER_TYPE_NOT_SUPPORT, ret);
213 }
214
215 ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
216 g_connId = 0;
217 };
218
219 /*
220 * @tc.name: testConnmanger004
221 * @tc.desc: test set unset callback and post disconnect without connect
222 * @tc.type: FUNC
223 * @tc.require:AR000GIIE9
224 */
225 HWTEST_F(ConnectionBrTest, testConnmanger004, TestSize.Level1)
226 {
227 printf("test begin ConnManagerTest004 \r\n");
228 int ret;
229 ConnectCallback connCb;
230 LocalListenerInfo info;
231
232 connCb.OnConnected = ConnectedCB;
233 connCb.OnDisconnected = DisConnectCB;
234 connCb.OnDataReceived = DataReceivedCB;
235 ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
236 EXPECT_EQ(SOFTBUS_OK, ret);
237 info.type = CONNECT_BR;
238 ret = ConnStartLocalListening(&info);
239 EXPECT_EQ(SOFTBUS_OK, ret);
240 ret = ConnStopLocalListening(&info);
241 EXPECT_EQ(SOFTBUS_OK, ret);
242 ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
243 g_connId = 0;
244 };
245
246 /*
247 * @tc.name: testConnmanger005
248 * @tc.desc: The test set unsets the return value of callback and disconnection after connection.
249 * @tc.type: FUNC
250 * @tc.require:AR000GIRGE
251 */
252 HWTEST_F(ConnectionBrTest, testConnmanger005, TestSize.Level1)
253 {
254 int reqId = 1;
255 int ret;
256 ConnectCallback connCb;
257 ConnectOption optionInfo;
258 ConnectionInfo info;
259 ConnectResult connRet;
260
261 connCb.OnConnected = ConnectedCB;
262 connCb.OnDisconnected = DisConnectCB;
263 connCb.OnDataReceived = DataReceivedCB;
264 ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
265 EXPECT_EQ(SOFTBUS_OK, ret);
266
267 optionInfo.type = CONNECT_BR;
268 (void)memcpy_s(optionInfo.brOption.brMac, BT_MAC_LEN, TEST_BR_MAC, BT_MAC_LEN);
269 printf("brMac: %s\n", optionInfo.brOption.brMac);
270 connRet.OnConnectFailed = ConnectFailedCB;
271 connRet.OnConnectSuccessed = ConnectSuccessedCB;
272 reqId = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
273 ret = ConnConnectDevice(&optionInfo, reqId, &connRet);
274 EXPECT_EQ(SOFTBUS_OK, ret);
275 if (g_connId) {
276 ret = ConnGetConnectionInfo(g_connId, &info);
277 EXPECT_EQ(SOFTBUS_OK, ret);
278 ret = ConnDisconnectDeviceAllConn(&optionInfo);
279 g_connId = 0;
280 EXPECT_EQ(SOFTBUS_OK, ret);
281 printf("testConnmanger005 ConnDisconnectDevice\r\n");
282 }
283 printf("testConnmanger005 ConnUnSetConnectCallback\r\n");
284 ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
285 printf("testConnmanger005 ConnUnSetConnectCallback end 11\r\n");
286 };
287
288 /*
289 * @tc.name: testConnmanger006
290 * @tc.desc: Test set unset callback.
291 * @tc.in: Test module, Test number, Test Levels.
292 * @tc.out: NA
293 * @tc.type: FUNC
294 * @tc.require: The ConnSetConnectCallback operates normally.
295 */
296 HWTEST_F(ConnectionBrTest, testConnmanger006, TestSize.Level1)
297 {
298 int ret;
299 ConnectCallback connCb;
300
301 connCb.OnConnected = ConnectedCB;
302 connCb.OnConnected = DisConnectCB;
303 connCb.OnDataReceived = DataReceivedCB;
304 ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
305 EXPECT_NE(SOFTBUS_OK, ret);
306 ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
307 g_connId = 0;
308 };
309
310 /*
311 * @tc.name: testConnmanger007
312 * @tc.desc: Test set unset callback and connect post disconnect.
313 * @tc.in: Test module, Test number, Test Levels.
314 * @tc.out: Zero
315 * @tc.type: FUNC
316 * @tc.require: The ConnSetConnectCallback and ConnDisconnectDevice
317 * and ConnDisconnectDevice and ConnPostBytes and operates normally.
318 */
319 HWTEST_F(ConnectionBrTest, testConnmanger007, TestSize.Level1)
320 {
321 int ret;
322 int reqId;
323 ConnectCallback connCb;
324 ConnectResult connRet;
325 ConnPostData data;
326 ConnectOption info;
327 const char *str = "send msg local2\r\n";
328
329 connCb.OnConnected = ConnectedCB;
330 connCb.OnDisconnected = DisConnectCB;
331 connCb.OnDataReceived = DataReceivedCB;
332 ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
333 EXPECT_EQ(SOFTBUS_OK, ret);
334 info.type = CONNECT_BR;
335 (void)memcpy_s(info.brOption.brMac, BT_MAC_LEN, TEST_BR_MAC, BT_MAC_LEN);
336 connRet.OnConnectFailed = ConnectFailedCB;
337 connRet.OnConnectSuccessed = ConnectSuccessedCB;
338 reqId = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
339 ret = ConnConnectDevice(&info, reqId, &connRet);
340 EXPECT_EQ(SOFTBUS_OK, ret);
341 if (g_connId != 0) {
342 data.buf = (char *)calloc(1, CONN_HEAD_SIZE + 20);
343 ASSERT_TRUE(data.buf != NULL);
344 (void)strcpy_s(data.buf + 1, strlen(str), str);
345 data.len = CONN_HEAD_SIZE + 20;
346 data.module = MODULE_TRUST_ENGINE;
347 data.pid = 0;
348 data.flag = 1;
349 data.seq = 1;
350 ret = ConnPostBytes(g_connId, &data);
351 EXPECT_EQ(SOFTBUS_OK, ret);
352 if (data.buf != nullptr) {
353 free(data.buf);
354 }
355 ret = ConnDisconnectDevice(g_connId);
356 EXPECT_EQ(SOFTBUS_OK, ret);
357 }
358
359 ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
360 g_connId = 0;
361 };
362
363 /*
364 * @tc.name: testConnmanger008
365 * @tc.desc: Test set unset callback and connect twice has same ConnectID.
366 * @tc.in: Test module, Test number, Test Levels.
367 * @tc.out: Zero
368 * @tc.type: FUNC
369 * @tc.require: The ConnSetConnectCallback and ConnConnectDevice operates normally.
370 */
371 HWTEST_F(ConnectionBrTest, testConnmanger008, TestSize.Level1)
372 {
373 int ret;
374 ConnectCallback connCb;
375 ConnectOption optionInfo;
376 ConnectionInfo info;
377 ConnectResult connRet;
378 ConnectResult connRet2;
379
380 connCb.OnConnected = ConnectedCB;
381 connCb.OnDisconnected = DisConnectCB;
382 connCb.OnDataReceived = DataReceivedCB;
383 ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
384 EXPECT_EQ(SOFTBUS_OK, ret);
385
386 optionInfo.type = CONNECT_BR;
387 (void)memcpy_s(optionInfo.brOption.brMac, BT_MAC_LEN, TEST_BR_MAC, BT_MAC_LEN);
388 connRet.OnConnectFailed = ConnectFailedCB;
389 connRet.OnConnectSuccessed = ConnectSuccessedCB;
390 int reqId = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
391 ret = ConnConnectDevice(&optionInfo, reqId, &connRet);
392
393 connRet2.OnConnectFailed = ConnectFailedCB;
394 connRet2.OnConnectSuccessed = SecondConnectSuccessedCB;
395 int reqId2 = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
396 ret = ConnConnectDevice(&optionInfo, reqId2, &connRet2);
397 EXPECT_EQ(SOFTBUS_OK, ret);
398 sleep(1);
399 if ((g_connId) && (g_secondConnId)) {
400 EXPECT_EQ(g_connId, g_secondConnId);
401 }
402
403 if (g_connId) {
404 ret = ConnGetConnectionInfo(g_connId, &info);
405 EXPECT_EQ(SOFTBUS_OK, ret);
406 ret = ConnDisconnectDeviceAllConn(&optionInfo);
407 g_connId = 0;
408 EXPECT_EQ(SOFTBUS_OK, ret);
409 }
410 ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
411 };
412
413 /*
414 * @tc.name: testConnmanger009
415 * @tc.desc: Test set unset callback and connect twice post disconnect post.
416 * @tc.in: Test module, Test number, Test Levels.
417 * @tc.out: Zero
418 * @tc.type: FUNC
419 * @tc.require: The ConnSetConnectCallback and ConnConnectDevice operates normally.
420 */
421 HWTEST_F(ConnectionBrTest, testConnmanger009, TestSize.Level1)
422 {
423 int ret;
424 ConnectCallback connCb;
425 ConnectResult connRet;
426 ConnPostData data;
427 ConnectOption info;
428 const char *str = "send msg local2\r\n";
429
430 connCb.OnConnected = ConnectedCB;
431 connCb.OnDisconnected = DisConnectCB;
432 connCb.OnDataReceived = DataReceivedCB;
433 ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
434 EXPECT_EQ(SOFTBUS_OK, ret);
435 info.type = CONNECT_BR;
436 (void)memcpy_s(info.brOption.brMac, BT_MAC_LEN, TEST_BR_MAC, BT_MAC_LEN);
437 printf("brMac: %s\n", info.brOption.brMac);
438 connRet.OnConnectFailed = ConnectFailedCB;
439 connRet.OnConnectSuccessed = ConnectSuccessedCB;
440 int reqId1 = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
441 ret = ConnConnectDevice(&info, reqId1, &connRet);
442 int reqId2 = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
443 ret = ConnConnectDevice(&info, reqId2, &connRet);
444 EXPECT_EQ(SOFTBUS_OK, ret);
445 if (g_connId != 0) {
446 data.buf = (char *)calloc(1, CONN_HEAD_SIZE + 20);
447 ASSERT_TRUE(data.buf != NULL);
448 (void)strcpy_s(data.buf + 1, strlen(str), str);
449 data.len = CONN_HEAD_SIZE + 20;
450 data.module = MODULE_TRUST_ENGINE;
451 data.pid = 0;
452 data.flag = 1;
453 data.seq = 1;
454 ret = ConnPostBytes(g_connId, &data);
455 EXPECT_EQ(SOFTBUS_OK, ret);
456
457 ret = ConnDisconnectDevice(g_connId);
458 EXPECT_EQ(SOFTBUS_OK, ret);
459
460 ret = ConnPostBytes(g_connId, &data);
461 ASSERT_EQ(SOFTBUS_OK, ret);
462 if (data.buf != nullptr) {
463 free(data.buf);
464 }
465 }
466 ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
467 g_connId = 0;
468 };
469
470 /*
471 * @tc.name: testConnmanger010
472 * @tc.desc: Test set unset callback and connect twice post disconnectAll post.
473 * @tc.in: Test module, Test number, Test Levels.
474 * @tc.out: Zero
475 * @tc.type: FUNC
476 * @tc.require: The ConnSetConnectCallback and ConnConnectDevice operates normally.
477 */
478 HWTEST_F(ConnectionBrTest, testConnmanger010, TestSize.Level1)
479 {
480 int ret;
481 ConnectCallback connCb;
482 ConnectResult connRet;
483 ConnPostData data;
484 ConnectOption info;
485 const char *str = "send msg local2\r\n";
486
487 connCb.OnConnected = ConnectedCB;
488 connCb.OnDisconnected = DisConnectCB;
489 connCb.OnDataReceived = DataReceivedCB;
490 ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
491 EXPECT_EQ(SOFTBUS_OK, ret);
492 info.type = CONNECT_BR;
493 (void)memcpy_s(info.brOption.brMac, BT_MAC_LEN, TEST_BR_MAC, BT_MAC_LEN);
494 connRet.OnConnectFailed = ConnectFailedCB;
495 connRet.OnConnectSuccessed = ConnectSuccessedCB;
496
497 int reqId1 = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
498 ret = ConnConnectDevice(&info, reqId1, &connRet);
499 EXPECT_EQ(SOFTBUS_OK, ret);
500 int reqId2 = ConnGetNewRequestId(MODULE_TRUST_ENGINE);
501 ret = ConnConnectDevice(&info, reqId2, &connRet);
502 EXPECT_EQ(SOFTBUS_OK, ret);
503
504 if (g_connId != 0) {
505 data.buf = (char *)calloc(1, CONN_HEAD_SIZE + 20);
506 ASSERT_TRUE(data.buf != NULL);
507 (void)strcpy_s(data.buf + 1, strlen(str), str);
508 data.len = CONN_HEAD_SIZE + 20;
509 data.module = MODULE_TRUST_ENGINE;
510 data.pid = 0;
511 data.flag = 1;
512 data.seq = 1;
513
514 ret = ConnPostBytes(g_connId, &data);
515 ASSERT_EQ(SOFTBUS_OK, ret);
516 ret = ConnDisconnectDeviceAllConn(&info);
517 EXPECT_EQ(SOFTBUS_OK, ret);
518 ret = ConnPostBytes(g_connId, &data);
519 ASSERT_NE(SOFTBUS_OK, ret);
520
521 g_connId = 0;
522 if (data.buf != nullptr) {
523 free(data.buf);
524 }
525 }
526 ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
527 };
528
529 HWTEST_F(ConnectionBrTest, testBrPendingPacket001, TestSize.Level1)
530 {
531 int ret;
532 ConnectOption info;
533 uint32_t time = 1;
534
535 ret = ConnPreventConnection(NULL, time);
536 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
537
538 time = 0;
539 ret = ConnPreventConnection(&info, time);
540 EXPECT_EQ(SOFTBUS_CONN_MANAGER_TYPE_NOT_SUPPORT, ret);
541
542 time = 30000;
543 ret = ConnPreventConnection(&info, time);
544 EXPECT_EQ(SOFTBUS_CONN_MANAGER_TYPE_NOT_SUPPORT, ret);
545
546 time = 1;
547 info.type = CONNECT_BLE;
548 ret = ConnPreventConnection(&info, time);
549 EXPECT_EQ(SOFTBUS_CONN_MANAGER_OP_NOT_SUPPORT, ret);
550 }
551
552 HWTEST_F(ConnectionBrTest, testBrPendingPacket002, TestSize.Level1)
553 {
554 int ret;
555 ConnectOption info;
556 uint32_t time = 1;
557
558 info.type = CONNECT_BR;
559 (void)memcpy_s(info.brOption.brMac, BT_MAC_LEN, TEST_BR_MAC, BT_MAC_LEN);
560 ret = ConnPreventConnection(&info, time);
561 EXPECT_EQ(SOFTBUS_OK, ret);
562 }
563
564 HWTEST_F(ConnectionBrTest, CheckActiveConnection001, TestSize.Level1)
565 {
566 ConnectOption info;
567
568 info.type = CONNECT_BR;
569 info.brOption.sideType = CONN_SIDE_ANY;
570 (void)memcpy_s(info.brOption.brMac, BT_MAC_LEN, TEST_BR_MAC, BT_MAC_LEN);
571 int32_t ret = CheckActiveConnection(&info, false);
572 EXPECT_EQ(false, ret);
573 }
574
575 HWTEST_F(ConnectionBrTest, CheckActiveConnection002, TestSize.Level1)
576 {
577 ConnBrConnection *connection = (ConnBrConnection *)SoftBusCalloc(sizeof(ConnBrConnection));
578 ASSERT_NE(nullptr, connection);
579 int32_t ret = SoftBusMutexInit(&connection->lock, NULL);
580 ASSERT_EQ(EOK, ret);
581 ret = strcpy_s(connection->addr, BT_MAC_LEN, TEST_BR_MAC);
582 EXPECT_EQ(EOK, ret);
583 ret = ConnBrSaveConnection(connection);
584 EXPECT_EQ(EOK, ret);
585 ConnectOption info;
586 info.type = CONNECT_BR;
587 info.brOption.sideType = CONN_SIDE_ANY;
588 (void)memcpy_s(info.brOption.brMac, BT_MAC_LEN, TEST_BR_MAC, BT_MAC_LEN);
589 ret = CheckActiveConnection(&info, false);
590 EXPECT_EQ(false, ret);
591 }
592
593 HWTEST_F(ConnectionBrTest, testBrPendingPacket004, TestSize.Level1)
594 {
595 int ret;
596 uint32_t id = 1;
597 ConnectionInfo info;
598
599 ret = ConnGetConnectionInfo(id, NULL);
600 EXPECT_EQ(SOFTBUS_CONN_MANAGER_TYPE_NOT_SUPPORT, ret);
601
602 id = 0;
603 ret = ConnGetConnectionInfo(id, &info);
604 EXPECT_EQ(SOFTBUS_CONN_MANAGER_TYPE_NOT_SUPPORT, ret);
605 }
606
607 HWTEST_F(ConnectionBrTest, testBrPendingPacket005, TestSize.Level1)
608 {
609 int ret;
610 uint32_t id = 0x20000;
611 ConnectOption info;
612
613 info.type = CONNECT_BR;
614 (void)memcpy_s(info.brOption.brMac, BT_MAC_LEN, TEST_BR_MAC, BT_MAC_LEN);
615 printf("brMac: %s\n", info.brOption.brMac);
616 ConnBrConnection *connection = ConnBrCreateConnection(TEST_BR_MAC, CONN_SIDE_CLIENT, INVALID_SOCKET_HANDLE);
617 ConnBrSaveConnection(connection);
618 ret = ConnDisconnectDeviceAllConn(&info);
619 EXPECT_EQ(SOFTBUS_OK, ret);
620
621 usleep(50);
622 ret = ConnDisconnectDevice(id);
623 EXPECT_EQ(SOFTBUS_CONN_BR_CONNECTION_NOT_EXIST_ERR, ret);
624 }
625
626 HWTEST_F(ConnectionBrTest, testBrPendingPacket006, TestSize.Level1)
627 {
628 int ret;
629 uint32_t id = 1;
630 int64_t seq = 1000;
631 uint32_t waitMillis = 1000;
632 void *data = nullptr;
633
634 ret = ConnBrInitBrPendingPacket();
635 EXPECT_EQ(SOFTBUS_OK, ret);
636
637 ret = ConnBrCreateBrPendingPacket(id, seq);
638 EXPECT_EQ(SOFTBUS_OK, ret);
639
640 ret = ConnBrCreateBrPendingPacket(id, seq);
641 EXPECT_EQ(SOFTBUS_ALREADY_EXISTED, ret);
642
643 ret = ConnBrGetBrPendingPacket(id, seq, waitMillis, &data);
644 EXPECT_EQ(SOFTBUS_TIMOUT, ret);
645
646 ConnBrDelBrPendingPacket(id, seq);
647 ret = ConnBrGetBrPendingPacket(id, seq, waitMillis, &data);
648 EXPECT_EQ(SOFTBUS_NOT_FIND, ret);
649
650 ret = ConnBrCreateBrPendingPacket(id, seq);
651 EXPECT_EQ(SOFTBUS_OK, ret);
652 ret = ConnBrSetBrPendingPacket(id, seq, data);
653 EXPECT_EQ(SOFTBUS_OK, ret);
654
655 ret = ConnBrGetBrPendingPacket(id, seq, waitMillis, &data);
656 EXPECT_EQ(SOFTBUS_ALREADY_TRIGGERED, ret);
657
658 ConnBrDelBrPendingPacket(id, seq);
659 ret = ConnBrSetBrPendingPacket(id, seq, data);
660 EXPECT_EQ(SOFTBUS_CONN_BR_SET_PENDING_PACKET_ERR, ret);
661 }
662
663 HWTEST_F(ConnectionBrTest, testBrPendingPacket007, TestSize.Level1)
664 {
665 int ret;
666 ConnBrConnection connection;
667
668 NiceMock<ConnectionBrInterfaceMock> brMock;
669 EXPECT_CALL(brMock, GetJsonObjectSignedNumberItem).WillRepeatedly(Return(false));
670 connection.connectionId = 1;
671 ret = ConnBrOnAckRequest(&connection, NULL);
672 EXPECT_EQ(SOFTBUS_PARSE_JSON_ERR, ret);
673
674
675 EXPECT_CALL(brMock, GetJsonObjectSignedNumberItem).WillRepeatedly(Return(true));
676 EXPECT_CALL(brMock, GetJsonObjectNumber64Item).WillRepeatedly(Return(true));
677 ret = ConnBrOnAckRequest(&connection, NULL);
678 EXPECT_EQ(SOFTBUS_LOCK_ERR, ret);
679
680 EXPECT_CALL(brMock, AddNumberToJsonObject).WillRepeatedly(Return(false));
681 SoftBusMutexInit(&connection.lock, NULL);
682 connection.connectionId = 0;
683 ret = ConnBrOnAckRequest(&connection, NULL);
684 EXPECT_EQ(SOFTBUS_CREATE_JSON_ERR, ret);
685
686 EXPECT_CALL(brMock, AddNumberToJsonObject).WillRepeatedly(Return(true));
687 EXPECT_CALL(brMock, AddNumber64ToJsonObject).WillRepeatedly(Return(true));
688 SoftBusMutexInit(&connection.lock, NULL);
689 connection.connectionId = 0;
690 ret = ConnBrOnAckRequest(&connection, NULL);
691 EXPECT_EQ(SOFTBUS_CONN_BR_CONNECTION_NOT_EXIST_ERR, ret);
692 }
693
694 HWTEST_F(ConnectionBrTest, testBrPendingPacket008, TestSize.Level1)
695 {
696 int ret;
697 uint32_t id = 1;
698 int64_t seq = 1000;
699 void *data = nullptr;
700 ConnBrConnection connection;
701
702 NiceMock<ConnectionBrInterfaceMock> brMock;
703 EXPECT_CALL(brMock, GetJsonObjectSignedNumberItem).WillRepeatedly(Return(false));
704 connection.connectionId = 1;
705 ret = ConnBrOnAckResponse(&connection, NULL);
706 EXPECT_EQ(SOFTBUS_PARSE_JSON_ERR, ret);
707
708 EXPECT_CALL(brMock, GetJsonObjectSignedNumberItem).WillRepeatedly(Return(true));
709 EXPECT_CALL(brMock, GetJsonObjectNumber64Item).WillRepeatedly(Return(true));
710 ConnBrDelBrPendingPacket(id, seq);
711 ret = ConnBrSetBrPendingPacket(id, seq, data);
712 ret = ConnBrOnAckResponse(&connection, NULL);
713 EXPECT_EQ(SOFTBUS_CONN_BR_SET_PENDING_PACKET_ERR, ret);
714 }
715
716 HWTEST_F(ConnectionBrTest, testBrQueue001, TestSize.Level1)
717 {
718 int ret;
719 SendBrQueueNode queueNode;
720
721 ret = ConnBrInnerQueueInit();
722 EXPECT_EQ(SOFTBUS_OK, ret);
723
724 ret = ConnBrEnqueueNonBlock(NULL);
725 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
726
727 queueNode.flag = CONN_HIGH;
728 queueNode.pid = 0;
729 queueNode.isInner = 1;
730 ret = ConnBrEnqueueNonBlock(&queueNode);
731 EXPECT_EQ(SOFTBUS_OK, ret);
732
733 queueNode.flag = CONN_MIDDLE;
734 queueNode.pid = 1;
735 queueNode.isInner = 1;
736 ret = ConnBrEnqueueNonBlock(&queueNode);
737 EXPECT_EQ(SOFTBUS_OK, ret);
738
739 queueNode.flag = CONN_LOW;
740 queueNode.pid = 1;
741 queueNode.isInner = 1;
742 ret = ConnBrEnqueueNonBlock(&queueNode);
743 EXPECT_EQ(SOFTBUS_OK, ret);
744
745 ConnBrInnerQueueDeinit();
746 }
747
748 HWTEST_F(ConnectionBrTest, testBrQueue002, TestSize.Level1)
749 {
750 int ret;
751 void *msg = nullptr;
752 SendBrQueueNode queueNode;
753
754 ret = ConnBrInnerQueueInit();
755 EXPECT_EQ(SOFTBUS_OK, ret);
756
757 queueNode.flag = CONN_LOW;
758 queueNode.pid = 1;
759 queueNode.isInner = 1;
760 ret = ConnBrEnqueueNonBlock(&queueNode);
761 EXPECT_EQ(SOFTBUS_OK, ret);
762 ret = ConnBrDequeueBlock(&msg);
763 EXPECT_EQ(SOFTBUS_OK, ret);
764
765 ret = ConnBrDequeueBlock(NULL);
766 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
767 }
768
769 HWTEST_F(ConnectionBrTest, testBrBrans001, TestSize.Level1)
770 {
771 int ret;
772 uint32_t connectionId;
773 int32_t socketHandle = 0;
774 LimitedBuffer buffer;
775 uint8_t *outData = nullptr;
776 ConnPktHead head;
777
778 head.magic = MAGIC_NUMBER;
779 head.len = 70;
780 buffer.capacity = 140;
781 buffer.length = 100;
782 buffer.buffer = (uint8_t *)(&head);
783 connectionId = 1;
784 ret = ConnBrTransReadOneFrame(connectionId, socketHandle, &buffer, &outData);
785 EXPECT_NE(SOFTBUS_OK, ret);
786
787 head.magic = MAGIC_NUMBER + 1;
788 head.len = 70;
789 buffer.capacity = 140;
790 buffer.length = 100;
791 buffer.buffer = (uint8_t *)(&head);
792 connectionId = 1;
793 ret = ConnBrTransReadOneFrame(connectionId, socketHandle, &buffer, &outData);
794 EXPECT_EQ(SOFTBUS_CONN_BR_UNDERLAY_READ_FAIL, ret);
795 }
796
797 HWTEST_F(ConnectionBrTest, testBrBrans002, TestSize.Level1)
798 {
799 int ret;
800 uint32_t connectionId;
801 int32_t socketHandle = 0;
802 LimitedBuffer buffer;
803 uint8_t *outData = nullptr;
804 ConnPktHead head;
805
806 head.magic = MAGIC_NUMBER;
807 head.len = 70;
808 buffer.capacity = 70;
809 buffer.length = 100;
810 buffer.buffer = (uint8_t *)(&head);
811 connectionId = 1;
812 ret = ConnBrTransReadOneFrame(connectionId, socketHandle, &buffer, &outData);
813 EXPECT_EQ(SOFTBUS_CONN_BR_UNDERLAY_READ_FAIL, ret);
814 }
815
816 HWTEST_F(ConnectionBrTest, testBrBrans003, TestSize.Level1)
817 {
818 int ret;
819 uint32_t connectionId;
820 int32_t socketHandle = 0;
821 LimitedBuffer buffer;
822 uint8_t *outData = nullptr;
823 ConnPktHead head;
824
825 head.magic = MAGIC_NUMBER;
826 head.len = 70;
827 buffer.capacity = 140;
828 buffer.length = 90;
829 buffer.buffer = (uint8_t *)(&head);
830 connectionId = 1;
831 ret = ConnBrTransReadOneFrame(connectionId, socketHandle, &buffer, &outData);
832 EXPECT_EQ(SOFTBUS_CONN_BR_UNDERLAY_READ_FAIL, ret);
833 }
834
835 HWTEST_F(ConnectionBrTest, testBrBrans004, TestSize.Level1)
836 {
837 int64_t ret;
838 BrCtlMessageSerializationContext ctx;
839 uint8_t *outData = nullptr;
840 uint32_t outDataLen;
841 NiceMock<ConnectionBrInterfaceMock> brMock;
842
843 EXPECT_CALL(brMock, AddNumberToJsonObject).WillRepeatedly(Return(true));
844 EXPECT_CALL(brMock, AddNumber64ToJsonObject).WillRepeatedly(Return(true));
845 ctx.method = BR_METHOD_ACK_RESPONSE;
846 ctx.connectionId = 1;
847 ret = ConnBrPackCtlMessage(ctx, &outData, &outDataLen);
848 EXPECT_NE(SOFTBUS_OK, ret);
849
850 EXPECT_CALL(brMock, AddNumberToJsonObject).WillRepeatedly(Return(false));
851 ctx.method = BR_METHOD_NOTIFY_REQUEST;
852 ctx.connectionId = 1;
853 ret = ConnBrPackCtlMessage(ctx, &outData, &outDataLen);
854 EXPECT_EQ(SOFTBUS_CREATE_JSON_ERR, ret);
855
856 EXPECT_CALL(brMock, AddNumberToJsonObject).WillRepeatedly(Return(false));
857 ctx.method = BR_METHOD_NOTIFY_RESPONSE;
858 ctx.connectionId = 1;
859 ret = ConnBrPackCtlMessage(ctx, &outData, &outDataLen);
860 EXPECT_EQ(SOFTBUS_CREATE_JSON_ERR, ret);
861
862 EXPECT_CALL(brMock, AddNumberToJsonObject).WillRepeatedly(Return(false));
863 ctx.method = BR_METHOD_NOTIFY_ACK;
864 ctx.connectionId = 1;
865 ret = ConnBrPackCtlMessage(ctx, &outData, &outDataLen);
866 EXPECT_EQ(SOFTBUS_CREATE_JSON_ERR, ret);
867
868 EXPECT_CALL(brMock, AddNumberToJsonObject).WillRepeatedly(Return(false));
869 ctx.method = BR_METHOD_ACK_RESPONSE;
870 ctx.connectionId = 1;
871 ret = ConnBrPackCtlMessage(ctx, &outData, &outDataLen);
872 EXPECT_EQ(SOFTBUS_CREATE_JSON_ERR, ret);
873
874 EXPECT_CALL(brMock, AddNumberToJsonObject).WillRepeatedly(Return(false));
875 ctx.method = (enum BrCtlMessageMethod)3;
876 ctx.connectionId = 1;
877 ret = ConnBrPackCtlMessage(ctx, &outData, &outDataLen);
878 EXPECT_EQ(SOFTBUS_CONN_BR_INTERNAL_ERR, ret);
879 }
880
881 HWTEST_F(ConnectionBrTest, testBrBrans005, TestSize.Level1)
882 {
883 int ret;
884 uint32_t connectionId = 1;
885 uint8_t *data = nullptr;
886 uint32_t len = 0;
887 int32_t pid = 0;
888 int32_t flag = 0;
889 int32_t module = 0;
890 int64_t seq = 0;
891
892 ret = ConnBrPostBytes(connectionId, data, len, pid, flag, module, seq);
893 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
894
895 len = MAX_DATA_LEN + 1;
896 data = static_cast<uint8_t *>(SoftBusCalloc(len));
897 ret = ConnBrPostBytes(connectionId, data, len, pid, flag, module, seq);
898 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
899
900 len = MAX_DATA_LEN;
901 data = static_cast<uint8_t *>(SoftBusCalloc(len));
902 connectionId = 0x20005;
903 ret = ConnBrPostBytes(connectionId, data, len, pid, flag, module, seq);
904 EXPECT_EQ(SOFTBUS_CONN_BR_CONNECTION_NOT_EXIST_ERR, ret);
905 }
906 }
907