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;
84 uts->force_fail_alloc = false;
85 uts->skip_post_probe = false;
86 if (fdt_action() == FDTCHK_CHECKSUM)
87 uts->fdt_chksum = crc8(0, gd->fdt_blob,
88 fdt_totalsize(gd->fdt_blob));
90 malloc_disable_testing();
91 if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA))
92 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
93 arch_reset_for_test();
95 /* Determine whether to make the live tree available */
96 gd_set_of_root(of_live ? uts->of_root : NULL);
97 ut_assertok(dm_init(of_live));
98 uts->root = dm_root();
103 static int dm_test_post_run(struct unit_test_state *uts)
108 switch (fdt_action()) {
110 memcpy((void *)gd->fdt_blob, uts->fdt_copy, uts->fdt_size);
112 case FDTCHK_CHECKSUM: {
115 chksum = crc8(0, gd->fdt_blob, fdt_totalsize(gd->fdt_blob));
117 if (chksum != uts->fdt_chksum)
118 printf("Device tree changed: cannot run live tree tests\n");
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.
131 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
132 for (id = 0; id < UCLASS_COUNT; id++) {
136 * If the uclass doesn't exist we don't want to create
137 * it. So check that here before we call
138 * uclass_find_device().
140 uc = uclass_find(id);
143 ut_assertok(uclass_destroy(uc));
150 /* Ensure all the test devices are probed */
151 static int do_autoprobe(struct unit_test_state *uts)
156 /* Scanning the uclass is enough to probe all the devices */
157 for (ret = uclass_first_device(UCLASS_TEST, &dev);
159 ret = uclass_next_device(&dev))
166 * ut_test_run_on_flattree() - Check if we should run a test with flat DT
168 * This skips long/slow tests where there is not much value in running a flat
169 * DT test in addition to a live DT test.
171 * Return: true to run the given test on the flat device tree
173 static bool ut_test_run_on_flattree(struct unit_test *test)
175 const char *fname = strrchr(test->file, '/') + 1;
177 if (!(test->flags & UT_TESTF_DM))
180 return !strstr(fname, "video") || strstr(test->name, "video_base");
184 * test_matches() - Check if a test should be run
186 * This checks if the a test should be run. In the normal case of running all
187 * tests, @select_name is NULL.
189 * @prefix: String prefix for the tests. Any tests that have this prefix will be
190 * printed without the prefix, so that it is easier to see the unique part
191 * of the test name. If NULL, any suite name (xxx_test) is considered to be
193 * @test_name: Name of current test
194 * @select_name: Name of test to run (or NULL for all)
195 * Return: true to run this test, false to skip it
197 static bool test_matches(const char *prefix, const char *test_name,
198 const char *select_name)
205 /* Allow glob expansion in the test name */
206 len = select_name[strlen(select_name) - 1] == '*' ? strlen(select_name) : 0;
210 if (!strncmp(test_name, select_name, len))
214 /* All tests have this prefix */
215 if (!strncmp(test_name, prefix, strlen(prefix)))
216 test_name += strlen(prefix);
218 const char *p = strstr(test_name, "_test_");
220 /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
222 test_name = p + strlen("_test_");
225 if (!strncmp(test_name, select_name, len))
232 * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
234 * @tests: List of tests to run
235 * @count: Number of tests to ru
236 * Return: true if any of the tests have the UT_TESTF_DM flag
238 static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
240 struct unit_test *test;
242 for (test = tests; test < tests + count; test++) {
243 if (test->flags & UT_TESTF_DM)
251 * dm_test_restore() Put things back to normal so sandbox works as expected
253 * @of_root: Value to set for of_root
254 * Return: 0 if OK, -ve on error
256 static int dm_test_restore(struct device_node *of_root)
260 gd_set_of_root(of_root);
262 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
266 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
273 * test_pre_run() - Handle any preparation needed to run a test
276 * @test: Test to prepare for
277 * Return: 0 if OK, -EAGAIN to skip this test since some required feature is not
278 * available, other -ve on error (meaning that testing cannot likely
281 static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
283 ut_assertok(event_init());
284 ut_assertok(cyclic_init());
286 if (test->flags & UT_TESTF_DM)
287 ut_assertok(dm_test_pre_run(uts));
289 ut_set_skip_delays(uts, false);
291 uts->start = mallinfo();
293 if (test->flags & UT_TESTF_SCAN_PDATA) {
294 ut_assertok(dm_scan_plat(false));
295 ut_assertok(dm_scan_other(false));
298 if (test->flags & UT_TESTF_PROBE_TEST)
299 ut_assertok(do_autoprobe(uts));
301 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
302 (test->flags & UT_TESTF_SCAN_FDT))
303 ut_assertok(dm_extended_scan(false));
305 if (test->flags & UT_TESTF_CONSOLE_REC) {
306 int ret = console_record_reset_enable();
309 printf("Skipping: Console recording disabled\n");
313 ut_silence_console(uts);
319 * test_post_run() - Handle cleaning up after a test
322 * @test: Test to clean up after
323 * Return: 0 if OK, -ve on error (meaning that testing cannot likely continue)
325 static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
327 ut_unsilence_console(uts);
328 if (test->flags & UT_TESTF_DM)
329 ut_assertok(dm_test_post_run(uts));
330 ut_assertok(cyclic_uninit());
331 ut_assertok(event_uninit());
337 * ut_run_test() - Run a single test
339 * This runs the test, handling any preparation and clean-up needed. It prints
340 * the name of each test before running it.
342 * @uts: Test state to update. The caller should ensure that this is zeroed for
343 * the first call to this function. On exit, @uts->fail_count is
344 * incremented by the number of failures (0, one hopes)
345 * @test_name: Test to run
346 * @name: Name of test, possibly skipping a prefix that should not be displayed
347 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
350 static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
351 const char *test_name)
353 const char *fname = strrchr(test->file, '/') + 1;
354 const char *note = "";
357 if ((test->flags & UT_TESTF_DM) && !uts->of_live)
358 note = " (flat tree)";
359 printf("Test: %s: %s%s\n", test_name, fname, note);
361 /* Allow access to test state from drivers */
364 ret = test_pre_run(uts, test);
372 ret = test_post_run(uts, test);
376 test_set_state( NULL);
382 * ut_run_test_live_flat() - Run a test with both live and flat tree
384 * This calls ut_run_test() with livetree enabled, which is the standard setup
385 * for runnig tests. Then, for driver model test, it calls it again with
386 * livetree disabled. This allows checking of flattree being used when OF_LIVE
387 * is enabled, as is the case in U-Boot proper before relocation, as well as in
390 * @uts: Test state to update. The caller should ensure that this is zeroed for
391 * the first call to this function. On exit, @uts->fail_count is
392 * incremented by the number of failures (0, one hopes)
394 * @name: Name of test, possibly skipping a prefix that should not be displayed
395 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
398 static int ut_run_test_live_flat(struct unit_test_state *uts,
399 struct unit_test *test, const char *name)
403 /* Run with the live tree if possible */
405 if (CONFIG_IS_ENABLED(OF_LIVE)) {
406 if (!(test->flags & UT_TESTF_FLAT_TREE)) {
408 ut_assertok(ut_run_test(uts, test, test->name));
414 * Run with the flat tree if we couldn't run it with live tree,
415 * or it is a core test.
417 if (!(test->flags & UT_TESTF_LIVE_TREE) &&
418 (!runs || ut_test_run_on_flattree(test))) {
419 uts->of_live = false;
420 ut_assertok(ut_run_test(uts, test, test->name));
428 * ut_run_tests() - Run a set of tests
430 * This runs the tests, handling any preparation and clean-up needed. It prints
431 * the name of each test before running it.
433 * @uts: Test state to update. The caller should ensure that this is zeroed for
434 * the first call to this function. On exit, @uts->fail_count is
435 * incremented by the number of failures (0, one hopes)
436 * @prefix: String prefix for the tests. Any tests that have this prefix will be
437 * printed without the prefix, so that it is easier to see the unique part
438 * of the test name. If NULL, no prefix processing is done
439 * @tests: List of tests to run
440 * @count: Number of tests to run
441 * @select_name: Name of a single test to run (from the list provided). If NULL
442 * then all tests are run
443 * Return: 0 if all tests passed, -ENOENT if test @select_name was not found,
444 * -EBADF if any failed
446 static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
447 struct unit_test *tests, int count,
448 const char *select_name)
450 struct unit_test *test;
453 for (test = tests; test < tests + count; test++) {
454 const char *test_name = test->name;
455 int ret, i, old_fail_count;
457 if (!test_matches(prefix, test_name, select_name))
459 old_fail_count = uts->fail_count;
460 for (i = 0; i < uts->runs_per_test; i++)
461 ret = ut_run_test_live_flat(uts, test, select_name);
462 if (uts->fail_count != old_fail_count) {
463 printf("Test %s failed %d times\n", select_name,
464 uts->fail_count - old_fail_count);
472 if (select_name && !found)
475 return uts->fail_count ? -EBADF : 0;
478 int ut_run_list(const char *category, const char *prefix,
479 struct unit_test *tests, int count, const char *select_name,
482 struct unit_test_state uts = { .fail_count = 0 };
483 bool has_dm_tests = false;
486 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
487 ut_list_has_dm_tests(tests, count)) {
490 * If we have no device tree, or it only has a root node, then
491 * these * tests clearly aren't going to work...
493 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
494 puts("Please run with test device tree:\n"
495 " ./u-boot -d arch/sandbox/dts/test.dtb\n");
496 return CMD_RET_FAILURE;
501 printf("Running %d %s tests\n", count, category);
503 uts.of_root = gd_of_root();
504 uts.runs_per_test = runs_per_test;
505 if (fdt_action() == FDTCHK_COPY && gd->fdt_blob) {
506 uts.fdt_size = fdt_totalsize(gd->fdt_blob);
507 uts.fdt_copy = os_malloc(uts.fdt_size);
509 printf("Out of memory for device tree copy\n");
512 memcpy(uts.fdt_copy, gd->fdt_blob, uts.fdt_size);
514 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
516 /* Best efforts only...ignore errors */
518 dm_test_restore(uts.of_root);
519 if (IS_ENABLED(CONFIG_SANDBOX))
520 os_free(uts.fdt_copy);
523 printf("Test '%s' not found\n", select_name);
525 printf("Failures: %d\n", uts.fail_count);
527 /* Best efforts only...ignore errors */
529 dm_test_restore(uts.of_root);