• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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_SoftbusGattcRegister001
103  * @tc.desc: test gatt client register
104  * @tc.type: FUNC
105  * @tc.require: NONE
106  */
107 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcRegister001, TestSize.Level3)
108 {
109     MockBluetooth mocker;
110     MockAll(mocker);
111 
112     EXPECT_CALL(mocker, BleGattcRegister).Times(1).WillOnce(Return(0));
113     EXPECT_EQ(SoftbusGattcRegister(), -1);
114 
115     EXPECT_CALL(mocker, BleGattcRegister).WillRepeatedly(ActionBleGattcRegister);
116     EXPECT_NE(SoftbusGattcRegister(), -1);
117 }
118 
119 /**
120  * @tc.name: AdapterBleGattClientTest_SoftbusGattcRegister002
121  * @tc.desc: test gatt client register.
122  * @tc.type: FUNC
123  * @tc.require: NONE
124  */
125 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcRegister002, TestSize.Level3)
126 {
127     MockBluetooth mocker;
128     MockAll(mocker);
129     EXPECT_CALL(mocker, BleGattcRegister).WillRepeatedly(ActionBleGattcRegister);
130     int32_t clientId = SoftbusGattcRegister();
131     EXPECT_NE(clientId, -1);
132 
133     SoftBusBtAddr addr = {
134         .addr = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }
135     };
136     EXPECT_CALL(mocker, BleGattcConnect).Times(1).WillOnce(Return(OHOS_BT_STATUS_FAIL));
137     EXPECT_EQ(SoftbusGattcConnect(clientId, &addr), SOFTBUS_GATTC_INTERFACE_FAILED);
138 
139     int32_t ret = SoftbusGattcUnRegister(clientId);
140     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
141 
142     InitSoftbusAdapterClient();
143     ret = SoftbusGattcUnRegister(clientId);
144     EXPECT_EQ(ret, SOFTBUS_OK);
145 
146     EXPECT_CALL(mocker, BleGattcUnRegister).Times(1).WillOnce(Return(OHOS_BT_STATUS_FAIL));
147     ret = SoftbusGattcUnRegister(-1);
148     EXPECT_EQ(ret, SOFTBUS_GATTC_INTERFACE_FAILED);
149 }
150 
151 /**
152  * @tc.name: AdapterBleGattClientTest_SoftbusGattcUnRegister001
153  * @tc.desc: test gatt client unregister
154  * @tc.type: FUNC
155  * @tc.require: NONE
156  */
157 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcUnRegister001, TestSize.Level3)
158 {
159     InitSoftbusAdapterClient();
160     MockBluetooth mocker;
161     MockAll(mocker);
162     EXPECT_CALL(mocker, BleGattcUnRegister).Times(1).WillOnce(Return(OHOS_BT_STATUS_FAIL));
163     EXPECT_EQ(SoftbusGattcUnRegister(1), SOFTBUS_GATTC_INTERFACE_FAILED);
164 
165     int32_t clientId = 10;
166     SoftbusGattcRegisterCallback(GetStubGattcCallback(), clientId);
167     EXPECT_CALL(mocker, BleGattcUnRegister).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
168     EXPECT_EQ(SoftbusGattcUnRegister(clientId), SOFTBUS_OK);
169 }
170 
171 /**
172  * @tc.name: AdapterBleGattClientTest_SoftbusGattcConnect001
173  * @tc.desc: test gatt client connect
174  * @tc.type: FUNC
175  * @tc.require: NONE
176  */
177 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcConnect001, TestSize.Level3)
178 {
179     MockBluetooth mocker;
180     MockAll(mocker);
181 
182     EXPECT_EQ(SoftbusGattcConnect(1, nullptr), SOFTBUS_INVALID_PARAM);
183 
184     SoftBusBtAddr addr = {
185         .addr = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }
186     };
187     EXPECT_CALL(mocker, BleGattcConnect).Times(1).WillOnce(Return(OHOS_BT_STATUS_FAIL));
188     EXPECT_EQ(SoftbusGattcConnect(1, &addr), SOFTBUS_GATTC_INTERFACE_FAILED);
189 
190     EXPECT_CALL(mocker, BleGattcConnect).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
191     EXPECT_EQ(SoftbusGattcConnect(1, &addr), SOFTBUS_OK);
192 }
193 
194 /**
195  * @tc.name: AdapterBleGattClientTest_SoftbusBleGattcDisconnect001
196  * @tc.desc: test gatt client disconnect
197  * @tc.type: FUNC
198  * @tc.require: NONE
199  */
200 HWTEST_F(AdapterBleGattClientTest, SoftbusBleGattcDisconnect001, TestSize.Level3)
201 {
202     MockBluetooth mocker;
203     MockAll(mocker);
204     EXPECT_CALL(mocker, BleGattcDisconnect).Times(1).WillOnce(Return(OHOS_BT_STATUS_FAIL));
205     EXPECT_EQ(SoftbusBleGattcDisconnect(1, false), SOFTBUS_GATTC_INTERFACE_FAILED);
206 
207     EXPECT_CALL(mocker, BleGattcDisconnect).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
208     EXPECT_EQ(SoftbusBleGattcDisconnect(1, false), SOFTBUS_OK);
209 }
210 
211 /**
212  * @tc.name: AdapterBleGattClientTest_SoftbusGattcSearchServices001
213  * @tc.desc: test gatt client search service
214  * @tc.type: FUNC
215  * @tc.require: NONE
216  */
217 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcSearchServices001, TestSize.Level3)
218 {
219     MockBluetooth mocker;
220     MockAll(mocker);
221     EXPECT_CALL(mocker, BleGattcSearchServices).Times(1).WillOnce(Return(OHOS_BT_STATUS_FAIL));
222     EXPECT_EQ(SoftbusGattcSearchServices(1), SOFTBUS_GATTC_INTERFACE_FAILED);
223 
224     EXPECT_CALL(mocker, BleGattcSearchServices).Times(1).WillOnce(Return(SOFTBUS_BT_STATUS_NOT_READY));
225     EXPECT_EQ(SoftbusGattcSearchServices(1), SOFTBUS_GATTC_INTERFACE_FAILED);
226 
227     EXPECT_CALL(mocker, BleGattcSearchServices).Times(1).WillOnce(Return(SOFTBUS_BT_STATUS_NOMEM));
228     EXPECT_EQ(SoftbusGattcSearchServices(1), SOFTBUS_GATTC_INTERFACE_FAILED);
229 
230     EXPECT_CALL(mocker, BleGattcSearchServices).Times(1).WillOnce(Return(SOFTBUS_BT_STATUS_BUSY));
231     EXPECT_EQ(SoftbusGattcSearchServices(1), SOFTBUS_GATTC_INTERFACE_FAILED);
232 
233     EXPECT_CALL(mocker, BleGattcSearchServices).Times(1).WillOnce(Return(SOFTBUS_BT_STATUS_DONE));
234     EXPECT_EQ(SoftbusGattcSearchServices(1), SOFTBUS_GATTC_INTERFACE_FAILED);
235 
236     EXPECT_CALL(mocker, BleGattcSearchServices).Times(1).WillOnce(Return(SOFTBUS_BT_STATUS_UNSUPPORTED));
237     EXPECT_EQ(SoftbusGattcSearchServices(1), SOFTBUS_GATTC_INTERFACE_FAILED);
238 
239     EXPECT_CALL(mocker, BleGattcSearchServices).Times(1).WillOnce(Return(SOFTBUS_BT_STATUS_PARM_INVALID));
240     EXPECT_EQ(SoftbusGattcSearchServices(1), SOFTBUS_GATTC_INTERFACE_FAILED);
241 
242     EXPECT_CALL(mocker, BleGattcSearchServices).Times(1).WillOnce(Return(SOFTBUS_BT_STATUS_UNHANDLED));
243     EXPECT_EQ(SoftbusGattcSearchServices(1), SOFTBUS_GATTC_INTERFACE_FAILED);
244 
245     EXPECT_CALL(mocker, BleGattcSearchServices).Times(1).WillOnce(Return(SOFTBUS_BT_STATUS_AUTH_FAILURE));
246     EXPECT_EQ(SoftbusGattcSearchServices(1), SOFTBUS_GATTC_INTERFACE_FAILED);
247 
248     EXPECT_CALL(mocker, BleGattcSearchServices).Times(1).WillOnce(Return(SOFTBUS_BT_STATUS_RMT_DEV_DOWN));
249     EXPECT_EQ(SoftbusGattcSearchServices(1), SOFTBUS_GATTC_INTERFACE_FAILED);
250 
251     EXPECT_CALL(mocker, BleGattcSearchServices).Times(1).WillOnce(Return(SOFTBUS_BT_STATUS_AUTH_REJECTED));
252     EXPECT_EQ(SoftbusGattcSearchServices(1), SOFTBUS_GATTC_INTERFACE_FAILED);
253 
254     EXPECT_CALL(mocker, BleGattcSearchServices).Times(1).WillOnce(Return((BtStatus)ILLEGAL_OHOS_BT_STATUS));
255     EXPECT_EQ(SoftbusGattcSearchServices(1), SOFTBUS_GATTC_INTERFACE_FAILED);
256 
257     EXPECT_CALL(mocker, BleGattcSearchServices).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
258     EXPECT_EQ(SoftbusGattcSearchServices(1), SOFTBUS_OK);
259 }
260 
261 /**
262  * @tc.name: AdapterBleGattClientTest_SoftbusGattcGetService001
263  * @tc.desc: test gatt client get service
264  * @tc.type: FUNC
265  * @tc.require: NONE
266  */
267 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcGetService001, TestSize.Level3)
268 {
269     MockBluetooth mocker;
270     MockAll(mocker);
271     const char *serviceUuidExample = "11C8B310-80E4-4276-AFC0-F81590B2177F";
272     SoftBusBtUuid serverUuid = {
273         .uuidLen = strlen(serviceUuidExample),
274         .uuid = (char *)serviceUuidExample,
275     };
276 
277     EXPECT_EQ(SoftbusGattcGetService(-1, &serverUuid), SOFTBUS_INVALID_PARAM);
278 
279     EXPECT_EQ(SoftbusGattcGetService(1, nullptr), SOFTBUS_INVALID_PARAM);
280 
281     EXPECT_CALL(mocker, BleGattcGetService).Times(1).WillOnce(Return(false));
282     EXPECT_EQ(SoftbusGattcGetService(1, &serverUuid), SOFTBUS_GATTC_INTERFACE_FAILED);
283 
284     EXPECT_CALL(mocker, BleGattcGetService).WillRepeatedly(Return(true));
285     EXPECT_EQ(SoftbusGattcGetService(1, &serverUuid), SOFTBUS_OK);
286 }
287 
288 /**
289  * @tc.name: AdapterBleGattClientTest_SoftbusGattcRegisterNotification001
290  * @tc.desc: test gatt client register notification
291  * @tc.type: FUNC
292  * @tc.require: NONE
293  */
294 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcRegisterNotification001, TestSize.Level3)
295 {
296     MockBluetooth mocker;
297     MockAll(mocker);
298 
299     const char *serviceUuidExample = "11C8B310-80E4-4276-AFC0-F81590B2177F";
300     SoftBusBtUuid serverUuid = {
301         .uuidLen = strlen(serviceUuidExample),
302         .uuid = (char *)serviceUuidExample,
303     };
304     const char *charaNetUuidExample = "00002B00-0000-1000-8000-00805F9B34FB";
305     SoftBusBtUuid netUuid = {
306         .uuidLen = strlen(charaNetUuidExample),
307         .uuid = (char *)charaNetUuidExample,
308     };
309 
310     EXPECT_EQ(SoftbusGattcRegisterNotification(1, nullptr, &netUuid, nullptr), SOFTBUS_INVALID_PARAM);
311 
312     EXPECT_EQ(SoftbusGattcRegisterNotification(1, &serverUuid, nullptr, nullptr), SOFTBUS_INVALID_PARAM);
313 
314     EXPECT_CALL(mocker, BleGattcRegisterNotification).Times(1).WillOnce(Return(OHOS_BT_STATUS_FAIL));
315     EXPECT_EQ(SoftbusGattcRegisterNotification(1, &serverUuid, &netUuid, nullptr), SOFTBUS_GATTC_INTERFACE_FAILED);
316 
317     EXPECT_CALL(mocker, BleGattcRegisterNotification).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
318     EXPECT_EQ(SoftbusGattcRegisterNotification(1, &serverUuid, &netUuid, nullptr), SOFTBUS_OK);
319 }
320 
321 /**
322  * @tc.name: AdapterBleGattClientTest_SoftbusGattcWriteCharacteristic001
323  * @tc.desc: test gatt client write characteristic
324  * @tc.type: FUNC
325  * @tc.require: NONE
326  */
327 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcWriteCharacteristic001, TestSize.Level3)
328 {
329     MockBluetooth mocker;
330     MockAll(mocker);
331 
332     const char *serviceUuidExample = "11C8B310-80E4-4276-AFC0-F81590B2177F";
333     SoftBusBtUuid serverUuid = {
334         .uuidLen = strlen(serviceUuidExample),
335         .uuid = (char *)serviceUuidExample,
336     };
337     const char *charaNetUuidExample = "00002B00-0000-1000-8000-00805F9B34FB";
338     SoftBusBtUuid netUuid = {
339         .uuidLen = strlen(charaNetUuidExample),
340         .uuid = (char *)charaNetUuidExample,
341     };
342     const char *valueExample = "hello dsoftbus";
343     SoftBusGattcData data = {
344         .serviceUuid = serverUuid,
345         .characterUuid = netUuid,
346         .valueLen = strlen(valueExample),
347         .value = (uint8_t *)valueExample,
348     };
349     EXPECT_CALL(mocker, BleGattcWriteCharacteristic).Times(1).WillOnce(Return(OHOS_BT_STATUS_FAIL));
350     EXPECT_EQ(SoftbusGattcWriteCharacteristic(1, &data), SOFTBUS_GATTC_INTERFACE_FAILED);
351 
352     EXPECT_CALL(mocker, BleGattcWriteCharacteristic).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
353     EXPECT_EQ(SoftbusGattcWriteCharacteristic(1, &data), SOFTBUS_OK);
354 
355     data.writeType = SOFTBUS_GATT_WRITE_NO_RSP;
356     EXPECT_EQ(SoftbusGattcWriteCharacteristic(1, &data), SOFTBUS_OK);
357 
358     data.writeType = SOFTBUS_GATT_WRITE_PREPARE;
359     EXPECT_EQ(SoftbusGattcWriteCharacteristic(1, &data), SOFTBUS_OK);
360 
361     data.writeType = SOFTBUS_GATT_WRITE_DEFAULT;
362     EXPECT_EQ(SoftbusGattcWriteCharacteristic(1, &data), SOFTBUS_OK);
363 
364     data.writeType = SOFTBUS_GATT_WRITE_SIGNED;
365     EXPECT_EQ(SoftbusGattcWriteCharacteristic(1, &data), SOFTBUS_OK);
366 }
367 
368 /**
369  * @tc.name: AdapterBleGattClientTest_SoftbusGattcConfigureMtuSize001
370  * @tc.desc: test gatt client write characteristic
371  * @tc.type: FUNC
372  * @tc.require: NONE
373  */
374 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcConfigureMtuSize001, TestSize.Level3)
375 {
376     MockBluetooth mocker;
377     MockAll(mocker);
378 
379     EXPECT_CALL(mocker, BleGattcConfigureMtuSize).Times(1).WillOnce(Return(OHOS_BT_STATUS_FAIL));
380     EXPECT_EQ(SoftbusGattcConfigureMtuSize(1, 512), SOFTBUS_GATTC_INTERFACE_FAILED);
381 
382     EXPECT_CALL(mocker, BleGattcConfigureMtuSize).WillRepeatedly(Return(OHOS_BT_STATUS_SUCCESS));
383     EXPECT_EQ(SoftbusGattcConfigureMtuSize(1, 512), SOFTBUS_OK);
384 }
385 
386 /**
387  * @tc.name: AdapterBleGattClientTest_GattClientConnectCycle001
388  * @tc.desc: test complete gatt client connect life cycle
389  * @tc.type: FUNC
390  * @tc.require: NONE
391  */
392 HWTEST_F(AdapterBleGattClientTest, GattClientConnectCycle001, TestSize.Level3)
393 {
394     MockBluetooth mocker;
395     MockAll(mocker);
396 
397     auto clientId = SoftbusGattcRegister();
398     ASSERT_NE(clientId, -1);
399 
400     SoftbusGattcRegisterCallback(GetStubGattcCallback(), clientId);
401     SoftBusBtAddr addr = {
402         .addr = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }
403     };
404     ASSERT_EQ(SoftbusGattcConnect(clientId, &addr), SOFTBUS_OK);
405     gattClientCallback->ConnectionStateCb(clientId, OHOS_STATE_CONNECTED, OHOS_BT_STATUS_SUCCESS);
406     ASSERT_TRUE(connectionStateCtx.Expect(clientId, OHOS_BT_STATUS_SUCCESS, OHOS_STATE_CONNECTED));
407 
408     ASSERT_EQ(SoftbusGattcSearchServices(clientId), SOFTBUS_OK);
409     gattClientCallback->searchServiceCompleteCb(clientId, OHOS_BT_STATUS_SUCCESS);
410     ASSERT_TRUE(serviceCompleteStateCtx.Expect(clientId, OHOS_BT_STATUS_SUCCESS));
411 
412     const char *serviceUuidExample = "11C8B310-80E4-4276-AFC0-F81590B2177F";
413     SoftBusBtUuid serverUuid = {
414         .uuidLen = strlen(serviceUuidExample),
415         .uuid = (char *)serviceUuidExample,
416     };
417     ASSERT_EQ(SoftbusGattcGetService(clientId, &serverUuid), SOFTBUS_OK);
418 
419     const char *charaNetUuidExample = "00002B00-0000-1000-8000-00805F9B34FB";
420     SoftBusBtUuid netUuid = {
421         .uuidLen = strlen(charaNetUuidExample),
422         .uuid = (char *)charaNetUuidExample,
423     };
424     ASSERT_EQ(SoftbusGattcRegisterNotification(clientId, &serverUuid, &netUuid, nullptr), SOFTBUS_OK);
425     gattClientCallback->registerNotificationCb(clientId, OHOS_BT_STATUS_SUCCESS);
426     ASSERT_TRUE(registNotificationCtx.Expect(clientId, OHOS_BT_STATUS_SUCCESS));
427 
428     const char *charaConnUuidExample = "00002B00-0000-1000-8000-00805F9B34FB";
429     SoftBusBtUuid connUuid = {
430         .uuidLen = strlen(charaConnUuidExample),
431         .uuid = (char *)charaConnUuidExample,
432     };
433     ASSERT_EQ(SoftbusGattcRegisterNotification(clientId, &serverUuid, &connUuid, nullptr), SOFTBUS_OK);
434     gattClientCallback->registerNotificationCb(clientId, OHOS_BT_STATUS_SUCCESS);
435     ASSERT_TRUE(registNotificationCtx.Expect(clientId, OHOS_BT_STATUS_SUCCESS));
436 
437     int32_t mtu = 512;
438     ASSERT_EQ(SoftbusGattcConfigureMtuSize(clientId, mtu), SOFTBUS_OK);
439     gattClientCallback->configureMtuSizeCb(clientId, mtu, OHOS_BT_STATUS_SUCCESS);
440     ASSERT_TRUE(configureMtuSizeCtx.Expect(clientId, OHOS_BT_STATUS_SUCCESS, mtu));
441 }
442 
443 /**
444  * @tc.name: AdapterBleGattClientTest_GattClientConnectCycle002
445  * @tc.desc: test complete gatt client connect life cycle
446  * @tc.type: FUNC
447  * @tc.require: NONE
448  */
449 HWTEST_F(AdapterBleGattClientTest, GattClientConnectCycle002, TestSize.Level3)
450 {
451     MockBluetooth mocker;
452     MockAll(mocker);
453 
454     auto clientId = SoftbusGattcRegister();
455     SoftbusGattcRegisterCallback(GetStubGattcCallback(), clientId);
456 
457     const char *serviceUuidExample = "11C8B310-80E4-4276-AFC0-F81590B2177F";
458     SoftBusBtUuid serverUuid = {
459         .uuidLen = strlen(serviceUuidExample),
460         .uuid = (char *)serviceUuidExample,
461     };
462 
463     const char *charaNetUuidExample = "00002B00-0000-1000-8000-00805F9B34FB";
464     SoftBusBtUuid netUuid = {
465         .uuidLen = strlen(charaNetUuidExample),
466         .uuid = (char *)charaNetUuidExample,
467     };
468 
469     const char *valueExample = "hello dsoftbus";
470     SoftBusGattcData data = {
471         .serviceUuid = serverUuid,
472         .characterUuid = netUuid,
473         .valueLen = strlen(valueExample),
474         .value = (uint8_t *)valueExample,
475     };
476     ASSERT_EQ(SoftbusGattcWriteCharacteristic(clientId, &data), SOFTBUS_OK);
477     BtGattCharacteristic characteristic {
478         .serviceUuid = {
479             .uuidLen = strlen(serviceUuidExample),
480             .uuid = (char *)serviceUuidExample,
481         },
482         .characteristicUuid = {
483             .uuidLen = strlen(charaNetUuidExample),
484             .uuid = (char *)charaNetUuidExample,
485         },
486     };
487     BtGattReadData readData = {
488         .attribute.characteristic = characteristic,
489         .dataLen = strlen(valueExample),
490         .data = (unsigned char *)valueExample,
491     };
492     gattClientCallback->notificationCb(clientId, &readData, OHOS_BT_STATUS_SUCCESS);
493 
494     SoftBusGattcNotify notify = {
495         .charaUuid = {
496             .uuidLen = strlen(charaNetUuidExample),
497             .uuid = (char *)charaNetUuidExample,
498         },
499         .dataLen = strlen(valueExample),
500         .data = (unsigned char *)valueExample,
501     };
502     ASSERT_TRUE(notificationReceiveCtx.Expect(clientId, OHOS_BT_STATUS_SUCCESS, &notify));
503     ASSERT_EQ(SoftbusGattcUnRegister(clientId), SOFTBUS_OK);
504 }
505 
506 /**
507  * @tc.name: AdapterBleGattClientTest_EnableFastestConn001
508  * @tc.desc: test ennable ble fatest connect
509  * @tc.type: FUNC
510  * @tc.require: NONE
511  */
512 HWTEST_F(AdapterBleGattClientTest, EnableFastestConn001, TestSize.Level3)
513 {
514     MockBluetooth mocker;
515     MockAll(mocker);
516 
517     ASSERT_EQ(SoftbusGattcSetFastestConn(-1), SOFTBUS_INVALID_PARAM);
518     EXPECT_CALL(mocker, BleGattcSetFastestConn)
519         .Times(2)
520         .WillOnce(Return(OHOS_BT_STATUS_FAIL))
521         .WillOnce(Return(OHOS_BT_STATUS_SUCCESS));
522     ASSERT_EQ(SoftbusGattcSetFastestConn(1), SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_SET_FASTEST_ERR);
523     ASSERT_EQ(SoftbusGattcSetFastestConn(1), SOFTBUS_OK);
524 }
525 
526 /**
527  * @tc.name: AdapterBleGattClientTest_SetBleConnectionPriority001
528  * @tc.desc: test ennable ble fatest connect
529  * @tc.type: FUNC
530  * @tc.require: NONE
531  */
532 HWTEST_F(AdapterBleGattClientTest, SetBleConnectionPriority001, TestSize.Level3)
533 {
534     MockBluetooth mocker;
535     MockAll(mocker);
536 
537     SoftBusBtAddr addr = {
538         .addr = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }
539     };
540     ASSERT_EQ(SoftbusGattcSetPriority(-1, &addr, SOFTBUS_GATT_PRIORITY_BALANCED), SOFTBUS_INVALID_PARAM);
541     ASSERT_EQ(SoftbusGattcSetPriority(1, nullptr, SOFTBUS_GATT_PRIORITY_BALANCED), SOFTBUS_INVALID_PARAM);
542     ASSERT_EQ(SoftbusGattcSetPriority(-1, nullptr, SOFTBUS_GATT_PRIORITY_BALANCED), SOFTBUS_INVALID_PARAM);
543     EXPECT_CALL(mocker, BleGattcSetPriority)
544         .Times(2)
545         .WillOnce(Return(OHOS_BT_STATUS_FAIL))
546         .WillOnce(Return(OHOS_BT_STATUS_SUCCESS));
547     ASSERT_EQ(SoftbusGattcSetPriority(1, &addr, SOFTBUS_GATT_PRIORITY_BALANCED),
548         SOFTBUS_CONN_BLE_UNDERLAY_CLIENT_SET_PRIORITY_ERR);
549     ASSERT_EQ(SoftbusGattcSetPriority(1, &addr, SOFTBUS_GATT_PRIORITY_BALANCED), SOFTBUS_OK);
550 }
551 
552 /**
553  * @tc.name: AdapterBleGattClientTest_SoftbusGattcCheckExistConnectionByAddr001
554  * @tc.desc: test check exist connection by addr.
555  * @tc.type: FUNC
556  * @tc.require: NONE
557  */
558 HWTEST_F(AdapterBleGattClientTest, SoftbusGattcCheckExistConnectionByAddr001, TestSize.Level3)
559 {
560     SoftBusBtAddr addr = {
561         .addr = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }
562     };
563     EXPECT_FALSE(SoftbusGattcCheckExistConnectionByAddr(nullptr));
564     EXPECT_TRUE(SoftbusGattcCheckExistConnectionByAddr(&addr));
565 
566     InitSoftbusAdapterClient();
567     EXPECT_FALSE(SoftbusGattcCheckExistConnectionByAddr(&addr));
568 }
569 
Reset()570 void GattcNotifyRecordCtx::Reset()
571 {
572     SoftBusFree(notify.charaUuid.uuid);
573     notify.charaUuid.uuid = nullptr;
574     SoftBusFree(notify.data);
575     notify.data = nullptr;
576     (void)memset_s(&notify, sizeof(SoftBusGattcNotify), 0, sizeof(SoftBusGattcNotify));
577 }
578 
Update(int32_t id,int32_t st,SoftBusGattcNotify * param)579 bool GattcNotifyRecordCtx::Update(int32_t id, int32_t st, SoftBusGattcNotify *param)
580 {
581     if (!StRecordCtx::Update(id, st)) {
582         return false;
583     }
584     this->notify = *param;
585     notify.charaUuid.uuid = (char *)SoftBusCalloc(param->charaUuid.uuidLen);
586     notify.data = (uint8_t *)SoftBusCalloc(param->dataLen);
587     if (notify.charaUuid.uuid == nullptr || notify.data == nullptr) {
588         SoftBusFree(notify.charaUuid.uuid);
589         SoftBusFree(notify.data);
590         return false;
591     }
592     if (memcpy_s(notify.charaUuid.uuid, notify.charaUuid.uuidLen, param->charaUuid.uuid, param->charaUuid.uuidLen) !=
593         EOK) {
594         SoftBusFree(notify.charaUuid.uuid);
595         SoftBusFree(notify.data);
596         return false;
597     }
598     if (memcpy_s(notify.data, notify.dataLen, param->data, param->dataLen) != EOK) {
599         SoftBusFree(notify.charaUuid.uuid);
600         SoftBusFree(notify.data);
601         return false;
602     }
603     return true;
604 }
605 
Expect(int32_t id,int32_t st,SoftBusGattcNotify * param)606 testing::AssertionResult GattcNotifyRecordCtx::Expect(int32_t id, int32_t st, SoftBusGattcNotify *param)
607 {
608     auto result = StRecordCtx::Expect(id, st);
609     if (!result) {
610         goto ClEANUP;
611     }
612 
613     if (notify.dataLen != param->dataLen || memcmp(notify.data, param->data, notify.dataLen) != 0) {
614         result = testing::AssertionFailure() << identifier << " is call by unexpectedly SoftBusGattcNotify data";
615         goto ClEANUP;
616     }
617 
618     if (notify.charaUuid.uuidLen != param->charaUuid.uuidLen ||
619         memcmp(notify.charaUuid.uuid, param->charaUuid.uuid, notify.charaUuid.uuidLen) != 0) {
620         result = testing::AssertionFailure() << identifier << " is call by unexpectedly SoftBusGattcNotify charaUuid";
621         goto ClEANUP;
622     }
623     result = testing::AssertionSuccess();
624 ClEANUP:
625     Reset();
626     return result;
627 }
628 
629 BtGattClientCallbacks *AdapterBleGattClientTest::gattClientCallback = nullptr;
630 IntRecordCtx AdapterBleGattClientTest::connectionStateCtx("ConnectionStateCallback");
631 StRecordCtx AdapterBleGattClientTest::serviceCompleteStateCtx("ServiceCompleteCallback");
632 StRecordCtx AdapterBleGattClientTest::registNotificationCtx("RegistNotificationCallback");
633 IntRecordCtx AdapterBleGattClientTest::configureMtuSizeCtx("ConfigureMtuSizeCallback");
634 GattcNotifyRecordCtx AdapterBleGattClientTest::notificationReceiveCtx("NotificationReceiveCallback");
635 
StubConnectionStateCallback(int32_t clientId,int32_t connState,int32_t status)636 void StubConnectionStateCallback(int32_t clientId, int32_t connState, int32_t status)
637 {
638     AdapterBleGattClientTest::connectionStateCtx.Update(clientId, status, connState);
639 }
640 
StubServiceCompleteCallback(int32_t clientId,int32_t status)641 void StubServiceCompleteCallback(int32_t clientId, int32_t status)
642 {
643     AdapterBleGattClientTest::serviceCompleteStateCtx.Update(clientId, status);
644 }
645 
StubRegistNotificationCallback(int32_t clientId,int32_t status)646 void StubRegistNotificationCallback(int32_t clientId, int32_t status)
647 {
648     AdapterBleGattClientTest::registNotificationCtx.Update(clientId, status);
649 }
650 
StubNotificationReceiveCallback(int32_t clientId,SoftBusGattcNotify * param,int32_t status)651 void StubNotificationReceiveCallback(int32_t clientId, SoftBusGattcNotify *param, int32_t status)
652 {
653     AdapterBleGattClientTest::notificationReceiveCtx.Update(clientId, status, param);
654 }
655 
StubConfigureMtuSizeCallback(int32_t clientId,int32_t mtuSize,int32_t status)656 void StubConfigureMtuSizeCallback(int32_t clientId, int32_t mtuSize, int32_t status)
657 {
658     AdapterBleGattClientTest::configureMtuSizeCtx.Update(clientId, status, mtuSize);
659 }
660 
GetStubGattcCallback()661 static SoftBusGattcCallback *GetStubGattcCallback()
662 {
663     static SoftBusGattcCallback callback = {
664         .connectionStateCallback = StubConnectionStateCallback,
665         .serviceCompleteCallback = StubServiceCompleteCallback,
666         .registNotificationCallback = StubRegistNotificationCallback,
667         .notificationReceiveCallback = StubNotificationReceiveCallback,
668         .configureMtuSizeCallback = StubConfigureMtuSizeCallback,
669     };
670     return &callback;
671 }
672 
673 } // namespace OHOS
674