1 /*
2 * Copyright (c) 2022 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "mocks/addr.h"
8 #include "mocks/addr_expects.h"
9 #include "mocks/adv.h"
10 #include "mocks/hci_core.h"
11 #include "testing_common_defs.h"
12
13 #include <zephyr/bluetooth/hci.h>
14 #include <zephyr/fff.h>
15 #include <zephyr/kernel.h>
16
17 #include <host/hci_core.h>
18 #include <host/id.h>
19
20 DEFINE_FFF_GLOBALS;
21
fff_reset_rule_before(const struct ztest_unit_test * test,void * fixture)22 static void fff_reset_rule_before(const struct ztest_unit_test *test, void *fixture)
23 {
24 memset(&bt_dev, 0x00, sizeof(struct bt_dev));
25
26 ADV_FFF_FAKES_LIST(RESET_FAKE);
27 ADDR_FFF_FAKES_LIST(RESET_FAKE);
28 HCI_CORE_FFF_FAKES_LIST(RESET_FAKE);
29 }
30
31 ZTEST_RULE(fff_reset_rule, fff_reset_rule_before, NULL);
32
33 ZTEST_SUITE(bt_id_reset, NULL, NULL, NULL, NULL, NULL);
34
bt_addr_le_create_static_custom_fake(bt_addr_le_t * addr)35 static int bt_addr_le_create_static_custom_fake(bt_addr_le_t *addr)
36 {
37 const char *func_name = "bt_addr_le_create_static";
38
39 zassert_not_null(addr, "'%s()' was called with incorrect '%s' value", func_name, "addr");
40
41 /* This is required to test when the generated address already exists in the ID list */
42 if (bt_addr_le_create_static_fake.call_count == 1) {
43 bt_addr_le_copy(addr, BT_STATIC_RANDOM_LE_ADDR_1);
44 } else {
45 bt_addr_le_copy(addr, BT_STATIC_RANDOM_LE_ADDR_2);
46 }
47
48 return 0;
49 }
50
51 /*
52 * Test resetting an ID while using a NULL value for the address.
53 * As a NULL is passed to bt_id_reset() for the address and 'BT_DEV_ENABLE' is set,
54 * a new random address is generated.
55 *
56 * Constraints:
57 * - Input address is NULL
58 * - Input IRK is NULL
59 * - 'BT_DEV_ENABLE' flag is set in bt_dev.flags
60 * - bt_addr_le_create_static() returns a zero error code (success)
61 *
62 * Expected behaviour:
63 * - A new identity is created and the address is loaded to bt_dev.id_addr[]
64 * - bt_dev.id_count isn't changed
65 */
ZTEST(bt_id_reset,test_reset_id_null_address)66 ZTEST(bt_id_reset, test_reset_id_null_address)
67 {
68 uint8_t input_id;
69 int id_count, returned_id;
70
71 bt_dev.id_count = 2;
72 input_id = bt_dev.id_count - 1; /* ID must not equal BT_ID_DEFAULT */
73 id_count = bt_dev.id_count;
74 atomic_set_bit(bt_dev.flags, BT_DEV_ENABLE);
75 bt_addr_le_create_static_fake.custom_fake = bt_addr_le_create_static_custom_fake;
76
77 returned_id = bt_id_reset(input_id, NULL, NULL);
78
79 expect_call_count_bt_addr_le_create_static(1);
80
81 zassert_true(returned_id >= 0, "Unexpected error code '%d' was returned", returned_id);
82 zassert_equal(returned_id, input_id, "Incorrect ID %d was returned", returned_id);
83 zassert_true(bt_dev.id_count == id_count, "Incorrect ID count %d was set", bt_dev.id_count);
84 zassert_mem_equal(&bt_dev.id_addr[returned_id], BT_STATIC_RANDOM_LE_ADDR_1,
85 sizeof(bt_addr_le_t), "Incorrect address was set");
86 }
87
88 /*
89 * Test resetting an identity and generating a new one while ensuring that the generated address
90 * isn't in the ID list. As a NULL is passed to bt_id_reset() for the address and 'BT_DEV_ENABLE'
91 * is set, a new random address is generated.
92 *
93 * Constraints:
94 * - Input address is NULL
95 * - Input IRK is NULL
96 * - 'BT_DEV_ENABLE' flag is set in bt_dev.flags
97 * - bt_addr_le_create_static() returns a zero error code (success)
98 *
99 * Expected behaviour:
100 * - A new identity is created and the address is loaded to bt_dev.id_addr[]
101 * - bt_dev.id_count isn't changed
102 */
ZTEST(bt_id_reset,test_reset_id_null_address_with_no_duplication)103 ZTEST(bt_id_reset, test_reset_id_null_address_with_no_duplication)
104 {
105 uint8_t input_id;
106 int id_count, returned_id;
107
108 bt_dev.id_count = 2;
109 bt_addr_le_copy(&bt_dev.id_addr[0], BT_STATIC_RANDOM_LE_ADDR_1);
110
111 input_id = bt_dev.id_count - 1; /* ID must not equal BT_ID_DEFAULT */
112 id_count = bt_dev.id_count;
113 atomic_set_bit(bt_dev.flags, BT_DEV_ENABLE);
114 bt_addr_le_create_static_fake.custom_fake = bt_addr_le_create_static_custom_fake;
115
116 returned_id = bt_id_reset(input_id, NULL, NULL);
117
118 expect_call_count_bt_addr_le_create_static(2);
119
120 zassert_true(returned_id >= 0, "Unexpected error code '%d' was returned", returned_id);
121 zassert_equal(returned_id, input_id, "Incorrect ID %d was returned", returned_id);
122 zassert_true(bt_dev.id_count == id_count, "Incorrect ID count %d was set", bt_dev.id_count);
123 zassert_mem_equal(&bt_dev.id_addr[returned_id], BT_STATIC_RANDOM_LE_ADDR_2,
124 sizeof(bt_addr_le_t), "Incorrect address was set");
125 }
126
127 /*
128 * Test resetting an identity and using BT_ADDR_LE_ANY as an input.
129 * As an initialized address to BT_ADDR_LE_ANY is passed to bt_id_reset() for the address and
130 * 'BT_DEV_ENABLE' is set, a new random address is generated.
131 * The generated address should be copied to the address reference passed
132 *
133 * Constraints:
134 * - Input address is NULL
135 * - Input IRK is NULL
136 * - 'BT_DEV_ENABLE' flag is set in bt_dev.flags
137 * - bt_addr_le_create_static() returns a zero error code (success)
138 *
139 * Expected behaviour:
140 * - A new identity is created and the address is loaded to bt_dev.id_addr[]
141 * - bt_dev.id_count isn't changed
142 * - Generated address is copied to the address reference passed
143 */
ZTEST(bt_id_reset,test_reset_id_bt_addr_le_any_address)144 ZTEST(bt_id_reset, test_reset_id_bt_addr_le_any_address)
145 {
146 uint8_t input_id;
147 int id_count, returned_id;
148 bt_addr_le_t addr = bt_addr_le_any;
149
150 bt_dev.id_count = 2;
151 input_id = bt_dev.id_count - 1; /* ID must not equal BT_ID_DEFAULT */
152 id_count = bt_dev.id_count;
153 atomic_set_bit(bt_dev.flags, BT_DEV_ENABLE);
154 bt_addr_le_create_static_fake.custom_fake = bt_addr_le_create_static_custom_fake;
155
156 returned_id = bt_id_reset(input_id, &addr, NULL);
157
158 expect_call_count_bt_addr_le_create_static(1);
159
160 zassert_true(returned_id >= 0, "Unexpected error code '%d' was returned", returned_id);
161 zassert_equal(returned_id, input_id, "Incorrect ID %d was returned", returned_id);
162 zassert_true(bt_dev.id_count == id_count, "Incorrect ID count %d was set", bt_dev.id_count);
163 zassert_mem_equal(&bt_dev.id_addr[returned_id], BT_STATIC_RANDOM_LE_ADDR_1,
164 sizeof(bt_addr_le_t), "Incorrect address was set");
165 zassert_mem_equal(&addr, BT_STATIC_RANDOM_LE_ADDR_1, sizeof(bt_addr_le_t),
166 "Incorrect address was set");
167 }
168
169 /*
170 * Test resetting an identity, but bt_addr_le_create_static() returns an error.
171 *
172 * Constraints:
173 * - Input address is NULL
174 * - Input IRK is NULL
175 * - 'BT_DEV_ENABLE' flag is set in bt_dev.flags
176 * - bt_addr_le_create_static() returns a non-zero error code (failure)
177 *
178 * Expected behaviour:
179 * - No new identity is created
180 * - bt_dev.id_count is kept unchanged
181 */
ZTEST(bt_id_reset,test_reset_id_null_address_fails)182 ZTEST(bt_id_reset, test_reset_id_null_address_fails)
183 {
184 uint8_t input_id;
185 int id_count, err;
186
187 bt_dev.id_count = 2;
188 input_id = bt_dev.id_count - 1; /* ID must not equal BT_ID_DEFAULT */
189 id_count = bt_dev.id_count;
190 atomic_set_bit(bt_dev.flags, BT_DEV_ENABLE);
191 bt_addr_le_create_static_fake.return_val = -1;
192
193 err = bt_id_reset(input_id, NULL, NULL);
194
195 expect_call_count_bt_addr_le_create_static(1);
196
197 zassert_true(err == -1, "Unexpected error code '%d' was returned", err);
198 zassert_true(bt_dev.id_count == id_count, "Incorrect ID count %d was set", bt_dev.id_count);
199 }
200
201 /*
202 * Test resetting an identity while a valid random static address is passed to bt_id_reset() for
203 * the address and 'BT_DEV_ENABLE' is set.
204 * The same address is used and copied to bt_dev.id_addr[].
205 *
206 * Constraints:
207 * - Valid private random address is used
208 * - Input IRK is NULL
209 * - 'BT_DEV_ENABLE' flag is set in bt_dev.flags
210 *
211 * Expected behaviour:
212 * - The same address is used and loaded to bt_dev.id_addr[]
213 * - bt_dev.id_count is kept unchanged
214 */
ZTEST(bt_id_reset,test_reset_id_valid_input_address)215 ZTEST(bt_id_reset, test_reset_id_valid_input_address)
216 {
217 uint8_t input_id;
218 int id_count, returned_id;
219 bt_addr_le_t addr = *BT_STATIC_RANDOM_LE_ADDR_1;
220
221 bt_dev.id_count = 2;
222 input_id = bt_dev.id_count - 1; /* ID must not equal BT_ID_DEFAULT */
223 id_count = bt_dev.id_count;
224 atomic_set_bit(bt_dev.flags, BT_DEV_ENABLE);
225 /* Calling bt_addr_le_create_static() isn't expected */
226 bt_addr_le_create_static_fake.return_val = -1;
227
228 returned_id = bt_id_reset(input_id, &addr, NULL);
229
230 expect_not_called_bt_addr_le_create_static();
231
232 zassert_true(returned_id >= 0, "Unexpected error code '%d' was returned", returned_id);
233 zassert_equal(returned_id, input_id, "Incorrect ID %d was returned", returned_id);
234 zassert_true(bt_dev.id_count == id_count, "Incorrect ID count %d was set", bt_dev.id_count);
235 zassert_mem_equal(&bt_dev.id_addr[returned_id], BT_STATIC_RANDOM_LE_ADDR_1,
236 sizeof(bt_addr_le_t), "Incorrect address was set");
237 }
238