1 /*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <gtest/gtest.h>
18
19 #include "model/controller/link_layer_controller.h"
20 #include "test_helpers.h"
21
22 namespace rootcanal {
23
24 using namespace bluetooth::hci;
25
26 class LeSetExtendedAdvertisingDataTest : public ::testing::Test {
27 public:
LeSetExtendedAdvertisingDataTest()28 LeSetExtendedAdvertisingDataTest() {
29 // Reduce the number of advertising sets to simplify testing.
30 properties_.le_num_supported_advertising_sets = 2;
31 properties_.le_max_advertising_data_length = 300;
32 };
33 ~LeSetExtendedAdvertisingDataTest() override = default;
34
35 protected:
36 Address address_{0};
37 ControllerProperties properties_{};
38 LinkLayerController controller_{address_, properties_};
39 };
40
TEST_F(LeSetExtendedAdvertisingDataTest,Complete)41 TEST_F(LeSetExtendedAdvertisingDataTest, Complete) {
42 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
43 0, MakeAdvertisingEventProperties(CONNECTABLE), 0x0800, 0x0800,
44 0x7, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
45 PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
46 Address::kEmpty, AdvertisingFilterPolicy::ALL_DEVICES, 0x70,
47 PrimaryPhyType::LE_1M, 0, SecondaryPhyType::LE_2M, 0x0, false),
48 ErrorCode::SUCCESS);
49
50 std::vector<uint8_t> advertising_data = {1, 2, 3};
51 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
52 0, Operation::COMPLETE_ADVERTISEMENT,
53 FragmentPreference::CONTROLLER_MAY_FRAGMENT, advertising_data),
54 ErrorCode::SUCCESS);
55 }
56
TEST_F(LeSetExtendedAdvertisingDataTest,Unchanged)57 TEST_F(LeSetExtendedAdvertisingDataTest, Unchanged) {
58 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
59 0, MakeAdvertisingEventProperties(CONNECTABLE), 0x0800, 0x0800,
60 0x7, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
61 PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
62 Address::kEmpty, AdvertisingFilterPolicy::ALL_DEVICES, 0x70,
63 PrimaryPhyType::LE_1M, 0, SecondaryPhyType::LE_2M, 0x0, false),
64 ErrorCode::SUCCESS);
65
66 std::vector<uint8_t> advertising_data = {1, 2, 3};
67 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
68 0, Operation::COMPLETE_ADVERTISEMENT,
69 FragmentPreference::CONTROLLER_MAY_FRAGMENT, advertising_data),
70 ErrorCode::SUCCESS);
71
72 ASSERT_EQ(controller_.LeSetExtendedAdvertisingEnable(
73 true, {MakeEnabledSet(0, 0, 0)}),
74 ErrorCode::SUCCESS);
75
76 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
77 0, Operation::UNCHANGED_DATA,
78 FragmentPreference::CONTROLLER_MAY_FRAGMENT, {}),
79 ErrorCode::SUCCESS);
80 }
81
TEST_F(LeSetExtendedAdvertisingDataTest,Fragmented)82 TEST_F(LeSetExtendedAdvertisingDataTest, Fragmented) {
83 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
84 0, MakeAdvertisingEventProperties(CONNECTABLE), 0x0800, 0x0800,
85 0x7, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
86 PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
87 Address::kEmpty, AdvertisingFilterPolicy::ALL_DEVICES, 0x70,
88 PrimaryPhyType::LE_1M, 0, SecondaryPhyType::LE_2M, 0x0, false),
89 ErrorCode::SUCCESS);
90
91 std::vector<uint8_t> first_advertising_data_fragment = {1, 2, 3};
92 std::vector<uint8_t> intermediate_advertising_data_fragment = {4, 5, 6};
93 std::vector<uint8_t> last_advertising_data_fragment = {7, 8, 9};
94
95 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
96 0, Operation::FIRST_FRAGMENT,
97 FragmentPreference::CONTROLLER_MAY_FRAGMENT,
98 first_advertising_data_fragment),
99 ErrorCode::SUCCESS);
100
101 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
102 0, Operation::INTERMEDIATE_FRAGMENT,
103 FragmentPreference::CONTROLLER_MAY_FRAGMENT,
104 intermediate_advertising_data_fragment),
105 ErrorCode::SUCCESS);
106
107 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
108 0, Operation::LAST_FRAGMENT,
109 FragmentPreference::CONTROLLER_MAY_FRAGMENT,
110 last_advertising_data_fragment),
111 ErrorCode::SUCCESS);
112 }
113
TEST_F(LeSetExtendedAdvertisingDataTest,UnknownAdvertisingHandle)114 TEST_F(LeSetExtendedAdvertisingDataTest, UnknownAdvertisingHandle) {
115 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
116 0, MakeAdvertisingEventProperties(CONNECTABLE), 0x0800, 0x0800,
117 0x7, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
118 PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
119 Address::kEmpty, AdvertisingFilterPolicy::ALL_DEVICES, 0x70,
120 PrimaryPhyType::LE_1M, 0, SecondaryPhyType::LE_2M, 0x0, false),
121 ErrorCode::SUCCESS);
122
123 std::vector<uint8_t> advertising_data = {1, 2, 3};
124 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
125 1, Operation::COMPLETE_ADVERTISEMENT,
126 FragmentPreference::CONTROLLER_MAY_FRAGMENT, advertising_data),
127 ErrorCode::UNKNOWN_ADVERTISING_IDENTIFIER);
128 }
129
TEST_F(LeSetExtendedAdvertisingDataTest,UnexpectedAdvertisingData)130 TEST_F(LeSetExtendedAdvertisingDataTest, UnexpectedAdvertisingData) {
131 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
132 0, MakeAdvertisingEventProperties(SCANNABLE), 0x0800, 0x0800,
133 0x7, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
134 PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
135 Address::kEmpty, AdvertisingFilterPolicy::ALL_DEVICES, 0x70,
136 PrimaryPhyType::LE_1M, 0, SecondaryPhyType::LE_2M, 0x0, false),
137 ErrorCode::SUCCESS);
138
139 std::vector<uint8_t> advertising_data = {1, 2, 3};
140 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
141 0, Operation::COMPLETE_ADVERTISEMENT,
142 FragmentPreference::CONTROLLER_MAY_FRAGMENT, advertising_data),
143 ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
144 }
145
TEST_F(LeSetExtendedAdvertisingDataTest,IncompleteLegacyAdvertisingData)146 TEST_F(LeSetExtendedAdvertisingDataTest, IncompleteLegacyAdvertisingData) {
147 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
148 0, MakeAdvertisingEventProperties(LEGACY | SCANNABLE), 0x0800,
149 0x0800, 0x7, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
150 PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
151 Address::kEmpty, AdvertisingFilterPolicy::ALL_DEVICES, 0x70,
152 PrimaryPhyType::LE_1M, 0, SecondaryPhyType::LE_2M, 0x0, false),
153 ErrorCode::SUCCESS);
154
155 std::vector<uint8_t> first_advertising_data_fragment = {1, 2, 3};
156 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
157 0, Operation::FIRST_FRAGMENT,
158 FragmentPreference::CONTROLLER_MAY_FRAGMENT,
159 first_advertising_data_fragment),
160 ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
161 }
162
TEST_F(LeSetExtendedAdvertisingDataTest,InvalidLegacyAdvertisingData)163 TEST_F(LeSetExtendedAdvertisingDataTest, InvalidLegacyAdvertisingData) {
164 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
165 0, MakeAdvertisingEventProperties(LEGACY | SCANNABLE), 0x0800,
166 0x0800, 0x7, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
167 PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
168 Address::kEmpty, AdvertisingFilterPolicy::ALL_DEVICES, 0x70,
169 PrimaryPhyType::LE_1M, 0, SecondaryPhyType::LE_2M, 0x0, false),
170 ErrorCode::SUCCESS);
171
172 std::vector<uint8_t> advertising_data = {1, 2, 3};
173 advertising_data.resize(32);
174 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
175 0, Operation::COMPLETE_ADVERTISEMENT,
176 FragmentPreference::CONTROLLER_MAY_FRAGMENT, advertising_data),
177 ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
178 }
179
TEST_F(LeSetExtendedAdvertisingDataTest,UnchangedWhenDisabled)180 TEST_F(LeSetExtendedAdvertisingDataTest, UnchangedWhenDisabled) {
181 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
182 0, MakeAdvertisingEventProperties(CONNECTABLE), 0x0800, 0x0800,
183 0x7, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
184 PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
185 Address::kEmpty, AdvertisingFilterPolicy::ALL_DEVICES, 0x70,
186 PrimaryPhyType::LE_1M, 0, SecondaryPhyType::LE_2M, 0x0, false),
187 ErrorCode::SUCCESS);
188
189 std::vector<uint8_t> advertising_data = {1, 2, 3};
190 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
191 0, Operation::COMPLETE_ADVERTISEMENT,
192 FragmentPreference::CONTROLLER_MAY_FRAGMENT, advertising_data),
193 ErrorCode::SUCCESS);
194
195 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
196 0, Operation::UNCHANGED_DATA,
197 FragmentPreference::CONTROLLER_MAY_FRAGMENT, advertising_data),
198 ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
199 }
200
TEST_F(LeSetExtendedAdvertisingDataTest,UnchangedWhenAdvertisingDataEmpty)201 TEST_F(LeSetExtendedAdvertisingDataTest, UnchangedWhenAdvertisingDataEmpty) {
202 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
203 0, MakeAdvertisingEventProperties(CONNECTABLE), 0x0800, 0x0800,
204 0x7, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
205 PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
206 Address::kEmpty, AdvertisingFilterPolicy::ALL_DEVICES, 0x70,
207 PrimaryPhyType::LE_1M, 0, SecondaryPhyType::LE_2M, 0x0, false),
208 ErrorCode::SUCCESS);
209
210 ASSERT_EQ(controller_.LeSetExtendedAdvertisingEnable(
211 true, {MakeEnabledSet(0, 0, 0)}),
212 ErrorCode::SUCCESS);
213
214 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
215 0, Operation::UNCHANGED_DATA,
216 FragmentPreference::CONTROLLER_MAY_FRAGMENT, {}),
217 ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
218 }
219
TEST_F(LeSetExtendedAdvertisingDataTest,UnchangedWhenUsingLegacyAdvertising)220 TEST_F(LeSetExtendedAdvertisingDataTest, UnchangedWhenUsingLegacyAdvertising) {
221 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
222 0, MakeAdvertisingEventProperties(LEGACY | SCANNABLE), 0x0800,
223 0x0800, 0x7, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
224 PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
225 Address::kEmpty, AdvertisingFilterPolicy::ALL_DEVICES, 0x70,
226 PrimaryPhyType::LE_1M, 0, SecondaryPhyType::LE_2M, 0x0, false),
227 ErrorCode::SUCCESS);
228
229 ASSERT_EQ(controller_.LeSetExtendedAdvertisingEnable(
230 true, {MakeEnabledSet(0, 0, 0)}),
231 ErrorCode::SUCCESS);
232
233 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
234 0, Operation::UNCHANGED_DATA,
235 FragmentPreference::CONTROLLER_MAY_FRAGMENT, {}),
236 ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
237 }
238
TEST_F(LeSetExtendedAdvertisingDataTest,EmptyAdvertisingDataFragment)239 TEST_F(LeSetExtendedAdvertisingDataTest, EmptyAdvertisingDataFragment) {
240 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
241 0, MakeAdvertisingEventProperties(CONNECTABLE), 0x0800, 0x0800,
242 0x7, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
243 PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
244 Address::kEmpty, AdvertisingFilterPolicy::ALL_DEVICES, 0x70,
245 PrimaryPhyType::LE_1M, 0, SecondaryPhyType::LE_2M, 0x0, false),
246 ErrorCode::SUCCESS);
247
248 std::vector<uint8_t> first_advertising_data_fragment = {1, 2, 3};
249 std::vector<uint8_t> intermediate_advertising_data_fragment = {4, 5, 6};
250 std::vector<uint8_t> last_advertising_data_fragment = {7, 8, 9};
251
252 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
253 0, Operation::FIRST_FRAGMENT,
254 FragmentPreference::CONTROLLER_MAY_FRAGMENT, {}),
255 ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
256
257 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
258 0, Operation::FIRST_FRAGMENT,
259 FragmentPreference::CONTROLLER_MAY_FRAGMENT,
260 first_advertising_data_fragment),
261 ErrorCode::SUCCESS);
262
263 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
264 0, Operation::INTERMEDIATE_FRAGMENT,
265 FragmentPreference::CONTROLLER_MAY_FRAGMENT, {}),
266 ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
267
268 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
269 0, Operation::INTERMEDIATE_FRAGMENT,
270 FragmentPreference::CONTROLLER_MAY_FRAGMENT,
271 intermediate_advertising_data_fragment),
272 ErrorCode::SUCCESS);
273
274 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
275 0, Operation::LAST_FRAGMENT,
276 FragmentPreference::CONTROLLER_MAY_FRAGMENT, {}),
277 ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
278
279 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
280 0, Operation::LAST_FRAGMENT,
281 FragmentPreference::CONTROLLER_MAY_FRAGMENT,
282 last_advertising_data_fragment),
283 ErrorCode::SUCCESS);
284 }
285
TEST_F(LeSetExtendedAdvertisingDataTest,AdvertisingEnabled)286 TEST_F(LeSetExtendedAdvertisingDataTest, AdvertisingEnabled) {
287 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
288 0, MakeAdvertisingEventProperties(CONNECTABLE), 0x0800, 0x0800,
289 0x7, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
290 PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
291 Address::kEmpty, AdvertisingFilterPolicy::ALL_DEVICES, 0x70,
292 PrimaryPhyType::LE_1M, 0, SecondaryPhyType::LE_2M, 0x0, false),
293 ErrorCode::SUCCESS);
294
295 ASSERT_EQ(controller_.LeSetExtendedAdvertisingEnable(
296 true, {MakeEnabledSet(0, 0, 0)}),
297 ErrorCode::SUCCESS);
298
299 std::vector<uint8_t> first_advertising_data_fragment = {1, 2, 3};
300 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
301 0, Operation::FIRST_FRAGMENT,
302 FragmentPreference::CONTROLLER_MAY_FRAGMENT,
303 first_advertising_data_fragment),
304 ErrorCode::COMMAND_DISALLOWED);
305 }
306
TEST_F(LeSetExtendedAdvertisingDataTest,AdvertisingDataLargerThanMemoryCapacity)307 TEST_F(LeSetExtendedAdvertisingDataTest,
308 AdvertisingDataLargerThanMemoryCapacity) {
309 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
310 0, MakeAdvertisingEventProperties(CONNECTABLE), 0x0800, 0x0800,
311 0x7, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
312 PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
313 Address::kEmpty, AdvertisingFilterPolicy::ALL_DEVICES, 0x70,
314 PrimaryPhyType::LE_1M, 0, SecondaryPhyType::LE_2M, 0x0, false),
315 ErrorCode::SUCCESS);
316
317 std::vector<uint8_t> advertising_data_fragment = {1, 2, 3};
318 advertising_data_fragment.resize(properties_.le_max_advertising_data_length);
319
320 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
321 0, Operation::FIRST_FRAGMENT,
322 FragmentPreference::CONTROLLER_MAY_FRAGMENT,
323 advertising_data_fragment),
324 ErrorCode::SUCCESS);
325 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
326 0, Operation::LAST_FRAGMENT,
327 FragmentPreference::CONTROLLER_MAY_FRAGMENT,
328 advertising_data_fragment),
329 ErrorCode::MEMORY_CAPACITY_EXCEEDED);
330 }
331
TEST_F(LeSetExtendedAdvertisingDataTest,AdvertisingDataLargerThanPduCapacity)332 TEST_F(LeSetExtendedAdvertisingDataTest, AdvertisingDataLargerThanPduCapacity) {
333 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
334 0, MakeAdvertisingEventProperties(CONNECTABLE), 0x0800, 0x0800,
335 0x7, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
336 PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
337 Address::kEmpty, AdvertisingFilterPolicy::ALL_DEVICES, 0x70,
338 PrimaryPhyType::LE_1M, 0, SecondaryPhyType::LE_2M, 0x0, false),
339 ErrorCode::SUCCESS);
340
341 ASSERT_EQ(controller_.LeSetExtendedAdvertisingEnable(
342 true, {MakeEnabledSet(0, 0, 0)}),
343 ErrorCode::SUCCESS);
344
345 // No AUX chain possible for connectable advertising PDUs,
346 // the advertising data is limited to one PDU's payload.
347 std::vector<uint8_t> advertising_data = {1, 2, 3};
348 advertising_data.resize(254);
349
350 ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(
351 0, Operation::COMPLETE_ADVERTISEMENT,
352 FragmentPreference::CONTROLLER_MAY_FRAGMENT, advertising_data),
353 ErrorCode::PACKET_TOO_LONG);
354 }
355
356 } // namespace rootcanal
357