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 "testing_common_defs.h"
9 
10 #include <zephyr/bluetooth/bluetooth.h>
11 #include <zephyr/fff.h>
12 #include <zephyr/kernel.h>
13 
14 #include <host/keys.h>
15 
16 DEFINE_FFF_GLOBALS;
17 
18 /* This LUT contains different combinations of ID, Address and key type.
19  * Item in this list will be used to fill keys pool.
20  */
21 static const struct id_addr_type testing_id_addr_type_lut[] = {
22 	{BT_ADDR_ID_1, BT_ADDR_LE_1, BT_KEYS_PERIPH_LTK},
23 	{BT_ADDR_ID_1, BT_ADDR_LE_2, BT_KEYS_IRK},
24 	{BT_ADDR_ID_2, BT_ADDR_LE_1, BT_KEYS_LTK},
25 	{BT_ADDR_ID_2, BT_ADDR_LE_2, BT_KEYS_LOCAL_CSRK},
26 	{BT_ADDR_ID_3, BT_ADDR_LE_1, BT_KEYS_REMOTE_CSRK},
27 	{BT_ADDR_ID_3, BT_ADDR_LE_2, BT_KEYS_LTK_P256},
28 	{BT_ADDR_ID_4, BT_ADDR_LE_1, BT_KEYS_ALL}};
29 
30 /* This list will hold returned references while filling keys pool */
31 static struct bt_keys *returned_keys_refs[CONFIG_BT_MAX_PAIRED];
32 
33 BUILD_ASSERT(ARRAY_SIZE(testing_id_addr_type_lut) == CONFIG_BT_MAX_PAIRED);
34 BUILD_ASSERT(ARRAY_SIZE(testing_id_addr_type_lut) == ARRAY_SIZE(returned_keys_refs));
35 
empty_list_ts_setup(void)36 static void *empty_list_ts_setup(void)
37 {
38 	clear_key_pool();
39 
40 	return NULL;
41 }
42 
43 ZTEST_SUITE(bt_keys_find_initially_empty_list, NULL, empty_list_ts_setup, NULL, NULL, NULL);
44 
45 /*
46  *  Test calling bt_keys_find() with non-existing item
47  *
48  *  Constraints:
49  *   - Valid values of non-existing items are used
50  *
51  *  Expected behaviour:
52  *   - NULL reference is returned
53  */
ZTEST(bt_keys_find_initially_empty_list,test_find_non_existing_item)54 ZTEST(bt_keys_find_initially_empty_list, test_find_non_existing_item)
55 {
56 	for (uint32_t i = 0; i < ARRAY_SIZE(testing_id_addr_type_lut); i++) {
57 		struct bt_keys *returned_ref;
58 		struct id_addr_type const *params_vector = &testing_id_addr_type_lut[i];
59 		int type = params_vector->type;
60 		uint8_t id = params_vector->id;
61 		const bt_addr_le_t *addr = params_vector->addr;
62 
63 		returned_ref = bt_keys_find(type, id, addr);
64 		zassert_true(returned_ref == NULL, "bt_keys_find() returned a non-NULL reference");
65 	}
66 }
67 
filled_list_ts_setup(void)68 static void *filled_list_ts_setup(void)
69 {
70 	clear_key_pool();
71 	int rv = fill_key_pool_by_id_addr_type(
72 		testing_id_addr_type_lut, ARRAY_SIZE(testing_id_addr_type_lut), returned_keys_refs);
73 
74 	zassert_true(rv == 0, "Failed to fill keys pool list, error code %d", -rv);
75 
76 	return NULL;
77 }
78 
79 ZTEST_SUITE(bt_keys_find_initially_filled_list, NULL, filled_list_ts_setup, NULL, NULL, NULL);
80 
81 /*
82  *  Test calling bt_keys_find() with existing items
83  *
84  *  Constraints:
85  *   - Keys pool list is filled
86  *   - Valid values of existing items are used
87  *
88  *  Expected behaviour:
89  *   - Valid reference pointer is returned and matches the correct reference
90  */
ZTEST(bt_keys_find_initially_filled_list,test_find_existing_item)91 ZTEST(bt_keys_find_initially_filled_list, test_find_existing_item)
92 {
93 	int type;
94 	uint8_t id;
95 	const bt_addr_le_t *addr;
96 	struct bt_keys *returned_ref, *expected_key_ref;
97 
98 	for (size_t it = 0; it < ARRAY_SIZE(testing_id_addr_type_lut); it++) {
99 
100 		type = testing_id_addr_type_lut[it].type;
101 		id = testing_id_addr_type_lut[it].id;
102 		addr = testing_id_addr_type_lut[it].addr;
103 
104 		expected_key_ref = returned_keys_refs[it];
105 
106 		returned_ref = bt_keys_find(type, id, addr);
107 
108 		zassert_true(returned_ref != NULL, "bt_keys_find() returned a NULL reference");
109 		zassert_equal_ptr(returned_ref, expected_key_ref,
110 				  "bt_keys_find() returned unexpected reference");
111 	}
112 }
113