2 // Copyright (C) 2015 Google, Inc.
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:
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <base/macros.h>
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
21 #include "service/adapter.h"
22 #include "service/hal/fake_bluetooth_gatt_interface.h"
23 #include "service/low_energy_client.h"
24 #include "stack/include/bt_types.h"
25 #include "stack/include/hcidefs.h"
26 #include "test/mock_adapter.h"
29 using ::testing::Return;
30 using ::testing::Pointee;
31 using ::testing::DoAll;
32 using ::testing::Invoke;
38 : public hal::FakeBluetoothGattInterface::TestClientHandler {
41 ON_CALL(*this, Scan(false))
42 .WillByDefault(Return(BT_STATUS_SUCCESS));
44 ~MockGattHandler() override = default;
46 MOCK_METHOD1(RegisterClient, bt_status_t(bt_uuid_t*));
47 MOCK_METHOD1(UnregisterClient, bt_status_t(int));
48 MOCK_METHOD1(Scan, bt_status_t(bool));
49 MOCK_METHOD4(Connect, bt_status_t(int , const bt_bdaddr_t *, bool, int));
50 MOCK_METHOD3(Disconnect, bt_status_t(int , const bt_bdaddr_t *, int));
51 MOCK_METHOD7(MultiAdvEnable, bt_status_t(int, int, int, int, int, int, int));
53 MultiAdvSetInstDataMock,
54 bt_status_t(bool, bool, bool, int, int, char*, int, char*, int, char*));
55 MOCK_METHOD1(MultiAdvDisable, bt_status_t(int));
57 // GMock has macros for up to 10 arguments (11 is really just too many...).
58 // For now we forward this call to a 10 argument mock, omitting the
59 // |client_if| argument.
60 bt_status_t MultiAdvSetInstData(
62 bool set_scan_rsp, bool include_name,
63 bool incl_txpower, int appearance,
64 int manufacturer_len, char* manufacturer_data,
65 int service_data_len, char* service_data,
66 int service_uuid_len, char* service_uuid) override {
67 return MultiAdvSetInstDataMock(
68 set_scan_rsp, include_name, incl_txpower, appearance,
69 manufacturer_len, manufacturer_data,
70 service_data_len, service_data,
71 service_uuid_len, service_uuid);
75 DISALLOW_COPY_AND_ASSIGN(MockGattHandler);
78 class TestDelegate : public LowEnergyClient::Delegate {
80 TestDelegate() : scan_result_count_(0), connection_state_count_(0) {
83 ~TestDelegate() override = default;
85 int scan_result_count() const { return scan_result_count_; }
86 const ScanResult& last_scan_result() const { return last_scan_result_; }
88 int connection_state_count() const { return connection_state_count_; }
90 void OnConnectionState(LowEnergyClient* client, int status,
91 const char* address, bool connected) {
93 connection_state_count_++;
96 void OnScanResult(LowEnergyClient* client, const ScanResult& scan_result) {
99 last_scan_result_ = scan_result;
103 int scan_result_count_;
104 ScanResult last_scan_result_;
106 int connection_state_count_;
108 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
111 // Created this class for testing Advertising Data Setting
112 // It provides a work around in order to verify the arguments
113 // in the arrays passed to MultiAdvSetInstData due to mocks
114 // not having an easy way to verify entire arrays
115 class AdvertiseDataHandler : public MockGattHandler {
117 AdvertiseDataHandler() : call_count_(0) {}
118 ~AdvertiseDataHandler() override = default;
120 bt_status_t MultiAdvSetInstData(
122 bool set_scan_rsp, bool include_name,
123 bool incl_txpower, int appearance,
124 int manufacturer_len, char* manufacturer_data,
125 int service_data_len, char* service_data,
126 int service_uuid_len, char* service_uuid) override {
128 service_data_.assign(
129 service_data, service_data+service_data_len);
130 manufacturer_data_.assign(
131 manufacturer_data, manufacturer_data+manufacturer_len);
133 service_uuid, service_uuid+service_uuid_len);
134 return BT_STATUS_SUCCESS;
137 const std::vector<uint8_t>& manufacturer_data() const {
138 return manufacturer_data_;
140 const std::vector<uint8_t>& service_data() const { return service_data_; }
141 const std::vector<uint8_t>& uuid_data() const { return uuid_data_; }
142 int call_count() const { return call_count_; }
146 std::vector<uint8_t> manufacturer_data_;
147 std::vector<uint8_t> service_data_;
148 std::vector<uint8_t> uuid_data_;
151 class LowEnergyClientTest : public ::testing::Test {
153 LowEnergyClientTest() = default;
154 ~LowEnergyClientTest() override = default;
156 void SetUp() override {
157 // Only set |mock_handler_| if a test hasn't set it.
159 mock_handler_.reset(new MockGattHandler());
160 fake_hal_gatt_iface_ = new hal::FakeBluetoothGattInterface(
161 std::static_pointer_cast<
162 hal::FakeBluetoothGattInterface::TestClientHandler>(mock_handler_),
164 hal::BluetoothGattInterface::InitializeForTesting(fake_hal_gatt_iface_);
165 ble_factory_.reset(new LowEnergyClientFactory(mock_adapter_));
168 void TearDown() override {
169 ble_factory_.reset();
170 hal::BluetoothGattInterface::CleanUp();
174 hal::FakeBluetoothGattInterface* fake_hal_gatt_iface_;
175 testing::MockAdapter mock_adapter_;
176 std::shared_ptr<MockGattHandler> mock_handler_;
177 std::unique_ptr<LowEnergyClientFactory> ble_factory_;
180 DISALLOW_COPY_AND_ASSIGN(LowEnergyClientTest);
183 // Used for tests that operate on a pre-registered client.
184 class LowEnergyClientPostRegisterTest : public LowEnergyClientTest {
186 LowEnergyClientPostRegisterTest() : next_client_id_(0) {
188 ~LowEnergyClientPostRegisterTest() override = default;
190 void SetUp() override {
191 LowEnergyClientTest::SetUp();
192 auto callback = [&](std::unique_ptr<LowEnergyClient> client) {
193 le_client_ = std::move(client);
195 RegisterTestClient(callback);
198 void TearDown() override {
199 EXPECT_CALL(*mock_handler_, MultiAdvDisable(_))
201 .WillOnce(Return(BT_STATUS_SUCCESS));
202 EXPECT_CALL(*mock_handler_, UnregisterClient(_))
204 .WillOnce(Return(BT_STATUS_SUCCESS));
206 LowEnergyClientTest::TearDown();
209 void RegisterTestClient(
210 const std::function<void(std::unique_ptr<LowEnergyClient> client)>
212 UUID uuid = UUID::GetRandom();
213 auto api_callback = [&](BLEStatus status, const UUID& in_uuid,
214 std::unique_ptr<BluetoothInstance> in_client) {
215 CHECK(in_uuid == uuid);
216 CHECK(in_client.get());
217 CHECK(status == BLE_STATUS_SUCCESS);
219 callback(std::unique_ptr<LowEnergyClient>(
220 static_cast<LowEnergyClient*>(in_client.release())));
223 EXPECT_CALL(*mock_handler_, RegisterClient(_))
225 .WillOnce(Return(BT_STATUS_SUCCESS));
227 ble_factory_->RegisterInstance(uuid, api_callback);
229 bt_uuid_t hal_uuid = uuid.GetBlueDroid();
230 fake_hal_gatt_iface_->NotifyRegisterClientCallback(
231 0, next_client_id_++, hal_uuid);
232 ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
235 void StartAdvertising() {
236 ASSERT_FALSE(le_client_->IsAdvertisingStarted());
237 ASSERT_FALSE(le_client_->IsStartingAdvertising());
238 ASSERT_FALSE(le_client_->IsStoppingAdvertising());
240 EXPECT_CALL(*mock_handler_, MultiAdvEnable(_, _, _, _, _, _, _))
242 .WillOnce(Return(BT_STATUS_SUCCESS));
243 EXPECT_CALL(*mock_handler_,
244 MultiAdvSetInstDataMock(_, _, _, _, _, _, _, _, _, _))
246 .WillOnce(Return(BT_STATUS_SUCCESS));
248 AdvertiseSettings settings;
249 AdvertiseData adv, scan_rsp;
250 ASSERT_TRUE(le_client_->StartAdvertising(
251 settings, adv, scan_rsp, LowEnergyClient::StatusCallback()));
252 ASSERT_TRUE(le_client_->IsStartingAdvertising());
254 fake_hal_gatt_iface_->NotifyMultiAdvEnableCallback(
255 le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
256 fake_hal_gatt_iface_->NotifyMultiAdvDataCallback(
257 le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
259 ASSERT_TRUE(le_client_->IsAdvertisingStarted());
260 ASSERT_FALSE(le_client_->IsStartingAdvertising());
261 ASSERT_FALSE(le_client_->IsStoppingAdvertising());
264 void AdvertiseDataTestHelper(AdvertiseData data, std::function<void(BLEStatus)> callback) {
265 AdvertiseSettings settings;
266 EXPECT_TRUE(le_client_->StartAdvertising(
267 settings, data, AdvertiseData(), callback));
268 fake_hal_gatt_iface_->NotifyMultiAdvEnableCallback(
269 le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
270 fake_hal_gatt_iface_->NotifyMultiAdvDataCallback(
271 le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
272 EXPECT_TRUE(le_client_->StopAdvertising(LowEnergyClient::StatusCallback()));
273 fake_hal_gatt_iface_->NotifyMultiAdvDisableCallback(
274 le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
278 std::unique_ptr<LowEnergyClient> le_client_;
283 DISALLOW_COPY_AND_ASSIGN(LowEnergyClientPostRegisterTest);
286 TEST_F(LowEnergyClientTest, RegisterInstance) {
287 EXPECT_CALL(*mock_handler_, RegisterClient(_))
289 .WillOnce(Return(BT_STATUS_FAIL))
290 .WillOnce(Return(BT_STATUS_SUCCESS));
292 // These will be asynchronously populated with a result when the callback
294 BLEStatus status = BLE_STATUS_SUCCESS;
296 std::unique_ptr<LowEnergyClient> client;
297 int callback_count = 0;
299 auto callback = [&](BLEStatus in_status, const UUID& uuid,
300 std::unique_ptr<BluetoothInstance> in_client) {
303 client = std::unique_ptr<LowEnergyClient>(
304 static_cast<LowEnergyClient*>(in_client.release()));
308 UUID uuid0 = UUID::GetRandom();
310 // HAL returns failure.
311 EXPECT_FALSE(ble_factory_->RegisterInstance(uuid0, callback));
312 EXPECT_EQ(0, callback_count);
314 // HAL returns success.
315 EXPECT_TRUE(ble_factory_->RegisterInstance(uuid0, callback));
316 EXPECT_EQ(0, callback_count);
318 // Calling twice with the same UUID should fail with no additional call into
320 EXPECT_FALSE(ble_factory_->RegisterInstance(uuid0, callback));
322 ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
324 // Call with a different UUID while one is pending.
325 UUID uuid1 = UUID::GetRandom();
326 EXPECT_CALL(*mock_handler_, RegisterClient(_))
328 .WillOnce(Return(BT_STATUS_SUCCESS));
329 EXPECT_TRUE(ble_factory_->RegisterInstance(uuid1, callback));
331 // Trigger callback with an unknown UUID. This should get ignored.
332 UUID uuid2 = UUID::GetRandom();
333 bt_uuid_t hal_uuid = uuid2.GetBlueDroid();
334 fake_hal_gatt_iface_->NotifyRegisterClientCallback(0, 0, hal_uuid);
335 EXPECT_EQ(0, callback_count);
338 int client_if0 = 2; // Pick something that's not 0.
339 hal_uuid = uuid0.GetBlueDroid();
340 fake_hal_gatt_iface_->NotifyRegisterClientCallback(
341 BT_STATUS_SUCCESS, client_if0, hal_uuid);
343 EXPECT_EQ(1, callback_count);
344 ASSERT_TRUE(client.get() != nullptr); // Assert to terminate in case of error
345 EXPECT_EQ(BLE_STATUS_SUCCESS, status);
346 EXPECT_EQ(client_if0, client->GetInstanceId());
347 EXPECT_EQ(uuid0, client->GetAppIdentifier());
348 EXPECT_EQ(uuid0, cb_uuid);
350 // The client should unregister itself when deleted.
351 EXPECT_CALL(*mock_handler_, MultiAdvDisable(client_if0))
353 .WillOnce(Return(BT_STATUS_SUCCESS));
354 EXPECT_CALL(*mock_handler_, UnregisterClient(client_if0))
356 .WillOnce(Return(BT_STATUS_SUCCESS));
358 ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
362 hal_uuid = uuid1.GetBlueDroid();
363 fake_hal_gatt_iface_->NotifyRegisterClientCallback(
364 BT_STATUS_FAIL, client_if1, hal_uuid);
366 EXPECT_EQ(2, callback_count);
367 ASSERT_TRUE(client.get() == nullptr); // Assert to terminate in case of error
368 EXPECT_EQ(BLE_STATUS_FAILURE, status);
369 EXPECT_EQ(uuid1, cb_uuid);
372 TEST_F(LowEnergyClientPostRegisterTest, StartAdvertisingBasic) {
373 EXPECT_FALSE(le_client_->IsAdvertisingStarted());
374 EXPECT_FALSE(le_client_->IsStartingAdvertising());
375 EXPECT_FALSE(le_client_->IsStoppingAdvertising());
377 // Use default advertising settings and data.
378 AdvertiseSettings settings;
379 AdvertiseData adv_data, scan_rsp;
380 int callback_count = 0;
381 BLEStatus last_status = BLE_STATUS_FAILURE;
382 auto callback = [&](BLEStatus status) {
383 last_status = status;
387 EXPECT_CALL(*mock_handler_, MultiAdvEnable(_, _, _, _, _, _, _))
389 .WillOnce(Return(BT_STATUS_FAIL))
390 .WillRepeatedly(Return(BT_STATUS_SUCCESS));
392 // Stack call returns failure.
393 EXPECT_FALSE(le_client_->StartAdvertising(
394 settings, adv_data, scan_rsp, callback));
395 EXPECT_FALSE(le_client_->IsAdvertisingStarted());
396 EXPECT_FALSE(le_client_->IsStartingAdvertising());
397 EXPECT_FALSE(le_client_->IsStoppingAdvertising());
398 EXPECT_EQ(0, callback_count);
400 // Stack call returns success.
401 EXPECT_TRUE(le_client_->StartAdvertising(
402 settings, adv_data, scan_rsp, callback));
403 EXPECT_FALSE(le_client_->IsAdvertisingStarted());
404 EXPECT_TRUE(le_client_->IsStartingAdvertising());
405 EXPECT_FALSE(le_client_->IsStoppingAdvertising());
406 EXPECT_EQ(0, callback_count);
409 EXPECT_FALSE(le_client_->StartAdvertising(
410 settings, adv_data, scan_rsp, callback));
413 fake_hal_gatt_iface_->NotifyMultiAdvEnableCallback(
414 le_client_->GetInstanceId(), BT_STATUS_FAIL);
415 EXPECT_FALSE(le_client_->IsAdvertisingStarted());
416 EXPECT_FALSE(le_client_->IsStartingAdvertising());
417 EXPECT_FALSE(le_client_->IsStoppingAdvertising());
418 EXPECT_EQ(1, callback_count);
419 EXPECT_EQ(BLE_STATUS_FAILURE, last_status);
422 EXPECT_TRUE(le_client_->StartAdvertising(
423 settings, adv_data, scan_rsp, callback));
424 EXPECT_FALSE(le_client_->IsAdvertisingStarted());
425 EXPECT_TRUE(le_client_->IsStartingAdvertising());
426 EXPECT_FALSE(le_client_->IsStoppingAdvertising());
427 EXPECT_EQ(1, callback_count);
429 // Success notification should trigger advertise data update.
430 EXPECT_CALL(*mock_handler_,
431 MultiAdvSetInstDataMock(
432 false, // set_scan_rsp
433 false, // include_name
434 false, // incl_txpower
435 _, _, _, _, _, _, _))
437 .WillOnce(Return(BT_STATUS_FAIL))
438 .WillRepeatedly(Return(BT_STATUS_SUCCESS));
440 // Notify success for enable. The procedure will fail since setting data will
442 fake_hal_gatt_iface_->NotifyMultiAdvEnableCallback(
443 le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
444 EXPECT_FALSE(le_client_->IsAdvertisingStarted());
445 EXPECT_FALSE(le_client_->IsStartingAdvertising());
446 EXPECT_FALSE(le_client_->IsStoppingAdvertising());
447 EXPECT_EQ(2, callback_count);
448 EXPECT_EQ(BLE_STATUS_FAILURE, last_status);
451 EXPECT_TRUE(le_client_->StartAdvertising(
452 settings, adv_data, scan_rsp, callback));
453 EXPECT_FALSE(le_client_->IsAdvertisingStarted());
454 EXPECT_TRUE(le_client_->IsStartingAdvertising());
455 EXPECT_FALSE(le_client_->IsStoppingAdvertising());
456 EXPECT_EQ(2, callback_count);
458 // Notify success for enable. the advertise data call should succeed but
459 // operation will remain pending.
460 fake_hal_gatt_iface_->NotifyMultiAdvEnableCallback(
461 le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
462 EXPECT_FALSE(le_client_->IsAdvertisingStarted());
463 EXPECT_TRUE(le_client_->IsStartingAdvertising());
464 EXPECT_FALSE(le_client_->IsStoppingAdvertising());
465 EXPECT_EQ(2, callback_count);
467 // Notify failure from advertising call.
468 fake_hal_gatt_iface_->NotifyMultiAdvDataCallback(
469 le_client_->GetInstanceId(), BT_STATUS_FAIL);
470 EXPECT_FALSE(le_client_->IsAdvertisingStarted());
471 EXPECT_FALSE(le_client_->IsStartingAdvertising());
472 EXPECT_FALSE(le_client_->IsStoppingAdvertising());
473 EXPECT_EQ(3, callback_count);
474 EXPECT_EQ(BLE_STATUS_FAILURE, last_status);
476 // Try again. Make everything succeed.
477 EXPECT_TRUE(le_client_->StartAdvertising(
478 settings, adv_data, scan_rsp, callback));
479 EXPECT_FALSE(le_client_->IsAdvertisingStarted());
480 EXPECT_TRUE(le_client_->IsStartingAdvertising());
481 EXPECT_FALSE(le_client_->IsStoppingAdvertising());
482 EXPECT_EQ(3, callback_count);
484 fake_hal_gatt_iface_->NotifyMultiAdvEnableCallback(
485 le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
486 fake_hal_gatt_iface_->NotifyMultiAdvDataCallback(
487 le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
488 EXPECT_TRUE(le_client_->IsAdvertisingStarted());
489 EXPECT_FALSE(le_client_->IsStartingAdvertising());
490 EXPECT_FALSE(le_client_->IsStoppingAdvertising());
491 EXPECT_EQ(4, callback_count);
492 EXPECT_EQ(BLE_STATUS_SUCCESS, last_status);
495 EXPECT_FALSE(le_client_->StartAdvertising(
496 settings, adv_data, scan_rsp, callback));
499 TEST_F(LowEnergyClientPostRegisterTest, StopAdvertisingBasic) {
500 AdvertiseSettings settings;
503 EXPECT_FALSE(le_client_->IsAdvertisingStarted());
504 EXPECT_FALSE(le_client_->StopAdvertising(LowEnergyClient::StatusCallback()));
506 // Start advertising for testing.
509 int callback_count = 0;
510 BLEStatus last_status = BLE_STATUS_FAILURE;
511 auto callback = [&](BLEStatus status) {
512 last_status = status;
516 EXPECT_CALL(*mock_handler_, MultiAdvDisable(_))
518 .WillOnce(Return(BT_STATUS_FAIL))
519 .WillRepeatedly(Return(BT_STATUS_SUCCESS));
521 // Stack call returns failure.
522 EXPECT_FALSE(le_client_->StopAdvertising(callback));
523 EXPECT_TRUE(le_client_->IsAdvertisingStarted());
524 EXPECT_FALSE(le_client_->IsStartingAdvertising());
525 EXPECT_FALSE(le_client_->IsStoppingAdvertising());
526 EXPECT_EQ(0, callback_count);
528 // Stack returns success.
529 EXPECT_TRUE(le_client_->StopAdvertising(callback));
530 EXPECT_TRUE(le_client_->IsAdvertisingStarted());
531 EXPECT_FALSE(le_client_->IsStartingAdvertising());
532 EXPECT_TRUE(le_client_->IsStoppingAdvertising());
533 EXPECT_EQ(0, callback_count);
535 // Already disabling.
536 EXPECT_FALSE(le_client_->StopAdvertising(callback));
537 EXPECT_TRUE(le_client_->IsAdvertisingStarted());
538 EXPECT_FALSE(le_client_->IsStartingAdvertising());
539 EXPECT_TRUE(le_client_->IsStoppingAdvertising());
540 EXPECT_EQ(0, callback_count);
543 fake_hal_gatt_iface_->NotifyMultiAdvDisableCallback(
544 le_client_->GetInstanceId(), BT_STATUS_FAIL);
545 EXPECT_TRUE(le_client_->IsAdvertisingStarted());
546 EXPECT_FALSE(le_client_->IsStartingAdvertising());
547 EXPECT_FALSE(le_client_->IsStoppingAdvertising());
548 EXPECT_EQ(1, callback_count);
549 EXPECT_EQ(BLE_STATUS_FAILURE, last_status);
552 EXPECT_TRUE(le_client_->StopAdvertising(callback));
553 EXPECT_TRUE(le_client_->IsAdvertisingStarted());
554 EXPECT_FALSE(le_client_->IsStartingAdvertising());
555 EXPECT_TRUE(le_client_->IsStoppingAdvertising());
556 EXPECT_EQ(1, callback_count);
559 fake_hal_gatt_iface_->NotifyMultiAdvDisableCallback(
560 le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
561 EXPECT_FALSE(le_client_->IsAdvertisingStarted());
562 EXPECT_FALSE(le_client_->IsStartingAdvertising());
563 EXPECT_FALSE(le_client_->IsStoppingAdvertising());
564 EXPECT_EQ(2, callback_count);
565 EXPECT_EQ(BLE_STATUS_SUCCESS, last_status);
568 EXPECT_FALSE(le_client_->StopAdvertising(callback));
571 TEST_F(LowEnergyClientPostRegisterTest, InvalidAdvertiseData) {
572 const std::vector<uint8_t> data0{ 0x02, HCI_EIR_FLAGS_TYPE, 0x00 };
573 const std::vector<uint8_t> data1{
574 0x04, HCI_EIR_MANUFACTURER_SPECIFIC_TYPE, 0x01, 0x02, 0x00
576 AdvertiseData invalid_adv(data0);
577 AdvertiseData valid_adv(data1);
579 AdvertiseSettings settings;
581 EXPECT_FALSE(le_client_->StartAdvertising(
582 settings, valid_adv, invalid_adv, LowEnergyClient::StatusCallback()));
583 EXPECT_FALSE(le_client_->StartAdvertising(
584 settings, invalid_adv, valid_adv, LowEnergyClient::StatusCallback()));
586 // Manufacturer data not correctly formatted according to spec. We let the
587 // stack handle this case.
588 const std::vector<uint8_t> data2{ 0x01, HCI_EIR_MANUFACTURER_SPECIFIC_TYPE };
589 AdvertiseData invalid_mfc(data2);
591 EXPECT_CALL(*mock_handler_, MultiAdvEnable(_, _, _, _, _, _, _))
593 .WillOnce(Return(BT_STATUS_SUCCESS));
594 EXPECT_TRUE(le_client_->StartAdvertising(
595 settings, invalid_mfc, valid_adv, LowEnergyClient::StatusCallback()));
598 TEST_F(LowEnergyClientPostRegisterTest, ScanResponse) {
599 EXPECT_FALSE(le_client_->IsAdvertisingStarted());
600 EXPECT_FALSE(le_client_->IsStartingAdvertising());
601 EXPECT_FALSE(le_client_->IsStoppingAdvertising());
603 AdvertiseSettings settings(
604 AdvertiseSettings::MODE_LOW_POWER,
605 base::TimeDelta::FromMilliseconds(300),
606 AdvertiseSettings::TX_POWER_LEVEL_MEDIUM,
607 false /* connectable */);
609 const std::vector<uint8_t> data0;
610 const std::vector<uint8_t> data1{
611 0x04, HCI_EIR_MANUFACTURER_SPECIFIC_TYPE, 0x01, 0x02, 0x00
614 int callback_count = 0;
615 BLEStatus last_status = BLE_STATUS_FAILURE;
616 auto callback = [&](BLEStatus status) {
617 last_status = status;
621 AdvertiseData adv0(data0);
622 adv0.set_include_tx_power_level(true);
624 AdvertiseData adv1(data1);
625 adv1.set_include_device_name(true);
627 EXPECT_CALL(*mock_handler_,
628 MultiAdvEnable(le_client_->GetInstanceId(), _, _,
629 kAdvertisingEventTypeScannable,
632 .WillRepeatedly(Return(BT_STATUS_SUCCESS));
635 MultiAdvSetInstDataMock(
636 false, // set_scan_rsp
637 false, // include_name
638 true, // incl_txpower,
643 .WillRepeatedly(Return(BT_STATUS_SUCCESS));
646 MultiAdvSetInstDataMock(
647 true, // set_scan_rsp
648 true, // include_name
649 false, // incl_txpower,
651 data1.size() - 2, // Mfc. Specific data field bytes.
654 .WillRepeatedly(Return(BT_STATUS_SUCCESS));
656 // Enable success; Adv. data success; Scan rsp. fail.
657 EXPECT_TRUE(le_client_->StartAdvertising(settings, adv0, adv1, callback));
658 fake_hal_gatt_iface_->NotifyMultiAdvEnableCallback(
659 le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
660 fake_hal_gatt_iface_->NotifyMultiAdvDataCallback(
661 le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
662 fake_hal_gatt_iface_->NotifyMultiAdvDataCallback(
663 le_client_->GetInstanceId(), BT_STATUS_FAIL);
665 EXPECT_EQ(1, callback_count);
666 EXPECT_EQ(BLE_STATUS_FAILURE, last_status);
667 EXPECT_FALSE(le_client_->IsAdvertisingStarted());
669 // Second time everything succeeds.
670 EXPECT_TRUE(le_client_->StartAdvertising(settings, adv0, adv1, callback));
671 fake_hal_gatt_iface_->NotifyMultiAdvEnableCallback(
672 le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
673 fake_hal_gatt_iface_->NotifyMultiAdvDataCallback(
674 le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
675 fake_hal_gatt_iface_->NotifyMultiAdvDataCallback(
676 le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
678 EXPECT_EQ(2, callback_count);
679 EXPECT_EQ(BLE_STATUS_SUCCESS, last_status);
680 EXPECT_TRUE(le_client_->IsAdvertisingStarted());
683 TEST_F(LowEnergyClientPostRegisterTest, AdvertiseDataParsing) {
684 // Re-initialize the test with our own custom handler.
686 std::shared_ptr<AdvertiseDataHandler> adv_handler(new AdvertiseDataHandler());
687 mock_handler_ = std::static_pointer_cast<MockGattHandler>(adv_handler);
690 const std::vector<uint8_t> kUUID16BitData{
691 0x03, HCI_EIR_COMPLETE_16BITS_UUID_TYPE, 0xDE, 0xAD,
694 const std::vector<uint8_t> kUUID32BitData{
695 0x05, HCI_EIR_COMPLETE_32BITS_UUID_TYPE, 0xDE, 0xAD, 0x01, 0x02
698 const std::vector<uint8_t> kUUID128BitData{
699 0x11, HCI_EIR_COMPLETE_128BITS_UUID_TYPE,
700 0xDE, 0xAD, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
701 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E
704 const std::vector<uint8_t> kMultiUUIDData{
705 0x11, HCI_EIR_COMPLETE_128BITS_UUID_TYPE,
706 0xDE, 0xAD, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
707 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
708 0x05, HCI_EIR_COMPLETE_32BITS_UUID_TYPE, 0xDE, 0xAD, 0xBE, 0xEF
711 const std::vector<uint8_t> kServiceData16Bit{
712 0x05, HCI_EIR_SERVICE_DATA_16BITS_UUID_TYPE, 0xDE, 0xAD, 0xBE, 0xEF
715 const std::vector<uint8_t> kServiceData32Bit{
716 0x07, HCI_EIR_SERVICE_DATA_32BITS_UUID_TYPE, 0xDE, 0xAD, 0x01, 0x02, 0xBE, 0xEF
719 const std::vector<uint8_t> kServiceData128Bit{
720 0x13, HCI_EIR_SERVICE_DATA_128BITS_UUID_TYPE,
721 0xDE, 0xAD, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
722 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0xBE, 0xEF
725 const std::vector<uint8_t> kMultiServiceData{
726 0x13, HCI_EIR_SERVICE_DATA_128BITS_UUID_TYPE,
727 0xDE, 0xAD, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xBE, 0xEF,
728 0xDE, 0xAD, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
729 0x05, HCI_EIR_SERVICE_DATA_16BITS_UUID_TYPE, 0xDE, 0xAD, 0xBE, 0xEF
732 const std::vector<uint8_t> kServiceUUIDMatch{
733 0x05, HCI_EIR_COMPLETE_32BITS_UUID_TYPE, 0xDE, 0xAD, 0x01, 0x02,
734 0x07, HCI_EIR_SERVICE_DATA_32BITS_UUID_TYPE, 0xDE, 0xAD, 0x01, 0x02, 0xBE, 0xEF
737 const std::vector<uint8_t> kServiceUUIDMismatch{
738 0x05, HCI_EIR_COMPLETE_32BITS_UUID_TYPE, 0xDE, 0xAD, 0x01, 0x01,
739 0x07, HCI_EIR_SERVICE_DATA_32BITS_UUID_TYPE, 0xDE, 0xAD, 0x01, 0x02, 0xBE, 0xEF
742 AdvertiseData uuid_16bit_adv(kUUID16BitData);
743 AdvertiseData uuid_32bit_adv(kUUID32BitData);
744 AdvertiseData uuid_128bit_adv(kUUID128BitData);
745 AdvertiseData multi_uuid_adv(kMultiUUIDData);
747 AdvertiseData service_16bit_adv(kServiceData16Bit);
748 AdvertiseData service_32bit_adv(kServiceData32Bit);
749 AdvertiseData service_128bit_adv(kServiceData128Bit);
750 AdvertiseData multi_service_adv(kMultiServiceData);
752 AdvertiseData service_uuid_match(kServiceUUIDMatch);
753 AdvertiseData service_uuid_mismatch(kServiceUUIDMismatch);
755 AdvertiseSettings settings;
757 int callback_count = 0;
758 BLEStatus last_status = BLE_STATUS_FAILURE;
759 auto callback = [&](BLEStatus status) {
760 last_status = status;
764 EXPECT_CALL(*mock_handler_, MultiAdvEnable(_, _, _, _, _, _, _))
765 .WillRepeatedly(Return(BT_STATUS_SUCCESS));
766 EXPECT_CALL(*mock_handler_, MultiAdvDisable(_))
767 .WillRepeatedly(Return(BT_STATUS_SUCCESS));
769 // Multiple UUID test, should fail due to only one UUID allowed
770 EXPECT_TRUE(le_client_->StartAdvertising(
771 settings, multi_uuid_adv, AdvertiseData(), callback));
772 fake_hal_gatt_iface_->NotifyMultiAdvEnableCallback(
773 le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
774 EXPECT_EQ(1, callback_count);
775 EXPECT_EQ(0, adv_handler->call_count());
776 EXPECT_EQ(BLE_STATUS_FAILURE, last_status);
778 // Multiple Service Data test, should fail due to only one service data allowed
779 EXPECT_TRUE(le_client_->StartAdvertising(
780 settings, multi_uuid_adv, AdvertiseData(), callback));
781 fake_hal_gatt_iface_->NotifyMultiAdvEnableCallback(
782 le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
783 EXPECT_EQ(2, callback_count);
784 EXPECT_EQ(0, adv_handler->call_count());
785 EXPECT_EQ(BLE_STATUS_FAILURE, last_status);
787 // 16bit uuid test, should succeed with correctly parsed uuid in little-endian
789 AdvertiseDataTestHelper(uuid_16bit_adv, callback);
790 EXPECT_EQ(3, callback_count);
791 EXPECT_EQ(1, adv_handler->call_count());
792 const std::vector<uint8_t> uuid_16bit_canonical{
793 0xFB, 0x34, 0x9b, 0x5F, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00,
794 0xDE, 0xAD, 0x00, 0x00
796 EXPECT_EQ(uuid_16bit_canonical, adv_handler->uuid_data());
798 // 32bit uuid test, should succeed with correctly parsed uuid
799 AdvertiseDataTestHelper(uuid_32bit_adv, callback);
800 EXPECT_EQ(4, callback_count);
801 EXPECT_EQ(2, adv_handler->call_count());
802 const std::vector<uint8_t> uuid_32bit_canonical{
803 0xFB, 0x34, 0x9b, 0x5F, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00,
804 0xDE, 0xAD, 0x01, 0x02
806 EXPECT_EQ(uuid_32bit_canonical, adv_handler->uuid_data());
808 // 128bit uuid test, should succeed with correctly parsed uuid
809 AdvertiseDataTestHelper(uuid_128bit_adv, callback);
810 EXPECT_EQ(5, callback_count);
811 EXPECT_EQ(3, adv_handler->call_count());
812 const std::vector<uint8_t> uuid_128bit{
813 0xDE, 0xAD, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
814 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E
816 EXPECT_EQ(uuid_128bit, adv_handler->uuid_data());
818 const std::vector<uint8_t> service_data{ 0xBE, 0xEF };
820 // Service data with 16bit uuid included, should succeed with
821 // uuid and service data parsed out
822 AdvertiseDataTestHelper(service_16bit_adv, callback);
823 EXPECT_EQ(6, callback_count);
824 EXPECT_EQ(4, adv_handler->call_count());
825 EXPECT_EQ(service_data, adv_handler->service_data());
826 EXPECT_EQ(uuid_16bit_canonical, adv_handler->uuid_data());
828 // Service data with 32bit uuid included, should succeed with
829 // uuid and service data parsed out
830 AdvertiseDataTestHelper(service_32bit_adv, callback);
831 EXPECT_EQ(7, callback_count);
832 EXPECT_EQ(5, adv_handler->call_count());
833 EXPECT_EQ(service_data, adv_handler->service_data());
834 EXPECT_EQ(uuid_32bit_canonical, adv_handler->uuid_data());
836 // Service data with 128bit uuid included, should succeed with
837 // uuid and service data parsed out
838 AdvertiseDataTestHelper(service_128bit_adv, callback);
839 EXPECT_EQ(8, callback_count);
840 EXPECT_EQ(6, adv_handler->call_count());
841 EXPECT_EQ(service_data, adv_handler->service_data());
842 EXPECT_EQ(uuid_128bit, adv_handler->uuid_data());
844 // Service data and UUID where the UUID for both match, should succeed.
845 AdvertiseDataTestHelper(service_uuid_match, callback);
846 EXPECT_EQ(9, callback_count);
847 EXPECT_EQ(7, adv_handler->call_count());
848 EXPECT_EQ(service_data, adv_handler->service_data());
849 EXPECT_EQ(uuid_32bit_canonical, adv_handler->uuid_data());
851 // Service data and UUID where the UUID for dont match, should fail
852 EXPECT_TRUE(le_client_->StartAdvertising(
853 settings, service_uuid_mismatch, AdvertiseData(), callback));
854 fake_hal_gatt_iface_->NotifyMultiAdvEnableCallback(
855 le_client_->GetInstanceId(), BT_STATUS_SUCCESS);
856 EXPECT_EQ(10, callback_count);
857 EXPECT_EQ(7, adv_handler->call_count());
858 EXPECT_EQ(BLE_STATUS_FAILURE, last_status);
861 TEST_F(LowEnergyClientPostRegisterTest, ScanSettings) {
862 EXPECT_CALL(mock_adapter_, IsEnabled())
863 .WillOnce(Return(false))
864 .WillRepeatedly(Return(true));
866 ScanSettings settings;
867 std::vector<ScanFilter> filters;
869 // Adapter is not enabled.
870 EXPECT_FALSE(le_client_->StartScan(settings, filters));
872 // TODO(jpawlowski): add tests checking settings and filter parsing when
875 // These should succeed and result in a HAL call
876 EXPECT_CALL(*mock_handler_, Scan(true))
878 .WillOnce(Return(BT_STATUS_SUCCESS));
879 EXPECT_TRUE(le_client_->StartScan(settings, filters));
881 // These should succeed and result in a HAL call
882 EXPECT_CALL(*mock_handler_, Scan(false))
884 .WillOnce(Return(BT_STATUS_SUCCESS));
885 EXPECT_TRUE(le_client_->StopScan());
887 ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
890 TEST_F(LowEnergyClientPostRegisterTest, ScanRecord) {
891 TestDelegate delegate;
892 le_client_->SetDelegate(&delegate);
894 EXPECT_EQ(0, delegate.scan_result_count());
896 const uint8_t kTestRecord0[] = { 0x02, 0x01, 0x00, 0x00 };
897 const uint8_t kTestRecord1[] = { 0x00 };
898 const uint8_t kTestRecord2[] = {
899 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
900 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
901 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
902 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
903 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
906 const bt_bdaddr_t kTestAddress = {
907 { 0x01, 0x02, 0x03, 0x0A, 0x0B, 0x0C }
909 const char kTestAddressStr[] = "01:02:03:0A:0B:0C";
910 const int kTestRssi = 64;
912 // Scan wasn't started. Result should be ignored.
913 fake_hal_gatt_iface_->NotifyScanResultCallback(
914 kTestAddress, kTestRssi, (uint8_t*) kTestRecord0);
915 EXPECT_EQ(0, delegate.scan_result_count());
917 // Start a scan session for |le_client_|.
918 EXPECT_CALL(mock_adapter_, IsEnabled())
920 .WillOnce(Return(true));
921 EXPECT_CALL(*mock_handler_, Scan(_))
923 .WillOnce(Return(BT_STATUS_SUCCESS))
924 .WillOnce(Return(BT_STATUS_SUCCESS));
925 ScanSettings settings;
926 std::vector<ScanFilter> filters;
927 ASSERT_TRUE(le_client_->StartScan(settings, filters));
929 fake_hal_gatt_iface_->NotifyScanResultCallback(
930 kTestAddress, kTestRssi, (uint8_t*) kTestRecord0);
931 EXPECT_EQ(1, delegate.scan_result_count());
932 EXPECT_EQ(kTestAddressStr, delegate.last_scan_result().device_address());
933 EXPECT_EQ(kTestRssi, delegate.last_scan_result().rssi());
934 EXPECT_EQ(3U, delegate.last_scan_result().scan_record().size());
936 fake_hal_gatt_iface_->NotifyScanResultCallback(
937 kTestAddress, kTestRssi, (uint8_t*) kTestRecord1);
938 EXPECT_EQ(2, delegate.scan_result_count());
939 EXPECT_EQ(kTestAddressStr, delegate.last_scan_result().device_address());
940 EXPECT_EQ(kTestRssi, delegate.last_scan_result().rssi());
941 EXPECT_TRUE(delegate.last_scan_result().scan_record().empty());
943 fake_hal_gatt_iface_->NotifyScanResultCallback(
944 kTestAddress, kTestRssi, (uint8_t*) kTestRecord2);
945 EXPECT_EQ(3, delegate.scan_result_count());
946 EXPECT_EQ(kTestAddressStr, delegate.last_scan_result().device_address());
947 EXPECT_EQ(kTestRssi, delegate.last_scan_result().rssi());
948 EXPECT_EQ(62U, delegate.last_scan_result().scan_record().size());
950 le_client_->SetDelegate(nullptr);
953 MATCHER_P(BitEq, x, std::string(negation ? "isn't" : "is") +
954 " bitwise equal to " + ::testing::PrintToString(x)) {
955 static_assert(sizeof(x) == sizeof(arg), "Size mismatch");
956 return std::memcmp(&arg, &x, sizeof(x)) == 0;
959 TEST_F(LowEnergyClientPostRegisterTest, Connect) {
960 const bt_bdaddr_t kTestAddress = {
961 { 0x01, 0x02, 0x03, 0x0A, 0x0B, 0x0C }
963 const char kTestAddressStr[] = "01:02:03:0A:0B:0C";
964 const bool kTestDirect = false;
965 const int connId = 12;
967 TestDelegate delegate;
968 le_client_->SetDelegate(&delegate);
970 // TODO(jpawlowski): NotifyConnectCallback should be called after returning
971 // success, fix it when it becomes important.
972 // These should succeed and result in a HAL call
973 EXPECT_CALL(*mock_handler_, Connect(le_client_->GetInstanceId(),
974 Pointee(BitEq(kTestAddress)), kTestDirect, BT_TRANSPORT_LE))
977 Invoke([&](int client_id, const bt_bdaddr_t *bd_addr, bool is_direct,
979 fake_hal_gatt_iface_->NotifyConnectCallback(connId, BT_STATUS_SUCCESS,
980 client_id, *bd_addr);
982 Return(BT_STATUS_SUCCESS)));
984 EXPECT_TRUE(le_client_->Connect(kTestAddressStr, kTestDirect));
985 EXPECT_EQ(1, delegate.connection_state_count());
987 // TODO(jpawlowski): same as above
988 // These should succeed and result in a HAL call
989 EXPECT_CALL(*mock_handler_, Disconnect(le_client_->GetInstanceId(),
990 Pointee(BitEq(kTestAddress)), connId))
993 Invoke([&](int client_id, const bt_bdaddr_t *bd_addr, int connId){
994 fake_hal_gatt_iface_->NotifyDisconnectCallback(connId,
996 client_id, *bd_addr);
998 Return(BT_STATUS_SUCCESS)));
1000 EXPECT_TRUE(le_client_->Disconnect(kTestAddressStr));
1001 EXPECT_EQ(2, delegate.connection_state_count());
1003 le_client_->SetDelegate(nullptr);
1004 ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
1009 } // namespace bluetooth