1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2021 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
14 #include <dm/ofnode.h>
17 #include <dm/uclass-internal.h>
18 #include <test/test.h>
20 #include <u-boot/crc.h>
22 DECLARE_GLOBAL_DATA_PTR;
25 * enum fdtchk_t - what to do with the device tree (gd->fdt_blob)
27 * This affects what happens with the device tree before and after a test
29 * @FDTCHK_NONE: Do nothing
30 * @FDTCHK_CHECKSUM: Take a checksum of the FDT before the test runs and
31 * compare it afterwards to detect any changes
32 * @FDTCHK_COPY: Make a copy of the FDT and restore it afterwards
41 * fdt_action() - get the required action for the FDT
43 * @return the action that should be taken for this build
45 static enum fdtchk_t fdt_action(void)
47 /* Do a copy for sandbox (but only the U-Boot build, not SPL) */
48 if (CONFIG_IS_ENABLED(SANDBOX))
51 /* For sandbox SPL builds, do nothing */
52 if (IS_ENABLED(CONFIG_SANDBOX))
55 /* For all other boards, do a checksum */
56 return FDTCHK_CHECKSUM;
59 /* This is valid when a test is running, NULL otherwise */
60 static struct unit_test_state *cur_test_state;
62 struct unit_test_state *test_get_state(void)
64 return cur_test_state;
67 void test_set_state(struct unit_test_state *uts)
73 * dm_test_pre_run() - Get ready to run a driver model test
75 * This clears out the driver model data structures. For sandbox it resets the
80 static int dm_test_pre_run(struct unit_test_state *uts)
82 bool of_live = uts->of_live;
84 if (of_live && (gd->flags & GD_FLG_FDT_CHANGED)) {
85 printf("Cannot run live tree test as device tree changed\n");
90 uts->force_fail_alloc = false;
91 uts->skip_post_probe = false;
92 if (fdt_action() == FDTCHK_CHECKSUM)
93 uts->fdt_chksum = crc8(0, gd->fdt_blob,
94 fdt_totalsize(gd->fdt_blob));
96 malloc_disable_testing();
97 if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA))
98 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
99 arch_reset_for_test();
101 /* Determine whether to make the live tree available */
102 gd_set_of_root(of_live ? uts->of_root : NULL);
104 ut_assertok(dm_init(of_live));
105 uts->root = dm_root();
110 static int dm_test_post_run(struct unit_test_state *uts)
115 switch (fdt_action()) {
117 memcpy((void *)gd->fdt_blob, uts->fdt_copy, uts->fdt_size);
119 case FDTCHK_CHECKSUM: {
122 chksum = crc8(0, gd->fdt_blob, fdt_totalsize(gd->fdt_blob));
123 if (chksum != uts->fdt_chksum) {
125 * We cannot run any more tests that need the
126 * live tree, since its strings point into the
127 * flat tree, which has changed. This likely
128 * means that at least some of the pointers from
129 * the live tree point to different things
131 printf("Device tree changed: cannot run live tree tests\n");
132 gd->flags |= GD_FLG_FDT_CHANGED;
142 * With of-platdata-inst the uclasses are created at build time. If we
143 * destroy them we cannot get them back since uclass_add() is not
144 * supported. So skip this.
146 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
147 for (id = 0; id < UCLASS_COUNT; id++) {
151 * If the uclass doesn't exist we don't want to create
152 * it. So check that here before we call
153 * uclass_find_device().
155 uc = uclass_find(id);
158 ut_assertok(uclass_destroy(uc));
165 /* Ensure all the test devices are probed */
166 static int do_autoprobe(struct unit_test_state *uts)
168 return uclass_probe_all(UCLASS_TEST);
172 * ut_test_run_on_flattree() - Check if we should run a test with flat DT
174 * This skips long/slow tests where there is not much value in running a flat
175 * DT test in addition to a live DT test.
177 * Return: true to run the given test on the flat device tree
179 static bool ut_test_run_on_flattree(struct unit_test *test)
181 const char *fname = strrchr(test->file, '/') + 1;
183 if (!(test->flags & UT_TESTF_DM))
186 return !strstr(fname, "video") || strstr(test->name, "video_base");
190 * test_matches() - Check if a test should be run
192 * This checks if the a test should be run. In the normal case of running all
193 * tests, @select_name is NULL.
195 * @prefix: String prefix for the tests. Any tests that have this prefix will be
196 * printed without the prefix, so that it is easier to see the unique part
197 * of the test name. If NULL, any suite name (xxx_test) is considered to be
199 * @test_name: Name of current test
200 * @select_name: Name of test to run (or NULL for all)
201 * Return: true to run this test, false to skip it
203 static bool test_matches(const char *prefix, const char *test_name,
204 const char *select_name)
211 /* Allow glob expansion in the test name */
212 len = select_name[strlen(select_name) - 1] == '*' ? strlen(select_name) : 0;
216 if (!strncmp(test_name, select_name, len))
220 /* All tests have this prefix */
221 if (!strncmp(test_name, prefix, strlen(prefix)))
222 test_name += strlen(prefix);
224 const char *p = strstr(test_name, "_test_");
226 /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
228 test_name = p + strlen("_test_");
231 if (!strncmp(test_name, select_name, len))
238 * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
240 * @tests: List of tests to run
241 * @count: Number of tests to ru
242 * Return: true if any of the tests have the UT_TESTF_DM flag
244 static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
246 struct unit_test *test;
248 for (test = tests; test < tests + count; test++) {
249 if (test->flags & UT_TESTF_DM)
257 * dm_test_restore() Put things back to normal so sandbox works as expected
259 * @of_root: Value to set for of_root
260 * Return: 0 if OK, -ve on error
262 static int dm_test_restore(struct device_node *of_root)
266 gd_set_of_root(of_root);
268 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
272 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
279 * test_pre_run() - Handle any preparation needed to run a test
282 * @test: Test to prepare for
283 * Return: 0 if OK, -EAGAIN to skip this test since some required feature is not
284 * available, other -ve on error (meaning that testing cannot likely
287 static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
289 ut_assertok(event_init());
290 ut_assertok(cyclic_init());
292 if (test->flags & UT_TESTF_DM)
293 ut_assertok(dm_test_pre_run(uts));
295 ut_set_skip_delays(uts, false);
297 uts->start = mallinfo();
299 if (test->flags & UT_TESTF_SCAN_PDATA) {
300 ut_assertok(dm_scan_plat(false));
301 ut_assertok(dm_scan_other(false));
304 if (test->flags & UT_TESTF_PROBE_TEST)
305 ut_assertok(do_autoprobe(uts));
307 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
308 (test->flags & UT_TESTF_SCAN_FDT))
309 ut_assertok(dm_extended_scan(false));
311 if (IS_ENABLED(CONFIG_SANDBOX) && (test->flags & UT_TESTF_OTHER_FDT)) {
312 /* make sure the other FDT is available */
313 ut_assertok(test_load_other_fdt(uts));
316 * create a new live tree with it for every test, in case a
317 * test modifies the tree
319 if (of_live_active()) {
320 ut_assertok(unflatten_device_tree(uts->other_fdt,
325 if (test->flags & UT_TESTF_CONSOLE_REC) {
326 int ret = console_record_reset_enable();
329 printf("Skipping: Console recording disabled\n");
333 ut_silence_console(uts);
339 * test_post_run() - Handle cleaning up after a test
342 * @test: Test to clean up after
343 * Return: 0 if OK, -ve on error (meaning that testing cannot likely continue)
345 static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
347 ut_unsilence_console(uts);
348 if (test->flags & UT_TESTF_DM)
349 ut_assertok(dm_test_post_run(uts));
350 ut_assertok(cyclic_uninit());
351 ut_assertok(event_uninit());
354 uts->of_other = NULL;
360 * skip_test() - Handle skipping a test
362 * @uts: Test state to update
363 * @return -EAGAIN (always)
365 static int skip_test(struct unit_test_state *uts)
373 * ut_run_test() - Run a single test
375 * This runs the test, handling any preparation and clean-up needed. It prints
376 * the name of each test before running it.
378 * @uts: Test state to update. The caller should ensure that this is zeroed for
379 * the first call to this function. On exit, @uts->fail_count is
380 * incremented by the number of failures (0, one hopes)
381 * @test_name: Test to run
382 * @name: Name of test, possibly skipping a prefix that should not be displayed
383 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
386 static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
387 const char *test_name)
389 const char *fname = strrchr(test->file, '/') + 1;
390 const char *note = "";
393 if ((test->flags & UT_TESTF_DM) && !uts->of_live)
394 note = " (flat tree)";
395 printf("Test: %s: %s%s\n", test_name, fname, note);
397 /* Allow access to test state from drivers */
400 ret = test_pre_run(uts, test);
402 return skip_test(uts);
406 ret = test->func(uts);
410 ret = test_post_run(uts, test);
414 test_set_state( NULL);
420 * ut_run_test_live_flat() - Run a test with both live and flat tree
422 * This calls ut_run_test() with livetree enabled, which is the standard setup
423 * for runnig tests. Then, for driver model test, it calls it again with
424 * livetree disabled. This allows checking of flattree being used when OF_LIVE
425 * is enabled, as is the case in U-Boot proper before relocation, as well as in
428 * @uts: Test state to update. The caller should ensure that this is zeroed for
429 * the first call to this function. On exit, @uts->fail_count is
430 * incremented by the number of failures (0, one hopes)
432 * @name: Name of test, possibly skipping a prefix that should not be displayed
433 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
436 static int ut_run_test_live_flat(struct unit_test_state *uts,
437 struct unit_test *test, const char *name)
441 if ((test->flags & UT_TESTF_OTHER_FDT) && !IS_ENABLED(CONFIG_SANDBOX))
442 return skip_test(uts);
444 /* Run with the live tree if possible */
446 if (CONFIG_IS_ENABLED(OF_LIVE)) {
447 if (!(test->flags & UT_TESTF_FLAT_TREE)) {
449 ut_assertok(ut_run_test(uts, test, test->name));
455 * Run with the flat tree if:
456 * - it is not marked for live tree only
457 * - it doesn't require the 'other' FDT when OFNODE_MULTI_TREE_MAX is
458 * not enabled (since flat tree can only support a single FDT in that
460 * - we couldn't run it with live tree,
461 * - it is a core test (dm tests except video)
462 * - the FDT is still valid and has not been updated by an earlier test
463 * (for sandbox we handle this by copying the tree, but not for other
466 if (!(test->flags & UT_TESTF_LIVE_TREE) &&
467 (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) ||
468 !(test->flags & UT_TESTF_OTHER_FDT)) &&
469 (!runs || ut_test_run_on_flattree(test)) &&
470 !(gd->flags & GD_FLG_FDT_CHANGED)) {
471 uts->of_live = false;
472 ut_assertok(ut_run_test(uts, test, test->name));
480 * ut_run_tests() - Run a set of tests
482 * This runs the tests, handling any preparation and clean-up needed. It prints
483 * the name of each test before running it.
485 * @uts: Test state to update. The caller should ensure that this is zeroed for
486 * the first call to this function. On exit, @uts->fail_count is
487 * incremented by the number of failures (0, one hopes)
488 * @prefix: String prefix for the tests. Any tests that have this prefix will be
489 * printed without the prefix, so that it is easier to see the unique part
490 * of the test name. If NULL, no prefix processing is done
491 * @tests: List of tests to run
492 * @count: Number of tests to run
493 * @select_name: Name of a single test to run (from the list provided). If NULL
494 * then all tests are run
495 * Return: 0 if all tests passed, -ENOENT if test @select_name was not found,
496 * -EBADF if any failed
498 static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
499 struct unit_test *tests, int count,
500 const char *select_name)
502 struct unit_test *test;
505 for (test = tests; test < tests + count; test++) {
506 const char *test_name = test->name;
507 int ret, i, old_fail_count;
509 if (!test_matches(prefix, test_name, select_name))
511 old_fail_count = uts->fail_count;
512 for (i = 0; i < uts->runs_per_test; i++)
513 ret = ut_run_test_live_flat(uts, test, select_name);
514 if (uts->fail_count != old_fail_count) {
515 printf("Test %s failed %d times\n", select_name,
516 uts->fail_count - old_fail_count);
524 if (select_name && !found)
527 return uts->fail_count ? -EBADF : 0;
530 int ut_run_list(const char *category, const char *prefix,
531 struct unit_test *tests, int count, const char *select_name,
534 struct unit_test_state uts = { .fail_count = 0 };
535 bool has_dm_tests = false;
538 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
539 ut_list_has_dm_tests(tests, count)) {
542 * If we have no device tree, or it only has a root node, then
543 * these * tests clearly aren't going to work...
545 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
546 puts("Please run with test device tree:\n"
547 " ./u-boot -d arch/sandbox/dts/test.dtb\n");
548 return CMD_RET_FAILURE;
553 printf("Running %d %s tests\n", count, category);
555 uts.of_root = gd_of_root();
556 uts.runs_per_test = runs_per_test;
557 if (fdt_action() == FDTCHK_COPY && gd->fdt_blob) {
558 uts.fdt_size = fdt_totalsize(gd->fdt_blob);
559 uts.fdt_copy = os_malloc(uts.fdt_size);
561 printf("Out of memory for device tree copy\n");
564 memcpy(uts.fdt_copy, gd->fdt_blob, uts.fdt_size);
566 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
568 /* Best efforts only...ignore errors */
570 dm_test_restore(uts.of_root);
571 if (IS_ENABLED(CONFIG_SANDBOX)) {
572 os_free(uts.fdt_copy);
573 os_free(uts.other_fdt);
577 printf("Skipped: %d, ", uts.skip_count);
579 printf("Test '%s' not found\n", select_name);
581 printf("Failures: %d\n", uts.fail_count);
583 /* Best efforts only...ignore errors */
585 dm_test_restore(uts.of_root);