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/keys.h"
14 #include "mocks/keys_expects.h"
15 #include "mocks/net_buf.h"
16 #include "mocks/net_buf_expects.h"
17 #include "mocks/scan.h"
18 #include "mocks/scan_expects.h"
19 #include "testing_common_defs.h"
20 
21 #include <zephyr/bluetooth/hci.h>
22 #include <zephyr/fff.h>
23 #include <zephyr/kernel.h>
24 
25 #include <host/hci_core.h>
26 #include <host/id.h>
27 
28 DEFINE_FFF_GLOBALS;
29 
fff_reset_rule_before(const struct ztest_unit_test * test,void * fixture)30 static void fff_reset_rule_before(const struct ztest_unit_test *test, void *fixture)
31 {
32 	memset(&bt_dev, 0x00, sizeof(struct bt_dev));
33 
34 	ADV_FFF_FAKES_LIST(RESET_FAKE);
35 	CONN_FFF_FAKES_LIST(RESET_FAKE);
36 	KEYS_FFF_FAKES_LIST(RESET_FAKE);
37 	NET_BUF_FFF_FAKES_LIST(RESET_FAKE);
38 	HCI_CORE_FFF_FAKES_LIST(RESET_FAKE);
39 }
40 
41 ZTEST_RULE(fff_reset_rule, fff_reset_rule_before, NULL);
42 
43 ZTEST_SUITE(bt_id_del, NULL, NULL, NULL, NULL, NULL);
44 
45 /*
46  *  Test deleting key from the resolving list when size of controller resolving list is zero
47  *
48  *  Constraints:
49  *   - bt_dev.le.rl_size is set to 0
50  *   - bt_dev.le.rl_entries is greater than 0
51  *
52  *  Expected behaviour:
53  *   - Passed key state is updated by clearing 'BT_KEYS_ID_ADDED' bit
54  */
ZTEST(bt_id_del,test_zero_controller_list_size)55 ZTEST(bt_id_del, test_zero_controller_list_size)
56 {
57 	struct bt_keys keys = {0};
58 	uint8_t expected_rl_entries;
59 
60 	bt_dev.le.rl_size = 0;
61 	bt_dev.le.rl_entries = 1;
62 	expected_rl_entries = bt_dev.le.rl_entries - 1;
63 	keys.state |= BT_KEYS_ID_ADDED;
64 
65 	bt_id_del(&keys);
66 
67 	expect_not_called_bt_conn_lookup_state_le();
68 
69 	zassert_equal(expected_rl_entries, bt_dev.le.rl_entries, "Incorrect entries count");
70 	zassert_false(keys.state & BT_KEYS_ID_ADDED, "Incorrect key state");
71 }
72 
73 /*
74  *  Test deleting key from the resolving list when size of controller resolving list isn't zero
75  *  and number of entries in the resolving list is greater than controller resolving list size.
76  *
77  *  Constraints:
78  *   - bt_dev.le.rl_size is set to 0
79  *   - bt_dev.le.rl_entries is greater than (bt_dev.le.rl_size + 1)
80  *
81  *  Expected behaviour:
82  *   - Passed key state is updated by clearing 'BT_KEYS_ID_ADDED' bit
83  */
ZTEST(bt_id_del,test_resolving_list_entries_greater_than_controller_list_size)84 ZTEST(bt_id_del, test_resolving_list_entries_greater_than_controller_list_size)
85 {
86 	struct bt_keys keys = {0};
87 	uint8_t expected_rl_entries;
88 
89 	bt_dev.le.rl_size = 1;
90 	bt_dev.le.rl_entries = 3;
91 	expected_rl_entries = bt_dev.le.rl_entries - 1;
92 	keys.state |= BT_KEYS_ID_ADDED;
93 
94 	bt_id_del(&keys);
95 
96 	expect_not_called_bt_conn_lookup_state_le();
97 
98 	zassert_equal(expected_rl_entries, bt_dev.le.rl_entries, "Incorrect entries count");
99 	zassert_false(keys.state & BT_KEYS_ID_ADDED, "Incorrect key state");
100 }
101 
102 /*
103  *  Test deleting key from the resolving list if host side resolving isn't used.
104  *  bt_conn_lookup_state_le() returns a valid connection reference.
105  *
106  *  Constraints:
107  *   - bt_dev.le.rl_size is set to a value greater than 0
108  *   - 'bt_dev.le.rl_entries > bt_dev.le.rl_size + 1' condition is false
109  *   - bt_conn_lookup_state_le() returns a valid connection reference.
110  *
111  *  Expected behaviour:
112  *   - Passed key state is updated by setting 'BT_KEYS_ID_PENDING_DEL' bit
113  *   - 'BT_DEV_ID_PENDING' in bt_dev.flags is set
114  */
ZTEST(bt_id_del,test_conn_lookup_returns_valid_conn_ref)115 ZTEST(bt_id_del, test_conn_lookup_returns_valid_conn_ref)
116 {
117 	struct bt_keys keys = {0};
118 	struct bt_conn conn_ref = {0};
119 
120 	/* Break the host-side resolving condition */
121 	bt_dev.le.rl_size = 1;
122 	bt_dev.le.rl_entries = 1;
123 
124 	bt_conn_lookup_state_le_fake.return_val = &conn_ref;
125 
126 	bt_id_del(&keys);
127 
128 	expect_single_call_bt_conn_lookup_state_le(BT_ID_DEFAULT, NULL, BT_CONN_INITIATING);
129 	expect_single_call_bt_conn_unref(&conn_ref);
130 
131 	zassert_true((keys.state & BT_KEYS_ID_PENDING_DEL) == BT_KEYS_ID_PENDING_DEL,
132 		     "Incorrect key state");
133 	zassert_true(atomic_test_bit(bt_dev.flags, BT_DEV_ID_PENDING),
134 		     "Flags were not correctly set");
135 }
136 
bt_le_ext_adv_foreach_custom_fake(void (* func)(struct bt_le_ext_adv * adv,void * data),void * data)137 void bt_le_ext_adv_foreach_custom_fake(void (*func)(struct bt_le_ext_adv *adv, void *data),
138 				       void *data)
139 {
140 	struct bt_le_ext_adv adv_params = {0};
141 
142 	__ASSERT_NO_MSG(func != NULL);
143 	__ASSERT_NO_MSG(data != NULL);
144 
145 	atomic_set_bit(adv_params.flags, BT_ADV_ENABLED);
146 	atomic_set_bit(adv_params.flags, BT_ADV_LIMITED);
147 
148 	func(&adv_params, data);
149 }
150 
151 /*
152  *  Test deleting key from the resolving list if host side resolving isn't used.
153  *  bt_conn_lookup_state_le() returns a NULL connection reference and 'CONFIG_BT_BROADCASTER' and
154  *  'CONFIG_BT_EXT_ADV' are enabled.
155  *
156  *  Constraints:
157  *   - bt_dev.le.rl_size is set to a value greater than 0
158  *   - 'bt_dev.le.rl_entries > bt_dev.le.rl_size + 1' condition is false
159  *   - bt_conn_lookup_state_le() returns NULL.
160  *   - 'CONFIG_BT_BROADCASTER' and 'CONFIG_BT_EXT_ADV' are enabled.
161  *   - adv_is_limited_enabled() sets advertise enable flag to true
162  *
163  *  Expected behaviour:
164  *   - Passed key state is updated by setting 'BT_KEYS_ID_PENDING_DEL' bit
165  *   - 'BT_DEV_ID_PENDING' in bt_dev.flags is set if advertising is enabled
166  */
ZTEST(bt_id_del,test_conn_lookup_returns_null_broadcaster_ext_adv_enabled)167 ZTEST(bt_id_del, test_conn_lookup_returns_null_broadcaster_ext_adv_enabled)
168 {
169 	struct bt_keys keys = {0};
170 
171 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_EXT_ADV);
172 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_BROADCASTER);
173 
174 	/* Break the host-side resolving condition */
175 	bt_dev.le.rl_size = 1;
176 	bt_dev.le.rl_entries = 1;
177 
178 	bt_conn_lookup_state_le_fake.return_val = NULL;
179 
180 	/* When bt_le_ext_adv_foreach() is called, this callback will be triggered and causes
181 	 * adv_is_limited_enabled() to set the advertising enable flag to true.
182 	 */
183 	bt_le_ext_adv_foreach_fake.custom_fake = bt_le_ext_adv_foreach_custom_fake;
184 
185 	bt_id_del(&keys);
186 
187 	expect_single_call_bt_le_ext_adv_foreach();
188 
189 	zassert_true((keys.state & BT_KEYS_ID_PENDING_DEL) == BT_KEYS_ID_PENDING_DEL,
190 		     "Incorrect key state");
191 	zassert_true(atomic_test_bit(bt_dev.flags, BT_DEV_ID_PENDING),
192 		     "Flags were not correctly set");
193 }
194 
195 /*
196  *  Test deleting key from the resolving list when host side resolving isn't used.
197  *  bt_conn_lookup_state_le() returns a NULL connection reference.
198  *  'CONFIG_BT_BROADCASTER' is enabled while 'CONFIG_BT_EXT_ADV' isn't enabled.
199  *
200  *  Constraints:
201  *   - bt_dev.le.rl_size is set to a value greater than 0
202  *   - (bt_dev.le.rl_entries > bt_dev.le.rl_size ) true
203  *   - (bt_dev.le.rl_entries > bt_dev.le.rl_size + 1) false
204  *   - bt_conn_lookup_state_le() returns NULL.
205  *   - 'CONFIG_BT_BROADCASTER' is enabled.
206  *   - 'CONFIG_BT_EXT_ADV' isn't enabled.
207  *   - 'CONFIG_BT_PRIVACY' isn't enabled.
208  *
209  *  Expected behaviour:
210  *   - Passed key state is updated by setting 'BT_KEYS_ID_ADDED' bit
211  */
ZTEST(bt_id_del,test_conn_lookup_returns_null_broadcaster_no_ext_adv)212 ZTEST(bt_id_del, test_conn_lookup_returns_null_broadcaster_no_ext_adv)
213 {
214 	struct bt_keys keys = {0};
215 	struct net_buf net_buff = {0};
216 	uint8_t expected_rl_entries;
217 
218 	Z_TEST_SKIP_IFDEF(CONFIG_BT_EXT_ADV);
219 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_BROADCASTER);
220 	Z_TEST_SKIP_IFDEF(CONFIG_BT_PRIVACY);
221 
222 	/* Break the host-side resolving condition */
223 	bt_dev.le.rl_size = 1;
224 	/*
225 	 * (bt_dev.le.rl_entries > bt_dev.le.rl_size ) true
226 	 * (bt_dev.le.rl_entries > bt_dev.le.rl_size + 1) false
227 	 */
228 	bt_dev.le.rl_entries = 2;
229 	expected_rl_entries = bt_dev.le.rl_entries - 1;
230 
231 	bt_conn_lookup_state_le_fake.return_val = NULL;
232 	keys.state |= BT_KEYS_ID_ADDED;
233 
234 	/* This makes addr_res_enable() succeeds and returns 0 */
235 	bt_hci_cmd_create_fake.return_val = &net_buff;
236 	bt_hci_cmd_send_sync_fake.return_val = 0;
237 
238 	bt_id_del(&keys);
239 
240 	expect_single_call_bt_keys_foreach_type(BT_KEYS_IRK);
241 
242 	zassert_equal(expected_rl_entries, bt_dev.le.rl_entries, "Incorrect entries count");
243 	zassert_false(keys.state & BT_KEYS_ID_ADDED, "Incorrect key state");
244 }
245 
246 /*
247  *  Test deleting key from the resolving list when host side resolving isn't used.
248  *  bt_conn_lookup_state_le() returns a NULL connection reference.
249  *  'CONFIG_BT_BROADCASTER' and 'CONFIG_BT_PRIVACY' are enabled while 'CONFIG_BT_EXT_ADV' isn't
250  *  enabled.
251  *
252  *  Constraints:
253  *   - bt_dev.le.rl_size is set to a value greater than 0
254  *   - (bt_dev.le.rl_entries > bt_dev.le.rl_size ) true
255  *   - (bt_dev.le.rl_entries > bt_dev.le.rl_size + 1) false
256  *   - bt_conn_lookup_state_le() returns NULL.
257  *   - 'CONFIG_BT_BROADCASTER' is enabled.
258  *   - 'CONFIG_BT_EXT_ADV' isn't enabled.
259  *   - 'CONFIG_BT_PRIVACY' is enabled.
260  *
261  *  Expected behaviour:
262  *   - Passed key state is updated by setting 'BT_KEYS_ID_ADDED' bit
263  */
ZTEST(bt_id_del,test_conn_lookup_returns_null_broadcaster_no_ext_adv_privacy_enabled)264 ZTEST(bt_id_del, test_conn_lookup_returns_null_broadcaster_no_ext_adv_privacy_enabled)
265 {
266 	struct bt_keys keys = {0};
267 	struct net_buf net_buff = {0};
268 	uint8_t expected_rl_entries;
269 
270 	Z_TEST_SKIP_IFDEF(CONFIG_BT_EXT_ADV);
271 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_BROADCASTER);
272 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_PRIVACY);
273 
274 	/* Break the host-side resolving condition */
275 	bt_dev.le.rl_size = 1;
276 	/*
277 	 * (bt_dev.le.rl_entries > bt_dev.le.rl_size ) true
278 	 * (bt_dev.le.rl_entries > bt_dev.le.rl_size + 1) false
279 	 */
280 	bt_dev.le.rl_entries = 2;
281 	expected_rl_entries = bt_dev.le.rl_entries - 1;
282 
283 	bt_conn_lookup_state_le_fake.return_val = NULL;
284 	keys.state |= BT_KEYS_ID_ADDED;
285 
286 	/* This makes addr_res_enable() succeeds and returns 0 */
287 	bt_hci_cmd_create_fake.return_val = &net_buff;
288 	bt_hci_cmd_send_sync_fake.return_val = 0;
289 
290 	bt_id_del(&keys);
291 
292 	expect_single_call_bt_keys_foreach_type(BT_KEYS_ALL);
293 
294 	zassert_equal(expected_rl_entries, bt_dev.le.rl_entries, "Incorrect entries count");
295 	zassert_false(keys.state & BT_KEYS_ID_ADDED, "Incorrect key state");
296 }
297 
298 /*
299  *  Test deleting key from the resolving list when host side resolving isn't used.
300  *  bt_conn_lookup_state_le() returns a NULL connection reference.
301  *  'CONFIG_BT_BROADCASTER' and 'CONFIG_BT_PRIVACY' are enabled while 'CONFIG_BT_EXT_ADV' isn't
302  *  enabled.
303  *  An HCI key address delete request is sent through hci_id_del()
304  *
305  *  Constraints:
306  *   - bt_dev.le.rl_size is set to a value greater than 0
307  *   - bt_dev.le.rl_entries equals bt_dev.le.rl_size
308  *   - bt_conn_lookup_state_le() returns NULL.
309  *   - 'CONFIG_BT_BROADCASTER' is enabled.
310  *   - 'CONFIG_BT_EXT_ADV' isn't enabled.
311  *   - 'CONFIG_BT_PRIVACY' is enabled.
312  *
313  *  Expected behaviour:
314  *   - hci_id_del() uses the correct address while creating the HCI request
315  *   - Passed key state is updated by setting 'BT_KEYS_ID_ADDED' bit
316  */
ZTEST(bt_id_del,test_send_hci_id_del)317 ZTEST(bt_id_del, test_send_hci_id_del)
318 {
319 	struct bt_keys keys = {0};
320 	struct net_buf net_buff = {0};
321 	struct bt_hci_cp_le_rem_dev_from_rl cp = {0};
322 	uint8_t expected_rl_entries;
323 
324 	/* Break the host-side resolving condition */
325 	bt_dev.le.rl_size = 1;
326 	bt_dev.le.rl_entries = 1;
327 	expected_rl_entries = bt_dev.le.rl_entries - 1;
328 
329 	bt_conn_lookup_state_le_fake.return_val = NULL;
330 	keys.state |= BT_KEYS_ID_ADDED;
331 
332 	bt_addr_le_copy(&keys.addr, BT_RPA_LE_ADDR);
333 
334 	/* This makes hci_id_del() succeeds and returns 0 */
335 	net_buf_simple_add_fake.return_val = &cp;
336 	bt_hci_cmd_create_fake.return_val = &net_buff;
337 	bt_hci_cmd_send_sync_fake.return_val = 0;
338 
339 	bt_id_del(&keys);
340 
341 	/* This verifies hci_id_del() behaviour */
342 	expect_single_call_net_buf_simple_add(&net_buff.b, sizeof(cp));
343 
344 	zassert_mem_equal(&cp.peer_id_addr, BT_RPA_LE_ADDR, sizeof(bt_addr_le_t),
345 			  "Incorrect address was set");
346 	zassert_equal(expected_rl_entries, bt_dev.le.rl_entries, "Incorrect entries count");
347 	zassert_false(keys.state & BT_KEYS_ID_ADDED, "Incorrect key state");
348 }
349 
350 /*
351  *  Test stopping scanning procedure if it is currently active and re-enable it after updating keys.
352  *  If it is active, it is disabled then re-enabled after updating the key status.
353  *  bt_conn_lookup_state_le() returns a NULL connection reference.
354  *  'CONFIG_BT_BROADCASTER', 'CONFIG_BT_OBSERVER' and 'CONFIG_BT_EXT_ADV' are enabled.
355  *
356  *  Constraints:
357  *   - bt_dev.le.rl_size is set to a value greater than 0
358  *   - bt_dev.le.rl_entries is set to 0
359  *   - bt_conn_lookup_state_le() returns NULL.
360  *   - 'CONFIG_BT_BROADCASTER' is enabled.
361  *   - 'CONFIG_BT_OBSERVER' is enabled.
362  *   - 'CONFIG_BT_EXT_ADV' is enabled.
363  *
364  *  Expected behaviour:
365  *   - Passed key state is updated by setting 'BT_KEYS_ID_ADDED' bit
366  */
ZTEST(bt_id_del,test_scan_re_enabled_observer_enabled_ext_adv)367 ZTEST(bt_id_del, test_scan_re_enabled_observer_enabled_ext_adv)
368 {
369 	struct bt_keys keys = {0};
370 	struct net_buf net_buff = {0};
371 	struct bt_hci_cp_le_rem_dev_from_rl cp = {0};
372 	uint8_t expected_args_history[] = {BT_HCI_LE_SCAN_DISABLE, BT_HCI_LE_SCAN_ENABLE};
373 	uint8_t expected_rl_entries;
374 
375 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_EXT_ADV);
376 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_OBSERVER);
377 
378 	/* Break the host-side resolving condition */
379 	bt_dev.le.rl_size = 1;
380 	bt_dev.le.rl_entries = 1;
381 	expected_rl_entries = bt_dev.le.rl_entries - 1;
382 
383 	/* Make scan enabled flag true */
384 	atomic_set_bit(bt_dev.flags, BT_DEV_SCANNING);
385 	atomic_set_bit(bt_dev.flags, BT_DEV_SCAN_LIMITED);
386 
387 	bt_conn_lookup_state_le_fake.return_val = NULL;
388 
389 	/* This makes hci_id_del() succeeds and returns 0 */
390 	net_buf_simple_add_fake.return_val = &cp;
391 	bt_hci_cmd_create_fake.return_val = &net_buff;
392 	bt_hci_cmd_send_sync_fake.return_val = 0;
393 
394 	bt_id_del(&keys);
395 
396 	expect_call_count_bt_le_scan_set_enable(2, expected_args_history);
397 
398 	zassert_equal(expected_rl_entries, bt_dev.le.rl_entries, "Incorrect entries count");
399 	zassert_false(keys.state & BT_KEYS_ID_ADDED, "Incorrect key state");
400 }
401