• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_br_manager_mock.h"
24 #include "softbus_adapter_mem.h"
25 #include "softbus_conn_br_connection.c"
26 #include "softbus_conn_br_manager.c"
27 #include "softbus_conn_br_pending_packet.h"
28 #include "softbus_conn_br_send_queue.h"
29 #include "softbus_conn_interface.h"
30 #include "softbus_conn_manager.h"
31 #include "softbus_error_code.h"
32 #include "softbus_feature_config.c"
33 #include "softbus_feature_config.h"
34 
35 #define SOFTBUS_CHARA_CONN_UUID "00002B01-0000-1000-8000-00805F9B34FB"
36 #define DATASIZE                256
37 
38 using namespace testing::ext;
39 using namespace testing;
40 
41 namespace OHOS {
GetRemoteDeviceInfo(int32_t clientFd,const BluetoothRemoteDevice * device)42 int32_t GetRemoteDeviceInfo(int32_t clientFd, const BluetoothRemoteDevice *device)
43 {
44     (void)device;
45     return clientFd;
46 }
47 
OnConnectFailed(uint32_t requestId,int32_t reason)48 void OnConnectFailed(uint32_t requestId, int32_t reason)
49 {
50     (void)requestId;
51     (void)reason;
52     return;
53 }
54 
OnConnectSuccessed(uint32_t requestId,uint32_t connectionId,const ConnectionInfo * info)55 void OnConnectSuccessed(uint32_t requestId, uint32_t connectionId, const ConnectionInfo *info)
56 {
57     (void)requestId;
58     (void)connectionId;
59     (void)info;
60     return;
61 }
62 
PostMessageDelay(const SoftBusLooper * looper,SoftBusMessage * msg,uint64_t delayMillis)63 void PostMessageDelay(const SoftBusLooper *looper, SoftBusMessage *msg, uint64_t delayMillis)
64 {
65     (void)looper;
66     (void)msg;
67     (void)delayMillis;
68     return;
69 }
70 
DeviceAction(ConnBrDevice * device,const char * anomizeAddress)71 int32_t DeviceAction(ConnBrDevice *device, const char *anomizeAddress)
72 {
73     return (int32_t)(device->state);
74 }
75 
OnReusedConnected(uint32_t connectionId,const ConnectionInfo * info)76 void OnReusedConnected(uint32_t connectionId, const ConnectionInfo *info)
77 {
78     (void)connectionId;
79     (void)info;
80 }
81 
OnConnected(uint32_t connectionId,const ConnectionInfo * info)82 void OnConnected(uint32_t connectionId, const ConnectionInfo *info)
83 {
84     (void)connectionId;
85     (void)info;
86     return;
87 }
88 
OnDisconnected(uint32_t connectionId,const ConnectionInfo * info)89 void OnDisconnected(uint32_t connectionId, const ConnectionInfo *info)
90 {
91     (void)connectionId;
92     (void)info;
93     return;
94 }
95 
OnDataReceived(uint32_t connectionId,ConnModule moduleId,int64_t seq,char * data,int32_t len)96 void OnDataReceived(uint32_t connectionId, ConnModule moduleId, int64_t seq, char *data, int32_t len)
97 {
98     (void)connectionId;
99     (void)moduleId;
100     (void)seq;
101     (void)data;
102     (void)len;
103 }
104 
MessageDelay(const SoftBusLooper * looper,SoftBusMessage * msg,uint64_t delayMillis)105 void MessageDelay(const SoftBusLooper *looper, SoftBusMessage *msg, uint64_t delayMillis)
106 {
107     (void)looper;
108     (void)msg;
109     (void)delayMillis;
110     return;
111 }
112 
RvMessageCustom(const SoftBusLooper * looper,const SoftBusHandler * handler,int32_t (* customFunc)(const SoftBusMessage *,void *),void * args)113 void RvMessageCustom(const SoftBusLooper *looper, const SoftBusHandler *handler,
114     int32_t (*customFunc)(const SoftBusMessage *, void *), void *args)
115 {
116     (void)looper;
117     (void)handler;
118     (void)customFunc;
119     (void)args;
120     return;
121 }
122 
handlePendingRequest(void)123 void handlePendingRequest(void)
124 {
125     return;
126 }
127 
connectRequest(const ConnBrConnectRequestContext * ctx)128 void connectRequest(const ConnBrConnectRequestContext *ctx)
129 {
130     (void)ctx;
131     return;
132 }
133 
clientConnected(uint32_t connectionId)134 void clientConnected(uint32_t connectionId)
135 {
136     (void)connectionId;
137     return;
138 }
139 
clientConnectTimeout(uint32_t connectionId,const char * address)140 void clientConnectTimeout(uint32_t connectionId, const char *address)
141 {
142     (void)connectionId;
143     (void)address;
144     return;
145 }
146 
clientConnectFailed(uint32_t connectionId,int32_t error)147 void clientConnectFailed(uint32_t connectionId, int32_t error)
148 {
149     (void)connectionId;
150     (void)error;
151     return;
152 }
153 
serverAccepted(uint32_t connectionId)154 void serverAccepted(uint32_t connectionId)
155 {
156     (void)connectionId;
157     return;
158 }
159 
dataReceived(ConnBrDataReceivedContext * ctx)160 void dataReceived(ConnBrDataReceivedContext *ctx)
161 {
162     (void)ctx;
163     return;
164 }
165 
connectionException(uint32_t connectionId,int32_t error)166 void connectionException(uint32_t connectionId, int32_t error)
167 {
168     (void)connectionId;
169     (void)error;
170     return;
171 }
172 
connectionResume(uint32_t connectionId)173 void connectionResume(uint32_t connectionId)
174 {
175     (void)connectionId;
176     return;
177 }
178 
disconnectRequest(uint32_t connectionId)179 void disconnectRequest(uint32_t connectionId)
180 {
181     (void)connectionId;
182     return;
183 }
Unpend(const char * addr)184 void Unpend(const char *addr)
185 {
186     (void)addr;
187     return;
188 }
189 
reset(int32_t reason)190 void reset(int32_t reason)
191 {
192     (void)reason;
193     return;
194 }
195 
BuildParam(ConnBrManager * manager)196 void BuildParam(ConnBrManager *manager)
197 {
198     manager->state->handlePendingRequest = handlePendingRequest;
199     manager->state->connectRequest = connectRequest;
200     manager->state->clientConnected = clientConnected;
201     manager->state->clientConnectTimeout = clientConnectTimeout;
202     manager->state->clientConnectFailed = clientConnectFailed;
203     manager->state->serverAccepted = serverAccepted;
204     manager->state->dataReceived = dataReceived;
205     manager->state->connectionException = connectionException;
206     manager->state->connectionResume = connectionResume;
207     manager->state->disconnectRequest = disconnectRequest;
208     manager->state->unpend = Unpend;
209     manager->state->reset = reset;
210 }
211 
212 class ConnectionBrConnectionTest : public testing::Test {
213 public:
ConnectionBrConnectionTest()214     ConnectionBrConnectionTest() { }
~ConnectionBrConnectionTest()215     ~ConnectionBrConnectionTest() { }
216     static void SetUpTestCase(void);
217     static void TearDownTestCase(void);
218     void SetUp();
219     void TearDown();
220 };
221 
SetUpTestCase(void)222 void ConnectionBrConnectionTest::SetUpTestCase(void)
223 {
224     LooperInit();
225     SoftbusConfigInit();
226     ConnServerInit();
227 }
228 
TearDownTestCase(void)229 void ConnectionBrConnectionTest::TearDownTestCase(void)
230 {
231     LooperDeinit();
232 }
233 
SetUp(void)234 void ConnectionBrConnectionTest::SetUp(void) { }
235 
TearDown(void)236 void ConnectionBrConnectionTest::TearDown(void) { }
237 
238 HWTEST_F(ConnectionBrConnectionTest, testBrConnection001, TestSize.Level1)
239 {
240     int32_t ret;
241     char mac[BT_MAC_LEN] = { 0 };
242     int32_t socketHandle = 111;
243     ConnBrConnection *connection = ConnBrCreateConnection(mac, CONN_SIDE_SERVER, socketHandle);
244     ConnBrSaveConnection(connection);
245     const cJSON *json = nullptr;
246     NiceMock<ConnectionBrInterfaceMock> brMock;
247 
248     EXPECT_CALL(brMock, GetJsonObjectSignedNumberItem).WillRepeatedly(Return(false));
249     ret = ConnBrOnReferenceRequest(connection, json);
250     EXPECT_EQ(SOFTBUS_PARSE_JSON_ERR, ret);
251 
252     EXPECT_CALL(brMock, GetJsonObjectSignedNumberItem).WillRepeatedly(Return(true));
253     EXPECT_CALL(brMock, AddNumberToJsonObject).WillRepeatedly(Return(false));
254     ret = ConnBrOnReferenceRequest(connection, json);
255     EXPECT_EQ(SOFTBUS_CREATE_JSON_ERR, ret);
256 
257     EXPECT_CALL(brMock, AddNumberToJsonObject).WillRepeatedly(Return(true));
258     EXPECT_CALL(brMock, AddNumber64ToJsonObject).WillRepeatedly(Return(true));
259     ret = ConnBrOnReferenceRequest(connection, json);
260     EXPECT_EQ(SOFTBUS_OK, ret);
261 }
262 
263 HWTEST_F(ConnectionBrConnectionTest, testBrConnection002, TestSize.Level1)
264 {
265     int32_t ret;
266     char mac[BT_MAC_LEN] = { 0 };
267     int32_t socketHandle = 222;
268     ConnBrConnection *connection = ConnBrCreateConnection(mac, CONN_SIDE_SERVER, socketHandle);
269     ConnBrSaveConnection(connection);
270     int32_t delta;
271     NiceMock<ConnectionBrInterfaceMock> brMock;
272 
273     EXPECT_CALL(brMock, AddNumberToJsonObject).WillRepeatedly(Return(false));
274     delta = 0;
275     ret = ConnBrUpdateConnectionRc(connection, delta);
276     EXPECT_EQ(SOFTBUS_CREATE_JSON_ERR, ret);
277 
278     EXPECT_CALL(brMock, AddNumberToJsonObject).WillRepeatedly(Return(true));
279     delta = 0;
280     ret = ConnBrUpdateConnectionRc(connection, delta);
281     EXPECT_EQ(SOFTBUS_OK, ret);
282 }
283 
284 HWTEST_F(ConnectionBrConnectionTest, testBrConnection003, TestSize.Level1)
285 {
286     int32_t ret;
287     ConnBrConnection connection;
288 
289     connection.socketHandle = MAX_BR_READ_BUFFER_CAPACITY;
290     ret = ConnBrDisconnectNow(&connection);
291     EXPECT_EQ(SOFTBUS_LOCK_ERR, ret);
292 }
293 
294 HWTEST_F(ConnectionBrConnectionTest, testBrConnection004, TestSize.Level1)
295 {
296     int32_t result = 0;
297     int32_t status = 0;
298 
299     BdAddr addr = {
300         .addr = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66},
301     };
302     BtUuid uuid = {
303         .uuid = (char *)SOFTBUS_CHARA_CONN_UUID,
304         .uuidLen = strlen(SOFTBUS_CHARA_CONN_UUID),
305     };
306     EXPECT_NO_FATAL_FAILURE(BrConnectStatusCallback(&addr, uuid, result, status));
307 }
308 
309 HWTEST_F(ConnectionBrConnectionTest, testBrConnection005, TestSize.Level1)
310 {
311     int32_t ret;
312     ConnBrConnection connection;
313     const cJSON *json = nullptr;
314     NiceMock<ConnectionBrInterfaceMock> brMock;
315 
316     EXPECT_CALL(brMock, GetJsonObjectSignedNumberItem).WillRepeatedly(Return(false));
317     ret = ConnBrOnReferenceResponse(&connection, json);
318     EXPECT_EQ(SOFTBUS_PARSE_JSON_ERR, ret);
319 
320     EXPECT_CALL(brMock, GetJsonObjectSignedNumberItem).WillRepeatedly(Return(true));
321     SoftBusMutexInit(&connection.lock, nullptr);
322     ret = ConnBrOnReferenceResponse(&connection, json);
323     EXPECT_EQ(SOFTBUS_OK, ret);
324 }
325 
326 HWTEST_F(ConnectionBrConnectionTest, testBrConnection006, TestSize.Level1)
327 {
328     int32_t ret;
329     ServerState serverState;
330 
331     g_serverState = &serverState;
332     ret = ConnBrStartServer();
333     EXPECT_EQ(SOFTBUS_OK, ret);
334 
335     g_serverState = nullptr;
336     ret = ConnBrStartServer();
337     EXPECT_EQ(SOFTBUS_OK, ret);
338 }
339 
340 HWTEST_F(ConnectionBrConnectionTest, testBrConnection007, TestSize.Level1)
341 {
342     int32_t ret;
343 
344     g_serverState->serverId = 1;
345     ret = ConnBrStopServer();
346     EXPECT_EQ(SOFTBUS_OK, ret);
347 
348     g_serverState = nullptr;
349     ret = ConnBrStopServer();
350     EXPECT_EQ(SOFTBUS_OK, ret);
351 }
352 
353 HWTEST_F(ConnectionBrConnectionTest, testBrConnection008, TestSize.Level1)
354 {
355     SoftBusMessage msg;
356 
357     msg.what = MSG_CONNECTION_WAIT_NEGOTIATION_CLOSING_TIMEOUT;
358     EXPECT_NO_FATAL_FAILURE(BrConnectionMsgHandler(&msg));
359     msg.what = MSG_CONNECTION_RETRY_NOTIFY_REFERENCE;
360     EXPECT_NO_FATAL_FAILURE(BrConnectionMsgHandler(&msg));
361     msg.what = MSG_CONNECTION_RETRY_NOTIFY_REFERENCE + 1;
362     EXPECT_NO_FATAL_FAILURE(BrConnectionMsgHandler(&msg));
363 }
364 
365 HWTEST_F(ConnectionBrConnectionTest, testBrConnection009, TestSize.Level1)
366 {
367     int32_t ret;
368     SoftBusMessage msg;
369     SoftBusMessage args;
370 
371     msg.what = MSG_CONNECTION_WAIT_NEGOTIATION_CLOSING_TIMEOUT;
372     args.what = MSG_CONNECTION_RETRY_NOTIFY_REFERENCE;
373     ret = BrCompareConnectionLooperEventFunc(&msg, (void *)(&args));
374     EXPECT_EQ(COMPARE_FAILED, ret);
375 
376     msg.what = MSG_CONNECTION_WAIT_NEGOTIATION_CLOSING_TIMEOUT;
377     args.what = MSG_CONNECTION_WAIT_NEGOTIATION_CLOSING_TIMEOUT;
378     msg.arg1 = 10;
379     args.arg1 = 10;
380     ret = BrCompareConnectionLooperEventFunc(&msg, (void *)(&args));
381     EXPECT_EQ(COMPARE_SUCCESS, ret);
382 
383     msg.what = MSG_CONNECTION_WAIT_NEGOTIATION_CLOSING_TIMEOUT;
384     args.what = MSG_CONNECTION_WAIT_NEGOTIATION_CLOSING_TIMEOUT;
385     msg.arg1 = 9;
386     args.arg1 = 10;
387     ret = BrCompareConnectionLooperEventFunc(&msg, (void *)(&args));
388     EXPECT_EQ(COMPARE_FAILED, ret);
389 
390     msg.what = MSG_CONNECTION_RETRY_NOTIFY_REFERENCE;
391     args.what = MSG_CONNECTION_RETRY_NOTIFY_REFERENCE;
392     msg.arg1 = 9;
393     args.arg1 = 10;
394     ret = BrCompareConnectionLooperEventFunc(&msg, (void *)(&args));
395     EXPECT_EQ(COMPARE_FAILED, ret);
396 
397     msg.what = MSG_CONNECTION_RETRY_NOTIFY_REFERENCE;
398     args.what = MSG_CONNECTION_RETRY_NOTIFY_REFERENCE;
399     args.arg1 = 0;
400     args.arg2 = 0;
401     args.obj = nullptr;
402     ret = BrCompareConnectionLooperEventFunc(&msg, (void *)(&args));
403     EXPECT_EQ(COMPARE_SUCCESS, ret);
404 }
405 
406 HWTEST_F(ConnectionBrConnectionTest, testBrConnection010, TestSize.Level1)
407 {
408     void *ret = nullptr;
409     ServerServeContext *ctx = nullptr;
410 
411     g_sppDriver->GetRemoteDeviceInfo = GetRemoteDeviceInfo;
412     ctx = (ServerServeContext *)SoftBusCalloc(sizeof(*ctx));
413     ASSERT_NE(nullptr, ctx);
414     ctx->socketHandle = 0;
415     ret = StartServerServe((void *)(ctx));
416     EXPECT_EQ(nullptr, ret);
417 }
418 
419 HWTEST_F(ConnectionBrConnectionTest, testBrConnection011, TestSize.Level1)
420 {
421     ConnBrConnection *connection;
422 
423     connection = (ConnBrConnection *)SoftBusCalloc(sizeof(*connection));
424     ASSERT_NE(nullptr, connection);
425     connection->connectProcessStatus = nullptr;
426     ConnBrFreeConnection(connection);
427 }
428 
429 HWTEST_F(ConnectionBrConnectionTest, testBrConnection012, TestSize.Level1)
430 {
431     void *ret;
432     ServerState *serverState;
433 
434     serverState = (ServerState *)SoftBusCalloc(sizeof(*serverState));
435     ASSERT_NE(nullptr, serverState);
436     serverState->available = 0;
437     serverState->traceId = 0;
438     serverState->serverId = 1;
439     SoftBusMutexInit(&serverState->mutex, nullptr);
440     ret = ListenTask((void *)serverState);
441     EXPECT_EQ(nullptr, ret);
442 }
443 
444 HWTEST_F(ConnectionBrConnectionTest, testBrConnection013, TestSize.Level1)
445 {
446     int32_t ret;
447     int32_t val;
448     int32_t mtu;
449 
450     g_configItems[SOFTBUS_INT_CONN_BR_MAX_DATA_LENGTH].len = 0;
451     ret = InitProperty();
452     EXPECT_EQ(SOFTBUS_NO_INIT, ret);
453 
454     val = MAX_BR_READ_BUFFER_CAPACITY + 1;
455     g_configItems[SOFTBUS_INT_CONN_BR_MAX_DATA_LENGTH].len = 4;
456     memcpy_s((void *)(g_configItems[SOFTBUS_INT_CONN_BR_MAX_DATA_LENGTH].val),
457         g_configItems[SOFTBUS_INT_CONN_BR_MAX_DATA_LENGTH].len, (void *)(&val), sizeof(int));
458     ret = InitProperty();
459     EXPECT_EQ(SOFTBUS_NO_INIT, ret);
460 
461     val = MAX_BR_READ_BUFFER_CAPACITY;
462     g_configItems[SOFTBUS_INT_CONN_BR_MAX_DATA_LENGTH].len = 4;
463     memcpy_s((void *)(g_configItems[SOFTBUS_INT_CONN_BR_MAX_DATA_LENGTH].val),
464         g_configItems[SOFTBUS_INT_CONN_BR_MAX_DATA_LENGTH].len, (void *)(&val), sizeof(int));
465     g_configItems[SOFTBUS_INT_CONN_RFCOM_SEND_MAX_LEN].len = 0;
466     ret = InitProperty();
467     EXPECT_EQ(SOFTBUS_NO_INIT, ret);
468 
469     val = MAX_BR_READ_BUFFER_CAPACITY;
470     g_configItems[SOFTBUS_INT_CONN_BR_MAX_DATA_LENGTH].len = 4;
471     memcpy_s((void *)(g_configItems[SOFTBUS_INT_CONN_BR_MAX_DATA_LENGTH].val),
472         g_configItems[SOFTBUS_INT_CONN_BR_MAX_DATA_LENGTH].len, (void *)(&val), sizeof(int));
473     mtu = MAX_BR_MTU_SIZE + 1;
474     g_configItems[SOFTBUS_INT_CONN_RFCOM_SEND_MAX_LEN].len = 4;
475     memcpy_s((void *)(g_configItems[SOFTBUS_INT_CONN_RFCOM_SEND_MAX_LEN].val),
476         g_configItems[SOFTBUS_INT_CONN_RFCOM_SEND_MAX_LEN].len, (void *)(&mtu), sizeof(int));
477     ret = InitProperty();
478     EXPECT_EQ(SOFTBUS_NO_INIT, ret);
479 
480     val = MAX_BR_READ_BUFFER_CAPACITY;
481     g_configItems[SOFTBUS_INT_CONN_BR_MAX_DATA_LENGTH].len = 4;
482     memcpy_s((void *)(g_configItems[SOFTBUS_INT_CONN_BR_MAX_DATA_LENGTH].val),
483         g_configItems[SOFTBUS_INT_CONN_BR_MAX_DATA_LENGTH].len, (void *)(&val), sizeof(int));
484     mtu = MAX_BR_MTU_SIZE;
485     g_configItems[SOFTBUS_INT_CONN_RFCOM_SEND_MAX_LEN].len = 4;
486     memcpy_s((void *)(g_configItems[SOFTBUS_INT_CONN_RFCOM_SEND_MAX_LEN].val),
487         g_configItems[SOFTBUS_INT_CONN_RFCOM_SEND_MAX_LEN].len, (void *)(&mtu), sizeof(int));
488     ret = InitProperty();
489     EXPECT_EQ(SOFTBUS_OK, ret);
490 }
491 
492 HWTEST_F(ConnectionBrConnectionTest, testBrManager001, TestSize.Level1)
493 {
494     uint32_t reqId = 0;
495     uint32_t pId = 0;
496     ConnBrDevice *device = nullptr;
497     ConnectStatistics statistics;
498     int32_t reason = 0;
499 
500     EXPECT_NO_FATAL_FAILURE(DfxRecordBrConnectFail(reqId, pId, device, nullptr, reason));
501     EXPECT_NO_FATAL_FAILURE(DfxRecordBrConnectFail(reqId, pId, device, &statistics, reason));
502 }
503 
504 /*
505 * @tc.name: testBrManager002
506 * @tc.desc: test DfxRecordBrConnectSuccess when NotifyDeviceConnectResult
507 * @tc.type: FUNC
508 * @tc.require:
509 */
510 HWTEST_F(ConnectionBrConnectionTest, testBrManager002, TestSize.Level1)
511 {
512     ConnBrConnection *connection = static_cast<ConnBrConnection *>(SoftBusMalloc(sizeof(ConnBrConnection)));
513     ASSERT_NE(nullptr, connection);
514     connection->connectionId = 1;
515 
516     ConnBrDevice device;
517     ConnBrRequest request;
518     request.requestId = 0;
519     request.result.OnConnectSuccessed = OnConnectSuccessed;
520     (void)strcpy_s(device.addr, BT_MAC_LEN, "24:DA:33:6A:06:EC");
521     ListInit(&device.requests);
522     ListAdd(&device.requests, &request.node);
523     NotifyDeviceConnectResult(&device, connection, true, 1);
524     ConnBrRequest *it = nullptr;
525     LIST_FOR_EACH_ENTRY(it, &device.requests, ConnBrRequest, node) {
526         EXPECT_EQ(it->statistics.reuse, true);
527     }
528     SoftBusFree(connection);
529 }
530 
531 HWTEST_F(ConnectionBrConnectionTest, testBrManager003, TestSize.Level1)
532 {
533     ConnBrDevice device;
534     ConnBrConnection connection;
535     bool isReuse = false;
536     int32_t reason = 0;
537     ConnBrRequest request;
538 
539     request.requestId = 0;
540     request.requestId = 0;
541     request.result.OnConnectFailed = OnConnectFailed;
542     (void)strcpy_s(device.addr, BT_MAC_LEN, "24:DA:33:6A:06:EC");
543     ListInit(&device.requests);
544     ListAdd(&device.requests, &request.node);
545     EXPECT_NO_FATAL_FAILURE(NotifyDeviceConnectResult(&device, nullptr, isReuse, reason));
546 
547     (void)strcpy_s(device.addr, BT_MAC_LEN, "24:DA:33:6A:06:EC");
548     ListInit(&device.requests);
549     EXPECT_NO_FATAL_FAILURE(NotifyDeviceConnectResult(&device, &connection, isReuse, reason));
550 
551     request.requestId = 0;
552     request.requestId = 0;
553     request.result.OnConnectFailed = OnConnectFailed;
554     request.result.OnConnectSuccessed = OnConnectSuccessed;
555     (void)strcpy_s(device.addr, BT_MAC_LEN, "24:DA:33:6A:06:EC");
556     ListInit(&device.requests);
557     ListAdd(&device.requests, &request.node);
558     reason = 1;
559     isReuse = true;
560     EXPECT_NO_FATAL_FAILURE(NotifyDeviceConnectResult(&device, &connection, isReuse, reason));
561 }
562 
563 HWTEST_F(ConnectionBrConnectionTest, testBrManager005, TestSize.Level1)
564 {
565     const char *addr = "24:DA:33:6A:06:EC";
566     ConnBrPendInfo info;
567     BrPending pending;
568 
569     ListInit(&(g_brManager.pendings->list));
570     ListAdd(&(g_brManager.pendings->list), &(pending.node));
571     (void)strcpy_s(info.addr, BT_MAC_LEN, addr);
572     pending.pendInfo = &info;
573     int32_t ret = SoftBusMutexInit(&g_brManager.pendings->lock, nullptr);
574     g_brManagerAsyncHandler.handler.looper->PostMessageDelay = PostMessageDelay;
575 
576     info.firstStartTimestamp = 0xfffffffffffffff;
577     info.firstDuration = 0x1;
578     EXPECT_EQ(SOFTBUS_OK, ret);
579 }
580 
581 HWTEST_F(ConnectionBrConnectionTest, testBrManager006, TestSize.Level1)
582 {
583     const char *addrress = "42:AD:54:6A:06:EC";
584     BrPending pending;
585     ConnBrPendInfo pendInfo;
586     ListInit(&(g_brManager.pendings->list));
587     (void)strcpy_s(pendInfo.addr, BT_MAC_LEN, "24:DA:33:6A:06:EC");
588     pending.pendInfo = &pendInfo;
589     ListAdd(&(g_brManager.pendings->list), &(pending.node));
590     bool ret1 = CheckPending(addrress);
591     EXPECT_EQ(false, ret1);
592 
593     ConnBrDevice device;
594     const char *anomizeAddress;
595 
596     (void)strcpy_s(device.addr, BT_MAC_LEN, "abc");
597     anomizeAddress = "123";
598     SoftBusMutexDestroy(&g_brManager.connections->lock);
599     int32_t ret2 = ConnectDeviceDirectly(&device, anomizeAddress);
600     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret2);
601 
602     (void)strcpy_s(device.addr, BT_MAC_LEN, "abc");
603     anomizeAddress = "123";
604     SoftBusMutexInit(&g_brManager.connections->lock, nullptr);
605     ret2 = ConnectDeviceDirectly(&device, anomizeAddress);
606     EXPECT_EQ(SOFTBUS_OK, ret2);
607 }
608 
609 HWTEST_F(ConnectionBrConnectionTest, testBrManager008, TestSize.Level1)
610 {
611     int32_t ret;
612     ConnBrDevice *device;
613     const char *anomizeAddress;
614     ConnBrDevice conn;
615     ConnBrDevice connBr;
616 
617     device = (ConnBrDevice *)SoftBusCalloc(sizeof(*device));
618     ASSERT_NE(nullptr, device);
619     device->state = BR_DEVICE_STATE_INIT;
620     ListInit(&device->requests);
621     (void)strcpy_s(device->addr, BT_MAC_LEN, "abc");
622     (void)strcpy_s(conn.addr, BT_MAC_LEN, "abc");
623     ListInit(&conn.requests);
624     g_brManager.connecting = &conn;
625     anomizeAddress = "abc";
626     ListInit(&g_brManager.waitings);
627     ret = PendingDevice(device, anomizeAddress);
628     EXPECT_EQ(SOFTBUS_OK, ret);
629 
630     device = (ConnBrDevice *)SoftBusCalloc(sizeof(*device));
631     ASSERT_NE(nullptr, device);
632     device->state = BR_DEVICE_STATE_INIT;
633     ListInit(&device->requests);
634     (void)strcpy_s(device->addr, BT_MAC_LEN, "abc");
635     (void)strcpy_s(conn.addr, BT_MAC_LEN, "abcd");
636     ListInit(&conn.requests);
637     g_brManager.connecting = &conn;
638     anomizeAddress = "abc";
639     ListInit(&g_brManager.waitings);
640     ret = PendingDevice(device, anomizeAddress);
641     EXPECT_EQ(SOFTBUS_OK, ret);
642 
643     device->state = BR_DEVICE_STATE_INIT;
644     ListInit(&device->requests);
645     (void)strcpy_s(device->addr, BT_MAC_LEN, "abc");
646     (void)strcpy_s(conn.addr, BT_MAC_LEN, "abcd");
647     (void)strcpy_s(connBr.addr, BT_MAC_LEN, "abc");
648     ListInit(&conn.requests);
649     g_brManager.connecting = &conn;
650     anomizeAddress = "abc";
651     ListInit(&g_brManager.waitings);
652     ListTailInsert(&g_brManager.waitings, &connBr.node);
653     ret = PendingDevice(device, anomizeAddress);
654     EXPECT_EQ(SOFTBUS_OK, ret);
655 }
656 
657 HWTEST_F(ConnectionBrConnectionTest, testBrManager009, TestSize.Level1)
658 {
659     bool ret;
660     ConnBrConnection connection;
661     ConnBrDevice device;
662 
663     connection.state = BR_CONNECTION_STATE_EXCEPTION;
664     SoftBusMutexInit(&connection.lock, nullptr);
665     connection.connectionId = 1;
666     ret = BrReuseConnection(&device, &connection);
667     EXPECT_EQ(false, ret);
668 }
669 
670 HWTEST_F(ConnectionBrConnectionTest, testBrManager010, TestSize.Level1)
671 {
672     bool ret;
673     const char *addr = "abc";
674     BrPending pending;
675     ConnBrPendInfo pendInfo;
676 
677     SoftBusMutexInit(&g_brManager.pendings->lock, nullptr);
678     ListInit(&g_brManager.pendings->list);
679     (void)strcpy_s(pendInfo.addr, BT_MAC_LEN, "abc");
680     pending.pendInfo = &pendInfo;
681     ListTailInsert(&g_brManager.pendings->list, &pending.node);
682     ret = CheckPending(addr);
683     EXPECT_EQ(false, ret);
684 
685     (void)strcpy_s(pendInfo.addr, BT_MAC_LEN, "abcd");
686     ret = CheckPending(addr);
687     EXPECT_EQ(false, ret);
688 }
689 
690 HWTEST_F(ConnectionBrConnectionTest, testBrManager014, TestSize.Level1)
691 {
692     uint32_t connectionId = 0;
693     ConnBrConnection *target;
694     NiceMock<ConnectionBrInterfaceMock> brMock;
695 
696     SoftBusMutexDestroy(&g_brManager.connections->lock);
697     ClientConnected(connectionId);
698 
699     EXPECT_CALL(brMock, AddNumberToJsonObject).WillRepeatedly(Return(false));
700     SoftBusMutexInit(&g_brManager.connections->lock, nullptr);
701     target = (ConnBrConnection *)SoftBusCalloc(sizeof(*target));
702     ASSERT_NE(nullptr, target);
703     SoftBusMutexInit(&g_brManager.connections->lock, nullptr);
704     (void)strcpy_s(target->addr, BT_MAC_LEN, "abcde");
705     target->connectionId = 0;
706     target->connectionRc = 10;
707     SoftBusMutexInit(&target->lock, nullptr);
708     ListInit(&g_brManager.connections->list);
709     ListTailInsert(&g_brManager.connections->list, &target->node);
710     g_brManager.connecting = nullptr;
711     ClientConnected(connectionId);
712 }
713 
714 HWTEST_F(ConnectionBrConnectionTest, testBrManager015, TestSize.Level1)
715 {
716     uint32_t connectionId = 0;
717     int32_t error = 0;
718     ConnBrConnection *target;
719 
720     SoftBusMutexDestroy(&g_brManager.connections->lock);
721     ClientConnectFailed(connectionId, error);
722 
723     SoftBusMutexInit(&g_brManager.connections->lock, nullptr);
724     target = reinterpret_cast<ConnBrConnection *>(SoftBusCalloc(sizeof(*target)));
725     ASSERT_NE(nullptr, target);
726     SoftBusMutexInit(&g_brManager.connections->lock, nullptr);
727     (void)strcpy_s(target->addr, BT_MAC_LEN, "abcde");
728     target->connectionId = 1;
729     target->connectionRc = 10;
730     SoftBusMutexInit(&target->lock, nullptr);
731     ListInit(&g_brManager.connections->list);
732     ListTailInsert(&g_brManager.connections->list, &target->node);
733     g_brManager.connecting = nullptr;
734     ClientConnectFailed(connectionId, error);
735 }
736 
737 HWTEST_F(ConnectionBrConnectionTest, testBrManager016, TestSize.Level1)
738 {
739     uint32_t connectionId = 0;
740     int32_t error = 0;
741     ConnBrConnection *target;
742     ConnBrDevice *connectingDevice;
743 
744     SoftBusMutexInit(&g_brManager.connections->lock, nullptr);
745     target = reinterpret_cast<ConnBrConnection *>(SoftBusCalloc(sizeof(*target)));
746     ASSERT_NE(nullptr, target);
747     SoftBusMutexInit(&g_brManager.connections->lock, nullptr);
748     (void)strcpy_s(target->addr, BT_MAC_LEN, "abcde");
749     target->connectionId = 0;
750     target->connectionRc = 10;
751     target->side = CONN_SIDE_SERVER;
752     target->state = BR_CONNECTION_STATE_EXCEPTION;
753     target->objectRc = 1;
754     SoftBusMutexInit(&target->lock, nullptr);
755     ListInit(&g_brManager.connections->list);
756     ListTailInsert(&g_brManager.connections->list, &target->node);
757     connectingDevice = reinterpret_cast<ConnBrDevice *>(SoftBusCalloc(sizeof(*connectingDevice)));
758     ASSERT_NE(nullptr, connectingDevice);
759     (void)strcpy_s(connectingDevice->addr, BT_MAC_LEN, "abcde");
760     ListInit(&connectingDevice->requests);
761     g_brManager.connecting = connectingDevice;
762     SoftBusList *list = CreateSoftBusList();
763     ConnBrConnection *connection = ConnBrGetConnectionById(connectionId);
764     connection->connectProcessStatus = list;
765     ClientConnectFailed(connectionId, error);
766     ConnBrReturnConnection(&connection);
767 }
768 
769 HWTEST_F(ConnectionBrConnectionTest, testBrManager017, TestSize.Level1)
770 {
771     uint32_t connectionId = 0;
772     const char *address = "abc";
773     ConnBrConnection *target;
774 
775     SoftBusMutexInit(&g_brManager.connections->lock, nullptr);
776     target = reinterpret_cast<ConnBrConnection *>(SoftBusCalloc(sizeof(*target)));
777     ASSERT_NE(nullptr, target);
778     SoftBusMutexInit(&g_brManager.connections->lock, nullptr);
779     (void)strcpy_s(target->addr, BT_MAC_LEN, "abc");
780     target->connectionId = 0;
781     target->objectRc = 1;
782     SoftBusMutexInit(&target->lock, nullptr);
783     ListInit(&g_brManager.connections->list);
784     ListTailInsert(&g_brManager.connections->list, &target->node);
785     g_brManager.connecting = nullptr;
786     ClientConnectTimeoutOnConnectingState(connectionId, address);
787 }
788 
789 HWTEST_F(ConnectionBrConnectionTest, testBrManager018, TestSize.Level1)
790 {
791     uint32_t connectionId = 0;
792     const char *address = "abc";
793     ConnBrDevice *connectingDevice;
794 
795     SoftBusMutexDestroy(&g_brManager.connections->lock);
796     connectingDevice = reinterpret_cast<ConnBrDevice *>(SoftBusCalloc(sizeof(*connectingDevice)));
797     ASSERT_NE(nullptr, connectingDevice);
798     (void)strcpy_s(connectingDevice->addr, BT_MAC_LEN, "abc");
799     ListInit(&connectingDevice->requests);
800     g_brManager.connecting = connectingDevice;
801     ClientConnectTimeoutOnConnectingState(connectionId, address);
802 }
803 
804 HWTEST_F(ConnectionBrConnectionTest, testBrManager019, TestSize.Level1)
805 {
806     ConnBrDataReceivedContext ctx;
807     ConnPktHead *head;
808 
809     SoftBusMutexInit(&g_brManager.connections->lock, nullptr);
810     head = reinterpret_cast<ConnPktHead *>(SoftBusCalloc(sizeof(*head)));
811     ASSERT_NE(nullptr, head);
812     head->flag = 0;
813     head->module = 0;
814     head->seq = 0;
815     ctx.data = (uint8_t *)head;
816     ctx.connectionId = 0;
817     DataReceived(&ctx);
818 }
819 
820 HWTEST_F(ConnectionBrConnectionTest, testBrManager020, TestSize.Level1)
821 {
822     ConnBrDataReceivedContext ctx;
823     ConnPktHead *head;
824     ConnBrConnection *target;
825     NiceMock<ConnectionBrInterfaceMock> brMock;
826 
827     EXPECT_CALL(brMock, cJSON_ParseWithLength).WillRepeatedly(Return(nullptr));
828     SoftBusMutexDestroy(&g_brManager.connections->lock);
829     target = reinterpret_cast<ConnBrConnection *>(SoftBusCalloc(sizeof(*target)));
830     ASSERT_NE(nullptr, target);
831     SoftBusMutexInit(&g_brManager.connections->lock, nullptr);
832     (void)strcpy_s(target->addr, BT_MAC_LEN, "abc");
833     target->connectionId = 0;
834     target->objectRc = 1;
835     SoftBusMutexInit(&target->lock, nullptr);
836     ListInit(&g_brManager.connections->list);
837     ListTailInsert(&g_brManager.connections->list, &target->node);
838 
839     head = reinterpret_cast<ConnPktHead *>(SoftBusCalloc(sizeof(*head)));
840     ASSERT_NE(nullptr, head);
841     head->flag = 0;
842     head->module = MODULE_CONNECTION;
843     head->seq = 0;
844     ctx.data = (uint8_t *)head;
845     ctx.connectionId = 0;
846     DataReceived(&ctx);
847 }
848 
849 HWTEST_F(ConnectionBrConnectionTest, testBrManager021, TestSize.Level1)
850 {
851     ConnBrDataReceivedContext ctx;
852     ConnPktHead *head;
853     ConnBrConnection *target;
854 
855     SoftBusMutexDestroy(&g_brManager.connections->lock);
856     target = reinterpret_cast<ConnBrConnection *>(SoftBusCalloc(sizeof(*target)));
857     ASSERT_NE(nullptr, target);
858     SoftBusMutexInit(&g_brManager.connections->lock, nullptr);
859     (void)strcpy_s(target->addr, BT_MAC_LEN, "abc");
860     target->connectionId = 0;
861     target->objectRc = 1;
862     SoftBusMutexInit(&target->lock, nullptr);
863     ListInit(&g_brManager.connections->list);
864     ListTailInsert(&g_brManager.connections->list, &target->node);
865 
866     head = reinterpret_cast<ConnPktHead *>(SoftBusCalloc(sizeof(*head)));
867     ASSERT_NE(nullptr, head);
868     head->flag = 0;
869     head->module = MODULE_NIP_BR_CHANNEL;
870     head->seq = (int64_t)BR_NIP_SEQ;
871     ctx.data = (uint8_t *)head;
872     ctx.connectionId = 0;
873     DataReceived(&ctx);
874 }
875 
876 HWTEST_F(ConnectionBrConnectionTest, testBrManager022, TestSize.Level1)
877 {
878     ConnBrDataReceivedContext ctx;
879     ConnPktHead *head;
880     ConnBrConnection *target;
881 
882     SoftBusMutexDestroy(&g_brManager.connections->lock);
883     target = reinterpret_cast<ConnBrConnection *>(SoftBusCalloc(sizeof(*target)));
884     ASSERT_NE(nullptr, target);
885     SoftBusMutexInit(&g_brManager.connections->lock, nullptr);
886     (void)strcpy_s(target->addr, BT_MAC_LEN, "abc");
887     target->connectionId = 0;
888     target->objectRc = 1;
889     SoftBusMutexInit(&target->lock, nullptr);
890     ListInit(&g_brManager.connections->list);
891     ListTailInsert(&g_brManager.connections->list, &target->node);
892 
893     head = reinterpret_cast<ConnPktHead *>(SoftBusCalloc(sizeof(*head)));
894     ASSERT_NE(nullptr, head);
895     head->flag = 0;
896     head->module = MODULE_OLD_NEARBY;
897     head->seq = (int64_t)BR_NIP_SEQ;
898     ctx.data = (uint8_t *)head;
899     ctx.connectionId = 0;
900     DataReceived(&ctx);
901 }
902 
903 HWTEST_F(ConnectionBrConnectionTest, testBrManager023, TestSize.Level1)
904 {
905     ConnBrConnection *connection = static_cast<ConnBrConnection *>(SoftBusMalloc(sizeof(ConnBrConnection)));
906     ASSERT_NE(nullptr, connection);
907     connection->connectionId = 0;
908     char data[DATASIZE] = { "{\
909             \"ESSION_KEY\": \"sdadad\",\
910             \"ENCRYPT\": 30,\
911             \"MY_HANDLE_ID\": 22,\
912             \"PEER_HANDLE_ID\": 25,\
913         }" };
914     NiceMock<ConnectionBrInterfaceMock> brMock;
915 
916     ReceivedControlData(connection, nullptr, 0);
917 
918     EXPECT_CALL(brMock, GetJsonObjectNumberItem).WillRepeatedly(Return(false));
919     ReceivedControlData(connection, (uint8_t *)data, DATASIZE);
920 
921     EXPECT_CALL(brMock, GetJsonObjectNumberItem).WillRepeatedly(Return(true));
922     ReceivedControlData(connection, (uint8_t *)data, DATASIZE);
923     SoftBusFree(connection);
924 }
925 
926 HWTEST_F(ConnectionBrConnectionTest, testBrManager024, TestSize.Level1)
927 {
928     uint32_t connectionId = 0;
929     int32_t error = 0;
930     ConnBrConnection *target;
931     ConnBrDevice it;
932 
933     SoftBusMutexInit(&g_brManager.connections->lock, nullptr);
934     target = reinterpret_cast<ConnBrConnection *>(SoftBusCalloc(sizeof(*target)));
935     ASSERT_NE(nullptr, target);
936     (void)strcpy_s(target->addr, BT_MAC_LEN, "abc");
937     target->connectionId = 0;
938     target->objectRc = 10;
939     SoftBusMutexInit(&target->lock, nullptr);
940     ListInit(&g_brManager.connections->list);
941     ListTailInsert(&g_brManager.connections->list, &target->node);
942 
943     ListInit(&g_brManager.waitings);
944     (void)strcpy_s(it.addr, BT_MAC_LEN, "abc");
945     ListTailInsert(&g_brManager.waitings, &it.node);
946     g_connectCallback.OnDisconnected = OnDisconnected;
947     g_brManagerAsyncHandler.handler.looper->PostMessageDelay = MessageDelay;
948     ConnectionException(connectionId, error);
949 }
950 
951 HWTEST_F(ConnectionBrConnectionTest, testBrManager025, TestSize.Level1)
952 {
953     uint32_t connectionId = 0;
954     ConnBrConnection *target;
955     ConnBrDevice *it;
956 
957     SoftBusMutexInit(&g_brManager.connections->lock, nullptr);
958     target = reinterpret_cast<ConnBrConnection *>(SoftBusCalloc(sizeof(*target)));
959     ASSERT_NE(nullptr, target);
960     (void)strcpy_s(target->addr, BT_MAC_LEN, "abc");
961     target->connectionId = 0;
962     target->objectRc = 10;
963     target->state = BR_CONNECTION_STATE_CONNECTED;
964     SoftBusMutexInit(&target->lock, nullptr);
965     ListInit(&g_brManager.connections->list);
966     ListTailInsert(&g_brManager.connections->list, &target->node);
967 
968     it = reinterpret_cast<ConnBrDevice *>(SoftBusCalloc(sizeof(*it)));
969     ASSERT_NE(nullptr, it);
970     (void)strcpy_s(it->addr, BT_MAC_LEN, "abc");
971     ListInit(&g_brManager.waitings);
972     ListTailInsert(&g_brManager.waitings, &it->node);
973     ListInit(&it->requests);
974     ConnectionResume(connectionId);
975 }
976 
977 HWTEST_F(ConnectionBrConnectionTest, testBrManager026, TestSize.Level1)
978 {
979     ConnBrPendInfo unpendInfo;
980 
981     SoftBusMutexInit(&g_brManager.pendings->lock, nullptr);
982     (void)strcpy_s(unpendInfo.addr, BT_MAC_LEN, "abc");
983     ListInit(&g_brManager.pendings->list);
984     g_brManagerAsyncHandler.handler.looper->RemoveMessageCustom = RvMessageCustom;
985     EXPECT_NO_FATAL_FAILURE(UnpendConnection(unpendInfo.addr));
986 }
987 
988 HWTEST_F(ConnectionBrConnectionTest, testBrManager027, TestSize.Level1)
989 {
990     enum BrServerState target = BR_STATE_AVAILABLE;
991 
992     TransitionToState(target);
993     TransitionToState(target);
994     target = BR_STATE_CONNECTING;
995     TransitionToState(target);
996     EXPECT_NE(g_brManager.state, nullptr);
997 }
998 
999 HWTEST_F(ConnectionBrConnectionTest, testBrManager028, TestSize.Level1)
1000 {
1001     SoftBusMessage msg;
1002     ErrorContext obj;
1003 
1004     BuildParam(&g_brManager);
1005     obj.connectionId = 0;
1006     obj.error = 0;
1007     msg.obj = &obj;
1008 
1009     msg.what = MSG_NEXT_CMD;
1010     EXPECT_NO_FATAL_FAILURE(BrManagerMsgHandler(&msg));
1011 
1012     msg.what = MSG_CONNECT_REQUEST;
1013     EXPECT_NO_FATAL_FAILURE(BrManagerMsgHandler(&msg));
1014 
1015     msg.what = MSG_CONNECT_SUCCESS;
1016     EXPECT_NO_FATAL_FAILURE(BrManagerMsgHandler(&msg));
1017 
1018     msg.what = MSG_CONNECT_TIMEOUT;
1019     EXPECT_NO_FATAL_FAILURE(BrManagerMsgHandler(&msg));
1020 
1021     msg.what = MSG_CONNECT_FAIL;
1022     EXPECT_NO_FATAL_FAILURE(BrManagerMsgHandler(&msg));
1023 
1024     msg.what = MSG_SERVER_ACCEPTED;
1025     EXPECT_NO_FATAL_FAILURE(BrManagerMsgHandler(&msg));
1026 
1027     msg.what = MSG_DATA_RECEIVED;
1028     EXPECT_NO_FATAL_FAILURE(BrManagerMsgHandler(&msg));
1029 
1030     msg.what = MSG_CONNECTION_EXECEPTION;
1031     EXPECT_NO_FATAL_FAILURE(BrManagerMsgHandler(&msg));
1032 
1033     msg.what = MSG_CONNECTION_RESUME;
1034     EXPECT_NO_FATAL_FAILURE(BrManagerMsgHandler(&msg));
1035 
1036     msg.what = MGR_DISCONNECT_REQUEST;
1037     EXPECT_NO_FATAL_FAILURE(BrManagerMsgHandler(&msg));
1038 
1039     msg.what = MSG_UNPEND;
1040     EXPECT_NO_FATAL_FAILURE(BrManagerMsgHandler(&msg));
1041 
1042     msg.what = MSG_RESET;
1043     EXPECT_NO_FATAL_FAILURE(BrManagerMsgHandler(&msg));
1044 
1045     msg.what = MSG_RESET + 1;
1046     EXPECT_NO_FATAL_FAILURE(BrManagerMsgHandler(&msg));
1047 }
1048 
1049 HWTEST_F(ConnectionBrConnectionTest, testBrManager029, TestSize.Level1)
1050 {
1051     int32_t ret;
1052     SoftBusMessage msg;
1053     SoftBusMessage args;
1054     ConnBrPendInfo msgInfo;
1055     ConnBrPendInfo ctxInfo;
1056 
1057     msg.what = MSG_CONNECT_TIMEOUT;
1058     args.what = MSG_UNPEND;
1059     ret = BrCompareManagerLooperEventFunc(&msg, &args);
1060     EXPECT_EQ(COMPARE_FAILED, ret);
1061 
1062     args.what = MSG_CONNECT_TIMEOUT;
1063     msg.arg1 = 1;
1064     args.arg1 = 1;
1065     ret = BrCompareManagerLooperEventFunc(&msg, &args);
1066     EXPECT_EQ(COMPARE_SUCCESS, ret);
1067 
1068     msg.arg1 = 0;
1069     ret = BrCompareManagerLooperEventFunc(&msg, &args);
1070     EXPECT_EQ(COMPARE_FAILED, ret);
1071 
1072     msg.what = MSG_UNPEND;
1073     args.what = MSG_UNPEND;
1074     msg.obj = nullptr;
1075     args.obj = nullptr;
1076     ret = BrCompareManagerLooperEventFunc(&msg, &args);
1077     EXPECT_EQ(COMPARE_FAILED, ret);
1078 
1079     msg.what = MSG_UNPEND;
1080     args.what = MSG_UNPEND;
1081     (void)strcpy_s(msgInfo.addr, BT_MAC_LEN, "abc");
1082     (void)strcpy_s(ctxInfo.addr, BT_MAC_LEN, "abc");
1083     msg.obj = &msgInfo;
1084     args.obj = &ctxInfo;
1085     ret = BrCompareManagerLooperEventFunc(&msg, &args);
1086     EXPECT_EQ(COMPARE_SUCCESS, ret);
1087 
1088     (void)strcpy_s(msgInfo.addr, BT_MAC_LEN, "abcd");
1089     (void)strcpy_s(ctxInfo.addr, BT_MAC_LEN, "abc");
1090     msg.obj = &msgInfo;
1091     args.obj = &ctxInfo;
1092     ret = BrCompareManagerLooperEventFunc(&msg, &args);
1093     EXPECT_EQ(COMPARE_FAILED, ret);
1094 }
1095 
1096 HWTEST_F(ConnectionBrConnectionTest, testBrManager030, TestSize.Level1)
1097 {
1098     SoftBusMessage msg;
1099     SoftBusMessage args;
1100 
1101     msg.what = MSG_CONNECT_REQUEST;
1102     args.what = MSG_CONNECT_REQUEST;
1103     args.arg1 = 1;
1104     int32_t ret = BrCompareManagerLooperEventFunc(&msg, &args);
1105     EXPECT_EQ(COMPARE_FAILED, ret);
1106 
1107     args.arg1 = 0;
1108     args.arg2 = 0;
1109     args.obj = nullptr;
1110     ret = BrCompareManagerLooperEventFunc(&msg, &args);
1111     EXPECT_EQ(COMPARE_SUCCESS, ret);
1112 }
1113 
1114 
1115 HWTEST_F(ConnectionBrConnectionTest, testBrManager031, TestSize.Level1)
1116 {
1117     uint32_t connectionId = 0;
1118     uint32_t len = 0;
1119     int32_t pid = 0;
1120     int32_t flag = 0;
1121     int32_t module = 0;
1122     int64_t seq = 0;
1123     int32_t error = 1;
1124 
1125     SoftBusMutexInit(&g_brManager.connections->lock, nullptr);
1126     ListInit(&g_brManager.connections->list);
1127     EXPECT_NO_FATAL_FAILURE(OnPostByteFinshed(connectionId, len, pid, flag, module, seq, error));
1128 }
1129 
1130 HWTEST_F(ConnectionBrConnectionTest, testBrManager032, TestSize.Level1)
1131 {
1132     int32_t ret;
1133     char mac[BT_MAC_LEN] = { 0 };
1134     int32_t socketHandle = 333;
1135     ConnBrConnection *connection = ConnBrCreateConnection(mac, CONN_SIDE_SERVER, socketHandle);
1136     ConnBrSaveConnection(connection);
1137     connection->connectionId = (CONNECT_BR << CONNECT_TYPE_SHIFT) + 6;
1138     SoftBusMutexInit(&g_brManager.connections->lock, nullptr);
1139     ListInit(&g_brManager.connections->list);
1140     ListTailInsert(&g_brManager.connections->list, &connection->node);
1141     ret = AllocateConnectionIdUnsafe();
1142     EXPECT_EQ(SOFTBUS_OK, ret);
1143 }
1144 
1145 HWTEST_F(ConnectionBrConnectionTest, testBrManager033, TestSize.Level1)
1146 {
1147     int32_t ret;
1148     ConnBrConnection it;
1149 
1150     ListInit(&g_brManager.connections->list);
1151     it.connectionId = 0;
1152     ListTailInsert(&g_brManager.connections->list, &it.node);
1153     ret = AllocateConnectionIdUnsafe();
1154     EXPECT_NE(SOFTBUS_OK, ret);
1155 }
1156 
1157 HWTEST_F(ConnectionBrConnectionTest, testBrManager034, TestSize.Level1)
1158 {
1159     ConnBrConnection *connection;
1160     ConnBrConnection target;
1161 
1162     connection = reinterpret_cast<ConnBrConnection *>(SoftBusCalloc(sizeof(*connection)));
1163     ASSERT_NE(nullptr, connection);
1164     SoftBusMutexInit(&g_brManager.connections->lock, nullptr);
1165     ListInit(&g_brManager.connections->list);
1166     connection->connectionId = 0;
1167     target.connectionId = 0;
1168     ListTailInsert(&g_brManager.connections->list, &target.node);
1169     ConnBrRemoveConnection(connection);
1170 }
1171 
1172 HWTEST_F(ConnectionBrConnectionTest, testBrManager035, TestSize.Level1)
1173 {
1174     ConnBrConnection *connection;
1175     ConnBrConnection target;
1176 
1177     connection = reinterpret_cast<ConnBrConnection *>(SoftBusCalloc(sizeof(*connection)));
1178     ASSERT_NE(nullptr, connection);
1179     SoftBusMutexInit(&g_brManager.connections->lock, nullptr);
1180     ListInit(&g_brManager.connections->list);
1181     connection->connectionId = 0;
1182     target.connectionId = 1;
1183     ListTailInsert(&g_brManager.connections->list, &target.node);
1184     ConnBrRemoveConnection(connection);
1185 }
1186 
1187 HWTEST_F(ConnectionBrConnectionTest, testBrManager036, TestSize.Level1)
1188 {
1189     int32_t ret;
1190     ConnectOption option;
1191     uint32_t time = 10;
1192     BrPending it;
1193     ConnBrPendInfo pendInfo;
1194 
1195     SoftBusMutexInit(&g_brManager.pendings->lock, nullptr);
1196     option.type = CONNECT_BR;
1197     (void)strcpy_s(option.brOption.brMac, BT_MAC_LEN, "abc");
1198     ListInit(&g_brManager.pendings->list);
1199     (void)strcpy_s(pendInfo.addr, BT_MAC_LEN, "abc");
1200     pendInfo.startTimestamp = 1;
1201     pendInfo.duration = 1;
1202     pendInfo.firstDuration = 1;
1203     pendInfo.firstStartTimestamp = 1;
1204     it.pendInfo = &pendInfo;
1205     ListTailInsert(&g_brManager.pendings->list, &it.node);
1206     g_brManagerAsyncHandler.handler.looper->RemoveMessageCustom = RvMessageCustom;
1207     g_brManagerAsyncHandler.handler.looper->PostMessageDelay = PostMessageDelay;
1208     ret = BrPendConnection(&option, time);
1209     EXPECT_EQ(SOFTBUS_OK, ret);
1210 }
1211 
1212 HWTEST_F(ConnectionBrConnectionTest, testBrManager037, TestSize.Level1)
1213 {
1214     int32_t ret;
1215     ConnectOption option;
1216     uint32_t time = 10;
1217     BrPending it;
1218     ConnBrPendInfo pendInfo;
1219 
1220     SoftBusMutexInit(&g_brManager.pendings->lock, nullptr);
1221     option.type = CONNECT_BR;
1222     (void)strcpy_s(option.brOption.brMac, BT_MAC_LEN, "abc");
1223     ListInit(&g_brManager.pendings->list);
1224     (void)strcpy_s(pendInfo.addr, BT_MAC_LEN, "abc");
1225     pendInfo.startTimestamp = 0xffffffff;
1226     pendInfo.duration = 0xffffffff;
1227     pendInfo.firstDuration = 1;
1228     pendInfo.firstStartTimestamp = 1;
1229     it.pendInfo = &pendInfo;
1230     ListTailInsert(&g_brManager.pendings->list, &it.node);
1231     g_brManagerAsyncHandler.handler.looper->RemoveMessageCustom = RvMessageCustom;
1232     g_brManagerAsyncHandler.handler.looper->PostMessageDelay = PostMessageDelay;
1233     ret = BrPendConnection(&option, time);
1234     EXPECT_EQ(SOFTBUS_OK, ret);
1235 }
1236 
1237 HWTEST_F(ConnectionBrConnectionTest, testBrManager038, TestSize.Level1)
1238 {
1239     int32_t ret;
1240     ConnectOption option;
1241     uint32_t time = 10;
1242     BrPending it;
1243     ConnBrPendInfo pendInfo;
1244 
1245     SoftBusMutexInit(&g_brManager.pendings->lock, nullptr);
1246     option.type = CONNECT_BR;
1247     (void)strcpy_s(option.brOption.brMac, BT_MAC_LEN, "abc");
1248     ListInit(&g_brManager.pendings->list);
1249     ListInit(&g_brManager.connections->list);
1250     (void)strcpy_s(pendInfo.addr, BT_MAC_LEN, "abce");
1251     it.pendInfo = &pendInfo;
1252     ListTailInsert(&g_brManager.pendings->list, &it.node);
1253     g_brManagerAsyncHandler.handler.looper->PostMessageDelay = PostMessageDelay;
1254     ret = BrPendConnection(&option, time);
1255     EXPECT_EQ(SOFTBUS_OK, ret);
1256 }
1257 
1258 HWTEST_F(ConnectionBrConnectionTest, testBrManager039, TestSize.Level1)
1259 {
1260     NiceMock<ConnectionBrInterfaceMock> brMock;
1261 
1262     EXPECT_CALL(brMock, SoftBusGetBtMacAddr).WillRepeatedly(Return(SOFTBUS_INVALID_PARAM));
1263     EXPECT_NO_FATAL_FAILURE(DumpLocalBtMac());
1264 
1265     EXPECT_CALL(brMock, SoftBusGetBtMacAddr).WillRepeatedly(Return(SOFTBUS_OK));
1266     EXPECT_NO_FATAL_FAILURE(DumpLocalBtMac());
1267 }
1268 
1269 HWTEST_F(ConnectionBrConnectionTest, testBrManager040, TestSize.Level1)
1270 {
1271     int32_t listenerId = 0;
1272     int32_t state = SOFTBUS_BR_STATE_TURN_ON;
1273     NiceMock<ConnectionBrInterfaceMock> brMock;
1274 
1275     EXPECT_CALL(brMock, SoftBusGetBtMacAddr).WillRepeatedly(Return(SOFTBUS_INVALID_PARAM));
1276     EXPECT_NO_FATAL_FAILURE(OnBtStateChanged(listenerId, state));
1277 
1278     state = SOFTBUS_BR_STATE_TURN_OFF;
1279     EXPECT_NO_FATAL_FAILURE(OnBtStateChanged(listenerId, state));
1280 }
1281 
1282 HWTEST_F(ConnectionBrConnectionTest, testBrManager041, TestSize.Level1)
1283 {
1284     const char *addr = "11:22:33:44:55:66";
1285     InitBrManager();
1286     ConnBrConnection *connection = ConnBrCreateConnection(addr, CONN_SIDE_CLIENT, -1);
1287     ASSERT_TRUE(connection != nullptr);
1288     int32_t ret = ConnBrSaveConnection(connection);
1289     EXPECT_EQ(SOFTBUS_OK, ret);
1290     int32_t error = SOFTBUS_CONN_BR_UNDERLAY_CONNECT_FAIL;
1291     bool isWait = IsNeedWaitCallbackError(connection->connectionId, &error);
1292     EXPECT_EQ(true, isWait);
1293 
1294     BrUnderlayerStatus *callbackStatus = (BrUnderlayerStatus *)SoftBusCalloc(sizeof(BrUnderlayerStatus));
1295     ASSERT_NE(nullptr, callbackStatus);
1296     ASSERT_TRUE(callbackStatus != nullptr);
1297     ListInit(&callbackStatus->node);
1298     callbackStatus->status = 0;
1299     callbackStatus->result = 4;
1300     ListAdd(&connection->connectProcessStatus->list, &callbackStatus->node);
1301     isWait = IsNeedWaitCallbackError(connection->connectionId, &error);
1302     EXPECT_EQ(false, isWait);
1303 
1304     BrUnderlayerStatus *it = nullptr;
1305     LIST_FOR_EACH_ENTRY(it, &connection->connectProcessStatus->list, BrUnderlayerStatus, node) {
1306         if (it->result == 4) {
1307             it->result = CONN_BR_CONNECT_UNDERLAYER_ERROR_UNDEFINED + 1;
1308         }
1309     }
1310     isWait = IsNeedWaitCallbackError(connection->connectionId, &error);
1311     EXPECT_EQ(true, isWait);
1312 }
1313 
1314 HWTEST_F(ConnectionBrConnectionTest, BrReuseConnection, TestSize.Level1)
1315 {
1316     const char *mac = "11:22:33:44:55:66";
1317     ConnBrConnection *connection = ConnBrCreateConnection(mac, CONN_SIDE_CLIENT, 1);
1318     ASSERT_NE(nullptr, connection);
1319     connection->state = BR_CONNECTION_STATE_CONNECTED;
1320     ASSERT_EQ(SOFTBUS_OK, ConnBrSaveConnection(connection));
1321     ConnectCallback callback = {
1322         .OnConnected = OnConnected,
1323         .OnReusedConnected = OnReusedConnected,
1324         .OnDisconnected = OnDisconnected,
1325         .OnDataReceived = OnDataReceived,
1326     };
1327     ClientConnected(1);
1328     ConnectFuncInterface *bleInterface = ConnInitBle(&callback);
1329     ASSERT_NE(nullptr, bleInterface);
1330 
1331     ConnBrDevice device = {
1332         .bleKeepAliveInfo.keepAliveBleConnectionId = 1,
1333         .bleKeepAliveInfo.keepAliveBleRequestId = 1,
1334     };
1335     EXPECT_EQ(EOK, memcpy_s(device.addr, BT_MAC_LEN, mac, BT_MAC_LEN));
1336     bool ret = BrReuseConnection(&device, connection);
1337     EXPECT_EQ(true, ret);
1338     ServerAccepted(1);
1339     ConnBrDevice *device1 = (ConnBrDevice *)SoftBusCalloc(sizeof(ConnBrDevice));
1340     ASSERT_NE(nullptr, device1);
1341     ClientConnectFailed(1, SOFTBUS_CONN_BR_INTERNAL_ERR);
1342     EXPECT_EQ(EOK, memcpy_s(device1->addr, BT_MAC_LEN, mac, BT_MAC_LEN));
1343     AttempReuseConnect(device1, ConnectDeviceDirectly);
1344 }
1345 
1346 HWTEST_F(ConnectionBrConnectionTest, BrOnOccupyRelease, TestSize.Level1)
1347 {
1348     InitBrManager();
1349     const char *mac = "11:00:33:44:55:66";
1350     ConnBrConnection *connection = ConnBrCreateConnection(mac, CONN_SIDE_CLIENT, 1);
1351     ASSERT_NE(nullptr, connection);
1352     connection->isOccupied = true;
1353     int32_t ret = ConnBrSaveConnection(connection);
1354     ASSERT_EQ(SOFTBUS_OK, ret);
1355 
1356     SoftBusMessage msg = {
1357         .what = MSG_CONNECTION_OCCUPY_RELEASE,
1358         .arg1 = connection->connectionId,
1359     };
1360     BrConnectionMsgHandler(&msg);
1361     msg.what = MSG_CONNECTION_UPDATE_PEER_RC + 1;
1362     BrConnectionMsgHandler(&msg);
1363     EXPECT_EQ(false, connection->isOccupied);
1364 
1365     connection->state = BR_CONNECTION_STATE_EXCEPTION;
1366     ret = ConnBrSaveConnection(connection);
1367     ASSERT_EQ(SOFTBUS_OK, ret);
1368 }
1369 
1370 HWTEST_F(ConnectionBrConnectionTest, WaitNegotiationClosingTimeoutHandler, TestSize.Level1)
1371 {
1372     InitBrManager();
1373     const char *mac = "11:00:33:44:55:77";
1374     ConnBrConnection *connection = ConnBrCreateConnection(mac, CONN_SIDE_CLIENT, 0);
1375     ASSERT_NE(nullptr, connection);
1376     connection->state = BR_CONNECTION_STATE_EXCEPTION;
1377     int32_t ret = ConnBrSaveConnection(connection);
1378     ASSERT_EQ(SOFTBUS_OK, ret);
1379     WaitNegotiationClosingTimeoutHandler(connection->connectionId);
1380 
1381     const char *addr = "11:00:33:44:55:22";
1382     ConnBrConnection *brConnection = ConnBrCreateConnection(addr, CONN_SIDE_CLIENT, 0);
1383     ASSERT_NE(nullptr, brConnection);
1384     brConnection->state = BR_CONNECTION_STATE_NEGOTIATION_CLOSING;
1385     brConnection->socketHandle = INVALID_SOCKET_HANDLE;
1386     ret = ConnBrSaveConnection(brConnection);
1387     ASSERT_EQ(SOFTBUS_OK, ret);
1388     WaitNegotiationClosingTimeoutHandler(brConnection->connectionId);
1389     EXPECT_EQ(BR_CONNECTION_STATE_CLOSED, brConnection->state);
1390 }
1391 } // namespace OHOS