1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <cstdio>
16 #include <cstring>
17 
18 #include <gtest/gtest.h>
19 #include <securec.h>
20 #include "softbus_feature_config.h"
21 #include "softbus_conn_ble_client.h"
22 #include "conn_log.h"
23 #include "bus_center_event.h"
24 #include "softbus_conn_manager.h"
25 #include "softbus_def.h"
26 #include "softbus_errcode.h"
27 #include "softbus_adapter_mem.h"
28 #include "softbus_adapter_ble_conflict.h"
29 #include "bus_center_info_key.h"
30 #include "softbus_conn_ble_manager.h"
31 #include "ble_protocol_interface_factory.h"
32 #include "connection_ble_client_mock.h"
33 
34 using namespace testing::ext;
35 using namespace testing;
36 using namespace std;
37 
38 static SoftBusGattcCallback *gattCb = NULL;
39 namespace OHOS {
40 
41 extern "C" {
ConnGattServerStartService(void)42 int32_t ConnGattServerStartService(void)
43 {
44     return SOFTBUS_OK;
45 }
46 
ConnGattServerStopService(void)47 int32_t ConnGattServerStopService(void)
48 {
49     return SOFTBUS_OK;
50 }
51 
ConnGattServerDisconnect(ConnBleConnection * connection)52 int32_t ConnGattServerDisconnect(ConnBleConnection *connection)
53 {
54     return SOFTBUS_OK;
55 }
56 
ConnGattServerConnect(ConnBleConnection * connection)57 int32_t ConnGattServerConnect(ConnBleConnection *connection)
58 {
59     return SOFTBUS_OK;
60 }
61 
ConnGattServerSend(ConnBleConnection * connection,const uint8_t * data,uint32_t dataLen,int32_t module)62 int32_t ConnGattServerSend(ConnBleConnection *connection, const uint8_t *data, uint32_t dataLen, int32_t module)
63 {
64     return SOFTBUS_OK;
65 }
66 
SoftbusGattcRegisterCallback(SoftBusGattcCallback * cb,int32_t clientId)67 int32_t SoftbusGattcRegisterCallback(SoftBusGattcCallback *cb, int32_t clientId)
68 {
69     gattCb = cb;
70     return SOFTBUS_OK;
71 }
72 
ConnBleInitTransModule(ConnBleTransEventListener * listener)73 int32_t ConnBleInitTransModule(ConnBleTransEventListener *listener)
74 {
75     return SOFTBUS_OK;
76 }
77 
SoftBusAddBtStateListener(const SoftBusBtStateListener * listener)78 int SoftBusAddBtStateListener(const SoftBusBtStateListener *listener)
79 {
80     return SOFTBUS_OK;
81 }
82 
SoftbusBleConflictRegisterListener(SoftBusBleConflictListener * listener)83 void SoftbusBleConflictRegisterListener(SoftBusBleConflictListener *listener)
84 {
85     (void)listener;
86 }
87 
LnnGetRemoteStrInfo(const char * networkId,InfoKey key,char * info,uint32_t len)88 int32_t LnnGetRemoteStrInfo(const char *networkId, InfoKey key, char *info, uint32_t len)
89 {
90     return SOFTBUS_OK;
91 }
92 
ConnGattInitServerModule(SoftBusLooper * looper,const ConnBleServerEventListener * listener)93 int32_t ConnGattInitServerModule(SoftBusLooper *looper, const ConnBleServerEventListener *listener)
94 {
95     return SOFTBUS_OK;
96 }
97 
InitSoftbusAdapterClient(void)98 int32_t InitSoftbusAdapterClient(void)
99 {
100     return SOFTBUS_OK;
101 }
ConnGetHeadSize(void)102 uint32_t ConnGetHeadSize(void)
103 {
104     return SOFTBUS_OK;
105 }
106 
SoftbusBleConflictNotifyDateReceive(int32_t underlayerHandle,const uint8_t * data,uint32_t dataLen)107 void SoftbusBleConflictNotifyDateReceive(int32_t underlayerHandle, const uint8_t *data, uint32_t dataLen)
108 {
109     (void)underlayerHandle;
110     (void)data;
111     (void)dataLen;
112 }
113 
SoftbusBleConflictNotifyDisconnect(const char * addr,const char * udid)114 void SoftbusBleConflictNotifyDisconnect(const char *addr, const char *udid)
115 {
116     (void)addr;
117     (void)udid;
118 }
119 
LnnGetConnSubFeatureByUdidHashStr(const char * udidHashStr,uint64_t * connSubFeature)120 int32_t LnnGetConnSubFeatureByUdidHashStr(const char *udidHashStr, uint64_t *connSubFeature)
121 {
122     (void)udidHashStr;
123     (void)connSubFeature;
124     return SOFTBUS_OK;
125 }
126 
LnnRegisterEventHandler(LnnEventType event,LnnEventHandler handler)127 int32_t LnnRegisterEventHandler(LnnEventType event, LnnEventHandler handler)
128 {
129     (void)event;
130     (void)handler;
131     return SOFTBUS_OK;
132 }
133 
SoftbusBleConflictNotifyConnectResult(uint32_t requestId,int32_t underlayerHandle,bool status)134 void SoftbusBleConflictNotifyConnectResult(uint32_t requestId, int32_t underlayerHandle, bool status)
135 {
136     (void)requestId;
137     (void)underlayerHandle;
138     (void)status;
139 }
140 
ConnBleTransConfigPostLimit(const LimitConfiguration * configuration)141 int32_t ConnBleTransConfigPostLimit(const LimitConfiguration *configuration)
142 {
143     (void)configuration;
144     return SOFTBUS_OK;
145 }
146 
ConnBlePackCtlMessage(BleCtlMessageSerializationContext ctx,uint8_t ** outData,uint32_t * outDataLen)147 int64_t ConnBlePackCtlMessage(BleCtlMessageSerializationContext ctx, uint8_t **outData, uint32_t *outDataLen)
148 {
149     (void)ctx;
150     (void)outData;
151     (void)outDataLen;
152     return SOFTBUS_OK;
153 }
154 
SoftbusGattcUnRegister(int32_t clientId)155 int32_t SoftbusGattcUnRegister(int32_t clientId)
156 {
157     (void)clientId;
158     return SOFTBUS_OK;
159 }
160 
SoftbusGattcRegister(void)161 int32_t SoftbusGattcRegister(void)
162 {
163     int32_t id = 100;
164     return id;
165 }
166 }
167 
OnConnected(uint32_t connectionId,const ConnectionInfo * info)168 void OnConnected(uint32_t connectionId, const ConnectionInfo *info)
169 {
170     (void)connectionId;
171     (void)info;
172 }
173 
OnReusedConnected(uint32_t connectionId,const ConnectionInfo * info)174 void OnReusedConnected(uint32_t connectionId, const ConnectionInfo *info)
175 {
176     (void)connectionId;
177     (void)info;
178 }
179 
OnDisconnected(uint32_t connectionId,const ConnectionInfo * info)180 void OnDisconnected(uint32_t connectionId, const ConnectionInfo *info)
181 {
182     (void)connectionId;
183     (void)info;
184 }
185 
OnDataReceived(uint32_t connectionId,ConnModule moduleId,int64_t seq,char * data,int32_t len)186 void OnDataReceived(uint32_t connectionId, ConnModule moduleId, int64_t seq, char *data, int32_t len)
187 {
188     (void)connectionId;
189     (void)moduleId;
190     (void)seq;
191     (void)data;
192     (void)len;
193 }
194 
195 class ClientConnectionTest : public testing::Test {
196 public:
SetUpTestCase()197     static void SetUpTestCase() {}
TearDownTestCase()198     static void TearDownTestCase() {}
199     void SetUp();
TearDown()200     void TearDown() {}
201 };
202 
SetUp()203 void ClientConnectionTest::SetUp()
204 {
205     SoftbusConfigInit();
206     ConnectCallback connectCb = {
207         .OnConnected = OnConnected,
208         .OnReusedConnected = OnReusedConnected,
209         .OnDisconnected = OnDisconnected,
210         .OnDataReceived = OnDataReceived,
211     };
212     LooperInit();
213     ConnInitBle(&connectCb);
214     NiceMock<ConnectionBleClientInterfaceMock> bleMock;
215     EXPECT_CALL(bleMock, SoftbusBleGattcDisconnect).
216         WillRepeatedly(Return(SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_CONNECT_ERR));
217 }
218 
219 /*
220 * @tc.name: ClientConnectionTest001
221 * @tc.desc: Test ConnectionStateCallback for disconenct.
222 * @tc.in: Test module, Test number, Test Levels.
223 * @tc.out: Zero
224 * @tc.type: FUNC
225 * @tc.require:
226 */
227 HWTEST_F(ClientConnectionTest, ConnectionStateCallback001, TestSize.Level1)
228 {
229     const char *addr = "11:22:33:44:55:66";
230     ConnBleConnection *connection = ConnBleCreateConnection(addr, BLE_GATT, CONN_SIDE_CLIENT, 100, false);
231 
232     NiceMock<ConnectionBleClientInterfaceMock> bleMock;
233     EXPECT_CALL(bleMock, SoftbusGattcConnect).WillRepeatedly(Return(SOFTBUS_OK));
234     int32_t ret = ConnGattClientConnect(connection);
235 
236     EXPECT_EQ(SOFTBUS_OK, ret);
237     connection->fastestConnectEnable = true;
238     connection->connectionRc = 0;
239     ret = ConnBleSaveConnection(connection);
240     EXPECT_EQ(SOFTBUS_OK, ret);
241     EXPECT_CALL(bleMock, SoftbusGattcConnect).WillRepeatedly(Return(SOFTBUS_INVALID_PARAM));
242     EXPECT_CALL(bleMock, SoftbusGattcSetFastestConn).WillRepeatedly(Return(SOFTBUS_OK));
243     ret = ConnGattClientConnect(connection);
244     SoftBusSleepMs(3500); // to call timeout event
245     EXPECT_EQ(SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_CONNECT_ERR, ret);
246 
247     EXPECT_CALL(bleMock, SoftbusGattcSetFastestConn).WillRepeatedly(Return(
248         SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_CONNECT_ERR));
249     EXPECT_CALL(bleMock, SoftbusGattcConnect).WillRepeatedly(Return(SOFTBUS_OK));
250     ret = ConnGattClientConnect(connection);
251     EXPECT_EQ(SOFTBUS_OK, ret);
252 
253     connection->state = BLE_CONNECTION_STATE_EXCHANGING_BASIC_INFO;
254     ret = ConnBleSaveConnection(connection);
255     EXPECT_EQ(SOFTBUS_OK, ret);
256     gattCb->ConnectionStateCallback(connection->underlayerHandle, 5, SOFTBUS_OK);
257     gattCb->ConnectionStateCallback(0, SOFTBUS_BT_CONNECT, SOFTBUS_OK);
258 
259     EXPECT_CALL(bleMock, SoftbusBleGattcDisconnect).WillRepeatedly(Return(
260         SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_CONNECT_ERR));
261     gattCb->ConnectionStateCallback(connection->underlayerHandle, SOFTBUS_BT_DISCONNECT,
262         SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_CONNECT_ERR);
263 
264     connection->state = BLE_CONNECTION_STATE_SERVICE_SEARCHING;
265     ret = ConnBleSaveConnection(connection);
266     EXPECT_EQ(SOFTBUS_OK, ret);
267 
268     gattCb->ConnectionStateCallback(connection->underlayerHandle, SOFTBUS_BT_DISCONNECT,
269         SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_CONNECT_ERR);
270     gattCb->ConnectionStateCallback(connection->underlayerHandle, SOFTBUS_BT_CONNECT,
271         SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_CONNECT_ERR);
272 
273     gattCb->ConnectionStateCallback(connection->underlayerHandle, SOFTBUS_BT_CONNECT,
274         SOFTBUS_OK);
275     SoftBusSleepMs(1500);
276 }
277 
278 /*
279 * @tc.name: ServiceCompleteCallback001
280 * @tc.desc: Test ConnectionStateCallback for connect.
281 * @tc.in: Test module, Test number, Test Levels.
282 * @tc.out: Zero
283 * @tc.type: FUNC
284 * @tc.require:
285 */
286 HWTEST_F(ClientConnectionTest, ServiceCompleteCallback001, TestSize.Level1)
287 {
288     NiceMock<ConnectionBleClientInterfaceMock> bleMock;
289     EXPECT_CALL(bleMock, SoftbusBleGattcDisconnect).WillRepeatedly(Return(
290         SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_CONNECT_ERR));
291     const char *addr = "11:22:33:44:55:12";
292     ConnBleConnection *connection = ConnBleCreateConnection(addr, BLE_GATT, CONN_SIDE_CLIENT, 100, false);
293     EXPECT_NE(NULL, connection);
294 
295     connection->fastestConnectEnable = true;
296     connection->connectionRc = 0;
297     connection->state = BLE_CONNECTION_STATE_SERVICE_SEARCHED;
298     int32_t ret = ConnBleSaveConnection(connection);
299     EXPECT_EQ(SOFTBUS_OK, ret);
300 
301     gattCb->ServiceCompleteCallback(10, SOFTBUS_OK);
302     gattCb->ServiceCompleteCallback(connection->underlayerHandle, SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_CONNECT_ERR);
303     gattCb->ServiceCompleteCallback(connection->underlayerHandle, SOFTBUS_OK);
304     connection->state = BLE_CONNECTION_STATE_SERVICE_SEARCHING;
305 
306     const char *bleAddr = "11:22:33:44:55:33";
307     ConnBleConnection *bleConnection = ConnBleCreateConnection(bleAddr, BLE_GATT, CONN_SIDE_CLIENT, 2, false);
308     EXPECT_NE(NULL, connection);
309     bleConnection->state = BLE_CONNECTION_STATE_SERVICE_SEARCHING;
310     bleConnection->retrySearchServiceCnt = 0;
311     ret = ConnBleSaveConnection(bleConnection);
312     EXPECT_EQ(SOFTBUS_OK, ret);
313     EXPECT_CALL(bleMock, SoftbusGattcGetService)
314         .WillRepeatedly(Return(SOFTBUS_OK));
315     EXPECT_CALL(bleMock, SoftbusGattcRegisterNotification)
316         .WillRepeatedly(Return(SOFTBUS_OK));
317     gattCb->ServiceCompleteCallback(bleConnection->underlayerHandle, SOFTBUS_OK);
318 
319     EXPECT_CALL(bleMock, SoftbusGattcGetService)
320         .WillRepeatedly(Return(  SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_GET_SERVICE_ERR));
321     EXPECT_CALL(bleMock, SoftbusGattcRefreshServices)
322         .WillRepeatedly(Return(SOFTBUS_CONN_BLE_INTERNAL_ERR));
323     gattCb->ServiceCompleteCallback(bleConnection->underlayerHandle, SOFTBUS_OK);
324 
325     EXPECT_CALL(bleMock, SoftbusGattcRefreshServices)
326         .WillRepeatedly(Return(SOFTBUS_OK));
327     EXPECT_CALL(bleMock, SoftbusGattcSearchServices)
328         .WillRepeatedly(Return(SOFTBUS_CONN_BLE_INTERNAL_ERR));
329     gattCb->ServiceCompleteCallback(bleConnection->underlayerHandle, SOFTBUS_OK);
330     EXPECT_CALL(bleMock, SoftbusGattcSearchServices)
331         .WillRepeatedly(Return(SOFTBUS_OK));
332     SoftBusSleepMs(500);
333 }
334 
335 /*
336 * @tc.name: ServiceCompleteCallback002
337 * @tc.desc: Test ConnectionStateCallback for connect.
338 * @tc.in: Test module, Test number, Test Levels.
339 * @tc.out: Zero
340 * @tc.type: FUNC
341 * @tc.require:
342 */
343 HWTEST_F(ClientConnectionTest, ServiceCompleteCallback002, TestSize.Level1)
344 {
345     NiceMock<ConnectionBleClientInterfaceMock> bleMock;
346     EXPECT_CALL(bleMock, SoftbusBleGattcDisconnect)
347         .WillRepeatedly(Return(SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_CONNECT_ERR));
348     const char *bleAddr = "11:22:33:44:55:00";
349     ConnBleConnection *bleConnection = ConnBleCreateConnection(bleAddr, BLE_GATT, CONN_SIDE_CLIENT, 3, false);
350     EXPECT_NE(NULL, bleConnection);
351     bleConnection->retrySearchServiceCnt = 0;
352     bleConnection->state = BLE_CONNECTION_STATE_SERVICE_SEARCHING;
353     int32_t ret = ConnBleSaveConnection(bleConnection);
354     EXPECT_EQ(SOFTBUS_OK, ret);
355     EXPECT_CALL(bleMock, SoftbusGattcGetService)
356         .WillRepeatedly(Return(SOFTBUS_OK));
357     EXPECT_CALL(bleMock, SoftbusGattcRegisterNotification)
358         .WillRepeatedly(Return(SOFTBUS_CONN_BLE_INTERNAL_ERR));
359     EXPECT_CALL(bleMock, SoftbusGattcRefreshServices)
360         .WillOnce(Return(SOFTBUS_CONN_BLE_INTERNAL_ERR));
361     gattCb->ServiceCompleteCallback(bleConnection->underlayerHandle, SOFTBUS_OK);
362     SoftBusSleepMs(500);
363 
364     EXPECT_CALL(bleMock, SoftbusGattcRefreshServices)
365         .WillOnce(Return(SOFTBUS_OK));
366     EXPECT_CALL(bleMock, SoftbusGattcSearchServices)
367         .WillOnce(Return(SOFTBUS_CONN_BLE_INTERNAL_ERR));
368     bleConnection->retrySearchServiceCnt = 0;
369     bleConnection->state = BLE_CONNECTION_STATE_SERVICE_SEARCHING;
370     ret = ConnBleSaveConnection(bleConnection);
371     EXPECT_EQ(SOFTBUS_OK, ret);
372     gattCb->ServiceCompleteCallback(bleConnection->underlayerHandle, SOFTBUS_OK);
373     SoftBusSleepMs(500);
374 }
375 
376 /*
377 * @tc.name: RegistNotificationCallback001
378 * @tc.desc: Test ConnectionStateCallback for connect.
379 * @tc.in: Test module, Test number, Test Levels.
380 * @tc.out: Zero
381 * @tc.type: FUNC
382 * @tc.require:
383 */
384 HWTEST_F(ClientConnectionTest, RegistNotificationCallback001, TestSize.Level1)
385 {
386     gattCb->RegistNotificationCallback(100, SOFTBUS_OK);
387     const char *bleAddr = "11:22:33:44:55:66";
388     ConnBleConnection *bleConnection = ConnBleCreateConnection(bleAddr, BLE_GATT, CONN_SIDE_CLIENT, 4, false);
389     EXPECT_NE(NULL, bleConnection);
390     bleConnection->retrySearchServiceCnt = 0;
391     bleConnection->state = BLE_CONNECTION_STATE_CONNECTED;
392     int32_t ret = ConnBleSaveConnection(bleConnection);
393     EXPECT_EQ(SOFTBUS_OK, ret);
394     NiceMock<ConnectionBleClientInterfaceMock> bleMock;
395     EXPECT_CALL(bleMock, SoftbusGattcRefreshServices)
396         .WillRepeatedly(Return(SOFTBUS_OK));
397     EXPECT_CALL(bleMock, SoftbusGattcSearchServices)
398         .WillRepeatedly(Return(SOFTBUS_OK));
399     gattCb->RegistNotificationCallback(bleConnection->underlayerHandle, SOFTBUS_CONN_BLE_INTERNAL_ERR);
400     SoftBusSleepMs(500);
401 
402     bleConnection->retrySearchServiceCnt = 5;
403     bleConnection->state = BLE_CONNECTION_STATE_CONN_NOTIFICATING;
404     ret = ConnBleSaveConnection(bleConnection);
405     EXPECT_EQ(SOFTBUS_OK, ret);
406     gattCb->RegistNotificationCallback(bleConnection->underlayerHandle, SOFTBUS_CONN_BLE_INTERNAL_ERR);
407     SoftBusSleepMs(500);
408 }
409 
410 /*
411 * @tc.name: RegistNotificationCallback002
412 * @tc.desc: Test ConnectionStateCallback for connect.
413 * @tc.in: Test module, Test number, Test Levels.
414 * @tc.out: Zero
415 * @tc.type: FUNC
416 * @tc.require:
417 */
418 HWTEST_F(ClientConnectionTest, RegistNotificationCallback002, TestSize.Level1)
419 {
420     const char *bleAddr = "11:22:33:44:44:99";
421     ConnBleConnection *bleConnection = ConnBleCreateConnection(bleAddr, BLE_GATT, CONN_SIDE_CLIENT, 5, false);
422     EXPECT_NE(NULL, bleConnection);
423     bleConnection->retrySearchServiceCnt = 0;
424     bleConnection->state = BLE_CONNECTION_STATE_CONN_NOTIFICATING;
425     int32_t ret = ConnBleSaveConnection(bleConnection);
426     EXPECT_EQ(SOFTBUS_OK, ret);
427     NiceMock<ConnectionBleClientInterfaceMock> bleMock;
428     EXPECT_CALL(bleMock, SoftbusGattcRegisterNotification)
429         .WillOnce(Return(SOFTBUS_OK));
430     gattCb->RegistNotificationCallback(bleConnection->underlayerHandle, SOFTBUS_OK);
431     SoftBusSleepMs(500);
432 
433     bleConnection->retrySearchServiceCnt = 0;
434     bleConnection->state = BLE_CONNECTION_STATE_CONN_NOTIFICATING;
435     ret = ConnBleSaveConnection(bleConnection);
436     EXPECT_CALL(bleMock, SoftbusGattcRegisterNotification)
437         .WillOnce(Return(SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_REGISTER_NOTIFICATION_ERR));
438     EXPECT_CALL(bleMock, SoftbusGattcRefreshServices)
439         .WillOnce(Return(SOFTBUS_OK));
440     EXPECT_CALL(bleMock, SoftbusGattcSearchServices)
441         .WillOnce(Return(SOFTBUS_OK));
442     gattCb->RegistNotificationCallback(bleConnection->underlayerHandle, SOFTBUS_OK);
443     SoftBusSleepMs(500);
444 }
445 
446 /*
447 * @tc.name: RegistNotificationCallback003
448 * @tc.desc: Test RegistNotificationCallback for connect.
449 * @tc.in: Test module, Test number, Test Levels.
450 * @tc.out: Zero
451 * @tc.type: FUNC
452 * @tc.require:
453 */
454 HWTEST_F(ClientConnectionTest, RegistNotificationCallback003, TestSize.Level1)
455 {
456     const char *bleAddr = "11:22:33:44:44:37";
457     ConnBleConnection *bleConnection = ConnBleCreateConnection(bleAddr, BLE_GATT, CONN_SIDE_CLIENT, 16, false);
458     EXPECT_NE(NULL, bleConnection);
459 
460     bleConnection->state = BLE_CONNECTION_STATE_NET_NOTIFICATING;
461     int32_t ret = ConnBleSaveConnection(bleConnection);
462     EXPECT_EQ(SOFTBUS_OK, ret);
463     NiceMock<ConnectionBleClientInterfaceMock> bleMock;
464     EXPECT_CALL(bleMock, SoftbusBleGattcDisconnect)
465         .WillRepeatedly(Return(SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_CONNECT_ERR));
466     EXPECT_CALL(bleMock, SoftbusGattcConfigureMtuSize)
467         .WillOnce(Return(SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_CONFIGURE_MTU_ERR));
468     gattCb->RegistNotificationCallback(bleConnection->underlayerHandle, SOFTBUS_OK);
469     SoftBusSleepMs(500);
470 
471     const char *addr = "11:22:33:44:44:37";
472     ConnBleConnection *connection = ConnBleCreateConnection(addr, BLE_GATT, CONN_SIDE_CLIENT, 17, false);
473     EXPECT_NE(NULL, connection);
474 
475     connection->state = BLE_CONNECTION_STATE_NET_NOTIFICATING;
476     ret = ConnBleSaveConnection(connection);
477     EXPECT_EQ(SOFTBUS_OK, ret);
478     EXPECT_CALL(bleMock, SoftbusGattcConfigureMtuSize)
479         .WillOnce(Return(SOFTBUS_OK));
480     gattCb->RegistNotificationCallback(connection->underlayerHandle, SOFTBUS_OK);
481     SoftBusSleepMs(500);
482 
483     connection->state = BLE_CONNECTION_STATE_CLOSED;
484     ret = ConnBleSaveConnection(connection);
485     EXPECT_EQ(SOFTBUS_OK, ret);
486     gattCb->RegistNotificationCallback(connection->underlayerHandle, SOFTBUS_OK);
487     SoftBusSleepMs(500);
488 }
489 
490 
491 /*
492 * @tc.name: NotificationReceiveCallback001
493 * @tc.desc: Test NotificationReceiveCallback
494 * @tc.in: Test module, Test number, Test Levels.
495 * @tc.out: Zero
496 * @tc.type: FUNC
497 * @tc.require:
498 */
499 HWTEST_F(ClientConnectionTest, NotificationReceiveCallback001, TestSize.Level1)
500 {
501     const char *bleAddr = "11:00:33:44:44:99";
502     ConnBleConnection *bleConnection = ConnBleCreateConnection(bleAddr, BLE_GATT, CONN_SIDE_CLIENT, 5, false);
503     EXPECT_NE(NULL, bleConnection);
504 
505     int32_t ret = ConnBleSaveConnection(bleConnection);
506     EXPECT_EQ(SOFTBUS_OK, ret);
507     SoftBusGattcNotify param = {
508         .dataLen = 50,
509         .charaUuid.uuidLen = strlen(SOFTBUS_SERVICE_UUID),
510     };
511     param.charaUuid.uuid = (char *)SoftBusCalloc(param.charaUuid.uuidLen + 1);
512     ret = strcpy_s(param.charaUuid.uuid, param.charaUuid.uuidLen + 1, SOFTBUS_SERVICE_UUID);
513     EXPECT_EQ(EOK, ret);
514 
515     gattCb->NotificationReceiveCallback(bleConnection->underlayerHandle, &param,
516         SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_REGISTER_NOTIFICATION_ERR);
517     gattCb->NotificationReceiveCallback(bleConnection->underlayerHandle, &param, SOFTBUS_OK);
518     SoftBusSleepMs(500);
519     SoftBusFree(param.charaUuid.uuid);
520     param.charaUuid.uuid = nullptr;
521 
522     param.charaUuid.uuidLen = strlen(SOFTBUS_CHARA_BLECONN_UUID);
523     param.charaUuid.uuid = (char *)SoftBusCalloc(param.charaUuid.uuidLen + 1);
524     ret = strcpy_s(param.charaUuid.uuid, param.charaUuid.uuidLen + 1, SOFTBUS_CHARA_BLECONN_UUID);
525     EXPECT_EQ(EOK, ret);
526     NiceMock<ConnectionBleClientInterfaceMock> bleMock;
527     EXPECT_CALL(bleMock, ConnGattTransRecv).WillOnce(Return(nullptr));
528     gattCb->NotificationReceiveCallback(bleConnection->underlayerHandle, &param, SOFTBUS_OK);
529 
530     SoftBusFree(param.charaUuid.uuid);
531     param.charaUuid.uuid = nullptr;
532     param.charaUuid.uuidLen = strlen(SOFTBUS_CHARA_BLENET_UUID);
533     param.charaUuid.uuid = (char *)SoftBusCalloc(param.charaUuid.uuidLen + 1);
534     ret = strcpy_s(param.charaUuid.uuid, param.charaUuid.uuidLen + 1, SOFTBUS_CHARA_BLENET_UUID);
535     EXPECT_EQ(EOK, ret);
536     SoftBusSleepMs(500);
537     EXPECT_CALL(bleMock, ConnGattTransRecv).WillOnce(Return(nullptr));
538     gattCb->NotificationReceiveCallback(bleConnection->underlayerHandle, &param, SOFTBUS_OK);
539     SoftBusSleepMs(500);
540 }
541 
542 /*
543 * @tc.name: NotificationReceiveCallback002
544 * @tc.desc: Test NotificationReceiveCallback
545 * @tc.in: Test module, Test number, Test Levels.
546 * @tc.out: Zero
547 * @tc.type: FUNC
548 * @tc.require:
549 */
550 HWTEST_F(ClientConnectionTest, NotificationReceiveCallback002, TestSize.Level1)
551 {
552     const char *bleAddr = "11:22:33:44:44:99";
553     ConnBleConnection *bleConnection = ConnBleCreateConnection(bleAddr, BLE_GATT, CONN_SIDE_CLIENT, 6, false);
554     EXPECT_NE(NULL, bleConnection);
555 
556     bleConnection->buffer.seq = 0;
557     bleConnection->buffer.total = 0;
558     int32_t ret = ConnBleSaveConnection(bleConnection);
559     EXPECT_EQ(SOFTBUS_OK, ret);
560     SoftBusGattcNotify param = {
561         .dataLen = 50,
562         .charaUuid.uuidLen = strlen(SOFTBUS_CHARA_BLECONN_UUID),
563     };
564     param.charaUuid.uuid = (char *)SoftBusCalloc(param.charaUuid.uuidLen + 1);
565     ret = strcpy_s(param.charaUuid.uuid, param.charaUuid.uuidLen + 1, SOFTBUS_CHARA_BLECONN_UUID);
566     EXPECT_EQ(EOK, ret);
567 
568     NiceMock<ConnectionBleClientInterfaceMock> bleMock;
569     EXPECT_CALL(bleMock, ConnGattTransRecv)
570         .WillOnce(ConnectionBleClientInterfaceMock::ConnGattTransRecvReturnConnModule);
571     gattCb->NotificationReceiveCallback(bleConnection->underlayerHandle, &param, SOFTBUS_OK);
572     SoftBusSleepMs(500);
573 
574     EXPECT_CALL(bleMock, ConnGattTransRecv)
575         .WillOnce(ConnectionBleClientInterfaceMock::ConnGattTransRecvReturnOldNearby);
576     gattCb->NotificationReceiveCallback(bleConnection->underlayerHandle, &param, SOFTBUS_OK);
577     SoftBusSleepMs(500);
578 
579     EXPECT_CALL(bleMock, ConnGattTransRecv).WillOnce(ConnectionBleClientInterfaceMock::ConnGattTransRecvReturnDefult);
580     gattCb->NotificationReceiveCallback(bleConnection->underlayerHandle, &param, SOFTBUS_OK);
581     SoftBusSleepMs(500);
582 }
583 
584 /*
585 * @tc.name: NotificationReceiveCallback002
586 * @tc.desc: Test NotificationReceiveCallback
587 * @tc.in: Test module, Test number, Test Levels.
588 * @tc.out: Zero
589 * @tc.type: FUNC
590 * @tc.require:
591 */
592 HWTEST_F(ClientConnectionTest, NotificationReceiveCallback003, TestSize.Level1)
593 {
594     const char *bleAddr = "11:22:33:44:22:99";
595     ConnBleConnection *bleConnection = ConnBleCreateConnection(bleAddr, BLE_GATT, CONN_SIDE_CLIENT, 7, false);
596     EXPECT_NE(NULL, bleConnection);
597 
598     bleConnection->buffer.seq = 0;
599     bleConnection->buffer.total = 0;
600     bleConnection->state = BLE_CONNECTION_STATE_CONNECTED;
601     int32_t ret = ConnBleSaveConnection(bleConnection);
602     EXPECT_EQ(SOFTBUS_OK, ret);
603     SoftBusGattcNotify param = {
604         .dataLen = 50,
605         .charaUuid.uuidLen = strlen(SOFTBUS_CHARA_BLENET_UUID),
606     };
607     param.charaUuid.uuid = (char *)SoftBusCalloc(param.charaUuid.uuidLen + 1);
608     ret = strcpy_s(param.charaUuid.uuid, param.charaUuid.uuidLen + 1, SOFTBUS_CHARA_BLENET_UUID);
609     EXPECT_EQ(EOK, ret);
610 
611     NiceMock<ConnectionBleClientInterfaceMock> bleMock;
612     EXPECT_CALL(bleMock, ConnGattTransRecv)
613         .WillOnce(ConnectionBleClientInterfaceMock::ConnGattTransRecvReturnConnModule);
614     gattCb->NotificationReceiveCallback(bleConnection->underlayerHandle, &param, SOFTBUS_OK);
615     SoftBusSleepMs(500);
616 
617     bleConnection->state = BLE_CONNECTION_STATE_EXCHANGING_BASIC_INFO;
618     ret = ConnBleSaveConnection(bleConnection);
619     EXPECT_EQ(SOFTBUS_OK, ret);
620     EXPECT_CALL(bleMock, ConnGattTransRecv).WillOnce(ConnectionBleClientInterfaceMock::ActionOfConnGattTransRecv);
621     EXPECT_CALL(bleMock, SoftbusBleGattcDisconnect)
622         .WillRepeatedly(Return(SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_CONNECT_ERR));
623     gattCb->NotificationReceiveCallback(bleConnection->underlayerHandle, &param, SOFTBUS_OK);
624     SoftBusSleepMs(500);
625 }
626 
627 
628 /*
629 * @tc.name: NotificationReceiveCallback004
630 * @tc.desc: Test NotificationReceiveCallback
631 * @tc.in: Test module, Test number, Test Levels.
632 * @tc.out: Zero
633 * @tc.type: FUNC
634 * @tc.require:
635 */
636 HWTEST_F(ClientConnectionTest, NotificationReceiveCallback004, TestSize.Level1)
637 {
638     const char *bleAddr = "11:22:33:44:22:03";
639     ConnBleConnection *bleConnection = ConnBleCreateConnection(bleAddr, BLE_GATT, CONN_SIDE_CLIENT, 8, false);
640     EXPECT_NE(NULL, bleConnection);
641 
642     bleConnection->buffer.seq = 0;
643     bleConnection->buffer.total = 0;
644     bleConnection->state = BLE_CONNECTION_STATE_EXCHANGING_BASIC_INFO;
645     int32_t ret = ConnBleSaveConnection(bleConnection);
646     EXPECT_EQ(SOFTBUS_OK, ret);
647     SoftBusGattcNotify param = {
648         .dataLen = 50,
649         .charaUuid.uuidLen = strlen(SOFTBUS_CHARA_BLENET_UUID),
650     };
651     param.charaUuid.uuid = (char *)SoftBusCalloc(param.charaUuid.uuidLen + 1);
652     ret = strcpy_s(param.charaUuid.uuid, param.charaUuid.uuidLen + 1, SOFTBUS_CHARA_BLENET_UUID);
653     EXPECT_EQ(EOK, ret);
654 
655     NiceMock<ConnectionBleClientInterfaceMock> bleMock;
656     EXPECT_CALL(bleMock, ConnGattTransRecv)
657         .WillOnce(ConnectionBleClientInterfaceMock::ConnGattTransRecvReturnConnModule1);
658     gattCb->NotificationReceiveCallback(bleConnection->underlayerHandle, &param, SOFTBUS_OK);
659     SoftBusSleepMs(500);
660 
661     const char *addr = "11:22:33:44:22:06";
662     ConnBleConnection *connection = ConnBleCreateConnection(addr, BLE_GATT, CONN_SIDE_CLIENT, 9, false);
663     EXPECT_NE(NULL, connection);
664     connection->state = BLE_CONNECTION_STATE_EXCHANGING_BASIC_INFO;
665     ret = ConnBleSaveConnection(connection);
666     EXPECT_EQ(SOFTBUS_OK, ret);
667     EXPECT_CALL(bleMock, ConnGattTransRecv)
668         .WillOnce(ConnectionBleClientInterfaceMock::ConnGattTransRecvReturnConnModule);
669     gattCb->NotificationReceiveCallback(connection->underlayerHandle, &param, SOFTBUS_OK);
670     SoftBusSleepMs(500);
671 }
672 
673 /*
674 * @tc.name: ConfigureMtuSizeCallback001
675 * @tc.desc: Test ConfigureMtuSizeCallback
676 * @tc.in: Test module, Test number, Test Levels.
677 * @tc.out: Zero
678 * @tc.type: FUNC
679 * @tc.require:
680 */
681 HWTEST_F(ClientConnectionTest, ConfigureMtuSizeCallback001, TestSize.Level1)
682 {
683     gattCb->ConfigureMtuSizeCallback(100, 20, SOFTBUS_OK);
684     SoftBusSleepMs(500);
685 
686     const char *bleAddr = "11:22:33:44:22:05";
687     ConnBleConnection *bleConnection = ConnBleCreateConnection(bleAddr, BLE_GATT, CONN_SIDE_CLIENT, 10, false);
688     EXPECT_NE(NULL, bleConnection);
689     int32_t ret = ConnBleSaveConnection(bleConnection);
690     EXPECT_EQ(SOFTBUS_OK, ret);
691 
692     NiceMock<ConnectionBleClientInterfaceMock> bleMock;
693     EXPECT_CALL(bleMock, SoftbusBleGattcDisconnect)
694         .WillRepeatedly(Return(SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_CONNECT_ERR));
695     gattCb->ConfigureMtuSizeCallback(bleConnection->underlayerHandle, 20, SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_CONNECT_ERR);
696     SoftBusSleepMs(500);
697 
698     gattCb->ConfigureMtuSizeCallback(bleConnection->underlayerHandle, 20, SOFTBUS_OK);
699     SoftBusSleepMs(500);
700     const char *addr = "11:22:33:44:22:06";
701     ConnBleConnection *connection = ConnBleCreateConnection(addr, BLE_GATT, CONN_SIDE_CLIENT, 11, false);
702     EXPECT_NE(NULL, connection);
703     connection->state = BLE_CONNECTION_STATE_MTU_SETTING;
704     ret = ConnBleSaveConnection(connection);
705     EXPECT_EQ(SOFTBUS_OK, ret);
706 
707     EXPECT_CALL(bleMock, LnnGetLocalStrInfo)
708         .WillOnce(Return(SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_CONNECT_ERR));
709 
710     gattCb->ConfigureMtuSizeCallback(connection->underlayerHandle, 21, SOFTBUS_OK);
711         SoftBusSleepMs(500);
712 }
713 
714 /*
715 * @tc.name: ConfigureMtuSizeCallback002
716 * @tc.desc: Test ConfigureMtuSizeCallback
717 * @tc.in: Test module, Test number, Test Levels.
718 * @tc.out: Zero
719 * @tc.type: FUNC
720 * @tc.require:
721 */
722 HWTEST_F(ClientConnectionTest, ConfigureMtuSizeCallback002, TestSize.Level1)
723 {
724     const char *addr = "11:22:33:44:22:05";
725     ConnBleConnection *connection = ConnBleCreateConnection(addr, BLE_GATT, CONN_SIDE_CLIENT, 12, false);
726     EXPECT_NE(NULL, connection);
727     connection->state = BLE_CONNECTION_STATE_MTU_SETTING;
728     int32_t ret = ConnBleSaveConnection(connection);
729     EXPECT_EQ(SOFTBUS_OK, ret);
730 
731     NiceMock<ConnectionBleClientInterfaceMock> bleMock;
732     EXPECT_CALL(bleMock, LnnGetLocalStrInfo).WillOnce(Return(SOFTBUS_OK));
733     EXPECT_CALL(bleMock, LnnGetLocalNumInfo)
734         .WillOnce(Return(SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_CONNECT_ERR));
735     gattCb->ConfigureMtuSizeCallback(connection->underlayerHandle, 21, SOFTBUS_OK);
736     SoftBusSleepMs(500);
737 
738     const char *bleAddr = "11:22:33:44:22:56";
739     ConnBleConnection *bleConnection = ConnBleCreateConnection(bleAddr, BLE_GATT, CONN_SIDE_CLIENT, 13, false);
740     EXPECT_NE(NULL, bleConnection);
741     bleConnection->state = BLE_CONNECTION_STATE_MTU_SETTING;
742     ret = ConnBleSaveConnection(bleConnection);
743     EXPECT_EQ(SOFTBUS_OK, ret);
744     EXPECT_CALL(bleMock, LnnGetLocalStrInfo).WillOnce(Return(SOFTBUS_OK));
745     EXPECT_CALL(bleMock, LnnGetLocalNumInfo).WillOnce(Return(SOFTBUS_OK));
746     EXPECT_CALL(bleMock, ConnBlePostBytesInner).WillOnce(Return(SOFTBUS_OK));
747     gattCb->ConfigureMtuSizeCallback(bleConnection->underlayerHandle, 21, SOFTBUS_OK);
748     SoftBusSleepMs(500);
749 
750     const char *addrBle = "11:22:33:44:22:56";
751     ConnBleConnection *connectionBle = ConnBleCreateConnection(addrBle, BLE_GATT, CONN_SIDE_CLIENT, 14, false);
752     EXPECT_NE(NULL, connectionBle);
753     connectionBle->state = BLE_CONNECTION_STATE_MTU_SETTING;
754     ret = ConnBleSaveConnection(connectionBle);
755     EXPECT_EQ(SOFTBUS_OK, ret);
756     EXPECT_CALL(bleMock, LnnGetLocalStrInfo).WillOnce(Return(SOFTBUS_OK));
757     EXPECT_CALL(bleMock, LnnGetLocalNumInfo).WillOnce(Return(SOFTBUS_OK));
758     EXPECT_CALL(bleMock, ConnBlePostBytesInner).WillOnce(Return(SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_CONNECT_ERR));
759     gattCb->ConfigureMtuSizeCallback(connectionBle->underlayerHandle, 22, SOFTBUS_OK);
760     SoftBusSleepMs(500);
761 }
762 }