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