1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2021 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
12 #include <dm/uclass-internal.h>
13 #include <test/test.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 /* This is valid when a test is running, NULL otherwise */
19 static struct unit_test_state *cur_test_state;
21 struct unit_test_state *test_get_state(void)
23 return cur_test_state;
26 void test_set_state(struct unit_test_state *uts)
32 * dm_test_pre_run() - Get ready to run a driver model test
34 * This clears out the driver model data structures. For sandbox it resets the
39 static int dm_test_pre_run(struct unit_test_state *uts)
41 bool of_live = uts->of_live;
45 uts->force_fail_alloc = false;
46 uts->skip_post_probe = false;
48 if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA))
49 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
50 arch_reset_for_test();
52 /* Determine whether to make the live tree available */
53 gd_set_of_root(of_live ? uts->of_root : NULL);
54 ut_assertok(dm_init(of_live));
55 uts->root = dm_root();
60 static int dm_test_post_run(struct unit_test_state *uts)
65 * With of-platdata-inst the uclasses are created at build time. If we
66 * destroy them we cannot get them back since uclass_add() is not
67 * supported. So skip this.
69 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
70 for (id = 0; id < UCLASS_COUNT; id++) {
74 * If the uclass doesn't exist we don't want to create
75 * it. So check that here before we call
76 * uclass_find_device().
81 ut_assertok(uclass_destroy(uc));
88 /* Ensure all the test devices are probed */
89 static int do_autoprobe(struct unit_test_state *uts)
94 /* Scanning the uclass is enough to probe all the devices */
95 for (ret = uclass_first_device(UCLASS_TEST, &dev);
97 ret = uclass_next_device(&dev))
104 * ut_test_run_on_flattree() - Check if we should run a test with flat DT
106 * This skips long/slow tests where there is not much value in running a flat
107 * DT test in addition to a live DT test.
109 * Return: true to run the given test on the flat device tree
111 static bool ut_test_run_on_flattree(struct unit_test *test)
113 const char *fname = strrchr(test->file, '/') + 1;
115 if (!(test->flags & UT_TESTF_DM))
118 return !strstr(fname, "video") || strstr(test->name, "video_base");
122 * test_matches() - Check if a test should be run
124 * This checks if the a test should be run. In the normal case of running all
125 * tests, @select_name is NULL.
127 * @prefix: String prefix for the tests. Any tests that have this prefix will be
128 * printed without the prefix, so that it is easier to see the unique part
129 * of the test name. If NULL, any suite name (xxx_test) is considered to be
131 * @test_name: Name of current test
132 * @select_name: Name of test to run (or NULL for all)
133 * Return: true to run this test, false to skip it
135 static bool test_matches(const char *prefix, const char *test_name,
136 const char *select_name)
143 /* Allow glob expansion in the test name */
144 len = select_name[strlen(select_name) - 1] == '*' ? strlen(select_name) : 0;
148 if (!strncmp(test_name, select_name, len))
152 /* All tests have this prefix */
153 if (!strncmp(test_name, prefix, strlen(prefix)))
154 test_name += strlen(prefix);
156 const char *p = strstr(test_name, "_test_");
158 /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
160 test_name = p + strlen("_test_");
163 if (!strncmp(test_name, select_name, len))
170 * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
172 * @tests: List of tests to run
173 * @count: Number of tests to ru
174 * Return: true if any of the tests have the UT_TESTF_DM flag
176 static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
178 struct unit_test *test;
180 for (test = tests; test < tests + count; test++) {
181 if (test->flags & UT_TESTF_DM)
189 * dm_test_restore() Put things back to normal so sandbox works as expected
191 * @of_root: Value to set for of_root
192 * Return: 0 if OK, -ve on error
194 static int dm_test_restore(struct device_node *of_root)
198 gd_set_of_root(of_root);
200 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
204 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
211 * test_pre_run() - Handle any preparation needed to run a test
214 * @test: Test to prepare for
215 * Return: 0 if OK, -EAGAIN to skip this test since some required feature is not
216 * available, other -ve on error (meaning that testing cannot likely
219 static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
221 if (test->flags & UT_TESTF_DM)
222 ut_assertok(dm_test_pre_run(uts));
224 ut_set_skip_delays(uts, false);
226 uts->start = mallinfo();
228 if (test->flags & UT_TESTF_SCAN_PDATA)
229 ut_assertok(dm_scan_plat(false));
231 if (test->flags & UT_TESTF_PROBE_TEST)
232 ut_assertok(do_autoprobe(uts));
234 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
235 (test->flags & UT_TESTF_SCAN_FDT))
236 ut_assertok(dm_extended_scan(false));
238 if (test->flags & UT_TESTF_CONSOLE_REC) {
239 int ret = console_record_reset_enable();
242 printf("Skipping: Console recording disabled\n");
246 ut_silence_console(uts);
252 * test_post_run() - Handle cleaning up after a test
255 * @test: Test to clean up after
256 * Return: 0 if OK, -ve on error (meaning that testing cannot likely continue)
258 static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
260 ut_unsilence_console(uts);
261 if (test->flags & UT_TESTF_DM)
262 ut_assertok(dm_test_post_run(uts));
268 * ut_run_test() - Run a single test
270 * This runs the test, handling any preparation and clean-up needed. It prints
271 * the name of each test before running it.
273 * @uts: Test state to update. The caller should ensure that this is zeroed for
274 * the first call to this function. On exit, @uts->fail_count is
275 * incremented by the number of failures (0, one hopes)
276 * @test_name: Test to run
277 * @name: Name of test, possibly skipping a prefix that should not be displayed
278 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
281 static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
282 const char *test_name)
284 const char *fname = strrchr(test->file, '/') + 1;
285 const char *note = "";
288 if ((test->flags & UT_TESTF_DM) && !uts->of_live)
289 note = " (flat tree)";
290 printf("Test: %s: %s%s\n", test_name, fname, note);
292 /* Allow access to test state from drivers */
295 ret = test_pre_run(uts, test);
303 ret = test_post_run(uts, test);
307 test_set_state( NULL);
313 * ut_run_test_live_flat() - Run a test with both live and flat tree
315 * This calls ut_run_test() with livetree enabled, which is the standard setup
316 * for runnig tests. Then, for driver model test, it calls it again with
317 * livetree disabled. This allows checking of flattree being used when OF_LIVE
318 * is enabled, as is the case in U-Boot proper before relocation, as well as in
321 * @uts: Test state to update. The caller should ensure that this is zeroed for
322 * the first call to this function. On exit, @uts->fail_count is
323 * incremented by the number of failures (0, one hopes)
325 * @name: Name of test, possibly skipping a prefix that should not be displayed
326 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
329 static int ut_run_test_live_flat(struct unit_test_state *uts,
330 struct unit_test *test, const char *name)
334 /* Run with the live tree if possible */
336 if (CONFIG_IS_ENABLED(OF_LIVE)) {
337 if (!(test->flags & UT_TESTF_FLAT_TREE)) {
339 ut_assertok(ut_run_test(uts, test, test->name));
345 * Run with the flat tree if we couldn't run it with live tree,
346 * or it is a core test.
348 if (!(test->flags & UT_TESTF_LIVE_TREE) &&
349 (!runs || ut_test_run_on_flattree(test))) {
350 uts->of_live = false;
351 ut_assertok(ut_run_test(uts, test, test->name));
359 * ut_run_tests() - Run a set of tests
361 * This runs the tests, handling any preparation and clean-up needed. It prints
362 * the name of each test before running it.
364 * @uts: Test state to update. The caller should ensure that this is zeroed for
365 * the first call to this function. On exit, @uts->fail_count is
366 * incremented by the number of failures (0, one hopes)
367 * @prefix: String prefix for the tests. Any tests that have this prefix will be
368 * printed without the prefix, so that it is easier to see the unique part
369 * of the test name. If NULL, no prefix processing is done
370 * @tests: List of tests to run
371 * @count: Number of tests to run
372 * @select_name: Name of a single test to run (from the list provided). If NULL
373 * then all tests are run
374 * Return: 0 if all tests passed, -ENOENT if test @select_name was not found,
375 * -EBADF if any failed
377 static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
378 struct unit_test *tests, int count,
379 const char *select_name)
381 struct unit_test *test;
384 for (test = tests; test < tests + count; test++) {
385 const char *test_name = test->name;
388 if (!test_matches(prefix, test_name, select_name))
390 ret = ut_run_test_live_flat(uts, test, select_name);
397 if (select_name && !found)
400 return uts->fail_count ? -EBADF : 0;
403 int ut_run_list(const char *category, const char *prefix,
404 struct unit_test *tests, int count, const char *select_name)
406 struct unit_test_state uts = { .fail_count = 0 };
407 bool has_dm_tests = false;
410 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
411 ut_list_has_dm_tests(tests, count)) {
414 * If we have no device tree, or it only has a root node, then
415 * these * tests clearly aren't going to work...
417 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
418 puts("Please run with test device tree:\n"
419 " ./u-boot -d arch/sandbox/dts/test.dtb\n");
420 return CMD_RET_FAILURE;
425 printf("Running %d %s tests\n", count, category);
427 uts.of_root = gd_of_root();
428 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
431 printf("Test '%s' not found\n", select_name);
433 printf("Failures: %d\n", uts.fail_count);
435 /* Best efforts only...ignore errors */
437 dm_test_restore(uts.of_root);