Skip to content

Commit 765c23d

Browse files
PavelVPVrlubos
authored andcommitted
[nrf fromtree] tests: bluetooth: host: conn: Add bt_conn_le_create unit test
Add a test that checks behavior of CONFIG_BT_CONN_CHECK_NULL_BEFORE_CREATE Kconfig option. Signed-off-by: Pavel Vasilyev <[email protected]> (cherry picked from commit 1fbcc0e)
1 parent fd0492a commit 765c23d

32 files changed

+734
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr COMPONENTS unittest REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
7+
project(conn)
8+
9+
include_directories(BEFORE
10+
${ZEPHYR_BASE}/tests/bluetooth/host/conn
11+
${ZEPHYR_BASE}/tests/bluetooth/host/conn/mocks/zephyr/include
12+
)
13+
14+
add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/host host_mocks)
15+
add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/host/conn/mocks mocks)
16+
17+
target_link_libraries(testbinary PRIVATE mocks host_mocks)
18+
19+
target_sources(testbinary
20+
PRIVATE
21+
src/main.c
22+
23+
${ZEPHYR_BASE}/subsys/bluetooth/host/conn.c
24+
${ZEPHYR_BASE}/subsys/logging/log_minimal.c
25+
${ZEPHYR_BASE}/lib/net_buf/buf.c
26+
${ZEPHYR_BASE}/lib/net_buf/buf_simple.c
27+
)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#
2+
# CMakeLists.txt file for creating of mocks library.
3+
#
4+
5+
add_library(mocks STATIC
6+
addr_internal.c
7+
att_internal.c
8+
bt_str.c
9+
buf_view.c
10+
hci_core.c
11+
id.c
12+
kernel.c
13+
l2cap_internal.c
14+
scan.c
15+
smp.c
16+
spinlock.c
17+
sys_clock.c
18+
)
19+
20+
target_include_directories(mocks PUBLIC
21+
${ZEPHYR_BASE}/tests/bluetooth/host/conn/mocks
22+
${ZEPHYR_BASE}/subsys/bluetooth
23+
)
24+
25+
target_compile_options(test_interface INTERFACE -include ztest.h)
26+
target_link_libraries(mocks PRIVATE test_interface)
27+
target_link_options(mocks PUBLIC
28+
"SHELL:-T ${ZEPHYR_BASE}/tests/bluetooth/host/conn/mocks/mock-sections.ld"
29+
)
30+
31+
target_compile_options(mocks
32+
PRIVATE
33+
-DCONFIG_BT_ID_MAX=1
34+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
9+
#include "addr_internal.h"
10+
11+
DEFINE_FAKE_VOID_FUNC(bt_addr_le_copy_resolved, bt_addr_le_t *, const bt_addr_le_t *);
12+
DEFINE_FAKE_VALUE_FUNC(bool, bt_addr_le_is_resolved, const bt_addr_le_t *);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/fff.h>
9+
#include <zephyr/bluetooth/addr.h>
10+
11+
/* List of fakes used by this unit tester */
12+
#define ADDR_INTERNAL_MOCKS_FFF_FAKES_LIST(FAKE) \
13+
FAKE(bt_addr_le_copy_resolved) \
14+
FAKE(bt_addr_le_is_resolved)
15+
16+
DECLARE_FAKE_VOID_FUNC(bt_addr_le_copy_resolved, bt_addr_le_t *, const bt_addr_le_t *);
17+
DECLARE_FAKE_VALUE_FUNC(bool, bt_addr_le_is_resolved, const bt_addr_le_t *);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
9+
#include "att_internal.h"
10+
11+
DEFINE_FAKE_VOID_FUNC(bt_att_init);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/fff.h>
9+
10+
/* List of fakes used by this unit tester */
11+
#define ATT_INTERNAL_MOCKS_FFF_FAKES_LIST(FAKE) FAKE(bt_att_init)
12+
13+
DECLARE_FAKE_VOID_FUNC(bt_att_init);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/bluetooth/addr.h>
9+
10+
#include "bt_str.h"
11+
12+
const char *bt_addr_le_str(const bt_addr_le_t *addr)
13+
{
14+
static char str[BT_ADDR_LE_STR_LEN];
15+
16+
bt_addr_le_to_str(addr, str, sizeof(str));
17+
18+
return str;
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/bluetooth/addr.h>
9+
10+
const char *bt_addr_le_str(const bt_addr_le_t *addr);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
9+
#include "buf_view.h"
10+
11+
DEFINE_FAKE_VALUE_FUNC(bool, bt_buf_has_view, const struct net_buf *);
12+
DEFINE_FAKE_VALUE_FUNC(struct net_buf *, bt_buf_make_view, struct net_buf *, struct net_buf *,
13+
size_t, struct bt_buf_view_meta *);
14+
DEFINE_FAKE_VOID_FUNC(bt_buf_destroy_view, struct net_buf *, struct bt_buf_view_meta *);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/fff.h>
9+
10+
/* List of fakes used by this unit tester */
11+
#define BUF_VIEW_MOCKS_FFF_FAKES_LIST(FAKE) FAKE(bt_buf_has_view) \
12+
FAKE(bt_buf_make_view) \
13+
FAKE(bt_buf_destroy_view)
14+
15+
DECLARE_FAKE_VALUE_FUNC(bool, bt_buf_has_view, const struct net_buf *);
16+
DECLARE_FAKE_VALUE_FUNC(struct net_buf *, bt_buf_make_view, struct net_buf *, struct net_buf *,
17+
size_t, struct bt_buf_view_meta *);
18+
DECLARE_FAKE_VOID_FUNC(bt_buf_destroy_view, struct net_buf *, struct bt_buf_view_meta *);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/bluetooth/hci.h>
9+
10+
#include <host/hci_core.h>
11+
12+
#include "hci_core.h"
13+
14+
DEFINE_FAKE_VALUE_FUNC(struct net_buf *, bt_hci_cmd_create, uint16_t, uint8_t);
15+
DEFINE_FAKE_VALUE_FUNC(int, bt_hci_cmd_send_sync, uint16_t, struct net_buf *, struct net_buf **);
16+
DEFINE_FAKE_VALUE_FUNC(int, bt_hci_le_read_remote_features, struct bt_conn *);
17+
DEFINE_FAKE_VALUE_FUNC(int, bt_hci_disconnect, uint16_t, uint8_t);
18+
DEFINE_FAKE_VALUE_FUNC(bool, bt_le_conn_params_valid, const struct bt_le_conn_param *);
19+
DEFINE_FAKE_VOID_FUNC(bt_tx_irq_raise);
20+
DEFINE_FAKE_VALUE_FUNC(int, bt_send, struct net_buf *);
21+
DEFINE_FAKE_VOID_FUNC(bt_send_one_host_num_completed_packets, uint16_t);
22+
DEFINE_FAKE_VOID_FUNC(bt_acl_set_ncp_sent, struct net_buf *, bool);
23+
DEFINE_FAKE_VALUE_FUNC(int, bt_le_create_conn, const struct bt_conn *);
24+
DEFINE_FAKE_VALUE_FUNC(int, bt_le_create_conn_cancel);
25+
DEFINE_FAKE_VALUE_FUNC(int, bt_le_create_conn_synced, const struct bt_conn *,
26+
const struct bt_le_ext_adv *, uint8_t);
27+
DEFINE_FAKE_VALUE_FUNC(const bt_addr_le_t *, bt_lookup_id_addr, uint8_t, const bt_addr_le_t *);
28+
DEFINE_FAKE_VALUE_FUNC(int, bt_le_set_phy, struct bt_conn *, uint8_t, uint8_t, uint8_t, uint8_t);
29+
30+
struct bt_dev bt_dev = {
31+
.manufacturer = 0x1234,
32+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/fff.h>
9+
10+
/* List of fakes used by this unit tester */
11+
#define HCI_CORE_MOCKS_FFF_FAKES_LIST(FAKE) \
12+
FAKE(bt_hci_cmd_create) \
13+
FAKE(bt_hci_cmd_send_sync) \
14+
FAKE(bt_hci_le_read_remote_features) \
15+
FAKE(bt_hci_disconnect) \
16+
FAKE(bt_le_conn_params_valid) \
17+
FAKE(bt_tx_irq_raise) \
18+
FAKE(bt_send) \
19+
FAKE(bt_send_one_host_num_completed_packets) \
20+
FAKE(bt_acl_set_ncp_sent) \
21+
FAKE(bt_le_create_conn) \
22+
FAKE(bt_le_create_conn_cancel) \
23+
FAKE(bt_le_create_conn_synced) \
24+
FAKE(bt_lookup_id_addr) \
25+
FAKE(bt_le_set_phy)
26+
27+
DECLARE_FAKE_VALUE_FUNC(struct net_buf *, bt_hci_cmd_create, uint16_t, uint8_t);
28+
DECLARE_FAKE_VALUE_FUNC(int, bt_hci_cmd_send_sync, uint16_t, struct net_buf *, struct net_buf **);
29+
DECLARE_FAKE_VALUE_FUNC(int, bt_hci_le_read_remote_features, struct bt_conn *);
30+
DECLARE_FAKE_VALUE_FUNC(int, bt_hci_disconnect, uint16_t, uint8_t);
31+
DECLARE_FAKE_VALUE_FUNC(bool, bt_le_conn_params_valid, const struct bt_le_conn_param *);
32+
DECLARE_FAKE_VOID_FUNC(bt_tx_irq_raise);
33+
DECLARE_FAKE_VALUE_FUNC(int, bt_send, struct net_buf *);
34+
DECLARE_FAKE_VOID_FUNC(bt_send_one_host_num_completed_packets, uint16_t);
35+
DECLARE_FAKE_VOID_FUNC(bt_acl_set_ncp_sent, struct net_buf *, bool);
36+
DECLARE_FAKE_VALUE_FUNC(int, bt_le_create_conn, const struct bt_conn *);
37+
DECLARE_FAKE_VALUE_FUNC(int, bt_le_create_conn_cancel);
38+
DECLARE_FAKE_VALUE_FUNC(int, bt_le_create_conn_synced, const struct bt_conn *,
39+
const struct bt_le_ext_adv *, uint8_t);
40+
DECLARE_FAKE_VALUE_FUNC(const bt_addr_le_t *, bt_lookup_id_addr, uint8_t, const bt_addr_le_t *);
41+
DECLARE_FAKE_VALUE_FUNC(int, bt_le_set_phy, struct bt_conn *, uint8_t, uint8_t, uint8_t, uint8_t);

tests/bluetooth/host/conn/mocks/id.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
9+
#include "id.h"
10+
11+
DEFINE_FAKE_VALUE_FUNC(bool, bt_id_scan_random_addr_check);

tests/bluetooth/host/conn/mocks/id.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/fff.h>
9+
10+
/* List of fakes used by this unit tester */
11+
#define ID_MOCKS_FFF_FAKES_LIST(FAKE) FAKE(bt_id_scan_random_addr_check)
12+
13+
DECLARE_FAKE_VALUE_FUNC(bool, bt_id_scan_random_addr_check);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
9+
#include "kernel.h"
10+
11+
DEFINE_FAKE_VALUE_FUNC(bool, k_is_in_isr);
12+
DEFINE_FAKE_VALUE_FUNC(int, k_poll_signal_raise, struct k_poll_signal *, int);
13+
DEFINE_FAKE_VALUE_FUNC(int, k_sem_take, struct k_sem *, k_timeout_t);
14+
DEFINE_FAKE_VALUE_FUNC(unsigned int, k_sem_count_get, struct k_sem *);
15+
DEFINE_FAKE_VOID_FUNC(k_sem_give, struct k_sem *);
16+
DEFINE_FAKE_VALUE_FUNC(k_tid_t, k_sched_current_thread_query);
17+
DEFINE_FAKE_VOID_FUNC(k_work_init, struct k_work *, k_work_handler_t);
18+
DEFINE_FAKE_VOID_FUNC(k_work_init_delayable, struct k_work_delayable *, k_work_handler_t);
19+
DEFINE_FAKE_VALUE_FUNC(int, k_work_cancel_delayable, struct k_work_delayable *);
20+
DEFINE_FAKE_VALUE_FUNC(bool, k_work_flush, struct k_work *, struct k_work_sync *);
21+
DEFINE_FAKE_VALUE_FUNC(int, k_work_submit, struct k_work *);
22+
DEFINE_FAKE_VALUE_FUNC(int, k_work_submit_to_queue, struct k_work_q *, struct k_work *);
23+
DEFINE_FAKE_VALUE_FUNC(int, k_work_reschedule, struct k_work_delayable *, k_timeout_t);
24+
DEFINE_FAKE_VALUE_FUNC(int, k_work_schedule, struct k_work_delayable *, k_timeout_t);
25+
DEFINE_FAKE_VOID_FUNC(k_queue_init, struct k_queue *);
26+
DEFINE_FAKE_VOID_FUNC(k_queue_append, struct k_queue *, void *);
27+
DEFINE_FAKE_VALUE_FUNC(int, k_queue_is_empty, struct k_queue *);
28+
DEFINE_FAKE_VALUE_FUNC(void *, k_queue_get, struct k_queue *, k_timeout_t);
29+
DEFINE_FAKE_VOID_FUNC(k_queue_prepend, struct k_queue *, void *);
30+
DEFINE_FAKE_VALUE_FUNC(void *, k_heap_alloc, struct k_heap *, size_t, k_timeout_t);
31+
DEFINE_FAKE_VOID_FUNC(k_heap_free, struct k_heap *, void *);
32+
33+
struct k_work_q k_sys_work_q;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/fff.h>
9+
10+
/* List of fakes used by this unit tester */
11+
#define KERNEL_MOCKS_FFF_FAKES_LIST(FAKE) \
12+
FAKE(k_is_in_isr) \
13+
FAKE(k_poll_signal_raise) \
14+
FAKE(k_sem_take) \
15+
FAKE(k_sem_count_get) \
16+
FAKE(k_sem_give) \
17+
FAKE(k_sched_current_thread_query) \
18+
FAKE(k_work_init) \
19+
FAKE(k_work_init_delayable) \
20+
FAKE(k_work_cancel_delayable) \
21+
FAKE(k_work_flush) \
22+
FAKE(k_work_submit) \
23+
FAKE(k_work_submit_to_queue) \
24+
FAKE(k_work_reschedule) \
25+
FAKE(k_work_schedule) \
26+
FAKE(k_queue_init) \
27+
FAKE(k_queue_append) \
28+
FAKE(k_queue_is_empty) \
29+
FAKE(k_queue_get) \
30+
FAKE(k_queue_prepend) \
31+
FAKE(k_heap_alloc) \
32+
FAKE(k_heap_free)
33+
34+
DECLARE_FAKE_VALUE_FUNC(bool, k_is_in_isr);
35+
DECLARE_FAKE_VALUE_FUNC(int, k_poll_signal_raise, struct k_poll_signal *, int);
36+
DECLARE_FAKE_VALUE_FUNC(int, k_sem_take, struct k_sem *, k_timeout_t);
37+
DECLARE_FAKE_VALUE_FUNC(unsigned int, k_sem_count_get, struct k_sem *);
38+
DECLARE_FAKE_VOID_FUNC(k_sem_give, struct k_sem *);
39+
DECLARE_FAKE_VALUE_FUNC(k_tid_t, k_sched_current_thread_query);
40+
DECLARE_FAKE_VOID_FUNC(k_work_init, struct k_work *, k_work_handler_t);
41+
DECLARE_FAKE_VOID_FUNC(k_work_init_delayable, struct k_work_delayable *, k_work_handler_t);
42+
DECLARE_FAKE_VALUE_FUNC(int, k_work_cancel_delayable, struct k_work_delayable *);
43+
DECLARE_FAKE_VALUE_FUNC(bool, k_work_flush, struct k_work *, struct k_work_sync *);
44+
DECLARE_FAKE_VALUE_FUNC(int, k_work_submit, struct k_work *);
45+
DECLARE_FAKE_VALUE_FUNC(int, k_work_submit_to_queue, struct k_work_q *, struct k_work *);
46+
DECLARE_FAKE_VALUE_FUNC(int, k_work_reschedule, struct k_work_delayable *, k_timeout_t);
47+
DECLARE_FAKE_VALUE_FUNC(int, k_work_schedule, struct k_work_delayable *, k_timeout_t);
48+
DECLARE_FAKE_VOID_FUNC(k_queue_init, struct k_queue *);
49+
DECLARE_FAKE_VOID_FUNC(k_queue_append, struct k_queue *, void *);
50+
DECLARE_FAKE_VALUE_FUNC(int, k_queue_is_empty, struct k_queue *);
51+
DECLARE_FAKE_VALUE_FUNC(void *, k_queue_get, struct k_queue *, k_timeout_t);
52+
DECLARE_FAKE_VOID_FUNC(k_queue_prepend, struct k_queue *, void *);
53+
DECLARE_FAKE_VALUE_FUNC(void *, k_heap_alloc, struct k_heap *, size_t, k_timeout_t);
54+
DECLARE_FAKE_VOID_FUNC(k_heap_free, struct k_heap *, void *);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
9+
#include "l2cap_internal.h"
10+
11+
DEFINE_FAKE_VOID_FUNC(bt_l2cap_init);
12+
DEFINE_FAKE_VOID_FUNC(bt_l2cap_recv, struct bt_conn *, struct net_buf *, bool);
13+
DEFINE_FAKE_VOID_FUNC(bt_l2cap_connected, struct bt_conn *);
14+
DEFINE_FAKE_VALUE_FUNC(int, bt_l2cap_update_conn_param, struct bt_conn *,
15+
const struct bt_le_conn_param *);
16+
DEFINE_FAKE_VOID_FUNC(bt_l2cap_disconnected, struct bt_conn *);
17+
DEFINE_FAKE_VALUE_FUNC(struct net_buf *, l2cap_data_pull, struct bt_conn *, size_t, size_t *);

0 commit comments

Comments
 (0)