Skip to content

Commit 0201fdb

Browse files
committed
Improve test coverage for A2DP related functions
1 parent c0378e5 commit 0201fdb

File tree

1 file changed

+82
-32
lines changed

1 file changed

+82
-32
lines changed

test/test-a2dp.c

+82-32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* test-a2dp.c
3-
* Copyright (c) 2016-2023 Arkadiusz Bokowy
3+
* Copyright (c) 2016-2024 Arkadiusz Bokowy
44
*
55
* This file is a part of bluez-alsa.
66
*
@@ -23,6 +23,7 @@
2323

2424
#include "a2dp.h"
2525
#include "a2dp-aac.h"
26+
#include "a2dp-aptx.h"
2627
#include "a2dp-sbc.h"
2728
#include "ba-transport.h"
2829
#include "ba-transport-pcm.h"
@@ -78,22 +79,24 @@ CK_START_TEST(test_a2dp_codecs_init) {
7879

7980
CK_START_TEST(test_a2dp_codec_cmp) {
8081

81-
struct a2dp_codec codec1 = { .dir = A2DP_SOURCE, .codec_id = A2DP_CODEC_SBC };
82-
struct a2dp_codec codec2 = { .dir = A2DP_SOURCE, .codec_id = A2DP_CODEC_MPEG24 };
83-
struct a2dp_codec codec3 = { .dir = A2DP_SOURCE, .codec_id = A2DP_CODEC_VENDOR_APTX };
84-
struct a2dp_codec codec4 = { .dir = A2DP_SINK, .codec_id = A2DP_CODEC_SBC };
85-
struct a2dp_codec codec5 = { .dir = A2DP_SINK, .codec_id = A2DP_CODEC_VENDOR_APTX };
86-
struct a2dp_codec codec6 = { .dir = A2DP_SINK, .codec_id = 0xFFFF };
82+
struct a2dp_codec c1 = { .dir = A2DP_SOURCE, .codec_id = A2DP_CODEC_SBC };
83+
struct a2dp_codec c2 = { .dir = A2DP_SOURCE, .codec_id = A2DP_CODEC_MPEG24 };
84+
struct a2dp_codec c3 = { .dir = A2DP_SOURCE, .codec_id = A2DP_CODEC_VENDOR_APTX };
85+
struct a2dp_codec c4 = { .dir = A2DP_SINK, .codec_id = A2DP_CODEC_SBC };
86+
struct a2dp_codec c5 = { .dir = A2DP_SINK, .codec_id = A2DP_CODEC_VENDOR_APTX };
87+
struct a2dp_codec c6 = { .dir = A2DP_SINK, .codec_id = A2DP_CODEC_VENDOR_LDAC };
88+
struct a2dp_codec c7 = { .dir = A2DP_SINK, .codec_id = 0xFFFF };
8789

88-
struct a2dp_codec * codecs[] = { &codec3, &codec1, &codec6, &codec4, &codec5, &codec2 };
90+
struct a2dp_codec * codecs[] = { &c3, &c1, &c6, &c4, &c7, &c5, &c2 };
8991
qsort(codecs, ARRAYSIZE(codecs), sizeof(*codecs), QSORT_COMPAR(a2dp_codec_ptr_cmp));
9092

91-
ck_assert_ptr_eq(codecs[0], &codec1);
92-
ck_assert_ptr_eq(codecs[1], &codec2);
93-
ck_assert_ptr_eq(codecs[2], &codec3);
94-
ck_assert_ptr_eq(codecs[3], &codec4);
95-
ck_assert_ptr_eq(codecs[4], &codec5);
96-
ck_assert_ptr_eq(codecs[5], &codec6);
93+
ck_assert_ptr_eq(codecs[0], &c1);
94+
ck_assert_ptr_eq(codecs[1], &c2);
95+
ck_assert_ptr_eq(codecs[2], &c3);
96+
ck_assert_ptr_eq(codecs[3], &c4);
97+
ck_assert_ptr_eq(codecs[4], &c5);
98+
ck_assert_ptr_eq(codecs[5], &c6);
99+
ck_assert_ptr_eq(codecs[6], &c7);
97100

98101
} CK_END_TEST
99102

@@ -149,6 +152,9 @@ CK_START_TEST(test_a2dp_check_configuration) {
149152
.max_bitpool = 62,
150153
};
151154

155+
ck_assert_int_eq(a2dp_check_configuration(&a2dp_sbc_source,
156+
&cfg_valid, sizeof(cfg_valid) + 1), A2DP_CHECK_ERR_SIZE);
157+
152158
ck_assert_int_eq(a2dp_check_configuration(&a2dp_sbc_source,
153159
&cfg_valid, sizeof(cfg_valid)), A2DP_CHECK_OK);
154160

@@ -174,9 +180,14 @@ CK_START_TEST(test_a2dp_check_configuration) {
174180

175181
} CK_END_TEST
176182

183+
CK_START_TEST(test_a2dp_check_strerror) {
184+
ck_assert_str_eq(a2dp_check_strerror(A2DP_CHECK_ERR_SIZE), "Invalid size");
185+
ck_assert_str_eq(a2dp_check_strerror(0xFFFF), "Check error");
186+
} CK_END_TEST
187+
177188
CK_START_TEST(test_a2dp_filter_capabilities) {
178189

179-
a2dp_sbc_t caps = {
190+
a2dp_sbc_t caps_sbc = {
180191
.frequency = SBC_SAMPLING_FREQ_44100,
181192
.channel_mode = SBC_CHANNEL_MODE_MONO | SBC_CHANNEL_MODE_STEREO,
182193
.block_length = SBC_BLOCK_LENGTH_4 | SBC_BLOCK_LENGTH_8,
@@ -186,23 +197,42 @@ CK_START_TEST(test_a2dp_filter_capabilities) {
186197
.max_bitpool = 255,
187198
};
188199

200+
#if ENABLE_APTX
201+
a2dp_aptx_t caps_aptx = {
202+
.info = A2DP_VENDOR_INFO_INIT(APTX_VENDOR_ID, APTX_CODEC_ID),
203+
.frequency = APTX_SAMPLING_FREQ_32000 | APTX_SAMPLING_FREQ_44100,
204+
.channel_mode = APTX_CHANNEL_MODE_MONO | APTX_CHANNEL_MODE_STEREO,
205+
};
206+
#endif
207+
189208
ck_assert_int_eq(a2dp_filter_capabilities(&a2dp_sbc_source,
190-
&a2dp_sbc_source.capabilities, &caps, sizeof(caps) + 1), -1);
209+
&a2dp_sbc_source.capabilities, &caps_sbc, sizeof(caps_sbc) + 1), -1);
191210
ck_assert_int_eq(errno, EINVAL);
192211

193-
hexdump("Capabilities A", &caps, sizeof(caps), true);
194-
hexdump("Capabilities B", &a2dp_sbc_source.capabilities, sizeof(caps), true);
212+
hexdump("Capabilities A", &caps_sbc, sizeof(caps_sbc), true);
213+
hexdump("Capabilities B", &a2dp_sbc_source.capabilities, sizeof(caps_sbc), true);
195214
ck_assert_int_eq(a2dp_filter_capabilities(&a2dp_sbc_source,
196-
&a2dp_sbc_source.capabilities, &caps, sizeof(caps)), 0);
197-
198-
hexdump("filterion", &caps, sizeof(caps), true);
199-
ck_assert_int_eq(caps.frequency, SBC_SAMPLING_FREQ_44100);
200-
ck_assert_int_eq(caps.channel_mode, SBC_CHANNEL_MODE_MONO | SBC_CHANNEL_MODE_STEREO);
201-
ck_assert_int_eq(caps.block_length, SBC_BLOCK_LENGTH_4 | SBC_BLOCK_LENGTH_8);
202-
ck_assert_int_eq(caps.subbands, SBC_SUBBANDS_4);
203-
ck_assert_int_eq(caps.allocation_method, SBC_ALLOCATION_SNR);
204-
ck_assert_int_eq(caps.min_bitpool, MAX(SBC_MIN_BITPOOL, 42));
205-
ck_assert_int_eq(caps.max_bitpool, MIN(SBC_MAX_BITPOOL, 255));
215+
&a2dp_sbc_source.capabilities, &caps_sbc, sizeof(caps_sbc)), 0);
216+
217+
hexdump("Capabilities filtered", &caps_sbc, sizeof(caps_sbc), true);
218+
ck_assert_int_eq(caps_sbc.frequency, SBC_SAMPLING_FREQ_44100);
219+
ck_assert_int_eq(caps_sbc.channel_mode, SBC_CHANNEL_MODE_MONO | SBC_CHANNEL_MODE_STEREO);
220+
ck_assert_int_eq(caps_sbc.block_length, SBC_BLOCK_LENGTH_4 | SBC_BLOCK_LENGTH_8);
221+
ck_assert_int_eq(caps_sbc.subbands, SBC_SUBBANDS_4);
222+
ck_assert_int_eq(caps_sbc.allocation_method, SBC_ALLOCATION_SNR);
223+
ck_assert_int_eq(caps_sbc.min_bitpool, MAX(SBC_MIN_BITPOOL, 42));
224+
ck_assert_int_eq(caps_sbc.max_bitpool, MIN(SBC_MAX_BITPOOL, 255));
225+
226+
#if ENABLE_APTX
227+
/* Check whether generic bitwise AND filtering works correctly. */
228+
hexdump("Capabilities A", &caps_aptx, sizeof(caps_aptx), true);
229+
hexdump("Capabilities B", &a2dp_aptx_source.capabilities, sizeof(caps_aptx), true);
230+
ck_assert_int_eq(a2dp_filter_capabilities(&a2dp_aptx_source,
231+
&a2dp_aptx_source.capabilities, &caps_aptx, sizeof(caps_aptx)), 0);
232+
hexdump("Capabilities filtered", &caps_aptx, sizeof(caps_aptx), true);
233+
ck_assert_int_eq(caps_aptx.frequency, APTX_SAMPLING_FREQ_32000 | APTX_SAMPLING_FREQ_44100);
234+
ck_assert_int_eq(caps_aptx.channel_mode, APTX_CHANNEL_MODE_STEREO);
235+
#endif
206236

207237
} CK_END_TEST
208238

@@ -252,12 +282,31 @@ CK_START_TEST(test_a2dp_select_configuration) {
252282
ck_assert_int_eq(cfg.max_bitpool, 250);
253283

254284
#if ENABLE_AAC
255-
a2dp_aac_t cfg_aac = {
256-
/* FDK-AAC encoder does not support AAC Long Term Prediction */
257-
.object_type = AAC_OBJECT_TYPE_MPEG4_LTP,
285+
286+
a2dp_aac_t cfg_aac;
287+
const a2dp_aac_t cfg_aac_ = {
288+
.object_type = AAC_OBJECT_TYPE_MPEG2_LC | AAC_OBJECT_TYPE_MPEG4_LC,
258289
A2DP_AAC_INIT_FREQUENCY(AAC_SAMPLING_FREQ_44100 | AAC_SAMPLING_FREQ_96000)
259-
.channels = AAC_CHANNELS_1 };
290+
.channels = AAC_CHANNELS_1,
291+
.vbr = 1 };
292+
293+
cfg_aac = cfg_aac_;
294+
ck_assert_int_eq(a2dp_select_configuration(&a2dp_aac_source, &cfg_aac, sizeof(cfg_aac)), 0);
295+
ck_assert_int_eq(cfg_aac.object_type, AAC_OBJECT_TYPE_MPEG4_LC);
296+
ck_assert_int_eq(A2DP_AAC_GET_FREQUENCY(cfg_aac), AAC_SAMPLING_FREQ_44100);
297+
ck_assert_int_eq(cfg_aac.channels, AAC_CHANNELS_1);
298+
ck_assert_int_eq(cfg_aac.vbr, 0);
299+
300+
cfg_aac = cfg_aac_;
301+
config.aac_prefer_vbr = true;
302+
ck_assert_int_eq(a2dp_select_configuration(&a2dp_aac_source, &cfg_aac, sizeof(cfg_aac)), 0);
303+
ck_assert_int_eq(cfg_aac.vbr, 1);
304+
305+
cfg_aac = cfg_aac_;
306+
/* FDK-AAC encoder does not support AAC Long Term Prediction */
307+
cfg_aac.object_type = AAC_OBJECT_TYPE_MPEG4_LTP;
260308
ck_assert_int_eq(a2dp_select_configuration(&a2dp_aac_source, &cfg_aac, sizeof(cfg_aac)), -1);
309+
261310
#endif
262311

263312
} CK_END_TEST
@@ -281,6 +330,7 @@ int main(void) {
281330
tcase_add_test(tc, test_a2dp_codec_lookup);
282331
tcase_add_test(tc, test_a2dp_get_vendor_codec_id);
283332
tcase_add_test(tc, test_a2dp_check_configuration);
333+
tcase_add_test(tc, test_a2dp_check_strerror);
284334
tcase_add_test(tc, test_a2dp_filter_capabilities);
285335
tcase_add_test(tc, test_a2dp_select_configuration);
286336

0 commit comments

Comments
 (0)