1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2021 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
16 #include <dm/ofnode.h>
19 #include <dm/uclass-internal.h>
20 #include <test/test.h>
22 #include <u-boot/crc.h>
24 DECLARE_GLOBAL_DATA_PTR;
27 * enum fdtchk_t - what to do with the device tree (gd->fdt_blob)
29 * This affects what happens with the device tree before and after a test
31 * @FDTCHK_NONE: Do nothing
32 * @FDTCHK_CHECKSUM: Take a checksum of the FDT before the test runs and
33 * compare it afterwards to detect any changes
34 * @FDTCHK_COPY: Make a copy of the FDT and restore it afterwards
43 * fdt_action() - get the required action for the FDT
45 * @return the action that should be taken for this build
47 static enum fdtchk_t fdt_action(void)
49 /* Do a copy for sandbox (but only the U-Boot build, not SPL) */
50 if (CONFIG_IS_ENABLED(SANDBOX))
53 /* For sandbox SPL builds, do nothing */
54 if (IS_ENABLED(CONFIG_SANDBOX))
57 /* For all other boards, do a checksum */
58 return FDTCHK_CHECKSUM;
61 /* This is valid when a test is running, NULL otherwise */
62 static struct unit_test_state *cur_test_state;
64 struct unit_test_state *test_get_state(void)
66 return cur_test_state;
69 void test_set_state(struct unit_test_state *uts)
75 * dm_test_pre_run() - Get ready to run a driver model test
77 * This clears out the driver model data structures. For sandbox it resets the
82 static int dm_test_pre_run(struct unit_test_state *uts)
84 bool of_live = uts->of_live;
86 if (of_live && (gd->flags & GD_FLG_FDT_CHANGED)) {
87 printf("Cannot run live tree test as device tree changed\n");
92 uts->force_fail_alloc = false;
93 uts->skip_post_probe = false;
94 if (fdt_action() == FDTCHK_CHECKSUM)
95 uts->fdt_chksum = crc8(0, gd->fdt_blob,
96 fdt_totalsize(gd->fdt_blob));
98 malloc_disable_testing();
99 if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA))
100 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
101 arch_reset_for_test();
103 /* Determine whether to make the live tree available */
104 gd_set_of_root(of_live ? uts->of_root : NULL);
106 ut_assertok(dm_init(of_live));
107 uts->root = dm_root();
112 static int dm_test_post_run(struct unit_test_state *uts)
117 switch (fdt_action()) {
119 memcpy((void *)gd->fdt_blob, uts->fdt_copy, uts->fdt_size);
121 case FDTCHK_CHECKSUM: {
124 chksum = crc8(0, gd->fdt_blob, fdt_totalsize(gd->fdt_blob));
125 if (chksum != uts->fdt_chksum) {
127 * We cannot run any more tests that need the
128 * live tree, since its strings point into the
129 * flat tree, which has changed. This likely
130 * means that at least some of the pointers from
131 * the live tree point to different things
133 printf("Device tree changed: cannot run live tree tests\n");
134 gd->flags |= GD_FLG_FDT_CHANGED;
144 * With of-platdata-inst the uclasses are created at build time. If we
145 * destroy them we cannot get them back since uclass_add() is not
146 * supported. So skip this.
148 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
149 for (id = 0; id < UCLASS_COUNT; id++) {
153 * If the uclass doesn't exist we don't want to create
154 * it. So check that here before we call
155 * uclass_find_device().
157 uc = uclass_find(id);
160 ut_assertok(uclass_destroy(uc));
167 /* Ensure all the test devices are probed */
168 static int do_autoprobe(struct unit_test_state *uts)
170 return uclass_probe_all(UCLASS_TEST);
174 * ut_test_run_on_flattree() - Check if we should run a test with flat DT
176 * This skips long/slow tests where there is not much value in running a flat
177 * DT test in addition to a live DT test.
179 * Return: true to run the given test on the flat device tree
181 static bool ut_test_run_on_flattree(struct unit_test *test)
183 const char *fname = strrchr(test->file, '/') + 1;
185 if (!(test->flags & UT_TESTF_DM))
188 return !strstr(fname, "video") || strstr(test->name, "video_base");
192 * test_matches() - Check if a test should be run
194 * This checks if the a test should be run. In the normal case of running all
195 * tests, @select_name is NULL.
197 * @prefix: String prefix for the tests. Any tests that have this prefix will be
198 * printed without the prefix, so that it is easier to see the unique part
199 * of the test name. If NULL, any suite name (xxx_test) is considered to be
201 * @test_name: Name of current test
202 * @select_name: Name of test to run (or NULL for all)
203 * Return: true to run this test, false to skip it
205 static bool test_matches(const char *prefix, const char *test_name,
206 const char *select_name)
213 /* Allow glob expansion in the test name */
214 len = select_name[strlen(select_name) - 1] == '*' ? strlen(select_name) : 0;
218 if (!strncmp(test_name, select_name, len))
222 /* All tests have this prefix */
223 if (!strncmp(test_name, prefix, strlen(prefix)))
224 test_name += strlen(prefix);
226 const char *p = strstr(test_name, "_test_");
228 /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
230 test_name = p + strlen("_test_");
233 if (!strncmp(test_name, select_name, len))
240 * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
242 * @tests: List of tests to run
243 * @count: Number of tests to ru
244 * Return: true if any of the tests have the UT_TESTF_DM flag
246 static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
248 struct unit_test *test;
250 for (test = tests; test < tests + count; test++) {
251 if (test->flags & UT_TESTF_DM)
259 * dm_test_restore() Put things back to normal so sandbox works as expected
261 * @of_root: Value to set for of_root
262 * Return: 0 if OK, -ve on error
264 static int dm_test_restore(struct device_node *of_root)
268 gd_set_of_root(of_root);
270 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
274 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
281 * test_pre_run() - Handle any preparation needed to run a test
284 * @test: Test to prepare for
285 * Return: 0 if OK, -EAGAIN to skip this test since some required feature is not
286 * available, other -ve on error (meaning that testing cannot likely
289 static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
291 ut_assertok(event_init());
293 if (test->flags & UT_TESTF_DM)
294 ut_assertok(dm_test_pre_run(uts));
296 ut_set_skip_delays(uts, false);
298 uts->start = mallinfo();
300 if (test->flags & UT_TESTF_SCAN_PDATA)
301 ut_assertok(dm_scan_plat(false));
303 if (test->flags & UT_TESTF_PROBE_TEST)
304 ut_assertok(do_autoprobe(uts));
306 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
307 (test->flags & UT_TESTF_SCAN_FDT)) {
309 * only set this if we know the ethernet uclass will be created
311 eth_set_enable_bootdevs(test->flags & UT_TESTF_ETH_BOOTDEV);
312 test_sf_set_enable_bootdevs(test->flags & UT_TESTF_SF_BOOTDEV);
313 ut_assertok(dm_extended_scan(false));
317 * Do this after FDT scan since dm_scan_other() in bootstd-uclass.c
318 * checks for the existence of bootstd
320 if (test->flags & UT_TESTF_SCAN_PDATA)
321 ut_assertok(dm_scan_other(false));
323 if (IS_ENABLED(CONFIG_SANDBOX) && (test->flags & UT_TESTF_OTHER_FDT)) {
324 /* make sure the other FDT is available */
325 ut_assertok(test_load_other_fdt(uts));
328 * create a new live tree with it for every test, in case a
329 * test modifies the tree
331 if (of_live_active()) {
332 ut_assertok(unflatten_device_tree(uts->other_fdt,
337 if (test->flags & UT_TESTF_CONSOLE_REC) {
338 int ret = console_record_reset_enable();
341 printf("Skipping: Console recording disabled\n");
345 ut_silence_console(uts);
351 * test_post_run() - Handle cleaning up after a test
354 * @test: Test to clean up after
355 * Return: 0 if OK, -ve on error (meaning that testing cannot likely continue)
357 static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
359 ut_unsilence_console(uts);
360 if (test->flags & UT_TESTF_DM)
361 ut_assertok(dm_test_post_run(uts));
362 ut_assertok(cyclic_unregister_all());
363 ut_assertok(event_uninit());
366 uts->of_other = NULL;
374 * skip_test() - Handle skipping a test
376 * @uts: Test state to update
377 * @return -EAGAIN (always)
379 static int skip_test(struct unit_test_state *uts)
387 * ut_run_test() - Run a single test
389 * This runs the test, handling any preparation and clean-up needed. It prints
390 * the name of each test before running it.
392 * @uts: Test state to update. The caller should ensure that this is zeroed for
393 * the first call to this function. On exit, @uts->fail_count is
394 * incremented by the number of failures (0, one hopes)
395 * @test_name: Test to run
396 * @name: Name of test, possibly skipping a prefix that should not be displayed
397 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
400 static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
401 const char *test_name)
403 const char *fname = strrchr(test->file, '/') + 1;
404 const char *note = "";
407 if ((test->flags & UT_TESTF_DM) && !uts->of_live)
408 note = " (flat tree)";
409 printf("Test: %s: %s%s\n", test_name, fname, note);
411 /* Allow access to test state from drivers */
414 ret = test_pre_run(uts, test);
416 return skip_test(uts);
420 ret = test->func(uts);
424 ret = test_post_run(uts, test);
428 test_set_state( NULL);
434 * ut_run_test_live_flat() - Run a test with both live and flat tree
436 * This calls ut_run_test() with livetree enabled, which is the standard setup
437 * for runnig tests. Then, for driver model test, it calls it again with
438 * livetree disabled. This allows checking of flattree being used when OF_LIVE
439 * is enabled, as is the case in U-Boot proper before relocation, as well as in
442 * @uts: Test state to update. The caller should ensure that this is zeroed for
443 * the first call to this function. On exit, @uts->fail_count is
444 * incremented by the number of failures (0, one hopes)
446 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
449 static int ut_run_test_live_flat(struct unit_test_state *uts,
450 struct unit_test *test)
454 if ((test->flags & UT_TESTF_OTHER_FDT) && !IS_ENABLED(CONFIG_SANDBOX))
455 return skip_test(uts);
457 /* Run with the live tree if possible */
459 if (CONFIG_IS_ENABLED(OF_LIVE)) {
460 if (!(test->flags & UT_TESTF_FLAT_TREE)) {
462 ut_assertok(ut_run_test(uts, test, test->name));
468 * Run with the flat tree if:
469 * - it is not marked for live tree only
470 * - it doesn't require the 'other' FDT when OFNODE_MULTI_TREE_MAX is
471 * not enabled (since flat tree can only support a single FDT in that
473 * - we couldn't run it with live tree,
474 * - it is a core test (dm tests except video)
475 * - the FDT is still valid and has not been updated by an earlier test
476 * (for sandbox we handle this by copying the tree, but not for other
479 if (!(test->flags & UT_TESTF_LIVE_TREE) &&
480 (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) ||
481 !(test->flags & UT_TESTF_OTHER_FDT)) &&
482 (!runs || ut_test_run_on_flattree(test)) &&
483 !(gd->flags & GD_FLG_FDT_CHANGED)) {
484 uts->of_live = false;
485 ut_assertok(ut_run_test(uts, test, test->name));
493 * ut_run_tests() - Run a set of tests
495 * This runs the tests, handling any preparation and clean-up needed. It prints
496 * the name of each test before running it.
498 * @uts: Test state to update. The caller should ensure that this is zeroed for
499 * the first call to this function. On exit, @uts->fail_count is
500 * incremented by the number of failures (0, one hopes)
501 * @prefix: String prefix for the tests. Any tests that have this prefix will be
502 * printed without the prefix, so that it is easier to see the unique part
503 * of the test name. If NULL, no prefix processing is done
504 * @tests: List of tests to run
505 * @count: Number of tests to run
506 * @select_name: Name of a single test to run (from the list provided). If NULL
507 * then all tests are run
508 * Return: 0 if all tests passed, -ENOENT if test @select_name was not found,
509 * -EBADF if any failed
511 static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
512 struct unit_test *tests, int count,
513 const char *select_name, const char *test_insert)
515 struct unit_test *test, *one;
524 pos = dectoul(test_insert, NULL);
525 p = strchr(test_insert, ':');
529 for (test = tests; test < tests + count; test++) {
530 if (!strcmp(p, test->name))
535 for (upto = 0, test = tests; test < tests + count; test++, upto++) {
536 const char *test_name = test->name;
537 int ret, i, old_fail_count;
539 if (!test_matches(prefix, test_name, select_name))
542 if (test->flags & UT_TESTF_MANUAL) {
546 * manual tests must have a name ending "_norun" as this
547 * is how pytest knows to skip them. See
548 * generate_ut_subtest() for this check.
550 len = strlen(test_name);
551 if (len < 6 || strcmp(test_name + len - 6, "_norun")) {
552 printf("Test %s is manual so must have a name ending in _norun\n",
557 if (!uts->force_run) {
559 printf("Test %s skipped as it is manual (use -f to run it)\n",
565 old_fail_count = uts->fail_count;
567 if (one && upto == pos) {
568 ret = ut_run_test_live_flat(uts, one);
569 if (uts->fail_count != old_fail_count) {
570 printf("Test %s failed %d times (position %d)\n",
572 uts->fail_count - old_fail_count, pos);
577 for (i = 0; i < uts->runs_per_test; i++)
578 ret = ut_run_test_live_flat(uts, test);
579 if (uts->fail_count != old_fail_count) {
580 printf("Test %s failed %d times\n", select_name,
581 uts->fail_count - old_fail_count);
589 if (select_name && !found)
592 return uts->fail_count ? -EBADF : 0;
595 int ut_run_list(const char *category, const char *prefix,
596 struct unit_test *tests, int count, const char *select_name,
597 int runs_per_test, bool force_run, const char *test_insert)
599 struct unit_test_state uts = { .fail_count = 0 };
600 bool has_dm_tests = false;
603 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
604 ut_list_has_dm_tests(tests, count)) {
607 * If we have no device tree, or it only has a root node, then
608 * these * tests clearly aren't going to work...
610 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
611 puts("Please run with test device tree:\n"
612 " ./u-boot -d arch/sandbox/dts/test.dtb\n");
613 return CMD_RET_FAILURE;
618 printf("Running %d %s tests\n", count, category);
620 uts.of_root = gd_of_root();
621 uts.runs_per_test = runs_per_test;
622 if (fdt_action() == FDTCHK_COPY && gd->fdt_blob) {
623 uts.fdt_size = fdt_totalsize(gd->fdt_blob);
624 uts.fdt_copy = os_malloc(uts.fdt_size);
626 printf("Out of memory for device tree copy\n");
629 memcpy(uts.fdt_copy, gd->fdt_blob, uts.fdt_size);
631 uts.force_run = force_run;
632 ret = ut_run_tests(&uts, prefix, tests, count, select_name,
635 /* Best efforts only...ignore errors */
637 dm_test_restore(uts.of_root);
638 if (IS_ENABLED(CONFIG_SANDBOX)) {
639 os_free(uts.fdt_copy);
640 os_free(uts.other_fdt);
644 printf("Skipped: %d, ", uts.skip_count);
646 printf("Test '%s' not found\n", select_name);
648 printf("Failures: %d\n", uts.fail_count);