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