1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "mocks/crypto.h"
8 #include "mocks/crypto_expects.h"
9 #include "mocks/hci_core.h"
10 #include "mocks/rpa.h"
11 #include "mocks/rpa_expects.h"
12 #include "testing_common_defs.h"
13 
14 #include <zephyr/bluetooth/hci.h>
15 #include <zephyr/fff.h>
16 #include <zephyr/kernel.h>
17 
18 #include <host/hci_core.h>
19 #include <host/id.h>
20 
21 DEFINE_FFF_GLOBALS;
22 
fff_reset_rule_before(const struct ztest_unit_test * test,void * fixture)23 static void fff_reset_rule_before(const struct ztest_unit_test *test, void *fixture)
24 {
25 	memset(&bt_dev, 0x00, sizeof(struct bt_dev));
26 	bt_addr_le_copy(&bt_dev.random_addr, &bt_addr_le_none);
27 
28 	RPA_FFF_FAKES_LIST(RESET_FAKE);
29 	CRYPTO_FFF_FAKES_LIST(RESET_FAKE);
30 	HCI_CORE_FFF_FAKES_LIST(RESET_FAKE);
31 }
32 
33 ZTEST_RULE(fff_reset_rule, fff_reset_rule_before, NULL);
34 
35 ZTEST_SUITE(bt_id_set_private_addr, NULL, NULL, NULL, NULL, NULL);
36 
bt_rand_custom_fake(void * buf,size_t len)37 static int bt_rand_custom_fake(void *buf, size_t len)
38 {
39 	__ASSERT_NO_MSG(buf != NULL);
40 	__ASSERT_NO_MSG(len == sizeof(BT_ADDR->val));
41 
42 	/* This will make set_random_address() succeeds and returns 0 */
43 	memcpy(buf, &BT_ADDR->val, len);
44 	bt_addr_copy(&bt_dev.random_addr.a, BT_ADDR);
45 
46 	return 0;
47 }
48 
bt_rpa_create_custom_fake(const uint8_t irk[16],bt_addr_t * rpa)49 static int bt_rpa_create_custom_fake(const uint8_t irk[16], bt_addr_t *rpa)
50 {
51 	__ASSERT_NO_MSG(irk != NULL);
52 	__ASSERT_NO_MSG(rpa != NULL);
53 
54 	/* This will make set_random_address() succeeds and returns 0 */
55 	bt_addr_copy(rpa, &BT_RPA_LE_ADDR->a);
56 	bt_addr_copy(&bt_dev.random_addr.a, &BT_RPA_LE_ADDR->a);
57 
58 	return 0;
59 }
60 
61 /*
62  *  Test setting private address with a valid 'id'
63  *
64  *  Constraints:
65  *   - A valid ID value should be used (<= CONFIG_BT_ID_MAX)
66  *
67  *  Expected behaviour:
68  *   - bt_id_set_private_addr() returns 0 (success)
69  */
ZTEST(bt_id_set_private_addr,test_setting_address_with_valid_id_succeeds)70 ZTEST(bt_id_set_private_addr, test_setting_address_with_valid_id_succeeds)
71 {
72 	int err;
73 	uint8_t id = BT_ID_DEFAULT;
74 
75 	if (!IS_ENABLED(CONFIG_BT_PRIVACY)) {
76 		bt_rand_fake.custom_fake = bt_rand_custom_fake;
77 	} else {
78 		bt_rpa_create_fake.custom_fake = bt_rpa_create_custom_fake;
79 	}
80 
81 	err = bt_id_set_private_addr(id);
82 
83 	if (!IS_ENABLED(CONFIG_BT_PRIVACY)) {
84 		expect_not_called_bt_rpa_create();
85 	} else {
86 #if defined(CONFIG_BT_PRIVACY)
87 		expect_single_call_bt_rpa_create(bt_dev.irk[id]);
88 #endif
89 		zassert_true(atomic_test_bit(bt_dev.flags, BT_DEV_RPA_VALID),
90 			     "Flags were not correctly set");
91 	}
92 
93 	zassert_ok(err, "Unexpected error code '%d' was returned", err);
94 }
95 
96 /*
97  *  Test setting private address with a valid 'id' after it has been set before.
98  *
99  *  Constraints:
100  *   - A valid ID value should be used (<= CONFIG_BT_ID_MAX)
101  *   - 'BT_DEV_RPA_VALID' flag in bt_dev.flags is set
102  *
103  *  Expected behaviour:
104  *   - bt_id_set_private_addr() returns 0 (success) without completing the procedure
105  */
ZTEST(bt_id_set_private_addr,test_setting_address_do_nothing_when_it_was_previously_set)106 ZTEST(bt_id_set_private_addr, test_setting_address_do_nothing_when_it_was_previously_set)
107 {
108 	int err;
109 	uint8_t id = BT_ID_DEFAULT;
110 
111 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_PRIVACY);
112 
113 	atomic_set_bit(bt_dev.flags, BT_DEV_RPA_VALID);
114 
115 	err = bt_id_set_private_addr(id);
116 
117 	expect_not_called_bt_rpa_create();
118 
119 	zassert_ok(err, "Unexpected error code '%d' was returned", err);
120 }
121