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.h"
9 #include "mocks/settings_store_expects.h"
10 #include "testing_common_defs.h"
11
12 #include <zephyr/bluetooth/addr.h>
13 #include <zephyr/kernel.h>
14
15 #include <host/keys.h>
16
17 /* This LUT contains different combinations of ID and Address pairs.
18 * It is defined in main.c.
19 */
20 extern const struct id_addr_pair testing_id_addr_pair_lut[CONFIG_BT_MAX_PAIRED];
21
22 /* This list holds returned references while filling keys pool.
23 * It is defined in main.c.
24 */
25 extern struct bt_keys *returned_keys_refs[CONFIG_BT_MAX_PAIRED];
26
tc_setup(void * f)27 static void tc_setup(void *f)
28 {
29 Z_TEST_SKIP_IFNDEF(CONFIG_BT_KEYS_SAVE_AGING_COUNTER_ON_PAIRING);
30
31 clear_key_pool();
32 int rv = fill_key_pool_by_id_addr(testing_id_addr_pair_lut,
33 ARRAY_SIZE(testing_id_addr_pair_lut), returned_keys_refs);
34
35 zassert_true(rv == 0, "Failed to fill keys pool list, error code %d", -rv);
36 }
37
38 ZTEST_SUITE(bt_keys_update_usage_save_aging_counter, NULL, NULL, tc_setup, NULL, NULL);
39
40 /*
41 * Request updating existing items aging counter
42 *
43 * Constraints:
44 * - Keys pool list is filled with items
45 * - ID and address used exist in the keys pool list
46 * - CONFIG_BT_KEYS_SAVE_AGING_COUNTER_ON_PAIRING is enabled
47 *
48 * Expected behaviour:
49 * - Last updated key reference matches the last updated key reference
50 * - bt_keys_store() is called once with the correct parameters
51 */
ZTEST(bt_keys_update_usage_save_aging_counter,test_update_usage_and_save_aging_counter)52 ZTEST(bt_keys_update_usage_save_aging_counter, test_update_usage_and_save_aging_counter)
53 {
54 uint8_t id;
55 uint32_t old_aging_counter;
56 bt_addr_le_t *addr;
57 struct bt_keys *expected_updated_keys;
58 struct id_addr_pair const *params_vector;
59
60 for (size_t it = 0; it < ARRAY_SIZE(testing_id_addr_pair_lut); it++) {
61 params_vector = &testing_id_addr_pair_lut[it];
62 id = params_vector->id;
63 addr = params_vector->addr;
64 expected_updated_keys = returned_keys_refs[it];
65 old_aging_counter = expected_updated_keys->aging_counter;
66
67 /* Reset fake functions call counter */
68 SETTINGS_STORE_FFF_FAKES_LIST(RESET_FAKE);
69
70 bt_keys_update_usage(id, addr);
71
72 zassert_true(expected_updated_keys->aging_counter > (old_aging_counter),
73 "bt_keys_update_usage() set incorrect aging counter");
74
75 zassert_equal_ptr(
76 bt_keys_get_last_keys_updated(), expected_updated_keys,
77 "bt_keys_update_usage() changed last updated key reference unexpectedly");
78
79 /* Check if bt_keys_store() was called */
80 expect_single_call_bt_settings_store_keys(expected_updated_keys->storage_start);
81 }
82 }
83