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 LeSetExtendedAdvertisingParametersTest : public ::testing::Test {
27 public:
LeSetExtendedAdvertisingParametersTest()28   LeSetExtendedAdvertisingParametersTest() {
29     // Reduce the number of advertising sets to simplify testing.
30     properties_.le_num_supported_advertising_sets = 2;
31   }
32   ~LeSetExtendedAdvertisingParametersTest() override = default;
33 
34 protected:
35   Address address_{0};
36   ControllerProperties properties_{};
37   LinkLayerController controller_{address_, properties_};
38 };
39 
TEST_F(LeSetExtendedAdvertisingParametersTest,Success)40 TEST_F(LeSetExtendedAdvertisingParametersTest, Success) {
41   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
42                     0, MakeAdvertisingEventProperties(CONNECTABLE), 0x0800, 0x0800, 0x7,
43                     OwnAddressType::PUBLIC_DEVICE_ADDRESS,
44                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
45                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
46                     SecondaryPhyType::LE_2M, 0x0, false),
47             ErrorCode::SUCCESS);
48 }
49 
TEST_F(LeSetExtendedAdvertisingParametersTest,LegacyUsed)50 TEST_F(LeSetExtendedAdvertisingParametersTest, LegacyUsed) {
51   ASSERT_EQ(controller_.LeSetScanParameters(LeScanType::PASSIVE, 0x2000, 0x200,
52                                             OwnAddressType::PUBLIC_DEVICE_ADDRESS,
53                                             LeScanningFilterPolicy::ACCEPT_ALL),
54             ErrorCode::SUCCESS);
55 
56   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
57                     0, MakeAdvertisingEventProperties(CONNECTABLE), 0x0800, 0x0800, 0x7,
58                     OwnAddressType::PUBLIC_DEVICE_ADDRESS,
59                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
60                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
61                     SecondaryPhyType::LE_2M, 0x0, false),
62             ErrorCode::COMMAND_DISALLOWED);
63 }
64 
TEST_F(LeSetExtendedAdvertisingParametersTest,AdvertisingSetsFull)65 TEST_F(LeSetExtendedAdvertisingParametersTest, AdvertisingSetsFull) {
66   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
67                     0, MakeAdvertisingEventProperties(CONNECTABLE), 0x0800, 0x0800, 0x7,
68                     OwnAddressType::PUBLIC_DEVICE_ADDRESS,
69                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
70                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
71                     SecondaryPhyType::LE_2M, 0x0, false),
72             ErrorCode::SUCCESS);
73 
74   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
75                     1, MakeAdvertisingEventProperties(CONNECTABLE), 0x0800, 0x0800, 0x7,
76                     OwnAddressType::PUBLIC_DEVICE_ADDRESS,
77                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
78                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
79                     SecondaryPhyType::LE_2M, 0x0, false),
80             ErrorCode::SUCCESS);
81 
82   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
83                     2, MakeAdvertisingEventProperties(CONNECTABLE), 0x0800, 0x0800, 0x7,
84                     OwnAddressType::PUBLIC_DEVICE_ADDRESS,
85                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
86                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
87                     SecondaryPhyType::LE_2M, 0x0, false),
88             ErrorCode::MEMORY_CAPACITY_EXCEEDED);
89 }
90 
TEST_F(LeSetExtendedAdvertisingParametersTest,InvalidLegacyAdvertisingEventProperties)91 TEST_F(LeSetExtendedAdvertisingParametersTest, InvalidLegacyAdvertisingEventProperties) {
92   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
93                     0, MakeAdvertisingEventProperties(LEGACY | DIRECTED | SCANNABLE), 0x0800,
94                     0x0800, 0x7, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
95                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
96                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
97                     SecondaryPhyType::LE_2M, 0x0, false),
98             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
99 }
100 
TEST_F(LeSetExtendedAdvertisingParametersTest,UnexpectedAdvertisingData)101 TEST_F(LeSetExtendedAdvertisingParametersTest, UnexpectedAdvertisingData) {
102   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
103                     0, MakeAdvertisingEventProperties(LEGACY | CONNECTABLE | SCANNABLE), 0x0800,
104                     0x0800, 0x7, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
105                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
106                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
107                     SecondaryPhyType::LE_2M, 0x0, false),
108             ErrorCode::SUCCESS);
109 
110   std::vector<uint8_t> advertising_data = {1, 2, 3};
111   ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(0, Operation::COMPLETE_ADVERTISEMENT,
112                                                      FragmentPreference::CONTROLLER_MAY_FRAGMENT,
113                                                      advertising_data),
114             ErrorCode::SUCCESS);
115 
116   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
117                     0, MakeAdvertisingEventProperties(LEGACY | DIRECTED | CONNECTABLE), 0x0800,
118                     0x0800, 0x7, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
119                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
120                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
121                     SecondaryPhyType::LE_2M, 0x0, false),
122             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
123 }
124 
TEST_F(LeSetExtendedAdvertisingParametersTest,UnexpectedScanResponseData)125 TEST_F(LeSetExtendedAdvertisingParametersTest, UnexpectedScanResponseData) {
126   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
127                     0, MakeAdvertisingEventProperties(LEGACY | CONNECTABLE | SCANNABLE), 0x0800,
128                     0x0800, 0x7, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
129                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
130                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
131                     SecondaryPhyType::LE_2M, 0x0, false),
132             ErrorCode::SUCCESS);
133 
134   std::vector<uint8_t> scan_response_data = {1, 2, 3};
135   ASSERT_EQ(controller_.LeSetExtendedScanResponseData(0, Operation::COMPLETE_ADVERTISEMENT,
136                                                       FragmentPreference::CONTROLLER_MAY_FRAGMENT,
137                                                       scan_response_data),
138             ErrorCode::SUCCESS);
139 
140   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
141                     0, MakeAdvertisingEventProperties(LEGACY | CONNECTABLE), 0x0800, 0x0800, 0x7,
142                     OwnAddressType::PUBLIC_DEVICE_ADDRESS,
143                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
144                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
145                     SecondaryPhyType::LE_2M, 0x0, false),
146             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
147 }
148 
TEST_F(LeSetExtendedAdvertisingParametersTest,InvalidLegacyAdvertisingData)149 TEST_F(LeSetExtendedAdvertisingParametersTest, InvalidLegacyAdvertisingData) {
150   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
151                     0, MakeAdvertisingEventProperties(CONNECTABLE), 0x0800, 0x0800, 0x7,
152                     OwnAddressType::PUBLIC_DEVICE_ADDRESS,
153                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
154                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
155                     SecondaryPhyType::LE_2M, 0x0, false),
156             ErrorCode::SUCCESS);
157 
158   std::vector<uint8_t> advertising_data = {1, 2, 3};
159   advertising_data.resize(32);
160   ASSERT_EQ(controller_.LeSetExtendedAdvertisingData(0, Operation::COMPLETE_ADVERTISEMENT,
161                                                      FragmentPreference::CONTROLLER_MAY_FRAGMENT,
162                                                      advertising_data),
163             ErrorCode::SUCCESS);
164 
165   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
166                     0, MakeAdvertisingEventProperties(LEGACY | CONNECTABLE), 0x0800, 0x0800, 0x7,
167                     OwnAddressType::PUBLIC_DEVICE_ADDRESS,
168                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
169                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
170                     SecondaryPhyType::LE_2M, 0x0, false),
171             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
172 }
173 
TEST_F(LeSetExtendedAdvertisingParametersTest,InvalidLegacyScanResponseData)174 TEST_F(LeSetExtendedAdvertisingParametersTest, InvalidLegacyScanResponseData) {
175   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
176                     0, MakeAdvertisingEventProperties(SCANNABLE), 0x0800, 0x0800, 0x7,
177                     OwnAddressType::PUBLIC_DEVICE_ADDRESS,
178                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
179                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
180                     SecondaryPhyType::LE_2M, 0x0, false),
181             ErrorCode::SUCCESS);
182 
183   std::vector<uint8_t> scan_response_data = {1, 2, 3};
184   scan_response_data.resize(32);
185   ASSERT_EQ(controller_.LeSetExtendedScanResponseData(0, Operation::COMPLETE_ADVERTISEMENT,
186                                                       FragmentPreference::CONTROLLER_MAY_FRAGMENT,
187                                                       scan_response_data),
188             ErrorCode::SUCCESS);
189 
190   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
191                     0, MakeAdvertisingEventProperties(LEGACY | SCANNABLE), 0x0800, 0x0800, 0x7,
192                     OwnAddressType::PUBLIC_DEVICE_ADDRESS,
193                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
194                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
195                     SecondaryPhyType::LE_2M, 0x0, false),
196             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
197 }
198 
TEST_F(LeSetExtendedAdvertisingParametersTest,InvalidExtendedAdvertisingEventProperties)199 TEST_F(LeSetExtendedAdvertisingParametersTest, InvalidExtendedAdvertisingEventProperties) {
200   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
201                     0, MakeAdvertisingEventProperties(CONNECTABLE | SCANNABLE), 0x0800, 0x0800, 0x7,
202                     OwnAddressType::PUBLIC_DEVICE_ADDRESS,
203                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
204                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
205                     SecondaryPhyType::LE_2M, 0x0, false),
206             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
207 
208   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
209                     0, MakeAdvertisingEventProperties(CONNECTABLE | DIRECTED | HIGH_DUTY_CYCLE),
210                     0x0800, 0x0800, 0x7, OwnAddressType::PUBLIC_DEVICE_ADDRESS,
211                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
212                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
213                     SecondaryPhyType::LE_2M, 0x0, false),
214             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
215 }
216 
TEST_F(LeSetExtendedAdvertisingParametersTest,InvalidPrimaryAdvertisingInterval)217 TEST_F(LeSetExtendedAdvertisingParametersTest, InvalidPrimaryAdvertisingInterval) {
218   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
219                     0, MakeAdvertisingEventProperties(CONNECTABLE), 0x10, 0x0800, 0x7,
220                     OwnAddressType::PUBLIC_DEVICE_ADDRESS,
221                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
222                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
223                     SecondaryPhyType::LE_2M, 0x0, false),
224             ErrorCode::UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
225 
226   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
227                     0, MakeAdvertisingEventProperties(CONNECTABLE), 0x0800, 0x10, 0x7,
228                     OwnAddressType::PUBLIC_DEVICE_ADDRESS,
229                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
230                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
231                     SecondaryPhyType::LE_2M, 0x0, false),
232             ErrorCode::UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
233 
234   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
235                     0, MakeAdvertisingEventProperties(CONNECTABLE), 0x0800, 0x0400, 0x7,
236                     OwnAddressType::PUBLIC_DEVICE_ADDRESS,
237                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
238                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
239                     SecondaryPhyType::LE_2M, 0x0, false),
240             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
241 }
242 
TEST_F(LeSetExtendedAdvertisingParametersTest,InvalidChannelMap)243 TEST_F(LeSetExtendedAdvertisingParametersTest, InvalidChannelMap) {
244   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
245                     0, MakeAdvertisingEventProperties(CONNECTABLE), 0x0800, 0x0800, 0x0,
246                     OwnAddressType::PUBLIC_DEVICE_ADDRESS,
247                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
248                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
249                     SecondaryPhyType::LE_2M, 0x0, false),
250             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
251 }
252 
TEST_F(LeSetExtendedAdvertisingParametersTest,InvalidPrimaryPhy)253 TEST_F(LeSetExtendedAdvertisingParametersTest, InvalidPrimaryPhy) {
254   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
255                     0, MakeAdvertisingEventProperties(LEGACY | CONNECTABLE), 0x0800, 0x0800, 0x7,
256                     OwnAddressType::PUBLIC_DEVICE_ADDRESS,
257                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
258                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_CODED, 0,
259                     SecondaryPhyType::LE_2M, 0x0, false),
260             ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
261 }
262 
TEST_F(LeSetExtendedAdvertisingParametersTest,AdvertisingActive)263 TEST_F(LeSetExtendedAdvertisingParametersTest, AdvertisingActive) {
264   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
265                     0, MakeAdvertisingEventProperties(CONNECTABLE), 0x0800, 0x0800, 0x7,
266                     OwnAddressType::PUBLIC_DEVICE_ADDRESS,
267                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
268                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
269                     SecondaryPhyType::LE_2M, 0x0, false),
270             ErrorCode::SUCCESS);
271   ASSERT_EQ(controller_.LeSetExtendedAdvertisingEnable(true, {MakeEnabledSet(0, 0, 0)}),
272             ErrorCode::SUCCESS);
273 
274   ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
275                     0, MakeAdvertisingEventProperties(0), 0x0800, 0x0800, 0x7,
276                     OwnAddressType::PUBLIC_DEVICE_ADDRESS,
277                     PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
278                     AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
279                     SecondaryPhyType::LE_2M, 0x0, false),
280             ErrorCode::COMMAND_DISALLOWED);
281 }
282 
283 }  // namespace rootcanal
284