1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "mocks/id.h"
8 #include "mocks/id_expects.h"
9 #include "mocks/keys_help_utils.h"
10 #include "testing_common_defs.h"
11 
12 #include <zephyr/bluetooth/bluetooth.h>
13 #include <zephyr/fff.h>
14 #include <zephyr/kernel.h>
15 
16 DEFINE_FFF_GLOBALS;
17 
tc_setup(void * f)18 static void tc_setup(void *f)
19 {
20 	/* Clear keys pool */
21 	clear_key_pool();
22 
23 	/* Register resets */
24 	ID_FFF_FAKES_LIST(RESET_FAKE);
25 }
26 
27 ZTEST_SUITE(bt_keys_clear_keys_with_state_not_set, NULL, NULL, tc_setup, NULL, NULL);
28 ZTEST_SUITE(bt_keys_clear_keys_with_state_set, NULL, NULL, tc_setup, NULL, NULL);
29 
30 /*
31  *  Clear an existing key and verify the result while 'BT_KEYS_ID_ADDED' state isn't set.
32  *  As 'BT_KEYS_ID_ADDED' isn't set, bt_id_del() shouldn't be called.
33  *
34  *  Constraints:
35  *   - Key reference points to a valid item
36  *
37  *  Expected behaviour:
38  *   - The key content is cleared
39  *   - bt_id_del() isn't called
40  */
ZTEST(bt_keys_clear_keys_with_state_not_set,test_key_cleared_bt_id_del_not_called)41 ZTEST(bt_keys_clear_keys_with_state_not_set, test_key_cleared_bt_id_del_not_called)
42 {
43 	struct bt_keys empty_key;
44 	struct bt_keys *key_ref_to_clear, *find_returned_ref;
45 	uint8_t id = BT_ADDR_ID_0;
46 	bt_addr_le_t *addr = BT_ADDR_LE_1;
47 
48 	if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
49 		ztest_test_skip();
50 	}
51 
52 	memset(&empty_key, 0x00, sizeof(struct bt_keys));
53 
54 	/* Add custom item to the keys pool */
55 	key_ref_to_clear = bt_keys_get_addr(id, addr);
56 	zassert_true(key_ref_to_clear != NULL, "bt_keys_get_addr() returned a non-valid reference");
57 
58 	/* Ensure that item exists in the keys pool */
59 	find_returned_ref = bt_keys_find_addr(id, addr);
60 	zassert_true(find_returned_ref != NULL, "bt_keys_find_addr() returned a NULL reference");
61 
62 	bt_keys_clear(key_ref_to_clear);
63 
64 	expect_not_called_bt_id_del();
65 
66 	/* Verify that memory was cleared */
67 	zassert_mem_equal(key_ref_to_clear, &empty_key, sizeof(struct bt_keys),
68 			  "Key content wasn't cleared by 'bt_keys_clear()'");
69 
70 	/* Ensure that item doesn't exist in the keys pool after calling bt_keys_clear() */
71 	find_returned_ref = bt_keys_find_addr(id, addr);
72 	zassert_true(find_returned_ref == NULL,
73 		     "bt_keys_find_addr() returned a non-NULL reference");
74 }
75 
76 /*
77  *  Clear an existing key and verify the result while 'BT_KEYS_ID_ADDED' state is set.
78  *  As 'BT_KEYS_ID_ADDED' is set, bt_id_del() should be called.
79  *
80  *  Constraints:
81  *   - Key reference points to a valid item
82  *
83  *  Expected behaviour:
84  *   - The key content is cleared
85  *   - bt_id_del() is called with correct key reference
86  */
ZTEST(bt_keys_clear_keys_with_state_set,test_key_cleared_bt_id_del_called)87 ZTEST(bt_keys_clear_keys_with_state_set, test_key_cleared_bt_id_del_called)
88 {
89 	struct bt_keys empty_key;
90 	struct bt_keys *key_ref_to_clear, *find_returned_ref;
91 	uint8_t id = BT_ADDR_ID_0;
92 	bt_addr_le_t *addr = BT_ADDR_LE_1;
93 
94 	if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
95 		ztest_test_skip();
96 	}
97 
98 	memset(&empty_key, 0x00, sizeof(struct bt_keys));
99 
100 	/* Add custom item to the keys pool */
101 	key_ref_to_clear = bt_keys_get_addr(id, addr);
102 	zassert_true(key_ref_to_clear != NULL, "bt_keys_get_addr() returned a non-valid reference");
103 
104 	/* Ensure that item exists in the keys pool */
105 	find_returned_ref = bt_keys_find_addr(id, addr);
106 	zassert_true(find_returned_ref != NULL, "bt_keys_find_addr() returned a NULL reference");
107 
108 	key_ref_to_clear->state = BT_KEYS_ID_ADDED;
109 
110 	bt_keys_clear(key_ref_to_clear);
111 
112 	expect_single_call_bt_id_del(key_ref_to_clear);
113 
114 	/* Verify that memory was cleared */
115 	zassert_mem_equal(key_ref_to_clear, &empty_key, sizeof(struct bt_keys),
116 			  "Key content wasn't cleared by 'bt_keys_clear()'");
117 
118 	/* Ensure that item doesn't exist in the keys pool after calling bt_keys_clear() */
119 	find_returned_ref = bt_keys_find_addr(id, addr);
120 	zassert_true(find_returned_ref == NULL,
121 		     "bt_keys_find_addr() returned a non-NULL reference");
122 }
123