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