1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "mocks/adv.h"
8 #include "mocks/hci_core.h"
9 #include "mocks/settings.h"
10 #include "mocks/settings_expects.h"
11 #include "testing_common_defs.h"
12 
13 #include <zephyr/bluetooth/hci.h>
14 #include <zephyr/kernel.h>
15 #include <zephyr/ztest.h>
16 
17 #include <host/hci_core.h>
18 #include <host/id.h>
19 
20 ZTEST_SUITE(bt_id_delete_bt_settings, NULL, NULL, NULL, NULL, NULL);
21 
22 /*
23  *  Test deleting an ID, but not the last one
24  *  As 'CONFIG_BT_SETTINGS' is enabled, settings should be saved by calling bt_settings_store_id()
25  *  and bt_settings_store_irk() if 'BT_DEV_READY' flag in bt_dev.flags is set
26  *
27  *  Constraints:
28  *   - ID value used is neither corresponds to default index nor the last index
29  *   - 'CONFIG_BT_SETTINGS' is enabled
30  *   - 'BT_DEV_READY' flag in bt_dev.flags is set
31  *
32  *  Expected behaviour:
33  *   - bt_dev.id_addr[] at index equals to the ID value used is cleared
34  *   - bt_dev.irk[] at index equals to the ID value used is cleared (if privacy is enabled)
35  *   - bt_settings_store_id() and bt_settings_store_irk() are called to save settings
36  *   - bt_dev.id_count is kept unchanged
37  *   - bt_id_delete() returns 0
38  */
ZTEST(bt_id_delete_bt_settings,test_delete_non_default_no_last_item_settings_enabled)39 ZTEST(bt_id_delete_bt_settings, test_delete_non_default_no_last_item_settings_enabled)
40 {
41 	int err;
42 	uint8_t id;
43 	int id_count;
44 #if defined(CONFIG_BT_PRIVACY)
45 	uint8_t zero_irk[16] = {0};
46 #endif
47 
48 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_SETTINGS);
49 
50 	bt_dev.id_count = 3;
51 	id = 1;
52 	id_count = bt_dev.id_count;
53 
54 	bt_addr_le_copy(&bt_dev.id_addr[0], BT_RPA_LE_ADDR);
55 	bt_addr_le_copy(&bt_dev.id_addr[1], BT_STATIC_RANDOM_LE_ADDR_1);
56 	bt_addr_le_copy(&bt_dev.id_addr[2], BT_STATIC_RANDOM_LE_ADDR_2);
57 
58 	atomic_set_bit(bt_dev.flags, BT_DEV_READY);
59 
60 	err = bt_id_delete(id);
61 
62 	expect_single_call_bt_settings_store_id();
63 	expect_single_call_bt_settings_store_irk();
64 
65 	zassert_ok(err, "Unexpected error code '%d' was returned", err);
66 	zassert_true(bt_dev.id_count == id_count, "Incorrect ID count %d was set", bt_dev.id_count);
67 
68 	zassert_mem_equal(&bt_dev.id_addr[id], BT_ADDR_LE_ANY, sizeof(bt_addr_le_t),
69 			  "Incorrect address was set");
70 #if defined(CONFIG_BT_PRIVACY)
71 	zassert_mem_equal(bt_dev.irk[id], zero_irk, sizeof(zero_irk),
72 			  "Incorrect IRK value was set");
73 #endif
74 }
75 
76 /*
77  *  Test deleting last ID. As 'CONFIG_BT_SETTINGS' is enabled, settings should
78  *  be saved by calling bt_settings_store_id() and bt_settings_store_irk() if
79  *  'BT_DEV_READY' flag in bt_dev.flags is set
80  *
81  *  Constraints:
82  *   - ID value used corresponds to the last item in the list bt_dev.id_addr[]
83  *   - 'CONFIG_BT_SETTINGS' is enabled
84  *   - 'BT_DEV_READY' flag in bt_dev.flags is set
85  *
86  *  Expected behaviour:
87  *   - bt_dev.id_addr[] at index equals to the ID value used is cleared
88  *   - bt_dev.irk[] at index equals to the ID value used is cleared (if privacy
89  *     is enabled)
90  *   - bt_settings_store_id() and bt_settings_store_irk() are called to save settings
91  *   - bt_dev.id_count is decremented
92  *   - bt_id_delete() returns 0
93  */
ZTEST(bt_id_delete_bt_settings,test_delete_last_id_settings_enabled)94 ZTEST(bt_id_delete_bt_settings, test_delete_last_id_settings_enabled)
95 {
96 	int err;
97 	uint8_t id;
98 	int id_count;
99 #if defined(CONFIG_BT_PRIVACY)
100 	uint8_t zero_irk[16] = {0};
101 #endif
102 
103 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_SETTINGS);
104 
105 	bt_dev.id_count = 2;
106 	id = bt_dev.id_count - 1;
107 	id_count = bt_dev.id_count;
108 
109 	bt_addr_le_copy(&bt_dev.id_addr[0], BT_STATIC_RANDOM_LE_ADDR_1);
110 	bt_addr_le_copy(&bt_dev.id_addr[1], BT_STATIC_RANDOM_LE_ADDR_2);
111 
112 	atomic_set_bit(bt_dev.flags, BT_DEV_READY);
113 
114 	err = bt_id_delete(id);
115 
116 	expect_single_call_bt_settings_store_id();
117 	expect_single_call_bt_settings_store_irk();
118 
119 	zassert_ok(err, "Unexpected error code '%d' was returned", err);
120 	zassert_true(bt_dev.id_count == (id_count - 1), "Incorrect ID count %d was set",
121 		     bt_dev.id_count);
122 
123 	zassert_mem_equal(&bt_dev.id_addr[id], BT_ADDR_LE_ANY, sizeof(bt_addr_le_t),
124 			  "Incorrect address was set");
125 #if defined(CONFIG_BT_PRIVACY)
126 	zassert_mem_equal(bt_dev.irk[id], zero_irk, sizeof(zero_irk),
127 			  "Incorrect IRK value was set");
128 #endif
129 }
130