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/hci_core.h"
9 #include "mocks/kernel.h"
10 #include "mocks/kernel_expects.h"
11 #include "mocks/smp.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 
27 	KERNEL_FFF_FAKES_LIST(RESET_FAKE);
28 	SMP_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_init, NULL, NULL, NULL, NULL, NULL);
36 
37 /*
38  *  Test initializing the device identity by calling bt_id_init() while the device identity count
39  *  bt_dev.id_count isn't 0.
40  *
41  *  Constraints:
42  *   - bt_dev.id_count is set to value greater than 0
43  *
44  *  Expected behaviour:
45  *   - bt_id_init() returns 0 and identity count isn't changed
46  */
ZTEST(bt_id_init,test_init_dev_identity_while_valid_identities_exist)47 ZTEST(bt_id_init, test_init_dev_identity_while_valid_identities_exist)
48 {
49 	int err;
50 
51 	bt_dev.id_count = 1;
52 
53 	err = bt_id_init();
54 
55 	zassert_ok(err, "Unexpected error code '%d' was returned", err);
56 	zassert_true(bt_dev.id_count == 1, "Incorrect value '%d' was set to bt_dev.id_count",
57 		     bt_dev.id_count);
58 
59 #if defined(CONFIG_BT_PRIVACY)
60 	expect_single_call_k_work_init_delayable(&bt_dev.rpa_update);
61 #endif
62 }
63 
64 /*
65  *  Test initializing the device identity by calling bt_id_init() while the device identity count
66  *  bt_dev.id_count is set to 0 and 'CONFIG_BT_SETTINGS' is enabled.
67  *
68  *  Constraints:
69  *   - bt_dev.id_count is set 0
70  *   - 'CONFIG_BT_SETTINGS' is enabled
71  *
72  *  Expected behaviour:
73  *   - bt_id_init() returns 0 and identity count isn't changed
74  */
ZTEST(bt_id_init,test_init_dev_identity_while_bt_settings_enabled)75 ZTEST(bt_id_init, test_init_dev_identity_while_bt_settings_enabled)
76 {
77 	int err;
78 
79 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_SETTINGS);
80 
81 	err = bt_id_init();
82 
83 	zassert_ok(err, "Unexpected error code '%d' was returned", err);
84 	zassert_true(bt_dev.id_count == 0, "Incorrect value '%d' was set to bt_dev.id_count",
85 		     bt_dev.id_count);
86 
87 #if defined(CONFIG_BT_PRIVACY)
88 	expect_single_call_k_work_init_delayable(&bt_dev.rpa_update);
89 #endif
90 }
91