1 // SPDX-License-Identifier: GPL-2.0-only
3 * soc-topology-test.c -- ALSA SoC Topology Kernel Unit Tests
5 * Copyright(c) 2021 Intel Corporation. All rights reserved.
8 #include <linux/firmware.h>
9 #include <sound/core.h>
10 #include <sound/soc.h>
11 #include <sound/soc-topology.h>
12 #include <kunit/test.h>
14 /* ===== HELPER FUNCTIONS =================================================== */
17 * snd_soc_component needs device to operate on (primarily for prints), create
18 * fake one, as we don't register with PCI or anything else
19 * device_driver name is used in some of the prints (fmt_single_name) so
20 * we also mock up minimal one
22 static struct device *test_dev;
24 static struct device_driver test_drv = {
25 .name = "sound-soc-topology-test-driver",
28 static int snd_soc_tplg_test_init(struct kunit *test)
30 test_dev = root_device_register("sound-soc-topology-test");
31 test_dev = get_device(test_dev);
35 test_dev->driver = &test_drv;
40 static void snd_soc_tplg_test_exit(struct kunit *test)
43 root_device_unregister(test_dev);
47 * helper struct we use when registering component, as we load topology during
48 * component probe, we need to pass struct kunit somehow to probe function, so
49 * we can report test result
51 struct kunit_soc_component {
53 int expect; /* what result we expect when loading topology */
54 struct snd_soc_component comp;
55 struct snd_soc_card card;
59 static int d_probe(struct snd_soc_component *component)
61 struct kunit_soc_component *kunit_comp =
62 container_of(component, struct kunit_soc_component, comp);
65 ret = snd_soc_tplg_component_load(component, NULL, &kunit_comp->fw);
66 KUNIT_EXPECT_EQ_MSG(kunit_comp->kunit, kunit_comp->expect, ret,
67 "Failed topology load");
72 static void d_remove(struct snd_soc_component *component)
74 struct kunit_soc_component *kunit_comp =
75 container_of(component, struct kunit_soc_component, comp);
78 ret = snd_soc_tplg_component_remove(component);
79 KUNIT_EXPECT_EQ(kunit_comp->kunit, 0, ret);
83 * ASoC minimal boiler plate
85 SND_SOC_DAILINK_DEF(dummy, DAILINK_COMP_ARRAY(COMP_DUMMY()));
87 SND_SOC_DAILINK_DEF(platform, DAILINK_COMP_ARRAY(COMP_PLATFORM("sound-soc-topology-test")));
89 static struct snd_soc_dai_link kunit_dai_links[] = {
91 .name = "KUNIT Audio Port",
93 .stream_name = "Audio Playback/Capture",
96 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
99 SND_SOC_DAILINK_REG(dummy, dummy, platform),
103 static const struct snd_soc_component_driver test_component = {
104 .name = "sound-soc-topology-test",
107 .non_legacy_dai_naming = 1,
110 /* ===== TOPOLOGY TEMPLATES ================================================= */
112 // Structural representation of topology which can be generated with:
114 // $ alsatplg -c empty -o empty.tplg
115 // $ xxd -i empty.tplg
117 struct tplg_tmpl_001 {
118 struct snd_soc_tplg_hdr header;
119 struct snd_soc_tplg_manifest manifest;
122 static struct tplg_tmpl_001 tplg_tmpl_empty = {
124 .magic = cpu_to_le32(SND_SOC_TPLG_MAGIC),
125 .abi = cpu_to_le32(5),
127 .type = cpu_to_le32(SND_SOC_TPLG_TYPE_MANIFEST),
128 .size = cpu_to_le32(sizeof(struct snd_soc_tplg_hdr)),
130 .payload_size = cpu_to_le32(sizeof(struct snd_soc_tplg_manifest)),
132 .count = cpu_to_le32(1),
136 .size = cpu_to_le32(sizeof(struct snd_soc_tplg_manifest)),
137 /* rest of fields is 0 */
141 // Structural representation of topology containing SectionPCM
143 struct tplg_tmpl_002 {
144 struct snd_soc_tplg_hdr header;
145 struct snd_soc_tplg_manifest manifest;
146 struct snd_soc_tplg_hdr pcm_header;
147 struct snd_soc_tplg_pcm pcm;
150 static struct tplg_tmpl_002 tplg_tmpl_with_pcm = {
152 .magic = cpu_to_le32(SND_SOC_TPLG_MAGIC),
153 .abi = cpu_to_le32(5),
155 .type = cpu_to_le32(SND_SOC_TPLG_TYPE_MANIFEST),
156 .size = cpu_to_le32(sizeof(struct snd_soc_tplg_hdr)),
158 .payload_size = cpu_to_le32(sizeof(struct snd_soc_tplg_manifest)),
160 .count = cpu_to_le32(1),
163 .size = cpu_to_le32(sizeof(struct snd_soc_tplg_manifest)),
164 .pcm_elems = cpu_to_le32(1),
165 /* rest of fields is 0 */
168 .magic = cpu_to_le32(SND_SOC_TPLG_MAGIC),
169 .abi = cpu_to_le32(5),
171 .type = cpu_to_le32(SND_SOC_TPLG_TYPE_PCM),
172 .size = cpu_to_le32(sizeof(struct snd_soc_tplg_hdr)),
174 .payload_size = cpu_to_le32(sizeof(struct snd_soc_tplg_pcm)),
176 .count = cpu_to_le32(1),
179 .size = cpu_to_le32(sizeof(struct snd_soc_tplg_pcm)),
180 .pcm_name = "KUNIT Audio",
181 .dai_name = "kunit-audio-dai",
184 .playback = cpu_to_le32(1),
185 .capture = cpu_to_le32(1),
189 .channels = cpu_to_le32(2),
192 .channels = cpu_to_le32(2),
198 .name = "kunit-audio-playback",
199 .channels_min = cpu_to_le32(2),
200 .channels_max = cpu_to_le32(2),
203 .name = "kunit-audio-capture",
204 .channels_min = cpu_to_le32(2),
205 .channels_max = cpu_to_le32(2),
214 /* ===== TEST CASES ========================================================= */
217 // Test passing NULL component as parameter to snd_soc_tplg_component_load
220 * need to override generic probe function with one using NULL when calling
221 * topology load during component initialization, we don't need .remove
222 * handler as load should fail
224 static int d_probe_null_comp(struct snd_soc_component *component)
226 struct kunit_soc_component *kunit_comp =
227 container_of(component, struct kunit_soc_component, comp);
230 /* instead of passing component pointer as first argument, pass NULL here */
231 ret = snd_soc_tplg_component_load(NULL, NULL, &kunit_comp->fw);
232 KUNIT_EXPECT_EQ_MSG(kunit_comp->kunit, kunit_comp->expect, ret,
233 "Failed topology load");
238 static const struct snd_soc_component_driver test_component_null_comp = {
239 .name = "sound-soc-topology-test",
240 .probe = d_probe_null_comp,
241 .non_legacy_dai_naming = 1,
244 static void snd_soc_tplg_test_load_with_null_comp(struct kunit *test)
246 struct kunit_soc_component *kunit_comp;
250 kunit_comp = kunit_kzalloc(test, sizeof(*kunit_comp), GFP_KERNEL);
251 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, kunit_comp);
252 kunit_comp->kunit = test;
253 kunit_comp->expect = -EINVAL; /* expect failure */
255 kunit_comp->card.dev = test_dev,
256 kunit_comp->card.name = "kunit-card",
257 kunit_comp->card.owner = THIS_MODULE,
258 kunit_comp->card.dai_link = kunit_dai_links,
259 kunit_comp->card.num_links = ARRAY_SIZE(kunit_dai_links),
260 kunit_comp->card.fully_routed = true,
263 ret = snd_soc_register_card(&kunit_comp->card);
264 if (ret != 0 && ret != -EPROBE_DEFER)
265 KUNIT_FAIL(test, "Failed to register card");
267 ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component_null_comp, test_dev);
268 KUNIT_EXPECT_EQ(test, 0, ret);
270 ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0);
271 KUNIT_EXPECT_EQ(test, 0, ret);
274 snd_soc_unregister_card(&kunit_comp->card);
275 snd_soc_unregister_component(test_dev);
279 // Test passing NULL ops as parameter to snd_soc_tplg_component_load
282 * NULL ops is default case, we pass empty topology (fw), so we don't have
283 * anything to parse and just do nothing, which results in return 0; from
284 * calling soc_tplg_dapm_complete in soc_tplg_process_headers
286 static void snd_soc_tplg_test_load_with_null_ops(struct kunit *test)
288 struct kunit_soc_component *kunit_comp;
292 kunit_comp = kunit_kzalloc(test, sizeof(*kunit_comp), GFP_KERNEL);
293 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, kunit_comp);
294 kunit_comp->kunit = test;
295 kunit_comp->expect = 0; /* expect success */
297 kunit_comp->card.dev = test_dev,
298 kunit_comp->card.name = "kunit-card",
299 kunit_comp->card.owner = THIS_MODULE,
300 kunit_comp->card.dai_link = kunit_dai_links,
301 kunit_comp->card.num_links = ARRAY_SIZE(kunit_dai_links),
302 kunit_comp->card.fully_routed = true,
305 ret = snd_soc_register_card(&kunit_comp->card);
306 if (ret != 0 && ret != -EPROBE_DEFER)
307 KUNIT_FAIL(test, "Failed to register card");
309 ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component, test_dev);
310 KUNIT_EXPECT_EQ(test, 0, ret);
312 ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0);
313 KUNIT_EXPECT_EQ(test, 0, ret);
316 snd_soc_unregister_card(&kunit_comp->card);
318 snd_soc_unregister_component(test_dev);
322 // Test passing NULL fw as parameter to snd_soc_tplg_component_load
325 * need to override generic probe function with one using NULL pointer to fw
326 * when calling topology load during component initialization, we don't need
327 * .remove handler as load should fail
329 static int d_probe_null_fw(struct snd_soc_component *component)
331 struct kunit_soc_component *kunit_comp =
332 container_of(component, struct kunit_soc_component, comp);
335 /* instead of passing fw pointer as third argument, pass NULL here */
336 ret = snd_soc_tplg_component_load(component, NULL, NULL);
337 KUNIT_EXPECT_EQ_MSG(kunit_comp->kunit, kunit_comp->expect, ret,
338 "Failed topology load");
343 static const struct snd_soc_component_driver test_component_null_fw = {
344 .name = "sound-soc-topology-test",
345 .probe = d_probe_null_fw,
346 .non_legacy_dai_naming = 1,
349 static void snd_soc_tplg_test_load_with_null_fw(struct kunit *test)
351 struct kunit_soc_component *kunit_comp;
355 kunit_comp = kunit_kzalloc(test, sizeof(*kunit_comp), GFP_KERNEL);
356 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, kunit_comp);
357 kunit_comp->kunit = test;
358 kunit_comp->expect = -EINVAL; /* expect failure */
360 kunit_comp->card.dev = test_dev,
361 kunit_comp->card.name = "kunit-card",
362 kunit_comp->card.owner = THIS_MODULE,
363 kunit_comp->card.dai_link = kunit_dai_links,
364 kunit_comp->card.num_links = ARRAY_SIZE(kunit_dai_links),
365 kunit_comp->card.fully_routed = true,
368 ret = snd_soc_register_card(&kunit_comp->card);
369 if (ret != 0 && ret != -EPROBE_DEFER)
370 KUNIT_FAIL(test, "Failed to register card");
372 ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component_null_fw, test_dev);
373 KUNIT_EXPECT_EQ(test, 0, ret);
375 ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0);
376 KUNIT_EXPECT_EQ(test, 0, ret);
379 snd_soc_unregister_card(&kunit_comp->card);
381 snd_soc_unregister_component(test_dev);
385 // Test passing "empty" topology file
386 static void snd_soc_tplg_test_load_empty_tplg(struct kunit *test)
388 struct kunit_soc_component *kunit_comp;
389 struct tplg_tmpl_001 *data;
394 kunit_comp = kunit_kzalloc(test, sizeof(*kunit_comp), GFP_KERNEL);
395 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, kunit_comp);
396 kunit_comp->kunit = test;
397 kunit_comp->expect = 0; /* expect success */
399 size = sizeof(tplg_tmpl_empty);
400 data = kunit_kzalloc(kunit_comp->kunit, size, GFP_KERNEL);
401 KUNIT_EXPECT_NOT_ERR_OR_NULL(kunit_comp->kunit, data);
403 memcpy(data, &tplg_tmpl_empty, sizeof(tplg_tmpl_empty));
405 kunit_comp->fw.data = (u8 *)data;
406 kunit_comp->fw.size = size;
408 kunit_comp->card.dev = test_dev,
409 kunit_comp->card.name = "kunit-card",
410 kunit_comp->card.owner = THIS_MODULE,
411 kunit_comp->card.dai_link = kunit_dai_links,
412 kunit_comp->card.num_links = ARRAY_SIZE(kunit_dai_links),
413 kunit_comp->card.fully_routed = true,
416 ret = snd_soc_register_card(&kunit_comp->card);
417 if (ret != 0 && ret != -EPROBE_DEFER)
418 KUNIT_FAIL(test, "Failed to register card");
420 ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component, test_dev);
421 KUNIT_EXPECT_EQ(test, 0, ret);
423 ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0);
424 KUNIT_EXPECT_EQ(test, 0, ret);
427 snd_soc_unregister_card(&kunit_comp->card);
429 snd_soc_unregister_component(test_dev);
433 // Test "empty" topology file, but with bad "magic"
434 // In theory we could loop through all possible bad values, but it takes too
435 // long, so just use SND_SOC_TPLG_MAGIC + 1
436 static void snd_soc_tplg_test_load_empty_tplg_bad_magic(struct kunit *test)
438 struct kunit_soc_component *kunit_comp;
439 struct tplg_tmpl_001 *data;
444 kunit_comp = kunit_kzalloc(test, sizeof(*kunit_comp), GFP_KERNEL);
445 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, kunit_comp);
446 kunit_comp->kunit = test;
447 kunit_comp->expect = -EINVAL; /* expect failure */
449 size = sizeof(tplg_tmpl_empty);
450 data = kunit_kzalloc(kunit_comp->kunit, size, GFP_KERNEL);
451 KUNIT_EXPECT_NOT_ERR_OR_NULL(kunit_comp->kunit, data);
453 memcpy(data, &tplg_tmpl_empty, sizeof(tplg_tmpl_empty));
456 * any value != magic number is wrong
458 data->header.magic = cpu_to_le32(SND_SOC_TPLG_MAGIC + 1);
460 kunit_comp->fw.data = (u8 *)data;
461 kunit_comp->fw.size = size;
463 kunit_comp->card.dev = test_dev,
464 kunit_comp->card.name = "kunit-card",
465 kunit_comp->card.owner = THIS_MODULE,
466 kunit_comp->card.dai_link = kunit_dai_links,
467 kunit_comp->card.num_links = ARRAY_SIZE(kunit_dai_links),
468 kunit_comp->card.fully_routed = true,
471 ret = snd_soc_register_card(&kunit_comp->card);
472 if (ret != 0 && ret != -EPROBE_DEFER)
473 KUNIT_FAIL(test, "Failed to register card");
475 ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component, test_dev);
476 KUNIT_EXPECT_EQ(test, 0, ret);
478 ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0);
479 KUNIT_EXPECT_EQ(test, 0, ret);
482 snd_soc_unregister_card(&kunit_comp->card);
484 snd_soc_unregister_component(test_dev);
488 // Test "empty" topology file, but with bad "abi"
489 // In theory we could loop through all possible bad values, but it takes too
490 // long, so just use SND_SOC_TPLG_ABI_VERSION + 1
491 static void snd_soc_tplg_test_load_empty_tplg_bad_abi(struct kunit *test)
493 struct kunit_soc_component *kunit_comp;
494 struct tplg_tmpl_001 *data;
499 kunit_comp = kunit_kzalloc(test, sizeof(*kunit_comp), GFP_KERNEL);
500 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, kunit_comp);
501 kunit_comp->kunit = test;
502 kunit_comp->expect = -EINVAL; /* expect failure */
504 size = sizeof(tplg_tmpl_empty);
505 data = kunit_kzalloc(kunit_comp->kunit, size, GFP_KERNEL);
506 KUNIT_EXPECT_NOT_ERR_OR_NULL(kunit_comp->kunit, data);
508 memcpy(data, &tplg_tmpl_empty, sizeof(tplg_tmpl_empty));
511 * any value != accepted range is wrong
513 data->header.abi = cpu_to_le32(SND_SOC_TPLG_ABI_VERSION + 1);
515 kunit_comp->fw.data = (u8 *)data;
516 kunit_comp->fw.size = size;
518 kunit_comp->card.dev = test_dev,
519 kunit_comp->card.name = "kunit-card",
520 kunit_comp->card.owner = THIS_MODULE,
521 kunit_comp->card.dai_link = kunit_dai_links,
522 kunit_comp->card.num_links = ARRAY_SIZE(kunit_dai_links),
523 kunit_comp->card.fully_routed = true,
526 ret = snd_soc_register_card(&kunit_comp->card);
527 if (ret != 0 && ret != -EPROBE_DEFER)
528 KUNIT_FAIL(test, "Failed to register card");
530 ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component, test_dev);
531 KUNIT_EXPECT_EQ(test, 0, ret);
533 ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0);
534 KUNIT_EXPECT_EQ(test, 0, ret);
537 snd_soc_unregister_card(&kunit_comp->card);
539 snd_soc_unregister_component(test_dev);
543 // Test "empty" topology file, but with bad "size"
544 // In theory we could loop through all possible bad values, but it takes too
545 // long, so just use sizeof(struct snd_soc_tplg_hdr) + 1
546 static void snd_soc_tplg_test_load_empty_tplg_bad_size(struct kunit *test)
548 struct kunit_soc_component *kunit_comp;
549 struct tplg_tmpl_001 *data;
554 kunit_comp = kunit_kzalloc(test, sizeof(*kunit_comp), GFP_KERNEL);
555 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, kunit_comp);
556 kunit_comp->kunit = test;
557 kunit_comp->expect = -EINVAL; /* expect failure */
559 size = sizeof(tplg_tmpl_empty);
560 data = kunit_kzalloc(kunit_comp->kunit, size, GFP_KERNEL);
561 KUNIT_EXPECT_NOT_ERR_OR_NULL(kunit_comp->kunit, data);
563 memcpy(data, &tplg_tmpl_empty, sizeof(tplg_tmpl_empty));
566 * any value != struct size is wrong
568 data->header.size = cpu_to_le32(sizeof(struct snd_soc_tplg_hdr) + 1);
570 kunit_comp->fw.data = (u8 *)data;
571 kunit_comp->fw.size = size;
573 kunit_comp->card.dev = test_dev,
574 kunit_comp->card.name = "kunit-card",
575 kunit_comp->card.owner = THIS_MODULE,
576 kunit_comp->card.dai_link = kunit_dai_links,
577 kunit_comp->card.num_links = ARRAY_SIZE(kunit_dai_links),
578 kunit_comp->card.fully_routed = true,
581 ret = snd_soc_register_card(&kunit_comp->card);
582 if (ret != 0 && ret != -EPROBE_DEFER)
583 KUNIT_FAIL(test, "Failed to register card");
585 ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component, test_dev);
586 KUNIT_EXPECT_EQ(test, 0, ret);
588 ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0);
589 KUNIT_EXPECT_EQ(test, 0, ret);
592 snd_soc_unregister_card(&kunit_comp->card);
594 snd_soc_unregister_component(test_dev);
598 // Test "empty" topology file, but with bad "payload_size"
599 // In theory we could loop through all possible bad values, but it takes too
600 // long, so just use the known wrong one
601 static void snd_soc_tplg_test_load_empty_tplg_bad_payload_size(struct kunit *test)
603 struct kunit_soc_component *kunit_comp;
604 struct tplg_tmpl_001 *data;
609 kunit_comp = kunit_kzalloc(test, sizeof(*kunit_comp), GFP_KERNEL);
610 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, kunit_comp);
611 kunit_comp->kunit = test;
612 kunit_comp->expect = -EINVAL; /* expect failure */
614 size = sizeof(tplg_tmpl_empty);
615 data = kunit_kzalloc(kunit_comp->kunit, size, GFP_KERNEL);
616 KUNIT_EXPECT_NOT_ERR_OR_NULL(kunit_comp->kunit, data);
618 memcpy(data, &tplg_tmpl_empty, sizeof(tplg_tmpl_empty));
620 * override payload size
621 * there is only explicit check for 0, so check with it, other values
622 * are handled by just not reading behind EOF
624 data->header.payload_size = 0;
626 kunit_comp->fw.data = (u8 *)data;
627 kunit_comp->fw.size = size;
629 kunit_comp->card.dev = test_dev,
630 kunit_comp->card.name = "kunit-card",
631 kunit_comp->card.owner = THIS_MODULE,
632 kunit_comp->card.dai_link = kunit_dai_links,
633 kunit_comp->card.num_links = ARRAY_SIZE(kunit_dai_links),
634 kunit_comp->card.fully_routed = true,
637 ret = snd_soc_register_card(&kunit_comp->card);
638 if (ret != 0 && ret != -EPROBE_DEFER)
639 KUNIT_FAIL(test, "Failed to register card");
641 ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component, test_dev);
642 KUNIT_EXPECT_EQ(test, 0, ret);
644 ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0);
645 KUNIT_EXPECT_EQ(test, 0, ret);
648 snd_soc_unregister_component(test_dev);
650 snd_soc_unregister_card(&kunit_comp->card);
654 // Test passing topology file with PCM definition
655 static void snd_soc_tplg_test_load_pcm_tplg(struct kunit *test)
657 struct kunit_soc_component *kunit_comp;
663 kunit_comp = kunit_kzalloc(test, sizeof(*kunit_comp), GFP_KERNEL);
664 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, kunit_comp);
665 kunit_comp->kunit = test;
666 kunit_comp->expect = 0; /* expect success */
668 size = sizeof(tplg_tmpl_with_pcm);
669 data = kunit_kzalloc(kunit_comp->kunit, size, GFP_KERNEL);
670 KUNIT_EXPECT_NOT_ERR_OR_NULL(kunit_comp->kunit, data);
672 memcpy(data, &tplg_tmpl_with_pcm, sizeof(tplg_tmpl_with_pcm));
674 kunit_comp->fw.data = data;
675 kunit_comp->fw.size = size;
677 kunit_comp->card.dev = test_dev,
678 kunit_comp->card.name = "kunit-card",
679 kunit_comp->card.owner = THIS_MODULE,
680 kunit_comp->card.dai_link = kunit_dai_links,
681 kunit_comp->card.num_links = ARRAY_SIZE(kunit_dai_links),
682 kunit_comp->card.fully_routed = true,
685 ret = snd_soc_register_card(&kunit_comp->card);
686 if (ret != 0 && ret != -EPROBE_DEFER)
687 KUNIT_FAIL(test, "Failed to register card");
689 ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component, test_dev);
690 KUNIT_EXPECT_EQ(test, 0, ret);
692 ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0);
693 KUNIT_EXPECT_EQ(test, 0, ret);
695 snd_soc_unregister_component(test_dev);
698 snd_soc_unregister_card(&kunit_comp->card);
702 // Test passing topology file with PCM definition
703 // with component reload
704 static void snd_soc_tplg_test_load_pcm_tplg_reload_comp(struct kunit *test)
706 struct kunit_soc_component *kunit_comp;
713 kunit_comp = kunit_kzalloc(test, sizeof(*kunit_comp), GFP_KERNEL);
714 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, kunit_comp);
715 kunit_comp->kunit = test;
716 kunit_comp->expect = 0; /* expect success */
718 size = sizeof(tplg_tmpl_with_pcm);
719 data = kunit_kzalloc(kunit_comp->kunit, size, GFP_KERNEL);
720 KUNIT_EXPECT_NOT_ERR_OR_NULL(kunit_comp->kunit, data);
722 memcpy(data, &tplg_tmpl_with_pcm, sizeof(tplg_tmpl_with_pcm));
724 kunit_comp->fw.data = data;
725 kunit_comp->fw.size = size;
727 kunit_comp->card.dev = test_dev,
728 kunit_comp->card.name = "kunit-card",
729 kunit_comp->card.owner = THIS_MODULE,
730 kunit_comp->card.dai_link = kunit_dai_links,
731 kunit_comp->card.num_links = ARRAY_SIZE(kunit_dai_links),
732 kunit_comp->card.fully_routed = true,
735 ret = snd_soc_register_card(&kunit_comp->card);
736 if (ret != 0 && ret != -EPROBE_DEFER)
737 KUNIT_FAIL(test, "Failed to register card");
739 for (i = 0; i < 100; i++) {
740 ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component, test_dev);
741 KUNIT_EXPECT_EQ(test, 0, ret);
743 ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0);
744 KUNIT_EXPECT_EQ(test, 0, ret);
746 snd_soc_unregister_component(test_dev);
750 snd_soc_unregister_card(&kunit_comp->card);
754 // Test passing topology file with PCM definition
756 static void snd_soc_tplg_test_load_pcm_tplg_reload_card(struct kunit *test)
758 struct kunit_soc_component *kunit_comp;
765 kunit_comp = kunit_kzalloc(test, sizeof(*kunit_comp), GFP_KERNEL);
766 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, kunit_comp);
767 kunit_comp->kunit = test;
768 kunit_comp->expect = 0; /* expect success */
770 size = sizeof(tplg_tmpl_with_pcm);
771 data = kunit_kzalloc(kunit_comp->kunit, size, GFP_KERNEL);
772 KUNIT_EXPECT_NOT_ERR_OR_NULL(kunit_comp->kunit, data);
774 memcpy(data, &tplg_tmpl_with_pcm, sizeof(tplg_tmpl_with_pcm));
776 kunit_comp->fw.data = data;
777 kunit_comp->fw.size = size;
779 kunit_comp->card.dev = test_dev,
780 kunit_comp->card.name = "kunit-card",
781 kunit_comp->card.owner = THIS_MODULE,
782 kunit_comp->card.dai_link = kunit_dai_links,
783 kunit_comp->card.num_links = ARRAY_SIZE(kunit_dai_links),
784 kunit_comp->card.fully_routed = true,
787 ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component, test_dev);
788 KUNIT_EXPECT_EQ(test, 0, ret);
790 ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0);
791 KUNIT_EXPECT_EQ(test, 0, ret);
793 for (i = 0; i < 100; i++) {
794 ret = snd_soc_register_card(&kunit_comp->card);
795 if (ret != 0 && ret != -EPROBE_DEFER)
796 KUNIT_FAIL(test, "Failed to register card");
798 snd_soc_unregister_card(&kunit_comp->card);
802 snd_soc_unregister_component(test_dev);
805 /* ===== KUNIT MODULE DEFINITIONS =========================================== */
807 static struct kunit_case snd_soc_tplg_test_cases[] = {
808 KUNIT_CASE(snd_soc_tplg_test_load_with_null_comp),
809 KUNIT_CASE(snd_soc_tplg_test_load_with_null_ops),
810 KUNIT_CASE(snd_soc_tplg_test_load_with_null_fw),
811 KUNIT_CASE(snd_soc_tplg_test_load_empty_tplg),
812 KUNIT_CASE(snd_soc_tplg_test_load_empty_tplg_bad_magic),
813 KUNIT_CASE(snd_soc_tplg_test_load_empty_tplg_bad_abi),
814 KUNIT_CASE(snd_soc_tplg_test_load_empty_tplg_bad_size),
815 KUNIT_CASE(snd_soc_tplg_test_load_empty_tplg_bad_payload_size),
816 KUNIT_CASE(snd_soc_tplg_test_load_pcm_tplg),
817 KUNIT_CASE(snd_soc_tplg_test_load_pcm_tplg_reload_comp),
818 KUNIT_CASE(snd_soc_tplg_test_load_pcm_tplg_reload_card),
822 static struct kunit_suite snd_soc_tplg_test_suite = {
823 .name = "snd_soc_tplg_test",
824 .init = snd_soc_tplg_test_init,
825 .exit = snd_soc_tplg_test_exit,
826 .test_cases = snd_soc_tplg_test_cases,
829 kunit_test_suites(&snd_soc_tplg_test_suite);
831 MODULE_LICENSE("GPL");