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 if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA))
51 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
52 arch_reset_for_test();
54 /* Determine whether to make the live tree available */
55 gd_set_of_root(of_live ? uts->of_root : NULL);
56 ut_assertok(dm_init(of_live));
57 uts->root = dm_root();
62 static int dm_test_post_run(struct unit_test_state *uts)
67 * With of-platdata-inst the uclasses are created at build time. If we
68 * destroy them we cannot get them back since uclass_add() is not
69 * supported. So skip this.
71 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
72 for (id = 0; id < UCLASS_COUNT; id++) {
76 * If the uclass doesn't exist we don't want to create
77 * it. So check that here before we call
78 * uclass_find_device().
83 ut_assertok(uclass_destroy(uc));
90 /* Ensure all the test devices are probed */
91 static int do_autoprobe(struct unit_test_state *uts)
96 /* Scanning the uclass is enough to probe all the devices */
97 for (ret = uclass_first_device(UCLASS_TEST, &dev);
99 ret = uclass_next_device(&dev))
106 * ut_test_run_on_flattree() - Check if we should run a test with flat DT
108 * This skips long/slow tests where there is not much value in running a flat
109 * DT test in addition to a live DT test.
111 * Return: true to run the given test on the flat device tree
113 static bool ut_test_run_on_flattree(struct unit_test *test)
115 const char *fname = strrchr(test->file, '/') + 1;
117 if (!(test->flags & UT_TESTF_DM))
120 return !strstr(fname, "video") || strstr(test->name, "video_base");
124 * test_matches() - Check if a test should be run
126 * This checks if the a test should be run. In the normal case of running all
127 * tests, @select_name is NULL.
129 * @prefix: String prefix for the tests. Any tests that have this prefix will be
130 * printed without the prefix, so that it is easier to see the unique part
131 * of the test name. If NULL, any suite name (xxx_test) is considered to be
133 * @test_name: Name of current test
134 * @select_name: Name of test to run (or NULL for all)
135 * Return: true to run this test, false to skip it
137 static bool test_matches(const char *prefix, const char *test_name,
138 const char *select_name)
145 /* Allow glob expansion in the test name */
146 len = select_name[strlen(select_name) - 1] == '*' ? strlen(select_name) : 0;
150 if (!strncmp(test_name, select_name, len))
154 /* All tests have this prefix */
155 if (!strncmp(test_name, prefix, strlen(prefix)))
156 test_name += strlen(prefix);
158 const char *p = strstr(test_name, "_test_");
160 /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
162 test_name = p + strlen("_test_");
165 if (!strncmp(test_name, select_name, len))
172 * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
174 * @tests: List of tests to run
175 * @count: Number of tests to ru
176 * Return: true if any of the tests have the UT_TESTF_DM flag
178 static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
180 struct unit_test *test;
182 for (test = tests; test < tests + count; test++) {
183 if (test->flags & UT_TESTF_DM)
191 * dm_test_restore() Put things back to normal so sandbox works as expected
193 * @of_root: Value to set for of_root
194 * Return: 0 if OK, -ve on error
196 static int dm_test_restore(struct device_node *of_root)
200 gd_set_of_root(of_root);
202 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
206 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
213 * test_pre_run() - Handle any preparation needed to run a test
216 * @test: Test to prepare for
217 * Return: 0 if OK, -EAGAIN to skip this test since some required feature is not
218 * available, other -ve on error (meaning that testing cannot likely
221 static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
223 ut_assertok(event_init());
224 ut_assertok(cyclic_init());
226 if (test->flags & UT_TESTF_DM)
227 ut_assertok(dm_test_pre_run(uts));
229 ut_set_skip_delays(uts, false);
231 uts->start = mallinfo();
233 if (test->flags & UT_TESTF_SCAN_PDATA) {
234 ut_assertok(dm_scan_plat(false));
235 ut_assertok(dm_scan_other(false));
238 if (test->flags & UT_TESTF_PROBE_TEST)
239 ut_assertok(do_autoprobe(uts));
241 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
242 (test->flags & UT_TESTF_SCAN_FDT))
243 ut_assertok(dm_extended_scan(false));
245 if (test->flags & UT_TESTF_CONSOLE_REC) {
246 int ret = console_record_reset_enable();
249 printf("Skipping: Console recording disabled\n");
253 ut_silence_console(uts);
259 * test_post_run() - Handle cleaning up after a test
262 * @test: Test to clean up after
263 * Return: 0 if OK, -ve on error (meaning that testing cannot likely continue)
265 static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
267 ut_unsilence_console(uts);
268 if (test->flags & UT_TESTF_DM)
269 ut_assertok(dm_test_post_run(uts));
270 ut_assertok(cyclic_uninit());
271 ut_assertok(event_uninit());
277 * ut_run_test() - Run a single test
279 * This runs the test, handling any preparation and clean-up needed. It prints
280 * the name of each test before running it.
282 * @uts: Test state to update. The caller should ensure that this is zeroed for
283 * the first call to this function. On exit, @uts->fail_count is
284 * incremented by the number of failures (0, one hopes)
285 * @test_name: Test to run
286 * @name: Name of test, possibly skipping a prefix that should not be displayed
287 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
290 static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
291 const char *test_name)
293 const char *fname = strrchr(test->file, '/') + 1;
294 const char *note = "";
297 if ((test->flags & UT_TESTF_DM) && !uts->of_live)
298 note = " (flat tree)";
299 printf("Test: %s: %s%s\n", test_name, fname, note);
301 /* Allow access to test state from drivers */
304 ret = test_pre_run(uts, test);
312 ret = test_post_run(uts, test);
316 test_set_state( NULL);
322 * ut_run_test_live_flat() - Run a test with both live and flat tree
324 * This calls ut_run_test() with livetree enabled, which is the standard setup
325 * for runnig tests. Then, for driver model test, it calls it again with
326 * livetree disabled. This allows checking of flattree being used when OF_LIVE
327 * is enabled, as is the case in U-Boot proper before relocation, as well as in
330 * @uts: Test state to update. The caller should ensure that this is zeroed for
331 * the first call to this function. On exit, @uts->fail_count is
332 * incremented by the number of failures (0, one hopes)
334 * @name: Name of test, possibly skipping a prefix that should not be displayed
335 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
338 static int ut_run_test_live_flat(struct unit_test_state *uts,
339 struct unit_test *test, const char *name)
343 /* Run with the live tree if possible */
345 if (CONFIG_IS_ENABLED(OF_LIVE)) {
346 if (!(test->flags & UT_TESTF_FLAT_TREE)) {
348 ut_assertok(ut_run_test(uts, test, test->name));
354 * Run with the flat tree if we couldn't run it with live tree,
355 * or it is a core test.
357 if (!(test->flags & UT_TESTF_LIVE_TREE) &&
358 (!runs || ut_test_run_on_flattree(test))) {
359 uts->of_live = false;
360 ut_assertok(ut_run_test(uts, test, test->name));
368 * ut_run_tests() - Run a set of tests
370 * This runs the tests, handling any preparation and clean-up needed. It prints
371 * the name of each test before running it.
373 * @uts: Test state to update. The caller should ensure that this is zeroed for
374 * the first call to this function. On exit, @uts->fail_count is
375 * incremented by the number of failures (0, one hopes)
376 * @prefix: String prefix for the tests. Any tests that have this prefix will be
377 * printed without the prefix, so that it is easier to see the unique part
378 * of the test name. If NULL, no prefix processing is done
379 * @tests: List of tests to run
380 * @count: Number of tests to run
381 * @select_name: Name of a single test to run (from the list provided). If NULL
382 * then all tests are run
383 * Return: 0 if all tests passed, -ENOENT if test @select_name was not found,
384 * -EBADF if any failed
386 static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
387 struct unit_test *tests, int count,
388 const char *select_name)
390 struct unit_test *test;
393 for (test = tests; test < tests + count; test++) {
394 const char *test_name = test->name;
395 int ret, i, old_fail_count;
397 if (!test_matches(prefix, test_name, select_name))
399 old_fail_count = uts->fail_count;
400 for (i = 0; i < uts->runs_per_test; i++)
401 ret = ut_run_test_live_flat(uts, test, select_name);
402 if (uts->fail_count != old_fail_count) {
403 printf("Test %s failed %d times\n", select_name,
404 uts->fail_count - old_fail_count);
412 if (select_name && !found)
415 return uts->fail_count ? -EBADF : 0;
418 int ut_run_list(const char *category, const char *prefix,
419 struct unit_test *tests, int count, const char *select_name,
422 struct unit_test_state uts = { .fail_count = 0 };
423 bool has_dm_tests = false;
426 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
427 ut_list_has_dm_tests(tests, count)) {
430 * If we have no device tree, or it only has a root node, then
431 * these * tests clearly aren't going to work...
433 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
434 puts("Please run with test device tree:\n"
435 " ./u-boot -d arch/sandbox/dts/test.dtb\n");
436 return CMD_RET_FAILURE;
441 printf("Running %d %s tests\n", count, category);
443 uts.of_root = gd_of_root();
444 uts.runs_per_test = runs_per_test;
445 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
448 printf("Test '%s' not found\n", select_name);
450 printf("Failures: %d\n", uts.fail_count);
452 /* Best efforts only...ignore errors */
454 dm_test_restore(uts.of_root);