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 LeSetPeriodicAdvertisingDataTest : public ::testing::Test {
27 public:
LeSetPeriodicAdvertisingDataTest()28 LeSetPeriodicAdvertisingDataTest() {
29 // Reduce the maximum advertising data length to simplify testing.
30 properties_.le_max_advertising_data_length = 300;
31 }
32 ~LeSetPeriodicAdvertisingDataTest() override = default;
33
34 protected:
35 Address address_{0};
36 ControllerProperties properties_{};
37 LinkLayerController controller_{address_, properties_};
38 };
39
TEST_F(LeSetPeriodicAdvertisingDataTest,Complete)40 TEST_F(LeSetPeriodicAdvertisingDataTest, Complete) {
41 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
42 0, MakeAdvertisingEventProperties(), 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 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingParameters(0, 0x6, 0xffff, false),
50 ErrorCode::SUCCESS);
51
52 std::vector<uint8_t> advertising_data = {1, 2, 3};
53 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingData(0, Operation::COMPLETE_ADVERTISEMENT,
54 advertising_data),
55 ErrorCode::SUCCESS);
56 }
57
TEST_F(LeSetPeriodicAdvertisingDataTest,Unchanged)58 TEST_F(LeSetPeriodicAdvertisingDataTest, Unchanged) {
59 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
60 0, MakeAdvertisingEventProperties(), 0x0800, 0x0800, 0x7,
61 OwnAddressType::PUBLIC_DEVICE_ADDRESS,
62 PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
63 AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
64 SecondaryPhyType::LE_2M, 0x0, false),
65 ErrorCode::SUCCESS);
66
67 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingParameters(0, 0x6, 0xffff, false),
68 ErrorCode::SUCCESS);
69
70 std::vector<uint8_t> advertising_data = {1, 2, 3};
71 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingData(0, Operation::COMPLETE_ADVERTISEMENT,
72 advertising_data),
73 ErrorCode::SUCCESS);
74
75 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingEnable(true, false, 0), ErrorCode::SUCCESS);
76
77 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingData(0, Operation::UNCHANGED_DATA, {}),
78 ErrorCode::SUCCESS);
79 }
80
TEST_F(LeSetPeriodicAdvertisingDataTest,Fragmented)81 TEST_F(LeSetPeriodicAdvertisingDataTest, Fragmented) {
82 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
83 0, MakeAdvertisingEventProperties(), 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::SUCCESS);
89
90 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingParameters(0, 0x6, 0xffff, false),
91 ErrorCode::SUCCESS);
92
93 std::vector<uint8_t> first_advertising_data_fragment = {1, 2, 3};
94 std::vector<uint8_t> intermediate_advertising_data_fragment = {4, 5, 6};
95 std::vector<uint8_t> last_advertising_data_fragment = {7, 8, 9};
96
97 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingData(0, Operation::FIRST_FRAGMENT,
98 first_advertising_data_fragment),
99 ErrorCode::SUCCESS);
100
101 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingData(0, Operation::INTERMEDIATE_FRAGMENT,
102 intermediate_advertising_data_fragment),
103 ErrorCode::SUCCESS);
104
105 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingData(0, Operation::LAST_FRAGMENT,
106 last_advertising_data_fragment),
107 ErrorCode::SUCCESS);
108 }
109
TEST_F(LeSetPeriodicAdvertisingDataTest,UnknownAdvertisingHandle)110 TEST_F(LeSetPeriodicAdvertisingDataTest, UnknownAdvertisingHandle) {
111 std::vector<uint8_t> advertising_data = {1, 2, 3};
112 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingData(0, Operation::COMPLETE_ADVERTISEMENT,
113 advertising_data),
114 ErrorCode::UNKNOWN_ADVERTISING_IDENTIFIER);
115 }
116
TEST_F(LeSetPeriodicAdvertisingDataTest,PeriodicAdvertisingNotConfigured)117 TEST_F(LeSetPeriodicAdvertisingDataTest, PeriodicAdvertisingNotConfigured) {
118 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
119 0, MakeAdvertisingEventProperties(), 0x0800, 0x0800, 0x7,
120 OwnAddressType::PUBLIC_DEVICE_ADDRESS,
121 PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
122 AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
123 SecondaryPhyType::LE_2M, 0x0, false),
124 ErrorCode::SUCCESS);
125
126 std::vector<uint8_t> advertising_data = {1, 2, 3};
127 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingData(0, Operation::COMPLETE_ADVERTISEMENT,
128 advertising_data),
129 ErrorCode::COMMAND_DISALLOWED);
130 }
131
TEST_F(LeSetPeriodicAdvertisingDataTest,PeriodicAdvertisingEnabled)132 TEST_F(LeSetPeriodicAdvertisingDataTest, PeriodicAdvertisingEnabled) {
133 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
134 0, MakeAdvertisingEventProperties(), 0x0800, 0x0800, 0x7,
135 OwnAddressType::PUBLIC_DEVICE_ADDRESS,
136 PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
137 AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
138 SecondaryPhyType::LE_2M, 0x0, false),
139 ErrorCode::SUCCESS);
140
141 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingParameters(0, 0x6, 0xffff, false),
142 ErrorCode::SUCCESS);
143
144 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingEnable(true, false, 0), ErrorCode::SUCCESS);
145
146 std::vector<uint8_t> advertising_data = {1, 2, 3};
147 ASSERT_EQ(
148 controller_.LeSetPeriodicAdvertisingData(0, Operation::FIRST_FRAGMENT, advertising_data),
149 ErrorCode::COMMAND_DISALLOWED);
150 }
151
TEST_F(LeSetPeriodicAdvertisingDataTest,EmptyAdvertisingDataFragment)152 TEST_F(LeSetPeriodicAdvertisingDataTest, EmptyAdvertisingDataFragment) {
153 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
154 0, MakeAdvertisingEventProperties(), 0x0800, 0x0800, 0x7,
155 OwnAddressType::PUBLIC_DEVICE_ADDRESS,
156 PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
157 AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
158 SecondaryPhyType::LE_2M, 0x0, false),
159 ErrorCode::SUCCESS);
160
161 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingParameters(0, 0x6, 0xffff, false),
162 ErrorCode::SUCCESS);
163
164 std::vector<uint8_t> first_advertising_data_fragment = {1, 2, 3};
165 std::vector<uint8_t> intermediate_advertising_data_fragment = {4, 5, 6};
166 std::vector<uint8_t> last_advertising_data_fragment = {7, 8, 9};
167
168 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingData(0, Operation::FIRST_FRAGMENT, {}),
169 ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
170
171 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingData(0, Operation::FIRST_FRAGMENT,
172 first_advertising_data_fragment),
173 ErrorCode::SUCCESS);
174
175 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingData(0, Operation::INTERMEDIATE_FRAGMENT, {}),
176 ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
177
178 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingData(0, Operation::INTERMEDIATE_FRAGMENT,
179 intermediate_advertising_data_fragment),
180 ErrorCode::SUCCESS);
181
182 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingData(0, Operation::LAST_FRAGMENT, {}),
183 ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
184
185 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingData(0, Operation::LAST_FRAGMENT,
186 last_advertising_data_fragment),
187 ErrorCode::SUCCESS);
188 }
189
TEST_F(LeSetPeriodicAdvertisingDataTest,UnchangedWhenDisabled)190 TEST_F(LeSetPeriodicAdvertisingDataTest, UnchangedWhenDisabled) {
191 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
192 0, MakeAdvertisingEventProperties(), 0x0800, 0x0800, 0x7,
193 OwnAddressType::PUBLIC_DEVICE_ADDRESS,
194 PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
195 AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
196 SecondaryPhyType::LE_2M, 0x0, false),
197 ErrorCode::SUCCESS);
198
199 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingParameters(0, 0x6, 0xffff, false),
200 ErrorCode::SUCCESS);
201
202 std::vector<uint8_t> advertising_data = {1, 2, 3};
203 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingData(0, Operation::COMPLETE_ADVERTISEMENT,
204 advertising_data),
205 ErrorCode::SUCCESS);
206
207 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingData(0, Operation::UNCHANGED_DATA, {}),
208 ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
209 }
210
TEST_F(LeSetPeriodicAdvertisingDataTest,UnchangedWhenAdvertisingDataEmpty)211 TEST_F(LeSetPeriodicAdvertisingDataTest, UnchangedWhenAdvertisingDataEmpty) {
212 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
213 0, MakeAdvertisingEventProperties(), 0x0800, 0x0800, 0x7,
214 OwnAddressType::PUBLIC_DEVICE_ADDRESS,
215 PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
216 AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
217 SecondaryPhyType::LE_2M, 0x0, false),
218 ErrorCode::SUCCESS);
219
220 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingParameters(0, 0x6, 0xffff, false),
221 ErrorCode::SUCCESS);
222
223 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingEnable(true, false, 0), ErrorCode::SUCCESS);
224
225 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingData(0, Operation::UNCHANGED_DATA, {}),
226 ErrorCode::INVALID_HCI_COMMAND_PARAMETERS);
227 }
228
TEST_F(LeSetPeriodicAdvertisingDataTest,AdvertisingDataLargerThanMemoryCapacity)229 TEST_F(LeSetPeriodicAdvertisingDataTest, AdvertisingDataLargerThanMemoryCapacity) {
230 ASSERT_EQ(controller_.LeSetExtendedAdvertisingParameters(
231 0, MakeAdvertisingEventProperties(), 0x0800, 0x0800, 0x7,
232 OwnAddressType::PUBLIC_DEVICE_ADDRESS,
233 PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, Address::kEmpty,
234 AdvertisingFilterPolicy::ALL_DEVICES, 0x70, PrimaryPhyType::LE_1M, 0,
235 SecondaryPhyType::LE_2M, 0x0, false),
236 ErrorCode::SUCCESS);
237
238 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingParameters(0, 0x6, 0xffff, false),
239 ErrorCode::SUCCESS);
240
241 std::vector<uint8_t> advertising_data_fragment = {1, 2, 3};
242 advertising_data_fragment.resize(properties_.le_max_advertising_data_length);
243
244 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingData(0, Operation::FIRST_FRAGMENT,
245 advertising_data_fragment),
246 ErrorCode::SUCCESS);
247 ASSERT_EQ(controller_.LeSetPeriodicAdvertisingData(0, Operation::LAST_FRAGMENT,
248 advertising_data_fragment),
249 ErrorCode::MEMORY_CAPACITY_EXCEEDED);
250 }
251
252 } // namespace rootcanal
253