1 /*
2 * Copyright (c) 2024 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 <cstring>
18
19 #include <gtest/gtest.h>
20 #include <securec.h>
21
22 #include "ble_protocol_interface_factory.h"
23 #include "softbus_conn_ble_manager_mock.h"
24 #include "softbus_adapter_ble_conflict.h"
25 #include "softbus_adapter_bt_common.h"
26 #include "softbus_adapter_crypto.h"
27 #include "softbus_adapter_mem.h"
28 #include "softbus_conn_ble_connection.h"
29 #include "softbus_conn_ble_manager.h"
30 #include "softbus_conn_ble_trans.h"
31 #include "softbus_conn_interface.h"
32 #include "softbus_error_code.h"
33 #include "softbus_feature_config.h"
34 #include "softbus_utils.h"
35 #include "softbus_conn_ble_manager.c"
36
37 using namespace testing::ext;
38 using namespace testing;
39 namespace OHOS {
40
41 #define SHORT_UDID_HASH_LEN 8
42 #define MAX_SIZE 100
43 #define SLEEP_TIME_MS 1000
44 #define WAIT_UPDATE_TIME_MS 3500
45
46 static ConnBleTransEventListener g_transEventListener = { 0 };
47 static SoftBusBtStateListener g_btListener = { 0 };
48 static SoftBusBleConflictListener g_conflictListener = { 0 };
49 static int32_t g_listenerId = 0;
50 static ConnectFuncInterface *g_bleInterface = nullptr;
OnConnected(uint32_t connectionId,const ConnectionInfo * info)51 void OnConnected(uint32_t connectionId, const ConnectionInfo *info)
52 {
53 (void)connectionId;
54 (void)info;
55 }
56
OnReusedConnected(uint32_t connectionId,const ConnectionInfo * info)57 void OnReusedConnected(uint32_t connectionId, const ConnectionInfo *info)
58 {
59 (void)connectionId;
60 (void)info;
61 }
62
OnDisconnected(uint32_t connectionId,const ConnectionInfo * info)63 void OnDisconnected(uint32_t connectionId, const ConnectionInfo *info)
64 {
65 (void)connectionId;
66 (void)info;
67 }
68
OnDataReceived(uint32_t connectionId,ConnModule moduleId,int64_t seq,char * data,int32_t len)69 void OnDataReceived(uint32_t connectionId, ConnModule moduleId, int64_t seq, char *data, int32_t len)
70 {
71 (void)connectionId;
72 (void)moduleId;
73 (void)seq;
74 (void)data;
75 (void)len;
76 }
77
OnConnectSuccessed(uint32_t requestId,uint32_t connectionId,const ConnectionInfo * info)78 void OnConnectSuccessed(uint32_t requestId, uint32_t connectionId, const ConnectionInfo *info)
79 {
80 (void)requestId;
81 (void)connectionId;
82 (void)info;
83 }
84
OnConnectFailed(uint32_t requestId,int32_t reason)85 void OnConnectFailed(uint32_t requestId, int32_t reason)
86 {
87 (void)requestId;
88 (void)reason;
89 }
90
91 extern "C" {
ConnBleInitTransModule(ConnBleTransEventListener * listener)92 int32_t ConnBleInitTransModule(ConnBleTransEventListener *listener)
93 {
94 if (listener == nullptr) {
95 return SOFTBUS_INVALID_PARAM;
96 }
97 g_transEventListener = *listener;
98 return SOFTBUS_OK;
99 }
100
SoftBusAddBtStateListener(const SoftBusBtStateListener * listener,int32_t * listenerId)101 int32_t SoftBusAddBtStateListener(const SoftBusBtStateListener *listener, int32_t *listenerId)
102 {
103 if (listener == nullptr || listenerId == nullptr) {
104 return SOFTBUS_INVALID_PARAM;
105 }
106 g_btListener = *listener;
107 if (g_listenerId > MAX_SIZE) {
108 g_listenerId = 0;
109 }
110 *listenerId = g_listenerId++;
111 return SOFTBUS_OK;
112 }
113
SoftbusBleConflictNotifyDateReceive(int32_t underlayerHandle,const uint8_t * data,uint32_t dataLen)114 void SoftbusBleConflictNotifyDateReceive(int32_t underlayerHandle, const uint8_t *data, uint32_t dataLen)
115 {
116 (void)underlayerHandle;
117 (void)data;
118 (void)dataLen;
119 }
120
SoftbusBleConflictNotifyDisconnect(const char * addr,const char * udid)121 void SoftbusBleConflictNotifyDisconnect(const char *addr, const char *udid)
122 {
123 (void)addr;
124 (void)udid;
125 }
126
SoftbusBleConflictNotifyConnectResult(uint32_t requestId,int32_t underlayerHandle,bool status)127 void SoftbusBleConflictNotifyConnectResult(uint32_t requestId, int32_t underlayerHandle, bool status)
128 {
129 (void)requestId;
130 (void)underlayerHandle;
131 (void)status;
132 }
133
LegacyBleReturnConnection(ConnBleConnection ** connection)134 void LegacyBleReturnConnection(ConnBleConnection **connection)
135 {
136 (void)connection;
137 }
138
SoftbusBleConflictRegisterListener(SoftBusBleConflictListener * listener)139 void SoftbusBleConflictRegisterListener(SoftBusBleConflictListener *listener)
140 {
141 if (listener == nullptr) {
142 return;
143 }
144 g_conflictListener = *listener;
145 }
146 }
147 class ConnectionBleManagerTest : public testing::Test {
148 public:
SetUpTestCase()149 static void SetUpTestCase() {};
150 static void TearDownTestCase();
SetUp()151 void SetUp() override
152 {
153 ConnectCallback connectCb = { 0 };
154 connectCb.OnConnected = OnConnected;
155 connectCb.OnReusedConnected = OnReusedConnected;
156 connectCb.OnDisconnected = OnDisconnected;
157 connectCb.OnDataReceived = OnDataReceived;
158
159 LooperInit();
160 SoftbusConfigInit();
161
162 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
163 EXPECT_CALL(bleMock, ConnGattInitClientModule).WillRepeatedly(Return(SOFTBUS_OK));
164 EXPECT_CALL(bleMock, ConnGattInitServerModule).WillRepeatedly(Return(SOFTBUS_OK));
165 g_bleInterface = ConnInitBle(&connectCb);
166 ASSERT_NE(g_bleInterface, nullptr);
167 }
TearDown()168 void TearDown() override
169 {
170 LooperDeinit();
171 }
172 };
173
TearDownTestCase()174 void ConnectionBleManagerTest::TearDownTestCase()
175 {
176 SoftBusSleepMs(SLEEP_TIME_MS);
177 }
178 /*
179 * @tc.name: TestTransListener001
180 * @tc.desc: Test TransListener.
181 * @tc.in: Test module, Test number, Test Levels.
182 * @tc.out: Zero
183 * @tc.type: FUNC
184 * @tc.require:
185 */
186 HWTEST_F(ConnectionBleManagerTest, TestTransListener001, TestSize.Level1)
187 {
188 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
189 EXPECT_CALL(bleMock, ConnGattClientDisconnect).WillRepeatedly(Return(SOFTBUS_OK));
190 g_btListener.OnBtStateChanged(g_listenerId, SOFTBUS_BLE_STATE_TURN_OFF);
191
192 EXPECT_CALL(bleMock, ConnGattServerStartService).WillOnce(Return(SOFTBUS_OK));
193 g_btListener.OnBtStateChanged(g_listenerId, SOFTBUS_BLE_STATE_TURN_ON);
194
195 uint32_t connectionId = 13000;
196 uint32_t len = 100;
197 int32_t pid = 0;
198 int32_t flag = 1;
199 int32_t module = MODULE_CONNECTION;
200 int64_t seq = 1000;
201 int32_t error = SOFTBUS_INVALID_PARAM;
202 g_transEventListener.onPostBytesFinished(connectionId, len, pid, flag, module, seq, error);
203
204 const char *addr = "22:33:44:55:66:77";
205 ConnBleConnection *connection =
206 ConnBleCreateConnection(addr, BLE_GATT, CONN_SIDE_CLIENT, INVALID_UNDERLAY_HANDLE, true);
207 ASSERT_NE(connection, nullptr);
208 connection->underlayerHandle = 3;
209 int32_t ret = ConnBleSaveConnection(connection);
210 ASSERT_EQ(SOFTBUS_OK, ret);
211 g_transEventListener.onPostBytesFinished(connection->connectionId, len, pid, flag, module, seq, error);
212 SoftBusSleepMs(SLEEP_TIME_MS);
213 }
214
215 /*
216 * @tc.name: TestConflictGetConnection001
217 * @tc.desc: Test ConflictGetConnection.
218 * @tc.in: Test module, Test number, Test Level
219 * @tc.out: Zero
220 * @tc.type: FUNC
221 * @tc.require:
222 */
223 HWTEST_F(ConnectionBleManagerTest, TestConflictGetConnection001, TestSize.Level1)
224 {
225 const char *addr = "11:22:33:44:55:66";
226 const char *udid = "1111222233334444";
227
228 g_conflictListener.cancelOccupy(udid);
229 ConnBleConnection *connection =
230 ConnBleCreateConnection(addr, BLE_GATT, CONN_SIDE_CLIENT, INVALID_UNDERLAY_HANDLE, true);
231 ASSERT_NE(connection, nullptr);
232 int32_t ret = strcpy_s(connection->udid, UDID_BUF_LEN, udid);
233 ASSERT_EQ(EOK, ret);
234
235 connection->underlayerHandle = 2;
236 ret = ConnBleSaveConnection(connection);
237 ASSERT_EQ(SOFTBUS_OK, ret);
238 ret = g_conflictListener.getConnection(udid);
239 EXPECT_EQ(2, ret);
240
241 int32_t underlayHandle = 2;
242 uint8_t *data = (uint8_t *)malloc(sizeof(uint8_t));
243 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
244 EXPECT_CALL(bleMock, ConnBlePostBytesInner).WillOnce(Return(SOFTBUS_OK));
245 bool res = g_conflictListener.postBytes(underlayHandle, data, sizeof(uint8_t));
246 EXPECT_EQ(true, res);
247
248 EXPECT_CALL(bleMock, ConnBlePostBytesInner).WillOnce(Return(SOFTBUS_INVALID_PARAM));
249 res = g_conflictListener.postBytes(underlayHandle, data, sizeof(uint8_t));
250 EXPECT_EQ(false, res);
251
252 g_conflictListener.occupy(udid, 1000);
253 g_conflictListener.occupy(udid, 1500);
254 g_conflictListener.cancelOccupy(udid);
255 char invaildUdid[100] = { 0 };
256 (void)memset_s(invaildUdid, 100, '1', 100);
257 g_conflictListener.occupy(invaildUdid, 2000);
258 }
259
260 /*
261 * @tc.name: TestConflictDisconnect001
262 * @tc.desc: Test ConflictDisconnect.
263 * @tc.in: Test module, Test number, Test Levels.
264 * @tc.out: Zero
265 * @tc.type: FUNC
266 * @tc.require:
267 */
268 HWTEST_F(ConnectionBleManagerTest, TestConflictDisconnect001, TestSize.Level1)
269 {
270 const char *addr = "11:22:33:44:55:66";
271 const char *udid = "1111222233334444";
272 ConnBleConnection *connection =
273 ConnBleCreateConnection(addr, BLE_GATT, CONN_SIDE_CLIENT, INVALID_UNDERLAY_HANDLE, true);
274 ASSERT_NE(connection, nullptr);
275 int32_t ret = strcpy_s(connection->udid, UDID_BUF_LEN, udid);
276 ASSERT_EQ(EOK, ret);
277
278 connection->underlayerHandle = 1;
279 connection->state = BLE_CONNECTION_STATE_EXCHANGED_BASIC_INFO;
280 ret = ConnBleSaveConnection(connection);
281 EXPECT_EQ(SOFTBUS_OK, ret);
282 uint32_t requestId = 1;
283 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
284 EXPECT_CALL(bleMock, ConnBlePackCtlMessage).WillOnce(Return(10));
285 EXPECT_CALL(bleMock, ConnBlePostBytesInner).WillOnce(Return(SOFTBUS_OK));
286 ret = g_conflictListener.reuseConnection(addr, udid, requestId);
287 EXPECT_EQ(1, ret);
288 const char *invaildAddr = "11:22:33:44:55:66:77:88:99";
289
290 ret = g_conflictListener.reuseConnection(invaildAddr, udid, requestId);
291 EXPECT_EQ(SOFTBUS_MEM_ERR, ret);
292
293 EXPECT_CALL(bleMock, ConnBlePackCtlMessage).WillOnce(Return(10));
294 EXPECT_CALL(bleMock, ConnBlePostBytesInner).WillOnce(Return(SOFTBUS_INVALID_PARAM));
295 ret = g_conflictListener.reuseConnection(addr, udid, requestId);
296 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
297 EXPECT_CALL(bleMock, ConnGattClientDisconnect).WillRepeatedly(Return(SOFTBUS_OK));
298 g_conflictListener.disconnect(1, true);
299
300 const char *bleAddr = "00:22:33:44:55:66";
301 const char *bleUdid = "1100222233334444";
302 ConnBleConnection *bleConnection =
303 ConnBleCreateConnection(bleAddr, BLE_GATT, CONN_SIDE_CLIENT, INVALID_UNDERLAY_HANDLE, true);
304 ASSERT_NE(nullptr, bleConnection);
305 ret = strcpy_s(bleConnection->udid, UDID_BUF_LEN, bleUdid);
306 ASSERT_EQ(EOK, ret);
307 bleConnection->underlayerHandle = 10;
308 ret = ConnBleSaveConnection(bleConnection);
309 ASSERT_EQ(SOFTBUS_OK, ret);
310 EXPECT_CALL(bleMock, ConnGattClientDisconnect).WillRepeatedly(Return(SOFTBUS_OK));
311 g_conflictListener.disconnect(10, false);
312 }
313
314 /*
315 * @tc.name: TestBleInterface001
316 * @tc.desc: Test BleInterface.
317 * @tc.in: Test module, Test number, Test Levels.
318 * @tc.out: Zero
319 * @tc.type: FUNC
320 * @tc.require:
321 */
322 HWTEST_F(ConnectionBleManagerTest, TestBleInterface001, TestSize.Level1)
323 {
324 const char *deviceId = "1234567";
325 const char *bleMac = "11:22:33:44:33:00";
326 const char *udid = "1119222233334440";
327 ConnectOption option = {
328 .type = CONNECT_BLE,
329 .bleOption.bleMac = "",
330 .bleOption.deviceIdHash = "",
331 .bleOption.protocol = BLE_GATT,
332 .bleOption.psm = 5,
333 .bleOption.challengeCode = 0,
334 };
335 int32_t ret = strcpy_s(option.bleOption.bleMac, BT_MAC_LEN, bleMac);
336 ASSERT_EQ(EOK, ret);
337 ret = memcpy_s(option.bleOption.deviceIdHash, UDID_HASH_LEN, deviceId, UDID_HASH_LEN);
338 ASSERT_EQ(EOK, ret);
339 uint32_t requestId = 10;
340 ConnectResult result = {
341 .OnConnectSuccessed = OnConnectSuccessed,
342 .OnConnectFailed = OnConnectFailed,
343 };
344 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
345 EXPECT_CALL(bleMock, LnnGetConnSubFeatureByUdidHashStr).WillRepeatedly(Return(SOFTBUS_OK));
346 g_bleInterface->ConnectDevice(&option, requestId, &result);
347
348 EXPECT_CALL(bleMock, ConnBlePostBytesInner).WillRepeatedly(Return(SOFTBUS_OK));
349 uint32_t connectionId = 131001;
350 uint8_t *data = (uint8_t *)malloc(sizeof(uint8_t));
351 uint32_t dataLen = sizeof(uint8_t);
352 int32_t pid = 0;
353 int32_t flag = 2;
354 int32_t module = MODULE_CONNECTION;
355 int64_t seq = 100;
356 ret = g_bleInterface->PostBytes(connectionId, data, dataLen, pid, flag, module, seq);
357 EXPECT_EQ(SOFTBUS_OK, ret);
358
359 ConnBleConnection *connection =
360 ConnBleCreateConnection(bleMac, BLE_GATT, CONN_SIDE_CLIENT, INVALID_UNDERLAY_HANDLE, false);
361 ASSERT_NE(connection, nullptr);
362 ret = strcpy_s(connection->udid, UDID_BUF_LEN, udid);
363 ASSERT_EQ(EOK, ret);
364 connection->underlayerHandle = 2;
365 ret = ConnBleSaveConnection(connection);
366 EXPECT_EQ(SOFTBUS_OK, ret);
367 ret = g_bleInterface->DisconnectDevice(connection->connectionId);
368 EXPECT_EQ(SOFTBUS_OK, ret);
369 SoftBusSleepMs(SLEEP_TIME_MS);
370 }
371
372 /*
373 * @tc.name: TestBleInterface002
374 * @tc.desc: Test BleInterface.
375 * @tc.in: Test module, Test number, Test Levels.
376 * @tc.out: Zero
377 * @tc.type: FUNC
378 * @tc.require:
379 */
380 HWTEST_F(ConnectionBleManagerTest, TestBleInterface002, TestSize.Level1)
381 {
382 const char *bleMac = "44:11:33:44:33:00";
383 const char *udid = "1119222233334419";
384
385 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
386 EXPECT_CALL(bleMock, LnnGetConnSubFeatureByUdidHashStr).WillRepeatedly(Return(SOFTBUS_OK));
387 ConnBleConnection *connection =
388 ConnBleCreateConnection(bleMac, BLE_GATT, CONN_SIDE_CLIENT, INVALID_UNDERLAY_HANDLE, false);
389 ASSERT_NE(connection, nullptr);
390 int32_t ret = strcpy_s(connection->udid, UDID_BUF_LEN, udid);
391
392 ASSERT_EQ(EOK, ret);
393 connection->underlayerHandle = 10;
394 ret = ConnBleSaveConnection(connection);
395 EXPECT_EQ(SOFTBUS_OK, ret);
396
397 ConnectOption option = {
398 .type = CONNECT_BLE,
399 .bleOption.bleMac = "",
400 .bleOption.deviceIdHash = "",
401 .bleOption.protocol = BLE_GATT,
402 .bleOption.psm = 5,
403 .bleOption.challengeCode = 0,
404 };
405 ret = strcpy_s(option.bleOption.bleMac, BT_MAC_LEN, bleMac);
406 ASSERT_EQ(EOK, ret);
407 EXPECT_CALL(bleMock, ConnGattClientDisconnect).WillRepeatedly(Return(SOFTBUS_OK));
408 ret = g_bleInterface->DisconnectDeviceNow(&option);
409 EXPECT_EQ(SOFTBUS_OK, ret);
410
411 ConnBleConnection *serverConnection =
412 ConnBleCreateConnection(bleMac, BLE_GATT, CONN_SIDE_SERVER, INVALID_UNDERLAY_HANDLE, false);
413 ASSERT_NE(serverConnection, nullptr);
414 serverConnection->underlayerHandle = 50;
415 ret = ConnBleSaveConnection(serverConnection);
416 EXPECT_EQ(SOFTBUS_OK, ret);
417 EXPECT_CALL(bleMock, ConnGattServerDisconnect).WillRepeatedly(Return(SOFTBUS_OK));
418 ret = g_bleInterface->DisconnectDeviceNow(&option);
419 EXPECT_EQ(SOFTBUS_OK, ret);
420 SoftBusSleepMs(SLEEP_TIME_MS);
421 }
422
423 /*
424 * @tc.name: TestBleInterface003
425 * @tc.desc: Test BleInterface.
426 * @tc.in: Test module, Test number, Test Levels.
427 * @tc.out: Zero
428 * @tc.type: FUNC
429 * @tc.require:
430 */
431 HWTEST_F(ConnectionBleManagerTest, TestBleInterface003, TestSize.Level1)
432 {
433 const char *bleMac = "77:22:33:44:33:00";
434 const char *udid = "1254222233334419";
435 const char *invaildMac = "77:22:33:44:33:00999";
436 const char *networkId = "testnetworkid123";
437 ConnBleConnection *connection =
438 ConnBleCreateConnection(invaildMac, BLE_COC, CONN_SIDE_CLIENT, INVALID_UNDERLAY_HANDLE, false);
439 EXPECT_EQ(connection, nullptr);
440 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
441 EXPECT_CALL(bleMock, LnnGetConnSubFeatureByUdidHashStr).WillRepeatedly(Return(SOFTBUS_OK));
442 ConnBleConnection *bleConnection =
443 ConnBleCreateConnection(bleMac, BLE_COC, CONN_SIDE_CLIENT, INVALID_UNDERLAY_HANDLE, false);
444 ASSERT_NE(bleConnection, nullptr);
445 bleConnection->state = BLE_CONNECTION_STATE_EXCHANGED_BASIC_INFO;
446 bleConnection->featureBitSet = false;
447 bleConnection->psm = 10;
448 int32_t ret = strcpy_s(bleConnection->udid, UDID_BUF_LEN, udid);
449 EXPECT_EQ(ret, EOK);
450 ret = strncpy_s(bleConnection->networkId, NETWORK_ID_BUF_LEN, networkId, strlen(networkId));
451 EXPECT_EQ(ret, EOK);
452 ret = ConnBleSaveConnection(bleConnection);
453 EXPECT_EQ(SOFTBUS_OK, ret);
454 EXPECT_CALL(bleMock, LnnGetRemoteStrInfo).WillRepeatedly(Return(SOFTBUS_OK));
455 ConnectionInfo info = { 0 };
456 ret = g_bleInterface->GetConnectionInfo(bleConnection->connectionId, &info);
457 EXPECT_EQ(SOFTBUS_OK, ret);
458
459 ConnBleConnection *invalidConnection =
460 ConnBleCreateConnection(bleMac, BLE_COC, CONN_SIDE_CLIENT, INVALID_UNDERLAY_HANDLE, false);
461 ASSERT_NE(invalidConnection, nullptr);
462 invalidConnection->state = BLE_CONNECTION_STATE_EXCHANGING_BASIC_INFO;
463 invalidConnection->featureBitSet = true;
464 invalidConnection->psm = 10;
465 const char *invaildUdid = "";
466 ret = strcpy_s(invalidConnection->udid, UDID_BUF_LEN, invaildUdid);
467 EXPECT_EQ(ret, EOK);
468 ret = strncpy_s(invalidConnection->networkId, NETWORK_ID_BUF_LEN, networkId, strlen(networkId));
469 EXPECT_EQ(ret, EOK);
470 ret = ConnBleSaveConnection(invalidConnection);
471 EXPECT_EQ(SOFTBUS_OK, ret);
472 ret = g_bleInterface->GetConnectionInfo(invalidConnection->connectionId, &info);
473 EXPECT_EQ(SOFTBUS_OK, ret);
474
475 invalidConnection->featureBitSet = false;
476 invalidConnection->protocol = BLE_GATT;
477 ret = ConnBleSaveConnection(invalidConnection);
478 EXPECT_EQ(SOFTBUS_OK, ret);
479
480 ret = g_bleInterface->GetConnectionInfo(invalidConnection->connectionId, &info);
481 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
482 }
483
484 /*
485 * @tc.name: TestBleInterface004
486 * @tc.desc: Test BleInterface.
487 * @tc.in: Test module, Test number, Test Levels.
488 * @tc.out: Zero
489 * @tc.type: FUNC
490 * @tc.require:
491 */
492 HWTEST_F(ConnectionBleManagerTest, TestBleInterface004, TestSize.Level1)
493 {
494 LocalListenerInfo info = {};
495 int32_t ret = g_bleInterface->StartLocalListening(&info);
496 EXPECT_EQ(SOFTBUS_OK, ret);
497
498 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
499 EXPECT_CALL(bleMock, ConnGattServerStopService).WillOnce(Return(SOFTBUS_OK));
500 ret = g_bleInterface->StopLocalListening(&info);
501 EXPECT_EQ(SOFTBUS_OK, ret);
502
503 ret = g_bleInterface->StopLocalListening(&info);
504 EXPECT_EQ(SOFTBUS_OK, ret);
505
506 EXPECT_CALL(bleMock, ConnGattServerStartService)
507 .WillOnce(Return(SOFTBUS_CONN_BLE_UNDERLAY_SERVER_ADD_SERVICE_ERR));
508 ret = g_bleInterface->StartLocalListening(&info);
509 EXPECT_EQ(SOFTBUS_OK, ret);
510 }
511
512 /*
513 * @tc.name: TestBleInterface005
514 * @tc.desc: Test BleInterface.
515 * @tc.in: Test module, Test number, Test Levels.
516 * @tc.out: Zero
517 * @tc.type: FUNC
518 * @tc.require:
519 */
520 HWTEST_F(ConnectionBleManagerTest, TestBleInterface005, TestSize.Level1)
521 {
522 ConnectOption option = {};
523 option.type = CONNECT_BLE;
524 const char *udid = "1234";
525
526 int32_t ret = memcpy_s(option.bleOption.deviceIdHash, sizeof(udid), udid, sizeof(udid));
527 EXPECT_EQ(ret, EOK);
528 option.bleOption.protocol = BLE_GATT;
529 char hashStr[HEXIFY_LEN(SHORT_UDID_HASH_LEN)] = { 0 };
530 ret = ConvertBytesToHexString(
531 hashStr, HEXIFY_LEN(SHORT_UDID_HASH_LEN), (unsigned char *)option.bleOption.deviceIdHash, SHORT_UDID_HASH_LEN);
532 EXPECT_EQ(ret, SOFTBUS_OK);
533
534 const char *bleMac = "77:02:33:44:33:00";
535 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
536 EXPECT_CALL(bleMock, LnnGetConnSubFeatureByUdidHashStr).WillRepeatedly(Return(SOFTBUS_OK));
537 ConnBleConnection *bleConnection =
538 ConnBleCreateConnection(bleMac, BLE_GATT, CONN_SIDE_CLIENT, INVALID_UNDERLAY_HANDLE, false);
539 ASSERT_NE(bleConnection, nullptr);
540
541 ret = memcpy_s(bleConnection->udid, UDID_HASH_LEN, hashStr, UDID_HASH_LEN);
542 EXPECT_EQ(ret, EOK);
543 bleConnection->state = BLE_CONNECTION_STATE_EXCHANGED_BASIC_INFO;
544 ret = ConnBleSaveConnection(bleConnection);
545 EXPECT_EQ(ret, EOK);
546
547 EXPECT_CALL(bleMock, LnnGetRemoteStrInfo).WillRepeatedly(Return(SOFTBUS_OK));
548 bool res = g_bleInterface->CheckActiveConnection(&option, false);
549 EXPECT_EQ(res, true);
550
551 UpdateOption options = {
552 .type = CONNECT_BLE,
553 .bleOption = {
554 .priority = CONN_BLE_PRIORITY_BALANCED,
555 }
556 };
557 EXPECT_CALL(bleMock, ConnGattClientUpdatePriority).WillRepeatedly(Return(SOFTBUS_OK));
558 ret = g_bleInterface->UpdateConnection(bleConnection->connectionId, &options);
559 EXPECT_EQ(ret, SOFTBUS_OK);
560 bleConnection->side = CONN_SIDE_SERVER;
561 ret = ConnBleSaveConnection(bleConnection);
562 EXPECT_EQ(ret, EOK);
563 ret = g_bleInterface->UpdateConnection(bleConnection->connectionId, &options);
564 EXPECT_EQ(ret, SOFTBUS_FUNC_NOT_SUPPORT);
565 }
566
567 /*
568 * @tc.name: NotifyReusedConnected001
569 * @tc.desc: Test NotifyReusedConnected.
570 * @tc.in: Test module, Test number, Test Levels.
571 * @tc.out: Zero
572 * @tc.type: FUNC
573 * @tc.require:
574 */
575 HWTEST_F(ConnectionBleManagerTest, NotifyReusedConnected001, TestSize.Level1)
576 {
577 const char *bleMac = "21:12:33:44:33:00";
578 const char *udid = "dcba";
579 const char *networkId = "testnetworkid123";
580 ConnBleConnection *bleConnection =
581 ConnBleCreateConnection(bleMac, BLE_GATT, CONN_SIDE_CLIENT, INVALID_UNDERLAY_HANDLE, false);
582 ASSERT_NE(bleConnection, nullptr);
583 int32_t ret = memcpy_s(bleConnection->udid, UDID_HASH_LEN, udid, UDID_HASH_LEN);
584 EXPECT_EQ(ret, EOK);
585
586 bleConnection->featureBitSet = true;
587 bleConnection->state = BLE_CONNECTION_STATE_EXCHANGED_BASIC_INFO;
588 bleConnection->psm = 101;
589
590 ret = ConnBleSaveConnection(bleConnection);
591 EXPECT_EQ(ret, EOK);
592 uint16_t challengeCode = 0x12;
593 NotifyReusedConnected(bleConnection->connectionId, challengeCode);
594 ret = strcpy_s(bleConnection->networkId, NETWORK_ID_BUF_LEN, networkId);
595 EXPECT_EQ(ret, EOK);
596 ConnBleRemoveConnection(bleConnection);
597 ConnBleRemoveConnection(bleConnection);
598 }
599
600 /*
601 * @tc.name: OnBtStateChanged001
602 * @tc.desc: Test OnBtStateChanged.
603 * @tc.in: Test module, Test number, Test Levels.
604 * @tc.out: Zero
605 * @tc.type: FUNC
606 * @tc.require:
607 */
608 HWTEST_F(ConnectionBleManagerTest, OnBtStateChanged001, TestSize.Level1)
609 {
610 const char *deviceId = "1234567";
611 const char *bleMac = "11:22:33:44:33:00";
612 ConnectOption option = {
613 .type = CONNECT_BLE,
614 .bleOption.bleMac = "",
615 .bleOption.deviceIdHash = "",
616 .bleOption.protocol = BLE_GATT,
617 .bleOption.psm = 5,
618 .bleOption.challengeCode = 0,
619 };
620 int32_t ret = strcpy_s(option.bleOption.bleMac, BT_MAC_LEN, bleMac);
621 ASSERT_EQ(EOK, ret);
622 ret = memcpy_s(option.bleOption.deviceIdHash, UDID_HASH_LEN, deviceId, UDID_HASH_LEN);
623 ASSERT_EQ(EOK, ret);
624 uint32_t requestId = 10;
625 ConnectResult result = {
626 .OnConnectSuccessed = OnConnectSuccessed,
627 .OnConnectFailed = OnConnectFailed,
628 };
629 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
630 EXPECT_CALL(bleMock, LnnGetConnSubFeatureByUdidHashStr).WillRepeatedly(Return(SOFTBUS_OK));
631 ret = g_bleInterface->ConnectDevice(&option, requestId, &result);
632 EXPECT_EQ(ret, SOFTBUS_OK);
633 g_btListener.OnBtStateChanged(g_listenerId, SOFTBUS_BLE_STATE_TURN_OFF);
634 SoftBusSleepMs(SLEEP_TIME_MS);
635 }
636
637 /*
638 * @tc.name: ConnectDevice001
639 * @tc.desc: Test ConnectDevice.
640 * @tc.in: Test module, Test number, Test Levels.
641 * @tc.out: Zero
642 * @tc.type: FUNC
643 * @tc.require:
644 */
645 HWTEST_F(ConnectionBleManagerTest, ConnectDevice001, TestSize.Level1)
646 {
647 const char *deviceId = "1234567";
648 const char *bleMac = "11:22:33:44:33:00";
649 ConnectOption option = {
650 .type = CONNECT_BLE,
651 .bleOption.bleMac = "",
652 .bleOption.deviceIdHash = "",
653 .bleOption.protocol = BLE_GATT,
654 .bleOption.psm = 5,
655 .bleOption.challengeCode = 0,
656 };
657 int32_t ret = strcpy_s(option.bleOption.bleMac, BT_MAC_LEN, bleMac);
658 ASSERT_EQ(EOK, ret);
659 ret = memcpy_s(option.bleOption.deviceIdHash, UDID_HASH_LEN, deviceId, UDID_HASH_LEN);
660 ASSERT_EQ(EOK, ret);
661 uint32_t requestId = 10;
662 ConnectResult result = {
663 .OnConnectSuccessed = OnConnectSuccessed,
664 .OnConnectFailed = OnConnectFailed,
665 };
666 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
667 EXPECT_CALL(bleMock, LnnGetConnSubFeatureByUdidHashStr).WillRepeatedly(Return(SOFTBUS_OK));
668 ret = g_bleInterface->ConnectDevice(&option, requestId, &result);
669 EXPECT_EQ(ret, SOFTBUS_OK);
670 g_bleInterface->ConnectDevice(&option, requestId, &result);
671 EXPECT_EQ(ret, SOFTBUS_OK);
672 SoftBusSleepMs(SLEEP_TIME_MS);
673 }
674
675 /*
676 * @tc.name: ConnectDevice002
677 * @tc.desc: Test ConnectDevice.
678 * @tc.in: Test module, Test number, Test Levels.
679 * @tc.out: Zero
680 * @tc.type: FUNC
681 * @tc.require:
682 */
683 HWTEST_F(ConnectionBleManagerTest, ConnectDevice002, TestSize.Level1)
684 {
685 const char *deviceId = "1234568";
686 const char *bleMac = "11:22:33:44:33:56";
687 ConnectOption option = {
688 .type = CONNECT_BLE,
689 .bleOption.bleMac = "",
690 .bleOption.deviceIdHash = "",
691 .bleOption.protocol = BLE_GATT,
692 .bleOption.psm = 5,
693 .bleOption.challengeCode = 0,
694 };
695 int32_t ret = strcpy_s(option.bleOption.bleMac, BT_MAC_LEN, bleMac);
696 ASSERT_EQ(EOK, ret);
697 ret = memcpy_s(option.bleOption.deviceIdHash, UDID_HASH_LEN, deviceId, UDID_HASH_LEN);
698 ASSERT_EQ(EOK, ret);
699 uint32_t requestId = 5;
700 ConnectResult result = {
701 .OnConnectSuccessed = OnConnectSuccessed,
702 .OnConnectFailed = OnConnectFailed,
703 };
704 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
705 EXPECT_CALL(bleMock, LnnGetConnSubFeatureByUdidHashStr).WillRepeatedly(Return(SOFTBUS_OK));
706 EXPECT_CALL(bleMock, ConnGattClientConnect).WillRepeatedly(Return(SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_CONNECT_ERR));
707 ret = g_bleInterface->ConnectDevice(&option, requestId, &result);
708 EXPECT_EQ(ret, SOFTBUS_OK);
709 EXPECT_CALL(bleMock, ConnGattClientConnect).WillRepeatedly(Return(SOFTBUS_OK));
710 requestId = 6;
711 const char *mac = "11:33:44:22:33:56";
712 const char *bleDeviceId = "1234569";
713 ret = strcpy_s(option.bleOption.bleMac, BT_MAC_LEN, mac);
714 ASSERT_EQ(EOK, ret);
715 ret = memcpy_s(option.bleOption.deviceIdHash, UDID_HASH_LEN, bleDeviceId, UDID_HASH_LEN);
716 ASSERT_EQ(EOK, ret);
717 ret = g_bleInterface->ConnectDevice(&option, requestId, &result);
718 EXPECT_EQ(ret, SOFTBUS_OK);
719 SoftBusSleepMs(SLEEP_TIME_MS);
720 }
721
722 /*
723 * @tc.name: ConnBleUpdateConnectionRc001
724 * @tc.desc: Test ConnBleUpdateConnectionRc.
725 * @tc.in: Test module, Test number, Test Levels.
726 * @tc.out: Zero
727 * @tc.type: FUNC
728 * @tc.require:
729 */
730 HWTEST_F(ConnectionBleManagerTest, ConnBleUpdateConnectionRc001, TestSize.Level1)
731 {
732 ConnBleConnection connection;
733 connection.connectionId = 196600;
734 connection.side = CONN_SIDE_CLIENT;
735 connection.featureBitSet = 0;
736 int32_t ret = SoftBusMutexInit(&connection.lock, nullptr);
737 ASSERT_EQ(EOK, ret);
738
739 connection.underlayerHandle = 10;
740 connection.connectionRc = 1;
741 const char *bleMac = "11:22:33:44:33:56";
742 ret = strcpy_s(connection.addr, BT_MAC_LEN, bleMac);
743 ASSERT_EQ(EOK, ret);
744 ret = ConnBleUpdateConnectionRc(&connection, 0, -1);
745 EXPECT_EQ(ret, SOFTBUS_OK);
746
747 connection.connectionId = 196601;
748 connection.featureBitSet = 2;
749 connection.connectionRc = 1;
750 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
751 EXPECT_CALL(bleMock, ConnBlePackCtlMessage).WillOnce(Return(-1)).WillRepeatedly(Return(100));
752 ret = ConnBleUpdateConnectionRc(&connection, 0, -1);
753 EXPECT_EQ(ret, -1);
754
755 connection.connectionRc = 1;
756 EXPECT_CALL(bleMock, ConnBlePostBytesInner).WillRepeatedly(Return(SOFTBUS_OK));
757 ret = ConnBleUpdateConnectionRc(&connection, 0, -1);
758 EXPECT_EQ(ret, SOFTBUS_OK);
759
760 connection.connectionRc = 2;
761 ret = ConnBleUpdateConnectionRc(&connection, 0, -1);
762 EXPECT_EQ(ret, SOFTBUS_OK);
763 }
764
765 /*
766 * @tc.name: ConnBleOnReferenceRequest001
767 * @tc.desc: Test ConnBleOnReferenceRequest.
768 * @tc.in: Test module, Test number, Test Levels.
769 * @tc.out: Zero
770 * @tc.type: FUNC
771 * @tc.require:
772 */
773 HWTEST_F(ConnectionBleManagerTest, ConnBleOnReferenceRequest001, TestSize.Level1)
774 {
775 ConnBleConnection connection;
776 int32_t ret = SoftBusMutexInit(&connection.lock, nullptr);
777 ASSERT_EQ(EOK, ret);
778 connection.connectionRc = 1;
779 connection.state = BLE_CONNECTION_STATE_NEGOTIATION_CLOSING;
780 cJSON json = { 0 };
781 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
782 EXPECT_CALL(bleMock, GetJsonObjectSignedNumberItem).WillOnce(Return(false));
783 ret = ConnBleOnReferenceRequest(&connection, &json);
784 EXPECT_EQ(SOFTBUS_PARSE_JSON_ERR, ret);
785
786 EXPECT_CALL(bleMock, GetJsonObjectSignedNumberItem).WillOnce(Return(true)).WillOnce(Return(false));
787 ret = ConnBleOnReferenceRequest(&connection, &json);
788 EXPECT_EQ(SOFTBUS_PARSE_JSON_ERR, ret);
789 }
790
791 /*
792 * @tc.name: ConnBleOnReferenceRequest002
793 * @tc.desc: Test ConnBleOnReferenceRequest.
794 * @tc.in: Test module, Test number, Test Levels.
795 * @tc.out: Zero
796 * @tc.type: FUNC
797 * @tc.require:
798 */
799 HWTEST_F(ConnectionBleManagerTest, ConnBleOnReferenceRequest002, TestSize.Level1)
800 {
801 ConnBleConnection *connection = (ConnBleConnection *)SoftBusCalloc(sizeof(ConnBleConnection));
802 ASSERT_NE(nullptr, connection);
803 int32_t ret = SoftBusMutexInit(&connection->lock, nullptr);
804 ASSERT_EQ(EOK, ret);
805 const char *bleMac = "11:22:33:44:33:56";
806 const char *udid = "1254222233334419";
807 ret = strcpy_s(connection->addr, BT_MAC_LEN, bleMac);
808 ASSERT_EQ(EOK, ret);
809 ret = strcpy_s(connection->udid, UDID_BUF_LEN, udid);
810 EXPECT_EQ(EOK, ret);
811
812 connection->protocol = BLE_GATT;
813 connection->connectionRc = 1;
814 connection->state = BLE_CONNECTION_STATE_NEGOTIATION_CLOSING;
815 connection->side = CONN_SIDE_SERVER;
816 ret = ConnBleSaveConnection(connection);
817 ASSERT_EQ(SOFTBUS_OK, ret);
818
819 cJSON json = { 0 };
820 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
821 EXPECT_CALL(bleMock, GetJsonObjectSignedNumberItem)
822 .WillOnce(ConnectionBleManagerInterfaceMock::ActionOfGetdelta)
823 .WillOnce(ConnectionBleManagerInterfaceMock::ActionOfGetPeerRc1);
824 EXPECT_CALL(bleMock, GetJsonObjectNumber16Item).WillOnce(Return(false));
825 ret = ConnBleOnReferenceRequest(connection, &json);
826 EXPECT_EQ(SOFTBUS_OK, ret);
827
828 EXPECT_CALL(bleMock, GetJsonObjectSignedNumberItem)
829 .WillOnce(ConnectionBleManagerInterfaceMock::ActionOfGetdelta)
830 .WillOnce(ConnectionBleManagerInterfaceMock::ActionOfGetPeerRc1);
831 connection->state = BLE_CONNECTION_STATE_EXCHANGED_BASIC_INFO;
832 EXPECT_CALL(bleMock, GetJsonObjectNumber16Item).WillOnce(Return(true));
833 ret = ConnBleOnReferenceRequest(connection, &json);
834 EXPECT_EQ(SOFTBUS_OK, ret);
835 }
836
837 /*
838 * @tc.name: ConnBleOnReferenceRequest003
839 * @tc.desc: Test ConnBleOnReferenceRequest.
840 * @tc.in: Test module, Test number, Test Levels.
841 * @tc.out: Zero
842 * @tc.type: FUNC
843 * @tc.require:
844 */
845 HWTEST_F(ConnectionBleManagerTest, ConnBleOnReferenceRequest003, TestSize.Level1)
846 {
847 ConnBleConnection *connection = (ConnBleConnection *)SoftBusCalloc(sizeof(ConnBleConnection));
848 ASSERT_NE(nullptr, connection);
849 int32_t ret = SoftBusMutexInit(&connection->lock, nullptr);
850 ASSERT_EQ(EOK, ret);
851 const char *bleMac = "11:22:33:44:33:56";
852 const char *udid = "1254222233334419";
853 ret = strcpy_s(connection->addr, BT_MAC_LEN, bleMac);
854 ASSERT_EQ(EOK, ret);
855 ret = strcpy_s(connection->udid, UDID_BUF_LEN, udid);
856 EXPECT_EQ(EOK, ret);
857
858 connection->protocol = BLE_GATT;
859 connection->connectionRc = 1;
860 connection->state = BLE_CONNECTION_STATE_NEGOTIATION_CLOSING;
861 connection->side = CONN_SIDE_SERVER;
862 ret = ConnBleSaveConnection(connection);
863 ASSERT_EQ(SOFTBUS_OK, ret);
864 cJSON json = { 0 };
865
866 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
867 EXPECT_CALL(bleMock, GetJsonObjectSignedNumberItem)
868 .WillOnce(ConnectionBleManagerInterfaceMock::ActionOfGetdelta)
869 .WillOnce(ConnectionBleManagerInterfaceMock::ActionOfGetPeerRc0);
870
871 EXPECT_CALL(bleMock, GetJsonObjectNumber16Item).WillRepeatedly(Return(false));
872 EXPECT_CALL(bleMock, ConnGattServerDisconnect).WillOnce(Return(SOFTBUS_OK));
873
874 ret = ConnBleOnReferenceRequest(connection, &json);
875 EXPECT_EQ(SOFTBUS_OK, ret);
876
877 connection->connectionRc = 3;
878 EXPECT_CALL(bleMock, GetJsonObjectSignedNumberItem)
879 .WillOnce(ConnectionBleManagerInterfaceMock::ActionOfGetdelta)
880 .WillOnce(ConnectionBleManagerInterfaceMock::ActionOfGetPeerRc0);
881 EXPECT_CALL(bleMock, ConnBlePackCtlMessage).WillOnce(Return(SOFTBUS_CREATE_JSON_ERR));
882 ret = ConnBleOnReferenceRequest(connection, &json);
883 EXPECT_EQ(SOFTBUS_CREATE_JSON_ERR, ret);
884
885 EXPECT_CALL(bleMock, GetJsonObjectSignedNumberItem)
886 .WillOnce(ConnectionBleManagerInterfaceMock::ActionOfGetdelta)
887 .WillOnce(ConnectionBleManagerInterfaceMock::ActionOfGetPeerRc0);
888 EXPECT_CALL(bleMock, ConnBlePackCtlMessage).WillOnce(Return(100));
889 EXPECT_CALL(bleMock, ConnBlePostBytesInner).WillOnce(Return(SOFTBUS_OK));
890 ret = ConnBleOnReferenceRequest(connection, &json);
891 EXPECT_EQ(SOFTBUS_OK, ret);
892 }
893
894 /*
895 * @tc.name: ConnBleStopServer001
896 * @tc.desc: Test ConnBleOccupy.
897 * @tc.in: Test module, Test number, Test Levels.
898 * @tc.out: Zero
899 * @tc.type: FUNC
900 * @tc.require:
901 */
902 HWTEST_F(ConnectionBleManagerTest, ConnBleStopServer001, TestSize.Level1)
903 {
904 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
905 EXPECT_CALL(bleMock, ConnGattServerStartService).WillRepeatedly(Return(SOFTBUS_OK));
906 int32_t ret = ConnBleStartServer();
907 EXPECT_EQ(ret, SOFTBUS_OK);
908
909 EXPECT_CALL(bleMock, ConnGattServerStopService).WillRepeatedly(Return(SOFTBUS_LOCK_ERR));
910 ret = ConnBleStopServer();
911 EXPECT_EQ(ret, SOFTBUS_OK);
912 SoftBusSleepMs(3 * 1000); //to call RetryServerStatConsistentHandler function
913 }
914
915 /*
916 * @tc.name: ConnBleSend001
917 * @tc.desc: Test ConnBleSend.
918 * @tc.in: Test module, Test number, Test Levels.
919 * @tc.out: Zero
920 * @tc.type: FUNC
921 * @tc.require:
922 */
923 HWTEST_F(ConnectionBleManagerTest, ConnBleSend001, TestSize.Level1)
924 {
925 ConnBleConnection *connection = (ConnBleConnection *)SoftBusCalloc(sizeof(ConnBleConnection));
926 ASSERT_NE(nullptr, connection);
927 int32_t ret = SoftBusMutexInit(&connection->lock, nullptr);
928 ASSERT_EQ(EOK, ret);
929 connection->protocol = BLE_GATT;
930 connection->side = CONN_SIDE_SERVER;
931 uint8_t *data = (uint8_t *)SoftBusMalloc(sizeof(uint8_t));
932 uint32_t dataLen = sizeof(uint8_t);
933
934 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
935 EXPECT_CALL(bleMock, ConnGattServerSend).WillOnce(Return(SOFTBUS_OK));
936 ret = ConnBleSend(connection, data, dataLen, MODULE_CONNECTION);
937 EXPECT_EQ(SOFTBUS_OK, ret);
938
939 connection->side = CONN_SIDE_CLIENT;
940 EXPECT_CALL(bleMock, ConnGattClientSend).WillOnce(Return(SOFTBUS_OK));
941 ret = ConnBleSend(connection, data, dataLen, MODULE_CONNECTION);
942 EXPECT_EQ(SOFTBUS_OK, ret);
943
944 const char *udid = "1111222233337777";
945 ret = strcpy_s(connection->udid, UDID_BUF_LEN, udid);
946 ASSERT_EQ(EOK, ret);
947 ret = ConnBleSaveConnection(connection);
948 ASSERT_EQ(SOFTBUS_OK, ret);
949 LnnEventBasicInfo info = {
950 .event = LNN_EVENT_NODE_ONLINE_STATE_CHANGED,
951 };
952 LnnOnlineEventListener(&info);
953 }
954
955 /*
956 * @tc.name: ConnBleOccupy001
957 * @tc.desc: Test ConnBleOccupy.
958 * @tc.in: Test module, Test number, Test Levels.
959 * @tc.out: Zero
960 * @tc.type: FUNC
961 * @tc.require:
962 */
963 HWTEST_F(ConnectionBleManagerTest, ConnBleOccupy001, TestSize.Level1)
964 {
965 const char *bleMac = "11:22:33:44:55:66";
966 ConnBleConnection *connection = ConnBleCreateConnection(bleMac, BLE_GATT,
967 CONN_SIDE_CLIENT, INVALID_UNDERLAY_HANDLE, true);
968 ASSERT_NE(nullptr, connection);
969 int32_t ret = ConnBleSaveConnection(connection);
970 ASSERT_EQ(SOFTBUS_OK, ret);
971 ConnBleOccupy(connection);
972 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
973 EXPECT_CALL(bleMock, ConnBlePackCtlMessage).WillRepeatedly(Return(100));
974 EXPECT_CALL(bleMock, ConnBlePostBytesInner).WillRepeatedly(Return(SOFTBUS_OK));
975 ret = ConnBleUpdateConnectionRc(connection, 1, 1);
976 EXPECT_EQ(ret, SOFTBUS_OK);
977
978 ret = ConnBleUpdateConnectionRc(connection, 1, -1);
979 EXPECT_EQ(ret, SOFTBUS_OK);
980 SoftBusSleepMs(WAIT_UPDATE_TIME_MS); // sleep 3.5s to retry update Rc
981 EXPECT_EQ(connection->state, BLE_CONNECTION_STATE_NEGOTIATION_CLOSING);
982 ConnBleRemoveConnection(connection);
983 ConnBleReturnConnection(&connection);
984 }
985
986 /*
987 * @tc.name: ConnBleDisconnectNow001
988 * @tc.desc: Test ConnBleOccupy.
989 * @tc.in: Test module, Test number, Test Levels.
990 * @tc.out: Zero
991 * @tc.type: FUNC
992 * @tc.require:
993 */
994 HWTEST_F(ConnectionBleManagerTest, ConnBleDisconnectNow001, TestSize.Level1)
995 {
996 ConnBleConnection connection = {{0}};
997 connection.protocol = BLE_GATT;
998 connection.connectionId = 1;
999 connection.side = CONN_SIDE_CLIENT;
1000 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
1001 EXPECT_CALL(bleMock, ConnGattClientDisconnect).WillRepeatedly(Return(SOFTBUS_OK));
1002 int32_t ret = ConnBleDisconnectNow(&connection, BLE_DISCONNECT_REASON_CONNECT_TIMEOUT);
1003 EXPECT_EQ(ret, SOFTBUS_OK);
1004
1005 ConnectStatistics statistics = {
1006 .connectTraceId = 1,
1007 .startTime = 0,
1008 .reuse = true,
1009 .reqId = 1,
1010 };
1011 DfxRecordBleConnectSuccess(0, &connection, &statistics);
1012 DfxRecordBleConnectSuccess(0, &connection, nullptr);
1013 }
1014
1015 /*
1016 * @tc.name: BleReuseConnection
1017 * @tc.desc: Test BleReuseConnection.
1018 * @tc.in: Test module, Test number, Test Levels.
1019 * @tc.out: Zero
1020 * @tc.type: FUNC
1021 * @tc.require:
1022 */
1023 HWTEST_F(ConnectionBleManagerTest, BleReuseConnection, TestSize.Level1)
1024 {
1025 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
1026 EXPECT_CALL(bleMock, LnnGetConnSubFeatureByUdidHashStr).WillRepeatedly(Return(SOFTBUS_INVALID_PARAM));
1027 ConnBleDevice *device = nullptr;
1028 const char *addr = "11:22:33:44:55:66";
1029 const char *udid = "1111222233334444";
1030 ConnBleConnectRequestContext ctx = {
1031 .fastestConnectEnable = true,
1032 .psm = 0,
1033 .protocol = BLE_GATT,
1034 };
1035 int32_t ret = strcpy_s(ctx.udid, UDID_BUF_LEN, udid);
1036 ASSERT_EQ(EOK, ret);
1037 ret = strcpy_s(ctx.addr, BT_MAC_LEN, addr);
1038 ASSERT_EQ(EOK, ret);
1039 ret = NewDevice(&device, &ctx);
1040 ASSERT_EQ(SOFTBUS_OK, ret);
1041 ConnBleConnection connection = {
1042 .state = BLE_CONNECTION_STATE_EXCHANGED_BASIC_INFO,
1043 .side = CONN_SIDE_SERVER,
1044 .protocol = BLE_GATT,
1045 .featureBitSet = 0,
1046 .psm = 0,
1047 };
1048 ret = strcpy_s(connection.udid, UDID_BUF_LEN, udid);
1049 ASSERT_EQ(EOK, ret);
1050 ret = SoftBusMutexInit(&connection.lock, nullptr);
1051 ASSERT_EQ(SOFTBUS_OK, ret);
1052 bool result = BleReuseConnection(device, &connection);
1053 EXPECT_EQ(true, result);
1054
1055 connection.state = BLE_CONNECTION_STATE_MTU_SETTED;
1056 result = BleReuseConnection(device, &connection);
1057 EXPECT_EQ(false, result);
1058 }
1059
1060 /*
1061 * @tc.name: BleCheckPreventing
1062 * @tc.desc: Test BleCheckPreventing.
1063 * @tc.in: Test module, Test number, Test Levels.
1064 * @tc.out: Zero
1065 * @tc.type: FUNC
1066 * @tc.require:
1067 */
1068 HWTEST_F(ConnectionBleManagerTest, BleCheckPreventing, TestSize.Level1)
1069 {
1070 BlePrevent *prevent = (BlePrevent *)SoftBusCalloc(sizeof(BlePrevent));
1071 ASSERT_NE(nullptr, prevent);
1072 ListAdd(&g_bleManager.prevents->list, &prevent->node);
1073 const char *udid = "1111222233334444";
1074 size_t udidLen = strlen(udid);
1075 int32_t result = memcpy_s(prevent->udid, UDID_BUF_LEN - 1, udid, udidLen);
1076 ASSERT_EQ(EOK, result);
1077 bool ret = BleCheckPreventing(udid);
1078 EXPECT_EQ(true, ret);
1079 ret = BleCheckPreventing(nullptr);
1080 EXPECT_EQ(false, ret);
1081 const char *bleUdid = "1111222233335555";
1082 ret = BleCheckPreventing(bleUdid);
1083 EXPECT_EQ(false, ret);
1084
1085 const char *addr = "22:33:44:55:66:00";
1086 ConnBleConnection *connection =
1087 ConnBleCreateConnection(addr, BLE_GATT, CONN_SIDE_CLIENT, INVALID_UNDERLAY_HANDLE, true);
1088 ASSERT_NE(connection, nullptr);
1089 const char *devId = "1111222233335555";
1090 memcpy_s(connection->networkId, NETWORK_ID_BUF_LEN, devId, DEVID_BUFF_LEN);
1091
1092 ConnBleDevice *device = nullptr;
1093 ConnBleConnectRequestContext ctx = {
1094 .fastestConnectEnable = true,
1095 .psm = 0,
1096 .protocol = BLE_GATT,
1097 };
1098 result = strcpy_s(ctx.udid, UDID_BUF_LEN, udid);
1099 ASSERT_EQ(EOK, result);
1100 result = strcpy_s(ctx.addr, BT_MAC_LEN, addr);
1101 ASSERT_EQ(EOK, result);
1102 NiceMock<ConnectionBleManagerInterfaceMock> bleMock;
1103 EXPECT_CALL(bleMock, LnnGetConnSubFeatureByUdidHashStr).WillRepeatedly(Return(SOFTBUS_INVALID_PARAM));
1104 EXPECT_CALL(bleMock, LnnGetRemoteStrInfo).WillRepeatedly(Return(SOFTBUS_OK));
1105
1106 result = NewDevice(&device, &ctx);
1107 EXPECT_EQ(SOFTBUS_OK, result);
1108 AttempReuseConnect(device, BleConnectDeviceDirectly);
1109 }
1110 } // namespace OHOS
1111