Skip to content

Commit fac21b3

Browse files
ahmedmoheb-nordiccarlescufi
authored andcommitted
tests: bluetooth: host: Add UT for bt_pub_key_gen()
Unit test project for bt_pub_key_gen(). This is part of subsys/bluetooth/host/ecc.c unit testing. Signed-off-by: Ahmed Moheb <[email protected]>
1 parent a774314 commit fac21b3

File tree

9 files changed

+485
-0
lines changed

9 files changed

+485
-0
lines changed

subsys/bluetooth/host/ecc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ int bt_pub_key_gen(struct bt_pub_key_cb *new_cb)
6161
LOG_WRN("ECC Debug keys HCI command not available");
6262
} else {
6363
atomic_set_bit(bt_dev.flags, BT_DEV_HAS_PUB_KEY);
64+
__ASSERT_NO_MSG(new_cb->func != NULL);
6465
new_cb->func(debug_public_key);
6566
return 0;
6667
}

tests/bluetooth/host/ecc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ add_library(mocks STATIC
66
mocks/net_buf.c
77
mocks/net_buf_expects.c
88
mocks/hci_core.c
9+
mocks/hci_core_expects.c
910
mocks/ecc_help_utils.c
1011

1112
${ZEPHYR_BASE}/subsys/bluetooth/host/ecc.c
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
set(SOURCES
6+
src/main.c
7+
src/test_suite_invalid_inputs.c
8+
)
9+
10+
project(bt_pub_key_gen)
11+
12+
find_package(Zephyr COMPONENTS unittest HINTS $ENV{ZEPHYR_BASE})
13+
14+
add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/host host_mocks)
15+
add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/host/ecc mocks)
16+
17+
target_link_libraries(testbinary PRIVATE mocks host_mocks)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_ZTEST_NEW_API=y
3+
CONFIG_BT=y
4+
CONFIG_BT_CENTRAL=y
5+
CONFIG_BT_MAX_PAIRED=7
6+
CONFIG_ASSERT=y
7+
CONFIG_ASSERT_LEVEL=2
8+
CONFIG_ASSERT_VERBOSE=y
9+
CONFIG_ASSERT_ON_ERRORS=y
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/*
2+
* Copyright (c) 2022 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "mocks/ecc_help_utils.h"
8+
#include "mocks/hci_core.h"
9+
#include "mocks/hci_core_expects.h"
10+
11+
#include <zephyr/bluetooth/hci.h>
12+
#include <zephyr/fff.h>
13+
#include <zephyr/kernel.h>
14+
15+
#include <host/ecc.h>
16+
#include <host/hci_core.h>
17+
18+
DEFINE_FFF_GLOBALS;
19+
20+
static void fff_reset_rule_before(const struct ztest_unit_test *test, void *fixture)
21+
{
22+
sys_slist_t *pub_key_cb_slist = bt_ecc_get_pub_key_cb_slist();
23+
24+
memset(&bt_dev, 0x00, sizeof(struct bt_dev));
25+
sys_slist_init(pub_key_cb_slist);
26+
27+
HCI_CORE_FFF_FAKES_LIST(RESET_FAKE);
28+
}
29+
30+
ZTEST_RULE(fff_reset_rule, fff_reset_rule_before, NULL);
31+
32+
ZTEST_SUITE(bt_pub_key_gen, NULL, NULL, NULL, NULL, NULL);
33+
34+
static void bt_pub_key_gen_debug_key_callback(const uint8_t key[BT_PUB_KEY_LEN])
35+
{
36+
uint8_t const *internal_dbg_key = bt_ecc_get_internal_debug_public_key();
37+
const char *func_name = "bt_pub_key_gen_null_key_callback";
38+
39+
zassert_equal_ptr(key, internal_dbg_key, "'%s()' was called with incorrect '%s' value",
40+
func_name, "key");
41+
}
42+
43+
/*
44+
* Test using the internal debug public key
45+
*
46+
* Constraints:
47+
* - "LE Read Local P-256 Public Key" command is supported
48+
* - "LE Generate DH Key" command is supported
49+
* - "ECC Debug Keys" command is supported
50+
* - 'CONFIG_BT_USE_DEBUG_KEYS' is enabled
51+
*
52+
* Expected behaviour:
53+
* - bt_pub_key_gen() returns 0 (success)
54+
*/
55+
ZTEST(bt_pub_key_gen, test_using_internal_debug_public_key)
56+
{
57+
int result;
58+
bool flags_check;
59+
struct bt_pub_key_cb new_cb = {0};
60+
61+
Z_TEST_SKIP_IFNDEF(CONFIG_BT_USE_DEBUG_KEYS);
62+
63+
new_cb.func = bt_pub_key_gen_debug_key_callback;
64+
65+
/* Set "LE Read Local P-256 Public Key" command support bit */
66+
bt_dev.supported_commands[34] |= BIT(1);
67+
/* Set "LE Generate DH Key" command support bit */
68+
bt_dev.supported_commands[34] |= BIT(2);
69+
/* Set "ECC Debug Keys" command support bit */
70+
bt_dev.supported_commands[41] |= BIT(2);
71+
72+
atomic_clear_bit(bt_dev.flags, BT_DEV_HAS_PUB_KEY);
73+
bt_hci_cmd_send_sync_fake.return_val = 0;
74+
75+
result = bt_pub_key_gen(&new_cb);
76+
77+
expect_not_called_bt_hci_cmd_send_sync();
78+
79+
zassert_ok(result, "Unexpected error code '%d' was returned", result);
80+
81+
flags_check = atomic_test_bit(bt_dev.flags, BT_DEV_HAS_PUB_KEY);
82+
zassert_true(flags_check, "Flags were not correctly set");
83+
}
84+
85+
static void bt_pub_key_gen_callback(const uint8_t key[BT_PUB_KEY_LEN])
86+
{
87+
zassert_unreachable("Unexpected call to '%s()' occurred", __func__);
88+
}
89+
90+
/*
91+
* Test generating a public key request
92+
*
93+
* Constraints:
94+
* - "LE Read Local P-256 Public Key" command is supported
95+
* - "LE Generate DH Key" command is supported
96+
* - bt_hci_cmd_send_sync() succeeds and returns 0
97+
*
98+
* Expected behaviour:
99+
* - bt_pub_key_gen() returns 0 (success)
100+
*/
101+
ZTEST(bt_pub_key_gen, test_public_key_generation_request_passes)
102+
{
103+
int result;
104+
bool flags_check;
105+
struct bt_pub_key_cb new_cb = {0};
106+
107+
new_cb.func = bt_pub_key_gen_callback;
108+
109+
/* Set "LE Read Local P-256 Public Key" command support bit */
110+
bt_dev.supported_commands[34] |= BIT(1);
111+
/* Set "LE Generate DH Key" command support bit */
112+
bt_dev.supported_commands[34] |= BIT(2);
113+
114+
atomic_set_bit(bt_dev.flags, BT_DEV_HAS_PUB_KEY);
115+
bt_hci_cmd_send_sync_fake.return_val = 0;
116+
117+
result = bt_pub_key_gen(&new_cb);
118+
119+
expect_single_call_bt_hci_cmd_send_sync(BT_HCI_OP_LE_P256_PUBLIC_KEY);
120+
121+
zassert_ok(result, "Unexpected error code '%d' was returned", result);
122+
123+
flags_check = atomic_test_bit(bt_dev.flags, BT_DEV_HAS_PUB_KEY);
124+
zassert_false(flags_check, "Flags were not correctly set");
125+
126+
flags_check = atomic_test_bit(bt_dev.flags, BT_DEV_PUB_KEY_BUSY);
127+
zassert_true(flags_check, "Flags were not correctly set");
128+
}
129+
130+
/*
131+
* Test generating a public key request while 'BT_DEV_PUB_KEY_BUSY' flag is set
132+
*
133+
* Constraints:
134+
* - "LE Read Local P-256 Public Key" command is supported
135+
* - "LE Generate DH Key" command is supported
136+
* - bt_hci_cmd_send_sync() isn't called
137+
*
138+
* Expected behaviour:
139+
* - bt_pub_key_gen() returns 0 (success)
140+
*/
141+
ZTEST(bt_pub_key_gen, test_no_public_key_generation_request_duplication)
142+
{
143+
int result;
144+
bool flags_check;
145+
struct bt_pub_key_cb new_cb = {0};
146+
147+
new_cb.func = bt_pub_key_gen_callback;
148+
149+
/* Set "LE Read Local P-256 Public Key" command support bit */
150+
bt_dev.supported_commands[34] |= BIT(1);
151+
/* Set "LE Generate DH Key" command support bit */
152+
bt_dev.supported_commands[34] |= BIT(2);
153+
154+
atomic_set_bit(bt_dev.flags, BT_DEV_PUB_KEY_BUSY);
155+
156+
result = bt_pub_key_gen(&new_cb);
157+
158+
expect_not_called_bt_hci_cmd_send_sync();
159+
160+
zassert_ok(result, "Unexpected error code '%d' was returned", result);
161+
162+
flags_check = atomic_test_bit(bt_dev.flags, BT_DEV_PUB_KEY_BUSY);
163+
zassert_true(flags_check, "Flags were not correctly set");
164+
}

0 commit comments

Comments
 (0)