test-main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2021 Google LLC
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #include <common.h>
  7. #include <console.h>
  8. #include <dm.h>
  9. #include <dm/root.h>
  10. #include <dm/test.h>
  11. #include <dm/uclass-internal.h>
  12. #include <test/test.h>
  13. #include <test/ut.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. /* This is valid when a test is running, NULL otherwise */
  16. static struct unit_test_state *cur_test_state;
  17. struct unit_test_state *test_get_state(void)
  18. {
  19. return cur_test_state;
  20. }
  21. void test_set_state(struct unit_test_state *uts)
  22. {
  23. cur_test_state = uts;
  24. }
  25. /**
  26. * dm_test_pre_run() - Get ready to run a driver model test
  27. *
  28. * This clears out the driver model data structures. For sandbox it resets the
  29. * state structure
  30. *
  31. * @uts: Test state
  32. */
  33. static int dm_test_pre_run(struct unit_test_state *uts)
  34. {
  35. bool of_live = uts->of_live;
  36. uts->root = NULL;
  37. uts->testdev = NULL;
  38. uts->force_fail_alloc = false;
  39. uts->skip_post_probe = false;
  40. gd->dm_root = NULL;
  41. if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA))
  42. memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
  43. arch_reset_for_test();
  44. /* Determine whether to make the live tree available */
  45. gd_set_of_root(of_live ? uts->of_root : NULL);
  46. ut_assertok(dm_init(of_live));
  47. uts->root = dm_root();
  48. return 0;
  49. }
  50. static int dm_test_post_run(struct unit_test_state *uts)
  51. {
  52. int id;
  53. /*
  54. * With of-platdata-inst the uclasses are created at build time. If we
  55. * destroy them we cannot get them back since uclass_add() is not
  56. * supported. So skip this.
  57. */
  58. if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
  59. for (id = 0; id < UCLASS_COUNT; id++) {
  60. struct uclass *uc;
  61. /*
  62. * If the uclass doesn't exist we don't want to create
  63. * it. So check that here before we call
  64. * uclass_find_device().
  65. */
  66. uc = uclass_find(id);
  67. if (!uc)
  68. continue;
  69. ut_assertok(uclass_destroy(uc));
  70. }
  71. }
  72. return 0;
  73. }
  74. /* Ensure all the test devices are probed */
  75. static int do_autoprobe(struct unit_test_state *uts)
  76. {
  77. struct udevice *dev;
  78. int ret;
  79. /* Scanning the uclass is enough to probe all the devices */
  80. for (ret = uclass_first_device(UCLASS_TEST, &dev);
  81. dev;
  82. ret = uclass_next_device(&dev))
  83. ;
  84. return ret;
  85. }
  86. /*
  87. * ut_test_run_on_flattree() - Check if we should run a test with flat DT
  88. *
  89. * This skips long/slow tests where there is not much value in running a flat
  90. * DT test in addition to a live DT test.
  91. *
  92. * @return true to run the given test on the flat device tree
  93. */
  94. static bool ut_test_run_on_flattree(struct unit_test *test)
  95. {
  96. const char *fname = strrchr(test->file, '/') + 1;
  97. if (!(test->flags & UT_TESTF_DM))
  98. return false;
  99. return !strstr(fname, "video") || strstr(test->name, "video_base");
  100. }
  101. /**
  102. * test_matches() - Check if a test should be run
  103. *
  104. * This checks if the a test should be run. In the normal case of running all
  105. * tests, @select_name is NULL.
  106. *
  107. * @prefix: String prefix for the tests. Any tests that have this prefix will be
  108. * printed without the prefix, so that it is easier to see the unique part
  109. * of the test name. If NULL, any suite name (xxx_test) is considered to be
  110. * a prefix.
  111. * @test_name: Name of current test
  112. * @select_name: Name of test to run (or NULL for all)
  113. * @return true to run this test, false to skip it
  114. */
  115. static bool test_matches(const char *prefix, const char *test_name,
  116. const char *select_name)
  117. {
  118. size_t len;
  119. if (!select_name)
  120. return true;
  121. /* Allow glob expansion in the test name */
  122. len = select_name[strlen(select_name) - 1] == '*' ? strlen(select_name) : 0;
  123. if (len-- == 1)
  124. return true;
  125. if (!strncmp(test_name, select_name, len))
  126. return true;
  127. if (prefix) {
  128. /* All tests have this prefix */
  129. if (!strncmp(test_name, prefix, strlen(prefix)))
  130. test_name += strlen(prefix);
  131. } else {
  132. const char *p = strstr(test_name, "_test_");
  133. /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
  134. if (p)
  135. test_name = p + strlen("_test_");
  136. }
  137. if (!strncmp(test_name, select_name, len))
  138. return true;
  139. return false;
  140. }
  141. /**
  142. * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
  143. *
  144. * @tests: List of tests to run
  145. * @count: Number of tests to ru
  146. * @return true if any of the tests have the UT_TESTF_DM flag
  147. */
  148. static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
  149. {
  150. struct unit_test *test;
  151. for (test = tests; test < tests + count; test++) {
  152. if (test->flags & UT_TESTF_DM)
  153. return true;
  154. }
  155. return false;
  156. }
  157. /**
  158. * dm_test_restore() Put things back to normal so sandbox works as expected
  159. *
  160. * @of_root: Value to set for of_root
  161. * @return 0 if OK, -ve on error
  162. */
  163. static int dm_test_restore(struct device_node *of_root)
  164. {
  165. int ret;
  166. gd_set_of_root(of_root);
  167. gd->dm_root = NULL;
  168. ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
  169. if (ret)
  170. return ret;
  171. dm_scan_plat(false);
  172. if (!CONFIG_IS_ENABLED(OF_PLATDATA))
  173. dm_scan_fdt(false);
  174. return 0;
  175. }
  176. /**
  177. * test_pre_run() - Handle any preparation needed to run a test
  178. *
  179. * @uts: Test state
  180. * @test: Test to prepare for
  181. * @return 0 if OK, -EAGAIN to skip this test since some required feature is not
  182. * available, other -ve on error (meaning that testing cannot likely
  183. * continue)
  184. */
  185. static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
  186. {
  187. if (test->flags & UT_TESTF_DM)
  188. ut_assertok(dm_test_pre_run(uts));
  189. ut_set_skip_delays(uts, false);
  190. uts->start = mallinfo();
  191. if (test->flags & UT_TESTF_SCAN_PDATA)
  192. ut_assertok(dm_scan_plat(false));
  193. if (test->flags & UT_TESTF_PROBE_TEST)
  194. ut_assertok(do_autoprobe(uts));
  195. if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
  196. (test->flags & UT_TESTF_SCAN_FDT))
  197. ut_assertok(dm_extended_scan(false));
  198. if (test->flags & UT_TESTF_CONSOLE_REC) {
  199. int ret = console_record_reset_enable();
  200. if (ret) {
  201. printf("Skipping: Console recording disabled\n");
  202. return -EAGAIN;
  203. }
  204. }
  205. ut_silence_console(uts);
  206. return 0;
  207. }
  208. /**
  209. * test_post_run() - Handle cleaning up after a test
  210. *
  211. * @uts: Test state
  212. * @test: Test to clean up after
  213. * @return 0 if OK, -ve on error (meaning that testing cannot likely continue)
  214. */
  215. static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
  216. {
  217. ut_unsilence_console(uts);
  218. if (test->flags & UT_TESTF_DM)
  219. ut_assertok(dm_test_post_run(uts));
  220. return 0;
  221. }
  222. /**
  223. * ut_run_test() - Run a single test
  224. *
  225. * This runs the test, handling any preparation and clean-up needed. It prints
  226. * the name of each test before running it.
  227. *
  228. * @uts: Test state to update. The caller should ensure that this is zeroed for
  229. * the first call to this function. On exit, @uts->fail_count is
  230. * incremented by the number of failures (0, one hopes)
  231. * @test_name: Test to run
  232. * @name: Name of test, possibly skipping a prefix that should not be displayed
  233. * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
  234. * any failed
  235. */
  236. static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
  237. const char *test_name)
  238. {
  239. const char *fname = strrchr(test->file, '/') + 1;
  240. const char *note = "";
  241. int ret;
  242. if ((test->flags & UT_TESTF_DM) && !uts->of_live)
  243. note = " (flat tree)";
  244. printf("Test: %s: %s%s\n", test_name, fname, note);
  245. /* Allow access to test state from drivers */
  246. test_set_state(uts);
  247. ret = test_pre_run(uts, test);
  248. if (ret == -EAGAIN)
  249. return -EAGAIN;
  250. if (ret)
  251. return ret;
  252. test->func(uts);
  253. ret = test_post_run(uts, test);
  254. if (ret)
  255. return ret;
  256. test_set_state( NULL);
  257. return 0;
  258. }
  259. /**
  260. * ut_run_test_live_flat() - Run a test with both live and flat tree
  261. *
  262. * This calls ut_run_test() with livetree enabled, which is the standard setup
  263. * for runnig tests. Then, for driver model test, it calls it again with
  264. * livetree disabled. This allows checking of flattree being used when OF_LIVE
  265. * is enabled, as is the case in U-Boot proper before relocation, as well as in
  266. * SPL.
  267. *
  268. * @uts: Test state to update. The caller should ensure that this is zeroed for
  269. * the first call to this function. On exit, @uts->fail_count is
  270. * incremented by the number of failures (0, one hopes)
  271. * @test: Test to run
  272. * @name: Name of test, possibly skipping a prefix that should not be displayed
  273. * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
  274. * any failed
  275. */
  276. static int ut_run_test_live_flat(struct unit_test_state *uts,
  277. struct unit_test *test, const char *name)
  278. {
  279. int runs;
  280. /* Run with the live tree if possible */
  281. runs = 0;
  282. if (CONFIG_IS_ENABLED(OF_LIVE)) {
  283. if (!(test->flags & UT_TESTF_FLAT_TREE)) {
  284. uts->of_live = true;
  285. ut_assertok(ut_run_test(uts, test, test->name));
  286. runs++;
  287. }
  288. }
  289. /*
  290. * Run with the flat tree if we couldn't run it with live tree,
  291. * or it is a core test.
  292. */
  293. if (!(test->flags & UT_TESTF_LIVE_TREE) &&
  294. (!runs || ut_test_run_on_flattree(test))) {
  295. uts->of_live = false;
  296. ut_assertok(ut_run_test(uts, test, test->name));
  297. runs++;
  298. }
  299. return 0;
  300. }
  301. /**
  302. * ut_run_tests() - Run a set of tests
  303. *
  304. * This runs the tests, handling any preparation and clean-up needed. It prints
  305. * the name of each test before running it.
  306. *
  307. * @uts: Test state to update. The caller should ensure that this is zeroed for
  308. * the first call to this function. On exit, @uts->fail_count is
  309. * incremented by the number of failures (0, one hopes)
  310. * @prefix: String prefix for the tests. Any tests that have this prefix will be
  311. * printed without the prefix, so that it is easier to see the unique part
  312. * of the test name. If NULL, no prefix processing is done
  313. * @tests: List of tests to run
  314. * @count: Number of tests to run
  315. * @select_name: Name of a single test to run (from the list provided). If NULL
  316. * then all tests are run
  317. * @return 0 if all tests passed, -ENOENT if test @select_name was not found,
  318. * -EBADF if any failed
  319. */
  320. static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
  321. struct unit_test *tests, int count,
  322. const char *select_name)
  323. {
  324. struct unit_test *test;
  325. int found = 0;
  326. for (test = tests; test < tests + count; test++) {
  327. const char *test_name = test->name;
  328. int ret;
  329. if (!test_matches(prefix, test_name, select_name))
  330. continue;
  331. ret = ut_run_test_live_flat(uts, test, select_name);
  332. found++;
  333. if (ret == -EAGAIN)
  334. continue;
  335. if (ret)
  336. return ret;
  337. }
  338. if (select_name && !found)
  339. return -ENOENT;
  340. return uts->fail_count ? -EBADF : 0;
  341. }
  342. int ut_run_list(const char *category, const char *prefix,
  343. struct unit_test *tests, int count, const char *select_name)
  344. {
  345. struct unit_test_state uts = { .fail_count = 0 };
  346. bool has_dm_tests = false;
  347. int ret;
  348. if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
  349. ut_list_has_dm_tests(tests, count)) {
  350. has_dm_tests = true;
  351. /*
  352. * If we have no device tree, or it only has a root node, then
  353. * these * tests clearly aren't going to work...
  354. */
  355. if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
  356. puts("Please run with test device tree:\n"
  357. " ./u-boot -d arch/sandbox/dts/test.dtb\n");
  358. return CMD_RET_FAILURE;
  359. }
  360. }
  361. if (!select_name)
  362. printf("Running %d %s tests\n", count, category);
  363. uts.of_root = gd_of_root();
  364. ret = ut_run_tests(&uts, prefix, tests, count, select_name);
  365. if (ret == -ENOENT)
  366. printf("Test '%s' not found\n", select_name);
  367. else
  368. printf("Failures: %d\n", uts.fail_count);
  369. /* Best efforts only...ignore errors */
  370. if (has_dm_tests)
  371. dm_test_restore(uts.of_root);
  372. return ret;
  373. }