1 /*
2 * Copyright (c) 2022 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "mocks/hci_core.h"
8 #include "mocks/hci_core_expects.h"
9 #include "testing_common_defs.h"
10
11 #include <zephyr/bluetooth/hci.h>
12 #include <zephyr/fff.h>
13 #include <zephyr/kernel.h>
14
15 #include <host/hci_core.h>
16 #include <host/id.h>
17
18 DEFINE_FFF_GLOBALS;
19
20 /* Hold data representing HCI command response for command BT_HCI_OP_READ_BD_ADDR */
21 static struct net_buf hci_cmd_rsp;
22
23 /* Hold data representing response data for HCI command BT_HCI_OP_READ_BD_ADDR */
24 static struct bt_hci_rp_read_bd_addr hci_rp_read_bd_addr;
25
tc_setup(void * f)26 static void tc_setup(void *f)
27 {
28 memset(&bt_dev, 0x00, sizeof(struct bt_dev));
29 memset(&hci_cmd_rsp, 0x00, sizeof(struct net_buf));
30 memset(&hci_rp_read_bd_addr, 0x00, sizeof(struct bt_hci_rp_read_bd_addr));
31
32 HCI_CORE_FFF_FAKES_LIST(RESET_FAKE);
33 }
34
35 /* Default test cases to run under all configuration */
36 ZTEST_SUITE(bt_setup_public_id_addr_default, NULL, NULL, tc_setup, NULL, NULL);
37
38 /*
39 * Test reading controller public address fails
40 *
41 * Constraints:
42 * - bt_id_read_public_addr() returns zero
43 *
44 * Expected behaviour:
45 * - ID count is set to 0 and bt_setup_public_id_addr() returns 0
46 */
ZTEST(bt_setup_public_id_addr_default,test_bt_id_read_public_addr_returns_zero)47 ZTEST(bt_setup_public_id_addr_default, test_bt_id_read_public_addr_returns_zero)
48 {
49 int err;
50
51 /* This will force bt_id_read_public_addr() to fail */
52 bt_hci_cmd_send_sync_fake.return_val = 1;
53
54 err = bt_setup_public_id_addr();
55
56 zassert_true(bt_dev.id_count == 0, "Incorrect value '%d' was set to bt_dev.id_count",
57 bt_dev.id_count);
58 zassert_true(err == 0, "Unexpected error code '%d' was returned", err);
59 }
60
bt_hci_cmd_send_sync_custom_fake(uint16_t opcode,struct net_buf * buf,struct net_buf ** rsp)61 static int bt_hci_cmd_send_sync_custom_fake(uint16_t opcode, struct net_buf *buf,
62 struct net_buf **rsp)
63 {
64 const char *func_name = "bt_hci_cmd_send_sync";
65
66 zassert_equal(opcode, BT_HCI_OP_READ_BD_ADDR, "'%s()' was called with incorrect '%s' value",
67 func_name, "opcode");
68 zassert_is_null(buf, "'%s()' was called with incorrect '%s' value", func_name, "buf");
69 zassert_not_null(rsp, "'%s()' was called with incorrect '%s' value", func_name, "rsp");
70
71 *rsp = &hci_cmd_rsp;
72 hci_cmd_rsp.data = (void *)&hci_rp_read_bd_addr;
73
74 return 0;
75 }
76
77 /*
78 * Test reading controller public address through bt_hci_cmd_send_sync().
79 * bt_hci_cmd_send_sync() return value is success and response data contains a valid BT address.
80 *
81 * Constraints:
82 * - bt_hci_cmd_send_sync() returns zero
83 * - Response data contains a valid address
84 *
85 * Expected behaviour:
86 * - Return value is 0
87 * - Public address is loaded to bt_dev.id_addr[]
88 */
ZTEST(bt_setup_public_id_addr_default,test_bt_id_read_public_addr_returns_valid_id_count)89 ZTEST(bt_setup_public_id_addr_default, test_bt_id_read_public_addr_returns_valid_id_count)
90 {
91 int err;
92 struct bt_hci_rp_read_bd_addr *rp = (void *)&hci_rp_read_bd_addr;
93
94 bt_addr_copy(&rp->bdaddr, BT_ADDR);
95 bt_hci_cmd_send_sync_fake.custom_fake = bt_hci_cmd_send_sync_custom_fake;
96
97 err = bt_setup_public_id_addr();
98
99 zassert_true(err == 0, "Unexpected error code '%d' was returned", err);
100 zassert_mem_equal(&bt_dev.id_addr[BT_ID_DEFAULT], BT_LE_ADDR, sizeof(bt_addr_le_t),
101 "Incorrect address was set");
102 zassert_true(bt_dev.id_count == 1, "Incorrect value '%d' was set to bt_dev.id_count",
103 bt_dev.id_count);
104 }
105