test-main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 (IS_ENABLED(CONFIG_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. if (!select_name)
  119. return true;
  120. if (!strcmp(test_name, select_name))
  121. return true;
  122. if (!prefix) {
  123. const char *p = strstr(test_name, "_test_");
  124. /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
  125. if (p)
  126. test_name = p + 6;
  127. } else {
  128. /* All tests have this prefix */
  129. if (!strncmp(test_name, prefix, strlen(prefix)))
  130. test_name += strlen(prefix);
  131. }
  132. if (!strcmp(test_name, select_name))
  133. return true;
  134. return false;
  135. }
  136. /**
  137. * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
  138. *
  139. * @tests: List of tests to run
  140. * @count: Number of tests to ru
  141. * @return true if any of the tests have the UT_TESTF_DM flag
  142. */
  143. static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
  144. {
  145. struct unit_test *test;
  146. for (test = tests; test < tests + count; test++) {
  147. if (test->flags & UT_TESTF_DM)
  148. return true;
  149. }
  150. return false;
  151. }
  152. /**
  153. * dm_test_restore() Put things back to normal so sandbox works as expected
  154. *
  155. * @of_root: Value to set for of_root
  156. * @return 0 if OK, -ve on error
  157. */
  158. static int dm_test_restore(struct device_node *of_root)
  159. {
  160. int ret;
  161. gd_set_of_root(of_root);
  162. gd->dm_root = NULL;
  163. ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
  164. if (ret)
  165. return ret;
  166. dm_scan_plat(false);
  167. if (!CONFIG_IS_ENABLED(OF_PLATDATA))
  168. dm_scan_fdt(false);
  169. return 0;
  170. }
  171. /**
  172. * test_pre_run() - Handle any preparation needed to run a test
  173. *
  174. * @uts: Test state
  175. * @test: Test to prepare for
  176. * @return 0 if OK, -EAGAIN to skip this test since some required feature is not
  177. * available, other -ve on error (meaning that testing cannot likely
  178. * continue)
  179. */
  180. static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
  181. {
  182. if (test->flags & UT_TESTF_DM)
  183. ut_assertok(dm_test_pre_run(uts));
  184. ut_set_skip_delays(uts, false);
  185. uts->start = mallinfo();
  186. if (test->flags & UT_TESTF_SCAN_PDATA)
  187. ut_assertok(dm_scan_plat(false));
  188. if (test->flags & UT_TESTF_PROBE_TEST)
  189. ut_assertok(do_autoprobe(uts));
  190. if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
  191. (test->flags & UT_TESTF_SCAN_FDT))
  192. ut_assertok(dm_extended_scan(false));
  193. if (test->flags & UT_TESTF_CONSOLE_REC) {
  194. int ret = console_record_reset_enable();
  195. if (ret) {
  196. printf("Skipping: Console recording disabled\n");
  197. return -EAGAIN;
  198. }
  199. }
  200. ut_silence_console(uts);
  201. return 0;
  202. }
  203. /**
  204. * test_post_run() - Handle cleaning up after a test
  205. *
  206. * @uts: Test state
  207. * @test: Test to clean up after
  208. * @return 0 if OK, -ve on error (meaning that testing cannot likely continue)
  209. */
  210. static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
  211. {
  212. ut_unsilence_console(uts);
  213. if (test->flags & UT_TESTF_DM)
  214. ut_assertok(dm_test_post_run(uts));
  215. return 0;
  216. }
  217. /**
  218. * ut_run_test() - Run a single test
  219. *
  220. * This runs the test, handling any preparation and clean-up needed. It prints
  221. * the name of each test before running it.
  222. *
  223. * @uts: Test state to update. The caller should ensure that this is zeroed for
  224. * the first call to this function. On exit, @uts->fail_count is
  225. * incremented by the number of failures (0, one hopes)
  226. * @test_name: Test to run
  227. * @name: Name of test, possibly skipping a prefix that should not be displayed
  228. * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
  229. * any failed
  230. */
  231. static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
  232. const char *test_name)
  233. {
  234. const char *fname = strrchr(test->file, '/') + 1;
  235. const char *note = "";
  236. int ret;
  237. if ((test->flags & UT_TESTF_DM) && !uts->of_live)
  238. note = " (flat tree)";
  239. printf("Test: %s: %s%s\n", test_name, fname, note);
  240. /* Allow access to test state from drivers */
  241. test_set_state(uts);
  242. ret = test_pre_run(uts, test);
  243. if (ret == -EAGAIN)
  244. return -EAGAIN;
  245. if (ret)
  246. return ret;
  247. test->func(uts);
  248. ret = test_post_run(uts, test);
  249. if (ret)
  250. return ret;
  251. test_set_state( NULL);
  252. return 0;
  253. }
  254. /**
  255. * ut_run_test_live_flat() - Run a test with both live and flat tree
  256. *
  257. * This calls ut_run_test() with livetree enabled, which is the standard setup
  258. * for runnig tests. Then, for driver model test, it calls it again with
  259. * livetree disabled. This allows checking of flattree being used when OF_LIVE
  260. * is enabled, as is the case in U-Boot proper before relocation, as well as in
  261. * SPL.
  262. *
  263. * @uts: Test state to update. The caller should ensure that this is zeroed for
  264. * the first call to this function. On exit, @uts->fail_count is
  265. * incremented by the number of failures (0, one hopes)
  266. * @test: Test to run
  267. * @name: Name of test, possibly skipping a prefix that should not be displayed
  268. * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
  269. * any failed
  270. */
  271. static int ut_run_test_live_flat(struct unit_test_state *uts,
  272. struct unit_test *test, const char *name)
  273. {
  274. int runs;
  275. /* Run with the live tree if possible */
  276. runs = 0;
  277. if (CONFIG_IS_ENABLED(OF_LIVE)) {
  278. if (!(test->flags & UT_TESTF_FLAT_TREE)) {
  279. uts->of_live = true;
  280. ut_assertok(ut_run_test(uts, test, test->name));
  281. runs++;
  282. }
  283. }
  284. /*
  285. * Run with the flat tree if we couldn't run it with live tree,
  286. * or it is a core test.
  287. */
  288. if (!(test->flags & UT_TESTF_LIVE_TREE) &&
  289. (!runs || ut_test_run_on_flattree(test))) {
  290. uts->of_live = false;
  291. ut_assertok(ut_run_test(uts, test, test->name));
  292. runs++;
  293. }
  294. return 0;
  295. }
  296. /**
  297. * ut_run_tests() - Run a set of tests
  298. *
  299. * This runs the tests, handling any preparation and clean-up needed. It prints
  300. * the name of each test before running it.
  301. *
  302. * @uts: Test state to update. The caller should ensure that this is zeroed for
  303. * the first call to this function. On exit, @uts->fail_count is
  304. * incremented by the number of failures (0, one hopes)
  305. * @prefix: String prefix for the tests. Any tests that have this prefix will be
  306. * printed without the prefix, so that it is easier to see the unique part
  307. * of the test name. If NULL, no prefix processing is done
  308. * @tests: List of tests to run
  309. * @count: Number of tests to run
  310. * @select_name: Name of a single test to run (from the list provided). If NULL
  311. * then all tests are run
  312. * @return 0 if all tests passed, -ENOENT if test @select_name was not found,
  313. * -EBADF if any failed
  314. */
  315. static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
  316. struct unit_test *tests, int count,
  317. const char *select_name)
  318. {
  319. struct unit_test *test;
  320. int found = 0;
  321. for (test = tests; test < tests + count; test++) {
  322. const char *test_name = test->name;
  323. int ret;
  324. if (!test_matches(prefix, test_name, select_name))
  325. continue;
  326. ret = ut_run_test_live_flat(uts, test, select_name);
  327. found++;
  328. if (ret == -EAGAIN)
  329. continue;
  330. if (ret)
  331. return ret;
  332. }
  333. if (select_name && !found)
  334. return -ENOENT;
  335. return uts->fail_count ? -EBADF : 0;
  336. }
  337. int ut_run_list(const char *category, const char *prefix,
  338. struct unit_test *tests, int count, const char *select_name)
  339. {
  340. struct unit_test_state uts = { .fail_count = 0 };
  341. bool has_dm_tests = false;
  342. int ret;
  343. if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
  344. ut_list_has_dm_tests(tests, count)) {
  345. has_dm_tests = true;
  346. /*
  347. * If we have no device tree, or it only has a root node, then
  348. * these * tests clearly aren't going to work...
  349. */
  350. if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
  351. puts("Please run with test device tree:\n"
  352. " ./u-boot -d arch/sandbox/dts/test.dtb\n");
  353. return CMD_RET_FAILURE;
  354. }
  355. }
  356. if (!select_name)
  357. printf("Running %d %s tests\n", count, category);
  358. uts.of_root = gd_of_root();
  359. ret = ut_run_tests(&uts, prefix, tests, count, select_name);
  360. if (ret == -ENOENT)
  361. printf("Test '%s' not found\n", select_name);
  362. else
  363. printf("Failures: %d\n", uts.fail_count);
  364. /* Best efforts only...ignore errors */
  365. if (has_dm_tests)
  366. dm_test_restore(uts.of_root);
  367. return ret;
  368. }