blob: ae34002a3d4cb88a67fb467e2f1e22a1e822da6a [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>
Stefan Roese5be8f372022-09-02 13:57:54 +02009#include <cyclic.h>
Simon Glassd18f7392021-03-07 17:34:50 -070010#include <dm.h>
Simon Glassa128b1b2022-03-04 08:43:01 -070011#include <event.h>
Simon Glassd18f7392021-03-07 17:34:50 -070012#include <dm/root.h>
Simon Glass2dba4762021-03-07 17:34:58 -070013#include <dm/test.h>
Simon Glass5b7e55b2021-03-07 17:34:59 -070014#include <dm/uclass-internal.h>
Simon Glass5722fb22021-03-07 17:34:47 -070015#include <test/test.h>
Simon Glassd18f7392021-03-07 17:34:50 -070016#include <test/ut.h>
Simon Glass5722fb22021-03-07 17:34:47 -070017
Simon Glass0f8f6772021-03-07 17:34:49 -070018DECLARE_GLOBAL_DATA_PTR;
19
Simon Glass4066d8d2021-03-07 17:35:04 -070020/* This is valid when a test is running, NULL otherwise */
21static struct unit_test_state *cur_test_state;
22
23struct unit_test_state *test_get_state(void)
24{
25 return cur_test_state;
26}
27
28void test_set_state(struct unit_test_state *uts)
29{
30 cur_test_state = uts;
31}
32
Simon Glass2dba4762021-03-07 17:34:58 -070033/**
34 * dm_test_pre_run() - Get ready to run a driver model test
35 *
36 * This clears out the driver model data structures. For sandbox it resets the
37 * state structure
38 *
39 * @uts: Test state
40 */
41static int dm_test_pre_run(struct unit_test_state *uts)
42{
43 bool of_live = uts->of_live;
44
45 uts->root = NULL;
46 uts->testdev = NULL;
47 uts->force_fail_alloc = false;
48 uts->skip_post_probe = false;
49 gd->dm_root = NULL;
Simon Glassf7005042021-07-05 16:32:43 -060050 if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA))
Simon Glass2dba4762021-03-07 17:34:58 -070051 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
Simon Glass45b807d2021-03-25 10:44:33 +130052 arch_reset_for_test();
Simon Glass2dba4762021-03-07 17:34:58 -070053
54 /* Determine whether to make the live tree available */
55 gd_set_of_root(of_live ? uts->of_root : NULL);
56 ut_assertok(dm_init(of_live));
57 uts->root = dm_root();
58
59 return 0;
60}
61
Simon Glass5b7e55b2021-03-07 17:34:59 -070062static int dm_test_post_run(struct unit_test_state *uts)
63{
64 int id;
65
Simon Glass96113c12021-03-15 17:25:21 +130066 /*
67 * With of-platdata-inst the uclasses are created at build time. If we
68 * destroy them we cannot get them back since uclass_add() is not
69 * supported. So skip this.
70 */
71 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
72 for (id = 0; id < UCLASS_COUNT; id++) {
73 struct uclass *uc;
Simon Glass5b7e55b2021-03-07 17:34:59 -070074
Simon Glass96113c12021-03-15 17:25:21 +130075 /*
76 * If the uclass doesn't exist we don't want to create
77 * it. So check that here before we call
78 * uclass_find_device().
79 */
80 uc = uclass_find(id);
81 if (!uc)
82 continue;
83 ut_assertok(uclass_destroy(uc));
84 }
Simon Glass5b7e55b2021-03-07 17:34:59 -070085 }
86
87 return 0;
88}
89
Simon Glass242357c2021-03-07 17:34:51 -070090/* Ensure all the test devices are probed */
91static int do_autoprobe(struct unit_test_state *uts)
92{
93 struct udevice *dev;
94 int ret;
95
96 /* Scanning the uclass is enough to probe all the devices */
97 for (ret = uclass_first_device(UCLASS_TEST, &dev);
98 dev;
99 ret = uclass_next_device(&dev))
100 ;
101
102 return ret;
103}
104
Simon Glass0d32ec22021-03-07 17:35:03 -0700105/*
106 * ut_test_run_on_flattree() - Check if we should run a test with flat DT
107 *
108 * This skips long/slow tests where there is not much value in running a flat
109 * DT test in addition to a live DT test.
110 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100111 * Return: true to run the given test on the flat device tree
Simon Glass0d32ec22021-03-07 17:35:03 -0700112 */
113static bool ut_test_run_on_flattree(struct unit_test *test)
114{
115 const char *fname = strrchr(test->file, '/') + 1;
116
117 if (!(test->flags & UT_TESTF_DM))
118 return false;
119
120 return !strstr(fname, "video") || strstr(test->name, "video_base");
121}
122
Simon Glass436687e2021-03-07 17:35:01 -0700123/**
Simon Glassbb2b1732021-03-07 17:35:05 -0700124 * test_matches() - Check if a test should be run
125 *
126 * This checks if the a test should be run. In the normal case of running all
127 * tests, @select_name is NULL.
128 *
129 * @prefix: String prefix for the tests. Any tests that have this prefix will be
130 * printed without the prefix, so that it is easier to see the unique part
Simon Glass1ef74ab2021-03-07 17:35:12 -0700131 * of the test name. If NULL, any suite name (xxx_test) is considered to be
132 * a prefix.
Simon Glassbb2b1732021-03-07 17:35:05 -0700133 * @test_name: Name of current test
134 * @select_name: Name of test to run (or NULL for all)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100135 * Return: true to run this test, false to skip it
Simon Glassbb2b1732021-03-07 17:35:05 -0700136 */
137static bool test_matches(const char *prefix, const char *test_name,
138 const char *select_name)
139{
Andy Shevchenkod91cfbb2021-02-11 16:40:10 +0200140 size_t len;
141
Simon Glassbb2b1732021-03-07 17:35:05 -0700142 if (!select_name)
143 return true;
144
Andy Shevchenkod91cfbb2021-02-11 16:40:10 +0200145 /* Allow glob expansion in the test name */
146 len = select_name[strlen(select_name) - 1] == '*' ? strlen(select_name) : 0;
147 if (len-- == 1)
148 return true;
149
150 if (!strncmp(test_name, select_name, len))
Simon Glassbb2b1732021-03-07 17:35:05 -0700151 return true;
152
Andy Shevchenko00d42902021-02-11 16:40:11 +0200153 if (prefix) {
154 /* All tests have this prefix */
155 if (!strncmp(test_name, prefix, strlen(prefix)))
156 test_name += strlen(prefix);
157 } else {
Simon Glass1ef74ab2021-03-07 17:35:12 -0700158 const char *p = strstr(test_name, "_test_");
159
160 /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
161 if (p)
Andy Shevchenko00d42902021-02-11 16:40:11 +0200162 test_name = p + strlen("_test_");
Simon Glass1ef74ab2021-03-07 17:35:12 -0700163 }
Simon Glassbb2b1732021-03-07 17:35:05 -0700164
Andy Shevchenkod91cfbb2021-02-11 16:40:10 +0200165 if (!strncmp(test_name, select_name, len))
Simon Glassbb2b1732021-03-07 17:35:05 -0700166 return true;
167
168 return false;
169}
170
Simon Glass53d1b192021-03-07 17:35:08 -0700171/**
Simon Glass1899e132021-03-07 17:35:07 -0700172 * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
173 *
174 * @tests: List of tests to run
175 * @count: Number of tests to ru
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100176 * Return: true if any of the tests have the UT_TESTF_DM flag
Simon Glass1899e132021-03-07 17:35:07 -0700177 */
178static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
179{
180 struct unit_test *test;
181
182 for (test = tests; test < tests + count; test++) {
183 if (test->flags & UT_TESTF_DM)
184 return true;
185 }
186
187 return false;
188}
189
Simon Glassbb2b1732021-03-07 17:35:05 -0700190/**
Simon Glass53d1b192021-03-07 17:35:08 -0700191 * dm_test_restore() Put things back to normal so sandbox works as expected
192 *
193 * @of_root: Value to set for of_root
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100194 * Return: 0 if OK, -ve on error
Simon Glass53d1b192021-03-07 17:35:08 -0700195 */
196static int dm_test_restore(struct device_node *of_root)
197{
198 int ret;
199
200 gd_set_of_root(of_root);
201 gd->dm_root = NULL;
202 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
203 if (ret)
204 return ret;
205 dm_scan_plat(false);
206 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
207 dm_scan_fdt(false);
208
209 return 0;
210}
211
212/**
Simon Glass436687e2021-03-07 17:35:01 -0700213 * test_pre_run() - Handle any preparation needed to run a test
214 *
215 * @uts: Test state
216 * @test: Test to prepare for
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100217 * Return: 0 if OK, -EAGAIN to skip this test since some required feature is not
Simon Glass436687e2021-03-07 17:35:01 -0700218 * available, other -ve on error (meaning that testing cannot likely
219 * continue)
220 */
221static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd93dc7d2021-03-07 17:34:48 -0700222{
Simon Glassa128b1b2022-03-04 08:43:01 -0700223 ut_assertok(event_init());
Stefan Roese5be8f372022-09-02 13:57:54 +0200224 ut_assertok(cyclic_init());
Simon Glassa128b1b2022-03-04 08:43:01 -0700225
Simon Glassb2890a12021-03-07 17:34:56 -0700226 if (test->flags & UT_TESTF_DM)
Simon Glass2dba4762021-03-07 17:34:58 -0700227 ut_assertok(dm_test_pre_run(uts));
Simon Glassb2890a12021-03-07 17:34:56 -0700228
Simon Glass59cad962021-03-07 17:34:55 -0700229 ut_set_skip_delays(uts, false);
230
Simon Glass86a5bd02021-03-07 17:34:53 -0700231 uts->start = mallinfo();
Simon Glassd93dc7d2021-03-07 17:34:48 -0700232
Simon Glass9c80e542022-07-30 15:52:26 -0600233 if (test->flags & UT_TESTF_SCAN_PDATA) {
Simon Glass177e0fd2021-03-07 17:34:52 -0700234 ut_assertok(dm_scan_plat(false));
Simon Glass9c80e542022-07-30 15:52:26 -0600235 ut_assertok(dm_scan_other(false));
236 }
Simon Glass177e0fd2021-03-07 17:34:52 -0700237
Simon Glass242357c2021-03-07 17:34:51 -0700238 if (test->flags & UT_TESTF_PROBE_TEST)
239 ut_assertok(do_autoprobe(uts));
240
Simon Glassd18f7392021-03-07 17:34:50 -0700241 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
242 (test->flags & UT_TESTF_SCAN_FDT))
243 ut_assertok(dm_extended_scan(false));
244
Simon Glassd93dc7d2021-03-07 17:34:48 -0700245 if (test->flags & UT_TESTF_CONSOLE_REC) {
246 int ret = console_record_reset_enable();
247
248 if (ret) {
249 printf("Skipping: Console recording disabled\n");
250 return -EAGAIN;
251 }
252 }
Simon Glass2b566b92021-03-07 17:34:54 -0700253 ut_silence_console(uts);
Simon Glassd93dc7d2021-03-07 17:34:48 -0700254
255 return 0;
256}
257
Simon Glass436687e2021-03-07 17:35:01 -0700258/**
259 * test_post_run() - Handle cleaning up after a test
260 *
261 * @uts: Test state
262 * @test: Test to clean up after
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100263 * Return: 0 if OK, -ve on error (meaning that testing cannot likely continue)
Simon Glass436687e2021-03-07 17:35:01 -0700264 */
265static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd93dc7d2021-03-07 17:34:48 -0700266{
Simon Glass2b566b92021-03-07 17:34:54 -0700267 ut_unsilence_console(uts);
Simon Glass5b7e55b2021-03-07 17:34:59 -0700268 if (test->flags & UT_TESTF_DM)
269 ut_assertok(dm_test_post_run(uts));
Stefan Roese5be8f372022-09-02 13:57:54 +0200270 ut_assertok(cyclic_uninit());
Simon Glassa128b1b2022-03-04 08:43:01 -0700271 ut_assertok(event_uninit());
Simon Glass0f8f6772021-03-07 17:34:49 -0700272
Simon Glassd93dc7d2021-03-07 17:34:48 -0700273 return 0;
274}
275
Simon Glass0d32ec22021-03-07 17:35:03 -0700276/**
277 * ut_run_test() - Run a single test
278 *
279 * This runs the test, handling any preparation and clean-up needed. It prints
280 * the name of each test before running it.
281 *
282 * @uts: Test state to update. The caller should ensure that this is zeroed for
283 * the first call to this function. On exit, @uts->fail_count is
284 * incremented by the number of failures (0, one hopes)
285 * @test_name: Test to run
286 * @name: Name of test, possibly skipping a prefix that should not be displayed
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100287 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
Simon Glass0d32ec22021-03-07 17:35:03 -0700288 * any failed
289 */
290static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
291 const char *test_name)
Simon Glass5517edb2021-03-07 17:35:00 -0700292{
Simon Glass436687e2021-03-07 17:35:01 -0700293 const char *fname = strrchr(test->file, '/') + 1;
294 const char *note = "";
Simon Glass5517edb2021-03-07 17:35:00 -0700295 int ret;
296
Simon Glass436687e2021-03-07 17:35:01 -0700297 if ((test->flags & UT_TESTF_DM) && !uts->of_live)
298 note = " (flat tree)";
299 printf("Test: %s: %s%s\n", test_name, fname, note);
Simon Glass5517edb2021-03-07 17:35:00 -0700300
Simon Glass4066d8d2021-03-07 17:35:04 -0700301 /* Allow access to test state from drivers */
302 test_set_state(uts);
303
Simon Glass5517edb2021-03-07 17:35:00 -0700304 ret = test_pre_run(uts, test);
305 if (ret == -EAGAIN)
306 return -EAGAIN;
307 if (ret)
308 return ret;
309
310 test->func(uts);
311
312 ret = test_post_run(uts, test);
313 if (ret)
314 return ret;
315
Simon Glass4066d8d2021-03-07 17:35:04 -0700316 test_set_state( NULL);
317
Simon Glass5517edb2021-03-07 17:35:00 -0700318 return 0;
319}
320
Simon Glassbb2b1732021-03-07 17:35:05 -0700321/**
322 * ut_run_test_live_flat() - Run a test with both live and flat tree
323 *
324 * This calls ut_run_test() with livetree enabled, which is the standard setup
325 * for runnig tests. Then, for driver model test, it calls it again with
326 * livetree disabled. This allows checking of flattree being used when OF_LIVE
327 * is enabled, as is the case in U-Boot proper before relocation, as well as in
328 * SPL.
329 *
330 * @uts: Test state to update. The caller should ensure that this is zeroed for
331 * the first call to this function. On exit, @uts->fail_count is
332 * incremented by the number of failures (0, one hopes)
333 * @test: Test to run
334 * @name: Name of test, possibly skipping a prefix that should not be displayed
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100335 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
Simon Glassbb2b1732021-03-07 17:35:05 -0700336 * any failed
337 */
338static int ut_run_test_live_flat(struct unit_test_state *uts,
339 struct unit_test *test, const char *name)
Simon Glass0d32ec22021-03-07 17:35:03 -0700340{
341 int runs;
342
343 /* Run with the live tree if possible */
344 runs = 0;
345 if (CONFIG_IS_ENABLED(OF_LIVE)) {
Simon Glass2f0cb8f2022-07-30 15:52:12 -0600346 if (!(test->flags &
347 (UT_TESTF_FLAT_TREE | UT_TESTF_LIVE_OR_FLAT))) {
Simon Glass0d32ec22021-03-07 17:35:03 -0700348 uts->of_live = true;
349 ut_assertok(ut_run_test(uts, test, test->name));
350 runs++;
351 }
352 }
353
354 /*
355 * Run with the flat tree if we couldn't run it with live tree,
356 * or it is a core test.
357 */
358 if (!(test->flags & UT_TESTF_LIVE_TREE) &&
359 (!runs || ut_test_run_on_flattree(test))) {
360 uts->of_live = false;
361 ut_assertok(ut_run_test(uts, test, test->name));
362 runs++;
363 }
364
365 return 0;
366}
367
Simon Glassbb2b1732021-03-07 17:35:05 -0700368/**
369 * ut_run_tests() - Run a set of tests
370 *
371 * This runs the tests, handling any preparation and clean-up needed. It prints
372 * the name of each test before running it.
373 *
374 * @uts: Test state to update. The caller should ensure that this is zeroed for
375 * the first call to this function. On exit, @uts->fail_count is
376 * incremented by the number of failures (0, one hopes)
377 * @prefix: String prefix for the tests. Any tests that have this prefix will be
378 * printed without the prefix, so that it is easier to see the unique part
379 * of the test name. If NULL, no prefix processing is done
380 * @tests: List of tests to run
381 * @count: Number of tests to run
382 * @select_name: Name of a single test to run (from the list provided). If NULL
383 * then all tests are run
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100384 * Return: 0 if all tests passed, -ENOENT if test @select_name was not found,
Simon Glassbb2b1732021-03-07 17:35:05 -0700385 * -EBADF if any failed
386 */
387static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
388 struct unit_test *tests, int count,
389 const char *select_name)
Simon Glass5722fb22021-03-07 17:34:47 -0700390{
391 struct unit_test *test;
Simon Glass5722fb22021-03-07 17:34:47 -0700392 int found = 0;
393
394 for (test = tests; test < tests + count; test++) {
395 const char *test_name = test->name;
Simon Glass91a187b2022-08-01 07:58:45 -0600396 int ret, i, old_fail_count;
Simon Glass5722fb22021-03-07 17:34:47 -0700397
Simon Glassbb2b1732021-03-07 17:35:05 -0700398 if (!test_matches(prefix, test_name, select_name))
Simon Glass5722fb22021-03-07 17:34:47 -0700399 continue;
Simon Glass91a187b2022-08-01 07:58:45 -0600400 old_fail_count = uts->fail_count;
401 for (i = 0; i < uts->runs_per_test; i++)
402 ret = ut_run_test_live_flat(uts, test, select_name);
403 if (uts->fail_count != old_fail_count) {
404 printf("Test %s failed %d times\n", select_name,
405 uts->fail_count - old_fail_count);
406 }
Simon Glass5722fb22021-03-07 17:34:47 -0700407 found++;
Simon Glassd93dc7d2021-03-07 17:34:48 -0700408 if (ret == -EAGAIN)
409 continue;
410 if (ret)
411 return ret;
Simon Glass5722fb22021-03-07 17:34:47 -0700412 }
413 if (select_name && !found)
414 return -ENOENT;
415
416 return uts->fail_count ? -EBADF : 0;
417}
418
419int ut_run_list(const char *category, const char *prefix,
Simon Glass91a187b2022-08-01 07:58:45 -0600420 struct unit_test *tests, int count, const char *select_name,
421 int runs_per_test)
Simon Glass5722fb22021-03-07 17:34:47 -0700422{
423 struct unit_test_state uts = { .fail_count = 0 };
Simon Glass53d1b192021-03-07 17:35:08 -0700424 bool has_dm_tests = false;
Simon Glass5722fb22021-03-07 17:34:47 -0700425 int ret;
426
Simon Glass1899e132021-03-07 17:35:07 -0700427 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
428 ut_list_has_dm_tests(tests, count)) {
Simon Glass53d1b192021-03-07 17:35:08 -0700429 has_dm_tests = true;
Simon Glass1899e132021-03-07 17:35:07 -0700430 /*
431 * If we have no device tree, or it only has a root node, then
432 * these * tests clearly aren't going to work...
433 */
434 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
435 puts("Please run with test device tree:\n"
436 " ./u-boot -d arch/sandbox/dts/test.dtb\n");
437 return CMD_RET_FAILURE;
438 }
439 }
440
Simon Glass5722fb22021-03-07 17:34:47 -0700441 if (!select_name)
442 printf("Running %d %s tests\n", count, category);
443
Simon Glassbb2b1732021-03-07 17:35:05 -0700444 uts.of_root = gd_of_root();
Simon Glass91a187b2022-08-01 07:58:45 -0600445 uts.runs_per_test = runs_per_test;
Simon Glass5722fb22021-03-07 17:34:47 -0700446 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
447
448 if (ret == -ENOENT)
449 printf("Test '%s' not found\n", select_name);
450 else
451 printf("Failures: %d\n", uts.fail_count);
452
Simon Glass53d1b192021-03-07 17:35:08 -0700453 /* Best efforts only...ignore errors */
454 if (has_dm_tests)
455 dm_test_restore(uts.of_root);
456
Simon Glass5722fb22021-03-07 17:34:47 -0700457 return ret;
458}