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