Skip to content

Commit aebd5d8

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

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed
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_get)
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: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
9+
#include <zephyr/bluetooth/hci.h>
10+
#include <zephyr/fff.h>
11+
#include <zephyr/kernel.h>
12+
13+
#include <host/ecc.h>
14+
#include <host/hci_core.h>
15+
16+
DEFINE_FFF_GLOBALS;
17+
18+
static void fff_reset_rule_before(const struct ztest_unit_test *test, void *fixture)
19+
{
20+
memset(&bt_dev, 0x00, sizeof(struct bt_dev));
21+
}
22+
23+
ZTEST_RULE(fff_reset_rule, fff_reset_rule_before, NULL);
24+
25+
ZTEST_SUITE(bt_pub_key_get, NULL, NULL, NULL, NULL, NULL);
26+
27+
/*
28+
* Test getting currently used public key if 'BT_DEV_HAS_PUB_KEY' is set and
29+
* 'CONFIG_BT_USE_DEBUG_KEYS' isn't enabled
30+
*
31+
* Constraints:
32+
* - 'BT_DEV_HAS_PUB_KEY' flag is set
33+
* - 'CONFIG_BT_USE_DEBUG_KEYS' isn't enabled
34+
*
35+
* Expected behaviour:
36+
* - A valid reference value is returned
37+
*/
38+
ZTEST(bt_pub_key_get, test_bt_dev_has_pub_key_set)
39+
{
40+
const uint8_t *pub_key;
41+
42+
Z_TEST_SKIP_IFDEF(CONFIG_BT_USE_DEBUG_KEYS);
43+
44+
atomic_set_bit(bt_dev.flags, BT_DEV_HAS_PUB_KEY);
45+
46+
pub_key = bt_pub_key_get();
47+
48+
zassert_equal(pub_key, bt_ecc_get_public_key(), "Incorrect reference was returned");
49+
}
50+
51+
/*
52+
* Test getting currently used debug public key if 'CONFIG_BT_USE_DEBUG_KEYS' is enabled and
53+
* "ECC Debug Keys" command is supported
54+
*
55+
* Constraints:
56+
* - 'CONFIG_BT_USE_DEBUG_KEYS' is enabled
57+
* - "ECC Debug Keys" command is supported
58+
* - 'BT_DEV_HAS_PUB_KEY' flag is set (just for testing and it shouldn't affect the result)
59+
*
60+
* Expected behaviour:
61+
* - A valid reference value is returned
62+
*/
63+
ZTEST(bt_pub_key_get, test_get_debug_pub_key1)
64+
{
65+
const uint8_t *pub_key;
66+
67+
Z_TEST_SKIP_IFNDEF(CONFIG_BT_USE_DEBUG_KEYS);
68+
69+
/* Set "ECC Debug Keys" command support bit */
70+
bt_dev.supported_commands[41] |= BIT(2);
71+
atomic_set_bit(bt_dev.flags, BT_DEV_HAS_PUB_KEY);
72+
73+
pub_key = bt_pub_key_get();
74+
75+
zassert_equal(pub_key, bt_ecc_get_internal_debug_public_key(),
76+
"Incorrect reference was returned");
77+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2022 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/bluetooth/hci.h>
8+
#include <zephyr/kernel.h>
9+
10+
#include <host/ecc.h>
11+
#include <host/hci_core.h>
12+
13+
ZTEST_SUITE(bt_pub_key_get_invalid_cases, NULL, NULL, NULL, NULL, NULL);
14+
15+
/*
16+
* Test getting currently used public key if 'BT_DEV_HAS_PUB_KEY' isn't set and
17+
* 'CONFIG_BT_USE_DEBUG_KEYS' isn't enabled
18+
*
19+
* Constraints:
20+
* - 'BT_DEV_HAS_PUB_KEY' flag isn't set
21+
* - 'CONFIG_BT_USE_DEBUG_KEYS' isn't enabled
22+
*
23+
* Expected behaviour:
24+
* - NULL value is returned
25+
*/
26+
ZTEST(bt_pub_key_get_invalid_cases, test_bt_dev_has_pub_key_not_set)
27+
{
28+
const uint8_t *pub_key;
29+
30+
Z_TEST_SKIP_IFDEF(CONFIG_BT_USE_DEBUG_KEYS);
31+
32+
atomic_clear_bit(bt_dev.flags, BT_DEV_HAS_PUB_KEY);
33+
34+
pub_key = bt_pub_key_get();
35+
36+
zassert_is_null(pub_key, "Incorrect reference was returned");
37+
}
38+
39+
/*
40+
* Test getting currently used debug public key if 'CONFIG_BT_USE_DEBUG_KEYS' is enabled, but
41+
* "ECC Debug Keys" command isn't supported
42+
*
43+
* Constraints:
44+
* - 'CONFIG_BT_USE_DEBUG_KEYS' is enabled
45+
* - "ECC Debug Keys" command isn't supported
46+
* - 'BT_DEV_HAS_PUB_KEY' flag isn't set
47+
*
48+
* Expected behaviour:
49+
* - NULL value is returned
50+
*/
51+
ZTEST(bt_pub_key_get_invalid_cases, test_get_debug_pub_key)
52+
{
53+
const uint8_t *pub_key;
54+
55+
Z_TEST_SKIP_IFNDEF(CONFIG_BT_USE_DEBUG_KEYS);
56+
57+
/* Clear "ECC Debug Keys" command support bit */
58+
bt_dev.supported_commands[41] &= ~BIT(2);
59+
atomic_clear_bit(bt_dev.flags, BT_DEV_HAS_PUB_KEY);
60+
61+
pub_key = bt_pub_key_get();
62+
63+
zassert_is_null(pub_key, "Incorrect reference was returned");
64+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
common:
2+
tags: test_framework bluetooth host
3+
tests:
4+
bluetooth.host.bt_pub_key_get.default:
5+
type: unit
6+
bluetooth.host.bt_pub_key_get.bt_use_debug_keys_enabled:
7+
type: unit
8+
extra_configs:
9+
- CONFIG_BT_SMP=y
10+
- CONFIG_BT_USE_DEBUG_KEYS=y

0 commit comments

Comments
 (0)