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