1 /*
2 * Copyright (c) 2022 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "mocks/keys_help_utils.h"
8 #include "mocks/rpa.h"
9 #include "testing_common_defs.h"
10
11 #include <zephyr/bluetooth/bluetooth.h>
12 #include <zephyr/fff.h>
13 #include <zephyr/kernel.h>
14
15 #include <host/keys.h>
16
17 DEFINE_FFF_GLOBALS;
18
19 /* This LUT contains different combinations of ID, Address and key type.
20 * Item in this list will be used to fill keys pool.
21 */
22 static const struct id_addr_pair testing_id_addr_pair_lut[] = {
23
24 {BT_ADDR_ID_1, BT_ADDR_LE_1}, {BT_ADDR_ID_1, BT_RPA_ADDR_LE_1},
25 {BT_ADDR_ID_1, BT_RPA_ADDR_LE_2}, {BT_ADDR_ID_1, BT_ADDR_LE_3},
26
27 {BT_ADDR_ID_2, BT_ADDR_LE_1}, {BT_ADDR_ID_2, BT_RPA_ADDR_LE_2},
28 {BT_ADDR_ID_2, BT_RPA_ADDR_LE_3}, {BT_ADDR_ID_2, BT_ADDR_LE_2},
29
30 {BT_ADDR_ID_3, BT_ADDR_LE_1}, {BT_ADDR_ID_3, BT_ADDR_LE_2},
31
32 {BT_ADDR_ID_4, BT_ADDR_LE_1}};
33
34 /* This list will hold returned references while filling keys pool */
35 static struct bt_keys *returned_keys_refs[CONFIG_BT_MAX_PAIRED];
36
37 BUILD_ASSERT(ARRAY_SIZE(testing_id_addr_pair_lut) == CONFIG_BT_MAX_PAIRED);
38 BUILD_ASSERT(ARRAY_SIZE(testing_id_addr_pair_lut) == ARRAY_SIZE(returned_keys_refs));
39
empty_list_ts_setup(void)40 static void *empty_list_ts_setup(void)
41 {
42 clear_key_pool();
43
44 return NULL;
45 }
46
47 ZTEST_SUITE(bt_keys_find_addr_initially_empty_list, NULL, empty_list_ts_setup, NULL, NULL, NULL);
48
49 /*
50 * Find a non-existing key reference for ID and Address pair
51 *
52 * Constraints:
53 * - Empty keys pool list
54 *
55 * Expected behaviour:
56 * - A NULL value is returned
57 */
ZTEST(bt_keys_find_addr_initially_empty_list,test_find_non_existing_key)58 ZTEST(bt_keys_find_addr_initially_empty_list, test_find_non_existing_key)
59 {
60 for (uint32_t i = 0; i < ARRAY_SIZE(testing_id_addr_pair_lut); i++) {
61 struct bt_keys *returned_ref;
62 struct id_addr_pair const *params_vector = &testing_id_addr_pair_lut[i];
63 uint8_t id = params_vector->id;
64 const bt_addr_le_t *addr = params_vector->addr;
65
66 returned_ref = bt_keys_find_addr(id, addr);
67
68 zassert_true(returned_ref == NULL, "bt_keys_find() returned a non-NULL reference");
69 }
70 }
71
filled_list_ts_setup(void)72 static void *filled_list_ts_setup(void)
73 {
74 clear_key_pool();
75 int rv = fill_key_pool_by_id_addr(testing_id_addr_pair_lut,
76 ARRAY_SIZE(testing_id_addr_pair_lut), returned_keys_refs);
77
78 zassert_true(rv == 0, "Failed to fill keys pool list, error code %d", -rv);
79
80 return NULL;
81 }
82
83 ZTEST_SUITE(bt_keys_find_addr_initially_filled_list, NULL, filled_list_ts_setup, NULL, NULL, NULL);
84
85 /*
86 * Find an existing key reference by ID and Address
87 *
88 * Constraints:
89 * - ID and address pair does exist in keys pool
90 *
91 * Expected behaviour:
92 * - A valid reference value is returned
93 */
ZTEST(bt_keys_find_addr_initially_filled_list,test_find_existing_key_by_id_and_address)94 ZTEST(bt_keys_find_addr_initially_filled_list, test_find_existing_key_by_id_and_address)
95 {
96 for (uint32_t i = 0; i < ARRAY_SIZE(testing_id_addr_pair_lut); i++) {
97 struct bt_keys *returned_ref, *expected_key_ref;
98 struct id_addr_pair const *params_vector = &testing_id_addr_pair_lut[i];
99 uint8_t id = params_vector->id;
100 const bt_addr_le_t *addr = params_vector->addr;
101
102 expected_key_ref = returned_keys_refs[i];
103
104 returned_ref = bt_keys_find_addr(id, addr);
105
106 zassert_true(returned_ref != NULL, "bt_keys_find_addr() returned a NULL reference");
107 zassert_equal_ptr(returned_ref, expected_key_ref,
108 "bt_keys_find_addr() returned unexpected reference");
109 }
110 }
111