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/hci_core.h"
9 #include "mocks/rpa.h"
10 #include "mocks/adv.h"
11 #include "mocks/adv_expects.h"
12 #include "testing_common_defs.h"
13 
14 #include <zephyr/bluetooth/hci.h>
15 #include <zephyr/bluetooth/hci_types.h>
16 #include <zephyr/fff.h>
17 #include <zephyr/kernel.h>
18 
19 #include <host/hci_core.h>
20 #include <host/id.h>
21 
22 DEFINE_FFF_GLOBALS;
23 
fff_reset_rule_before(const struct ztest_unit_test * test,void * fixture)24 static void fff_reset_rule_before(const struct ztest_unit_test *test, void *fixture)
25 {
26 	memset(&bt_dev, 0x00, sizeof(struct bt_dev));
27 
28 	RPA_FFF_FAKES_LIST(RESET_FAKE);
29 	CRYPTO_FFF_FAKES_LIST(RESET_FAKE);
30 	HCI_CORE_FFF_FAKES_LIST(RESET_FAKE);
31 }
32 
33 ZTEST_RULE(fff_reset_rule, fff_reset_rule_before, NULL);
34 
35 ZTEST_SUITE(bt_id_set_scan_own_addr, NULL, NULL, NULL, NULL, NULL);
36 
bt_rand_custom_fake(void * buf,size_t len)37 static int bt_rand_custom_fake(void *buf, size_t len)
38 {
39 	__ASSERT_NO_MSG(buf != NULL);
40 	__ASSERT_NO_MSG(len == sizeof(BT_ADDR->val));
41 
42 	/* This will make set_random_address() succeeds and returns 0 */
43 	memcpy(buf, &BT_ADDR->val, len);
44 	bt_addr_copy(&bt_dev.random_addr.a, BT_ADDR);
45 
46 	return 0;
47 }
48 
49 /*
50  *  Test setting scan own address while 'CONFIG_BT_PRIVACY' isn't enabled.
51  *  bt_id_set_private_addr() is called to generate a NRPA and passed to set_random_address().
52  *  Address type reference is updated upon success.
53  *
54  *  Constraints:
55  *   - bt_id_set_private_addr() succeeds and returns 0
56  *   - 'CONFIG_BT_SCAN_WITH_IDENTITY' isn't enabled
57  *   - 'CONFIG_BT_PRIVACY' isn't enabled
58  *
59  *  Expected behaviour:
60  *   - bt_id_set_scan_own_addr() returns 0
61  *   - Address type reference is updated
62  */
ZTEST(bt_id_set_scan_own_addr,test_set_nrpa_scan_address_no_privacy)63 ZTEST(bt_id_set_scan_own_addr, test_set_nrpa_scan_address_no_privacy)
64 {
65 	int err;
66 	uint8_t own_addr_type = BT_ADDR_LE_ANONYMOUS;
67 
68 	Z_TEST_SKIP_IFDEF(CONFIG_BT_PRIVACY);
69 	Z_TEST_SKIP_IFDEF(CONFIG_BT_SCAN_WITH_IDENTITY);
70 
71 	bt_rand_fake.custom_fake = bt_rand_custom_fake;
72 
73 	err = bt_id_set_scan_own_addr(false, &own_addr_type);
74 
75 	zassert_ok(err, "Unexpected error code '%d' was returned", err);
76 	zassert_true(own_addr_type == BT_HCI_OWN_ADDR_RANDOM,
77 		     "Address type reference was incorrectly set");
78 }
79 
80 /*
81  *  Test setting scan own address while 'CONFIG_BT_PRIVACY' isn't enabled.
82  *  Advertising is ongoing and uses a random device address.
83  *
84  *  Constraints:
85  *   - bt_id_set_private_addr() succeeds and returns 0
86  *   - 'CONFIG_BT_SCAN_WITH_IDENTITY' isn't enabled
87  *   - 'CONFIG_BT_PRIVACY' isn't enabled
88  *
89  *  Expected behaviour:
90  *   - bt_id_set_scan_own_addr() returns 0
91  *   - Address type reference is updated
92  */
ZTEST(bt_id_set_scan_own_addr,test_set_nrpa_scan_address_no_privacy_adv_ongoing_random_identity)93 ZTEST(bt_id_set_scan_own_addr, test_set_nrpa_scan_address_no_privacy_adv_ongoing_random_identity)
94 {
95 	int err;
96 	struct bt_le_ext_adv *adv = &bt_dev.adv;
97 	uint8_t own_addr_type = BT_ADDR_LE_ANONYMOUS;
98 
99 	Z_TEST_SKIP_IFDEF(CONFIG_BT_PRIVACY);
100 	Z_TEST_SKIP_IFDEF(CONFIG_BT_SCAN_WITH_IDENTITY);
101 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_BROADCASTER);
102 
103 	bt_rand_fake.custom_fake = bt_rand_custom_fake;
104 	bt_le_adv_lookup_legacy_fake.return_val = adv;
105 
106 	bt_addr_le_copy(&bt_dev.id_addr[BT_ID_DEFAULT], BT_STATIC_RANDOM_LE_ADDR_1);
107 
108 	atomic_set_bit(adv->flags, BT_ADV_ENABLED);
109 
110 	err = bt_id_set_scan_own_addr(false, &own_addr_type);
111 
112 	zassert_ok(err, "Unexpected error code '%d' was returned", err);
113 	zassert_true(own_addr_type == BT_HCI_OWN_ADDR_RANDOM,
114 		     "Address type reference was incorrectly set");
115 }
116 
117 /*
118  *  Test setting scan own address while 'CONFIG_BT_PRIVACY' isn't enabled.
119  *  Advertising is ongoing and uses a public device address.
120  *
121  *  Constraints:
122  *   - bt_id_set_private_addr() succeeds and returns 0
123  *   - 'CONFIG_BT_SCAN_WITH_IDENTITY' isn't enabled
124  *   - 'CONFIG_BT_PRIVACY' isn't enabled
125  *
126  *  Expected behaviour:
127  *   - bt_id_set_scan_own_addr() returns 0
128  *   - Address type reference is updated
129  */
ZTEST(bt_id_set_scan_own_addr,test_set_nrpa_scan_address_no_privacy_adv_ongoing_public_identity)130 ZTEST(bt_id_set_scan_own_addr, test_set_nrpa_scan_address_no_privacy_adv_ongoing_public_identity)
131 {
132 	int err;
133 	struct bt_le_ext_adv *adv = &bt_dev.adv;
134 	uint8_t own_addr_type = BT_ADDR_LE_ANONYMOUS;
135 
136 	Z_TEST_SKIP_IFDEF(CONFIG_BT_PRIVACY);
137 	Z_TEST_SKIP_IFDEF(CONFIG_BT_SCAN_WITH_IDENTITY);
138 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_BROADCASTER);
139 
140 	bt_rand_fake.custom_fake = bt_rand_custom_fake;
141 	bt_le_adv_lookup_legacy_fake.return_val = adv;
142 
143 	bt_addr_le_copy(&bt_dev.id_addr[BT_ID_DEFAULT], BT_LE_ADDR);
144 
145 	atomic_set_bit(adv->flags, BT_ADV_ENABLED);
146 	atomic_set_bit(adv->flags, BT_ADV_USE_IDENTITY);
147 
148 	err = bt_id_set_scan_own_addr(false, &own_addr_type);
149 
150 	zassert_ok(err, "Unexpected error code '%d' was returned", err);
151 	zassert_true(own_addr_type == BT_HCI_OWN_ADDR_PUBLIC,
152 		     "Address type reference was incorrectly set");
153 }
154 
155 /*
156  *  Test setting scan own address while 'CONFIG_BT_PRIVACY' isn't enabled.
157  *  If 'CONFIG_BT_SCAN_WITH_IDENTITY' is enabled and the default identity has an RPA address of type
158  * 'BT_HCI_OWN_ADDR_RANDOM', set_random_address() is called and address type reference is updated
159  * upon success.
160  *
161  *  Constraints:
162  *   - Default identity has an address with the type 'BT_HCI_OWN_ADDR_RANDOM'
163  *   - 'CONFIG_BT_PRIVACY' isn't enabled
164  *   - 'CONFIG_BT_SCAN_WITH_IDENTITY' is enabled
165  *   - set_random_address() succeeds and returns 0
166  *
167  *  Expected behaviour:
168  *   - bt_id_set_scan_own_addr() returns 0
169  *   - Address type reference is updated
170  */
ZTEST(bt_id_set_scan_own_addr,test_setting_scan_own_rpa_address_no_privacy)171 ZTEST(bt_id_set_scan_own_addr, test_setting_scan_own_rpa_address_no_privacy)
172 {
173 	int err;
174 	uint8_t own_addr_type = BT_ADDR_LE_ANONYMOUS;
175 
176 	Z_TEST_SKIP_IFDEF(CONFIG_BT_PRIVACY);
177 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_SCAN_WITH_IDENTITY);
178 
179 	bt_addr_le_copy(&bt_dev.id_addr[BT_ID_DEFAULT], BT_RPA_LE_ADDR);
180 
181 	/* This will make set_random_address() succeeds and returns 0 */
182 	bt_addr_copy(&bt_dev.random_addr.a, &BT_RPA_LE_ADDR->a);
183 
184 	err = bt_id_set_scan_own_addr(false, &own_addr_type);
185 
186 	zassert_ok(err, "Unexpected error code '%d' was returned", err);
187 	zassert_true(own_addr_type == BT_HCI_OWN_ADDR_RANDOM,
188 		     "Address type reference was incorrectly set");
189 }
190 
191 /*
192  *  Test setting scan own address while 'CONFIG_BT_PRIVACY' is enabled and privacy features
193  *  'BT_LE_FEAT_BIT_PRIVACY' bit isn't set.
194  *  bt_id_set_private_addr() is called with 'BT_ID_DEFAULT' as the ID and address type reference is
195  *  updated upon success.
196  *
197  *  Constraints:
198  *   - 'CONFIG_BT_PRIVACY' is enabled
199  *   - 'BT_LE_FEAT_BIT_PRIVACY' bit isn't set.
200  *   - bt_id_set_private_addr() succeeds and returns 0
201  *
202  *  Expected behaviour:
203  *   - bt_id_set_scan_own_addr() returns 0
204  *   - Address type reference is updated with the value 'BT_HCI_OWN_ADDR_RANDOM'
205  */
ZTEST(bt_id_set_scan_own_addr,test_setting_scan_own_address_privacy_enabled)206 ZTEST(bt_id_set_scan_own_addr, test_setting_scan_own_address_privacy_enabled)
207 {
208 	int err;
209 	uint8_t own_addr_type = BT_ADDR_LE_ANONYMOUS;
210 
211 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_PRIVACY);
212 
213 	/* This will cause bt_id_set_private_addr() to return 0 (success) */
214 	atomic_set_bit(bt_dev.flags, BT_DEV_RPA_VALID);
215 
216 	err = bt_id_set_scan_own_addr(true, &own_addr_type);
217 
218 	zassert_ok(err, "Unexpected error code '%d' was returned", err);
219 	zassert_true(own_addr_type == BT_HCI_OWN_ADDR_RANDOM,
220 		     "Address type reference was incorrectly set");
221 }
222 
223 /*
224  *  Test setting scan own address while 'CONFIG_BT_PRIVACY' is enabled and privacy features
225  *  'BT_LE_FEAT_BIT_PRIVACY' bit is set.
226  *  bt_id_set_private_addr() is called with 'BT_ID_DEFAULT' as the ID and address type reference
227  *  is updated upon success.
228  *
229  *  Constraints:
230  *   - 'CONFIG_BT_PRIVACY' is enabled
231  *   - 'BT_LE_FEAT_BIT_PRIVACY' bit is set.
232  *   - bt_id_set_private_addr() succeeds and returns 0
233  *
234  *  Expected behaviour:
235  *   - bt_id_set_scan_own_addr() returns 0
236  *   - Address type reference is updated with the value 'BT_HCI_OWN_ADDR_RPA_OR_RANDOM'
237  */
ZTEST(bt_id_set_scan_own_addr,test_setting_scan_own_address_privacy_features_set)238 ZTEST(bt_id_set_scan_own_addr, test_setting_scan_own_address_privacy_features_set)
239 {
240 	int err;
241 	uint8_t own_addr_type = BT_ADDR_LE_ANONYMOUS;
242 
243 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_PRIVACY);
244 
245 	/* This will cause bt_id_set_private_addr() to return 0 (success) */
246 	atomic_set_bit(bt_dev.flags, BT_DEV_RPA_VALID);
247 
248 	bt_dev.le.features[(BT_LE_FEAT_BIT_PRIVACY) >> 3] |= BIT((BT_LE_FEAT_BIT_PRIVACY)&7);
249 
250 	err = bt_id_set_scan_own_addr(true, &own_addr_type);
251 
252 	zassert_ok(err, "Unexpected error code '%d' was returned", err);
253 	zassert_true(own_addr_type == BT_HCI_OWN_ADDR_RPA_OR_RANDOM,
254 		     "Address type reference was incorrectly set");
255 }
256