blob: 9ab090b7b33a9abafe1bf7915cfe5ae87c6479d3 [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>
Simon Glass76c62692022-10-29 19:47:08 -06008#include <blk.h>
Simon Glass5722fb22021-03-07 17:34:47 -07009#include <console.h>
Stefan Roese5be8f372022-09-02 13:57:54 +020010#include <cyclic.h>
Simon Glassd18f7392021-03-07 17:34:50 -070011#include <dm.h>
Simon Glassa128b1b2022-03-04 08:43:01 -070012#include <event.h>
Simon Glassb9958072022-09-06 20:27:11 -060013#include <of_live.h>
Simon Glass3ba76752022-09-06 20:27:05 -060014#include <os.h>
Simon Glassba8457b2022-09-06 20:27:19 -060015#include <dm/ofnode.h>
Simon Glassd18f7392021-03-07 17:34:50 -070016#include <dm/root.h>
Simon Glass2dba4762021-03-07 17:34:58 -070017#include <dm/test.h>
Simon Glass5b7e55b2021-03-07 17:34:59 -070018#include <dm/uclass-internal.h>
Simon Glass5722fb22021-03-07 17:34:47 -070019#include <test/test.h>
Simon Glassd18f7392021-03-07 17:34:50 -070020#include <test/ut.h>
Simon Glass3ba76752022-09-06 20:27:05 -060021#include <u-boot/crc.h>
Simon Glass5722fb22021-03-07 17:34:47 -070022
Simon Glass0f8f6772021-03-07 17:34:49 -070023DECLARE_GLOBAL_DATA_PTR;
24
Simon Glass3ba76752022-09-06 20:27:05 -060025/**
26 * enum fdtchk_t - what to do with the device tree (gd->fdt_blob)
27 *
28 * This affects what happens with the device tree before and after a test
29 *
30 * @FDTCHK_NONE: Do nothing
31 * @FDTCHK_CHECKSUM: Take a checksum of the FDT before the test runs and
32 * compare it afterwards to detect any changes
33 * @FDTCHK_COPY: Make a copy of the FDT and restore it afterwards
34 */
35enum fdtchk_t {
36 FDTCHK_NONE,
37 FDTCHK_CHECKSUM,
38 FDTCHK_COPY,
39};
40
41/**
42 * fdt_action() - get the required action for the FDT
43 *
44 * @return the action that should be taken for this build
45 */
46static enum fdtchk_t fdt_action(void)
47{
48 /* Do a copy for sandbox (but only the U-Boot build, not SPL) */
49 if (CONFIG_IS_ENABLED(SANDBOX))
50 return FDTCHK_COPY;
51
52 /* For sandbox SPL builds, do nothing */
53 if (IS_ENABLED(CONFIG_SANDBOX))
54 return FDTCHK_NONE;
55
56 /* For all other boards, do a checksum */
57 return FDTCHK_CHECKSUM;
58}
59
Simon Glass4066d8d2021-03-07 17:35:04 -070060/* This is valid when a test is running, NULL otherwise */
61static struct unit_test_state *cur_test_state;
62
63struct unit_test_state *test_get_state(void)
64{
65 return cur_test_state;
66}
67
68void test_set_state(struct unit_test_state *uts)
69{
70 cur_test_state = uts;
71}
72
Simon Glass2dba4762021-03-07 17:34:58 -070073/**
74 * dm_test_pre_run() - Get ready to run a driver model test
75 *
76 * This clears out the driver model data structures. For sandbox it resets the
77 * state structure
78 *
79 * @uts: Test state
80 */
81static int dm_test_pre_run(struct unit_test_state *uts)
82{
83 bool of_live = uts->of_live;
84
Simon Glass60f08992022-09-06 20:27:06 -060085 if (of_live && (gd->flags & GD_FLG_FDT_CHANGED)) {
86 printf("Cannot run live tree test as device tree changed\n");
87 return -EFAULT;
88 }
Simon Glass2dba4762021-03-07 17:34:58 -070089 uts->root = NULL;
90 uts->testdev = NULL;
91 uts->force_fail_alloc = false;
92 uts->skip_post_probe = false;
Simon Glass3ba76752022-09-06 20:27:05 -060093 if (fdt_action() == FDTCHK_CHECKSUM)
94 uts->fdt_chksum = crc8(0, gd->fdt_blob,
95 fdt_totalsize(gd->fdt_blob));
Simon Glass2dba4762021-03-07 17:34:58 -070096 gd->dm_root = NULL;
Simon Glass1a3e39b2022-09-06 20:27:00 -060097 malloc_disable_testing();
Simon Glassf7005042021-07-05 16:32:43 -060098 if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA))
Simon Glass2dba4762021-03-07 17:34:58 -070099 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
Simon Glass45b807d2021-03-25 10:44:33 +1300100 arch_reset_for_test();
Simon Glass2dba4762021-03-07 17:34:58 -0700101
102 /* Determine whether to make the live tree available */
103 gd_set_of_root(of_live ? uts->of_root : NULL);
Simon Glassba8457b2022-09-06 20:27:19 -0600104 oftree_reset();
Simon Glass2dba4762021-03-07 17:34:58 -0700105 ut_assertok(dm_init(of_live));
106 uts->root = dm_root();
107
108 return 0;
109}
110
Simon Glass5b7e55b2021-03-07 17:34:59 -0700111static int dm_test_post_run(struct unit_test_state *uts)
112{
113 int id;
114
Simon Glass3ba76752022-09-06 20:27:05 -0600115 if (gd->fdt_blob) {
116 switch (fdt_action()) {
117 case FDTCHK_COPY:
118 memcpy((void *)gd->fdt_blob, uts->fdt_copy, uts->fdt_size);
119 break;
120 case FDTCHK_CHECKSUM: {
121 uint chksum;
122
123 chksum = crc8(0, gd->fdt_blob, fdt_totalsize(gd->fdt_blob));
Simon Glass60f08992022-09-06 20:27:06 -0600124 if (chksum != uts->fdt_chksum) {
125 /*
126 * We cannot run any more tests that need the
127 * live tree, since its strings point into the
128 * flat tree, which has changed. This likely
129 * means that at least some of the pointers from
130 * the live tree point to different things
131 */
Simon Glass3ba76752022-09-06 20:27:05 -0600132 printf("Device tree changed: cannot run live tree tests\n");
Simon Glass60f08992022-09-06 20:27:06 -0600133 gd->flags |= GD_FLG_FDT_CHANGED;
134 }
Simon Glass3ba76752022-09-06 20:27:05 -0600135 break;
136 }
137 case FDTCHK_NONE:
138 break;
139 }
140 }
141
Simon Glass96113c12021-03-15 17:25:21 +1300142 /*
143 * With of-platdata-inst the uclasses are created at build time. If we
144 * destroy them we cannot get them back since uclass_add() is not
145 * supported. So skip this.
146 */
147 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
148 for (id = 0; id < UCLASS_COUNT; id++) {
149 struct uclass *uc;
Simon Glass5b7e55b2021-03-07 17:34:59 -0700150
Simon Glass96113c12021-03-15 17:25:21 +1300151 /*
152 * If the uclass doesn't exist we don't want to create
153 * it. So check that here before we call
154 * uclass_find_device().
155 */
156 uc = uclass_find(id);
157 if (!uc)
158 continue;
159 ut_assertok(uclass_destroy(uc));
160 }
Simon Glass5b7e55b2021-03-07 17:34:59 -0700161 }
162
163 return 0;
164}
165
Simon Glass242357c2021-03-07 17:34:51 -0700166/* Ensure all the test devices are probed */
167static int do_autoprobe(struct unit_test_state *uts)
168{
Michal Suchanek44322b52022-10-12 21:57:51 +0200169 return uclass_probe_all(UCLASS_TEST);
Simon Glass242357c2021-03-07 17:34:51 -0700170}
171
Simon Glass0d32ec22021-03-07 17:35:03 -0700172/*
173 * ut_test_run_on_flattree() - Check if we should run a test with flat DT
174 *
175 * This skips long/slow tests where there is not much value in running a flat
176 * DT test in addition to a live DT test.
177 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100178 * Return: true to run the given test on the flat device tree
Simon Glass0d32ec22021-03-07 17:35:03 -0700179 */
180static bool ut_test_run_on_flattree(struct unit_test *test)
181{
182 const char *fname = strrchr(test->file, '/') + 1;
183
184 if (!(test->flags & UT_TESTF_DM))
185 return false;
186
187 return !strstr(fname, "video") || strstr(test->name, "video_base");
188}
189
Simon Glass436687e2021-03-07 17:35:01 -0700190/**
Simon Glassbb2b1732021-03-07 17:35:05 -0700191 * test_matches() - Check if a test should be run
192 *
193 * This checks if the a test should be run. In the normal case of running all
194 * tests, @select_name is NULL.
195 *
196 * @prefix: String prefix for the tests. Any tests that have this prefix will be
197 * printed without the prefix, so that it is easier to see the unique part
Simon Glass1ef74ab2021-03-07 17:35:12 -0700198 * of the test name. If NULL, any suite name (xxx_test) is considered to be
199 * a prefix.
Simon Glassbb2b1732021-03-07 17:35:05 -0700200 * @test_name: Name of current test
201 * @select_name: Name of test to run (or NULL for all)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100202 * Return: true to run this test, false to skip it
Simon Glassbb2b1732021-03-07 17:35:05 -0700203 */
204static bool test_matches(const char *prefix, const char *test_name,
205 const char *select_name)
206{
Andy Shevchenkod91cfbb2021-02-11 16:40:10 +0200207 size_t len;
208
Simon Glassbb2b1732021-03-07 17:35:05 -0700209 if (!select_name)
210 return true;
211
Andy Shevchenkod91cfbb2021-02-11 16:40:10 +0200212 /* Allow glob expansion in the test name */
213 len = select_name[strlen(select_name) - 1] == '*' ? strlen(select_name) : 0;
214 if (len-- == 1)
215 return true;
216
217 if (!strncmp(test_name, select_name, len))
Simon Glassbb2b1732021-03-07 17:35:05 -0700218 return true;
219
Andy Shevchenko00d42902021-02-11 16:40:11 +0200220 if (prefix) {
221 /* All tests have this prefix */
222 if (!strncmp(test_name, prefix, strlen(prefix)))
223 test_name += strlen(prefix);
224 } else {
Simon Glass1ef74ab2021-03-07 17:35:12 -0700225 const char *p = strstr(test_name, "_test_");
226
227 /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
228 if (p)
Andy Shevchenko00d42902021-02-11 16:40:11 +0200229 test_name = p + strlen("_test_");
Simon Glass1ef74ab2021-03-07 17:35:12 -0700230 }
Simon Glassbb2b1732021-03-07 17:35:05 -0700231
Andy Shevchenkod91cfbb2021-02-11 16:40:10 +0200232 if (!strncmp(test_name, select_name, len))
Simon Glassbb2b1732021-03-07 17:35:05 -0700233 return true;
234
235 return false;
236}
237
Simon Glass53d1b192021-03-07 17:35:08 -0700238/**
Simon Glass1899e132021-03-07 17:35:07 -0700239 * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
240 *
241 * @tests: List of tests to run
242 * @count: Number of tests to ru
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100243 * Return: true if any of the tests have the UT_TESTF_DM flag
Simon Glass1899e132021-03-07 17:35:07 -0700244 */
245static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
246{
247 struct unit_test *test;
248
249 for (test = tests; test < tests + count; test++) {
250 if (test->flags & UT_TESTF_DM)
251 return true;
252 }
253
254 return false;
255}
256
Simon Glassbb2b1732021-03-07 17:35:05 -0700257/**
Simon Glass53d1b192021-03-07 17:35:08 -0700258 * dm_test_restore() Put things back to normal so sandbox works as expected
259 *
260 * @of_root: Value to set for of_root
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100261 * Return: 0 if OK, -ve on error
Simon Glass53d1b192021-03-07 17:35:08 -0700262 */
263static int dm_test_restore(struct device_node *of_root)
264{
265 int ret;
266
267 gd_set_of_root(of_root);
268 gd->dm_root = NULL;
269 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
270 if (ret)
271 return ret;
272 dm_scan_plat(false);
273 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
274 dm_scan_fdt(false);
275
276 return 0;
277}
278
279/**
Simon Glass436687e2021-03-07 17:35:01 -0700280 * test_pre_run() - Handle any preparation needed to run a test
281 *
282 * @uts: Test state
283 * @test: Test to prepare for
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100284 * Return: 0 if OK, -EAGAIN to skip this test since some required feature is not
Simon Glass436687e2021-03-07 17:35:01 -0700285 * available, other -ve on error (meaning that testing cannot likely
286 * continue)
287 */
288static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd93dc7d2021-03-07 17:34:48 -0700289{
Simon Glassa128b1b2022-03-04 08:43:01 -0700290 ut_assertok(event_init());
291
Simon Glassb2890a12021-03-07 17:34:56 -0700292 if (test->flags & UT_TESTF_DM)
Simon Glass2dba4762021-03-07 17:34:58 -0700293 ut_assertok(dm_test_pre_run(uts));
Simon Glassb2890a12021-03-07 17:34:56 -0700294
Simon Glass59cad962021-03-07 17:34:55 -0700295 ut_set_skip_delays(uts, false);
296
Simon Glass86a5bd02021-03-07 17:34:53 -0700297 uts->start = mallinfo();
Simon Glassd93dc7d2021-03-07 17:34:48 -0700298
Simon Glass9c80e542022-07-30 15:52:26 -0600299 if (test->flags & UT_TESTF_SCAN_PDATA) {
Simon Glass177e0fd2021-03-07 17:34:52 -0700300 ut_assertok(dm_scan_plat(false));
Simon Glass9c80e542022-07-30 15:52:26 -0600301 ut_assertok(dm_scan_other(false));
302 }
Simon Glass177e0fd2021-03-07 17:34:52 -0700303
Simon Glass242357c2021-03-07 17:34:51 -0700304 if (test->flags & UT_TESTF_PROBE_TEST)
305 ut_assertok(do_autoprobe(uts));
306
Simon Glassd18f7392021-03-07 17:34:50 -0700307 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
308 (test->flags & UT_TESTF_SCAN_FDT))
309 ut_assertok(dm_extended_scan(false));
310
Simon Glassb9958072022-09-06 20:27:11 -0600311 if (IS_ENABLED(CONFIG_SANDBOX) && (test->flags & UT_TESTF_OTHER_FDT)) {
312 /* make sure the other FDT is available */
313 ut_assertok(test_load_other_fdt(uts));
314
315 /*
316 * create a new live tree with it for every test, in case a
317 * test modifies the tree
318 */
319 if (of_live_active()) {
320 ut_assertok(unflatten_device_tree(uts->other_fdt,
321 &uts->of_other));
322 }
323 }
324
Simon Glassd93dc7d2021-03-07 17:34:48 -0700325 if (test->flags & UT_TESTF_CONSOLE_REC) {
326 int ret = console_record_reset_enable();
327
328 if (ret) {
329 printf("Skipping: Console recording disabled\n");
330 return -EAGAIN;
331 }
332 }
Simon Glass2b566b92021-03-07 17:34:54 -0700333 ut_silence_console(uts);
Simon Glassd93dc7d2021-03-07 17:34:48 -0700334
335 return 0;
336}
337
Simon Glass436687e2021-03-07 17:35:01 -0700338/**
339 * test_post_run() - Handle cleaning up after a test
340 *
341 * @uts: Test state
342 * @test: Test to clean up after
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100343 * Return: 0 if OK, -ve on error (meaning that testing cannot likely continue)
Simon Glass436687e2021-03-07 17:35:01 -0700344 */
345static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd93dc7d2021-03-07 17:34:48 -0700346{
Simon Glass2b566b92021-03-07 17:34:54 -0700347 ut_unsilence_console(uts);
Simon Glass5b7e55b2021-03-07 17:34:59 -0700348 if (test->flags & UT_TESTF_DM)
349 ut_assertok(dm_test_post_run(uts));
Rasmus Villemoesc794e492022-10-28 13:50:54 +0200350 ut_assertok(cyclic_unregister_all());
Simon Glassa128b1b2022-03-04 08:43:01 -0700351 ut_assertok(event_uninit());
Simon Glass0f8f6772021-03-07 17:34:49 -0700352
Simon Glassb9958072022-09-06 20:27:11 -0600353 free(uts->of_other);
354 uts->of_other = NULL;
355
Simon Glass76c62692022-10-29 19:47:08 -0600356 blkcache_free();
357
Simon Glassd93dc7d2021-03-07 17:34:48 -0700358 return 0;
359}
360
Simon Glass0d32ec22021-03-07 17:35:03 -0700361/**
Simon Glass816fe6c2022-10-20 18:22:48 -0600362 * skip_test() - Handle skipping a test
363 *
364 * @uts: Test state to update
365 * @return -EAGAIN (always)
366 */
367static int skip_test(struct unit_test_state *uts)
368{
369 uts->skip_count++;
370
371 return -EAGAIN;
372}
373
374/**
Simon Glass0d32ec22021-03-07 17:35:03 -0700375 * ut_run_test() - Run a single test
376 *
377 * This runs the test, handling any preparation and clean-up needed. It prints
378 * the name of each test before running it.
379 *
380 * @uts: Test state to update. The caller should ensure that this is zeroed for
381 * the first call to this function. On exit, @uts->fail_count is
382 * incremented by the number of failures (0, one hopes)
383 * @test_name: Test to run
384 * @name: Name of test, possibly skipping a prefix that should not be displayed
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100385 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
Simon Glass0d32ec22021-03-07 17:35:03 -0700386 * any failed
387 */
388static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
389 const char *test_name)
Simon Glass5517edb2021-03-07 17:35:00 -0700390{
Simon Glass436687e2021-03-07 17:35:01 -0700391 const char *fname = strrchr(test->file, '/') + 1;
392 const char *note = "";
Simon Glass5517edb2021-03-07 17:35:00 -0700393 int ret;
394
Simon Glass436687e2021-03-07 17:35:01 -0700395 if ((test->flags & UT_TESTF_DM) && !uts->of_live)
396 note = " (flat tree)";
397 printf("Test: %s: %s%s\n", test_name, fname, note);
Simon Glass5517edb2021-03-07 17:35:00 -0700398
Simon Glass4066d8d2021-03-07 17:35:04 -0700399 /* Allow access to test state from drivers */
400 test_set_state(uts);
401
Simon Glass5517edb2021-03-07 17:35:00 -0700402 ret = test_pre_run(uts, test);
403 if (ret == -EAGAIN)
Simon Glass816fe6c2022-10-20 18:22:48 -0600404 return skip_test(uts);
Simon Glass5517edb2021-03-07 17:35:00 -0700405 if (ret)
406 return ret;
407
Simon Glass816fe6c2022-10-20 18:22:48 -0600408 ret = test->func(uts);
409 if (ret == -EAGAIN)
410 skip_test(uts);
Simon Glass5517edb2021-03-07 17:35:00 -0700411
412 ret = test_post_run(uts, test);
413 if (ret)
414 return ret;
415
Simon Glass4066d8d2021-03-07 17:35:04 -0700416 test_set_state( NULL);
417
Simon Glass5517edb2021-03-07 17:35:00 -0700418 return 0;
419}
420
Simon Glassbb2b1732021-03-07 17:35:05 -0700421/**
422 * ut_run_test_live_flat() - Run a test with both live and flat tree
423 *
424 * This calls ut_run_test() with livetree enabled, which is the standard setup
425 * for runnig tests. Then, for driver model test, it calls it again with
426 * livetree disabled. This allows checking of flattree being used when OF_LIVE
427 * is enabled, as is the case in U-Boot proper before relocation, as well as in
428 * SPL.
429 *
430 * @uts: Test state to update. The caller should ensure that this is zeroed for
431 * the first call to this function. On exit, @uts->fail_count is
432 * incremented by the number of failures (0, one hopes)
433 * @test: Test to run
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100434 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
Simon Glassbb2b1732021-03-07 17:35:05 -0700435 * any failed
436 */
437static int ut_run_test_live_flat(struct unit_test_state *uts,
Simon Glass0f7eb632022-10-29 19:47:09 -0600438 struct unit_test *test)
Simon Glass0d32ec22021-03-07 17:35:03 -0700439{
440 int runs;
441
Simon Glassb9958072022-09-06 20:27:11 -0600442 if ((test->flags & UT_TESTF_OTHER_FDT) && !IS_ENABLED(CONFIG_SANDBOX))
Simon Glass816fe6c2022-10-20 18:22:48 -0600443 return skip_test(uts);
Simon Glassb9958072022-09-06 20:27:11 -0600444
Simon Glass0d32ec22021-03-07 17:35:03 -0700445 /* Run with the live tree if possible */
446 runs = 0;
447 if (CONFIG_IS_ENABLED(OF_LIVE)) {
Simon Glass7f9b5802022-09-06 20:26:59 -0600448 if (!(test->flags & UT_TESTF_FLAT_TREE)) {
Simon Glass0d32ec22021-03-07 17:35:03 -0700449 uts->of_live = true;
450 ut_assertok(ut_run_test(uts, test, test->name));
451 runs++;
452 }
453 }
454
455 /*
Simon Glassb9958072022-09-06 20:27:11 -0600456 * Run with the flat tree if:
457 * - it is not marked for live tree only
458 * - it doesn't require the 'other' FDT when OFNODE_MULTI_TREE_MAX is
459 * not enabled (since flat tree can only support a single FDT in that
460 * case
461 * - we couldn't run it with live tree,
462 * - it is a core test (dm tests except video)
463 * - the FDT is still valid and has not been updated by an earlier test
464 * (for sandbox we handle this by copying the tree, but not for other
465 * boards)
Simon Glass0d32ec22021-03-07 17:35:03 -0700466 */
467 if (!(test->flags & UT_TESTF_LIVE_TREE) &&
Simon Glassb9958072022-09-06 20:27:11 -0600468 (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) ||
469 !(test->flags & UT_TESTF_OTHER_FDT)) &&
Simon Glass60f08992022-09-06 20:27:06 -0600470 (!runs || ut_test_run_on_flattree(test)) &&
471 !(gd->flags & GD_FLG_FDT_CHANGED)) {
Simon Glass0d32ec22021-03-07 17:35:03 -0700472 uts->of_live = false;
473 ut_assertok(ut_run_test(uts, test, test->name));
474 runs++;
475 }
476
477 return 0;
478}
479
Simon Glassbb2b1732021-03-07 17:35:05 -0700480/**
481 * ut_run_tests() - Run a set of tests
482 *
483 * This runs the tests, handling any preparation and clean-up needed. It prints
484 * the name of each test before running it.
485 *
486 * @uts: Test state to update. The caller should ensure that this is zeroed for
487 * the first call to this function. On exit, @uts->fail_count is
488 * incremented by the number of failures (0, one hopes)
489 * @prefix: String prefix for the tests. Any tests that have this prefix will be
490 * printed without the prefix, so that it is easier to see the unique part
491 * of the test name. If NULL, no prefix processing is done
492 * @tests: List of tests to run
493 * @count: Number of tests to run
494 * @select_name: Name of a single test to run (from the list provided). If NULL
495 * then all tests are run
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100496 * Return: 0 if all tests passed, -ENOENT if test @select_name was not found,
Simon Glassbb2b1732021-03-07 17:35:05 -0700497 * -EBADF if any failed
498 */
499static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
500 struct unit_test *tests, int count,
Simon Glass85ba7c32022-10-29 19:47:13 -0600501 const char *select_name, const char *test_insert)
Simon Glass5722fb22021-03-07 17:34:47 -0700502{
Simon Glass85ba7c32022-10-29 19:47:13 -0600503 struct unit_test *test, *one;
Simon Glass5722fb22021-03-07 17:34:47 -0700504 int found = 0;
Simon Glass85ba7c32022-10-29 19:47:13 -0600505 int pos = 0;
506 int upto;
Simon Glass5722fb22021-03-07 17:34:47 -0700507
Simon Glass85ba7c32022-10-29 19:47:13 -0600508 one = NULL;
509 if (test_insert) {
510 char *p;
511
512 pos = dectoul(test_insert, NULL);
513 p = strchr(test_insert, ':');
514 if (p)
515 p++;
516
517 for (test = tests; test < tests + count; test++) {
518 if (!strcmp(p, test->name))
519 one = test;
520 }
521 }
522
523 for (upto = 0, test = tests; test < tests + count; test++, upto++) {
Simon Glass5722fb22021-03-07 17:34:47 -0700524 const char *test_name = test->name;
Simon Glass91a187b2022-08-01 07:58:45 -0600525 int ret, i, old_fail_count;
Simon Glass5722fb22021-03-07 17:34:47 -0700526
Simon Glassbb2b1732021-03-07 17:35:05 -0700527 if (!test_matches(prefix, test_name, select_name))
Simon Glass5722fb22021-03-07 17:34:47 -0700528 continue;
Simon Glass1f1614b2022-10-20 18:22:50 -0600529
530 if (test->flags & UT_TESTF_MANUAL) {
531 int len;
532
533 /*
534 * manual tests must have a name ending "_norun" as this
535 * is how pytest knows to skip them. See
536 * generate_ut_subtest() for this check.
537 */
538 len = strlen(test_name);
539 if (len < 6 || strcmp(test_name + len - 6, "_norun")) {
540 printf("Test %s is manual so must have a name ending in _norun\n",
541 test_name);
542 uts->fail_count++;
543 return -EBADF;
544 }
545 if (!uts->force_run) {
546 if (select_name) {
547 printf("Test %s skipped as it is manual (use -f to run it)\n",
548 test_name);
549 }
550 continue;
551 }
552 }
Simon Glass91a187b2022-08-01 07:58:45 -0600553 old_fail_count = uts->fail_count;
Simon Glass85ba7c32022-10-29 19:47:13 -0600554
555 if (one && upto == pos) {
556 ret = ut_run_test_live_flat(uts, one);
557 if (uts->fail_count != old_fail_count) {
558 printf("Test %s failed %d times (position %d)\n",
559 one->name,
560 uts->fail_count - old_fail_count, pos);
561 }
562 return -EBADF;
563 }
564
Simon Glass91a187b2022-08-01 07:58:45 -0600565 for (i = 0; i < uts->runs_per_test; i++)
Simon Glass0f7eb632022-10-29 19:47:09 -0600566 ret = ut_run_test_live_flat(uts, test);
Simon Glass91a187b2022-08-01 07:58:45 -0600567 if (uts->fail_count != old_fail_count) {
568 printf("Test %s failed %d times\n", select_name,
569 uts->fail_count - old_fail_count);
570 }
Simon Glass5722fb22021-03-07 17:34:47 -0700571 found++;
Simon Glassd93dc7d2021-03-07 17:34:48 -0700572 if (ret == -EAGAIN)
573 continue;
574 if (ret)
575 return ret;
Simon Glass5722fb22021-03-07 17:34:47 -0700576 }
577 if (select_name && !found)
578 return -ENOENT;
579
580 return uts->fail_count ? -EBADF : 0;
581}
582
583int ut_run_list(const char *category, const char *prefix,
Simon Glass91a187b2022-08-01 07:58:45 -0600584 struct unit_test *tests, int count, const char *select_name,
Simon Glass85ba7c32022-10-29 19:47:13 -0600585 int runs_per_test, bool force_run, const char *test_insert)
Simon Glass5722fb22021-03-07 17:34:47 -0700586{
587 struct unit_test_state uts = { .fail_count = 0 };
Simon Glass53d1b192021-03-07 17:35:08 -0700588 bool has_dm_tests = false;
Simon Glass5722fb22021-03-07 17:34:47 -0700589 int ret;
590
Simon Glass1899e132021-03-07 17:35:07 -0700591 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
592 ut_list_has_dm_tests(tests, count)) {
Simon Glass53d1b192021-03-07 17:35:08 -0700593 has_dm_tests = true;
Simon Glass1899e132021-03-07 17:35:07 -0700594 /*
595 * If we have no device tree, or it only has a root node, then
596 * these * tests clearly aren't going to work...
597 */
598 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
599 puts("Please run with test device tree:\n"
600 " ./u-boot -d arch/sandbox/dts/test.dtb\n");
601 return CMD_RET_FAILURE;
602 }
603 }
604
Simon Glass5722fb22021-03-07 17:34:47 -0700605 if (!select_name)
606 printf("Running %d %s tests\n", count, category);
607
Simon Glassbb2b1732021-03-07 17:35:05 -0700608 uts.of_root = gd_of_root();
Simon Glass91a187b2022-08-01 07:58:45 -0600609 uts.runs_per_test = runs_per_test;
Simon Glass3ba76752022-09-06 20:27:05 -0600610 if (fdt_action() == FDTCHK_COPY && gd->fdt_blob) {
611 uts.fdt_size = fdt_totalsize(gd->fdt_blob);
612 uts.fdt_copy = os_malloc(uts.fdt_size);
613 if (!uts.fdt_copy) {
614 printf("Out of memory for device tree copy\n");
615 return -ENOMEM;
616 }
617 memcpy(uts.fdt_copy, gd->fdt_blob, uts.fdt_size);
618 }
Simon Glass1f1614b2022-10-20 18:22:50 -0600619 uts.force_run = force_run;
Simon Glass85ba7c32022-10-29 19:47:13 -0600620 ret = ut_run_tests(&uts, prefix, tests, count, select_name,
621 test_insert);
Simon Glass5722fb22021-03-07 17:34:47 -0700622
Simon Glass3ba76752022-09-06 20:27:05 -0600623 /* Best efforts only...ignore errors */
624 if (has_dm_tests)
625 dm_test_restore(uts.of_root);
Simon Glassb9958072022-09-06 20:27:11 -0600626 if (IS_ENABLED(CONFIG_SANDBOX)) {
Simon Glass3ba76752022-09-06 20:27:05 -0600627 os_free(uts.fdt_copy);
Simon Glassb9958072022-09-06 20:27:11 -0600628 os_free(uts.other_fdt);
629 }
Simon Glass3ba76752022-09-06 20:27:05 -0600630
Simon Glass816fe6c2022-10-20 18:22:48 -0600631 if (uts.skip_count)
632 printf("Skipped: %d, ", uts.skip_count);
Simon Glass5722fb22021-03-07 17:34:47 -0700633 if (ret == -ENOENT)
634 printf("Test '%s' not found\n", select_name);
635 else
636 printf("Failures: %d\n", uts.fail_count);
637
Simon Glass5722fb22021-03-07 17:34:47 -0700638 return ret;
639}