soc-topology-test.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * soc-topology-test.c -- ALSA SoC Topology Kernel Unit Tests
  4. *
  5. * Copyright(c) 2021 Intel Corporation. All rights reserved.
  6. */
  7. #include <linux/firmware.h>
  8. #include <sound/core.h>
  9. #include <sound/soc.h>
  10. #include <sound/soc-topology.h>
  11. #include <kunit/test.h>
  12. /* ===== HELPER FUNCTIONS =================================================== */
  13. /*
  14. * snd_soc_component needs device to operate on (primarily for prints), create
  15. * fake one, as we don't register with PCI or anything else
  16. * device_driver name is used in some of the prints (fmt_single_name) so
  17. * we also mock up minimal one
  18. */
  19. static struct device *test_dev;
  20. static struct device_driver test_drv = {
  21. .name = "sound-soc-topology-test-driver",
  22. };
  23. static int snd_soc_tplg_test_init(struct kunit *test)
  24. {
  25. test_dev = root_device_register("sound-soc-topology-test");
  26. test_dev = get_device(test_dev);
  27. if (!test_dev)
  28. return -ENODEV;
  29. test_dev->driver = &test_drv;
  30. return 0;
  31. }
  32. static void snd_soc_tplg_test_exit(struct kunit *test)
  33. {
  34. put_device(test_dev);
  35. root_device_unregister(test_dev);
  36. }
  37. /*
  38. * helper struct we use when registering component, as we load topology during
  39. * component probe, we need to pass struct kunit somehow to probe function, so
  40. * we can report test result
  41. */
  42. struct kunit_soc_component {
  43. struct kunit *kunit;
  44. int expect; /* what result we expect when loading topology */
  45. struct snd_soc_component comp;
  46. struct snd_soc_card card;
  47. struct firmware fw;
  48. };
  49. static int d_probe(struct snd_soc_component *component)
  50. {
  51. struct kunit_soc_component *kunit_comp =
  52. container_of(component, struct kunit_soc_component, comp);
  53. int ret;
  54. ret = snd_soc_tplg_component_load(component, NULL, &kunit_comp->fw);
  55. KUNIT_EXPECT_EQ_MSG(kunit_comp->kunit, kunit_comp->expect, ret,
  56. "Failed topology load");
  57. return 0;
  58. }
  59. static void d_remove(struct snd_soc_component *component)
  60. {
  61. struct kunit_soc_component *kunit_comp =
  62. container_of(component, struct kunit_soc_component, comp);
  63. int ret;
  64. ret = snd_soc_tplg_component_remove(component);
  65. KUNIT_EXPECT_EQ(kunit_comp->kunit, 0, ret);
  66. }
  67. /*
  68. * ASoC minimal boiler plate
  69. */
  70. SND_SOC_DAILINK_DEF(dummy, DAILINK_COMP_ARRAY(COMP_DUMMY()));
  71. SND_SOC_DAILINK_DEF(platform, DAILINK_COMP_ARRAY(COMP_PLATFORM("sound-soc-topology-test")));
  72. static struct snd_soc_dai_link kunit_dai_links[] = {
  73. {
  74. .name = "KUNIT Audio Port",
  75. .id = 0,
  76. .stream_name = "Audio Playback/Capture",
  77. .nonatomic = 1,
  78. .dynamic = 1,
  79. .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
  80. .dpcm_playback = 1,
  81. .dpcm_capture = 1,
  82. SND_SOC_DAILINK_REG(dummy, dummy, platform),
  83. },
  84. };
  85. static const struct snd_soc_component_driver test_component = {
  86. .name = "sound-soc-topology-test",
  87. .probe = d_probe,
  88. .remove = d_remove,
  89. };
  90. /* ===== TOPOLOGY TEMPLATES ================================================= */
  91. // Structural representation of topology which can be generated with:
  92. // $ touch empty
  93. // $ alsatplg -c empty -o empty.tplg
  94. // $ xxd -i empty.tplg
  95. struct tplg_tmpl_001 {
  96. struct snd_soc_tplg_hdr header;
  97. struct snd_soc_tplg_manifest manifest;
  98. } __packed;
  99. static struct tplg_tmpl_001 tplg_tmpl_empty = {
  100. .header = {
  101. .magic = cpu_to_le32(SND_SOC_TPLG_MAGIC),
  102. .abi = cpu_to_le32(5),
  103. .version = 0,
  104. .type = cpu_to_le32(SND_SOC_TPLG_TYPE_MANIFEST),
  105. .size = cpu_to_le32(sizeof(struct snd_soc_tplg_hdr)),
  106. .vendor_type = 0,
  107. .payload_size = cpu_to_le32(sizeof(struct snd_soc_tplg_manifest)),
  108. .index = 0,
  109. .count = cpu_to_le32(1),
  110. },
  111. .manifest = {
  112. .size = cpu_to_le32(sizeof(struct snd_soc_tplg_manifest)),
  113. /* rest of fields is 0 */
  114. },
  115. };
  116. // Structural representation of topology containing SectionPCM
  117. struct tplg_tmpl_002 {
  118. struct snd_soc_tplg_hdr header;
  119. struct snd_soc_tplg_manifest manifest;
  120. struct snd_soc_tplg_hdr pcm_header;
  121. struct snd_soc_tplg_pcm pcm;
  122. } __packed;
  123. static struct tplg_tmpl_002 tplg_tmpl_with_pcm = {
  124. .header = {
  125. .magic = cpu_to_le32(SND_SOC_TPLG_MAGIC),
  126. .abi = cpu_to_le32(5),
  127. .version = 0,
  128. .type = cpu_to_le32(SND_SOC_TPLG_TYPE_MANIFEST),
  129. .size = cpu_to_le32(sizeof(struct snd_soc_tplg_hdr)),
  130. .vendor_type = 0,
  131. .payload_size = cpu_to_le32(sizeof(struct snd_soc_tplg_manifest)),
  132. .index = 0,
  133. .count = cpu_to_le32(1),
  134. },
  135. .manifest = {
  136. .size = cpu_to_le32(sizeof(struct snd_soc_tplg_manifest)),
  137. .pcm_elems = cpu_to_le32(1),
  138. /* rest of fields is 0 */
  139. },
  140. .pcm_header = {
  141. .magic = cpu_to_le32(SND_SOC_TPLG_MAGIC),
  142. .abi = cpu_to_le32(5),
  143. .version = 0,
  144. .type = cpu_to_le32(SND_SOC_TPLG_TYPE_PCM),
  145. .size = cpu_to_le32(sizeof(struct snd_soc_tplg_hdr)),
  146. .vendor_type = 0,
  147. .payload_size = cpu_to_le32(sizeof(struct snd_soc_tplg_pcm)),
  148. .index = 0,
  149. .count = cpu_to_le32(1),
  150. },
  151. .pcm = {
  152. .size = cpu_to_le32(sizeof(struct snd_soc_tplg_pcm)),
  153. .pcm_name = "KUNIT Audio",
  154. .dai_name = "kunit-audio-dai",
  155. .pcm_id = 0,
  156. .dai_id = 0,
  157. .playback = cpu_to_le32(1),
  158. .capture = cpu_to_le32(1),
  159. .compress = 0,
  160. .stream = {
  161. [0] = {
  162. .channels = cpu_to_le32(2),
  163. },
  164. [1] = {
  165. .channels = cpu_to_le32(2),
  166. },
  167. },
  168. .num_streams = 0,
  169. .caps = {
  170. [0] = {
  171. .name = "kunit-audio-playback",
  172. .channels_min = cpu_to_le32(2),
  173. .channels_max = cpu_to_le32(2),
  174. },
  175. [1] = {
  176. .name = "kunit-audio-capture",
  177. .channels_min = cpu_to_le32(2),
  178. .channels_max = cpu_to_le32(2),
  179. },
  180. },
  181. .flag_mask = 0,
  182. .flags = 0,
  183. .priv = { 0 },
  184. },
  185. };
  186. /* ===== TEST CASES ========================================================= */
  187. // TEST CASE
  188. // Test passing NULL component as parameter to snd_soc_tplg_component_load
  189. /*
  190. * need to override generic probe function with one using NULL when calling
  191. * topology load during component initialization, we don't need .remove
  192. * handler as load should fail
  193. */
  194. static int d_probe_null_comp(struct snd_soc_component *component)
  195. {
  196. struct kunit_soc_component *kunit_comp =
  197. container_of(component, struct kunit_soc_component, comp);
  198. int ret;
  199. /* instead of passing component pointer as first argument, pass NULL here */
  200. ret = snd_soc_tplg_component_load(NULL, NULL, &kunit_comp->fw);
  201. KUNIT_EXPECT_EQ_MSG(kunit_comp->kunit, kunit_comp->expect, ret,
  202. "Failed topology load");
  203. return 0;
  204. }
  205. static const struct snd_soc_component_driver test_component_null_comp = {
  206. .name = "sound-soc-topology-test",
  207. .probe = d_probe_null_comp,
  208. };
  209. static void snd_soc_tplg_test_load_with_null_comp(struct kunit *test)
  210. {
  211. struct kunit_soc_component *kunit_comp;
  212. int ret;
  213. /* prepare */
  214. kunit_comp = kunit_kzalloc(test, sizeof(*kunit_comp), GFP_KERNEL);
  215. KUNIT_EXPECT_NOT_ERR_OR_NULL(test, kunit_comp);
  216. kunit_comp->kunit = test;
  217. kunit_comp->expect = -EINVAL; /* expect failure */
  218. kunit_comp->card.dev = test_dev,
  219. kunit_comp->card.name = "kunit-card",
  220. kunit_comp->card.owner = THIS_MODULE,
  221. kunit_comp->card.dai_link = kunit_dai_links,
  222. kunit_comp->card.num_links = ARRAY_SIZE(kunit_dai_links),
  223. kunit_comp->card.fully_routed = true,
  224. /* run test */
  225. ret = snd_soc_register_card(&kunit_comp->card);
  226. if (ret != 0 && ret != -EPROBE_DEFER)
  227. KUNIT_FAIL(test, "Failed to register card");
  228. ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component_null_comp, test_dev);
  229. KUNIT_EXPECT_EQ(test, 0, ret);
  230. ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0);
  231. KUNIT_EXPECT_EQ(test, 0, ret);
  232. /* cleanup */
  233. snd_soc_unregister_card(&kunit_comp->card);
  234. snd_soc_unregister_component(test_dev);
  235. }
  236. // TEST CASE
  237. // Test passing NULL ops as parameter to snd_soc_tplg_component_load
  238. /*
  239. * NULL ops is default case, we pass empty topology (fw), so we don't have
  240. * anything to parse and just do nothing, which results in return 0; from
  241. * calling soc_tplg_dapm_complete in soc_tplg_process_headers
  242. */
  243. static void snd_soc_tplg_test_load_with_null_ops(struct kunit *test)
  244. {
  245. struct kunit_soc_component *kunit_comp;
  246. int ret;
  247. /* prepare */
  248. kunit_comp = kunit_kzalloc(test, sizeof(*kunit_comp), GFP_KERNEL);
  249. KUNIT_EXPECT_NOT_ERR_OR_NULL(test, kunit_comp);
  250. kunit_comp->kunit = test;
  251. kunit_comp->expect = 0; /* expect success */
  252. kunit_comp->card.dev = test_dev,
  253. kunit_comp->card.name = "kunit-card",
  254. kunit_comp->card.owner = THIS_MODULE,
  255. kunit_comp->card.dai_link = kunit_dai_links,
  256. kunit_comp->card.num_links = ARRAY_SIZE(kunit_dai_links),
  257. kunit_comp->card.fully_routed = true,
  258. /* run test */
  259. ret = snd_soc_register_card(&kunit_comp->card);
  260. if (ret != 0 && ret != -EPROBE_DEFER)
  261. KUNIT_FAIL(test, "Failed to register card");
  262. ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component, test_dev);
  263. KUNIT_EXPECT_EQ(test, 0, ret);
  264. ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0);
  265. KUNIT_EXPECT_EQ(test, 0, ret);
  266. /* cleanup */
  267. snd_soc_unregister_card(&kunit_comp->card);
  268. snd_soc_unregister_component(test_dev);
  269. }
  270. // TEST CASE
  271. // Test passing NULL fw as parameter to snd_soc_tplg_component_load
  272. /*
  273. * need to override generic probe function with one using NULL pointer to fw
  274. * when calling topology load during component initialization, we don't need
  275. * .remove handler as load should fail
  276. */
  277. static int d_probe_null_fw(struct snd_soc_component *component)
  278. {
  279. struct kunit_soc_component *kunit_comp =
  280. container_of(component, struct kunit_soc_component, comp);
  281. int ret;
  282. /* instead of passing fw pointer as third argument, pass NULL here */
  283. ret = snd_soc_tplg_component_load(component, NULL, NULL);
  284. KUNIT_EXPECT_EQ_MSG(kunit_comp->kunit, kunit_comp->expect, ret,
  285. "Failed topology load");
  286. return 0;
  287. }
  288. static const struct snd_soc_component_driver test_component_null_fw = {
  289. .name = "sound-soc-topology-test",
  290. .probe = d_probe_null_fw,
  291. };
  292. static void snd_soc_tplg_test_load_with_null_fw(struct kunit *test)
  293. {
  294. struct kunit_soc_component *kunit_comp;
  295. int ret;
  296. /* prepare */
  297. kunit_comp = kunit_kzalloc(test, sizeof(*kunit_comp), GFP_KERNEL);
  298. KUNIT_EXPECT_NOT_ERR_OR_NULL(test, kunit_comp);
  299. kunit_comp->kunit = test;
  300. kunit_comp->expect = -EINVAL; /* expect failure */
  301. kunit_comp->card.dev = test_dev,
  302. kunit_comp->card.name = "kunit-card",
  303. kunit_comp->card.owner = THIS_MODULE,
  304. kunit_comp->card.dai_link = kunit_dai_links,
  305. kunit_comp->card.num_links = ARRAY_SIZE(kunit_dai_links),
  306. kunit_comp->card.fully_routed = true,
  307. /* run test */
  308. ret = snd_soc_register_card(&kunit_comp->card);
  309. if (ret != 0 && ret != -EPROBE_DEFER)
  310. KUNIT_FAIL(test, "Failed to register card");
  311. ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component_null_fw, test_dev);
  312. KUNIT_EXPECT_EQ(test, 0, ret);
  313. ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0);
  314. KUNIT_EXPECT_EQ(test, 0, ret);
  315. /* cleanup */
  316. snd_soc_unregister_card(&kunit_comp->card);
  317. snd_soc_unregister_component(test_dev);
  318. }
  319. // TEST CASE
  320. // Test passing "empty" topology file
  321. static void snd_soc_tplg_test_load_empty_tplg(struct kunit *test)
  322. {
  323. struct kunit_soc_component *kunit_comp;
  324. struct tplg_tmpl_001 *data;
  325. int size;
  326. int ret;
  327. /* prepare */
  328. kunit_comp = kunit_kzalloc(test, sizeof(*kunit_comp), GFP_KERNEL);
  329. KUNIT_EXPECT_NOT_ERR_OR_NULL(test, kunit_comp);
  330. kunit_comp->kunit = test;
  331. kunit_comp->expect = 0; /* expect success */
  332. size = sizeof(tplg_tmpl_empty);
  333. data = kunit_kzalloc(kunit_comp->kunit, size, GFP_KERNEL);
  334. KUNIT_EXPECT_NOT_ERR_OR_NULL(kunit_comp->kunit, data);
  335. memcpy(data, &tplg_tmpl_empty, sizeof(tplg_tmpl_empty));
  336. kunit_comp->fw.data = (u8 *)data;
  337. kunit_comp->fw.size = size;
  338. kunit_comp->card.dev = test_dev,
  339. kunit_comp->card.name = "kunit-card",
  340. kunit_comp->card.owner = THIS_MODULE,
  341. kunit_comp->card.dai_link = kunit_dai_links,
  342. kunit_comp->card.num_links = ARRAY_SIZE(kunit_dai_links),
  343. kunit_comp->card.fully_routed = true,
  344. /* run test */
  345. ret = snd_soc_register_card(&kunit_comp->card);
  346. if (ret != 0 && ret != -EPROBE_DEFER)
  347. KUNIT_FAIL(test, "Failed to register card");
  348. ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component, test_dev);
  349. KUNIT_EXPECT_EQ(test, 0, ret);
  350. ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0);
  351. KUNIT_EXPECT_EQ(test, 0, ret);
  352. /* cleanup */
  353. snd_soc_unregister_card(&kunit_comp->card);
  354. snd_soc_unregister_component(test_dev);
  355. }
  356. // TEST CASE
  357. // Test "empty" topology file, but with bad "magic"
  358. // In theory we could loop through all possible bad values, but it takes too
  359. // long, so just use SND_SOC_TPLG_MAGIC + 1
  360. static void snd_soc_tplg_test_load_empty_tplg_bad_magic(struct kunit *test)
  361. {
  362. struct kunit_soc_component *kunit_comp;
  363. struct tplg_tmpl_001 *data;
  364. int size;
  365. int ret;
  366. /* prepare */
  367. kunit_comp = kunit_kzalloc(test, sizeof(*kunit_comp), GFP_KERNEL);
  368. KUNIT_EXPECT_NOT_ERR_OR_NULL(test, kunit_comp);
  369. kunit_comp->kunit = test;
  370. kunit_comp->expect = -EINVAL; /* expect failure */
  371. size = sizeof(tplg_tmpl_empty);
  372. data = kunit_kzalloc(kunit_comp->kunit, size, GFP_KERNEL);
  373. KUNIT_EXPECT_NOT_ERR_OR_NULL(kunit_comp->kunit, data);
  374. memcpy(data, &tplg_tmpl_empty, sizeof(tplg_tmpl_empty));
  375. /*
  376. * override abi
  377. * any value != magic number is wrong
  378. */
  379. data->header.magic = cpu_to_le32(SND_SOC_TPLG_MAGIC + 1);
  380. kunit_comp->fw.data = (u8 *)data;
  381. kunit_comp->fw.size = size;
  382. kunit_comp->card.dev = test_dev,
  383. kunit_comp->card.name = "kunit-card",
  384. kunit_comp->card.owner = THIS_MODULE,
  385. kunit_comp->card.dai_link = kunit_dai_links,
  386. kunit_comp->card.num_links = ARRAY_SIZE(kunit_dai_links),
  387. kunit_comp->card.fully_routed = true,
  388. /* run test */
  389. ret = snd_soc_register_card(&kunit_comp->card);
  390. if (ret != 0 && ret != -EPROBE_DEFER)
  391. KUNIT_FAIL(test, "Failed to register card");
  392. ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component, test_dev);
  393. KUNIT_EXPECT_EQ(test, 0, ret);
  394. ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0);
  395. KUNIT_EXPECT_EQ(test, 0, ret);
  396. /* cleanup */
  397. snd_soc_unregister_card(&kunit_comp->card);
  398. snd_soc_unregister_component(test_dev);
  399. }
  400. // TEST CASE
  401. // Test "empty" topology file, but with bad "abi"
  402. // In theory we could loop through all possible bad values, but it takes too
  403. // long, so just use SND_SOC_TPLG_ABI_VERSION + 1
  404. static void snd_soc_tplg_test_load_empty_tplg_bad_abi(struct kunit *test)
  405. {
  406. struct kunit_soc_component *kunit_comp;
  407. struct tplg_tmpl_001 *data;
  408. int size;
  409. int ret;
  410. /* prepare */
  411. kunit_comp = kunit_kzalloc(test, sizeof(*kunit_comp), GFP_KERNEL);
  412. KUNIT_EXPECT_NOT_ERR_OR_NULL(test, kunit_comp);
  413. kunit_comp->kunit = test;
  414. kunit_comp->expect = -EINVAL; /* expect failure */
  415. size = sizeof(tplg_tmpl_empty);
  416. data = kunit_kzalloc(kunit_comp->kunit, size, GFP_KERNEL);
  417. KUNIT_EXPECT_NOT_ERR_OR_NULL(kunit_comp->kunit, data);
  418. memcpy(data, &tplg_tmpl_empty, sizeof(tplg_tmpl_empty));
  419. /*
  420. * override abi
  421. * any value != accepted range is wrong
  422. */
  423. data->header.abi = cpu_to_le32(SND_SOC_TPLG_ABI_VERSION + 1);
  424. kunit_comp->fw.data = (u8 *)data;
  425. kunit_comp->fw.size = size;
  426. kunit_comp->card.dev = test_dev,
  427. kunit_comp->card.name = "kunit-card",
  428. kunit_comp->card.owner = THIS_MODULE,
  429. kunit_comp->card.dai_link = kunit_dai_links,
  430. kunit_comp->card.num_links = ARRAY_SIZE(kunit_dai_links),
  431. kunit_comp->card.fully_routed = true,
  432. /* run test */
  433. ret = snd_soc_register_card(&kunit_comp->card);
  434. if (ret != 0 && ret != -EPROBE_DEFER)
  435. KUNIT_FAIL(test, "Failed to register card");
  436. ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component, test_dev);
  437. KUNIT_EXPECT_EQ(test, 0, ret);
  438. ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0);
  439. KUNIT_EXPECT_EQ(test, 0, ret);
  440. /* cleanup */
  441. snd_soc_unregister_card(&kunit_comp->card);
  442. snd_soc_unregister_component(test_dev);
  443. }
  444. // TEST CASE
  445. // Test "empty" topology file, but with bad "size"
  446. // In theory we could loop through all possible bad values, but it takes too
  447. // long, so just use sizeof(struct snd_soc_tplg_hdr) + 1
  448. static void snd_soc_tplg_test_load_empty_tplg_bad_size(struct kunit *test)
  449. {
  450. struct kunit_soc_component *kunit_comp;
  451. struct tplg_tmpl_001 *data;
  452. int size;
  453. int ret;
  454. /* prepare */
  455. kunit_comp = kunit_kzalloc(test, sizeof(*kunit_comp), GFP_KERNEL);
  456. KUNIT_EXPECT_NOT_ERR_OR_NULL(test, kunit_comp);
  457. kunit_comp->kunit = test;
  458. kunit_comp->expect = -EINVAL; /* expect failure */
  459. size = sizeof(tplg_tmpl_empty);
  460. data = kunit_kzalloc(kunit_comp->kunit, size, GFP_KERNEL);
  461. KUNIT_EXPECT_NOT_ERR_OR_NULL(kunit_comp->kunit, data);
  462. memcpy(data, &tplg_tmpl_empty, sizeof(tplg_tmpl_empty));
  463. /*
  464. * override size
  465. * any value != struct size is wrong
  466. */
  467. data->header.size = cpu_to_le32(sizeof(struct snd_soc_tplg_hdr) + 1);
  468. kunit_comp->fw.data = (u8 *)data;
  469. kunit_comp->fw.size = size;
  470. kunit_comp->card.dev = test_dev,
  471. kunit_comp->card.name = "kunit-card",
  472. kunit_comp->card.owner = THIS_MODULE,
  473. kunit_comp->card.dai_link = kunit_dai_links,
  474. kunit_comp->card.num_links = ARRAY_SIZE(kunit_dai_links),
  475. kunit_comp->card.fully_routed = true,
  476. /* run test */
  477. ret = snd_soc_register_card(&kunit_comp->card);
  478. if (ret != 0 && ret != -EPROBE_DEFER)
  479. KUNIT_FAIL(test, "Failed to register card");
  480. ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component, test_dev);
  481. KUNIT_EXPECT_EQ(test, 0, ret);
  482. ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0);
  483. KUNIT_EXPECT_EQ(test, 0, ret);
  484. /* cleanup */
  485. snd_soc_unregister_card(&kunit_comp->card);
  486. snd_soc_unregister_component(test_dev);
  487. }
  488. // TEST CASE
  489. // Test "empty" topology file, but with bad "payload_size"
  490. // In theory we could loop through all possible bad values, but it takes too
  491. // long, so just use the known wrong one
  492. static void snd_soc_tplg_test_load_empty_tplg_bad_payload_size(struct kunit *test)
  493. {
  494. struct kunit_soc_component *kunit_comp;
  495. struct tplg_tmpl_001 *data;
  496. int size;
  497. int ret;
  498. /* prepare */
  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 */
  503. size = sizeof(tplg_tmpl_empty);
  504. data = kunit_kzalloc(kunit_comp->kunit, size, GFP_KERNEL);
  505. KUNIT_EXPECT_NOT_ERR_OR_NULL(kunit_comp->kunit, data);
  506. memcpy(data, &tplg_tmpl_empty, sizeof(tplg_tmpl_empty));
  507. /*
  508. * override payload size
  509. * there is only explicit check for 0, so check with it, other values
  510. * are handled by just not reading behind EOF
  511. */
  512. data->header.payload_size = 0;
  513. kunit_comp->fw.data = (u8 *)data;
  514. kunit_comp->fw.size = size;
  515. kunit_comp->card.dev = test_dev,
  516. kunit_comp->card.name = "kunit-card",
  517. kunit_comp->card.owner = THIS_MODULE,
  518. kunit_comp->card.dai_link = kunit_dai_links,
  519. kunit_comp->card.num_links = ARRAY_SIZE(kunit_dai_links),
  520. kunit_comp->card.fully_routed = true,
  521. /* run test */
  522. ret = snd_soc_register_card(&kunit_comp->card);
  523. if (ret != 0 && ret != -EPROBE_DEFER)
  524. KUNIT_FAIL(test, "Failed to register card");
  525. ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component, test_dev);
  526. KUNIT_EXPECT_EQ(test, 0, ret);
  527. ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0);
  528. KUNIT_EXPECT_EQ(test, 0, ret);
  529. /* cleanup */
  530. snd_soc_unregister_component(test_dev);
  531. snd_soc_unregister_card(&kunit_comp->card);
  532. }
  533. // TEST CASE
  534. // Test passing topology file with PCM definition
  535. static void snd_soc_tplg_test_load_pcm_tplg(struct kunit *test)
  536. {
  537. struct kunit_soc_component *kunit_comp;
  538. u8 *data;
  539. int size;
  540. int ret;
  541. /* prepare */
  542. kunit_comp = kunit_kzalloc(test, sizeof(*kunit_comp), GFP_KERNEL);
  543. KUNIT_EXPECT_NOT_ERR_OR_NULL(test, kunit_comp);
  544. kunit_comp->kunit = test;
  545. kunit_comp->expect = 0; /* expect success */
  546. size = sizeof(tplg_tmpl_with_pcm);
  547. data = kunit_kzalloc(kunit_comp->kunit, size, GFP_KERNEL);
  548. KUNIT_EXPECT_NOT_ERR_OR_NULL(kunit_comp->kunit, data);
  549. memcpy(data, &tplg_tmpl_with_pcm, sizeof(tplg_tmpl_with_pcm));
  550. kunit_comp->fw.data = data;
  551. kunit_comp->fw.size = size;
  552. kunit_comp->card.dev = test_dev,
  553. kunit_comp->card.name = "kunit-card",
  554. kunit_comp->card.owner = THIS_MODULE,
  555. kunit_comp->card.dai_link = kunit_dai_links,
  556. kunit_comp->card.num_links = ARRAY_SIZE(kunit_dai_links),
  557. kunit_comp->card.fully_routed = true,
  558. /* run test */
  559. ret = snd_soc_register_card(&kunit_comp->card);
  560. if (ret != 0 && ret != -EPROBE_DEFER)
  561. KUNIT_FAIL(test, "Failed to register card");
  562. ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component, test_dev);
  563. KUNIT_EXPECT_EQ(test, 0, ret);
  564. ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0);
  565. KUNIT_EXPECT_EQ(test, 0, ret);
  566. snd_soc_unregister_component(test_dev);
  567. /* cleanup */
  568. snd_soc_unregister_card(&kunit_comp->card);
  569. }
  570. // TEST CASE
  571. // Test passing topology file with PCM definition
  572. // with component reload
  573. static void snd_soc_tplg_test_load_pcm_tplg_reload_comp(struct kunit *test)
  574. {
  575. struct kunit_soc_component *kunit_comp;
  576. u8 *data;
  577. int size;
  578. int ret;
  579. int i;
  580. /* prepare */
  581. kunit_comp = kunit_kzalloc(test, sizeof(*kunit_comp), GFP_KERNEL);
  582. KUNIT_EXPECT_NOT_ERR_OR_NULL(test, kunit_comp);
  583. kunit_comp->kunit = test;
  584. kunit_comp->expect = 0; /* expect success */
  585. size = sizeof(tplg_tmpl_with_pcm);
  586. data = kunit_kzalloc(kunit_comp->kunit, size, GFP_KERNEL);
  587. KUNIT_EXPECT_NOT_ERR_OR_NULL(kunit_comp->kunit, data);
  588. memcpy(data, &tplg_tmpl_with_pcm, sizeof(tplg_tmpl_with_pcm));
  589. kunit_comp->fw.data = data;
  590. kunit_comp->fw.size = size;
  591. kunit_comp->card.dev = test_dev,
  592. kunit_comp->card.name = "kunit-card",
  593. kunit_comp->card.owner = THIS_MODULE,
  594. kunit_comp->card.dai_link = kunit_dai_links,
  595. kunit_comp->card.num_links = ARRAY_SIZE(kunit_dai_links),
  596. kunit_comp->card.fully_routed = true,
  597. /* run test */
  598. ret = snd_soc_register_card(&kunit_comp->card);
  599. if (ret != 0 && ret != -EPROBE_DEFER)
  600. KUNIT_FAIL(test, "Failed to register card");
  601. for (i = 0; i < 100; i++) {
  602. ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component, test_dev);
  603. KUNIT_EXPECT_EQ(test, 0, ret);
  604. ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0);
  605. KUNIT_EXPECT_EQ(test, 0, ret);
  606. snd_soc_unregister_component(test_dev);
  607. }
  608. /* cleanup */
  609. snd_soc_unregister_card(&kunit_comp->card);
  610. }
  611. // TEST CASE
  612. // Test passing topology file with PCM definition
  613. // with card reload
  614. static void snd_soc_tplg_test_load_pcm_tplg_reload_card(struct kunit *test)
  615. {
  616. struct kunit_soc_component *kunit_comp;
  617. u8 *data;
  618. int size;
  619. int ret;
  620. int i;
  621. /* prepare */
  622. kunit_comp = kunit_kzalloc(test, sizeof(*kunit_comp), GFP_KERNEL);
  623. KUNIT_EXPECT_NOT_ERR_OR_NULL(test, kunit_comp);
  624. kunit_comp->kunit = test;
  625. kunit_comp->expect = 0; /* expect success */
  626. size = sizeof(tplg_tmpl_with_pcm);
  627. data = kunit_kzalloc(kunit_comp->kunit, size, GFP_KERNEL);
  628. KUNIT_EXPECT_NOT_ERR_OR_NULL(kunit_comp->kunit, data);
  629. memcpy(data, &tplg_tmpl_with_pcm, sizeof(tplg_tmpl_with_pcm));
  630. kunit_comp->fw.data = data;
  631. kunit_comp->fw.size = size;
  632. kunit_comp->card.dev = test_dev,
  633. kunit_comp->card.name = "kunit-card",
  634. kunit_comp->card.owner = THIS_MODULE,
  635. kunit_comp->card.dai_link = kunit_dai_links,
  636. kunit_comp->card.num_links = ARRAY_SIZE(kunit_dai_links),
  637. kunit_comp->card.fully_routed = true,
  638. /* run test */
  639. ret = snd_soc_component_initialize(&kunit_comp->comp, &test_component, test_dev);
  640. KUNIT_EXPECT_EQ(test, 0, ret);
  641. ret = snd_soc_add_component(&kunit_comp->comp, NULL, 0);
  642. KUNIT_EXPECT_EQ(test, 0, ret);
  643. for (i = 0; i < 100; i++) {
  644. ret = snd_soc_register_card(&kunit_comp->card);
  645. if (ret != 0 && ret != -EPROBE_DEFER)
  646. KUNIT_FAIL(test, "Failed to register card");
  647. snd_soc_unregister_card(&kunit_comp->card);
  648. }
  649. /* cleanup */
  650. snd_soc_unregister_component(test_dev);
  651. }
  652. /* ===== KUNIT MODULE DEFINITIONS =========================================== */
  653. static struct kunit_case snd_soc_tplg_test_cases[] = {
  654. KUNIT_CASE(snd_soc_tplg_test_load_with_null_comp),
  655. KUNIT_CASE(snd_soc_tplg_test_load_with_null_ops),
  656. KUNIT_CASE(snd_soc_tplg_test_load_with_null_fw),
  657. KUNIT_CASE(snd_soc_tplg_test_load_empty_tplg),
  658. KUNIT_CASE(snd_soc_tplg_test_load_empty_tplg_bad_magic),
  659. KUNIT_CASE(snd_soc_tplg_test_load_empty_tplg_bad_abi),
  660. KUNIT_CASE(snd_soc_tplg_test_load_empty_tplg_bad_size),
  661. KUNIT_CASE(snd_soc_tplg_test_load_empty_tplg_bad_payload_size),
  662. KUNIT_CASE(snd_soc_tplg_test_load_pcm_tplg),
  663. KUNIT_CASE(snd_soc_tplg_test_load_pcm_tplg_reload_comp),
  664. KUNIT_CASE(snd_soc_tplg_test_load_pcm_tplg_reload_card),
  665. {}
  666. };
  667. static struct kunit_suite snd_soc_tplg_test_suite = {
  668. .name = "snd_soc_tplg_test",
  669. .init = snd_soc_tplg_test_init,
  670. .exit = snd_soc_tplg_test_exit,
  671. .test_cases = snd_soc_tplg_test_cases,
  672. };
  673. kunit_test_suites(&snd_soc_tplg_test_suite);
  674. MODULE_LICENSE("GPL");