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/fff.h>
15 #include <zephyr/kernel.h>
16
17 #include <host/hci_core.h>
18 #include <host/id.h>
19
20 DEFINE_FFF_GLOBALS;
21
fff_reset_rule_before(const struct ztest_unit_test * test,void * fixture)22 static void fff_reset_rule_before(const struct ztest_unit_test *test, void *fixture)
23 {
24 memset(&bt_dev, 0x00, sizeof(struct bt_dev));
25
26 ADV_FFF_FAKES_LIST(RESET_FAKE);
27 SETTINGS_FFF_FAKES_LIST(RESET_FAKE);
28 HCI_CORE_FFF_FAKES_LIST(RESET_FAKE);
29 }
30
31 ZTEST_RULE(fff_reset_rule, fff_reset_rule_before, NULL);
32
33 ZTEST_SUITE(bt_id_delete, NULL, NULL, NULL, NULL, NULL);
34
35 /*
36 * Test deleting an ID, but not the last one
37 *
38 * Constraints:
39 * - ID value used is neither corresponds to default index nor the last index
40 *
41 * Expected behaviour:
42 * - bt_dev.id_addr[] at index equals to the ID value used is cleared
43 * - bt_dev.irk[] at index equals to the ID value used is cleared (if privacy is enabled)
44 * - bt_id_delete() returns 0
45 */
ZTEST(bt_id_delete,test_delete_non_default_no_last_item)46 ZTEST(bt_id_delete, test_delete_non_default_no_last_item)
47 {
48 int err;
49 uint8_t id;
50 int id_count;
51 #if defined(CONFIG_BT_PRIVACY)
52 uint8_t zero_irk[16] = {0};
53 #endif
54
55 bt_dev.id_count = 3;
56 id = 1;
57 id_count = bt_dev.id_count;
58
59 bt_addr_le_copy(&bt_dev.id_addr[0], BT_RPA_LE_ADDR);
60 bt_addr_le_copy(&bt_dev.id_addr[1], BT_STATIC_RANDOM_LE_ADDR_1);
61 bt_addr_le_copy(&bt_dev.id_addr[2], BT_STATIC_RANDOM_LE_ADDR_2);
62
63 atomic_clear_bit(bt_dev.flags, BT_DEV_READY);
64
65 err = bt_id_delete(id);
66
67 expect_not_called_bt_settings_store_id();
68 expect_not_called_bt_settings_store_irk();
69
70 zassert_ok(err, "Unexpected error code '%d' was returned", err);
71 zassert_true(bt_dev.id_count == id_count, "Incorrect ID count %d was set", bt_dev.id_count);
72
73 zassert_mem_equal(&bt_dev.id_addr[id], BT_ADDR_LE_ANY, sizeof(bt_addr_le_t),
74 "Incorrect address was set");
75 #if defined(CONFIG_BT_PRIVACY)
76 zassert_mem_equal(bt_dev.irk[id], zero_irk, sizeof(zero_irk),
77 "Incorrect IRK value was set");
78 #endif
79 }
80
81 /*
82 * Test deleting last ID
83 *
84 * Constraints:
85 * - ID value used corresponds to the last item in the list bt_dev.id_addr[]
86 *
87 * Expected behaviour:
88 * - bt_dev.id_addr[] at index equals to the ID value used is cleared
89 * - bt_dev.irk[] at index equals to the ID value used is cleared (if privacy is enabled)
90 * - bt_dev.id_count is decremented
91 * - bt_id_delete() returns 0
92 */
ZTEST(bt_id_delete,test_delete_last_id)93 ZTEST(bt_id_delete, test_delete_last_id)
94 {
95 int err;
96 uint8_t id;
97 int id_count;
98 #if defined(CONFIG_BT_PRIVACY)
99 uint8_t zero_irk[16] = {0};
100 #endif
101
102 bt_dev.id_count = 2;
103 id = bt_dev.id_count - 1;
104 id_count = bt_dev.id_count;
105
106 bt_addr_le_copy(&bt_dev.id_addr[0], BT_STATIC_RANDOM_LE_ADDR_1);
107 bt_addr_le_copy(&bt_dev.id_addr[1], BT_STATIC_RANDOM_LE_ADDR_2);
108
109 atomic_clear_bit(bt_dev.flags, BT_DEV_READY);
110
111 err = bt_id_delete(id);
112
113 expect_not_called_bt_settings_store_id();
114 expect_not_called_bt_settings_store_irk();
115
116 zassert_ok(err, "Unexpected error code '%d' was returned", err);
117
118 zassert_true(bt_dev.id_count == (id_count - 1), "Incorrect ID count %d was set",
119 bt_dev.id_count);
120
121 zassert_mem_equal(&bt_dev.id_addr[id], BT_ADDR_LE_ANY, sizeof(bt_addr_le_t),
122 "Incorrect address was set");
123 #if defined(CONFIG_BT_PRIVACY)
124 zassert_mem_equal(bt_dev.irk[id], zero_irk, sizeof(zero_irk),
125 "Incorrect IRK value was set");
126 #endif
127 }
128