1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2021 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
15 #include <dm/ofnode.h>
18 #include <dm/uclass-internal.h>
19 #include <test/test.h>
21 #include <u-boot/crc.h>
23 DECLARE_GLOBAL_DATA_PTR;
26 * enum fdtchk_t - what to do with the device tree (gd->fdt_blob)
28 * This affects what happens with the device tree before and after a test
30 * @FDTCHK_NONE: Do nothing
31 * @FDTCHK_CHECKSUM: Take a checksum of the FDT before the test runs and
32 * compare it afterwards to detect any changes
33 * @FDTCHK_COPY: Make a copy of the FDT and restore it afterwards
42 * fdt_action() - get the required action for the FDT
44 * @return the action that should be taken for this build
46 static enum fdtchk_t fdt_action(void)
48 /* Do a copy for sandbox (but only the U-Boot build, not SPL) */
49 if (CONFIG_IS_ENABLED(SANDBOX))
52 /* For sandbox SPL builds, do nothing */
53 if (IS_ENABLED(CONFIG_SANDBOX))
56 /* For all other boards, do a checksum */
57 return FDTCHK_CHECKSUM;
60 /* This is valid when a test is running, NULL otherwise */
61 static struct unit_test_state *cur_test_state;
63 struct unit_test_state *test_get_state(void)
65 return cur_test_state;
68 void test_set_state(struct unit_test_state *uts)
74 * dm_test_pre_run() - Get ready to run a driver model test
76 * This clears out the driver model data structures. For sandbox it resets the
81 static int dm_test_pre_run(struct unit_test_state *uts)
83 bool of_live = uts->of_live;
85 if (of_live && (gd->flags & GD_FLG_FDT_CHANGED)) {
86 printf("Cannot run live tree test as device tree changed\n");
91 uts->force_fail_alloc = false;
92 uts->skip_post_probe = false;
93 if (fdt_action() == FDTCHK_CHECKSUM)
94 uts->fdt_chksum = crc8(0, gd->fdt_blob,
95 fdt_totalsize(gd->fdt_blob));
97 malloc_disable_testing();
98 if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA))
99 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
100 arch_reset_for_test();
102 /* Determine whether to make the live tree available */
103 gd_set_of_root(of_live ? uts->of_root : NULL);
105 ut_assertok(dm_init(of_live));
106 uts->root = dm_root();
111 static int dm_test_post_run(struct unit_test_state *uts)
116 switch (fdt_action()) {
118 memcpy((void *)gd->fdt_blob, uts->fdt_copy, uts->fdt_size);
120 case FDTCHK_CHECKSUM: {
123 chksum = crc8(0, gd->fdt_blob, fdt_totalsize(gd->fdt_blob));
124 if (chksum != uts->fdt_chksum) {
126 * We cannot run any more tests that need the
127 * live tree, since its strings point into the
128 * flat tree, which has changed. This likely
129 * means that at least some of the pointers from
130 * the live tree point to different things
132 printf("Device tree changed: cannot run live tree tests\n");
133 gd->flags |= GD_FLG_FDT_CHANGED;
143 * With of-platdata-inst the uclasses are created at build time. If we
144 * destroy them we cannot get them back since uclass_add() is not
145 * supported. So skip this.
147 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
148 for (id = 0; id < UCLASS_COUNT; id++) {
152 * If the uclass doesn't exist we don't want to create
153 * it. So check that here before we call
154 * uclass_find_device().
156 uc = uclass_find(id);
159 ut_assertok(uclass_destroy(uc));
166 /* Ensure all the test devices are probed */
167 static int do_autoprobe(struct unit_test_state *uts)
169 return uclass_probe_all(UCLASS_TEST);
173 * ut_test_run_on_flattree() - Check if we should run a test with flat DT
175 * This skips long/slow tests where there is not much value in running a flat
176 * DT test in addition to a live DT test.
178 * Return: true to run the given test on the flat device tree
180 static bool ut_test_run_on_flattree(struct unit_test *test)
182 const char *fname = strrchr(test->file, '/') + 1;
184 if (!(test->flags & UT_TESTF_DM))
187 return !strstr(fname, "video") || strstr(test->name, "video_base");
191 * test_matches() - Check if a test should be run
193 * This checks if the a test should be run. In the normal case of running all
194 * tests, @select_name is NULL.
196 * @prefix: String prefix for the tests. Any tests that have this prefix will be
197 * printed without the prefix, so that it is easier to see the unique part
198 * of the test name. If NULL, any suite name (xxx_test) is considered to be
200 * @test_name: Name of current test
201 * @select_name: Name of test to run (or NULL for all)
202 * Return: true to run this test, false to skip it
204 static bool test_matches(const char *prefix, const char *test_name,
205 const char *select_name)
212 /* Allow glob expansion in the test name */
213 len = select_name[strlen(select_name) - 1] == '*' ? strlen(select_name) : 0;
217 if (!strncmp(test_name, select_name, len))
221 /* All tests have this prefix */
222 if (!strncmp(test_name, prefix, strlen(prefix)))
223 test_name += strlen(prefix);
225 const char *p = strstr(test_name, "_test_");
227 /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
229 test_name = p + strlen("_test_");
232 if (!strncmp(test_name, select_name, len))
239 * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
241 * @tests: List of tests to run
242 * @count: Number of tests to ru
243 * Return: true if any of the tests have the UT_TESTF_DM flag
245 static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
247 struct unit_test *test;
249 for (test = tests; test < tests + count; test++) {
250 if (test->flags & UT_TESTF_DM)
258 * dm_test_restore() Put things back to normal so sandbox works as expected
260 * @of_root: Value to set for of_root
261 * Return: 0 if OK, -ve on error
263 static int dm_test_restore(struct device_node *of_root)
267 gd_set_of_root(of_root);
269 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
273 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
280 * test_pre_run() - Handle any preparation needed to run a test
283 * @test: Test to prepare for
284 * Return: 0 if OK, -EAGAIN to skip this test since some required feature is not
285 * available, other -ve on error (meaning that testing cannot likely
288 static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
290 ut_assertok(event_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_unregister_all());
351 ut_assertok(event_uninit());
354 uts->of_other = NULL;
362 * skip_test() - Handle skipping a test
364 * @uts: Test state to update
365 * @return -EAGAIN (always)
367 static int skip_test(struct unit_test_state *uts)
375 * ut_run_test() - Run a single test
377 * This runs the test, handling any preparation and clean-up needed. It prints
378 * the name of each test before running it.
380 * @uts: Test state to update. The caller should ensure that this is zeroed for
381 * the first call to this function. On exit, @uts->fail_count is
382 * incremented by the number of failures (0, one hopes)
383 * @test_name: Test to run
384 * @name: Name of test, possibly skipping a prefix that should not be displayed
385 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
388 static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
389 const char *test_name)
391 const char *fname = strrchr(test->file, '/') + 1;
392 const char *note = "";
395 if ((test->flags & UT_TESTF_DM) && !uts->of_live)
396 note = " (flat tree)";
397 printf("Test: %s: %s%s\n", test_name, fname, note);
399 /* Allow access to test state from drivers */
402 ret = test_pre_run(uts, test);
404 return skip_test(uts);
408 ret = test->func(uts);
412 ret = test_post_run(uts, test);
416 test_set_state( NULL);
422 * ut_run_test_live_flat() - Run a test with both live and flat tree
424 * This calls ut_run_test() with livetree enabled, which is the standard setup
425 * for runnig tests. Then, for driver model test, it calls it again with
426 * livetree disabled. This allows checking of flattree being used when OF_LIVE
427 * is enabled, as is the case in U-Boot proper before relocation, as well as in
430 * @uts: Test state to update. The caller should ensure that this is zeroed for
431 * the first call to this function. On exit, @uts->fail_count is
432 * incremented by the number of failures (0, one hopes)
434 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
437 static int ut_run_test_live_flat(struct unit_test_state *uts,
438 struct unit_test *test)
442 if ((test->flags & UT_TESTF_OTHER_FDT) && !IS_ENABLED(CONFIG_SANDBOX))
443 return skip_test(uts);
445 /* Run with the live tree if possible */
447 if (CONFIG_IS_ENABLED(OF_LIVE)) {
448 if (!(test->flags & UT_TESTF_FLAT_TREE)) {
450 ut_assertok(ut_run_test(uts, test, test->name));
456 * Run with the flat tree if:
457 * - it is not marked for live tree only
458 * - it doesn't require the 'other' FDT when OFNODE_MULTI_TREE_MAX is
459 * not enabled (since flat tree can only support a single FDT in that
461 * - we couldn't run it with live tree,
462 * - it is a core test (dm tests except video)
463 * - the FDT is still valid and has not been updated by an earlier test
464 * (for sandbox we handle this by copying the tree, but not for other
467 if (!(test->flags & UT_TESTF_LIVE_TREE) &&
468 (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) ||
469 !(test->flags & UT_TESTF_OTHER_FDT)) &&
470 (!runs || ut_test_run_on_flattree(test)) &&
471 !(gd->flags & GD_FLG_FDT_CHANGED)) {
472 uts->of_live = false;
473 ut_assertok(ut_run_test(uts, test, test->name));
481 * ut_run_tests() - Run a set of tests
483 * This runs the tests, handling any preparation and clean-up needed. It prints
484 * the name of each test before running it.
486 * @uts: Test state to update. The caller should ensure that this is zeroed for
487 * the first call to this function. On exit, @uts->fail_count is
488 * incremented by the number of failures (0, one hopes)
489 * @prefix: String prefix for the tests. Any tests that have this prefix will be
490 * printed without the prefix, so that it is easier to see the unique part
491 * of the test name. If NULL, no prefix processing is done
492 * @tests: List of tests to run
493 * @count: Number of tests to run
494 * @select_name: Name of a single test to run (from the list provided). If NULL
495 * then all tests are run
496 * Return: 0 if all tests passed, -ENOENT if test @select_name was not found,
497 * -EBADF if any failed
499 static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
500 struct unit_test *tests, int count,
501 const char *select_name, const char *test_insert)
503 struct unit_test *test, *one;
512 pos = dectoul(test_insert, NULL);
513 p = strchr(test_insert, ':');
517 for (test = tests; test < tests + count; test++) {
518 if (!strcmp(p, test->name))
523 for (upto = 0, test = tests; test < tests + count; test++, upto++) {
524 const char *test_name = test->name;
525 int ret, i, old_fail_count;
527 if (!test_matches(prefix, test_name, select_name))
530 if (test->flags & UT_TESTF_MANUAL) {
534 * manual tests must have a name ending "_norun" as this
535 * is how pytest knows to skip them. See
536 * generate_ut_subtest() for this check.
538 len = strlen(test_name);
539 if (len < 6 || strcmp(test_name + len - 6, "_norun")) {
540 printf("Test %s is manual so must have a name ending in _norun\n",
545 if (!uts->force_run) {
547 printf("Test %s skipped as it is manual (use -f to run it)\n",
553 old_fail_count = uts->fail_count;
555 if (one && upto == pos) {
556 ret = ut_run_test_live_flat(uts, one);
557 if (uts->fail_count != old_fail_count) {
558 printf("Test %s failed %d times (position %d)\n",
560 uts->fail_count - old_fail_count, pos);
565 for (i = 0; i < uts->runs_per_test; i++)
566 ret = ut_run_test_live_flat(uts, test);
567 if (uts->fail_count != old_fail_count) {
568 printf("Test %s failed %d times\n", select_name,
569 uts->fail_count - old_fail_count);
577 if (select_name && !found)
580 return uts->fail_count ? -EBADF : 0;
583 int ut_run_list(const char *category, const char *prefix,
584 struct unit_test *tests, int count, const char *select_name,
585 int runs_per_test, bool force_run, const char *test_insert)
587 struct unit_test_state uts = { .fail_count = 0 };
588 bool has_dm_tests = false;
591 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
592 ut_list_has_dm_tests(tests, count)) {
595 * If we have no device tree, or it only has a root node, then
596 * these * tests clearly aren't going to work...
598 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
599 puts("Please run with test device tree:\n"
600 " ./u-boot -d arch/sandbox/dts/test.dtb\n");
601 return CMD_RET_FAILURE;
606 printf("Running %d %s tests\n", count, category);
608 uts.of_root = gd_of_root();
609 uts.runs_per_test = runs_per_test;
610 if (fdt_action() == FDTCHK_COPY && gd->fdt_blob) {
611 uts.fdt_size = fdt_totalsize(gd->fdt_blob);
612 uts.fdt_copy = os_malloc(uts.fdt_size);
614 printf("Out of memory for device tree copy\n");
617 memcpy(uts.fdt_copy, gd->fdt_blob, uts.fdt_size);
619 uts.force_run = force_run;
620 ret = ut_run_tests(&uts, prefix, tests, count, select_name,
623 /* Best efforts only...ignore errors */
625 dm_test_restore(uts.of_root);
626 if (IS_ENABLED(CONFIG_SANDBOX)) {
627 os_free(uts.fdt_copy);
628 os_free(uts.other_fdt);
632 printf("Skipped: %d, ", uts.skip_count);
634 printf("Test '%s' not found\n", select_name);
636 printf("Failures: %d\n", uts.fail_count);
638 /* Best efforts only...ignore errors */
640 dm_test_restore(uts.of_root);