blob: 952aa41c09cc664dc4dbc38d88750d243b8fc6de [file] [log] [blame]
Jakub Pawlowskie47b7692016-09-28 07:36:54 -07001/******************************************************************************
2 *
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07003 * Copyright 2016 The Android Open Source Project
Jakub Pawlowskie47b7692016-09-28 07:36:54 -07004 *
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
Jakub Pawlowski757e9b22017-08-28 09:56:13 -070019#include <array>
Jakub Pawlowskie47b7692016-09-28 07:36:54 -070020#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
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -070027using ::testing::Args;
Jakub Pawlowski757e9b22017-08-28 09:56:13 -070028using ::testing::Contains;
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -070029using ::testing::ElementsAreArray;
Jakub Pawlowski1dd77512016-10-10 14:25:52 -070030using ::testing::Exactly;
Jakub Pawlowski757e9b22017-08-28 09:56:13 -070031using ::testing::Field;
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -070032using ::testing::IsEmpty;
Jakub Pawlowskie47b7692016-09-28 07:36:54 -070033using ::testing::SaveArg;
Jakub Pawlowski757e9b22017-08-28 09:56:13 -070034using ::testing::SizeIs;
35using ::testing::_;
Jakub Pawlowski1f62c122017-04-04 04:08:19 -070036using base::Bind;
Jakub Pawlowskie47b7692016-09-28 07:36:54 -070037using status_cb = BleAdvertiserHciInterface::status_cb;
Jakub Pawlowski256afc42017-03-17 12:31:42 -070038using parameters_cb = BleAdvertiserHciInterface::parameters_cb;
Jakub Pawlowski757e9b22017-08-28 09:56:13 -070039using SetEnableData = BleAdvertiserHciInterface::SetEnableData;
Jakub Pawlowskie47b7692016-09-28 07:36:54 -070040
41const int num_adv_instances = 16;
42
43/* Below are methods that must be implemented if we don't want to compile the
44 * whole stack. They will be removed, or changed into mocks one by one in the
45 * future, as the refactoring progresses */
Jakub Pawlowskie47b7692016-09-28 07:36:54 -070046bool BTM_BleLocalPrivacyEnabled() { return true; }
Myles Watson911d1ae2016-11-28 16:44:40 -080047uint16_t BTM_ReadDiscoverability(uint16_t* p_window, uint16_t* p_interval) {
Jakub Pawlowskie47b7692016-09-28 07:36:54 -070048 return true;
49}
Jakub Pawlowski73af4032017-08-18 04:14:22 -070050void btm_acl_update_conn_addr(uint16_t conn_handle, const RawAddress& address) {
51}
Jakub Pawlowskiae572112018-06-14 17:40:34 -070052void btm_gen_resolvable_private_addr(
53 base::Callback<void(const RawAddress& rpa)> cb) {
54 cb.Run(RawAddress::kEmpty);
Jakub Pawlowski8c7d0602017-03-16 14:47:55 -070055}
Jakub Pawlowski1f62c122017-04-04 04:08:19 -070056
57alarm_callback_t last_alarm_cb = nullptr;
58void* last_alarm_data = nullptr;
Jack Hed97e0952018-08-15 22:17:23 -070059void alarm_set_on_mloop(alarm_t* alarm, uint64_t interval_ms,
Jakub Pawlowskibe8bbd72017-09-08 11:26:25 -070060 alarm_callback_t cb, void* data) {
Jakub Pawlowski1f62c122017-04-04 04:08:19 -070061 last_alarm_cb = cb;
62 last_alarm_data = data;
Jakub Pawlowskie47b7692016-09-28 07:36:54 -070063}
Jakub Pawlowski1f62c122017-04-04 04:08:19 -070064
Myles Watson911d1ae2016-11-28 16:44:40 -080065void alarm_cancel(alarm_t* alarm) {}
66alarm_t* alarm_new_periodic(const char* name) { return nullptr; }
67alarm_t* alarm_new(const char* name) { return nullptr; }
68void alarm_free(alarm_t* alarm) {}
69const controller_t* controller_get_interface() { return nullptr; }
Jakub Pawlowskie47b7692016-09-28 07:36:54 -070070
71namespace {
Jakub Pawlowski1f62c122017-04-04 04:08:19 -070072void DoNothing(uint8_t) {}
73
74void DoNothing2(uint8_t, uint8_t) {}
75
76void 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}
Jakub Pawlowskie47b7692016-09-28 07:36:54 -070081
Jakub Pawlowski331c8012017-03-16 22:10:32 -070082constexpr uint8_t INTERMEDIATE =
83 0x00; // Intermediate fragment of fragmented data
84constexpr uint8_t FIRST = 0x01; // First fragment of fragmented data
85constexpr uint8_t LAST = 0x02; // Last fragment of fragmented data
86constexpr uint8_t COMPLETE = 0x03; // Complete extended advertising data
87
Jakub Pawlowskie47b7692016-09-28 07:36:54 -070088class AdvertiserHciMock : public BleAdvertiserHciInterface {
89 public:
90 AdvertiserHciMock() = default;
91 ~AdvertiserHciMock() override = default;
92
Myles Watson911d1ae2016-11-28 16:44:40 -080093 MOCK_METHOD1(ReadInstanceCount,
94 void(base::Callback<void(uint8_t /* inst_cnt*/)>));
95 MOCK_METHOD1(SetAdvertisingEventObserver,
96 void(AdvertisingEventObserver* observer));
Jakub Pawlowski69988822016-12-01 15:32:58 -080097 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));
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700101 MOCK_METHOD3(SetRandomAddress, void(uint8_t, const RawAddress&, status_cb));
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700102 MOCK_METHOD3(Enable, void(uint8_t, std::vector<SetEnableData>, status_cb));
Lakshmipathi K8e774d12017-03-13 10:35:12 -0700103 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));
Jakub Pawlowskibb858fb2017-03-11 16:40:52 -0800108 MOCK_METHOD2(RemoveAdvertisingSet, void(uint8_t, status_cb));
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700109 MOCK_METHOD1(ClearAdvertisingSets, void(status_cb));
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700110
Jakub Pawlowski69988822016-12-01 15:32:58 -0800111 MOCK_METHOD9(SetParameters1,
112 void(uint8_t, uint16_t, uint32_t, uint32_t, uint8_t, uint8_t,
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700113 const RawAddress&, uint8_t, const RawAddress&));
Jakub Pawlowskicd0d1892017-02-24 11:35:07 -0800114 MOCK_METHOD8(SetParameters2, void(uint8_t, int8_t, uint8_t, uint8_t, uint8_t,
Jakub Pawlowski256afc42017-03-17 12:31:42 -0700115 uint8_t, uint8_t, parameters_cb));
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700116
Jakub Pawlowski69988822016-12-01 15:32:58 -0800117 void SetParameters(uint8_t handle, uint16_t properties, uint32_t adv_int_min,
118 uint32_t adv_int_max, uint8_t channel_map,
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700119 uint8_t own_address_type, const RawAddress& own_address,
120 uint8_t peer_address_type, const RawAddress& peer_address,
Jakub Pawlowskicd0d1892017-02-24 11:35:07 -0800121 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,
Jakub Pawlowski69988822016-12-01 15:32:58 -0800124 uint8_t scan_request_notify_enable,
Jakub Pawlowski256afc42017-03-17 12:31:42 -0700125 parameters_cb cmd_complete) override {
Jakub Pawlowski69988822016-12-01 15:32:58 -0800126 SetParameters1(handle, properties, adv_int_min, adv_int_max, channel_map,
Jakub Pawlowskicd0d1892017-02-24 11:35:07 -0800127 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);
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700132 };
133
Yi Kong183eace2019-03-29 15:42:58 -0700134 bool QuirkAdvertiserZeroHandle() override { return false; }
Jakub Pawlowski8c7d0602017-03-16 14:47:55 -0700135
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700136 private:
137 DISALLOW_COPY_AND_ASSIGN(AdvertiserHciMock);
138};
139
140} // namespace
141
142class 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;
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800149 int start_advertising_status = -1;
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700150 int start_advertising_set_advertiser_id = -1;
151 int start_advertising_set_tx_power = -1;
152 int start_advertising_set_status = -1;
153
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700154 std::unique_ptr<AdvertiserHciMock> hci_mock;
155
Yi Kong183eace2019-03-29 15:42:58 -0700156 void SetUp() override {
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700157 hci_mock.reset(new AdvertiserHciMock());
Jakub Pawlowski1dd77512016-10-10 14:25:52 -0700158
159 base::Callback<void(uint8_t)> inst_cnt_Cb;
160 EXPECT_CALL(*hci_mock, ReadInstanceCount(_))
Myles Watson911d1ae2016-11-28 16:44:40 -0800161 .Times(Exactly(1))
162 .WillOnce(SaveArg<0>(&inst_cnt_Cb));
Jakub Pawlowski1dd77512016-10-10 14:25:52 -0700163
164 BleAdvertisingManager::Initialize(hci_mock.get());
Jakub Pawlowski3b276562017-01-25 03:06:49 -0800165 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
Jakub Pawlowski1dd77512016-10-10 14:25:52 -0700166
167 // we are a truly gracious fake controller, let the command succeed!
168 inst_cnt_Cb.Run(num_adv_instances);
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700169 }
170
Yi Kong183eace2019-03-29 15:42:58 -0700171 void TearDown() override {
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700172 BleAdvertisingManager::CleanUp();
173 hci_mock.reset();
174 }
175
176 public:
177 void RegistrationCb(uint8_t inst_id, uint8_t status) {
178 reg_inst_id = inst_id;
179 reg_status = status;
180 }
181
Jakub Pawlowski256afc42017-03-17 12:31:42 -0700182 void SetParametersCb(uint8_t status, int8_t tx_power) {
183 set_params_status = status;
184 }
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700185 void SetDataCb(uint8_t status) { set_data_status = status; }
186 void EnableCb(uint8_t status) { enable_status = status; }
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800187 void StartAdvertisingCb(uint8_t status) { start_advertising_status = status; }
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700188 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 }
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700194};
195
196TEST_F(BleAdvertisingManagerTest, test_registration) {
Jakub Pawlowski06325e32016-11-14 11:20:52 -0800197 for (int i = 0; i < num_adv_instances; i++) {
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700198 BleAdvertisingManager::Get()->RegisterAdvertiser(Bind(
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700199 &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.
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700205 BleAdvertisingManager::Get()->RegisterAdvertiser(
206 Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
Jakub Pawlowski318e3102016-11-22 12:51:20 -0800207 EXPECT_EQ(ADVERTISE_FAILED_TOO_MANY_ADVERTISERS, reg_status);
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700208 // Don't bother checking inst_id, it doesn't matter
209
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700210 status_cb remove_cb;
211 EXPECT_CALL(*hci_mock, RemoveAdvertisingSet(_, _))
212 .Times(1)
213 .WillOnce(SaveArg<1>(&remove_cb));
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700214 BleAdvertisingManager::Get()->Unregister(5);
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700215 remove_cb.Run(0);
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700216
217 // One advertiser was freed, so should be able to register one now
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700218 BleAdvertisingManager::Get()->RegisterAdvertiser(
219 Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700220 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
221 EXPECT_EQ(5, reg_inst_id);
222}
223
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800224/* This test verifies that the following flow is working correctly: register,
225 * set parameters, set data, enable, ... (advertise) ..., unregister*/
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700226TEST_F(BleAdvertisingManagerTest, test_android_flow) {
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700227 BleAdvertisingManager::Get()->RegisterAdvertiser(
228 Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700229 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
230 int advertiser_id = reg_inst_id;
231
Jakub Pawlowski256afc42017-03-17 12:31:42 -0700232 parameters_cb set_params_cb;
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700233 tBTM_BLE_ADV_PARAMS params;
Jakub Pawlowski69988822016-12-01 15:32:58 -0800234 EXPECT_CALL(*hci_mock, SetParameters1(advertiser_id, _, _, _, _, _, _, _, _))
235 .Times(1);
Jakub Pawlowskicd0d1892017-02-24 11:35:07 -0800236 EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, _, _, _, _, _))
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700237 .Times(1)
Jakub Pawlowskicd0d1892017-02-24 11:35:07 -0800238 .WillOnce(SaveArg<7>(&set_params_cb));
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700239 BleAdvertisingManager::Get()->SetParameters(
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700240 advertiser_id, &params,
241 Bind(&BleAdvertisingManagerTest::SetParametersCb,
242 base::Unretained(this)));
Jakub Pawlowski3b276562017-01-25 03:06:49 -0800243 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700244
Jakub Pawlowski1dd77512016-10-10 14:25:52 -0700245 // we are a truly gracious fake controller, let the command succeed!
Jakub Pawlowski256afc42017-03-17 12:31:42 -0700246 set_params_cb.Run(0, 0);
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700247 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_params_status);
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700248
249 status_cb set_data_cb;
Jakub Pawlowski69988822016-12-01 15:32:58 -0800250 EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, _, _, _, _, _))
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700251 .Times(1)
Jakub Pawlowski69988822016-12-01 15:32:58 -0800252 .WillOnce(SaveArg<5>(&set_data_cb));
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700253 BleAdvertisingManager::Get()->SetData(
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700254 advertiser_id, false, std::vector<uint8_t>(),
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700255 Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
Jakub Pawlowski3b276562017-01-25 03:06:49 -0800256 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
257
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700258 set_data_cb.Run(0);
259 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700260
261 status_cb enable_cb;
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700262 EXPECT_CALL(*hci_mock,
263 Enable(0x01 /* enable */,
264 AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
265 advertiser_id))),
266 _))
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700267 .Times(1)
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700268 .WillOnce(SaveArg<2>(&enable_cb));
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700269 BleAdvertisingManager::Get()->Enable(
270 advertiser_id, true,
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700271 Bind(&BleAdvertisingManagerTest::EnableCb, base::Unretained(this)), 0, 0,
272 base::Callback<void(uint8_t)>());
Jakub Pawlowski3b276562017-01-25 03:06:49 -0800273 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
274
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700275 enable_cb.Run(0);
276 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, enable_status);
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700277
278 /* fake controller should be advertising */
279
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700280 EXPECT_CALL(*hci_mock,
281 Enable(0x00 /* disable */,
282 AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
283 advertiser_id))),
284 _))
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700285 .Times(1)
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700286 .WillOnce(SaveArg<2>(&enable_cb));
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700287 status_cb remove_cb;
288 EXPECT_CALL(*hci_mock, RemoveAdvertisingSet(_, _))
289 .Times(1)
290 .WillOnce(SaveArg<1>(&remove_cb));
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700291 BleAdvertisingManager::Get()->Unregister(advertiser_id);
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700292 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
Jakub Pawlowski3b276562017-01-25 03:06:49 -0800293
294 enable_cb.Run(0);
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700295 remove_cb.Run(0);
Jakub Pawlowskie47b7692016-09-28 07:36:54 -0700296}
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700297
298/* This test verifies that when advertising data is set, tx power and flags will
299 * be properly filled. */
300TEST_F(BleAdvertisingManagerTest, test_adv_data_filling) {
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700301 BleAdvertisingManager::Get()->RegisterAdvertiser(
302 Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700303 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
304 int advertiser_id = reg_inst_id;
305
Jakub Pawlowski256afc42017-03-17 12:31:42 -0700306 parameters_cb set_params_cb;
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700307 tBTM_BLE_ADV_PARAMS params;
Jakub Pawlowskie0750ca2016-12-05 11:46:23 -0800308 params.advertising_event_properties =
309 BleAdvertisingManager::advertising_prop_legacy_connectable;
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700310 params.tx_power = -15;
Jakub Pawlowski69988822016-12-01 15:32:58 -0800311 EXPECT_CALL(*hci_mock, SetParameters1(advertiser_id, _, _, _, _, _, _, _, _))
312 .Times(1);
Jakub Pawlowskicd0d1892017-02-24 11:35:07 -0800313 EXPECT_CALL(*hci_mock, SetParameters2(_, params.tx_power, _, _, _, _, _, _))
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700314 .Times(1)
Jakub Pawlowskicd0d1892017-02-24 11:35:07 -0800315 .WillOnce(SaveArg<7>(&set_params_cb));
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700316 BleAdvertisingManager::Get()->SetParameters(
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700317 advertiser_id, &params,
318 Bind(&BleAdvertisingManagerTest::SetParametersCb,
319 base::Unretained(this)));
Jakub Pawlowski3b276562017-01-25 03:06:49 -0800320 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700321
322 // let the set parameters command succeed!
Jakub Pawlowski256afc42017-03-17 12:31:42 -0700323 set_params_cb.Run(0, 0);
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700324 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_params_status);
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700325
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 */
Jakub Pawlowskie0750ca2016-12-05 11:46:23 -0800329 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)};
Jakub Pawlowski69988822016-12-01 15:32:58 -0800333 EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, _, _, _, _, _))
334 .With(Args<4, 3>(ElementsAreArray(expected_adv_data)))
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700335 .Times(1)
Jakub Pawlowski69988822016-12-01 15:32:58 -0800336 .WillOnce(SaveArg<5>(&set_data_cb));
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700337 BleAdvertisingManager::Get()->SetData(
338 advertiser_id, false,
339 std::vector<uint8_t>({0x02 /* len */, 0x0A /* tx_power */, 0x00}),
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700340 Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
Jakub Pawlowski3b276562017-01-25 03:06:49 -0800341 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
342
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700343 set_data_cb.Run(0);
344 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700345}
346
347/* This test verifies that when advertising is non-connectable, flags will not
348 * be added. */
349TEST_F(BleAdvertisingManagerTest, test_adv_data_not_filling) {
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700350 BleAdvertisingManager::Get()->RegisterAdvertiser(
351 Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700352 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, reg_status);
353 int advertiser_id = reg_inst_id;
354
Jakub Pawlowski256afc42017-03-17 12:31:42 -0700355 parameters_cb set_params_cb;
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700356 tBTM_BLE_ADV_PARAMS params;
Jakub Pawlowskie0750ca2016-12-05 11:46:23 -0800357 params.advertising_event_properties =
358 BleAdvertisingManager::advertising_prop_legacy_non_connectable;
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700359 params.tx_power = -15;
Jakub Pawlowski69988822016-12-01 15:32:58 -0800360 EXPECT_CALL(*hci_mock, SetParameters1(advertiser_id, _, _, _, _, _, _, _, _))
361 .Times(1);
362 EXPECT_CALL(*hci_mock,
Jakub Pawlowskicd0d1892017-02-24 11:35:07 -0800363 SetParameters2(_, (uint8_t)params.tx_power, _, _, _, _, _, _))
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700364 .Times(1)
Jakub Pawlowskicd0d1892017-02-24 11:35:07 -0800365 .WillOnce(SaveArg<7>(&set_params_cb));
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700366 BleAdvertisingManager::Get()->SetParameters(
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700367 advertiser_id, &params,
368 Bind(&BleAdvertisingManagerTest::SetParametersCb,
369 base::Unretained(this)));
Jakub Pawlowski3b276562017-01-25 03:06:49 -0800370 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700371
372 // let the set parameters command succeed!
Jakub Pawlowski256afc42017-03-17 12:31:42 -0700373 set_params_cb.Run(0, -15);
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700374 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_params_status);
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700375
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 */};
Jakub Pawlowski69988822016-12-01 15:32:58 -0800380 EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, _, _, _, _, _))
381 .With(Args<4, 3>(ElementsAreArray(expected_adv_data)))
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700382 .Times(1)
Jakub Pawlowski69988822016-12-01 15:32:58 -0800383 .WillOnce(SaveArg<5>(&set_data_cb));
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700384 BleAdvertisingManager::Get()->SetData(
385 advertiser_id, false, std::vector<uint8_t>({0x02 /* len */, 0xFF, 0x01}),
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700386 Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
Jakub Pawlowski3b276562017-01-25 03:06:49 -0800387 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
388
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700389 set_data_cb.Run(0);
390 EXPECT_EQ(BTM_BLE_MULTI_ADV_SUCCESS, set_data_status);
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700391}
Jakub Pawlowski06325e32016-11-14 11:20:52 -0800392
393TEST_F(BleAdvertisingManagerTest, test_reenabling) {
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700394 BleAdvertisingManager::Get()->RegisterAdvertiser(
395 Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
Jakub Pawlowski06325e32016-11-14 11:20:52 -0800396 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;
Jakub Pawlowski06325e32016-11-14 11:20:52 -0800400 status_cb enable_cb;
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700401 EXPECT_CALL(*hci_mock,
402 Enable(0x01 /* enable */,
403 AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
404 advertiser_id))),
405 _))
Jakub Pawlowski06325e32016-11-14 11:20:52 -0800406 .Times(1)
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700407 .WillOnce(SaveArg<2>(&enable_cb));
408 BleAdvertisingManager::Get()->Enable(advertiser_id, true, Bind(DoNothing), 0,
409 0, Bind(DoNothing));
410 enable_cb.Run(0);
Jakub Pawlowski06325e32016-11-14 11:20:52 -0800411 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
Jakub Pawlowski3b276562017-01-25 03:06:49 -0800412
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700413 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);
Jakub Pawlowski3b276562017-01-25 03:06:49 -0800422 enable_cb.Run(0);
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700423 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
Jakub Pawlowski06325e32016-11-14 11:20:52 -0800424}
425
426/* Make sure that instance is not reenabled if it's already disabled */
427TEST_F(BleAdvertisingManagerTest, test_reenabling_disabled_instance) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800428 uint8_t advertiser_id = 1; // any unregistered value
Jakub Pawlowski06325e32016-11-14 11:20:52 -0800429
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700430 EXPECT_CALL(*hci_mock, Enable(_, _, _)).Times(Exactly(0));
Jakub Pawlowski69988822016-12-01 15:32:58 -0800431 BleAdvertisingManager::Get()->OnAdvertisingSetTerminated(advertiser_id, 0x00,
432 0x05, 0x00);
Jakub Pawlowski06325e32016-11-14 11:20:52 -0800433 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800434}
435
436/* This test verifies that the only flow that is currently used on Android, is
437 * working correctly in happy case scenario. */
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700438TEST_F(BleAdvertisingManagerTest, test_start_advertising_set) {
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800439 std::vector<uint8_t> adv_data;
440 std::vector<uint8_t> scan_resp;
441 tBTM_BLE_ADV_PARAMS params;
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700442 tBLE_PERIODIC_ADV_PARAMS periodic_params;
443 periodic_params.enable = false;
444 std::vector<uint8_t> periodic_data;
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800445
Jakub Pawlowski256afc42017-03-17 12:31:42 -0700446 parameters_cb set_params_cb;
Jakub Pawlowski8c7d0602017-03-16 14:47:55 -0700447 status_cb set_address_cb;
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800448 status_cb set_data_cb;
449 status_cb set_scan_resp_data_cb;
450 status_cb enable_cb;
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700451 EXPECT_CALL(*hci_mock, SetParameters1(_, _, _, _, _, _, _, _, _)).Times(1);
Jakub Pawlowskicd0d1892017-02-24 11:35:07 -0800452 EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, _, _, _, _, _))
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800453 .Times(1)
Jakub Pawlowskicd0d1892017-02-24 11:35:07 -0800454 .WillOnce(SaveArg<7>(&set_params_cb));
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700455 EXPECT_CALL(*hci_mock, SetRandomAddress(_, _, _))
Jakub Pawlowski8c7d0602017-03-16 14:47:55 -0700456 .Times(1)
457 .WillOnce(SaveArg<2>(&set_address_cb));
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700458 EXPECT_CALL(*hci_mock, SetAdvertisingData(_, _, _, _, _, _))
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800459 .Times(1)
Jakub Pawlowski69988822016-12-01 15:32:58 -0800460 .WillOnce(SaveArg<5>(&set_data_cb));
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700461 EXPECT_CALL(*hci_mock, SetScanResponseData(_, _, _, _, _, _))
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800462 .Times(1)
Jakub Pawlowski69988822016-12-01 15:32:58 -0800463 .WillOnce(SaveArg<5>(&set_scan_resp_data_cb));
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700464 EXPECT_CALL(*hci_mock, Enable(0x01 /* enable */, _, _))
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800465 .Times(1)
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700466 .WillOnce(SaveArg<2>(&enable_cb));
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800467
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700468 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));
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800473
474 // we are a truly gracious fake controller, let the commands succeed!
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700475 int selected_tx_power = -15;
476 set_params_cb.Run(0, selected_tx_power);
Jakub Pawlowski8c7d0602017-03-16 14:47:55 -0700477 set_address_cb.Run(0);
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800478 set_data_cb.Run(0);
479 set_scan_resp_data_cb.Run(0);
480 enable_cb.Run(0);
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700481 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;
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800484 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
485
486 // ... advertising ...
487
488 // Disable advertiser
489 status_cb disable_cb;
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700490 EXPECT_CALL(*hci_mock,
491 Enable(0x00 /* disable */,
492 AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
493 advertiser_id))),
494 _))
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800495 .Times(1)
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700496 .WillOnce(SaveArg<2>(&disable_cb));
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700497 status_cb remove_cb;
498 EXPECT_CALL(*hci_mock, RemoveAdvertisingSet(advertiser_id, _))
499 .Times(1)
500 .WillOnce(SaveArg<1>(&remove_cb));
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800501 BleAdvertisingManager::Get()->Unregister(advertiser_id);
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800502 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
Jakub Pawlowski3b276562017-01-25 03:06:49 -0800503
504 disable_cb.Run(0);
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700505 remove_cb.Run(0);
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800506}
507
508TEST_F(BleAdvertisingManagerTest, test_start_advertising_set_params_failed) {
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700509 BleAdvertisingManager::Get()->RegisterAdvertiser(
510 Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800511 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
Jakub Pawlowski256afc42017-03-17 12:31:42 -0700518 parameters_cb set_params_cb;
Jakub Pawlowski69988822016-12-01 15:32:58 -0800519 EXPECT_CALL(*hci_mock, SetParameters1(advertiser_id, _, _, _, _, _, _, _, _))
520 .Times(1);
Jakub Pawlowskicd0d1892017-02-24 11:35:07 -0800521 EXPECT_CALL(*hci_mock, SetParameters2(_, _, _, _, _, _, _, _))
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800522 .Times(1)
Jakub Pawlowskicd0d1892017-02-24 11:35:07 -0800523 .WillOnce(SaveArg<7>(&set_params_cb));
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800524
Jakub Pawlowski69988822016-12-01 15:32:58 -0800525 EXPECT_CALL(*hci_mock, SetAdvertisingData(advertiser_id, _, _, _, _, _))
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800526 .Times(Exactly(0));
527
528 BleAdvertisingManager::Get()->StartAdvertising(
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700529 advertiser_id,
530 Bind(&BleAdvertisingManagerTest::StartAdvertisingCb,
531 base::Unretained(this)),
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800532 &params, adv_data, scan_resp, 0, base::Callback<void(uint8_t)>());
Jakub Pawlowski3b276562017-01-25 03:06:49 -0800533 ::testing::Mock::VerifyAndClearExpectations(hci_mock.get());
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800534
535 // set params failed
Jakub Pawlowski256afc42017-03-17 12:31:42 -0700536 set_params_cb.Run(0x01, 0);
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800537
538 // Expect the whole flow to fail right away
539 EXPECT_EQ(BTM_BLE_MULTI_ADV_FAILURE, start_advertising_status);
Jakub Pawlowskia75087e2016-11-17 13:27:28 -0800540}
Jakub Pawlowski331c8012017-03-16 22:10:32 -0700541
542TEST_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
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700548 BleAdvertisingManager::Get()->RegisterAdvertiser(
549 Bind(&BleAdvertisingManagerTest::RegistrationCb, base::Unretained(this)));
Jakub Pawlowski331c8012017-03-16 22:10:32 -0700550 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,
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700566 Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
Jakub Pawlowski331c8012017-03-16 22:10:32 -0700567 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,
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700588 Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
Jakub Pawlowski331c8012017-03-16 22:10:32 -0700589 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,
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700606 Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
Jakub Pawlowski331c8012017-03-16 22:10:32 -0700607 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,
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700624 Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
Jakub Pawlowski331c8012017-03-16 22:10:32 -0700625 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,
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700640 Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
Jakub Pawlowski331c8012017-03-16 22:10:32 -0700641 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,
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700654 Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
Jakub Pawlowski331c8012017-03-16 22:10:32 -0700655 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,
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700668 Bind(&BleAdvertisingManagerTest::SetDataCb, base::Unretained(this)));
Jakub Pawlowski331c8012017-03-16 22:10:32 -0700669 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}
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700674
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.*/
677TEST_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));
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700707 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 _))
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700715 .Times(1)
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700716 .WillOnce(SaveArg<2>(&enable_cb));
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700717
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));
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700752 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 _))
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700760 .Times(1)
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700761 .WillOnce(SaveArg<2>(&enable_cb));
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700762 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;
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700772 EXPECT_CALL(*hci_mock,
773 Enable(0x00 /* disable */,
774 AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
775 advertiser_id))),
776 _))
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700777 .Times(1)
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700778 .WillOnce(SaveArg<2>(&disable_cb));
Jakub Pawlowski1f62c122017-04-04 04:08:19 -0700779 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}
Sunny Kapdic15f64f2017-06-02 18:11:05 -0700789
790/* This test makes sure that periodic advertising is stopped before
791 * unregistering the advertiser, if it was enabled. */
792TEST_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));
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700831 EXPECT_CALL(*hci_mock, Enable(0x01 /* enable */, _, _))
Sunny Kapdic15f64f2017-06-02 18:11:05 -0700832 .Times(1)
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700833 .WillOnce(SaveArg<2>(&enable_cb));
Sunny Kapdic15f64f2017-06-02 18:11:05 -0700834
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;
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700860 EXPECT_CALL(*hci_mock,
861 Enable(0x00 /* disable */,
862 AllOf(SizeIs(1), Contains(Field(&SetEnableData::handle,
863 advertiser_id))),
864 _))
Sunny Kapdic15f64f2017-06-02 18:11:05 -0700865 .Times(1)
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700866 .WillOnce(SaveArg<2>(&disable_cb));
Sunny Kapdic15f64f2017-06-02 18:11:05 -0700867 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}
Jakub Pawlowski757e9b22017-08-28 09:56:13 -0700883
884TEST_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.*/
952TEST_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
Jakub Pawlowskiba726292017-06-22 09:46:18 -07001059/* This test verifies that stack cleanup, and shutdown happening while there is
1060 * outstanding HCI command is not triggering the callback */
1061TEST_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
Jakub Pawlowski757e9b22017-08-28 09:56:13 -07001106extern void testRecomputeTimeout1();
1107extern void testRecomputeTimeout2();
1108extern void testRecomputeTimeout3();
1109
1110TEST_F(BleAdvertisingManagerTest, test_recompute_timeout) {
1111 testRecomputeTimeout1();
1112 testRecomputeTimeout2();
1113 testRecomputeTimeout3();
1114}