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