• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
108     EXPECT_CALL(mocker, BleGattcRegister).Times(1).WillOnce(Return(0));
109     EXPECT_EQ(SoftbusGattcRegister(), -1);
110 
111     EXPECT_CALL(mocker, BleGattcRegister).WillRepeatedly(ActionBleGattcRegister);
112     EXPECT_NE(SoftbusGattcRegister(), -1);
113 }
114 
115 /**
116  * @tc.name: AdapterBleGattClientTest_SoftbusGattcUnRegister
117  * @tc.desc: test gatt client unregister
118  * @tc.type: FUNC
119  * @tc.require: NONE
120  */
121 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcUnRegister, TestSize.Level3)
122 {
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     EXPECT_CALL(mocker, BleGattcUnRegister).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
129     EXPECT_EQ(SoftbusGattcUnRegister(1), SOFTBUS_OK);
130 }
131 
132 /**
133  * @tc.name: AdapterBleGattClientTest_SoftbusGattcConnect
134  * @tc.desc: test gatt client connect
135  * @tc.type: FUNC
136  * @tc.require: NONE
137  */
138 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcConnect, TestSize.Level3)
139 {
140     MockBluetooth mocker;
141     MockAll(mocker);
142 
143     SoftBusBtAddr addr = {
144         .addr = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}
145     };
146     EXPECT_CALL(mocker, BleGattcConnect).Times(1).WillOnce(Return(OHOS_BT_STATUS_FAIL));
147     EXPECT_EQ(SoftbusGattcConnect(1, &addr), SOFTBUS_GATTC_INTERFACE_FAILED);
148 
149     EXPECT_CALL(mocker, BleGattcConnect).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
150     EXPECT_EQ(SoftbusGattcConnect(1, &addr), SOFTBUS_OK);
151 }
152 
153 /**
154  * @tc.name: AdapterBleGattClientTest_SoftbusBleGattcDisconnect
155  * @tc.desc: test gatt client disconnect
156  * @tc.type: FUNC
157  * @tc.require: NONE
158  */
159 HWTEST_F(AdapterBleGattClientTest, SoftbusBleGattcDisconnect, TestSize.Level3)
160 {
161     MockBluetooth mocker;
162     MockAll(mocker);
163     EXPECT_CALL(mocker, BleGattcDisconnect).Times(1).WillOnce(Return(OHOS_BT_STATUS_FAIL));
164     EXPECT_EQ(SoftbusBleGattcDisconnect(1, false), SOFTBUS_GATTC_INTERFACE_FAILED);
165 
166     EXPECT_CALL(mocker, BleGattcDisconnect).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
167     EXPECT_EQ(SoftbusBleGattcDisconnect(1, false), SOFTBUS_OK);
168 }
169 
170 /**
171  * @tc.name: AdapterBleGattClientTest_SoftbusGattcSearchServices
172  * @tc.desc: test gatt client search service
173  * @tc.type: FUNC
174  * @tc.require: NONE
175  */
176 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcSearchServices, TestSize.Level3)
177 {
178     MockBluetooth mocker;
179     MockAll(mocker);
180     EXPECT_CALL(mocker, BleGattcSearchServices).Times(1).WillOnce(Return(OHOS_BT_STATUS_FAIL));
181     EXPECT_EQ(SoftbusGattcSearchServices(1), SOFTBUS_GATTC_INTERFACE_FAILED);
182 
183     EXPECT_CALL(mocker, BleGattcSearchServices).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
184     EXPECT_EQ(SoftbusGattcSearchServices(1), SOFTBUS_OK);
185 }
186 
187 /**
188  * @tc.name: AdapterBleGattClientTest_SoftbusGattcGetService
189  * @tc.desc: test gatt client get service
190  * @tc.type: FUNC
191  * @tc.require: NONE
192  */
193 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcGetService, TestSize.Level3)
194 {
195     MockBluetooth mocker;
196     MockAll(mocker);
197     const char *serviceUuidExample = "11C8B310-80E4-4276-AFC0-F81590B2177F";
198     SoftBusBtUuid serverUuid = {
199         .uuidLen = strlen(serviceUuidExample),
200         .uuid = (char *)serviceUuidExample,
201     };
202 
203     EXPECT_CALL(mocker, BleGattcGetService).Times(1).WillOnce(Return(false));
204     EXPECT_EQ(SoftbusGattcGetService(1, &serverUuid), SOFTBUS_GATTC_INTERFACE_FAILED);
205 
206     EXPECT_CALL(mocker, BleGattcGetService).WillRepeatedly(Return(true));
207     EXPECT_EQ(SoftbusGattcGetService(1, &serverUuid), SOFTBUS_OK);
208 }
209 
210 /**
211  * @tc.name: AdapterBleGattClientTest_SoftbusGattcRegisterNotification
212  * @tc.desc: test gatt client register notification
213  * @tc.type: FUNC
214  * @tc.require: NONE
215  */
216 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcRegisterNotification, TestSize.Level3)
217 {
218     MockBluetooth mocker;
219     MockAll(mocker);
220 
221     const char *serviceUuidExample = "11C8B310-80E4-4276-AFC0-F81590B2177F";
222     SoftBusBtUuid serverUuid = {
223         .uuidLen = strlen(serviceUuidExample),
224         .uuid = (char *)serviceUuidExample,
225     };
226     const char *charaNetUuidExample = "00002B00-0000-1000-8000-00805F9B34FB";
227     SoftBusBtUuid netUuid = {
228         .uuidLen = strlen(charaNetUuidExample),
229         .uuid = (char *)charaNetUuidExample,
230     };
231     EXPECT_CALL(mocker, BleGattcRegisterNotification).Times(1).WillOnce(Return(OHOS_BT_STATUS_FAIL));
232     EXPECT_EQ(SoftbusGattcRegisterNotification(1, &serverUuid, &netUuid, NULL), SOFTBUS_GATTC_INTERFACE_FAILED);
233 
234     EXPECT_CALL(mocker, BleGattcRegisterNotification).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
235     EXPECT_EQ(SoftbusGattcRegisterNotification(1, &serverUuid, &netUuid, NULL), SOFTBUS_OK);
236 }
237 
238 /**
239  * @tc.name: AdapterBleGattClientTest_SoftbusGattcWriteCharacteristic
240  * @tc.desc: test gatt client write characteristic
241  * @tc.type: FUNC
242  * @tc.require: NONE
243  */
244 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcWriteCharacteristic, TestSize.Level3)
245 {
246     MockBluetooth mocker;
247     MockAll(mocker);
248 
249     const char *serviceUuidExample = "11C8B310-80E4-4276-AFC0-F81590B2177F";
250     SoftBusBtUuid serverUuid = {
251         .uuidLen = strlen(serviceUuidExample),
252         .uuid = (char *)serviceUuidExample,
253     };
254     const char *charaNetUuidExample = "00002B00-0000-1000-8000-00805F9B34FB";
255     SoftBusBtUuid netUuid = {
256         .uuidLen = strlen(charaNetUuidExample),
257         .uuid = (char *)charaNetUuidExample,
258     };
259     const char *valueExample = "hello dsoftbus";
260     SoftBusGattcData data = {
261         .serviceUuid = serverUuid,
262         .characterUuid = netUuid,
263         .valueLen = strlen(valueExample),
264         .value = (uint8_t *)valueExample,
265     };
266     EXPECT_CALL(mocker, BleGattcWriteCharacteristic).Times(1).WillOnce(Return(OHOS_BT_STATUS_FAIL));
267     EXPECT_EQ(SoftbusGattcWriteCharacteristic(1, &data), SOFTBUS_GATTC_INTERFACE_FAILED);
268 
269     EXPECT_CALL(mocker, BleGattcWriteCharacteristic).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
270     EXPECT_EQ(SoftbusGattcWriteCharacteristic(1, &data), SOFTBUS_OK);
271 }
272 
273 /**
274  * @tc.name: AdapterBleGattClientTest_SoftbusGattcConfigureMtuSize
275  * @tc.desc: test gatt client write characteristic
276  * @tc.type: FUNC
277  * @tc.require: NONE
278  */
279 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcConfigureMtuSize, TestSize.Level3)
280 {
281     MockBluetooth mocker;
282     MockAll(mocker);
283 
284     EXPECT_CALL(mocker, BleGattcConfigureMtuSize).Times(1).WillOnce(Return(OHOS_BT_STATUS_FAIL));
285     EXPECT_EQ(SoftbusGattcConfigureMtuSize(1, 512), SOFTBUS_GATTC_INTERFACE_FAILED);
286 
287     EXPECT_CALL(mocker, BleGattcConfigureMtuSize).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
288     EXPECT_EQ(SoftbusGattcConfigureMtuSize(1, 512), SOFTBUS_OK);
289 }
290 
291 /**
292  * @tc.name: AdapterBleGattClientTest_ScanLifecycle
293  * @tc.desc: test complete gatt client connect life cycle
294  * @tc.type: FUNC
295  * @tc.require: NONE
296  */
297 HWTEST_F(AdapterBleGattClientTest, GattClientConnectCycle, TestSize.Level3)
298 {
299     SoftbusGattcRegisterCallback(GetStubGattcCallback());
300     MockBluetooth mocker;
301     MockAll(mocker);
302 
303     auto clientId = SoftbusGattcRegister();
304     ASSERT_NE(clientId, -1);
305 
306     SoftBusBtAddr addr = {
307         .addr = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}
308     };
309     ASSERT_EQ(SoftbusGattcConnect(clientId, &addr), SOFTBUS_OK);
310     gattClientCallback->ConnectionStateCb(clientId, OHOS_STATE_CONNECTED, OHOS_BT_STATUS_SUCCESS);
311     ASSERT_TRUE(connectionStateCtx.Expect(clientId, OHOS_BT_STATUS_SUCCESS, OHOS_STATE_CONNECTED));
312 
313     ASSERT_EQ(SoftbusGattcSearchServices(clientId), SOFTBUS_OK);
314     gattClientCallback->searchServiceCompleteCb(clientId, OHOS_BT_STATUS_SUCCESS);
315     ASSERT_TRUE(serviceCompleteStateCtx.Expect(clientId, OHOS_BT_STATUS_SUCCESS));
316 
317     const char *serviceUuidExample = "11C8B310-80E4-4276-AFC0-F81590B2177F";
318     SoftBusBtUuid serverUuid = {
319         .uuidLen = strlen(serviceUuidExample),
320         .uuid = (char *)serviceUuidExample,
321     };
322     ASSERT_EQ(SoftbusGattcGetService(clientId, &serverUuid), SOFTBUS_OK);
323 
324     const char *charaNetUuidExample = "00002B00-0000-1000-8000-00805F9B34FB";
325     SoftBusBtUuid netUuid = {
326         .uuidLen = strlen(charaNetUuidExample),
327         .uuid = (char *)charaNetUuidExample,
328     };
329     ASSERT_EQ(SoftbusGattcRegisterNotification(clientId, &serverUuid, &netUuid, NULL), SOFTBUS_OK);
330     gattClientCallback->registerNotificationCb(clientId, OHOS_BT_STATUS_SUCCESS);
331     ASSERT_TRUE(registNotificationCtx.Expect(clientId, OHOS_BT_STATUS_SUCCESS));
332 
333     const char *charaConnUuidExample = "00002B00-0000-1000-8000-00805F9B34FB";
334     SoftBusBtUuid connUuid = {
335         .uuidLen = strlen(charaConnUuidExample),
336         .uuid = (char *)charaConnUuidExample,
337     };
338     ASSERT_EQ(SoftbusGattcRegisterNotification(clientId, &serverUuid, &connUuid, NULL), SOFTBUS_OK);
339     gattClientCallback->registerNotificationCb(clientId, OHOS_BT_STATUS_SUCCESS);
340     ASSERT_TRUE(registNotificationCtx.Expect(clientId, OHOS_BT_STATUS_SUCCESS));
341 
342     int mtu = 512;
343     ASSERT_EQ(SoftbusGattcConfigureMtuSize(clientId, mtu), SOFTBUS_OK);
344     gattClientCallback->configureMtuSizeCb(clientId, mtu, OHOS_BT_STATUS_SUCCESS);
345     ASSERT_TRUE(configureMtuSizeCtx.Expect(clientId, OHOS_BT_STATUS_SUCCESS, mtu));
346 
347     const char *valueExample = "hello dsoftbus";
348     SoftBusGattcData data = {
349         .serviceUuid = serverUuid,
350         .characterUuid = netUuid,
351         .valueLen = strlen(valueExample),
352         .value = (uint8_t *)valueExample,
353     };
354     ASSERT_EQ(SoftbusGattcWriteCharacteristic(clientId, &data), SOFTBUS_OK);
355     BtGattCharacteristic characteristic {
356         .serviceUuid = {
357             .uuidLen = strlen(serviceUuidExample),
358             .uuid = (char *)serviceUuidExample,
359         },
360         .characteristicUuid = {
361             .uuidLen = strlen(charaNetUuidExample),
362             .uuid = (char *)charaNetUuidExample,
363         },
364     };
365     BtGattReadData readData = {
366         .attribute.characteristic = characteristic,
367         .dataLen = strlen(valueExample),
368         .data = (unsigned char *)valueExample,
369     };
370     gattClientCallback->notificationCb(clientId, &readData, OHOS_BT_STATUS_SUCCESS);
371 
372     SoftBusGattcNotify notify = {
373         .charaUuid =
374             {
375                         .uuidLen = strlen(charaNetUuidExample),
376                         .uuid = (char *)charaNetUuidExample,
377                         },
378         .dataLen = strlen(valueExample),
379         .data = (unsigned char *)valueExample,
380     };
381     ASSERT_TRUE(notificationReceiveCtx.Expect(clientId, OHOS_BT_STATUS_SUCCESS, &notify));
382     ASSERT_EQ(SoftbusGattcUnRegister(clientId), SOFTBUS_OK);
383 }
384 
385 /**
386  * @tc.name: AdapterBleGattClientTest_EnableFastestConn
387  * @tc.desc: test ennable ble fatest connect
388  * @tc.type: FUNC
389  * @tc.require: NONE
390  */
391 HWTEST_F(AdapterBleGattClientTest, EnableFastestConn, TestSize.Level3)
392 {
393     MockBluetooth mocker;
394     MockAll(mocker);
395 
396     ASSERT_EQ(SoftbusGattcSetFastestConn(-1), SOFTBUS_INVALID_PARAM);
397     EXPECT_CALL(mocker, BleGattcSetFastestConn)
398         .Times(2)
399         .WillOnce(Return(OHOS_BT_STATUS_FAIL))
400         .WillOnce(Return(OHOS_BT_STATUS_SUCCESS));
401     ASSERT_EQ(SoftbusGattcSetFastestConn(1), SOFTBUS_ERR);
402     ASSERT_EQ(SoftbusGattcSetFastestConn(1), SOFTBUS_OK);
403 }
404 
405 /**
406  * @tc.name: AdapterBleGattClientTest_SetBleConnectionPriority
407  * @tc.desc: test ennable ble fatest connect
408  * @tc.type: FUNC
409  * @tc.require: NONE
410  */
411 HWTEST_F(AdapterBleGattClientTest, SetBleConnectionPriority, TestSize.Level3)
412 {
413     MockBluetooth mocker;
414     MockAll(mocker);
415 
416     SoftBusBtAddr addr = {
417         .addr = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}
418     };
419     ASSERT_EQ(SoftbusGattcSetPriority(-1, &addr, SOFTBUS_GATT_PRIORITY_BALANCED), SOFTBUS_INVALID_PARAM);
420     ASSERT_EQ(SoftbusGattcSetPriority(1, nullptr, SOFTBUS_GATT_PRIORITY_BALANCED), SOFTBUS_INVALID_PARAM);
421     ASSERT_EQ(SoftbusGattcSetPriority(-1, nullptr, SOFTBUS_GATT_PRIORITY_BALANCED), SOFTBUS_INVALID_PARAM);
422     EXPECT_CALL(mocker, BleGattcSetPriority)
423         .Times(2)
424         .WillOnce(Return(OHOS_BT_STATUS_FAIL))
425         .WillOnce(Return(OHOS_BT_STATUS_SUCCESS));
426     ASSERT_EQ(SoftbusGattcSetPriority(1, &addr, SOFTBUS_GATT_PRIORITY_BALANCED), SOFTBUS_ERR);
427     ASSERT_EQ(SoftbusGattcSetPriority(1, &addr, SOFTBUS_GATT_PRIORITY_BALANCED), SOFTBUS_OK);
428 }
429 
Reset()430 void GattcNotifyRecordCtx::Reset()
431 {
432     SoftBusFree(notify.charaUuid.uuid);
433     notify.charaUuid.uuid = nullptr;
434     SoftBusFree(notify.data);
435     notify.data = nullptr;
436     (void)memset_s(&notify, sizeof(SoftBusGattcNotify), 0, sizeof(SoftBusGattcNotify));
437 }
438 
Update(int id,int st,SoftBusGattcNotify * param)439 bool GattcNotifyRecordCtx::Update(int id, int st, SoftBusGattcNotify *param)
440 {
441     if (!StRecordCtx::Update(id, st)) {
442         return false;
443     }
444     this->notify = *param;
445     notify.charaUuid.uuid = (char *)SoftBusCalloc(param->charaUuid.uuidLen);
446     notify.data = (uint8_t *)SoftBusCalloc(param->dataLen);
447     if (notify.charaUuid.uuid == nullptr || notify.data == nullptr) {
448         SoftBusFree(notify.charaUuid.uuid);
449         SoftBusFree(notify.data);
450         return false;
451     }
452     if (memcpy_s(notify.charaUuid.uuid, notify.charaUuid.uuidLen, param->charaUuid.uuid, param->charaUuid.uuidLen) !=
453         EOK) {
454         SoftBusFree(notify.charaUuid.uuid);
455         SoftBusFree(notify.data);
456         return false;
457     }
458     if (memcpy_s(notify.data, notify.dataLen, param->data, param->dataLen) != EOK) {
459         SoftBusFree(notify.charaUuid.uuid);
460         SoftBusFree(notify.data);
461         return false;
462     }
463     return true;
464 }
465 
Expect(int id,int st,SoftBusGattcNotify * param)466 testing::AssertionResult GattcNotifyRecordCtx::Expect(int id, int st, SoftBusGattcNotify *param)
467 {
468     auto result = StRecordCtx::Expect(id, st);
469     if (!result) {
470         goto ClEANUP;
471     }
472 
473     if (notify.dataLen != param->dataLen || memcmp(notify.data, param->data, notify.dataLen) != 0) {
474         result = testing::AssertionFailure() << identifier << " is call by unexpectedly SoftBusGattcNotify data";
475         goto ClEANUP;
476     }
477 
478     if (notify.charaUuid.uuidLen != param->charaUuid.uuidLen ||
479         memcmp(notify.charaUuid.uuid, param->charaUuid.uuid, notify.charaUuid.uuidLen) != 0) {
480         result = testing::AssertionFailure() << identifier << " is call by unexpectedly SoftBusGattcNotify charaUuid";
481         goto ClEANUP;
482     }
483     result = testing::AssertionSuccess();
484 ClEANUP:
485     Reset();
486     return result;
487 }
488 
489 BtGattClientCallbacks *AdapterBleGattClientTest::gattClientCallback = nullptr;
490 IntRecordCtx AdapterBleGattClientTest::connectionStateCtx("ConnectionStateCallback");
491 StRecordCtx AdapterBleGattClientTest::serviceCompleteStateCtx("ServiceCompleteCallback");
492 StRecordCtx AdapterBleGattClientTest::registNotificationCtx("RegistNotificationCallback");
493 IntRecordCtx AdapterBleGattClientTest::configureMtuSizeCtx("ConfigureMtuSizeCallback");
494 GattcNotifyRecordCtx AdapterBleGattClientTest::notificationReceiveCtx("NotificationReceiveCallback");
495 
StubConnectionStateCallback(int32_t clientId,int32_t connState,int32_t status)496 void StubConnectionStateCallback(int32_t clientId, int32_t connState, int32_t status)
497 {
498     AdapterBleGattClientTest::connectionStateCtx.Update(clientId, status, connState);
499 }
500 
StubServiceCompleteCallback(int32_t clientId,int32_t status)501 void StubServiceCompleteCallback(int32_t clientId, int32_t status)
502 {
503     AdapterBleGattClientTest::serviceCompleteStateCtx.Update(clientId, status);
504 }
505 
StubRegistNotificationCallback(int32_t clientId,int status)506 void StubRegistNotificationCallback(int32_t clientId, int status)
507 {
508     AdapterBleGattClientTest::registNotificationCtx.Update(clientId, status);
509 }
510 
StubNotificationReceiveCallback(int32_t clientId,SoftBusGattcNotify * param,int32_t status)511 void StubNotificationReceiveCallback(int32_t clientId, SoftBusGattcNotify *param, int32_t status)
512 {
513     AdapterBleGattClientTest::notificationReceiveCtx.Update(clientId, status, param);
514 }
515 
StubConfigureMtuSizeCallback(int clientId,int mtuSize,int status)516 void StubConfigureMtuSizeCallback(int clientId, int mtuSize, int status)
517 {
518     AdapterBleGattClientTest::configureMtuSizeCtx.Update(clientId, status, mtuSize);
519 }
520 
GetStubGattcCallback()521 static SoftBusGattcCallback *GetStubGattcCallback()
522 {
523     static SoftBusGattcCallback callback = {
524         .ConnectionStateCallback = StubConnectionStateCallback,
525         .ServiceCompleteCallback = StubServiceCompleteCallback,
526         .RegistNotificationCallback = StubRegistNotificationCallback,
527         .NotificationReceiveCallback = StubNotificationReceiveCallback,
528         .ConfigureMtuSizeCallback = StubConfigureMtuSizeCallback,
529     };
530     return &callback;
531 }
532 
533 } // namespace OHOS
534