1 /*
2 * Copyright (c) 2022 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/bluetooth/addr.h>
9 #include <host/keys.h>
10 #include "mocks/conn.h"
11 #include "mocks/hci_core.h"
12 #include "mocks/keys_help_utils.h"
13 #include "mocks/hci_core_expects.h"
14 #include "host_mocks/assert.h"
15 #include "testing_common_defs.h"
16
17 /* This LUT contains different combinations of ID and Address pairs */
18 extern const struct id_addr_pair testing_id_addr_pair_lut[CONFIG_BT_MAX_PAIRED];
19
20 /* This list holds returned references while filling keys pool */
21 extern struct bt_keys *returned_keys_refs[CONFIG_BT_MAX_PAIRED];
22
bt_unpair_unreachable_custom_fake(uint8_t id,const bt_addr_le_t * addr)23 static int bt_unpair_unreachable_custom_fake(uint8_t id, const bt_addr_le_t *addr)
24 {
25 ARG_UNUSED(id);
26 ARG_UNUSED(addr);
27 zassert_unreachable("Unexpected call to 'bt_unpair()' occurred");
28 return 0;
29 }
30
bt_conn_foreach_conn_ref_null_custom_fake(enum bt_conn_type type,bt_conn_foreach_cb func,void * data)31 static void bt_conn_foreach_conn_ref_null_custom_fake(enum bt_conn_type type,
32 bt_conn_foreach_cb func, void *data)
33 {
34 func(NULL, data);
35 }
36
bt_conn_foreach_data_ref_null_custom_fake(enum bt_conn_type type,bt_conn_foreach_cb func,void * data)37 static void bt_conn_foreach_data_ref_null_custom_fake(enum bt_conn_type type,
38 bt_conn_foreach_cb func, void *data)
39 {
40 struct bt_conn conn;
41
42 func(&conn, NULL);
43 }
44
45 /* Setup test variables */
test_case_setup(void * f)46 static void test_case_setup(void *f)
47 {
48 clear_key_pool();
49
50 int rv = fill_key_pool_by_id_addr(testing_id_addr_pair_lut,
51 ARRAY_SIZE(testing_id_addr_pair_lut), returned_keys_refs);
52
53 zassert_true(rv == 0, "Failed to fill keys pool list, error code %d", -rv);
54 }
55
56 ZTEST_SUITE(bt_keys_find_key_in_use_invalid_cases, NULL, NULL, test_case_setup, NULL, NULL);
57
58 /*
59 * Test adding extra (ID, Address) pair while the keys pool list is full, but while looking
60 * for the keys in use, find_key_in_use() receives an invalid NULL connection reference.
61 *
62 * Constraints:
63 * - Keys pool list is full
64 * - CONFIG_BT_KEYS_OVERWRITE_OLDEST is enabled
65 *
66 * Expected behaviour:
67 * - Internal function find_key_in_use() receives a NULL connection reference
68 * - An assertion fails at find_key_in_use() and execution stops
69 */
ZTEST(bt_keys_find_key_in_use_invalid_cases,test_find_key_in_use_receives_null_conn_ref)70 ZTEST(bt_keys_find_key_in_use_invalid_cases, test_find_key_in_use_receives_null_conn_ref)
71 {
72 uint8_t id = BT_ADDR_ID_5;
73 bt_addr_le_t *addr = BT_ADDR_LE_5;
74
75 Z_TEST_SKIP_IFNDEF(CONFIG_BT_KEYS_OVERWRITE_OLDEST);
76
77 bt_unpair_fake.custom_fake = bt_unpair_unreachable_custom_fake;
78 bt_conn_foreach_fake.custom_fake = bt_conn_foreach_conn_ref_null_custom_fake;
79
80 expect_assert();
81 bt_keys_get_addr(id, addr);
82
83 /* Should not reach this point */
84 zassert_unreachable(NULL);
85 }
86
87 /*
88 * Test adding extra (ID, Address) pair while the keys pool list is full, but while looking
89 * for the keys in use, find_key_in_use() receives an invalid NULL data reference.
90 *
91 * Constraints:
92 * - Keys pool list is full
93 * - CONFIG_BT_KEYS_OVERWRITE_OLDEST is enabled
94 *
95 * Expected behaviour:
96 * - Internal function find_key_in_use() receives a NULL data reference
97 * - An assertion fails at find_key_in_use() and execution stops
98 */
ZTEST(bt_keys_find_key_in_use_invalid_cases,test_find_key_in_use_receives_null_data_ref)99 ZTEST(bt_keys_find_key_in_use_invalid_cases, test_find_key_in_use_receives_null_data_ref)
100 {
101 uint8_t id = BT_ADDR_ID_5;
102 bt_addr_le_t *addr = BT_ADDR_LE_5;
103
104 Z_TEST_SKIP_IFNDEF(CONFIG_BT_KEYS_OVERWRITE_OLDEST);
105
106 bt_unpair_fake.custom_fake = bt_unpair_unreachable_custom_fake;
107 bt_conn_foreach_fake.custom_fake = bt_conn_foreach_data_ref_null_custom_fake;
108
109 expect_assert();
110 bt_keys_get_addr(id, addr);
111
112 /* Should not reach this point */
113 zassert_unreachable(NULL);
114 }
115
116 ZTEST_SUITE(bt_keys_get_addr_null_reference, NULL, NULL, NULL, NULL, NULL);
117
118 /*
119 * Test invalid (NULL) BT address reference
120 *
121 * Constraints:
122 * - Address value is NULL
123 *
124 * Expected behaviour:
125 * - An assertion fails and execution stops
126 */
ZTEST(bt_keys_get_addr_null_reference,test_null_address_reference)127 ZTEST(bt_keys_get_addr_null_reference, test_null_address_reference)
128 {
129 expect_assert();
130 bt_keys_get_addr(0x00, NULL);
131 }
132