1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2021 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
14 #include <dm/uclass-internal.h>
15 #include <test/test.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 /* This is valid when a test is running, NULL otherwise */
21 static struct unit_test_state *cur_test_state;
23 struct unit_test_state *test_get_state(void)
25 return cur_test_state;
28 void test_set_state(struct unit_test_state *uts)
34 * dm_test_pre_run() - Get ready to run a driver model test
36 * This clears out the driver model data structures. For sandbox it resets the
41 static int dm_test_pre_run(struct unit_test_state *uts)
43 bool of_live = uts->of_live;
47 uts->force_fail_alloc = false;
48 uts->skip_post_probe = false;
50 malloc_disable_testing();
51 if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA))
52 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
53 arch_reset_for_test();
55 /* Determine whether to make the live tree available */
56 gd_set_of_root(of_live ? uts->of_root : NULL);
57 ut_assertok(dm_init(of_live));
58 uts->root = dm_root();
63 static int dm_test_post_run(struct unit_test_state *uts)
68 * With of-platdata-inst the uclasses are created at build time. If we
69 * destroy them we cannot get them back since uclass_add() is not
70 * supported. So skip this.
72 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
73 for (id = 0; id < UCLASS_COUNT; id++) {
77 * If the uclass doesn't exist we don't want to create
78 * it. So check that here before we call
79 * uclass_find_device().
84 ut_assertok(uclass_destroy(uc));
91 /* Ensure all the test devices are probed */
92 static int do_autoprobe(struct unit_test_state *uts)
97 /* Scanning the uclass is enough to probe all the devices */
98 for (ret = uclass_first_device(UCLASS_TEST, &dev);
100 ret = uclass_next_device(&dev))
107 * ut_test_run_on_flattree() - Check if we should run a test with flat DT
109 * This skips long/slow tests where there is not much value in running a flat
110 * DT test in addition to a live DT test.
112 * Return: true to run the given test on the flat device tree
114 static bool ut_test_run_on_flattree(struct unit_test *test)
116 const char *fname = strrchr(test->file, '/') + 1;
118 if (!(test->flags & UT_TESTF_DM))
121 return !strstr(fname, "video") || strstr(test->name, "video_base");
125 * test_matches() - Check if a test should be run
127 * This checks if the a test should be run. In the normal case of running all
128 * tests, @select_name is NULL.
130 * @prefix: String prefix for the tests. Any tests that have this prefix will be
131 * printed without the prefix, so that it is easier to see the unique part
132 * of the test name. If NULL, any suite name (xxx_test) is considered to be
134 * @test_name: Name of current test
135 * @select_name: Name of test to run (or NULL for all)
136 * Return: true to run this test, false to skip it
138 static bool test_matches(const char *prefix, const char *test_name,
139 const char *select_name)
146 /* Allow glob expansion in the test name */
147 len = select_name[strlen(select_name) - 1] == '*' ? strlen(select_name) : 0;
151 if (!strncmp(test_name, select_name, len))
155 /* All tests have this prefix */
156 if (!strncmp(test_name, prefix, strlen(prefix)))
157 test_name += strlen(prefix);
159 const char *p = strstr(test_name, "_test_");
161 /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
163 test_name = p + strlen("_test_");
166 if (!strncmp(test_name, select_name, len))
173 * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
175 * @tests: List of tests to run
176 * @count: Number of tests to ru
177 * Return: true if any of the tests have the UT_TESTF_DM flag
179 static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
181 struct unit_test *test;
183 for (test = tests; test < tests + count; test++) {
184 if (test->flags & UT_TESTF_DM)
192 * dm_test_restore() Put things back to normal so sandbox works as expected
194 * @of_root: Value to set for of_root
195 * Return: 0 if OK, -ve on error
197 static int dm_test_restore(struct device_node *of_root)
201 gd_set_of_root(of_root);
203 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
207 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
214 * test_pre_run() - Handle any preparation needed to run a test
217 * @test: Test to prepare for
218 * Return: 0 if OK, -EAGAIN to skip this test since some required feature is not
219 * available, other -ve on error (meaning that testing cannot likely
222 static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
224 ut_assertok(event_init());
225 ut_assertok(cyclic_init());
227 if (test->flags & UT_TESTF_DM)
228 ut_assertok(dm_test_pre_run(uts));
230 ut_set_skip_delays(uts, false);
232 uts->start = mallinfo();
234 if (test->flags & UT_TESTF_SCAN_PDATA) {
235 ut_assertok(dm_scan_plat(false));
236 ut_assertok(dm_scan_other(false));
239 if (test->flags & UT_TESTF_PROBE_TEST)
240 ut_assertok(do_autoprobe(uts));
242 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
243 (test->flags & UT_TESTF_SCAN_FDT))
244 ut_assertok(dm_extended_scan(false));
246 if (test->flags & UT_TESTF_CONSOLE_REC) {
247 int ret = console_record_reset_enable();
250 printf("Skipping: Console recording disabled\n");
254 ut_silence_console(uts);
260 * test_post_run() - Handle cleaning up after a test
263 * @test: Test to clean up after
264 * Return: 0 if OK, -ve on error (meaning that testing cannot likely continue)
266 static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
268 ut_unsilence_console(uts);
269 if (test->flags & UT_TESTF_DM)
270 ut_assertok(dm_test_post_run(uts));
271 ut_assertok(cyclic_uninit());
272 ut_assertok(event_uninit());
278 * ut_run_test() - Run a single test
280 * This runs the test, handling any preparation and clean-up needed. It prints
281 * the name of each test before running it.
283 * @uts: Test state to update. The caller should ensure that this is zeroed for
284 * the first call to this function. On exit, @uts->fail_count is
285 * incremented by the number of failures (0, one hopes)
286 * @test_name: Test to run
287 * @name: Name of test, possibly skipping a prefix that should not be displayed
288 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
291 static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
292 const char *test_name)
294 const char *fname = strrchr(test->file, '/') + 1;
295 const char *note = "";
298 if ((test->flags & UT_TESTF_DM) && !uts->of_live)
299 note = " (flat tree)";
300 printf("Test: %s: %s%s\n", test_name, fname, note);
302 /* Allow access to test state from drivers */
305 ret = test_pre_run(uts, test);
313 ret = test_post_run(uts, test);
317 test_set_state( NULL);
323 * ut_run_test_live_flat() - Run a test with both live and flat tree
325 * This calls ut_run_test() with livetree enabled, which is the standard setup
326 * for runnig tests. Then, for driver model test, it calls it again with
327 * livetree disabled. This allows checking of flattree being used when OF_LIVE
328 * is enabled, as is the case in U-Boot proper before relocation, as well as in
331 * @uts: Test state to update. The caller should ensure that this is zeroed for
332 * the first call to this function. On exit, @uts->fail_count is
333 * incremented by the number of failures (0, one hopes)
335 * @name: Name of test, possibly skipping a prefix that should not be displayed
336 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
339 static int ut_run_test_live_flat(struct unit_test_state *uts,
340 struct unit_test *test, const char *name)
344 /* Run with the live tree if possible */
346 if (CONFIG_IS_ENABLED(OF_LIVE)) {
347 if (!(test->flags & UT_TESTF_FLAT_TREE)) {
349 ut_assertok(ut_run_test(uts, test, test->name));
355 * Run with the flat tree if we couldn't run it with live tree,
356 * or it is a core test.
358 if (!(test->flags & UT_TESTF_LIVE_TREE) &&
359 (!runs || ut_test_run_on_flattree(test))) {
360 uts->of_live = false;
361 ut_assertok(ut_run_test(uts, test, test->name));
369 * ut_run_tests() - Run a set of tests
371 * This runs the tests, handling any preparation and clean-up needed. It prints
372 * the name of each test before running it.
374 * @uts: Test state to update. The caller should ensure that this is zeroed for
375 * the first call to this function. On exit, @uts->fail_count is
376 * incremented by the number of failures (0, one hopes)
377 * @prefix: String prefix for the tests. Any tests that have this prefix will be
378 * printed without the prefix, so that it is easier to see the unique part
379 * of the test name. If NULL, no prefix processing is done
380 * @tests: List of tests to run
381 * @count: Number of tests to run
382 * @select_name: Name of a single test to run (from the list provided). If NULL
383 * then all tests are run
384 * Return: 0 if all tests passed, -ENOENT if test @select_name was not found,
385 * -EBADF if any failed
387 static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
388 struct unit_test *tests, int count,
389 const char *select_name)
391 struct unit_test *test;
394 for (test = tests; test < tests + count; test++) {
395 const char *test_name = test->name;
396 int ret, i, old_fail_count;
398 if (!test_matches(prefix, test_name, select_name))
400 old_fail_count = uts->fail_count;
401 for (i = 0; i < uts->runs_per_test; i++)
402 ret = ut_run_test_live_flat(uts, test, select_name);
403 if (uts->fail_count != old_fail_count) {
404 printf("Test %s failed %d times\n", select_name,
405 uts->fail_count - old_fail_count);
413 if (select_name && !found)
416 return uts->fail_count ? -EBADF : 0;
419 int ut_run_list(const char *category, const char *prefix,
420 struct unit_test *tests, int count, const char *select_name,
423 struct unit_test_state uts = { .fail_count = 0 };
424 bool has_dm_tests = false;
427 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
428 ut_list_has_dm_tests(tests, count)) {
431 * If we have no device tree, or it only has a root node, then
432 * these * tests clearly aren't going to work...
434 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
435 puts("Please run with test device tree:\n"
436 " ./u-boot -d arch/sandbox/dts/test.dtb\n");
437 return CMD_RET_FAILURE;
442 printf("Running %d %s tests\n", count, category);
444 uts.of_root = gd_of_root();
445 uts.runs_per_test = runs_per_test;
446 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
449 printf("Test '%s' not found\n", select_name);
451 printf("Failures: %d\n", uts.fail_count);
453 /* Best efforts only...ignore errors */
455 dm_test_restore(uts.of_root);