1 /* test_ase_state_transition_invalid.c - ASE state transition tests */
2 
3 /*
4  * Copyright (c) 2023 Codecoup
5  * Copyright (c) 2024 Demant A/S
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 #include <stdlib.h>
11 #include <zephyr/kernel.h>
12 #include <zephyr/types.h>
13 #include <zephyr/bluetooth/audio/audio.h>
14 #include <zephyr/bluetooth/audio/bap.h>
15 #include <zephyr/bluetooth/conn.h>
16 #include <zephyr/bluetooth/gatt.h>
17 #include <zephyr/sys/util_macro.h>
18 
19 #include "bap_unicast_server.h"
20 #include "bap_unicast_server_expects.h"
21 #include "bap_stream.h"
22 #include "bap_stream_expects.h"
23 #include "conn.h"
24 #include "gatt.h"
25 #include "gatt_expects.h"
26 #include "iso.h"
27 
28 #include "test_common.h"
29 
30 struct test_ase_state_transition_invalid_fixture {
31 	const struct bt_gatt_attr *ase_cp;
32 	const struct bt_gatt_attr *ase_snk;
33 	const struct bt_gatt_attr *ase_src;
34 	struct bt_bap_stream stream;
35 	struct bt_conn conn;
36 };
37 
test_ase_state_transition_invalid_setup(void)38 static void *test_ase_state_transition_invalid_setup(void)
39 {
40 	struct test_ase_state_transition_invalid_fixture *fixture;
41 
42 	fixture = malloc(sizeof(*fixture));
43 	zassert_not_null(fixture);
44 
45 
46 	return fixture;
47 }
48 
test_ase_state_transition_invalid_before(void * f)49 static void test_ase_state_transition_invalid_before(void *f)
50 {
51 	struct test_ase_state_transition_invalid_fixture *fixture =
52 		(struct test_ase_state_transition_invalid_fixture *)f;
53 	struct bt_bap_unicast_server_register_param param = {
54 		CONFIG_BT_ASCS_MAX_ASE_SNK_COUNT,
55 		CONFIG_BT_ASCS_MAX_ASE_SRC_COUNT
56 	};
57 	int err;
58 
59 	err = bt_bap_unicast_server_register(&param);
60 	zassert_equal(err, 0, "unexpected err response %d", err);
61 
62 	err = bt_bap_unicast_server_register_cb(&mock_bap_unicast_server_cb);
63 	zassert_equal(err, 0, "unexpected err response %d", err);
64 
65 	memset(fixture, 0, sizeof(struct test_ase_state_transition_invalid_fixture));
66 	fixture->ase_cp = test_ase_control_point_get();
67 	test_conn_init(&fixture->conn);
68 	test_ase_snk_get(CONFIG_BT_ASCS_MAX_ASE_SNK_COUNT, &fixture->ase_snk);
69 	test_ase_src_get(CONFIG_BT_ASCS_MAX_ASE_SRC_COUNT, &fixture->ase_src);
70 }
71 
test_ase_state_transition_invalid_after(void * f)72 static void test_ase_state_transition_invalid_after(void *f)
73 {
74 	int err;
75 
76 	err = bt_bap_unicast_server_unregister_cb(&mock_bap_unicast_server_cb);
77 	zassert_equal(err, 0, "unexpected err response %d", err);
78 
79 	/* Sleep to trigger any pending state changes from unregister_cb */
80 	k_sleep(K_SECONDS(1));
81 
82 	err = bt_bap_unicast_server_unregister();
83 	zassert_equal(err, 0, "Unexpected err response %d", err);
84 }
85 
test_ase_state_transition_invalid_teardown(void * f)86 static void test_ase_state_transition_invalid_teardown(void *f)
87 {
88 	free(f);
89 }
90 
91 ZTEST_SUITE(test_ase_state_transition_invalid, NULL, test_ase_state_transition_invalid_setup,
92 	    test_ase_state_transition_invalid_before, test_ase_state_transition_invalid_after,
93 	    test_ase_state_transition_invalid_teardown);
94 
test_client_config_codec_expect_transition_error(struct bt_conn * conn,uint8_t ase_id,const struct bt_gatt_attr * ase_cp)95 static void test_client_config_codec_expect_transition_error(struct bt_conn *conn, uint8_t ase_id,
96 							     const struct bt_gatt_attr *ase_cp)
97 {
98 	const uint8_t expected_error[] = {
99 		0x01,           /* Opcode = Config Codec */
100 		0x01,           /* Number_of_ASEs */
101 		ase_id,         /* ASE_ID[0] */
102 		0x04,           /* Response_Code[0] = Invalid ASE State Machine Transition */
103 		0x00,           /* Reason[0] */
104 	};
105 
106 	test_ase_control_client_config_codec(conn, ase_id, NULL);
107 	expect_bt_gatt_notify_cb_called_once(conn, BT_UUID_ASCS_ASE_CP, ase_cp, expected_error,
108 					     sizeof(expected_error));
109 	test_mocks_reset();
110 }
111 
test_client_config_qos_expect_transition_error(struct bt_conn * conn,uint8_t ase_id,const struct bt_gatt_attr * ase_cp)112 static void test_client_config_qos_expect_transition_error(struct bt_conn *conn, uint8_t ase_id,
113 							   const struct bt_gatt_attr *ase_cp)
114 {
115 	const uint8_t expected_error[] = {
116 		0x02,           /* Opcode = Config QoS */
117 		0x01,           /* Number_of_ASEs */
118 		ase_id,         /* ASE_ID[0] */
119 		0x04,           /* Response_Code[0] = Invalid ASE State Machine Transition */
120 		0x00,           /* Reason[0] */
121 	};
122 
123 	test_ase_control_client_config_qos(conn, ase_id);
124 	expect_bt_gatt_notify_cb_called_once(conn, BT_UUID_ASCS_ASE_CP, ase_cp, expected_error,
125 					     sizeof(expected_error));
126 	test_mocks_reset();
127 }
128 
test_client_enable_expect_transition_error(struct bt_conn * conn,uint8_t ase_id,const struct bt_gatt_attr * ase_cp)129 static void test_client_enable_expect_transition_error(struct bt_conn *conn, uint8_t ase_id,
130 						       const struct bt_gatt_attr *ase_cp)
131 {
132 	const uint8_t expected_error[] = {
133 		0x03,           /* Opcode = Enable */
134 		0x01,           /* Number_of_ASEs */
135 		ase_id,         /* ASE_ID[0] */
136 		0x04,           /* Response_Code[0] = Invalid ASE State Machine Transition */
137 		0x00,           /* Reason[0] */
138 	};
139 
140 	test_ase_control_client_enable(conn, ase_id);
141 	expect_bt_gatt_notify_cb_called_once(conn, BT_UUID_ASCS_ASE_CP, ase_cp, expected_error,
142 					     sizeof(expected_error));
143 	test_mocks_reset();
144 }
145 
test_client_receiver_start_ready_expect_transition_error(struct bt_conn * conn,uint8_t ase_id,const struct bt_gatt_attr * ase_cp)146 static void test_client_receiver_start_ready_expect_transition_error(
147 	struct bt_conn *conn, uint8_t ase_id, const struct bt_gatt_attr *ase_cp)
148 {
149 	const uint8_t expected_error[] = {
150 		0x04,           /* Opcode = Receiver Start Ready */
151 		0x01,           /* Number_of_ASEs */
152 		ase_id,         /* ASE_ID[0] */
153 		0x04,           /* Response_Code[0] = Invalid ASE State Machine Transition */
154 		0x00,           /* Reason[0] */
155 	};
156 
157 	test_ase_control_client_receiver_start_ready(conn, ase_id);
158 	expect_bt_gatt_notify_cb_called_once(conn, BT_UUID_ASCS_ASE_CP, ase_cp, expected_error,
159 					     sizeof(expected_error));
160 	test_mocks_reset();
161 }
162 
test_client_receiver_start_ready_expect_ase_direction_error(struct bt_conn * conn,uint8_t ase_id,const struct bt_gatt_attr * ase_cp)163 static void test_client_receiver_start_ready_expect_ase_direction_error(
164 	struct bt_conn *conn, uint8_t ase_id, const struct bt_gatt_attr *ase_cp)
165 {
166 	const uint8_t expected_error[] = {
167 		0x04,           /* Opcode = Receiver Start Ready */
168 		0x01,           /* Number_of_ASEs */
169 		ase_id,         /* ASE_ID[0] */
170 		0x05,           /* Response_Code[0] = Invalid ASE direction */
171 		0x00,           /* Reason[0] */
172 	};
173 
174 	test_ase_control_client_receiver_start_ready(conn, ase_id);
175 	expect_bt_gatt_notify_cb_called_once(conn, BT_UUID_ASCS_ASE_CP, ase_cp, expected_error,
176 					     sizeof(expected_error));
177 	test_mocks_reset();
178 }
179 
test_client_disable_expect_transition_error(struct bt_conn * conn,uint8_t ase_id,const struct bt_gatt_attr * ase_cp)180 static void test_client_disable_expect_transition_error(struct bt_conn *conn, uint8_t ase_id,
181 							const struct bt_gatt_attr *ase_cp)
182 {
183 	const uint8_t expected_error[] = {
184 		0x05,           /* Opcode = Disable */
185 		0x01,           /* Number_of_ASEs */
186 		ase_id,         /* ASE_ID[0] */
187 		0x04,           /* Response_Code[0] = Invalid ASE State Machine Transition */
188 		0x00,           /* Reason[0] */
189 	};
190 
191 	test_ase_control_client_disable(conn, ase_id);
192 	expect_bt_gatt_notify_cb_called_once(conn, BT_UUID_ASCS_ASE_CP, ase_cp, expected_error,
193 					     sizeof(expected_error));
194 	test_mocks_reset();
195 }
196 
test_client_receiver_stop_ready_expect_transition_error(struct bt_conn * conn,uint8_t ase_id,const struct bt_gatt_attr * ase_cp)197 static void test_client_receiver_stop_ready_expect_transition_error(
198 	struct bt_conn *conn, uint8_t ase_id, const struct bt_gatt_attr *ase_cp)
199 {
200 	const uint8_t expected_error[] = {
201 		0x06,           /* Opcode = Receiver Stop Ready */
202 		0x01,           /* Number_of_ASEs */
203 		ase_id,         /* ASE_ID[0] */
204 		0x04,           /* Response_Code[0] = Invalid ASE State Machine Transition */
205 		0x00,           /* Reason[0] */
206 	};
207 
208 	test_ase_control_client_receiver_stop_ready(conn, ase_id);
209 	expect_bt_gatt_notify_cb_called_once(conn, BT_UUID_ASCS_ASE_CP, ase_cp, expected_error,
210 					     sizeof(expected_error));
211 	test_mocks_reset();
212 }
213 
test_client_receiver_stop_ready_expect_ase_direction_error(struct bt_conn * conn,uint8_t ase_id,const struct bt_gatt_attr * ase_cp)214 static void test_client_receiver_stop_ready_expect_ase_direction_error(
215 	struct bt_conn *conn, uint8_t ase_id, const struct bt_gatt_attr *ase_cp)
216 {
217 	const uint8_t expected_error[] = {
218 		0x06,           /* Opcode = Receiver Stop Ready */
219 		0x01,           /* Number_of_ASEs */
220 		ase_id,         /* ASE_ID[0] */
221 		0x05,           /* Response_Code[0] = Invalid ASE State Machine Transition */
222 		0x00,           /* Reason[0] */
223 	};
224 
225 	test_ase_control_client_receiver_stop_ready(conn, ase_id);
226 	expect_bt_gatt_notify_cb_called_once(conn, BT_UUID_ASCS_ASE_CP, ase_cp, expected_error,
227 					     sizeof(expected_error));
228 	test_mocks_reset();
229 }
230 
test_client_update_metadata_expect_transition_error(struct bt_conn * conn,uint8_t ase_id,const struct bt_gatt_attr * ase_cp)231 static void test_client_update_metadata_expect_transition_error(
232 	struct bt_conn *conn, uint8_t ase_id, const struct bt_gatt_attr *ase_cp)
233 {
234 	const uint8_t expected_error[] = {
235 		0x07,           /* Opcode = Update Metadata */
236 		0x01,           /* Number_of_ASEs */
237 		ase_id,         /* ASE_ID[0] */
238 		0x04,           /* Response_Code[0] = Invalid ASE State Machine Transition */
239 		0x00,           /* Reason[0] */
240 	};
241 
242 	test_ase_control_client_update_metadata(conn, ase_id);
243 	expect_bt_gatt_notify_cb_called_once(conn, BT_UUID_ASCS_ASE_CP, ase_cp, expected_error,
244 					     sizeof(expected_error));
245 	test_mocks_reset();
246 }
247 
test_client_release_expect_transition_error(struct bt_conn * conn,uint8_t ase_id,const struct bt_gatt_attr * ase_cp)248 static void test_client_release_expect_transition_error(struct bt_conn *conn, uint8_t ase_id,
249 							const struct bt_gatt_attr *ase_cp)
250 {
251 	const uint8_t expected_error[] = {
252 		0x08,           /* Opcode = Release */
253 		0x01,           /* Number_of_ASEs */
254 		ase_id,         /* ASE_ID[0] */
255 		0x04,           /* Response_Code[0] = Invalid ASE State Machine Transition */
256 		0x00,           /* Reason[0] */
257 	};
258 
259 	test_ase_control_client_release(conn, ase_id);
260 	expect_bt_gatt_notify_cb_called_once(conn, BT_UUID_ASCS_ASE_CP, ase_cp, expected_error,
261 					     sizeof(expected_error));
262 	test_mocks_reset();
263 }
264 
ZTEST_F(test_ase_state_transition_invalid,test_client_sink_state_idle)265 ZTEST_F(test_ase_state_transition_invalid, test_client_sink_state_idle)
266 {
267 	const struct bt_gatt_attr *ase_cp = fixture->ase_cp;
268 	struct bt_conn *conn = &fixture->conn;
269 	uint8_t ase_id;
270 
271 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
272 
273 	ase_id = test_ase_id_get(fixture->ase_snk);
274 
275 	test_client_config_qos_expect_transition_error(conn, ase_id, ase_cp);
276 	test_client_enable_expect_transition_error(conn, ase_id, ase_cp);
277 	test_client_receiver_start_ready_expect_ase_direction_error(conn, ase_id, ase_cp);
278 	test_client_disable_expect_transition_error(conn, ase_id, ase_cp);
279 	test_client_receiver_stop_ready_expect_ase_direction_error(conn, ase_id, ase_cp);
280 	test_client_update_metadata_expect_transition_error(conn, ase_id, ase_cp);
281 	test_client_release_expect_transition_error(conn, ase_id, ase_cp);
282 }
283 
ZTEST_F(test_ase_state_transition_invalid,test_client_sink_state_codec_configured)284 ZTEST_F(test_ase_state_transition_invalid, test_client_sink_state_codec_configured)
285 {
286 	const struct bt_gatt_attr *ase_cp = fixture->ase_cp;
287 	struct bt_bap_stream *stream = &fixture->stream;
288 	struct bt_conn *conn = &fixture->conn;
289 	uint8_t ase_id;
290 
291 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
292 
293 	ase_id = test_ase_id_get(fixture->ase_snk);
294 	test_preamble_state_codec_configured(conn, ase_id, stream);
295 
296 	test_client_enable_expect_transition_error(conn, ase_id, ase_cp);
297 	test_client_receiver_start_ready_expect_ase_direction_error(conn, ase_id, ase_cp);
298 	test_client_disable_expect_transition_error(conn, ase_id, ase_cp);
299 	test_client_receiver_stop_ready_expect_ase_direction_error(conn, ase_id, ase_cp);
300 	test_client_update_metadata_expect_transition_error(conn, ase_id, ase_cp);
301 }
302 
ZTEST_F(test_ase_state_transition_invalid,test_client_sink_state_qos_configured)303 ZTEST_F(test_ase_state_transition_invalid, test_client_sink_state_qos_configured)
304 {
305 	const struct bt_gatt_attr *ase_cp = fixture->ase_cp;
306 	struct bt_bap_stream *stream = &fixture->stream;
307 	struct bt_conn *conn = &fixture->conn;
308 	uint8_t ase_id;
309 
310 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
311 
312 	ase_id = test_ase_id_get(fixture->ase_snk);
313 	test_preamble_state_qos_configured(conn, ase_id, stream);
314 
315 	test_client_receiver_start_ready_expect_ase_direction_error(conn, ase_id, ase_cp);
316 	test_client_disable_expect_transition_error(conn, ase_id, ase_cp);
317 	test_client_receiver_stop_ready_expect_ase_direction_error(conn, ase_id, ase_cp);
318 	test_client_update_metadata_expect_transition_error(conn, ase_id, ase_cp);
319 }
320 
ZTEST_F(test_ase_state_transition_invalid,test_client_sink_state_enabling)321 ZTEST_F(test_ase_state_transition_invalid, test_client_sink_state_enabling)
322 {
323 	const struct bt_gatt_attr *ase_cp = fixture->ase_cp;
324 	struct bt_bap_stream *stream = &fixture->stream;
325 	struct bt_conn *conn = &fixture->conn;
326 	uint8_t ase_id;
327 
328 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
329 
330 	ase_id = test_ase_id_get(fixture->ase_snk);
331 	test_preamble_state_enabling(conn, ase_id, stream);
332 
333 	test_client_config_codec_expect_transition_error(conn, ase_id, ase_cp);
334 	test_client_config_qos_expect_transition_error(conn, ase_id, ase_cp);
335 	test_client_enable_expect_transition_error(conn, ase_id, ase_cp);
336 	test_client_receiver_start_ready_expect_ase_direction_error(conn, ase_id, ase_cp);
337 	test_client_receiver_stop_ready_expect_ase_direction_error(conn, ase_id, ase_cp);
338 }
339 
ZTEST_F(test_ase_state_transition_invalid,test_sink_client_state_streaming)340 ZTEST_F(test_ase_state_transition_invalid, test_sink_client_state_streaming)
341 {
342 	const struct bt_gatt_attr *ase_cp = fixture->ase_cp;
343 	struct bt_bap_stream *stream = &fixture->stream;
344 	struct bt_conn *conn = &fixture->conn;
345 	struct bt_iso_chan *chan;
346 	uint8_t ase_id;
347 
348 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
349 
350 	ase_id = test_ase_id_get(fixture->ase_snk);
351 	test_preamble_state_streaming(conn, ase_id, stream, &chan, false);
352 
353 	test_client_config_codec_expect_transition_error(conn, ase_id, ase_cp);
354 	test_client_config_qos_expect_transition_error(conn, ase_id, ase_cp);
355 	test_client_enable_expect_transition_error(conn, ase_id, ase_cp);
356 	test_client_receiver_start_ready_expect_ase_direction_error(conn, ase_id, ase_cp);
357 	test_client_receiver_stop_ready_expect_ase_direction_error(conn, ase_id, ase_cp);
358 }
359 
expect_ase_state_releasing(struct bt_conn * conn,const struct bt_gatt_attr * ase)360 static void expect_ase_state_releasing(struct bt_conn *conn, const struct bt_gatt_attr *ase)
361 {
362 	struct test_ase_chrc_value_hdr hdr = { 0xff };
363 	ssize_t ret;
364 
365 	zexpect_not_null(conn);
366 	zexpect_not_null(ase);
367 
368 	ret = ase->read(conn, ase, &hdr, sizeof(hdr), 0);
369 	zassert_false(ret < 0, "attr->read returned unexpected (err 0x%02x)", BT_GATT_ERR(ret));
370 	zassert_equal(BT_BAP_EP_STATE_RELEASING, hdr.ase_state,
371 		      "unexpected ASE_State 0x%02x", hdr.ase_state);
372 }
373 
ZTEST_F(test_ase_state_transition_invalid,test_client_sink_state_releasing)374 ZTEST_F(test_ase_state_transition_invalid, test_client_sink_state_releasing)
375 {
376 	const struct bt_gatt_attr *ase_cp = fixture->ase_cp;
377 	struct bt_bap_stream *stream = &fixture->stream;
378 	struct bt_conn *conn = &fixture->conn;
379 	struct bt_iso_chan *chan;
380 	uint8_t ase_id;
381 
382 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
383 
384 	ase_id = test_ase_id_get(fixture->ase_snk);
385 	test_preamble_state_releasing(conn, ase_id, stream, &chan, false);
386 	expect_ase_state_releasing(conn, fixture->ase_snk);
387 
388 	test_client_config_codec_expect_transition_error(conn, ase_id, ase_cp);
389 	test_client_config_qos_expect_transition_error(conn, ase_id, ase_cp);
390 	test_client_enable_expect_transition_error(conn, ase_id, ase_cp);
391 	test_client_receiver_start_ready_expect_ase_direction_error(conn, ase_id, ase_cp);
392 	test_client_receiver_stop_ready_expect_ase_direction_error(conn, ase_id, ase_cp);
393 	test_client_disable_expect_transition_error(conn, ase_id, ase_cp);
394 	test_client_update_metadata_expect_transition_error(conn, ase_id, ase_cp);
395 }
396 
ZTEST_F(test_ase_state_transition_invalid,test_client_source_state_idle)397 ZTEST_F(test_ase_state_transition_invalid, test_client_source_state_idle)
398 {
399 	const struct bt_gatt_attr *ase_cp = fixture->ase_cp;
400 	struct bt_conn *conn = &fixture->conn;
401 	uint8_t ase_id;
402 
403 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
404 
405 	ase_id = test_ase_id_get(fixture->ase_src);
406 
407 	test_client_config_qos_expect_transition_error(conn, ase_id, ase_cp);
408 	test_client_enable_expect_transition_error(conn, ase_id, ase_cp);
409 	test_client_receiver_start_ready_expect_transition_error(conn, ase_id, ase_cp);
410 	test_client_disable_expect_transition_error(conn, ase_id, ase_cp);
411 	test_client_receiver_stop_ready_expect_transition_error(conn, ase_id, ase_cp);
412 	test_client_update_metadata_expect_transition_error(conn, ase_id, ase_cp);
413 	test_client_release_expect_transition_error(conn, ase_id, ase_cp);
414 }
415 
ZTEST_F(test_ase_state_transition_invalid,test_client_source_state_codec_configured)416 ZTEST_F(test_ase_state_transition_invalid, test_client_source_state_codec_configured)
417 {
418 	const struct bt_gatt_attr *ase_cp = fixture->ase_cp;
419 	struct bt_bap_stream *stream = &fixture->stream;
420 	struct bt_conn *conn = &fixture->conn;
421 	uint8_t ase_id;
422 
423 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
424 
425 	ase_id = test_ase_id_get(fixture->ase_src);
426 	test_preamble_state_codec_configured(conn, ase_id, stream);
427 
428 	test_client_enable_expect_transition_error(conn, ase_id, ase_cp);
429 	test_client_receiver_start_ready_expect_transition_error(conn, ase_id, ase_cp);
430 	test_client_disable_expect_transition_error(conn, ase_id, ase_cp);
431 	test_client_receiver_stop_ready_expect_transition_error(conn, ase_id, ase_cp);
432 	test_client_update_metadata_expect_transition_error(conn, ase_id, ase_cp);
433 }
434 
ZTEST_F(test_ase_state_transition_invalid,test_client_source_state_qos_configured)435 ZTEST_F(test_ase_state_transition_invalid, test_client_source_state_qos_configured)
436 {
437 	const struct bt_gatt_attr *ase_cp = fixture->ase_cp;
438 	struct bt_bap_stream *stream = &fixture->stream;
439 	struct bt_conn *conn = &fixture->conn;
440 	uint8_t ase_id;
441 
442 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
443 
444 	ase_id = test_ase_id_get(fixture->ase_src);
445 	test_preamble_state_qos_configured(conn, ase_id, stream);
446 
447 	test_client_receiver_start_ready_expect_transition_error(conn, ase_id, ase_cp);
448 	test_client_disable_expect_transition_error(conn, ase_id, ase_cp);
449 	test_client_receiver_stop_ready_expect_transition_error(conn, ase_id, ase_cp);
450 	test_client_update_metadata_expect_transition_error(conn, ase_id, ase_cp);
451 }
452 
ZTEST_F(test_ase_state_transition_invalid,test_client_source_state_enabling)453 ZTEST_F(test_ase_state_transition_invalid, test_client_source_state_enabling)
454 {
455 	const struct bt_gatt_attr *ase_cp = fixture->ase_cp;
456 	struct bt_bap_stream *stream = &fixture->stream;
457 	struct bt_conn *conn = &fixture->conn;
458 	uint8_t ase_id;
459 
460 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
461 
462 	ase_id = test_ase_id_get(fixture->ase_src);
463 	test_preamble_state_enabling(conn, ase_id, stream);
464 
465 	test_client_config_codec_expect_transition_error(conn, ase_id, ase_cp);
466 	test_client_config_qos_expect_transition_error(conn, ase_id, ase_cp);
467 	test_client_enable_expect_transition_error(conn, ase_id, ase_cp);
468 	test_client_receiver_stop_ready_expect_transition_error(conn, ase_id, ase_cp);
469 }
470 
ZTEST_F(test_ase_state_transition_invalid,test_client_source_state_streaming)471 ZTEST_F(test_ase_state_transition_invalid, test_client_source_state_streaming)
472 {
473 	const struct bt_gatt_attr *ase_cp = fixture->ase_cp;
474 	struct bt_bap_stream *stream = &fixture->stream;
475 	struct bt_conn *conn = &fixture->conn;
476 	struct bt_iso_chan *chan;
477 	uint8_t ase_id;
478 
479 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
480 
481 	ase_id = test_ase_id_get(fixture->ase_src);
482 	test_preamble_state_streaming(conn, ase_id, stream, &chan, true);
483 
484 	test_client_config_codec_expect_transition_error(conn, ase_id, ase_cp);
485 	test_client_config_qos_expect_transition_error(conn, ase_id, ase_cp);
486 	test_client_enable_expect_transition_error(conn, ase_id, ase_cp);
487 	test_client_receiver_start_ready_expect_transition_error(conn, ase_id, ase_cp);
488 	test_client_receiver_stop_ready_expect_transition_error(conn, ase_id, ase_cp);
489 }
490 
ZTEST_F(test_ase_state_transition_invalid,test_client_source_state_disabling)491 ZTEST_F(test_ase_state_transition_invalid, test_client_source_state_disabling)
492 {
493 	const struct bt_gatt_attr *ase_cp = fixture->ase_cp;
494 	struct bt_bap_stream *stream = &fixture->stream;
495 	struct bt_conn *conn = &fixture->conn;
496 	struct bt_iso_chan *chan;
497 	uint8_t ase_id;
498 
499 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
500 
501 	ase_id = test_ase_id_get(fixture->ase_src);
502 	test_preamble_state_disabling(conn, ase_id, stream, &chan);
503 
504 	test_client_config_codec_expect_transition_error(conn, ase_id, ase_cp);
505 	test_client_config_qos_expect_transition_error(conn, ase_id, ase_cp);
506 	test_client_enable_expect_transition_error(conn, ase_id, ase_cp);
507 	test_client_receiver_start_ready_expect_transition_error(conn, ase_id, ase_cp);
508 	test_client_disable_expect_transition_error(conn, ase_id, ase_cp);
509 	test_client_update_metadata_expect_transition_error(conn, ase_id, ase_cp);
510 }
511 
ZTEST_F(test_ase_state_transition_invalid,test_client_source_state_releasing)512 ZTEST_F(test_ase_state_transition_invalid, test_client_source_state_releasing)
513 {
514 	const struct bt_gatt_attr *ase_cp = fixture->ase_cp;
515 	struct bt_bap_stream *stream = &fixture->stream;
516 	struct bt_conn *conn = &fixture->conn;
517 	struct bt_iso_chan *chan;
518 	uint8_t ase_id;
519 
520 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
521 
522 	ase_id = test_ase_id_get(fixture->ase_src);
523 	test_preamble_state_releasing(conn, ase_id, stream, &chan, true);
524 	expect_ase_state_releasing(conn, fixture->ase_src);
525 
526 	test_client_config_codec_expect_transition_error(conn, ase_id, ase_cp);
527 	test_client_config_qos_expect_transition_error(conn, ase_id, ase_cp);
528 	test_client_enable_expect_transition_error(conn, ase_id, ase_cp);
529 	test_client_receiver_start_ready_expect_transition_error(conn, ase_id, ase_cp);
530 	test_client_receiver_stop_ready_expect_transition_error(conn, ase_id, ase_cp);
531 	test_client_disable_expect_transition_error(conn, ase_id, ase_cp);
532 	test_client_update_metadata_expect_transition_error(conn, ase_id, ase_cp);
533 }
534 
test_server_config_codec_expect_error(struct bt_bap_stream * stream)535 static void test_server_config_codec_expect_error(struct bt_bap_stream *stream)
536 {
537 	struct bt_audio_codec_cfg codec_cfg = BT_AUDIO_CODEC_LC3_CONFIG(
538 		BT_AUDIO_CODEC_CFG_FREQ_16KHZ, BT_AUDIO_CODEC_CFG_DURATION_10,
539 		BT_AUDIO_LOCATION_FRONT_LEFT, 40U, 1, BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
540 	int err;
541 
542 	err = bt_bap_stream_reconfig(stream, &codec_cfg);
543 	zassert_false(err == 0, "bt_bap_stream_reconfig unexpected success");
544 }
545 
test_server_receiver_start_ready_expect_error(struct bt_bap_stream * stream)546 static void test_server_receiver_start_ready_expect_error(struct bt_bap_stream *stream)
547 {
548 	int err;
549 
550 	err = bt_bap_stream_start(stream);
551 	zassert_false(err == 0, "bt_bap_stream_start unexpected success");
552 }
553 
test_server_disable_expect_error(struct bt_bap_stream * stream)554 static void test_server_disable_expect_error(struct bt_bap_stream *stream)
555 {
556 	int err;
557 
558 	err = bt_bap_stream_disable(stream);
559 	zassert_false(err == 0, "bt_bap_stream_disable unexpected success");
560 }
561 
562 #if defined(CONFIG_BT_BAP_UNICAST_CLIENT)
563 #include "bap_endpoint.h"
564 
test_server_config_qos_expect_error(struct bt_bap_stream * stream)565 static void test_server_config_qos_expect_error(struct bt_bap_stream *stream)
566 {
567 	struct bt_bap_unicast_group group;
568 	int err;
569 
570 	sys_slist_init(&group.streams);
571 	sys_slist_append(&group.streams, &stream->_node);
572 
573 	err = bt_bap_stream_qos(stream->conn, &group);
574 	zassert_false(err == 0, "bt_bap_stream_qos unexpected success");
575 }
576 
test_server_enable_expect_error(struct bt_bap_stream * stream)577 static void test_server_enable_expect_error(struct bt_bap_stream *stream)
578 {
579 	const uint8_t meta[] = {
580 		BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_STREAM_CONTEXT,
581 				    BT_BYTES_LIST_LE16(BT_AUDIO_CONTEXT_TYPE_RINGTONE)),
582 	};
583 	int err;
584 
585 	err = bt_bap_stream_enable(stream, meta, ARRAY_SIZE(meta));
586 	zassert_false(err == 0, "bt_bap_stream_enable unexpected success");
587 }
588 
test_server_receiver_stop_ready_expect_error(struct bt_bap_stream * stream)589 static void test_server_receiver_stop_ready_expect_error(struct bt_bap_stream *stream)
590 {
591 	int err;
592 
593 	err = bt_bap_stream_stop(stream);
594 	zassert_false(err == 0, "bt_bap_stream_stop unexpected success");
595 }
596 #else
597 #define test_server_config_qos_expect_error(...)
598 #define test_server_enable_expect_error(...)
599 #define test_server_receiver_stop_ready_expect_error(...)
600 #endif /* CONFIG_BT_BAP_UNICAST_CLIENT */
601 
test_server_update_metadata_expect_error(struct bt_bap_stream * stream)602 static void test_server_update_metadata_expect_error(struct bt_bap_stream *stream)
603 {
604 	const uint8_t meta[] = {
605 		BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_STREAM_CONTEXT,
606 				    BT_BYTES_LIST_LE16(BT_AUDIO_CONTEXT_TYPE_RINGTONE)),
607 	};
608 	int err;
609 
610 	err = bt_bap_stream_metadata(stream, meta, ARRAY_SIZE(meta));
611 	zassert_false(err == 0, "bt_bap_stream_metadata unexpected success");
612 }
613 
ZTEST_F(test_ase_state_transition_invalid,test_server_sink_state_codec_configured)614 ZTEST_F(test_ase_state_transition_invalid, test_server_sink_state_codec_configured)
615 {
616 	struct bt_bap_stream *stream = &fixture->stream;
617 	struct bt_conn *conn = &fixture->conn;
618 	uint8_t ase_id;
619 
620 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
621 
622 	ase_id = test_ase_id_get(fixture->ase_snk);
623 	test_preamble_state_codec_configured(conn, ase_id, stream);
624 
625 	test_server_config_qos_expect_error(stream);
626 	test_server_enable_expect_error(stream);
627 	test_server_receiver_start_ready_expect_error(stream);
628 	test_server_disable_expect_error(stream);
629 	test_server_receiver_stop_ready_expect_error(stream);
630 	test_server_update_metadata_expect_error(stream);
631 }
632 
ZTEST_F(test_ase_state_transition_invalid,test_server_sink_state_qos_configured)633 ZTEST_F(test_ase_state_transition_invalid, test_server_sink_state_qos_configured)
634 {
635 	struct bt_bap_stream *stream = &fixture->stream;
636 	struct bt_conn *conn = &fixture->conn;
637 	uint8_t ase_id;
638 
639 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
640 
641 	ase_id = test_ase_id_get(fixture->ase_snk);
642 	test_preamble_state_qos_configured(conn, ase_id, stream);
643 
644 	test_server_config_qos_expect_error(stream);
645 	test_server_enable_expect_error(stream);
646 	test_server_receiver_start_ready_expect_error(stream);
647 	test_server_disable_expect_error(stream);
648 	test_server_receiver_stop_ready_expect_error(stream);
649 	test_server_update_metadata_expect_error(stream);
650 }
651 
ZTEST_F(test_ase_state_transition_invalid,test_server_sink_state_enabling)652 ZTEST_F(test_ase_state_transition_invalid, test_server_sink_state_enabling)
653 {
654 	struct bt_bap_stream *stream = &fixture->stream;
655 	struct bt_conn *conn = &fixture->conn;
656 	uint8_t ase_id;
657 
658 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
659 
660 	ase_id = test_ase_id_get(fixture->ase_snk);
661 	test_preamble_state_enabling(conn, ase_id, stream);
662 
663 	test_server_config_codec_expect_error(stream);
664 	test_server_config_qos_expect_error(stream);
665 	test_server_enable_expect_error(stream);
666 	test_server_receiver_stop_ready_expect_error(stream);
667 }
668 
ZTEST_F(test_ase_state_transition_invalid,test_server_sink_state_streaming)669 ZTEST_F(test_ase_state_transition_invalid, test_server_sink_state_streaming)
670 {
671 	struct bt_bap_stream *stream = &fixture->stream;
672 	struct bt_conn *conn = &fixture->conn;
673 	struct bt_iso_chan *chan;
674 	uint8_t ase_id;
675 
676 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
677 
678 	ase_id = test_ase_id_get(fixture->ase_snk);
679 	test_preamble_state_streaming(conn, ase_id, stream, &chan, false);
680 
681 	test_server_config_codec_expect_error(stream);
682 	test_server_config_qos_expect_error(stream);
683 	test_server_enable_expect_error(stream);
684 	test_server_receiver_start_ready_expect_error(stream);
685 	test_server_receiver_stop_ready_expect_error(stream);
686 }
687 
ZTEST_F(test_ase_state_transition_invalid,test_server_sink_state_releasing)688 ZTEST_F(test_ase_state_transition_invalid, test_server_sink_state_releasing)
689 {
690 	struct bt_bap_stream *stream = &fixture->stream;
691 	struct bt_conn *conn = &fixture->conn;
692 	struct bt_iso_chan *chan;
693 	uint8_t ase_id;
694 
695 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
696 
697 	ase_id = test_ase_id_get(fixture->ase_snk);
698 	test_preamble_state_releasing(conn, ase_id, stream, &chan, false);
699 	expect_ase_state_releasing(conn, fixture->ase_snk);
700 
701 	test_server_config_codec_expect_error(stream);
702 	test_server_config_qos_expect_error(stream);
703 	test_server_enable_expect_error(stream);
704 	test_server_receiver_start_ready_expect_error(stream);
705 	test_server_disable_expect_error(stream);
706 	test_server_receiver_stop_ready_expect_error(stream);
707 	test_server_update_metadata_expect_error(stream);
708 }
709 
ZTEST_F(test_ase_state_transition_invalid,test_server_source_state_codec_configured)710 ZTEST_F(test_ase_state_transition_invalid, test_server_source_state_codec_configured)
711 {
712 	struct bt_bap_stream *stream = &fixture->stream;
713 	struct bt_conn *conn = &fixture->conn;
714 	uint8_t ase_id;
715 
716 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
717 
718 	ase_id = test_ase_id_get(fixture->ase_src);
719 	test_preamble_state_codec_configured(conn, ase_id, stream);
720 
721 	test_server_config_qos_expect_error(stream);
722 	test_server_enable_expect_error(stream);
723 	test_server_receiver_start_ready_expect_error(stream);
724 	test_server_disable_expect_error(stream);
725 	test_server_receiver_stop_ready_expect_error(stream);
726 	test_server_update_metadata_expect_error(stream);
727 }
728 
ZTEST_F(test_ase_state_transition_invalid,test_server_source_state_qos_configured)729 ZTEST_F(test_ase_state_transition_invalid, test_server_source_state_qos_configured)
730 {
731 	struct bt_bap_stream *stream = &fixture->stream;
732 	struct bt_conn *conn = &fixture->conn;
733 	uint8_t ase_id;
734 
735 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
736 
737 	ase_id = test_ase_id_get(fixture->ase_src);
738 	test_preamble_state_qos_configured(conn, ase_id, stream);
739 
740 	test_server_config_qos_expect_error(stream);
741 	test_server_enable_expect_error(stream);
742 	test_server_receiver_start_ready_expect_error(stream);
743 	test_server_disable_expect_error(stream);
744 	test_server_receiver_stop_ready_expect_error(stream);
745 	test_server_update_metadata_expect_error(stream);
746 }
747 
ZTEST_F(test_ase_state_transition_invalid,test_server_source_state_enabling)748 ZTEST_F(test_ase_state_transition_invalid, test_server_source_state_enabling)
749 {
750 	struct bt_bap_stream *stream = &fixture->stream;
751 	struct bt_conn *conn = &fixture->conn;
752 	uint8_t ase_id;
753 
754 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
755 
756 	ase_id = test_ase_id_get(fixture->ase_src);
757 	test_preamble_state_enabling(conn, ase_id, stream);
758 
759 	test_server_config_codec_expect_error(stream);
760 	test_server_config_qos_expect_error(stream);
761 	test_server_enable_expect_error(stream);
762 	test_server_receiver_stop_ready_expect_error(stream);
763 }
764 
ZTEST_F(test_ase_state_transition_invalid,test_server_source_state_streaming)765 ZTEST_F(test_ase_state_transition_invalid, test_server_source_state_streaming)
766 {
767 	struct bt_bap_stream *stream = &fixture->stream;
768 	struct bt_conn *conn = &fixture->conn;
769 	struct bt_iso_chan *chan;
770 	uint8_t ase_id;
771 
772 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
773 
774 	ase_id = test_ase_id_get(fixture->ase_src);
775 	test_preamble_state_streaming(conn, ase_id, stream, &chan, true);
776 
777 	test_server_config_codec_expect_error(stream);
778 	test_server_config_qos_expect_error(stream);
779 	test_server_enable_expect_error(stream);
780 	test_server_receiver_start_ready_expect_error(stream);
781 	test_server_receiver_stop_ready_expect_error(stream);
782 }
783 
ZTEST_F(test_ase_state_transition_invalid,test_server_source_state_disabling)784 ZTEST_F(test_ase_state_transition_invalid, test_server_source_state_disabling)
785 {
786 	struct bt_bap_stream *stream = &fixture->stream;
787 	struct bt_conn *conn = &fixture->conn;
788 	struct bt_iso_chan *chan;
789 	uint8_t ase_id;
790 
791 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
792 
793 	ase_id = test_ase_id_get(fixture->ase_src);
794 	test_preamble_state_disabling(conn, ase_id, stream, &chan);
795 
796 	test_server_config_codec_expect_error(stream);
797 	test_server_config_qos_expect_error(stream);
798 	test_server_enable_expect_error(stream);
799 	test_server_receiver_start_ready_expect_error(stream);
800 	test_server_disable_expect_error(stream);
801 	test_server_receiver_stop_ready_expect_error(stream);
802 	test_server_update_metadata_expect_error(stream);
803 }
804 
ZTEST_F(test_ase_state_transition_invalid,test_server_source_state_releasing)805 ZTEST_F(test_ase_state_transition_invalid, test_server_source_state_releasing)
806 {
807 	struct bt_bap_stream *stream = &fixture->stream;
808 	struct bt_conn *conn = &fixture->conn;
809 	struct bt_iso_chan *chan;
810 	uint8_t ase_id;
811 
812 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
813 
814 	ase_id = test_ase_id_get(fixture->ase_src);
815 	test_preamble_state_releasing(conn, ase_id, stream, &chan, true);
816 	expect_ase_state_releasing(conn, fixture->ase_src);
817 
818 	test_server_config_codec_expect_error(stream);
819 	test_server_config_qos_expect_error(stream);
820 	test_server_enable_expect_error(stream);
821 	test_server_receiver_start_ready_expect_error(stream);
822 	test_server_disable_expect_error(stream);
823 	test_server_receiver_stop_ready_expect_error(stream);
824 	test_server_update_metadata_expect_error(stream);
825 }
826