From 4b195b8748d69ef9b21d7c537aa5f13d0ec9ebd3 Mon Sep 17 00:00:00 2001 From: Emil Gydesen Date: Mon, 29 Sep 2025 15:15:08 +0200 Subject: [PATCH 1/2] tests: Bluetooth: CAP: Modify initiator unittests to native_sim Modify the unit tests to use the native_sim board instead of the unit_testing board. The native_sim board allows us to use the kernel in a more regular way, while still being able to treat the tests as unittest. To support mocking we cannot rely on the default Kconfig options and values. The required Kconfig options to satisfy dependencies are overridden in the Kconfig file and the necessary Kconfig files are sourced. We cannot enable e.g. CONFIG_BT as that would attempt to pull in a lot of files we do not want in this test. This commit also removes unused includes in the CMakelists Signed-off-by: Emil Gydesen --- .../audio/cap_initiator/CMakeLists.txt | 44 ++++++++--- tests/bluetooth/audio/cap_initiator/Kconfig | 79 +++++++++++++++++++ tests/bluetooth/audio/cap_initiator/prj.conf | 22 +----- .../bluetooth/audio/cap_initiator/src/main.c | 4 +- .../audio/cap_initiator/src/test_common.c | 4 +- .../cap_initiator/src/test_unicast_group.c | 6 +- .../cap_initiator/src/test_unicast_start.c | 9 ++- .../cap_initiator/src/test_unicast_stop.c | 6 +- .../audio/cap_initiator/testcase.yaml | 5 +- .../audio/cap_initiator/uut/CMakeLists.txt | 31 -------- .../cap_initiator/uut/bap_unicast_client.c | 4 +- 11 files changed, 134 insertions(+), 80 deletions(-) create mode 100644 tests/bluetooth/audio/cap_initiator/Kconfig delete mode 100644 tests/bluetooth/audio/cap_initiator/uut/CMakeLists.txt diff --git a/tests/bluetooth/audio/cap_initiator/CMakeLists.txt b/tests/bluetooth/audio/cap_initiator/CMakeLists.txt index 29d56f22a2831..bbca7b6f2f362 100644 --- a/tests/bluetooth/audio/cap_initiator/CMakeLists.txt +++ b/tests/bluetooth/audio/cap_initiator/CMakeLists.txt @@ -2,21 +2,43 @@ cmake_minimum_required(VERSION 3.20.0) -find_package(Zephyr COMPONENTS unittest HINTS $ENV{ZEPHYR_BASE}) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bluetooth_cap_initiator) -add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/audio/cap_initiator/uut uut) +target_include_directories(app PRIVATE + ${ZEPHYR_BASE}/subsys/bluetooth/ -target_link_libraries(testbinary PRIVATE uut) + ${ZEPHYR_BASE}/tests/bluetooth/audio/cap_initiator/include + ${ZEPHYR_BASE}/tests/bluetooth/audio/mocks/include +) + +target_sources(app PRIVATE + # Test source files + src/main.c + src/test_common.c + src/test_unicast_group.c + src/test_unicast_start.c + src/test_unicast_stop.c + + # UUT files + uut/bap_unicast_client.c + uut/cap_initiator.c + uut/csip.c -target_include_directories(testbinary PRIVATE include) + # Stack source file + ${ZEPHYR_BASE}/subsys/bluetooth/audio/audio.c + ${ZEPHYR_BASE}/subsys/bluetooth/audio/bap_iso.c + ${ZEPHYR_BASE}/subsys/bluetooth/audio/bap_stream.c + ${ZEPHYR_BASE}/subsys/bluetooth/audio/cap_initiator.c + ${ZEPHYR_BASE}/subsys/bluetooth/audio/cap_common.c + ${ZEPHYR_BASE}/subsys/bluetooth/audio/cap_stream.c + ${ZEPHYR_BASE}/subsys/bluetooth/audio/ccid.c + ${ZEPHYR_BASE}/subsys/bluetooth/common/bt_str.c + ${ZEPHYR_BASE}/subsys/bluetooth/host/uuid.c -target_sources(testbinary - PRIVATE - src/main.c - src/test_common.c - src/test_unicast_group.c - src/test_unicast_start.c - src/test_unicast_stop.c + # Mock files + ${ZEPHYR_BASE}/tests/bluetooth/audio/mocks/src/conn.c + ${ZEPHYR_BASE}/tests/bluetooth/audio/mocks/src/gatt.c + ${ZEPHYR_BASE}/tests/bluetooth/audio/mocks/src/iso.c ) diff --git a/tests/bluetooth/audio/cap_initiator/Kconfig b/tests/bluetooth/audio/cap_initiator/Kconfig new file mode 100644 index 0000000000000..b428f659508e0 --- /dev/null +++ b/tests/bluetooth/audio/cap_initiator/Kconfig @@ -0,0 +1,79 @@ +# Copyright (c) 2025 Nordic Semiconductor ASA +# SPDX-License-Identifier: Apache-2.0 + +config BT_ATT_PREPARE_COUNT + int + default 1 + +config BT_AUDIO + bool + default y + +config BT_BAP_UNICAST_CLIENT + bool + default y + +config BT_BAP_UNICAST_CLIENT_GROUP_COUNT + int + default 1 + +config BT_BAP_UNICAST_CLIENT_GROUP_STREAM_COUNT + int + default 4 + +config BT_BONDABLE + bool + default y + +config BT_BUF_ACL_RX_SIZE + int + default 69 + +config BT_BUF_ACL_TX_COUNT + int + default 5 + +config BT_BUF_EVT_RX_COUNT + int + default 10 + +config BT_CONN + bool + default y + +config BT_CSIP_SET_COORDINATOR + bool + default y + +config BT_ISO_MAX_CIG + int + default 1 + +config BT_ISO_MAX_CHAN + int + default 4 + +config BT_MAX_PAIRED + int + default 1 + +config BT_L2CAP_TX_MTU + int + default 65 + +config BT_LOG + bool + default y + +config BT_MAX_CONN + int + default 2 + +config BT_SMP + bool + default y + +# Include Zephyr's Kconfig. +source "Kconfig" +source "subsys/bluetooth/audio/Kconfig" +source "subsys/bluetooth/Kconfig.logging" diff --git a/tests/bluetooth/audio/cap_initiator/prj.conf b/tests/bluetooth/audio/cap_initiator/prj.conf index 85b8fe6aad4e6..973a55f89ac76 100644 --- a/tests/bluetooth/audio/cap_initiator/prj.conf +++ b/tests/bluetooth/audio/cap_initiator/prj.conf @@ -1,32 +1,12 @@ CONFIG_ZTEST=y -CONFIG_LOG=y - -CONFIG_BT=y -CONFIG_BT_SMP=y -CONFIG_BT_CENTRAL=y -CONFIG_BT_ISO_CENTRAL=y -CONFIG_BT_GATT_CLIENT=y -CONFIG_BT_GATT_AUTO_DISCOVER_CCC=y -CONFIG_BT_GATT_AUTO_UPDATE_MTU=y -CONFIG_BT_AUDIO=y - -# Dependencies for CAP initiator unicast -CONFIG_BT_BAP_UNICAST_CLIENT=y -CONFIG_BT_CSIP_SET_COORDINATOR=y CONFIG_BT_CAP_INITIATOR=y -# Support setting up a sink and source stream on 2 acceptors -CONFIG_BT_MAX_CONN=2 -CONFIG_BT_ISO_MAX_CHAN=4 -CONFIG_BT_BAP_UNICAST_CLIENT_GROUP_STREAM_COUNT=4 -CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT=2 -CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT=2 - CONFIG_ASSERT=y CONFIG_ASSERT_LEVEL=2 CONFIG_ASSERT_VERBOSE=y +CONFIG_LOG=y CONFIG_BT_BAP_STREAM_LOG_LEVEL_DBG=y CONFIG_BT_CAP_COMMON_LOG_LEVEL_DBG=y CONFIG_BT_CAP_INITIATOR_LOG_LEVEL_DBG=y diff --git a/tests/bluetooth/audio/cap_initiator/src/main.c b/tests/bluetooth/audio/cap_initiator/src/main.c index a15c1fc1c1f04..48c82a7ecccc9 100644 --- a/tests/bluetooth/audio/cap_initiator/src/main.c +++ b/tests/bluetooth/audio/cap_initiator/src/main.c @@ -16,13 +16,13 @@ #include #include #include +#include +#include #include "cap_initiator.h" #include "conn.h" #include "expects_util.h" #include "test_common.h" -#include "ztest_assert.h" -#include "ztest_test.h" static void mock_init_rule_before(const struct ztest_unit_test *test, void *fixture) { diff --git a/tests/bluetooth/audio/cap_initiator/src/test_common.c b/tests/bluetooth/audio/cap_initiator/src/test_common.c index fe3650c7c1160..4d8f36989a308 100644 --- a/tests/bluetooth/audio/cap_initiator/src/test_common.c +++ b/tests/bluetooth/audio/cap_initiator/src/test_common.c @@ -13,12 +13,12 @@ #include #include #include +#include -#include "bap_endpoint.h" +#include "audio/bap_endpoint.h" #include "cap_initiator.h" #include "conn.h" #include "test_common.h" -#include "ztest_assert.h" DEFINE_FFF_GLOBALS; diff --git a/tests/bluetooth/audio/cap_initiator/src/test_unicast_group.c b/tests/bluetooth/audio/cap_initiator/src/test_unicast_group.c index bf39491cb7d38..8023625b621d2 100644 --- a/tests/bluetooth/audio/cap_initiator/src/test_unicast_group.c +++ b/tests/bluetooth/audio/cap_initiator/src/test_unicast_group.c @@ -22,12 +22,12 @@ #include #include #include +#include +#include #include -#include "bap_endpoint.h" +#include "audio/bap_endpoint.h" #include "test_common.h" -#include "ztest_assert.h" -#include "ztest_test.h" struct cap_initiator_test_unicast_group_fixture { struct bt_cap_unicast_group_param *group_param; diff --git a/tests/bluetooth/audio/cap_initiator/src/test_unicast_start.c b/tests/bluetooth/audio/cap_initiator/src/test_unicast_start.c index 09e85780ffdd3..0b4a62750c5b5 100644 --- a/tests/bluetooth/audio/cap_initiator/src/test_unicast_start.c +++ b/tests/bluetooth/audio/cap_initiator/src/test_unicast_start.c @@ -25,16 +25,17 @@ #include #include #include +#include +#include + #include -#include "bap_endpoint.h" -#include "bap_iso.h" +#include "audio/bap_endpoint.h" +#include "audio/bap_iso.h" #include "cap_initiator.h" #include "conn.h" #include "expects_util.h" #include "test_common.h" -#include "ztest_assert.h" -#include "ztest_test.h" struct cap_initiator_test_unicast_start_fixture { struct bt_cap_stream cap_streams[CONFIG_BT_BAP_UNICAST_CLIENT_GROUP_STREAM_COUNT]; diff --git a/tests/bluetooth/audio/cap_initiator/src/test_unicast_stop.c b/tests/bluetooth/audio/cap_initiator/src/test_unicast_stop.c index dd456d16a6e09..34e8d533c054f 100644 --- a/tests/bluetooth/audio/cap_initiator/src/test_unicast_stop.c +++ b/tests/bluetooth/audio/cap_initiator/src/test_unicast_stop.c @@ -22,15 +22,15 @@ #include #include #include +#include + #include -#include "bap_endpoint.h" +#include "audio/bap_endpoint.h" #include "cap_initiator.h" #include "conn.h" #include "expects_util.h" #include "test_common.h" -#include "ztest_assert.h" -#include "ztest_test.h" struct cap_initiator_test_unicast_stop_fixture { struct bt_cap_stream cap_streams[CONFIG_BT_BAP_UNICAST_CLIENT_GROUP_STREAM_COUNT]; diff --git a/tests/bluetooth/audio/cap_initiator/testcase.yaml b/tests/bluetooth/audio/cap_initiator/testcase.yaml index 7000d54778ef5..7a2ac5f606081 100644 --- a/tests/bluetooth/audio/cap_initiator/testcase.yaml +++ b/tests/bluetooth/audio/cap_initiator/testcase.yaml @@ -4,4 +4,7 @@ common: - bluetooth_audio tests: bluetooth.audio.cap_initiator.test_default: - type: unit + platform_allow: + - native_sim + integration_platforms: + - native_sim diff --git a/tests/bluetooth/audio/cap_initiator/uut/CMakeLists.txt b/tests/bluetooth/audio/cap_initiator/uut/CMakeLists.txt deleted file mode 100644 index ebd97dc155b86..0000000000000 --- a/tests/bluetooth/audio/cap_initiator/uut/CMakeLists.txt +++ /dev/null @@ -1,31 +0,0 @@ -# -# Copyright (c) 2023-2024 Nordic Semiconductor ASA -# -# SPDX-License-Identifier: Apache-2.0 -# -# CMakeLists.txt file for creating of uut library. -# - -add_library(uut STATIC - ${ZEPHYR_BASE}/subsys/bluetooth/audio/audio.c - ${ZEPHYR_BASE}/subsys/bluetooth/audio/bap_iso.c - ${ZEPHYR_BASE}/subsys/bluetooth/audio/bap_stream.c - ${ZEPHYR_BASE}/subsys/bluetooth/audio/cap_initiator.c - ${ZEPHYR_BASE}/subsys/bluetooth/audio/cap_common.c - ${ZEPHYR_BASE}/subsys/bluetooth/audio/cap_stream.c - ${ZEPHYR_BASE}/subsys/bluetooth/audio/ccid.c - ${ZEPHYR_BASE}/subsys/bluetooth/common/bt_str.c - ${ZEPHYR_BASE}/subsys/bluetooth/host/uuid.c - ${ZEPHYR_BASE}/subsys/logging/log_minimal.c - ${ZEPHYR_BASE}/lib/net_buf/buf_simple.c - bap_unicast_client.c - cap_initiator.c - csip.c -) - -add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/audio/mocks mocks) - -target_link_libraries(uut PUBLIC test_interface mocks) -target_include_directories(uut PRIVATE ${ZEPHYR_BASE}/tests/bluetooth/audio/cap_initiator/include) - -target_compile_options(uut PRIVATE -std=c11 -include ztest.h) diff --git a/tests/bluetooth/audio/cap_initiator/uut/bap_unicast_client.c b/tests/bluetooth/audio/cap_initiator/uut/bap_unicast_client.c index eb34b97b4a048..4c5435e58427e 100644 --- a/tests/bluetooth/audio/cap_initiator/uut/bap_unicast_client.c +++ b/tests/bluetooth/audio/cap_initiator/uut/bap_unicast_client.c @@ -22,8 +22,8 @@ #include #include -#include "bap_endpoint.h" -#include "bap_iso.h" +#include "audio/bap_endpoint.h" +#include "audio/bap_iso.h" static struct bt_bap_unicast_client_cb *unicast_client_cb; static struct bt_bap_unicast_group bap_unicast_group; From 000b971cb90f608e44f671f81be0ddcae7651cd9 Mon Sep 17 00:00:00 2001 From: Emil Gydesen Date: Mon, 13 Oct 2025 15:28:41 +0200 Subject: [PATCH 2/2] tests: Bluetooth: CAP: Unit tests: Delete group after creation Some of the CAP initiator unit tests did not delete the group after creation, even if they easily could. Especially for test_initiator_unicast_group_add_streams it was a problem, as the stream added was stack allocated, and by the end of the test it was deallocated, causing issues when the group was finally deleted in cap_initiator_test_unicast_group_after. Signed-off-by: Emil Gydesen --- .../audio/cap_initiator/src/test_unicast_group.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tests/bluetooth/audio/cap_initiator/src/test_unicast_group.c b/tests/bluetooth/audio/cap_initiator/src/test_unicast_group.c index 8023625b621d2..eed6b880b2140 100644 --- a/tests/bluetooth/audio/cap_initiator/src/test_unicast_group.c +++ b/tests/bluetooth/audio/cap_initiator/src/test_unicast_group.c @@ -126,6 +126,9 @@ static ZTEST_F(cap_initiator_test_unicast_group, test_initiator_unicast_group_cr err = bt_cap_unicast_group_create(fixture->group_param, &fixture->unicast_group); zassert_equal(err, 0, "Unexpected return value %d", err); + + err = bt_cap_unicast_group_delete(fixture->unicast_group); + zassert_equal(err, 0, "Unexpected return value %d", err); } static ZTEST_F(cap_initiator_test_unicast_group, @@ -185,6 +188,9 @@ static ZTEST_F(cap_initiator_test_unicast_group, test_initiator_unicast_group_re err = bt_cap_unicast_group_reconfig(fixture->unicast_group, fixture->group_param); zassert_equal(err, 0, "Unexpected return value %d", err); + + err = bt_cap_unicast_group_delete(fixture->unicast_group); + zassert_equal(err, 0, "Unexpected return value %d", err); } static ZTEST_F(cap_initiator_test_unicast_group, @@ -213,7 +219,7 @@ static ZTEST_F(cap_initiator_test_unicast_group, static ZTEST_F(cap_initiator_test_unicast_group, test_initiator_unicast_group_add_streams) { - struct bt_cap_stream stream = {}; + struct bt_cap_stream stream = {0}; struct bt_cap_unicast_group_stream_param stream_param = { .stream = &stream, .qos_cfg = fixture->qos_cfg, @@ -228,12 +234,15 @@ static ZTEST_F(cap_initiator_test_unicast_group, test_initiator_unicast_group_ad err = bt_cap_unicast_group_add_streams(fixture->unicast_group, &pair_param, 1); zassert_equal(err, 0, "Unexpected return value %d", err); + + err = bt_cap_unicast_group_delete(fixture->unicast_group); + zassert_equal(err, 0, "Unexpected return value %d", err); } static ZTEST_F(cap_initiator_test_unicast_group, test_initiator_unicast_group_add_streams_inval_null_group) { - struct bt_cap_stream stream = {}; + struct bt_cap_stream stream = {0}; struct bt_cap_unicast_group_stream_param stream_param = { .stream = &stream, .qos_cfg = fixture->qos_cfg, @@ -265,7 +274,7 @@ static ZTEST_F(cap_initiator_test_unicast_group, static ZTEST_F(cap_initiator_test_unicast_group, test_initiator_unicast_group_add_streams_inval_0_param) { - struct bt_cap_stream stream = {}; + struct bt_cap_stream stream = {0}; struct bt_cap_unicast_group_stream_param stream_param = { .stream = &stream, .qos_cfg = fixture->qos_cfg,