1 //
2 // Copyright 2015 Google, Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at:
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "service/gatt_server.h"
18
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21
22 #include "service/hal/fake_bluetooth_gatt_interface.h"
23 #include "types/raw_address.h"
24
25 using ::testing::_;
26 using ::testing::Return;
27
28 namespace bluetooth {
29 namespace {
30
31 class MockGattHandler
32 : public hal::FakeBluetoothGattInterface::TestServerHandler {
33 public:
34 MockGattHandler() = default;
35 MockGattHandler(const MockGattHandler&) = delete;
36 MockGattHandler& operator=(const MockGattHandler&) = delete;
37
38 ~MockGattHandler() override = default;
39
40 MOCK_METHOD2(RegisterServer,
41 bt_status_t(const bluetooth::Uuid&, bool eatt_support));
42 MOCK_METHOD1(UnregisterServer, bt_status_t(int));
43 MOCK_METHOD2(AddService, bt_status_t(int, std::vector<btgatt_db_element_t>));
44 MOCK_METHOD5(AddCharacteristic,
45 bt_status_t(int, int, bluetooth::Uuid*, int, int));
46 MOCK_METHOD4(AddDescriptor, bt_status_t(int, int, bluetooth::Uuid*, int));
47 MOCK_METHOD3(StartService, bt_status_t(int, int, int));
48 MOCK_METHOD2(DeleteService, bt_status_t(int, int));
49 MOCK_METHOD5(SendIndication,
50 bt_status_t(int, int, int, int, std::vector<uint8_t>));
51 MOCK_METHOD4(SendResponse,
52 bt_status_t(int, int, int, const btgatt_response_t&));
53 };
54
55 class TestDelegate : public GattServer::Delegate {
56 public:
57 TestDelegate() = default;
58 ~TestDelegate() override = default;
59
60 struct RequestData {
RequestDatabluetooth::__anon5ed327020111::TestDelegate::RequestData61 RequestData()
62 : id(-1),
63 offset(-1),
64 is_long(false),
65 is_prep(false),
66 need_rsp(false),
67 is_exec(false),
68 count(0),
69 connected(false) {}
70 ~RequestData() = default;
71
72 std::string device_address;
73 int id;
74 int offset;
75 bool is_long;
76 bool is_prep;
77 bool need_rsp;
78 bool is_exec;
79 uint16_t handle;
80 int count;
81 std::vector<uint8_t> write_value;
82 bool connected;
83 };
84
OnCharacteristicReadRequest(GattServer * gatt_server,const std::string & device_address,int request_id,int offset,bool is_long,uint16_t handle)85 void OnCharacteristicReadRequest(GattServer* gatt_server,
86 const std::string& device_address,
87 int request_id, int offset, bool is_long,
88 uint16_t handle) override {
89 ASSERT_TRUE(gatt_server);
90 char_read_req_.device_address = device_address;
91 char_read_req_.id = request_id;
92 char_read_req_.offset = offset;
93 char_read_req_.is_long = is_long;
94 char_read_req_.handle = handle;
95 char_read_req_.count++;
96 }
97
OnDescriptorReadRequest(GattServer * gatt_server,const std::string & device_address,int request_id,int offset,bool is_long,uint16_t handle)98 void OnDescriptorReadRequest(GattServer* gatt_server,
99 const std::string& device_address,
100 int request_id, int offset, bool is_long,
101 uint16_t handle) override {
102 ASSERT_TRUE(gatt_server);
103 desc_read_req_.device_address = device_address;
104 desc_read_req_.id = request_id;
105 desc_read_req_.offset = offset;
106 desc_read_req_.is_long = is_long;
107 desc_read_req_.handle = handle;
108 desc_read_req_.count++;
109 }
110
OnCharacteristicWriteRequest(GattServer * gatt_server,const std::string & device_address,int request_id,int offset,bool is_prepare_write,bool need_response,const std::vector<uint8_t> & value,uint16_t handle)111 void OnCharacteristicWriteRequest(GattServer* gatt_server,
112 const std::string& device_address,
113 int request_id, int offset,
114 bool is_prepare_write, bool need_response,
115 const std::vector<uint8_t>& value,
116 uint16_t handle) override {
117 ASSERT_TRUE(gatt_server);
118 char_write_req_.device_address = device_address;
119 char_write_req_.id = request_id;
120 char_write_req_.offset = offset;
121 char_write_req_.is_prep = is_prepare_write;
122 char_write_req_.need_rsp = need_response;
123 char_write_req_.handle = handle;
124 char_write_req_.count++;
125 char_write_req_.write_value = value;
126 }
127
OnDescriptorWriteRequest(GattServer * gatt_server,const std::string & device_address,int request_id,int offset,bool is_prepare_write,bool need_response,const std::vector<uint8_t> & value,uint16_t handle)128 void OnDescriptorWriteRequest(GattServer* gatt_server,
129 const std::string& device_address,
130 int request_id, int offset,
131 bool is_prepare_write, bool need_response,
132 const std::vector<uint8_t>& value,
133 uint16_t handle) override {
134 ASSERT_TRUE(gatt_server);
135 desc_write_req_.device_address = device_address;
136 desc_write_req_.id = request_id;
137 desc_write_req_.offset = offset;
138 desc_write_req_.is_prep = is_prepare_write;
139 desc_write_req_.need_rsp = need_response;
140 desc_write_req_.handle = handle;
141 desc_write_req_.count++;
142 desc_write_req_.write_value = value;
143 }
144
OnExecuteWriteRequest(GattServer * gatt_server,const std::string & device_address,int request_id,bool is_execute)145 void OnExecuteWriteRequest(GattServer* gatt_server,
146 const std::string& device_address, int request_id,
147 bool is_execute) override {
148 ASSERT_TRUE(gatt_server);
149 exec_req_.device_address = device_address;
150 exec_req_.id = request_id;
151 exec_req_.is_exec = is_execute;
152 exec_req_.count++;
153 }
154
OnConnectionStateChanged(GattServer * gatt_server,const std::string & device_address,bool connected)155 void OnConnectionStateChanged(GattServer* gatt_server,
156 const std::string& device_address,
157 bool connected) override {
158 ASSERT_TRUE(gatt_server);
159 conn_state_changed_.device_address = device_address;
160 conn_state_changed_.connected = connected;
161 conn_state_changed_.count++;
162 }
163
char_read_req() const164 const RequestData& char_read_req() const { return char_read_req_; }
desc_read_req() const165 const RequestData& desc_read_req() const { return desc_read_req_; }
char_write_req() const166 const RequestData& char_write_req() const { return char_write_req_; }
desc_write_req() const167 const RequestData& desc_write_req() const { return desc_write_req_; }
conn_state_changed() const168 const RequestData& conn_state_changed() const { return conn_state_changed_; }
169
170 private:
171 RequestData char_read_req_;
172 RequestData desc_read_req_;
173 RequestData char_write_req_;
174 RequestData desc_write_req_;
175 RequestData exec_req_;
176 RequestData conn_state_changed_;
177 };
178
179 class GattServerTest : public ::testing::Test {
180 public:
181 GattServerTest() = default;
182 GattServerTest(const GattServerTest&) = delete;
183 GattServerTest& operator=(const GattServerTest&) = delete;
184
185 ~GattServerTest() override = default;
186
SetUp()187 void SetUp() override {
188 mock_handler_.reset(new MockGattHandler());
189 fake_hal_gatt_iface_ = new hal::FakeBluetoothGattInterface(
190 nullptr, nullptr, nullptr,
191 std::static_pointer_cast<
192 hal::FakeBluetoothGattInterface::TestServerHandler>(mock_handler_));
193
194 hal::BluetoothGattInterface::InitializeForTesting(fake_hal_gatt_iface_);
195 factory_.reset(new GattServerFactory());
196 }
197
TearDown()198 void TearDown() override {
199 factory_.reset();
200 hal::BluetoothGattInterface::CleanUp();
201 }
202
203 protected:
204 hal::FakeBluetoothGattInterface* fake_hal_gatt_iface_;
205 std::shared_ptr<MockGattHandler> mock_handler_;
206 std::unique_ptr<GattServerFactory> factory_;
207 };
208
209 const int kDefaultServerId = 4;
210
211 class GattServerPostRegisterTest : public GattServerTest {
212 public:
213 GattServerPostRegisterTest() = default;
214 GattServerPostRegisterTest(const GattServerPostRegisterTest&) = delete;
215 GattServerPostRegisterTest& operator=(const GattServerPostRegisterTest&) =
216 delete;
217
218 ~GattServerPostRegisterTest() override = default;
219
SetUp()220 void SetUp() override {
221 GattServerTest::SetUp();
222 Uuid uuid = Uuid::GetRandom();
223 auto callback = [&](BLEStatus status, const Uuid& in_uuid,
224 std::unique_ptr<BluetoothInstance> in_client) {
225 CHECK(in_uuid == uuid);
226 CHECK(in_client.get());
227 CHECK(status == BLE_STATUS_SUCCESS);
228
229 gatt_server_ = std::unique_ptr<GattServer>(
230 static_cast<GattServer*>(in_client.release()));
231 };
232
233 EXPECT_CALL(*mock_handler_, RegisterServer(_, _))
234 .Times(1)
235 .WillOnce(Return(BT_STATUS_SUCCESS));
236
237 factory_->RegisterInstance(uuid, callback);
238
239 fake_hal_gatt_iface_->NotifyRegisterServerCallback(BT_STATUS_SUCCESS,
240 kDefaultServerId, uuid);
241 }
242
TearDown()243 void TearDown() override {
244 EXPECT_CALL(*mock_handler_, UnregisterServer(_))
245 .Times(1)
246 .WillOnce(Return(BT_STATUS_SUCCESS));
247 gatt_server_ = nullptr;
248 GattServerTest::TearDown();
249 }
250
SetUpTestService()251 void SetUpTestService() {
252 EXPECT_CALL(*mock_handler_, AddService(_, _))
253 .Times(1)
254 .WillOnce(Return(BT_STATUS_SUCCESS));
255
256 Uuid uuid0 = Uuid::GetRandom();
257 Uuid uuid1 = Uuid::GetRandom();
258 Uuid uuid2 = Uuid::GetRandom();
259
260 bool register_success = false;
261
262 Service service(0, true, uuid0, {}, {});
263
264 ASSERT_TRUE(gatt_server_->AddService(
265 service, [&](BLEStatus status, const Service& added_service) {
266 ASSERT_EQ(BLE_STATUS_SUCCESS, status);
267 ASSERT_TRUE(Uuid(added_service.uuid()) == Uuid(service.uuid()));
268 ASSERT_TRUE(added_service.handle() == 0x0001);
269 register_success = true;
270 }));
271
272 srvc_handle_ = 0x0001;
273 char_handle_ = 0x0002;
274 desc_handle_ = 0x0004;
275
276 std::vector<btgatt_db_element_t> service_with_handles = {
277 {.uuid = uuid0,
278 .type = BTGATT_DB_PRIMARY_SERVICE,
279 .attribute_handle = srvc_handle_},
280 {.uuid = uuid1,
281 .type = BTGATT_DB_CHARACTERISTIC,
282 .attribute_handle = char_handle_},
283 {.uuid = uuid2,
284 .type = BTGATT_DB_DESCRIPTOR,
285 .attribute_handle = desc_handle_},
286 };
287
288 fake_hal_gatt_iface_->NotifyServiceAddedCallback(
289 BT_STATUS_SUCCESS, kDefaultServerId, service_with_handles);
290
291 testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
292
293 ASSERT_TRUE(register_success);
294 }
295
296 protected:
297 std::unique_ptr<GattServer> gatt_server_;
298
299 uint16_t srvc_handle_;
300 uint16_t char_handle_;
301 uint16_t desc_handle_;
302 };
303
TEST_F(GattServerTest,RegisterServer)304 TEST_F(GattServerTest, RegisterServer) {
305 EXPECT_CALL(*mock_handler_, RegisterServer(_, _))
306 .Times(2)
307 .WillOnce(Return(BT_STATUS_FAIL))
308 .WillOnce(Return(BT_STATUS_SUCCESS));
309
310 // These will be asynchronously populate with a result when the callback
311 // executes.
312 BLEStatus status = BLE_STATUS_SUCCESS;
313 Uuid cb_uuid;
314 std::unique_ptr<GattServer> server;
315 int callback_count = 0;
316
317 auto callback = [&](BLEStatus in_status, const Uuid& uuid,
318 std::unique_ptr<BluetoothInstance> in_server) {
319 status = in_status;
320 cb_uuid = uuid;
321 server = std::unique_ptr<GattServer>(
322 static_cast<GattServer*>(in_server.release()));
323 callback_count++;
324 };
325
326 Uuid uuid0 = Uuid::GetRandom();
327
328 // HAL returns failure.
329 EXPECT_FALSE(factory_->RegisterInstance(uuid0, callback));
330 EXPECT_EQ(0, callback_count);
331
332 // HAL returns success.
333 EXPECT_TRUE(factory_->RegisterInstance(uuid0, callback));
334 EXPECT_EQ(0, callback_count);
335
336 // Calling twice with the same Uuid should fail with no additional calls into
337 // the stack.
338 EXPECT_FALSE(factory_->RegisterInstance(uuid0, callback));
339
340 testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
341
342 // Call with a different Uuid while one is pending.
343 Uuid uuid1 = Uuid::GetRandom();
344 EXPECT_CALL(*mock_handler_, RegisterServer(_, _))
345 .Times(1)
346 .WillOnce(Return(BT_STATUS_SUCCESS));
347 EXPECT_TRUE(factory_->RegisterInstance(uuid1, callback));
348
349 // Trigger callback with an unknown Uuid. This should get ignored.
350 bluetooth::Uuid hal_uuid = bluetooth::Uuid::GetRandom();
351 fake_hal_gatt_iface_->NotifyRegisterServerCallback(0, 0, hal_uuid);
352 EXPECT_EQ(0, callback_count);
353
354 // |uuid0| succeeds.
355 int server_if0 = 2; // Pick something that's not 0.
356 fake_hal_gatt_iface_->NotifyRegisterServerCallback(BT_STATUS_SUCCESS,
357 server_if0, uuid0);
358
359 EXPECT_EQ(1, callback_count);
360 ASSERT_TRUE(server.get() != nullptr); // Assert to terminate in case of error
361 EXPECT_EQ(BLE_STATUS_SUCCESS, status);
362 EXPECT_EQ(server_if0, server->GetInstanceId());
363 EXPECT_EQ(uuid0, server->GetAppIdentifier());
364 EXPECT_EQ(uuid0, cb_uuid);
365
366 // The server should unregister itself when deleted.
367 EXPECT_CALL(*mock_handler_, UnregisterServer(server_if0))
368 .Times(1)
369 .WillOnce(Return(BT_STATUS_SUCCESS));
370 server.reset();
371
372 testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
373
374 // |uuid1| fails.
375 int server_if1 = 3;
376 fake_hal_gatt_iface_->NotifyRegisterServerCallback(BT_STATUS_FAIL, server_if1,
377 uuid1);
378
379 EXPECT_EQ(2, callback_count);
380 ASSERT_TRUE(server.get() == nullptr); // Assert to terminate in case of error
381 EXPECT_EQ(BLE_STATUS_FAILURE, status);
382 EXPECT_EQ(uuid1, cb_uuid);
383 }
384
TEST_F(GattServerPostRegisterTest,RequestRead)385 TEST_F(GattServerPostRegisterTest, RequestRead) {
386 SetUpTestService();
387
388 TestDelegate test_delegate;
389 gatt_server_->SetDelegate(&test_delegate);
390
391 const std::vector<uint8_t> kTestValue = {0x01, 0x02, 0x03};
392 const std::vector<uint8_t> kTestValueTooLarge(BTGATT_MAX_ATTR_LEN + 1, 0);
393 const std::string kTestAddress0 = "01:23:45:67:89:AB";
394 const std::string kTestAddress1 = "CD:EF:01:23:45:67";
395 const int kReqId0 = 0;
396 const int kReqId1 = 1;
397 const int kConnId0 = 1;
398
399 // No pending request.
400 EXPECT_FALSE(gatt_server_->SendResponse(kTestAddress0, kReqId0,
401 GATT_ERROR_NONE, 0, kTestValue));
402
403 RawAddress hal_addr0, hal_addr1;
404 ASSERT_TRUE(RawAddress::FromString(kTestAddress0, hal_addr0));
405 ASSERT_TRUE(RawAddress::FromString(kTestAddress1, hal_addr1));
406
407 // Send a connection callback. The GattServer should store the connection
408 // information and be able to process the incoming read requests for this
409 // connection.
410 fake_hal_gatt_iface_->NotifyServerConnectionCallback(
411 kConnId0, kDefaultServerId, true, hal_addr0);
412
413 // Unknown connection ID shouldn't trigger anything.
414 fake_hal_gatt_iface_->NotifyRequestReadCharacteristicCallback(
415 kConnId0 + 1, kReqId0, hal_addr0, char_handle_, 0, false);
416 EXPECT_EQ(0, test_delegate.char_read_req().count);
417 EXPECT_EQ(0, test_delegate.desc_read_req().count);
418
419 // Unknown device address shouldn't trigger anything.
420 fake_hal_gatt_iface_->NotifyRequestReadCharacteristicCallback(
421 kConnId0, kReqId0, hal_addr1, char_handle_, 0, false);
422 EXPECT_EQ(0, test_delegate.char_read_req().count);
423 EXPECT_EQ(0, test_delegate.desc_read_req().count);
424
425 // Characteristic and descriptor handles should trigger correct callbacks.
426 fake_hal_gatt_iface_->NotifyRequestReadCharacteristicCallback(
427 kConnId0, kReqId0, hal_addr0, char_handle_, 0, false);
428 EXPECT_EQ(1, test_delegate.char_read_req().count);
429 EXPECT_EQ(kTestAddress0, test_delegate.char_read_req().device_address);
430 EXPECT_EQ(kReqId0, test_delegate.char_read_req().id);
431 EXPECT_EQ(0, test_delegate.char_read_req().offset);
432 EXPECT_FALSE(test_delegate.char_read_req().is_long);
433 EXPECT_TRUE(char_handle_ == test_delegate.char_read_req().handle);
434 EXPECT_EQ(0, test_delegate.desc_read_req().count);
435
436 fake_hal_gatt_iface_->NotifyRequestReadDescriptorCallback(
437 kConnId0, kReqId1, hal_addr0, desc_handle_, 2, true);
438 EXPECT_EQ(1, test_delegate.char_read_req().count);
439 EXPECT_EQ(1, test_delegate.desc_read_req().count);
440 EXPECT_EQ(kTestAddress0, test_delegate.desc_read_req().device_address);
441 EXPECT_EQ(kReqId1, test_delegate.desc_read_req().id);
442 EXPECT_EQ(2, test_delegate.desc_read_req().offset);
443 EXPECT_TRUE(test_delegate.desc_read_req().is_long);
444 EXPECT_TRUE(desc_handle_ == test_delegate.desc_read_req().handle);
445
446 // Callback with a pending request ID will be ignored.
447 fake_hal_gatt_iface_->NotifyRequestReadCharacteristicCallback(
448 kConnId0, kReqId0, hal_addr0, char_handle_, 0, false);
449 fake_hal_gatt_iface_->NotifyRequestReadCharacteristicCallback(
450 kConnId0, kReqId1, hal_addr0, char_handle_, 0, false);
451 EXPECT_EQ(1, test_delegate.char_read_req().count);
452 EXPECT_EQ(1, test_delegate.desc_read_req().count);
453
454 // Send response for wrong device address.
455 EXPECT_FALSE(gatt_server_->SendResponse(kTestAddress1, kReqId0,
456 GATT_ERROR_NONE, 0, kTestValue));
457
458 // Send response for a value that's too large.
459 EXPECT_FALSE(gatt_server_->SendResponse(
460 kTestAddress0, kReqId0, GATT_ERROR_NONE, 0, kTestValueTooLarge));
461
462 EXPECT_CALL(*mock_handler_,
463 SendResponse(kConnId0, kReqId0, BT_STATUS_SUCCESS, _))
464 .Times(2)
465 .WillOnce(Return(BT_STATUS_FAIL))
466 .WillOnce(Return(BT_STATUS_SUCCESS));
467
468 // Stack call fails.
469 EXPECT_FALSE(gatt_server_->SendResponse(kTestAddress0, kReqId0,
470 GATT_ERROR_NONE, 0, kTestValue));
471
472 // Successful send response for characteristic.
473 EXPECT_TRUE(gatt_server_->SendResponse(kTestAddress0, kReqId0,
474 GATT_ERROR_NONE, 0, kTestValue));
475
476 // Characteristic request ID no longer pending.
477 EXPECT_FALSE(gatt_server_->SendResponse(kTestAddress0, kReqId0,
478 GATT_ERROR_NONE, 0, kTestValue));
479
480 EXPECT_CALL(*mock_handler_,
481 SendResponse(kConnId0, kReqId1, BT_STATUS_SUCCESS, _))
482 .Times(1)
483 .WillOnce(Return(BT_STATUS_SUCCESS));
484
485 // Successful send response for descriptor.
486 EXPECT_TRUE(gatt_server_->SendResponse(kTestAddress0, kReqId1,
487 GATT_ERROR_NONE, 0, kTestValue));
488
489 // Descriptor request ID no longer pending.
490 EXPECT_FALSE(gatt_server_->SendResponse(kTestAddress0, kReqId1,
491 GATT_ERROR_NONE, 0, kTestValue));
492
493 gatt_server_->SetDelegate(nullptr);
494 }
495
TEST_F(GattServerPostRegisterTest,RequestWrite)496 TEST_F(GattServerPostRegisterTest, RequestWrite) {
497 SetUpTestService();
498
499 TestDelegate test_delegate;
500 gatt_server_->SetDelegate(&test_delegate);
501
502 const std::vector<uint8_t> kTestValue = {0x01, 0x02, 0x03};
503 const std::string kTestAddress0 = "01:23:45:67:89:AB";
504 const std::string kTestAddress1 = "CD:EF:01:23:45:67";
505 const int kReqId0 = 0;
506 const int kReqId1 = 1;
507 const int kConnId0 = 1;
508
509 // No pending request.
510 EXPECT_FALSE(gatt_server_->SendResponse(kTestAddress0, kReqId0,
511 GATT_ERROR_NONE, 0, kTestValue));
512
513 RawAddress hal_addr0, hal_addr1;
514 ASSERT_TRUE(RawAddress::FromString(kTestAddress0, hal_addr0));
515 ASSERT_TRUE(RawAddress::FromString(kTestAddress1, hal_addr1));
516
517 // Send a connection callback. The GattServer should store the connection
518 // information and be able to process the incoming read requests for this
519 // connection.
520 fake_hal_gatt_iface_->NotifyServerConnectionCallback(
521 kConnId0, kDefaultServerId, true, hal_addr0);
522
523 // Unknown connection ID shouldn't trigger anything.
524 fake_hal_gatt_iface_->NotifyRequestWriteCharacteristicCallback(
525 kConnId0 + 1, kReqId0, hal_addr0, char_handle_, 0, true, false,
526 kTestValue);
527 EXPECT_EQ(0, test_delegate.char_write_req().count);
528 EXPECT_EQ(0, test_delegate.desc_write_req().count);
529
530 // Unknown device address shouldn't trigger anything.
531 fake_hal_gatt_iface_->NotifyRequestWriteCharacteristicCallback(
532 kConnId0, kReqId0, hal_addr1, char_handle_, 0, true, false, kTestValue);
533 EXPECT_EQ(0, test_delegate.char_write_req().count);
534 EXPECT_EQ(0, test_delegate.desc_write_req().count);
535
536 // Characteristic and descriptor handles should trigger correct callbacks.
537 fake_hal_gatt_iface_->NotifyRequestWriteCharacteristicCallback(
538 kConnId0, kReqId0, hal_addr0, char_handle_, 0, true, false, kTestValue);
539 EXPECT_EQ(1, test_delegate.char_write_req().count);
540 EXPECT_EQ(kTestAddress0, test_delegate.char_write_req().device_address);
541 EXPECT_EQ(kReqId0, test_delegate.char_write_req().id);
542 EXPECT_EQ(0, test_delegate.char_write_req().offset);
543 EXPECT_EQ(true, test_delegate.char_write_req().need_rsp);
544 EXPECT_EQ(false, test_delegate.char_write_req().is_exec);
545 EXPECT_EQ(kTestValue, test_delegate.char_write_req().write_value);
546 EXPECT_TRUE(char_handle_ == test_delegate.char_write_req().handle);
547 EXPECT_EQ(0, test_delegate.desc_write_req().count);
548
549 fake_hal_gatt_iface_->NotifyRequestWriteDescriptorCallback(
550 kConnId0, kReqId1, hal_addr0, desc_handle_, 2, true, false, kTestValue);
551 EXPECT_EQ(1, test_delegate.char_write_req().count);
552 EXPECT_EQ(1, test_delegate.desc_write_req().count);
553 EXPECT_EQ(kTestAddress0, test_delegate.desc_write_req().device_address);
554 EXPECT_EQ(kReqId1, test_delegate.desc_write_req().id);
555 EXPECT_EQ(2, test_delegate.desc_write_req().offset);
556 EXPECT_EQ(true, test_delegate.desc_write_req().need_rsp);
557 EXPECT_EQ(false, test_delegate.desc_write_req().is_exec);
558 EXPECT_EQ(kTestValue, test_delegate.desc_write_req().write_value);
559 EXPECT_TRUE(desc_handle_ == test_delegate.desc_write_req().handle);
560
561 // Callback with a pending request ID will be ignored.
562 fake_hal_gatt_iface_->NotifyRequestWriteCharacteristicCallback(
563 kConnId0, kReqId0, hal_addr0, char_handle_, 0, true, false, kTestValue);
564 fake_hal_gatt_iface_->NotifyRequestWriteCharacteristicCallback(
565 kConnId0, kReqId1, hal_addr0, char_handle_, 0, true, false, kTestValue);
566 EXPECT_EQ(1, test_delegate.char_write_req().count);
567 EXPECT_EQ(1, test_delegate.desc_write_req().count);
568
569 // Send response for wrong device address.
570 EXPECT_FALSE(gatt_server_->SendResponse(kTestAddress1, kReqId0,
571 GATT_ERROR_NONE, 0, kTestValue));
572
573 EXPECT_CALL(*mock_handler_,
574 SendResponse(kConnId0, kReqId0, BT_STATUS_SUCCESS, _))
575 .Times(2)
576 .WillOnce(Return(BT_STATUS_FAIL))
577 .WillOnce(Return(BT_STATUS_SUCCESS));
578
579 // Stack call fails.
580 EXPECT_FALSE(gatt_server_->SendResponse(kTestAddress0, kReqId0,
581 GATT_ERROR_NONE, 0, kTestValue));
582
583 // Successful send response for characteristic.
584 EXPECT_TRUE(gatt_server_->SendResponse(kTestAddress0, kReqId0,
585 GATT_ERROR_NONE, 0, kTestValue));
586
587 // Characteristic request ID no longer pending.
588 EXPECT_FALSE(gatt_server_->SendResponse(kTestAddress0, kReqId0,
589 GATT_ERROR_NONE, 0, kTestValue));
590
591 EXPECT_CALL(*mock_handler_,
592 SendResponse(kConnId0, kReqId1, BT_STATUS_SUCCESS, _))
593 .Times(1)
594 .WillOnce(Return(BT_STATUS_SUCCESS));
595
596 // Successful send response for descriptor.
597 EXPECT_TRUE(gatt_server_->SendResponse(kTestAddress0, kReqId1,
598 GATT_ERROR_NONE, 0, kTestValue));
599
600 // Descriptor request ID no longer pending.
601 EXPECT_FALSE(gatt_server_->SendResponse(kTestAddress0, kReqId1,
602 GATT_ERROR_NONE, 0, kTestValue));
603
604 // SendResponse should fail for a "Write Without Response".
605 fake_hal_gatt_iface_->NotifyRequestWriteCharacteristicCallback(
606 kConnId0, kReqId0, hal_addr0, char_handle_, 0, false, false, kTestValue);
607 EXPECT_EQ(false, test_delegate.char_write_req().need_rsp);
608 EXPECT_FALSE(gatt_server_->SendResponse(kTestAddress0, kReqId0,
609 GATT_ERROR_NONE, 0, kTestValue));
610
611 gatt_server_->SetDelegate(nullptr);
612 }
613
TEST_F(GattServerPostRegisterTest,SendNotification)614 TEST_F(GattServerPostRegisterTest, SendNotification) {
615 SetUpTestService();
616
617 const std::string kTestAddress0 = "01:23:45:67:89:AB";
618 const std::string kTestAddress1 = "cd:ef:01:23:45:67";
619 const std::string kInvalidAddress = "thingamajig blabbidyboop";
620 const int kConnId0 = 0;
621 const int kConnId1 = 1;
622 std::vector<uint8_t> value;
623 RawAddress hal_addr0;
624 ASSERT_TRUE(RawAddress::FromString(kTestAddress0, hal_addr0));
625
626 // Set up two connections with the same address.
627 fake_hal_gatt_iface_->NotifyServerConnectionCallback(
628 kConnId0, kDefaultServerId, true, hal_addr0);
629 fake_hal_gatt_iface_->NotifyServerConnectionCallback(
630 kConnId1, kDefaultServerId, true, hal_addr0);
631
632 // Set up a test callback.
633 GATTError gatt_error;
634 int callback_count = 0;
635 auto callback = [&](GATTError in_error) {
636 gatt_error = in_error;
637 callback_count++;
638 };
639
640 // Bad device address.
641 EXPECT_FALSE(gatt_server_->SendNotification(kInvalidAddress, char_handle_,
642 false, value, callback));
643
644 // Bad connection.
645 EXPECT_FALSE(gatt_server_->SendNotification(kTestAddress1, char_handle_,
646 false, value, callback));
647
648 // We should get a HAL call for each connection for this address. The calls
649 // fail.
650 EXPECT_CALL(*mock_handler_, SendIndication(kDefaultServerId, char_handle_,
651 kConnId0, 0, value))
652 .Times(1)
653 .WillOnce(Return(BT_STATUS_FAIL));
654 EXPECT_CALL(*mock_handler_, SendIndication(kDefaultServerId, char_handle_,
655 kConnId1, 0, value))
656 .Times(1)
657 .WillOnce(Return(BT_STATUS_FAIL));
658 EXPECT_FALSE(gatt_server_->SendNotification(kTestAddress0, char_handle_,
659 false, value, callback));
660
661 // One of the calls succeeds.
662 EXPECT_CALL(*mock_handler_, SendIndication(kDefaultServerId, char_handle_,
663 kConnId0, 0, value))
664 .Times(1)
665 .WillOnce(Return(BT_STATUS_SUCCESS));
666 EXPECT_CALL(*mock_handler_, SendIndication(kDefaultServerId, char_handle_,
667 kConnId1, 0, value))
668 .Times(1)
669 .WillOnce(Return(BT_STATUS_FAIL));
670 EXPECT_TRUE(gatt_server_->SendNotification(kTestAddress0, char_handle_, false,
671 value, callback));
672
673 // One of the connections is already pending so there should be only one call.
674 // This one we send with confirm=true.
675 EXPECT_CALL(*mock_handler_, SendIndication(kDefaultServerId, char_handle_,
676 kConnId1, 1, value))
677 .Times(1)
678 .WillOnce(Return(BT_STATUS_SUCCESS));
679 EXPECT_TRUE(gatt_server_->SendNotification(kTestAddress0, char_handle_, true,
680 value, callback));
681
682 // Calls are already pending.
683 EXPECT_FALSE(gatt_server_->SendNotification(kTestAddress0, char_handle_, true,
684 value, callback));
685
686 // Trigger one confirmation callback. We should get calls for two callbacks
687 // since we have two separate calls pending.
688 fake_hal_gatt_iface_->NotifyIndicationSentCallback(kConnId0,
689 BT_STATUS_SUCCESS);
690 fake_hal_gatt_iface_->NotifyIndicationSentCallback(kConnId1,
691 BT_STATUS_SUCCESS);
692 EXPECT_EQ(2, callback_count);
693 EXPECT_EQ(GATT_ERROR_NONE, gatt_error);
694
695 callback_count = 0;
696
697 // Restart. Both calls succeed now.
698 EXPECT_CALL(*mock_handler_, SendIndication(kDefaultServerId, char_handle_,
699 kConnId0, 0, value))
700 .Times(1)
701 .WillOnce(Return(BT_STATUS_SUCCESS));
702 EXPECT_CALL(*mock_handler_, SendIndication(kDefaultServerId, char_handle_,
703 kConnId1, 0, value))
704 .Times(1)
705 .WillOnce(Return(BT_STATUS_SUCCESS));
706 EXPECT_TRUE(gatt_server_->SendNotification(kTestAddress0, char_handle_, false,
707 value, callback));
708
709 // Trigger one confirmation callback. The callback we passed should still be
710 // pending. The first callback is for the wrong connection ID.
711 fake_hal_gatt_iface_->NotifyIndicationSentCallback(kConnId0 + 50,
712 BT_STATUS_FAIL);
713 fake_hal_gatt_iface_->NotifyIndicationSentCallback(kConnId0,
714 BT_STATUS_SUCCESS);
715 EXPECT_EQ(0, callback_count);
716
717 // This should be ignored since |kConnId0| was already processed.
718 fake_hal_gatt_iface_->NotifyIndicationSentCallback(kConnId0,
719 BT_STATUS_SUCCESS);
720 EXPECT_EQ(0, callback_count);
721
722 // Run the callback with failure. Since the previous callback reported
723 // success, we should report success.
724 fake_hal_gatt_iface_->NotifyIndicationSentCallback(kConnId1,
725 BT_STATUS_SUCCESS);
726 EXPECT_EQ(1, callback_count);
727 EXPECT_EQ(GATT_ERROR_NONE, gatt_error);
728 }
729
730 } // namespace
731 } // namespace bluetooth
732