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