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/common/bluetooth/util/address_helper.h" 21#include "service/gatt_server.h" 22#include "service/hal/fake_bluetooth_gatt_interface.h" 23#include "service/hal/gatt_helpers.h" 24 25using ::testing::_; 26using ::testing::Return; 27 28namespace bluetooth { 29namespace { 30 31class MockGattHandler 32 : public hal::FakeBluetoothGattInterface::TestServerHandler { 33 public: 34 MockGattHandler() = default; 35 ~MockGattHandler() override = default; 36 37 MOCK_METHOD1(RegisterServer, bt_status_t(bt_uuid_t*)); 38 MOCK_METHOD1(UnregisterServer, bt_status_t(int)); 39 MOCK_METHOD3(AddService, bt_status_t(int, btgatt_srvc_id_t*, int)); 40 MOCK_METHOD5(AddCharacteristic, bt_status_t(int, int, bt_uuid_t*, int, int)); 41 MOCK_METHOD4(AddDescriptor, bt_status_t(int, int, bt_uuid_t*, int)); 42 MOCK_METHOD3(StartService, bt_status_t(int, int, int)); 43 MOCK_METHOD2(DeleteService, bt_status_t(int, int)); 44 MOCK_METHOD6(SendIndication, bt_status_t(int, int, int, int, int, char*)); 45 MOCK_METHOD4(SendResponse, bt_status_t(int, int, int, btgatt_response_t*)); 46 47 private: 48 DISALLOW_COPY_AND_ASSIGN(MockGattHandler); 49}; 50 51class TestDelegate : public GattServer::Delegate { 52 public: 53 TestDelegate() = default; 54 ~TestDelegate() override = default; 55 56 struct RequestData { 57 RequestData() : id(-1), offset(-1), is_long(false), is_prep(false), 58 need_rsp(false), is_exec(false), count(0) {} 59 ~RequestData() = default; 60 61 std::string device_address; 62 int id; 63 int offset; 64 bool is_long; 65 bool is_prep; 66 bool need_rsp; 67 bool is_exec; 68 GattIdentifier gatt_id; 69 int count; 70 std::vector<uint8_t> write_value; 71 }; 72 73 void OnCharacteristicReadRequest( 74 GattServer* gatt_server, 75 const std::string& device_address, 76 int request_id, int offset, bool is_long, 77 const bluetooth::GattIdentifier& characteristic_id) override { 78 ASSERT_TRUE(gatt_server); 79 char_read_req_.device_address = device_address; 80 char_read_req_.id = request_id; 81 char_read_req_.offset = offset; 82 char_read_req_.is_long = is_long; 83 char_read_req_.gatt_id = characteristic_id; 84 char_read_req_.count++; 85 } 86 87 void OnDescriptorReadRequest( 88 GattServer* gatt_server, 89 const std::string& device_address, 90 int request_id, int offset, bool is_long, 91 const bluetooth::GattIdentifier& descriptor_id) override { 92 ASSERT_TRUE(gatt_server); 93 desc_read_req_.device_address = device_address; 94 desc_read_req_.id = request_id; 95 desc_read_req_.offset = offset; 96 desc_read_req_.is_long = is_long; 97 desc_read_req_.gatt_id = descriptor_id; 98 desc_read_req_.count++; 99 } 100 101 void OnCharacteristicWriteRequest( 102 GattServer* gatt_server, 103 const std::string& device_address, 104 int request_id, int offset, bool is_prepare_write, bool need_response, 105 const std::vector<uint8_t>& value, 106 const bluetooth::GattIdentifier& characteristic_id) override { 107 ASSERT_TRUE(gatt_server); 108 char_write_req_.device_address = device_address; 109 char_write_req_.id = request_id; 110 char_write_req_.offset = offset; 111 char_write_req_.is_prep = is_prepare_write; 112 char_write_req_.need_rsp = need_response; 113 char_write_req_.gatt_id = characteristic_id; 114 char_write_req_.count++; 115 char_write_req_.write_value = value; 116 } 117 118 void OnDescriptorWriteRequest( 119 GattServer* gatt_server, 120 const std::string& device_address, 121 int request_id, int offset, bool is_prepare_write, bool need_response, 122 const std::vector<uint8_t>& value, 123 const bluetooth::GattIdentifier& descriptor_id) override { 124 ASSERT_TRUE(gatt_server); 125 desc_write_req_.device_address = device_address; 126 desc_write_req_.id = request_id; 127 desc_write_req_.offset = offset; 128 desc_write_req_.is_prep = is_prepare_write; 129 desc_write_req_.need_rsp = need_response; 130 desc_write_req_.gatt_id = descriptor_id; 131 desc_write_req_.count++; 132 desc_write_req_.write_value = value; 133 } 134 135 void OnExecuteWriteRequest( 136 GattServer* gatt_server, 137 const std::string& device_address, 138 int request_id, bool is_execute) override { 139 ASSERT_TRUE(gatt_server); 140 exec_req_.device_address = device_address; 141 exec_req_.id = request_id; 142 exec_req_.is_exec = is_execute; 143 exec_req_.count++; 144 } 145 146 const RequestData& char_read_req() const { return char_read_req_; } 147 const RequestData& desc_read_req() const { return desc_read_req_; } 148 const RequestData& char_write_req() const { return char_write_req_; } 149 const RequestData& desc_write_req() const { return desc_write_req_; } 150 151 private: 152 RequestData char_read_req_; 153 RequestData desc_read_req_; 154 RequestData char_write_req_; 155 RequestData desc_write_req_; 156 RequestData exec_req_; 157}; 158 159class GattServerTest : public ::testing::Test { 160 public: 161 GattServerTest() = default; 162 ~GattServerTest() override = default; 163 164 void SetUp() override { 165 mock_handler_.reset(new MockGattHandler()); 166 fake_hal_gatt_iface_ = new hal::FakeBluetoothGattInterface( 167 nullptr, 168 std::static_pointer_cast< 169 hal::FakeBluetoothGattInterface::TestServerHandler>(mock_handler_)); 170 171 hal::BluetoothGattInterface::InitializeForTesting(fake_hal_gatt_iface_); 172 factory_.reset(new GattServerFactory()); 173 } 174 175 void TearDown() override { 176 factory_.reset(); 177 hal::BluetoothGattInterface::CleanUp(); 178 } 179 180 protected: 181 hal::FakeBluetoothGattInterface* fake_hal_gatt_iface_; 182 std::shared_ptr<MockGattHandler> mock_handler_; 183 std::unique_ptr<GattServerFactory> factory_; 184 185 private: 186 DISALLOW_COPY_AND_ASSIGN(GattServerTest); 187}; 188 189const int kDefaultServerId = 4; 190 191class GattServerPostRegisterTest : public GattServerTest { 192 public: 193 GattServerPostRegisterTest() = default; 194 ~GattServerPostRegisterTest() override = default; 195 196 void SetUp() override { 197 GattServerTest::SetUp(); 198 UUID uuid = UUID::GetRandom(); 199 auto callback = [&](BLEStatus status, const UUID& in_uuid, 200 std::unique_ptr<BluetoothInstance> in_client) { 201 CHECK(in_uuid == uuid); 202 CHECK(in_client.get()); 203 CHECK(status == BLE_STATUS_SUCCESS); 204 205 gatt_server_ = std::unique_ptr<GattServer>( 206 static_cast<GattServer*>(in_client.release())); 207 }; 208 209 EXPECT_CALL(*mock_handler_, RegisterServer(_)) 210 .Times(1) 211 .WillOnce(Return(BT_STATUS_SUCCESS)); 212 213 factory_->RegisterInstance(uuid, callback); 214 215 bt_uuid_t hal_uuid = uuid.GetBlueDroid(); 216 fake_hal_gatt_iface_->NotifyRegisterServerCallback( 217 BT_STATUS_SUCCESS, 218 kDefaultServerId, 219 hal_uuid); 220 } 221 222 void TearDown() override { 223 EXPECT_CALL(*mock_handler_, UnregisterServer(_)) 224 .Times(1) 225 .WillOnce(Return(BT_STATUS_SUCCESS)); 226 gatt_server_ = nullptr; 227 GattServerTest::TearDown(); 228 } 229 230 void SetUpTestService() { 231 EXPECT_CALL(*mock_handler_, AddService(_, _, _)) 232 .Times(1) 233 .WillOnce(Return(BT_STATUS_SUCCESS)); 234 EXPECT_CALL(*mock_handler_, AddCharacteristic(_, _, _, _, _)) 235 .Times(1) 236 .WillOnce(Return(BT_STATUS_SUCCESS)); 237 EXPECT_CALL(*mock_handler_, AddDescriptor(_, _, _, _)) 238 .Times(1) 239 .WillOnce(Return(BT_STATUS_SUCCESS)); 240 EXPECT_CALL(*mock_handler_, StartService(_, _, _)) 241 .Times(1) 242 .WillOnce(Return(BT_STATUS_SUCCESS)); 243 244 UUID uuid0 = UUID::GetRandom(); 245 UUID uuid1 = UUID::GetRandom(); 246 UUID uuid2 = UUID::GetRandom(); 247 248 bool register_success = false; 249 250 // Doesn't matter what the permissions/properties are since this is all 251 // fake. 252 test_service_id_ = *gatt_server_->BeginServiceDeclaration(uuid0, true); 253 test_char_id_ = *gatt_server_->AddCharacteristic(uuid1, 0, 0); 254 test_desc_id_ = *gatt_server_->AddDescriptor(uuid2, 0); 255 ASSERT_TRUE(gatt_server_->EndServiceDeclaration([&]( 256 BLEStatus status, const GattIdentifier& gatt_id) { 257 ASSERT_EQ(BLE_STATUS_SUCCESS, status); 258 ASSERT_TRUE(gatt_id == test_service_id_); 259 register_success = true; 260 })); 261 262 btgatt_srvc_id_t hal_srvc_id; 263 hal::GetHALServiceId(test_service_id_, &hal_srvc_id); 264 bt_uuid_t hal_uuid1 = uuid1.GetBlueDroid(); 265 bt_uuid_t hal_uuid2 = uuid2.GetBlueDroid(); 266 267 srvc_handle_ = 0x0001; 268 char_handle_ = 0x0003; 269 desc_handle_ = 0x0004; 270 271 fake_hal_gatt_iface_->NotifyServiceAddedCallback( 272 BT_STATUS_SUCCESS, kDefaultServerId, hal_srvc_id, srvc_handle_); 273 fake_hal_gatt_iface_->NotifyCharacteristicAddedCallback( 274 BT_STATUS_SUCCESS, kDefaultServerId, hal_uuid1, 275 srvc_handle_, char_handle_); 276 fake_hal_gatt_iface_->NotifyDescriptorAddedCallback( 277 BT_STATUS_SUCCESS, kDefaultServerId, hal_uuid2, 278 srvc_handle_, desc_handle_); 279 fake_hal_gatt_iface_->NotifyServiceStartedCallback( 280 BT_STATUS_SUCCESS, kDefaultServerId, srvc_handle_); 281 282 testing::Mock::VerifyAndClearExpectations(mock_handler_.get()); 283 284 ASSERT_TRUE(register_success); 285 } 286 287 protected: 288 std::unique_ptr<GattServer> gatt_server_; 289 290 GattIdentifier test_service_id_; 291 GattIdentifier test_char_id_; 292 GattIdentifier test_desc_id_; 293 int srvc_handle_; 294 int char_handle_; 295 int desc_handle_; 296 297 private: 298 DISALLOW_COPY_AND_ASSIGN(GattServerPostRegisterTest); 299}; 300 301TEST_F(GattServerTest, RegisterServer) { 302 EXPECT_CALL(*mock_handler_, RegisterServer(_)) 303 .Times(2) 304 .WillOnce(Return(BT_STATUS_FAIL)) 305 .WillOnce(Return(BT_STATUS_SUCCESS)); 306 307 // These will be asynchronously populate with a result when the callback 308 // executes. 309 BLEStatus status = BLE_STATUS_SUCCESS; 310 UUID cb_uuid; 311 std::unique_ptr<GattServer> server; 312 int callback_count = 0; 313 314 auto callback = [&](BLEStatus in_status, const UUID& uuid, 315 std::unique_ptr<BluetoothInstance> in_server) { 316 status = in_status; 317 cb_uuid = uuid; 318 server = std::unique_ptr<GattServer>( 319 static_cast<GattServer*>(in_server.release())); 320 callback_count++; 321 }; 322 323 UUID uuid0 = UUID::GetRandom(); 324 325 // HAL returns failure. 326 EXPECT_FALSE(factory_->RegisterInstance(uuid0, callback)); 327 EXPECT_EQ(0, callback_count); 328 329 // HAL returns success. 330 EXPECT_TRUE(factory_->RegisterInstance(uuid0, callback)); 331 EXPECT_EQ(0, callback_count); 332 333 // Calling twice with the same UUID should fail with no additional calls into 334 // the stack. 335 EXPECT_FALSE(factory_->RegisterInstance(uuid0, callback)); 336 337 testing::Mock::VerifyAndClearExpectations(mock_handler_.get()); 338 339 // Call with a different UUID while one is pending. 340 UUID uuid1 = UUID::GetRandom(); 341 EXPECT_CALL(*mock_handler_, RegisterServer(_)) 342 .Times(1) 343 .WillOnce(Return(BT_STATUS_SUCCESS)); 344 EXPECT_TRUE(factory_->RegisterInstance(uuid1, callback)); 345 346 // Trigger callback with an unknown UUID. This should get ignored. 347 UUID uuid2 = UUID::GetRandom(); 348 bt_uuid_t hal_uuid = uuid2.GetBlueDroid(); 349 fake_hal_gatt_iface_->NotifyRegisterServerCallback(0, 0, hal_uuid); 350 EXPECT_EQ(0, callback_count); 351 352 // |uuid0| succeeds. 353 int server_if0 = 2; // Pick something that's not 0. 354 hal_uuid = uuid0.GetBlueDroid(); 355 fake_hal_gatt_iface_->NotifyRegisterServerCallback( 356 BT_STATUS_SUCCESS, server_if0, hal_uuid); 357 358 EXPECT_EQ(1, callback_count); 359 ASSERT_TRUE(server.get() != nullptr); // Assert to terminate in case of error 360 EXPECT_EQ(BLE_STATUS_SUCCESS, status); 361 EXPECT_EQ(server_if0, server->GetInstanceId()); 362 EXPECT_EQ(uuid0, server->GetAppIdentifier()); 363 EXPECT_EQ(uuid0, cb_uuid); 364 365 // The server should unregister itself when deleted. 366 EXPECT_CALL(*mock_handler_, UnregisterServer(server_if0)) 367 .Times(1) 368 .WillOnce(Return(BT_STATUS_SUCCESS)); 369 server.reset(); 370 371 testing::Mock::VerifyAndClearExpectations(mock_handler_.get()); 372 373 // |uuid1| fails. 374 int server_if1 = 3; 375 hal_uuid = uuid1.GetBlueDroid(); 376 fake_hal_gatt_iface_->NotifyRegisterServerCallback( 377 BT_STATUS_FAIL, server_if1, hal_uuid); 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 385TEST_F(GattServerPostRegisterTest, SimpleServiceTest) { 386 // Setup a service callback. 387 GattIdentifier cb_id; 388 BLEStatus cb_status = BLE_STATUS_SUCCESS; 389 int cb_count = 0; 390 auto callback = [&](BLEStatus in_status, const GattIdentifier& in_id) { 391 cb_id = in_id; 392 cb_status = in_status; 393 cb_count++; 394 }; 395 396 // Service declaration not started. 397 EXPECT_FALSE(gatt_server_->EndServiceDeclaration(callback)); 398 399 const UUID uuid = UUID::GetRandom(); 400 auto service_id = gatt_server_->BeginServiceDeclaration(uuid, true); 401 EXPECT_TRUE(service_id != nullptr); 402 EXPECT_TRUE(service_id->IsService()); 403 404 // Already started. 405 EXPECT_FALSE(gatt_server_->BeginServiceDeclaration(uuid, false)); 406 407 // Callback is NULL. 408 EXPECT_FALSE( 409 gatt_server_->EndServiceDeclaration(GattServer::ResultCallback())); 410 411 // We should get a call for a service with one handle. 412 EXPECT_CALL(*mock_handler_, AddService(gatt_server_->GetInstanceId(), _, 1)) 413 .Times(2) 414 .WillOnce(Return(BT_STATUS_FAIL)) 415 .WillOnce(Return(BT_STATUS_SUCCESS)); 416 417 // Stack returns failure. This will cause the entire service declaration to 418 // end and needs to be restarted. 419 EXPECT_FALSE(gatt_server_->EndServiceDeclaration(callback)); 420 421 service_id = gatt_server_->BeginServiceDeclaration(uuid, true); 422 EXPECT_TRUE(service_id != nullptr); 423 EXPECT_TRUE(service_id->IsService()); 424 425 // Stack returns success. 426 EXPECT_TRUE(gatt_server_->EndServiceDeclaration(callback)); 427 428 // EndServiceDeclaration already in progress. 429 EXPECT_FALSE(gatt_server_->EndServiceDeclaration(callback)); 430 431 EXPECT_EQ(0, cb_count); 432 433 btgatt_srvc_id_t hal_id; 434 hal::GetHALServiceId(*service_id, &hal_id); 435 int srvc_handle = 0x0001; 436 437 // Report success for AddService but for wrong server. Should be ignored. 438 fake_hal_gatt_iface_->NotifyServiceAddedCallback( 439 BT_STATUS_SUCCESS, kDefaultServerId + 1, hal_id, srvc_handle); 440 EXPECT_EQ(0, cb_count); 441 442 // Report success for AddService. 443 EXPECT_CALL(*mock_handler_, StartService(kDefaultServerId, srvc_handle, _)) 444 .Times(1) 445 .WillOnce(Return(BT_STATUS_SUCCESS)); 446 447 fake_hal_gatt_iface_->NotifyServiceAddedCallback( 448 BT_STATUS_SUCCESS, kDefaultServerId, hal_id, srvc_handle); 449 EXPECT_EQ(0, cb_count); 450 451 // Report success for StartService but for wrong server. Should be ignored. 452 fake_hal_gatt_iface_->NotifyServiceStartedCallback( 453 BT_STATUS_SUCCESS, kDefaultServerId + 1, srvc_handle); 454 EXPECT_EQ(0, cb_count); 455 456 // Report success for StartService. 457 fake_hal_gatt_iface_->NotifyServiceStartedCallback( 458 BT_STATUS_SUCCESS, kDefaultServerId, srvc_handle); 459 EXPECT_EQ(1, cb_count); 460 EXPECT_EQ(BLE_STATUS_SUCCESS, cb_status); 461 EXPECT_TRUE(cb_id == *service_id); 462 463 // Start new service declaration with same UUID. We should get a different ID. 464 auto service_id1 = gatt_server_->BeginServiceDeclaration(uuid, true); 465 EXPECT_TRUE(service_id1 != nullptr); 466 EXPECT_TRUE(service_id1->IsService()); 467 EXPECT_TRUE(*service_id != *service_id1); 468} 469 470TEST_F(GattServerPostRegisterTest, AddServiceFailures) { 471 // Setup a service callback. 472 GattIdentifier cb_id; 473 BLEStatus cb_status = BLE_STATUS_SUCCESS; 474 int cb_count = 0; 475 auto callback = [&](BLEStatus in_status, const GattIdentifier& in_id) { 476 cb_id = in_id; 477 cb_status = in_status; 478 cb_count++; 479 }; 480 481 const UUID uuid = UUID::GetRandom(); 482 auto service_id = gatt_server_->BeginServiceDeclaration(uuid, true); 483 btgatt_srvc_id_t hal_id; 484 hal::GetHALServiceId(*service_id, &hal_id); 485 int srvc_handle = 0x0001; 486 487 EXPECT_CALL(*mock_handler_, AddService(gatt_server_->GetInstanceId(), _, 1)) 488 .Times(3) 489 .WillRepeatedly(Return(BT_STATUS_SUCCESS)); 490 EXPECT_TRUE(gatt_server_->EndServiceDeclaration(callback)); 491 492 // Report failure for AddService. 493 fake_hal_gatt_iface_->NotifyServiceAddedCallback( 494 BT_STATUS_FAIL, kDefaultServerId, hal_id, srvc_handle); 495 EXPECT_EQ(1, cb_count); 496 EXPECT_NE(BLE_STATUS_SUCCESS, cb_status); 497 EXPECT_TRUE(cb_id == *service_id); 498 499 // Restart. We should get the same ID back. 500 auto service_id1 = gatt_server_->BeginServiceDeclaration(uuid, true); 501 EXPECT_TRUE(*service_id1 == *service_id); 502 EXPECT_TRUE(gatt_server_->EndServiceDeclaration(callback)); 503 504 // Report success for AddService but return failure from StartService. 505 EXPECT_CALL(*mock_handler_, StartService(gatt_server_->GetInstanceId(), 1, _)) 506 .Times(2) 507 .WillOnce(Return(BT_STATUS_FAIL)) 508 .WillOnce(Return(BT_STATUS_SUCCESS)); 509 510 fake_hal_gatt_iface_->NotifyServiceAddedCallback( 511 BT_STATUS_SUCCESS, kDefaultServerId, hal_id, srvc_handle); 512 EXPECT_EQ(2, cb_count); 513 EXPECT_NE(BLE_STATUS_SUCCESS, cb_status); 514 EXPECT_TRUE(cb_id == *service_id); 515 516 // Restart. 517 service_id = gatt_server_->BeginServiceDeclaration(uuid, true); 518 EXPECT_TRUE(gatt_server_->EndServiceDeclaration(callback)); 519 520 // Report success for AddService, return success from StartService. 521 fake_hal_gatt_iface_->NotifyServiceAddedCallback( 522 BT_STATUS_SUCCESS, kDefaultServerId, hal_id, srvc_handle); 523 EXPECT_EQ(2, cb_count); 524 525 // Report failure for StartService. Added service data should get deleted. 526 EXPECT_CALL(*mock_handler_, 527 DeleteService(gatt_server_->GetInstanceId(), srvc_handle)) 528 .Times(1) 529 .WillOnce(Return(BT_STATUS_SUCCESS)); 530 fake_hal_gatt_iface_->NotifyServiceStartedCallback( 531 BT_STATUS_FAIL, kDefaultServerId, srvc_handle); 532 EXPECT_EQ(3, cb_count); 533 EXPECT_NE(BLE_STATUS_SUCCESS, cb_status); 534 EXPECT_TRUE(cb_id == *service_id); 535} 536 537TEST_F(GattServerPostRegisterTest, AddCharacteristic) { 538 // Just pick some values. 539 const int props = bluetooth::kCharacteristicPropertyRead | 540 bluetooth::kCharacteristicPropertyNotify; 541 const int perms = kAttributePermissionReadEncrypted; 542 const UUID char_uuid = UUID::GetRandom(); 543 bt_uuid_t hal_char_uuid = char_uuid.GetBlueDroid(); 544 545 // Declaration not started. 546 EXPECT_EQ(nullptr, gatt_server_->AddCharacteristic(char_uuid, props, perms)); 547 548 // Start a service declaration. 549 const UUID service_uuid = UUID::GetRandom(); 550 auto service_id = gatt_server_->BeginServiceDeclaration(service_uuid, true); 551 EXPECT_TRUE(service_id != nullptr); 552 btgatt_srvc_id_t hal_id; 553 hal::GetHALServiceId(*service_id, &hal_id); 554 555 // Add two characteristics with the same UUID. 556 auto char_id0 = gatt_server_->AddCharacteristic(char_uuid, props, perms); 557 auto char_id1 = gatt_server_->AddCharacteristic(char_uuid, props, perms); 558 559 EXPECT_TRUE(char_id0 != nullptr); 560 EXPECT_TRUE(char_id1 != nullptr); 561 EXPECT_TRUE(char_id0 != char_id1); 562 EXPECT_TRUE(char_id0->IsCharacteristic()); 563 EXPECT_TRUE(char_id1->IsCharacteristic()); 564 EXPECT_TRUE(*char_id0->GetOwningServiceId() == *service_id); 565 EXPECT_TRUE(*char_id1->GetOwningServiceId() == *service_id); 566 567 // Expect calls for 5 handles in total as we have 2 characteristics. 568 EXPECT_CALL(*mock_handler_, AddService(kDefaultServerId, _, 5)) 569 .WillRepeatedly(Return(BT_STATUS_SUCCESS)); 570 571 GattIdentifier cb_id; 572 BLEStatus cb_status; 573 int cb_count = 0; 574 auto callback = [&](BLEStatus in_status, const GattIdentifier& in_id) { 575 cb_id = in_id; 576 cb_status = in_status; 577 cb_count++; 578 }; 579 580 int srvc_handle = 0x0001; 581 int char_handle0 = 0x0002; 582 int char_handle1 = 0x0004; 583 EXPECT_TRUE(gatt_server_->EndServiceDeclaration(callback)); 584 585 // Cannot add any more characteristics while EndServiceDeclaration is in 586 // progress. 587 EXPECT_EQ(nullptr, gatt_server_->AddCharacteristic(char_uuid, props, perms)); 588 589 EXPECT_CALL(*mock_handler_, AddCharacteristic(_, _, _, _, _)) 590 .Times(8) 591 .WillOnce(Return(BT_STATUS_FAIL)) // char_id0 - try 1 592 .WillOnce(Return(BT_STATUS_SUCCESS)) // char_id0 - try 2 593 .WillOnce(Return(BT_STATUS_SUCCESS)) // char_id0 - try 3 594 .WillOnce(Return(BT_STATUS_FAIL)) // char_id1 - try 3 595 .WillOnce(Return(BT_STATUS_SUCCESS)) // char_id0 - try 4 596 .WillOnce(Return(BT_STATUS_SUCCESS)) // char_id1 - try 4 597 .WillOnce(Return(BT_STATUS_SUCCESS)) // char_id0 - try 5 598 .WillOnce(Return(BT_STATUS_SUCCESS)); // char_id1 - try 5 599 600 // First AddCharacteristic call will fail. 601 fake_hal_gatt_iface_->NotifyServiceAddedCallback( 602 BT_STATUS_SUCCESS, kDefaultServerId, hal_id, srvc_handle); 603 EXPECT_EQ(1, cb_count); 604 EXPECT_NE(BLE_STATUS_SUCCESS, cb_status); 605 EXPECT_TRUE(cb_id == *service_id); 606 607 // Restart. (try 2) 608 service_id = gatt_server_->BeginServiceDeclaration(service_uuid, true); 609 char_id0 = gatt_server_->AddCharacteristic(char_uuid, props, perms); 610 char_id1 = gatt_server_->AddCharacteristic(char_uuid, props, perms); 611 hal::GetHALServiceId(*service_id, &hal_id); 612 EXPECT_TRUE(gatt_server_->EndServiceDeclaration(callback)); 613 614 fake_hal_gatt_iface_->NotifyServiceAddedCallback( 615 BT_STATUS_SUCCESS, kDefaultServerId, hal_id, srvc_handle); 616 EXPECT_EQ(1, cb_count); 617 618 // Report failure for pending AddCharacteristic. 619 fake_hal_gatt_iface_->NotifyCharacteristicAddedCallback( 620 BT_STATUS_FAIL, kDefaultServerId, hal_char_uuid, 621 srvc_handle, char_handle0); 622 EXPECT_EQ(2, cb_count); 623 EXPECT_NE(BLE_STATUS_SUCCESS, cb_status); 624 EXPECT_TRUE(cb_id == *service_id); 625 626 // Restart. (try 3) 627 service_id = gatt_server_->BeginServiceDeclaration(service_uuid, true); 628 char_id0 = gatt_server_->AddCharacteristic(char_uuid, props, perms); 629 char_id1 = gatt_server_->AddCharacteristic(char_uuid, props, perms); 630 hal::GetHALServiceId(*service_id, &hal_id); 631 EXPECT_TRUE(gatt_server_->EndServiceDeclaration(callback)); 632 633 fake_hal_gatt_iface_->NotifyServiceAddedCallback( 634 BT_STATUS_SUCCESS, kDefaultServerId, hal_id, srvc_handle); 635 EXPECT_EQ(2, cb_count); 636 637 // Report success for pending AddCharacteristic we should receive a call for 638 // the second characteristic which will fail. 639 fake_hal_gatt_iface_->NotifyCharacteristicAddedCallback( 640 BT_STATUS_SUCCESS, kDefaultServerId, hal_char_uuid, 641 srvc_handle, char_handle0); 642 EXPECT_EQ(3, cb_count); 643 EXPECT_NE(BLE_STATUS_SUCCESS, cb_status); 644 EXPECT_TRUE(cb_id == *service_id); 645 646 // Restart. (try 4) 647 service_id = gatt_server_->BeginServiceDeclaration(service_uuid, true); 648 char_id0 = gatt_server_->AddCharacteristic(char_uuid, props, perms); 649 char_id1 = gatt_server_->AddCharacteristic(char_uuid, props, perms); 650 hal::GetHALServiceId(*service_id, &hal_id); 651 EXPECT_TRUE(gatt_server_->EndServiceDeclaration(callback)); 652 653 fake_hal_gatt_iface_->NotifyServiceAddedCallback( 654 BT_STATUS_SUCCESS, kDefaultServerId, hal_id, srvc_handle); 655 EXPECT_EQ(3, cb_count); 656 657 // Report success for pending AddCharacteristic. Second characteristic call 658 // will start normally. We shouldn't receive any new callback. 659 fake_hal_gatt_iface_->NotifyCharacteristicAddedCallback( 660 BT_STATUS_SUCCESS, kDefaultServerId, hal_char_uuid, 661 srvc_handle, char_handle0); 662 EXPECT_EQ(3, cb_count); 663 664 // Report failure for pending AddCharacteristic call for second 665 // characteristic. 666 fake_hal_gatt_iface_->NotifyCharacteristicAddedCallback( 667 BT_STATUS_FAIL, kDefaultServerId, hal_char_uuid, 668 srvc_handle, char_handle1); 669 EXPECT_EQ(4, cb_count); 670 EXPECT_NE(BLE_STATUS_SUCCESS, cb_status); 671 EXPECT_TRUE(cb_id == *service_id); 672 673 // Restart. (try 5) 674 service_id = gatt_server_->BeginServiceDeclaration(service_uuid, true); 675 char_id0 = gatt_server_->AddCharacteristic(char_uuid, props, perms); 676 char_id1 = gatt_server_->AddCharacteristic(char_uuid, props, perms); 677 hal::GetHALServiceId(*service_id, &hal_id); 678 EXPECT_TRUE(gatt_server_->EndServiceDeclaration(callback)); 679 680 fake_hal_gatt_iface_->NotifyServiceAddedCallback( 681 BT_STATUS_SUCCESS, kDefaultServerId, hal_id, srvc_handle); 682 EXPECT_EQ(4, cb_count); 683 684 // Report success for pending AddCharacteristic. Second characteristic call 685 // will start normally. We shouldn't receive any new callback. 686 fake_hal_gatt_iface_->NotifyCharacteristicAddedCallback( 687 BT_STATUS_SUCCESS, kDefaultServerId, hal_char_uuid, 688 srvc_handle, char_handle0); 689 EXPECT_EQ(4, cb_count); 690 691 // Report success for pending AddCharacteristic call for second 692 // characteristic. We shouldn't receive any new callback but we'll get a call 693 // to StartService. 694 EXPECT_CALL(*mock_handler_, StartService(kDefaultServerId, srvc_handle, _)) 695 .Times(1) 696 .WillOnce(Return(BT_STATUS_SUCCESS)); 697 fake_hal_gatt_iface_->NotifyCharacteristicAddedCallback( 698 BT_STATUS_SUCCESS, kDefaultServerId, hal_char_uuid, 699 srvc_handle, char_handle1); 700 EXPECT_EQ(4, cb_count); 701} 702 703TEST_F(GattServerPostRegisterTest, AddDescriptor) { 704 // Set up some values for UUIDs, permissions, and properties. 705 const UUID service_uuid = UUID::GetRandom(); 706 const UUID char_uuid0 = UUID::GetRandom(); 707 const UUID char_uuid1 = UUID::GetRandom(); 708 const UUID desc_uuid = UUID::GetRandom(); 709 bt_uuid_t hal_char_uuid0 = char_uuid0.GetBlueDroid(); 710 bt_uuid_t hal_char_uuid1 = char_uuid1.GetBlueDroid(); 711 bt_uuid_t hal_desc_uuid = desc_uuid.GetBlueDroid(); 712 const int props = bluetooth::kCharacteristicPropertyRead | 713 bluetooth::kCharacteristicPropertyNotify; 714 const int perms = kAttributePermissionReadEncrypted; 715 716 // Service declaration not started. 717 EXPECT_EQ(nullptr, gatt_server_->AddDescriptor(desc_uuid, perms)); 718 719 // Start a service declaration. 720 auto service_id = gatt_server_->BeginServiceDeclaration(service_uuid, true); 721 btgatt_srvc_id_t hal_id; 722 hal::GetHALServiceId(*service_id, &hal_id); 723 724 // No characteristic was inserted. 725 EXPECT_EQ(nullptr, gatt_server_->AddDescriptor(desc_uuid, perms)); 726 727 // Add two characeristics. 728 auto char_id0 = gatt_server_->AddCharacteristic(char_uuid0, props, perms); 729 auto char_id1 = gatt_server_->AddCharacteristic(char_uuid1, props, perms); 730 731 // Add a descriptor. 732 auto desc_id = gatt_server_->AddDescriptor(desc_uuid, perms); 733 EXPECT_NE(nullptr, desc_id); 734 EXPECT_TRUE(desc_id->IsDescriptor()); 735 EXPECT_TRUE(*desc_id->GetOwningCharacteristicId() == *char_id1); 736 EXPECT_TRUE(*desc_id->GetOwningServiceId() == *service_id); 737 738 // Add a second descriptor with the same UUID. 739 auto desc_id1 = gatt_server_->AddDescriptor(desc_uuid, perms); 740 EXPECT_NE(nullptr, desc_id1); 741 EXPECT_TRUE(*desc_id1 != *desc_id); 742 EXPECT_TRUE(desc_id1->IsDescriptor()); 743 EXPECT_TRUE(*desc_id1->GetOwningCharacteristicId() == *char_id1); 744 EXPECT_TRUE(*desc_id1->GetOwningServiceId() == *service_id); 745 746 // Expect calls for 7 handles. 747 EXPECT_CALL(*mock_handler_, AddService(kDefaultServerId, _, 7)) 748 .WillRepeatedly(Return(BT_STATUS_SUCCESS)); 749 EXPECT_CALL(*mock_handler_, AddCharacteristic(_, _, _, _, _)) 750 .WillRepeatedly(Return(BT_STATUS_SUCCESS)); 751 752 GattIdentifier cb_id; 753 BLEStatus cb_status; 754 int cb_count = 0; 755 auto callback = [&](BLEStatus in_status, const GattIdentifier& in_id) { 756 cb_id = in_id; 757 cb_status = in_status; 758 cb_count++; 759 }; 760 761 int srvc_handle = 0x0001; 762 int char_handle0 = 0x0002; 763 int char_handle1 = 0x0004; 764 int desc_handle0 = 0x0005; 765 int desc_handle1 = 0x0006; 766 767 EXPECT_TRUE(gatt_server_->EndServiceDeclaration(callback)); 768 769 // Cannot add any more descriptors while EndServiceDeclaration is in progress. 770 EXPECT_EQ(nullptr, gatt_server_->AddDescriptor(desc_uuid, perms)); 771 772 fake_hal_gatt_iface_->NotifyServiceAddedCallback( 773 BT_STATUS_SUCCESS, kDefaultServerId, hal_id, srvc_handle); 774 EXPECT_EQ(0, cb_count); 775 776 EXPECT_CALL(*mock_handler_, AddDescriptor(_, _, _, _)) 777 .Times(8) 778 .WillOnce(Return(BT_STATUS_FAIL)) // desc_id0 - try 1 779 .WillOnce(Return(BT_STATUS_SUCCESS)) // desc_id0 - try 2 780 .WillOnce(Return(BT_STATUS_SUCCESS)) // desc_id0 - try 3 781 .WillOnce(Return(BT_STATUS_FAIL)) // desc_id1 - try 3 782 .WillOnce(Return(BT_STATUS_SUCCESS)) // desc_id0 - try 4 783 .WillOnce(Return(BT_STATUS_SUCCESS)) // desc_id1 - try 4 784 .WillOnce(Return(BT_STATUS_SUCCESS)) // desc_id0 - try 5 785 .WillOnce(Return(BT_STATUS_SUCCESS)); // desc_id1 - try 5 786 787 // Notify success for both characteristics. First descriptor call will fail. 788 fake_hal_gatt_iface_->NotifyCharacteristicAddedCallback( 789 BT_STATUS_SUCCESS, kDefaultServerId, hal_char_uuid0, 790 srvc_handle, char_handle0); 791 EXPECT_EQ(0, cb_count); 792 fake_hal_gatt_iface_->NotifyCharacteristicAddedCallback( 793 BT_STATUS_SUCCESS, kDefaultServerId, hal_char_uuid1, 794 srvc_handle, char_handle1); 795 EXPECT_EQ(1, cb_count); 796 EXPECT_NE(BLE_STATUS_SUCCESS, cb_status); 797 EXPECT_TRUE(cb_id == *service_id); 798 799 // Restart (try 2) 800 cb_count = 0; 801 service_id = gatt_server_->BeginServiceDeclaration(service_uuid, true); 802 hal::GetHALServiceId(*service_id, &hal_id); 803 char_id0 = gatt_server_->AddCharacteristic(char_uuid0, props, perms); 804 char_id1 = gatt_server_->AddCharacteristic(char_uuid1, props, perms); 805 desc_id = gatt_server_->AddDescriptor(desc_uuid, perms); 806 ASSERT_NE(nullptr, desc_id); 807 desc_id1 = gatt_server_->AddDescriptor(desc_uuid, perms); 808 ASSERT_NE(nullptr, desc_id1); 809 EXPECT_TRUE(gatt_server_->EndServiceDeclaration(callback)); 810 811 fake_hal_gatt_iface_->NotifyServiceAddedCallback( 812 BT_STATUS_SUCCESS, kDefaultServerId, hal_id, srvc_handle); 813 EXPECT_EQ(0, cb_count); 814 fake_hal_gatt_iface_->NotifyCharacteristicAddedCallback( 815 BT_STATUS_SUCCESS, kDefaultServerId, hal_char_uuid0, 816 srvc_handle, char_handle0); 817 EXPECT_EQ(0, cb_count); 818 fake_hal_gatt_iface_->NotifyCharacteristicAddedCallback( 819 BT_STATUS_SUCCESS, kDefaultServerId, hal_char_uuid1, 820 srvc_handle, char_handle1); 821 EXPECT_EQ(0, cb_count); 822 823 // Notify failure for first descriptor. 824 fake_hal_gatt_iface_->NotifyDescriptorAddedCallback( 825 BT_STATUS_FAIL, kDefaultServerId, hal_desc_uuid, 826 srvc_handle, desc_handle0); 827 EXPECT_EQ(1, cb_count); 828 EXPECT_NE(BLE_STATUS_SUCCESS, cb_status); 829 EXPECT_TRUE(cb_id == *service_id); 830 831 // Restart (try 3) 832 cb_count = 0; 833 service_id = gatt_server_->BeginServiceDeclaration(service_uuid, true); 834 hal::GetHALServiceId(*service_id, &hal_id); 835 char_id0 = gatt_server_->AddCharacteristic(char_uuid0, props, perms); 836 char_id1 = gatt_server_->AddCharacteristic(char_uuid1, props, perms); 837 desc_id = gatt_server_->AddDescriptor(desc_uuid, perms); 838 ASSERT_NE(nullptr, desc_id); 839 desc_id1 = gatt_server_->AddDescriptor(desc_uuid, perms); 840 ASSERT_NE(nullptr, desc_id1); 841 EXPECT_TRUE(gatt_server_->EndServiceDeclaration(callback)); 842 843 fake_hal_gatt_iface_->NotifyServiceAddedCallback( 844 BT_STATUS_SUCCESS, kDefaultServerId, hal_id, srvc_handle); 845 EXPECT_EQ(0, cb_count); 846 fake_hal_gatt_iface_->NotifyCharacteristicAddedCallback( 847 BT_STATUS_SUCCESS, kDefaultServerId, hal_char_uuid0, 848 srvc_handle, char_handle0); 849 EXPECT_EQ(0, cb_count); 850 fake_hal_gatt_iface_->NotifyCharacteristicAddedCallback( 851 BT_STATUS_SUCCESS, kDefaultServerId, hal_char_uuid1, 852 srvc_handle, char_handle1); 853 EXPECT_EQ(0, cb_count); 854 855 // Notify success for first descriptor; the second descriptor will fail 856 // immediately. 857 fake_hal_gatt_iface_->NotifyDescriptorAddedCallback( 858 BT_STATUS_SUCCESS, kDefaultServerId, hal_desc_uuid, 859 srvc_handle, desc_handle0); 860 EXPECT_EQ(1, cb_count); 861 EXPECT_NE(BLE_STATUS_SUCCESS, cb_status); 862 EXPECT_TRUE(cb_id == *service_id); 863 864 // Restart (try 4) 865 cb_count = 0; 866 service_id = gatt_server_->BeginServiceDeclaration(service_uuid, true); 867 hal::GetHALServiceId(*service_id, &hal_id); 868 char_id0 = gatt_server_->AddCharacteristic(char_uuid0, props, perms); 869 char_id1 = gatt_server_->AddCharacteristic(char_uuid1, props, perms); 870 desc_id = gatt_server_->AddDescriptor(desc_uuid, perms); 871 ASSERT_NE(nullptr, desc_id); 872 desc_id1 = gatt_server_->AddDescriptor(desc_uuid, perms); 873 ASSERT_NE(nullptr, desc_id1); 874 EXPECT_TRUE(gatt_server_->EndServiceDeclaration(callback)); 875 876 fake_hal_gatt_iface_->NotifyServiceAddedCallback( 877 BT_STATUS_SUCCESS, kDefaultServerId, hal_id, srvc_handle); 878 EXPECT_EQ(0, cb_count); 879 fake_hal_gatt_iface_->NotifyCharacteristicAddedCallback( 880 BT_STATUS_SUCCESS, kDefaultServerId, hal_char_uuid0, 881 srvc_handle, char_handle0); 882 EXPECT_EQ(0, cb_count); 883 fake_hal_gatt_iface_->NotifyCharacteristicAddedCallback( 884 BT_STATUS_SUCCESS, kDefaultServerId, hal_char_uuid1, 885 srvc_handle, char_handle1); 886 EXPECT_EQ(0, cb_count); 887 888 // Notify success for first first descriptor and failure for second 889 // descriptor. 890 fake_hal_gatt_iface_->NotifyDescriptorAddedCallback( 891 BT_STATUS_SUCCESS, kDefaultServerId, hal_desc_uuid, 892 srvc_handle, desc_handle0); 893 EXPECT_EQ(0, cb_count); 894 895 fake_hal_gatt_iface_->NotifyDescriptorAddedCallback( 896 BT_STATUS_FAIL, kDefaultServerId, hal_desc_uuid, 897 srvc_handle, desc_handle1); 898 EXPECT_EQ(1, cb_count); 899 EXPECT_NE(BLE_STATUS_SUCCESS, cb_status); 900 EXPECT_TRUE(cb_id == *service_id); 901 902 // Restart (try 5) 903 cb_count = 0; 904 service_id = gatt_server_->BeginServiceDeclaration(service_uuid, true); 905 hal::GetHALServiceId(*service_id, &hal_id); 906 char_id0 = gatt_server_->AddCharacteristic(char_uuid0, props, perms); 907 char_id1 = gatt_server_->AddCharacteristic(char_uuid1, props, perms); 908 desc_id = gatt_server_->AddDescriptor(desc_uuid, perms); 909 ASSERT_NE(nullptr, desc_id); 910 desc_id1 = gatt_server_->AddDescriptor(desc_uuid, perms); 911 ASSERT_NE(nullptr, desc_id1); 912 EXPECT_TRUE(gatt_server_->EndServiceDeclaration(callback)); 913 914 fake_hal_gatt_iface_->NotifyServiceAddedCallback( 915 BT_STATUS_SUCCESS, kDefaultServerId, hal_id, srvc_handle); 916 EXPECT_EQ(0, cb_count); 917 fake_hal_gatt_iface_->NotifyCharacteristicAddedCallback( 918 BT_STATUS_SUCCESS, kDefaultServerId, hal_char_uuid0, 919 srvc_handle, char_handle0); 920 EXPECT_EQ(0, cb_count); 921 fake_hal_gatt_iface_->NotifyCharacteristicAddedCallback( 922 BT_STATUS_SUCCESS, kDefaultServerId, hal_char_uuid1, 923 srvc_handle, char_handle1); 924 EXPECT_EQ(0, cb_count); 925 926 // Notify success for both descriptors. 927 fake_hal_gatt_iface_->NotifyDescriptorAddedCallback( 928 BT_STATUS_SUCCESS, kDefaultServerId, hal_desc_uuid, 929 srvc_handle, desc_handle0); 930 EXPECT_EQ(0, cb_count); 931 932 // The second descriptor callback should trigger the end routine. 933 EXPECT_CALL(*mock_handler_, StartService(kDefaultServerId, srvc_handle, _)) 934 .Times(1) 935 .WillOnce(Return(BT_STATUS_SUCCESS)); 936 fake_hal_gatt_iface_->NotifyDescriptorAddedCallback( 937 BT_STATUS_SUCCESS, kDefaultServerId, hal_desc_uuid, 938 srvc_handle, desc_handle1); 939 EXPECT_EQ(0, cb_count); 940} 941 942TEST_F(GattServerPostRegisterTest, RequestRead) { 943 SetUpTestService(); 944 945 TestDelegate test_delegate; 946 gatt_server_->SetDelegate(&test_delegate); 947 948 const std::vector<uint8_t> kTestValue = { 0x01, 0x02, 0x03 }; 949 const std::vector<uint8_t> kTestValueTooLarge(BTGATT_MAX_ATTR_LEN + 1, 0); 950 const std::string kTestAddress0 = "01:23:45:67:89:AB"; 951 const std::string kTestAddress1 = "CD:EF:01:23:45:67"; 952 const int kReqId0 = 0; 953 const int kReqId1 = 1; 954 const int kConnId0 = 1; 955 956 // No pending request. 957 EXPECT_FALSE(gatt_server_->SendResponse( 958 kTestAddress0, kReqId0, 959 GATT_ERROR_NONE, 0, kTestValue)); 960 961 bt_bdaddr_t hal_addr0, hal_addr1; 962 ASSERT_TRUE(util::BdAddrFromString(kTestAddress0, &hal_addr0)); 963 ASSERT_TRUE(util::BdAddrFromString(kTestAddress1, &hal_addr1)); 964 965 // Send a connection callback. The GattServer should store the connection 966 // information and be able to process the incoming read requests for this 967 // connection. 968 fake_hal_gatt_iface_->NotifyServerConnectionCallback( 969 kConnId0, kDefaultServerId, true, hal_addr0); 970 971 // Unknown connection ID shouldn't trigger anything. 972 fake_hal_gatt_iface_->NotifyRequestReadCallback( 973 kConnId0 + 1, kReqId0, hal_addr0, char_handle_, 0, false); 974 EXPECT_EQ(0, test_delegate.char_read_req().count); 975 EXPECT_EQ(0, test_delegate.desc_read_req().count); 976 977 // Unknown device address shouldn't trigger anything. 978 fake_hal_gatt_iface_->NotifyRequestReadCallback( 979 kConnId0, kReqId0, hal_addr1, char_handle_, 0, false); 980 EXPECT_EQ(0, test_delegate.char_read_req().count); 981 EXPECT_EQ(0, test_delegate.desc_read_req().count); 982 983 // Unknown attribute handle shouldn't trigger anything. 984 fake_hal_gatt_iface_->NotifyRequestReadCallback( 985 kConnId0, kReqId0, hal_addr0, char_handle_ + 50, 0, false); 986 EXPECT_EQ(0, test_delegate.char_read_req().count); 987 EXPECT_EQ(0, test_delegate.desc_read_req().count); 988 989 // Characteristic and descriptor handles should trigger correct callbacks. 990 fake_hal_gatt_iface_->NotifyRequestReadCallback( 991 kConnId0, kReqId0, hal_addr0, char_handle_, 0, false); 992 EXPECT_EQ(1, test_delegate.char_read_req().count); 993 EXPECT_EQ(kTestAddress0, test_delegate.char_read_req().device_address); 994 EXPECT_EQ(kReqId0, test_delegate.char_read_req().id); 995 EXPECT_EQ(0, test_delegate.char_read_req().offset); 996 EXPECT_FALSE(test_delegate.char_read_req().is_long); 997 EXPECT_TRUE(test_char_id_ == test_delegate.char_read_req().gatt_id); 998 EXPECT_EQ(0, test_delegate.desc_read_req().count); 999 1000 fake_hal_gatt_iface_->NotifyRequestReadCallback( 1001 kConnId0, kReqId1, hal_addr0, desc_handle_, 2, true); 1002 EXPECT_EQ(1, test_delegate.char_read_req().count); 1003 EXPECT_EQ(1, test_delegate.desc_read_req().count); 1004 EXPECT_EQ(kTestAddress0, test_delegate.desc_read_req().device_address); 1005 EXPECT_EQ(kReqId1, test_delegate.desc_read_req().id); 1006 EXPECT_EQ(2, test_delegate.desc_read_req().offset); 1007 EXPECT_TRUE(test_delegate.desc_read_req().is_long); 1008 EXPECT_TRUE(test_desc_id_ == test_delegate.desc_read_req().gatt_id); 1009 1010 // Callback with a pending request ID will be ignored. 1011 fake_hal_gatt_iface_->NotifyRequestReadCallback( 1012 kConnId0, kReqId0, hal_addr0, char_handle_, 0, false); 1013 fake_hal_gatt_iface_->NotifyRequestReadCallback( 1014 kConnId0, kReqId1, hal_addr0, char_handle_, 0, false); 1015 EXPECT_EQ(1, test_delegate.char_read_req().count); 1016 EXPECT_EQ(1, test_delegate.desc_read_req().count); 1017 1018 // Send response for wrong device address. 1019 EXPECT_FALSE(gatt_server_->SendResponse( 1020 kTestAddress1, kReqId0, 1021 GATT_ERROR_NONE, 0, kTestValue)); 1022 1023 // Send response for a value that's too large. 1024 EXPECT_FALSE(gatt_server_->SendResponse( 1025 kTestAddress0, kReqId0, 1026 GATT_ERROR_NONE, 0, kTestValueTooLarge)); 1027 1028 EXPECT_CALL(*mock_handler_, SendResponse(kConnId0, kReqId0, 1029 BT_STATUS_SUCCESS, _)) 1030 .Times(2) 1031 .WillOnce(Return(BT_STATUS_FAIL)) 1032 .WillOnce(Return(BT_STATUS_SUCCESS)); 1033 1034 // Stack call fails. 1035 EXPECT_FALSE(gatt_server_->SendResponse( 1036 kTestAddress0, kReqId0, 1037 GATT_ERROR_NONE, 0, kTestValue)); 1038 1039 // Successful send response for characteristic. 1040 EXPECT_TRUE(gatt_server_->SendResponse( 1041 kTestAddress0, kReqId0, 1042 GATT_ERROR_NONE, 0, kTestValue)); 1043 1044 // Characteristic request ID no longer pending. 1045 EXPECT_FALSE(gatt_server_->SendResponse( 1046 kTestAddress0, kReqId0, 1047 GATT_ERROR_NONE, 0, kTestValue)); 1048 1049 EXPECT_CALL(*mock_handler_, SendResponse(kConnId0, kReqId1, 1050 BT_STATUS_SUCCESS, _)) 1051 .Times(1) 1052 .WillOnce(Return(BT_STATUS_SUCCESS)); 1053 1054 // Successful send response for descriptor. 1055 EXPECT_TRUE(gatt_server_->SendResponse( 1056 kTestAddress0, kReqId1, 1057 GATT_ERROR_NONE, 0, kTestValue)); 1058 1059 // Descriptor request ID no longer pending. 1060 EXPECT_FALSE(gatt_server_->SendResponse( 1061 kTestAddress0, kReqId1, 1062 GATT_ERROR_NONE, 0, kTestValue)); 1063 1064 gatt_server_->SetDelegate(nullptr); 1065} 1066 1067TEST_F(GattServerPostRegisterTest, RequestWrite) { 1068 SetUpTestService(); 1069 1070 TestDelegate test_delegate; 1071 gatt_server_->SetDelegate(&test_delegate); 1072 1073 const std::vector<uint8_t> kTestValue = { 0x01, 0x02, 0x03 }; 1074 const std::string kTestAddress0 = "01:23:45:67:89:AB"; 1075 const std::string kTestAddress1 = "CD:EF:01:23:45:67"; 1076 const int kReqId0 = 0; 1077 const int kReqId1 = 1; 1078 const int kConnId0 = 1; 1079 1080 // No pending request. 1081 EXPECT_FALSE(gatt_server_->SendResponse( 1082 kTestAddress0, kReqId0, 1083 GATT_ERROR_NONE, 0, kTestValue)); 1084 1085 bt_bdaddr_t hal_addr0, hal_addr1; 1086 ASSERT_TRUE(util::BdAddrFromString(kTestAddress0, &hal_addr0)); 1087 ASSERT_TRUE(util::BdAddrFromString(kTestAddress1, &hal_addr1)); 1088 1089 // Send a connection callback. The GattServer should store the connection 1090 // information and be able to process the incoming read requests for this 1091 // connection. 1092 fake_hal_gatt_iface_->NotifyServerConnectionCallback( 1093 kConnId0, kDefaultServerId, true, hal_addr0); 1094 1095 // Unknown connection ID shouldn't trigger anything. 1096 fake_hal_gatt_iface_->NotifyRequestWriteCallback( 1097 kConnId0 + 1, kReqId0, hal_addr0, char_handle_, 0, 1098 kTestValue.size(), true, false, (uint8_t *)kTestValue.data()); 1099 EXPECT_EQ(0, test_delegate.char_write_req().count); 1100 EXPECT_EQ(0, test_delegate.desc_write_req().count); 1101 1102 // Unknown device address shouldn't trigger anything. 1103 fake_hal_gatt_iface_->NotifyRequestWriteCallback( 1104 kConnId0, kReqId0, hal_addr1, char_handle_, 0, 1105 kTestValue.size(), true, false, (uint8_t *)kTestValue.data()); 1106 EXPECT_EQ(0, test_delegate.char_write_req().count); 1107 EXPECT_EQ(0, test_delegate.desc_write_req().count); 1108 1109 // Unknown attribute handle shouldn't trigger anything. 1110 fake_hal_gatt_iface_->NotifyRequestWriteCallback( 1111 kConnId0, kReqId0, hal_addr0, char_handle_ + 50, 0, 1112 kTestValue.size(), true, false, (uint8_t *)kTestValue.data()); 1113 EXPECT_EQ(0, test_delegate.char_write_req().count); 1114 EXPECT_EQ(0, test_delegate.desc_write_req().count); 1115 1116 // Characteristic and descriptor handles should trigger correct callbacks. 1117 fake_hal_gatt_iface_->NotifyRequestWriteCallback( 1118 kConnId0, kReqId0, hal_addr0, char_handle_, 0, 1119 kTestValue.size(), true, false, (uint8_t *)kTestValue.data()); 1120 EXPECT_EQ(1, test_delegate.char_write_req().count); 1121 EXPECT_EQ(kTestAddress0, test_delegate.char_write_req().device_address); 1122 EXPECT_EQ(kReqId0, test_delegate.char_write_req().id); 1123 EXPECT_EQ(0, test_delegate.char_write_req().offset); 1124 EXPECT_EQ(true, test_delegate.char_write_req().need_rsp); 1125 EXPECT_EQ(false, test_delegate.char_write_req().is_exec); 1126 EXPECT_EQ(kTestValue, test_delegate.char_write_req().write_value); 1127 EXPECT_TRUE(test_char_id_ == test_delegate.char_write_req().gatt_id); 1128 EXPECT_EQ(0, test_delegate.desc_write_req().count); 1129 1130 fake_hal_gatt_iface_->NotifyRequestWriteCallback( 1131 kConnId0, kReqId1, hal_addr0, desc_handle_, 2, 1132 kTestValue.size(), true, false, (uint8_t *)kTestValue.data()); 1133 EXPECT_EQ(1, test_delegate.char_write_req().count); 1134 EXPECT_EQ(1, test_delegate.desc_write_req().count); 1135 EXPECT_EQ(kTestAddress0, test_delegate.desc_write_req().device_address); 1136 EXPECT_EQ(kReqId1, test_delegate.desc_write_req().id); 1137 EXPECT_EQ(2, test_delegate.desc_write_req().offset); 1138 EXPECT_EQ(true, test_delegate.desc_write_req().need_rsp); 1139 EXPECT_EQ(false, test_delegate.desc_write_req().is_exec); 1140 EXPECT_EQ(kTestValue, test_delegate.desc_write_req().write_value); 1141 EXPECT_TRUE(test_desc_id_ == test_delegate.desc_write_req().gatt_id); 1142 1143 // Callback with a pending request ID will be ignored. 1144 fake_hal_gatt_iface_->NotifyRequestWriteCallback( 1145 kConnId0, kReqId0, hal_addr0, char_handle_, 0, 1146 kTestValue.size(), true, false, (uint8_t *)kTestValue.data()); 1147 fake_hal_gatt_iface_->NotifyRequestWriteCallback( 1148 kConnId0, kReqId1, hal_addr0, char_handle_, 0, 1149 kTestValue.size(), true, false, (uint8_t *)kTestValue.data()); 1150 EXPECT_EQ(1, test_delegate.char_write_req().count); 1151 EXPECT_EQ(1, test_delegate.desc_write_req().count); 1152 1153 // Send response for wrong device address. 1154 EXPECT_FALSE(gatt_server_->SendResponse( 1155 kTestAddress1, kReqId0, 1156 GATT_ERROR_NONE, 0, kTestValue)); 1157 1158 EXPECT_CALL(*mock_handler_, SendResponse(kConnId0, kReqId0, 1159 BT_STATUS_SUCCESS, _)) 1160 .Times(2) 1161 .WillOnce(Return(BT_STATUS_FAIL)) 1162 .WillOnce(Return(BT_STATUS_SUCCESS)); 1163 1164 // Stack call fails. 1165 EXPECT_FALSE(gatt_server_->SendResponse( 1166 kTestAddress0, kReqId0, 1167 GATT_ERROR_NONE, 0, kTestValue)); 1168 1169 // Successful send response for characteristic. 1170 EXPECT_TRUE(gatt_server_->SendResponse( 1171 kTestAddress0, kReqId0, 1172 GATT_ERROR_NONE, 0, kTestValue)); 1173 1174 // Characteristic request ID no longer pending. 1175 EXPECT_FALSE(gatt_server_->SendResponse( 1176 kTestAddress0, kReqId0, 1177 GATT_ERROR_NONE, 0, kTestValue)); 1178 1179 EXPECT_CALL(*mock_handler_, SendResponse(kConnId0, kReqId1, 1180 BT_STATUS_SUCCESS, _)) 1181 .Times(1) 1182 .WillOnce(Return(BT_STATUS_SUCCESS)); 1183 1184 // Successful send response for descriptor. 1185 EXPECT_TRUE(gatt_server_->SendResponse( 1186 kTestAddress0, kReqId1, 1187 GATT_ERROR_NONE, 0, kTestValue)); 1188 1189 // Descriptor request ID no longer pending. 1190 EXPECT_FALSE(gatt_server_->SendResponse( 1191 kTestAddress0, kReqId1, 1192 GATT_ERROR_NONE, 0, kTestValue)); 1193 1194 // SendResponse should fail for a "Write Without Response". 1195 fake_hal_gatt_iface_->NotifyRequestWriteCallback( 1196 kConnId0, kReqId0, hal_addr0, char_handle_, 0, 1197 kTestValue.size(), false, false, (uint8_t *)kTestValue.data()); 1198 EXPECT_EQ(false, test_delegate.char_write_req().need_rsp); 1199 EXPECT_FALSE(gatt_server_->SendResponse( 1200 kTestAddress0, kReqId0, 1201 GATT_ERROR_NONE, 0, kTestValue)); 1202 1203 gatt_server_->SetDelegate(nullptr); 1204} 1205 1206TEST_F(GattServerPostRegisterTest, SendNotification) { 1207 SetUpTestService(); 1208 1209 const std::string kTestAddress0 = "01:23:45:67:89:AB"; 1210 const std::string kTestAddress1 = "cd:ef:01:23:45:67"; 1211 const std::string kInvalidAddress = "thingamajig blabbidyboop"; 1212 const int kConnId0 = 0; 1213 const int kConnId1 = 1; 1214 std::vector<uint8_t> value; 1215 bt_bdaddr_t hal_addr0; 1216 ASSERT_TRUE(util::BdAddrFromString(kTestAddress0, &hal_addr0)); 1217 1218 // Set up two connections with the same address. 1219 fake_hal_gatt_iface_->NotifyServerConnectionCallback( 1220 kConnId0, kDefaultServerId, true, hal_addr0); 1221 fake_hal_gatt_iface_->NotifyServerConnectionCallback( 1222 kConnId1, kDefaultServerId, true, hal_addr0); 1223 1224 // Set up a test callback. 1225 GATTError gatt_error; 1226 int callback_count = 0; 1227 auto callback = [&](GATTError in_error) { 1228 gatt_error = in_error; 1229 callback_count++; 1230 }; 1231 1232 // Bad device address. 1233 EXPECT_FALSE(gatt_server_->SendNotification( 1234 kInvalidAddress, 1235 test_char_id_, false, value, callback)); 1236 1237 // Bad connection. 1238 EXPECT_FALSE(gatt_server_->SendNotification( 1239 kTestAddress1, 1240 test_char_id_, false, value, callback)); 1241 1242 // We should get a HAL call for each connection for this address. The calls 1243 // fail. 1244 EXPECT_CALL(*mock_handler_, 1245 SendIndication(kDefaultServerId, char_handle_, kConnId0, 1246 value.size(), 0, nullptr)) 1247 .Times(1) 1248 .WillOnce(Return(BT_STATUS_FAIL)); 1249 EXPECT_CALL(*mock_handler_, 1250 SendIndication(kDefaultServerId, char_handle_, kConnId1, 1251 value.size(), 0, nullptr)) 1252 .Times(1) 1253 .WillOnce(Return(BT_STATUS_FAIL)); 1254 EXPECT_FALSE(gatt_server_->SendNotification( 1255 kTestAddress0, 1256 test_char_id_, false, value, callback)); 1257 1258 // One of the calls succeeds. 1259 EXPECT_CALL(*mock_handler_, 1260 SendIndication(kDefaultServerId, char_handle_, kConnId0, 1261 value.size(), 0, nullptr)) 1262 .Times(1) 1263 .WillOnce(Return(BT_STATUS_SUCCESS)); 1264 EXPECT_CALL(*mock_handler_, 1265 SendIndication(kDefaultServerId, char_handle_, kConnId1, 1266 value.size(), 0, nullptr)) 1267 .Times(1) 1268 .WillOnce(Return(BT_STATUS_FAIL)); 1269 EXPECT_TRUE(gatt_server_->SendNotification( 1270 kTestAddress0, 1271 test_char_id_, false, value, callback)); 1272 1273 // One of the connections is already pending so there should be only one call. 1274 // This one we send with confirm=true. 1275 EXPECT_CALL(*mock_handler_, 1276 SendIndication(kDefaultServerId, char_handle_, kConnId1, 1277 value.size(), 1, nullptr)) 1278 .Times(1) 1279 .WillOnce(Return(BT_STATUS_SUCCESS)); 1280 EXPECT_TRUE(gatt_server_->SendNotification( 1281 kTestAddress0, 1282 test_char_id_, true, value, callback)); 1283 1284 // Calls are already pending. 1285 EXPECT_FALSE(gatt_server_->SendNotification( 1286 kTestAddress0, test_char_id_, true, value, callback)); 1287 1288 // Trigger one confirmation callback. We should get calls for two callbacks 1289 // since we have two separate calls pending. 1290 fake_hal_gatt_iface_->NotifyIndicationSentCallback( 1291 kConnId0, BT_STATUS_SUCCESS); 1292 fake_hal_gatt_iface_->NotifyIndicationSentCallback( 1293 kConnId1, BT_STATUS_SUCCESS); 1294 EXPECT_EQ(2, callback_count); 1295 EXPECT_EQ(GATT_ERROR_NONE, gatt_error); 1296 1297 callback_count = 0; 1298 1299 // Restart. Both calls succeed now. 1300 EXPECT_CALL(*mock_handler_, 1301 SendIndication(kDefaultServerId, char_handle_, kConnId0, 1302 value.size(), 0, nullptr)) 1303 .Times(1) 1304 .WillOnce(Return(BT_STATUS_SUCCESS)); 1305 EXPECT_CALL(*mock_handler_, 1306 SendIndication(kDefaultServerId, char_handle_, kConnId1, 1307 value.size(), 0, nullptr)) 1308 .Times(1) 1309 .WillOnce(Return(BT_STATUS_SUCCESS)); 1310 EXPECT_TRUE(gatt_server_->SendNotification( 1311 kTestAddress0, 1312 test_char_id_, false, value, callback)); 1313 1314 // Trigger one confirmation callback. The callback we passed should still be 1315 // pending. The first callback is for the wrong connection ID. 1316 fake_hal_gatt_iface_->NotifyIndicationSentCallback( 1317 kConnId0 + 50, BT_STATUS_FAIL); 1318 fake_hal_gatt_iface_->NotifyIndicationSentCallback( 1319 kConnId0, BT_STATUS_SUCCESS); 1320 EXPECT_EQ(0, callback_count); 1321 1322 // This should be ignored since |kConnId0| was already processed. 1323 fake_hal_gatt_iface_->NotifyIndicationSentCallback( 1324 kConnId0, BT_STATUS_SUCCESS); 1325 EXPECT_EQ(0, callback_count); 1326 1327 // Run the callback with failure. Since the previous callback reported 1328 // success, we should report success. 1329 fake_hal_gatt_iface_->NotifyIndicationSentCallback( 1330 kConnId1, BT_STATUS_SUCCESS); 1331 EXPECT_EQ(1, callback_count); 1332 EXPECT_EQ(GATT_ERROR_NONE, gatt_error); 1333} 1334 1335} // namespace 1336} // namespace bluetooth 1337