• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 2016 The Android Open Source Project
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #include <array>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 
23 #include "device/include/controller.h"
24 #include "stack/btm/ble_advertiser_hci_interface.h"
25 #include "stack/include/ble_advertiser.h"
26 
27 using ::testing::Args;
28 using ::testing::Contains;
29 using ::testing::DoAll;
30 using ::testing::ElementsAreArray;
31 using ::testing::Exactly;
32 using ::testing::Field;
33 using ::testing::IsEmpty;
34 using ::testing::SaveArg;
35 using ::testing::SizeIs;
36 using ::testing::_;
37 using base::Bind;
38 using status_cb = BleAdvertiserHciInterface::status_cb;
39 using parameters_cb = BleAdvertiserHciInterface::parameters_cb;
40 using SetEnableData = BleAdvertiserHciInterface::SetEnableData;
41 
42 const int num_adv_instances = 16;
43 
44 /* Below are methods that must be implemented if we don't want to compile the
45  * whole stack. They will be removed, or changed into mocks one by one in the
46  * future, as the refactoring progresses */
BTM_BleLocalPrivacyEnabled()47 bool BTM_BleLocalPrivacyEnabled() { return true; }
btm_acl_update_conn_addr(uint16_t conn_handle,const RawAddress & address)48 void btm_acl_update_conn_addr(uint16_t conn_handle, const RawAddress& address) {
49 }
btm_gen_resolvable_private_addr(base::Callback<void (const RawAddress & rpa)> cb)50 void btm_gen_resolvable_private_addr(
51     base::Callback<void(const RawAddress& rpa)> cb) {
52   cb.Run(RawAddress::kEmpty);
53 }
54 
55 alarm_callback_t last_alarm_cb = nullptr;
56 void* last_alarm_data = nullptr;
alarm_set_on_mloop(alarm_t * alarm,uint64_t interval_ms,alarm_callback_t cb,void * data)57 void alarm_set_on_mloop(alarm_t* alarm, uint64_t interval_ms,
58                         alarm_callback_t cb, void* data) {
59   last_alarm_cb = cb;
60   last_alarm_data = data;
61 }
62 
alarm_cancel(alarm_t * alarm)63 void alarm_cancel(alarm_t* alarm) {}
alarm_new_periodic(const char * name)64 alarm_t* alarm_new_periodic(const char* name) { return nullptr; }
alarm_new(const char * name)65 alarm_t* alarm_new(const char* name) { return nullptr; }
alarm_free(alarm_t * alarm)66 void alarm_free(alarm_t* alarm) {}
controller_get_interface()67 const controller_t* controller_get_interface() { return nullptr; }
68 
btm_get_next_private_addrress_interval_ms()69 uint64_t btm_get_next_private_addrress_interval_ms() { return 15 * 60 * 1000; }
70 
71 namespace {
DoNothing(uint8_t)72 void DoNothing(uint8_t) {}
73 
DoNothing2(uint8_t,uint8_t)74 void DoNothing2(uint8_t, uint8_t) {}
75 
TriggerRandomAddressUpdate()76 void TriggerRandomAddressUpdate() {
77   // Call to StartAdvertisingSet set the last_alarm_cb to random address timeout
78   // callback. Call it now in order to trigger address update
79   last_alarm_cb(last_alarm_data);
80 }
81 
82 constexpr uint8_t INTERMEDIATE =
83     0x00;                           // Intermediate fragment of fragmented data
84 constexpr uint8_t FIRST = 0x01;     // First fragment of fragmented data
85 constexpr uint8_t LAST = 0x02;      // Last fragment of fragmented data
86 constexpr uint8_t COMPLETE = 0x03;  // Complete extended advertising data
87 
88 class AdvertiserHciMock : public BleAdvertiserHciInterface {
89  public:
90   AdvertiserHciMock() = default;
91   ~AdvertiserHciMock() override = default;
92 
93   MOCK_METHOD1(ReadInstanceCount,
94                void(base::Callback<void(uint8_t /* inst_cnt*/)>));
95   MOCK_METHOD1(SetAdvertisingEventObserver,
96                void(AdvertisingEventObserver* observer));
97   MOCK_METHOD6(SetAdvertisingData,
98                void(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t*, status_cb));
99   MOCK_METHOD6(SetScanResponseData,
100                void(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t*, status_cb));
101   MOCK_METHOD3(SetRandomAddress, void(uint8_t, const RawAddress&, status_cb));
102   MOCK_METHOD3(Enable, void(uint8_t, std::vector<SetEnableData>, status_cb));
103   MOCK_METHOD5(SetPeriodicAdvertisingParameters,
104                void(uint8_t, uint16_t, uint16_t, uint16_t, status_cb));
105   MOCK_METHOD5(SetPeriodicAdvertisingData,
106                void(uint8_t, uint8_t, uint8_t, uint8_t*, status_cb));
107   MOCK_METHOD3(SetPeriodicAdvertisingEnable, void(uint8_t, uint8_t, status_cb));
108   MOCK_METHOD2(RemoveAdvertisingSet, void(uint8_t, status_cb));
109   MOCK_METHOD1(ClearAdvertisingSets, void(status_cb));
110 
111   MOCK_METHOD9(SetParameters1,
112                void(uint8_t, uint16_t, uint32_t, uint32_t, uint8_t, uint8_t,
113                     const RawAddress&, uint8_t, const RawAddress&));
114   MOCK_METHOD8(SetParameters2, void(uint8_t, int8_t, uint8_t, uint8_t, uint8_t,
115                                     uint8_t, uint8_t, parameters_cb));
116 
SetParameters(uint8_t handle,uint16_t properties,uint32_t adv_int_min,uint32_t adv_int_max,uint8_t channel_map,uint8_t own_address_type,const RawAddress & own_address,uint8_t peer_address_type,const RawAddress & peer_address,uint8_t filter_policy,int8_t tx_power,uint8_t primary_phy,uint8_t secondary_max_skip,uint8_t secondary_phy,uint8_t advertising_sid,uint8_t scan_request_notify_enable,parameters_cb cmd_complete)117   void SetParameters(uint8_t handle, uint16_t properties, uint32_t adv_int_min,
118                      uint32_t adv_int_max, uint8_t channel_map,
119                      uint8_t own_address_type, const RawAddress& own_address,
120                      uint8_t peer_address_type, const RawAddress& peer_address,
121                      uint8_t filter_policy, int8_t tx_power,
122                      uint8_t primary_phy, uint8_t secondary_max_skip,
123                      uint8_t secondary_phy, uint8_t advertising_sid,
124                      uint8_t scan_request_notify_enable,
125                      parameters_cb cmd_complete) override {
126     SetParameters1(handle, properties, adv_int_min, adv_int_max, channel_map,
127                    own_address_type, own_address, peer_address_type,
128                    peer_address);
129     SetParameters2(filter_policy, tx_power, primary_phy, secondary_max_skip,
130                    secondary_phy, advertising_sid, scan_request_notify_enable,
131                    cmd_complete);
132   };
133 
QuirkAdvertiserZeroHandle()134   bool QuirkAdvertiserZeroHandle() override { return false; }
135 
136  private:
137   DISALLOW_COPY_AND_ASSIGN(AdvertiserHciMock);
138 };
139 
140 }  // namespace
141 
142 class BleAdvertisingManagerTest : public testing::Test {
143  protected:
144   int reg_inst_id = -1;
145   int reg_status = -1;
146   int set_params_status = -1;
147   int set_data_status = -1;
148   int enable_status = -1;
149   int start_advertising_status = -1;
150   int start_advertising_set_advertiser_id = -1;
151   int start_advertising_set_tx_power = -1;
152   int start_advertising_set_status = -1;
153 
154   std::unique_ptr<AdvertiserHciMock> hci_mock;
155 
SetUp()156   void SetUp() override {
157     hci_mock.reset(new AdvertiserHciMock());
158 
159     base::Callback<void(uint8_t)> inst_cnt_Cb;
160     EXPECT_CALL(*hci_mock, ReadInstanceCount(_))
161         .Times(Exactly(1))
162         .WillOnce(SaveArg<0>(&inst_cnt_Cb));
163 
164     BleAdvertisingManager::Initialize(hci_mock.get());
165     ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
166 
167     // we are a truly gracious fake controller, let the command succeed!
168     inst_cnt_Cb.Run(num_adv_instances);
169   }
170 
TearDown()171   void TearDown() override {
172     BleAdvertisingManager::CleanUp();
173     hci_mock.reset();
174   }
175 
176  public:
RegistrationCb(uint8_t inst_id,uint8_t status)177   void RegistrationCb(uint8_t inst_id, uint8_t status) {
178     reg_inst_id = inst_id;
179     reg_status = status;
180   }
181 
SetParametersCb(uint8_t status,int8_t tx_power)182   void SetParametersCb(uint8_t status, int8_t tx_power) {
183     set_params_status = status;
184   }
SetDataCb(uint8_t status)185   void SetDataCb(uint8_t status) { set_data_status = status; }
EnableCb(uint8_t status)186   void EnableCb(uint8_t status) { enable_status = status; }
StartAdvertisingCb(uint8_t status)187   void StartAdvertisingCb(uint8_t status) { start_advertising_status = status; }
StartAdvertisingSetCb(uint8_t advertiser_id,int8_t tx_power,uint8_t status)188   void StartAdvertisingSetCb(uint8_t advertiser_id, int8_t tx_power,
189                              uint8_t status) {
190     start_advertising_set_advertiser_id = advertiser_id;
191     start_advertising_set_tx_power = tx_power;
192     start_advertising_set_status = status;
193   }
194 };
195 
TEST_F(BleAdvertisingManagerTest,test_registration)196 TEST_F(BleAdvertisingManagerTest, test_registration) {
197   for (int i = 0; i < num_adv_instances; i++) {
198     BleAdvertisingManager::Get()->RegisterAdvertiser(Bind(
199         &BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
200     EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
201     EXPECT_EQ(i, reg_inst_id);
202   }
203 
204   // This call should return an error - no more advertisers left.
205   BleAdvertisingManager::Get()->RegisterAdvertiser(
206       Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
207   EXPECT_EQ(ADVERTISE_FAILED_TOO_MANY_ADVERTISERS, reg_status);
208   // Don't bother checking inst_id, it doesn't matter
209 
210   status_cb remove_cb;
211   EXPECT_CALL(*hci_mock, RemoveAdvertisingSet(_, _))
212       .Times(1)
213       .WillOnce(SaveArg<1>(&remove_cb));
214   BleAdvertisingManager::Get()->Unregister(5);
215   remove_cb.Run(0);
216 
217   // One advertiser was freed, so should be able to register one now
218   BleAdvertisingManager::Get()->RegisterAdvertiser(
219       Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
220   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
221   EXPECT_EQ(5, reg_inst_id);
222 }
223 
224 /* This test verifies that the following flow is working correctly: register,
225  * set parameters, set data, enable, ... (advertise) ..., unregister*/
TEST_F(BleAdvertisingManagerTest,test_android_flow)226 TEST_F(BleAdvertisingManagerTest, test_android_flow) {
227   BleAdvertisingManager::Get()->RegisterAdvertiser(
228       Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
229   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
230   int advertiser_id = reg_inst_id;
231 
232   parameters_cb set_params_cb;
233   tBTM_BLE_ADV_PARAMS params;
234   EXPECT_CALL(*hci_mock, SetParameters1(advertiser_id, _, _, _, _, _, _, _, _))
235       .Times(1);
236   EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, _, _, _, _, _))
237       .Times(1)
238       .WillOnce(SaveArg<7>(&set_params_cb));
239   BleAdvertisingManager::Get()->SetParameters(
240       advertiser_id, &params,
241       Bind(&BleAdvertisingManagerTest::SetParametersCb,
242            base::Unretained(this)));
243   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
244 
245   // we are a truly gracious fake controller, let the command succeed!
246   set_params_cb.Run(0, 0);
247   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_params_status);
248 
249   status_cb set_data_cb;
250   EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, _, _, _, _, _))
251       .Times(1)
252       .WillOnce(SaveArg<5>(&set_data_cb));
253   BleAdvertisingManager::Get()->SetData(
254       advertiser_id, false, std::vector<uint8_t>(),
255       Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
256   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
257 
258   set_data_cb.Run(0);
259   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
260 
261   status_cb enable_cb;
262   EXPECT_CALL(*hci_mock,
263               Enable(0x01 /* enable */,
264                      AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
265                                                      advertiser_id))),
266                      _))
267       .Times(1)
268       .WillOnce(SaveArg<2>(&enable_cb));
269   BleAdvertisingManager::Get()->Enable(
270       advertiser_id, true,
271       Bind(&BleAdvertisingManagerTest::EnableCb, base::Unretained(this)), 0, 0,
272       base::Callback<void(uint8_t)>());
273   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
274 
275   enable_cb.Run(0);
276   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, enable_status);
277 
278   /* fake controller should be advertising */
279 
280   EXPECT_CALL(*hci_mock,
281               Enable(0x00 /* disable */,
282                      AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
283                                                      advertiser_id))),
284                      _))
285       .Times(1)
286       .WillOnce(SaveArg<2>(&enable_cb));
287   status_cb remove_cb;
288   EXPECT_CALL(*hci_mock, RemoveAdvertisingSet(_, _))
289       .Times(1)
290       .WillOnce(SaveArg<1>(&remove_cb));
291   BleAdvertisingManager::Get()->Unregister(advertiser_id);
292   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
293 
294   enable_cb.Run(0);
295   remove_cb.Run(0);
296 }
297 
298 /* This test verifies that when advertising data is set, tx power and flags will
299  * be properly filled. */
TEST_F(BleAdvertisingManagerTest,test_adv_data_filling)300 TEST_F(BleAdvertisingManagerTest, test_adv_data_filling) {
301   BleAdvertisingManager::Get()->RegisterAdvertiser(
302       Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
303   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
304   int advertiser_id = reg_inst_id;
305 
306   parameters_cb set_params_cb;
307   tBTM_BLE_ADV_PARAMS params;
308   params.advertising_event_properties =
309       BleAdvertisingManager::advertising_prop_legacy_connectable;
310   params.tx_power = -15;
311   EXPECT_CALL(*hci_mock, SetParameters1(advertiser_id, _, _, _, _, _, _, _, _))
312       .Times(1);
313   EXPECT_CALL(*hci_mock, SetParameters2(_, params.tx_power, _, _, _, _, _, _))
314       .Times(1)
315       .WillOnce(SaveArg<7>(&set_params_cb));
316   BleAdvertisingManager::Get()->SetParameters(
317       advertiser_id, &params,
318       Bind(&BleAdvertisingManagerTest::SetParametersCb,
319            base::Unretained(this)));
320   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
321 
322   // let the set parameters command succeed!
323   set_params_cb.Run(0, 0);
324   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_params_status);
325 
326   status_cb set_data_cb;
327   /* verify that flags will be added, and tx power filled, if call to SetData
328    * contained only tx power, and the advertisement is connectable */
329   uint8_t expected_adv_data[] = {
330       0x02 /* len */,         0x01 /* flags */,
331       0x02 /* flags value */, 0x02 /* len */,
332       0x0A /* tx_power */,    static_cast<uint8_t>(params.tx_power)};
333   EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, _, _, _, _, _))
334       .With(Args<4, 3>(ElementsAreArray(expected_adv_data)))
335       .Times(1)
336       .WillOnce(SaveArg<5>(&set_data_cb));
337   BleAdvertisingManager::Get()->SetData(
338       advertiser_id, false,
339       std::vector<uint8_t>({0x02 /* len */, 0x0A /* tx_power */, 0x00}),
340       Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
341   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
342 
343   set_data_cb.Run(0);
344   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
345 }
346 
347 /* This test verifies that when advertising is non-connectable, flags will not
348  * be added. */
TEST_F(BleAdvertisingManagerTest,test_adv_data_not_filling)349 TEST_F(BleAdvertisingManagerTest, test_adv_data_not_filling) {
350   BleAdvertisingManager::Get()->RegisterAdvertiser(
351       Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
352   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
353   int advertiser_id = reg_inst_id;
354 
355   parameters_cb set_params_cb;
356   tBTM_BLE_ADV_PARAMS params;
357   params.advertising_event_properties =
358       BleAdvertisingManager::advertising_prop_legacy_non_connectable;
359   params.tx_power = -15;
360   EXPECT_CALL(*hci_mock, SetParameters1(advertiser_id, _, _, _, _, _, _, _, _))
361       .Times(1);
362   EXPECT_CALL(*hci_mock,
363               SetParameters2(_, (uint8_t)params.tx_power, _, _, _, _, _, _))
364       .Times(1)
365       .WillOnce(SaveArg<7>(&set_params_cb));
366   BleAdvertisingManager::Get()->SetParameters(
367       advertiser_id, &params,
368       Bind(&BleAdvertisingManagerTest::SetParametersCb,
369            base::Unretained(this)));
370   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
371 
372   // let the set parameters command succeed!
373   set_params_cb.Run(0, -15);
374   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_params_status);
375 
376   status_cb set_data_cb;
377   /* verify that flags will not be added */
378   uint8_t expected_adv_data[] = {
379       0x02 /* len */, 0xFF /* manufacturer specific */, 0x01 /* data */};
380   EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, _, _, _, _, _))
381       .With(Args<4, 3>(ElementsAreArray(expected_adv_data)))
382       .Times(1)
383       .WillOnce(SaveArg<5>(&set_data_cb));
384   BleAdvertisingManager::Get()->SetData(
385       advertiser_id, false, std::vector<uint8_t>({0x02 /* len */, 0xFF, 0x01}),
386       Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
387   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
388 
389   set_data_cb.Run(0);
390   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
391 }
392 
TEST_F(BleAdvertisingManagerTest,test_reenabling)393 TEST_F(BleAdvertisingManagerTest, test_reenabling) {
394   BleAdvertisingManager::Get()->RegisterAdvertiser(
395       Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
396   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
397   EXPECT_EQ(0, reg_inst_id);
398 
399   uint8_t advertiser_id = reg_inst_id;
400   status_cb enable_cb;
401   EXPECT_CALL(*hci_mock,
402               Enable(0x01 /* enable */,
403                      AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
404                                                      advertiser_id))),
405                      _))
406       .Times(1)
407       .WillOnce(SaveArg<2>(&enable_cb));
408   BleAdvertisingManager::Get()->Enable(advertiser_id, true, Bind(DoNothing), 0,
409                                        0, Bind(DoNothing));
410   enable_cb.Run(0);
411   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
412 
413   EXPECT_CALL(*hci_mock,
414               Enable(0x01 /* enable */,
415                      AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
416                                                      advertiser_id))),
417                      _))
418       .Times(1)
419       .WillOnce(SaveArg<2>(&enable_cb));
420   BleAdvertisingManager::Get()->OnAdvertisingSetTerminated(advertiser_id, 0x00,
421                                                            0x01ed, 0x00);
422   enable_cb.Run(0);
423   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
424 }
425 
426 /* Make sure that instance is not reenabled if it's already disabled */
TEST_F(BleAdvertisingManagerTest,test_reenabling_disabled_instance)427 TEST_F(BleAdvertisingManagerTest, test_reenabling_disabled_instance) {
428   uint8_t advertiser_id = 1;  // any unregistered value
429 
430   EXPECT_CALL(*hci_mock, Enable(_, _, _)).Times(Exactly(0));
431   BleAdvertisingManager::Get()->OnAdvertisingSetTerminated(advertiser_id, 0x00,
432                                                            0x05, 0x00);
433   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
434 }
435 
436 /* This test verifies that the only flow that is currently used on Android, is
437  * working correctly in happy case scenario. */
TEST_F(BleAdvertisingManagerTest,test_start_advertising_set)438 TEST_F(BleAdvertisingManagerTest, test_start_advertising_set) {
439   std::vector<uint8_t> adv_data;
440   std::vector<uint8_t> scan_resp;
441   tBTM_BLE_ADV_PARAMS params;
442   tBLE_PERIODIC_ADV_PARAMS periodic_params;
443   periodic_params.enable = false;
444   std::vector<uint8_t> periodic_data;
445 
446   parameters_cb set_params_cb;
447   status_cb set_address_cb;
448   status_cb set_data_cb;
449   status_cb set_scan_resp_data_cb;
450   status_cb enable_cb;
451   EXPECT_CALL(*hci_mock, SetParameters1(_, _, _, _, _, _, _, _, _)).Times(1);
452   EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, _, _, _, _, _))
453       .Times(1)
454       .WillOnce(SaveArg<7>(&set_params_cb));
455   EXPECT_CALL(*hci_mock, SetRandomAddress(_, _, _))
456       .Times(1)
457       .WillOnce(SaveArg<2>(&set_address_cb));
458   EXPECT_CALL(*hci_mock, SetAdvertisingData(_, _, _, _, _, _))
459       .Times(1)
460       .WillOnce(SaveArg<5>(&set_data_cb));
461   EXPECT_CALL(*hci_mock, SetScanResponseData(_, _, _, _, _, _))
462       .Times(1)
463       .WillOnce(SaveArg<5>(&set_scan_resp_data_cb));
464   EXPECT_CALL(*hci_mock, Enable(0x01 /* enable */, _, _))
465       .Times(1)
466       .WillOnce(SaveArg<2>(&enable_cb));
467 
468   BleAdvertisingManager::Get()->StartAdvertisingSet(
469       Bind(&BleAdvertisingManagerTest::StartAdvertisingSetCb,
470            base::Unretained(this)),
471       &params, adv_data, scan_resp, &periodic_params, periodic_data,
472       0 /* duration */, 0 /* maxExtAdvEvents */, Bind(DoNothing2));
473 
474   // we are a truly gracious fake controller, let the commands succeed!
475   int selected_tx_power = -15;
476   set_params_cb.Run(0, selected_tx_power);
477   set_address_cb.Run(0);
478   set_data_cb.Run(0);
479   set_scan_resp_data_cb.Run(0);
480   enable_cb.Run(0);
481   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, start_advertising_set_status);
482   EXPECT_EQ(selected_tx_power, start_advertising_set_tx_power);
483   int advertiser_id = start_advertising_set_advertiser_id;
484   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
485 
486   // ... advertising ...
487 
488   // Disable advertiser
489   status_cb disable_cb;
490   EXPECT_CALL(*hci_mock,
491               Enable(0x00 /* disable */,
492                      AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
493                                                      advertiser_id))),
494                      _))
495       .Times(1)
496       .WillOnce(SaveArg<2>(&disable_cb));
497   status_cb remove_cb;
498   EXPECT_CALL(*hci_mock, RemoveAdvertisingSet(advertiser_id, _))
499       .Times(1)
500       .WillOnce(SaveArg<1>(&remove_cb));
501   BleAdvertisingManager::Get()->Unregister(advertiser_id);
502   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
503 
504   disable_cb.Run(0);
505   remove_cb.Run(0);
506 }
507 
TEST_F(BleAdvertisingManagerTest,test_start_advertising_set_params_failed)508 TEST_F(BleAdvertisingManagerTest, test_start_advertising_set_params_failed) {
509   BleAdvertisingManager::Get()->RegisterAdvertiser(
510       Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
511   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
512   int advertiser_id = reg_inst_id;
513 
514   std::vector<uint8_t> adv_data;
515   std::vector<uint8_t> scan_resp;
516   tBTM_BLE_ADV_PARAMS params;
517 
518   parameters_cb set_params_cb;
519   EXPECT_CALL(*hci_mock, SetParameters1(advertiser_id, _, _, _, _, _, _, _, _))
520       .Times(1);
521   EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, _, _, _, _, _))
522       .Times(1)
523       .WillOnce(SaveArg<7>(&set_params_cb));
524 
525   EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, _, _, _, _, _))
526       .Times(Exactly(0));
527 
528   BleAdvertisingManager::Get()->StartAdvertising(
529       advertiser_id,
530       Bind(&BleAdvertisingManagerTest::StartAdvertisingCb,
531            base::Unretained(this)),
532       &params, adv_data, scan_resp, 0, base::Callback<void(uint8_t)>());
533   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
534 
535   // set params failed
536   set_params_cb.Run(0x01, 0);
537 
538   // Expect the whole flow to fail right away
539   EXPECT_EQ(BTM_BLE_MULTI_ADV_FAILURE, start_advertising_status);
540 }
541 
TEST_F(BleAdvertisingManagerTest,test_data_sender)542 TEST_F(BleAdvertisingManagerTest, test_data_sender) {
543   // prepare test input vector
544   const int max_data_size = 1650;
545   std::vector<uint8_t> data(max_data_size);
546   for (int i = 0; i < max_data_size; i++) data[i] = i;
547 
548   BleAdvertisingManager::Get()->RegisterAdvertiser(
549       Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
550   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
551   int advertiser_id = reg_inst_id;
552 
553   status_cb set_data_cb;
554   EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, FIRST, _, 251, _, _))
555       .Times(1)
556       .WillOnce(SaveArg<5>(&set_data_cb));
557   EXPECT_CALL(*hci_mock,
558               SetAdvertisingData(advertiser_id, INTERMEDIATE, _, 251, _, _))
559       .Times(5)
560       .WillRepeatedly(SaveArg<5>(&set_data_cb));
561   EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, LAST, _, 144, _, _))
562       .Times(1)
563       .WillOnce(SaveArg<5>(&set_data_cb));
564   BleAdvertisingManager::Get()->SetData(
565       advertiser_id, false, data,
566       Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
567   for (int i = 0; i < 7; i++) {
568     set_data_cb.Run(0x00);
569   }
570   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
571   // Expect the whole flow to succeed
572   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
573 
574   // ***************** Try again with different data size *********************
575   data.resize(503);
576   EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, FIRST, _, 251, _, _))
577       .Times(1)
578       .WillOnce(SaveArg<5>(&set_data_cb));
579   EXPECT_CALL(*hci_mock,
580               SetAdvertisingData(advertiser_id, INTERMEDIATE, _, 251, _, _))
581       .Times(1)
582       .WillRepeatedly(SaveArg<5>(&set_data_cb));
583   EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, LAST, _, 1, _, _))
584       .Times(1)
585       .WillOnce(SaveArg<5>(&set_data_cb));
586   BleAdvertisingManager::Get()->SetData(
587       advertiser_id, false, data,
588       Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
589   for (int i = 0; i < 3; i++) {
590     set_data_cb.Run(0x00);
591   }
592   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
593   // Expect the whole flow to succeed
594   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
595 
596   // ***************** Try again with different data size *********************
597   data.resize(502);
598   EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, FIRST, _, 251, _, _))
599       .Times(1)
600       .WillOnce(SaveArg<5>(&set_data_cb));
601   EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, LAST, _, 251, _, _))
602       .Times(1)
603       .WillOnce(SaveArg<5>(&set_data_cb));
604   BleAdvertisingManager::Get()->SetData(
605       advertiser_id, false, data,
606       Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
607   for (int i = 0; i < 2; i++) {
608     set_data_cb.Run(0x00);
609   }
610   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
611   // Expect the whole flow to succeed
612   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
613 
614   // ***************** Try again with different data size *********************
615   data.resize(501);
616   EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, FIRST, _, 251, _, _))
617       .Times(1)
618       .WillOnce(SaveArg<5>(&set_data_cb));
619   EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, LAST, _, 250, _, _))
620       .Times(1)
621       .WillOnce(SaveArg<5>(&set_data_cb));
622   BleAdvertisingManager::Get()->SetData(
623       advertiser_id, false, data,
624       Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
625   for (int i = 0; i < 2; i++) {
626     set_data_cb.Run(0x00);
627   }
628   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
629   // Expect the whole flow to succeed
630   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
631 
632   // ***************** Try again with different data size *********************
633   data.resize(251);
634   EXPECT_CALL(*hci_mock,
635               SetAdvertisingData(advertiser_id, COMPLETE, _, 251, _, _))
636       .Times(1)
637       .WillOnce(SaveArg<5>(&set_data_cb));
638   BleAdvertisingManager::Get()->SetData(
639       advertiser_id, false, data,
640       Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
641   set_data_cb.Run(0x00);
642   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
643   // Expect the whole flow to succeed
644   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
645 
646   // ***************** Try again with different data size *********************
647   data.resize(120);
648   EXPECT_CALL(*hci_mock,
649               SetAdvertisingData(advertiser_id, COMPLETE, _, 120, _, _))
650       .Times(1)
651       .WillOnce(SaveArg<5>(&set_data_cb));
652   BleAdvertisingManager::Get()->SetData(
653       advertiser_id, false, data,
654       Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
655   set_data_cb.Run(0x00);
656   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
657   // Expect the whole flow to succeed
658   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
659 
660   // ***************** Try again with different data size *********************
661   data.resize(0);
662   EXPECT_CALL(*hci_mock,
663               SetAdvertisingData(advertiser_id, COMPLETE, _, 0, _, _))
664       .Times(1)
665       .WillOnce(SaveArg<5>(&set_data_cb));
666   BleAdvertisingManager::Get()->SetData(
667       advertiser_id, false, data,
668       Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
669   set_data_cb.Run(0x00);
670   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
671   // Expect the whole flow to succeed
672   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
673 }
674 
675 /* This test makes sure that conectable advertisment with timeout will get it's
676  * address updated once the timeout passes and one tries to enable it again.*/
TEST_F(BleAdvertisingManagerTest,test_connectable_address_update_during_timeout)677 TEST_F(BleAdvertisingManagerTest,
678        test_connectable_address_update_during_timeout) {
679   std::vector<uint8_t> adv_data;
680   std::vector<uint8_t> scan_resp;
681   tBTM_BLE_ADV_PARAMS params;
682   params.advertising_event_properties = 0x1 /* connectable */;
683   tBLE_PERIODIC_ADV_PARAMS periodic_params;
684   periodic_params.enable = false;
685   std::vector<uint8_t> periodic_data;
686 
687   uint8_t maxExtAdvEvents = 50;
688 
689   parameters_cb set_params_cb;
690   status_cb set_address_cb;
691   status_cb set_data_cb;
692   status_cb set_scan_resp_data_cb;
693   status_cb enable_cb;
694   EXPECT_CALL(*hci_mock, SetParameters1(_, _, _, _, _, _, _, _, _)).Times(1);
695   EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, _, _, _, _, _))
696       .Times(1)
697       .WillOnce(SaveArg<7>(&set_params_cb));
698   EXPECT_CALL(*hci_mock, SetRandomAddress(_, _, _))
699       .Times(1)
700       .WillOnce(SaveArg<2>(&set_address_cb));
701   EXPECT_CALL(*hci_mock, SetAdvertisingData(_, _, _, _, _, _))
702       .Times(1)
703       .WillOnce(SaveArg<5>(&set_data_cb));
704   EXPECT_CALL(*hci_mock, SetScanResponseData(_, _, _, _, _, _))
705       .Times(1)
706       .WillOnce(SaveArg<5>(&set_scan_resp_data_cb));
707   EXPECT_CALL(
708       *hci_mock,
709       Enable(
710           0x01 /* enable */,
711           AllOf(SizeIs(1),
712                 Contains(Field(&SetEnableData::max_extended_advertising_events,
713                                maxExtAdvEvents))),
714           _))
715       .Times(1)
716       .WillOnce(SaveArg<2>(&enable_cb));
717 
718   BleAdvertisingManager::Get()->StartAdvertisingSet(
719       Bind(&BleAdvertisingManagerTest::StartAdvertisingSetCb,
720            base::Unretained(this)),
721       &params, adv_data, scan_resp, &periodic_params, periodic_data,
722       0 /* duration */, maxExtAdvEvents, Bind(DoNothing2));
723 
724   // we are a truly gracious fake controller, let the commands succeed!
725   int selected_tx_power = -15;
726   set_params_cb.Run(0, selected_tx_power);
727   set_address_cb.Run(0);
728   set_data_cb.Run(0);
729   set_scan_resp_data_cb.Run(0);
730   enable_cb.Run(0);
731   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, start_advertising_set_status);
732   EXPECT_EQ(selected_tx_power, start_advertising_set_tx_power);
733   int advertiser_id = start_advertising_set_advertiser_id;
734   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
735 
736   // ... advertising ...
737 
738   // No HCI calls should be triggered, becuase there is a timeout on a
739   // connectable advertisement.
740   TriggerRandomAddressUpdate();
741   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
742 
743   // Set terminated because we advertised maxExtAdvEvents times!
744   BleAdvertisingManager::Get()->OnAdvertisingSetTerminated(
745       0x43 /*status */, advertiser_id, 0x00 /* conn_handle*/, maxExtAdvEvents);
746   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
747 
748   // Try to Enable the advertiser. It should first update it's random address.
749   EXPECT_CALL(*hci_mock, SetRandomAddress(_, _, _))
750       .Times(1)
751       .WillOnce(SaveArg<2>(&set_address_cb));
752   EXPECT_CALL(
753       *hci_mock,
754       Enable(
755           0x01 /* enable */,
756           AllOf(SizeIs(1),
757                 Contains(Field(&SetEnableData::max_extended_advertising_events,
758                                maxExtAdvEvents))),
759           _))
760       .Times(1)
761       .WillOnce(SaveArg<2>(&enable_cb));
762   BleAdvertisingManager::Get()->Enable(
763       advertiser_id, true,
764       Bind(&BleAdvertisingManagerTest::EnableCb, base::Unretained(this)), 0,
765       maxExtAdvEvents, Bind(DoNothing));
766   set_address_cb.Run(0);
767   enable_cb.Run(0);
768   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
769 
770   // Disable advertiser
771   status_cb disable_cb;
772   EXPECT_CALL(*hci_mock,
773               Enable(0x00 /* disable */,
774                      AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
775                                                      advertiser_id))),
776                      _))
777       .Times(1)
778       .WillOnce(SaveArg<2>(&disable_cb));
779   status_cb remove_cb;
780   EXPECT_CALL(*hci_mock, RemoveAdvertisingSet(advertiser_id, _))
781       .Times(1)
782       .WillOnce(SaveArg<1>(&remove_cb));
783   BleAdvertisingManager::Get()->Unregister(advertiser_id);
784   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
785 
786   disable_cb.Run(0);
787   remove_cb.Run(0);
788 }
789 
790 /* This test makes sure that periodic advertising is stopped before
791  * unregistering the advertiser, if it was enabled. */
TEST_F(BleAdvertisingManagerTest,test_periodic_adv_disable_on_unregister)792 TEST_F(BleAdvertisingManagerTest, test_periodic_adv_disable_on_unregister) {
793   std::vector<uint8_t> adv_data;
794   std::vector<uint8_t> scan_resp;
795   tBTM_BLE_ADV_PARAMS params;
796   params.advertising_event_properties = 0x1 /* connectable */;
797   tBLE_PERIODIC_ADV_PARAMS periodic_params;
798   periodic_params.enable = true;  // enable periodic advertising
799   std::vector<uint8_t> periodic_data;
800 
801   parameters_cb set_params_cb;
802   status_cb set_address_cb;
803   status_cb set_data_cb;
804   status_cb set_scan_resp_data_cb;
805   status_cb enable_cb;
806   status_cb set_periodic_params_cb;
807   status_cb set_periodic_data_cb;
808   status_cb set_periodic_enable_cb;
809   EXPECT_CALL(*hci_mock, SetParameters1(_, _, _, _, _, _, _, _, _)).Times(1);
810   EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, _, _, _, _, _))
811       .Times(1)
812       .WillOnce(SaveArg<7>(&set_params_cb));
813   EXPECT_CALL(*hci_mock, SetRandomAddress(_, _, _))
814       .Times(1)
815       .WillOnce(SaveArg<2>(&set_address_cb));
816   EXPECT_CALL(*hci_mock, SetAdvertisingData(_, _, _, _, _, _))
817       .Times(1)
818       .WillOnce(SaveArg<5>(&set_data_cb));
819   EXPECT_CALL(*hci_mock, SetScanResponseData(_, _, _, _, _, _))
820       .Times(1)
821       .WillOnce(SaveArg<5>(&set_scan_resp_data_cb));
822   EXPECT_CALL(*hci_mock, SetPeriodicAdvertisingParameters(_, _, _, _, _))
823       .Times(1)
824       .WillOnce(SaveArg<4>(&set_periodic_params_cb));
825   EXPECT_CALL(*hci_mock, SetPeriodicAdvertisingData(_, _, _, _, _))
826       .Times(1)
827       .WillOnce(SaveArg<4>(&set_periodic_data_cb));
828   EXPECT_CALL(*hci_mock, SetPeriodicAdvertisingEnable(0x01 /* enable */, _, _))
829       .Times(1)
830       .WillOnce(SaveArg<2>(&set_periodic_enable_cb));
831   EXPECT_CALL(*hci_mock, Enable(0x01 /* enable */, _, _))
832       .Times(1)
833       .WillOnce(SaveArg<2>(&enable_cb));
834 
835   BleAdvertisingManager::Get()->StartAdvertisingSet(
836       Bind(&BleAdvertisingManagerTest::StartAdvertisingSetCb,
837            base::Unretained(this)),
838       &params, adv_data, scan_resp, &periodic_params, periodic_data,
839       0 /* duration */, 0 /* maxExtAdvEvents */, Bind(DoNothing2));
840 
841   // we are a truly gracious fake controller, let the commands succeed!
842   int selected_tx_power = -15;
843   set_params_cb.Run(0, selected_tx_power);
844   set_address_cb.Run(0);
845   set_data_cb.Run(0);
846   set_scan_resp_data_cb.Run(0);
847   set_periodic_params_cb.Run(0);
848   set_periodic_data_cb.Run(0);
849   set_periodic_enable_cb.Run(0);
850   enable_cb.Run(0);
851   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, start_advertising_set_status);
852   EXPECT_EQ(selected_tx_power, start_advertising_set_tx_power);
853   int advertiser_id = start_advertising_set_advertiser_id;
854   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
855 
856   // ... advertising ...
857 
858   // Unregister advertiser - should disable periodic advertising
859   status_cb disable_cb;
860   EXPECT_CALL(*hci_mock,
861               Enable(0x00 /* disable */,
862                      AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
863                                                      advertiser_id))),
864                      _))
865       .Times(1)
866       .WillOnce(SaveArg<2>(&disable_cb));
867   status_cb disable_periodic_cb;
868   EXPECT_CALL(*hci_mock, SetPeriodicAdvertisingEnable(0x00 /* disable */,
869                                                       advertiser_id, _))
870       .Times(1)
871       .WillOnce(SaveArg<2>(&disable_periodic_cb));
872   status_cb remove_cb;
873   EXPECT_CALL(*hci_mock, RemoveAdvertisingSet(advertiser_id, _))
874       .Times(1)
875       .WillOnce(SaveArg<1>(&remove_cb));
876   BleAdvertisingManager::Get()->Unregister(advertiser_id);
877   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
878 
879   disable_cb.Run(0);
880   disable_periodic_cb.Run(0);
881   remove_cb.Run(0);
882 }
883 
TEST_F(BleAdvertisingManagerTest,test_suspend_resume)884 TEST_F(BleAdvertisingManagerTest, test_suspend_resume) {
885   for (int i = 0; i < 10; i++) {
886     BleAdvertisingManager::Get()->RegisterAdvertiser(Bind(
887         &BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
888     EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
889     EXPECT_EQ(i, reg_inst_id);
890   }
891 
892   std::array<int, 3> enabled = {{1, 3, 9}};
893 
894   for (int advertiser_id : enabled) {
895     status_cb enable_cb;
896     EXPECT_CALL(*hci_mock,
897                 Enable(0x01 /* enable */,
898                        AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
899                                                        advertiser_id))),
900                        _))
901         .Times(1)
902         .WillOnce(SaveArg<2>(&enable_cb));
903     BleAdvertisingManager::Get()->Enable(
904         advertiser_id, true,
905         Bind(&BleAdvertisingManagerTest::EnableCb, base::Unretained(this)), 0,
906         0, base::Callback<void(uint8_t)>());
907     enable_cb.Run(0);
908     ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
909   }
910 
911   // we have some advertisers registered, three advertising.
912 
913   // Call to Suspend() should disable all running advertisers
914   status_cb disable_cb;
915   EXPECT_CALL(
916       *hci_mock,
917       Enable(0x00 /* disable */,
918              AllOf(SizeIs(3), Contains(Field(&SetEnableData::handle, 1)),
919                    Contains(Field(&SetEnableData::handle, 3)),
920                    Contains(Field(&SetEnableData::handle, 9))),
921              _))
922       .Times(1)
923       .WillOnce(SaveArg<2>(&disable_cb));
924 
925   BleAdvertisingManager::Get()->Suspend();
926 
927   disable_cb.Run(0);
928   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
929 
930   // Call to Resume() should re-enable advertisers
931   status_cb enable_cb;
932   EXPECT_CALL(
933       *hci_mock,
934       Enable(0x01 /* enable */,
935              AllOf(SizeIs(3), Contains(Field(&SetEnableData::handle, 1)),
936                    Contains(Field(&SetEnableData::handle, 3)),
937                    Contains(Field(&SetEnableData::handle, 9))),
938              _))
939       .Times(1)
940       .WillOnce(SaveArg<2>(&enable_cb));
941 
942   BleAdvertisingManager::Get()->Resume();
943 
944   enable_cb.Run(0);
945 
946   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
947 }
948 
949 /* This test makes sure that conectable advertisment with timeout will get it's
950  * duration and maxExtAdvEvents updated, when it's terminated due to incoming
951  * connection.*/
TEST_F(BleAdvertisingManagerTest,test_duration_update_during_timeout)952 TEST_F(BleAdvertisingManagerTest, test_duration_update_during_timeout) {
953   std::vector<uint8_t> adv_data;
954   std::vector<uint8_t> scan_resp;
955   tBTM_BLE_ADV_PARAMS params;
956   params.advertising_event_properties = 0x1 /* connectable */;
957   params.adv_int_min = params.adv_int_max = 0xA0 /* 100ms */;
958   tBLE_PERIODIC_ADV_PARAMS periodic_params;
959   periodic_params.enable = false;
960   std::vector<uint8_t> periodic_data;
961 
962   uint8_t maxExtAdvEvents = 50;
963   uint16_t duration = 500 /* 5s */;
964 
965   parameters_cb set_params_cb;
966   status_cb set_address_cb;
967   status_cb set_data_cb;
968   status_cb set_scan_resp_data_cb;
969   status_cb enable_cb;
970   EXPECT_CALL(*hci_mock, SetParameters1(_, _, _, _, _, _, _, _, _)).Times(1);
971   EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, _, _, _, _, _))
972       .Times(1)
973       .WillOnce(SaveArg<7>(&set_params_cb));
974   EXPECT_CALL(*hci_mock, SetRandomAddress(_, _, _))
975       .Times(1)
976       .WillOnce(SaveArg<2>(&set_address_cb));
977   EXPECT_CALL(*hci_mock, SetAdvertisingData(_, _, _, _, _, _))
978       .Times(1)
979       .WillOnce(SaveArg<5>(&set_data_cb));
980   EXPECT_CALL(*hci_mock, SetScanResponseData(_, _, _, _, _, _))
981       .Times(1)
982       .WillOnce(SaveArg<5>(&set_scan_resp_data_cb));
983   EXPECT_CALL(
984       *hci_mock,
985       Enable(0x01 /* enable */,
986              AllOf(SizeIs(1),
987                    Contains(AllOf(
988                        Field(&SetEnableData::max_extended_advertising_events,
989                              maxExtAdvEvents),
990                        Field(&SetEnableData::duration, duration)))),
991              _))
992       .Times(1)
993       .WillOnce(SaveArg<2>(&enable_cb));
994 
995   BleAdvertisingManager::Get()->StartAdvertisingSet(
996       Bind(&BleAdvertisingManagerTest::StartAdvertisingSetCb,
997            base::Unretained(this)),
998       &params, adv_data, scan_resp, &periodic_params, periodic_data, duration,
999       maxExtAdvEvents, Bind(DoNothing2));
1000 
1001   // we are a truly gracious fake controller, let the commands succeed!
1002   int selected_tx_power = -15;
1003   set_params_cb.Run(0, selected_tx_power);
1004   set_address_cb.Run(0);
1005   set_data_cb.Run(0);
1006   set_scan_resp_data_cb.Run(0);
1007   enable_cb.Run(0);
1008   EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, start_advertising_set_status);
1009   EXPECT_EQ(selected_tx_power, start_advertising_set_tx_power);
1010   int advertiser_id = start_advertising_set_advertiser_id;
1011   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
1012 
1013   // ... advertising ...
1014 
1015   sleep(1);
1016 
1017   std::vector<SetEnableData> setEnableData;
1018   // Set terminated because we received connect request! Should trigger
1019   // re-enabling of the set
1020   EXPECT_CALL(*hci_mock, Enable(0x01 /* enable */, _, _))
1021       .Times(1)
1022       .WillOnce(DoAll(SaveArg<1>(&setEnableData), SaveArg<2>(&enable_cb)));
1023 
1024   BleAdvertisingManager::Get()->OnAdvertisingSetTerminated(
1025       0x00 /* Advertising successfully ended with a connection being created */,
1026       advertiser_id, 0x01fe /* conn_handle*/, 20 /* completed ExtAdvEvents */);
1027   enable_cb.Run(0);
1028 
1029   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
1030 
1031   uint16_t new_duration = setEnableData[0].duration;
1032   uint8_t new_extAdvEvents = setEnableData[0].max_extended_advertising_events;
1033 
1034   // Sleep is not super-accurate, so assume the recomputed timeouts are around
1035   // 4s +/- 100ms
1036   EXPECT_NEAR((duration - new_duration), 100 /*4s */, 10);
1037   EXPECT_NEAR((maxExtAdvEvents - new_extAdvEvents), 10, 1);
1038 
1039   // Disable advertiser
1040   status_cb disable_cb;
1041   EXPECT_CALL(*hci_mock,
1042               Enable(0x00 /* disable */,
1043                      AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
1044                                                      advertiser_id))),
1045                      _))
1046       .Times(1)
1047       .WillOnce(SaveArg<2>(&disable_cb));
1048   status_cb remove_cb;
1049   EXPECT_CALL(*hci_mock, RemoveAdvertisingSet(advertiser_id, _))
1050       .Times(1)
1051       .WillOnce(SaveArg<1>(&remove_cb));
1052   BleAdvertisingManager::Get()->Unregister(advertiser_id);
1053   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
1054 
1055   disable_cb.Run(0);
1056   remove_cb.Run(0);
1057 }
1058 
1059 /* This test verifies that stack cleanup, and shutdown happening while there is
1060  * outstanding HCI command is not triggering the callback */
TEST_F(BleAdvertisingManagerTest,test_cleanup_during_execution)1061 TEST_F(BleAdvertisingManagerTest, test_cleanup_during_execution) {
1062   std::vector<uint8_t> adv_data;
1063   std::vector<uint8_t> scan_resp;
1064   tBTM_BLE_ADV_PARAMS params;
1065   tBLE_PERIODIC_ADV_PARAMS periodic_params;
1066   periodic_params.enable = false;
1067   std::vector<uint8_t> periodic_data;
1068 
1069   parameters_cb set_params_cb;
1070   status_cb set_address_cb;
1071   status_cb set_data_cb;
1072   EXPECT_CALL(*hci_mock, SetParameters1(_, _, _, _, _, _, _, _, _)).Times(1);
1073   EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, _, _, _, _, _))
1074       .Times(1)
1075       .WillOnce(SaveArg<7>(&set_params_cb));
1076   EXPECT_CALL(*hci_mock, SetRandomAddress(_, _, _))
1077       .Times(1)
1078       .WillOnce(SaveArg<2>(&set_address_cb));
1079   EXPECT_CALL(*hci_mock, SetAdvertisingData(_, _, _, _, _, _))
1080       .Times(1)
1081       .WillOnce(SaveArg<5>(&set_data_cb));
1082 
1083   BleAdvertisingManager::Get()->StartAdvertisingSet(
1084       Bind(&BleAdvertisingManagerTest::StartAdvertisingSetCb,
1085            base::Unretained(this)),
1086       &params, adv_data, scan_resp, &periodic_params, periodic_data,
1087       0 /* duration */, 0 /* maxExtAdvEvents */, Bind(DoNothing2));
1088 
1089   // we are a truly gracious fake controller, let the commands succeed!
1090   int selected_tx_power = -15;
1091   set_params_cb.Run(0, selected_tx_power);
1092   set_address_cb.Run(0);
1093 
1094   // Someone shut down the stack in the middle of flow, when the HCI Set
1095   // Advertise Data was scheduled!
1096   BleAdvertisingManager::Get()->CleanUp();
1097 
1098   // The HCI call returns with status, and tries to execute the callback. This
1099   // should just silently drop the call. If it got executed, we would get crash,
1100   // because BleAdvertisingManager object was already deleted.
1101   set_data_cb.Run(0);
1102 
1103   ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
1104 }
1105 
1106 extern void testRecomputeTimeout1();
1107 extern void testRecomputeTimeout2();
1108 extern void testRecomputeTimeout3();
1109 
TEST_F(BleAdvertisingManagerTest,test_recompute_timeout)1110 TEST_F(BleAdvertisingManagerTest, test_recompute_timeout) {
1111   testRecomputeTimeout1();
1112   testRecomputeTimeout2();
1113   testRecomputeTimeout3();
1114 }
1115