1 /*
2  * Copyright (c) 2022 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 "gmock/gmock.h"
17 #include "gtest/gtest.h"
18 #include <cstdint>
19 
20 #include "bluetooth_mock.h"
21 #include "c_header/ohos_bt_def.h"
22 #include "softbus_adapter_ble_gatt_client.h"
23 #include "softbus_error_code.h"
24 
25 #include "assert_helper.h"
26 
27 using namespace testing::ext;
28 using ::testing::Return;
29 
30 namespace OHOS {
31 
32 // 调用上下文存储辅助对象
33 class GattcNotifyRecordCtx : public StRecordCtx {
34 public:
GattcNotifyRecordCtx(const char * identifier)35     explicit GattcNotifyRecordCtx(const char *identifier) : StRecordCtx(identifier)
36     {
37         Reset();
38     }
~GattcNotifyRecordCtx()39     ~GattcNotifyRecordCtx()
40     {
41         Reset();
42     }
43 
44     bool Update(int id, int st, SoftBusGattcNotify *param);
45     testing::AssertionResult Expect(int id, int st, SoftBusGattcNotify *param);
46 private:
47     SoftBusGattcNotify notify;
48     void Reset();
49 };
50 
51 class AdapterBleGattClientTest : public testing::Test {
52 public:
53     static BtGattClientCallbacks *gattClientCallback;
54 
55     static IntRecordCtx connectionStateCtx;
56     static StRecordCtx serviceCompleteStateCtx;
57     static StRecordCtx registNotificationCtx;
58     static IntRecordCtx configureMtuSizeCtx;
59     static GattcNotifyRecordCtx notificationReceiveCtx;
60 };
61 
62 static SoftBusGattcCallback *GetStubGattcCallback();
63 
ActionBleGattcRegister(BtUuid appUuid)64 int ActionBleGattcRegister(BtUuid appUuid)
65 {
66     (void)appUuid;
67     static int idGenerator = 0;
68     return ++idGenerator;
69 }
70 
ActionBleGattcConnect(int clientId,BtGattClientCallbacks * func,const BdAddr * bdAddr,bool isAutoConnect,BtTransportType transport)71 int ActionBleGattcConnect(
72     int clientId, BtGattClientCallbacks *func, const BdAddr *bdAddr, bool isAutoConnect, BtTransportType transport)
73 {
74     (void)clientId;
75     (void)bdAddr;
76     (void)isAutoConnect;
77     (void)transport;
78     AdapterBleGattClientTest::gattClientCallback = func;
79     return OHOS_BT_STATUS_SUCCESS;
80 }
81 
MockAll(MockBluetooth & mocker)82 static void MockAll(MockBluetooth &mocker)
83 {
84     EXPECT_CALL(mocker, BleGattcRegister).WillRepeatedly(ActionBleGattcRegister);
85     EXPECT_CALL(mocker, BleGattcConnect).WillRepeatedly(ActionBleGattcConnect);
86     EXPECT_CALL(mocker, BleGattcDisconnect).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
87     EXPECT_CALL(mocker, BleGattcSearchServices).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
88     EXPECT_CALL(mocker, BleGattcGetService).WillRepeatedly(Return(true));
89     EXPECT_CALL(mocker, BleGattcRegisterNotification).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
90     EXPECT_CALL(mocker, BleGattcConfigureMtuSize).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
91     EXPECT_CALL(mocker, BleGattcWriteCharacteristic).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
92     EXPECT_CALL(mocker, BleGattcUnRegister).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
93     EXPECT_CALL(mocker, BleGattcSetFastestConn).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
94     EXPECT_CALL(mocker, BleGattcSetPriority).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
95 }
96 
97 /**
98  * @tc.name: AdapterBleGattClientTest_SoftbusGattcRegister
99  * @tc.desc: test gatt client register
100  * @tc.type: FUNC
101  * @tc.require: NONE
102  */
103 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcRegister, TestSize.Level3)
104 {
105     MockBluetooth mocker;
106     MockAll(mocker);
107     EXPECT_CALL(mocker, BleGattcRegister).Times(1).WillOnce(Return(0));
108     EXPECT_EQ(SoftbusGattcRegister(), -1);
109 
110     EXPECT_CALL(mocker, BleGattcRegister).WillRepeatedly(ActionBleGattcRegister);
111     EXPECT_NE(SoftbusGattcRegister(), -1);
112 }
113 
114 /**
115  * @tc.name: AdapterBleGattClientTest_SoftbusGattcUnRegister
116  * @tc.desc: test gatt client unregister
117  * @tc.type: FUNC
118  * @tc.require: NONE
119  */
120 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcUnRegister, TestSize.Level3)
121 {
122     InitSoftbusAdapterClient();
123     MockBluetooth mocker;
124     MockAll(mocker);
125     EXPECT_CALL(mocker, BleGattcUnRegister).Times(1).WillOnce(Return(OHOS_BT_STATUS_FAIL));
126     EXPECT_EQ(SoftbusGattcUnRegister(1), SOFTBUS_GATTC_INTERFACE_FAILED);
127 
128     int32_t clientId = 10;
129     SoftbusGattcRegisterCallback(GetStubGattcCallback(), clientId);
130     EXPECT_CALL(mocker, BleGattcUnRegister).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
131     EXPECT_EQ(SoftbusGattcUnRegister(clientId), SOFTBUS_OK);
132 }
133 
134 /**
135  * @tc.name: AdapterBleGattClientTest_SoftbusGattcConnect
136  * @tc.desc: test gatt client connect
137  * @tc.type: FUNC
138  * @tc.require: NONE
139  */
140 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcConnect, TestSize.Level3)
141 {
142     MockBluetooth mocker;
143     MockAll(mocker);
144 
145     SoftBusBtAddr addr = {
146         .addr = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}
147     };
148     EXPECT_CALL(mocker, BleGattcConnect).Times(1).WillOnce(Return(OHOS_BT_STATUS_FAIL));
149     EXPECT_EQ(SoftbusGattcConnect(1, &addr), SOFTBUS_GATTC_INTERFACE_FAILED);
150 
151     EXPECT_CALL(mocker, BleGattcConnect).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
152     EXPECT_EQ(SoftbusGattcConnect(1, &addr), SOFTBUS_OK);
153 }
154 
155 /**
156  * @tc.name: AdapterBleGattClientTest_SoftbusBleGattcDisconnect
157  * @tc.desc: test gatt client disconnect
158  * @tc.type: FUNC
159  * @tc.require: NONE
160  */
161 HWTEST_F(AdapterBleGattClientTest, SoftbusBleGattcDisconnect, TestSize.Level3)
162 {
163     MockBluetooth mocker;
164     MockAll(mocker);
165     EXPECT_CALL(mocker, BleGattcDisconnect).Times(1).WillOnce(Return(OHOS_BT_STATUS_FAIL));
166     EXPECT_EQ(SoftbusBleGattcDisconnect(1, false), SOFTBUS_GATTC_INTERFACE_FAILED);
167 
168     EXPECT_CALL(mocker, BleGattcDisconnect).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
169     EXPECT_EQ(SoftbusBleGattcDisconnect(1, false), SOFTBUS_OK);
170 }
171 
172 /**
173  * @tc.name: AdapterBleGattClientTest_SoftbusGattcSearchServices
174  * @tc.desc: test gatt client search service
175  * @tc.type: FUNC
176  * @tc.require: NONE
177  */
178 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcSearchServices, TestSize.Level3)
179 {
180     MockBluetooth mocker;
181     MockAll(mocker);
182     EXPECT_CALL(mocker, BleGattcSearchServices).Times(1).WillOnce(Return(OHOS_BT_STATUS_FAIL));
183     EXPECT_EQ(SoftbusGattcSearchServices(1), SOFTBUS_GATTC_INTERFACE_FAILED);
184 
185     EXPECT_CALL(mocker, BleGattcSearchServices).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
186     EXPECT_EQ(SoftbusGattcSearchServices(1), SOFTBUS_OK);
187 }
188 
189 /**
190  * @tc.name: AdapterBleGattClientTest_SoftbusGattcGetService
191  * @tc.desc: test gatt client get service
192  * @tc.type: FUNC
193  * @tc.require: NONE
194  */
195 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcGetService, TestSize.Level3)
196 {
197     MockBluetooth mocker;
198     MockAll(mocker);
199     const char *serviceUuidExample = "11C8B310-80E4-4276-AFC0-F81590B2177F";
200     SoftBusBtUuid serverUuid = {
201         .uuidLen = strlen(serviceUuidExample),
202         .uuid = (char *)serviceUuidExample,
203     };
204 
205     EXPECT_CALL(mocker, BleGattcGetService).Times(1).WillOnce(Return(false));
206     EXPECT_EQ(SoftbusGattcGetService(1, &serverUuid), SOFTBUS_GATTC_INTERFACE_FAILED);
207 
208     EXPECT_CALL(mocker, BleGattcGetService).WillRepeatedly(Return(true));
209     EXPECT_EQ(SoftbusGattcGetService(1, &serverUuid), SOFTBUS_OK);
210 }
211 
212 /**
213  * @tc.name: AdapterBleGattClientTest_SoftbusGattcRegisterNotification
214  * @tc.desc: test gatt client register notification
215  * @tc.type: FUNC
216  * @tc.require: NONE
217  */
218 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcRegisterNotification, TestSize.Level3)
219 {
220     MockBluetooth mocker;
221     MockAll(mocker);
222 
223     const char *serviceUuidExample = "11C8B310-80E4-4276-AFC0-F81590B2177F";
224     SoftBusBtUuid serverUuid = {
225         .uuidLen = strlen(serviceUuidExample),
226         .uuid = (char *)serviceUuidExample,
227     };
228     const char *charaNetUuidExample = "00002B00-0000-1000-8000-00805F9B34FB";
229     SoftBusBtUuid netUuid = {
230         .uuidLen = strlen(charaNetUuidExample),
231         .uuid = (char *)charaNetUuidExample,
232     };
233     EXPECT_CALL(mocker, BleGattcRegisterNotification).Times(1).WillOnce(Return(OHOS_BT_STATUS_FAIL));
234     EXPECT_EQ(SoftbusGattcRegisterNotification(1, &serverUuid, &netUuid, NULL), SOFTBUS_GATTC_INTERFACE_FAILED);
235 
236     EXPECT_CALL(mocker, BleGattcRegisterNotification).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
237     EXPECT_EQ(SoftbusGattcRegisterNotification(1, &serverUuid, &netUuid, NULL), SOFTBUS_OK);
238 }
239 
240 /**
241  * @tc.name: AdapterBleGattClientTest_SoftbusGattcWriteCharacteristic
242  * @tc.desc: test gatt client write characteristic
243  * @tc.type: FUNC
244  * @tc.require: NONE
245  */
246 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcWriteCharacteristic, TestSize.Level3)
247 {
248     MockBluetooth mocker;
249     MockAll(mocker);
250 
251     const char *serviceUuidExample = "11C8B310-80E4-4276-AFC0-F81590B2177F";
252     SoftBusBtUuid serverUuid = {
253         .uuidLen = strlen(serviceUuidExample),
254         .uuid = (char *)serviceUuidExample,
255     };
256     const char *charaNetUuidExample = "00002B00-0000-1000-8000-00805F9B34FB";
257     SoftBusBtUuid netUuid = {
258         .uuidLen = strlen(charaNetUuidExample),
259         .uuid = (char *)charaNetUuidExample,
260     };
261     const char *valueExample = "hello dsoftbus";
262     SoftBusGattcData data = {
263         .serviceUuid = serverUuid,
264         .characterUuid = netUuid,
265         .valueLen = strlen(valueExample),
266         .value = (uint8_t *)valueExample,
267     };
268     EXPECT_CALL(mocker, BleGattcWriteCharacteristic).Times(1).WillOnce(Return(OHOS_BT_STATUS_FAIL));
269     EXPECT_EQ(SoftbusGattcWriteCharacteristic(1, &data), SOFTBUS_GATTC_INTERFACE_FAILED);
270 
271     EXPECT_CALL(mocker, BleGattcWriteCharacteristic).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
272     EXPECT_EQ(SoftbusGattcWriteCharacteristic(1, &data), SOFTBUS_OK);
273 
274     data.writeType = SOFTBUS_GATT_WRITE_NO_RSP;
275     EXPECT_EQ(SoftbusGattcWriteCharacteristic(1, &data), SOFTBUS_OK);
276 
277     data.writeType = SOFTBUS_GATT_WRITE_PREPARE;
278     EXPECT_EQ(SoftbusGattcWriteCharacteristic(1, &data), SOFTBUS_OK);
279 
280     data.writeType = SOFTBUS_GATT_WRITE_DEFAULT;
281     EXPECT_EQ(SoftbusGattcWriteCharacteristic(1, &data), SOFTBUS_OK);
282 
283     data.writeType = SOFTBUS_GATT_WRITE_SIGNED;
284     EXPECT_EQ(SoftbusGattcWriteCharacteristic(1, &data), SOFTBUS_OK);
285 }
286 
287 /**
288  * @tc.name: AdapterBleGattClientTest_SoftbusGattcConfigureMtuSize
289  * @tc.desc: test gatt client write characteristic
290  * @tc.type: FUNC
291  * @tc.require: NONE
292  */
293 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcConfigureMtuSize, TestSize.Level3)
294 {
295     MockBluetooth mocker;
296     MockAll(mocker);
297 
298     EXPECT_CALL(mocker, BleGattcConfigureMtuSize).Times(1).WillOnce(Return(OHOS_BT_STATUS_FAIL));
299     EXPECT_EQ(SoftbusGattcConfigureMtuSize(1, 512), SOFTBUS_GATTC_INTERFACE_FAILED);
300 
301     EXPECT_CALL(mocker, BleGattcConfigureMtuSize).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
302     EXPECT_EQ(SoftbusGattcConfigureMtuSize(1, 512), SOFTBUS_OK);
303 }
304 
305 /**
306  * @tc.name: AdapterBleGattClientTest_ScanLifecycle
307  * @tc.desc: test complete gatt client connect life cycle
308  * @tc.type: FUNC
309  * @tc.require: NONE
310  */
311 HWTEST_F(AdapterBleGattClientTest, GattClientConnectCycle1, TestSize.Level3)
312 {
313     MockBluetooth mocker;
314     MockAll(mocker);
315 
316     auto clientId = SoftbusGattcRegister();
317     ASSERT_NE(clientId, -1);
318     SoftbusGattcRegisterCallback(GetStubGattcCallback(), clientId);
319     SoftBusBtAddr addr = {
320         .addr = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}
321     };
322     ASSERT_EQ(SoftbusGattcConnect(clientId, &addr), SOFTBUS_OK);
323     gattClientCallback->ConnectionStateCb(clientId, OHOS_STATE_CONNECTED, OHOS_BT_STATUS_SUCCESS);
324     ASSERT_TRUE(connectionStateCtx.Expect(clientId, OHOS_BT_STATUS_SUCCESS, OHOS_STATE_CONNECTED));
325 
326     ASSERT_EQ(SoftbusGattcSearchServices(clientId), SOFTBUS_OK);
327     gattClientCallback->searchServiceCompleteCb(clientId, OHOS_BT_STATUS_SUCCESS);
328     ASSERT_TRUE(serviceCompleteStateCtx.Expect(clientId, OHOS_BT_STATUS_SUCCESS));
329 
330     const char *serviceUuidExample = "11C8B310-80E4-4276-AFC0-F81590B2177F";
331     SoftBusBtUuid serverUuid = {
332         .uuidLen = strlen(serviceUuidExample),
333         .uuid = (char *)serviceUuidExample,
334     };
335     ASSERT_EQ(SoftbusGattcGetService(clientId, &serverUuid), SOFTBUS_OK);
336 
337     const char *charaNetUuidExample = "00002B00-0000-1000-8000-00805F9B34FB";
338     SoftBusBtUuid netUuid = {
339         .uuidLen = strlen(charaNetUuidExample),
340         .uuid = (char *)charaNetUuidExample,
341     };
342     ASSERT_EQ(SoftbusGattcRegisterNotification(clientId, &serverUuid, &netUuid, NULL), SOFTBUS_OK);
343     gattClientCallback->registerNotificationCb(clientId, OHOS_BT_STATUS_SUCCESS);
344     ASSERT_TRUE(registNotificationCtx.Expect(clientId, OHOS_BT_STATUS_SUCCESS));
345 
346     const char *charaConnUuidExample = "00002B00-0000-1000-8000-00805F9B34FB";
347     SoftBusBtUuid connUuid = {
348         .uuidLen = strlen(charaConnUuidExample),
349         .uuid = (char *)charaConnUuidExample,
350     };
351     ASSERT_EQ(SoftbusGattcRegisterNotification(clientId, &serverUuid, &connUuid, NULL), SOFTBUS_OK);
352     gattClientCallback->registerNotificationCb(clientId, OHOS_BT_STATUS_SUCCESS);
353     ASSERT_TRUE(registNotificationCtx.Expect(clientId, OHOS_BT_STATUS_SUCCESS));
354 
355     int mtu = 512;
356     ASSERT_EQ(SoftbusGattcConfigureMtuSize(clientId, mtu), SOFTBUS_OK);
357     gattClientCallback->configureMtuSizeCb(clientId, mtu, OHOS_BT_STATUS_SUCCESS);
358     ASSERT_TRUE(configureMtuSizeCtx.Expect(clientId, OHOS_BT_STATUS_SUCCESS, mtu));
359 }
360 
361 /**
362  * @tc.name: AdapterBleGattClientTest_ScanLifecycle
363  * @tc.desc: test complete gatt client connect life cycle
364  * @tc.type: FUNC
365  * @tc.require: NONE
366  */
367 HWTEST_F(AdapterBleGattClientTest, GattClientConnectCycle2, TestSize.Level3)
368 {
369     MockBluetooth mocker;
370     MockAll(mocker);
371 
372     auto clientId = SoftbusGattcRegister();
373     SoftbusGattcRegisterCallback(GetStubGattcCallback(), clientId);
374 
375     const char *serviceUuidExample = "11C8B310-80E4-4276-AFC0-F81590B2177F";
376     SoftBusBtUuid serverUuid = {
377         .uuidLen = strlen(serviceUuidExample),
378         .uuid = (char *)serviceUuidExample,
379     };
380 
381     const char *charaNetUuidExample = "00002B00-0000-1000-8000-00805F9B34FB";
382     SoftBusBtUuid netUuid = {
383         .uuidLen = strlen(charaNetUuidExample),
384         .uuid = (char *)charaNetUuidExample,
385     };
386 
387     const char *valueExample = "hello dsoftbus";
388     SoftBusGattcData data = {
389         .serviceUuid = serverUuid,
390         .characterUuid = netUuid,
391         .valueLen = strlen(valueExample),
392         .value = (uint8_t *)valueExample,
393     };
394     ASSERT_EQ(SoftbusGattcWriteCharacteristic(clientId, &data), SOFTBUS_OK);
395     BtGattCharacteristic characteristic {
396         .serviceUuid = {
397             .uuidLen = strlen(serviceUuidExample),
398             .uuid = (char *)serviceUuidExample,
399         },
400         .characteristicUuid = {
401             .uuidLen = strlen(charaNetUuidExample),
402             .uuid = (char *)charaNetUuidExample,
403         },
404     };
405     BtGattReadData readData = {
406         .attribute.characteristic = characteristic,
407         .dataLen = strlen(valueExample),
408         .data = (unsigned char *)valueExample,
409     };
410     gattClientCallback->notificationCb(clientId, &readData, OHOS_BT_STATUS_SUCCESS);
411 
412     SoftBusGattcNotify notify = {
413         .charaUuid = {
414             .uuidLen = strlen(charaNetUuidExample),
415             .uuid = (char *)charaNetUuidExample,
416         },
417         .dataLen = strlen(valueExample),
418         .data = (unsigned char *)valueExample,
419     };
420     ASSERT_TRUE(notificationReceiveCtx.Expect(clientId, OHOS_BT_STATUS_SUCCESS, &notify));
421     ASSERT_EQ(SoftbusGattcUnRegister(clientId), SOFTBUS_OK);
422 }
423 
424 /**
425  * @tc.name: AdapterBleGattClientTest_EnableFastestConn
426  * @tc.desc: test ennable ble fatest connect
427  * @tc.type: FUNC
428  * @tc.require: NONE
429  */
430 HWTEST_F(AdapterBleGattClientTest, EnableFastestConn, TestSize.Level3)
431 {
432     MockBluetooth mocker;
433     MockAll(mocker);
434 
435     ASSERT_EQ(SoftbusGattcSetFastestConn(-1), SOFTBUS_INVALID_PARAM);
436     EXPECT_CALL(mocker, BleGattcSetFastestConn)
437         .Times(2)
438         .WillOnce(Return(OHOS_BT_STATUS_FAIL))
439         .WillOnce(Return(OHOS_BT_STATUS_SUCCESS));
440     ASSERT_EQ(SoftbusGattcSetFastestConn(1), SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_SET_FASTEST_ERR);
441     ASSERT_EQ(SoftbusGattcSetFastestConn(1), SOFTBUS_OK);
442 }
443 
444 /**
445  * @tc.name: AdapterBleGattClientTest_SetBleConnectionPriority
446  * @tc.desc: test ennable ble fatest connect
447  * @tc.type: FUNC
448  * @tc.require: NONE
449  */
450 HWTEST_F(AdapterBleGattClientTest, SetBleConnectionPriority, TestSize.Level3)
451 {
452     MockBluetooth mocker;
453     MockAll(mocker);
454 
455     SoftBusBtAddr addr = {
456         .addr = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}
457     };
458     ASSERT_EQ(SoftbusGattcSetPriority(-1, &addr, SOFTBUS_GATT_PRIORITY_BALANCED), SOFTBUS_INVALID_PARAM);
459     ASSERT_EQ(SoftbusGattcSetPriority(1, nullptr, SOFTBUS_GATT_PRIORITY_BALANCED), SOFTBUS_INVALID_PARAM);
460     ASSERT_EQ(SoftbusGattcSetPriority(-1, nullptr, SOFTBUS_GATT_PRIORITY_BALANCED), SOFTBUS_INVALID_PARAM);
461     EXPECT_CALL(mocker, BleGattcSetPriority)
462         .Times(2)
463         .WillOnce(Return(OHOS_BT_STATUS_FAIL))
464         .WillOnce(Return(OHOS_BT_STATUS_SUCCESS));
465     ASSERT_EQ(SoftbusGattcSetPriority(1, &addr, SOFTBUS_GATT_PRIORITY_BALANCED),
466         SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_SET_PRIORITY_ERR);
467     ASSERT_EQ(SoftbusGattcSetPriority(1, &addr, SOFTBUS_GATT_PRIORITY_BALANCED), SOFTBUS_OK);
468 }
469 
Reset()470 void GattcNotifyRecordCtx::Reset()
471 {
472     SoftBusFree(notify.charaUuid.uuid);
473     notify.charaUuid.uuid = nullptr;
474     SoftBusFree(notify.data);
475     notify.data = nullptr;
476     (void)memset_s(&notify, sizeof(SoftBusGattcNotify), 0, sizeof(SoftBusGattcNotify));
477 }
478 
Update(int id,int st,SoftBusGattcNotify * param)479 bool GattcNotifyRecordCtx::Update(int id, int st, SoftBusGattcNotify *param)
480 {
481     if (!StRecordCtx::Update(id, st)) {
482         return false;
483     }
484     this->notify = *param;
485     notify.charaUuid.uuid = (char *)SoftBusCalloc(param->charaUuid.uuidLen);
486     notify.data = (uint8_t *)SoftBusCalloc(param->dataLen);
487     if (notify.charaUuid.uuid == nullptr || notify.data == nullptr) {
488         SoftBusFree(notify.charaUuid.uuid);
489         SoftBusFree(notify.data);
490         return false;
491     }
492     if (memcpy_s(notify.charaUuid.uuid, notify.charaUuid.uuidLen, param->charaUuid.uuid, param->charaUuid.uuidLen) !=
493         EOK) {
494         SoftBusFree(notify.charaUuid.uuid);
495         SoftBusFree(notify.data);
496         return false;
497     }
498     if (memcpy_s(notify.data, notify.dataLen, param->data, param->dataLen) != EOK) {
499         SoftBusFree(notify.charaUuid.uuid);
500         SoftBusFree(notify.data);
501         return false;
502     }
503     return true;
504 }
505 
Expect(int id,int st,SoftBusGattcNotify * param)506 testing::AssertionResult GattcNotifyRecordCtx::Expect(int id, int st, SoftBusGattcNotify *param)
507 {
508     auto result = StRecordCtx::Expect(id, st);
509     if (!result) {
510         goto ClEANUP;
511     }
512 
513     if (notify.dataLen != param->dataLen || memcmp(notify.data, param->data, notify.dataLen) != 0) {
514         result = testing::AssertionFailure() << identifier << " is call by unexpectedly SoftBusGattcNotify data";
515         goto ClEANUP;
516     }
517 
518     if (notify.charaUuid.uuidLen != param->charaUuid.uuidLen ||
519         memcmp(notify.charaUuid.uuid, param->charaUuid.uuid, notify.charaUuid.uuidLen) != 0) {
520         result = testing::AssertionFailure() << identifier << " is call by unexpectedly SoftBusGattcNotify charaUuid";
521         goto ClEANUP;
522     }
523     result = testing::AssertionSuccess();
524 ClEANUP:
525     Reset();
526     return result;
527 }
528 
529 BtGattClientCallbacks *AdapterBleGattClientTest::gattClientCallback = nullptr;
530 IntRecordCtx AdapterBleGattClientTest::connectionStateCtx("ConnectionStateCallback");
531 StRecordCtx AdapterBleGattClientTest::serviceCompleteStateCtx("ServiceCompleteCallback");
532 StRecordCtx AdapterBleGattClientTest::registNotificationCtx("RegistNotificationCallback");
533 IntRecordCtx AdapterBleGattClientTest::configureMtuSizeCtx("ConfigureMtuSizeCallback");
534 GattcNotifyRecordCtx AdapterBleGattClientTest::notificationReceiveCtx("NotificationReceiveCallback");
535 
StubConnectionStateCallback(int32_t clientId,int32_t connState,int32_t status)536 void StubConnectionStateCallback(int32_t clientId, int32_t connState, int32_t status)
537 {
538     AdapterBleGattClientTest::connectionStateCtx.Update(clientId, status, connState);
539 }
540 
StubServiceCompleteCallback(int32_t clientId,int32_t status)541 void StubServiceCompleteCallback(int32_t clientId, int32_t status)
542 {
543     AdapterBleGattClientTest::serviceCompleteStateCtx.Update(clientId, status);
544 }
545 
StubRegistNotificationCallback(int32_t clientId,int status)546 void StubRegistNotificationCallback(int32_t clientId, int status)
547 {
548     AdapterBleGattClientTest::registNotificationCtx.Update(clientId, status);
549 }
550 
StubNotificationReceiveCallback(int32_t clientId,SoftBusGattcNotify * param,int32_t status)551 void StubNotificationReceiveCallback(int32_t clientId, SoftBusGattcNotify *param, int32_t status)
552 {
553     AdapterBleGattClientTest::notificationReceiveCtx.Update(clientId, status, param);
554 }
555 
StubConfigureMtuSizeCallback(int clientId,int mtuSize,int status)556 void StubConfigureMtuSizeCallback(int clientId, int mtuSize, int status)
557 {
558     AdapterBleGattClientTest::configureMtuSizeCtx.Update(clientId, status, mtuSize);
559 }
560 
GetStubGattcCallback()561 static SoftBusGattcCallback *GetStubGattcCallback()
562 {
563     static SoftBusGattcCallback callback = {
564         .ConnectionStateCallback = StubConnectionStateCallback,
565         .ServiceCompleteCallback = StubServiceCompleteCallback,
566         .RegistNotificationCallback = StubRegistNotificationCallback,
567         .NotificationReceiveCallback = StubNotificationReceiveCallback,
568         .ConfigureMtuSizeCallback = StubConfigureMtuSizeCallback,
569     };
570     return &callback;
571 }
572 
573 } // namespace OHOS
574