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 // TODO(b/369381361) Enfore -Wmissing-prototypes
22 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
23 
24 using bluetooth::le_audio::LeAudioDevice;
25 
26 namespace bluetooth::le_audio {
GetTestAddress(uint8_t index)27 RawAddress GetTestAddress(uint8_t index) {
28   EXPECT_LT(index, UINT8_MAX);
29   RawAddress result = {{0xC0, 0xDE, 0xC0, 0xDE, 0x00, index}};
30   return result;
31 }
32 
33 class StorageHelperTest : public ::testing::Test {};
34 
TEST(StorageHelperTest,DeserializeSinkPacs)35 TEST(StorageHelperTest, DeserializeSinkPacs) {
36   // clang-format off
37         const std::vector<uint8_t> validSinkPack = {
38                 0x00,  // Magic
39                 0x01,  // Num of PACs
40                 0x02, 0x12,  // handle
41                 0x03, 0x12,  // cc handle
42                 0x02,  // Number of records in PAC
43                 0x1e,  // PAC entry size
44                 0x06, 0x00, 0x00, 0x00, 0x00,  // Codec Id
45                 0x13,  // Codec specific cap. size
46                 0x03, 0x01, 0x04, 0x00, 0x02, 0x02, 0x01, 0x02, 0x03, 0x01, 0x05, 0x04, 0x1e, 0x00, 0x1e, 0x00, 0x02, 0x05, 0x01,  // Codec specific capa
47                 0x04,  // Metadata size
48                 0x03, 0x01, 0xff, 0x0f,  // Metadata
49                 0x1e,  //
50                 0x06, 0x00, 0x00, 0x00, 0x00,  // Codec ID
51                 0x13,  // Codec specific cap. size
52                 0x03, 0x01, 0x20, 0x00, 0x02, 0x02, 0x01, 0x02, 0x03, 0x01, 0x05, 0x04, 0x3c, 0x00, 0x3c, 0x00, 0x02, 0x05, 0x01,  // Codec specific capa
53                 0x04,  // Codec specific capa
54                 0x03, 0x01, 0xff, 0x0f,  // Metadata
55         };
56 
57         const std::vector<uint8_t> invalidSinkPackNumOfPacs = {
58                 0x00,  // Magic
59                 0x05,  // Num of PACs
60                 0x02, 0x12,  // handle
61                 0x03, 0x12,  // cc handle
62                 0x01,  // Number of records in PAC
63                 0x1e,  // PAC entry size
64                 0x06, 0x00, 0x00, 0x00, 0x00,  // Codec Id
65                 0x13,  // Codec specific cap. size
66                 0x03, 0x01, 0x04, 0x00, 0x02, 0x02, 0x01, 0x02, 0x03, 0x01, 0x05, 0x04, 0x1e, 0x00, 0x1e, 0x00, 0x02, 0x05, 0x01,  // Codec specific capa
67                 0x04,  // Metadata size
68                 0x03, 0x01, 0xff, 0x0f,  // Metadata
69                 0x1e,  //
70                 0x06, 0x00, 0x00, 0x00, 0x00,  // Codec ID
71                 0x13,  // Codec specific cap. size
72                 0x03, 0x01, 0x20, 0x00, 0x02, 0x02, 0x01, 0x02, 0x03, 0x01, 0x05, 0x04, 0x3c, 0x00, 0x3c, 0x00, 0x02, 0x05, 0x01,  // Codec specific capa
73                 0x04,  // Codec specific capa
74                 0x03, 0x01, 0xff, 0x0f,  // Metadata
75         };
76 
77         const std::vector<uint8_t> invalidSinkPackMagic = {
78                 0x01,  // Magic
79                 0x01,  // Num of PACs
80                 0x02, 0x12,  // handle
81                 0x03, 0x12,  // cc handle
82                 0x02,  // Number of records in PAC
83                 0x1e,  // PAC entry size
84                 0x06, 0x00, 0x00, 0x00, 0x00,  // Codec Id
85                 0x13,  // Codec specific cap. size
86                 0x03, 0x01, 0x04, 0x00, 0x02, 0x02, 0x01, 0x02, 0x03, 0x01, 0x05, 0x04, 0x1e, 0x00, 0x1e, 0x00, 0x02, 0x05, 0x01,  // Codec specific capa
87                 0x04,  // Metadata size
88                 0x03, 0x01, 0xff, 0x0f,  // Metadata
89                 0x1e,  //
90                 0x06, 0x00, 0x00, 0x00, 0x00,  // Codec ID
91                 0x13,  // Codec specific cap. size
92                 0x03, 0x01, 0x20, 0x00, 0x02, 0x02, 0x01, 0x02, 0x03, 0x01, 0x05, 0x04, 0x3c, 0x00, 0x3c, 0x00, 0x02, 0x05, 0x01,  // Codec specific capa
93                 0x04,  // Codec specific capa
94                 0x03, 0x01, 0xff, 0x0f,  // Metadata
95         };
96   // clang-format on
97 
98   RawAddress test_address0 = GetTestAddress(0);
99   LeAudioDevice leAudioDevice(test_address0, DeviceConnectState::DISCONNECTED);
100   ASSERT_TRUE(DeserializeSinkPacs(&leAudioDevice, validSinkPack));
101   std::vector<uint8_t> serialize;
102   ASSERT_TRUE(SerializeSinkPacs(&leAudioDevice, serialize));
103   ASSERT_TRUE(serialize == validSinkPack);
104 
105   ASSERT_FALSE(DeserializeSinkPacs(&leAudioDevice, invalidSinkPackMagic));
106   ASSERT_FALSE(DeserializeSinkPacs(&leAudioDevice, invalidSinkPackNumOfPacs));
107 }
108 
TEST(StorageHelperTest,DeserializeSourcePacs)109 TEST(StorageHelperTest, DeserializeSourcePacs) {
110   // clang-format off
111   const std::vector<uint8_t> validSourcePack = {
112         0x00,  // Magic
113         0x01,  // Num of PACs
114         0x08, 0x12,  // handle
115         0x09, 0x12,  // cc handle
116         0x02,  // Number of records in PAC
117         0x1e,  // PAC entry size
118         0x06, 0x00, 0x00, 0x00, 0x00,  // Codec Id
119         0x13,  // Codec specific cap. size
120         0x03, 0x01, 0x04, 0x00, 0x02, 0x02, 0x01, 0x02, 0x03, 0x01, 0x05, 0x04, 0x1e, 0x00, 0x1e, 0x00, 0x02, 0x05, 0x01,
121         0x04,  // Metadata size
122         0x03, 0x01, 0x03, 0x00,  // Metadata
123         0x1e,  // PAC entry size
124         0x06, 0x00, 0x00, 0x00, 0x00,  // Codec Id
125         0x13,  // Codec specific cap. size
126         0x03, 0x01, 0x20, 0x00, 0x02, 0x02, 0x01, 0x02,  // Codec specific capa
127         0x03, 0x01, 0x05, 0x04, 0x3c, 0x00, 0x3c, 0x00,  // Codec specific capa
128         0x02, 0x05, 0x01,  // Codec specific capa
129         0x04,  // Metadata size
130         0x03, 0x01, 0x03, 0x00  // Metadata
131   };
132 
133   const std::vector<uint8_t> invalidSourcePackNumOfPacs = {
134         0x00,  // Magic
135         0x04,  // Num of PACs
136         0x08, 0x12,  // handle
137         0x09, 0x12,  // cc handle
138         0x01,  // Number of records in PAC
139         0x1e,  // PAC entry size
140         0x06, 0x00, 0x00, 0x00, 0x00,  // Codec Id
141         0x13,  // Codec specific cap. size
142         0x03, 0x01, 0x04, 0x00, 0x02, 0x02, 0x01, 0x02,  // Codec specific capa
143         0x03, 0x01, 0x05, 0x04, 0x1e, 0x00, 0x1e, 0x00,  // Codec specific capa
144         0x02, 0x05, 0x01,  // Codec specific capa
145         0x04,  // Metadata size
146         0x03, 0x01, 0x03, 0x00,  // Metadata
147         0x1e,  // PAC entry size
148         0x06, 0x00, 0x00, 0x00, 0x00,  // Codec Id
149         0x13,  // Codec specific cap. size
150         0x03, 0x01, 0x20, 0x00, 0x02, 0x02, 0x01, 0x02,  // Codec specific capa
151         0x03, 0x01, 0x05, 0x04, 0x3c, 0x00, 0x3c, 0x00,  // Codec specific capa
152         0x02, 0x05, 0x01,  // Codec specific capa
153         0x04,  // Metadata size
154         0x03, 0x01, 0x03, 0x00  // Metadata
155  };
156 
157   const std::vector<uint8_t> invalidSourcePackMagic = {
158         0x01,  // Magic
159         0x01,  // Num of PACs
160         0x08, 0x12,  // handle
161         0x09, 0x12,  // cc handle
162         0x02,  // Number of records in PAC
163         0x1e,  // PAC entry size
164         0x06, 0x00, 0x00, 0x00, 0x00,  // Codec Id
165         0x13,  // Codec specific cap. size
166         0x03, 0x01, 0x04, 0x00, 0x02, 0x02, 0x01, 0x02,  // Codec specific capa
167         0x03, 0x01, 0x05, 0x04, 0x1e, 0x00, 0x1e, 0x00,  // Codec specific capa
168         0x02, 0x05, 0x01,  // Codec specific capa
169         0x04,  // Metadata size
170         0x03, 0x01, 0x03, 0x00,  // Metadata
171         0x1e,  // PAC entry size
172         0x06, 0x00, 0x00, 0x00, 0x00,  // Codec Id
173         0x13,  // Codec specific cap. size
174         0x03, 0x01, 0x20, 0x00, 0x02, 0x02, 0x01, 0x02,  // Codec specific capa
175         0x03, 0x01, 0x05, 0x04, 0x3c, 0x00, 0x3c, 0x00,  // Codec specific capa
176         0x02, 0x05, 0x01,  // Codec specific capa
177         0x04,  // Metadata size
178         0x03, 0x01, 0x03, 0x00  // Metadata
179   };
180   // clang-format on
181 
182   RawAddress test_address0 = GetTestAddress(0);
183   LeAudioDevice leAudioDevice(test_address0, DeviceConnectState::DISCONNECTED);
184   ASSERT_TRUE(DeserializeSourcePacs(&leAudioDevice, validSourcePack));
185   std::vector<uint8_t> serialize;
186   ASSERT_TRUE(SerializeSourcePacs(&leAudioDevice, serialize));
187   ASSERT_TRUE(serialize == validSourcePack);
188 
189   ASSERT_FALSE(DeserializeSourcePacs(&leAudioDevice, invalidSourcePackMagic));
190   ASSERT_FALSE(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