blob: e1b49e091ab662f97105240f7287a95a2dd7bef2 [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
65 for (id = 0; id < UCLASS_COUNT; id++) {
66 struct uclass *uc;
67
68 /*
69 * If the uclass doesn't exist we don't want to create it. So
70 * check that here before we call uclass_find_device().
71 */
72 uc = uclass_find(id);
73 if (!uc)
74 continue;
75 ut_assertok(uclass_destroy(uc));
76 }
77
78 return 0;
79}
80
Simon Glass4b8b27e2021-03-07 17:34:51 -070081/* Ensure all the test devices are probed */
82static int do_autoprobe(struct unit_test_state *uts)
83{
84 struct udevice *dev;
85 int ret;
86
87 /* Scanning the uclass is enough to probe all the devices */
88 for (ret = uclass_first_device(UCLASS_TEST, &dev);
89 dev;
90 ret = uclass_next_device(&dev))
91 ;
92
93 return ret;
94}
95
Simon Glassd2281bb2021-03-07 17:35:03 -070096/*
97 * ut_test_run_on_flattree() - Check if we should run a test with flat DT
98 *
99 * This skips long/slow tests where there is not much value in running a flat
100 * DT test in addition to a live DT test.
101 *
102 * @return true to run the given test on the flat device tree
103 */
104static bool ut_test_run_on_flattree(struct unit_test *test)
105{
106 const char *fname = strrchr(test->file, '/') + 1;
107
108 if (!(test->flags & UT_TESTF_DM))
109 return false;
110
111 return !strstr(fname, "video") || strstr(test->name, "video_base");
112}
113
Simon Glassca44ca02021-03-07 17:35:01 -0700114/**
Simon Glassf97f85e2021-03-07 17:35:05 -0700115 * test_matches() - Check if a test should be run
116 *
117 * This checks if the a test should be run. In the normal case of running all
118 * tests, @select_name is NULL.
119 *
120 * @prefix: String prefix for the tests. Any tests that have this prefix will be
121 * printed without the prefix, so that it is easier to see the unique part
Simon Glass84823562021-03-07 17:35:12 -0700122 * of the test name. If NULL, any suite name (xxx_test) is considered to be
123 * a prefix.
Simon Glassf97f85e2021-03-07 17:35:05 -0700124 * @test_name: Name of current test
125 * @select_name: Name of test to run (or NULL for all)
126 * @return true to run this test, false to skip it
127 */
128static bool test_matches(const char *prefix, const char *test_name,
129 const char *select_name)
130{
131 if (!select_name)
132 return true;
133
134 if (!strcmp(test_name, select_name))
135 return true;
136
Simon Glass84823562021-03-07 17:35:12 -0700137 if (!prefix) {
138 const char *p = strstr(test_name, "_test_");
139
140 /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
141 if (p)
142 test_name = p + 6;
143 } else {
144 /* All tests have this prefix */
145 if (!strncmp(test_name, prefix, strlen(prefix)))
146 test_name += strlen(prefix);
147 }
Simon Glassf97f85e2021-03-07 17:35:05 -0700148
149 if (!strcmp(test_name, select_name))
150 return true;
151
152 return false;
153}
154
Simon Glass664277f2021-03-07 17:35:08 -0700155/**
Simon Glass1fc9c122021-03-07 17:35:07 -0700156 * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
157 *
158 * @tests: List of tests to run
159 * @count: Number of tests to ru
160 * @return true if any of the tests have the UT_TESTF_DM flag
161 */
162static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
163{
164 struct unit_test *test;
165
166 for (test = tests; test < tests + count; test++) {
167 if (test->flags & UT_TESTF_DM)
168 return true;
169 }
170
171 return false;
172}
173
Simon Glassf97f85e2021-03-07 17:35:05 -0700174/**
Simon Glass664277f2021-03-07 17:35:08 -0700175 * dm_test_restore() Put things back to normal so sandbox works as expected
176 *
177 * @of_root: Value to set for of_root
178 * @return 0 if OK, -ve on error
179 */
180static int dm_test_restore(struct device_node *of_root)
181{
182 int ret;
183
184 gd_set_of_root(of_root);
185 gd->dm_root = NULL;
186 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
187 if (ret)
188 return ret;
189 dm_scan_plat(false);
190 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
191 dm_scan_fdt(false);
192
193 return 0;
194}
195
196/**
Simon Glassca44ca02021-03-07 17:35:01 -0700197 * test_pre_run() - Handle any preparation needed to run a test
198 *
199 * @uts: Test state
200 * @test: Test to prepare for
201 * @return 0 if OK, -EAGAIN to skip this test since some required feature is not
202 * available, other -ve on error (meaning that testing cannot likely
203 * continue)
204 */
205static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd002a272021-03-07 17:34:48 -0700206{
Simon Glass72b524c2021-03-07 17:34:56 -0700207 if (test->flags & UT_TESTF_DM)
Simon Glassc79705e2021-03-07 17:34:58 -0700208 ut_assertok(dm_test_pre_run(uts));
Simon Glass72b524c2021-03-07 17:34:56 -0700209
Simon Glass47ec3ed2021-03-07 17:34:55 -0700210 ut_set_skip_delays(uts, false);
211
Simon Glass19fb3db2021-03-07 17:34:53 -0700212 uts->start = mallinfo();
Simon Glassd002a272021-03-07 17:34:48 -0700213
Simon Glass5a986f32021-03-07 17:34:52 -0700214 if (test->flags & UT_TESTF_SCAN_PDATA)
215 ut_assertok(dm_scan_plat(false));
216
Simon Glass4b8b27e2021-03-07 17:34:51 -0700217 if (test->flags & UT_TESTF_PROBE_TEST)
218 ut_assertok(do_autoprobe(uts));
219
Simon Glassd8ed2342021-03-07 17:34:50 -0700220 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
221 (test->flags & UT_TESTF_SCAN_FDT))
222 ut_assertok(dm_extended_scan(false));
223
Simon Glassd002a272021-03-07 17:34:48 -0700224 if (test->flags & UT_TESTF_CONSOLE_REC) {
225 int ret = console_record_reset_enable();
226
227 if (ret) {
228 printf("Skipping: Console recording disabled\n");
229 return -EAGAIN;
230 }
231 }
Simon Glass74524712021-03-07 17:34:54 -0700232 ut_silence_console(uts);
Simon Glassd002a272021-03-07 17:34:48 -0700233
234 return 0;
235}
236
Simon Glassca44ca02021-03-07 17:35:01 -0700237/**
238 * test_post_run() - Handle cleaning up after a test
239 *
240 * @uts: Test state
241 * @test: Test to clean up after
242 * @return 0 if OK, -ve on error (meaning that testing cannot likely continue)
243 */
244static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd002a272021-03-07 17:34:48 -0700245{
Simon Glass74524712021-03-07 17:34:54 -0700246 ut_unsilence_console(uts);
Simon Glasse77615d2021-03-07 17:34:59 -0700247 if (test->flags & UT_TESTF_DM)
248 ut_assertok(dm_test_post_run(uts));
Simon Glass30a0d202021-03-07 17:34:49 -0700249
Simon Glassd002a272021-03-07 17:34:48 -0700250 return 0;
251}
252
Simon Glassd2281bb2021-03-07 17:35:03 -0700253/**
254 * ut_run_test() - Run a single test
255 *
256 * This runs the test, handling any preparation and clean-up needed. It prints
257 * the name of each test before running it.
258 *
259 * @uts: Test state to update. The caller should ensure that this is zeroed for
260 * the first call to this function. On exit, @uts->fail_count is
261 * incremented by the number of failures (0, one hopes)
262 * @test_name: Test to run
263 * @name: Name of test, possibly skipping a prefix that should not be displayed
264 * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
265 * any failed
266 */
267static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
268 const char *test_name)
Simon Glass99a88fe2021-03-07 17:35:00 -0700269{
Simon Glassca44ca02021-03-07 17:35:01 -0700270 const char *fname = strrchr(test->file, '/') + 1;
271 const char *note = "";
Simon Glass99a88fe2021-03-07 17:35:00 -0700272 int ret;
273
Simon Glassca44ca02021-03-07 17:35:01 -0700274 if ((test->flags & UT_TESTF_DM) && !uts->of_live)
275 note = " (flat tree)";
276 printf("Test: %s: %s%s\n", test_name, fname, note);
Simon Glass99a88fe2021-03-07 17:35:00 -0700277
Simon Glassfe806862021-03-07 17:35:04 -0700278 /* Allow access to test state from drivers */
279 test_set_state(uts);
280
Simon Glass99a88fe2021-03-07 17:35:00 -0700281 ret = test_pre_run(uts, test);
282 if (ret == -EAGAIN)
283 return -EAGAIN;
284 if (ret)
285 return ret;
286
287 test->func(uts);
288
289 ret = test_post_run(uts, test);
290 if (ret)
291 return ret;
292
Simon Glassfe806862021-03-07 17:35:04 -0700293 test_set_state( NULL);
294
Simon Glass99a88fe2021-03-07 17:35:00 -0700295 return 0;
296}
297
Simon Glassf97f85e2021-03-07 17:35:05 -0700298/**
299 * ut_run_test_live_flat() - Run a test with both live and flat tree
300 *
301 * This calls ut_run_test() with livetree enabled, which is the standard setup
302 * for runnig tests. Then, for driver model test, it calls it again with
303 * livetree disabled. This allows checking of flattree being used when OF_LIVE
304 * is enabled, as is the case in U-Boot proper before relocation, as well as in
305 * SPL.
306 *
307 * @uts: Test state to update. The caller should ensure that this is zeroed for
308 * the first call to this function. On exit, @uts->fail_count is
309 * incremented by the number of failures (0, one hopes)
310 * @test: Test to run
311 * @name: Name of test, possibly skipping a prefix that should not be displayed
312 * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
313 * any failed
314 */
315static int ut_run_test_live_flat(struct unit_test_state *uts,
316 struct unit_test *test, const char *name)
Simon Glassd2281bb2021-03-07 17:35:03 -0700317{
318 int runs;
319
320 /* Run with the live tree if possible */
321 runs = 0;
322 if (CONFIG_IS_ENABLED(OF_LIVE)) {
323 if (!(test->flags & UT_TESTF_FLAT_TREE)) {
324 uts->of_live = true;
325 ut_assertok(ut_run_test(uts, test, test->name));
326 runs++;
327 }
328 }
329
330 /*
331 * Run with the flat tree if we couldn't run it with live tree,
332 * or it is a core test.
333 */
334 if (!(test->flags & UT_TESTF_LIVE_TREE) &&
335 (!runs || ut_test_run_on_flattree(test))) {
336 uts->of_live = false;
337 ut_assertok(ut_run_test(uts, test, test->name));
338 runs++;
339 }
340
341 return 0;
342}
343
Simon Glassf97f85e2021-03-07 17:35:05 -0700344/**
345 * ut_run_tests() - Run a set of tests
346 *
347 * This runs the tests, handling any preparation and clean-up needed. It prints
348 * the name of each test before running it.
349 *
350 * @uts: Test state to update. The caller should ensure that this is zeroed for
351 * the first call to this function. On exit, @uts->fail_count is
352 * incremented by the number of failures (0, one hopes)
353 * @prefix: String prefix for the tests. Any tests that have this prefix will be
354 * printed without the prefix, so that it is easier to see the unique part
355 * of the test name. If NULL, no prefix processing is done
356 * @tests: List of tests to run
357 * @count: Number of tests to run
358 * @select_name: Name of a single test to run (from the list provided). If NULL
359 * then all tests are run
360 * @return 0 if all tests passed, -ENOENT if test @select_name was not found,
361 * -EBADF if any failed
362 */
363static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
364 struct unit_test *tests, int count,
365 const char *select_name)
Simon Glass1c721752021-03-07 17:34:47 -0700366{
367 struct unit_test *test;
Simon Glass1c721752021-03-07 17:34:47 -0700368 int found = 0;
369
370 for (test = tests; test < tests + count; test++) {
371 const char *test_name = test->name;
Simon Glassd002a272021-03-07 17:34:48 -0700372 int ret;
Simon Glass1c721752021-03-07 17:34:47 -0700373
Simon Glassf97f85e2021-03-07 17:35:05 -0700374 if (!test_matches(prefix, test_name, select_name))
Simon Glass1c721752021-03-07 17:34:47 -0700375 continue;
Simon Glassf97f85e2021-03-07 17:35:05 -0700376 ret = ut_run_test_live_flat(uts, test, select_name);
Simon Glass1c721752021-03-07 17:34:47 -0700377 found++;
Simon Glassd002a272021-03-07 17:34:48 -0700378 if (ret == -EAGAIN)
379 continue;
380 if (ret)
381 return ret;
Simon Glass1c721752021-03-07 17:34:47 -0700382 }
383 if (select_name && !found)
384 return -ENOENT;
385
386 return uts->fail_count ? -EBADF : 0;
387}
388
389int ut_run_list(const char *category, const char *prefix,
390 struct unit_test *tests, int count, const char *select_name)
391{
392 struct unit_test_state uts = { .fail_count = 0 };
Simon Glass664277f2021-03-07 17:35:08 -0700393 bool has_dm_tests = false;
Simon Glass1c721752021-03-07 17:34:47 -0700394 int ret;
395
Simon Glass1fc9c122021-03-07 17:35:07 -0700396 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
397 ut_list_has_dm_tests(tests, count)) {
Simon Glass664277f2021-03-07 17:35:08 -0700398 has_dm_tests = true;
Simon Glass1fc9c122021-03-07 17:35:07 -0700399 /*
400 * If we have no device tree, or it only has a root node, then
401 * these * tests clearly aren't going to work...
402 */
403 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
404 puts("Please run with test device tree:\n"
405 " ./u-boot -d arch/sandbox/dts/test.dtb\n");
406 return CMD_RET_FAILURE;
407 }
408 }
409
Simon Glass1c721752021-03-07 17:34:47 -0700410 if (!select_name)
411 printf("Running %d %s tests\n", count, category);
412
Simon Glassf97f85e2021-03-07 17:35:05 -0700413 uts.of_root = gd_of_root();
Simon Glass1c721752021-03-07 17:34:47 -0700414 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
415
416 if (ret == -ENOENT)
417 printf("Test '%s' not found\n", select_name);
418 else
419 printf("Failures: %d\n", uts.fail_count);
420
Simon Glass664277f2021-03-07 17:35:08 -0700421 /* Best efforts only...ignore errors */
422 if (has_dm_tests)
423 dm_test_restore(uts.of_root);
424
Simon Glass1c721752021-03-07 17:34:47 -0700425 return ret;
426}