blob: 3cdf6849c57ffd937c1877dee1b20339d60ac094 [file] [log] [blame]
Simon Glass1c721752021-03-07 17:34:47 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2021 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
7#include <common.h>
8#include <console.h>
Simon Glassd8ed2342021-03-07 17:34:50 -07009#include <dm.h>
10#include <dm/root.h>
Simon Glassc79705e2021-03-07 17:34:58 -070011#include <dm/test.h>
Simon Glasse77615d2021-03-07 17:34:59 -070012#include <dm/uclass-internal.h>
Simon Glass1c721752021-03-07 17:34:47 -070013#include <test/test.h>
Simon Glassd8ed2342021-03-07 17:34:50 -070014#include <test/ut.h>
Simon Glass1c721752021-03-07 17:34:47 -070015
Simon Glass30a0d202021-03-07 17:34:49 -070016DECLARE_GLOBAL_DATA_PTR;
17
Simon Glassfe806862021-03-07 17:35:04 -070018/* This is valid when a test is running, NULL otherwise */
19static struct unit_test_state *cur_test_state;
20
21struct unit_test_state *test_get_state(void)
22{
23 return cur_test_state;
24}
25
26void test_set_state(struct unit_test_state *uts)
27{
28 cur_test_state = uts;
29}
30
Simon Glassc79705e2021-03-07 17:34:58 -070031/**
32 * dm_test_pre_run() - Get ready to run a driver model test
33 *
34 * This clears out the driver model data structures. For sandbox it resets the
35 * state structure
36 *
37 * @uts: Test state
38 */
39static int dm_test_pre_run(struct unit_test_state *uts)
40{
41 bool of_live = uts->of_live;
42
43 uts->root = NULL;
44 uts->testdev = NULL;
45 uts->force_fail_alloc = false;
46 uts->skip_post_probe = false;
47 gd->dm_root = NULL;
Simon Glass719d2862021-07-05 16:32:43 -060048 if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA))
Simon Glassc79705e2021-03-07 17:34:58 -070049 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
Simon Glassd4a15922021-03-25 10:44:33 +130050 arch_reset_for_test();
Simon Glassc79705e2021-03-07 17:34:58 -070051
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();
56
57 return 0;
58}
59
Simon Glasse77615d2021-03-07 17:34:59 -070060static int dm_test_post_run(struct unit_test_state *uts)
61{
62 int id;
63
Simon Glasse8c023c2021-03-15 17:25:21 +130064 /*
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.
68 */
69 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
70 for (id = 0; id < UCLASS_COUNT; id++) {
71 struct uclass *uc;
Simon Glasse77615d2021-03-07 17:34:59 -070072
Simon Glasse8c023c2021-03-15 17:25:21 +130073 /*
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().
77 */
78 uc = uclass_find(id);
79 if (!uc)
80 continue;
81 ut_assertok(uclass_destroy(uc));
82 }
Simon Glasse77615d2021-03-07 17:34:59 -070083 }
84
85 return 0;
86}
87
Simon Glass4b8b27e2021-03-07 17:34:51 -070088/* Ensure all the test devices are probed */
89static int do_autoprobe(struct unit_test_state *uts)
90{
91 struct udevice *dev;
92 int ret;
93
94 /* Scanning the uclass is enough to probe all the devices */
95 for (ret = uclass_first_device(UCLASS_TEST, &dev);
96 dev;
97 ret = uclass_next_device(&dev))
98 ;
99
100 return ret;
101}
102
Simon Glassd2281bb2021-03-07 17:35:03 -0700103/*
104 * ut_test_run_on_flattree() - Check if we should run a test with flat DT
105 *
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.
108 *
109 * @return true to run the given test on the flat device tree
110 */
111static bool ut_test_run_on_flattree(struct unit_test *test)
112{
113 const char *fname = strrchr(test->file, '/') + 1;
114
115 if (!(test->flags & UT_TESTF_DM))
116 return false;
117
118 return !strstr(fname, "video") || strstr(test->name, "video_base");
119}
120
Simon Glassca44ca02021-03-07 17:35:01 -0700121/**
Simon Glassf97f85e2021-03-07 17:35:05 -0700122 * test_matches() - Check if a test should be run
123 *
124 * This checks if the a test should be run. In the normal case of running all
125 * tests, @select_name is NULL.
126 *
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
Simon Glass84823562021-03-07 17:35:12 -0700129 * of the test name. If NULL, any suite name (xxx_test) is considered to be
130 * a prefix.
Simon Glassf97f85e2021-03-07 17:35:05 -0700131 * @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
134 */
135static bool test_matches(const char *prefix, const char *test_name,
136 const char *select_name)
137{
Andy Shevchenkoff232a72021-02-11 16:40:10 +0200138 size_t len;
139
Simon Glassf97f85e2021-03-07 17:35:05 -0700140 if (!select_name)
141 return true;
142
Andy Shevchenkoff232a72021-02-11 16:40:10 +0200143 /* Allow glob expansion in the test name */
144 len = select_name[strlen(select_name) - 1] == '*' ? strlen(select_name) : 0;
145 if (len-- == 1)
146 return true;
147
148 if (!strncmp(test_name, select_name, len))
Simon Glassf97f85e2021-03-07 17:35:05 -0700149 return true;
150
Andy Shevchenko494a5e12021-02-11 16:40:11 +0200151 if (prefix) {
152 /* All tests have this prefix */
153 if (!strncmp(test_name, prefix, strlen(prefix)))
154 test_name += strlen(prefix);
155 } else {
Simon Glass84823562021-03-07 17:35:12 -0700156 const char *p = strstr(test_name, "_test_");
157
158 /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
159 if (p)
Andy Shevchenko494a5e12021-02-11 16:40:11 +0200160 test_name = p + strlen("_test_");
Simon Glass84823562021-03-07 17:35:12 -0700161 }
Simon Glassf97f85e2021-03-07 17:35:05 -0700162
Andy Shevchenkoff232a72021-02-11 16:40:10 +0200163 if (!strncmp(test_name, select_name, len))
Simon Glassf97f85e2021-03-07 17:35:05 -0700164 return true;
165
166 return false;
167}
168
Simon Glass664277f2021-03-07 17:35:08 -0700169/**
Simon Glass1fc9c122021-03-07 17:35:07 -0700170 * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
171 *
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
175 */
176static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
177{
178 struct unit_test *test;
179
180 for (test = tests; test < tests + count; test++) {
181 if (test->flags & UT_TESTF_DM)
182 return true;
183 }
184
185 return false;
186}
187
Simon Glassf97f85e2021-03-07 17:35:05 -0700188/**
Simon Glass664277f2021-03-07 17:35:08 -0700189 * dm_test_restore() Put things back to normal so sandbox works as expected
190 *
191 * @of_root: Value to set for of_root
192 * @return 0 if OK, -ve on error
193 */
194static int dm_test_restore(struct device_node *of_root)
195{
196 int ret;
197
198 gd_set_of_root(of_root);
199 gd->dm_root = NULL;
200 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
201 if (ret)
202 return ret;
203 dm_scan_plat(false);
204 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
205 dm_scan_fdt(false);
206
207 return 0;
208}
209
210/**
Simon Glassca44ca02021-03-07 17:35:01 -0700211 * test_pre_run() - Handle any preparation needed to run a test
212 *
213 * @uts: Test state
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
217 * continue)
218 */
219static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd002a272021-03-07 17:34:48 -0700220{
Simon Glass72b524c2021-03-07 17:34:56 -0700221 if (test->flags & UT_TESTF_DM)
Simon Glassc79705e2021-03-07 17:34:58 -0700222 ut_assertok(dm_test_pre_run(uts));
Simon Glass72b524c2021-03-07 17:34:56 -0700223
Simon Glass47ec3ed2021-03-07 17:34:55 -0700224 ut_set_skip_delays(uts, false);
225
Simon Glass19fb3db2021-03-07 17:34:53 -0700226 uts->start = mallinfo();
Simon Glassd002a272021-03-07 17:34:48 -0700227
Simon Glass5a986f32021-03-07 17:34:52 -0700228 if (test->flags & UT_TESTF_SCAN_PDATA)
229 ut_assertok(dm_scan_plat(false));
230
Simon Glass4b8b27e2021-03-07 17:34:51 -0700231 if (test->flags & UT_TESTF_PROBE_TEST)
232 ut_assertok(do_autoprobe(uts));
233
Simon Glassd8ed2342021-03-07 17:34:50 -0700234 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
235 (test->flags & UT_TESTF_SCAN_FDT))
236 ut_assertok(dm_extended_scan(false));
237
Simon Glassd002a272021-03-07 17:34:48 -0700238 if (test->flags & UT_TESTF_CONSOLE_REC) {
239 int ret = console_record_reset_enable();
240
241 if (ret) {
242 printf("Skipping: Console recording disabled\n");
243 return -EAGAIN;
244 }
245 }
Simon Glass74524712021-03-07 17:34:54 -0700246 ut_silence_console(uts);
Simon Glassd002a272021-03-07 17:34:48 -0700247
248 return 0;
249}
250
Simon Glassca44ca02021-03-07 17:35:01 -0700251/**
252 * test_post_run() - Handle cleaning up after a test
253 *
254 * @uts: Test state
255 * @test: Test to clean up after
256 * @return 0 if OK, -ve on error (meaning that testing cannot likely continue)
257 */
258static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd002a272021-03-07 17:34:48 -0700259{
Simon Glass74524712021-03-07 17:34:54 -0700260 ut_unsilence_console(uts);
Simon Glasse77615d2021-03-07 17:34:59 -0700261 if (test->flags & UT_TESTF_DM)
262 ut_assertok(dm_test_post_run(uts));
Simon Glass30a0d202021-03-07 17:34:49 -0700263
Simon Glassd002a272021-03-07 17:34:48 -0700264 return 0;
265}
266
Simon Glassd2281bb2021-03-07 17:35:03 -0700267/**
268 * ut_run_test() - Run a single test
269 *
270 * This runs the test, handling any preparation and clean-up needed. It prints
271 * the name of each test before running it.
272 *
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
279 * any failed
280 */
281static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
282 const char *test_name)
Simon Glass99a88fe2021-03-07 17:35:00 -0700283{
Simon Glassca44ca02021-03-07 17:35:01 -0700284 const char *fname = strrchr(test->file, '/') + 1;
285 const char *note = "";
Simon Glass99a88fe2021-03-07 17:35:00 -0700286 int ret;
287
Simon Glassca44ca02021-03-07 17:35:01 -0700288 if ((test->flags & UT_TESTF_DM) && !uts->of_live)
289 note = " (flat tree)";
290 printf("Test: %s: %s%s\n", test_name, fname, note);
Simon Glass99a88fe2021-03-07 17:35:00 -0700291
Simon Glassfe806862021-03-07 17:35:04 -0700292 /* Allow access to test state from drivers */
293 test_set_state(uts);
294
Simon Glass99a88fe2021-03-07 17:35:00 -0700295 ret = test_pre_run(uts, test);
296 if (ret == -EAGAIN)
297 return -EAGAIN;
298 if (ret)
299 return ret;
300
301 test->func(uts);
302
303 ret = test_post_run(uts, test);
304 if (ret)
305 return ret;
306
Simon Glassfe806862021-03-07 17:35:04 -0700307 test_set_state( NULL);
308
Simon Glass99a88fe2021-03-07 17:35:00 -0700309 return 0;
310}
311
Simon Glassf97f85e2021-03-07 17:35:05 -0700312/**
313 * ut_run_test_live_flat() - Run a test with both live and flat tree
314 *
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
319 * SPL.
320 *
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)
324 * @test: Test to run
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
327 * any failed
328 */
329static int ut_run_test_live_flat(struct unit_test_state *uts,
330 struct unit_test *test, const char *name)
Simon Glassd2281bb2021-03-07 17:35:03 -0700331{
332 int runs;
333
334 /* Run with the live tree if possible */
335 runs = 0;
336 if (CONFIG_IS_ENABLED(OF_LIVE)) {
337 if (!(test->flags & UT_TESTF_FLAT_TREE)) {
338 uts->of_live = true;
339 ut_assertok(ut_run_test(uts, test, test->name));
340 runs++;
341 }
342 }
343
344 /*
345 * Run with the flat tree if we couldn't run it with live tree,
346 * or it is a core test.
347 */
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));
352 runs++;
353 }
354
355 return 0;
356}
357
Simon Glassf97f85e2021-03-07 17:35:05 -0700358/**
359 * ut_run_tests() - Run a set of tests
360 *
361 * This runs the tests, handling any preparation and clean-up needed. It prints
362 * the name of each test before running it.
363 *
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
376 */
377static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
378 struct unit_test *tests, int count,
379 const char *select_name)
Simon Glass1c721752021-03-07 17:34:47 -0700380{
381 struct unit_test *test;
Simon Glass1c721752021-03-07 17:34:47 -0700382 int found = 0;
383
384 for (test = tests; test < tests + count; test++) {
385 const char *test_name = test->name;
Simon Glassd002a272021-03-07 17:34:48 -0700386 int ret;
Simon Glass1c721752021-03-07 17:34:47 -0700387
Simon Glassf97f85e2021-03-07 17:35:05 -0700388 if (!test_matches(prefix, test_name, select_name))
Simon Glass1c721752021-03-07 17:34:47 -0700389 continue;
Simon Glassf97f85e2021-03-07 17:35:05 -0700390 ret = ut_run_test_live_flat(uts, test, select_name);
Simon Glass1c721752021-03-07 17:34:47 -0700391 found++;
Simon Glassd002a272021-03-07 17:34:48 -0700392 if (ret == -EAGAIN)
393 continue;
394 if (ret)
395 return ret;
Simon Glass1c721752021-03-07 17:34:47 -0700396 }
397 if (select_name && !found)
398 return -ENOENT;
399
400 return uts->fail_count ? -EBADF : 0;
401}
402
403int ut_run_list(const char *category, const char *prefix,
404 struct unit_test *tests, int count, const char *select_name)
405{
406 struct unit_test_state uts = { .fail_count = 0 };
Simon Glass664277f2021-03-07 17:35:08 -0700407 bool has_dm_tests = false;
Simon Glass1c721752021-03-07 17:34:47 -0700408 int ret;
409
Simon Glass1fc9c122021-03-07 17:35:07 -0700410 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
411 ut_list_has_dm_tests(tests, count)) {
Simon Glass664277f2021-03-07 17:35:08 -0700412 has_dm_tests = true;
Simon Glass1fc9c122021-03-07 17:35:07 -0700413 /*
414 * If we have no device tree, or it only has a root node, then
415 * these * tests clearly aren't going to work...
416 */
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;
421 }
422 }
423
Simon Glass1c721752021-03-07 17:34:47 -0700424 if (!select_name)
425 printf("Running %d %s tests\n", count, category);
426
Simon Glassf97f85e2021-03-07 17:35:05 -0700427 uts.of_root = gd_of_root();
Simon Glass1c721752021-03-07 17:34:47 -0700428 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
429
430 if (ret == -ENOENT)
431 printf("Test '%s' not found\n", select_name);
432 else
433 printf("Failures: %d\n", uts.fail_count);
434
Simon Glass664277f2021-03-07 17:35:08 -0700435 /* Best efforts only...ignore errors */
436 if (has_dm_tests)
437 dm_test_restore(uts.of_root);
438
Simon Glass1c721752021-03-07 17:34:47 -0700439 return ret;
440}