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