1 /*
2 * Copyright (c) 2022 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "mocks/adv.h"
8 #include "mocks/adv_expects.h"
9 #include "mocks/conn.h"
10 #include "mocks/conn_expects.h"
11 #include "mocks/hci_core.h"
12 #include "mocks/hci_core_expects.h"
13 #include "mocks/net_buf.h"
14 #include "mocks/net_buf_expects.h"
15 #include "mocks/scan.h"
16 #include "mocks/scan_expects.h"
17 #include "testing_common_defs.h"
18
19 #include <zephyr/bluetooth/hci.h>
20 #include <zephyr/fff.h>
21 #include <zephyr/kernel.h>
22
23 #include <host/hci_core.h>
24 #include <host/id.h>
25 #include <host/keys.h>
26
27 DEFINE_FFF_GLOBALS;
28
fff_reset_rule_before(const struct ztest_unit_test * test,void * fixture)29 static void fff_reset_rule_before(const struct ztest_unit_test *test, void *fixture)
30 {
31 memset(&bt_dev, 0x00, sizeof(struct bt_dev));
32
33 ADV_FFF_FAKES_LIST(RESET_FAKE);
34 CONN_FFF_FAKES_LIST(RESET_FAKE);
35 NET_BUF_FFF_FAKES_LIST(RESET_FAKE);
36 HCI_CORE_FFF_FAKES_LIST(RESET_FAKE);
37 }
38
39 ZTEST_RULE(fff_reset_rule, fff_reset_rule_before, NULL);
40
41 ZTEST_SUITE(bt_id_add, NULL, NULL, NULL, NULL, NULL);
42
43 /*
44 * Test adding key to the resolving list when size of the controller resolving list is zero
45 *
46 * Constraints:
47 * - bt_dev.le.rl_size is set to 0
48 *
49 * Expected behaviour:
50 * - Passed key state is updated by setting 'BT_KEYS_ID_ADDED' bit
51 */
ZTEST(bt_id_add,test_zero_controller_list_size)52 ZTEST(bt_id_add, test_zero_controller_list_size)
53 {
54 struct bt_keys keys = {0};
55 uint8_t expected_rl_entries;
56
57 bt_dev.le.rl_size = 0;
58 bt_dev.le.rl_entries = 0;
59 expected_rl_entries = bt_dev.le.rl_entries + 1;
60
61 bt_id_add(&keys);
62
63 expect_not_called_bt_conn_lookup_state_le();
64
65 zassert_equal(expected_rl_entries, bt_dev.le.rl_entries, "Incorrect entries count");
66 zassert_true((keys.state & BT_KEYS_ID_ADDED) == BT_KEYS_ID_ADDED, "Incorrect key state");
67 }
68
69 /*
70 * Test adding key to the resolving list when host side resolving is used
71 *
72 * Constraints:
73 * - bt_dev.le.rl_size is set to a value greater than 0
74 * - 'bt_dev.le.rl_entries > bt_dev.le.rl_size' condition is true
75 *
76 * Expected behaviour:
77 * - Passed key state is updated by setting 'BT_KEYS_ID_ADDED' bit
78 */
ZTEST(bt_id_add,test_host_side_resolving_used)79 ZTEST(bt_id_add, test_host_side_resolving_used)
80 {
81 struct bt_keys keys = {0};
82 uint8_t expected_rl_entries;
83
84 bt_dev.le.rl_size = 1;
85 bt_dev.le.rl_entries = 2;
86 expected_rl_entries = bt_dev.le.rl_entries + 1;
87
88 bt_id_add(&keys);
89
90 expect_not_called_bt_conn_lookup_state_le();
91
92 zassert_equal(expected_rl_entries, bt_dev.le.rl_entries, "Incorrect entries count");
93 zassert_true((keys.state & BT_KEYS_ID_ADDED) == BT_KEYS_ID_ADDED, "Incorrect key state");
94 }
95
96 /*
97 * Test adding key to the resolving list if host side resolving isn't used.
98 * bt_conn_lookup_state_le() returns a valid connection reference.
99 *
100 * Constraints:
101 * - bt_dev.le.rl_size is set to a value greater than 0
102 * - 'bt_dev.le.rl_entries > bt_dev.le.rl_size' condition is false
103 * - bt_conn_lookup_state_le() returns a valid connection reference.
104 *
105 * Expected behaviour:
106 * - Passed key state is updated by setting 'BT_KEYS_ID_PENDING_ADD' bit
107 * - 'BT_DEV_ID_PENDING' in bt_dev.flags is set
108 */
ZTEST(bt_id_add,test_conn_lookup_returns_valid_conn_ref)109 ZTEST(bt_id_add, test_conn_lookup_returns_valid_conn_ref)
110 {
111 struct bt_keys keys = {0};
112 struct bt_conn conn_ref = {0};
113
114 /* Break the host-side resolving condition */
115 bt_dev.le.rl_size = 1;
116 bt_dev.le.rl_entries = 1;
117
118 bt_conn_lookup_state_le_fake.return_val = &conn_ref;
119
120 bt_id_add(&keys);
121
122 expect_single_call_bt_conn_lookup_state_le(BT_ID_DEFAULT, NULL, BT_CONN_INITIATING);
123 expect_single_call_bt_conn_unref(&conn_ref);
124
125 zassert_true((keys.state & BT_KEYS_ID_PENDING_ADD) == BT_KEYS_ID_PENDING_ADD,
126 "Incorrect key state");
127 zassert_true(atomic_test_bit(bt_dev.flags, BT_DEV_ID_PENDING),
128 "Flags were not correctly set");
129 }
130
bt_le_ext_adv_foreach_custom_fake(void (* func)(struct bt_le_ext_adv * adv,void * data),void * data)131 void bt_le_ext_adv_foreach_custom_fake(void (*func)(struct bt_le_ext_adv *adv, void *data),
132 void *data)
133 {
134 struct bt_le_ext_adv adv_params = {0};
135
136 __ASSERT_NO_MSG(func != NULL);
137 __ASSERT_NO_MSG(data != NULL);
138
139 atomic_set_bit(adv_params.flags, BT_ADV_ENABLED);
140 atomic_set_bit(adv_params.flags, BT_ADV_LIMITED);
141
142 func(&adv_params, data);
143 }
144
145 /*
146 * Test adding key to the resolving list if host side resolving isn't used.
147 * bt_conn_lookup_state_le() returns a NULL connection reference and 'CONFIG_BT_BROADCASTER' and
148 * 'CONFIG_BT_EXT_ADV' are enabled.
149 *
150 * Constraints:
151 * - bt_dev.le.rl_size is set to a value greater than 0
152 * - 'bt_dev.le.rl_entries > bt_dev.le.rl_size' condition is false
153 * - bt_conn_lookup_state_le() returns NULL.
154 * - 'CONFIG_BT_BROADCASTER' and 'CONFIG_BT_EXT_ADV' are enabled.
155 * - adv_is_limited_enabled() sets advertise enable flag to true
156 *
157 * Expected behaviour:
158 * - Passed key state is updated by setting 'BT_KEYS_ID_PENDING_ADD' bit and 'BT_DEV_ID_PENDING'
159 * in bt_dev.flags is set if advertising is enabled
160 */
ZTEST(bt_id_add,test_conn_lookup_returns_null_broadcaster_ext_adv_enabled)161 ZTEST(bt_id_add, test_conn_lookup_returns_null_broadcaster_ext_adv_enabled)
162 {
163 struct bt_keys keys = {0};
164
165 Z_TEST_SKIP_IFNDEF(CONFIG_BT_EXT_ADV);
166 Z_TEST_SKIP_IFNDEF(CONFIG_BT_BROADCASTER);
167
168 /* Break the host-side resolving condition */
169 bt_dev.le.rl_size = 1;
170 bt_dev.le.rl_entries = 1;
171
172 bt_conn_lookup_state_le_fake.return_val = NULL;
173
174 /* When bt_le_ext_adv_foreach() is called, this callback will be triggered and causes
175 * adv_is_limited_enabled() to set the advertising enable flag to true.
176 */
177 bt_le_ext_adv_foreach_fake.custom_fake = bt_le_ext_adv_foreach_custom_fake;
178
179 bt_id_add(&keys);
180
181 expect_single_call_bt_le_ext_adv_foreach();
182
183 zassert_true((keys.state & BT_KEYS_ID_PENDING_ADD) == BT_KEYS_ID_PENDING_ADD,
184 "Incorrect key state");
185 zassert_true(atomic_test_bit(bt_dev.flags, BT_DEV_ID_PENDING),
186 "Flags were not correctly set");
187 }
188
189 /*
190 * Test adding key to the resolving list if host side resolving isn't used.
191 * bt_conn_lookup_state_le() returns a NULL connection reference.
192 * 'CONFIG_BT_BROADCASTER' is enabled while 'CONFIG_BT_EXT_ADV' isn't enabled.
193 *
194 * Constraints:
195 * - bt_dev.le.rl_size is set to a value greater than 0
196 * - bt_dev.le.rl_entries equals bt_dev.le.rl_size
197 * - bt_conn_lookup_state_le() returns NULL.
198 * - 'CONFIG_BT_BROADCASTER' is enabled.
199 * - 'CONFIG_BT_EXT_ADV' isn't enabled.
200 *
201 * Expected behaviour:
202 * - Passed key state is updated by setting 'BT_KEYS_ID_ADDED' bit
203 */
ZTEST(bt_id_add,test_conn_lookup_returns_null_broadcaster_no_ext_adv)204 ZTEST(bt_id_add, test_conn_lookup_returns_null_broadcaster_no_ext_adv)
205 {
206 struct bt_keys keys = {0};
207 struct net_buf net_buff = {0};
208 uint8_t expected_rl_entries;
209
210 Z_TEST_SKIP_IFDEF(CONFIG_BT_EXT_ADV);
211 Z_TEST_SKIP_IFNDEF(CONFIG_BT_BROADCASTER);
212
213 /* Break the host-side resolving condition */
214 bt_dev.le.rl_size = 1;
215 bt_dev.le.rl_entries = 1;
216 expected_rl_entries = bt_dev.le.rl_entries + 1;
217
218 bt_conn_lookup_state_le_fake.return_val = NULL;
219
220 /* This makes addr_res_enable() succeeds and returns 0 */
221 bt_hci_cmd_create_fake.return_val = &net_buff;
222 bt_hci_cmd_send_sync_fake.return_val = 0;
223
224 bt_id_add(&keys);
225
226 zassert_equal(expected_rl_entries, bt_dev.le.rl_entries, "Incorrect entries count");
227 zassert_true((keys.state & BT_KEYS_ID_ADDED) == BT_KEYS_ID_ADDED, "Incorrect key state");
228 }
229
230 /*
231 * Test adding key to the resolving list if host side resolving isn't used.
232 * bt_conn_lookup_state_le() returns a NULL connection reference.
233 * Number of entries in the resolving list is 0.
234 * 'CONFIG_BT_BROADCASTER' is enabled while 'CONFIG_BT_EXT_ADV' isn't enabled.
235 *
236 * Constraints:
237 * - bt_dev.le.rl_size is set to a value greater than 0
238 * - bt_dev.le.rl_entries is set to 0
239 * - bt_conn_lookup_state_le() returns NULL.
240 * - 'CONFIG_BT_BROADCASTER' is enabled.
241 * - 'CONFIG_BT_EXT_ADV' isn't enabled.
242 *
243 * Expected behaviour:
244 * - Passed key state is updated by setting 'BT_KEYS_ID_ADDED' bit
245 */
ZTEST(bt_id_add,test_conn_lookup_returns_null_no_ext_adv_no_resolving_entries)246 ZTEST(bt_id_add, test_conn_lookup_returns_null_no_ext_adv_no_resolving_entries)
247 {
248 struct bt_keys keys = {0};
249 struct net_buf net_buff = {0};
250 struct bt_hci_cp_le_add_dev_to_rl cp = {0};
251 uint8_t expected_rl_entries;
252 uint8_t zero_irk[16] = {0};
253 uint8_t testing_irk_value[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
254 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15};
255
256 Z_TEST_SKIP_IFDEF(CONFIG_BT_EXT_ADV);
257 Z_TEST_SKIP_IFNDEF(CONFIG_BT_BROADCASTER);
258
259 bt_addr_le_copy(&keys.addr, BT_RPA_LE_ADDR);
260 memcpy(keys.irk.val, testing_irk_value, 16);
261
262 /* Break the host-side resolving condition */
263 bt_dev.le.rl_size = 1;
264 bt_dev.le.rl_entries = 0;
265 expected_rl_entries = bt_dev.le.rl_entries + 1;
266
267 bt_conn_lookup_state_le_fake.return_val = NULL;
268
269 /* This makes hci_id_add() succeeds and returns 0 */
270 net_buf_simple_add_fake.return_val = &cp;
271 bt_hci_cmd_create_fake.return_val = &net_buff;
272 bt_hci_cmd_send_sync_fake.return_val = 0;
273
274 bt_id_add(&keys);
275
276 /* This verifies hci_id_add() behaviour */
277 expect_single_call_net_buf_simple_add(&net_buff.b, sizeof(cp));
278 zassert_mem_equal(&cp.peer_id_addr, BT_RPA_LE_ADDR, sizeof(bt_addr_le_t),
279 "Incorrect address was set");
280 zassert_mem_equal(cp.peer_irk, testing_irk_value, sizeof(testing_irk_value),
281 "Incorrect IRK value was set");
282 zassert_mem_equal(cp.local_irk, zero_irk, sizeof(zero_irk), "Incorrect IRK value was set");
283
284 zassert_equal(expected_rl_entries, bt_dev.le.rl_entries, "Incorrect entries count");
285 zassert_true((keys.state & BT_KEYS_ID_ADDED) == BT_KEYS_ID_ADDED, "Incorrect key state");
286 }
287
288 /*
289 * Test stopping scanning procedure if it is currently active and re-enable it after updating keys.
290 * If it is active, it is disabled then re-enabled after updating the key status.
291 * bt_conn_lookup_state_le() returns a NULL connection reference.
292 * 'CONFIG_BT_BROADCASTER', 'CONFIG_BT_OBSERVER' and 'CONFIG_BT_EXT_ADV' are enabled.
293 *
294 * Constraints:
295 * - bt_dev.le.rl_size is set to a value greater than 0
296 * - bt_dev.le.rl_entries is set to 0
297 * - bt_conn_lookup_state_le() returns NULL.
298 * - 'CONFIG_BT_BROADCASTER' is enabled.
299 * - 'CONFIG_BT_OBSERVER' is enabled.
300 * - 'CONFIG_BT_EXT_ADV' is enabled.
301 *
302 * Expected behaviour:
303 * - Passed key state is updated by setting 'BT_KEYS_ID_ADDED' bit
304 */
ZTEST(bt_id_add,test_scan_re_enabled_observer_enabled_ext_adv)305 ZTEST(bt_id_add, test_scan_re_enabled_observer_enabled_ext_adv)
306 {
307 struct bt_keys keys = {0};
308 struct net_buf net_buff = {0};
309 struct bt_hci_cp_le_add_dev_to_rl cp = {0};
310 uint8_t expected_args_history[] = {BT_HCI_LE_SCAN_DISABLE, BT_HCI_LE_SCAN_ENABLE};
311 uint8_t expected_rl_entries;
312
313 Z_TEST_SKIP_IFNDEF(CONFIG_BT_EXT_ADV);
314 Z_TEST_SKIP_IFNDEF(CONFIG_BT_OBSERVER);
315
316 /* Break the host-side resolving condition */
317 bt_dev.le.rl_size = 1;
318 bt_dev.le.rl_entries = 0;
319 expected_rl_entries = bt_dev.le.rl_entries + 1;
320
321 /* Make scan enabled flag true */
322 atomic_set_bit(bt_dev.flags, BT_DEV_SCANNING);
323 atomic_set_bit(bt_dev.flags, BT_DEV_SCAN_LIMITED);
324
325 bt_conn_lookup_state_le_fake.return_val = NULL;
326
327 /* This makes hci_id_add() succeeds and returns 0 */
328 net_buf_simple_add_fake.return_val = &cp;
329 bt_hci_cmd_create_fake.return_val = &net_buff;
330 bt_hci_cmd_send_sync_fake.return_val = 0;
331
332 bt_id_add(&keys);
333
334 expect_call_count_bt_le_scan_set_enable(2, expected_args_history);
335
336 zassert_equal(expected_rl_entries, bt_dev.le.rl_entries, "Incorrect entries count");
337 zassert_true((keys.state & BT_KEYS_ID_ADDED) == BT_KEYS_ID_ADDED, "Incorrect key state");
338 zassert_true((keys.state & BT_KEYS_ID_PENDING_ADD) == BT_KEYS_ID_PENDING_ADD,
339 "Incorrect key state");
340 }
341