1 /* main.c - Application main entry point */
2 
3 /*
4  * Copyright (c) 2023 Nordic Semiconductor ASA
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <stdlib.h>
10 #include <string.h>
11 
12 #include <zephyr/bluetooth/audio/audio.h>
13 #include <zephyr/bluetooth/audio/bap.h>
14 #include <zephyr/bluetooth/iso.h>
15 #include <zephyr/fff.h>
16 #include <zephyr/kernel.h>
17 #include <zephyr/sys/util_macro.h>
18 
19 #include "bluetooth.h"
20 #include "bap_stream_expects.h"
21 
22 DEFINE_FFF_GLOBALS;
23 
mock_init_rule_before(const struct ztest_unit_test * test,void * fixture)24 static void mock_init_rule_before(const struct ztest_unit_test *test, void *fixture)
25 {
26 	mock_bap_stream_init();
27 }
28 
mock_destroy_rule_after(const struct ztest_unit_test * test,void * fixture)29 static void mock_destroy_rule_after(const struct ztest_unit_test *test, void *fixture)
30 {
31 	mock_bap_stream_cleanup();
32 }
33 
34 ZTEST_RULE(mock_rule, mock_init_rule_before, mock_destroy_rule_after);
35 
36 struct bap_broadcast_source_test_suite_fixture {
37 	struct bt_bap_broadcast_source_param *param;
38 	size_t stream_cnt;
39 	struct bt_bap_broadcast_source *source;
40 };
41 
bap_broadcast_source_test_suite_fixture_init(struct bap_broadcast_source_test_suite_fixture * fixture)42 static void bap_broadcast_source_test_suite_fixture_init(
43 	struct bap_broadcast_source_test_suite_fixture *fixture)
44 {
45 	const uint8_t bis_cfg_data[] = {
46 		BT_AUDIO_CODEC_DATA(BT_AUDIO_CODEC_CONFIG_LC3_CHAN_ALLOC,
47 				    BT_AUDIO_LOCATION_FRONT_LEFT | BT_AUDIO_LOCATION_FRONT_RIGHT),
48 	};
49 	const size_t streams_per_subgroup = CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT /
50 					    CONFIG_BT_BAP_BROADCAST_SRC_SUBGROUP_COUNT;
51 	const enum bt_audio_context ctx = BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED;
52 	const enum bt_audio_location loc = BT_AUDIO_LOCATION_FRONT_LEFT;
53 	struct bt_bap_broadcast_source_subgroup_param *subgroup_param;
54 	struct bt_bap_broadcast_source_stream_param *stream_params;
55 	struct bt_audio_codec_cfg *codec_cfg;
56 	struct bt_audio_codec_qos *codec_qos;
57 	struct bt_bap_stream *streams;
58 	const uint16_t latency = 10U; /* ms*/
59 	const uint32_t pd = 40000U;   /* us */
60 	const uint16_t sdu = 40U;     /* octets */
61 	const uint8_t rtn = 2U;
62 	uint8_t *bis_data;
63 
64 	zassert_true(streams_per_subgroup > 0U);
65 	zassert_true(sizeof(bis_cfg_data) <= CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE);
66 
67 	/* Allocate memory for everything */
68 	fixture->param = malloc(sizeof(struct bt_bap_broadcast_source_param));
69 	subgroup_param = malloc(sizeof(struct bt_bap_broadcast_source_subgroup_param) *
70 				CONFIG_BT_BAP_BROADCAST_SRC_SUBGROUP_COUNT);
71 	zassert_not_null(subgroup_param);
72 	stream_params = malloc(sizeof(struct bt_bap_broadcast_source_stream_param) *
73 			       CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT);
74 	zassert_not_null(stream_params);
75 	codec_cfg = malloc(sizeof(struct bt_audio_codec_cfg));
76 	zassert_not_null(codec_cfg);
77 	codec_qos = malloc(sizeof(struct bt_audio_codec_qos));
78 	zassert_not_null(codec_qos);
79 	streams = malloc(sizeof(struct bt_bap_stream) * CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT);
80 	zassert_not_null(streams);
81 	bis_data = malloc(CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE);
82 	zassert_not_null(bis_data);
83 
84 	/* Memset everything to 0 */
85 	memset(fixture->param, 0, sizeof(*fixture->param));
86 	memset(subgroup_param, 0,
87 	       sizeof(struct bt_bap_broadcast_source_subgroup_param) *
88 		       CONFIG_BT_BAP_BROADCAST_SRC_SUBGROUP_COUNT);
89 	memset(stream_params, 0,
90 	       sizeof(struct bt_bap_broadcast_source_stream_param) *
91 		       CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT);
92 	memset(codec_cfg, 0, sizeof(struct bt_audio_codec_cfg));
93 	memset(codec_qos, 0, sizeof(struct bt_audio_codec_qos));
94 	memset(streams, 0, sizeof(struct bt_bap_stream) * CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT);
95 	memset(bis_data, 0, CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE);
96 
97 	/* Initialize default values*/
98 	*codec_cfg = BT_AUDIO_CODEC_LC3_CONFIG_16_2(loc, ctx);
99 	*codec_qos = BT_AUDIO_CODEC_LC3_QOS_10_UNFRAMED(sdu, rtn, latency, pd);
100 	memcpy(bis_data, bis_cfg_data, sizeof(bis_cfg_data));
101 
102 	for (size_t i = 0U; i < CONFIG_BT_BAP_BROADCAST_SRC_SUBGROUP_COUNT; i++) {
103 		subgroup_param[i].params_count = streams_per_subgroup;
104 		subgroup_param[i].params = stream_params + i * streams_per_subgroup;
105 		subgroup_param[i].codec_cfg = codec_cfg;
106 	}
107 
108 	for (size_t i = 0U; i < CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT; i++) {
109 		stream_params[i].stream = &streams[i];
110 		stream_params[i].data = bis_data;
111 		stream_params[i].data_len = sizeof(bis_cfg_data);
112 		bt_bap_stream_cb_register(stream_params[i].stream, &mock_bap_stream_ops);
113 	}
114 
115 	fixture->param->params_count = CONFIG_BT_BAP_BROADCAST_SRC_SUBGROUP_COUNT;
116 	fixture->param->params = subgroup_param;
117 	fixture->param->qos = codec_qos;
118 	fixture->param->encryption = false;
119 	memset(fixture->param->broadcast_code, 0, sizeof(fixture->param->broadcast_code));
120 	fixture->param->packing = BT_ISO_PACKING_SEQUENTIAL;
121 
122 	fixture->stream_cnt = fixture->param->params_count * streams_per_subgroup;
123 }
124 
bap_broadcast_source_test_suite_setup(void)125 static void *bap_broadcast_source_test_suite_setup(void)
126 {
127 	struct bap_broadcast_source_test_suite_fixture *fixture;
128 
129 	fixture = malloc(sizeof(*fixture));
130 	zassert_not_null(fixture);
131 
132 	return fixture;
133 }
134 
bap_broadcast_source_test_suite_before(void * f)135 static void bap_broadcast_source_test_suite_before(void *f)
136 {
137 	memset(f, 0, sizeof(struct bap_broadcast_source_test_suite_fixture));
138 	bap_broadcast_source_test_suite_fixture_init(f);
139 }
140 
bap_broadcast_source_test_suite_after(void * f)141 static void bap_broadcast_source_test_suite_after(void *f)
142 {
143 	struct bap_broadcast_source_test_suite_fixture *fixture = f;
144 	struct bt_bap_broadcast_source_param *param;
145 
146 	if (fixture->source != NULL) {
147 		int err;
148 
149 		(void)bt_bap_broadcast_source_stop(fixture->source);
150 
151 		err = bt_bap_broadcast_source_delete(fixture->source);
152 		zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
153 		fixture->source = NULL;
154 	}
155 
156 	param = fixture->param;
157 
158 	free(param->params[0].params[0].data);
159 	free(param->params[0].params[0].stream);
160 	free(param->params[0].params);
161 	free(param->params[0].codec_cfg);
162 	free(param->params);
163 	free(param->qos);
164 	free(param);
165 }
166 
bap_broadcast_source_test_suite_teardown(void * f)167 static void bap_broadcast_source_test_suite_teardown(void *f)
168 {
169 	free(f);
170 }
171 
172 ZTEST_SUITE(bap_broadcast_source_test_suite, NULL, bap_broadcast_source_test_suite_setup,
173 	    bap_broadcast_source_test_suite_before, bap_broadcast_source_test_suite_after,
174 	    bap_broadcast_source_test_suite_teardown);
175 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_create_delete)176 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_create_delete)
177 {
178 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
179 	int err;
180 
181 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
182 	       create_param->params_count, fixture->stream_cnt);
183 
184 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
185 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
186 
187 	for (size_t i = 0u; i < create_param->params_count; i++) {
188 		for (size_t j = 0u; j < create_param->params[i].params_count; j++) {
189 			const struct bt_bap_stream *stream =
190 				create_param->params[i].params[j].stream;
191 
192 			zassert_equal(create_param->qos->sdu, stream->qos->sdu,
193 				      "Unexpected stream SDU");
194 			zassert_equal(create_param->qos->rtn, stream->qos->rtn,
195 				      "Unexpected stream RTN");
196 			zassert_equal(create_param->qos->phy, stream->qos->phy,
197 				      "Unexpected stream PHY");
198 		}
199 	}
200 
201 	err = bt_bap_broadcast_source_delete(fixture->source);
202 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
203 	fixture->source = NULL;
204 }
205 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_create_start_send_stop_delete)206 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_create_start_send_stop_delete)
207 {
208 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
209 	struct bt_le_ext_adv ext_adv = {0};
210 	int err;
211 
212 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
213 	       create_param->params_count, fixture->stream_cnt);
214 
215 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
216 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
217 
218 	err = bt_bap_broadcast_source_start(fixture->source, &ext_adv);
219 	zassert_equal(0, err, "Unable to start broadcast source: err %d", err);
220 
221 	zexpect_call_count("bt_bap_stream_ops.started", fixture->stream_cnt,
222 			   mock_bap_stream_started_cb_fake.call_count);
223 
224 	for (size_t i = 0U; i < create_param->params_count; i++) {
225 		for (size_t j = 0U; j < create_param->params[i].params_count; j++) {
226 			struct bt_bap_stream *bap_stream = create_param->params[i].params[j].stream;
227 
228 			/* Since BAP doesn't care about the `buf` we can just provide NULL */
229 			err = bt_bap_stream_send(bap_stream, NULL, 0, BT_ISO_TIMESTAMP_NONE);
230 			zassert_equal(0, err,
231 				      "Unable to send on broadcast stream[%zu][%zu]: err %d", i, j,
232 				      err);
233 		}
234 	}
235 
236 	zexpect_call_count("bt_bap_stream_ops.sent", fixture->stream_cnt,
237 			   mock_bap_stream_sent_cb_fake.call_count);
238 
239 	err = bt_bap_broadcast_source_stop(fixture->source);
240 	zassert_equal(0, err, "Unable to stop broadcast source: err %d", err);
241 
242 	zexpect_call_count("bt_bap_stream_ops.stopped", fixture->stream_cnt,
243 			   mock_bap_stream_stopped_cb_fake.call_count);
244 
245 	err = bt_bap_broadcast_source_delete(fixture->source);
246 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
247 	fixture->source = NULL;
248 }
249 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_create_inval_param_null)250 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_create_inval_param_null)
251 {
252 	int err;
253 
254 	err = bt_bap_broadcast_source_create(NULL, &fixture->source);
255 	zassert_not_equal(0, err, "Did not fail with null params");
256 }
257 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_create_inval_source_null)258 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_create_inval_source_null)
259 {
260 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
261 	int err;
262 
263 	err = bt_bap_broadcast_source_create(create_param, NULL);
264 	zassert_not_equal(0, err, "Did not fail with null source");
265 }
266 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_create_inval_subgroup_params_count_0)267 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_create_inval_subgroup_params_count_0)
268 {
269 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
270 	int err;
271 
272 	create_param->params_count = 0U;
273 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
274 	zassert_not_equal(0, err, "Did not fail with params_count %u", create_param->params_count);
275 }
276 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_create_inval_subgroup_params_count_above_max)277 ZTEST_F(bap_broadcast_source_test_suite,
278 	test_broadcast_source_create_inval_subgroup_params_count_above_max)
279 {
280 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
281 	int err;
282 
283 	create_param->params_count = CONFIG_BT_BAP_BROADCAST_SRC_SUBGROUP_COUNT + 1;
284 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
285 	zassert_not_equal(0, err, "Did not fail with params_count %u", create_param->params_count);
286 }
287 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_create_inval_subgroup_params_null)288 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_create_inval_subgroup_params_null)
289 {
290 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
291 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &create_param->params[0];
292 	int err;
293 
294 	create_param->params = NULL;
295 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
296 	/* Restore the params for the cleanup after function */
297 	create_param->params = subgroup_params;
298 	zassert_not_equal(0, err, "Did not fail with NULL subgroup params");
299 }
300 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_create_inval_qos_null)301 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_create_inval_qos_null)
302 {
303 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
304 	struct bt_audio_codec_qos *qos = create_param->qos;
305 	int err;
306 
307 	create_param->qos = NULL;
308 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
309 	/* Restore the params for the cleanup after function */
310 	create_param->qos = qos;
311 	zassert_not_equal(0, err, "Did not fail with NULL qos");
312 }
313 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_create_inval_packing)314 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_create_inval_packing)
315 {
316 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
317 	int err;
318 
319 	create_param->packing = 0x02;
320 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
321 	zassert_not_equal(0, err, "Did not fail with packing %u", create_param->packing);
322 }
323 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_create_inval_subgroup_params_params_count_0)324 ZTEST_F(bap_broadcast_source_test_suite,
325 	test_broadcast_source_create_inval_subgroup_params_params_count_0)
326 {
327 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
328 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &create_param->params[0];
329 	int err;
330 
331 	subgroup_params->params_count = 0U;
332 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
333 	zassert_not_equal(0, err, "Did not fail with %u stream params",
334 			  subgroup_params->params_count);
335 }
336 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_create_inval_subgroup_params_params_count_above_max)337 ZTEST_F(bap_broadcast_source_test_suite,
338 	test_broadcast_source_create_inval_subgroup_params_params_count_above_max)
339 {
340 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
341 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &create_param->params[0];
342 	int err;
343 
344 	subgroup_params->params_count = CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT + 1;
345 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
346 	zassert_not_equal(0, err, "Did not fail with %u stream params",
347 			  subgroup_params->params_count);
348 }
349 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_create_inval_subgroup_params_stream_params_null)350 ZTEST_F(bap_broadcast_source_test_suite,
351 	test_broadcast_source_create_inval_subgroup_params_stream_params_null)
352 {
353 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
354 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &create_param->params[0];
355 	struct bt_bap_broadcast_source_stream_param *stream_params = &subgroup_params->params[0];
356 	int err;
357 
358 	subgroup_params->params = NULL;
359 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
360 	/* Restore the params for the cleanup after function */
361 	subgroup_params->params = stream_params;
362 	zassert_not_equal(0, err, "Did not fail with NULL stream params");
363 }
364 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_create_inval_subgroup_params_codec_cfg_null)365 ZTEST_F(bap_broadcast_source_test_suite,
366 	test_broadcast_source_create_inval_subgroup_params_codec_cfg_null)
367 {
368 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
369 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &create_param->params[0];
370 	struct bt_audio_codec_cfg *codec_cfg = subgroup_params->codec_cfg;
371 	int err;
372 
373 	subgroup_params->codec_cfg = NULL;
374 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
375 	/* Restore the params for the cleanup after function */
376 	subgroup_params->codec_cfg = codec_cfg;
377 	zassert_not_equal(0, err, "Did not fail with NULL codec_cfg");
378 }
379 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_create_inval_subgroup_params_codec_cfg_data_len)380 ZTEST_F(bap_broadcast_source_test_suite,
381 	test_broadcast_source_create_inval_subgroup_params_codec_cfg_data_len)
382 {
383 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
384 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &create_param->params[0];
385 	struct bt_audio_codec_cfg *codec_cfg = subgroup_params->codec_cfg;
386 	int err;
387 
388 	codec_cfg->data_len = CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE + 1;
389 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
390 	zassert_not_equal(0, err, "Did not fail with codec_cfg->data_len %zu", codec_cfg->data_len);
391 }
392 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_create_inval_subgroup_params_codec_cfg_meta_len)393 ZTEST_F(bap_broadcast_source_test_suite,
394 	test_broadcast_source_create_inval_subgroup_params_codec_cfg_meta_len)
395 {
396 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
397 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &create_param->params[0];
398 	struct bt_audio_codec_cfg *codec_cfg = subgroup_params->codec_cfg;
399 	int err;
400 
401 	codec_cfg->meta_len = CONFIG_BT_AUDIO_CODEC_CFG_MAX_METADATA_SIZE + 1;
402 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
403 	zassert_not_equal(0, err, "Did not fail with codec_cfg->meta_len %zu", codec_cfg->meta_len);
404 }
405 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_create_inval_subgroup_params_codec_cfg_cid)406 ZTEST_F(bap_broadcast_source_test_suite,
407 	test_broadcast_source_create_inval_subgroup_params_codec_cfg_cid)
408 {
409 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
410 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &create_param->params[0];
411 	struct bt_audio_codec_cfg *codec_cfg = subgroup_params->codec_cfg;
412 	int err;
413 
414 	codec_cfg->id = BT_HCI_CODING_FORMAT_LC3;
415 	codec_cfg->cid = 0x01; /* Shall be 0 if id == 0x06 (LC3)*/
416 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
417 	zassert_not_equal(0, err, "Did not fail with codec_cfg->cid %u", codec_cfg->cid);
418 }
419 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_create_inval_subgroup_params_codec_cfg_vid)420 ZTEST_F(bap_broadcast_source_test_suite,
421 	test_broadcast_source_create_inval_subgroup_params_codec_cfg_vid)
422 {
423 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
424 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &create_param->params[0];
425 	struct bt_audio_codec_cfg *codec_cfg = subgroup_params->codec_cfg;
426 	int err;
427 
428 	codec_cfg->id = BT_HCI_CODING_FORMAT_LC3;
429 	codec_cfg->vid = 0x01; /* Shall be 0 if id == 0x06 (LC3)*/
430 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
431 	zassert_not_equal(0, err, "Did not fail with codec_cfg->vid %u", codec_cfg->vid);
432 }
433 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_create_inval_stream_params_stream_null)434 ZTEST_F(bap_broadcast_source_test_suite,
435 	test_broadcast_source_create_inval_stream_params_stream_null)
436 {
437 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
438 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &create_param->params[0];
439 	struct bt_bap_broadcast_source_stream_param *stream_params = &subgroup_params->params[0];
440 	struct bt_bap_stream *stream = stream_params->stream;
441 	int err;
442 
443 	stream_params->stream = NULL;
444 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
445 	/* Restore the params for the cleanup after function */
446 	stream_params->stream = stream;
447 	zassert_not_equal(0, err, "Did not fail with NULL stream_params->stream");
448 }
449 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_create_inval_stream_params_data_null)450 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_create_inval_stream_params_data_null)
451 {
452 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
453 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &create_param->params[0];
454 	struct bt_bap_broadcast_source_stream_param *stream_params = &subgroup_params->params[0];
455 	uint8_t *data = stream_params->data;
456 	int err;
457 
458 	stream_params->data = NULL;
459 	stream_params->data_len = 1;
460 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
461 	/* Restore the params for the cleanup after function */
462 	stream_params->data = data;
463 	zassert_not_equal(
464 		0, err,
465 		"Did not fail with NULL stream_params->data and stream_params_>data_len %zu",
466 		stream_params->data_len);
467 }
468 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_create_inval_stream_params_data_len)469 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_create_inval_stream_params_data_len)
470 {
471 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
472 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &create_param->params[0];
473 	struct bt_bap_broadcast_source_stream_param *stream_params = &subgroup_params->params[0];
474 	int err;
475 
476 	stream_params->data_len = CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE + 1;
477 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
478 	zassert_not_equal(0, err, "Did not fail with stream_params_>data_len %zu",
479 			  stream_params->data_len);
480 }
481 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_start_inval_source_null)482 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_start_inval_source_null)
483 {
484 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
485 	struct bt_le_ext_adv ext_adv = {0};
486 	int err;
487 
488 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
489 	       create_param->params_count, fixture->stream_cnt);
490 
491 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
492 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
493 
494 	err = bt_bap_broadcast_source_start(NULL, &ext_adv);
495 	zassert_not_equal(0, err, "Did not fail with null source");
496 }
497 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_start_inval_ext_adv_null)498 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_start_inval_ext_adv_null)
499 {
500 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
501 	int err;
502 
503 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
504 	       create_param->params_count, fixture->stream_cnt);
505 
506 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
507 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
508 
509 	err = bt_bap_broadcast_source_start(fixture->source, NULL);
510 	zassert_not_equal(0, err, "Did not fail with null ext_adv");
511 }
512 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_start_inval_double_start)513 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_start_inval_double_start)
514 {
515 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
516 	struct bt_le_ext_adv ext_adv = {0};
517 	int err;
518 
519 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
520 	       create_param->params_count, fixture->stream_cnt);
521 
522 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
523 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
524 
525 	err = bt_bap_broadcast_source_start(fixture->source, &ext_adv);
526 	zassert_equal(0, err, "Unable to start broadcast source: err %d", err);
527 
528 	err = bt_bap_broadcast_source_start(fixture->source, &ext_adv);
529 	zassert_not_equal(0, err, "Did not fail with starting already started source");
530 }
531 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_reconfigure_single_subgroup)532 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_reconfigure_single_subgroup)
533 {
534 	struct bt_bap_broadcast_source_param *reconf_param = fixture->param;
535 	const size_t subgroup_cnt = reconf_param->params_count;
536 	int err;
537 
538 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
539 	       reconf_param->params_count, fixture->stream_cnt);
540 
541 	err = bt_bap_broadcast_source_create(reconf_param, &fixture->source);
542 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
543 
544 	for (size_t i = 0u; i < reconf_param->params_count; i++) {
545 		for (size_t j = 0u; j < reconf_param->params[i].params_count; j++) {
546 			const struct bt_bap_stream *stream =
547 				reconf_param->params[i].params[j].stream;
548 
549 			zassert_equal(reconf_param->qos->sdu, stream->qos->sdu,
550 				      "Unexpected stream SDU");
551 			zassert_equal(reconf_param->qos->rtn, stream->qos->rtn,
552 				      "Unexpected stream RTN");
553 			zassert_equal(reconf_param->qos->phy, stream->qos->phy,
554 				      "Unexpected stream PHY");
555 		}
556 	}
557 
558 	reconf_param->params_count = 1U;
559 	reconf_param->qos->sdu = 100U;
560 	reconf_param->qos->rtn = 3U;
561 	reconf_param->qos->phy = 1U;
562 
563 	err = bt_bap_broadcast_source_reconfig(fixture->source, reconf_param);
564 	zassert_equal(0, err, "Unable to reconfigure broadcast source: err %d", err);
565 
566 	for (size_t i = 0u; i < subgroup_cnt; i++) {
567 		for (size_t j = 0u; j < reconf_param->params[i].params_count; j++) {
568 			const struct bt_bap_stream *stream =
569 				reconf_param->params[i].params[j].stream;
570 
571 			zassert_equal(reconf_param->qos->sdu, stream->qos->sdu,
572 				      "Unexpected stream SDU");
573 			zassert_equal(reconf_param->qos->rtn, stream->qos->rtn,
574 				      "Unexpected stream RTN");
575 			zassert_equal(reconf_param->qos->phy, stream->qos->phy,
576 				      "Unexpected stream PHY");
577 		}
578 	}
579 
580 	err = bt_bap_broadcast_source_delete(fixture->source);
581 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
582 	fixture->source = NULL;
583 }
584 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_reconfigure_all)585 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_reconfigure_all)
586 {
587 	struct bt_bap_broadcast_source_param *reconf_param = fixture->param;
588 	int err;
589 
590 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
591 	       reconf_param->params_count, fixture->stream_cnt);
592 
593 	err = bt_bap_broadcast_source_create(reconf_param, &fixture->source);
594 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
595 
596 	for (size_t i = 0u; i < reconf_param->params_count; i++) {
597 		for (size_t j = 0u; j < reconf_param->params[i].params_count; j++) {
598 			const struct bt_bap_stream *stream =
599 				reconf_param->params[i].params[j].stream;
600 
601 			zassert_equal(reconf_param->qos->sdu, stream->qos->sdu,
602 				      "Unexpected stream SDU");
603 			zassert_equal(reconf_param->qos->rtn, stream->qos->rtn,
604 				      "Unexpected stream RTN");
605 			zassert_equal(reconf_param->qos->phy, stream->qos->phy,
606 				      "Unexpected stream PHY");
607 		}
608 	}
609 
610 	reconf_param->qos->sdu = 100U;
611 	reconf_param->qos->rtn = 3U;
612 	reconf_param->qos->phy = 1U;
613 
614 	err = bt_bap_broadcast_source_reconfig(fixture->source, reconf_param);
615 	zassert_equal(0, err, "Unable to reconfigure broadcast source: err %d", err);
616 
617 	for (size_t i = 0u; i < reconf_param->params_count; i++) {
618 		for (size_t j = 0u; j < reconf_param->params[i].params_count; j++) {
619 			const struct bt_bap_stream *stream =
620 				reconf_param->params[i].params[j].stream;
621 
622 			zassert_equal(reconf_param->qos->sdu, stream->qos->sdu,
623 				      "Unexpected stream SDU");
624 			zassert_equal(reconf_param->qos->rtn, stream->qos->rtn,
625 				      "Unexpected stream RTN");
626 			zassert_equal(reconf_param->qos->phy, stream->qos->phy,
627 				      "Unexpected stream PHY");
628 		}
629 	}
630 
631 	err = bt_bap_broadcast_source_delete(fixture->source);
632 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
633 	fixture->source = NULL;
634 }
635 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_reconfigure_inval_param_null)636 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_reconfigure_inval_param_null)
637 {
638 	struct bt_bap_broadcast_source_param *param = fixture->param;
639 	int err;
640 
641 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
642 	       param->params_count, fixture->stream_cnt);
643 
644 	err = bt_bap_broadcast_source_create(param, &fixture->source);
645 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
646 
647 	err = bt_bap_broadcast_source_reconfig(fixture->source, NULL);
648 	zassert_not_equal(0, err, "Did not fail with null params");
649 
650 	err = bt_bap_broadcast_source_delete(fixture->source);
651 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
652 	fixture->source = NULL;
653 }
654 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_reconfigure_inval_source_null)655 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_reconfigure_inval_source_null)
656 {
657 	struct bt_bap_broadcast_source_param *param = fixture->param;
658 	int err;
659 
660 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
661 	       param->params_count, fixture->stream_cnt);
662 
663 	err = bt_bap_broadcast_source_create(param, &fixture->source);
664 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
665 
666 	err = bt_bap_broadcast_source_reconfig(NULL, param);
667 	zassert_not_equal(0, err, "Did not fail with null source");
668 
669 	err = bt_bap_broadcast_source_delete(fixture->source);
670 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
671 	fixture->source = NULL;
672 }
673 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_reconfigure_inval_subgroup_params_count_0)674 ZTEST_F(bap_broadcast_source_test_suite,
675 	test_broadcast_source_reconfigure_inval_subgroup_params_count_0)
676 {
677 	struct bt_bap_broadcast_source_param *param = fixture->param;
678 	int err;
679 
680 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
681 	       param->params_count, fixture->stream_cnt);
682 
683 	err = bt_bap_broadcast_source_create(param, &fixture->source);
684 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
685 
686 	param->params_count = 0U;
687 	err = bt_bap_broadcast_source_reconfig(fixture->source, param);
688 	zassert_not_equal(0, err, "Did not fail with params_count %u", param->params_count);
689 
690 	err = bt_bap_broadcast_source_delete(fixture->source);
691 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
692 	fixture->source = NULL;
693 }
694 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_reconfigure_inval_subgroup_params_count_above_max)695 ZTEST_F(bap_broadcast_source_test_suite,
696 	test_broadcast_source_reconfigure_inval_subgroup_params_count_above_max)
697 {
698 	struct bt_bap_broadcast_source_param *param = fixture->param;
699 	int err;
700 
701 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
702 	       param->params_count, fixture->stream_cnt);
703 
704 	err = bt_bap_broadcast_source_create(param, &fixture->source);
705 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
706 
707 	param->params_count = CONFIG_BT_BAP_BROADCAST_SRC_SUBGROUP_COUNT + 1;
708 	err = bt_bap_broadcast_source_reconfig(fixture->source, param);
709 	zassert_not_equal(0, err, "Did not fail with params_count %u", param->params_count);
710 
711 	err = bt_bap_broadcast_source_delete(fixture->source);
712 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
713 	fixture->source = NULL;
714 }
715 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_reconfigure_inval_subgroup_params_null)716 ZTEST_F(bap_broadcast_source_test_suite,
717 	test_broadcast_source_reconfigure_inval_subgroup_params_null)
718 {
719 	struct bt_bap_broadcast_source_param *param = fixture->param;
720 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &param->params[0];
721 	int err;
722 
723 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
724 	       param->params_count, fixture->stream_cnt);
725 
726 	err = bt_bap_broadcast_source_create(param, &fixture->source);
727 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
728 
729 	param->params = NULL;
730 	err = bt_bap_broadcast_source_reconfig(fixture->source, param);
731 	/* Restore the params for the cleanup after function */
732 	param->params = subgroup_params;
733 	zassert_not_equal(0, err, "Did not fail with NULL subgroup params");
734 
735 	err = bt_bap_broadcast_source_delete(fixture->source);
736 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
737 	fixture->source = NULL;
738 }
739 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_reconfigure_inval_qos_null)740 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_reconfigure_inval_qos_null)
741 {
742 	struct bt_bap_broadcast_source_param *param = fixture->param;
743 	struct bt_audio_codec_qos *qos = param->qos;
744 	int err;
745 
746 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
747 	       param->params_count, fixture->stream_cnt);
748 
749 	err = bt_bap_broadcast_source_create(param, &fixture->source);
750 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
751 
752 	param->qos = NULL;
753 	err = bt_bap_broadcast_source_reconfig(fixture->source, param);
754 	/* Restore the params for the cleanup after function */
755 	param->qos = qos;
756 	zassert_not_equal(0, err, "Did not fail with NULL qos");
757 
758 	err = bt_bap_broadcast_source_delete(fixture->source);
759 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
760 	fixture->source = NULL;
761 }
762 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_reconfigure_inval_packing)763 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_reconfigure_inval_packing)
764 {
765 	struct bt_bap_broadcast_source_param *param = fixture->param;
766 	int err;
767 
768 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
769 	       param->params_count, fixture->stream_cnt);
770 
771 	err = bt_bap_broadcast_source_create(param, &fixture->source);
772 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
773 
774 	param->packing = 0x02;
775 	err = bt_bap_broadcast_source_reconfig(fixture->source, param);
776 	zassert_not_equal(0, err, "Did not fail with packing %u", param->packing);
777 
778 	err = bt_bap_broadcast_source_delete(fixture->source);
779 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
780 	fixture->source = NULL;
781 }
782 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_reconfigure_inval_subgroup_params_params_count_0)783 ZTEST_F(bap_broadcast_source_test_suite,
784 	test_broadcast_source_reconfigure_inval_subgroup_params_params_count_0)
785 {
786 	struct bt_bap_broadcast_source_param *param = fixture->param;
787 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &param->params[0];
788 	int err;
789 
790 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
791 	       param->params_count, fixture->stream_cnt);
792 
793 	err = bt_bap_broadcast_source_create(param, &fixture->source);
794 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
795 
796 	subgroup_params->params_count = 0U;
797 	err = bt_bap_broadcast_source_reconfig(fixture->source, param);
798 	zassert_not_equal(0, err, "Did not fail with %u stream params",
799 			  subgroup_params->params_count);
800 
801 	err = bt_bap_broadcast_source_delete(fixture->source);
802 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
803 	fixture->source = NULL;
804 }
805 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_reconfigure_inval_subgroup_params_params_count_above_max)806 ZTEST_F(bap_broadcast_source_test_suite,
807 	test_broadcast_source_reconfigure_inval_subgroup_params_params_count_above_max)
808 {
809 	struct bt_bap_broadcast_source_param *param = fixture->param;
810 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &param->params[0];
811 	int err;
812 
813 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
814 	       param->params_count, fixture->stream_cnt);
815 
816 	err = bt_bap_broadcast_source_create(param, &fixture->source);
817 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
818 
819 	subgroup_params->params_count = CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT + 1;
820 	err = bt_bap_broadcast_source_reconfig(fixture->source, param);
821 	zassert_not_equal(0, err, "Did not fail with %u stream params",
822 			  subgroup_params->params_count);
823 
824 	err = bt_bap_broadcast_source_delete(fixture->source);
825 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
826 	fixture->source = NULL;
827 }
828 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_reconfigure_inval_subgroup_params_stream_params_null)829 ZTEST_F(bap_broadcast_source_test_suite,
830 	test_broadcast_source_reconfigure_inval_subgroup_params_stream_params_null)
831 {
832 	struct bt_bap_broadcast_source_param *param = fixture->param;
833 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &param->params[0];
834 	struct bt_bap_broadcast_source_stream_param *stream_params = &subgroup_params->params[0];
835 	int err;
836 
837 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
838 	       param->params_count, fixture->stream_cnt);
839 
840 	err = bt_bap_broadcast_source_create(param, &fixture->source);
841 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
842 
843 	subgroup_params->params = NULL;
844 	err = bt_bap_broadcast_source_reconfig(fixture->source, param);
845 	/* Restore the params for the cleanup after function */
846 	subgroup_params->params = stream_params;
847 	zassert_not_equal(0, err, "Did not fail with NULL stream params");
848 
849 	err = bt_bap_broadcast_source_delete(fixture->source);
850 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
851 	fixture->source = NULL;
852 }
853 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_reconfigure_inval_subgroup_params_codec_cfg_null)854 ZTEST_F(bap_broadcast_source_test_suite,
855 	test_broadcast_source_reconfigure_inval_subgroup_params_codec_cfg_null)
856 {
857 	struct bt_bap_broadcast_source_param *param = fixture->param;
858 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &param->params[0];
859 	struct bt_audio_codec_cfg *codec_cfg = subgroup_params->codec_cfg;
860 	int err;
861 
862 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
863 	       param->params_count, fixture->stream_cnt);
864 
865 	err = bt_bap_broadcast_source_create(param, &fixture->source);
866 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
867 
868 	subgroup_params->codec_cfg = NULL;
869 	err = bt_bap_broadcast_source_reconfig(fixture->source, param);
870 	/* Restore the params for the cleanup after function */
871 	subgroup_params->codec_cfg = codec_cfg;
872 	zassert_not_equal(0, err, "Did not fail with NULL codec_cfg");
873 
874 	err = bt_bap_broadcast_source_delete(fixture->source);
875 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
876 	fixture->source = NULL;
877 }
878 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_reconfigure_inval_subgroup_params_codec_cfg_data_len)879 ZTEST_F(bap_broadcast_source_test_suite,
880 	test_broadcast_source_reconfigure_inval_subgroup_params_codec_cfg_data_len)
881 {
882 	struct bt_bap_broadcast_source_param *param = fixture->param;
883 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &param->params[0];
884 	struct bt_audio_codec_cfg *codec_cfg = subgroup_params->codec_cfg;
885 	int err;
886 
887 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
888 	       param->params_count, fixture->stream_cnt);
889 
890 	err = bt_bap_broadcast_source_create(param, &fixture->source);
891 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
892 
893 	codec_cfg->data_len = CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE + 1;
894 	err = bt_bap_broadcast_source_reconfig(fixture->source, param);
895 	zassert_not_equal(0, err, "Did not fail with codec_cfg->data_len %zu", codec_cfg->data_len);
896 
897 	err = bt_bap_broadcast_source_delete(fixture->source);
898 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
899 	fixture->source = NULL;
900 }
901 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_reconfigure_inval_subgroup_params_codec_cfg_meta_len)902 ZTEST_F(bap_broadcast_source_test_suite,
903 	test_broadcast_source_reconfigure_inval_subgroup_params_codec_cfg_meta_len)
904 {
905 	struct bt_bap_broadcast_source_param *param = fixture->param;
906 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &param->params[0];
907 	struct bt_audio_codec_cfg *codec_cfg = subgroup_params->codec_cfg;
908 	int err;
909 
910 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
911 	       param->params_count, fixture->stream_cnt);
912 
913 	err = bt_bap_broadcast_source_create(param, &fixture->source);
914 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
915 
916 	codec_cfg->meta_len = CONFIG_BT_AUDIO_CODEC_CFG_MAX_METADATA_SIZE + 1;
917 	err = bt_bap_broadcast_source_reconfig(fixture->source, param);
918 	zassert_not_equal(0, err, "Did not fail with codec_cfg->meta_len %zu", codec_cfg->meta_len);
919 
920 	err = bt_bap_broadcast_source_delete(fixture->source);
921 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
922 	fixture->source = NULL;
923 }
924 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_reconfigure_inval_subgroup_params_codec_cfg_cid)925 ZTEST_F(bap_broadcast_source_test_suite,
926 	test_broadcast_source_reconfigure_inval_subgroup_params_codec_cfg_cid)
927 {
928 	struct bt_bap_broadcast_source_param *param = fixture->param;
929 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &param->params[0];
930 	struct bt_audio_codec_cfg *codec_cfg = subgroup_params->codec_cfg;
931 	int err;
932 
933 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
934 	       param->params_count, fixture->stream_cnt);
935 
936 	err = bt_bap_broadcast_source_create(param, &fixture->source);
937 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
938 
939 	codec_cfg->id = 0x06;
940 	codec_cfg->cid = 0x01; /* Shall be 0 if id == 0x06 (LC3)*/
941 	err = bt_bap_broadcast_source_reconfig(fixture->source, param);
942 	zassert_not_equal(0, err, "Did not fail with codec_cfg->cid %u", codec_cfg->cid);
943 
944 	err = bt_bap_broadcast_source_delete(fixture->source);
945 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
946 	fixture->source = NULL;
947 }
948 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_reconfigure_inval_subgroup_params_codec_cfg_vid)949 ZTEST_F(bap_broadcast_source_test_suite,
950 	test_broadcast_source_reconfigure_inval_subgroup_params_codec_cfg_vid)
951 {
952 	struct bt_bap_broadcast_source_param *param = fixture->param;
953 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &param->params[0];
954 	struct bt_audio_codec_cfg *codec_cfg = subgroup_params->codec_cfg;
955 	int err;
956 
957 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
958 	       param->params_count, fixture->stream_cnt);
959 
960 	err = bt_bap_broadcast_source_create(param, &fixture->source);
961 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
962 
963 	codec_cfg->id = 0x06;
964 	codec_cfg->vid = 0x01; /* Shall be 0 if id == 0x06 (LC3)*/
965 	err = bt_bap_broadcast_source_reconfig(fixture->source, param);
966 	zassert_not_equal(0, err, "Did not fail with codec_cfg->vid %u", codec_cfg->vid);
967 
968 	err = bt_bap_broadcast_source_delete(fixture->source);
969 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
970 	fixture->source = NULL;
971 }
972 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_reconfigure_inval_stream_params_stream_null)973 ZTEST_F(bap_broadcast_source_test_suite,
974 	test_broadcast_source_reconfigure_inval_stream_params_stream_null)
975 {
976 	struct bt_bap_broadcast_source_param *param = fixture->param;
977 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &param->params[0];
978 	struct bt_bap_broadcast_source_stream_param *stream_params = &subgroup_params->params[0];
979 	struct bt_bap_stream *stream = stream_params->stream;
980 	int err;
981 
982 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
983 	       param->params_count, fixture->stream_cnt);
984 
985 	err = bt_bap_broadcast_source_create(param, &fixture->source);
986 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
987 
988 	stream_params->stream = NULL;
989 	err = bt_bap_broadcast_source_reconfig(fixture->source, param);
990 	/* Restore the params for the cleanup after function */
991 	stream_params->stream = stream;
992 	zassert_not_equal(0, err, "Did not fail with NULL stream_params->stream");
993 
994 	err = bt_bap_broadcast_source_delete(fixture->source);
995 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
996 	fixture->source = NULL;
997 }
998 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_reconfigure_inval_stream_params_data_null)999 ZTEST_F(bap_broadcast_source_test_suite,
1000 	test_broadcast_source_reconfigure_inval_stream_params_data_null)
1001 {
1002 	struct bt_bap_broadcast_source_param *param = fixture->param;
1003 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &param->params[0];
1004 	struct bt_bap_broadcast_source_stream_param *stream_params = &subgroup_params->params[0];
1005 	uint8_t *data = stream_params->data;
1006 	int err;
1007 
1008 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
1009 	       param->params_count, fixture->stream_cnt);
1010 
1011 	err = bt_bap_broadcast_source_create(param, &fixture->source);
1012 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
1013 
1014 	stream_params->data = NULL;
1015 	stream_params->data_len = 1;
1016 	err = bt_bap_broadcast_source_reconfig(fixture->source, param);
1017 	/* Restore the params for the cleanup after function */
1018 	stream_params->data = data;
1019 	zassert_not_equal(
1020 		0, err,
1021 		"Did not fail with NULL stream_params->data and stream_params_>data_len %zu",
1022 		stream_params->data_len);
1023 
1024 	err = bt_bap_broadcast_source_delete(fixture->source);
1025 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
1026 	fixture->source = NULL;
1027 }
1028 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_reconfigure_inval_stream_params_data_len)1029 ZTEST_F(bap_broadcast_source_test_suite,
1030 	test_broadcast_source_reconfigure_inval_stream_params_data_len)
1031 {
1032 	struct bt_bap_broadcast_source_param *param = fixture->param;
1033 	struct bt_bap_broadcast_source_subgroup_param *subgroup_params = &param->params[0];
1034 	struct bt_bap_broadcast_source_stream_param *stream_params = &subgroup_params->params[0];
1035 	int err;
1036 
1037 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
1038 	       param->params_count, fixture->stream_cnt);
1039 
1040 	err = bt_bap_broadcast_source_create(param, &fixture->source);
1041 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
1042 
1043 	stream_params->data_len = CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE + 1;
1044 	err = bt_bap_broadcast_source_reconfig(fixture->source, param);
1045 	zassert_not_equal(0, err, "Did not fail with stream_params_>data_len %zu",
1046 			  stream_params->data_len);
1047 
1048 	err = bt_bap_broadcast_source_delete(fixture->source);
1049 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
1050 	fixture->source = NULL;
1051 }
1052 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_reconfigure_inval_state)1053 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_reconfigure_inval_state)
1054 {
1055 	struct bt_bap_broadcast_source_param *param = fixture->param;
1056 	struct bt_bap_broadcast_source *source;
1057 	int err;
1058 
1059 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
1060 	       param->params_count, fixture->stream_cnt);
1061 
1062 	err = bt_bap_broadcast_source_create(param, &fixture->source);
1063 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
1064 	source = fixture->source;
1065 
1066 	err = bt_bap_broadcast_source_delete(fixture->source);
1067 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
1068 	fixture->source = NULL;
1069 
1070 	err = bt_bap_broadcast_source_reconfig(source, param);
1071 	zassert_not_equal(0, err, "Did not fail with deleted broadcast source");
1072 }
1073 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_stop_inval_source_null)1074 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_stop_inval_source_null)
1075 {
1076 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
1077 	struct bt_le_ext_adv ext_adv = {0};
1078 	int err;
1079 
1080 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
1081 	       create_param->params_count, fixture->stream_cnt);
1082 
1083 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
1084 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
1085 
1086 	err = bt_bap_broadcast_source_start(fixture->source, &ext_adv);
1087 	zassert_equal(0, err, "Unable to start broadcast source: err %d", err);
1088 
1089 	err = bt_bap_broadcast_source_stop(NULL);
1090 	zassert_not_equal(0, err, "Did not fail with null source");
1091 }
1092 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_stop_inval_state)1093 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_stop_inval_state)
1094 {
1095 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
1096 	struct bt_le_ext_adv ext_adv = {0};
1097 	int err;
1098 
1099 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
1100 	       create_param->params_count, fixture->stream_cnt);
1101 
1102 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
1103 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
1104 
1105 	err = bt_bap_broadcast_source_start(fixture->source, &ext_adv);
1106 	zassert_equal(0, err, "Unable to start broadcast source: err %d", err);
1107 
1108 	err = bt_bap_broadcast_source_stop(fixture->source);
1109 	zassert_equal(0, err, "Unable to stop broadcast source: err %d", err);
1110 
1111 	err = bt_bap_broadcast_source_stop(NULL);
1112 	zassert_not_equal(0, err, "Did not fail with stopping already stopped source");
1113 }
1114 
1115 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_delete_inval_source_null)1116 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_delete_inval_source_null)
1117 {
1118 	int err;
1119 
1120 	err = bt_bap_broadcast_source_delete(NULL);
1121 	zassert_not_equal(0, err, "Did not fail with null source");
1122 }
1123 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_delete_inval_double_start)1124 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_delete_inval_double_start)
1125 {
1126 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
1127 	struct bt_bap_broadcast_source *source;
1128 	int err;
1129 
1130 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
1131 	       create_param->params_count, fixture->stream_cnt);
1132 
1133 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
1134 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
1135 
1136 	err = bt_bap_broadcast_source_delete(fixture->source);
1137 	zassert_equal(0, err, "Unable to start broadcast source: err %d", err);
1138 
1139 	source = fixture->source;
1140 	/* Set to NULL to avoid deleting it in bap_broadcast_source_test_suite_after */
1141 	fixture->source = NULL;
1142 
1143 	err = bt_bap_broadcast_source_delete(source);
1144 	zassert_not_equal(0, err, "Did not fail with deleting already deleting source");
1145 }
1146 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_get_id)1147 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_get_id)
1148 {
1149 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
1150 	uint32_t broadcast_id;
1151 	int err;
1152 
1153 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
1154 	       create_param->params_count, fixture->stream_cnt);
1155 
1156 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
1157 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
1158 
1159 	err = bt_bap_broadcast_source_get_id(fixture->source, &broadcast_id);
1160 	zassert_equal(0, err, "Unable to get broadcast ID: err %d", err);
1161 
1162 	err = bt_bap_broadcast_source_delete(fixture->source);
1163 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
1164 	fixture->source = NULL;
1165 }
1166 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_get_base_single_bis)1167 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_get_base_single_bis)
1168 {
1169 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
1170 	int err;
1171 
1172 	const uint8_t expected_base[] = {
1173 		0x51, 0x18,                   /* uuid */
1174 		0x40, 0x9C, 0x00,             /* pd */
1175 		0x01,                         /* subgroup count */
1176 		0x01,                         /* bis count */
1177 		0x06, 0x00, 0x00, 0x00, 0x00, /* LC3 codec_id*/
1178 		0x10,                         /* cc length */
1179 		0x02, 0x01, 0x03, 0x02, 0x02, 0x01, 0x05, 0x03,
1180 		0x01, 0x00, 0x00, 0x00, 0x03, 0x04, 0x28, 0x00, /* cc */
1181 		0x04,                                           /* meta length */
1182 		0x03, 0x02, 0x01, 0x00,                         /* meta */
1183 		0x01,                                           /* bis index */
1184 		0x03,                                           /* bis cc length */
1185 		0x02, 0x03, 0x03                                /* bis cc length */
1186 	};
1187 
1188 	NET_BUF_SIMPLE_DEFINE(base_buf, 64);
1189 
1190 	/* Make the create param simpler for verification */
1191 	create_param->params_count = 1U;
1192 	create_param->params[0].params_count = 1U;
1193 
1194 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
1195 	       create_param->params_count, fixture->stream_cnt);
1196 
1197 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
1198 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
1199 
1200 	err = bt_bap_broadcast_source_get_base(fixture->source, &base_buf);
1201 	zassert_equal(0, err, "Unable to get broadcast source BASE: err %d", err);
1202 
1203 	zassert_equal(sizeof(expected_base), base_buf.len, "Incorrect base_buf.len %u, expected %u",
1204 		      base_buf.len, sizeof(expected_base));
1205 
1206 	/* Use memcmp to print the buffers if they are not identical as zassert_mem_equal does not
1207 	 * do that
1208 	 */
1209 	if (memcmp(expected_base, base_buf.data, base_buf.len) != 0) {
1210 		for (size_t i = 0U; i < base_buf.len; i++) {
1211 			printk("[%zu]: 0x%02X %s 0x%02X\n", i, expected_base[i],
1212 			       expected_base[i] == base_buf.data[i] ? "==" : "!=",
1213 			       base_buf.data[i]);
1214 		}
1215 
1216 		zassert_mem_equal(expected_base, base_buf.data, base_buf.len);
1217 	}
1218 
1219 	err = bt_bap_broadcast_source_delete(fixture->source);
1220 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
1221 	fixture->source = NULL;
1222 }
1223 
ZTEST_F(bap_broadcast_source_test_suite,test_broadcast_source_get_base)1224 ZTEST_F(bap_broadcast_source_test_suite, test_broadcast_source_get_base)
1225 {
1226 	struct bt_bap_broadcast_source_param *create_param = fixture->param;
1227 	int err;
1228 
1229 	const uint8_t expected_base[] = {
1230 		0x51, 0x18,                   /* uuid */
1231 		0x40, 0x9C, 0x00,             /* pd */
1232 		0x02,                         /* subgroup count */
1233 		0x01,                         /* Subgroup 1: bis count */
1234 		0x06, 0x00, 0x00, 0x00, 0x00, /* LC3 codec_id*/
1235 		0x10,                         /* cc length */
1236 		0x02, 0x01, 0x03, 0x02, 0x02, 0x01, 0x05, 0x03,
1237 		0x01, 0x00, 0x00, 0x00, 0x03, 0x04, 0x28, 0x00, /* cc */
1238 		0x04,                                           /* meta length */
1239 		0x03, 0x02, 0x01, 0x00,                         /* meta */
1240 		0x01,                                           /* bis index */
1241 		0x03,                                           /* bis cc length */
1242 		0x02, 0x03, 0x03,                               /* bis cc length */
1243 		0x01,                                           /* Subgroup 1: bis count */
1244 		0x06, 0x00, 0x00, 0x00, 0x00,                   /* LC3 codec_id*/
1245 		0x10,                                           /* cc length */
1246 		0x02, 0x01, 0x03, 0x02, 0x02, 0x01, 0x05, 0x03,
1247 		0x01, 0x00, 0x00, 0x00, 0x03, 0x04, 0x28, 0x00, /* cc */
1248 		0x04,                                           /* meta length */
1249 		0x03, 0x02, 0x01, 0x00,                         /* meta */
1250 		0x02,                                           /* bis index */
1251 		0x03,                                           /* bis cc length */
1252 		0x02, 0x03, 0x03                                /* bis cc length */
1253 	};
1254 
1255 	NET_BUF_SIMPLE_DEFINE(base_buf, 128);
1256 
1257 	printk("Creating broadcast source with %zu subgroups with %zu streams\n",
1258 	       create_param->params_count, fixture->stream_cnt);
1259 
1260 	err = bt_bap_broadcast_source_create(create_param, &fixture->source);
1261 	zassert_equal(0, err, "Unable to create broadcast source: err %d", err);
1262 
1263 	err = bt_bap_broadcast_source_get_base(fixture->source, &base_buf);
1264 	zassert_equal(0, err, "Unable to get broadcast source BASE: err %d", err);
1265 
1266 	zassert_equal(sizeof(expected_base), base_buf.len, "Incorrect base_buf.len %u, expected %u",
1267 		      base_buf.len, sizeof(expected_base));
1268 
1269 	/* Use memcmp to print the buffers if they are not identical as zassert_mem_equal does not
1270 	 * do that
1271 	 */
1272 	if (memcmp(expected_base, base_buf.data, base_buf.len) != 0) {
1273 		for (size_t i = 0U; i < base_buf.len; i++) {
1274 			printk("[%zu]: 0x%02X %s 0x%02X\n", i, expected_base[i],
1275 			       expected_base[i] == base_buf.data[i] ? "==" : "!=",
1276 			       base_buf.data[i]);
1277 		}
1278 
1279 		zassert_mem_equal(expected_base, base_buf.data, base_buf.len);
1280 	}
1281 
1282 	err = bt_bap_broadcast_source_delete(fixture->source);
1283 	zassert_equal(0, err, "Unable to delete broadcast source: err %d", err);
1284 	fixture->source = NULL;
1285 }
1286