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