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/addr.h>
9 #include <host/keys.h>
10 #include "mocks/keys_help_utils.h"
11 #include "mocks/hci_core_expects.h"
12 #include "testing_common_defs.h"
13 
14 /* This LUT contains different combinations of ID and Address pairs */
15 extern const struct id_addr_pair testing_id_addr_pair_lut[CONFIG_BT_MAX_PAIRED];
16 
17 /* This list holds returned references while filling keys pool */
18 extern struct bt_keys *returned_keys_refs[CONFIG_BT_MAX_PAIRED];
19 
20 /* Setup test variables */
test_case_setup(void * f)21 static void test_case_setup(void *f)
22 {
23 	Z_TEST_SKIP_IFDEF(CONFIG_BT_KEYS_OVERWRITE_OLDEST);
24 
25 	clear_key_pool();
26 
27 	int rv = fill_key_pool_by_id_addr(testing_id_addr_pair_lut,
28 					  ARRAY_SIZE(testing_id_addr_pair_lut), returned_keys_refs);
29 
30 	zassert_true(rv == 0, "Failed to fill keys pool list, error code %d", -rv);
31 }
32 
33 ZTEST_SUITE(bt_keys_get_addr_full_list_no_overwrite, NULL, NULL, test_case_setup, NULL, NULL);
34 
35 /*
36  *  Test adding extra (ID, Address) pair while the keys pool list is full.
37  *  As 'CONFIG_BT_KEYS_OVERWRITE_OLDEST' isn't enabled, no (ID, Address) pairs can be added while
38  *  the list is full.
39  *
40  *  Constraints:
41  *   - Keys pool list is full
42  *   - CONFIG_BT_KEYS_OVERWRITE_OLDEST isn't enabled
43  *
44  *  Expected behaviour:
45  *   - NULL reference pointer is returned
46  */
ZTEST(bt_keys_get_addr_full_list_no_overwrite,test_adding_new_pair_to_full_list)47 ZTEST(bt_keys_get_addr_full_list_no_overwrite, test_adding_new_pair_to_full_list)
48 {
49 	struct bt_keys *returned_key;
50 	uint8_t id = BT_ADDR_ID_3;
51 	bt_addr_le_t *addr = BT_ADDR_LE_3;
52 
53 	returned_key = bt_keys_get_addr(id, addr);
54 
55 	expect_not_called_bt_unpair();
56 
57 	zassert_true(returned_key == NULL, "bt_keys_get_addr() returned a non-NULL reference");
58 }
59