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 "storage_helper.h"
18
19 #include <gtest/gtest.h>
20
21 #include "common/init_flags.h"
22
23 using bluetooth::le_audio::LeAudioDevice;
24
25 namespace bluetooth::le_audio {
GetTestAddress(uint8_t index)26 RawAddress GetTestAddress(uint8_t index) {
27 EXPECT_LT(index, UINT8_MAX);
28 RawAddress result = {{0xC0, 0xDE, 0xC0, 0xDE, 0x00, index}};
29 return result;
30 }
31
32 class StorageHelperTest : public ::testing::Test {};
33
TEST(StorageHelperTest,DeserializeSinkPacs)34 TEST(StorageHelperTest, DeserializeSinkPacs) {
35 // clang-format off
36 const std::vector<uint8_t> validSinkPack = {
37 0x00, // Magic
38 0x01, // Num of PACs
39 0x02,0x12, // handle
40 0x03,0x12, // cc handle
41 0x02, // Number of records in PAC
42 0x1e, // PAC entry size
43 0x06,0x00,0x00,0x00,0x00, // Codec Id
44 0x13, // Codec specific cap. size
45 0x03,0x01,0x04,0x00,0x02,0x02,0x01,0x02,0x03,0x01,0x05,0x04,0x1e,0x00,0x1e,0x00,0x02,0x05,0x01, // Codec specific capa
46 0x04, // Metadata size
47 0x03,0x01,0xff,0x0f, // Metadata
48 0x1e, //
49 0x06,0x00,0x00,0x00,0x00, // Codec ID
50 0x13, // Codec specific cap. size
51 0x03,0x01,0x20,0x00,0x02,0x02,0x01,0x02,0x03,0x01,0x05,0x04,0x3c,0x00,0x3c,0x00,0x02,0x05,0x01, // Codec specific capa
52 0x04, // Codec specific capa
53 0x03,0x01,0xff,0x0f, // Metadata
54 };
55
56 const std::vector<uint8_t> invalidSinkPackNumOfPacs = {
57 0x00, // Magic
58 0x05, // Num of PACs
59 0x02,0x12, // handle
60 0x03,0x12, // cc handle
61 0x01, // Number of records in PAC
62 0x1e, // PAC entry size
63 0x06,0x00,0x00,0x00,0x00, // Codec Id
64 0x13, // Codec specific cap. size
65 0x03,0x01,0x04,0x00,0x02,0x02,0x01,0x02,0x03,0x01,0x05,0x04,0x1e,0x00,0x1e,0x00,0x02,0x05,0x01, // Codec specific capa
66 0x04, // Metadata size
67 0x03,0x01,0xff,0x0f, // Metadata
68 0x1e, //
69 0x06,0x00,0x00,0x00,0x00, // Codec ID
70 0x13, // Codec specific cap. size
71 0x03,0x01,0x20,0x00,0x02,0x02,0x01,0x02,0x03,0x01,0x05,0x04,0x3c,0x00,0x3c,0x00,0x02,0x05,0x01, // Codec specific capa
72 0x04, // Codec specific capa
73 0x03,0x01,0xff,0x0f, // Metadata
74 };
75
76 const std::vector<uint8_t> invalidSinkPackMagic = {
77 0x01, // Magic
78 0x01, // Num of PACs
79 0x02,0x12, // handle
80 0x03,0x12, // cc handle
81 0x02, // Number of records in PAC
82 0x1e, // PAC entry size
83 0x06,0x00,0x00,0x00,0x00, // Codec Id
84 0x13, // Codec specific cap. size
85 0x03,0x01,0x04,0x00,0x02,0x02,0x01,0x02,0x03,0x01,0x05,0x04,0x1e,0x00,0x1e,0x00,0x02,0x05,0x01, // Codec specific capa
86 0x04, // Metadata size
87 0x03,0x01,0xff,0x0f, // Metadata
88 0x1e, //
89 0x06,0x00,0x00,0x00,0x00, // Codec ID
90 0x13, // Codec specific cap. size
91 0x03,0x01,0x20,0x00,0x02,0x02,0x01,0x02,0x03,0x01,0x05,0x04,0x3c,0x00,0x3c,0x00,0x02,0x05,0x01, // Codec specific capa
92 0x04, // Codec specific capa
93 0x03,0x01,0xff,0x0f, // Metadata
94 };
95 // clang-format on
96
97 RawAddress test_address0 = GetTestAddress(0);
98 LeAudioDevice leAudioDevice(test_address0, DeviceConnectState::DISCONNECTED);
99 ASSERT_TRUE(DeserializeSinkPacs(&leAudioDevice, validSinkPack));
100 std::vector<uint8_t> serialize;
101 ASSERT_TRUE(SerializeSinkPacs(&leAudioDevice, serialize));
102 ASSERT_TRUE(serialize == validSinkPack);
103
104 ASSERT_FALSE(DeserializeSinkPacs(&leAudioDevice, invalidSinkPackMagic));
105 ASSERT_FALSE(DeserializeSinkPacs(&leAudioDevice, invalidSinkPackNumOfPacs));
106 }
107
TEST(StorageHelperTest,DeserializeSourcePacs)108 TEST(StorageHelperTest, DeserializeSourcePacs) {
109 // clang-format off
110 const std::vector<uint8_t> validSourcePack = {
111 0x00, // Magic
112 0x01, // Num of PACs
113 0x08,0x12, // handle
114 0x09,0x12, // cc handle
115 0x02, // Number of records in PAC
116 0x1e, // PAC entry size
117 0x06,0x00,0x00,0x00,0x00, // Codec Id
118 0x13, // Codec specific cap. size
119 0x03,0x01,0x04,0x00,0x02,0x02,0x01,0x02,0x03,0x01,0x05,0x04,0x1e,0x00,0x1e,0x00,0x02,0x05,0x01,
120 0x04, // Metadata size
121 0x03,0x01,0x03,0x00, // Metadata
122 0x1e, // PAC entry size
123 0x06,0x00,0x00,0x00,0x00, // Codec Id
124 0x13, // Codec specific cap. size
125 0x03,0x01,0x20,0x00,0x02,0x02,0x01,0x02, // Codec specific capa
126 0x03,0x01,0x05,0x04,0x3c,0x00,0x3c,0x00, // Codec specific capa
127 0x02,0x05,0x01, // Codec specific capa
128 0x04, // Metadata size
129 0x03,0x01,0x03,0x00 // Metadata
130 };
131
132 const std::vector<uint8_t> invalidSourcePackNumOfPacs = {
133 0x00, // Magic
134 0x04, // Num of PACs
135 0x08,0x12, // handle
136 0x09,0x12, // cc handle
137 0x01, // Number of records in PAC
138 0x1e, // PAC entry size
139 0x06,0x00,0x00,0x00,0x00, // Codec Id
140 0x13, // Codec specific cap. size
141 0x03,0x01,0x04,0x00,0x02,0x02,0x01,0x02, // Codec specific capa
142 0x03,0x01,0x05,0x04,0x1e,0x00,0x1e,0x00, // Codec specific capa
143 0x02,0x05,0x01, // Codec specific capa
144 0x04, // Metadata size
145 0x03,0x01,0x03,0x00, // Metadata
146 0x1e, // PAC entry size
147 0x06,0x00,0x00,0x00,0x00, // Codec Id
148 0x13, // Codec specific cap. size
149 0x03,0x01,0x20,0x00,0x02,0x02,0x01,0x02, // Codec specific capa
150 0x03,0x01,0x05,0x04,0x3c,0x00,0x3c,0x00, // Codec specific capa
151 0x02,0x05,0x01, // Codec specific capa
152 0x04, // Metadata size
153 0x03,0x01,0x03,0x00 // Metadata
154 };
155
156 const std::vector<uint8_t> invalidSourcePackMagic = {
157 0x01, // Magic
158 0x01, // Num of PACs
159 0x08,0x12, // handle
160 0x09,0x12, // cc handle
161 0x02, // Number of records in PAC
162 0x1e, // PAC entry size
163 0x06,0x00,0x00,0x00,0x00, // Codec Id
164 0x13, // Codec specific cap. size
165 0x03,0x01,0x04,0x00,0x02,0x02,0x01,0x02, // Codec specific capa
166 0x03,0x01,0x05,0x04,0x1e,0x00,0x1e,0x00, // Codec specific capa
167 0x02,0x05,0x01, // Codec specific capa
168 0x04, // Metadata size
169 0x03,0x01,0x03,0x00, // Metadata
170 0x1e, // PAC entry size
171 0x06,0x00,0x00,0x00,0x00, // Codec Id
172 0x13, // Codec specific cap. size
173 0x03,0x01,0x20,0x00,0x02,0x02,0x01,0x02, // Codec specific capa
174 0x03,0x01,0x05,0x04,0x3c,0x00,0x3c,0x00, // Codec specific capa
175 0x02,0x05,0x01, // Codec specific capa
176 0x04, // Metadata size
177 0x03,0x01,0x03,0x00 // Metadata
178 };
179 // clang-format on
180
181 RawAddress test_address0 = GetTestAddress(0);
182 LeAudioDevice leAudioDevice(test_address0, DeviceConnectState::DISCONNECTED);
183 ASSERT_TRUE(DeserializeSourcePacs(&leAudioDevice, validSourcePack));
184 std::vector<uint8_t> serialize;
185 ASSERT_TRUE(SerializeSourcePacs(&leAudioDevice, serialize));
186 ASSERT_TRUE(serialize == validSourcePack);
187
188 ASSERT_FALSE(DeserializeSourcePacs(&leAudioDevice, invalidSourcePackMagic));
189 ASSERT_FALSE(
190 DeserializeSourcePacs(&leAudioDevice, invalidSourcePackNumOfPacs));
191 }
192
TEST(StorageHelperTest,DeserializeAses)193 TEST(StorageHelperTest, DeserializeAses) {
194 // clang-format off
195 const std::vector<uint8_t> validAses {
196 0x00, // Magic
197 0x03, // Num of ASEs
198 0x05, 0x11, // handle
199 0x06, 0x11, // ccc handle
200 0x01, // ASE id
201 0x01, // direction
202 0x08, 0x11, // handle
203 0x09, 0x11, // ccc handle
204 0x02, // ASE id
205 0x01, // direction
206 0x0b, 0x11, // handle
207 0x0c, 0x11, // ccc handle
208 0x03, // ASE id
209 0x02 // direction
210 };
211 const std::vector<uint8_t> invalidAsesNumOfAses {
212 0x00, // Magic
213 0x05, // Num of ASEs
214 0x05, 0x11, // handle
215 0x06, 0x11, // ccc handle
216 0x01, // ASE id
217 0x01, // direction
218 0x08, 0x11, // handle
219 0x09, 0x11, // ccc handle
220 0x02, // ASE id
221 0x01, // direction
222 0x0b, 0x11, // handle
223 0x0c, 0x11, // ccc handle
224 0x03, // ASE id
225 0x02 // direction
226 };
227 const std::vector<uint8_t> invalidAsesMagic {
228 0x01, // Magic
229 0x03, // Num of ASEs
230 0x05, 0x11, // handle
231 0x06, 0x11, // ccc handle
232 0x01, // ASE id
233 0x01, // direction
234 0x08, 0x11, // handle
235 0x09, 0x11, // ccc handle
236 0x02, // ASE id
237 0x01, // direction
238 0x0b, 0x11, // handle
239 0x0c, 0x11, // ccc handle
240 0x03, // ASE id
241 0x02 // direction
242 };
243 // clang-format on
244 RawAddress test_address0 = GetTestAddress(0);
245 LeAudioDevice leAudioDevice(test_address0, DeviceConnectState::DISCONNECTED);
246 ASSERT_TRUE(DeserializeAses(&leAudioDevice, validAses));
247
248 std::vector<uint8_t> serialize;
249 ASSERT_TRUE(SerializeAses(&leAudioDevice, serialize));
250 ASSERT_TRUE(serialize == validAses);
251
252 ASSERT_FALSE(DeserializeAses(&leAudioDevice, invalidAsesNumOfAses));
253 ASSERT_FALSE(DeserializeAses(&leAudioDevice, invalidAsesMagic));
254 }
255
TEST(StorageHelperTest,DeserializeHandles)256 TEST(StorageHelperTest, DeserializeHandles) {
257 // clang-format off
258 const std::vector<uint8_t> validHandles {
259 0x00, // Magic
260 0x0e, 0x11, // Control point handle
261 0x0f, 0x11, // Control point ccc handle
262 0x05, 0x12, // Sink audio location handle
263 0x06, 0x12, // Sink audio location ccc handle
264 0x0b, 0x12, // Source audio location handle
265 0x0c, 0x12, // Source audio location ccc handle
266 0x11, 0x12, // Supported context types handle
267 0x12, 0x12, // Supported context types ccc handle
268 0x0e, 0x12, // Available context types handle
269 0x0f, 0x12, // Available context types ccc handle
270 0x03, 0xa3 // TMAP role handle
271 };
272 const std::vector<uint8_t> invalidHandlesMagic {
273 0x01, // Magic
274 0x0e, 0x11, // Control point handle
275 0x0f, 0x11, // Control point ccc handle
276 0x05, 0x12, // Sink audio location handle
277 0x06, 0x12, // Sink audio location ccc handle
278 0x0b, 0x12, // Source audio location handle
279 0x0c, 0x12, // Source audio location ccc handle
280 0x11, 0x12, // Supported context types handle
281 0x12, 0x12, // Supported context types ccc handle
282 0x0e, 0x12, // Available context types handle
283 0x0f, 0x12, // Available context types ccc handle
284 0x03, 0xa3 // TMAP role handle
285 };
286 const std::vector<uint8_t> invalidHandles {
287 0x00, // Magic
288 0x0e, 0x11, // Control point handle
289 0x0f, 0x11, // Control point ccc handle
290 0x05, 0x12, // Sink audio location handle
291 0x06, 0x12, // Sink audio location ccc handle
292 0x0b, 0x12, // Source audio location handle
293 0x0c, 0x12, // Source audio location ccc handle
294 0x11, 0x12, // Supported context types handle
295 0x12, 0x12, // Supported context types ccc handle
296 0x0e, 0x12, // Available context types handle
297 0x0f, 0x12, // Available context types ccc handle
298 0x03, 0xa3, // TMAP role handle
299 0x00, 0x00, // corrupted
300 };
301 // clang-format on
302 RawAddress test_address0 = GetTestAddress(0);
303 LeAudioDevice leAudioDevice(test_address0, DeviceConnectState::DISCONNECTED);
304 ASSERT_TRUE(DeserializeHandles(&leAudioDevice, validHandles));
305 std::vector<uint8_t> serialize;
306 ASSERT_TRUE(SerializeHandles(&leAudioDevice, serialize));
307 ASSERT_TRUE(serialize == validHandles);
308
309 ASSERT_FALSE(DeserializeHandles(&leAudioDevice, invalidHandlesMagic));
310 ASSERT_FALSE(DeserializeHandles(&leAudioDevice, invalidHandles));
311 }
312 } // namespace bluetooth::le_audio
313