Skip to content

Commit e73bfb9

Browse files
Thalleynashif
authored andcommitted
tests: Bluetooth: ASCS: Fix various issues in ASCS unit tests
The ASCS unit tests had various errors after adding support for dynamic registration. Several tests did not properly clean up after failure, causing other tests to fail when they shouldn't. Moved the register tests to their own file as they should not do the register in the "before" function. The test_ascs_unregister_with_ases_in_config_state test was also removed, as it had both issues and the state that it wants to test cannot be reached with the current API - It is not possible to put an ASE in the configured state without callbacks, and registered callbacks prevents us from calling bt_bap_unicast_server_unregister to trigger the case as that can only be done if callbacks are unregistered. Since unregistering callbacks also puts all ASEs to the idle state, it is not possible to call bt_bap_unicast_server_unregister for a non-idle ASE. The testcase.yaml was also missing some Kconfig options to properly enable the client tests. Signed-off-by: Emil Gydesen <[email protected]>
1 parent bc43826 commit e73bfb9

File tree

8 files changed

+199
-190
lines changed

8 files changed

+199
-190
lines changed

subsys/bluetooth/audio/ascs.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3283,6 +3283,8 @@ int bt_ascs_unregister(void)
32833283

32843284
for (size_t i = 0; i < ARRAY_SIZE(ascs.ase_pool); i++) {
32853285
if (ascs.ase_pool[i].ep.status.state != BT_BAP_EP_STATE_IDLE) {
3286+
LOG_DBG("[%zu] ase %p not in idle state: %s", i, &ascs.ase_pool[i].ep,
3287+
bt_bap_ep_state_str(ascs.ase_pool[i].ep.status.state));
32863288
return -EBUSY;
32873289
}
32883290
}

tests/bluetooth/audio/ascs/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ target_sources(testbinary
1616
PRIVATE
1717
src/main.c
1818
src/test_ase_control_params.c
19+
src/test_ase_register.c
1920
src/test_ase_state_transition_invalid.c
2021
src/test_ase_state_transition.c
2122
src/test_common.c

tests/bluetooth/audio/ascs/src/main.c

Lines changed: 15 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
/*
44
* Copyright (c) 2023 Codecoup
55
* Copyright (c) 2024 Demant A/S
6+
* Copyright (c) 2024 Nordic Semiconductor ASA
67
*
78
* SPDX-License-Identifier: Apache-2.0
89
*/
910

11+
#include <errno.h>
1012
#include <stdlib.h>
1113
#include <string.h>
1214
#include <zephyr/fff.h>
@@ -34,6 +36,7 @@
3436
#include "pacs.h"
3537

3638
#include "test_common.h"
39+
#include "ztest_assert.h"
3740

3841
DEFINE_FFF_GLOBALS;
3942

@@ -70,6 +73,7 @@ static void ascs_test_suite_fixture_init(struct ascs_test_suite_fixture *fixture
7073
memset(fixture, 0, sizeof(*fixture));
7174

7275
err = bt_bap_unicast_server_register(&param);
76+
zassert_equal(err, 0, "Unexpected err response %d", err);
7377

7478
fixture->ase_cp = test_ase_control_point_get();
7579

@@ -109,11 +113,17 @@ static void ascs_test_suite_teardown(void *f)
109113

110114
static void ascs_test_suite_after(void *f)
111115
{
112-
/* We skip error-checking this, as somehow this breaks the tests, due to seemingly
113-
* memory corruption, causing incorrect lookup of attributes in following 'before' calls
114-
*/
115-
bt_bap_unicast_server_unregister_cb(&mock_bap_unicast_server_cb);
116-
bt_bap_unicast_server_unregister();
116+
int err;
117+
118+
/* If any of these fails, it's a fatal error for any tests running afterwards */
119+
err = bt_bap_unicast_server_unregister_cb(&mock_bap_unicast_server_cb);
120+
zassert_true(err == 0 || err == -EALREADY, "Unexpected err response %d", err);
121+
122+
/* Sleep to trigger any pending state changes from unregister_cb */
123+
k_sleep(K_SECONDS(1));
124+
125+
err = bt_bap_unicast_server_unregister();
126+
zassert_equal(err, 0, "Unexpected err response %d", err);
117127
}
118128

119129
ZTEST_SUITE(ascs_test_suite, NULL, ascs_test_suite_setup, ascs_test_suite_before,
@@ -153,158 +163,6 @@ ZTEST_F(ascs_test_suite, test_sink_ase_read_state_idle)
153163
zassert_equal(0x00, hdr.ase_state, "unexpected ASE_State 0x%02x", hdr.ase_state);
154164
}
155165

156-
ZTEST_F(ascs_test_suite, test_cb_register_without_ascs_registered)
157-
{
158-
int err;
159-
160-
/* Unregister ASCS, as its registered through setup */
161-
err = bt_bap_unicast_server_unregister();
162-
zassert_equal(err, 0, "unexpected err response %d", err);
163-
164-
err = bt_bap_unicast_server_register_cb(&mock_bap_unicast_server_cb);
165-
zassert_equal(err, -ENOTSUP, "unexpected err response %d", err);
166-
}
167-
168-
ZTEST_F(ascs_test_suite, test_ascs_register_with_null_param)
169-
{
170-
int err;
171-
172-
/* Unregister ASCS, as its registered through setup */
173-
err = bt_bap_unicast_server_unregister();
174-
zassert_equal(err, 0, "unexpected err response %d", err);
175-
176-
err = bt_bap_unicast_server_register(NULL);
177-
zassert_equal(err, -EINVAL, "unexpected err response %d", err);
178-
}
179-
180-
ZTEST_F(ascs_test_suite, test_ascs_register_twice)
181-
{
182-
int err;
183-
struct bt_bap_unicast_server_register_param param = {
184-
CONFIG_BT_ASCS_MAX_ASE_SNK_COUNT,
185-
CONFIG_BT_ASCS_MAX_ASE_SRC_COUNT
186-
};
187-
188-
/* Setup already registered once, so calling once here should be sufficient */
189-
err = bt_bap_unicast_server_register(&param);
190-
zassert_equal(err, -EALREADY, "unexpected err response %d", err);
191-
}
192-
193-
ZTEST_F(ascs_test_suite, test_ascs_register_too_many_sinks)
194-
{
195-
int err;
196-
struct bt_bap_unicast_server_register_param param = {
197-
CONFIG_BT_ASCS_MAX_ASE_SNK_COUNT + 1,
198-
CONFIG_BT_ASCS_MAX_ASE_SRC_COUNT
199-
};
200-
201-
/* Unregister ASCS, as its registered through setup */
202-
err = bt_bap_unicast_server_unregister();
203-
zassert_equal(err, 0, "unexpected err response %d", err);
204-
205-
err = bt_bap_unicast_server_register(&param);
206-
zassert_equal(err, -EINVAL, "unexpected err response %d", err);
207-
}
208-
209-
ZTEST_F(ascs_test_suite, test_ascs_register_too_many_sources)
210-
{
211-
int err;
212-
struct bt_bap_unicast_server_register_param param = {
213-
CONFIG_BT_ASCS_MAX_ASE_SNK_COUNT,
214-
CONFIG_BT_ASCS_MAX_ASE_SRC_COUNT + 1
215-
};
216-
217-
/* Unregister ASCS, as its registered through setup */
218-
err = bt_bap_unicast_server_unregister();
219-
zassert_equal(err, 0, "unexpected err response %d", err);
220-
221-
err = bt_bap_unicast_server_register(&param);
222-
zassert_equal(err, -EINVAL, "unexpected err response %d", err);
223-
}
224-
225-
ZTEST_F(ascs_test_suite, test_ascs_register_zero_ases)
226-
{
227-
int err;
228-
struct bt_bap_unicast_server_register_param param = {
229-
0,
230-
0
231-
};
232-
233-
/* Unregister ASCS, as its registered through setup */
234-
err = bt_bap_unicast_server_unregister();
235-
zassert_equal(err, 0, "unexpected err response %d", err);
236-
237-
err = bt_bap_unicast_server_register(&param);
238-
zassert_equal(err, -EINVAL, "unexpected err response %d", err);
239-
}
240-
241-
ZTEST_F(ascs_test_suite, test_ascs_register_fewer_than_max_ases)
242-
{
243-
int err;
244-
struct bt_bap_unicast_server_register_param param = {
245-
CONFIG_BT_ASCS_MAX_ASE_SNK_COUNT > 0 ? CONFIG_BT_ASCS_MAX_ASE_SNK_COUNT - 1 : 0,
246-
CONFIG_BT_ASCS_MAX_ASE_SRC_COUNT > 0 ? CONFIG_BT_ASCS_MAX_ASE_SRC_COUNT - 1 : 0
247-
};
248-
249-
/* Unregister ASCS, as its registered through setup */
250-
err = bt_bap_unicast_server_unregister();
251-
zassert_equal(err, 0, "unexpected err response %d", err);
252-
253-
err = bt_bap_unicast_server_register(&param);
254-
zassert_equal(err, 0, "unexpected err response %d", err);
255-
}
256-
257-
ZTEST_F(ascs_test_suite, test_ascs_unregister_without_register)
258-
{
259-
int err;
260-
261-
/* Unregister ASCS, as its registered through setup */
262-
err = bt_bap_unicast_server_unregister();
263-
zassert_equal(err, 0, "unexpected err response %d", err);
264-
265-
err = bt_bap_unicast_server_unregister();
266-
zassert_equal(err, -EALREADY, "unexpected err response %d", err);
267-
}
268-
269-
ZTEST_F(ascs_test_suite, test_ascs_unregister_with_ases_in_config_state)
270-
{
271-
const struct test_ase_chrc_value_hdr *hdr;
272-
const struct bt_gatt_attr *ase;
273-
struct bt_bap_stream *stream = &fixture->stream;
274-
struct bt_conn *conn = &fixture->conn;
275-
struct bt_gatt_notify_params *notify_params;
276-
uint8_t ase_id;
277-
int err;
278-
279-
if (IS_ENABLED(CONFIG_BT_ASCS_ASE_SNK)) {
280-
ase = fixture->ase_snk.attr;
281-
ase_id = fixture->ase_snk.id;
282-
} else {
283-
ase = fixture->ase_src.attr;
284-
ase_id = fixture->ase_src.id;
285-
}
286-
287-
zexpect_not_null(ase);
288-
zexpect_true(ase_id != 0x00);
289-
290-
err = bt_bap_unicast_server_register_cb(&mock_bap_unicast_server_cb);
291-
zassert_equal(err, 0, "unexpected err response %d", err);
292-
293-
/* Set ASE to non-idle state */
294-
test_ase_control_client_config_codec(conn, ase_id, stream);
295-
296-
err = bt_bap_unicast_server_unregister_cb(&mock_bap_unicast_server_cb);
297-
zassert_equal(err, 0, "unexpected err response %d", err);
298-
299-
err = bt_bap_unicast_server_unregister();
300-
301-
/* Expected to notify the upper layers */
302-
expect_bt_bap_unicast_server_cb_release_called_once(stream);
303-
expect_bt_bap_stream_ops_released_called_once(stream);
304-
305-
zassert_equal(err, 0, "unexpected err response %d", err);
306-
}
307-
308166
ZTEST_F(ascs_test_suite, test_release_ase_on_callback_unregister)
309167
{
310168
const struct test_ase_chrc_value_hdr *hdr;
@@ -424,8 +282,6 @@ ZTEST_F(ascs_test_suite, test_release_ase_on_acl_disconnection)
424282

425283
/* Mock CIS disconnection */
426284
mock_bt_iso_disconnected(chan, BT_HCI_ERR_CONN_TIMEOUT);
427-
428-
bt_bap_unicast_server_unregister_cb(&mock_bap_unicast_server_cb);
429285
}
430286

431287
ZTEST_F(ascs_test_suite, test_release_ase_pair_on_acl_disconnection)
@@ -486,8 +342,6 @@ ZTEST_F(ascs_test_suite, test_release_ase_pair_on_acl_disconnection)
486342

487343
/* Mock CIS disconnection */
488344
mock_bt_iso_disconnected(chan, BT_HCI_ERR_CONN_TIMEOUT);
489-
490-
bt_bap_unicast_server_unregister_cb(&mock_bap_unicast_server_cb);
491345
}
492346

493347
ZTEST_F(ascs_test_suite, test_recv_in_streaming_state)
@@ -514,8 +368,6 @@ ZTEST_F(ascs_test_suite, test_recv_in_streaming_state)
514368

515369
/* Verification */
516370
expect_bt_bap_stream_ops_recv_called_once(stream, &info, &buf);
517-
518-
bt_bap_unicast_server_unregister_cb(&mock_bap_unicast_server_cb);
519371
}
520372

521373
ZTEST_F(ascs_test_suite, test_recv_in_enabling_state)
@@ -547,8 +399,6 @@ ZTEST_F(ascs_test_suite, test_recv_in_enabling_state)
547399

548400
/* Verification */
549401
expect_bt_bap_stream_ops_recv_not_called();
550-
551-
bt_bap_unicast_server_unregister_cb(&mock_bap_unicast_server_cb);
552402
}
553403

554404
ZTEST_F(ascs_test_suite, test_cis_link_loss_in_streaming_state)
@@ -584,8 +434,6 @@ ZTEST_F(ascs_test_suite, test_cis_link_loss_in_streaming_state)
584434
expect_bt_bap_stream_ops_disabled_called_once(stream);
585435
expect_bt_bap_stream_ops_released_not_called();
586436
expect_bt_bap_stream_ops_disconnected_called_once(stream);
587-
588-
bt_bap_unicast_server_unregister_cb(&mock_bap_unicast_server_cb);
589437
}
590438

591439
static void test_cis_link_loss_in_disabling_state(struct ascs_test_suite_fixture *fixture,
@@ -630,8 +478,6 @@ static void test_cis_link_loss_in_disabling_state(struct ascs_test_suite_fixture
630478
expect_bt_bap_stream_ops_disabled_not_called();
631479
expect_bt_bap_stream_ops_released_not_called();
632480
expect_bt_bap_stream_ops_disconnected_called_once(stream);
633-
634-
bt_bap_unicast_server_unregister_cb(&mock_bap_unicast_server_cb);
635481
}
636482

637483
ZTEST_F(ascs_test_suite, test_cis_link_loss_in_disabling_state_v1)
@@ -690,8 +536,6 @@ ZTEST_F(ascs_test_suite, test_cis_link_loss_in_enabling_state)
690536
/* Server-initiated disable operation that shall not cause transition to QoS */
691537
expect_bt_bap_stream_ops_qos_set_not_called();
692538
}
693-
694-
bt_bap_unicast_server_unregister_cb(&mock_bap_unicast_server_cb);
695539
}
696540

697541
ZTEST_F(ascs_test_suite, test_cis_link_loss_in_enabling_state_client_retries)
@@ -741,8 +585,6 @@ ZTEST_F(ascs_test_suite, test_cis_link_loss_in_enabling_state_client_retries)
741585

742586
expect_bt_bap_stream_ops_connected_called_twice(stream);
743587
expect_bt_bap_stream_ops_started_called_once(stream);
744-
745-
bt_bap_unicast_server_unregister_cb(&mock_bap_unicast_server_cb);
746588
}
747589

748590
static struct bt_bap_stream *stream_allocated;
@@ -823,6 +665,4 @@ ZTEST_F(ascs_test_suite, test_ase_state_notification_retry)
823665
k_sleep(K_MSEC(BT_CONN_INTERVAL_TO_MS(info.le.interval)));
824666

825667
expect_bt_bap_stream_ops_configured_called_once(stream, EMPTY);
826-
827-
bt_bap_unicast_server_unregister_cb(&mock_bap_unicast_server_cb);
828668
}

tests/bluetooth/audio/ascs/src/test_ase_control_params.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <stdint.h>
1111
#include <stdlib.h>
12+
#include <zephyr/kernel.h>
1213
#include <zephyr/types.h>
1314
#include <zephyr/bluetooth/audio/audio.h>
1415
#include <zephyr/bluetooth/audio/bap.h>
@@ -80,12 +81,11 @@ static void test_ase_control_params_after(void *f)
8081
err = bt_bap_unicast_server_unregister_cb(&mock_bap_unicast_server_cb);
8182
zassert_equal(err, 0, "unexpected err response %d", err);
8283

84+
/* Sleep to trigger any pending state changes from unregister_cb */
85+
k_sleep(K_SECONDS(1));
86+
8387
err = bt_bap_unicast_server_unregister();
84-
while (err != 0) {
85-
zassert_equal(err, -EBUSY, "unexpected err response %d", err);
86-
k_sleep(K_MSEC(10));
87-
err = bt_bap_unicast_server_unregister();
88-
}
88+
zassert_equal(err, 0, "Unexpected err response %d", err);
8989
}
9090

9191
static void test_ase_control_params_teardown(void *f)

0 commit comments

Comments
 (0)