low_energy_advertiser_unittest.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. //
  2. // Copyright 2016 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. #include <base/macros.h>
  17. #include <gmock/gmock.h>
  18. #include <gtest/gtest.h>
  19. #include "service/adapter.h"
  20. #include "service/hal/fake_bluetooth_gatt_interface.h"
  21. #include "service/low_energy_advertiser.h"
  22. #include "stack/include/bt_types.h"
  23. #include "stack/include/hcidefs.h"
  24. #include "test/mock_adapter.h"
  25. using ::testing::_;
  26. using ::testing::DoAll;
  27. using ::testing::Exactly;
  28. using ::testing::Invoke;
  29. using ::testing::Pointee;
  30. using ::testing::Return;
  31. using ::testing::SaveArg;
  32. using ::testing::Matcher;
  33. using status_cb = base::Callback<void(uint8_t)>;
  34. using reg_cb =
  35. base::Callback<void(uint8_t /* advertiser_id */, uint8_t /* status */)>;
  36. namespace bluetooth {
  37. namespace {
  38. class MockAdvertiserHandler : public BleAdvertiserInterface {
  39. public:
  40. MockAdvertiserHandler() {}
  41. ~MockAdvertiserHandler() override = default;
  42. MOCK_METHOD1(RegisterAdvertiser, void(IdStatusCallback));
  43. MOCK_METHOD1(Unregister, void(uint8_t));
  44. MOCK_METHOD2(GetOwnAddress, void(uint8_t, GetAddressCallback));
  45. MOCK_METHOD3(SetParameters,
  46. void(uint8_t, AdvertiseParameters, ParametersCallback));
  47. MOCK_METHOD4(SetData, void(int, bool, std::vector<uint8_t>, StatusCallback));
  48. MOCK_METHOD6(Enable, void(uint8_t, bool, StatusCallback, uint16_t, uint8_t,
  49. StatusCallback));
  50. MOCK_METHOD7(StartAdvertising,
  51. void(uint8_t advertiser_id, StatusCallback cb,
  52. AdvertiseParameters, std::vector<uint8_t>,
  53. std::vector<uint8_t>, int, StatusCallback));
  54. MOCK_METHOD9(StartAdvertisingSet,
  55. void(IdTxPowerStatusCallback cb, AdvertiseParameters params,
  56. std::vector<uint8_t> advertise_data,
  57. std::vector<uint8_t> scan_response_data,
  58. PeriodicAdvertisingParameters periodic_params,
  59. std::vector<uint8_t> periodic_data, uint16_t duration,
  60. uint8_t maxExtAdvEvents, IdStatusCallback timeout_cb));
  61. MOCK_METHOD3(SetPeriodicAdvertisingParameters,
  62. void(int, PeriodicAdvertisingParameters, StatusCallback));
  63. MOCK_METHOD3(SetPeriodicAdvertisingData,
  64. void(int, std::vector<uint8_t>, StatusCallback));
  65. MOCK_METHOD3(SetPeriodicAdvertisingEnable, void(int, bool, StatusCallback));
  66. private:
  67. DISALLOW_COPY_AND_ASSIGN(MockAdvertiserHandler);
  68. };
  69. class LowEnergyAdvertiserTest : public ::testing::Test {
  70. public:
  71. LowEnergyAdvertiserTest() = default;
  72. ~LowEnergyAdvertiserTest() override = default;
  73. void SetUp() override {
  74. // Only set |mock_handler_| if a test hasn't set it.
  75. if (!mock_handler_) mock_handler_.reset(new MockAdvertiserHandler());
  76. hal::BluetoothGattInterface::InitializeForTesting(
  77. new hal::FakeBluetoothGattInterface(
  78. std::static_pointer_cast<BleAdvertiserInterface>(mock_handler_),
  79. nullptr, nullptr, nullptr));
  80. ble_advertiser_factory_.reset(new LowEnergyAdvertiserFactory());
  81. }
  82. void TearDown() override {
  83. ble_advertiser_factory_.reset();
  84. hal::BluetoothGattInterface::CleanUp();
  85. }
  86. protected:
  87. std::shared_ptr<MockAdvertiserHandler> mock_handler_;
  88. std::unique_ptr<LowEnergyAdvertiserFactory> ble_advertiser_factory_;
  89. private:
  90. DISALLOW_COPY_AND_ASSIGN(LowEnergyAdvertiserTest);
  91. };
  92. // Used for tests that operate on a pre-registered advertiser.
  93. class LowEnergyAdvertiserPostRegisterTest : public LowEnergyAdvertiserTest {
  94. public:
  95. LowEnergyAdvertiserPostRegisterTest() : next_client_id_(0) {}
  96. ~LowEnergyAdvertiserPostRegisterTest() override = default;
  97. void SetUp() override {
  98. LowEnergyAdvertiserTest::SetUp();
  99. auto callback = [&](std::unique_ptr<LowEnergyAdvertiser> advertiser) {
  100. le_advertiser_ = std::move(advertiser);
  101. };
  102. RegisterTestAdvertiser(callback);
  103. }
  104. void TearDown() override {
  105. EXPECT_CALL(*mock_handler_, Enable(_, false, _, _, _, _)).Times(1);
  106. EXPECT_CALL(*mock_handler_, Unregister(_)).Times(1);
  107. le_advertiser_.reset();
  108. LowEnergyAdvertiserTest::TearDown();
  109. }
  110. void RegisterTestAdvertiser(
  111. const std::function<void(std::unique_ptr<LowEnergyAdvertiser> advertiser)>
  112. callback) {
  113. Uuid uuid = Uuid::GetRandom();
  114. auto api_callback = [&](BLEStatus status, const Uuid& in_uuid,
  115. std::unique_ptr<BluetoothInstance> in_client) {
  116. CHECK(in_uuid == uuid);
  117. CHECK(in_client.get());
  118. CHECK(status == BLE_STATUS_SUCCESS);
  119. callback(std::unique_ptr<LowEnergyAdvertiser>(
  120. static_cast<LowEnergyAdvertiser*>(in_client.release())));
  121. };
  122. reg_cb reg_adv_cb;
  123. EXPECT_CALL(*mock_handler_, RegisterAdvertiser(_))
  124. .Times(1)
  125. .WillOnce(SaveArg<0>(&reg_adv_cb));
  126. ble_advertiser_factory_->RegisterInstance(uuid, api_callback);
  127. reg_adv_cb.Run(next_client_id_++, BT_STATUS_SUCCESS);
  128. ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
  129. }
  130. void StartAdvertising() {
  131. ASSERT_FALSE(le_advertiser_->IsAdvertisingStarted());
  132. ASSERT_FALSE(le_advertiser_->IsStartingAdvertising());
  133. ASSERT_FALSE(le_advertiser_->IsStoppingAdvertising());
  134. status_cb start_advertising_cb;
  135. EXPECT_CALL(*mock_handler_, StartAdvertising(_, _, _, _, _, _, _))
  136. .Times(1)
  137. .WillOnce(SaveArg<1>(&start_advertising_cb));
  138. status_cb set_data_cb;
  139. AdvertiseSettings settings;
  140. AdvertiseData adv, scan_rsp;
  141. ASSERT_TRUE(le_advertiser_->StartAdvertising(
  142. settings, adv, scan_rsp, LowEnergyAdvertiser::StatusCallback()));
  143. ASSERT_TRUE(le_advertiser_->IsStartingAdvertising());
  144. start_advertising_cb.Run(BT_STATUS_SUCCESS);
  145. ASSERT_TRUE(le_advertiser_->IsAdvertisingStarted());
  146. ASSERT_FALSE(le_advertiser_->IsStartingAdvertising());
  147. ASSERT_FALSE(le_advertiser_->IsStoppingAdvertising());
  148. }
  149. void AdvertiseDataTestHelper(AdvertiseData data,
  150. std::function<void(BLEStatus)> callback,
  151. status_cb* set_data_cb) {
  152. AdvertiseSettings settings;
  153. LOG_ASSERT(set_data_cb) << "set_data_cb must be set";
  154. EXPECT_TRUE(le_advertiser_->StartAdvertising(settings, data,
  155. AdvertiseData(), callback));
  156. set_data_cb->Run(BT_STATUS_SUCCESS);
  157. status_cb disable_cb;
  158. EXPECT_CALL(*mock_handler_, Enable(_, false, _, _, _, _))
  159. .Times(1)
  160. .WillOnce(SaveArg<2>(&disable_cb));
  161. EXPECT_TRUE(
  162. le_advertiser_->StopAdvertising(LowEnergyAdvertiser::StatusCallback()));
  163. disable_cb.Run(BT_STATUS_SUCCESS);
  164. }
  165. protected:
  166. std::unique_ptr<LowEnergyAdvertiser> le_advertiser_;
  167. private:
  168. int next_client_id_;
  169. DISALLOW_COPY_AND_ASSIGN(LowEnergyAdvertiserPostRegisterTest);
  170. };
  171. TEST_F(LowEnergyAdvertiserTest, RegisterInstance) {
  172. // These will be asynchronously populated with a result when the callback
  173. // executes.
  174. BLEStatus status = BLE_STATUS_SUCCESS;
  175. Uuid cb_uuid;
  176. std::unique_ptr<LowEnergyAdvertiser> advertiser;
  177. int callback_count = 0;
  178. auto callback = [&](BLEStatus in_status, const Uuid& uuid,
  179. std::unique_ptr<BluetoothInstance> in_client) {
  180. status = in_status;
  181. cb_uuid = uuid;
  182. advertiser = std::unique_ptr<LowEnergyAdvertiser>(
  183. static_cast<LowEnergyAdvertiser*>(in_client.release()));
  184. callback_count++;
  185. };
  186. Uuid uuid0 = Uuid::GetRandom();
  187. reg_cb reg_adv1_cb;
  188. EXPECT_CALL(*mock_handler_, RegisterAdvertiser(_))
  189. .Times(1)
  190. .WillOnce(SaveArg<0>(&reg_adv1_cb));
  191. // Success.
  192. EXPECT_TRUE(ble_advertiser_factory_->RegisterInstance(uuid0, callback));
  193. EXPECT_EQ(0, callback_count);
  194. // Calling twice with the same Uuid should fail with no additional call into
  195. // the stack.
  196. EXPECT_FALSE(ble_advertiser_factory_->RegisterInstance(uuid0, callback));
  197. ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
  198. // Call with a different Uuid while one is pending.
  199. Uuid uuid1 = Uuid::GetRandom();
  200. reg_cb reg_adv2_cb;
  201. EXPECT_CALL(*mock_handler_, RegisterAdvertiser(_))
  202. .Times(1)
  203. .WillOnce(SaveArg<0>(&reg_adv2_cb));
  204. EXPECT_TRUE(ble_advertiser_factory_->RegisterInstance(uuid1, callback));
  205. // |uuid0| succeeds.
  206. int client_if0 = 2; // Pick something that's not 0.
  207. reg_adv1_cb.Run(client_if0, BT_STATUS_SUCCESS);
  208. EXPECT_EQ(1, callback_count);
  209. ASSERT_TRUE(advertiser.get() !=
  210. nullptr); // Assert to terminate in case of error
  211. EXPECT_EQ(BLE_STATUS_SUCCESS, status);
  212. EXPECT_EQ(client_if0, advertiser->GetInstanceId());
  213. EXPECT_EQ(uuid0, advertiser->GetAppIdentifier());
  214. EXPECT_EQ(uuid0, cb_uuid);
  215. // The advertiser should unregister itself when deleted.
  216. EXPECT_CALL(*mock_handler_, Enable(client_if0, false, _, _, _, _)).Times(1);
  217. EXPECT_CALL(*mock_handler_, Unregister(client_if0)).Times(1);
  218. advertiser.reset();
  219. ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
  220. // |uuid1| fails.
  221. uint8_t client_if1 = 10;
  222. reg_adv2_cb.Run(client_if1, BT_STATUS_FAIL);
  223. EXPECT_EQ(2, callback_count);
  224. ASSERT_TRUE(advertiser.get() ==
  225. nullptr); // Assert to terminate in case of error
  226. EXPECT_EQ(BLE_STATUS_FAILURE, status);
  227. EXPECT_EQ(uuid1, cb_uuid);
  228. }
  229. TEST_F(LowEnergyAdvertiserPostRegisterTest, StartAdvertisingBasic) {
  230. EXPECT_FALSE(le_advertiser_->IsAdvertisingStarted());
  231. EXPECT_FALSE(le_advertiser_->IsStartingAdvertising());
  232. EXPECT_FALSE(le_advertiser_->IsStoppingAdvertising());
  233. // Use default advertising settings and data.
  234. AdvertiseSettings settings;
  235. AdvertiseData adv_data, scan_rsp;
  236. int callback_count = 0;
  237. BLEStatus last_status = BLE_STATUS_FAILURE;
  238. auto callback = [&](BLEStatus status) {
  239. last_status = status;
  240. callback_count++;
  241. };
  242. status_cb start_advertising_cb;
  243. EXPECT_CALL(*mock_handler_, StartAdvertising(_, _, _, _, _, _, _))
  244. .Times(2)
  245. .WillRepeatedly(SaveArg<1>(&start_advertising_cb));
  246. // Stack call returns success.
  247. EXPECT_TRUE(
  248. le_advertiser_->StartAdvertising(settings, adv_data, scan_rsp, callback));
  249. EXPECT_FALSE(le_advertiser_->IsAdvertisingStarted());
  250. EXPECT_TRUE(le_advertiser_->IsStartingAdvertising());
  251. EXPECT_FALSE(le_advertiser_->IsStoppingAdvertising());
  252. EXPECT_EQ(0, callback_count);
  253. // Already starting.
  254. EXPECT_FALSE(
  255. le_advertiser_->StartAdvertising(settings, adv_data, scan_rsp, callback));
  256. // Notify failure.
  257. start_advertising_cb.Run(BT_STATUS_FAIL);
  258. EXPECT_FALSE(le_advertiser_->IsAdvertisingStarted());
  259. EXPECT_FALSE(le_advertiser_->IsStartingAdvertising());
  260. EXPECT_FALSE(le_advertiser_->IsStoppingAdvertising());
  261. EXPECT_EQ(1, callback_count);
  262. EXPECT_EQ(BLE_STATUS_FAILURE, last_status);
  263. // Try again.
  264. EXPECT_TRUE(
  265. le_advertiser_->StartAdvertising(settings, adv_data, scan_rsp, callback));
  266. EXPECT_FALSE(le_advertiser_->IsAdvertisingStarted());
  267. EXPECT_TRUE(le_advertiser_->IsStartingAdvertising());
  268. EXPECT_FALSE(le_advertiser_->IsStoppingAdvertising());
  269. EXPECT_EQ(1, callback_count);
  270. start_advertising_cb.Run(BT_STATUS_SUCCESS);
  271. EXPECT_TRUE(le_advertiser_->IsAdvertisingStarted());
  272. EXPECT_FALSE(le_advertiser_->IsStartingAdvertising());
  273. EXPECT_FALSE(le_advertiser_->IsStoppingAdvertising());
  274. EXPECT_EQ(2, callback_count);
  275. EXPECT_EQ(BLE_STATUS_SUCCESS, last_status);
  276. // Already started.
  277. EXPECT_FALSE(
  278. le_advertiser_->StartAdvertising(settings, adv_data, scan_rsp, callback));
  279. }
  280. TEST_F(LowEnergyAdvertiserPostRegisterTest, StopAdvertisingBasic) {
  281. AdvertiseSettings settings;
  282. // Not enabled.
  283. EXPECT_FALSE(le_advertiser_->IsAdvertisingStarted());
  284. EXPECT_FALSE(
  285. le_advertiser_->StopAdvertising(LowEnergyAdvertiser::StatusCallback()));
  286. // Start advertising for testing.
  287. StartAdvertising();
  288. int callback_count = 0;
  289. BLEStatus last_status = BLE_STATUS_FAILURE;
  290. auto callback = [&](BLEStatus status) {
  291. last_status = status;
  292. callback_count++;
  293. };
  294. status_cb enable_cb;
  295. EXPECT_CALL(*mock_handler_, Enable(_, false, _, _, _, _))
  296. .Times(2)
  297. .WillRepeatedly(SaveArg<2>(&enable_cb));
  298. // Stack returns success.
  299. EXPECT_TRUE(le_advertiser_->StopAdvertising(callback));
  300. EXPECT_TRUE(le_advertiser_->IsAdvertisingStarted());
  301. EXPECT_FALSE(le_advertiser_->IsStartingAdvertising());
  302. EXPECT_TRUE(le_advertiser_->IsStoppingAdvertising());
  303. EXPECT_EQ(0, callback_count);
  304. // Already disabling.
  305. EXPECT_FALSE(le_advertiser_->StopAdvertising(callback));
  306. EXPECT_TRUE(le_advertiser_->IsAdvertisingStarted());
  307. EXPECT_FALSE(le_advertiser_->IsStartingAdvertising());
  308. EXPECT_TRUE(le_advertiser_->IsStoppingAdvertising());
  309. EXPECT_EQ(0, callback_count);
  310. // Notify failure.
  311. enable_cb.Run(BT_STATUS_FAIL);
  312. EXPECT_TRUE(le_advertiser_->IsAdvertisingStarted());
  313. EXPECT_FALSE(le_advertiser_->IsStartingAdvertising());
  314. EXPECT_FALSE(le_advertiser_->IsStoppingAdvertising());
  315. EXPECT_EQ(1, callback_count);
  316. EXPECT_EQ(BLE_STATUS_FAILURE, last_status);
  317. // Try again.
  318. EXPECT_TRUE(le_advertiser_->StopAdvertising(callback));
  319. EXPECT_TRUE(le_advertiser_->IsAdvertisingStarted());
  320. EXPECT_FALSE(le_advertiser_->IsStartingAdvertising());
  321. EXPECT_TRUE(le_advertiser_->IsStoppingAdvertising());
  322. EXPECT_EQ(1, callback_count);
  323. // Notify success.
  324. enable_cb.Run(BT_STATUS_SUCCESS);
  325. EXPECT_FALSE(le_advertiser_->IsAdvertisingStarted());
  326. EXPECT_FALSE(le_advertiser_->IsStartingAdvertising());
  327. EXPECT_FALSE(le_advertiser_->IsStoppingAdvertising());
  328. EXPECT_EQ(2, callback_count);
  329. EXPECT_EQ(BLE_STATUS_SUCCESS, last_status);
  330. // Already stopped.
  331. EXPECT_FALSE(le_advertiser_->StopAdvertising(callback));
  332. }
  333. TEST_F(LowEnergyAdvertiserPostRegisterTest, InvalidAdvertiseData) {
  334. const std::vector<uint8_t> data0{0x02, HCI_EIR_FLAGS_TYPE, 0x00};
  335. const std::vector<uint8_t> data1{0x04, HCI_EIR_MANUFACTURER_SPECIFIC_TYPE,
  336. 0x01, 0x02, 0x00};
  337. AdvertiseData invalid_adv(data0);
  338. AdvertiseData valid_adv(data1);
  339. AdvertiseSettings settings;
  340. EXPECT_FALSE(le_advertiser_->StartAdvertising(
  341. settings, valid_adv, invalid_adv, LowEnergyAdvertiser::StatusCallback()));
  342. EXPECT_FALSE(le_advertiser_->StartAdvertising(
  343. settings, invalid_adv, valid_adv, LowEnergyAdvertiser::StatusCallback()));
  344. // Manufacturer data not correctly formatted according to spec. We let the
  345. // stack handle this case.
  346. const std::vector<uint8_t> data2{0x01, HCI_EIR_MANUFACTURER_SPECIFIC_TYPE};
  347. AdvertiseData invalid_mfc(data2);
  348. EXPECT_CALL(*mock_handler_, StartAdvertising(_, _, _, _, _, _, _)).Times(1);
  349. EXPECT_TRUE(le_advertiser_->StartAdvertising(
  350. settings, invalid_mfc, valid_adv, LowEnergyAdvertiser::StatusCallback()));
  351. }
  352. TEST_F(LowEnergyAdvertiserPostRegisterTest, AdvertiseDataParsing) {
  353. const std::vector<uint8_t> kUuid16BitData{
  354. 0x03, HCI_EIR_COMPLETE_16BITS_UUID_TYPE, 0xDE, 0xAD,
  355. };
  356. const std::vector<uint8_t> kUuid32BitData{
  357. 0x05, HCI_EIR_COMPLETE_32BITS_UUID_TYPE, 0xDE, 0xAD, 0x01, 0x02};
  358. const std::vector<uint8_t> kUUID128BitData{
  359. 0x11, HCI_EIR_COMPLETE_128BITS_UUID_TYPE,
  360. 0xDE, 0xAD,
  361. 0x01, 0x02,
  362. 0x03, 0x04,
  363. 0x05, 0x06,
  364. 0x07, 0x08,
  365. 0x09, 0x0A,
  366. 0x0B, 0x0C,
  367. 0x0D, 0x0E};
  368. const std::vector<uint8_t> kMultiUuidData{
  369. 0x11, HCI_EIR_COMPLETE_128BITS_UUID_TYPE,
  370. 0xDE, 0xAD,
  371. 0x01, 0x02,
  372. 0x03, 0x04,
  373. 0x05, 0x06,
  374. 0x07, 0x08,
  375. 0x09, 0x0A,
  376. 0x0B, 0x0C,
  377. 0x0D, 0x0E,
  378. 0x05, HCI_EIR_COMPLETE_32BITS_UUID_TYPE,
  379. 0xDE, 0xAD,
  380. 0xBE, 0xEF};
  381. const std::vector<uint8_t> kServiceData16Bit{
  382. 0x05, HCI_EIR_SERVICE_DATA_16BITS_UUID_TYPE, 0xDE, 0xAD, 0xBE, 0xEF};
  383. const std::vector<uint8_t> kServiceData32Bit{
  384. 0x07, HCI_EIR_SERVICE_DATA_32BITS_UUID_TYPE, 0xDE, 0xAD, 0x01, 0x02, 0xBE,
  385. 0xEF};
  386. const std::vector<uint8_t> kServiceData128Bit{
  387. 0x13, HCI_EIR_SERVICE_DATA_128BITS_UUID_TYPE,
  388. 0xDE, 0xAD,
  389. 0x01, 0x02,
  390. 0x03, 0x04,
  391. 0x05, 0x06,
  392. 0x07, 0x08,
  393. 0x09, 0x0A,
  394. 0x0B, 0x0C,
  395. 0x0D, 0x0E,
  396. 0xBE, 0xEF};
  397. const std::vector<uint8_t> kMultiServiceData{
  398. 0x13, HCI_EIR_SERVICE_DATA_128BITS_UUID_TYPE,
  399. 0xDE, 0xAD,
  400. 0x01, 0x02,
  401. 0x03, 0x04,
  402. 0x05, 0x06,
  403. 0xBE, 0xEF,
  404. 0xDE, 0xAD,
  405. 0x01, 0x02,
  406. 0x03, 0x04,
  407. 0x05, 0x06,
  408. 0x05, HCI_EIR_SERVICE_DATA_16BITS_UUID_TYPE,
  409. 0xDE, 0xAD,
  410. 0xBE, 0xEF};
  411. const std::vector<uint8_t> kServiceUuidMatch{
  412. 0x05, HCI_EIR_COMPLETE_32BITS_UUID_TYPE,
  413. 0xDE, 0xAD,
  414. 0x01, 0x02,
  415. 0x07, HCI_EIR_SERVICE_DATA_32BITS_UUID_TYPE,
  416. 0xDE, 0xAD,
  417. 0x01, 0x02,
  418. 0xBE, 0xEF};
  419. const std::vector<uint8_t> kServiceUuidMismatch{
  420. 0x05, HCI_EIR_COMPLETE_32BITS_UUID_TYPE,
  421. 0xDE, 0xAD,
  422. 0x01, 0x01,
  423. 0x07, HCI_EIR_SERVICE_DATA_32BITS_UUID_TYPE,
  424. 0xDE, 0xAD,
  425. 0x01, 0x02,
  426. 0xBE, 0xEF};
  427. AdvertiseData uuid_16bit_adv(kUuid16BitData);
  428. AdvertiseData uuid_32bit_adv(kUuid32BitData);
  429. AdvertiseData uuid_128bit_adv(kUUID128BitData);
  430. AdvertiseData multi_uuid_adv(kMultiUuidData);
  431. AdvertiseData service_16bit_adv(kServiceData16Bit);
  432. AdvertiseData service_32bit_adv(kServiceData32Bit);
  433. AdvertiseData service_128bit_adv(kServiceData128Bit);
  434. AdvertiseData multi_service_adv(kMultiServiceData);
  435. AdvertiseData service_uuid_match(kServiceUuidMatch);
  436. AdvertiseData service_uuid_mismatch(kServiceUuidMismatch);
  437. AdvertiseSettings settings;
  438. int callback_count = 0;
  439. BLEStatus last_status = BLE_STATUS_FAILURE;
  440. auto callback = [&](BLEStatus status) {
  441. last_status = status;
  442. callback_count++;
  443. };
  444. status_cb start_advertising_cb;
  445. // Multiple Uuid test
  446. EXPECT_CALL(*mock_handler_, StartAdvertising(_, _, _, _, _, _, _))
  447. .Times(1)
  448. .WillOnce(SaveArg<1>(&start_advertising_cb));
  449. AdvertiseDataTestHelper(multi_uuid_adv, callback, &start_advertising_cb);
  450. EXPECT_EQ(1, callback_count);
  451. ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
  452. // Multiple Service Data test
  453. EXPECT_CALL(*mock_handler_, StartAdvertising(_, _, _, _, _, _, _))
  454. .Times(1)
  455. .WillOnce(SaveArg<1>(&start_advertising_cb));
  456. AdvertiseDataTestHelper(multi_service_adv, callback, &start_advertising_cb);
  457. EXPECT_EQ(2, callback_count);
  458. ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
  459. // 16bit uuid test, should succeed with correctly parsed uuid in little-endian
  460. // 128-bit format.
  461. const std::vector<uint8_t> uuid_16bit_canonical{
  462. 0xFB, 0x34, 0x9b, 0x5F, 0x80, 0x00, 0x00, 0x80,
  463. 0x00, 0x10, 0x00, 0x00, 0xDE, 0xAD, 0x00, 0x00};
  464. EXPECT_CALL(*mock_handler_, StartAdvertising(_, _, _, _, _, _, _))
  465. .Times(1)
  466. .WillOnce(SaveArg<1>(&start_advertising_cb));
  467. AdvertiseDataTestHelper(uuid_16bit_adv, callback, &start_advertising_cb);
  468. EXPECT_EQ(3, callback_count);
  469. ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
  470. // 32bit uuid test, should succeed with correctly parsed uuid
  471. const std::vector<uint8_t> uuid_32bit_canonical{
  472. 0xFB, 0x34, 0x9b, 0x5F, 0x80, 0x00, 0x00, 0x80,
  473. 0x00, 0x10, 0x00, 0x00, 0xDE, 0xAD, 0x01, 0x02};
  474. EXPECT_CALL(*mock_handler_, StartAdvertising(_, _, _, _, _, _, _))
  475. .Times(1)
  476. .WillOnce(SaveArg<1>(&start_advertising_cb));
  477. AdvertiseDataTestHelper(uuid_32bit_adv, callback, &start_advertising_cb);
  478. EXPECT_EQ(4, callback_count);
  479. ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
  480. // 128bit uuid test, should succeed with correctly parsed uuid
  481. const std::vector<uint8_t> uuid_128bit{0xDE, 0xAD, 0x01, 0x02, 0x03, 0x04,
  482. 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
  483. 0x0B, 0x0C, 0x0D, 0x0E};
  484. EXPECT_CALL(*mock_handler_, StartAdvertising(_, _, _, _, _, _, _))
  485. .Times(1)
  486. .WillOnce(SaveArg<1>(&start_advertising_cb));
  487. AdvertiseDataTestHelper(uuid_128bit_adv, callback, &start_advertising_cb);
  488. EXPECT_EQ(5, callback_count);
  489. ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
  490. const std::vector<uint8_t> service_data{0xBE, 0xEF};
  491. // Service data with 16bit uuid included, should succeed with
  492. // uuid and service data parsed out
  493. EXPECT_CALL(*mock_handler_, StartAdvertising(_, _, _, _, _, _, _))
  494. .Times(1)
  495. .WillOnce(SaveArg<1>(&start_advertising_cb));
  496. AdvertiseDataTestHelper(service_16bit_adv, callback, &start_advertising_cb);
  497. EXPECT_EQ(6, callback_count);
  498. ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
  499. // Service data with 32bit uuid included, should succeed with
  500. // uuid and service data parsed out
  501. EXPECT_CALL(*mock_handler_, StartAdvertising(_, _, _, _, _, _, _))
  502. .Times(1)
  503. .WillOnce(SaveArg<1>(&start_advertising_cb));
  504. AdvertiseDataTestHelper(service_32bit_adv, callback, &start_advertising_cb);
  505. EXPECT_EQ(7, callback_count);
  506. ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
  507. // Service data with 128bit uuid included, should succeed with
  508. // uuid and service data parsed out
  509. EXPECT_CALL(*mock_handler_, StartAdvertising(_, _, _, _, _, _, _))
  510. .Times(1)
  511. .WillOnce(SaveArg<1>(&start_advertising_cb));
  512. AdvertiseDataTestHelper(service_128bit_adv, callback, &start_advertising_cb);
  513. EXPECT_EQ(8, callback_count);
  514. ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
  515. // Service data and Uuid where the Uuid for both match, should succeed.
  516. EXPECT_CALL(*mock_handler_, StartAdvertising(_, _, _, _, _, _, _))
  517. .Times(1)
  518. .WillOnce(SaveArg<1>(&start_advertising_cb));
  519. AdvertiseDataTestHelper(service_uuid_match, callback, &start_advertising_cb);
  520. EXPECT_EQ(9, callback_count);
  521. ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
  522. // Service data and Uuid where the Uuid for dont match, should fail
  523. EXPECT_CALL(*mock_handler_, StartAdvertising(_, _, _, _, _, _, _))
  524. .Times(1)
  525. .WillOnce(SaveArg<1>(&start_advertising_cb));
  526. AdvertiseDataTestHelper(service_uuid_match, callback, &start_advertising_cb);
  527. EXPECT_EQ(10, callback_count);
  528. ::testing::Mock::VerifyAndClearExpectations(mock_handler_.get());
  529. }
  530. MATCHER_P(BitEq, x, std::string(negation ? "isn't" : "is") +
  531. " bitwise equal to " + ::testing::PrintToString(x)) {
  532. static_assert(sizeof(x) == sizeof(arg), "Size mismatch");
  533. return std::memcmp(&arg, &x, sizeof(x)) == 0;
  534. }
  535. } // namespace
  536. } // namespace bluetooth