1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "mocks/crypto.h"
8 #include "mocks/scan.h"
9 #include "mocks/scan_expects.h"
10 #include "mocks/hci_core.h"
11 #include "mocks/hci_core_expects.h"
12 #include "mocks/net_buf.h"
13 #include "mocks/net_buf_expects.h"
14 #include "testing_common_defs.h"
15 
16 #include <zephyr/bluetooth/hci.h>
17 #include <zephyr/bluetooth/hci_types.h>
18 #include <zephyr/fff.h>
19 #include <zephyr/kernel.h>
20 
21 #include <host/hci_core.h>
22 #include <host/id.h>
23 
24 DEFINE_FFF_GLOBALS;
25 
fff_reset_rule_before(const struct ztest_unit_test * test,void * fixture)26 static void fff_reset_rule_before(const struct ztest_unit_test *test, void *fixture)
27 {
28 	memset(&bt_dev, 0x00, sizeof(struct bt_dev));
29 
30 	CRYPTO_FFF_FAKES_LIST(RESET_FAKE);
31 	HCI_CORE_FFF_FAKES_LIST(RESET_FAKE);
32 }
33 
34 ZTEST_RULE(fff_reset_rule, fff_reset_rule_before, NULL);
35 
36 ZTEST_SUITE(bt_id_set_adv_own_addr, NULL, NULL, NULL, NULL, NULL);
37 
38 /*
39  *  Test setting the advertising private address through bt_id_set_adv_private_addr() if
40  *  privacy is enabled and 'BT_LE_ADV_OPT_USE_IDENTITY' options bit isn't set.
41  *
42  *  Constraints:
43  *   - Options 'BT_LE_ADV_OPT_CONN' bit is set
44  *   - Options 'BT_LE_ADV_OPT_USE_IDENTITY' bit isn't set
45  *   - 'CONFIG_BT_PRIVACY' is enabled
46  *   - bt_id_set_adv_private_addr() succeeds and returns 0
47  *
48  *  Expected behaviour:
49  *   - bt_id_set_adv_own_addr() succeeds and returns 0
50  *   - Address type reference is updated
51  */
ZTEST(bt_id_set_adv_own_addr,test_bt_id_set_adv_private_addr_succeeds_adv_connectable)52 ZTEST(bt_id_set_adv_own_addr, test_bt_id_set_adv_private_addr_succeeds_adv_connectable)
53 {
54 	int err;
55 	uint32_t options = 0;
56 	struct bt_le_ext_adv adv = {0};
57 	uint8_t own_addr_type = BT_ADDR_LE_ANONYMOUS;
58 	bool dir_adv_test_lut[] = {true, false};
59 
60 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_PRIVACY);
61 
62 	options |= BT_LE_ADV_OPT_CONN;
63 
64 	/* This will cause bt_id_set_adv_private_addr() to return 0 */
65 	atomic_set_bit(bt_dev.flags, BT_DEV_RPA_VALID);
66 
67 	for (size_t i = 0; i < ARRAY_SIZE(dir_adv_test_lut); i++) {
68 		err = bt_id_set_adv_own_addr(&adv, options, dir_adv_test_lut[i], &own_addr_type);
69 
70 		zassert_ok(err, "Unexpected error code '%d' was returned", err);
71 		zassert_true(own_addr_type == BT_HCI_OWN_ADDR_RANDOM,
72 			     "Address type reference was incorrectly set");
73 	}
74 
75 	options |= BT_LE_ADV_OPT_DIR_ADDR_RPA;
76 
77 	bt_dev.le.features[(BT_LE_FEAT_BIT_PRIVACY) >> 3] |= BIT((BT_LE_FEAT_BIT_PRIVACY)&7);
78 
79 	err = bt_id_set_adv_own_addr(&adv, options, true, &own_addr_type);
80 
81 	zassert_ok(err, "Unexpected error code '%d' was returned", err);
82 	zassert_true(own_addr_type == BT_HCI_OWN_ADDR_RPA_OR_RANDOM,
83 		     "Address type reference was incorrectly set");
84 }
85 
86 /*
87  *  Test setting the advertising private address with a static random address through
88  *  bt_id_set_adv_random_addr() if privacy isn't enabled.
89  *
90  *  Constraints:
91  *   - Options 'BT_LE_ADV_OPT_CONN' bit is set
92  *   - 'CONFIG_BT_PRIVACY' isn't enabled
93  *   - bt_id_set_adv_random_addr() succeeds and returns 0
94  *
95  *  Expected behaviour:
96  *   - bt_id_set_adv_own_addr() succeeds and returns 0
97  *   - Address type reference is updated
98  */
ZTEST(bt_id_set_adv_own_addr,test_bt_id_set_adv_random_addr_succeeds_adv_connectable)99 ZTEST(bt_id_set_adv_own_addr, test_bt_id_set_adv_random_addr_succeeds_adv_connectable)
100 {
101 	int err;
102 	uint32_t options = 0;
103 	struct bt_le_ext_adv adv = {0};
104 	uint8_t own_addr_type = BT_ADDR_LE_ANONYMOUS;
105 	bool dir_adv_test_lut[] = {true, false};
106 
107 	Z_TEST_SKIP_IFDEF(CONFIG_BT_PRIVACY);
108 	/* If 'CONFIG_BT_EXT_ADV' is defined, it changes bt_id_set_adv_random_addr() behaviour */
109 	Z_TEST_SKIP_IFDEF(CONFIG_BT_EXT_ADV);
110 
111 	options |= BT_LE_ADV_OPT_CONN;
112 
113 	adv.id = 0;
114 	bt_addr_le_copy(&bt_dev.id_addr[adv.id], BT_RPA_LE_ADDR);
115 
116 	/* This will cause bt_id_set_adv_random_addr() to return 0 */
117 	bt_addr_copy(&bt_dev.random_addr.a, &BT_RPA_LE_ADDR->a);
118 
119 	for (size_t i = 0; i < ARRAY_SIZE(dir_adv_test_lut); i++) {
120 		err = bt_id_set_adv_own_addr(&adv, options, dir_adv_test_lut[i], &own_addr_type);
121 
122 		zassert_ok(err, "Unexpected error code '%d' was returned", err);
123 		zassert_true(own_addr_type == BT_HCI_OWN_ADDR_RANDOM,
124 			     "Address type reference was incorrectly set");
125 	}
126 
127 	options |= BT_LE_ADV_OPT_DIR_ADDR_RPA;
128 
129 	bt_dev.le.features[(BT_LE_FEAT_BIT_PRIVACY) >> 3] |= BIT((BT_LE_FEAT_BIT_PRIVACY)&7);
130 
131 	err = bt_id_set_adv_own_addr(&adv, options, true, &own_addr_type);
132 
133 	zassert_ok(err, "Unexpected error code '%d' was returned", err);
134 	zassert_true(own_addr_type == BT_HCI_OWN_ADDR_RPA_OR_RANDOM,
135 		     "Address type reference was incorrectly set");
136 }
137 
138 /*
139  *  Test setting the advertising private address with a static random address through
140  *  bt_id_set_adv_random_addr() when device isn't advertising as a connectable device (i.e.
141  *  BT_LE_ADV_OPT_CONN bit in options isn't set) and the advertisement is using the device
142  *  identity (i.e. BT_LE_ADV_OPT_USE_IDENTITY bit is set in options).
143  *
144  *  Constraints:
145  *   - Options 'BT_LE_ADV_OPT_USE_IDENTITY' bit is set
146  *   - Options 'BT_LE_ADV_OPT_CONN' bit isn't set
147  *   - bt_id_set_adv_random_addr() succeeds and returns 0
148  *
149  *  Expected behaviour:
150  *   - bt_id_set_adv_own_addr() succeeds and returns 0
151  *   - Address type reference is updated
152  */
ZTEST(bt_id_set_adv_own_addr,test_bt_id_set_adv_random_addr_succeeds_not_connectable)153 ZTEST(bt_id_set_adv_own_addr, test_bt_id_set_adv_random_addr_succeeds_not_connectable)
154 {
155 	int err;
156 	uint32_t options = 0;
157 	struct bt_le_ext_adv adv = {0};
158 	uint8_t own_addr_type = BT_ADDR_LE_ANONYMOUS;
159 	bool dir_adv_test_lut[] = {true, false};
160 
161 	/* If 'CONFIG_BT_EXT_ADV' is defined, it changes bt_id_set_adv_random_addr() behaviour */
162 	Z_TEST_SKIP_IFDEF(CONFIG_BT_EXT_ADV);
163 
164 	options |= BT_LE_ADV_OPT_USE_IDENTITY;
165 	options &= ~BT_LE_ADV_OPT_CONN;
166 
167 	adv.id = 0;
168 	bt_addr_le_copy(&bt_dev.id_addr[adv.id], BT_RPA_LE_ADDR);
169 
170 	/* This will cause bt_id_set_adv_random_addr() to return 0 */
171 	bt_addr_copy(&bt_dev.random_addr.a, &BT_RPA_LE_ADDR->a);
172 
173 	for (size_t i = 0; i < ARRAY_SIZE(dir_adv_test_lut); i++) {
174 		err = bt_id_set_adv_own_addr(&adv, options, dir_adv_test_lut[i], &own_addr_type);
175 
176 		zassert_ok(err, "Unexpected error code '%d' was returned", err);
177 		zassert_true(own_addr_type == BT_HCI_OWN_ADDR_RANDOM,
178 			     "Address type reference was incorrectly set");
179 	}
180 }
181 
182 /*
183  *  Test setting the advertising private address through bt_id_set_adv_private_addr() if
184  *  'BT_LE_ADV_OPT_USE_IDENTITY' and 'BT_LE_ADV_OPT_USE_IDENTITY' options bits aren't set.
185  *
186  *  Constraints:
187  *   - Options 'BT_LE_ADV_OPT_CONN' bit isn't set
188  *   - Options 'BT_LE_ADV_OPT_USE_IDENTITY' bit isn't set
189  *   - bt_id_set_adv_random_addr() succeeds and returns 0
190  *
191  *  Expected behaviour:
192  *   - bt_id_set_adv_own_addr() succeeds and returns 0
193  *   - Address type reference is updated
194  */
ZTEST(bt_id_set_adv_own_addr,test_bt_id_set_adv_private_addr_succeeds_not_connectable)195 ZTEST(bt_id_set_adv_own_addr, test_bt_id_set_adv_private_addr_succeeds_not_connectable)
196 {
197 	int err;
198 	uint32_t options = 0;
199 	struct bt_le_ext_adv adv = {0};
200 	uint8_t own_addr_type = BT_ADDR_LE_ANONYMOUS;
201 	bool dir_adv_test_lut[] = {true, false};
202 
203 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_EXT_ADV);
204 
205 	options &= ~BT_LE_ADV_OPT_CONN;
206 	options &= ~BT_LE_ADV_OPT_USE_IDENTITY;
207 
208 	/* This will cause bt_id_set_adv_private_addr() to return 0 */
209 	atomic_set_bit(bt_dev.flags, BT_DEV_RPA_VALID);
210 
211 	for (size_t i = 0; i < ARRAY_SIZE(dir_adv_test_lut); i++) {
212 		err = bt_id_set_adv_own_addr(&adv, options, dir_adv_test_lut[i], &own_addr_type);
213 
214 		zassert_ok(err, "Unexpected error code '%d' was returned", err);
215 		zassert_true(own_addr_type == BT_HCI_OWN_ADDR_RANDOM,
216 			     "Address type reference was incorrectly set");
217 	}
218 }
219 
220 /*
221  *  Test stopping scanning if it is supported through enabling 'CONFIG_BT_OBSERVER' and active
222  *  before updating the device advertising address and then re-enable it after the update is done.
223  *
224  *  Constraints:
225  *   - Options 'BT_LE_ADV_OPT_CONN' bit isn't set
226  *   - Options 'BT_LE_ADV_OPT_USE_IDENTITY' bit isn't set
227  *
228  *  Expected behaviour:
229  *   - Scanning is disabled and then re-enabled again after updating the address
230  */
ZTEST(bt_id_set_adv_own_addr,test_observer_scanning_re_enabled_after_updating_address)231 ZTEST(bt_id_set_adv_own_addr, test_observer_scanning_re_enabled_after_updating_address)
232 {
233 	uint32_t options = 0;
234 	struct bt_le_ext_adv adv = {0};
235 	uint8_t own_addr_type = BT_ADDR_LE_ANONYMOUS;
236 	uint8_t expected_args_history[] = {BT_HCI_LE_SCAN_DISABLE, BT_HCI_LE_SCAN_ENABLE};
237 
238 	Z_TEST_SKIP_IFDEF(CONFIG_BT_PRIVACY);
239 	Z_TEST_SKIP_IFDEF(CONFIG_BT_EXT_ADV);
240 	Z_TEST_SKIP_IFDEF(CONFIG_BT_SCAN_WITH_IDENTITY);
241 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_OBSERVER);
242 
243 	options &= ~BT_LE_ADV_OPT_CONN;
244 	options &= ~BT_LE_ADV_OPT_USE_IDENTITY;
245 
246 	/* Set device scanning active flag */
247 	atomic_set_bit(bt_dev.flags, BT_DEV_SCANNING);
248 
249 	bt_id_set_adv_own_addr(&adv, options, true, &own_addr_type);
250 	zassert_true(own_addr_type == BT_HCI_OWN_ADDR_RANDOM,
251 		     "Address type reference was incorrectly set");
252 
253 	expect_call_count_bt_le_scan_set_enable(2, expected_args_history);
254 }
255 
256 /*
257  *  Test setting the advertiser address while 'CONFIG_BT_SCAN_WITH_IDENTITY' is enabled
258  *  and scanning is ongoing. The scanner is using a random identity address.
259  *
260  *  Constraints:
261  *   - Options 'BT_LE_ADV_OPT_CONN' bit isn't set
262  *
263  *  Expected behaviour:
264  *   - Scanning is not disabled.
265  *   - The advertiser doesn't attempt to change the identity addr with bt_id_set_adv_private_addr()
266  *   - The advertiser uses the same identity address as the scanner.
267  */
ZTEST(bt_id_set_adv_own_addr,test_set_adv_own_addr_while_scanning_with_identity_random)268 ZTEST(bt_id_set_adv_own_addr, test_set_adv_own_addr_while_scanning_with_identity_random)
269 {
270 	uint32_t options = 0;
271 	struct bt_le_ext_adv adv = {0};
272 	struct net_buf net_buff;
273 	int err;
274 	uint8_t scan_own_addr_type = BT_ADDR_LE_ANONYMOUS;
275 	uint8_t adv_own_addr_type = BT_ADDR_LE_ANONYMOUS;
276 
277 	Z_TEST_SKIP_IFDEF(CONFIG_BT_PRIVACY);
278 	Z_TEST_SKIP_IFDEF(CONFIG_BT_EXT_ADV);
279 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_OBSERVER);
280 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_SCAN_WITH_IDENTITY);
281 
282 	bt_hci_cmd_alloc_fake.return_val = &net_buff;
283 	bt_hci_cmd_send_sync_fake.return_val = 0;
284 
285 	options &= ~BT_LE_ADV_OPT_CONN;
286 	bt_addr_le_copy(&bt_dev.id_addr[BT_ID_DEFAULT], BT_STATIC_RANDOM_LE_ADDR_1);
287 
288 	err = bt_id_set_scan_own_addr(false, &scan_own_addr_type);
289 
290 	expect_single_call_bt_hci_cmd_alloc();
291 	expect_single_call_bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_RANDOM_ADDRESS);
292 
293 	zassert_ok(err, "Unexpected error code '%d' was returned", err);
294 	zassert_true(scan_own_addr_type == BT_HCI_OWN_ADDR_RANDOM,
295 		     "Address type reference was incorrectly set");
296 
297 	atomic_set_bit(bt_dev.flags, BT_DEV_SCANNING);
298 
299 	bt_id_set_adv_own_addr(&adv, options, true, &adv_own_addr_type);
300 	zassert_true(adv_own_addr_type == BT_HCI_OWN_ADDR_RANDOM,
301 		     "Address type reference was incorrectly set");
302 
303 	expect_call_count_bt_le_scan_set_enable(0, NULL);
304 }
305 
306 /*
307  *  Test setting the advertiser address while 'CONFIG_BT_SCAN_WITH_IDENTITY' is enabled
308  *  and scanning is ongoing. The scanner is using a public identity address.
309  *
310  *  Constraints:
311  *   - Options 'BT_LE_ADV_OPT_CONN' bit isn't set
312  *
313  *  Expected behaviour:
314  *   - Scanning is not disabled.
315  *   - The advertiser doesn't attempt to change the identity addr with bt_id_set_adv_private_addr()
316  *   - The advertiser uses the same identity address as the scanner.
317  */
ZTEST(bt_id_set_adv_own_addr,test_set_adv_own_addr_while_scanning_with_identity_public)318 ZTEST(bt_id_set_adv_own_addr, test_set_adv_own_addr_while_scanning_with_identity_public)
319 {
320 	uint32_t options = 0;
321 	struct bt_le_ext_adv adv = {0};
322 	int err;
323 	uint8_t scan_own_addr_type = BT_ADDR_LE_ANONYMOUS;
324 	uint8_t adv_own_addr_type = BT_ADDR_LE_ANONYMOUS;
325 
326 	Z_TEST_SKIP_IFDEF(CONFIG_BT_PRIVACY);
327 	Z_TEST_SKIP_IFDEF(CONFIG_BT_EXT_ADV);
328 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_OBSERVER);
329 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_SCAN_WITH_IDENTITY);
330 
331 	options &= ~BT_LE_ADV_OPT_CONN;
332 
333 	bt_addr_le_copy(&bt_dev.id_addr[BT_ID_DEFAULT], BT_LE_ADDR);
334 
335 	err = bt_id_set_scan_own_addr(false, &scan_own_addr_type);
336 
337 	zassert_ok(err, "Unexpected error code '%d' was returned", err);
338 	zassert_true(scan_own_addr_type == BT_HCI_OWN_ADDR_PUBLIC,
339 		     "Address type reference was incorrectly set");
340 
341 	atomic_set_bit(bt_dev.flags, BT_DEV_SCANNING);
342 
343 	bt_id_set_adv_own_addr(&adv, options, true, &adv_own_addr_type);
344 	zassert_true(adv_own_addr_type == BT_HCI_OWN_ADDR_PUBLIC,
345 		     "Address type reference was incorrectly set");
346 
347 	expect_call_count_bt_le_scan_set_enable(0, NULL);
348 }
349