1 /* test_ase_control_params.c - ASE Control Operations with invalid arguments */
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_control_params_fixture {
29 	struct bt_conn conn;
30 	struct bt_bap_stream stream;
31 	const struct bt_gatt_attr *ase_cp;
32 	const struct bt_gatt_attr *ase;
33 };
34 
test_ase_control_params_setup(void)35 static void *test_ase_control_params_setup(void)
36 {
37 	struct test_ase_control_params_fixture *fixture;
38 
39 	fixture = malloc(sizeof(*fixture));
40 	zassert_not_null(fixture);
41 
42 	test_conn_init(&fixture->conn);
43 	fixture->ase_cp = test_ase_control_point_get();
44 
45 	if (IS_ENABLED(CONFIG_BT_ASCS_ASE_SNK)) {
46 		test_ase_snk_get(1, &fixture->ase);
47 	} else {
48 		test_ase_src_get(1, &fixture->ase);
49 	}
50 
51 	return fixture;
52 }
53 
test_ase_control_params_before(void * f)54 static void test_ase_control_params_before(void *f)
55 {
56 	struct test_ase_control_params_fixture *fixture = f;
57 
58 	ARG_UNUSED(fixture);
59 
60 	bt_bap_unicast_server_register_cb(&mock_bap_unicast_server_cb);
61 }
62 
test_ase_control_params_after(void * f)63 static void test_ase_control_params_after(void *f)
64 {
65 	bt_bap_unicast_server_unregister_cb(&mock_bap_unicast_server_cb);
66 }
67 
test_ase_control_params_teardown(void * f)68 static void test_ase_control_params_teardown(void *f)
69 {
70 	struct test_ase_control_params_fixture *fixture = f;
71 
72 	free(fixture);
73 }
74 
75 ZTEST_SUITE(test_ase_control_params, NULL, test_ase_control_params_setup,
76 	    test_ase_control_params_before, test_ase_control_params_after,
77 	    test_ase_control_params_teardown);
78 
ZTEST_F(test_ase_control_params,test_sink_ase_control_operation_zero_length_write)79 ZTEST_F(test_ase_control_params, test_sink_ase_control_operation_zero_length_write)
80 {
81 	uint8_t buf[] = {};
82 	ssize_t ret;
83 
84 	ret = fixture->ase_cp->write(&fixture->conn, fixture->ase_cp, (void *)buf, 0, 0, 0);
85 	zassert_true(ret < 0, "ase_cp_attr->write returned unexpected (err 0x%02x)",
86 		     BT_GATT_ERR(ret));
87 }
88 
test_expect_unsupported_opcode(struct test_ase_control_params_fixture * fixture,uint8_t opcode)89 static void test_expect_unsupported_opcode(struct test_ase_control_params_fixture *fixture,
90 					   uint8_t opcode)
91 {
92 	const uint8_t buf[] = {
93 		opcode, /* Opcode */
94 		0x01,   /* Number_of_ASEs */
95 		0x01,   /* ASE_ID[0] */
96 	};
97 	const uint8_t data_expected[] = {
98 		opcode, /* Opcode */
99 		0xFF,   /* Number_of_ASEs */
100 		0x00,   /* ASE_ID[0] */
101 		0x01,   /* Response_Code[0] = Unsupported Opcode */
102 		0x00,   /* Reason[0] */
103 	};
104 
105 	fixture->ase_cp->write(&fixture->conn, fixture->ase_cp, (void *)buf, sizeof(buf), 0, 0);
106 
107 	expect_bt_gatt_notify_cb_called_once(&fixture->conn, BT_UUID_ASCS_ASE_CP,
108 					     fixture->ase_cp, data_expected, sizeof(data_expected));
109 }
110 
ZTEST_F(test_ase_control_params,test_unsupported_opcode_0x00)111 ZTEST_F(test_ase_control_params, test_unsupported_opcode_0x00)
112 {
113 	test_expect_unsupported_opcode(fixture, 0x00);
114 }
115 
ZTEST_F(test_ase_control_params,test_unsupported_opcode_rfu)116 ZTEST_F(test_ase_control_params, test_unsupported_opcode_rfu)
117 {
118 	test_expect_unsupported_opcode(fixture, 0x09);
119 }
120 
test_codec_configure_expect_invalid_length(struct test_ase_control_params_fixture * fixture,const uint8_t * buf,size_t len)121 static void test_codec_configure_expect_invalid_length(
122 	struct test_ase_control_params_fixture *fixture, const uint8_t *buf, size_t len)
123 {
124 	const uint8_t data_expected[] = {
125 		0x01,           /* Opcode = Config Codec */
126 		0xFF,           /* Number_of_ASEs */
127 		0x00,           /* ASE_ID[0] */
128 		0x02,           /* Response_Code[0] = Invalid Length */
129 		0x00,           /* Reason[0] */
130 	};
131 
132 	fixture->ase_cp->write(&fixture->conn, fixture->ase_cp, buf, len, 0, 0);
133 
134 	expect_bt_gatt_notify_cb_called_once(&fixture->conn, BT_UUID_ASCS_ASE_CP,
135 					     fixture->ase_cp, data_expected, sizeof(data_expected));
136 }
137 
138 /*
139  *  Test correctly formatted ASE Control Point 'Invalid Length' notification is sent
140  *
141  *  ASCS_v1.0; 5 ASE Control operations
142  *  "A client-initiated ASE Control operation shall be defined as an invalid length operation
143  *   if the Number_of_ASEs parameter value is less than 1"
144  *
145  *  Constraints:
146  *   - Number_of_ASEs is set to 0
147  *   - Config Codec operation parameter array is valid
148  *
149  *  Expected behaviour:
150  *   - "If the Response_Code value is 0x01 or 0x02, Number_of_ASEs shall be set to 0xFF."
151  *   - ASE Control Point notification is correctly formatted
152  */
ZTEST_F(test_ase_control_params,test_codec_configure_number_of_ases_0x00)153 ZTEST_F(test_ase_control_params, test_codec_configure_number_of_ases_0x00)
154 {
155 	const uint8_t buf[] = {
156 		0x01,           /* Opcode = Config Codec */
157 		0x00,           /* Number_of_ASEs */
158 		0x01,           /* ASE_ID[0] */
159 		0x01,           /* Target_Latency[0] = Target low latency */
160 		0x02,           /* Target_PHY[0] = LE 2M PHY */
161 		0x06,           /* Codec_ID[0].Coding_Format = LC3 */
162 		0x00, 0x00,     /* Codec_ID[0].Company_ID */
163 		0x00, 0x00,     /* Codec_ID[0].Vendor_Specific_Codec_ID */
164 		0x00,           /* Codec_Specific_Configuration_Length[0] */
165 	};
166 
167 	test_codec_configure_expect_invalid_length(fixture, buf, sizeof(buf));
168 }
169 
170 /*
171  *  Test correctly formatted ASE Control Point 'Invalid Length' notification is sent
172  *
173  *  ASCS_v1.0; 5 ASE Control operations
174  *  "A client-initiated ASE Control operation shall be defined as an invalid length operation(...)
175  *   if the Number_of_ASEs parameter value does not match the number of parameter arrays written by
176  *   the client"
177  *
178  *  Constraints:
179  *   - Number_of_ASEs is set to 1
180  *   - Config Codec operation parameter arrays != Number_of_ASEs and is set to 2
181  *
182  *  Expected behaviour:
183  *   - "If the Response_Code value is 0x01 or 0x02, Number_of_ASEs shall be set to 0xFF."
184  *   - ASE Control Point notification is correctly formatted
185  */
ZTEST_F(test_ase_control_params,test_codec_configure_too_many_parameter_arrays)186 ZTEST_F(test_ase_control_params, test_codec_configure_too_many_parameter_arrays)
187 {
188 	const uint8_t buf[] = {
189 		0x01,           /* Opcode = Config Codec */
190 		0x01,           /* Number_of_ASEs */
191 		0x01,           /* ASE_ID[0] */
192 		0x01,           /* Target_Latency[0] = Target low latency */
193 		0x02,           /* Target_PHY[0] = LE 2M PHY */
194 		0x06,           /* Codec_ID[0].Coding_Format = LC3 */
195 		0x00, 0x00,     /* Codec_ID[0].Company_ID */
196 		0x00, 0x00,     /* Codec_ID[0].Vendor_Specific_Codec_ID */
197 		0x00,           /* Codec_Specific_Configuration_Length[0] */
198 		0x02,           /* ASE_ID[1] */
199 		0x01,           /* Target_Latency[1] = Target low latency */
200 		0x02,           /* Target_PHY[1] = LE 2M PHY */
201 		0x06,           /* Codec_ID[1].Coding_Format = LC3 */
202 		0x00, 0x00,     /* Codec_ID[1].Company_ID */
203 		0x00, 0x00,     /* Codec_ID[1].Vendor_Specific_Codec_ID */
204 		0x00,           /* Codec_Specific_Configuration_Length[1] */
205 	};
206 
207 	test_codec_configure_expect_invalid_length(fixture, buf, sizeof(buf));
208 }
209 
210 /*
211  *  Test correctly formatted ASE Control Point 'Invalid Length' notification is sent
212  *
213  *  ASCS_v1.0; 5 ASE Control operations
214  *  "A client-initiated ASE Control operation shall be defined as an invalid length operation
215  *   if the total length of all parameters written by the client is not equal to the total length
216  *   of all fixed parameters plus the length of any variable length parameters for that operation"
217  *
218  *  Constraints:
219  *   - Number_of_ASEs is set to 1
220  *   - Config Codec operation parameter arrays == Number_of_ASEs
221  *   - Codec_Specific_Configuration_Length[i] > sizeof(Codec_Specific_Configuration[i])
222  *
223  *  Expected behaviour:
224  *   - "If the Response_Code value is 0x01 or 0x02, Number_of_ASEs shall be set to 0xFF."
225  *   - ASE Control Point notification is correctly formatted
226  */
ZTEST_F(test_ase_control_params,test_codec_specific_configuration_too_short)227 ZTEST_F(test_ase_control_params, test_codec_specific_configuration_too_short)
228 {
229 	const uint8_t buf[] = {
230 		0x01,           /* Opcode = Config Codec */
231 		0x01,           /* Number_of_ASEs */
232 		0x01,           /* ASE_ID[0] */
233 		0x01,           /* Target_Latency[0] = Target low latency */
234 		0x02,           /* Target_PHY[0] = LE 2M PHY */
235 		0x06,           /* Codec_ID[0].Coding_Format = LC3 */
236 		0x00, 0x00,     /* Codec_ID[0].Company_ID */
237 		0x00, 0x00,     /* Codec_ID[0].Vendor_Specific_Codec_ID */
238 		0x05,           /* Codec_Specific_Configuration_Length[0] */
239 		0x00, 0x00,     /* Codec_Specific_Configuration[0] */
240 		0x00, 0x00,
241 	};
242 
243 	test_codec_configure_expect_invalid_length(fixture, buf, sizeof(buf));
244 }
245 
246 /*
247  *  Test correctly formatted ASE Control Point 'Invalid Length' notification is sent
248  *
249  *  ASCS_v1.0; 5 ASE Control operations
250  *  "A client-initiated ASE Control operation shall be defined as an invalid length operation
251  *   if the total length of all parameters written by the client is not equal to the total length
252  *   of all fixed parameters plus the length of any variable length parameters for that operation"
253  *
254  *  Constraints:
255  *   - Number_of_ASEs is set to 1
256  *   - Config Codec operation parameter arrays == Number_of_ASEs
257  *   - Codec_Specific_Configuration_Length[i] < sizeof(Codec_Specific_Configuration[i])
258  *
259  *  Expected behaviour:
260  *   - "If the Response_Code value is 0x01 or 0x02, Number_of_ASEs shall be set to 0xFF."
261  *   - ASE Control Point notification is correctly formatted
262  */
ZTEST_F(test_ase_control_params,test_codec_specific_configuration_too_long)263 ZTEST_F(test_ase_control_params, test_codec_specific_configuration_too_long)
264 {
265 	const uint8_t buf[] = {
266 		0x01,           /* Opcode = Config Codec */
267 		0x01,           /* Number_of_ASEs */
268 		0x01,           /* ASE_ID[0] */
269 		0x01,           /* Target_Latency[0] = Target low latency */
270 		0x02,           /* Target_PHY[0] = LE 2M PHY */
271 		0x06,           /* Codec_ID[0].Coding_Format = LC3 */
272 		0x00, 0x00,     /* Codec_ID[0].Company_ID */
273 		0x00, 0x00,     /* Codec_ID[0].Vendor_Specific_Codec_ID */
274 		0x05,           /* Codec_Specific_Configuration_Length[0] */
275 		0x00, 0x00,     /* Codec_Specific_Configuration[0] */
276 		0x00, 0x00,
277 		0x00, 0x00,
278 	};
279 
280 	test_codec_configure_expect_invalid_length(fixture, buf, sizeof(buf));
281 }
282 
283 /*
284  *  Test correctly formatted ASE Control Point 'Invalid ASE_ID' notification is sent
285  *
286  *  Constraints:
287  *   - Number_of_ASEs is set to 1
288  *   - Requested ASE_ID is not present on the server.
289  *
290  *  Expected behaviour:
291  *   - Correctly formatted ASE Control Point notification is sent with Invalid ASE_ID response code.
292  */
ZTEST_F(test_ase_control_params,test_codec_configure_invalid_ase_id_0x00)293 ZTEST_F(test_ase_control_params, test_codec_configure_invalid_ase_id_0x00)
294 {
295 	const uint8_t ase_id_invalid = 0x00;
296 	const uint8_t buf[] = {
297 		0x01,           /* Opcode = Config Codec */
298 		0x01,           /* Number_of_ASEs */
299 		ase_id_invalid, /* ASE_ID[0] */
300 		0x01,           /* Target_Latency[0] = Target low latency */
301 		0x02,           /* Target_PHY[0] = LE 2M PHY */
302 		0x06,           /* Codec_ID[0].Coding_Format = LC3 */
303 		0x00, 0x00,     /* Codec_ID[0].Company_ID */
304 		0x00, 0x00,     /* Codec_ID[0].Vendor_Specific_Codec_ID */
305 		0x00,           /* Codec_Specific_Configuration_Length[0] */
306 	};
307 	const uint8_t data_expected[] = {
308 		0x01,           /* Opcode = Config Codec */
309 		0x01,           /* Number_of_ASEs */
310 		ase_id_invalid, /* ASE_ID[0] */
311 		0x03,           /* Response_Code[0] = Invalid ASE_ID */
312 		0x00,           /* Reason[0] */
313 	};
314 
315 	fixture->ase_cp->write(&fixture->conn, fixture->ase_cp, buf, sizeof(buf), 0, 0);
316 
317 	expect_bt_gatt_notify_cb_called_once(&fixture->conn, BT_UUID_ASCS_ASE_CP,
318 					     fixture->ase_cp, data_expected, sizeof(data_expected));
319 }
320 
321 static struct bt_bap_stream test_stream;
322 static const struct bt_audio_codec_qos_pref qos_pref =
323 	BT_AUDIO_CODEC_QOS_PREF(true, BT_GAP_LE_PHY_2M, 0x02, 10, 40000, 40000, 40000, 40000);
324 
unicast_server_cb_config_custom_fake(struct bt_conn * conn,const struct bt_bap_ep * ep,enum bt_audio_dir dir,const struct bt_audio_codec_cfg * codec_cfg,struct bt_bap_stream ** stream,struct bt_audio_codec_qos_pref * const pref,struct bt_bap_ascs_rsp * rsp)325 static int unicast_server_cb_config_custom_fake(struct bt_conn *conn, const struct bt_bap_ep *ep,
326 						enum bt_audio_dir dir,
327 						const struct bt_audio_codec_cfg *codec_cfg,
328 						struct bt_bap_stream **stream,
329 						struct bt_audio_codec_qos_pref *const pref,
330 						struct bt_bap_ascs_rsp *rsp)
331 {
332 	*stream = &test_stream;
333 	*pref = qos_pref;
334 	*rsp = BT_BAP_ASCS_RSP(BT_BAP_ASCS_RSP_CODE_SUCCESS, BT_BAP_ASCS_REASON_NONE);
335 
336 	bt_bap_stream_cb_register(*stream, &mock_bap_stream_ops);
337 
338 	return 0;
339 }
340 
ZTEST_F(test_ase_control_params,test_codec_configure_invalid_ase_id_unavailable)341 ZTEST_F(test_ase_control_params, test_codec_configure_invalid_ase_id_unavailable)
342 {
343 	const uint8_t ase_id_valid = 0x01;
344 	const uint8_t ase_id_invalid = CONFIG_BT_ASCS_ASE_SNK_COUNT +
345 				       CONFIG_BT_ASCS_ASE_SRC_COUNT + 1;
346 	const uint8_t buf[] = {
347 		0x01,           /* Opcode = Config Codec */
348 		0x02,           /* Number_of_ASEs */
349 		ase_id_invalid, /* ASE_ID[0] */
350 		0x01,           /* Target_Latency[0] = Target low latency */
351 		0x02,           /* Target_PHY[0] = LE 2M PHY */
352 		0x06,           /* Codec_ID[0].Coding_Format = LC3 */
353 		0x00, 0x00,     /* Codec_ID[0].Company_ID */
354 		0x00, 0x00,     /* Codec_ID[0].Vendor_Specific_Codec_ID */
355 		0x04,           /* Codec_Specific_Configuration_Length[0] */
356 		0x00, 0x00,     /* Codec_Specific_Configuration[0] */
357 		0x00, 0x00,
358 		ase_id_valid,   /* ASE_ID[1] */
359 		0x01,           /* Target_Latency[1] = Target low latency */
360 		0x02,           /* Target_PHY[1] = LE 2M PHY */
361 		0x06,           /* Codec_ID[1].Coding_Format = LC3 */
362 		0x00, 0x00,     /* Codec_ID[1].Company_ID */
363 		0x00, 0x00,     /* Codec_ID[1].Vendor_Specific_Codec_ID */
364 		0x00,           /* Codec_Specific_Configuration_Length[1] */
365 	};
366 	const uint8_t data_expected[] = {
367 		0x01,           /* Opcode = Config Codec */
368 		0x02,           /* Number_of_ASEs */
369 		ase_id_invalid, /* ASE_ID[0] */
370 		0x03,           /* Response_Code[0] = Invalid ASE_ID */
371 		0x00,           /* Reason[0] */
372 		ase_id_valid,   /* ASE_ID[1] */
373 		0x00,           /* Response_Code[1] = Success */
374 		0x00,           /* Reason[1] */
375 	};
376 
377 	mock_bap_unicast_server_cb_config_fake.custom_fake = unicast_server_cb_config_custom_fake;
378 
379 	fixture->ase_cp->write(&fixture->conn, fixture->ase_cp, buf, sizeof(buf), 0, 0);
380 
381 	expect_bt_gatt_notify_cb_called_once(&fixture->conn, BT_UUID_ASCS_ASE_CP,
382 					     fixture->ase_cp, data_expected, sizeof(data_expected));
383 }
384 
test_target_latency_out_of_range(struct test_ase_control_params_fixture * fixture,uint8_t target_latency)385 static void test_target_latency_out_of_range(struct test_ase_control_params_fixture *fixture,
386 					     uint8_t target_latency)
387 {
388 	const uint8_t buf[] = {
389 		0x01,           /* Opcode = Config Codec */
390 		0x01,           /* Number_of_ASEs */
391 		0x01,           /* ASE_ID[0] */
392 		target_latency, /* Target_Latency[0] */
393 		0x02,           /* Target_PHY[0] = LE 2M PHY */
394 		0x06,           /* Codec_ID[0].Coding_Format = LC3 */
395 		0x00, 0x00,     /* Codec_ID[0].Company_ID */
396 		0x00, 0x00,     /* Codec_ID[0].Vendor_Specific_Codec_ID */
397 		0x00,           /* Codec_Specific_Configuration_Length[0] */
398 	};
399 	const uint8_t data_expected[] = {
400 		0x01,           /* Opcode = Config Codec */
401 		0x01,           /* Number_of_ASEs */
402 		0x01,           /* ASE_ID[0] */
403 		0x00,           /* Response_Code[0] = Success */
404 		0x00,           /* Reason[0] */
405 	};
406 
407 	fixture->ase_cp->write(&fixture->conn, fixture->ase_cp, buf, sizeof(buf), 0, 0);
408 
409 	expect_bt_gatt_notify_cb_called_once(&fixture->conn, BT_UUID_ASCS_ASE_CP,
410 					     fixture->ase_cp, data_expected, sizeof(data_expected));
411 }
412 
ZTEST_F(test_ase_control_params,test_target_latency_out_of_range_0x00)413 ZTEST_F(test_ase_control_params, test_target_latency_out_of_range_0x00)
414 {
415 	/* TODO: Remove once resolved */
416 	Z_TEST_SKIP_IFNDEF(BUG_55794);
417 
418 	test_target_latency_out_of_range(fixture, 0x00);
419 }
420 
ZTEST_F(test_ase_control_params,test_target_latency_out_of_range_0x04)421 ZTEST_F(test_ase_control_params, test_target_latency_out_of_range_0x04)
422 {
423 	/* TODO: Remove once resolved */
424 	Z_TEST_SKIP_IFNDEF(BUG_55794);
425 
426 	test_target_latency_out_of_range(fixture, 0x04);
427 }
428 
test_target_phy_out_of_range(struct test_ase_control_params_fixture * fixture,uint8_t target_phy)429 static void test_target_phy_out_of_range(struct test_ase_control_params_fixture *fixture,
430 					 uint8_t target_phy)
431 {
432 	const uint8_t buf[] = {
433 		0x01,           /* Opcode = Config Codec */
434 		0x01,           /* Number_of_ASEs */
435 		0x01,           /* ASE_ID[0] */
436 		0x01,           /* Target_Latency[0] */
437 		target_phy,     /* Target_PHY[0] */
438 		0x06,           /* Codec_ID[0].Coding_Format = LC3 */
439 		0x00, 0x00,     /* Codec_ID[0].Company_ID */
440 		0x00, 0x00,     /* Codec_ID[0].Vendor_Specific_Codec_ID */
441 		0x00,           /* Codec_Specific_Configuration_Length[0] */
442 	};
443 	const uint8_t data_expected[] = {
444 		0x01,           /* Opcode = Config Codec */
445 		0x01,           /* Number_of_ASEs */
446 		0x01,           /* ASE_ID[0] */
447 		0x00,           /* Response_Code[0] = Success */
448 		0x00,           /* Reason[0] */
449 	};
450 
451 	fixture->ase_cp->write(&fixture->conn, fixture->ase_cp, buf, sizeof(buf), 0, 0);
452 
453 	expect_bt_gatt_notify_cb_called_once(&fixture->conn, BT_UUID_ASCS_ASE_CP,
454 					     fixture->ase_cp, data_expected, sizeof(data_expected));
455 }
456 
ZTEST_F(test_ase_control_params,test_target_phy_out_of_range_0x00)457 ZTEST_F(test_ase_control_params, test_target_phy_out_of_range_0x00)
458 {
459 	/* TODO: Remove once resolved */
460 	Z_TEST_SKIP_IFNDEF(BUG_55794);
461 
462 	test_target_phy_out_of_range(fixture, 0x00);
463 }
464 
ZTEST_F(test_ase_control_params,test_target_phy_out_of_range_0x04)465 ZTEST_F(test_ase_control_params, test_target_phy_out_of_range_0x04)
466 {
467 	/* TODO: Remove once resolved */
468 	Z_TEST_SKIP_IFNDEF(BUG_55794);
469 
470 	test_target_phy_out_of_range(fixture, 0x04);
471 }
472 
test_config_qos_expect_invalid_length(struct bt_conn * conn,uint8_t ase_id,const struct bt_gatt_attr * ase_cp,struct bt_bap_stream * stream,const uint8_t * buf,size_t len)473 static void test_config_qos_expect_invalid_length(struct bt_conn *conn, uint8_t ase_id,
474 						  const struct bt_gatt_attr *ase_cp,
475 						  struct bt_bap_stream *stream,
476 						  const uint8_t *buf, size_t len)
477 {
478 	const uint8_t data_expected[] = {
479 		0x02,           /* Opcode = Config QoS */
480 		0xFF,           /* Number_of_ASEs */
481 		0x00,           /* ASE_ID[0] */
482 		0x02,           /* Response_Code[0] = Invalid Length */
483 		0x00,           /* Reason[0] */
484 	};
485 
486 	test_preamble_state_codec_configured(conn, ase_id, stream);
487 
488 	ase_cp->write(conn, ase_cp, buf, len, 0, 0);
489 
490 	expect_bt_gatt_notify_cb_called_once(conn, BT_UUID_ASCS_ASE_CP, ase_cp, data_expected,
491 					     sizeof(data_expected));
492 }
493 
ZTEST_F(test_ase_control_params,test_config_qos_number_of_ases_0x00)494 ZTEST_F(test_ase_control_params, test_config_qos_number_of_ases_0x00)
495 {
496 	const uint8_t ase_id = test_ase_id_get(fixture->ase);
497 	const uint8_t buf[] = {
498 		0x02,                   /* Opcode = Config QoS */
499 		0x00,                   /* Number_of_ASEs */
500 		ase_id,                 /* ASE_ID[0] */
501 		0x01,                   /* CIG_ID[0] */
502 		0x01,                   /* CIS_ID[0] */
503 		0xFF, 0x00, 0x00,       /* SDU_Interval[0] */
504 		0x00,                   /* Framing[0] */
505 		0x02,                   /* PHY[0] */
506 		0x64, 0x00,             /* Max_SDU[0] */
507 		0x02,                   /* Retransmission_Number[0] */
508 		0x0A, 0x00,             /* Max_Transport_Latency[0] */
509 		0x40, 0x9C, 0x00,       /* Presentation_Delay[0] */
510 	};
511 
512 	test_config_qos_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp,
513 					      &fixture->stream, buf, sizeof(buf));
514 }
515 
ZTEST_F(test_ase_control_params,test_config_qos_too_short)516 ZTEST_F(test_ase_control_params, test_config_qos_too_short)
517 {
518 	const uint8_t ase_id = test_ase_id_get(fixture->ase);
519 	const uint8_t buf[] = {
520 		0x02,                   /* Opcode = Config QoS */
521 		0x01,                   /* Number_of_ASEs */
522 		ase_id,                 /* ASE_ID[0] */
523 		0x01,                   /* CIG_ID[0] */
524 		0x01,                   /* CIS_ID[0] */
525 		0xFF, 0x00, 0x00,       /* SDU_Interval[0] */
526 		0x00,                   /* Framing[0] */
527 		0x02,                   /* PHY[0] */
528 		0x64, 0x00,             /* Max_SDU[0] */
529 		0x02,                   /* Retransmission_Number[0] */
530 		0x0A, 0x00,             /* Max_Transport_Latency[0] */
531 		0x40, 0x9C, 0x00,       /* Presentation_Delay[0] */
532 		0x00,
533 	};
534 
535 	test_config_qos_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp,
536 					      &fixture->stream, buf, sizeof(buf));
537 }
538 
ZTEST_F(test_ase_control_params,test_config_qos_too_long)539 ZTEST_F(test_ase_control_params, test_config_qos_too_long)
540 {
541 	const uint8_t ase_id = test_ase_id_get(fixture->ase);
542 	const uint8_t buf[] = {
543 		0x02,                   /* Opcode = Config QoS */
544 		0x01,                   /* Number_of_ASEs */
545 		ase_id,                 /* ASE_ID[0] */
546 		0x01,                   /* CIG_ID[0] */
547 		0x01,                   /* CIS_ID[0] */
548 		0xFF, 0x00, 0x00,       /* SDU_Interval[0] */
549 		0x00,                   /* Framing[0] */
550 		0x02,                   /* PHY[0] */
551 		0x64, 0x00,             /* Max_SDU[0] */
552 		0x02,                   /* Retransmission_Number[0] */
553 		0x0A, 0x00,             /* Max_Transport_Latency[0] */
554 		0x40, 0x9C,             /* Presentation_Delay[0] */
555 	};
556 
557 	test_config_qos_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp,
558 					      &fixture->stream, buf, sizeof(buf));
559 }
560 
test_enable_expect_invalid_length(struct bt_conn * conn,uint8_t ase_id,const struct bt_gatt_attr * ase_cp,struct bt_bap_stream * stream,const uint8_t * buf,size_t len)561 static void test_enable_expect_invalid_length(struct bt_conn *conn, uint8_t ase_id,
562 					      const struct bt_gatt_attr *ase_cp,
563 					      struct bt_bap_stream *stream,
564 					      const uint8_t *buf, size_t len)
565 {
566 	const uint8_t data_expected[] = {
567 		0x03,           /* Opcode = Enable */
568 		0xFF,           /* Number_of_ASEs */
569 		0x00,           /* ASE_ID[0] */
570 		0x02,           /* Response_Code[0] = Invalid Length */
571 		0x00,           /* Reason[0] */
572 	};
573 
574 	test_preamble_state_qos_configured(conn, ase_id, stream);
575 
576 	ase_cp->write(conn, ase_cp, buf, len, 0, 0);
577 
578 	expect_bt_gatt_notify_cb_called_once(conn, BT_UUID_ASCS_ASE_CP, ase_cp, data_expected,
579 					     sizeof(data_expected));
580 }
581 
ZTEST_F(test_ase_control_params,test_enable_number_of_ases_0x00)582 ZTEST_F(test_ase_control_params, test_enable_number_of_ases_0x00)
583 {
584 	const uint8_t ase_id = test_ase_id_get(fixture->ase);
585 	const uint8_t buf[] = {
586 		0x03,           /* Opcode = Enable */
587 		0x00,           /* Number_of_ASEs */
588 		ase_id,         /* ASE_ID[0] */
589 		0x00,           /* Metadata_Length[0] */
590 	};
591 
592 	test_enable_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp, &fixture->stream,
593 					  buf, sizeof(buf));
594 }
595 
ZTEST_F(test_ase_control_params,test_enable_too_long)596 ZTEST_F(test_ase_control_params, test_enable_too_long)
597 {
598 	const uint8_t ase_id = test_ase_id_get(fixture->ase);
599 	const uint8_t buf[] = {
600 		0x03,           /* Opcode = Enable */
601 		0x01,           /* Number_of_ASEs */
602 		ase_id,         /* ASE_ID[0] */
603 		0x00,           /* Metadata_Length[0] */
604 		0x00,
605 	};
606 
607 	test_enable_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp, &fixture->stream,
608 					  buf, sizeof(buf));
609 }
610 
ZTEST_F(test_ase_control_params,test_enable_too_short)611 ZTEST_F(test_ase_control_params, test_enable_too_short)
612 {
613 	const uint8_t ase_id = test_ase_id_get(fixture->ase);
614 	const uint8_t buf[] = {
615 		0x03,           /* Opcode = Enable */
616 		0x01,           /* Number_of_ASEs */
617 		ase_id,         /* ASE_ID[0] */
618 	};
619 
620 	test_enable_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp, &fixture->stream,
621 					  buf, sizeof(buf));
622 }
623 
ZTEST_F(test_ase_control_params,test_enable_metadata_too_short)624 ZTEST_F(test_ase_control_params, test_enable_metadata_too_short)
625 {
626 	const uint8_t ase_id = test_ase_id_get(fixture->ase);
627 	const uint8_t buf[] = {
628 		0x03,           /* Opcode = Enable */
629 		0x01,           /* Number_of_ASEs */
630 		ase_id,         /* ASE_ID[0] */
631 		0x03,           /* Metadata_Length[0] */
632 		0x02, 0x02,     /* Metadata[0] */
633 	};
634 
635 	test_enable_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp, &fixture->stream,
636 					  buf, sizeof(buf));
637 }
638 
ZTEST_F(test_ase_control_params,test_enable_invalid_ase_id)639 ZTEST_F(test_ase_control_params, test_enable_invalid_ase_id)
640 {
641 	const uint8_t ase_id_valid = 0x01;
642 	const uint8_t ase_id_invalid = CONFIG_BT_ASCS_ASE_SNK_COUNT +
643 				       CONFIG_BT_ASCS_ASE_SRC_COUNT + 1;
644 	const uint8_t buf[] = {
645 		0x03,                   /* Opcode = Enable */
646 		0x02,                   /* Number_of_ASEs */
647 		ase_id_invalid,         /* ASE_ID[0] */
648 		0x04,                   /* Metadata_Length[0] */
649 		0x03, 0x02, 0x04, 0x00, /* Metadata[0] = Streaming Context (Media) */
650 		ase_id_valid,           /* ASE_ID[1] */
651 		0x04,                   /* Metadata_Length[0] */
652 		0x03, 0x02, 0x04, 0x00, /* Metadata[0] = Streaming Context (Media) */
653 	};
654 	const uint8_t data_expected[] = {
655 		0x03,                   /* Opcode = Enable */
656 		0x02,                   /* Number_of_ASEs */
657 		ase_id_invalid,         /* ASE_ID[0] */
658 		0x03,                   /* Response_Code[0] = Invalid ASE_ID */
659 		0x00,                   /* Reason[0] */
660 		ase_id_valid,           /* ASE_ID[1] */
661 		0x00,                   /* Response_Code[1] = Success */
662 		0x00,                   /* Reason[1] */
663 	};
664 
665 	test_preamble_state_qos_configured(&fixture->conn, ase_id_valid, &fixture->stream);
666 
667 	fixture->ase_cp->write(&fixture->conn, fixture->ase_cp, buf, sizeof(buf), 0, 0);
668 
669 	expect_bt_gatt_notify_cb_called_once(&fixture->conn, BT_UUID_ASCS_ASE_CP,
670 					     fixture->ase_cp, data_expected, sizeof(data_expected));
671 }
672 
ZTEST_F(test_ase_control_params,test_enable_metadata_prohibited_context)673 ZTEST_F(test_ase_control_params, test_enable_metadata_prohibited_context)
674 {
675 	const uint8_t ase_id_valid = 0x01;
676 	const uint8_t buf[] = {
677 		0x03,                   /* Opcode = Enable */
678 		0x01,                   /* Number_of_ASEs */
679 		ase_id_valid,           /* ASE_ID[0] */
680 		0x04,                   /* Metadata_Length[0] */
681 		0x03, 0x02, 0x00, 0x00, /* Metadata[0] = Streaming Context (Prohibited) */
682 	};
683 	const uint8_t data_expected[] = {
684 		0x03,                   /* Opcode = Enable */
685 		0x01,                   /* Number_of_ASEs */
686 		ase_id_valid,           /* ASE_ID[0] */
687 		0x0C,                   /* Response_Code[0] = Invalid Metadata */
688 		0x02,                   /* Reason[0] = Streaming Context */
689 	};
690 
691 	test_preamble_state_qos_configured(&fixture->conn, ase_id_valid, &fixture->stream);
692 
693 	fixture->ase_cp->write(&fixture->conn, fixture->ase_cp, buf, sizeof(buf), 0, 0);
694 
695 	expect_bt_gatt_notify_cb_called_once(&fixture->conn, BT_UUID_ASCS_ASE_CP,
696 					     fixture->ase_cp, data_expected, sizeof(data_expected));
697 }
698 
test_receiver_start_ready_expect_invalid_length(struct bt_conn * conn,uint8_t ase_id,const struct bt_gatt_attr * ase_cp,struct bt_bap_stream * stream,const uint8_t * buf,size_t len)699 static void test_receiver_start_ready_expect_invalid_length(struct bt_conn *conn, uint8_t ase_id,
700 							    const struct bt_gatt_attr *ase_cp,
701 							    struct bt_bap_stream *stream,
702 							    const uint8_t *buf, size_t len)
703 {
704 	const uint8_t data_expected[] = {
705 		0x04,           /* Opcode = Receiver Start Ready */
706 		0xFF,           /* Number_of_ASEs */
707 		0x00,           /* ASE_ID[0] */
708 		0x02,           /* Response_Code[0] = Invalid Length */
709 		0x00,           /* Reason[0] */
710 	};
711 	struct bt_iso_chan *chan;
712 	int err;
713 
714 	test_preamble_state_enabling(conn, ase_id, stream);
715 
716 	err = mock_bt_iso_accept(conn, 0x01, 0x01, &chan);
717 	zassert_equal(0, err, "Failed to connect iso: err %d", err);
718 
719 	ase_cp->write(conn, ase_cp, buf, len, 0, 0);
720 
721 	expect_bt_gatt_notify_cb_called_once(conn, BT_UUID_ASCS_ASE_CP, ase_cp, data_expected,
722 					     sizeof(data_expected));
723 }
724 
ZTEST_F(test_ase_control_params,test_receiver_start_ready_number_of_ases_0x00)725 ZTEST_F(test_ase_control_params, test_receiver_start_ready_number_of_ases_0x00)
726 {
727 	const struct bt_gatt_attr *ase;
728 	uint8_t ase_id;
729 
730 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
731 
732 	test_ase_src_get(1, &ase);
733 	zassume_not_null(ase);
734 	ase_id = test_ase_id_get(fixture->ase);
735 
736 	const uint8_t buf[] = {
737 		0x04,           /* Opcode = Receiver Start Ready */
738 		0x00,           /* Number_of_ASEs */
739 		ase_id,         /* ASE_ID[0] */
740 	};
741 
742 	test_receiver_start_ready_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp,
743 							&fixture->stream, buf, sizeof(buf));
744 }
745 
ZTEST_F(test_ase_control_params,test_receiver_start_ready_too_long)746 ZTEST_F(test_ase_control_params, test_receiver_start_ready_too_long)
747 {
748 	const struct bt_gatt_attr *ase;
749 	uint8_t ase_id;
750 
751 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
752 
753 	test_ase_src_get(1, &ase);
754 	zassume_not_null(ase);
755 	ase_id = test_ase_id_get(fixture->ase);
756 
757 	const uint8_t buf[] = {
758 		0x04,           /* Opcode = Receiver Start Ready */
759 		0x01,           /* Number_of_ASEs */
760 		ase_id,         /* ASE_ID[0] */
761 		0x00,
762 	};
763 
764 	test_receiver_start_ready_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp,
765 							&fixture->stream, buf, sizeof(buf));
766 }
767 
ZTEST_F(test_ase_control_params,test_receiver_start_ready_too_short)768 ZTEST_F(test_ase_control_params, test_receiver_start_ready_too_short)
769 {
770 	const uint8_t buf[] = {
771 		0x04,           /* Opcode = Receiver Start Ready */
772 		0x01,           /* Number_of_ASEs */
773 	};
774 	const struct bt_gatt_attr *ase;
775 	uint8_t ase_id;
776 
777 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
778 
779 	test_ase_src_get(1, &ase);
780 	zassume_not_null(ase);
781 	ase_id = test_ase_id_get(fixture->ase);
782 
783 	test_receiver_start_ready_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp,
784 							&fixture->stream, buf, sizeof(buf));
785 }
786 
test_disable_expect_invalid_length(struct bt_conn * conn,uint8_t ase_id,const struct bt_gatt_attr * ase_cp,struct bt_bap_stream * stream,const uint8_t * buf,size_t len)787 static void test_disable_expect_invalid_length(struct bt_conn *conn, uint8_t ase_id,
788 					       const struct bt_gatt_attr *ase_cp,
789 					       struct bt_bap_stream *stream,
790 					       const uint8_t *buf, size_t len)
791 {
792 	const uint8_t data_expected[] = {
793 		0x05,           /* Opcode = Disable */
794 		0xFF,           /* Number_of_ASEs */
795 		0x00,           /* ASE_ID[0] */
796 		0x02,           /* Response_Code[0] = Invalid Length */
797 		0x00,           /* Reason[0] */
798 	};
799 
800 	test_preamble_state_enabling(conn, ase_id, stream);
801 
802 	ase_cp->write(conn, ase_cp, buf, len, 0, 0);
803 
804 	expect_bt_gatt_notify_cb_called_once(conn, BT_UUID_ASCS_ASE_CP, ase_cp, data_expected,
805 					     sizeof(data_expected));
806 }
807 
ZTEST_F(test_ase_control_params,test_disable_number_of_ases_0x00)808 ZTEST_F(test_ase_control_params, test_disable_number_of_ases_0x00)
809 {
810 	const uint8_t ase_id = test_ase_id_get(fixture->ase);
811 	const uint8_t buf[] = {
812 		0x05,           /* Opcode = Disable */
813 		0x00,           /* Number_of_ASEs */
814 		ase_id,         /* ASE_ID[0] */
815 	};
816 
817 	test_disable_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp,
818 					   &fixture->stream, buf, sizeof(buf));
819 }
820 
ZTEST_F(test_ase_control_params,test_disable_too_long)821 ZTEST_F(test_ase_control_params, test_disable_too_long)
822 {
823 	const uint8_t ase_id = test_ase_id_get(fixture->ase);
824 	const uint8_t buf[] = {
825 		0x05,           /* Opcode = Disable */
826 		0x01,           /* Number_of_ASEs */
827 		ase_id,         /* ASE_ID[0] */
828 		0x00,
829 	};
830 
831 	test_disable_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp,
832 					   &fixture->stream, buf, sizeof(buf));
833 }
834 
ZTEST_F(test_ase_control_params,test_disable_too_short)835 ZTEST_F(test_ase_control_params, test_disable_too_short)
836 {
837 	uint8_t ase_id = test_ase_id_get(fixture->ase);
838 	const uint8_t buf[] = {
839 		0x05,           /* Opcode = Disable */
840 		0x01,           /* Number_of_ASEs */
841 	};
842 
843 	test_disable_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp,
844 					   &fixture->stream, buf, sizeof(buf));
845 }
846 
test_receiver_stop_ready_expect_invalid_length(struct bt_conn * conn,uint8_t ase_id,const struct bt_gatt_attr * ase_cp,struct bt_bap_stream * stream,const uint8_t * buf,size_t len)847 static void test_receiver_stop_ready_expect_invalid_length(struct bt_conn *conn, uint8_t ase_id,
848 							   const struct bt_gatt_attr *ase_cp,
849 							   struct bt_bap_stream *stream,
850 							   const uint8_t *buf, size_t len)
851 {
852 	const uint8_t data_expected[] = {
853 		0x06,           /* Opcode = Receiver Stop Ready */
854 		0xFF,           /* Number_of_ASEs */
855 		0x00,           /* ASE_ID[0] */
856 		0x02,           /* Response_Code[0] = Invalid Length */
857 		0x00,           /* Reason[0] */
858 	};
859 	struct bt_iso_chan *chan;
860 
861 	test_preamble_state_disabling(conn, ase_id, stream, &chan);
862 
863 	ase_cp->write(conn, ase_cp, buf, len, 0, 0);
864 
865 	expect_bt_gatt_notify_cb_called_once(conn, BT_UUID_ASCS_ASE_CP, ase_cp, data_expected,
866 					     sizeof(data_expected));
867 }
868 
ZTEST_F(test_ase_control_params,test_receiver_stop_ready_number_of_ases_0x00)869 ZTEST_F(test_ase_control_params, test_receiver_stop_ready_number_of_ases_0x00)
870 {
871 	const struct bt_gatt_attr *ase;
872 	uint8_t ase_id;
873 
874 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
875 
876 	test_ase_src_get(1, &ase);
877 	zassume_not_null(ase);
878 	ase_id = test_ase_id_get(fixture->ase);
879 
880 	const uint8_t buf[] = {
881 		0x06,           /* Opcode = Receiver Stop Ready */
882 		0x00,           /* Number_of_ASEs */
883 		ase_id,         /* ASE_ID[0] */
884 	};
885 
886 	test_receiver_stop_ready_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp,
887 						       &fixture->stream, buf, sizeof(buf));
888 }
889 
ZTEST_F(test_ase_control_params,test_receiver_stop_ready_too_long)890 ZTEST_F(test_ase_control_params, test_receiver_stop_ready_too_long)
891 {
892 	const struct bt_gatt_attr *ase;
893 	uint8_t ase_id;
894 
895 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
896 
897 	test_ase_src_get(1, &ase);
898 	zassume_not_null(ase);
899 	ase_id = test_ase_id_get(fixture->ase);
900 
901 	const uint8_t buf[] = {
902 		0x06,           /* Opcode = Receiver Stop Ready */
903 		0x01,           /* Number_of_ASEs */
904 		ase_id,         /* ASE_ID[0] */
905 		0x00,
906 	};
907 
908 	test_receiver_stop_ready_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp,
909 						       &fixture->stream, buf, sizeof(buf));
910 }
911 
ZTEST_F(test_ase_control_params,test_receiver_stop_ready_too_short)912 ZTEST_F(test_ase_control_params, test_receiver_stop_ready_too_short)
913 {
914 	const uint8_t buf[] = {
915 		0x06,           /* Opcode = Receiver Stop Ready */
916 		0x01,           /* Number_of_ASEs */
917 	};
918 	const struct bt_gatt_attr *ase;
919 	uint8_t ase_id;
920 
921 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
922 
923 	test_ase_src_get(1, &ase);
924 	zassume_not_null(ase);
925 	ase_id = test_ase_id_get(fixture->ase);
926 
927 	test_receiver_stop_ready_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp,
928 						       &fixture->stream, buf, sizeof(buf));
929 }
930 
test_update_metadata_expect_invalid_length(struct bt_conn * conn,uint8_t ase_id,const struct bt_gatt_attr * ase_cp,struct bt_bap_stream * stream,const uint8_t * buf,size_t len)931 static void test_update_metadata_expect_invalid_length(struct bt_conn *conn, uint8_t ase_id,
932 						       const struct bt_gatt_attr *ase_cp,
933 						       struct bt_bap_stream *stream,
934 						       const uint8_t *buf, size_t len)
935 {
936 	const uint8_t data_expected[] = {
937 		0x07,           /* Opcode = Update Metadata */
938 		0xFF,           /* Number_of_ASEs */
939 		0x00,           /* ASE_ID[0] */
940 		0x02,           /* Response_Code[0] = Invalid Length */
941 		0x00,           /* Reason[0] */
942 	};
943 
944 	test_preamble_state_enabling(conn, ase_id, stream);
945 
946 	ase_cp->write(conn, ase_cp, buf, len, 0, 0);
947 
948 	expect_bt_gatt_notify_cb_called_once(conn, BT_UUID_ASCS_ASE_CP, ase_cp, data_expected,
949 					     sizeof(data_expected));
950 }
951 
ZTEST_F(test_ase_control_params,test_update_metadata_number_of_ases_0x00)952 ZTEST_F(test_ase_control_params, test_update_metadata_number_of_ases_0x00)
953 {
954 	const uint8_t ase_id = test_ase_id_get(fixture->ase);
955 	const uint8_t buf[] = {
956 		0x07,           /* Opcode = Update Metadata */
957 		0x00,           /* Number_of_ASEs */
958 		ase_id,         /* ASE_ID[0] */
959 		0x00,           /* Metadata_Length[0] */
960 	};
961 
962 	test_update_metadata_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp,
963 						   &fixture->stream, buf, sizeof(buf));
964 }
965 
ZTEST_F(test_ase_control_params,test_update_metadata_too_long)966 ZTEST_F(test_ase_control_params, test_update_metadata_too_long)
967 {
968 	const uint8_t ase_id = test_ase_id_get(fixture->ase);
969 	const uint8_t buf[] = {
970 		0x07,           /* Opcode = Update Metadata */
971 		0x01,           /* Number_of_ASEs */
972 		ase_id,         /* ASE_ID[0] */
973 		0x00,           /* Metadata_Length[0] */
974 		0x00,
975 	};
976 
977 	test_update_metadata_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp,
978 						   &fixture->stream, buf, sizeof(buf));
979 }
980 
ZTEST_F(test_ase_control_params,test_update_metadata_too_short)981 ZTEST_F(test_ase_control_params, test_update_metadata_too_short)
982 {
983 	const uint8_t ase_id = test_ase_id_get(fixture->ase);
984 	const uint8_t buf[] = {
985 		0x07,           /* Opcode = Update Metadata */
986 		0x01,           /* Number_of_ASEs */
987 		ase_id,         /* ASE_ID[0] */
988 	};
989 
990 	test_update_metadata_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp,
991 						   &fixture->stream, buf, sizeof(buf));
992 }
993 
ZTEST_F(test_ase_control_params,test_update_metadata_metadata_too_short)994 ZTEST_F(test_ase_control_params, test_update_metadata_metadata_too_short)
995 {
996 	const uint8_t ase_id = test_ase_id_get(fixture->ase);
997 	const uint8_t buf[] = {
998 		0x07,           /* Opcode = Update Metadata */
999 		0x01,           /* Number_of_ASEs */
1000 		ase_id,         /* ASE_ID[0] */
1001 		0x03,           /* Metadata_Length[0] */
1002 		0x02, 0x02,     /* Metadata[0] */
1003 	};
1004 
1005 	test_update_metadata_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp,
1006 						   &fixture->stream, buf, sizeof(buf));
1007 }
1008 
ZTEST_F(test_ase_control_params,test_update_metadata_invalid_ase_id)1009 ZTEST_F(test_ase_control_params, test_update_metadata_invalid_ase_id)
1010 {
1011 	const uint8_t ase_id_valid = 0x01;
1012 	const uint8_t ase_id_invalid = CONFIG_BT_ASCS_ASE_SNK_COUNT +
1013 				       CONFIG_BT_ASCS_ASE_SRC_COUNT + 1;
1014 	const uint8_t buf[] = {
1015 		0x07,                   /* Opcode = Update Metadata */
1016 		0x02,                   /* Number_of_ASEs */
1017 		ase_id_invalid,         /* ASE_ID[0] */
1018 		0x04,                   /* Metadata_Length[0] */
1019 		0x03, 0x02, 0x04, 0x00, /* Metadata[0] = Streaming Context (Media) */
1020 		ase_id_valid,           /* ASE_ID[1] */
1021 		0x04,                   /* Metadata_Length[0] */
1022 		0x03, 0x02, 0x04, 0x00, /* Metadata[0] = Streaming Context (Media) */
1023 	};
1024 	const uint8_t data_expected[] = {
1025 		0x07,                   /* Opcode = Update Metadata */
1026 		0x02,                   /* Number_of_ASEs */
1027 		ase_id_invalid,         /* ASE_ID[0] */
1028 		0x03,                   /* Response_Code[0] = Invalid ASE_ID */
1029 		0x00,                   /* Reason[0] */
1030 		ase_id_valid,           /* ASE_ID[1] */
1031 		0x00,                   /* Response_Code[1] = Success */
1032 		0x00,                   /* Reason[1] */
1033 	};
1034 
1035 	test_preamble_state_enabling(&fixture->conn, ase_id_valid, &fixture->stream);
1036 
1037 	fixture->ase_cp->write(&fixture->conn, fixture->ase_cp, buf, sizeof(buf), 0, 0);
1038 
1039 	expect_bt_gatt_notify_cb_called_once(&fixture->conn, BT_UUID_ASCS_ASE_CP,
1040 					     fixture->ase_cp, data_expected, sizeof(data_expected));
1041 }
1042 
test_release_expect_invalid_length(struct bt_conn * conn,uint8_t ase_id,const struct bt_gatt_attr * ase_cp,struct bt_bap_stream * stream,const uint8_t * buf,size_t len)1043 static void test_release_expect_invalid_length(struct bt_conn *conn, uint8_t ase_id,
1044 					       const struct bt_gatt_attr *ase_cp,
1045 					       struct bt_bap_stream *stream,
1046 					       const uint8_t *buf, size_t len)
1047 {
1048 	const uint8_t data_expected[] = {
1049 		0x08,           /* Opcode = Release */
1050 		0xFF,           /* Number_of_ASEs */
1051 		0x00,           /* ASE_ID[0] */
1052 		0x02,           /* Response_Code[0] = Invalid Length */
1053 		0x00,           /* Reason[0] */
1054 	};
1055 
1056 	test_preamble_state_enabling(conn, ase_id, stream);
1057 
1058 	ase_cp->write(conn, ase_cp, buf, len, 0, 0);
1059 
1060 	expect_bt_gatt_notify_cb_called_once(conn, BT_UUID_ASCS_ASE_CP, ase_cp, data_expected,
1061 					     sizeof(data_expected));
1062 }
1063 
ZTEST_F(test_ase_control_params,test_release_number_of_ases_0x00)1064 ZTEST_F(test_ase_control_params, test_release_number_of_ases_0x00)
1065 {
1066 	const uint8_t ase_id = test_ase_id_get(fixture->ase);
1067 	const uint8_t buf[] = {
1068 		0x08,           /* Opcode = Release */
1069 		0x00,           /* Number_of_ASEs */
1070 		ase_id,         /* ASE_ID[0] */
1071 	};
1072 
1073 	test_release_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp,
1074 					   &fixture->stream, buf, sizeof(buf));
1075 }
1076 
ZTEST_F(test_ase_control_params,test_release_too_long)1077 ZTEST_F(test_ase_control_params, test_release_too_long)
1078 {
1079 	const uint8_t ase_id = test_ase_id_get(fixture->ase);
1080 	const uint8_t buf[] = {
1081 		0x08,           /* Opcode = Release */
1082 		0x01,           /* Number_of_ASEs */
1083 		ase_id,         /* ASE_ID[0] */
1084 		0x00,
1085 	};
1086 
1087 	test_release_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp,
1088 					   &fixture->stream, buf, sizeof(buf));
1089 }
1090 
ZTEST_F(test_ase_control_params,test_release_too_short)1091 ZTEST_F(test_ase_control_params, test_release_too_short)
1092 {
1093 	const uint8_t ase_id = test_ase_id_get(fixture->ase);
1094 	const uint8_t buf[] = {
1095 		0x08,           /* Opcode = Release */
1096 		0x01,           /* Number_of_ASEs */
1097 	};
1098 
1099 	test_release_expect_invalid_length(&fixture->conn, ase_id, fixture->ase_cp,
1100 					   &fixture->stream, buf, sizeof(buf));
1101 }
1102