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 
11 #include <zephyr/bluetooth/audio/bap.h>
12 #include <zephyr/fff.h>
13 
14 DEFINE_FFF_GLOBALS;
15 
16 struct bap_base_test_suite_fixture {
17 	struct bt_data valid_base_ad;
18 	uint8_t *valid_base_data;
19 	struct bt_data invalid_base_ad;
20 	uint8_t *invalid_base_data;
21 };
22 
bap_base_test_suite_fixture_init(struct bap_base_test_suite_fixture * fixture)23 static void bap_base_test_suite_fixture_init(struct bap_base_test_suite_fixture *fixture)
24 {
25 	uint8_t base_data[] = {
26 		0x51, 0x18,                   /* uuid */
27 		0x40, 0x9C, 0x00,             /* pd */
28 		0x02,                         /* subgroup count */
29 		0x01,                         /* Subgroup 1: bis count */
30 		0x06, 0x00, 0x00, 0x00, 0x00, /* LC3 codec_id*/
31 		0x10,                         /* cc length */
32 		0x02, 0x01, 0x03, 0x02, 0x02, 0x01, 0x05, 0x03,
33 		0x01, 0x00, 0x00, 0x00, 0x03, 0x04, 0x28, 0x00, /* cc */
34 		0x04,                                           /* meta length */
35 		0x03, 0x02, 0x01, 0x00,                         /* meta */
36 		0x01,                                           /* bis index */
37 		0x03,                                           /* bis cc length */
38 		0x02, 0x03, 0x03,                               /* bis cc length */
39 		0x01,                                           /* Subgroup 1: bis count */
40 		0x06, 0x00, 0x00, 0x00, 0x00,                   /* LC3 codec_id*/
41 		0x10,                                           /* cc length */
42 		0x02, 0x01, 0x03, 0x02, 0x02, 0x01, 0x05, 0x03,
43 		0x01, 0x00, 0x00, 0x00, 0x03, 0x04, 0x28, 0x00, /* cc */
44 		0x04,                                           /* meta length */
45 		0x03, 0x02, 0x01, 0x00,                         /* meta */
46 		0x02,                                           /* bis index */
47 		0x03,                                           /* bis cc length */
48 		0x02, 0x03, 0x03                                /* bis cc length */
49 	};
50 
51 	fixture->valid_base_data = malloc(sizeof(base_data));
52 	zassert_not_null(fixture->valid_base_data);
53 	memcpy(fixture->valid_base_data, base_data, sizeof(base_data));
54 
55 	fixture->valid_base_ad.type = 0x16; /* service data */
56 	fixture->valid_base_ad.data_len = sizeof(base_data);
57 	fixture->valid_base_ad.data = fixture->valid_base_data;
58 
59 	/* Modify the CC length to generate an invalid BASE for invalid BASE tests */
60 	base_data[12] = 0xaa; /* Set invalid CC length*/
61 	fixture->invalid_base_data = malloc(sizeof(base_data));
62 	zassert_not_null(fixture->invalid_base_data);
63 	memcpy(fixture->invalid_base_data, base_data, sizeof(base_data));
64 
65 	fixture->invalid_base_ad.type = 0x16; /* service data */
66 	fixture->invalid_base_ad.data_len = sizeof(base_data);
67 	fixture->invalid_base_ad.data = fixture->invalid_base_data;
68 }
69 
bap_base_test_suite_setup(void)70 static void *bap_base_test_suite_setup(void)
71 {
72 	struct bap_base_test_suite_fixture *fixture;
73 
74 	fixture = malloc(sizeof(*fixture));
75 	zassert_not_null(fixture);
76 
77 	return fixture;
78 }
79 
bap_base_test_suite_before(void * f)80 static void bap_base_test_suite_before(void *f)
81 {
82 	memset(f, 0, sizeof(struct bap_base_test_suite_fixture));
83 	bap_base_test_suite_fixture_init(f);
84 }
85 
bap_base_test_suite_after(void * f)86 static void bap_base_test_suite_after(void *f)
87 {
88 	struct bap_base_test_suite_fixture *fixture = f;
89 
90 	free(fixture->valid_base_data);
91 }
92 
bap_base_test_suite_teardown(void * f)93 static void bap_base_test_suite_teardown(void *f)
94 {
95 	free(f);
96 }
97 
98 ZTEST_SUITE(bap_base_test_suite, NULL, bap_base_test_suite_setup, bap_base_test_suite_before,
99 	    bap_base_test_suite_after, bap_base_test_suite_teardown);
100 
ZTEST_F(bap_base_test_suite,test_base_get_base_from_ad)101 ZTEST_F(bap_base_test_suite, test_base_get_base_from_ad)
102 {
103 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
104 
105 	zassert_not_null(base);
106 }
107 
ZTEST_F(bap_base_test_suite,test_base_get_base_from_ad_inval_base)108 ZTEST_F(bap_base_test_suite, test_base_get_base_from_ad_inval_base)
109 {
110 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->invalid_base_ad);
111 
112 	zassert_is_null(base);
113 }
114 
ZTEST_F(bap_base_test_suite,test_base_get_base_from_ad_inval_param_null)115 ZTEST_F(bap_base_test_suite, test_base_get_base_from_ad_inval_param_null)
116 {
117 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(NULL);
118 
119 	zassert_is_null(base);
120 }
121 
ZTEST_F(bap_base_test_suite,test_base_get_base_from_ad_inval_param_type)122 ZTEST_F(bap_base_test_suite, test_base_get_base_from_ad_inval_param_type)
123 {
124 	const struct bt_bap_base *base;
125 
126 	fixture->valid_base_ad.type = 0x03; /* BT_DATA_UUID16_ALL */
127 
128 	base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
129 
130 	zassert_is_null(base);
131 }
132 
ZTEST_F(bap_base_test_suite,test_base_get_base_from_ad_inval_param_len)133 ZTEST_F(bap_base_test_suite, test_base_get_base_from_ad_inval_param_len)
134 {
135 	const struct bt_bap_base *base;
136 
137 	fixture->valid_base_ad.data_len = 0x03; /* Minimum len is BASE_MIN_SIZE (16) */
138 
139 	base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
140 
141 	zassert_is_null(base);
142 }
143 
ZTEST_F(bap_base_test_suite,test_base_get_base_from_ad_inval_param_uuid)144 ZTEST_F(bap_base_test_suite, test_base_get_base_from_ad_inval_param_uuid)
145 {
146 	const struct bt_bap_base *base;
147 
148 	/* Modify the BASE data to have invalid UUID */
149 	fixture->valid_base_data[0] = 0x01;
150 	fixture->valid_base_data[1] = 0x02;
151 
152 	base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
153 
154 	zassert_is_null(base);
155 }
156 
ZTEST_F(bap_base_test_suite,test_base_get_size)157 ZTEST_F(bap_base_test_suite, test_base_get_size)
158 {
159 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
160 	int ret;
161 
162 	zassert_not_null(base);
163 
164 	ret = bt_bap_base_get_size(base);
165 	zassert_equal(ret, 70, "Unexpected BASE size: %d", ret);
166 }
167 
ZTEST_F(bap_base_test_suite,test_base_get_size_inval_param_null)168 ZTEST_F(bap_base_test_suite, test_base_get_size_inval_param_null)
169 {
170 	int ret;
171 
172 	ret = bt_bap_base_get_size(NULL);
173 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
174 }
175 
ZTEST_F(bap_base_test_suite,test_base_get_pres_delay)176 ZTEST_F(bap_base_test_suite, test_base_get_pres_delay)
177 {
178 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
179 	int ret;
180 
181 	zassert_not_null(base);
182 
183 	ret = bt_bap_base_get_pres_delay(base);
184 	zassert_equal(ret, 40000, "Unexpected presentation delay: %d", ret);
185 }
186 
ZTEST_F(bap_base_test_suite,test_base_get_pres_delay_inval_param_null)187 ZTEST_F(bap_base_test_suite, test_base_get_pres_delay_inval_param_null)
188 {
189 	int ret;
190 
191 	ret = bt_bap_base_get_pres_delay(NULL);
192 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
193 }
194 
ZTEST_F(bap_base_test_suite,test_base_get_subgroup_count)195 ZTEST_F(bap_base_test_suite, test_base_get_subgroup_count)
196 {
197 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
198 	int ret;
199 
200 	zassert_not_null(base);
201 
202 	ret = bt_bap_base_get_subgroup_count(base);
203 	zassert_equal(ret, 2, "Unexpected presentation delay: %d", ret);
204 }
205 
ZTEST_F(bap_base_test_suite,test_base_get_subgroup_count_inval_param_null)206 ZTEST_F(bap_base_test_suite, test_base_get_subgroup_count_inval_param_null)
207 {
208 	int ret;
209 
210 	ret = bt_bap_base_get_subgroup_count(NULL);
211 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
212 }
213 
ZTEST_F(bap_base_test_suite,test_base_get_bis_indexes)214 ZTEST_F(bap_base_test_suite, test_base_get_bis_indexes)
215 {
216 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
217 	uint32_t bis_indexes;
218 	int ret;
219 
220 	zassert_not_null(base);
221 
222 	ret = bt_bap_base_get_bis_indexes(base, &bis_indexes);
223 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
224 	zassert_equal(bis_indexes, 0x00000003 /* Bit 1 and 2 */,
225 		      "Unexpected BIS index value: 0x%08X", bis_indexes);
226 }
227 
ZTEST_F(bap_base_test_suite,test_base_get_bis_indexes_inval_param_null_base)228 ZTEST_F(bap_base_test_suite, test_base_get_bis_indexes_inval_param_null_base)
229 {
230 	uint32_t bis_indexes;
231 	int ret;
232 
233 	ret = bt_bap_base_get_bis_indexes(NULL, &bis_indexes);
234 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
235 }
236 
ZTEST_F(bap_base_test_suite,test_base_get_bis_indexes_inval_param_null_index)237 ZTEST_F(bap_base_test_suite, test_base_get_bis_indexes_inval_param_null_index)
238 {
239 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
240 	int ret;
241 
242 	zassert_not_null(base);
243 
244 	ret = bt_bap_base_get_bis_indexes(base, NULL);
245 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
246 }
247 
test_base_foreach_subgroup_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)248 static bool test_base_foreach_subgroup_cb(const struct bt_bap_base_subgroup *subgroup,
249 					  void *user_data)
250 {
251 	size_t *count = user_data;
252 
253 	(*count)++;
254 
255 	return true;
256 }
257 
ZTEST_F(bap_base_test_suite,test_base_foreach_subgroup)258 ZTEST_F(bap_base_test_suite, test_base_foreach_subgroup)
259 {
260 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
261 	size_t count = 0U;
262 	int ret;
263 
264 	zassert_not_null(base);
265 
266 	ret = bt_bap_base_foreach_subgroup(base, test_base_foreach_subgroup_cb, &count);
267 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
268 	zassert_equal(count, 0x02, "Unexpected subgroup count value: %u", count);
269 }
270 
ZTEST_F(bap_base_test_suite,test_base_foreach_subgroup_inval_param_null_base)271 ZTEST_F(bap_base_test_suite, test_base_foreach_subgroup_inval_param_null_base)
272 {
273 	size_t count;
274 	int ret;
275 
276 	ret = bt_bap_base_foreach_subgroup(NULL, test_base_foreach_subgroup_cb, &count);
277 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
278 }
279 
ZTEST_F(bap_base_test_suite,test_base_foreach_subgroup_inval_param_null_cb)280 ZTEST_F(bap_base_test_suite, test_base_foreach_subgroup_inval_param_null_cb)
281 {
282 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
283 	int ret;
284 
285 	zassert_not_null(base);
286 
287 	ret = bt_bap_base_foreach_subgroup(base, NULL, NULL);
288 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
289 }
290 
test_base_get_subgroup_codec_id_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)291 static bool test_base_get_subgroup_codec_id_cb(const struct bt_bap_base_subgroup *subgroup,
292 					       void *user_data)
293 {
294 	struct bt_bap_base_codec_id codec_id;
295 	int ret;
296 
297 	ret = bt_bap_base_get_subgroup_codec_id(subgroup, &codec_id);
298 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
299 	zassert_equal(codec_id.id, 0x06, "Unexpected codec.id value: %u", codec_id.id);
300 	zassert_equal(codec_id.cid, 0x0000, "Unexpected codec.cid value: %u", codec_id.cid);
301 	zassert_equal(codec_id.vid, 0x0000, "Unexpected codec.vid value: %u", codec_id.vid);
302 
303 	return true;
304 }
305 
ZTEST_F(bap_base_test_suite,test_base_get_subgroup_codec_id)306 ZTEST_F(bap_base_test_suite, test_base_get_subgroup_codec_id)
307 {
308 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
309 	int ret;
310 
311 	zassert_not_null(base);
312 
313 	ret = bt_bap_base_foreach_subgroup(base, test_base_get_subgroup_codec_id_cb, NULL);
314 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
315 }
316 
test_base_get_subgroup_codec_id_inval_param_null_subgroup_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)317 static bool test_base_get_subgroup_codec_id_inval_param_null_subgroup_cb(
318 	const struct bt_bap_base_subgroup *subgroup, void *user_data)
319 {
320 	struct bt_bap_base_codec_id codec_id;
321 	int ret;
322 
323 	ret = bt_bap_base_get_subgroup_codec_id(NULL, &codec_id);
324 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
325 
326 	return true;
327 }
328 
ZTEST_F(bap_base_test_suite,test_base_get_subgroup_codec_id_inval_param_null_subgroup)329 ZTEST_F(bap_base_test_suite, test_base_get_subgroup_codec_id_inval_param_null_subgroup)
330 {
331 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
332 	int ret;
333 
334 	zassert_not_null(base);
335 
336 	ret = bt_bap_base_foreach_subgroup(
337 		base, test_base_get_subgroup_codec_id_inval_param_null_subgroup_cb, NULL);
338 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
339 }
340 
341 static bool
test_base_get_subgroup_codec_id_inval_param_null_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)342 test_base_get_subgroup_codec_id_inval_param_null_cb(const struct bt_bap_base_subgroup *subgroup,
343 						    void *user_data)
344 {
345 	int ret;
346 
347 	ret = bt_bap_base_get_subgroup_codec_id(subgroup, NULL);
348 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
349 
350 	return true;
351 }
352 
ZTEST_F(bap_base_test_suite,test_base_get_subgroup_codec_id_inval_param_null)353 ZTEST_F(bap_base_test_suite, test_base_get_subgroup_codec_id_inval_param_null)
354 {
355 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
356 	int ret;
357 
358 	zassert_not_null(base);
359 
360 	ret = bt_bap_base_foreach_subgroup(
361 		base, test_base_get_subgroup_codec_id_inval_param_null_cb, NULL);
362 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
363 }
364 
test_base_get_subgroup_codec_data_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)365 static bool test_base_get_subgroup_codec_data_cb(const struct bt_bap_base_subgroup *subgroup,
366 						 void *user_data)
367 {
368 	const uint8_t expected_data[] = {
369 		0x02, 0x01, 0x03, 0x02, 0x02, 0x01, 0x05, 0x03,
370 		0x01, 0x00, 0x00, 0x00, 0x03, 0x04, 0x28, 0x00,
371 	};
372 	uint8_t *data;
373 	int ret;
374 
375 	ret = bt_bap_base_get_subgroup_codec_data(subgroup, &data);
376 	zassert_equal(ret, sizeof(expected_data), "Unexpected return value: %d", ret);
377 	zassert_mem_equal(data, expected_data, sizeof(expected_data));
378 
379 	return true;
380 }
381 
ZTEST_F(bap_base_test_suite,test_base_get_subgroup_codec_data)382 ZTEST_F(bap_base_test_suite, test_base_get_subgroup_codec_data)
383 {
384 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
385 	int ret;
386 
387 	zassert_not_null(base);
388 
389 	ret = bt_bap_base_foreach_subgroup(base, test_base_get_subgroup_codec_data_cb, NULL);
390 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
391 }
392 
test_base_get_subgroup_codec_data_inval_param_null_subgroup_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)393 static bool test_base_get_subgroup_codec_data_inval_param_null_subgroup_cb(
394 	const struct bt_bap_base_subgroup *subgroup, void *user_data)
395 {
396 	uint8_t *data;
397 	int ret;
398 
399 	ret = bt_bap_base_get_subgroup_codec_data(NULL, &data);
400 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
401 
402 	return true;
403 }
404 
ZTEST_F(bap_base_test_suite,test_base_get_subgroup_codec_data_inval_param_null_subgroup)405 ZTEST_F(bap_base_test_suite, test_base_get_subgroup_codec_data_inval_param_null_subgroup)
406 {
407 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
408 	int ret;
409 
410 	zassert_not_null(base);
411 
412 	ret = bt_bap_base_foreach_subgroup(
413 		base, test_base_get_subgroup_codec_data_inval_param_null_subgroup_cb, NULL);
414 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
415 }
416 
417 static bool
test_base_get_subgroup_codec_data_inval_param_null_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)418 test_base_get_subgroup_codec_data_inval_param_null_cb(const struct bt_bap_base_subgroup *subgroup,
419 						      void *user_data)
420 {
421 	int ret;
422 
423 	ret = bt_bap_base_get_subgroup_codec_data(subgroup, NULL);
424 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
425 
426 	return true;
427 }
428 
ZTEST_F(bap_base_test_suite,test_base_get_subgroup_codec_data_inval_param_null)429 ZTEST_F(bap_base_test_suite, test_base_get_subgroup_codec_data_inval_param_null)
430 {
431 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
432 	int ret;
433 
434 	zassert_not_null(base);
435 
436 	ret = bt_bap_base_foreach_subgroup(
437 		base, test_base_get_subgroup_codec_data_inval_param_null_cb, NULL);
438 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
439 }
440 
test_base_get_subgroup_codec_meta_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)441 static bool test_base_get_subgroup_codec_meta_cb(const struct bt_bap_base_subgroup *subgroup,
442 						 void *user_data)
443 {
444 	const uint8_t expected_data[] = {0x03, 0x02, 0x01, 0x00};
445 	uint8_t *data;
446 	int ret;
447 
448 	ret = bt_bap_base_get_subgroup_codec_meta(subgroup, &data);
449 	zassert_equal(ret, sizeof(expected_data), "Unexpected return value: %d", ret);
450 	zassert_mem_equal(data, expected_data, sizeof(expected_data));
451 
452 	return true;
453 }
454 
ZTEST_F(bap_base_test_suite,test_base_get_subgroup_codec_meta)455 ZTEST_F(bap_base_test_suite, test_base_get_subgroup_codec_meta)
456 {
457 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
458 	int ret;
459 
460 	zassert_not_null(base);
461 
462 	ret = bt_bap_base_foreach_subgroup(base, test_base_get_subgroup_codec_meta_cb, NULL);
463 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
464 }
465 
test_base_get_subgroup_codec_meta_inval_param_null_subgroup_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)466 static bool test_base_get_subgroup_codec_meta_inval_param_null_subgroup_cb(
467 	const struct bt_bap_base_subgroup *subgroup, void *user_data)
468 {
469 	uint8_t *data;
470 	int ret;
471 
472 	ret = bt_bap_base_get_subgroup_codec_meta(NULL, &data);
473 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
474 
475 	return true;
476 }
477 
ZTEST_F(bap_base_test_suite,test_base_get_subgroup_codec_meta_inval_param_null_subgroup)478 ZTEST_F(bap_base_test_suite, test_base_get_subgroup_codec_meta_inval_param_null_subgroup)
479 {
480 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
481 	int ret;
482 
483 	zassert_not_null(base);
484 
485 	ret = bt_bap_base_foreach_subgroup(
486 		base, test_base_get_subgroup_codec_meta_inval_param_null_subgroup_cb, NULL);
487 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
488 }
489 
490 static bool
test_base_get_subgroup_codec_meta_inval_param_null_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)491 test_base_get_subgroup_codec_meta_inval_param_null_cb(const struct bt_bap_base_subgroup *subgroup,
492 						      void *user_data)
493 {
494 	int ret;
495 
496 	ret = bt_bap_base_get_subgroup_codec_meta(subgroup, NULL);
497 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
498 
499 	return true;
500 }
501 
ZTEST_F(bap_base_test_suite,test_base_get_subgroup_codec_meta_inval_param_null)502 ZTEST_F(bap_base_test_suite, test_base_get_subgroup_codec_meta_inval_param_null)
503 {
504 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
505 	int ret;
506 
507 	zassert_not_null(base);
508 
509 	ret = bt_bap_base_foreach_subgroup(
510 		base, test_base_get_subgroup_codec_meta_inval_param_null_cb, NULL);
511 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
512 }
513 
test_base_subgroup_codec_to_codec_cfg_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)514 static bool test_base_subgroup_codec_to_codec_cfg_cb(const struct bt_bap_base_subgroup *subgroup,
515 						     void *user_data)
516 {
517 	const uint8_t expected_meta[] = {0x03, 0x02, 0x01, 0x00};
518 	const uint8_t expected_data[] = {
519 		0x02, 0x01, 0x03, 0x02, 0x02, 0x01, 0x05, 0x03,
520 		0x01, 0x00, 0x00, 0x00, 0x03, 0x04, 0x28, 0x00,
521 	};
522 	struct bt_audio_codec_cfg codec_cfg;
523 	int ret;
524 
525 	ret = bt_bap_base_subgroup_codec_to_codec_cfg(subgroup, &codec_cfg);
526 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
527 	zassert_equal(codec_cfg.data_len, sizeof(expected_data), "Unexpected data length: %d", ret);
528 	zassert_equal(codec_cfg.meta_len, sizeof(expected_meta), "Unexpected meta length: %d", ret);
529 	zassert_mem_equal(codec_cfg.data, expected_data, sizeof(expected_data));
530 	zassert_mem_equal(codec_cfg.meta, expected_meta, sizeof(expected_meta));
531 
532 	return true;
533 }
534 
ZTEST_F(bap_base_test_suite,test_base_subgroup_codec_to_codec_cfg)535 ZTEST_F(bap_base_test_suite, test_base_subgroup_codec_to_codec_cfg)
536 {
537 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
538 	int ret;
539 
540 	zassert_not_null(base);
541 
542 	ret = bt_bap_base_foreach_subgroup(base, test_base_subgroup_codec_to_codec_cfg_cb, NULL);
543 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
544 }
545 
test_base_subgroup_codec_to_codec_cfg_inval_param_null_subgroup_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)546 static bool test_base_subgroup_codec_to_codec_cfg_inval_param_null_subgroup_cb(
547 	const struct bt_bap_base_subgroup *subgroup, void *user_data)
548 {
549 	struct bt_audio_codec_cfg codec_cfg;
550 	int ret;
551 
552 	ret = bt_bap_base_subgroup_codec_to_codec_cfg(NULL, &codec_cfg);
553 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
554 
555 	return true;
556 }
557 
ZTEST_F(bap_base_test_suite,test_base_subgroup_codec_to_codec_cfg_inval_param_null_subgroup)558 ZTEST_F(bap_base_test_suite, test_base_subgroup_codec_to_codec_cfg_inval_param_null_subgroup)
559 {
560 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
561 	int ret;
562 
563 	zassert_not_null(base);
564 
565 	ret = bt_bap_base_foreach_subgroup(
566 		base, test_base_subgroup_codec_to_codec_cfg_inval_param_null_subgroup_cb, NULL);
567 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
568 }
569 
test_base_subgroup_codec_to_codec_cfg_inval_param_null_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)570 static bool test_base_subgroup_codec_to_codec_cfg_inval_param_null_cb(
571 	const struct bt_bap_base_subgroup *subgroup, void *user_data)
572 {
573 	int ret;
574 
575 	ret = bt_bap_base_subgroup_codec_to_codec_cfg(subgroup, NULL);
576 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
577 
578 	return true;
579 }
580 
ZTEST_F(bap_base_test_suite,test_base_subgroup_codec_to_codec_cfg_inval_param_null)581 ZTEST_F(bap_base_test_suite, test_base_subgroup_codec_to_codec_cfg_inval_param_null)
582 {
583 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
584 	int ret;
585 
586 	zassert_not_null(base);
587 
588 	ret = bt_bap_base_foreach_subgroup(
589 		base, test_base_subgroup_codec_to_codec_cfg_inval_param_null_cb, NULL);
590 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
591 }
592 
test_base_get_subgroup_bis_count_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)593 static bool test_base_get_subgroup_bis_count_cb(const struct bt_bap_base_subgroup *subgroup,
594 						void *user_data)
595 {
596 	int ret;
597 
598 	ret = bt_bap_base_get_subgroup_bis_count(subgroup);
599 	zassert_equal(ret, 0x01, "Unexpected return value: %d", ret);
600 
601 	return true;
602 }
603 
ZTEST_F(bap_base_test_suite,test_base_get_subgroup_bis_count)604 ZTEST_F(bap_base_test_suite, test_base_get_subgroup_bis_count)
605 {
606 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
607 	int ret;
608 
609 	zassert_not_null(base);
610 
611 	ret = bt_bap_base_foreach_subgroup(base, test_base_get_subgroup_bis_count_cb, NULL);
612 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
613 }
614 
test_base_get_subgroup_bis_count_inval_param_null_subgroup_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)615 static bool test_base_get_subgroup_bis_count_inval_param_null_subgroup_cb(
616 	const struct bt_bap_base_subgroup *subgroup, void *user_data)
617 {
618 	int ret;
619 
620 	ret = bt_bap_base_get_subgroup_bis_count(NULL);
621 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
622 
623 	return true;
624 }
625 
ZTEST_F(bap_base_test_suite,test_base_get_subgroup_bis_count_inval_param_null_subgroup)626 ZTEST_F(bap_base_test_suite, test_base_get_subgroup_bis_count_inval_param_null_subgroup)
627 {
628 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
629 	int ret;
630 
631 	zassert_not_null(base);
632 
633 	ret = bt_bap_base_foreach_subgroup(
634 		base, test_base_get_subgroup_bis_count_inval_param_null_subgroup_cb, NULL);
635 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
636 }
637 
638 static bool
test_bt_bap_base_subgroup_get_bis_indexes_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)639 test_bt_bap_base_subgroup_get_bis_indexes_cb(const struct bt_bap_base_subgroup *subgroup,
640 					     void *user_data)
641 {
642 	uint32_t bis_indexes;
643 	int ret;
644 
645 	ret = bt_bap_base_subgroup_get_bis_indexes(subgroup, &bis_indexes);
646 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
647 	zassert_not_equal(bis_indexes, 0 /* May be Bit 1 or 2 */,
648 			  "Unexpected BIS index value: 0x%08X", bis_indexes);
649 
650 	return true;
651 }
652 
ZTEST_F(bap_base_test_suite,test_bt_bap_base_subgroup_get_bis_indexes)653 ZTEST_F(bap_base_test_suite, test_bt_bap_base_subgroup_get_bis_indexes)
654 {
655 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
656 	int ret;
657 
658 	zassert_not_null(base);
659 
660 	ret = bt_bap_base_foreach_subgroup(base, test_bt_bap_base_subgroup_get_bis_indexes_cb,
661 					   NULL);
662 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
663 }
664 
test_bt_bap_base_subgroup_get_bis_indexes_inval_param_null_subgroup_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)665 static bool test_bt_bap_base_subgroup_get_bis_indexes_inval_param_null_subgroup_cb(
666 	const struct bt_bap_base_subgroup *subgroup, void *user_data)
667 {
668 	uint32_t bis_indexes;
669 	int ret;
670 
671 	ret = bt_bap_base_subgroup_get_bis_indexes(NULL, &bis_indexes);
672 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
673 
674 	return true;
675 }
676 
ZTEST_F(bap_base_test_suite,test_bt_bap_base_subgroup_get_bis_indexes_inval_param_null_subgroup)677 ZTEST_F(bap_base_test_suite, test_bt_bap_base_subgroup_get_bis_indexes_inval_param_null_subgroup)
678 {
679 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
680 	int ret;
681 
682 	zassert_not_null(base);
683 
684 	ret = bt_bap_base_foreach_subgroup(
685 		base, test_bt_bap_base_subgroup_get_bis_indexes_inval_param_null_subgroup_cb, NULL);
686 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
687 }
688 
test_bt_bap_base_subgroup_get_bis_indexes_inval_param_null_index_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)689 static bool test_bt_bap_base_subgroup_get_bis_indexes_inval_param_null_index_cb(
690 	const struct bt_bap_base_subgroup *subgroup, void *user_data)
691 {
692 	int ret;
693 
694 	ret = bt_bap_base_subgroup_get_bis_indexes(subgroup, NULL);
695 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
696 
697 	return true;
698 }
699 
ZTEST_F(bap_base_test_suite,test_bt_bap_base_subgroup_get_bis_indexes_inval_param_null_index)700 ZTEST_F(bap_base_test_suite, test_bt_bap_base_subgroup_get_bis_indexes_inval_param_null_index)
701 {
702 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
703 	int ret;
704 
705 	zassert_not_null(base);
706 
707 	ret = bt_bap_base_foreach_subgroup(
708 		base, test_bt_bap_base_subgroup_get_bis_indexes_inval_param_null_index_cb, NULL);
709 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
710 }
711 
712 static bool
test_base_subgroup_foreach_bis_subgroup_bis_cb(const struct bt_bap_base_subgroup_bis * bis,void * user_data)713 test_base_subgroup_foreach_bis_subgroup_bis_cb(const struct bt_bap_base_subgroup_bis *bis,
714 					       void *user_data)
715 {
716 	size_t *count = user_data;
717 
718 	(*count)++;
719 
720 	return true;
721 }
722 
test_base_subgroup_foreach_bis_subgroup_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)723 static bool test_base_subgroup_foreach_bis_subgroup_cb(const struct bt_bap_base_subgroup *subgroup,
724 						       void *user_data)
725 {
726 	size_t *total_count = user_data;
727 	size_t count = 0U;
728 	int ret;
729 
730 	ret = bt_bap_base_subgroup_foreach_bis(
731 		subgroup, test_base_subgroup_foreach_bis_subgroup_bis_cb, &count);
732 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
733 	zassert_equal(count, 0x01, "Unexpected subgroup count value: %u", count);
734 
735 	*total_count += count;
736 
737 	return true;
738 }
739 
ZTEST_F(bap_base_test_suite,test_base_subgroup_foreach_bis)740 ZTEST_F(bap_base_test_suite, test_base_subgroup_foreach_bis)
741 {
742 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
743 	size_t count = 0U;
744 	int ret;
745 
746 	zassert_not_null(base);
747 
748 	ret = bt_bap_base_foreach_subgroup(base, test_base_subgroup_foreach_bis_subgroup_cb,
749 					   &count);
750 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
751 	zassert_equal(count, 0x02, "Unexpected subgroup count value: %u", count);
752 }
753 
test_base_subgroup_foreach_bis_inval_param_null_subgroup_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)754 static bool test_base_subgroup_foreach_bis_inval_param_null_subgroup_cb(
755 	const struct bt_bap_base_subgroup *subgroup, void *user_data)
756 {
757 	size_t count;
758 	int ret;
759 
760 	ret = bt_bap_base_subgroup_foreach_bis(NULL, test_base_subgroup_foreach_bis_subgroup_bis_cb,
761 					       &count);
762 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
763 
764 	return true;
765 }
766 
ZTEST_F(bap_base_test_suite,test_base_subgroup_foreach_bis_inval_param_null_subgroup)767 ZTEST_F(bap_base_test_suite, test_base_subgroup_foreach_bis_inval_param_null_subgroup)
768 {
769 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
770 	int ret;
771 
772 	zassert_not_null(base);
773 
774 	ret = bt_bap_base_foreach_subgroup(
775 		base, test_base_subgroup_foreach_bis_inval_param_null_subgroup_cb, NULL);
776 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
777 }
778 
779 static bool
test_base_subgroup_foreach_bis_inval_param_null_cb_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)780 test_base_subgroup_foreach_bis_inval_param_null_cb_cb(const struct bt_bap_base_subgroup *subgroup,
781 						      void *user_data)
782 {
783 	int ret;
784 
785 	ret = bt_bap_base_subgroup_foreach_bis(subgroup, NULL, NULL);
786 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
787 
788 	return true;
789 }
790 
ZTEST_F(bap_base_test_suite,test_base_subgroup_foreach_bis_inval_param_null_cb)791 ZTEST_F(bap_base_test_suite, test_base_subgroup_foreach_bis_inval_param_null_cb)
792 {
793 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
794 	int ret;
795 
796 	zassert_not_null(base);
797 
798 	ret = bt_bap_base_foreach_subgroup(
799 		base, test_base_subgroup_foreach_bis_inval_param_null_cb_cb, NULL);
800 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
801 }
802 
803 static bool
test_base_subgroup_bis_codec_to_codec_cfg_bis_cb(const struct bt_bap_base_subgroup_bis * bis,void * user_data)804 test_base_subgroup_bis_codec_to_codec_cfg_bis_cb(const struct bt_bap_base_subgroup_bis *bis,
805 						 void *user_data)
806 {
807 	const uint8_t expected_data[] = {0x02, 0x03, 0x03};
808 	struct bt_audio_codec_cfg codec_cfg;
809 	int ret;
810 
811 	ret = bt_bap_base_subgroup_bis_codec_to_codec_cfg(bis, &codec_cfg);
812 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
813 	zassert_equal(codec_cfg.data_len, sizeof(expected_data), "Unexpected data length: %d", ret);
814 	zassert_mem_equal(codec_cfg.data, expected_data, sizeof(expected_data));
815 
816 	return true;
817 }
818 
819 static bool
test_base_subgroup_bis_codec_to_codec_cfg_subgroup_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)820 test_base_subgroup_bis_codec_to_codec_cfg_subgroup_cb(const struct bt_bap_base_subgroup *subgroup,
821 						      void *user_data)
822 {
823 	int ret;
824 
825 	ret = bt_bap_base_subgroup_foreach_bis(
826 		subgroup, test_base_subgroup_bis_codec_to_codec_cfg_bis_cb, NULL);
827 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
828 
829 	return true;
830 }
831 
ZTEST_F(bap_base_test_suite,test_base_subgroup_bis_codec_to_codec_cfg)832 ZTEST_F(bap_base_test_suite, test_base_subgroup_bis_codec_to_codec_cfg)
833 {
834 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
835 	int ret;
836 
837 	zassert_not_null(base);
838 
839 	ret = bt_bap_base_foreach_subgroup(
840 		base, test_base_subgroup_bis_codec_to_codec_cfg_subgroup_cb, NULL);
841 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
842 }
843 
test_base_subgroup_bis_codec_to_codec_cfg_inval_param_null_bis_bis_cb(const struct bt_bap_base_subgroup_bis * bis,void * user_data)844 static bool test_base_subgroup_bis_codec_to_codec_cfg_inval_param_null_bis_bis_cb(
845 	const struct bt_bap_base_subgroup_bis *bis, void *user_data)
846 {
847 	struct bt_audio_codec_cfg codec_cfg;
848 	int ret;
849 
850 	ret = bt_bap_base_subgroup_bis_codec_to_codec_cfg(NULL, &codec_cfg);
851 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
852 
853 	return true;
854 }
855 
test_base_subgroup_bis_codec_to_codec_cfg_inval_param_null_bis_subgroup_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)856 static bool test_base_subgroup_bis_codec_to_codec_cfg_inval_param_null_bis_subgroup_cb(
857 	const struct bt_bap_base_subgroup *subgroup, void *user_data)
858 {
859 	int ret;
860 
861 	ret = bt_bap_base_subgroup_foreach_bis(
862 		NULL, test_base_subgroup_bis_codec_to_codec_cfg_inval_param_null_bis_bis_cb, NULL);
863 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
864 
865 	return true;
866 }
867 
ZTEST_F(bap_base_test_suite,test_base_subgroup_foreach_bis_inval_param_null_bis)868 ZTEST_F(bap_base_test_suite, test_base_subgroup_foreach_bis_inval_param_null_bis)
869 {
870 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
871 	int ret;
872 
873 	zassert_not_null(base);
874 
875 	ret = bt_bap_base_foreach_subgroup(
876 		base, test_base_subgroup_bis_codec_to_codec_cfg_inval_param_null_bis_subgroup_cb,
877 		NULL);
878 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
879 }
880 
test_base_subgroup_bis_codec_to_codec_cfg_inval_param_null_codec_cfg_bis_cb(const struct bt_bap_base_subgroup_bis * bis,void * user_data)881 static bool test_base_subgroup_bis_codec_to_codec_cfg_inval_param_null_codec_cfg_bis_cb(
882 	const struct bt_bap_base_subgroup_bis *bis, void *user_data)
883 {
884 	int ret;
885 
886 	ret = bt_bap_base_subgroup_bis_codec_to_codec_cfg(bis, NULL);
887 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
888 
889 	return true;
890 }
891 
test_base_subgroup_bis_codec_to_codec_cfg_inval_param_null_codec_cfg_subgroup_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)892 static bool test_base_subgroup_bis_codec_to_codec_cfg_inval_param_null_codec_cfg_subgroup_cb(
893 	const struct bt_bap_base_subgroup *subgroup, void *user_data)
894 {
895 	int ret;
896 
897 	ret = bt_bap_base_subgroup_foreach_bis(
898 		NULL, test_base_subgroup_bis_codec_to_codec_cfg_inval_param_null_codec_cfg_bis_cb,
899 		NULL);
900 	zassert_equal(ret, -EINVAL, "Unexpected return value: %d", ret);
901 
902 	return true;
903 }
904 
ZTEST_F(bap_base_test_suite,test_base_subgroup_foreach_bis_inval_param_null_codec_cfg)905 ZTEST_F(bap_base_test_suite, test_base_subgroup_foreach_bis_inval_param_null_codec_cfg)
906 {
907 	const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(&fixture->valid_base_ad);
908 	int ret;
909 
910 	zassert_not_null(base);
911 
912 	ret = bt_bap_base_foreach_subgroup(
913 		base,
914 		test_base_subgroup_bis_codec_to_codec_cfg_inval_param_null_codec_cfg_subgroup_cb,
915 		NULL);
916 	zassert_equal(ret, 0, "Unexpected return value: %d", ret);
917 }
918