1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "testing_common_defs.h"
8 
9 #include <zephyr/bluetooth/hci.h>
10 #include <zephyr/fff.h>
11 #include <zephyr/kernel.h>
12 
13 #include <host/hci_core.h>
14 #include <host/id.h>
15 
16 DEFINE_FFF_GLOBALS;
17 
18 /* This LUT contains different testing addresses. */
19 static const bt_addr_le_t *testing_addr_lut[] = {BT_LE_ADDR, BT_STATIC_RANDOM_LE_ADDR_1,
20 						 BT_STATIC_RANDOM_LE_ADDR_2, BT_RPA_LE_ADDR};
21 
22 /* This is the destination address used during UTs */
23 static bt_addr_le_t copy_dst_addrs[CONFIG_BT_ID_MAX];
24 
25 BUILD_ASSERT(ARRAY_SIZE(testing_addr_lut) == CONFIG_BT_ID_MAX);
26 
fff_reset_rule_before(const struct ztest_unit_test * test,void * fixture)27 static void fff_reset_rule_before(const struct ztest_unit_test *test, void *fixture)
28 {
29 	memset(&bt_dev, 0x00, sizeof(struct bt_dev));
30 	memset(copy_dst_addrs, 0x00, sizeof(copy_dst_addrs));
31 
32 	for (size_t i = 0; i < CONFIG_BT_ID_MAX; i++) {
33 		const bt_addr_le_t *src = testing_addr_lut[i];
34 		bt_addr_le_t *dst = &bt_dev.id_addr[bt_dev.id_count++];
35 
36 		memcpy(dst, src, sizeof(bt_addr_le_t));
37 	}
38 
39 	for (size_t i = 0; i < CONFIG_BT_ID_MAX; i++) {
40 		memcpy(&copy_dst_addrs[i], BT_ADDR_LE_ANY, sizeof(bt_addr_le_t));
41 	}
42 }
43 
44 ZTEST_RULE(fff_reset_rule, fff_reset_rule_before, NULL);
45 
46 ZTEST_SUITE(bt_id_get, NULL, NULL, NULL, NULL, NULL);
47 
48 /*
49  *  Get currently stored ID count
50  *
51  *  Constraints:
52  *   - Use NULL value for the address
53  *
54  *  Expected behaviour:
55  *   - Count parameter pointer is dereferenced and loaded with the current bt_dev.id_count
56  */
ZTEST(bt_id_get,test_get_current_id_count)57 ZTEST(bt_id_get, test_get_current_id_count)
58 {
59 	size_t count;
60 
61 	bt_id_get(NULL, &count);
62 
63 	zassert_equal(count, CONFIG_BT_ID_MAX, "Incorrect ID count %d was returned", count);
64 }
65 
66 /*
67  *  Copy minimum number of addresses to the destination array
68  *
69  *  Constraints:
70  *   - Destination array is initially cleared
71  *
72  *  Expected behaviour:
73  *   - Count parameter pointer is dereferenced and loaded with actual number of copied items
74  */
ZTEST(bt_id_get,test_copy_minimum_count)75 ZTEST(bt_id_get, test_copy_minimum_count)
76 {
77 	size_t stored_count = bt_dev.id_count;
78 	size_t testing_counts[] = {0, 1, bt_dev.id_count, bt_dev.id_count + 2};
79 
80 	for (size_t it = 0; it < ARRAY_SIZE(testing_counts); it++) {
81 		size_t count = testing_counts[it];
82 		size_t expected_count = MIN(count, stored_count);
83 
84 		bt_id_get(copy_dst_addrs, &count);
85 
86 		zassert_equal(count, expected_count, "Incorrect ID count %d was returned", count);
87 
88 		/* Verify copied items */
89 		for (size_t i = 0; i < count; i++) {
90 			const bt_addr_le_t *src = testing_addr_lut[i];
91 			bt_addr_le_t *dst = &copy_dst_addrs[i];
92 
93 			zassert_mem_equal(src, dst, sizeof(bt_addr_le_t),
94 					  "Incorrect address was set");
95 		}
96 
97 		/* Verify the rest of items */
98 		for (size_t i = count; i < stored_count; i++) {
99 			bt_addr_le_t *src = &copy_dst_addrs[i];
100 
101 			zassert_mem_equal(src, BT_ADDR_LE_ANY, sizeof(bt_addr_le_t),
102 					  "Incorrect address was set");
103 		}
104 	}
105 }
106