1 /*
2  * Copyright (c) 2020 Demant
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <string.h>
8 #include <zephyr/types.h>
9 #include <zephyr/ztest.h>
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 
14 #define CONFIG_BT_CTLR_FILTER_ACCEPT_LIST 1
15 #define CONFIG_BT_CTLR_PRIVACY 1
16 #define CONFIG_BT_CTLR_RL_SIZE 8
17 #define CONFIG_BT_CTLR_FAL_SIZE 8
18 #define CONFIG_BT_CTLR_RPA_CACHE_SIZE 4
19 #define CONFIG_BT_CTLR_SW_DEFERRED_PRIVACY 1
20 #define CONFIG_BT_LOG_LEVEL 1
21 
22 #include "ll_sw/ull_filter.c"
23 
24 /*
25  * Unit test of SW deferred privacy data structure and related methods
26  * Tests the prpa_cache_add, prpa_cache_clear and prpa_cache_find functions
27  */
28 
29 #define BT_ADDR_INIT(P0, P1, P2, P3, P4, P5)                                   \
30 	(&(bt_addr_t){ { P0, P1, P2, P3, P4, P5 } })
31 
helper_privacy_clear(void)32 void helper_privacy_clear(void)
33 {
34 	zassert_equal(newest_prpa, 0, "");
35 	for (uint8_t i = 0; i < CONFIG_BT_CTLR_RPA_CACHE_SIZE; i++) {
36 		zassert_equal(prpa_cache[i].taken, 0U, "");
37 	}
38 }
39 
helper_privacy_add(int skew)40 void helper_privacy_add(int skew)
41 {
42 	bt_addr_t a1, a2, a3, a4, a5;
43 	uint8_t pos, ex_pos;
44 
45 	bt_addr_copy(&a1, BT_ADDR_INIT(0x12, 0x13, 0x14, 0x15, 0x16, 0x17));
46 	bt_addr_copy(&a2, BT_ADDR_INIT(0x22, 0x23, 0x24, 0x25, 0x26, 0x27));
47 	bt_addr_copy(&a3, BT_ADDR_INIT(0x32, 0x33, 0x34, 0x35, 0x36, 0x37));
48 	bt_addr_copy(&a4, BT_ADDR_INIT(0x42, 0x43, 0x44, 0x45, 0x46, 0x47));
49 	bt_addr_copy(&a5, BT_ADDR_INIT(0x52, 0x53, 0x54, 0x55, 0x56, 0x57));
50 
51 	prpa_cache_add(&a1);
52 	pos = prpa_cache_find(&a1);
53 	ex_pos = (1 + skew) % CONFIG_BT_CTLR_RPA_CACHE_SIZE;
54 	zassert_equal(pos, ex_pos, "%d == %d", pos, ex_pos);
55 
56 	prpa_cache_add(&a2);
57 	pos = prpa_cache_find(&a2);
58 	ex_pos = (2 + skew) % CONFIG_BT_CTLR_RPA_CACHE_SIZE;
59 	zassert_equal(pos, ex_pos, "");
60 
61 	prpa_cache_add(&a3);
62 	pos = prpa_cache_find(&a3);
63 	ex_pos = (3 + skew) % CONFIG_BT_CTLR_RPA_CACHE_SIZE;
64 	zassert_equal(pos, ex_pos, "");
65 
66 	/* Adding this should cause wrap around */
67 	prpa_cache_add(&a4);
68 	pos = prpa_cache_find(&a4);
69 	ex_pos = (4 + skew) % CONFIG_BT_CTLR_RPA_CACHE_SIZE;
70 	zassert_equal(pos, ex_pos, "");
71 
72 	/* adding this should cause a1 to be dropped */
73 	prpa_cache_add(&a5);
74 	pos = prpa_cache_find(&a5);
75 	ex_pos = (1 + skew) % CONFIG_BT_CTLR_RPA_CACHE_SIZE;
76 	zassert_equal(pos, ex_pos, "");
77 
78 	/* check that a1 can no longer be found */
79 	pos = prpa_cache_find(&a1);
80 	zassert_equal(pos, FILTER_IDX_NONE, "");
81 }
82 
before(void * data)83 static void before(void *data)
84 {
85 	ARG_UNUSED(data);
86 
87 	/* Run before each test - clear the cache so we start fresh each time. */
88 	prpa_cache_clear();
89 }
90 
91 ZTEST_SUITE(test_ctrl_sw_privacy_unit, NULL, NULL, before, NULL, NULL);
92 
ZTEST(test_ctrl_sw_privacy_unit,test_privacy_clear)93 ZTEST(test_ctrl_sw_privacy_unit, test_privacy_clear)
94 {
95 	helper_privacy_clear();
96 }
97 
ZTEST(test_ctrl_sw_privacy_unit,test_privacy_add)98 ZTEST(test_ctrl_sw_privacy_unit, test_privacy_add)
99 {
100 	helper_privacy_add(0);
101 }
102 
ZTEST(test_ctrl_sw_privacy_unit,test_privacy_add_stress)103 ZTEST(test_ctrl_sw_privacy_unit, test_privacy_add_stress)
104 {
105 	bt_addr_t ar;
106 
107 	for (uint8_t skew = 0; skew < CONFIG_BT_CTLR_RPA_CACHE_SIZE; skew++) {
108 		for (uint8_t i = 0; i < skew; i++) {
109 			bt_addr_copy(&ar,
110 				     BT_ADDR_INIT(0xde, 0xad, 0xbe,
111 						  0xef, 0xaa, 0xff));
112 			prpa_cache_add(&ar);
113 		}
114 		helper_privacy_add(skew);
115 		prpa_cache_clear();
116 	}
117 }
118