summaryrefslogtreecommitdiff
path: root/sound/soc/soc-topology-test.c
blob: 7276ed95d83db8a484ee5914febaa858aef54946 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// SPDX-License-Identifier: GPL-2.0-only
/*
 * soc-topology-test.c  --  ALSA SoC Topology Kernel Unit Tests
 *
 * Copyright(c) 2021 Intel Corporation. All rights reserved.
 */

#include <linux/firmware.h>
#include <linux/random.h>
#include <sound/core.h>
#include <sound/soc.h>
#include <sound/soc-topology.h>
#include <kunit/test.h>

/* ===== HELPER FUNCTIONS =================================================== */

/*
 * snd_soc_component needs device to operate on (primarily for prints), create
 * fake one, as we don't register with PCI or anything else
 * device_driver name is used in some of the prints (fmt_single_name) so
 * we also mock up minimal one
 */
static struct device *test_dev;

static struct device_driver test_drv = {
	.name = "sound-soc-topology-test-driver",
};

static int snd_soc_tplg_test_init(struct kunit *test)
{
	test_dev = root_device_register("sound-soc-topology-test");
	test_dev = get_device(test_dev);
	if (!test_dev)
		return -ENODEV;

	test_dev->driver = &test_drv;

	return 0;
}

static void snd_soc_tplg_test_exit(struct kunit *test)
{
	put_device(test_dev);
	root_device_unregister(test_dev);
}

/*
 * helper struct we use when registering component, as we load topology during
 * component probe, we need to pass struct kunit somehow to probe function, so
 * we can report test result
 */
struct kunit_soc_component {
	struct kunit *kunit;
	int expect; /* what result we expect when loading topology */
	struct snd_soc_component comp;
	struct snd_soc_card card;
	struct firmware fw;
};

static int d_probe(struct snd_soc_component *component)
{
	struct kunit_soc_component *kunit_comp =
			container_of(component, struct kunit_soc_component, comp);
	int ret;

	ret = snd_soc_tplg_component_load(component, NULL, &kunit_comp->fw);
	KUNIT_EXPECT_EQ_MSG(kunit_comp->kunit, kunit_comp->expect, ret,
			    "Failed topology load");

	return 0;
}

static void d_remove(struct snd_soc_component *component)
{
	struct kunit_soc_component *kunit_comp =
			container_of(component, struct kunit_soc_component, comp);
	int ret;

	ret = snd_soc_tplg_component_remove(component);
	KUNIT_EXPECT_EQ(kunit_comp->kunit, 0, ret);
}

/*
 * ASoC minimal boiler plate
 */
SND_SOC_DAILINK_DEF(dummy, DAILINK_COMP_ARRAY(COMP_DUMMY()));

SND_SOC_DAILINK_DEF(platform, DAILINK_COMP_ARRAY(COMP_PLATFORM("sound-soc-topology-test")));

static struct snd_soc_dai_link kunit_dai_links[] = {
	{
		.name = "KUNIT Audio Port",
		.id = 0,
		.stream_name = "Audio Playback/Capture",
		.nonatomic = 1,
		.dynamic = 1,
		.trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
		.dpcm_playback = 1,
		.dpcm_capture = 1,
		SND_SOC_DAILINK_REG(dummy, dummy, platform),
	},
};

static const struct snd_soc_component_driver test_component = {
	.name = "sound-soc-topology-test",
	.probe = d_probe,
	.remove = d_remove,
	.non_legacy_dai_naming = 1,
};

/* ===== TEST CASES ========================================================= */

// TEST CASE
// Test passing NULL component as parameter to snd_soc_tplg_component_load

/*
 * need to override generic probe function with one using NULL when calling
 * topology load during component initialization, we don't need .remove
 * handler as load should fail
 */
static int d_probe_null_comp(struct snd_soc_component *component)
{
	struct kunit_soc_component *kunit_comp =
			container_of(component, struct kunit_soc_component, comp);
	int ret;

	/* instead of passing component pointer as first argument, pass NULL here */
	ret = snd_soc_tplg_component_load(NULL, NULL, &kunit_comp->fw);
	KUNIT_EXPECT_EQ_MSG(kunit_comp->kunit, kunit_comp->expect, ret,
			    "Failed topology load");

	return 0;
}

static const struct snd_soc_component_driver test_component_null_comp = {
	.name = "sound-soc-topology-test",
	.probe = d_probe_null_comp,
	.non_legacy_dai_naming = 1,
};

static void snd_soc_tplg_test_load_with_null_comp(struct kunit *test)
{
	struct kunit_soc_component *kunit_comp;
	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 */

	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_null_comp, 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 passing NULL ops as parameter to snd_soc_tplg_component_load

/*
 * NULL ops is default case, we pass empty topology (fw), so we don't have
 * anything to parse and just do nothing, which results in return 0; from
 * calling soc_tplg_dapm_complete in soc_tplg_process_headers
 */
static void snd_soc_tplg_test_load_with_null_ops(struct kunit *test)
{
	struct kunit_soc_component *kunit_comp;
	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 */

	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 passing NULL fw as parameter to snd_soc_tplg_component_load

/*
 * need to override generic probe function with one using NULL pointer to fw
 * when calling topology load during component initialization, we don't need
 * .remove handler as load should fail
 */
static int d_probe_null_fw(struct snd_soc_component *component)
{
	struct kunit_soc_component *kunit_comp =
			container_of(component, struct kunit_soc_component, comp);
	int ret;

	/* instead of passing fw pointer as third argument, pass NULL here */
	ret = snd_soc_tplg_component_load(component, NULL, NULL);
	KUNIT_EXPECT_EQ_MSG(kunit_comp->kunit, kunit_comp->expect, ret,
			    "Failed topology load");

	return 0;
}

static const struct snd_soc_component_driver test_component_null_fw = {
	.name = "sound-soc-topology-test",
	.probe = d_probe_null_fw,
	.non_legacy_dai_naming = 1,
};

static void snd_soc_tplg_test_load_with_null_fw(struct kunit *test)
{
	struct kunit_soc_component *kunit_comp;
	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 */

	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_null_fw, 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);
}

/* ===== 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),
	{}
};

static struct kunit_suite snd_soc_tplg_test_suite = {
	.name = "snd_soc_tplg_test",
	.init = snd_soc_tplg_test_init,
	.exit = snd_soc_tplg_test_exit,
	.test_cases = snd_soc_tplg_test_cases,
};

kunit_test_suites(&snd_soc_tplg_test_suite);

MODULE_LICENSE("GPL");