1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2021 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
13 #include <dm/uclass-internal.h>
14 #include <test/test.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 /* This is valid when a test is running, NULL otherwise */
20 static struct unit_test_state *cur_test_state;
22 struct unit_test_state *test_get_state(void)
24 return cur_test_state;
27 void test_set_state(struct unit_test_state *uts)
33 * dm_test_pre_run() - Get ready to run a driver model test
35 * This clears out the driver model data structures. For sandbox it resets the
40 static int dm_test_pre_run(struct unit_test_state *uts)
42 bool of_live = uts->of_live;
46 uts->force_fail_alloc = false;
47 uts->skip_post_probe = false;
49 if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA))
50 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
51 arch_reset_for_test();
53 /* Determine whether to make the live tree available */
54 gd_set_of_root(of_live ? uts->of_root : NULL);
55 ut_assertok(dm_init(of_live));
56 uts->root = dm_root();
61 static int dm_test_post_run(struct unit_test_state *uts)
66 * With of-platdata-inst the uclasses are created at build time. If we
67 * destroy them we cannot get them back since uclass_add() is not
68 * supported. So skip this.
70 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
71 for (id = 0; id < UCLASS_COUNT; id++) {
75 * If the uclass doesn't exist we don't want to create
76 * it. So check that here before we call
77 * uclass_find_device().
82 ut_assertok(uclass_destroy(uc));
89 /* Ensure all the test devices are probed */
90 static int do_autoprobe(struct unit_test_state *uts)
95 /* Scanning the uclass is enough to probe all the devices */
96 for (ret = uclass_first_device(UCLASS_TEST, &dev);
98 ret = uclass_next_device(&dev))
105 * ut_test_run_on_flattree() - Check if we should run a test with flat DT
107 * This skips long/slow tests where there is not much value in running a flat
108 * DT test in addition to a live DT test.
110 * Return: true to run the given test on the flat device tree
112 static bool ut_test_run_on_flattree(struct unit_test *test)
114 const char *fname = strrchr(test->file, '/') + 1;
116 if (!(test->flags & UT_TESTF_DM))
119 return !strstr(fname, "video") || strstr(test->name, "video_base");
123 * test_matches() - Check if a test should be run
125 * This checks if the a test should be run. In the normal case of running all
126 * tests, @select_name is NULL.
128 * @prefix: String prefix for the tests. Any tests that have this prefix will be
129 * printed without the prefix, so that it is easier to see the unique part
130 * of the test name. If NULL, any suite name (xxx_test) is considered to be
132 * @test_name: Name of current test
133 * @select_name: Name of test to run (or NULL for all)
134 * Return: true to run this test, false to skip it
136 static bool test_matches(const char *prefix, const char *test_name,
137 const char *select_name)
144 /* Allow glob expansion in the test name */
145 len = select_name[strlen(select_name) - 1] == '*' ? strlen(select_name) : 0;
149 if (!strncmp(test_name, select_name, len))
153 /* All tests have this prefix */
154 if (!strncmp(test_name, prefix, strlen(prefix)))
155 test_name += strlen(prefix);
157 const char *p = strstr(test_name, "_test_");
159 /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
161 test_name = p + strlen("_test_");
164 if (!strncmp(test_name, select_name, len))
171 * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
173 * @tests: List of tests to run
174 * @count: Number of tests to ru
175 * Return: true if any of the tests have the UT_TESTF_DM flag
177 static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
179 struct unit_test *test;
181 for (test = tests; test < tests + count; test++) {
182 if (test->flags & UT_TESTF_DM)
190 * dm_test_restore() Put things back to normal so sandbox works as expected
192 * @of_root: Value to set for of_root
193 * Return: 0 if OK, -ve on error
195 static int dm_test_restore(struct device_node *of_root)
199 gd_set_of_root(of_root);
201 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
205 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
212 * test_pre_run() - Handle any preparation needed to run a test
215 * @test: Test to prepare for
216 * Return: 0 if OK, -EAGAIN to skip this test since some required feature is not
217 * available, other -ve on error (meaning that testing cannot likely
220 static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
222 ut_assertok(event_init());
224 if (test->flags & UT_TESTF_DM)
225 ut_assertok(dm_test_pre_run(uts));
227 ut_set_skip_delays(uts, false);
229 uts->start = mallinfo();
231 if (test->flags & UT_TESTF_SCAN_PDATA) {
232 ut_assertok(dm_scan_plat(false));
233 ut_assertok(dm_scan_other(false));
236 if (test->flags & UT_TESTF_PROBE_TEST)
237 ut_assertok(do_autoprobe(uts));
239 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
240 (test->flags & UT_TESTF_SCAN_FDT))
241 ut_assertok(dm_extended_scan(false));
243 if (test->flags & UT_TESTF_CONSOLE_REC) {
244 int ret = console_record_reset_enable();
247 printf("Skipping: Console recording disabled\n");
251 ut_silence_console(uts);
257 * test_post_run() - Handle cleaning up after a test
260 * @test: Test to clean up after
261 * Return: 0 if OK, -ve on error (meaning that testing cannot likely continue)
263 static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
265 ut_unsilence_console(uts);
266 if (test->flags & UT_TESTF_DM)
267 ut_assertok(dm_test_post_run(uts));
268 ut_assertok(event_uninit());
274 * ut_run_test() - Run a single test
276 * This runs the test, handling any preparation and clean-up needed. It prints
277 * the name of each test before running it.
279 * @uts: Test state to update. The caller should ensure that this is zeroed for
280 * the first call to this function. On exit, @uts->fail_count is
281 * incremented by the number of failures (0, one hopes)
282 * @test_name: Test to run
283 * @name: Name of test, possibly skipping a prefix that should not be displayed
284 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
287 static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
288 const char *test_name)
290 const char *fname = strrchr(test->file, '/') + 1;
291 const char *note = "";
294 if ((test->flags & UT_TESTF_DM) && !uts->of_live)
295 note = " (flat tree)";
296 printf("Test: %s: %s%s\n", test_name, fname, note);
298 /* Allow access to test state from drivers */
301 ret = test_pre_run(uts, test);
309 ret = test_post_run(uts, test);
313 test_set_state( NULL);
319 * ut_run_test_live_flat() - Run a test with both live and flat tree
321 * This calls ut_run_test() with livetree enabled, which is the standard setup
322 * for runnig tests. Then, for driver model test, it calls it again with
323 * livetree disabled. This allows checking of flattree being used when OF_LIVE
324 * is enabled, as is the case in U-Boot proper before relocation, as well as in
327 * @uts: Test state to update. The caller should ensure that this is zeroed for
328 * the first call to this function. On exit, @uts->fail_count is
329 * incremented by the number of failures (0, one hopes)
331 * @name: Name of test, possibly skipping a prefix that should not be displayed
332 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
335 static int ut_run_test_live_flat(struct unit_test_state *uts,
336 struct unit_test *test, const char *name)
340 /* Run with the live tree if possible */
342 if (CONFIG_IS_ENABLED(OF_LIVE)) {
344 (UT_TESTF_FLAT_TREE | UT_TESTF_LIVE_OR_FLAT))) {
346 ut_assertok(ut_run_test(uts, test, test->name));
352 * Run with the flat tree if we couldn't run it with live tree,
353 * or it is a core test.
355 if (!(test->flags & UT_TESTF_LIVE_TREE) &&
356 (!runs || ut_test_run_on_flattree(test))) {
357 uts->of_live = false;
358 ut_assertok(ut_run_test(uts, test, test->name));
366 * ut_run_tests() - Run a set of tests
368 * This runs the tests, handling any preparation and clean-up needed. It prints
369 * the name of each test before running it.
371 * @uts: Test state to update. The caller should ensure that this is zeroed for
372 * the first call to this function. On exit, @uts->fail_count is
373 * incremented by the number of failures (0, one hopes)
374 * @prefix: String prefix for the tests. Any tests that have this prefix will be
375 * printed without the prefix, so that it is easier to see the unique part
376 * of the test name. If NULL, no prefix processing is done
377 * @tests: List of tests to run
378 * @count: Number of tests to run
379 * @select_name: Name of a single test to run (from the list provided). If NULL
380 * then all tests are run
381 * Return: 0 if all tests passed, -ENOENT if test @select_name was not found,
382 * -EBADF if any failed
384 static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
385 struct unit_test *tests, int count,
386 const char *select_name)
388 struct unit_test *test;
391 for (test = tests; test < tests + count; test++) {
392 const char *test_name = test->name;
395 if (!test_matches(prefix, test_name, select_name))
397 ret = ut_run_test_live_flat(uts, test, select_name);
404 if (select_name && !found)
407 return uts->fail_count ? -EBADF : 0;
410 int ut_run_list(const char *category, const char *prefix,
411 struct unit_test *tests, int count, const char *select_name)
413 struct unit_test_state uts = { .fail_count = 0 };
414 bool has_dm_tests = false;
417 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
418 ut_list_has_dm_tests(tests, count)) {
421 * If we have no device tree, or it only has a root node, then
422 * these * tests clearly aren't going to work...
424 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
425 puts("Please run with test device tree:\n"
426 " ./u-boot -d arch/sandbox/dts/test.dtb\n");
427 return CMD_RET_FAILURE;
432 printf("Running %d %s tests\n", count, category);
434 uts.of_root = gd_of_root();
435 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
438 printf("Test '%s' not found\n", select_name);
440 printf("Failures: %d\n", uts.fail_count);
442 /* Best efforts only...ignore errors */
444 dm_test_restore(uts.of_root);