blob: 6edd49f0b618643037902d420459ae0ce2c2da24 [file] [log] [blame]
Simon Glass5722fb22021-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 Glassd18f7392021-03-07 17:34:50 -07009#include <dm.h>
Simon Glass2dba4762021-03-07 17:34:58 -070010#include <asm/state.h>
Simon Glassd18f7392021-03-07 17:34:50 -070011#include <dm/root.h>
Simon Glass2dba4762021-03-07 17:34:58 -070012#include <dm/test.h>
Simon Glass5b7e55b2021-03-07 17:34:59 -070013#include <dm/uclass-internal.h>
Simon Glass5722fb22021-03-07 17:34:47 -070014#include <test/test.h>
Simon Glassd18f7392021-03-07 17:34:50 -070015#include <test/ut.h>
Simon Glass5722fb22021-03-07 17:34:47 -070016
Simon Glass0f8f6772021-03-07 17:34:49 -070017DECLARE_GLOBAL_DATA_PTR;
18
Simon Glass4066d8d2021-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 Glass2dba4762021-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 Glass5b7e55b2021-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 Glass242357c2021-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 Glass0d32ec22021-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 Glass436687e2021-03-07 17:35:01 -0700114/**
Simon Glassbb2b1732021-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
122 * of the test name. If NULL, no prefix processing is done
123 * @test_name: Name of current test
124 * @select_name: Name of test to run (or NULL for all)
125 * @return true to run this test, false to skip it
126 */
127static bool test_matches(const char *prefix, const char *test_name,
128 const char *select_name)
129{
130 if (!select_name)
131 return true;
132
133 if (!strcmp(test_name, select_name))
134 return true;
135
136 /* All tests have this prefix */
137 if (prefix && !strncmp(test_name, prefix, strlen(prefix)))
138 test_name += strlen(prefix);
139
140 if (!strcmp(test_name, select_name))
141 return true;
142
143 return false;
144}
145
Simon Glass53d1b192021-03-07 17:35:08 -0700146/**
Simon Glass1899e132021-03-07 17:35:07 -0700147 * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
148 *
149 * @tests: List of tests to run
150 * @count: Number of tests to ru
151 * @return true if any of the tests have the UT_TESTF_DM flag
152 */
153static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
154{
155 struct unit_test *test;
156
157 for (test = tests; test < tests + count; test++) {
158 if (test->flags & UT_TESTF_DM)
159 return true;
160 }
161
162 return false;
163}
164
Simon Glassbb2b1732021-03-07 17:35:05 -0700165/**
Simon Glass53d1b192021-03-07 17:35:08 -0700166 * dm_test_restore() Put things back to normal so sandbox works as expected
167 *
168 * @of_root: Value to set for of_root
169 * @return 0 if OK, -ve on error
170 */
171static int dm_test_restore(struct device_node *of_root)
172{
173 int ret;
174
175 gd_set_of_root(of_root);
176 gd->dm_root = NULL;
177 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
178 if (ret)
179 return ret;
180 dm_scan_plat(false);
181 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
182 dm_scan_fdt(false);
183
184 return 0;
185}
186
187/**
Simon Glass436687e2021-03-07 17:35:01 -0700188 * test_pre_run() - Handle any preparation needed to run a test
189 *
190 * @uts: Test state
191 * @test: Test to prepare for
192 * @return 0 if OK, -EAGAIN to skip this test since some required feature is not
193 * available, other -ve on error (meaning that testing cannot likely
194 * continue)
195 */
196static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd93dc7d2021-03-07 17:34:48 -0700197{
Simon Glassb2890a12021-03-07 17:34:56 -0700198 if (test->flags & UT_TESTF_DM)
Simon Glass2dba4762021-03-07 17:34:58 -0700199 ut_assertok(dm_test_pre_run(uts));
Simon Glassb2890a12021-03-07 17:34:56 -0700200
Simon Glass59cad962021-03-07 17:34:55 -0700201 ut_set_skip_delays(uts, false);
202
Simon Glass86a5bd02021-03-07 17:34:53 -0700203 uts->start = mallinfo();
Simon Glassd93dc7d2021-03-07 17:34:48 -0700204
Simon Glass177e0fd2021-03-07 17:34:52 -0700205 if (test->flags & UT_TESTF_SCAN_PDATA)
206 ut_assertok(dm_scan_plat(false));
207
Simon Glass242357c2021-03-07 17:34:51 -0700208 if (test->flags & UT_TESTF_PROBE_TEST)
209 ut_assertok(do_autoprobe(uts));
210
Simon Glassd18f7392021-03-07 17:34:50 -0700211 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
212 (test->flags & UT_TESTF_SCAN_FDT))
213 ut_assertok(dm_extended_scan(false));
214
Simon Glassd93dc7d2021-03-07 17:34:48 -0700215 if (test->flags & UT_TESTF_CONSOLE_REC) {
216 int ret = console_record_reset_enable();
217
218 if (ret) {
219 printf("Skipping: Console recording disabled\n");
220 return -EAGAIN;
221 }
222 }
Simon Glass2b566b92021-03-07 17:34:54 -0700223 ut_silence_console(uts);
Simon Glassd93dc7d2021-03-07 17:34:48 -0700224
225 return 0;
226}
227
Simon Glass436687e2021-03-07 17:35:01 -0700228/**
229 * test_post_run() - Handle cleaning up after a test
230 *
231 * @uts: Test state
232 * @test: Test to clean up after
233 * @return 0 if OK, -ve on error (meaning that testing cannot likely continue)
234 */
235static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd93dc7d2021-03-07 17:34:48 -0700236{
Simon Glass2b566b92021-03-07 17:34:54 -0700237 ut_unsilence_console(uts);
Simon Glass5b7e55b2021-03-07 17:34:59 -0700238 if (test->flags & UT_TESTF_DM)
239 ut_assertok(dm_test_post_run(uts));
Simon Glass0f8f6772021-03-07 17:34:49 -0700240
Simon Glassd93dc7d2021-03-07 17:34:48 -0700241 return 0;
242}
243
Simon Glass0d32ec22021-03-07 17:35:03 -0700244/**
245 * ut_run_test() - Run a single test
246 *
247 * This runs the test, handling any preparation and clean-up needed. It prints
248 * the name of each test before running it.
249 *
250 * @uts: Test state to update. The caller should ensure that this is zeroed for
251 * the first call to this function. On exit, @uts->fail_count is
252 * incremented by the number of failures (0, one hopes)
253 * @test_name: Test to run
254 * @name: Name of test, possibly skipping a prefix that should not be displayed
255 * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
256 * any failed
257 */
258static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
259 const char *test_name)
Simon Glass5517edb2021-03-07 17:35:00 -0700260{
Simon Glass436687e2021-03-07 17:35:01 -0700261 const char *fname = strrchr(test->file, '/') + 1;
262 const char *note = "";
Simon Glass5517edb2021-03-07 17:35:00 -0700263 int ret;
264
Simon Glass436687e2021-03-07 17:35:01 -0700265 if ((test->flags & UT_TESTF_DM) && !uts->of_live)
266 note = " (flat tree)";
267 printf("Test: %s: %s%s\n", test_name, fname, note);
Simon Glass5517edb2021-03-07 17:35:00 -0700268
Simon Glass4066d8d2021-03-07 17:35:04 -0700269 /* Allow access to test state from drivers */
270 test_set_state(uts);
271
Simon Glass5517edb2021-03-07 17:35:00 -0700272 ret = test_pre_run(uts, test);
273 if (ret == -EAGAIN)
274 return -EAGAIN;
275 if (ret)
276 return ret;
277
278 test->func(uts);
279
280 ret = test_post_run(uts, test);
281 if (ret)
282 return ret;
283
Simon Glass4066d8d2021-03-07 17:35:04 -0700284 test_set_state( NULL);
285
Simon Glass5517edb2021-03-07 17:35:00 -0700286 return 0;
287}
288
Simon Glassbb2b1732021-03-07 17:35:05 -0700289/**
290 * ut_run_test_live_flat() - Run a test with both live and flat tree
291 *
292 * This calls ut_run_test() with livetree enabled, which is the standard setup
293 * for runnig tests. Then, for driver model test, it calls it again with
294 * livetree disabled. This allows checking of flattree being used when OF_LIVE
295 * is enabled, as is the case in U-Boot proper before relocation, as well as in
296 * SPL.
297 *
298 * @uts: Test state to update. The caller should ensure that this is zeroed for
299 * the first call to this function. On exit, @uts->fail_count is
300 * incremented by the number of failures (0, one hopes)
301 * @test: Test to run
302 * @name: Name of test, possibly skipping a prefix that should not be displayed
303 * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
304 * any failed
305 */
306static int ut_run_test_live_flat(struct unit_test_state *uts,
307 struct unit_test *test, const char *name)
Simon Glass0d32ec22021-03-07 17:35:03 -0700308{
309 int runs;
310
311 /* Run with the live tree if possible */
312 runs = 0;
313 if (CONFIG_IS_ENABLED(OF_LIVE)) {
314 if (!(test->flags & UT_TESTF_FLAT_TREE)) {
315 uts->of_live = true;
316 ut_assertok(ut_run_test(uts, test, test->name));
317 runs++;
318 }
319 }
320
321 /*
322 * Run with the flat tree if we couldn't run it with live tree,
323 * or it is a core test.
324 */
325 if (!(test->flags & UT_TESTF_LIVE_TREE) &&
326 (!runs || ut_test_run_on_flattree(test))) {
327 uts->of_live = false;
328 ut_assertok(ut_run_test(uts, test, test->name));
329 runs++;
330 }
331
332 return 0;
333}
334
Simon Glassbb2b1732021-03-07 17:35:05 -0700335/**
336 * ut_run_tests() - Run a set of tests
337 *
338 * This runs the tests, handling any preparation and clean-up needed. It prints
339 * the name of each test before running it.
340 *
341 * @uts: Test state to update. The caller should ensure that this is zeroed for
342 * the first call to this function. On exit, @uts->fail_count is
343 * incremented by the number of failures (0, one hopes)
344 * @prefix: String prefix for the tests. Any tests that have this prefix will be
345 * printed without the prefix, so that it is easier to see the unique part
346 * of the test name. If NULL, no prefix processing is done
347 * @tests: List of tests to run
348 * @count: Number of tests to run
349 * @select_name: Name of a single test to run (from the list provided). If NULL
350 * then all tests are run
351 * @return 0 if all tests passed, -ENOENT if test @select_name was not found,
352 * -EBADF if any failed
353 */
354static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
355 struct unit_test *tests, int count,
356 const char *select_name)
Simon Glass5722fb22021-03-07 17:34:47 -0700357{
358 struct unit_test *test;
Simon Glass5722fb22021-03-07 17:34:47 -0700359 int found = 0;
360
361 for (test = tests; test < tests + count; test++) {
362 const char *test_name = test->name;
Simon Glassd93dc7d2021-03-07 17:34:48 -0700363 int ret;
Simon Glass5722fb22021-03-07 17:34:47 -0700364
Simon Glassbb2b1732021-03-07 17:35:05 -0700365 if (!test_matches(prefix, test_name, select_name))
Simon Glass5722fb22021-03-07 17:34:47 -0700366 continue;
Simon Glassbb2b1732021-03-07 17:35:05 -0700367 ret = ut_run_test_live_flat(uts, test, select_name);
Simon Glass5722fb22021-03-07 17:34:47 -0700368 found++;
Simon Glassd93dc7d2021-03-07 17:34:48 -0700369 if (ret == -EAGAIN)
370 continue;
371 if (ret)
372 return ret;
Simon Glass5722fb22021-03-07 17:34:47 -0700373 }
374 if (select_name && !found)
375 return -ENOENT;
376
377 return uts->fail_count ? -EBADF : 0;
378}
379
380int ut_run_list(const char *category, const char *prefix,
381 struct unit_test *tests, int count, const char *select_name)
382{
383 struct unit_test_state uts = { .fail_count = 0 };
Simon Glass53d1b192021-03-07 17:35:08 -0700384 bool has_dm_tests = false;
Simon Glass5722fb22021-03-07 17:34:47 -0700385 int ret;
386
Simon Glass1899e132021-03-07 17:35:07 -0700387 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
388 ut_list_has_dm_tests(tests, count)) {
Simon Glass53d1b192021-03-07 17:35:08 -0700389 has_dm_tests = true;
Simon Glass1899e132021-03-07 17:35:07 -0700390 /*
391 * If we have no device tree, or it only has a root node, then
392 * these * tests clearly aren't going to work...
393 */
394 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
395 puts("Please run with test device tree:\n"
396 " ./u-boot -d arch/sandbox/dts/test.dtb\n");
397 return CMD_RET_FAILURE;
398 }
399 }
400
Simon Glass5722fb22021-03-07 17:34:47 -0700401 if (!select_name)
402 printf("Running %d %s tests\n", count, category);
403
Simon Glassbb2b1732021-03-07 17:35:05 -0700404 uts.of_root = gd_of_root();
Simon Glass5722fb22021-03-07 17:34:47 -0700405 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
406
407 if (ret == -ENOENT)
408 printf("Test '%s' not found\n", select_name);
409 else
410 printf("Failures: %d\n", uts.fail_count);
411
Simon Glass53d1b192021-03-07 17:35:08 -0700412 /* Best efforts only...ignore errors */
413 if (has_dm_tests)
414 dm_test_restore(uts.of_root);
415
Simon Glass5722fb22021-03-07 17:34:47 -0700416 return ret;
417}