Skip to content

Commit 5860554

Browse files
Thalleykartben
authored andcommitted
tests: Bluetooth: PACS: Add unit test of PACS register
Adds initiation unittesting of PACS by testing the bt_pacs_register API. Signed-off-by: Emil Gydesen <[email protected]>
1 parent 37e4af6 commit 5860554

File tree

10 files changed

+270
-1
lines changed

10 files changed

+270
-1
lines changed

tests/bluetooth/audio/mocks/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ add_library(mocks STATIC
1414
src/crypto.c
1515
src/fatal.c
1616
src/gatt.c
17+
src/hci_core.c
1718
src/iso.c
1819
src/kernel.c
1920
src/mem_slab.c

tests/bluetooth/audio/mocks/include/conn.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,21 @@
1111

1212
#include <stdint.h>
1313

14+
#include <zephyr/bluetooth/addr.h>
1415
#include <zephyr/bluetooth/conn.h>
16+
#include <zephyr/fff.h>
17+
18+
/* List of fakes used by this unit tester */
19+
#define CONN_FFF_FAKES_LIST(FAKE) \
20+
FAKE(bt_conn_foreach) \
21+
FAKE(bt_conn_get_dst) \
22+
FAKE(bt_foreach_bond)
23+
24+
typedef void (*bt_conn_foreach_cb)(struct bt_conn *, void *);
25+
typedef void (*bt_foreach_bond_cb)(const struct bt_bond_info *, void *);
26+
DECLARE_FAKE_VOID_FUNC(bt_conn_foreach, enum bt_conn_type, bt_conn_foreach_cb, void *);
27+
DECLARE_FAKE_VALUE_FUNC(const bt_addr_le_t *, bt_conn_get_dst, const struct bt_conn *);
28+
DECLARE_FAKE_VOID_FUNC(bt_foreach_bond, uint8_t, bt_foreach_bond_cb, void *);
1529

1630
struct bt_conn {
1731
uint8_t index;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef MOCKS_HCI_CORE_H_
8+
#define MOCKS_HCI_CORE_H_
9+
10+
#include <stdint.h>
11+
12+
#include <zephyr/bluetooth/addr.h>
13+
#include <zephyr/fff.h>
14+
15+
bool bt_addr_le_is_bonded(uint8_t id, const bt_addr_le_t *addr);
16+
#endif /* MOCKS_HCI_CORE_H_ */

tests/bluetooth/audio/mocks/src/conn.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
/*
22
* Copyright (c) 2023 Codecoup
3-
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
* Copyright (c) 2024-2025 Nordic Semiconductor ASA
44
*
55
* SPDX-License-Identifier: Apache-2.0
66
*/
77
#include <stdint.h>
88

9+
#include <errno.h>
10+
#include <stddef.h>
911
#include <stdint.h>
1012

13+
#include <zephyr/bluetooth/addr.h>
1114
#include <zephyr/bluetooth/conn.h>
15+
#include <zephyr/fff.h>
1216
#include <zephyr/sys/iterable_sections.h>
1317

1418
#include "conn.h"
1519

20+
DEFINE_FAKE_VOID_FUNC(bt_conn_foreach, enum bt_conn_type, bt_conn_foreach_cb, void *);
21+
DEFINE_FAKE_VALUE_FUNC(const bt_addr_le_t *, bt_conn_get_dst, const struct bt_conn *);
22+
DEFINE_FAKE_VOID_FUNC(bt_foreach_bond, uint8_t, bt_foreach_bond_cb, void *);
23+
24+
static struct bt_conn_auth_info_cb *bt_auth_info_cb;
25+
1626
uint8_t bt_conn_index(const struct bt_conn *conn)
1727
{
1828
return conn->index;
@@ -34,6 +44,21 @@ void bt_conn_unref(struct bt_conn *conn)
3444
{
3545
}
3646

47+
int bt_conn_auth_info_cb_register(struct bt_conn_auth_info_cb *cb)
48+
{
49+
if (cb == NULL) {
50+
return -EINVAL;
51+
}
52+
53+
if (bt_auth_info_cb != NULL) {
54+
return -EALREADY;
55+
}
56+
57+
bt_auth_info_cb = cb;
58+
59+
return 0;
60+
}
61+
3762
void mock_bt_conn_connected(struct bt_conn *conn, uint8_t err)
3863
{
3964
STRUCT_SECTION_FOREACH(bt_conn_cb, cb) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdint.h>
8+
9+
#include <zephyr/bluetooth/addr.h>
10+
#include <zephyr/fff.h>
11+
12+
bool bt_addr_le_is_bonded(uint8_t id, const bt_addr_le_t *addr)
13+
{
14+
return true;
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr COMPONENTS unittest HINTS $ENV{ZEPHYR_BASE})
6+
7+
project(bluetooth_pacs)
8+
9+
add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/audio/pacs/uut uut)
10+
11+
target_link_libraries(testbinary PRIVATE uut)
12+
13+
target_include_directories(testbinary PRIVATE include)
14+
15+
target_sources(testbinary
16+
PRIVATE
17+
src/main.c
18+
)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
CONFIG_ZTEST=y
2+
3+
CONFIG_BT=y
4+
CONFIG_BT_PERIPHERAL=y
5+
CONFIG_BT_SMP=y
6+
CONFIG_BT_GATT_DYNAMIC_DB=y
7+
CONFIG_BT_AUDIO=y
8+
CONFIG_BT_PAC_SNK=y
9+
CONFIG_BT_PAC_SNK_LOC=y
10+
CONFIG_BT_PAC_SRC=y
11+
CONFIG_BT_PAC_SRC_LOC=y
12+
13+
CONFIG_LOG=y
14+
CONFIG_BT_PACS_LOG_LEVEL_DBG=y
15+
16+
CONFIG_ASSERT=y
17+
CONFIG_ASSERT_LEVEL=2
18+
CONFIG_ASSERT_VERBOSE=y
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/* main.c - Application main entry point */
2+
3+
/*
4+
* Copyright (c) 2025 Nordic Semiconductor ASA
5+
*
6+
* SPDX-License-Identifier: Apache-2.0
7+
*/
8+
9+
#include <errno.h>
10+
#include <stdint.h>
11+
#include <stdlib.h>
12+
13+
#include <zephyr/bluetooth/audio/audio.h>
14+
#include <zephyr/bluetooth/audio/pacs.h>
15+
#include <zephyr/bluetooth/bluetooth.h>
16+
#include <zephyr/fff.h>
17+
#include <zephyr/sys/util.h>
18+
#include <zephyr/sys/util_macro.h>
19+
#include <zephyr/ztest_assert.h>
20+
#include <zephyr/ztest_test.h>
21+
22+
DEFINE_FFF_GLOBALS;
23+
24+
static void pacs_test_suite_after(void *f)
25+
{
26+
/* attempt to clean up after any failures */
27+
(void)bt_pacs_unregister();
28+
}
29+
30+
ZTEST_SUITE(pacs_test_suite, NULL, NULL, NULL, pacs_test_suite_after, NULL);
31+
32+
/* Helper macro to define parameters ignoring unsupported features */
33+
#define PACS_REGISTER_PARAM(_snk_pac, _snk_loc, _src_pac, _src_loc) \
34+
(struct bt_bap_pacs_register_param) \
35+
{ \
36+
IF_ENABLED(CONFIG_BT_PAC_SNK, (.snk_pac = (_snk_pac),)) \
37+
IF_ENABLED(CONFIG_BT_PAC_SNK_LOC, (.snk_loc = (_snk_loc),)) \
38+
IF_ENABLED(CONFIG_BT_PAC_SRC, (.src_pac = (_src_pac),)) \
39+
IF_ENABLED(CONFIG_BT_PAC_SRC_LOC, (.src_loc = (_src_loc),)) \
40+
}
41+
42+
static ZTEST(pacs_test_suite, test_pacs_register)
43+
{
44+
const struct bt_bap_pacs_register_param pacs_params[] = {
45+
/* valid snk_pac combinations */
46+
PACS_REGISTER_PARAM(true, true, true, true),
47+
PACS_REGISTER_PARAM(true, true, true, false),
48+
PACS_REGISTER_PARAM(true, true, false, false),
49+
PACS_REGISTER_PARAM(true, false, true, true),
50+
PACS_REGISTER_PARAM(true, false, true, false),
51+
PACS_REGISTER_PARAM(true, false, false, false),
52+
53+
/* valid src_pac combinations */
54+
PACS_REGISTER_PARAM(true, true, true, true),
55+
PACS_REGISTER_PARAM(true, false, true, true),
56+
PACS_REGISTER_PARAM(false, false, true, true),
57+
PACS_REGISTER_PARAM(true, true, true, false),
58+
PACS_REGISTER_PARAM(true, false, true, false),
59+
PACS_REGISTER_PARAM(false, false, true, false),
60+
};
61+
62+
for (size_t i = 0U; i < ARRAY_SIZE(pacs_params); i++) {
63+
int err;
64+
65+
err = bt_pacs_register(&pacs_params[i]);
66+
zassert_equal(err, 0, "[%zu]: Unexpected return value %d", i, err);
67+
68+
err = bt_pacs_unregister();
69+
zassert_equal(err, 0, "[%zu]: Unexpected return value %d", i, err);
70+
}
71+
}
72+
73+
static ZTEST(pacs_test_suite, test_pacs_register_inval_null_param)
74+
{
75+
int err;
76+
77+
err = bt_pacs_register(NULL);
78+
zassert_equal(err, -EINVAL, "Unexpected return value %d", err);
79+
}
80+
81+
static ZTEST(pacs_test_suite, test_pacs_register_inval_double_register)
82+
{
83+
const struct bt_bap_pacs_register_param pacs_param =
84+
PACS_REGISTER_PARAM(true, true, true, true);
85+
int err;
86+
87+
err = bt_pacs_register(&pacs_param);
88+
zassert_equal(err, 0, "Unexpected return value %d", err);
89+
90+
err = bt_pacs_register(&pacs_param);
91+
zassert_equal(err, -EALREADY, "Unexpected return value %d", err);
92+
}
93+
94+
static ZTEST(pacs_test_suite, test_pacs_register_inval_snk_loc_without_snk_pac)
95+
{
96+
const struct bt_bap_pacs_register_param pacs_param =
97+
PACS_REGISTER_PARAM(false, true, true, true);
98+
int err;
99+
100+
if (!(IS_ENABLED(CONFIG_BT_PAC_SNK) && IS_ENABLED(CONFIG_BT_PAC_SNK_LOC))) {
101+
ztest_test_skip();
102+
}
103+
104+
err = bt_pacs_register(&pacs_param);
105+
zassert_equal(err, -EINVAL, "Unexpected return value %d", err);
106+
}
107+
108+
static ZTEST(pacs_test_suite, test_pacs_register_inval_src_loc_without_src_pac)
109+
{
110+
const struct bt_bap_pacs_register_param pacs_param =
111+
PACS_REGISTER_PARAM(true, true, false, true);
112+
int err;
113+
114+
if (!(IS_ENABLED(CONFIG_BT_PAC_SRC) && IS_ENABLED(CONFIG_BT_PAC_SRC_LOC))) {
115+
ztest_test_skip();
116+
}
117+
118+
err = bt_pacs_register(&pacs_param);
119+
zassert_equal(err, -EINVAL, "Unexpected return value %d", err);
120+
}
121+
122+
static ZTEST(pacs_test_suite, test_pacs_register_inval_no_pac)
123+
{
124+
const struct bt_bap_pacs_register_param pacs_param =
125+
PACS_REGISTER_PARAM(false, false, false, false);
126+
int err;
127+
128+
if (!(IS_ENABLED(CONFIG_BT_PAC_SNK) && IS_ENABLED(CONFIG_BT_PAC_SNK_LOC))) {
129+
ztest_test_skip();
130+
}
131+
132+
err = bt_pacs_register(&pacs_param);
133+
zassert_equal(err, -EINVAL, "Unexpected return value %d", err);
134+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
common:
2+
tags:
3+
- bluetooth
4+
- bluetooth_audio
5+
tests:
6+
bluetooth.audio.pacs.test_default:
7+
type: unit
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# CMakeLists.txt file for creating of uut library.
7+
#
8+
9+
add_library(uut STATIC
10+
${ZEPHYR_BASE}/subsys/bluetooth/audio/audio.c
11+
${ZEPHYR_BASE}/subsys/bluetooth/audio/pacs.c
12+
${ZEPHYR_BASE}/subsys/bluetooth/host/uuid.c
13+
${ZEPHYR_BASE}/subsys/logging/log_minimal.c
14+
${ZEPHYR_BASE}/lib/net_buf/buf_simple.c
15+
)
16+
17+
add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/audio/mocks mocks)
18+
19+
target_link_libraries(uut PUBLIC test_interface mocks)
20+
21+
target_compile_options(uut PRIVATE -std=c11 -include ztest.h)

0 commit comments

Comments
 (0)