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.h"
9 #include "mocks/settings_expects.h"
10 #include "mocks/settings_store.h"
11 #include "mocks/settings_store_expects.h"
12 #include "mocks/util.h"
13 #include "mocks/util_expects.h"
14 #include "testing_common_defs.h"
15 
16 #include <zephyr/bluetooth/addr.h>
17 #include <zephyr/fff.h>
18 #include <zephyr/kernel.h>
19 
20 #include <host/keys.h>
21 
22 DEFINE_FFF_GLOBALS;
23 
tc_setup(void * f)24 static void tc_setup(void *f)
25 {
26 	/* Clear keys pool */
27 	clear_key_pool();
28 
29 	/* Register resets */
30 	UTIL_FFF_FAKES_LIST(RESET_FAKE);
31 	SETTINGS_FFF_FAKES_LIST(RESET_FAKE);
32 	SETTINGS_STORE_FFF_FAKES_LIST(RESET_FAKE);
33 }
34 
35 ZTEST_SUITE(bt_keys_store_key_bt_settings_enabled, NULL, NULL, tc_setup, NULL, NULL);
36 
37 /*
38  *  Store an existing key (ID = 0) and verify the result.
39  *  bt_settings_store_keys() returns 0 which represents success.
40  *
41  *  Constraints:
42  *   - Key reference points to a valid item
43  *   - Item ID is set to 0
44  *   - Return value from bt_settings_store_keys() is 0
45  *
46  *  Expected behaviour:
47  *   - bt_keys_store() returns 0 which represent success
48  */
ZTEST(bt_keys_store_key_bt_settings_enabled,test_id_equal_0_with_no_error)49 ZTEST(bt_keys_store_key_bt_settings_enabled, test_id_equal_0_with_no_error)
50 {
51 	int returned_code;
52 	struct bt_keys *returned_key;
53 	uint8_t id = BT_ADDR_ID_0;
54 	bt_addr_le_t *addr = BT_ADDR_LE_1;
55 
56 	/* Add custom item to the keys pool */
57 	returned_key = bt_keys_get_addr(id, addr);
58 	zassert_true(returned_key != NULL, "bt_keys_get_addr() returned a non-valid reference");
59 
60 	bt_settings_store_keys_fake.return_val = 0;
61 
62 	/* Store the key */
63 	returned_code = bt_keys_store(returned_key);
64 
65 	zassert_true(returned_code == 0, "bt_keys_store() returned a non-zero code");
66 
67 	expect_single_call_bt_settings_store_keys(returned_key->storage_start);
68 }
69 
70 /*
71  *  Store an existing key (ID = 0) and verify the result.
72  *  bt_settings_store_keys() returns a negative value of -1 which represents
73  *  failure.
74  *
75  *  Constraints:
76  *   - Key reference points to a valid item
77  *   - Item ID is set to 0
78  *   - Return value from bt_settings_store_keys() is -1
79  *
80  *  Expected behaviour:
81  *   - bt_keys_store() returns a negative error code of -1
82  */
ZTEST(bt_keys_store_key_bt_settings_enabled,test_id_equal_0_with_error)83 ZTEST(bt_keys_store_key_bt_settings_enabled, test_id_equal_0_with_error)
84 {
85 	int returned_code;
86 	struct bt_keys *returned_key;
87 	uint8_t id = BT_ADDR_ID_0;
88 	bt_addr_le_t *addr = BT_ADDR_LE_1;
89 
90 	/* Add custom item to the keys pool */
91 	returned_key = bt_keys_get_addr(id, addr);
92 	zassert_true(returned_key != NULL, "bt_keys_get_addr() returned a non-valid reference");
93 
94 	bt_settings_store_keys_fake.return_val = -1;
95 
96 	/* Store the key */
97 	returned_code = bt_keys_store(returned_key);
98 
99 	zassert_true(returned_code == -1, "bt_keys_store() returned a non-zero code");
100 
101 	expect_single_call_bt_settings_store_keys(returned_key->storage_start);
102 }
103 
104 /*
105  *  Store an existing key (ID != 0) and verify the result.
106  *  bt_settings_store_keys() returns 0 which represents success.
107  *
108  *  Constraints:
109  *   - Key reference points to a valid item
110  *   - Item ID isn't set to 0
111  *   - Return value from bt_settings_store_keys() is 0
112  *
113  *  Expected behaviour:
114  *   - bt_keys_store() returns 0 which represent success
115  */
ZTEST(bt_keys_store_key_bt_settings_enabled,test_id_not_equal_0_with_no_error)116 ZTEST(bt_keys_store_key_bt_settings_enabled, test_id_not_equal_0_with_no_error)
117 {
118 	int returned_code;
119 	struct bt_keys *returned_key;
120 	uint8_t id = BT_ADDR_ID_1;
121 	bt_addr_le_t *addr = BT_ADDR_LE_1;
122 
123 	/* Add custom item to the keys pool */
124 	returned_key = bt_keys_get_addr(id, addr);
125 	zassert_true(returned_key != NULL, "bt_keys_get_addr() returned a non-valid reference");
126 
127 	bt_settings_store_keys_fake.return_val = 0;
128 
129 	/* Store the key */
130 	returned_code = bt_keys_store(returned_key);
131 
132 	zassert_true(returned_code == 0, "bt_keys_store() returned a non-zero code");
133 
134 	expect_single_call_bt_settings_store_keys(returned_key->storage_start);
135 }
136 
137 /*
138  *  Store an existing key (ID != 0) and verify the result
139  *  bt_settings_store_keys() returns a negative value of -1 which represents
140  *  failure.
141  *
142  *  Constraints:
143  *   - Key reference points to a valid item
144  *   - Item ID isn't set to 0
145  *   - Return value from settings_save_one() is -1
146  *
147  *  Expected behaviour:
148  *   - bt_keys_store() returns a negative error code of -1
149  */
ZTEST(bt_keys_store_key_bt_settings_enabled,test_id_not_equal_0_with_error)150 ZTEST(bt_keys_store_key_bt_settings_enabled, test_id_not_equal_0_with_error)
151 {
152 	int returned_code;
153 	struct bt_keys *returned_key;
154 	uint8_t id = BT_ADDR_ID_1;
155 	bt_addr_le_t *addr = BT_ADDR_LE_1;
156 
157 	/* Add custom item to the keys pool */
158 	returned_key = bt_keys_get_addr(id, addr);
159 	zassert_true(returned_key != NULL, "bt_keys_get_addr() returned a non-valid reference");
160 
161 	bt_settings_store_keys_fake.return_val = -1;
162 
163 	/* Store the key */
164 	returned_code = bt_keys_store(returned_key);
165 
166 	zassert_true(returned_code == -1, "bt_keys_store() returned a non-zero code");
167 
168 	expect_single_call_bt_settings_store_keys(returned_key->storage_start);
169 }
170