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/settings_store_expects.h"
9 #include "testing_common_defs.h"
10
11 #include <zephyr/bluetooth/addr.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 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 struct bt_keys *returned_keys_refs[CONFIG_BT_MAX_PAIRED];
36
37 /* Holds the last key reference updated */
38 static struct bt_keys *last_keys_updated;
39
40 BUILD_ASSERT(ARRAY_SIZE(testing_id_addr_pair_lut) == CONFIG_BT_MAX_PAIRED);
41 BUILD_ASSERT(ARRAY_SIZE(testing_id_addr_pair_lut) == ARRAY_SIZE(returned_keys_refs));
42
tc_setup(void * f)43 static void tc_setup(void *f)
44 {
45 clear_key_pool();
46 int rv = fill_key_pool_by_id_addr(testing_id_addr_pair_lut,
47 ARRAY_SIZE(testing_id_addr_pair_lut), returned_keys_refs);
48
49 zassert_true(rv == 0, "Failed to fill keys pool list, error code %d", -rv);
50
51 last_keys_updated = returned_keys_refs[CONFIG_BT_MAX_PAIRED - 1];
52 }
53
54 ZTEST_SUITE(bt_keys_update_usage_overwrite_oldest_enabled, NULL, NULL, tc_setup, NULL, NULL);
55
56 /*
57 * Request updating non-existing item in the keys pool list
58 *
59 * Constraints:
60 * - Keys pool list is filled with items that are different from the testing ID and address pair
61 * used
62 *
63 * Expected behaviour:
64 * - Last updated key reference isn't changed
65 */
ZTEST(bt_keys_update_usage_overwrite_oldest_enabled,test_update_non_existing_key)66 ZTEST(bt_keys_update_usage_overwrite_oldest_enabled, test_update_non_existing_key)
67 {
68 uint8_t id = BT_ADDR_ID_5;
69 bt_addr_le_t *addr = BT_ADDR_LE_5;
70
71 bt_keys_update_usage(id, addr);
72
73 zassert_equal_ptr(bt_keys_get_last_keys_updated(), last_keys_updated,
74 "bt_keys_update_usage() changed last updated key reference unexpectedly");
75 }
76
77 /*
78 * Request updating the latest key reference
79 *
80 * Constraints:
81 * - Keys pool list is filled with items
82 * - ID and address pair used are the last added pair to the list
83 *
84 * Expected behaviour:
85 * - Last updated key reference isn't changed
86 */
ZTEST(bt_keys_update_usage_overwrite_oldest_enabled,test_update_latest_reference)87 ZTEST(bt_keys_update_usage_overwrite_oldest_enabled, test_update_latest_reference)
88 {
89 uint8_t id = testing_id_addr_pair_lut[CONFIG_BT_MAX_PAIRED - 1].id;
90 bt_addr_le_t *addr = testing_id_addr_pair_lut[CONFIG_BT_MAX_PAIRED - 1].addr;
91
92 bt_keys_update_usage(id, addr);
93
94 zassert_equal_ptr(bt_keys_get_last_keys_updated(), last_keys_updated,
95 "bt_keys_update_usage() changed last updated key reference unexpectedly");
96 }
97
98 /*
99 * Request updating existing items aging counter
100 *
101 * Constraints:
102 * - Keys pool list is filled with items
103 * - ID and address used exist in the keys pool list
104 * - CONFIG_BT_KEYS_SAVE_AGING_COUNTER_ON_PAIRING isn't enabled
105 *
106 * Expected behaviour:
107 * - Last updated key reference matches the last updated key reference
108 */
ZTEST(bt_keys_update_usage_overwrite_oldest_enabled,test_update_non_latest_reference)109 ZTEST(bt_keys_update_usage_overwrite_oldest_enabled, test_update_non_latest_reference)
110 {
111 uint8_t id;
112 uint32_t old_aging_counter;
113 bt_addr_le_t *addr;
114 struct bt_keys *expected_updated_keys;
115 struct id_addr_pair const *params_vector;
116
117 if (IS_ENABLED(CONFIG_BT_KEYS_SAVE_AGING_COUNTER_ON_PAIRING)) {
118 ztest_test_skip();
119 }
120
121 for (size_t it = 0; it < ARRAY_SIZE(testing_id_addr_pair_lut); it++) {
122 params_vector = &testing_id_addr_pair_lut[it];
123 id = params_vector->id;
124 addr = params_vector->addr;
125 expected_updated_keys = returned_keys_refs[it];
126 old_aging_counter = expected_updated_keys->aging_counter;
127
128 bt_keys_update_usage(id, addr);
129
130 zassert_true(expected_updated_keys->aging_counter > (old_aging_counter),
131 "bt_keys_update_usage() set incorrect aging counter");
132
133 zassert_equal_ptr(
134 bt_keys_get_last_keys_updated(), expected_updated_keys,
135 "bt_keys_update_usage() changed last updated key reference unexpectedly");
136
137 expect_not_called_bt_settings_store_keys();
138 }
139 }
140