blob: 46a0c2ee2f6e9559453ccdd927d9cf6f2de958f1 [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>
Simon Glassc79705e2021-03-07 17:34:58 -070010#include <asm/state.h>
Simon Glassd8ed2342021-03-07 17:34:50 -070011#include <dm/root.h>
Simon Glassc79705e2021-03-07 17:34:58 -070012#include <dm/test.h>
Simon Glasse77615d2021-03-07 17:34:59 -070013#include <dm/uclass-internal.h>
Simon Glass1c721752021-03-07 17:34:47 -070014#include <test/test.h>
Simon Glassd8ed2342021-03-07 17:34:50 -070015#include <test/ut.h>
Simon Glass1c721752021-03-07 17:34:47 -070016
Simon Glass30a0d202021-03-07 17:34:49 -070017DECLARE_GLOBAL_DATA_PTR;
18
Simon Glassfe806862021-03-07 17:35:04 -070019/* This is valid when a test is running, NULL otherwise */
20static struct unit_test_state *cur_test_state;
21
22struct unit_test_state *test_get_state(void)
23{
24 return cur_test_state;
25}
26
27void test_set_state(struct unit_test_state *uts)
28{
29 cur_test_state = uts;
30}
31
Simon Glassc79705e2021-03-07 17:34:58 -070032/**
33 * dm_test_pre_run() - Get ready to run a driver model test
34 *
35 * This clears out the driver model data structures. For sandbox it resets the
36 * state structure
37 *
38 * @uts: Test state
39 */
40static int dm_test_pre_run(struct unit_test_state *uts)
41{
42 bool of_live = uts->of_live;
43
44 uts->root = NULL;
45 uts->testdev = NULL;
46 uts->force_fail_alloc = false;
47 uts->skip_post_probe = false;
48 gd->dm_root = NULL;
49 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
50 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
51 state_reset_for_test(state_get_current());
52
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();
57
58 return 0;
59}
60
Simon Glasse77615d2021-03-07 17:34:59 -070061static int dm_test_post_run(struct unit_test_state *uts)
62{
63 int id;
64
Simon Glasse8c023c2021-03-15 17:25:21 +130065 /*
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.
69 */
70 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
71 for (id = 0; id < UCLASS_COUNT; id++) {
72 struct uclass *uc;
Simon Glasse77615d2021-03-07 17:34:59 -070073
Simon Glasse8c023c2021-03-15 17:25:21 +130074 /*
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().
78 */
79 uc = uclass_find(id);
80 if (!uc)
81 continue;
82 ut_assertok(uclass_destroy(uc));
83 }
Simon Glasse77615d2021-03-07 17:34:59 -070084 }
85
86 return 0;
87}
88
Simon Glass4b8b27e2021-03-07 17:34:51 -070089/* Ensure all the test devices are probed */
90static int do_autoprobe(struct unit_test_state *uts)
91{
92 struct udevice *dev;
93 int ret;
94
95 /* Scanning the uclass is enough to probe all the devices */
96 for (ret = uclass_first_device(UCLASS_TEST, &dev);
97 dev;
98 ret = uclass_next_device(&dev))
99 ;
100
101 return ret;
102}
103
Simon Glassd2281bb2021-03-07 17:35:03 -0700104/*
105 * ut_test_run_on_flattree() - Check if we should run a test with flat DT
106 *
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.
109 *
110 * @return true to run the given test on the flat device tree
111 */
112static bool ut_test_run_on_flattree(struct unit_test *test)
113{
114 const char *fname = strrchr(test->file, '/') + 1;
115
116 if (!(test->flags & UT_TESTF_DM))
117 return false;
118
119 return !strstr(fname, "video") || strstr(test->name, "video_base");
120}
121
Simon Glassca44ca02021-03-07 17:35:01 -0700122/**
Simon Glassf97f85e2021-03-07 17:35:05 -0700123 * test_matches() - Check if a test should be run
124 *
125 * This checks if the a test should be run. In the normal case of running all
126 * tests, @select_name is NULL.
127 *
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
Simon Glass84823562021-03-07 17:35:12 -0700130 * of the test name. If NULL, any suite name (xxx_test) is considered to be
131 * a prefix.
Simon Glassf97f85e2021-03-07 17:35:05 -0700132 * @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
135 */
136static bool test_matches(const char *prefix, const char *test_name,
137 const char *select_name)
138{
139 if (!select_name)
140 return true;
141
142 if (!strcmp(test_name, select_name))
143 return true;
144
Simon Glass84823562021-03-07 17:35:12 -0700145 if (!prefix) {
146 const char *p = strstr(test_name, "_test_");
147
148 /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
149 if (p)
150 test_name = p + 6;
151 } else {
152 /* All tests have this prefix */
153 if (!strncmp(test_name, prefix, strlen(prefix)))
154 test_name += strlen(prefix);
155 }
Simon Glassf97f85e2021-03-07 17:35:05 -0700156
157 if (!strcmp(test_name, select_name))
158 return true;
159
160 return false;
161}
162
Simon Glass664277f2021-03-07 17:35:08 -0700163/**
Simon Glass1fc9c122021-03-07 17:35:07 -0700164 * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
165 *
166 * @tests: List of tests to run
167 * @count: Number of tests to ru
168 * @return true if any of the tests have the UT_TESTF_DM flag
169 */
170static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
171{
172 struct unit_test *test;
173
174 for (test = tests; test < tests + count; test++) {
175 if (test->flags & UT_TESTF_DM)
176 return true;
177 }
178
179 return false;
180}
181
Simon Glassf97f85e2021-03-07 17:35:05 -0700182/**
Simon Glass664277f2021-03-07 17:35:08 -0700183 * dm_test_restore() Put things back to normal so sandbox works as expected
184 *
185 * @of_root: Value to set for of_root
186 * @return 0 if OK, -ve on error
187 */
188static int dm_test_restore(struct device_node *of_root)
189{
190 int ret;
191
192 gd_set_of_root(of_root);
193 gd->dm_root = NULL;
194 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
195 if (ret)
196 return ret;
197 dm_scan_plat(false);
198 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
199 dm_scan_fdt(false);
200
201 return 0;
202}
203
204/**
Simon Glassca44ca02021-03-07 17:35:01 -0700205 * test_pre_run() - Handle any preparation needed to run a test
206 *
207 * @uts: Test state
208 * @test: Test to prepare for
209 * @return 0 if OK, -EAGAIN to skip this test since some required feature is not
210 * available, other -ve on error (meaning that testing cannot likely
211 * continue)
212 */
213static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd002a272021-03-07 17:34:48 -0700214{
Simon Glass72b524c2021-03-07 17:34:56 -0700215 if (test->flags & UT_TESTF_DM)
Simon Glassc79705e2021-03-07 17:34:58 -0700216 ut_assertok(dm_test_pre_run(uts));
Simon Glass72b524c2021-03-07 17:34:56 -0700217
Simon Glass47ec3ed2021-03-07 17:34:55 -0700218 ut_set_skip_delays(uts, false);
219
Simon Glass19fb3db2021-03-07 17:34:53 -0700220 uts->start = mallinfo();
Simon Glassd002a272021-03-07 17:34:48 -0700221
Simon Glass5a986f32021-03-07 17:34:52 -0700222 if (test->flags & UT_TESTF_SCAN_PDATA)
223 ut_assertok(dm_scan_plat(false));
224
Simon Glass4b8b27e2021-03-07 17:34:51 -0700225 if (test->flags & UT_TESTF_PROBE_TEST)
226 ut_assertok(do_autoprobe(uts));
227
Simon Glassd8ed2342021-03-07 17:34:50 -0700228 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
229 (test->flags & UT_TESTF_SCAN_FDT))
230 ut_assertok(dm_extended_scan(false));
231
Simon Glassd002a272021-03-07 17:34:48 -0700232 if (test->flags & UT_TESTF_CONSOLE_REC) {
233 int ret = console_record_reset_enable();
234
235 if (ret) {
236 printf("Skipping: Console recording disabled\n");
237 return -EAGAIN;
238 }
239 }
Simon Glass74524712021-03-07 17:34:54 -0700240 ut_silence_console(uts);
Simon Glassd002a272021-03-07 17:34:48 -0700241
242 return 0;
243}
244
Simon Glassca44ca02021-03-07 17:35:01 -0700245/**
246 * test_post_run() - Handle cleaning up after a test
247 *
248 * @uts: Test state
249 * @test: Test to clean up after
250 * @return 0 if OK, -ve on error (meaning that testing cannot likely continue)
251 */
252static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd002a272021-03-07 17:34:48 -0700253{
Simon Glass74524712021-03-07 17:34:54 -0700254 ut_unsilence_console(uts);
Simon Glasse77615d2021-03-07 17:34:59 -0700255 if (test->flags & UT_TESTF_DM)
256 ut_assertok(dm_test_post_run(uts));
Simon Glass30a0d202021-03-07 17:34:49 -0700257
Simon Glassd002a272021-03-07 17:34:48 -0700258 return 0;
259}
260
Simon Glassd2281bb2021-03-07 17:35:03 -0700261/**
262 * ut_run_test() - Run a single test
263 *
264 * This runs the test, handling any preparation and clean-up needed. It prints
265 * the name of each test before running it.
266 *
267 * @uts: Test state to update. The caller should ensure that this is zeroed for
268 * the first call to this function. On exit, @uts->fail_count is
269 * incremented by the number of failures (0, one hopes)
270 * @test_name: Test to run
271 * @name: Name of test, possibly skipping a prefix that should not be displayed
272 * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
273 * any failed
274 */
275static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
276 const char *test_name)
Simon Glass99a88fe2021-03-07 17:35:00 -0700277{
Simon Glassca44ca02021-03-07 17:35:01 -0700278 const char *fname = strrchr(test->file, '/') + 1;
279 const char *note = "";
Simon Glass99a88fe2021-03-07 17:35:00 -0700280 int ret;
281
Simon Glassca44ca02021-03-07 17:35:01 -0700282 if ((test->flags & UT_TESTF_DM) && !uts->of_live)
283 note = " (flat tree)";
284 printf("Test: %s: %s%s\n", test_name, fname, note);
Simon Glass99a88fe2021-03-07 17:35:00 -0700285
Simon Glassfe806862021-03-07 17:35:04 -0700286 /* Allow access to test state from drivers */
287 test_set_state(uts);
288
Simon Glass99a88fe2021-03-07 17:35:00 -0700289 ret = test_pre_run(uts, test);
290 if (ret == -EAGAIN)
291 return -EAGAIN;
292 if (ret)
293 return ret;
294
295 test->func(uts);
296
297 ret = test_post_run(uts, test);
298 if (ret)
299 return ret;
300
Simon Glassfe806862021-03-07 17:35:04 -0700301 test_set_state( NULL);
302
Simon Glass99a88fe2021-03-07 17:35:00 -0700303 return 0;
304}
305
Simon Glassf97f85e2021-03-07 17:35:05 -0700306/**
307 * ut_run_test_live_flat() - Run a test with both live and flat tree
308 *
309 * This calls ut_run_test() with livetree enabled, which is the standard setup
310 * for runnig tests. Then, for driver model test, it calls it again with
311 * livetree disabled. This allows checking of flattree being used when OF_LIVE
312 * is enabled, as is the case in U-Boot proper before relocation, as well as in
313 * SPL.
314 *
315 * @uts: Test state to update. The caller should ensure that this is zeroed for
316 * the first call to this function. On exit, @uts->fail_count is
317 * incremented by the number of failures (0, one hopes)
318 * @test: Test to run
319 * @name: Name of test, possibly skipping a prefix that should not be displayed
320 * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
321 * any failed
322 */
323static int ut_run_test_live_flat(struct unit_test_state *uts,
324 struct unit_test *test, const char *name)
Simon Glassd2281bb2021-03-07 17:35:03 -0700325{
326 int runs;
327
328 /* Run with the live tree if possible */
329 runs = 0;
330 if (CONFIG_IS_ENABLED(OF_LIVE)) {
331 if (!(test->flags & UT_TESTF_FLAT_TREE)) {
332 uts->of_live = true;
333 ut_assertok(ut_run_test(uts, test, test->name));
334 runs++;
335 }
336 }
337
338 /*
339 * Run with the flat tree if we couldn't run it with live tree,
340 * or it is a core test.
341 */
342 if (!(test->flags & UT_TESTF_LIVE_TREE) &&
343 (!runs || ut_test_run_on_flattree(test))) {
344 uts->of_live = false;
345 ut_assertok(ut_run_test(uts, test, test->name));
346 runs++;
347 }
348
349 return 0;
350}
351
Simon Glassf97f85e2021-03-07 17:35:05 -0700352/**
353 * ut_run_tests() - Run a set of tests
354 *
355 * This runs the tests, handling any preparation and clean-up needed. It prints
356 * the name of each test before running it.
357 *
358 * @uts: Test state to update. The caller should ensure that this is zeroed for
359 * the first call to this function. On exit, @uts->fail_count is
360 * incremented by the number of failures (0, one hopes)
361 * @prefix: String prefix for the tests. Any tests that have this prefix will be
362 * printed without the prefix, so that it is easier to see the unique part
363 * of the test name. If NULL, no prefix processing is done
364 * @tests: List of tests to run
365 * @count: Number of tests to run
366 * @select_name: Name of a single test to run (from the list provided). If NULL
367 * then all tests are run
368 * @return 0 if all tests passed, -ENOENT if test @select_name was not found,
369 * -EBADF if any failed
370 */
371static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
372 struct unit_test *tests, int count,
373 const char *select_name)
Simon Glass1c721752021-03-07 17:34:47 -0700374{
375 struct unit_test *test;
Simon Glass1c721752021-03-07 17:34:47 -0700376 int found = 0;
377
378 for (test = tests; test < tests + count; test++) {
379 const char *test_name = test->name;
Simon Glassd002a272021-03-07 17:34:48 -0700380 int ret;
Simon Glass1c721752021-03-07 17:34:47 -0700381
Simon Glassf97f85e2021-03-07 17:35:05 -0700382 if (!test_matches(prefix, test_name, select_name))
Simon Glass1c721752021-03-07 17:34:47 -0700383 continue;
Simon Glassf97f85e2021-03-07 17:35:05 -0700384 ret = ut_run_test_live_flat(uts, test, select_name);
Simon Glass1c721752021-03-07 17:34:47 -0700385 found++;
Simon Glassd002a272021-03-07 17:34:48 -0700386 if (ret == -EAGAIN)
387 continue;
388 if (ret)
389 return ret;
Simon Glass1c721752021-03-07 17:34:47 -0700390 }
391 if (select_name && !found)
392 return -ENOENT;
393
394 return uts->fail_count ? -EBADF : 0;
395}
396
397int ut_run_list(const char *category, const char *prefix,
398 struct unit_test *tests, int count, const char *select_name)
399{
400 struct unit_test_state uts = { .fail_count = 0 };
Simon Glass664277f2021-03-07 17:35:08 -0700401 bool has_dm_tests = false;
Simon Glass1c721752021-03-07 17:34:47 -0700402 int ret;
403
Simon Glass1fc9c122021-03-07 17:35:07 -0700404 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
405 ut_list_has_dm_tests(tests, count)) {
Simon Glass664277f2021-03-07 17:35:08 -0700406 has_dm_tests = true;
Simon Glass1fc9c122021-03-07 17:35:07 -0700407 /*
408 * If we have no device tree, or it only has a root node, then
409 * these * tests clearly aren't going to work...
410 */
411 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
412 puts("Please run with test device tree:\n"
413 " ./u-boot -d arch/sandbox/dts/test.dtb\n");
414 return CMD_RET_FAILURE;
415 }
416 }
417
Simon Glass1c721752021-03-07 17:34:47 -0700418 if (!select_name)
419 printf("Running %d %s tests\n", count, category);
420
Simon Glassf97f85e2021-03-07 17:35:05 -0700421 uts.of_root = gd_of_root();
Simon Glass1c721752021-03-07 17:34:47 -0700422 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
423
424 if (ret == -ENOENT)
425 printf("Test '%s' not found\n", select_name);
426 else
427 printf("Failures: %d\n", uts.fail_count);
428
Simon Glass664277f2021-03-07 17:35:08 -0700429 /* Best efforts only...ignore errors */
430 if (has_dm_tests)
431 dm_test_restore(uts.of_root);
432
Simon Glass1c721752021-03-07 17:34:47 -0700433 return ret;
434}