1 /* test_broadcast_reception.c - unit test for broadcast reception start and stop */
2 
3 /*
4  * Copyright (c) 2024 Nordic Semiconductor ASA
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 #include <errno.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <string.h>
12 
13 #include <zephyr/autoconf.h>
14 #include <zephyr/bluetooth/addr.h>
15 #include <zephyr/bluetooth/audio/audio.h>
16 #include <zephyr/bluetooth/audio/bap.h>
17 #include <zephyr/bluetooth/audio/cap.h>
18 #include <zephyr/bluetooth/conn.h>
19 #include <zephyr/bluetooth/gap.h>
20 #include <zephyr/bluetooth/hci_types.h>
21 #include <zephyr/fff.h>
22 #include <zephyr/logging/log.h>
23 #include <zephyr/sys/printk.h>
24 #include <zephyr/sys/util.h>
25 #include <zephyr/sys/util_macro.h>
26 #include <zephyr/ztest_assert.h>
27 #include <zephyr/ztest_test.h>
28 
29 #include "cap_commander.h"
30 #include "conn.h"
31 #include "expects_util.h"
32 #include "cap_mocks.h"
33 #include "test_common.h"
34 
35 LOG_MODULE_REGISTER(bt_broadcast_reception_test, CONFIG_BT_CAP_COMMANDER_LOG_LEVEL);
36 
37 #define FFF_GLOBALS
38 
39 #define SID          0x0E
40 #define ADV_INTERVAL 10
41 #define BROADCAST_ID 0x55AA55
42 
43 struct cap_commander_test_broadcast_reception_fixture {
44 	struct bt_conn conns[CONFIG_BT_MAX_CONN];
45 
46 	struct bt_bap_bass_subgroup subgroups[CONFIG_BT_BAP_BASS_MAX_SUBGROUPS];
47 	struct bt_cap_commander_broadcast_reception_start_member_param
48 		start_member_params[CONFIG_BT_MAX_CONN];
49 	struct bt_cap_commander_broadcast_reception_start_param start_param;
50 	struct bt_cap_commander_broadcast_reception_stop_member_param
51 		stop_member_params[CONFIG_BT_MAX_CONN];
52 	struct bt_cap_commander_broadcast_reception_stop_param stop_param;
53 	struct bt_bap_broadcast_assistant_cb broadcast_assistant_cb;
54 };
55 
56 /* src_id can not be part of the fixture since it is accessed in the callback function */
57 static uint8_t src_id[CONFIG_BT_MAX_CONN];
58 
59 static void test_start_param_init(void *f);
60 static void test_stop_param_init(void *f);
61 
cap_commander_broadcast_assistant_recv_state_cb(struct bt_conn * conn,int err,const struct bt_bap_scan_delegator_recv_state * state)62 static void cap_commander_broadcast_assistant_recv_state_cb(
63 	struct bt_conn *conn, int err, const struct bt_bap_scan_delegator_recv_state *state)
64 {
65 	uint8_t index;
66 
67 	index = bt_conn_index(conn);
68 	src_id[index] = state->src_id;
69 }
70 
cap_commander_test_broadcast_reception_fixture_init(struct cap_commander_test_broadcast_reception_fixture * fixture)71 static void cap_commander_test_broadcast_reception_fixture_init(
72 	struct cap_commander_test_broadcast_reception_fixture *fixture)
73 {
74 	int err;
75 
76 	for (size_t i = 0; i < ARRAY_SIZE(fixture->conns); i++) {
77 		test_conn_init(&fixture->conns[i]);
78 		fixture->conns[i].index = i;
79 	}
80 	test_start_param_init(fixture);
81 	test_stop_param_init(fixture);
82 
83 	fixture->broadcast_assistant_cb.recv_state =
84 		cap_commander_broadcast_assistant_recv_state_cb;
85 	err = bt_bap_broadcast_assistant_register_cb(&fixture->broadcast_assistant_cb);
86 	zassert_equal(0, err, "Failed registering broadcast assistant callback functions %d", err);
87 }
88 
cap_commander_test_broadcast_reception_setup(void)89 static void *cap_commander_test_broadcast_reception_setup(void)
90 {
91 	struct cap_commander_test_broadcast_reception_fixture *fixture;
92 
93 	fixture = malloc(sizeof(*fixture));
94 	zassert_not_null(fixture);
95 
96 	return fixture;
97 }
98 
cap_commander_test_broadcast_reception_before(void * f)99 static void cap_commander_test_broadcast_reception_before(void *f)
100 {
101 	int err;
102 	struct cap_commander_test_broadcast_reception_fixture *fixture = f;
103 
104 	memset(f, 0, sizeof(struct cap_commander_test_broadcast_reception_fixture));
105 	cap_commander_test_broadcast_reception_fixture_init(fixture);
106 
107 	for (size_t i = 0; i < ARRAY_SIZE(fixture->conns); i++) {
108 		err = bt_cap_commander_discover(&fixture->conns[i]);
109 		zassert_equal(0, err, "Unexpected return value %d", err);
110 	}
111 }
112 
cap_commander_test_broadcast_reception_after(void * f)113 static void cap_commander_test_broadcast_reception_after(void *f)
114 {
115 	struct cap_commander_test_broadcast_reception_fixture *fixture = f;
116 
117 	bt_cap_commander_unregister_cb(&mock_cap_commander_cb);
118 	bt_bap_broadcast_assistant_unregister_cb(&fixture->broadcast_assistant_cb);
119 
120 	/* We need to cleanup since the CAP commander remembers state */
121 	(void)bt_cap_commander_cancel();
122 
123 	for (size_t i = 0; i < ARRAY_SIZE(fixture->conns); i++) {
124 		mock_bt_conn_disconnected(&fixture->conns[i], BT_HCI_ERR_REMOTE_USER_TERM_CONN);
125 	}
126 }
127 
cap_commander_test_broadcast_reception_teardown(void * f)128 static void cap_commander_test_broadcast_reception_teardown(void *f)
129 {
130 	free(f);
131 }
132 
test_start_param_init(void * f)133 static void test_start_param_init(void *f)
134 {
135 	struct cap_commander_test_broadcast_reception_fixture *fixture = f;
136 	int err;
137 
138 	fixture->start_param.type = BT_CAP_SET_TYPE_AD_HOC;
139 	fixture->start_param.param = fixture->start_member_params;
140 
141 	fixture->start_param.count = ARRAY_SIZE(fixture->start_member_params);
142 
143 	for (size_t i = 0; i < ARRAY_SIZE(fixture->subgroups); i++) {
144 		fixture->subgroups[i].bis_sync = BIT(i);
145 		fixture->subgroups[i].metadata_len = 0;
146 	}
147 
148 	for (size_t i = 0U; i < ARRAY_SIZE(fixture->start_member_params); i++) {
149 		fixture->start_member_params[i].member.member = &fixture->conns[i];
150 		bt_addr_le_copy(&fixture->start_member_params[i].addr, BT_ADDR_LE_ANY);
151 		fixture->start_member_params[i].adv_sid = SID;
152 		fixture->start_member_params[i].pa_interval = ADV_INTERVAL;
153 		fixture->start_member_params[i].broadcast_id = BROADCAST_ID;
154 		memcpy(fixture->start_member_params[i].subgroups, &fixture->subgroups[0],
155 		       sizeof(struct bt_bap_bass_subgroup) * CONFIG_BT_BAP_BASS_MAX_SUBGROUPS);
156 		fixture->start_member_params[i].num_subgroups = CONFIG_BT_BAP_BASS_MAX_SUBGROUPS;
157 	}
158 
159 	for (size_t i = 0; i < ARRAY_SIZE(fixture->conns); i++) {
160 		err = bt_cap_commander_discover(&fixture->conns[i]);
161 		zassert_equal(0, err, "Unexpected return value %d", err);
162 	}
163 }
164 
test_stop_param_init(void * f)165 static void test_stop_param_init(void *f)
166 {
167 	struct cap_commander_test_broadcast_reception_fixture *fixture = f;
168 
169 	fixture->stop_param.type = BT_CAP_SET_TYPE_AD_HOC;
170 	fixture->stop_param.param = fixture->stop_member_params;
171 	fixture->stop_param.count = ARRAY_SIZE(fixture->stop_member_params);
172 
173 	for (size_t i = 0U; i < ARRAY_SIZE(fixture->stop_member_params); i++) {
174 		fixture->stop_member_params[i].member.member = &fixture->conns[i];
175 		fixture->stop_member_params[i].src_id = 0;
176 		fixture->stop_member_params[i].num_subgroups = CONFIG_BT_BAP_BASS_MAX_SUBGROUPS;
177 	}
178 }
179 
180 static void
test_broadcast_reception_start(struct bt_cap_commander_broadcast_reception_start_param * start_param)181 test_broadcast_reception_start(struct bt_cap_commander_broadcast_reception_start_param *start_param)
182 {
183 	int err;
184 
185 	err = bt_cap_commander_broadcast_reception_start(start_param);
186 	zassert_equal(0, err, "Unexpected return value %d", err);
187 
188 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_start", 1,
189 			   mock_cap_commander_broadcast_reception_start_cb_fake.call_count);
190 	zassert_equal_ptr(NULL,
191 			  mock_cap_commander_broadcast_reception_start_cb_fake.arg0_history[0]);
192 	zassert_equal(0, mock_cap_commander_broadcast_reception_start_cb_fake.arg1_history[0]);
193 }
194 
195 static void
test_broadcast_reception_stop(struct bt_cap_commander_broadcast_reception_stop_param * stop_param)196 test_broadcast_reception_stop(struct bt_cap_commander_broadcast_reception_stop_param *stop_param)
197 {
198 	int err;
199 
200 	err = bt_cap_commander_broadcast_reception_stop(stop_param);
201 	zassert_equal(0, err, "Unexpected return value %d", err);
202 
203 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_stop", 1,
204 			   mock_cap_commander_broadcast_reception_stop_cb_fake.call_count);
205 	zassert_equal_ptr(NULL,
206 			  mock_cap_commander_broadcast_reception_stop_cb_fake.arg0_history[0]);
207 	zassert_equal(0, mock_cap_commander_broadcast_reception_stop_cb_fake.arg1_history[0]);
208 }
209 
210 ZTEST_SUITE(cap_commander_test_broadcast_reception, NULL,
211 	    cap_commander_test_broadcast_reception_setup,
212 	    cap_commander_test_broadcast_reception_before,
213 	    cap_commander_test_broadcast_reception_after,
214 	    cap_commander_test_broadcast_reception_teardown);
215 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_start)216 ZTEST_F(cap_commander_test_broadcast_reception, test_commander_reception_start)
217 {
218 	int err;
219 
220 	err = bt_cap_commander_register_cb(&mock_cap_commander_cb);
221 	zassert_equal(0, err, "Unexpected return value %d", err);
222 
223 	test_broadcast_reception_start(&fixture->start_param);
224 }
225 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_start_one_subgroup)226 ZTEST_F(cap_commander_test_broadcast_reception, test_commander_reception_start_one_subgroup)
227 {
228 	int err;
229 
230 	err = bt_cap_commander_register_cb(&mock_cap_commander_cb);
231 	zassert_equal(0, err, "Unexpected return value %d", err);
232 
233 	/* We test with one subgroup, instead of CONFIG_BT_BAP_BASS_MAX_SUBGROUPS subgroups */
234 	for (size_t i = 0U; i < CONFIG_BT_MAX_CONN; i++) {
235 		fixture->start_param.param[i].num_subgroups = 1;
236 	}
237 
238 	test_broadcast_reception_start(&fixture->start_param);
239 }
240 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_start_double)241 ZTEST_F(cap_commander_test_broadcast_reception, test_commander_reception_start_double)
242 {
243 	int err;
244 
245 	err = bt_cap_commander_register_cb(&mock_cap_commander_cb);
246 	zassert_equal(0, err, "Unexpected return value %d", err);
247 
248 	test_broadcast_reception_start(&fixture->start_param);
249 
250 	/*
251 	 * We can not use test_broadcast_reception_start because of the check on how often the
252 	 * callback function is called
253 	 */
254 	err = bt_cap_commander_broadcast_reception_start(&fixture->start_param);
255 	zassert_equal(0, err, "Unexpected return value %d", err);
256 
257 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_start", 2,
258 			   mock_cap_commander_broadcast_reception_start_cb_fake.call_count);
259 }
260 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_start_inval_param_null)261 ZTEST_F(cap_commander_test_broadcast_reception, test_commander_reception_start_inval_param_null)
262 {
263 	int err;
264 
265 	err = bt_cap_commander_broadcast_reception_start(NULL);
266 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
267 
268 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_start", 0,
269 			   mock_cap_commander_broadcast_reception_start_cb_fake.call_count);
270 }
271 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_start_inval_param_zero_count)272 ZTEST_F(cap_commander_test_broadcast_reception,
273 	test_commander_reception_start_inval_param_zero_count)
274 {
275 	int err;
276 
277 	fixture->start_param.count = 0;
278 
279 	err = bt_cap_commander_broadcast_reception_start(&fixture->start_param);
280 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
281 
282 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_start", 0,
283 			   mock_cap_commander_broadcast_reception_start_cb_fake.call_count);
284 }
285 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_start_inval_param_high_count)286 ZTEST_F(cap_commander_test_broadcast_reception,
287 	test_commander_reception_start_inval_param_high_count)
288 {
289 	int err;
290 
291 	fixture->start_param.count = CONFIG_BT_MAX_CONN + 1;
292 
293 	err = bt_cap_commander_broadcast_reception_start(&fixture->start_param);
294 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
295 
296 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_start", 0,
297 			   mock_cap_commander_broadcast_reception_start_cb_fake.call_count);
298 }
299 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_start_inval_param_null_param)300 ZTEST_F(cap_commander_test_broadcast_reception,
301 	test_commander_reception_start_inval_param_null_param)
302 {
303 	int err;
304 
305 	fixture->start_param.type = BT_CAP_SET_TYPE_AD_HOC;
306 	fixture->start_param.param = NULL;
307 	fixture->start_param.count = ARRAY_SIZE(fixture->conns);
308 
309 	err = bt_cap_commander_broadcast_reception_start(&fixture->start_param);
310 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
311 
312 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_start", 0,
313 			   mock_cap_commander_broadcast_reception_start_cb_fake.call_count);
314 }
315 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_start_inval_null_member)316 ZTEST_F(cap_commander_test_broadcast_reception, test_commander_reception_start_inval_null_member)
317 {
318 	int err;
319 
320 	fixture->start_param.param[0].member.member = NULL;
321 
322 	err = bt_cap_commander_broadcast_reception_start(&fixture->start_param);
323 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
324 
325 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_start", 0,
326 			   mock_cap_commander_broadcast_reception_start_cb_fake.call_count);
327 }
328 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_start_inval_missing_cas)329 ZTEST_F(cap_commander_test_broadcast_reception, test_commander_reception_start_inval_missing_cas)
330 {
331 	int err;
332 
333 	fixture->start_param.type = BT_CAP_SET_TYPE_CSIP;
334 
335 	err = bt_cap_commander_broadcast_reception_start(&fixture->start_param);
336 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
337 
338 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_start", 0,
339 			   mock_cap_commander_broadcast_reception_start_cb_fake.call_count);
340 }
341 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_start_inval_addr_type)342 ZTEST_F(cap_commander_test_broadcast_reception, test_commander_reception_start_inval_addr_type)
343 {
344 	int err;
345 
346 	fixture->start_param.param[0].addr.type = BT_ADDR_LE_RANDOM + 1;
347 
348 	err = bt_cap_commander_broadcast_reception_start(&fixture->start_param);
349 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
350 
351 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_start", 0,
352 			   mock_cap_commander_broadcast_reception_start_cb_fake.call_count);
353 }
354 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_start_inval_sid)355 ZTEST_F(cap_commander_test_broadcast_reception, test_commander_reception_start_inval_sid)
356 {
357 	int err;
358 
359 	fixture->start_param.param[0].adv_sid = BT_GAP_SID_MAX + 1;
360 
361 	err = bt_cap_commander_broadcast_reception_start(&fixture->start_param);
362 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
363 
364 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_start", 0,
365 			   mock_cap_commander_broadcast_reception_start_cb_fake.call_count);
366 }
367 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_start_inval_pa_interval_low)368 ZTEST_F(cap_commander_test_broadcast_reception,
369 	test_commander_reception_start_inval_pa_interval_low)
370 {
371 	int err;
372 
373 	fixture->start_param.param[0].pa_interval = BT_GAP_PER_ADV_MIN_INTERVAL - 1;
374 
375 	err = bt_cap_commander_broadcast_reception_start(&fixture->start_param);
376 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
377 
378 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_start", 0,
379 			   mock_cap_commander_broadcast_reception_start_cb_fake.call_count);
380 }
381 
382 /*
383  * Test for pa_interval_high omitted
384  * pa_interval is a uint16_t, BT_GAP_PER_ADV_MAX_INTERVAL is defined as 0xFFFF
385  * and therefor we can not test in the current implementation
386  */
387 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_start_inval_broadcast_id)388 ZTEST_F(cap_commander_test_broadcast_reception, test_commander_reception_start_inval_broadcast_id)
389 {
390 	int err;
391 
392 	fixture->start_param.param[0].broadcast_id = BT_AUDIO_BROADCAST_ID_MAX + 1;
393 
394 	err = bt_cap_commander_broadcast_reception_start(&fixture->start_param);
395 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
396 
397 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_start", 0,
398 			   mock_cap_commander_broadcast_reception_start_cb_fake.call_count);
399 }
400 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_start_inval_no_subgroups)401 ZTEST_F(cap_commander_test_broadcast_reception, test_commander_reception_start_inval_no_subgroups)
402 {
403 	int err;
404 
405 	fixture->start_param.param[0].num_subgroups = 0;
406 
407 	err = bt_cap_commander_broadcast_reception_start(&fixture->start_param);
408 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
409 
410 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_start", 0,
411 			   mock_cap_commander_broadcast_reception_start_cb_fake.call_count);
412 }
413 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_start_inval_num_subgroups)414 ZTEST_F(cap_commander_test_broadcast_reception, test_commander_reception_start_inval_num_subgroups)
415 {
416 	int err;
417 
418 	fixture->start_param.param[0].num_subgroups = CONFIG_BT_BAP_BASS_MAX_SUBGROUPS + 1;
419 
420 	err = bt_cap_commander_broadcast_reception_start(&fixture->start_param);
421 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
422 
423 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_start", 0,
424 			   mock_cap_commander_broadcast_reception_start_cb_fake.call_count);
425 }
426 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_start_inval_duplicate_bis_sync)427 ZTEST_F(cap_commander_test_broadcast_reception,
428 	test_commander_reception_start_inval_duplicate_bis_sync)
429 {
430 	int err;
431 
432 	if (CONFIG_BT_BAP_BASS_MAX_SUBGROUPS == 1) {
433 		ztest_test_skip();
434 	}
435 
436 	fixture->start_param.param[0].subgroups[0].bis_sync =
437 		fixture->start_param.param[0].subgroups[1].bis_sync;
438 
439 	err = bt_cap_commander_broadcast_reception_start(&fixture->start_param);
440 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
441 
442 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_start", 0,
443 			   mock_cap_commander_broadcast_reception_start_cb_fake.call_count);
444 }
445 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_start_inval_metadata_len)446 ZTEST_F(cap_commander_test_broadcast_reception, test_commander_reception_start_inval_metadata_len)
447 {
448 	int err;
449 
450 	if (CONFIG_BT_AUDIO_CODEC_CFG_MAX_METADATA_SIZE >= UINT8_MAX) {
451 		ztest_test_skip();
452 	}
453 
454 	fixture->start_param.param[0].subgroups[0].metadata_len =
455 		(uint8_t)(CONFIG_BT_AUDIO_CODEC_CFG_MAX_METADATA_SIZE + 1);
456 
457 	err = bt_cap_commander_broadcast_reception_start(&fixture->start_param);
458 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
459 
460 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_start", 0,
461 			   mock_cap_commander_broadcast_reception_start_cb_fake.call_count);
462 }
463 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_stop_default_subgroups)464 ZTEST_F(cap_commander_test_broadcast_reception, test_commander_reception_stop_default_subgroups)
465 {
466 	int err;
467 
468 	err = bt_cap_commander_register_cb(&mock_cap_commander_cb);
469 	zassert_equal(0, err, "Unexpected return value %d", err);
470 
471 	test_broadcast_reception_start(&fixture->start_param);
472 
473 	for (size_t i = 0U; i < CONFIG_BT_MAX_CONN; i++) {
474 		fixture->stop_param.param[i].src_id = src_id[i];
475 	}
476 
477 	test_broadcast_reception_stop(&fixture->stop_param);
478 }
479 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_stop_one_subgroup)480 ZTEST_F(cap_commander_test_broadcast_reception, test_commander_reception_stop_one_subgroup)
481 {
482 	int err;
483 
484 	err = bt_cap_commander_register_cb(&mock_cap_commander_cb);
485 	zassert_equal(0, err, "Unexpected return value %d", err);
486 
487 	test_broadcast_reception_start(&fixture->start_param);
488 
489 	/* We test with one subgroup, instead of CONFIG_BT_BAP_BASS_MAX_SUBGROUPS subgroups */
490 	for (size_t i = 0U; i < CONFIG_BT_MAX_CONN; i++) {
491 		fixture->stop_param.param[i].num_subgroups = 1;
492 		fixture->stop_param.param[i].src_id = src_id[i];
493 	}
494 
495 	test_broadcast_reception_stop(&fixture->stop_param);
496 }
497 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_stop_double)498 ZTEST_F(cap_commander_test_broadcast_reception, test_commander_reception_stop_double)
499 {
500 	int err;
501 
502 	err = bt_cap_commander_register_cb(&mock_cap_commander_cb);
503 	zassert_equal(0, err, "Unexpected return value %d", err);
504 
505 	test_broadcast_reception_start(&fixture->start_param);
506 
507 	for (size_t i = 0U; i < CONFIG_BT_MAX_CONN; i++) {
508 		printk("Source ID %d: %d %d\n", i, fixture->stop_param.param[i].src_id, src_id[i]);
509 
510 		fixture->stop_param.param[i].src_id = src_id[i];
511 	}
512 
513 	test_broadcast_reception_stop(&fixture->stop_param);
514 
515 	/*
516 	 * We can not use test_broadcast_reception_stop because of the check on how often the
517 	 * callback function is called
518 	 */
519 	err = bt_cap_commander_broadcast_reception_stop(&fixture->stop_param);
520 	zassert_equal(0, err, "Unexpected return value %d", err);
521 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_stop", 2,
522 			   mock_cap_commander_broadcast_reception_stop_cb_fake.call_count);
523 }
524 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_stop_inval_param_null)525 ZTEST_F(cap_commander_test_broadcast_reception, test_commander_reception_stop_inval_param_null)
526 {
527 	int err;
528 
529 	err = bt_cap_commander_broadcast_reception_stop(NULL);
530 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
531 
532 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_stop", 0,
533 			   mock_cap_commander_broadcast_reception_stop_cb_fake.call_count);
534 }
535 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_stop_inval_param_zero_count)536 ZTEST_F(cap_commander_test_broadcast_reception,
537 	test_commander_reception_stop_inval_param_zero_count)
538 {
539 	int err;
540 
541 	fixture->stop_param.count = 0;
542 
543 	err = bt_cap_commander_broadcast_reception_stop(&fixture->stop_param);
544 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
545 
546 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_stop", 0,
547 			   mock_cap_commander_broadcast_reception_stop_cb_fake.call_count);
548 }
549 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_stop_inval_param_high_count)550 ZTEST_F(cap_commander_test_broadcast_reception,
551 	test_commander_reception_stop_inval_param_high_count)
552 {
553 	int err;
554 
555 	fixture->stop_param.count = CONFIG_BT_MAX_CONN + 1;
556 
557 	err = bt_cap_commander_broadcast_reception_stop(&fixture->stop_param);
558 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
559 
560 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_stop", 0,
561 			   mock_cap_commander_broadcast_reception_stop_cb_fake.call_count);
562 }
563 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_stop_inval_param_null_param)564 ZTEST_F(cap_commander_test_broadcast_reception,
565 	test_commander_reception_stop_inval_param_null_param)
566 {
567 	int err;
568 
569 	fixture->stop_param.type = BT_CAP_SET_TYPE_AD_HOC;
570 	fixture->stop_param.param = NULL;
571 	fixture->stop_param.count = ARRAY_SIZE(fixture->conns);
572 
573 	err = bt_cap_commander_broadcast_reception_stop(&fixture->stop_param);
574 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
575 
576 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_stop", 0,
577 			   mock_cap_commander_broadcast_reception_stop_cb_fake.call_count);
578 }
579 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_stop_inval_null_member)580 ZTEST_F(cap_commander_test_broadcast_reception, test_commander_reception_stop_inval_null_member)
581 {
582 	int err;
583 
584 	fixture->stop_param.param[0].member.member = NULL;
585 
586 	err = bt_cap_commander_broadcast_reception_stop(&fixture->stop_param);
587 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
588 
589 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_stop", 0,
590 			   mock_cap_commander_broadcast_reception_stop_cb_fake.call_count);
591 }
592 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_stop_inval_missing_cas)593 ZTEST_F(cap_commander_test_broadcast_reception, test_commander_reception_stop_inval_missing_cas)
594 {
595 	int err;
596 
597 	fixture->stop_param.type = BT_CAP_SET_TYPE_CSIP;
598 
599 	err = bt_cap_commander_broadcast_reception_stop(&fixture->stop_param);
600 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
601 
602 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_stop", 0,
603 			   mock_cap_commander_broadcast_reception_stop_cb_fake.call_count);
604 }
605 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_stop_inval_no_subgroups)606 ZTEST_F(cap_commander_test_broadcast_reception, test_commander_reception_stop_inval_no_subgroups)
607 {
608 	int err;
609 
610 	fixture->stop_param.param[0].num_subgroups = 0;
611 
612 	err = bt_cap_commander_broadcast_reception_stop(&fixture->stop_param);
613 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
614 
615 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_stop", 0,
616 			   mock_cap_commander_broadcast_reception_stop_cb_fake.call_count);
617 }
618 
ZTEST_F(cap_commander_test_broadcast_reception,test_commander_reception_stop_inval_num_subgroups)619 ZTEST_F(cap_commander_test_broadcast_reception, test_commander_reception_stop_inval_num_subgroups)
620 {
621 	int err;
622 
623 	fixture->stop_param.param[0].num_subgroups = CONFIG_BT_BAP_BASS_MAX_SUBGROUPS + 1;
624 
625 	err = bt_cap_commander_broadcast_reception_stop(&fixture->stop_param);
626 	zassert_equal(-EINVAL, err, "Unexpected return value %d", err);
627 
628 	zexpect_call_count("bt_cap_commander_cb.broadcast_reception_stop", 0,
629 			   mock_cap_commander_broadcast_reception_stop_cb_fake.call_count);
630 }
631