Add KUNIT tests for ASoC topology
cover Add KUNIT tests for ASoC topology
This series adds unit tests for ASoC topology.
First fix problems found when developing and running test cases and then add tests implementation.
Tests themselves are quite simple and just call snd_soc_tplg_component_load() with various parameters and check the result. Tests themselves are described in more detail in commits adding them.
Goal is to expand the amount of test cases in following patches.
Prerequisity for this patchset are 2 patches which have already been sent:
https://lore.kernel.org/alsa-devel/[email protected]/T/#t
Description on how typical test case itself works:
In order to load topology we need to have 3 things:
card, codec component & platform component.
In typical test case we register card and platform component and bind to dummy codec. There are of course execeptions, when we want to test behaviour of topology API when component or card is missing. Note that this is bit different from typical scenario (in SOF and skylake drivers) where card is registered by machine driver and component by platform driver, as we register both when setting up test.
If you check the test case most of them have similar architecture of:
1.
/* run test */ ret = snd_soc_register_card(&kunit_comp->card); if (ret != 0 && ret != -EPROBE_DEFER) KUNIT_FAIL(test, "Failed to register card");
2.
ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component, test_dev); KUNIT_EXPECT_EQ(test, 0, ret);
3.
ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0); KUNIT_EXPECT_EQ(test, 0, ret);
Ad. 1.
First we register card, which in most tests returns -EPROBE_DEFER (from snd_soc_bind_card()), as platform component is not yet created. I test for both 0 and -EPROBE_DEFER, as it makes it easier to reshuffle this code around if needed and there is one test case which does it in different order.
Ad. 2.
Then we initialize platform component with structure pointing at proper probe function, which calls snd_soc_tplg_component_load() with test parameters and checks expected result.
Ad. 3.
And then in follow up we call snd_soc_add_component() which creates platform component for us and calls snd_soc_try_rebind_card() which if everything is bound properly calls previously set probe function.
Amadeusz Sławiński (5):
ASoC: topology: Properly unregister DAI on removal Revert "ASoC: soc-devres: add devm_snd_soc_register_dai()"
ASoC: topology: KUnit: Add KUnit tests passing various arguments to
snd_soc_tplg_component_load
ASoC: topology: KUnit: Add KUnit tests passing empty topology with
variants to snd_soc_tplg_component_load
ASoC: topology: KUnit: Add KUnit tests passing topology with PCM to
snd_soc_tplg_component_load
include/sound/soc.h | 4 - sound/soc/Kconfig | 17 + sound/soc/Makefile | 5 + sound/soc/soc-devres.c | 37 -- sound/soc/soc-topology-test.c | 843 ++++++++++++++++++++++++++++++++++ sound/soc/soc-topology.c | 9 +- 6 files changed, 870 insertions(+), 45 deletions(-) create mode 100644 sound/soc/soc-topology-test.c
Signature · 1 line
2.25.1
PATCH 1/5 ASoC: topology: Properly unregister DAI on removal
DAIs need to be removed when topology unload function is called (usually done when component is being removed). We can't do this when device is being removed, as structures we operate on when removing DAI can already be freed.
--- sound/soc/soc-topology.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
sound/soc/soc-topology.c +5 -4
@@ -447,7 +447,7 @@ static void remove_dai(struct snd_soc_component *comp, {struct snd_soc_dai_driver *dai_drv = container_of(dobj, struct snd_soc_dai_driver, dobj); -struct snd_soc_dai *dai; +struct snd_soc_dai *dai, *_dai;if (pass !=SOC_TPLG_PASS_PCM_DAI )return ;
@@ -455,9 +455,9 @@ static void remove_dai(struct snd_soc_component *comp,if (dobj->ops && dobj->ops->dai_unload) dobj->ops->dai_unload (comp, dobj); -for_each_component_dais (comp, dai) +for_each_component_dais_safe (comp, dai, _dai)if (dai->driver == dai_drv) - dai->driver =NULL ; +snd_soc_unregister_dai (dai);list_del (&dobj->list); }
@@ -1742,7 +1742,7 @@ static int soc_tplg_dai_create(struct soc_tplg *tplg,list_add (&dai_drv->dobj.list, &tplg->comp->dobj_list);/* register the DAI to the component */ - dai =devm_snd_soc_register_dai (tplg->dev, tplg->comp, dai_drv, false); + dai =snd_soc_register_dai (tplg->comp, dai_drv, false);if (!dai)return -ENOMEM ;
@@ -1750,6 +1750,7 @@ static int soc_tplg_dai_create(struct soc_tplg *tplg, ret =snd_soc_dapm_new_dai_widgets (dapm, dai);if (ret !=0 ) {dev_err (dai->dev,"Failed to create DAI widgets %d\n" , ret); +snd_soc_unregister_dai (dai);return ret; }
Signature · 1 line
2.25.1
PATCH 2/5 Revert "ASoC: soc-devres: add devm_snd_soc_register_dai()"
In reply to Amadeusz SławińskiAs described in preceding patch, waiting with DAI removal until device is being unregistered can lead to working on freed memory, hence having DAIs managed by resource managed memory is a bad idea, revert commit creating devm_ API for this, so no one makes a mistake of using it.
This reverts commit 0fae253af563cf5d1f5dc651d520c3eafd74f183.
--- include/sound/soc.h | 4 ---- sound/soc/soc-devres.c | 37 ------------------------------------- 2 files changed, 41 deletions(-)
include/sound/soc.h +0 -4
@@ -1236,10 +1236,6 @@ void snd_soc_remove_pcm_runtime(struct snd_soc_card *card,struct snd_soc_dai *snd_soc_register_dai (struct snd_soc_component *component,struct snd_soc_dai_driver *dai_drv,bool legacy_dai_naming); -struct snd_soc_dai *devm_snd_soc_register_dai (struct device *dev, -struct snd_soc_component *component, -struct snd_soc_dai_driver *dai_drv, -bool legacy_dai_naming);void snd_soc_unregister_dai (struct snd_soc_dai *dai); struct snd_soc_dai *snd_soc_find_dai(
sound/soc/soc-devres.c +0 -37
@@ -9,43 +9,6 @@#include <sound/soc.h> #include <sound/dmaengine_pcm.h> -static void devm_dai_release (struct device *dev,void *res) -{ -snd_soc_unregister_dai (*(struct snd_soc_dai **)res); -} - -/** -* devm_snd_soc_register_dai - resource-managed dai registration -* @dev: Device used to manage component -* @component: The component the DAIs are registered for -* @dai_drv: DAI driver to use for the DAI -* @legacy_dai_naming: if %true, use legacy single-name format; -* if %false, use multiple-name format; -*/ -struct snd_soc_dai *devm_snd_soc_register_dai (struct device *dev, -struct snd_soc_component *component, -struct snd_soc_dai_driver *dai_drv, -bool legacy_dai_naming) -{ -struct snd_soc_dai **ptr; -struct snd_soc_dai *dai; - - ptr =devres_alloc (devm_dai_release,sizeof (*ptr),GFP_KERNEL ); -if (!ptr) -return NULL ; - - dai =snd_soc_register_dai (component, dai_drv, legacy_dai_naming); -if (dai) { - *ptr = dai; -devres_add (dev, ptr); - }else { -devres_free (ptr); - } - -return dai; -} -EXPORT_SYMBOL_GPL (devm_snd_soc_register_dai); -static void devm_component_release (struct device *dev,void *res) {const struct snd_soc_component_driver **cmpnt_drv = res;
Signature · 1 line
2.25.1
Mark Brown
maintainerRe: [PATCH 2/5] Revert "ASoC: soc-devres: add devm_snd_soc_register_dai()"
Quoted 2 lines · Amadeusz Sławiński
As described in preceding patch, waiting with DAI removal until device is being unregistered can lead to working on freed memory, hence having
There is a potential for misuse with essentially all devm functions - why is this one so special?
Please submit patches using subject lines reflecting the style for the subsystem, this makes it easier for people to identify relevant patches. Look at what existing commits in the area you're changing are doing and make sure your subject lines visually resemble what they're doing. There's no need to resubmit to fix this alone.
Quoted 4 lines
DAIs managed by resource managed memory is a bad idea, revert commit creating
devm_API for this, so no one makes a mistake of using it.This reverts commit
0fae253af563cf5d1f5dc651d520c3eafd74f183.
Please include human readable descriptions of things like commits and issues being discussed in e-mail in your mails, this makes them much easier for humans to read especially when they have no internet access. I do frequently catch up on my mail on flights or while otherwise travelling so this is even more pressing for me than just being about making things a bit easier to read.
Re: [PATCH 2/5] Revert "ASoC: soc-devres: add devm_snd_soc_register_dai()"
Quoted 6 lines · Mark Brown
Quoted 2 lines · Amadeusz Sławiński
As described in preceding patch, waiting with DAI removal until device is being unregistered can lead to working on freed memory, hence having
There is a potential for misuse with essentially all devm functions - why is this one so special?
Hm... right, I blinded myself thinking too much about specific use case where it was used in topology. As topology is used with drivers split into multiple modules where it caused problem. It should work fine if someone writes monolithic module and needs to register DAI. I guess we can skip this patch then.
Quoted 19 lines
Please submit patches using subject lines reflecting the style for the subsystem, this makes it easier for people to identify relevant patches. Look at what existing commits in the area you're changing are doing and make sure your subject lines visually resemble what they're doing. There's no need to resubmit to fix this alone.
Quoted 4 lines
DAIs managed by resource managed memory is a bad idea, revert commit creating
devm_API for this, so no one makes a mistake of using it.This reverts commit
0fae253af563cf5d1f5dc651d520c3eafd74f183.Please include human readable descriptions of things like commits and issues being discussed in e-mail in your mails, this makes them much easier for humans to read especially when they have no internet access. I do frequently catch up on my mail on flights or while otherwise travelling so this is even more pressing for me than just being about making things a bit easier to read.
PATCH 3/5 ASoC: topology: KUnit: Add KUnit tests passing various arguments to snd_soc_tplg_component_load
In reply to Amadeusz SławińskiIn order to ensure correct behaviour of topology API, add unit tests exercising topology functionality.
Start with adding cases for passing various arguments to snd_soc_tplg_component_load as it is part of exposed topology API.
First test case adds test passing NULL component as argument. Following one adds test case for passing NULL ops as argument. Finally add test case passing NULL fw as argument.
--- sound/soc/Kconfig | 17 ++ sound/soc/Makefile | 5 + sound/soc/soc-topology-test.c | 303 ++++++++++++++++++++++++++++++++++ 3 files changed, 325 insertions(+) create mode 100644 sound/soc/soc-topology-test.c
sound/soc/Kconfig +17 -0
@@ -37,6 +37,23 @@ config SND_SOC_COMPRESS config SND_SOC_TOPOLOGY bool +config SND_SOC_TOPOLOGY_KUNIT_TESTS + tristate "KUnit tests for SoC topology" + depends on KUNIT + depends on SND_SOC_TOPOLOGY + default KUNIT_ALL_TESTS + help + If you want to perform tests on ALSA SoC topology support say Y here. + + This builds a module which can be later manually loaded to run KUNIT + test cases against soc-topology.c API. This should be primarily used + by developers to test their changes to ASoC. + + Do note that it creates fake playback devices which do not interact + well with userspace. When running tests one may want to disable + userspace applications such as pulseaudio, to prevent unnecessary + problems. + config SND_SOC_ACPI tristate
sound/soc/Makefile +5 -0
@@ -7,6 +7,11 @@ ifneq ($(CONFIG_SND_SOC_TOPOLOGY),) snd-soc-core-objs += soc-topology.o endif +ifneq ($(CONFIG_SND_SOC_TOPOLOGY_KUNIT_TESTS),) +# snd-soc-test-objs := soc-topology-test.o +obj-$(CONFIG_SND_SOC_TOPOLOGY_KUNIT_TESTS) := soc-topology-test.o +endif + ifneq ($(CONFIG_SND_SOC_GENERIC_DMAENGINE_PCM),) snd-soc-core-objs += soc-generic-dmaengine-pcm.o endif
sound/soc/soc-topology-test.c new file +303 -0
303 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH 4/5 ASoC: topology: KUnit: Add KUnit tests passing empty topology with variants to snd_soc_tplg_component_load
In reply to Amadeusz SławińskiIn order to ensure correct behaviour of topology API, add unit tests exercising topology functionality.
Add "empty" topology template and tests for parsing it. Also adds few variants with bad magic numbers.
--- sound/soc/soc-topology-test.c | 311 +++++++++++++++++++++++++++++++++- 1 file changed, 310 insertions(+), 1 deletion(-)
sound/soc/soc-topology-test.c +310 -1
@@ -6,7 +6,6 @@*/ #include <linux/firmware.h> -#include <linux/random.h> #include <sound/core.h> #include <sound/soc.h> #include <sound/soc-topology.h>
@@ -108,6 +107,37 @@ static const struct snd_soc_component_driver test_component = { .non_legacy_dai_naming = 1, }; +/* ===== TOPOLOGY TEMPLATES ================================================= */ + +// Structural representation of topology which can be generated with: +// $ touch empty +// $ alsatplg -c empty -o empty.tplg +// $ xxd -i empty.tplg + +struct tplg_tmpl_001 { +struct snd_soc_tplg_hdr header; +struct snd_soc_tplg_manifest manifest; +} __packed; + +static struct tplg_tmpl_001 tplg_tmpl_empty = { + .header = { + .magic =SND_SOC_TPLG_MAGIC , + .abi =5 , + .version =0 , + .type =SND_SOC_TPLG_TYPE_MANIFEST , + .size =sizeof (struct snd_soc_tplg_hdr ), + .vendor_type =0 , + .payload_size =sizeof (struct snd_soc_tplg_manifest ), + .index =0 , + .count =1 , + }, + + .manifest = { + .size =sizeof (struct snd_soc_tplg_manifest ), +/* rest of fields is 0 */ + }, +}; +/* ===== TEST CASES ========================================================= */ // TEST CASE
@@ -282,12 +312,291 @@ static void snd_soc_tplg_test_load_with_null_fw(struct kunit *test)snd_soc_unregister_component (test_dev); } +// TEST CASE +// Test passing "empty" topology file +static void snd_soc_tplg_test_load_empty_tplg (struct kunit *test) +{ +struct kunit_soc_component *kunit_comp; +struct tplg_tmpl_001 *data; +int size; +int ret; + +/* prepare */ + kunit_comp =kunit_kzalloc (test,sizeof (*kunit_comp),GFP_KERNEL ); +KUNIT_EXPECT_NOT_ERR_OR_NULL (test, kunit_comp); + kunit_comp->kunit = test; + kunit_comp->expect =0 ;/* expect success */ + + size =sizeof (tplg_tmpl_empty); + data =kunit_kzalloc (kunit_comp->kunit, size,GFP_KERNEL ); +KUNIT_EXPECT_NOT_ERR_OR_NULL (kunit_comp->kunit, data); + +memcpy (data, &tplg_tmpl_empty,sizeof (tplg_tmpl_empty)); + + kunit_comp->fw.data = (u8 *)data; + kunit_comp->fw.size = size; + + kunit_comp->card.dev = test_dev, + kunit_comp->card.name ="kunit-card" , + kunit_comp->card.owner =THIS_MODULE , + kunit_comp->card.dai_link = kunit_dai_links, + kunit_comp->card.num_links =ARRAY_SIZE (kunit_dai_links), + kunit_comp->card.fully_routed = true, + +/* run test */ + ret =snd_soc_register_card (&kunit_comp->card); +if (ret !=0 && ret != -EPROBE_DEFER ) +KUNIT_FAIL (test,"Failed to register card" ); + + ret =snd_soc_component_initialize (&kunit_comp->comp, &test_component, test_dev); +KUNIT_EXPECT_EQ (test,0 , ret); + + ret =snd_soc_add_component (&kunit_comp->comp,NULL ,0 ); +KUNIT_EXPECT_EQ (test,0 , ret); + +/* cleanup */ + ret =snd_soc_unregister_card (&kunit_comp->card); +KUNIT_EXPECT_EQ (test,0 , ret); + +snd_soc_unregister_component (test_dev); +} + +// TEST CASE +// Test "empty" topology file, but with bad "magic" +// In theory we could loop through all possible bad values, but it takes too +// long, so just use SND_SOC_TPLG_MAGIC + 1 +static void snd_soc_tplg_test_load_empty_tplg_bad_magic (struct kunit *test) +{ +struct kunit_soc_component *kunit_comp; +struct tplg_tmpl_001 *data; +int size; +int ret; + +/* prepare */ + kunit_comp =kunit_kzalloc (test,sizeof (*kunit_comp),GFP_KERNEL ); +KUNIT_EXPECT_NOT_ERR_OR_NULL (test, kunit_comp); + kunit_comp->kunit = test; + kunit_comp->expect = -EINVAL ;/* expect failure */ + + size =sizeof (tplg_tmpl_empty); + data =kunit_kzalloc (kunit_comp->kunit, size,GFP_KERNEL ); +KUNIT_EXPECT_NOT_ERR_OR_NULL (kunit_comp->kunit, data); + +memcpy (data, &tplg_tmpl_empty,sizeof (tplg_tmpl_empty)); +/* +* override abi +* any value != magic number is wrong +*/ + data->header.magic =SND_SOC_TPLG_MAGIC +1 ; + + kunit_comp->fw.data = (u8 *)data; + kunit_comp->fw.size = size; + + kunit_comp->card.dev = test_dev, + kunit_comp->card.name ="kunit-card" , + kunit_comp->card.owner =THIS_MODULE , + kunit_comp->card.dai_link = kunit_dai_links, + kunit_comp->card.num_links =ARRAY_SIZE (kunit_dai_links), + kunit_comp->card.fully_routed = true, + +/* run test */ + ret =snd_soc_register_card (&kunit_comp->card); +if (ret !=0 && ret != -EPROBE_DEFER ) +KUNIT_FAIL (test,"Failed to register card" ); + + ret =snd_soc_component_initialize (&kunit_comp->comp, &test_component, test_dev); +KUNIT_EXPECT_EQ (test,0 , ret); + + ret =snd_soc_add_component (&kunit_comp->comp,NULL ,0 ); +KUNIT_EXPECT_EQ (test,0 , ret); + +/* cleanup */ + ret =snd_soc_unregister_card (&kunit_comp->card); +KUNIT_EXPECT_EQ (test,0 , ret); + +snd_soc_unregister_component (test_dev); +} + +// TEST CASE +// Test "empty" topology file, but with bad "abi" +// In theory we could loop through all possible bad values, but it takes too +// long, so just use SND_SOC_TPLG_ABI_VERSION + 1 +static void snd_soc_tplg_test_load_empty_tplg_bad_abi (struct kunit *test) +{ +struct kunit_soc_component *kunit_comp; +struct tplg_tmpl_001 *data; +int size; +int ret; + +/* prepare */ + kunit_comp =kunit_kzalloc (test,sizeof (*kunit_comp),GFP_KERNEL ); +KUNIT_EXPECT_NOT_ERR_OR_NULL (test, kunit_comp); + kunit_comp->kunit = test; + kunit_comp->expect = -EINVAL ;/* expect failure */ + + size =sizeof (tplg_tmpl_empty); + data =kunit_kzalloc (kunit_comp->kunit, size,GFP_KERNEL ); +KUNIT_EXPECT_NOT_ERR_OR_NULL (kunit_comp->kunit, data); + +memcpy (data, &tplg_tmpl_empty,sizeof (tplg_tmpl_empty)); +/* +* override abi +* any value != accepted range is wrong +*/ + data->header.abi =SND_SOC_TPLG_ABI_VERSION +1 ; + + kunit_comp->fw.data = (u8 *)data; + kunit_comp->fw.size = size; + + kunit_comp->card.dev = test_dev, + kunit_comp->card.name ="kunit-card" , + kunit_comp->card.owner =THIS_MODULE , + kunit_comp->card.dai_link = kunit_dai_links, + kunit_comp->card.num_links =ARRAY_SIZE (kunit_dai_links), + kunit_comp->card.fully_routed = true, + +/* run test */ + ret =snd_soc_register_card (&kunit_comp->card); +if (ret !=0 && ret != -EPROBE_DEFER ) +KUNIT_FAIL (test,"Failed to register card" ); + + ret =snd_soc_component_initialize (&kunit_comp->comp, &test_component, test_dev); +KUNIT_EXPECT_EQ (test,0 , ret); + + ret =snd_soc_add_component (&kunit_comp->comp,NULL ,0 ); +KUNIT_EXPECT_EQ (test,0 , ret); + +/* cleanup */ + ret =snd_soc_unregister_card (&kunit_comp->card); +KUNIT_EXPECT_EQ (test,0 , ret); + +snd_soc_unregister_component (test_dev); +} + +// TEST CASE +// Test "empty" topology file, but with bad "size" +// In theory we could loop through all possible bad values, but it takes too +// long, so just use sizeof(struct snd_soc_tplg_hdr) + 1 +static void snd_soc_tplg_test_load_empty_tplg_bad_size (struct kunit *test) +{ +struct kunit_soc_component *kunit_comp; +struct tplg_tmpl_001 *data; +int size; +int ret; + +/* prepare */ + kunit_comp =kunit_kzalloc (test,sizeof (*kunit_comp),GFP_KERNEL ); +KUNIT_EXPECT_NOT_ERR_OR_NULL (test, kunit_comp); + kunit_comp->kunit = test; + kunit_comp->expect = -EINVAL ;/* expect failure */ + + size =sizeof (tplg_tmpl_empty); + data =kunit_kzalloc (kunit_comp->kunit, size,GFP_KERNEL ); +KUNIT_EXPECT_NOT_ERR_OR_NULL (kunit_comp->kunit, data); + +memcpy (data, &tplg_tmpl_empty,sizeof (tplg_tmpl_empty)); +/* +* override size +* any value != struct size is wrong +*/ + data->header.size =sizeof (struct snd_soc_tplg_hdr ) +1 ; + + kunit_comp->fw.data = (u8 *)data; + kunit_comp->fw.size = size; + + kunit_comp->card.dev = test_dev, + kunit_comp->card.name ="kunit-card" , + kunit_comp->card.owner =THIS_MODULE , + kunit_comp->card.dai_link = kunit_dai_links, + kunit_comp->card.num_links =ARRAY_SIZE (kunit_dai_links), + kunit_comp->card.fully_routed = true, + +/* run test */ + ret =snd_soc_register_card (&kunit_comp->card); +if (ret !=0 && ret != -EPROBE_DEFER ) +KUNIT_FAIL (test,"Failed to register card" ); + + ret =snd_soc_component_initialize (&kunit_comp->comp, &test_component, test_dev); +KUNIT_EXPECT_EQ (test,0 , ret); + + ret =snd_soc_add_component (&kunit_comp->comp,NULL ,0 ); +KUNIT_EXPECT_EQ (test,0 , ret); + +/* cleanup */ + ret =snd_soc_unregister_card (&kunit_comp->card); +KUNIT_EXPECT_EQ (test,0 , ret); + +snd_soc_unregister_component (test_dev); +} + +// TEST CASE +// Test "empty" topology file, but with bad "payload_size" +// In theory we could loop through all possible bad values, but it takes too +// long, so just use the known wrong one +static void snd_soc_tplg_test_load_empty_tplg_bad_payload_size (struct kunit *test) +{ +struct kunit_soc_component *kunit_comp; +struct tplg_tmpl_001 *data; +int size; +int ret; + +/* prepare */ + kunit_comp =kunit_kzalloc (test,sizeof (*kunit_comp),GFP_KERNEL ); +KUNIT_EXPECT_NOT_ERR_OR_NULL (test, kunit_comp); + kunit_comp->kunit = test; + kunit_comp->expect = -EINVAL ;/* expect failure */ + + size =sizeof (tplg_tmpl_empty); + data =kunit_kzalloc (kunit_comp->kunit, size,GFP_KERNEL ); +KUNIT_EXPECT_NOT_ERR_OR_NULL (kunit_comp->kunit, data); + +memcpy (data, &tplg_tmpl_empty,sizeof (tplg_tmpl_empty)); +/* +* override payload size +* there is only explicit check for 0, so check with it, other values +* are handled by just not reading behind EOF +*/ + data->header.payload_size =0 ; + + kunit_comp->fw.data = (u8 *)data; + kunit_comp->fw.size = size; + + kunit_comp->card.dev = test_dev, + kunit_comp->card.name ="kunit-card" , + kunit_comp->card.owner =THIS_MODULE , + kunit_comp->card.dai_link = kunit_dai_links, + kunit_comp->card.num_links =ARRAY_SIZE (kunit_dai_links), + kunit_comp->card.fully_routed = true, + +/* run test */ + ret =snd_soc_register_card (&kunit_comp->card); +if (ret !=0 && ret != -EPROBE_DEFER ) +KUNIT_FAIL (test,"Failed to register card" ); + + ret =snd_soc_component_initialize (&kunit_comp->comp, &test_component, test_dev); +KUNIT_EXPECT_EQ (test,0 , ret); + + ret =snd_soc_add_component (&kunit_comp->comp,NULL ,0 ); +KUNIT_EXPECT_EQ (test,0 , ret); + +/* cleanup */ +snd_soc_unregister_component (test_dev); + + ret =snd_soc_unregister_card (&kunit_comp->card); +KUNIT_EXPECT_EQ (test,0 , ret); +} +/* ===== KUNIT MODULE DEFINITIONS =========================================== */ static struct kunit_case snd_soc_tplg_test_cases[] = {KUNIT_CASE (snd_soc_tplg_test_load_with_null_comp),KUNIT_CASE (snd_soc_tplg_test_load_with_null_ops),KUNIT_CASE (snd_soc_tplg_test_load_with_null_fw), +KUNIT_CASE (snd_soc_tplg_test_load_empty_tplg), +KUNIT_CASE (snd_soc_tplg_test_load_empty_tplg_bad_magic), +KUNIT_CASE (snd_soc_tplg_test_load_empty_tplg_bad_abi), +KUNIT_CASE (snd_soc_tplg_test_load_empty_tplg_bad_size), +KUNIT_CASE (snd_soc_tplg_test_load_empty_tplg_bad_payload_size), {} };
Signature · 1 line
2.25.1
PATCH 5/5 ASoC: topology: KUnit: Add KUnit tests passing topology with PCM to snd_soc_tplg_component_load
In reply to Amadeusz SławińskiIn order to ensure correct behaviour of topology API, add unit tests exercising topology functionality.
Add topology containing PCM template and tests for parsing it. Also adds test cases simulating modules reloads in case of separate drivers for card and component.
--- sound/soc/soc-topology-test.c | 231 ++++++++++++++++++++++++++++++++++ 1 file changed, 231 insertions(+)
sound/soc/soc-topology-test.c +231 -0
@@ -138,6 +138,79 @@ static struct tplg_tmpl_001 tplg_tmpl_empty = { }, }; +// Structural representation of topology containing SectionPCM + +struct tplg_tmpl_002 { +struct snd_soc_tplg_hdr header; +struct snd_soc_tplg_manifest manifest; +struct snd_soc_tplg_hdr pcm_header; +struct snd_soc_tplg_pcm pcm; +} __packed; + +static struct tplg_tmpl_002 tplg_tmpl_with_pcm = { + .header = { + .magic =SND_SOC_TPLG_MAGIC , + .abi =5 , + .version =0 , + .type =SND_SOC_TPLG_TYPE_MANIFEST , + .size =sizeof (struct snd_soc_tplg_hdr ), + .vendor_type =0 , + .payload_size =sizeof (struct snd_soc_tplg_manifest ), + .index =0 , + .count =1 , + }, + .manifest = { + .size =sizeof (struct snd_soc_tplg_manifest ), + .pcm_elems =1 , +/* rest of fields is 0 */ + }, + .pcm_header = { + .magic =SND_SOC_TPLG_MAGIC , + .abi =5 , + .version =0 , + .type =SND_SOC_TPLG_TYPE_PCM , + .size =sizeof (struct snd_soc_tplg_hdr ), + .vendor_type =0 , + .payload_size =sizeof (struct snd_soc_tplg_pcm ), + .index =0 , + .count =1 , + }, + .pcm = { + .size =sizeof (struct snd_soc_tplg_pcm ), + .pcm_name ="KUNIT Audio" , + .dai_name ="kunit-audio-dai" , + .pcm_id =0 , + .dai_id =0 , + .playback =1 , + .capture =1 , + .compress =0 , + .stream = { + [0 ] = { + .channels =2 , + }, + [1 ] = { + .channels =2 , + }, + }, + .num_streams =0 , + .caps = { + [0 ] = { + .name ="kunit-audio-playback" , + .channels_min =2 , + .channels_max =2 , + }, + [1 ] = { + .name ="kunit-audio-capture" , + .channels_min =2 , + .channels_max =2 , + }, + }, + .flag_mask =0 , + .flags =0 , + .priv = {0 }, + }, +}; +/* ===== TEST CASES ========================================================= */ // TEST CASE
@@ -586,6 +659,161 @@ static void snd_soc_tplg_test_load_empty_tplg_bad_payload_size(struct kunit *tesKUNIT_EXPECT_EQ (test,0 , ret); } +// TEST CASE +// Test passing topology file with PCM definition +static void snd_soc_tplg_test_load_pcm_tplg (struct kunit *test) +{ +struct kunit_soc_component *kunit_comp; +u8 *data; +int size; +int ret; + +/* prepare */ + kunit_comp =kunit_kzalloc (test,sizeof (*kunit_comp),GFP_KERNEL ); +KUNIT_EXPECT_NOT_ERR_OR_NULL (test, kunit_comp); + kunit_comp->kunit = test; + kunit_comp->expect =0 ;/* expect success */ + + size =sizeof (tplg_tmpl_with_pcm); + data =kunit_kzalloc (kunit_comp->kunit, size,GFP_KERNEL ); +KUNIT_EXPECT_NOT_ERR_OR_NULL (kunit_comp->kunit, data); + +memcpy (data, &tplg_tmpl_with_pcm,sizeof (tplg_tmpl_with_pcm)); + + kunit_comp->fw.data = data; + kunit_comp->fw.size = size; + + kunit_comp->card.dev = test_dev, + kunit_comp->card.name ="kunit-card" , + kunit_comp->card.owner =THIS_MODULE , + kunit_comp->card.dai_link = kunit_dai_links, + kunit_comp->card.num_links =ARRAY_SIZE (kunit_dai_links), + kunit_comp->card.fully_routed = true, + +/* run test */ + ret =snd_soc_register_card (&kunit_comp->card); +if (ret !=0 && ret != -EPROBE_DEFER ) +KUNIT_FAIL (test,"Failed to register card" ); + + ret =snd_soc_component_initialize (&kunit_comp->comp, &test_component, test_dev); +KUNIT_EXPECT_EQ (test,0 , ret); + + ret =snd_soc_add_component (&kunit_comp->comp,NULL ,0 ); +KUNIT_EXPECT_EQ (test,0 , ret); + +snd_soc_unregister_component (test_dev); + +/* cleanup */ + ret =snd_soc_unregister_card (&kunit_comp->card); +KUNIT_EXPECT_EQ (test,0 , ret); +} + +// TEST CASE +// Test passing topology file with PCM definition +// with component reload +static void snd_soc_tplg_test_load_pcm_tplg_reload_comp (struct kunit *test) +{ +struct kunit_soc_component *kunit_comp; +u8 *data; +int size; +int ret; +int i; + +/* prepare */ + kunit_comp =kunit_kzalloc (test,sizeof (*kunit_comp),GFP_KERNEL ); +KUNIT_EXPECT_NOT_ERR_OR_NULL (test, kunit_comp); + kunit_comp->kunit = test; + kunit_comp->expect =0 ;/* expect success */ + + size =sizeof (tplg_tmpl_with_pcm); + data =kunit_kzalloc (kunit_comp->kunit, size,GFP_KERNEL ); +KUNIT_EXPECT_NOT_ERR_OR_NULL (kunit_comp->kunit, data); + +memcpy (data, &tplg_tmpl_with_pcm,sizeof (tplg_tmpl_with_pcm)); + + kunit_comp->fw.data = data; + kunit_comp->fw.size = size; + + kunit_comp->card.dev = test_dev, + kunit_comp->card.name ="kunit-card" , + kunit_comp->card.owner =THIS_MODULE , + kunit_comp->card.dai_link = kunit_dai_links, + kunit_comp->card.num_links =ARRAY_SIZE (kunit_dai_links), + kunit_comp->card.fully_routed = true, + +/* run test */ + ret =snd_soc_register_card (&kunit_comp->card); +if (ret !=0 && ret != -EPROBE_DEFER ) +KUNIT_FAIL (test,"Failed to register card" ); + +for (i =0 ; i <100 ; i++) { + ret =snd_soc_component_initialize (&kunit_comp->comp, &test_component, test_dev); +KUNIT_EXPECT_EQ (test,0 , ret); + + ret =snd_soc_add_component (&kunit_comp->comp,NULL ,0 ); +KUNIT_EXPECT_EQ (test,0 , ret); + +snd_soc_unregister_component (test_dev); + } + +/* cleanup */ + ret =snd_soc_unregister_card (&kunit_comp->card); +KUNIT_EXPECT_EQ (test,0 , ret); +} + +// TEST CASE +// Test passing topology file with PCM definition +// with card reload +static void snd_soc_tplg_test_load_pcm_tplg_reload_card (struct kunit *test) +{ +struct kunit_soc_component *kunit_comp; +u8 *data; +int size; +int ret; +int i; + +/* prepare */ + kunit_comp =kunit_kzalloc (test,sizeof (*kunit_comp),GFP_KERNEL ); +KUNIT_EXPECT_NOT_ERR_OR_NULL (test, kunit_comp); + kunit_comp->kunit = test; + kunit_comp->expect =0 ;/* expect success */ + + size =sizeof (tplg_tmpl_with_pcm); + data =kunit_kzalloc (kunit_comp->kunit, size,GFP_KERNEL ); +KUNIT_EXPECT_NOT_ERR_OR_NULL (kunit_comp->kunit, data); + +memcpy (data, &tplg_tmpl_with_pcm,sizeof (tplg_tmpl_with_pcm)); + + kunit_comp->fw.data = data; + kunit_comp->fw.size = size; + + kunit_comp->card.dev = test_dev, + kunit_comp->card.name ="kunit-card" , + kunit_comp->card.owner =THIS_MODULE , + kunit_comp->card.dai_link = kunit_dai_links, + kunit_comp->card.num_links =ARRAY_SIZE (kunit_dai_links), + kunit_comp->card.fully_routed = true, + +/* run test */ + ret =snd_soc_component_initialize (&kunit_comp->comp, &test_component, test_dev); +KUNIT_EXPECT_EQ (test,0 , ret); + + ret =snd_soc_add_component (&kunit_comp->comp,NULL ,0 ); +KUNIT_EXPECT_EQ (test,0 , ret); + +for (i =0 ; i <100 ; i++) { + ret =snd_soc_register_card (&kunit_comp->card); +if (ret !=0 && ret != -EPROBE_DEFER ) +KUNIT_FAIL (test,"Failed to register card" ); + + ret =snd_soc_unregister_card (&kunit_comp->card); +KUNIT_EXPECT_EQ (test,0 , ret); + } + +/* cleanup */ +snd_soc_unregister_component (test_dev); +} +/* ===== KUNIT MODULE DEFINITIONS =========================================== */ static struct kunit_case snd_soc_tplg_test_cases[] = {
@@ -597,6 +825,9 @@ static struct kunit_case snd_soc_tplg_test_cases[] = {KUNIT_CASE (snd_soc_tplg_test_load_empty_tplg_bad_abi),KUNIT_CASE (snd_soc_tplg_test_load_empty_tplg_bad_size),KUNIT_CASE (snd_soc_tplg_test_load_empty_tplg_bad_payload_size), +KUNIT_CASE (snd_soc_tplg_test_load_pcm_tplg), +KUNIT_CASE (snd_soc_tplg_test_load_pcm_tplg_reload_comp), + KUNIT_CASE(snd_soc_tplg_test_load_pcm_tplg_reload_card), {} };
Signature · 1 line
2.25.1
Mark Brown
maintainerRe: (subset) [PATCH 0/5] Add KUNIT tests for ASoC topology
In reply to Amadeusz SławińskiQuoted 11 lines · Amadeusz Sławiński
This series adds unit tests for ASoC topology.
First fix problems found when developing and running test cases and then add tests implementation.
Tests themselves are quite simple and just call
snd_soc_tplg_component_load()with various parameters and check the result. Tests themselves are described in more detail in commits adding them.[...]
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-next
Thanks!
[1/5] ASoC: topology: Properly unregister DAI on removal
commit: fc4cb1e15f0c66f2e37314349dc4a82bd946fbb1
All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying to this mail.
Thanks,
Mark
Mark Brown
maintainerRe: (subset) [PATCH 0/5] Add KUNIT tests for ASoC topology
In reply to Amadeusz SławińskiQuoted 11 lines · Amadeusz Sławiński
This series adds unit tests for ASoC topology.
First fix problems found when developing and running test cases and then add tests implementation.
Tests themselves are quite simple and just call
snd_soc_tplg_component_load()with various parameters and check the result. Tests themselves are described in more detail in commits adding them.[...]
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-next
Thanks!
[3/5] ASoC: topology: KUnit: Add KUnit tests passing various arguments to snd_soc_tplg_component_load
commit: d52bbf747cfa8a2988289009241214a84982cc7d
[4/5] ASoC: topology: KUnit: Add KUnit tests passing empty topology with variants to snd_soc_tplg_component_load
commit: cec9128dfcf9101f903470e43d46278e5b07ef24
[5/5] ASoC: topology: KUnit: Add KUnit tests passing topology with PCM to snd_soc_tplg_component_load
commit: 3ad8c8e9efc53d14d928b84aabe1a27dd5d3171bAll being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying to this mail.
Thanks,
Mark