1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2021 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
15 #include <dm/uclass-internal.h>
16 #include <test/test.h>
18 #include <u-boot/crc.h>
20 DECLARE_GLOBAL_DATA_PTR;
23 * enum fdtchk_t - what to do with the device tree (gd->fdt_blob)
25 * This affects what happens with the device tree before and after a test
27 * @FDTCHK_NONE: Do nothing
28 * @FDTCHK_CHECKSUM: Take a checksum of the FDT before the test runs and
29 * compare it afterwards to detect any changes
30 * @FDTCHK_COPY: Make a copy of the FDT and restore it afterwards
39 * fdt_action() - get the required action for the FDT
41 * @return the action that should be taken for this build
43 static enum fdtchk_t fdt_action(void)
45 /* Do a copy for sandbox (but only the U-Boot build, not SPL) */
46 if (CONFIG_IS_ENABLED(SANDBOX))
49 /* For sandbox SPL builds, do nothing */
50 if (IS_ENABLED(CONFIG_SANDBOX))
53 /* For all other boards, do a checksum */
54 return FDTCHK_CHECKSUM;
57 /* This is valid when a test is running, NULL otherwise */
58 static struct unit_test_state *cur_test_state;
60 struct unit_test_state *test_get_state(void)
62 return cur_test_state;
65 void test_set_state(struct unit_test_state *uts)
71 * dm_test_pre_run() - Get ready to run a driver model test
73 * This clears out the driver model data structures. For sandbox it resets the
78 static int dm_test_pre_run(struct unit_test_state *uts)
80 bool of_live = uts->of_live;
82 if (of_live && (gd->flags & GD_FLG_FDT_CHANGED)) {
83 printf("Cannot run live tree test as device tree changed\n");
88 uts->force_fail_alloc = false;
89 uts->skip_post_probe = false;
90 if (fdt_action() == FDTCHK_CHECKSUM)
91 uts->fdt_chksum = crc8(0, gd->fdt_blob,
92 fdt_totalsize(gd->fdt_blob));
94 malloc_disable_testing();
95 if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA))
96 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
97 arch_reset_for_test();
99 /* Determine whether to make the live tree available */
100 gd_set_of_root(of_live ? uts->of_root : NULL);
101 ut_assertok(dm_init(of_live));
102 uts->root = dm_root();
107 static int dm_test_post_run(struct unit_test_state *uts)
112 switch (fdt_action()) {
114 memcpy((void *)gd->fdt_blob, uts->fdt_copy, uts->fdt_size);
116 case FDTCHK_CHECKSUM: {
119 chksum = crc8(0, gd->fdt_blob, fdt_totalsize(gd->fdt_blob));
120 if (chksum != uts->fdt_chksum) {
122 * We cannot run any more tests that need the
123 * live tree, since its strings point into the
124 * flat tree, which has changed. This likely
125 * means that at least some of the pointers from
126 * the live tree point to different things
128 printf("Device tree changed: cannot run live tree tests\n");
129 gd->flags |= GD_FLG_FDT_CHANGED;
139 * With of-platdata-inst the uclasses are created at build time. If we
140 * destroy them we cannot get them back since uclass_add() is not
141 * supported. So skip this.
143 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
144 for (id = 0; id < UCLASS_COUNT; id++) {
148 * If the uclass doesn't exist we don't want to create
149 * it. So check that here before we call
150 * uclass_find_device().
152 uc = uclass_find(id);
155 ut_assertok(uclass_destroy(uc));
162 /* Ensure all the test devices are probed */
163 static int do_autoprobe(struct unit_test_state *uts)
168 /* Scanning the uclass is enough to probe all the devices */
169 for (ret = uclass_first_device(UCLASS_TEST, &dev);
171 ret = uclass_next_device(&dev))
178 * ut_test_run_on_flattree() - Check if we should run a test with flat DT
180 * This skips long/slow tests where there is not much value in running a flat
181 * DT test in addition to a live DT test.
183 * Return: true to run the given test on the flat device tree
185 static bool ut_test_run_on_flattree(struct unit_test *test)
187 const char *fname = strrchr(test->file, '/') + 1;
189 if (!(test->flags & UT_TESTF_DM))
192 return !strstr(fname, "video") || strstr(test->name, "video_base");
196 * test_matches() - Check if a test should be run
198 * This checks if the a test should be run. In the normal case of running all
199 * tests, @select_name is NULL.
201 * @prefix: String prefix for the tests. Any tests that have this prefix will be
202 * printed without the prefix, so that it is easier to see the unique part
203 * of the test name. If NULL, any suite name (xxx_test) is considered to be
205 * @test_name: Name of current test
206 * @select_name: Name of test to run (or NULL for all)
207 * Return: true to run this test, false to skip it
209 static bool test_matches(const char *prefix, const char *test_name,
210 const char *select_name)
217 /* Allow glob expansion in the test name */
218 len = select_name[strlen(select_name) - 1] == '*' ? strlen(select_name) : 0;
222 if (!strncmp(test_name, select_name, len))
226 /* All tests have this prefix */
227 if (!strncmp(test_name, prefix, strlen(prefix)))
228 test_name += strlen(prefix);
230 const char *p = strstr(test_name, "_test_");
232 /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
234 test_name = p + strlen("_test_");
237 if (!strncmp(test_name, select_name, len))
244 * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
246 * @tests: List of tests to run
247 * @count: Number of tests to ru
248 * Return: true if any of the tests have the UT_TESTF_DM flag
250 static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
252 struct unit_test *test;
254 for (test = tests; test < tests + count; test++) {
255 if (test->flags & UT_TESTF_DM)
263 * dm_test_restore() Put things back to normal so sandbox works as expected
265 * @of_root: Value to set for of_root
266 * Return: 0 if OK, -ve on error
268 static int dm_test_restore(struct device_node *of_root)
272 gd_set_of_root(of_root);
274 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
278 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
285 * test_pre_run() - Handle any preparation needed to run a test
288 * @test: Test to prepare for
289 * Return: 0 if OK, -EAGAIN to skip this test since some required feature is not
290 * available, other -ve on error (meaning that testing cannot likely
293 static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
295 ut_assertok(event_init());
296 ut_assertok(cyclic_init());
298 if (test->flags & UT_TESTF_DM)
299 ut_assertok(dm_test_pre_run(uts));
301 ut_set_skip_delays(uts, false);
303 uts->start = mallinfo();
305 if (test->flags & UT_TESTF_SCAN_PDATA) {
306 ut_assertok(dm_scan_plat(false));
307 ut_assertok(dm_scan_other(false));
310 if (test->flags & UT_TESTF_PROBE_TEST)
311 ut_assertok(do_autoprobe(uts));
313 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
314 (test->flags & UT_TESTF_SCAN_FDT))
315 ut_assertok(dm_extended_scan(false));
317 if (test->flags & UT_TESTF_CONSOLE_REC) {
318 int ret = console_record_reset_enable();
321 printf("Skipping: Console recording disabled\n");
325 ut_silence_console(uts);
331 * test_post_run() - Handle cleaning up after a test
334 * @test: Test to clean up after
335 * Return: 0 if OK, -ve on error (meaning that testing cannot likely continue)
337 static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
339 ut_unsilence_console(uts);
340 if (test->flags & UT_TESTF_DM)
341 ut_assertok(dm_test_post_run(uts));
342 ut_assertok(cyclic_uninit());
343 ut_assertok(event_uninit());
349 * ut_run_test() - Run a single test
351 * This runs the test, handling any preparation and clean-up needed. It prints
352 * the name of each test before running it.
354 * @uts: Test state to update. The caller should ensure that this is zeroed for
355 * the first call to this function. On exit, @uts->fail_count is
356 * incremented by the number of failures (0, one hopes)
357 * @test_name: Test to run
358 * @name: Name of test, possibly skipping a prefix that should not be displayed
359 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
362 static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
363 const char *test_name)
365 const char *fname = strrchr(test->file, '/') + 1;
366 const char *note = "";
369 if ((test->flags & UT_TESTF_DM) && !uts->of_live)
370 note = " (flat tree)";
371 printf("Test: %s: %s%s\n", test_name, fname, note);
373 /* Allow access to test state from drivers */
376 ret = test_pre_run(uts, test);
384 ret = test_post_run(uts, test);
388 test_set_state( NULL);
394 * ut_run_test_live_flat() - Run a test with both live and flat tree
396 * This calls ut_run_test() with livetree enabled, which is the standard setup
397 * for runnig tests. Then, for driver model test, it calls it again with
398 * livetree disabled. This allows checking of flattree being used when OF_LIVE
399 * is enabled, as is the case in U-Boot proper before relocation, as well as in
402 * @uts: Test state to update. The caller should ensure that this is zeroed for
403 * the first call to this function. On exit, @uts->fail_count is
404 * incremented by the number of failures (0, one hopes)
406 * @name: Name of test, possibly skipping a prefix that should not be displayed
407 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
410 static int ut_run_test_live_flat(struct unit_test_state *uts,
411 struct unit_test *test, const char *name)
415 /* Run with the live tree if possible */
417 if (CONFIG_IS_ENABLED(OF_LIVE)) {
418 if (!(test->flags & UT_TESTF_FLAT_TREE)) {
420 ut_assertok(ut_run_test(uts, test, test->name));
426 * Run with the flat tree if we couldn't run it with live tree,
427 * or it is a core test.
429 if (!(test->flags & UT_TESTF_LIVE_TREE) &&
430 (!runs || ut_test_run_on_flattree(test)) &&
431 !(gd->flags & GD_FLG_FDT_CHANGED)) {
432 uts->of_live = false;
433 ut_assertok(ut_run_test(uts, test, test->name));
441 * ut_run_tests() - Run a set of tests
443 * This runs the tests, handling any preparation and clean-up needed. It prints
444 * the name of each test before running it.
446 * @uts: Test state to update. The caller should ensure that this is zeroed for
447 * the first call to this function. On exit, @uts->fail_count is
448 * incremented by the number of failures (0, one hopes)
449 * @prefix: String prefix for the tests. Any tests that have this prefix will be
450 * printed without the prefix, so that it is easier to see the unique part
451 * of the test name. If NULL, no prefix processing is done
452 * @tests: List of tests to run
453 * @count: Number of tests to run
454 * @select_name: Name of a single test to run (from the list provided). If NULL
455 * then all tests are run
456 * Return: 0 if all tests passed, -ENOENT if test @select_name was not found,
457 * -EBADF if any failed
459 static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
460 struct unit_test *tests, int count,
461 const char *select_name)
463 struct unit_test *test;
466 for (test = tests; test < tests + count; test++) {
467 const char *test_name = test->name;
468 int ret, i, old_fail_count;
470 if (!test_matches(prefix, test_name, select_name))
472 old_fail_count = uts->fail_count;
473 for (i = 0; i < uts->runs_per_test; i++)
474 ret = ut_run_test_live_flat(uts, test, select_name);
475 if (uts->fail_count != old_fail_count) {
476 printf("Test %s failed %d times\n", select_name,
477 uts->fail_count - old_fail_count);
485 if (select_name && !found)
488 return uts->fail_count ? -EBADF : 0;
491 int ut_run_list(const char *category, const char *prefix,
492 struct unit_test *tests, int count, const char *select_name,
495 struct unit_test_state uts = { .fail_count = 0 };
496 bool has_dm_tests = false;
499 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
500 ut_list_has_dm_tests(tests, count)) {
503 * If we have no device tree, or it only has a root node, then
504 * these * tests clearly aren't going to work...
506 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
507 puts("Please run with test device tree:\n"
508 " ./u-boot -d arch/sandbox/dts/test.dtb\n");
509 return CMD_RET_FAILURE;
514 printf("Running %d %s tests\n", count, category);
516 uts.of_root = gd_of_root();
517 uts.runs_per_test = runs_per_test;
518 if (fdt_action() == FDTCHK_COPY && gd->fdt_blob) {
519 uts.fdt_size = fdt_totalsize(gd->fdt_blob);
520 uts.fdt_copy = os_malloc(uts.fdt_size);
522 printf("Out of memory for device tree copy\n");
525 memcpy(uts.fdt_copy, gd->fdt_blob, uts.fdt_size);
527 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
529 /* Best efforts only...ignore errors */
531 dm_test_restore(uts.of_root);
532 if (IS_ENABLED(CONFIG_SANDBOX))
533 os_free(uts.fdt_copy);
536 printf("Test '%s' not found\n", select_name);
538 printf("Failures: %d\n", uts.fail_count);
540 /* Best efforts only...ignore errors */
542 dm_test_restore(uts.of_root);