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/bluetooth.h>
9 #include <zephyr/bluetooth/addr.h>
10 #include <host/keys.h>
11 #include <zephyr/fff.h>
12 #include "mocks/keys_help_utils.h"
13 #include "testing_common_defs.h"
14
15 DEFINE_FFF_GLOBALS;
16
17 /* This LUT contains different combinations of ID and Address pairs */
18 const struct id_addr_pair testing_id_addr_pair_lut[] = {
19 { BT_ADDR_ID_1, BT_ADDR_LE_1 },
20 { BT_ADDR_ID_1, BT_ADDR_LE_2 },
21 { BT_ADDR_ID_2, BT_ADDR_LE_1 },
22 { BT_ADDR_ID_2, BT_ADDR_LE_2 }
23 };
24
25 /* This list will hold returned references while filling keys pool */
26 static struct bt_keys *returned_keys_refs[CONFIG_BT_MAX_PAIRED];
27
28 BUILD_ASSERT(ARRAY_SIZE(testing_id_addr_pair_lut) == CONFIG_BT_MAX_PAIRED);
29 BUILD_ASSERT(ARRAY_SIZE(testing_id_addr_pair_lut) == ARRAY_SIZE(returned_keys_refs));
30
type_not_set_ts_setup(void)31 static void *type_not_set_ts_setup(void)
32 {
33 clear_key_pool();
34 int rv = fill_key_pool_by_id_addr(testing_id_addr_pair_lut,
35 ARRAY_SIZE(testing_id_addr_pair_lut), returned_keys_refs);
36
37 zassert_true(rv == 0, "Failed to fill keys pool list, error code %d", -rv);
38
39 return NULL;
40 }
41
42 ZTEST_SUITE(bt_keys_foreach_bond_keys_type_not_set, NULL, type_not_set_ts_setup, NULL, NULL, NULL);
43
44 /* Callback to be used when no calls are expected by bt_foreach_bond() */
bt_foreach_bond_unreachable_cb(const struct bt_bond_info * info,void * user_data)45 static void bt_foreach_bond_unreachable_cb(const struct bt_bond_info *info, void *user_data)
46 {
47 zassert_unreachable("Unexpected call to '%s()' occurred", __func__);
48 }
49
50 /*
51 * Test calling bt_foreach_bond() with a valid ID that exists
52 * in the keys pool but the keys type isn't set
53 *
54 * Constraints:
55 * - Keys pool has been filled
56 * - Keys type isn't set
57 *
58 * Expected behaviour:
59 * - Callback should never be called
60 */
ZTEST(bt_keys_foreach_bond_keys_type_not_set,test_existing_id_type_is_not_set)61 ZTEST(bt_keys_foreach_bond_keys_type_not_set, test_existing_id_type_is_not_set)
62 {
63 struct id_addr_pair const *current_params_vector;
64 uint8_t id;
65
66 for (size_t i = 0; i < ARRAY_SIZE(testing_id_addr_pair_lut); i++) {
67
68 current_params_vector = &testing_id_addr_pair_lut[i];
69 id = current_params_vector->id;
70
71 bt_foreach_bond(id, bt_foreach_bond_unreachable_cb, NULL);
72 }
73 }
74
type_set_ts_setup(void)75 static void *type_set_ts_setup(void)
76 {
77 clear_key_pool();
78 int rv = fill_key_pool_by_id_addr(testing_id_addr_pair_lut,
79 ARRAY_SIZE(testing_id_addr_pair_lut), returned_keys_refs);
80
81 zassert_true(rv == 0, "Failed to fill keys pool list, error code %d", -rv);
82
83 for (uint32_t i = 0; i < ARRAY_SIZE(returned_keys_refs); i++) {
84 returned_keys_refs[i]->keys |= BT_KEYS_ALL;
85 }
86
87 return NULL;
88 }
89
90 ZTEST_SUITE(bt_keys_foreach_bond_keys_type_set, NULL, type_set_ts_setup, NULL, NULL, NULL);
91
92 /* Callback to be used when calls are expected by bt_foreach_bond() */
bt_foreach_bond_expected_cb(const struct bt_bond_info * info,void * user_data)93 static void bt_foreach_bond_expected_cb(const struct bt_bond_info *info, void *user_data)
94 {
95 uint32_t *call_counter = (uint32_t *)user_data;
96
97 zassert_true(info != NULL, "Unexpected NULL reference pointer for parameter '%s'", "info");
98 zassert_true(user_data != NULL, "Unexpected NULL reference pointer for parameter '%s'",
99 "user_data");
100
101 (*call_counter)++;
102 }
103
104 /*
105 * Test calling bt_foreach_bond() with a valid ID that exists
106 * in the keys pool while the keys type is set
107 *
108 * Constraints:
109 * - Keys pool has been filled
110 * - Keys type is set
111 *
112 * Expected behaviour:
113 * - Callback should be called for each occurrence
114 */
ZTEST(bt_keys_foreach_bond_keys_type_set,test_existing_id_type_is_set)115 ZTEST(bt_keys_foreach_bond_keys_type_set, test_existing_id_type_is_set)
116 {
117 uint32_t call_counter = 0;
118 struct id_addr_pair const *current_params_vector;
119 uint8_t id;
120
121 for (size_t i = 0; i < ARRAY_SIZE(testing_id_addr_pair_lut); i++) {
122
123 current_params_vector = &testing_id_addr_pair_lut[i];
124 id = current_params_vector->id;
125
126 /* Each ID was registered in the list with 2 different addresses */
127 bt_foreach_bond(id, bt_foreach_bond_expected_cb, (void *)&call_counter);
128 zassert_true(call_counter == 2,
129 "Incorrect call counter for 'bt_foreach_bond_expected_cb()'");
130
131 call_counter = 0;
132 }
133 }
134