1 /******************************************************************************
2 *
3 * Copyright (C) 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 <gmock/gmock.h>
20 #include <gtest/gtest.h>
21
22 #include "device/include/controller.h"
23 #include "stack/btm/ble_advertiser_hci_interface.h"
24 #include "stack/include/ble_advertiser.h"
25
26 using ::testing::_;
27 using ::testing::Args;
28 using ::testing::ElementsAreArray;
29 using ::testing::Exactly;
30 using ::testing::IsEmpty;
31 using ::testing::SaveArg;
32 using base::Bind;
33 using status_cb = BleAdvertiserHciInterface::status_cb;
34 using parameters_cb = BleAdvertiserHciInterface::parameters_cb;
35
36 const int num_adv_instances = 16;
37
38 /* Below are methods that must be implemented if we don't want to compile the
39 * whole stack. They will be removed, or changed into mocks one by one in the
40 * future, as the refactoring progresses */
BTM_BleLocalPrivacyEnabled()41 bool BTM_BleLocalPrivacyEnabled() { return true; }
BTM_ReadDiscoverability(uint16_t * p_window,uint16_t * p_interval)42 uint16_t BTM_ReadDiscoverability(uint16_t* p_window, uint16_t* p_interval) {
43 return true;
44 }
SMP_Encrypt(uint8_t * key,uint8_t key_len,uint8_t * plain_text,uint8_t pt_len,tSMP_ENC * p_out)45 bool SMP_Encrypt(uint8_t* key, uint8_t key_len, uint8_t* plain_text,
46 uint8_t pt_len, tSMP_ENC* p_out) {
47 return true;
48 }
BTM_GetDeviceIDRoot(BT_OCTET16 irk)49 void BTM_GetDeviceIDRoot(BT_OCTET16 irk) {}
btm_ble_update_dmt_flag_bits(uint8_t * flag_value,const uint16_t connect_mode,const uint16_t disc_mode)50 void btm_ble_update_dmt_flag_bits(uint8_t* flag_value,
51 const uint16_t connect_mode,
52 const uint16_t disc_mode) {}
btm_acl_update_conn_addr(uint8_t conn_handle,BD_ADDR address)53 void btm_acl_update_conn_addr(uint8_t conn_handle, BD_ADDR address) {}
btm_gen_resolvable_private_addr(base::Callback<void (uint8_t[8])> cb)54 void btm_gen_resolvable_private_addr(base::Callback<void(uint8_t[8])> cb) {
55 uint8_t fake_rand[8] = {0, 0, 0, 0, 0, 0, 0, 0};
56 cb.Run(fake_rand);
57 }
58
59 alarm_callback_t last_alarm_cb = nullptr;
60 void* last_alarm_data = nullptr;
alarm_set_on_queue(alarm_t * alarm,period_ms_t interval_ms,alarm_callback_t cb,void * data,fixed_queue_t * queue)61 void alarm_set_on_queue(alarm_t* alarm, period_ms_t interval_ms,
62 alarm_callback_t cb, void* data, fixed_queue_t* queue) {
63 last_alarm_cb = cb;
64 last_alarm_data = data;
65 }
66
alarm_cancel(alarm_t * alarm)67 void alarm_cancel(alarm_t* alarm) {}
alarm_new_periodic(const char * name)68 alarm_t* alarm_new_periodic(const char* name) { return nullptr; }
alarm_new(const char * name)69 alarm_t* alarm_new(const char* name) { return nullptr; }
alarm_free(alarm_t * alarm)70 void alarm_free(alarm_t* alarm) {}
controller_get_interface()71 const controller_t* controller_get_interface() { return nullptr; }
72 fixed_queue_t* btu_general_alarm_queue = nullptr;
73
74 namespace {
DoNothing(uint8_t)75 void DoNothing(uint8_t) {}
76
DoNothing2(uint8_t,uint8_t)77 void DoNothing2(uint8_t, uint8_t) {}
78
TriggerRandomAddressUpdate()79 void TriggerRandomAddressUpdate() {
80 // Call to StartAdvertisingSet set the last_alarm_cb to random address timeout
81 // callback. Call it now in order to trigger address update
82 last_alarm_cb(last_alarm_data);
83 }
84
85 constexpr uint8_t INTERMEDIATE =
86 0x00; // Intermediate fragment of fragmented data
87 constexpr uint8_t FIRST = 0x01; // First fragment of fragmented data
88 constexpr uint8_t LAST = 0x02; // Last fragment of fragmented data
89 constexpr uint8_t COMPLETE = 0x03; // Complete extended advertising data
90
91 class AdvertiserHciMock : public BleAdvertiserHciInterface {
92 public:
93 AdvertiserHciMock() = default;
94 ~AdvertiserHciMock() override = default;
95
96 MOCK_METHOD1(ReadInstanceCount,
97 void(base::Callback<void(uint8_t /* inst_cnt*/)>));
98 MOCK_METHOD1(SetAdvertisingEventObserver,
99 void(AdvertisingEventObserver* observer));
100 MOCK_METHOD6(SetAdvertisingData,
101 void(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t*, status_cb));
102 MOCK_METHOD6(SetScanResponseData,
103 void(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t*, status_cb));
104 MOCK_METHOD3(SetRandomAddress, void(uint8_t, BD_ADDR, status_cb));
105 MOCK_METHOD5(Enable, void(uint8_t, uint8_t, uint16_t, uint8_t, status_cb));
106 MOCK_METHOD5(SetPeriodicAdvertisingParameters,
107 void(uint8_t, uint16_t, uint16_t, uint16_t, status_cb));
108 MOCK_METHOD5(SetPeriodicAdvertisingData,
109 void(uint8_t, uint8_t, uint8_t, uint8_t*, status_cb));
110 MOCK_METHOD3(SetPeriodicAdvertisingEnable, void(uint8_t, uint8_t, status_cb));
111 MOCK_METHOD2(RemoveAdvertisingSet, void(uint8_t, status_cb));
112 MOCK_METHOD1(ClearAdvertisingSets, void(status_cb));
113
114 MOCK_METHOD9(SetParameters1,
115 void(uint8_t, uint16_t, uint32_t, uint32_t, uint8_t, uint8_t,
116 BD_ADDR, uint8_t, BD_ADDR));
117 MOCK_METHOD8(SetParameters2, void(uint8_t, int8_t, uint8_t, uint8_t, uint8_t,
118 uint8_t, uint8_t, parameters_cb));
119
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,BD_ADDR own_address,uint8_t peer_address_type,BD_ADDR 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)120 void SetParameters(uint8_t handle, uint16_t properties, uint32_t adv_int_min,
121 uint32_t adv_int_max, uint8_t channel_map,
122 uint8_t own_address_type, BD_ADDR own_address,
123 uint8_t peer_address_type, BD_ADDR peer_address,
124 uint8_t filter_policy, int8_t tx_power,
125 uint8_t primary_phy, uint8_t secondary_max_skip,
126 uint8_t secondary_phy, uint8_t advertising_sid,
127 uint8_t scan_request_notify_enable,
128 parameters_cb cmd_complete) override {
129 SetParameters1(handle, properties, adv_int_min, adv_int_max, channel_map,
130 own_address_type, own_address, peer_address_type,
131 peer_address);
132 SetParameters2(filter_policy, tx_power, primary_phy, secondary_max_skip,
133 secondary_phy, advertising_sid, scan_request_notify_enable,
134 cmd_complete);
135 };
136
QuirkAdvertiserZeroHandle()137 bool QuirkAdvertiserZeroHandle() { return false; }
138
139 private:
140 DISALLOW_COPY_AND_ASSIGN(AdvertiserHciMock);
141 };
142
143 } // namespace
144
145 class BleAdvertisingManagerTest : public testing::Test {
146 protected:
147 int reg_inst_id = -1;
148 int reg_status = -1;
149 int set_params_status = -1;
150 int set_data_status = -1;
151 int enable_status = -1;
152 int start_advertising_status = -1;
153 int start_advertising_set_advertiser_id = -1;
154 int start_advertising_set_tx_power = -1;
155 int start_advertising_set_status = -1;
156
157 std::unique_ptr<AdvertiserHciMock> hci_mock;
158
SetUp()159 virtual void SetUp() {
160 hci_mock.reset(new AdvertiserHciMock());
161
162 base::Callback<void(uint8_t)> inst_cnt_Cb;
163 EXPECT_CALL(*hci_mock, ReadInstanceCount(_))
164 .Times(Exactly(1))
165 .WillOnce(SaveArg<0>(&inst_cnt_Cb));
166
167 BleAdvertisingManager::Initialize(hci_mock.get());
168 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
169
170 // we are a truly gracious fake controller, let the command succeed!
171 inst_cnt_Cb.Run(num_adv_instances);
172 }
173
TearDown()174 virtual void TearDown() {
175 BleAdvertisingManager::CleanUp();
176 hci_mock.reset();
177 }
178
179 public:
RegistrationCb(uint8_t inst_id,uint8_t status)180 void RegistrationCb(uint8_t inst_id, uint8_t status) {
181 reg_inst_id = inst_id;
182 reg_status = status;
183 }
184
SetParametersCb(uint8_t status,int8_t tx_power)185 void SetParametersCb(uint8_t status, int8_t tx_power) {
186 set_params_status = status;
187 }
SetDataCb(uint8_t status)188 void SetDataCb(uint8_t status) { set_data_status = status; }
EnableCb(uint8_t status)189 void EnableCb(uint8_t status) { enable_status = status; }
StartAdvertisingCb(uint8_t status)190 void StartAdvertisingCb(uint8_t status) { start_advertising_status = status; }
StartAdvertisingSetCb(uint8_t advertiser_id,int8_t tx_power,uint8_t status)191 void StartAdvertisingSetCb(uint8_t advertiser_id, int8_t tx_power,
192 uint8_t status) {
193 start_advertising_set_advertiser_id = advertiser_id;
194 start_advertising_set_tx_power = tx_power;
195 start_advertising_set_status = status;
196 }
197 };
198
TEST_F(BleAdvertisingManagerTest,test_registration)199 TEST_F(BleAdvertisingManagerTest, test_registration) {
200 for (int i = 0; i < num_adv_instances; i++) {
201 BleAdvertisingManager::Get()->RegisterAdvertiser(Bind(
202 &BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
203 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
204 EXPECT_EQ(i, reg_inst_id);
205 }
206
207 // This call should return an error - no more advertisers left.
208 BleAdvertisingManager::Get()->RegisterAdvertiser(
209 Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
210 EXPECT_EQ(ADVERTISE_FAILED_TOO_MANY_ADVERTISERS, reg_status);
211 // Don't bother checking inst_id, it doesn't matter
212
213 status_cb remove_cb;
214 EXPECT_CALL(*hci_mock, RemoveAdvertisingSet(_, _))
215 .Times(1)
216 .WillOnce(SaveArg<1>(&remove_cb));
217 BleAdvertisingManager::Get()->Unregister(5);
218 remove_cb.Run(0);
219
220 // One advertiser was freed, so should be able to register one now
221 BleAdvertisingManager::Get()->RegisterAdvertiser(
222 Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
223 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
224 EXPECT_EQ(5, reg_inst_id);
225 }
226
227 /* This test verifies that the following flow is working correctly: register,
228 * set parameters, set data, enable, ... (advertise) ..., unregister*/
TEST_F(BleAdvertisingManagerTest,test_android_flow)229 TEST_F(BleAdvertisingManagerTest, test_android_flow) {
230 BleAdvertisingManager::Get()->RegisterAdvertiser(
231 Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
232 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
233 int advertiser_id = reg_inst_id;
234
235 parameters_cb set_params_cb;
236 tBTM_BLE_ADV_PARAMS params;
237 EXPECT_CALL(*hci_mock, SetParameters1(advertiser_id, _, _, _, _, _, _, _, _))
238 .Times(1);
239 EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, _, _, _, _, _))
240 .Times(1)
241 .WillOnce(SaveArg<7>(&set_params_cb));
242 BleAdvertisingManager::Get()->SetParameters(
243 advertiser_id, ¶ms, Bind(&BleAdvertisingManagerTest::SetParametersCb,
244 base::Unretained(this)));
245 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
246
247 // we are a truly gracious fake controller, let the command succeed!
248 set_params_cb.Run(0, 0);
249 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_params_status);
250
251 status_cb set_data_cb;
252 EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, _, _, _, _, _))
253 .Times(1)
254 .WillOnce(SaveArg<5>(&set_data_cb));
255 BleAdvertisingManager::Get()->SetData(
256 advertiser_id, false, std::vector<uint8_t>(),
257 Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
258 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
259
260 set_data_cb.Run(0);
261 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
262
263 status_cb enable_cb;
264 EXPECT_CALL(*hci_mock, Enable(0x01 /* enable */, advertiser_id, _, _, _))
265 .Times(1)
266 .WillOnce(SaveArg<4>(&enable_cb));
267 BleAdvertisingManager::Get()->Enable(
268 advertiser_id, true,
269 Bind(&BleAdvertisingManagerTest::EnableCb, base::Unretained(this)), 0, 0,
270 base::Callback<void(uint8_t)>());
271 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
272
273 enable_cb.Run(0);
274 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, enable_status);
275
276 /* fake controller should be advertising */
277
278 EXPECT_CALL(*hci_mock, Enable(0x00 /* disable */, advertiser_id, _, _, _))
279 .Times(1)
280 .WillOnce(SaveArg<4>(&enable_cb));
281 status_cb remove_cb;
282 EXPECT_CALL(*hci_mock, RemoveAdvertisingSet(_, _))
283 .Times(1)
284 .WillOnce(SaveArg<1>(&remove_cb));
285 BleAdvertisingManager::Get()->Unregister(advertiser_id);
286 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
287
288 enable_cb.Run(0);
289 remove_cb.Run(0);
290 }
291
292 /* This test verifies that when advertising data is set, tx power and flags will
293 * be properly filled. */
TEST_F(BleAdvertisingManagerTest,test_adv_data_filling)294 TEST_F(BleAdvertisingManagerTest, test_adv_data_filling) {
295 BleAdvertisingManager::Get()->RegisterAdvertiser(
296 Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
297 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
298 int advertiser_id = reg_inst_id;
299
300 parameters_cb set_params_cb;
301 tBTM_BLE_ADV_PARAMS params;
302 params.advertising_event_properties =
303 BleAdvertisingManager::advertising_prop_legacy_connectable;
304 params.tx_power = -15;
305 EXPECT_CALL(*hci_mock, SetParameters1(advertiser_id, _, _, _, _, _, _, _, _))
306 .Times(1);
307 EXPECT_CALL(*hci_mock, SetParameters2(_, params.tx_power, _, _, _, _, _, _))
308 .Times(1)
309 .WillOnce(SaveArg<7>(&set_params_cb));
310 BleAdvertisingManager::Get()->SetParameters(
311 advertiser_id, ¶ms, Bind(&BleAdvertisingManagerTest::SetParametersCb,
312 base::Unretained(this)));
313 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
314
315 // let the set parameters command succeed!
316 set_params_cb.Run(0, 0);
317 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_params_status);
318
319 status_cb set_data_cb;
320 /* verify that flags will be added, and tx power filled, if call to SetData
321 * contained only tx power, and the advertisement is connectable */
322 uint8_t expected_adv_data[] = {
323 0x02 /* len */, 0x01 /* flags */,
324 0x02 /* flags value */, 0x02 /* len */,
325 0x0A /* tx_power */, static_cast<uint8_t>(params.tx_power)};
326 EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, _, _, _, _, _))
327 .With(Args<4, 3>(ElementsAreArray(expected_adv_data)))
328 .Times(1)
329 .WillOnce(SaveArg<5>(&set_data_cb));
330 BleAdvertisingManager::Get()->SetData(
331 advertiser_id, false,
332 std::vector<uint8_t>({0x02 /* len */, 0x0A /* tx_power */, 0x00}),
333 Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
334 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
335
336 set_data_cb.Run(0);
337 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
338 }
339
340 /* This test verifies that when advertising is non-connectable, flags will not
341 * be added. */
TEST_F(BleAdvertisingManagerTest,test_adv_data_not_filling)342 TEST_F(BleAdvertisingManagerTest, test_adv_data_not_filling) {
343 BleAdvertisingManager::Get()->RegisterAdvertiser(
344 Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
345 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
346 int advertiser_id = reg_inst_id;
347
348 parameters_cb set_params_cb;
349 tBTM_BLE_ADV_PARAMS params;
350 params.advertising_event_properties =
351 BleAdvertisingManager::advertising_prop_legacy_non_connectable;
352 params.tx_power = -15;
353 EXPECT_CALL(*hci_mock, SetParameters1(advertiser_id, _, _, _, _, _, _, _, _))
354 .Times(1);
355 EXPECT_CALL(*hci_mock,
356 SetParameters2(_, (uint8_t)params.tx_power, _, _, _, _, _, _))
357 .Times(1)
358 .WillOnce(SaveArg<7>(&set_params_cb));
359 BleAdvertisingManager::Get()->SetParameters(
360 advertiser_id, ¶ms, Bind(&BleAdvertisingManagerTest::SetParametersCb,
361 base::Unretained(this)));
362 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
363
364 // let the set parameters command succeed!
365 set_params_cb.Run(0, -15);
366 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_params_status);
367
368 status_cb set_data_cb;
369 /* verify that flags will not be added */
370 uint8_t expected_adv_data[] = {
371 0x02 /* len */, 0xFF /* manufacturer specific */, 0x01 /* data */};
372 EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, _, _, _, _, _))
373 .With(Args<4, 3>(ElementsAreArray(expected_adv_data)))
374 .Times(1)
375 .WillOnce(SaveArg<5>(&set_data_cb));
376 BleAdvertisingManager::Get()->SetData(
377 advertiser_id, false, std::vector<uint8_t>({0x02 /* len */, 0xFF, 0x01}),
378 Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
379 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
380
381 set_data_cb.Run(0);
382 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
383 }
384
TEST_F(BleAdvertisingManagerTest,test_reenabling)385 TEST_F(BleAdvertisingManagerTest, test_reenabling) {
386 BleAdvertisingManager::Get()->RegisterAdvertiser(
387 Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
388 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
389 EXPECT_EQ(0, reg_inst_id);
390
391 uint8_t advertiser_id = reg_inst_id;
392
393 status_cb enable_cb;
394 EXPECT_CALL(*hci_mock, Enable(0x01 /* enable */, advertiser_id, _, _, _))
395 .Times(1)
396 .WillOnce(SaveArg<4>(&enable_cb));
397 BleAdvertisingManager::Get()->OnAdvertisingSetTerminated(advertiser_id, 0x00,
398 0x05, 0x00);
399 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
400
401 enable_cb.Run(0);
402 }
403
404 /* Make sure that instance is not reenabled if it's already disabled */
TEST_F(BleAdvertisingManagerTest,test_reenabling_disabled_instance)405 TEST_F(BleAdvertisingManagerTest, test_reenabling_disabled_instance) {
406 uint8_t advertiser_id = 1; // any unregistered value
407
408 EXPECT_CALL(*hci_mock, Enable(_, _, _, _, _)).Times(Exactly(0));
409 BleAdvertisingManager::Get()->OnAdvertisingSetTerminated(advertiser_id, 0x00,
410 0x05, 0x00);
411 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
412 }
413
414 /* This test verifies that the only flow that is currently used on Android, is
415 * working correctly in happy case scenario. */
TEST_F(BleAdvertisingManagerTest,test_start_advertising_set)416 TEST_F(BleAdvertisingManagerTest, test_start_advertising_set) {
417 std::vector<uint8_t> adv_data;
418 std::vector<uint8_t> scan_resp;
419 tBTM_BLE_ADV_PARAMS params;
420 tBLE_PERIODIC_ADV_PARAMS periodic_params;
421 periodic_params.enable = false;
422 std::vector<uint8_t> periodic_data;
423
424 parameters_cb set_params_cb;
425 status_cb set_address_cb;
426 status_cb set_data_cb;
427 status_cb set_scan_resp_data_cb;
428 status_cb enable_cb;
429 EXPECT_CALL(*hci_mock, SetParameters1(_, _, _, _, _, _, _, _, _)).Times(1);
430 EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, _, _, _, _, _))
431 .Times(1)
432 .WillOnce(SaveArg<7>(&set_params_cb));
433 EXPECT_CALL(*hci_mock, SetRandomAddress(_, _, _))
434 .Times(1)
435 .WillOnce(SaveArg<2>(&set_address_cb));
436 EXPECT_CALL(*hci_mock, SetAdvertisingData(_, _, _, _, _, _))
437 .Times(1)
438 .WillOnce(SaveArg<5>(&set_data_cb));
439 EXPECT_CALL(*hci_mock, SetScanResponseData(_, _, _, _, _, _))
440 .Times(1)
441 .WillOnce(SaveArg<5>(&set_scan_resp_data_cb));
442 EXPECT_CALL(*hci_mock, Enable(0x01 /* enable */, _, _, _, _))
443 .Times(1)
444 .WillOnce(SaveArg<4>(&enable_cb));
445
446 BleAdvertisingManager::Get()->StartAdvertisingSet(
447 Bind(&BleAdvertisingManagerTest::StartAdvertisingSetCb,
448 base::Unretained(this)),
449 ¶ms, adv_data, scan_resp, &periodic_params, periodic_data,
450 0 /* duration */, 0 /* maxExtAdvEvents */, Bind(DoNothing2));
451
452 // we are a truly gracious fake controller, let the commands succeed!
453 int selected_tx_power = -15;
454 set_params_cb.Run(0, selected_tx_power);
455 set_address_cb.Run(0);
456 set_data_cb.Run(0);
457 set_scan_resp_data_cb.Run(0);
458 enable_cb.Run(0);
459 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, start_advertising_set_status);
460 EXPECT_EQ(selected_tx_power, start_advertising_set_tx_power);
461 int advertiser_id = start_advertising_set_advertiser_id;
462 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
463
464 // ... advertising ...
465
466 // Disable advertiser
467 status_cb disable_cb;
468 EXPECT_CALL(*hci_mock, Enable(0x00 /* disable */, advertiser_id, _, _, _))
469 .Times(1)
470 .WillOnce(SaveArg<4>(&disable_cb));
471 status_cb remove_cb;
472 EXPECT_CALL(*hci_mock, RemoveAdvertisingSet(advertiser_id, _))
473 .Times(1)
474 .WillOnce(SaveArg<1>(&remove_cb));
475 BleAdvertisingManager::Get()->Unregister(advertiser_id);
476 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
477
478 disable_cb.Run(0);
479 remove_cb.Run(0);
480 }
481
TEST_F(BleAdvertisingManagerTest,test_start_advertising_set_params_failed)482 TEST_F(BleAdvertisingManagerTest, test_start_advertising_set_params_failed) {
483 BleAdvertisingManager::Get()->RegisterAdvertiser(
484 Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
485 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
486 int advertiser_id = reg_inst_id;
487
488 std::vector<uint8_t> adv_data;
489 std::vector<uint8_t> scan_resp;
490 tBTM_BLE_ADV_PARAMS params;
491
492 parameters_cb set_params_cb;
493 EXPECT_CALL(*hci_mock, SetParameters1(advertiser_id, _, _, _, _, _, _, _, _))
494 .Times(1);
495 EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, _, _, _, _, _))
496 .Times(1)
497 .WillOnce(SaveArg<7>(&set_params_cb));
498
499 EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, _, _, _, _, _))
500 .Times(Exactly(0));
501
502 BleAdvertisingManager::Get()->StartAdvertising(
503 advertiser_id, Bind(&BleAdvertisingManagerTest::StartAdvertisingCb,
504 base::Unretained(this)),
505 ¶ms, adv_data, scan_resp, 0, base::Callback<void(uint8_t)>());
506 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
507
508 // set params failed
509 set_params_cb.Run(0x01, 0);
510
511 // Expect the whole flow to fail right away
512 EXPECT_EQ(BTM_BLE_MULTI_ADV_FAILURE, start_advertising_status);
513 }
514
TEST_F(BleAdvertisingManagerTest,test_data_sender)515 TEST_F(BleAdvertisingManagerTest, test_data_sender) {
516 // prepare test input vector
517 const int max_data_size = 1650;
518 std::vector<uint8_t> data(max_data_size);
519 for (int i = 0; i < max_data_size; i++) data[i] = i;
520
521 BleAdvertisingManager::Get()->RegisterAdvertiser(
522 Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
523 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
524 int advertiser_id = reg_inst_id;
525
526 status_cb set_data_cb;
527 EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, FIRST, _, 251, _, _))
528 .Times(1)
529 .WillOnce(SaveArg<5>(&set_data_cb));
530 EXPECT_CALL(*hci_mock,
531 SetAdvertisingData(advertiser_id, INTERMEDIATE, _, 251, _, _))
532 .Times(5)
533 .WillRepeatedly(SaveArg<5>(&set_data_cb));
534 EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, LAST, _, 144, _, _))
535 .Times(1)
536 .WillOnce(SaveArg<5>(&set_data_cb));
537 BleAdvertisingManager::Get()->SetData(
538 advertiser_id, false, data,
539 Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
540 for (int i = 0; i < 7; i++) {
541 set_data_cb.Run(0x00);
542 }
543 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
544 // Expect the whole flow to succeed
545 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
546
547 // ***************** Try again with different data size *********************
548 data.resize(503);
549 EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, FIRST, _, 251, _, _))
550 .Times(1)
551 .WillOnce(SaveArg<5>(&set_data_cb));
552 EXPECT_CALL(*hci_mock,
553 SetAdvertisingData(advertiser_id, INTERMEDIATE, _, 251, _, _))
554 .Times(1)
555 .WillRepeatedly(SaveArg<5>(&set_data_cb));
556 EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, LAST, _, 1, _, _))
557 .Times(1)
558 .WillOnce(SaveArg<5>(&set_data_cb));
559 BleAdvertisingManager::Get()->SetData(
560 advertiser_id, false, data,
561 Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
562 for (int i = 0; i < 3; i++) {
563 set_data_cb.Run(0x00);
564 }
565 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
566 // Expect the whole flow to succeed
567 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
568
569 // ***************** Try again with different data size *********************
570 data.resize(502);
571 EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, FIRST, _, 251, _, _))
572 .Times(1)
573 .WillOnce(SaveArg<5>(&set_data_cb));
574 EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, LAST, _, 251, _, _))
575 .Times(1)
576 .WillOnce(SaveArg<5>(&set_data_cb));
577 BleAdvertisingManager::Get()->SetData(
578 advertiser_id, false, data,
579 Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
580 for (int i = 0; i < 2; i++) {
581 set_data_cb.Run(0x00);
582 }
583 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
584 // Expect the whole flow to succeed
585 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
586
587 // ***************** Try again with different data size *********************
588 data.resize(501);
589 EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, FIRST, _, 251, _, _))
590 .Times(1)
591 .WillOnce(SaveArg<5>(&set_data_cb));
592 EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, LAST, _, 250, _, _))
593 .Times(1)
594 .WillOnce(SaveArg<5>(&set_data_cb));
595 BleAdvertisingManager::Get()->SetData(
596 advertiser_id, false, data,
597 Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
598 for (int i = 0; i < 2; i++) {
599 set_data_cb.Run(0x00);
600 }
601 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
602 // Expect the whole flow to succeed
603 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
604
605 // ***************** Try again with different data size *********************
606 data.resize(251);
607 EXPECT_CALL(*hci_mock,
608 SetAdvertisingData(advertiser_id, COMPLETE, _, 251, _, _))
609 .Times(1)
610 .WillOnce(SaveArg<5>(&set_data_cb));
611 BleAdvertisingManager::Get()->SetData(
612 advertiser_id, false, data,
613 Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
614 set_data_cb.Run(0x00);
615 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
616 // Expect the whole flow to succeed
617 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
618
619 // ***************** Try again with different data size *********************
620 data.resize(120);
621 EXPECT_CALL(*hci_mock,
622 SetAdvertisingData(advertiser_id, COMPLETE, _, 120, _, _))
623 .Times(1)
624 .WillOnce(SaveArg<5>(&set_data_cb));
625 BleAdvertisingManager::Get()->SetData(
626 advertiser_id, false, data,
627 Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
628 set_data_cb.Run(0x00);
629 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
630 // Expect the whole flow to succeed
631 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
632
633 // ***************** Try again with different data size *********************
634 data.resize(0);
635 EXPECT_CALL(*hci_mock,
636 SetAdvertisingData(advertiser_id, COMPLETE, _, 0, _, _))
637 .Times(1)
638 .WillOnce(SaveArg<5>(&set_data_cb));
639 BleAdvertisingManager::Get()->SetData(
640 advertiser_id, false, data,
641 Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
642 set_data_cb.Run(0x00);
643 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
644 // Expect the whole flow to succeed
645 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
646 }
647
648 /* This test makes sure that conectable advertisment with timeout will get it's
649 * address updated once the timeout passes and one tries to enable it again.*/
TEST_F(BleAdvertisingManagerTest,test_connectable_address_update_during_timeout)650 TEST_F(BleAdvertisingManagerTest,
651 test_connectable_address_update_during_timeout) {
652 std::vector<uint8_t> adv_data;
653 std::vector<uint8_t> scan_resp;
654 tBTM_BLE_ADV_PARAMS params;
655 params.advertising_event_properties = 0x1 /* connectable */;
656 tBLE_PERIODIC_ADV_PARAMS periodic_params;
657 periodic_params.enable = false;
658 std::vector<uint8_t> periodic_data;
659
660 uint8_t maxExtAdvEvents = 50;
661
662 parameters_cb set_params_cb;
663 status_cb set_address_cb;
664 status_cb set_data_cb;
665 status_cb set_scan_resp_data_cb;
666 status_cb enable_cb;
667 EXPECT_CALL(*hci_mock, SetParameters1(_, _, _, _, _, _, _, _, _)).Times(1);
668 EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, _, _, _, _, _))
669 .Times(1)
670 .WillOnce(SaveArg<7>(&set_params_cb));
671 EXPECT_CALL(*hci_mock, SetRandomAddress(_, _, _))
672 .Times(1)
673 .WillOnce(SaveArg<2>(&set_address_cb));
674 EXPECT_CALL(*hci_mock, SetAdvertisingData(_, _, _, _, _, _))
675 .Times(1)
676 .WillOnce(SaveArg<5>(&set_data_cb));
677 EXPECT_CALL(*hci_mock, SetScanResponseData(_, _, _, _, _, _))
678 .Times(1)
679 .WillOnce(SaveArg<5>(&set_scan_resp_data_cb));
680 EXPECT_CALL(*hci_mock, Enable(0x01 /* enable */, _, _, maxExtAdvEvents, _))
681 .Times(1)
682 .WillOnce(SaveArg<4>(&enable_cb));
683
684 BleAdvertisingManager::Get()->StartAdvertisingSet(
685 Bind(&BleAdvertisingManagerTest::StartAdvertisingSetCb,
686 base::Unretained(this)),
687 ¶ms, adv_data, scan_resp, &periodic_params, periodic_data,
688 0 /* duration */, maxExtAdvEvents, Bind(DoNothing2));
689
690 // we are a truly gracious fake controller, let the commands succeed!
691 int selected_tx_power = -15;
692 set_params_cb.Run(0, selected_tx_power);
693 set_address_cb.Run(0);
694 set_data_cb.Run(0);
695 set_scan_resp_data_cb.Run(0);
696 enable_cb.Run(0);
697 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, start_advertising_set_status);
698 EXPECT_EQ(selected_tx_power, start_advertising_set_tx_power);
699 int advertiser_id = start_advertising_set_advertiser_id;
700 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
701
702 // ... advertising ...
703
704 // No HCI calls should be triggered, becuase there is a timeout on a
705 // connectable advertisement.
706 TriggerRandomAddressUpdate();
707 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
708
709 // Set terminated because we advertised maxExtAdvEvents times!
710 BleAdvertisingManager::Get()->OnAdvertisingSetTerminated(
711 0x43 /*status */, advertiser_id, 0x00 /* conn_handle*/, maxExtAdvEvents);
712 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
713
714 // Try to Enable the advertiser. It should first update it's random address.
715 EXPECT_CALL(*hci_mock, SetRandomAddress(_, _, _))
716 .Times(1)
717 .WillOnce(SaveArg<2>(&set_address_cb));
718 EXPECT_CALL(*hci_mock, Enable(0x01 /* enable */, _, _, maxExtAdvEvents, _))
719 .Times(1)
720 .WillOnce(SaveArg<4>(&enable_cb));
721 BleAdvertisingManager::Get()->Enable(
722 advertiser_id, true,
723 Bind(&BleAdvertisingManagerTest::EnableCb, base::Unretained(this)), 0,
724 maxExtAdvEvents, Bind(DoNothing));
725 set_address_cb.Run(0);
726 enable_cb.Run(0);
727 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
728
729 // Disable advertiser
730 status_cb disable_cb;
731 EXPECT_CALL(*hci_mock, Enable(0x00 /* disable */, advertiser_id, _, _, _))
732 .Times(1)
733 .WillOnce(SaveArg<4>(&disable_cb));
734 status_cb remove_cb;
735 EXPECT_CALL(*hci_mock, RemoveAdvertisingSet(advertiser_id, _))
736 .Times(1)
737 .WillOnce(SaveArg<1>(&remove_cb));
738 BleAdvertisingManager::Get()->Unregister(advertiser_id);
739 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
740
741 disable_cb.Run(0);
742 remove_cb.Run(0);
743 }
744