1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #if defined(CONFIG_BT_PRIVACY)
8 
9 #include "mocks/crypto.h"
10 #include "mocks/crypto_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 #include <mocks/addr.h>
20 #include <mocks/addr_expects.h>
21 
22 static uint8_t testing_irk_value[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
23 					0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15};
24 
tc_setup(void * f)25 static void tc_setup(void *f)
26 {
27 	CRYPTO_FFF_FAKES_LIST(RESET_FAKE);
28 }
29 
30 ZTEST_SUITE(bt_id_create_privacy_enabled, NULL, NULL, tc_setup, NULL, NULL);
31 
bt_rand_custom_fake(void * buf,size_t len)32 static int bt_rand_custom_fake(void *buf, size_t len)
33 {
34 	__ASSERT_NO_MSG(buf != NULL);
35 	__ASSERT_NO_MSG(len == 16);
36 
37 	memcpy(buf, testing_irk_value, len);
38 
39 	return 0;
40 }
41 
42 /*
43  *  Test creating a new identity.
44  *  A valid random static address is passed to bt_id_create() for the address and 'BT_DEV_ENABLE' is
45  *  set, the same address is used and copied to bt_dev.id_addr[].
46  *
47  *  Constraints:
48  *   - Valid private random address is used
49  *   - Input IRK is NULL
50  *   - 'BT_DEV_ENABLE' flag is set in bt_dev.flags
51  *
52  *  Expected behaviour:
53  *   - The same address is used and loaded to bt_dev.id_addr[]
54  *   - IRK is loaded to bt_dev.irk[]
55  *   - bt_dev.id_count is incremented
56  */
ZTEST(bt_id_create_privacy_enabled,test_create_id_valid_input_address_null_irk)57 ZTEST(bt_id_create_privacy_enabled, test_create_id_valid_input_address_null_irk)
58 {
59 	int id_count, new_id;
60 	bt_addr_le_t addr = *BT_STATIC_RANDOM_LE_ADDR_1;
61 
62 	id_count = bt_dev.id_count;
63 	atomic_set_bit(bt_dev.flags, BT_DEV_ENABLE);
64 	bt_rand_fake.custom_fake = bt_rand_custom_fake;
65 	/* Calling bt_addr_le_create_static() isn't expected */
66 	bt_addr_le_create_static_fake.return_val = -1;
67 
68 	new_id = bt_id_create(&addr, NULL);
69 
70 	expect_not_called_bt_addr_le_create_static();
71 	expect_single_call_bt_rand(&bt_dev.irk[new_id], 16);
72 
73 	zassert_true(new_id >= 0, "Unexpected error code '%d' was returned", new_id);
74 	zassert_true(bt_dev.id_count == (id_count + 1), "Incorrect ID count %d was set",
75 		     bt_dev.id_count);
76 	zassert_mem_equal(&bt_dev.id_addr[new_id], BT_STATIC_RANDOM_LE_ADDR_1, sizeof(bt_addr_le_t),
77 			  "Incorrect address was set");
78 	zassert_mem_equal(&bt_dev.irk[new_id], testing_irk_value, sizeof(testing_irk_value),
79 			  "Incorrect address was set");
80 }
81 
82 /*
83  *  Test creating a new identity.
84  *  A valid random static address is passed to bt_id_create() for the address and 'BT_DEV_ENABLE' is
85  *  set, the same address is used and copied to bt_dev.id_addr[].
86  *
87  *  Constraints:
88  *   - Valid private random address is used
89  *   - Input IRK is cleared (zero filled)
90  *   - 'BT_DEV_ENABLE' flag is set in bt_dev.flags
91  *
92  *  Expected behaviour:
93  *   - The same address is used and loaded to bt_dev.id_addr[]
94  *   - IRK is loaded to bt_dev.irk[]
95  *   - IRK is loaded to input IRK buffer
96  *   - bt_dev.id_count is incremented
97  */
ZTEST(bt_id_create_privacy_enabled,test_create_id_valid_input_address_cleared_irk)98 ZTEST(bt_id_create_privacy_enabled, test_create_id_valid_input_address_cleared_irk)
99 {
100 	int id_count, new_id;
101 	bt_addr_le_t addr = *BT_STATIC_RANDOM_LE_ADDR_1;
102 	uint8_t zero_irk[16] = {0};
103 
104 	id_count = bt_dev.id_count;
105 	atomic_set_bit(bt_dev.flags, BT_DEV_ENABLE);
106 	bt_rand_fake.custom_fake = bt_rand_custom_fake;
107 	/* Calling bt_addr_le_create_static() isn't expected */
108 	bt_addr_le_create_static_fake.return_val = -1;
109 
110 	new_id = bt_id_create(&addr, zero_irk);
111 
112 	expect_not_called_bt_addr_le_create_static();
113 	expect_single_call_bt_rand(&bt_dev.irk[new_id], 16);
114 
115 	zassert_true(new_id >= 0, "Unexpected error code '%d' was returned", new_id);
116 	zassert_true(bt_dev.id_count == (id_count + 1), "Incorrect ID count %d was set",
117 		     bt_dev.id_count);
118 	zassert_mem_equal(&bt_dev.id_addr[new_id], BT_STATIC_RANDOM_LE_ADDR_1, sizeof(bt_addr_le_t),
119 			  "Incorrect address was set");
120 	zassert_mem_equal(&bt_dev.irk[new_id], testing_irk_value, sizeof(testing_irk_value),
121 			  "Incorrect address was set");
122 	zassert_mem_equal(zero_irk, testing_irk_value, sizeof(testing_irk_value),
123 			  "Incorrect address was set");
124 }
125 
126 /*
127  *  Test creating a new identity.
128  *  A valid random static address is passed to bt_id_create() for the address and 'BT_DEV_ENABLE' is
129  *  set, the same address is used and copied to bt_dev.id_addr[].
130  *
131  *  Constraints:
132  *   - Valid private random address is used
133  *   - Input IRK is filled with non-zero values
134  *   - 'BT_DEV_ENABLE' flag is set in bt_dev.flags
135  *
136  *  Expected behaviour:
137  *   - The same address is used and loaded to bt_dev.id_addr[]
138  *   - Input IRK is loaded to bt_dev.irk[]
139  *   - bt_dev.id_count is incremented
140  */
ZTEST(bt_id_create_privacy_enabled,test_create_id_valid_input_address_filled_irk)141 ZTEST(bt_id_create_privacy_enabled, test_create_id_valid_input_address_filled_irk)
142 {
143 	int id_count, new_id;
144 	bt_addr_le_t addr = *BT_STATIC_RANDOM_LE_ADDR_1;
145 
146 	id_count = bt_dev.id_count;
147 	atomic_set_bit(bt_dev.flags, BT_DEV_ENABLE);
148 	bt_rand_fake.custom_fake = bt_rand_custom_fake;
149 	/* Calling bt_addr_le_create_static() isn't expected */
150 	bt_addr_le_create_static_fake.return_val = -1;
151 
152 	new_id = bt_id_create(&addr, testing_irk_value);
153 
154 	expect_not_called_bt_addr_le_create_static();
155 	expect_not_called_bt_rand();
156 
157 	zassert_true(new_id >= 0, "Unexpected error code '%d' was returned", new_id);
158 	zassert_true(bt_dev.id_count == (id_count + 1), "Incorrect ID count %d was set",
159 		     bt_dev.id_count);
160 	zassert_mem_equal(&bt_dev.id_addr[new_id], BT_STATIC_RANDOM_LE_ADDR_1, sizeof(bt_addr_le_t),
161 			  "Incorrect address was set");
162 	zassert_mem_equal(&bt_dev.irk[new_id], testing_irk_value, sizeof(testing_irk_value),
163 			  "Incorrect address was set");
164 }
165 #endif
166