blob: 8c852d72f482bf92bb58cb920dfd6564a80cf5ef [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 Glassd4a15922021-03-25 10:44:33 +130048 if (IS_ENABLED(CONFIG_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{
138 if (!select_name)
139 return true;
140
141 if (!strcmp(test_name, select_name))
142 return true;
143
Simon Glass84823562021-03-07 17:35:12 -0700144 if (!prefix) {
145 const char *p = strstr(test_name, "_test_");
146
147 /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
148 if (p)
149 test_name = p + 6;
150 } else {
151 /* All tests have this prefix */
152 if (!strncmp(test_name, prefix, strlen(prefix)))
153 test_name += strlen(prefix);
154 }
Simon Glassf97f85e2021-03-07 17:35:05 -0700155
156 if (!strcmp(test_name, select_name))
157 return true;
158
159 return false;
160}
161
Simon Glass664277f2021-03-07 17:35:08 -0700162/**
Simon Glass1fc9c122021-03-07 17:35:07 -0700163 * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
164 *
165 * @tests: List of tests to run
166 * @count: Number of tests to ru
167 * @return true if any of the tests have the UT_TESTF_DM flag
168 */
169static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
170{
171 struct unit_test *test;
172
173 for (test = tests; test < tests + count; test++) {
174 if (test->flags & UT_TESTF_DM)
175 return true;
176 }
177
178 return false;
179}
180
Simon Glassf97f85e2021-03-07 17:35:05 -0700181/**
Simon Glass664277f2021-03-07 17:35:08 -0700182 * dm_test_restore() Put things back to normal so sandbox works as expected
183 *
184 * @of_root: Value to set for of_root
185 * @return 0 if OK, -ve on error
186 */
187static int dm_test_restore(struct device_node *of_root)
188{
189 int ret;
190
191 gd_set_of_root(of_root);
192 gd->dm_root = NULL;
193 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
194 if (ret)
195 return ret;
196 dm_scan_plat(false);
197 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
198 dm_scan_fdt(false);
199
200 return 0;
201}
202
203/**
Simon Glassca44ca02021-03-07 17:35:01 -0700204 * test_pre_run() - Handle any preparation needed to run a test
205 *
206 * @uts: Test state
207 * @test: Test to prepare for
208 * @return 0 if OK, -EAGAIN to skip this test since some required feature is not
209 * available, other -ve on error (meaning that testing cannot likely
210 * continue)
211 */
212static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd002a272021-03-07 17:34:48 -0700213{
Simon Glass72b524c2021-03-07 17:34:56 -0700214 if (test->flags & UT_TESTF_DM)
Simon Glassc79705e2021-03-07 17:34:58 -0700215 ut_assertok(dm_test_pre_run(uts));
Simon Glass72b524c2021-03-07 17:34:56 -0700216
Simon Glass47ec3ed2021-03-07 17:34:55 -0700217 ut_set_skip_delays(uts, false);
218
Simon Glass19fb3db2021-03-07 17:34:53 -0700219 uts->start = mallinfo();
Simon Glassd002a272021-03-07 17:34:48 -0700220
Simon Glass5a986f32021-03-07 17:34:52 -0700221 if (test->flags & UT_TESTF_SCAN_PDATA)
222 ut_assertok(dm_scan_plat(false));
223
Simon Glass4b8b27e2021-03-07 17:34:51 -0700224 if (test->flags & UT_TESTF_PROBE_TEST)
225 ut_assertok(do_autoprobe(uts));
226
Simon Glassd8ed2342021-03-07 17:34:50 -0700227 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
228 (test->flags & UT_TESTF_SCAN_FDT))
229 ut_assertok(dm_extended_scan(false));
230
Simon Glassd002a272021-03-07 17:34:48 -0700231 if (test->flags & UT_TESTF_CONSOLE_REC) {
232 int ret = console_record_reset_enable();
233
234 if (ret) {
235 printf("Skipping: Console recording disabled\n");
236 return -EAGAIN;
237 }
238 }
Simon Glass74524712021-03-07 17:34:54 -0700239 ut_silence_console(uts);
Simon Glassd002a272021-03-07 17:34:48 -0700240
241 return 0;
242}
243
Simon Glassca44ca02021-03-07 17:35:01 -0700244/**
245 * test_post_run() - Handle cleaning up after a test
246 *
247 * @uts: Test state
248 * @test: Test to clean up after
249 * @return 0 if OK, -ve on error (meaning that testing cannot likely continue)
250 */
251static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd002a272021-03-07 17:34:48 -0700252{
Simon Glass74524712021-03-07 17:34:54 -0700253 ut_unsilence_console(uts);
Simon Glasse77615d2021-03-07 17:34:59 -0700254 if (test->flags & UT_TESTF_DM)
255 ut_assertok(dm_test_post_run(uts));
Simon Glass30a0d202021-03-07 17:34:49 -0700256
Simon Glassd002a272021-03-07 17:34:48 -0700257 return 0;
258}
259
Simon Glassd2281bb2021-03-07 17:35:03 -0700260/**
261 * ut_run_test() - Run a single test
262 *
263 * This runs the test, handling any preparation and clean-up needed. It prints
264 * the name of each test before running it.
265 *
266 * @uts: Test state to update. The caller should ensure that this is zeroed for
267 * the first call to this function. On exit, @uts->fail_count is
268 * incremented by the number of failures (0, one hopes)
269 * @test_name: Test to run
270 * @name: Name of test, possibly skipping a prefix that should not be displayed
271 * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
272 * any failed
273 */
274static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
275 const char *test_name)
Simon Glass99a88fe2021-03-07 17:35:00 -0700276{
Simon Glassca44ca02021-03-07 17:35:01 -0700277 const char *fname = strrchr(test->file, '/') + 1;
278 const char *note = "";
Simon Glass99a88fe2021-03-07 17:35:00 -0700279 int ret;
280
Simon Glassca44ca02021-03-07 17:35:01 -0700281 if ((test->flags & UT_TESTF_DM) && !uts->of_live)
282 note = " (flat tree)";
283 printf("Test: %s: %s%s\n", test_name, fname, note);
Simon Glass99a88fe2021-03-07 17:35:00 -0700284
Simon Glassfe806862021-03-07 17:35:04 -0700285 /* Allow access to test state from drivers */
286 test_set_state(uts);
287
Simon Glass99a88fe2021-03-07 17:35:00 -0700288 ret = test_pre_run(uts, test);
289 if (ret == -EAGAIN)
290 return -EAGAIN;
291 if (ret)
292 return ret;
293
294 test->func(uts);
295
296 ret = test_post_run(uts, test);
297 if (ret)
298 return ret;
299
Simon Glassfe806862021-03-07 17:35:04 -0700300 test_set_state( NULL);
301
Simon Glass99a88fe2021-03-07 17:35:00 -0700302 return 0;
303}
304
Simon Glassf97f85e2021-03-07 17:35:05 -0700305/**
306 * ut_run_test_live_flat() - Run a test with both live and flat tree
307 *
308 * This calls ut_run_test() with livetree enabled, which is the standard setup
309 * for runnig tests. Then, for driver model test, it calls it again with
310 * livetree disabled. This allows checking of flattree being used when OF_LIVE
311 * is enabled, as is the case in U-Boot proper before relocation, as well as in
312 * SPL.
313 *
314 * @uts: Test state to update. The caller should ensure that this is zeroed for
315 * the first call to this function. On exit, @uts->fail_count is
316 * incremented by the number of failures (0, one hopes)
317 * @test: Test to run
318 * @name: Name of test, possibly skipping a prefix that should not be displayed
319 * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
320 * any failed
321 */
322static int ut_run_test_live_flat(struct unit_test_state *uts,
323 struct unit_test *test, const char *name)
Simon Glassd2281bb2021-03-07 17:35:03 -0700324{
325 int runs;
326
327 /* Run with the live tree if possible */
328 runs = 0;
329 if (CONFIG_IS_ENABLED(OF_LIVE)) {
330 if (!(test->flags & UT_TESTF_FLAT_TREE)) {
331 uts->of_live = true;
332 ut_assertok(ut_run_test(uts, test, test->name));
333 runs++;
334 }
335 }
336
337 /*
338 * Run with the flat tree if we couldn't run it with live tree,
339 * or it is a core test.
340 */
341 if (!(test->flags & UT_TESTF_LIVE_TREE) &&
342 (!runs || ut_test_run_on_flattree(test))) {
343 uts->of_live = false;
344 ut_assertok(ut_run_test(uts, test, test->name));
345 runs++;
346 }
347
348 return 0;
349}
350
Simon Glassf97f85e2021-03-07 17:35:05 -0700351/**
352 * ut_run_tests() - Run a set of tests
353 *
354 * This runs the tests, handling any preparation and clean-up needed. It prints
355 * the name of each test before running it.
356 *
357 * @uts: Test state to update. The caller should ensure that this is zeroed for
358 * the first call to this function. On exit, @uts->fail_count is
359 * incremented by the number of failures (0, one hopes)
360 * @prefix: String prefix for the tests. Any tests that have this prefix will be
361 * printed without the prefix, so that it is easier to see the unique part
362 * of the test name. If NULL, no prefix processing is done
363 * @tests: List of tests to run
364 * @count: Number of tests to run
365 * @select_name: Name of a single test to run (from the list provided). If NULL
366 * then all tests are run
367 * @return 0 if all tests passed, -ENOENT if test @select_name was not found,
368 * -EBADF if any failed
369 */
370static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
371 struct unit_test *tests, int count,
372 const char *select_name)
Simon Glass1c721752021-03-07 17:34:47 -0700373{
374 struct unit_test *test;
Simon Glass1c721752021-03-07 17:34:47 -0700375 int found = 0;
376
377 for (test = tests; test < tests + count; test++) {
378 const char *test_name = test->name;
Simon Glassd002a272021-03-07 17:34:48 -0700379 int ret;
Simon Glass1c721752021-03-07 17:34:47 -0700380
Simon Glassf97f85e2021-03-07 17:35:05 -0700381 if (!test_matches(prefix, test_name, select_name))
Simon Glass1c721752021-03-07 17:34:47 -0700382 continue;
Simon Glassf97f85e2021-03-07 17:35:05 -0700383 ret = ut_run_test_live_flat(uts, test, select_name);
Simon Glass1c721752021-03-07 17:34:47 -0700384 found++;
Simon Glassd002a272021-03-07 17:34:48 -0700385 if (ret == -EAGAIN)
386 continue;
387 if (ret)
388 return ret;
Simon Glass1c721752021-03-07 17:34:47 -0700389 }
390 if (select_name && !found)
391 return -ENOENT;
392
393 return uts->fail_count ? -EBADF : 0;
394}
395
396int ut_run_list(const char *category, const char *prefix,
397 struct unit_test *tests, int count, const char *select_name)
398{
399 struct unit_test_state uts = { .fail_count = 0 };
Simon Glass664277f2021-03-07 17:35:08 -0700400 bool has_dm_tests = false;
Simon Glass1c721752021-03-07 17:34:47 -0700401 int ret;
402
Simon Glass1fc9c122021-03-07 17:35:07 -0700403 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
404 ut_list_has_dm_tests(tests, count)) {
Simon Glass664277f2021-03-07 17:35:08 -0700405 has_dm_tests = true;
Simon Glass1fc9c122021-03-07 17:35:07 -0700406 /*
407 * If we have no device tree, or it only has a root node, then
408 * these * tests clearly aren't going to work...
409 */
410 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
411 puts("Please run with test device tree:\n"
412 " ./u-boot -d arch/sandbox/dts/test.dtb\n");
413 return CMD_RET_FAILURE;
414 }
415 }
416
Simon Glass1c721752021-03-07 17:34:47 -0700417 if (!select_name)
418 printf("Running %d %s tests\n", count, category);
419
Simon Glassf97f85e2021-03-07 17:35:05 -0700420 uts.of_root = gd_of_root();
Simon Glass1c721752021-03-07 17:34:47 -0700421 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
422
423 if (ret == -ENOENT)
424 printf("Test '%s' not found\n", select_name);
425 else
426 printf("Failures: %d\n", uts.fail_count);
427
Simon Glass664277f2021-03-07 17:35:08 -0700428 /* Best efforts only...ignore errors */
429 if (has_dm_tests)
430 dm_test_restore(uts.of_root);
431
Simon Glass1c721752021-03-07 17:34:47 -0700432 return ret;
433}