test-main.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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 <blk.h>
  8. #include <console.h>
  9. #include <cyclic.h>
  10. #include <dm.h>
  11. #include <event.h>
  12. #include <net.h>
  13. #include <of_live.h>
  14. #include <os.h>
  15. #include <dm/ofnode.h>
  16. #include <dm/root.h>
  17. #include <dm/test.h>
  18. #include <dm/uclass-internal.h>
  19. #include <test/test.h>
  20. #include <test/ut.h>
  21. #include <u-boot/crc.h>
  22. DECLARE_GLOBAL_DATA_PTR;
  23. /**
  24. * enum fdtchk_t - what to do with the device tree (gd->fdt_blob)
  25. *
  26. * This affects what happens with the device tree before and after a test
  27. *
  28. * @FDTCHK_NONE: Do nothing
  29. * @FDTCHK_CHECKSUM: Take a checksum of the FDT before the test runs and
  30. * compare it afterwards to detect any changes
  31. * @FDTCHK_COPY: Make a copy of the FDT and restore it afterwards
  32. */
  33. enum fdtchk_t {
  34. FDTCHK_NONE,
  35. FDTCHK_CHECKSUM,
  36. FDTCHK_COPY,
  37. };
  38. /**
  39. * fdt_action() - get the required action for the FDT
  40. *
  41. * @return the action that should be taken for this build
  42. */
  43. static enum fdtchk_t fdt_action(void)
  44. {
  45. /* For sandbox SPL builds, do nothing */
  46. if (IS_ENABLED(CONFIG_SANDBOX) && IS_ENABLED(CONFIG_SPL_BUILD))
  47. return FDTCHK_NONE;
  48. /* Do a copy for sandbox (but only the U-Boot build, not SPL) */
  49. if (IS_ENABLED(CONFIG_SANDBOX))
  50. return FDTCHK_COPY;
  51. /* For all other boards, do a checksum */
  52. return FDTCHK_CHECKSUM;
  53. }
  54. /* This is valid when a test is running, NULL otherwise */
  55. static struct unit_test_state *cur_test_state;
  56. struct unit_test_state *test_get_state(void)
  57. {
  58. return cur_test_state;
  59. }
  60. void test_set_state(struct unit_test_state *uts)
  61. {
  62. cur_test_state = uts;
  63. }
  64. /**
  65. * dm_test_pre_run() - Get ready to run a driver model test
  66. *
  67. * This clears out the driver model data structures. For sandbox it resets the
  68. * state structure
  69. *
  70. * @uts: Test state
  71. */
  72. static int dm_test_pre_run(struct unit_test_state *uts)
  73. {
  74. bool of_live = uts->of_live;
  75. if (of_live && (gd->flags & GD_FLG_FDT_CHANGED)) {
  76. printf("Cannot run live tree test as device tree changed\n");
  77. return -EFAULT;
  78. }
  79. uts->root = NULL;
  80. uts->testdev = NULL;
  81. uts->force_fail_alloc = false;
  82. uts->skip_post_probe = false;
  83. if (fdt_action() == FDTCHK_CHECKSUM)
  84. uts->fdt_chksum = crc8(0, gd->fdt_blob,
  85. fdt_totalsize(gd->fdt_blob));
  86. gd->dm_root = NULL;
  87. malloc_disable_testing();
  88. if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA))
  89. memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
  90. arch_reset_for_test();
  91. /* Determine whether to make the live tree available */
  92. gd_set_of_root(of_live ? uts->of_root : NULL);
  93. oftree_reset();
  94. ut_assertok(dm_init(of_live));
  95. uts->root = dm_root();
  96. return 0;
  97. }
  98. static int dm_test_post_run(struct unit_test_state *uts)
  99. {
  100. int id;
  101. if (gd->fdt_blob) {
  102. switch (fdt_action()) {
  103. case FDTCHK_COPY:
  104. memcpy((void *)gd->fdt_blob, uts->fdt_copy, uts->fdt_size);
  105. break;
  106. case FDTCHK_CHECKSUM: {
  107. uint chksum;
  108. chksum = crc8(0, gd->fdt_blob, fdt_totalsize(gd->fdt_blob));
  109. if (chksum != uts->fdt_chksum) {
  110. /*
  111. * We cannot run any more tests that need the
  112. * live tree, since its strings point into the
  113. * flat tree, which has changed. This likely
  114. * means that at least some of the pointers from
  115. * the live tree point to different things
  116. */
  117. printf("Device tree changed: cannot run live tree tests\n");
  118. gd->flags |= GD_FLG_FDT_CHANGED;
  119. }
  120. break;
  121. }
  122. case FDTCHK_NONE:
  123. break;
  124. }
  125. }
  126. /*
  127. * With of-platdata-inst the uclasses are created at build time. If we
  128. * destroy them we cannot get them back since uclass_add() is not
  129. * supported. So skip this.
  130. */
  131. if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
  132. for (id = 0; id < UCLASS_COUNT; id++) {
  133. struct uclass *uc;
  134. /*
  135. * If the uclass doesn't exist we don't want to create
  136. * it. So check that here before we call
  137. * uclass_find_device().
  138. */
  139. uc = uclass_find(id);
  140. if (!uc)
  141. continue;
  142. ut_assertok(uclass_destroy(uc));
  143. }
  144. }
  145. return 0;
  146. }
  147. /* Ensure all the test devices are probed */
  148. static int do_autoprobe(struct unit_test_state *uts)
  149. {
  150. return uclass_probe_all(UCLASS_TEST);
  151. }
  152. /*
  153. * ut_test_run_on_flattree() - Check if we should run a test with flat DT
  154. *
  155. * This skips long/slow tests where there is not much value in running a flat
  156. * DT test in addition to a live DT test.
  157. *
  158. * Return: true to run the given test on the flat device tree
  159. */
  160. static bool ut_test_run_on_flattree(struct unit_test *test)
  161. {
  162. const char *fname = strrchr(test->file, '/') + 1;
  163. if (!(test->flags & UT_TESTF_DM))
  164. return false;
  165. return !strstr(fname, "video") || strstr(test->name, "video_base");
  166. }
  167. /**
  168. * test_matches() - Check if a test should be run
  169. *
  170. * This checks if the a test should be run. In the normal case of running all
  171. * tests, @select_name is NULL.
  172. *
  173. * @prefix: String prefix for the tests. Any tests that have this prefix will be
  174. * printed without the prefix, so that it is easier to see the unique part
  175. * of the test name. If NULL, any suite name (xxx_test) is considered to be
  176. * a prefix.
  177. * @test_name: Name of current test
  178. * @select_name: Name of test to run (or NULL for all)
  179. * Return: true to run this test, false to skip it
  180. */
  181. static bool test_matches(const char *prefix, const char *test_name,
  182. const char *select_name)
  183. {
  184. size_t len;
  185. if (!select_name)
  186. return true;
  187. /* Allow glob expansion in the test name */
  188. len = select_name[strlen(select_name) - 1] == '*' ? strlen(select_name) : 0;
  189. if (len-- == 1)
  190. return true;
  191. if (!strncmp(test_name, select_name, len))
  192. return true;
  193. if (prefix) {
  194. /* All tests have this prefix */
  195. if (!strncmp(test_name, prefix, strlen(prefix)))
  196. test_name += strlen(prefix);
  197. } else {
  198. const char *p = strstr(test_name, "_test_");
  199. /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
  200. if (p)
  201. test_name = p + strlen("_test_");
  202. }
  203. if (!strncmp(test_name, select_name, len))
  204. return true;
  205. return false;
  206. }
  207. /**
  208. * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
  209. *
  210. * @tests: List of tests to run
  211. * @count: Number of tests to ru
  212. * Return: true if any of the tests have the UT_TESTF_DM flag
  213. */
  214. static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
  215. {
  216. struct unit_test *test;
  217. for (test = tests; test < tests + count; test++) {
  218. if (test->flags & UT_TESTF_DM)
  219. return true;
  220. }
  221. return false;
  222. }
  223. /**
  224. * dm_test_restore() Put things back to normal so sandbox works as expected
  225. *
  226. * @of_root: Value to set for of_root
  227. * Return: 0 if OK, -ve on error
  228. */
  229. static int dm_test_restore(struct device_node *of_root)
  230. {
  231. int ret;
  232. gd_set_of_root(of_root);
  233. gd->dm_root = NULL;
  234. ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
  235. if (ret)
  236. return ret;
  237. dm_scan_plat(false);
  238. if (!CONFIG_IS_ENABLED(OF_PLATDATA))
  239. dm_extended_scan(false);
  240. return 0;
  241. }
  242. /**
  243. * test_pre_run() - Handle any preparation needed to run a test
  244. *
  245. * @uts: Test state
  246. * @test: Test to prepare for
  247. * Return: 0 if OK, -EAGAIN to skip this test since some required feature is not
  248. * available, other -ve on error (meaning that testing cannot likely
  249. * continue)
  250. */
  251. static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
  252. {
  253. ut_assertok(event_init());
  254. if (test->flags & UT_TESTF_DM)
  255. ut_assertok(dm_test_pre_run(uts));
  256. ut_set_skip_delays(uts, false);
  257. uts->start = mallinfo();
  258. if (test->flags & UT_TESTF_SCAN_PDATA)
  259. ut_assertok(dm_scan_plat(false));
  260. if (test->flags & UT_TESTF_PROBE_TEST)
  261. ut_assertok(do_autoprobe(uts));
  262. if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
  263. (test->flags & UT_TESTF_SCAN_FDT)) {
  264. /*
  265. * only set this if we know the ethernet uclass will be created
  266. */
  267. eth_set_enable_bootdevs(test->flags & UT_TESTF_ETH_BOOTDEV);
  268. test_sf_set_enable_bootdevs(test->flags & UT_TESTF_SF_BOOTDEV);
  269. ut_assertok(dm_extended_scan(false));
  270. }
  271. /*
  272. * Do this after FDT scan since dm_scan_other() in bootstd-uclass.c
  273. * checks for the existence of bootstd
  274. */
  275. if (test->flags & UT_TESTF_SCAN_PDATA)
  276. ut_assertok(dm_scan_other(false));
  277. if (IS_ENABLED(CONFIG_SANDBOX) && (test->flags & UT_TESTF_OTHER_FDT)) {
  278. /* make sure the other FDT is available */
  279. ut_assertok(test_load_other_fdt(uts));
  280. /*
  281. * create a new live tree with it for every test, in case a
  282. * test modifies the tree
  283. */
  284. if (of_live_active()) {
  285. ut_assertok(unflatten_device_tree(uts->other_fdt,
  286. &uts->of_other));
  287. }
  288. }
  289. if (test->flags & UT_TESTF_CONSOLE_REC) {
  290. int ret = console_record_reset_enable();
  291. if (ret) {
  292. printf("Skipping: Console recording disabled\n");
  293. return -EAGAIN;
  294. }
  295. }
  296. ut_silence_console(uts);
  297. return 0;
  298. }
  299. /**
  300. * test_post_run() - Handle cleaning up after a test
  301. *
  302. * @uts: Test state
  303. * @test: Test to clean up after
  304. * Return: 0 if OK, -ve on error (meaning that testing cannot likely continue)
  305. */
  306. static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
  307. {
  308. ut_unsilence_console(uts);
  309. if (test->flags & UT_TESTF_DM)
  310. ut_assertok(dm_test_post_run(uts));
  311. ut_assertok(cyclic_unregister_all());
  312. ut_assertok(event_uninit());
  313. free(uts->of_other);
  314. uts->of_other = NULL;
  315. blkcache_free();
  316. return 0;
  317. }
  318. /**
  319. * skip_test() - Handle skipping a test
  320. *
  321. * @uts: Test state to update
  322. * @return -EAGAIN (always)
  323. */
  324. static int skip_test(struct unit_test_state *uts)
  325. {
  326. uts->skip_count++;
  327. return -EAGAIN;
  328. }
  329. /**
  330. * ut_run_test() - Run a single test
  331. *
  332. * This runs the test, handling any preparation and clean-up needed. It prints
  333. * the name of each test before running it.
  334. *
  335. * @uts: Test state to update. The caller should ensure that this is zeroed for
  336. * the first call to this function. On exit, @uts->fail_count is
  337. * incremented by the number of failures (0, one hopes)
  338. * @test_name: Test to run
  339. * @name: Name of test, possibly skipping a prefix that should not be displayed
  340. * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
  341. * any failed
  342. */
  343. static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
  344. const char *test_name)
  345. {
  346. const char *fname = strrchr(test->file, '/') + 1;
  347. const char *note = "";
  348. int ret;
  349. if ((test->flags & UT_TESTF_DM) && !uts->of_live)
  350. note = " (flat tree)";
  351. printf("Test: %s: %s%s\n", test_name, fname, note);
  352. /* Allow access to test state from drivers */
  353. test_set_state(uts);
  354. ret = test_pre_run(uts, test);
  355. if (ret == -EAGAIN)
  356. return skip_test(uts);
  357. if (ret)
  358. return ret;
  359. ret = test->func(uts);
  360. if (ret == -EAGAIN)
  361. skip_test(uts);
  362. ret = test_post_run(uts, test);
  363. if (ret)
  364. return ret;
  365. test_set_state( NULL);
  366. return 0;
  367. }
  368. /**
  369. * ut_run_test_live_flat() - Run a test with both live and flat tree
  370. *
  371. * This calls ut_run_test() with livetree enabled, which is the standard setup
  372. * for runnig tests. Then, for driver model test, it calls it again with
  373. * livetree disabled. This allows checking of flattree being used when OF_LIVE
  374. * is enabled, as is the case in U-Boot proper before relocation, as well as in
  375. * SPL.
  376. *
  377. * @uts: Test state to update. The caller should ensure that this is zeroed for
  378. * the first call to this function. On exit, @uts->fail_count is
  379. * incremented by the number of failures (0, one hopes)
  380. * @test: Test to run
  381. * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
  382. * any failed
  383. */
  384. static int ut_run_test_live_flat(struct unit_test_state *uts,
  385. struct unit_test *test)
  386. {
  387. int runs;
  388. if ((test->flags & UT_TESTF_OTHER_FDT) && !IS_ENABLED(CONFIG_SANDBOX))
  389. return skip_test(uts);
  390. /* Run with the live tree if possible */
  391. runs = 0;
  392. if (CONFIG_IS_ENABLED(OF_LIVE)) {
  393. if (!(test->flags & UT_TESTF_FLAT_TREE)) {
  394. uts->of_live = true;
  395. ut_assertok(ut_run_test(uts, test, test->name));
  396. runs++;
  397. }
  398. }
  399. /*
  400. * Run with the flat tree if:
  401. * - it is not marked for live tree only
  402. * - it doesn't require the 'other' FDT when OFNODE_MULTI_TREE_MAX is
  403. * not enabled (since flat tree can only support a single FDT in that
  404. * case
  405. * - we couldn't run it with live tree,
  406. * - it is a core test (dm tests except video)
  407. * - the FDT is still valid and has not been updated by an earlier test
  408. * (for sandbox we handle this by copying the tree, but not for other
  409. * boards)
  410. */
  411. if ((test->flags & UT_TESTF_SCAN_FDT) &&
  412. !(test->flags & UT_TESTF_LIVE_TREE) &&
  413. (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) ||
  414. !(test->flags & UT_TESTF_OTHER_FDT)) &&
  415. (!runs || ut_test_run_on_flattree(test)) &&
  416. !(gd->flags & GD_FLG_FDT_CHANGED)) {
  417. uts->of_live = false;
  418. ut_assertok(ut_run_test(uts, test, test->name));
  419. runs++;
  420. }
  421. return 0;
  422. }
  423. /**
  424. * ut_run_tests() - Run a set of tests
  425. *
  426. * This runs the tests, handling any preparation and clean-up needed. It prints
  427. * the name of each test before running it.
  428. *
  429. * @uts: Test state to update. The caller should ensure that this is zeroed for
  430. * the first call to this function. On exit, @uts->fail_count is
  431. * incremented by the number of failures (0, one hopes)
  432. * @prefix: String prefix for the tests. Any tests that have this prefix will be
  433. * printed without the prefix, so that it is easier to see the unique part
  434. * of the test name. If NULL, no prefix processing is done
  435. * @tests: List of tests to run
  436. * @count: Number of tests to run
  437. * @select_name: Name of a single test to run (from the list provided). If NULL
  438. * then all tests are run
  439. * Return: 0 if all tests passed, -ENOENT if test @select_name was not found,
  440. * -EBADF if any failed
  441. */
  442. static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
  443. struct unit_test *tests, int count,
  444. const char *select_name, const char *test_insert)
  445. {
  446. struct unit_test *test, *one;
  447. int found = 0;
  448. int pos = 0;
  449. int upto;
  450. one = NULL;
  451. if (test_insert) {
  452. char *p;
  453. pos = dectoul(test_insert, NULL);
  454. p = strchr(test_insert, ':');
  455. if (p)
  456. p++;
  457. for (test = tests; test < tests + count; test++) {
  458. if (!strcmp(p, test->name))
  459. one = test;
  460. }
  461. }
  462. for (upto = 0, test = tests; test < tests + count; test++, upto++) {
  463. const char *test_name = test->name;
  464. int ret, i, old_fail_count;
  465. if (!test_matches(prefix, test_name, select_name))
  466. continue;
  467. if (test->flags & UT_TESTF_MANUAL) {
  468. int len;
  469. /*
  470. * manual tests must have a name ending "_norun" as this
  471. * is how pytest knows to skip them. See
  472. * generate_ut_subtest() for this check.
  473. */
  474. len = strlen(test_name);
  475. if (len < 6 || strcmp(test_name + len - 6, "_norun")) {
  476. printf("Test %s is manual so must have a name ending in _norun\n",
  477. test_name);
  478. uts->fail_count++;
  479. return -EBADF;
  480. }
  481. if (!uts->force_run) {
  482. if (select_name) {
  483. printf("Test %s skipped as it is manual (use -f to run it)\n",
  484. test_name);
  485. }
  486. continue;
  487. }
  488. }
  489. old_fail_count = uts->fail_count;
  490. if (one && upto == pos) {
  491. ret = ut_run_test_live_flat(uts, one);
  492. if (uts->fail_count != old_fail_count) {
  493. printf("Test %s failed %d times (position %d)\n",
  494. one->name,
  495. uts->fail_count - old_fail_count, pos);
  496. }
  497. return -EBADF;
  498. }
  499. for (i = 0; i < uts->runs_per_test; i++)
  500. ret = ut_run_test_live_flat(uts, test);
  501. if (uts->fail_count != old_fail_count) {
  502. printf("Test %s failed %d times\n", select_name,
  503. uts->fail_count - old_fail_count);
  504. }
  505. found++;
  506. if (ret == -EAGAIN)
  507. continue;
  508. if (ret)
  509. return ret;
  510. }
  511. if (select_name && !found)
  512. return -ENOENT;
  513. return uts->fail_count ? -EBADF : 0;
  514. }
  515. int ut_run_list(const char *category, const char *prefix,
  516. struct unit_test *tests, int count, const char *select_name,
  517. int runs_per_test, bool force_run, const char *test_insert)
  518. {
  519. struct unit_test_state uts = { .fail_count = 0 };
  520. bool has_dm_tests = false;
  521. int ret;
  522. if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
  523. ut_list_has_dm_tests(tests, count)) {
  524. has_dm_tests = true;
  525. /*
  526. * If we have no device tree, or it only has a root node, then
  527. * these * tests clearly aren't going to work...
  528. */
  529. if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
  530. puts("Please run with test device tree:\n"
  531. " ./u-boot -d arch/sandbox/dts/test.dtb\n");
  532. return CMD_RET_FAILURE;
  533. }
  534. }
  535. if (!select_name)
  536. printf("Running %d %s tests\n", count, category);
  537. uts.of_root = gd_of_root();
  538. uts.runs_per_test = runs_per_test;
  539. if (fdt_action() == FDTCHK_COPY && gd->fdt_blob) {
  540. uts.fdt_size = fdt_totalsize(gd->fdt_blob);
  541. uts.fdt_copy = os_malloc(uts.fdt_size);
  542. if (!uts.fdt_copy) {
  543. printf("Out of memory for device tree copy\n");
  544. return -ENOMEM;
  545. }
  546. memcpy(uts.fdt_copy, gd->fdt_blob, uts.fdt_size);
  547. }
  548. uts.force_run = force_run;
  549. ret = ut_run_tests(&uts, prefix, tests, count, select_name,
  550. test_insert);
  551. /* Best efforts only...ignore errors */
  552. if (has_dm_tests)
  553. dm_test_restore(uts.of_root);
  554. if (IS_ENABLED(CONFIG_SANDBOX)) {
  555. os_free(uts.fdt_copy);
  556. os_free(uts.other_fdt);
  557. }
  558. if (uts.skip_count)
  559. printf("Skipped: %d, ", uts.skip_count);
  560. if (ret == -ENOENT)
  561. printf("Test '%s' not found\n", select_name);
  562. else
  563. printf("Failures: %d\n", uts.fail_count);
  564. return ret;
  565. }