soc-topology-test.c 24 KB

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