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 #include <mocks/adv.h>
16 #include <mocks/adv_expects.h>
17 #include <mocks/conn.h>
18 #include <mocks/conn_expects.h>
19 #include <mocks/smp.h>
20 #include <mocks/smp_expects.h>
21 
22 DEFINE_FFF_GLOBALS;
23 
fff_reset_rule_before(const struct ztest_unit_test * test,void * fixture)24 static void fff_reset_rule_before(const struct ztest_unit_test *test, void *fixture)
25 {
26 	memset(&bt_dev, 0x00, sizeof(struct bt_dev));
27 
28 	ADV_FFF_FAKES_LIST(RESET_FAKE);
29 	SMP_FFF_FAKES_LIST(RESET_FAKE);
30 	CONN_FFF_FAKES_LIST(RESET_FAKE);
31 }
32 
33 ZTEST_RULE(fff_reset_rule, fff_reset_rule_before, NULL);
34 
35 ZTEST_SUITE(bt_le_oob_get_local, NULL, NULL, NULL, NULL, NULL);
36 
37 /*
38  *  Get LE local Out Of Band information while privacy isn't enabled
39  *
40  *  Constraints:
41  *   - Use a valid reference
42  *   - 'CONFIG_BT_PRIVACY' bit isn't enabled
43  *
44  *  Expected behaviour:
45  *   - Address is copied to the passed OOB reference
46  */
ZTEST(bt_le_oob_get_local,test_get_local_out_of_band_information_no_privacy)47 ZTEST(bt_le_oob_get_local, test_get_local_out_of_band_information_no_privacy)
48 {
49 	int err;
50 	struct bt_le_oob oob;
51 
52 	Z_TEST_SKIP_IFDEF(CONFIG_BT_PRIVACY);
53 
54 	atomic_set_bit(bt_dev.flags, BT_DEV_READY);
55 
56 	/* Not supported error should not affect the return value of bt_le_oob_get_local() */
57 	bt_smp_le_oob_generate_sc_data_fake.return_val = -ENOTSUP;
58 
59 	for (size_t i = 0; i < CONFIG_BT_ID_MAX; i++) {
60 
61 		SMP_FFF_FAKES_LIST(RESET_FAKE);
62 		memset(bt_dev.id_addr, 0x00, sizeof(bt_dev.id_addr));
63 		bt_addr_le_copy(&bt_dev.id_addr[i], BT_RPA_LE_ADDR);
64 
65 		err = bt_le_oob_get_local(i, &oob);
66 
67 		if (IS_ENABLED(CONFIG_BT_SMP)) {
68 			expect_single_call_bt_smp_le_oob_generate_sc_data(&oob.le_sc_data);
69 		}
70 
71 		zassert_ok(err, "Unexpected error code '%d' was returned", err);
72 		zassert_mem_equal(&oob.addr, BT_RPA_LE_ADDR, sizeof(bt_addr_le_t),
73 				  "Incorrect address was set");
74 	}
75 }
76 
77 /*
78  *  Get LE local Out Of Band information while privacy is enabled
79  *
80  *  Constraints:
81  *   - Use a valid reference
82  *   - 'CONFIG_BT_PRIVACY' bit is enabled
83  *
84  *  Expected behaviour:
85  *   - Address is copied to the passed OOB reference
86  */
ZTEST(bt_le_oob_get_local,test_get_local_out_of_band_information_privacy_enabled)87 ZTEST(bt_le_oob_get_local, test_get_local_out_of_band_information_privacy_enabled)
88 {
89 	int err;
90 	struct bt_le_oob oob;
91 
92 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_PRIVACY);
93 
94 	/* Not supported error should not affect the return value of bt_le_oob_get_local() */
95 	bt_smp_le_oob_generate_sc_data_fake.return_val = -ENOTSUP;
96 
97 	atomic_set_bit(bt_dev.flags, BT_DEV_READY);
98 	bt_addr_le_copy(&bt_dev.random_addr, BT_RPA_LE_ADDR);
99 
100 	err = bt_le_oob_get_local(BT_ID_DEFAULT, &oob);
101 
102 	if (IS_ENABLED(CONFIG_BT_SMP)) {
103 		expect_single_call_bt_smp_le_oob_generate_sc_data(&oob.le_sc_data);
104 	}
105 
106 	zassert_ok(err, "Unexpected error code '%d' was returned", err);
107 	zassert_mem_equal(&oob.addr, BT_RPA_LE_ADDR, sizeof(bt_addr_le_t),
108 			  "Incorrect address was set");
109 }
110