xref: /linux/sound/core/sound_kunit.c (revision 447e140e66fd226350b3ce86cffc965eaae4c856)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Sound core KUnit test
4  * Author: Ivan Orlov <ivan.orlov0322@gmail.com>
5  */
6 
7 #include <kunit/test.h>
8 #include <sound/core.h>
9 #include <sound/pcm.h>
10 
11 #define SILENCE_BUFFER_MAX_FRAMES 260
12 #define SILENCE_BUFFER_SIZE (sizeof(u64) * SILENCE_BUFFER_MAX_FRAMES)
13 #define SILENCE(...) { __VA_ARGS__ }
14 #define DEFINE_FORMAT(fmt, pbits, wd, endianness, signd, silence_arr) {		\
15 	.format = SNDRV_PCM_FORMAT_##fmt, .physical_bits = pbits,		\
16 	.width = wd, .le = endianness, .sd = signd, .silence = silence_arr,	\
17 	.name = #fmt,								\
18 }
19 
20 #define WRONG_FORMAT_1 (__force snd_pcm_format_t)((__force int)SNDRV_PCM_FORMAT_LAST + 1)
21 #define WRONG_FORMAT_2 (__force snd_pcm_format_t)-1
22 
23 #define VALID_NAME "ValidName"
24 #define NAME_W_SPEC_CHARS "In%v@1id name"
25 #define NAME_W_SPACE "Test name"
26 #define NAME_W_SPACE_REMOVED "Testname"
27 
28 #define TEST_FIRST_COMPONENT "Component1"
29 #define TEST_SECOND_COMPONENT "Component2"
30 
31 struct snd_format_test_data {
32 	snd_pcm_format_t format;
33 	int physical_bits;
34 	int width;
35 	int le;
36 	int sd;
37 	unsigned char silence[8];
38 	unsigned char *name;
39 };
40 
41 struct avail_test_data {
42 	snd_pcm_uframes_t buffer_size;
43 	snd_pcm_uframes_t hw_ptr;
44 	snd_pcm_uframes_t appl_ptr;
45 	snd_pcm_uframes_t expected_avail;
46 };
47 
48 static const struct snd_format_test_data valid_fmt[] = {
49 	DEFINE_FORMAT(S8, 8, 8, -1, 1, SILENCE()),
50 	DEFINE_FORMAT(U8, 8, 8, -1, 0, SILENCE(0x80)),
51 	DEFINE_FORMAT(S16_LE, 16, 16, 1, 1, SILENCE()),
52 	DEFINE_FORMAT(S16_BE, 16, 16, 0, 1, SILENCE()),
53 	DEFINE_FORMAT(U16_LE, 16, 16, 1, 0, SILENCE(0x00, 0x80)),
54 	DEFINE_FORMAT(U16_BE, 16, 16, 0, 0, SILENCE(0x80, 0x00)),
55 	DEFINE_FORMAT(S24_LE, 32, 24, 1, 1, SILENCE()),
56 	DEFINE_FORMAT(S24_BE, 32, 24, 0, 1, SILENCE()),
57 	DEFINE_FORMAT(U24_LE, 32, 24, 1, 0, SILENCE(0x00, 0x00, 0x80)),
58 	DEFINE_FORMAT(U24_BE, 32, 24, 0, 0, SILENCE(0x00, 0x80, 0x00, 0x00)),
59 	DEFINE_FORMAT(S32_LE, 32, 32, 1, 1, SILENCE()),
60 	DEFINE_FORMAT(S32_BE, 32, 32, 0, 1, SILENCE()),
61 	DEFINE_FORMAT(U32_LE, 32, 32, 1, 0, SILENCE(0x00, 0x00, 0x00, 0x80)),
62 	DEFINE_FORMAT(U32_BE, 32, 32, 0, 0, SILENCE(0x80, 0x00, 0x00, 0x00)),
63 	DEFINE_FORMAT(FLOAT_LE, 32, 32, 1, -1, SILENCE()),
64 	DEFINE_FORMAT(FLOAT_BE, 32, 32, 0, -1, SILENCE()),
65 	DEFINE_FORMAT(FLOAT64_LE, 64, 64, 1, -1, SILENCE()),
66 	DEFINE_FORMAT(FLOAT64_BE, 64, 64, 0, -1, SILENCE()),
67 	DEFINE_FORMAT(IEC958_SUBFRAME_LE, 32, 32, 1, -1, SILENCE()),
68 	DEFINE_FORMAT(IEC958_SUBFRAME_BE, 32, 32, 0, -1, SILENCE()),
69 	DEFINE_FORMAT(MU_LAW, 8, 8, -1, -1, SILENCE(0x7f)),
70 	DEFINE_FORMAT(A_LAW, 8, 8, -1, -1, SILENCE(0x55)),
71 	DEFINE_FORMAT(IMA_ADPCM, 4, 4, -1, -1, SILENCE()),
72 	DEFINE_FORMAT(G723_24, 3, 3, -1, -1, SILENCE()),
73 	DEFINE_FORMAT(G723_40, 5, 5, -1, -1, SILENCE()),
74 	DEFINE_FORMAT(DSD_U8, 8, 8, 1, 0, SILENCE(0x69)),
75 	DEFINE_FORMAT(DSD_U16_LE, 16, 16, 1, 0, SILENCE(0x69, 0x69)),
76 	DEFINE_FORMAT(DSD_U32_LE, 32, 32, 1, 0, SILENCE(0x69, 0x69, 0x69, 0x69)),
77 	DEFINE_FORMAT(DSD_U16_BE, 16, 16, 0, 0, SILENCE(0x69, 0x69)),
78 	DEFINE_FORMAT(DSD_U32_BE, 32, 32, 0, 0, SILENCE(0x69, 0x69, 0x69, 0x69)),
79 	DEFINE_FORMAT(S20_LE, 32, 20, 1, 1, SILENCE()),
80 	DEFINE_FORMAT(S20_BE, 32, 20, 0, 1, SILENCE()),
81 	DEFINE_FORMAT(U20_LE, 32, 20, 1, 0, SILENCE(0x00, 0x00, 0x08, 0x00)),
82 	DEFINE_FORMAT(U20_BE, 32, 20, 0, 0, SILENCE(0x00, 0x08, 0x00, 0x00)),
83 	DEFINE_FORMAT(S24_3LE, 24, 24, 1, 1, SILENCE()),
84 	DEFINE_FORMAT(S24_3BE, 24, 24, 0, 1, SILENCE()),
85 	DEFINE_FORMAT(U24_3LE, 24, 24, 1, 0, SILENCE(0x00, 0x00, 0x80)),
86 	DEFINE_FORMAT(U24_3BE, 24, 24, 0, 0, SILENCE(0x80, 0x00, 0x00)),
87 	DEFINE_FORMAT(S20_3LE, 24, 20, 1, 1, SILENCE()),
88 	DEFINE_FORMAT(S20_3BE, 24, 20, 0, 1, SILENCE()),
89 	DEFINE_FORMAT(U20_3LE, 24, 20, 1, 0, SILENCE(0x00, 0x00, 0x08)),
90 	DEFINE_FORMAT(U20_3BE, 24, 20, 0, 0, SILENCE(0x08, 0x00, 0x00)),
91 	DEFINE_FORMAT(S18_3LE, 24, 18, 1, 1, SILENCE()),
92 	DEFINE_FORMAT(S18_3BE, 24, 18, 0, 1, SILENCE()),
93 	DEFINE_FORMAT(U18_3LE, 24, 18, 1, 0, SILENCE(0x00, 0x00, 0x02)),
94 	DEFINE_FORMAT(U18_3BE, 24, 18, 0, 0, SILENCE(0x02, 0x00, 0x00)),
95 	DEFINE_FORMAT(G723_24_1B, 8, 3, -1, -1, SILENCE()),
96 	DEFINE_FORMAT(G723_40_1B, 8, 5, -1, -1, SILENCE()),
97 };
98 
99 static void test_phys_format_size(struct kunit *test)
100 {
101 	u32 i;
102 
103 	for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) {
104 		KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(valid_fmt[i].format),
105 				valid_fmt[i].physical_bits);
106 	}
107 
108 	KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(WRONG_FORMAT_1), -EINVAL);
109 	KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(WRONG_FORMAT_2), -EINVAL);
110 }
111 
112 static void test_format_width(struct kunit *test)
113 {
114 	u32 i;
115 
116 	for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) {
117 		KUNIT_EXPECT_EQ(test, snd_pcm_format_width(valid_fmt[i].format),
118 				valid_fmt[i].width);
119 	}
120 
121 	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_1), -EINVAL);
122 	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_2), -EINVAL);
123 }
124 
125 static void test_format_signed(struct kunit *test)
126 {
127 	u32 i;
128 
129 	for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) {
130 		KUNIT_EXPECT_EQ(test, snd_pcm_format_signed(valid_fmt[i].format),
131 				valid_fmt[i].sd < 0 ? -EINVAL : valid_fmt[i].sd);
132 		KUNIT_EXPECT_EQ(test, snd_pcm_format_unsigned(valid_fmt[i].format),
133 				valid_fmt[i].sd < 0 ? -EINVAL : 1 - valid_fmt[i].sd);
134 	}
135 
136 	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_1), -EINVAL);
137 	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_2), -EINVAL);
138 }
139 
140 static void test_format_endianness(struct kunit *test)
141 {
142 	u32 i;
143 
144 	for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) {
145 		KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(valid_fmt[i].format),
146 				valid_fmt[i].le < 0 ? -EINVAL : valid_fmt[i].le);
147 		KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(valid_fmt[i].format),
148 				valid_fmt[i].le < 0 ? -EINVAL : 1 - valid_fmt[i].le);
149 	}
150 
151 	KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(WRONG_FORMAT_1), -EINVAL);
152 	KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(WRONG_FORMAT_2), -EINVAL);
153 	KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(WRONG_FORMAT_1), -EINVAL);
154 	KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(WRONG_FORMAT_2), -EINVAL);
155 }
156 
157 static void _test_fill_silence(struct kunit *test, const struct snd_format_test_data *data,
158 			       u8 *buffer, size_t samples_count)
159 {
160 	size_t sample_bytes = data->physical_bits >> 3;
161 	u32 i;
162 
163 	KUNIT_ASSERT_EQ(test, snd_pcm_format_set_silence(data->format, buffer, samples_count), 0);
164 	for (i = 0; i < samples_count * sample_bytes; i++)
165 		KUNIT_EXPECT_EQ(test, buffer[i], data->silence[i % sample_bytes]);
166 }
167 
168 static void test_format_fill_silence(struct kunit *test)
169 {
170 	static const u32 buf_samples[] = { 10, 20, 32, 64, 129, SILENCE_BUFFER_MAX_FRAMES };
171 	u8 *buffer;
172 	u32 i, j;
173 
174 	buffer = kunit_kzalloc(test, SILENCE_BUFFER_SIZE, GFP_KERNEL);
175 
176 	for (i = 0; i < ARRAY_SIZE(buf_samples); i++) {
177 		for (j = 0; j < ARRAY_SIZE(valid_fmt); j++)
178 			_test_fill_silence(test, &valid_fmt[j], buffer, buf_samples[i]);
179 	}
180 
181 	KUNIT_EXPECT_EQ(test, snd_pcm_format_set_silence(WRONG_FORMAT_1, buffer, 20), -EINVAL);
182 	KUNIT_EXPECT_EQ(test, snd_pcm_format_set_silence(SNDRV_PCM_FORMAT_LAST, buffer, 0), 0);
183 }
184 
185 static snd_pcm_uframes_t calculate_boundary(snd_pcm_uframes_t buffer_size)
186 {
187 	snd_pcm_uframes_t boundary = buffer_size;
188 
189 	while (boundary * 2 <= 0x7fffffffUL - buffer_size)
190 		boundary *= 2;
191 	return boundary;
192 }
193 
194 static const struct avail_test_data p_avail_data[] = {
195 	/* buf_size + hw_ptr < appl_ptr => avail = buf_size + hw_ptr - appl_ptr + boundary */
196 	{ 128, 1000, 1129, 1073741824UL - 1 },
197 	/*
198 	 * buf_size + hw_ptr - appl_ptr >= boundary =>
199 	 * => avail = buf_size + hw_ptr - appl_ptr - boundary
200 	 */
201 	{ 128, 1073741824UL, 10, 118 },
202 	/* standard case: avail = buf_size + hw_ptr - appl_ptr */
203 	{ 128, 1000, 1001, 127 },
204 };
205 
206 static void test_playback_avail(struct kunit *test)
207 {
208 	struct snd_pcm_runtime *r = kunit_kzalloc(test, sizeof(*r), GFP_KERNEL);
209 	u32 i;
210 
211 	r->status = kunit_kzalloc(test, sizeof(*r->status), GFP_KERNEL);
212 	r->control = kunit_kzalloc(test, sizeof(*r->control), GFP_KERNEL);
213 
214 	for (i = 0; i < ARRAY_SIZE(p_avail_data); i++) {
215 		r->buffer_size = p_avail_data[i].buffer_size;
216 		r->boundary = calculate_boundary(r->buffer_size);
217 		r->status->hw_ptr = p_avail_data[i].hw_ptr;
218 		r->control->appl_ptr = p_avail_data[i].appl_ptr;
219 		KUNIT_EXPECT_EQ(test, snd_pcm_playback_avail(r), p_avail_data[i].expected_avail);
220 	}
221 }
222 
223 static const struct avail_test_data c_avail_data[] = {
224 	/* hw_ptr - appl_ptr < 0 => avail = hw_ptr - appl_ptr + boundary */
225 	{ 128, 1000, 1001, 1073741824UL - 1 },
226 	/* standard case: avail = hw_ptr - appl_ptr */
227 	{ 128, 1001, 1000, 1 },
228 };
229 
230 static void test_capture_avail(struct kunit *test)
231 {
232 	struct snd_pcm_runtime *r = kunit_kzalloc(test, sizeof(*r), GFP_KERNEL);
233 	u32 i;
234 
235 	r->status = kunit_kzalloc(test, sizeof(*r->status), GFP_KERNEL);
236 	r->control = kunit_kzalloc(test, sizeof(*r->control), GFP_KERNEL);
237 
238 	for (i = 0; i < ARRAY_SIZE(c_avail_data); i++) {
239 		r->buffer_size = c_avail_data[i].buffer_size;
240 		r->boundary = calculate_boundary(r->buffer_size);
241 		r->status->hw_ptr = c_avail_data[i].hw_ptr;
242 		r->control->appl_ptr = c_avail_data[i].appl_ptr;
243 		KUNIT_EXPECT_EQ(test, snd_pcm_capture_avail(r), c_avail_data[i].expected_avail);
244 	}
245 }
246 
247 static void test_card_set_id(struct kunit *test)
248 {
249 	struct snd_card *card = kunit_kzalloc(test, sizeof(*card), GFP_KERNEL);
250 
251 	snd_card_set_id(card, VALID_NAME);
252 	KUNIT_EXPECT_STREQ(test, card->id, VALID_NAME);
253 
254 	/* clear the first id character so we can set it again */
255 	card->id[0] = '\0';
256 	snd_card_set_id(card, NAME_W_SPEC_CHARS);
257 	KUNIT_EXPECT_STRNEQ(test, card->id, NAME_W_SPEC_CHARS);
258 
259 	card->id[0] = '\0';
260 	snd_card_set_id(card, NAME_W_SPACE);
261 	kunit_info(test, "%s", card->id);
262 	KUNIT_EXPECT_STREQ(test, card->id, NAME_W_SPACE_REMOVED);
263 }
264 
265 static void test_pcm_format_name(struct kunit *test)
266 {
267 	u32 i;
268 	const char *name;
269 
270 	for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) {
271 		name = snd_pcm_format_name(valid_fmt[i].format);
272 		KUNIT_ASSERT_NOT_NULL_MSG(test, name, "Don't have name for %s", valid_fmt[i].name);
273 		KUNIT_EXPECT_STREQ(test, name, valid_fmt[i].name);
274 	}
275 
276 	KUNIT_ASSERT_STREQ(test, snd_pcm_format_name(WRONG_FORMAT_1), "Unknown");
277 	KUNIT_ASSERT_STREQ(test, snd_pcm_format_name(WRONG_FORMAT_2), "Unknown");
278 }
279 
280 static void test_card_add_component(struct kunit *test)
281 {
282 	struct snd_card *card = kunit_kzalloc(test, sizeof(*card), GFP_KERNEL);
283 
284 	snd_component_add(card, TEST_FIRST_COMPONENT);
285 	KUNIT_ASSERT_STREQ(test, card->components, TEST_FIRST_COMPONENT);
286 
287 	snd_component_add(card, TEST_SECOND_COMPONENT);
288 	KUNIT_ASSERT_STREQ(test, card->components, TEST_FIRST_COMPONENT " " TEST_SECOND_COMPONENT);
289 }
290 
291 static struct kunit_case sound_utils_cases[] = {
292 	KUNIT_CASE(test_phys_format_size),
293 	KUNIT_CASE(test_format_width),
294 	KUNIT_CASE(test_format_endianness),
295 	KUNIT_CASE(test_format_signed),
296 	KUNIT_CASE(test_format_fill_silence),
297 	KUNIT_CASE(test_playback_avail),
298 	KUNIT_CASE(test_capture_avail),
299 	KUNIT_CASE(test_card_set_id),
300 	KUNIT_CASE(test_pcm_format_name),
301 	KUNIT_CASE(test_card_add_component),
302 	{},
303 };
304 
305 static struct kunit_suite sound_utils_suite = {
306 	.name = "sound-core-test",
307 	.test_cases = sound_utils_cases,
308 };
309 
310 kunit_test_suite(sound_utils_suite);
311 MODULE_DESCRIPTION("Sound core KUnit test");
312 MODULE_AUTHOR("Ivan Orlov");
313 MODULE_LICENSE("GPL");
314