xref: /btstack/test/le_device_db_tlv/le_device_db_tlv_test.cpp (revision ce000057228091a6995fedd05cb79a00d991f20a)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 
39 #include <stdint.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 
44 #include "CppUTest/TestHarness.h"
45 #include "CppUTest/CommandLineTestRunner.h"
46 
47 #include "ble/le_device_db.h"
48 #include "ble/le_device_db_tlv.h"
49 
50 #include "btstack_util.h"
51 #include "bluetooth.h"
52 #include "btstack_tlv_flash_bank.h"
53 #include "hal_flash_bank_memory.h"
54 
55 #define HAL_FLASH_BANK_MEMORY_STORAGE_SIZE 4096
56 static uint8_t hal_flash_bank_memory_storage[HAL_FLASH_BANK_MEMORY_STORAGE_SIZE];
57 static int empty_db_index = NVM_NUM_DEVICE_DB_ENTRIES-1;
58 
59 
TEST_GROUP(LE_DEVICE_DB_TLV)60 TEST_GROUP(LE_DEVICE_DB_TLV){
61     const hal_flash_bank_t * hal_flash_bank_impl;
62     hal_flash_bank_memory_t  hal_flash_bank_context;
63 
64     const btstack_tlv_t *      btstack_tlv_impl;
65     btstack_tlv_flash_bank_t btstack_tlv_context;
66 
67     bd_addr_t addr_zero,    addr_aa,  addr_bb,   addr_cc;
68     sm_key_t  sm_key_zero, sm_key_aa, sm_key_bb, sm_key_cc;
69 
70 	void set_addr_and_sm_key(uint8_t value, bd_addr_t addr, sm_key_t sm_key){
71 	    memset(addr, value, 6);
72 	    memset(sm_key, value, 16);
73 	}
74 
75     void setup(void){
76         // hal_flash_bank
77         hal_flash_bank_impl = hal_flash_bank_memory_init_instance(&hal_flash_bank_context, hal_flash_bank_memory_storage, HAL_FLASH_BANK_MEMORY_STORAGE_SIZE);
78         hal_flash_bank_impl->erase(&hal_flash_bank_context, 0);
79         hal_flash_bank_impl->erase(&hal_flash_bank_context, 1);
80         // btstack_tlv
81         btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(&btstack_tlv_context, hal_flash_bank_impl, &hal_flash_bank_context);
82         // le_device_db_tlv
83         le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_context);
84         le_device_db_init();
85 
86         set_addr_and_sm_key(0x00, addr_zero, sm_key_zero);
87         set_addr_and_sm_key(0xaa, addr_aa,   sm_key_aa);
88         set_addr_and_sm_key(0xbb, addr_bb,   sm_key_bb);
89         set_addr_and_sm_key(0xcc, addr_cc,   sm_key_cc);
90 	}
91 };
92 
93 
TEST(LE_DEVICE_DB_TLV,Empty)94 TEST(LE_DEVICE_DB_TLV, Empty){
95     CHECK_EQUAL(0, le_device_db_count());
96     le_device_db_dump();
97 }
98 
TEST(LE_DEVICE_DB_TLV,AddZero)99 TEST(LE_DEVICE_DB_TLV, AddZero){
100     int index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_zero, sm_key_zero);
101 
102     CHECK_TRUE(index >= 0);
103     CHECK_EQUAL(1, le_device_db_count());
104     le_device_db_dump();
105     le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_context);
106 }
107 
TEST(LE_DEVICE_DB_TLV,AddZero2)108 TEST(LE_DEVICE_DB_TLV, AddZero2){
109     int index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_zero, sm_key_zero);
110     CHECK_TRUE(index >= 0);
111     CHECK_EQUAL(1, le_device_db_count());
112 
113     index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_zero, sm_key_zero);
114     CHECK_TRUE(index >= 0);
115     CHECK_EQUAL(1, le_device_db_count());
116 
117     index = le_device_db_add(BD_ADDR_TYPE_LE_RANDOM, addr_zero, sm_key_zero);
118     CHECK_TRUE(index >= 0);
119     CHECK_EQUAL(2, le_device_db_count());
120 }
121 
TEST(LE_DEVICE_DB_TLV,AddOne)122 TEST(LE_DEVICE_DB_TLV, AddOne){
123     int index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_aa, sm_key_aa);
124     CHECK_TRUE(index >= 0);
125     CHECK_EQUAL(1, le_device_db_count());
126 }
127 
TEST(LE_DEVICE_DB_TLV,RetrieveOne)128 TEST(LE_DEVICE_DB_TLV, RetrieveOne){
129     bd_addr_t addr;
130     sm_key_t sm_key;
131     int addr_type;
132 
133     le_device_db_info(0, NULL, addr, sm_key);
134     le_device_db_info(0, &addr_type, NULL, sm_key);
135 
136     int index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_aa, sm_key_aa);
137     CHECK_TRUE(index >= 0);
138     CHECK_EQUAL(1, le_device_db_count());
139 
140     le_device_db_info((uint16_t) index, &addr_type, addr, sm_key);
141     MEMCMP_EQUAL(sm_key_aa, sm_key, 16);
142     MEMCMP_EQUAL(addr_aa, addr, 6);
143 }
144 
TEST(LE_DEVICE_DB_TLV,AddTwo)145 TEST(LE_DEVICE_DB_TLV, AddTwo){
146     le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_aa, sm_key_aa);
147     le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_bb, sm_key_bb);
148     CHECK_EQUAL(2, le_device_db_count());
149 }
150 
TEST(LE_DEVICE_DB_TLV,AddOTwoRemoveOne)151 TEST(LE_DEVICE_DB_TLV, AddOTwoRemoveOne){
152     int index_a = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_aa, sm_key_aa);
153     CHECK_TRUE(index_a >= 0);
154     int index_b = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_bb, sm_key_bb);
155     CHECK_TRUE(index_b >= 0);
156     le_device_db_remove((uint16_t) index_a);
157     CHECK_EQUAL(1, le_device_db_count());
158     bd_addr_t addr;
159     sm_key_t sm_key;
160     int addr_type;
161     le_device_db_info((uint16_t) index_b, &addr_type, addr, sm_key);
162     MEMCMP_EQUAL(sm_key_bb, sm_key, 16);
163     MEMCMP_EQUAL(addr_bb, addr, 6);
164 }
165 
TEST(LE_DEVICE_DB_TLV,AddOTwoRemoveThree)166 TEST(LE_DEVICE_DB_TLV, AddOTwoRemoveThree){
167 	int index_a = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_aa, sm_key_aa);
168     CHECK_TRUE(index_a >= 0);
169     int index_b = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_bb, sm_key_bb);
170     CHECK_TRUE(index_b >= 0);
171     CHECK_EQUAL(2, le_device_db_count());
172 
173     le_device_db_remove((uint16_t) index_a);
174     CHECK_EQUAL(1, le_device_db_count());
175     le_device_db_remove((uint16_t) index_b);
176     CHECK_EQUAL(0, le_device_db_count());
177     le_device_db_remove((uint16_t) index_b);
178     CHECK_EQUAL(0, le_device_db_count());
179 }
180 
TEST(LE_DEVICE_DB_TLV,RemoveOneFromEmpty)181 TEST(LE_DEVICE_DB_TLV, RemoveOneFromEmpty){
182 	CHECK_EQUAL(0, le_device_db_count());
183 
184     le_device_db_remove(0);
185     CHECK_EQUAL(0, le_device_db_count());
186 }
187 
TEST(LE_DEVICE_DB_TLV,AddTwoRemoveOneAddOne)188 TEST(LE_DEVICE_DB_TLV, AddTwoRemoveOneAddOne){
189     int index_a = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_aa, sm_key_aa);
190     CHECK_TRUE(index_a >= 0);
191     int index_b = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_bb, sm_key_bb);
192     CHECK_TRUE(index_b >= 0);
193     le_device_db_remove((uint16_t) index_a);
194     int index_c = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_cc, sm_key_cc);
195     CHECK_TRUE(index_c >= 0);
196     CHECK_EQUAL(2, le_device_db_count());
197     bd_addr_t addr;
198     sm_key_t sm_key;
199     int addr_type;
200     le_device_db_info((uint16_t) index_c, &addr_type, addr, sm_key);
201     MEMCMP_EQUAL(sm_key_cc, sm_key, 16);
202     MEMCMP_EQUAL(addr_cc, addr, 6);
203 }
204 
TEST(LE_DEVICE_DB_TLV,AddExisting)205 TEST(LE_DEVICE_DB_TLV, AddExisting){
206 	int index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_aa, sm_key_aa);
207     CHECK_TRUE(index >= 0);
208     CHECK_EQUAL(1, le_device_db_count());
209 
210     index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_aa, sm_key_aa);
211     CHECK_TRUE(index >= 0);
212     CHECK_EQUAL(1, le_device_db_count());
213 }
214 
TEST(LE_DEVICE_DB_TLV,ReplaceOldest)215 TEST(LE_DEVICE_DB_TLV, ReplaceOldest){
216     bd_addr_t addr;
217     sm_key_t  sm_key;
218     set_addr_and_sm_key(0x10, addr, sm_key);
219     int oldest_index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr, sm_key);
220     CHECK_TRUE(oldest_index >= 0);
221     // fill table
222     int i;
223     for (i=1;i<NVM_NUM_DEVICE_DB_ENTRIES;i++){
224         set_addr_and_sm_key(0x10 + i, addr, sm_key);
225         int index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr, sm_key);
226         CHECK_TRUE(index >= 0);
227     }
228     uint16_t num_entries = le_device_db_count();
229     // add another one that overwrites first one
230     set_addr_and_sm_key(0x22 + i, addr, sm_key);
231     int index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr, sm_key);
232     CHECK_EQUAL(oldest_index, index);
233     uint16_t num_entries_test = le_device_db_count();
234     CHECK_EQUAL(num_entries, num_entries_test);
235 }
236 
TEST(LE_DEVICE_DB_TLV,le_device_db_encryption_set_non_existing)237 TEST(LE_DEVICE_DB_TLV, le_device_db_encryption_set_non_existing){
238     uint16_t ediv = 16;
239     int encryption_key_size = 10;
240     int le_db_index = 10;
241 
242     le_device_db_encryption_set(le_db_index, ediv, NULL, NULL, encryption_key_size, 1, 1, 1);
243 
244     int expected_authenticated = 0;
245     int expected_authorized = 0;
246     int expected_secure_connection = 0;
247     uint16_t expected_ediv = 0;
248     uint8_t  expected_rand[8];
249     sm_key_t expected_ltk;
250     int      expected_encryption_key_size = 0;
251 
252     le_device_db_encryption_get(le_db_index, &expected_ediv, expected_rand, expected_ltk, &expected_encryption_key_size,
253         &expected_authenticated, &expected_authorized, &expected_secure_connection);
254 
255     CHECK_TRUE(expected_ediv != ediv);
256     CHECK_TRUE(expected_authenticated != 1);
257     CHECK_TRUE(expected_authorized != 1);
258     CHECK_TRUE(expected_secure_connection != 1);
259     CHECK_TRUE(expected_encryption_key_size != encryption_key_size);
260 }
261 
TEST(LE_DEVICE_DB_TLV,le_device_db_encryption_set_zero_ltk)262 TEST(LE_DEVICE_DB_TLV, le_device_db_encryption_set_zero_ltk){
263     uint16_t ediv = 16;
264     int encryption_key_size = 10;
265 
266     int le_db_index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_zero, sm_key_zero);
267     CHECK_TRUE(le_db_index >= 0);
268 
269     le_device_db_encryption_set(le_db_index, ediv, NULL, NULL, encryption_key_size, 1, 1, 1);
270 
271     le_device_db_encryption_get(le_db_index, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
272 }
273 
TEST(LE_DEVICE_DB_TLV,le_device_db_encryption_set)274 TEST(LE_DEVICE_DB_TLV, le_device_db_encryption_set){
275     bd_addr_t addr;
276     sm_key_t  sm_key;
277     set_addr_and_sm_key(0x10, addr, sm_key);
278 
279     uint16_t ediv = 1;
280     int encryption_key_size = 10;
281     uint8_t zero_rand[8];
282     memset(zero_rand, 5, 8);
283 
284     int le_db_index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr, sm_key);
285     CHECK_TRUE(le_db_index >= 0);
286     le_device_db_encryption_set(le_db_index, ediv, zero_rand, sm_key, encryption_key_size, 1, 1, 1);
287 
288     int expected_authenticated;
289     int expected_authorized;
290     int expected_secure_connection;
291 
292     uint16_t expected_ediv;
293     uint8_t  expected_rand[8];
294     sm_key_t expected_ltk;
295     int      expected_encryption_key_size;
296 
297     le_device_db_encryption_get(le_db_index, &expected_ediv, expected_rand, expected_ltk, &expected_encryption_key_size,
298         &expected_authenticated, &expected_authorized, &expected_secure_connection);
299 
300     CHECK_EQUAL(expected_ediv, ediv);
301     CHECK_EQUAL(expected_authenticated, 1);
302     CHECK_EQUAL(expected_authorized, 1);
303     CHECK_EQUAL(expected_secure_connection, 1);
304     CHECK_EQUAL(expected_encryption_key_size, encryption_key_size);
305     MEMCMP_EQUAL(expected_rand, zero_rand, 8);
306 }
307 
TEST(LE_DEVICE_DB_TLV,le_device_db_remote_csrk_set)308 TEST(LE_DEVICE_DB_TLV, le_device_db_remote_csrk_set){
309     le_device_db_remote_csrk_set(empty_db_index, NULL);
310 
311     sm_key_t csrk;
312     (void)memset(csrk, 5, 16);
313 
314     le_device_db_remote_csrk_set(empty_db_index, csrk);
315 
316     bd_addr_t addr;
317     sm_key_t  sm_key;
318     set_addr_and_sm_key(0x10, addr, sm_key);
319     int le_db_index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr, sm_key);
320 
321     le_device_db_remote_csrk_set(le_db_index, NULL);
322     le_device_db_remote_csrk_set(le_db_index, csrk);
323 }
324 
TEST(LE_DEVICE_DB_TLV,le_device_db_remote_csrk_get)325 TEST(LE_DEVICE_DB_TLV, le_device_db_remote_csrk_get){
326     le_device_db_remote_csrk_get(empty_db_index, NULL);
327 
328     bd_addr_t addr;
329     sm_key_t  sm_key;
330     set_addr_and_sm_key(0x10, addr, sm_key);
331     int le_db_index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr, sm_key);
332 
333     le_device_db_remote_csrk_get(le_db_index, NULL);
334 
335     sm_key_t csrk;
336     (void)memset(csrk, 5, 16);
337     le_device_db_remote_csrk_set(le_db_index, csrk);
338 
339     sm_key_t expected_csrk;
340     le_device_db_remote_csrk_get(le_db_index, expected_csrk);
341     MEMCMP_EQUAL(expected_csrk, csrk, 16);
342 }
343 
TEST(LE_DEVICE_DB_TLV,le_device_db_local_csrk_set)344 TEST(LE_DEVICE_DB_TLV, le_device_db_local_csrk_set){
345     le_device_db_local_csrk_set(empty_db_index, NULL);
346 
347     sm_key_t csrk;
348     (void)memset(csrk, 5, 16);
349 
350     le_device_db_local_csrk_set(empty_db_index, csrk);
351 
352     bd_addr_t addr;
353     sm_key_t  sm_key;
354     set_addr_and_sm_key(0x10, addr, sm_key);
355     int le_db_index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr, sm_key);
356 
357     le_device_db_local_csrk_set(le_db_index, NULL);
358     le_device_db_local_csrk_set(le_db_index, csrk);
359 }
360 
TEST(LE_DEVICE_DB_TLV,le_device_db_local_csrk_get)361 TEST(LE_DEVICE_DB_TLV, le_device_db_local_csrk_get){
362     le_device_db_local_csrk_get(empty_db_index, NULL);
363 
364     bd_addr_t addr;
365     sm_key_t  sm_key;
366     set_addr_and_sm_key(0x10, addr, sm_key);
367     int le_db_index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr, sm_key);
368 
369     le_device_db_local_csrk_get(le_db_index, NULL);
370 
371     sm_key_t csrk;
372     (void)memset(csrk, 5, 16);
373     le_device_db_local_csrk_set(le_db_index, csrk);
374 
375     sm_key_t expected_csrk;
376     le_device_db_local_csrk_get(le_db_index, expected_csrk);
377     MEMCMP_EQUAL(expected_csrk, csrk, 16);
378 }
379 
TEST(LE_DEVICE_DB_TLV,le_device_db_remote_counter)380 TEST(LE_DEVICE_DB_TLV, le_device_db_remote_counter){
381     le_device_db_remote_counter_set(empty_db_index, 0);
382 
383     uint32_t expected_counter = le_device_db_remote_counter_get(empty_db_index);
384     CHECK_EQUAL(expected_counter, 0);
385 
386     bd_addr_t addr;
387     sm_key_t  sm_key;
388     set_addr_and_sm_key(0x10, addr, sm_key);
389     int le_db_index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr, sm_key);
390 
391     le_device_db_remote_counter_set(le_db_index, 10);
392 
393 
394     expected_counter = le_device_db_remote_counter_get(le_db_index);
395     CHECK_EQUAL(expected_counter, 10);
396 }
397 
TEST(LE_DEVICE_DB_TLV,le_device_db_local_counter)398 TEST(LE_DEVICE_DB_TLV, le_device_db_local_counter){
399     le_device_db_local_counter_set(empty_db_index, 0);
400 
401     uint32_t expected_counter = le_device_db_local_counter_get(empty_db_index);
402     CHECK_EQUAL(expected_counter, 0);
403 
404     bd_addr_t addr;
405     sm_key_t  sm_key;
406     set_addr_and_sm_key(0x10, addr, sm_key);
407     int le_db_index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr, sm_key);
408 
409     le_device_db_local_counter_set(le_db_index, 10);
410 
411     expected_counter = le_device_db_local_counter_get(le_db_index);
412     CHECK_EQUAL(expected_counter, 10);
413 }
414 
main(int argc,const char * argv[])415 int main (int argc, const char * argv[]){
416     return CommandLineTestRunner::RunAllTests(argc, argv);
417 }
418