1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "mocks/keys.h"
8 #include "mocks/keys_expects.h"
9 #include "testing_common_defs.h"
10 
11 #include <zephyr/bluetooth/hci.h>
12 #include <zephyr/fff.h>
13 #include <zephyr/kernel.h>
14 
15 #include <host/hci_core.h>
16 #include <host/id.h>
17 
18 DEFINE_FFF_GLOBALS;
19 
20 /* Hold data representing HCI command response for command BT_HCI_OP_READ_BD_ADDR */
21 static struct bt_keys resolving_key;
22 
fff_reset_rule_before(const struct ztest_unit_test * test,void * fixture)23 static void fff_reset_rule_before(const struct ztest_unit_test *test, void *fixture)
24 {
25 	memset(&bt_dev, 0x00, sizeof(struct bt_dev));
26 	memset(&resolving_key, 0x00, sizeof(struct bt_keys));
27 
28 	KEYS_FFF_FAKES_LIST(RESET_FAKE);
29 }
30 
31 ZTEST_RULE(fff_reset_rule, fff_reset_rule_before, NULL);
32 
33 ZTEST_SUITE(bt_lookup_id_addr, NULL, NULL, NULL, NULL, NULL);
34 
35 /*
36  *  Test returning the same address pointer passed when 'CONFIG_BT_SMP' isn't enabled
37  *
38  *  Constraints:
39  *   - A valid ID (< CONFIG_BT_ID_MAX) is used
40  *   - A valid address reference is used
41  *   - 'CONFIG_BT_SMP' isn't enabled
42  *
43  *  Expected behaviour:
44  *   - bt_lookup_id_addr() returns the same address pointer used as an argument
45  */
ZTEST(bt_lookup_id_addr,test_config_bt_smp_not_enabled)46 ZTEST(bt_lookup_id_addr, test_config_bt_smp_not_enabled)
47 {
48 	uint8_t id = 0x00;
49 	const bt_addr_le_t *addr = BT_RPA_LE_ADDR;
50 	const bt_addr_le_t *returned_addr;
51 
52 	Z_TEST_SKIP_IFDEF(CONFIG_BT_SMP);
53 
54 	returned_addr = bt_lookup_id_addr(id, addr);
55 
56 	expect_not_called_bt_keys_find_irk();
57 
58 	zassert_true(returned_addr == addr, "Incorrect address was returned");
59 }
60 
61 /*
62  *  Test returning the same address pointer passed when 'CONFIG_BT_SMP' is enabled, but the
63  *  address couldn't be resolved by bt_keys_find_irk().
64  *
65  *  Constraints:
66  *   - A valid ID (< CONFIG_BT_ID_MAX) is used
67  *   - A valid address reference is used
68  *   - bt_keys_find_irk() returns NULL (which represents that address couldn't be resolved)
69  *   - 'CONFIG_BT_SMP' is enabled
70  *
71  *  Expected behaviour:
72  *   - bt_lookup_id_addr() returns the same address pointer used as an argument
73  */
ZTEST(bt_lookup_id_addr,test_config_bt_smp_enabled_address_resolving_fails)74 ZTEST(bt_lookup_id_addr, test_config_bt_smp_enabled_address_resolving_fails)
75 {
76 	uint8_t id = 0x00;
77 	const bt_addr_le_t *addr = BT_RPA_LE_ADDR;
78 	const bt_addr_le_t *returned_addr;
79 
80 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_SMP);
81 
82 	bt_keys_find_irk_fake.return_val = NULL;
83 
84 	returned_addr = bt_lookup_id_addr(id, addr);
85 
86 	expect_single_call_bt_keys_find_irk(id, addr);
87 
88 	zassert_true(returned_addr == addr, "Incorrect address was returned");
89 }
90 
bt_keys_find_irk_custom_fake(uint8_t id,const bt_addr_le_t * addr)91 static struct bt_keys *bt_keys_find_irk_custom_fake(uint8_t id, const bt_addr_le_t *addr)
92 {
93 	return &resolving_key;
94 }
95 
96 /*
97  *  Test returning a pointer to the resolved address for the address passed.
98  *  'CONFIG_BT_SMP' is enabled and address is resolved by bt_keys_find_irk().
99  *
100  *  Constraints:
101  *   - A valid ID (< CONFIG_BT_ID_MAX) is used
102  *   - A valid address reference is used
103  *   - bt_keys_find_irk() returns a valid key reference
104  *   - 'CONFIG_BT_SMP' is enabled
105  *
106  *  Expected behaviour:
107  *   - bt_lookup_id_addr() returns the resolved address instead of the input address
108  */
ZTEST(bt_lookup_id_addr,test_config_bt_smp_enabled_address_resolving_succeeds)109 ZTEST(bt_lookup_id_addr, test_config_bt_smp_enabled_address_resolving_succeeds)
110 {
111 	uint8_t id = 0x00;
112 	const bt_addr_le_t *addr = BT_RPA_LE_ADDR;
113 	const bt_addr_le_t *returned_addr;
114 
115 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_SMP);
116 
117 	bt_keys_find_irk_fake.custom_fake = bt_keys_find_irk_custom_fake;
118 
119 	returned_addr = bt_lookup_id_addr(id, addr);
120 
121 	expect_single_call_bt_keys_find_irk(id, addr);
122 
123 	zassert_true(returned_addr == &resolving_key.addr, "Incorrect address was returned");
124 }
125