test-main.c 11 KB

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