Skip to content

Commit 3c76bd0

Browse files
ahmedmoheb-nordiccarlescufi
authored andcommitted
tests: bluetooth: host: Add mocks for ecc.c
Add required mocks to be able to compile/test /bluetooth/host/ecc.c Signed-off-by: Ahmed Moheb <[email protected]>
1 parent 1dce3c3 commit 3c76bd0

File tree

11 files changed

+193
-0
lines changed

11 files changed

+193
-0
lines changed

subsys/bluetooth/host/ecc.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <stdint.h>
99

10+
#include <zephyr/sys/check.h>
1011
#include <zephyr/bluetooth/hci.h>
1112

1213
#include "ecc.h"
@@ -236,3 +237,25 @@ void bt_hci_evt_le_dhkey_complete(struct net_buf *buf)
236237
cb(evt->status ? NULL : evt->dhkey);
237238
}
238239
}
240+
241+
#ifdef ZTEST_UNITTEST
242+
uint8_t const *bt_ecc_get_public_key(void)
243+
{
244+
return pub_key;
245+
}
246+
247+
uint8_t const *bt_ecc_get_internal_debug_public_key(void)
248+
{
249+
return debug_public_key;
250+
}
251+
252+
sys_slist_t *bt_ecc_get_pub_key_cb_slist(void)
253+
{
254+
return &pub_key_cb_slist;
255+
}
256+
257+
bt_dh_key_cb_t *bt_ecc_get_dh_key_cb(void)
258+
{
259+
return &dh_key_cb;
260+
}
261+
#endif /* ZTEST_UNITTEST */
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#
2+
# CMakeLists.txt file for creating of mocks library.
3+
#
4+
5+
add_library(mocks STATIC
6+
mocks/net_buf.c
7+
mocks/net_buf_expects.c
8+
mocks/hci_core.c
9+
mocks/ecc_help_utils.c
10+
11+
${ZEPHYR_BASE}/subsys/bluetooth/host/ecc.c
12+
${ZEPHYR_BASE}/subsys/logging/log_minimal.c
13+
)
14+
15+
target_include_directories(mocks PUBLIC
16+
.
17+
${ZEPHYR_BASE}/subsys/bluetooth
18+
${ZEPHYR_BASE}/subsys/bluetooth/host
19+
${ZEPHYR_BASE}/tests/bluetooth/host
20+
${ZEPHYR_BASE}/tests/bluetooth/host/ecc/mocks
21+
)
22+
23+
target_link_libraries(mocks PRIVATE test_interface)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright (c) 2022 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "ecc_help_utils.h"
8+
9+
#include <zephyr/kernel.h>
10+
11+
#include <host/ecc.h>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (c) 2022 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/sys/slist.h>
8+
9+
typedef void (*bt_dh_key_cb_t)(const uint8_t *key);
10+
11+
/* ecc.c declarations */
12+
uint8_t const *bt_ecc_get_public_key(void);
13+
uint8_t const *bt_ecc_get_internal_debug_public_key(void);
14+
sys_slist_t *bt_ecc_get_pub_key_cb_slist(void);
15+
bt_dh_key_cb_t *bt_ecc_get_dh_key_cb(void);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
9+
#include <zephyr/bluetooth/hci.h>
10+
#include <zephyr/kernel.h>
11+
12+
#include <host/hci_core.h>
13+
14+
struct bt_dev bt_dev = {
15+
.manufacturer = 0x1234,
16+
};
17+
18+
DEFINE_FAKE_VALUE_FUNC(struct net_buf *, bt_hci_cmd_create, uint16_t, uint8_t);
19+
DEFINE_FAKE_VALUE_FUNC(int, bt_hci_cmd_send_sync, uint16_t, struct net_buf *, struct net_buf **);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) 2022 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/fff.h>
8+
#include <zephyr/kernel.h>
9+
10+
/* List of fakes used by this unit tester */
11+
#define HCI_CORE_FFF_FAKES_LIST(FAKE) \
12+
FAKE(bt_hci_cmd_create) \
13+
FAKE(bt_hci_cmd_send_sync)
14+
15+
DECLARE_FAKE_VALUE_FUNC(struct net_buf *, bt_hci_cmd_create, uint16_t, uint8_t);
16+
DECLARE_FAKE_VALUE_FUNC(int, bt_hci_cmd_send_sync, uint16_t, struct net_buf *, struct net_buf **);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) 2022 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/bluetooth/buf.h>
8+
#include <zephyr/kernel.h>
9+
#include <zephyr/net/buf.h>
10+
11+
#include <mocks/net_buf.h>
12+
13+
DEFINE_FAKE_VALUE_FUNC(void *, net_buf_simple_add, struct net_buf_simple *, size_t);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) 2022 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/fff.h>
8+
#include <zephyr/kernel.h>
9+
10+
/* List of fakes used by this unit tester */
11+
#define NET_BUF_FFF_FAKES_LIST(FAKE) FAKE(net_buf_simple_add)
12+
13+
DECLARE_FAKE_VALUE_FUNC(void *, net_buf_simple_add, struct net_buf_simple *, size_t);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2022 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "mocks/net_buf.h"
8+
#include "mocks/net_buf_expects.h"
9+
10+
#include <zephyr/bluetooth/buf.h>
11+
#include <zephyr/kernel.h>
12+
13+
void expect_single_call_net_buf_simple_add(struct net_buf_simple *buf, size_t len)
14+
{
15+
const char *func_name = "net_buf_simple_add";
16+
17+
zassert_equal(net_buf_simple_add_fake.call_count, 1, "'%s()' was called more than once",
18+
func_name);
19+
20+
zassert_equal(net_buf_simple_add_fake.arg0_val, buf,
21+
"'%s()' was called with incorrect '%s' value", func_name, "buf");
22+
zassert_equal(net_buf_simple_add_fake.arg1_val, len,
23+
"'%s()' was called with incorrect '%s' value", func_name, "len");
24+
}
25+
26+
void expect_not_called_net_buf_simple_add(void)
27+
{
28+
const char *func_name = "net_buf_simple_add";
29+
30+
zassert_equal(net_buf_simple_add_fake.call_count, 0, "'%s()' was called unexpectedly",
31+
func_name);
32+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2022 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
9+
/*
10+
* Validate expected behaviour when net_buf_simple_add() is called
11+
*
12+
* Expected behaviour:
13+
* - net_buf_simple_add() to be called once with correct parameters
14+
*/
15+
void expect_single_call_net_buf_simple_add(struct net_buf_simple *buf, size_t len);
16+
17+
/*
18+
* Validate expected behaviour when net_buf_simple_add() isn't called
19+
*
20+
* Expected behaviour:
21+
* - net_buf_simple_add() isn't called at all
22+
*/
23+
void expect_not_called_net_buf_simple_add(void);

0 commit comments

Comments
 (0)