blob: cabc736a524eefacba44cf6fb31bd481f50938c6 [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
Simon Glassbe277ee2024-10-28 13:47:53 +01007#define LOG_CATEGORY LOGC_TEST
8
Simon Glass76c62692022-10-29 19:47:08 -06009#include <blk.h>
Simon Glass5722fb22021-03-07 17:34:47 -070010#include <console.h>
Stefan Roese5be8f372022-09-02 13:57:54 +020011#include <cyclic.h>
Simon Glassd18f7392021-03-07 17:34:50 -070012#include <dm.h>
Simon Glassa128b1b2022-03-04 08:43:01 -070013#include <event.h>
Simon Glassb490f5f2023-01-17 10:47:28 -070014#include <net.h>
Simon Glassb9958072022-09-06 20:27:11 -060015#include <of_live.h>
Simon Glass3ba76752022-09-06 20:27:05 -060016#include <os.h>
Simon Glass5fa92d62025-02-07 11:30:35 -070017#include <spl.h>
Simon Glass1880b8a2024-09-01 16:26:20 -060018#include <usb.h>
Simon Glassba8457b2022-09-06 20:27:19 -060019#include <dm/ofnode.h>
Simon Glassd18f7392021-03-07 17:34:50 -070020#include <dm/root.h>
Simon Glass2dba4762021-03-07 17:34:58 -070021#include <dm/test.h>
Simon Glass5b7e55b2021-03-07 17:34:59 -070022#include <dm/uclass-internal.h>
Simon Glass5722fb22021-03-07 17:34:47 -070023#include <test/test.h>
Simon Glassd18f7392021-03-07 17:34:50 -070024#include <test/ut.h>
Simon Glass3ba76752022-09-06 20:27:05 -060025#include <u-boot/crc.h>
Simon Glass5722fb22021-03-07 17:34:47 -070026
Simon Glass0f8f6772021-03-07 17:34:49 -070027DECLARE_GLOBAL_DATA_PTR;
28
Simon Glass3ba76752022-09-06 20:27:05 -060029/**
30 * enum fdtchk_t - what to do with the device tree (gd->fdt_blob)
31 *
32 * This affects what happens with the device tree before and after a test
33 *
34 * @FDTCHK_NONE: Do nothing
35 * @FDTCHK_CHECKSUM: Take a checksum of the FDT before the test runs and
36 * compare it afterwards to detect any changes
37 * @FDTCHK_COPY: Make a copy of the FDT and restore it afterwards
38 */
39enum fdtchk_t {
40 FDTCHK_NONE,
41 FDTCHK_CHECKSUM,
42 FDTCHK_COPY,
43};
44
45/**
46 * fdt_action() - get the required action for the FDT
47 *
48 * @return the action that should be taken for this build
49 */
50static enum fdtchk_t fdt_action(void)
51{
Simon Glass3ba76752022-09-06 20:27:05 -060052 /* For sandbox SPL builds, do nothing */
Simon Glass0e84d962024-09-29 19:49:50 -060053 if (IS_ENABLED(CONFIG_SANDBOX) && IS_ENABLED(CONFIG_XPL_BUILD))
Simon Glass3ba76752022-09-06 20:27:05 -060054 return FDTCHK_NONE;
55
Simon Glass234f05b2023-02-22 09:34:12 -070056 /* Do a copy for sandbox (but only the U-Boot build, not SPL) */
57 if (IS_ENABLED(CONFIG_SANDBOX))
58 return FDTCHK_COPY;
59
Simon Glass3ba76752022-09-06 20:27:05 -060060 /* For all other boards, do a checksum */
61 return FDTCHK_CHECKSUM;
62}
63
Simon Glass4066d8d2021-03-07 17:35:04 -070064/* This is valid when a test is running, NULL otherwise */
65static struct unit_test_state *cur_test_state;
66
Simon Glasse6411372025-01-20 14:25:24 -070067struct unit_test_state *ut_get_state(void)
Simon Glass4066d8d2021-03-07 17:35:04 -070068{
69 return cur_test_state;
70}
71
Simon Glasse6411372025-01-20 14:25:24 -070072void ut_set_state(struct unit_test_state *uts)
Simon Glass4066d8d2021-03-07 17:35:04 -070073{
74 cur_test_state = uts;
75}
76
Simon Glass2d989e82025-01-20 14:25:25 -070077void ut_init_state(struct unit_test_state *uts)
78{
79 memset(uts, '\0', sizeof(*uts));
80}
81
82void ut_uninit_state(struct unit_test_state *uts)
83{
84 if (IS_ENABLED(CONFIG_SANDBOX)) {
85 os_free(uts->fdt_copy);
86 os_free(uts->other_fdt);
87 }
88}
89
Simon Glass2dba4762021-03-07 17:34:58 -070090/**
91 * dm_test_pre_run() - Get ready to run a driver model test
92 *
93 * This clears out the driver model data structures. For sandbox it resets the
94 * state structure
95 *
96 * @uts: Test state
97 */
98static int dm_test_pre_run(struct unit_test_state *uts)
99{
100 bool of_live = uts->of_live;
101
Simon Glass60f08992022-09-06 20:27:06 -0600102 if (of_live && (gd->flags & GD_FLG_FDT_CHANGED)) {
103 printf("Cannot run live tree test as device tree changed\n");
104 return -EFAULT;
105 }
Simon Glass2dba4762021-03-07 17:34:58 -0700106 uts->root = NULL;
107 uts->testdev = NULL;
108 uts->force_fail_alloc = false;
109 uts->skip_post_probe = false;
Simon Glass3ba76752022-09-06 20:27:05 -0600110 if (fdt_action() == FDTCHK_CHECKSUM)
111 uts->fdt_chksum = crc8(0, gd->fdt_blob,
112 fdt_totalsize(gd->fdt_blob));
Simon Glass2dba4762021-03-07 17:34:58 -0700113 gd->dm_root = NULL;
Simon Glass1a3e39b2022-09-06 20:27:00 -0600114 malloc_disable_testing();
Simon Glassf7005042021-07-05 16:32:43 -0600115 if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA))
Simon Glass2dba4762021-03-07 17:34:58 -0700116 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
Simon Glass45b807d2021-03-25 10:44:33 +1300117 arch_reset_for_test();
Simon Glass2dba4762021-03-07 17:34:58 -0700118
119 /* Determine whether to make the live tree available */
120 gd_set_of_root(of_live ? uts->of_root : NULL);
Simon Glassba8457b2022-09-06 20:27:19 -0600121 oftree_reset();
Simon Glass2dba4762021-03-07 17:34:58 -0700122 ut_assertok(dm_init(of_live));
123 uts->root = dm_root();
124
125 return 0;
126}
127
Simon Glass5b7e55b2021-03-07 17:34:59 -0700128static int dm_test_post_run(struct unit_test_state *uts)
129{
130 int id;
131
Simon Glass3ba76752022-09-06 20:27:05 -0600132 if (gd->fdt_blob) {
133 switch (fdt_action()) {
134 case FDTCHK_COPY:
135 memcpy((void *)gd->fdt_blob, uts->fdt_copy, uts->fdt_size);
136 break;
137 case FDTCHK_CHECKSUM: {
138 uint chksum;
139
140 chksum = crc8(0, gd->fdt_blob, fdt_totalsize(gd->fdt_blob));
Simon Glass60f08992022-09-06 20:27:06 -0600141 if (chksum != uts->fdt_chksum) {
142 /*
143 * We cannot run any more tests that need the
144 * live tree, since its strings point into the
145 * flat tree, which has changed. This likely
146 * means that at least some of the pointers from
147 * the live tree point to different things
148 */
Simon Glass3ba76752022-09-06 20:27:05 -0600149 printf("Device tree changed: cannot run live tree tests\n");
Simon Glass60f08992022-09-06 20:27:06 -0600150 gd->flags |= GD_FLG_FDT_CHANGED;
151 }
Simon Glass3ba76752022-09-06 20:27:05 -0600152 break;
153 }
154 case FDTCHK_NONE:
155 break;
156 }
157 }
158
Simon Glass96113c12021-03-15 17:25:21 +1300159 /*
160 * With of-platdata-inst the uclasses are created at build time. If we
161 * destroy them we cannot get them back since uclass_add() is not
162 * supported. So skip this.
163 */
164 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
165 for (id = 0; id < UCLASS_COUNT; id++) {
166 struct uclass *uc;
Simon Glass5b7e55b2021-03-07 17:34:59 -0700167
Simon Glass96113c12021-03-15 17:25:21 +1300168 /*
169 * If the uclass doesn't exist we don't want to create
170 * it. So check that here before we call
171 * uclass_find_device().
172 */
173 uc = uclass_find(id);
174 if (!uc)
175 continue;
176 ut_assertok(uclass_destroy(uc));
177 }
Simon Glass5b7e55b2021-03-07 17:34:59 -0700178 }
179
180 return 0;
181}
182
Simon Glass242357c2021-03-07 17:34:51 -0700183/* Ensure all the test devices are probed */
184static int do_autoprobe(struct unit_test_state *uts)
185{
Michal Suchanek44322b52022-10-12 21:57:51 +0200186 return uclass_probe_all(UCLASS_TEST);
Simon Glass242357c2021-03-07 17:34:51 -0700187}
188
Simon Glass0d32ec22021-03-07 17:35:03 -0700189/*
190 * ut_test_run_on_flattree() - Check if we should run a test with flat DT
191 *
192 * This skips long/slow tests where there is not much value in running a flat
193 * DT test in addition to a live DT test.
194 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100195 * Return: true to run the given test on the flat device tree
Simon Glass0d32ec22021-03-07 17:35:03 -0700196 */
197static bool ut_test_run_on_flattree(struct unit_test *test)
198{
199 const char *fname = strrchr(test->file, '/') + 1;
200
Simon Glass1a92f832024-08-22 07:57:48 -0600201 if (!(test->flags & UTF_DM))
Simon Glass0d32ec22021-03-07 17:35:03 -0700202 return false;
203
204 return !strstr(fname, "video") || strstr(test->name, "video_base");
205}
206
Simon Glass436687e2021-03-07 17:35:01 -0700207/**
Simon Glassbb2b1732021-03-07 17:35:05 -0700208 * test_matches() - Check if a test should be run
209 *
210 * This checks if the a test should be run. In the normal case of running all
211 * tests, @select_name is NULL.
212 *
213 * @prefix: String prefix for the tests. Any tests that have this prefix will be
214 * printed without the prefix, so that it is easier to see the unique part
Simon Glass1ef74ab2021-03-07 17:35:12 -0700215 * of the test name. If NULL, any suite name (xxx_test) is considered to be
216 * a prefix.
Simon Glassbb2b1732021-03-07 17:35:05 -0700217 * @test_name: Name of current test
218 * @select_name: Name of test to run (or NULL for all)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100219 * Return: true to run this test, false to skip it
Simon Glassbb2b1732021-03-07 17:35:05 -0700220 */
221static bool test_matches(const char *prefix, const char *test_name,
222 const char *select_name)
223{
Andy Shevchenkod91cfbb2021-02-11 16:40:10 +0200224 size_t len;
225
Simon Glassbb2b1732021-03-07 17:35:05 -0700226 if (!select_name)
227 return true;
228
Andy Shevchenkod91cfbb2021-02-11 16:40:10 +0200229 /* Allow glob expansion in the test name */
230 len = select_name[strlen(select_name) - 1] == '*' ? strlen(select_name) : 0;
231 if (len-- == 1)
232 return true;
233
234 if (!strncmp(test_name, select_name, len))
Simon Glassbb2b1732021-03-07 17:35:05 -0700235 return true;
236
Andy Shevchenko00d42902021-02-11 16:40:11 +0200237 if (prefix) {
238 /* All tests have this prefix */
239 if (!strncmp(test_name, prefix, strlen(prefix)))
240 test_name += strlen(prefix);
241 } else {
Simon Glass1ef74ab2021-03-07 17:35:12 -0700242 const char *p = strstr(test_name, "_test_");
243
244 /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
245 if (p)
Andy Shevchenko00d42902021-02-11 16:40:11 +0200246 test_name = p + strlen("_test_");
Simon Glass1ef74ab2021-03-07 17:35:12 -0700247 }
Simon Glassbb2b1732021-03-07 17:35:05 -0700248
Andy Shevchenkod91cfbb2021-02-11 16:40:10 +0200249 if (!strncmp(test_name, select_name, len))
Simon Glassbb2b1732021-03-07 17:35:05 -0700250 return true;
251
252 return false;
253}
254
Simon Glass53d1b192021-03-07 17:35:08 -0700255/**
Simon Glass1899e132021-03-07 17:35:07 -0700256 * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
257 *
258 * @tests: List of tests to run
Simon Glass798b5c52024-10-19 09:21:56 -0600259 * @count: Number of tests to run
260 * @prefix: String prefix for the tests. Any tests that have this prefix will be
261 * printed without the prefix, so that it is easier to see the unique part
262 * of the test name. If NULL, no prefix processing is done
263 * @select_name: Name of a single test being run (from the list provided). If
264 * NULL all tests are being run
Simon Glass1a92f832024-08-22 07:57:48 -0600265 * Return: true if any of the tests have the UTF_DM flag
Simon Glass1899e132021-03-07 17:35:07 -0700266 */
Simon Glass798b5c52024-10-19 09:21:56 -0600267static bool ut_list_has_dm_tests(struct unit_test *tests, int count,
268 const char *prefix, const char *select_name)
Simon Glass1899e132021-03-07 17:35:07 -0700269{
270 struct unit_test *test;
271
272 for (test = tests; test < tests + count; test++) {
Simon Glass798b5c52024-10-19 09:21:56 -0600273 if (test_matches(prefix, test->name, select_name) &&
274 (test->flags & UTF_DM))
Simon Glass1899e132021-03-07 17:35:07 -0700275 return true;
276 }
277
278 return false;
279}
280
Simon Glassbb2b1732021-03-07 17:35:05 -0700281/**
Simon Glass53d1b192021-03-07 17:35:08 -0700282 * dm_test_restore() Put things back to normal so sandbox works as expected
283 *
284 * @of_root: Value to set for of_root
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100285 * Return: 0 if OK, -ve on error
Simon Glass53d1b192021-03-07 17:35:08 -0700286 */
287static int dm_test_restore(struct device_node *of_root)
288{
289 int ret;
290
291 gd_set_of_root(of_root);
292 gd->dm_root = NULL;
293 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
294 if (ret)
295 return ret;
296 dm_scan_plat(false);
297 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
AKASHI Takahiro16f51472023-06-08 09:55:59 +0900298 dm_extended_scan(false);
Simon Glass53d1b192021-03-07 17:35:08 -0700299
300 return 0;
301}
302
303/**
Simon Glass436687e2021-03-07 17:35:01 -0700304 * test_pre_run() - Handle any preparation needed to run a test
305 *
306 * @uts: Test state
307 * @test: Test to prepare for
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100308 * Return: 0 if OK, -EAGAIN to skip this test since some required feature is not
Simon Glass436687e2021-03-07 17:35:01 -0700309 * available, other -ve on error (meaning that testing cannot likely
310 * continue)
311 */
312static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd93dc7d2021-03-07 17:34:48 -0700313{
Simon Glassa128b1b2022-03-04 08:43:01 -0700314 ut_assertok(event_init());
315
Simon Glass1880b8a2024-09-01 16:26:20 -0600316 /*
317 * Remove any USB keyboard, so that we can add and remove USB devices
318 * in tests.
319 *
Simon Glassb0fd7a22024-10-14 14:17:53 -0600320 * For UTF_DM tests, the old driver model state is saved and
Simon Glass1880b8a2024-09-01 16:26:20 -0600321 * restored across each test. Within in each test there is therefore a
322 * new driver model state, which means that any USB keyboard device in
323 * stdio points to the old state.
324 *
Simon Glassb0fd7a22024-10-14 14:17:53 -0600325 * This is fine in most cases. But if a non-UTF_DM test starts up
Simon Glass1880b8a2024-09-01 16:26:20 -0600326 * USB (thus creating a stdio record pointing to the USB keyboard
327 * device) then when the test finishes, the new driver model state is
328 * freed, meaning that there is now a stale pointer in stdio.
329 *
Simon Glassb0fd7a22024-10-14 14:17:53 -0600330 * This means that any future UTF_DM test which uses stdin will
Simon Glass1880b8a2024-09-01 16:26:20 -0600331 * cause the console system to call tstc() on the stale device pointer,
332 * causing a crash.
333 *
Simon Glassb0fd7a22024-10-14 14:17:53 -0600334 * We don't want to fix this by enabling UTF_DM for all tests as
Simon Glass1880b8a2024-09-01 16:26:20 -0600335 * this causes other problems. For example, bootflow_efi relies on
336 * U-Boot going through a proper init - without that we don't have the
337 * TCG measurement working and get an error
338 * 'tcg2 measurement fails(0x8000000000000007)'. Once we tidy up how EFI
339 * runs tests (e.g. get rid of all the restarting of U-Boot) we could
Simon Glassb0fd7a22024-10-14 14:17:53 -0600340 * potentially make the bootstd tests set UTF_DM, but other tests
Simon Glass1880b8a2024-09-01 16:26:20 -0600341 * might do the same thing.
342 *
343 * We could add a test flag to declare that USB is being used, but that
344 * seems unnecessary, at least for now. We could detect USB being used
345 * in a test, but there is no obvious drawback to clearing out stale
346 * pointers always.
347 *
348 * So just remove any USB keyboards from the console tables. This allows
Simon Glassb0fd7a22024-10-14 14:17:53 -0600349 * UTF_DM and non-UTF_DM tests to coexist happily.
Simon Glass1880b8a2024-09-01 16:26:20 -0600350 */
351 usb_kbd_remove_for_test();
352
Simon Glass1a92f832024-08-22 07:57:48 -0600353 if (test->flags & UTF_DM)
Simon Glass2dba4762021-03-07 17:34:58 -0700354 ut_assertok(dm_test_pre_run(uts));
Simon Glassb2890a12021-03-07 17:34:56 -0700355
Simon Glass59cad962021-03-07 17:34:55 -0700356 ut_set_skip_delays(uts, false);
357
Simon Glass86a5bd02021-03-07 17:34:53 -0700358 uts->start = mallinfo();
Simon Glassd93dc7d2021-03-07 17:34:48 -0700359
Simon Glass1a92f832024-08-22 07:57:48 -0600360 if (test->flags & UTF_SCAN_PDATA)
Simon Glass177e0fd2021-03-07 17:34:52 -0700361 ut_assertok(dm_scan_plat(false));
362
Simon Glass1a92f832024-08-22 07:57:48 -0600363 if (test->flags & UTF_PROBE_TEST)
Simon Glass242357c2021-03-07 17:34:51 -0700364 ut_assertok(do_autoprobe(uts));
365
Sean Anderson7bf2f8c2023-10-14 16:47:46 -0400366 if (CONFIG_IS_ENABLED(OF_REAL) &&
Simon Glass1a92f832024-08-22 07:57:48 -0600367 (test->flags & UTF_SCAN_FDT)) {
Simon Glassb490f5f2023-01-17 10:47:28 -0700368 /*
369 * only set this if we know the ethernet uclass will be created
370 */
Simon Glass1a92f832024-08-22 07:57:48 -0600371 eth_set_enable_bootdevs(test->flags & UTF_ETH_BOOTDEV);
372 test_sf_set_enable_bootdevs(test->flags & UTF_SF_BOOTDEV);
Simon Glassd18f7392021-03-07 17:34:50 -0700373 ut_assertok(dm_extended_scan(false));
Simon Glassb490f5f2023-01-17 10:47:28 -0700374 }
Simon Glassd18f7392021-03-07 17:34:50 -0700375
Simon Glassb9ff6482023-01-17 10:47:23 -0700376 /*
377 * Do this after FDT scan since dm_scan_other() in bootstd-uclass.c
378 * checks for the existence of bootstd
379 */
Simon Glass1a92f832024-08-22 07:57:48 -0600380 if (test->flags & UTF_SCAN_PDATA)
Simon Glassb9ff6482023-01-17 10:47:23 -0700381 ut_assertok(dm_scan_other(false));
382
Simon Glass1a92f832024-08-22 07:57:48 -0600383 if (IS_ENABLED(CONFIG_SANDBOX) && (test->flags & UTF_OTHER_FDT)) {
Simon Glassb9958072022-09-06 20:27:11 -0600384 /* make sure the other FDT is available */
385 ut_assertok(test_load_other_fdt(uts));
386
387 /*
388 * create a new live tree with it for every test, in case a
389 * test modifies the tree
390 */
391 if (of_live_active()) {
392 ut_assertok(unflatten_device_tree(uts->other_fdt,
393 &uts->of_other));
394 }
395 }
396
Simon Glass11fcfa32024-08-22 07:57:50 -0600397 if (test->flags & UTF_CONSOLE) {
Simon Glassd93dc7d2021-03-07 17:34:48 -0700398 int ret = console_record_reset_enable();
399
400 if (ret) {
401 printf("Skipping: Console recording disabled\n");
402 return -EAGAIN;
403 }
404 }
Simon Glassbe277ee2024-10-28 13:47:53 +0100405 if (test->flags & UFT_BLOBLIST) {
406 log_debug("save bloblist %p\n", gd_bloblist());
407 uts->old_bloblist = gd_bloblist();
408 gd_set_bloblist(NULL);
409 }
410
Simon Glass2b566b92021-03-07 17:34:54 -0700411 ut_silence_console(uts);
Simon Glassd93dc7d2021-03-07 17:34:48 -0700412
413 return 0;
414}
415
Simon Glass436687e2021-03-07 17:35:01 -0700416/**
417 * test_post_run() - Handle cleaning up after a test
418 *
419 * @uts: Test state
420 * @test: Test to clean up after
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100421 * Return: 0 if OK, -ve on error (meaning that testing cannot likely continue)
Simon Glass436687e2021-03-07 17:35:01 -0700422 */
423static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd93dc7d2021-03-07 17:34:48 -0700424{
Simon Glass2b566b92021-03-07 17:34:54 -0700425 ut_unsilence_console(uts);
Simon Glass1a92f832024-08-22 07:57:48 -0600426 if (test->flags & UTF_DM)
Simon Glass5b7e55b2021-03-07 17:34:59 -0700427 ut_assertok(dm_test_post_run(uts));
Rasmus Villemoesc794e492022-10-28 13:50:54 +0200428 ut_assertok(cyclic_unregister_all());
Simon Glassa128b1b2022-03-04 08:43:01 -0700429 ut_assertok(event_uninit());
Simon Glass0f8f6772021-03-07 17:34:49 -0700430
Simon Glassb9958072022-09-06 20:27:11 -0600431 free(uts->of_other);
432 uts->of_other = NULL;
433
Simon Glassbe277ee2024-10-28 13:47:53 +0100434 if (test->flags & UFT_BLOBLIST) {
435 gd_set_bloblist(uts->old_bloblist);
436 log_debug("restore bloblist %p\n", gd_bloblist());
437 }
438
Simon Glass76c62692022-10-29 19:47:08 -0600439 blkcache_free();
440
Simon Glassd93dc7d2021-03-07 17:34:48 -0700441 return 0;
442}
443
Simon Glass0d32ec22021-03-07 17:35:03 -0700444/**
Simon Glass816fe6c2022-10-20 18:22:48 -0600445 * skip_test() - Handle skipping a test
446 *
447 * @uts: Test state to update
448 * @return -EAGAIN (always)
449 */
450static int skip_test(struct unit_test_state *uts)
451{
Simon Glassef69dbe2025-01-20 14:25:59 -0700452 uts->cur.skip_count++;
Simon Glass816fe6c2022-10-20 18:22:48 -0600453
454 return -EAGAIN;
455}
456
457/**
Simon Glass0d32ec22021-03-07 17:35:03 -0700458 * ut_run_test() - Run a single test
459 *
460 * This runs the test, handling any preparation and clean-up needed. It prints
461 * the name of each test before running it.
462 *
463 * @uts: Test state to update. The caller should ensure that this is zeroed for
Simon Glassef69dbe2025-01-20 14:25:59 -0700464 * the first call to this function. On exit, @uts->cur.fail_count is
Simon Glass0d32ec22021-03-07 17:35:03 -0700465 * incremented by the number of failures (0, one hopes)
466 * @test_name: Test to run
467 * @name: Name of test, possibly skipping a prefix that should not be displayed
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100468 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
Simon Glass0d32ec22021-03-07 17:35:03 -0700469 * any failed
470 */
471static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
472 const char *test_name)
Simon Glass5517edb2021-03-07 17:35:00 -0700473{
Simon Glass436687e2021-03-07 17:35:01 -0700474 const char *fname = strrchr(test->file, '/') + 1;
475 const char *note = "";
Simon Glass5517edb2021-03-07 17:35:00 -0700476 int ret;
477
Simon Glass1a92f832024-08-22 07:57:48 -0600478 if ((test->flags & UTF_DM) && !uts->of_live)
Simon Glass436687e2021-03-07 17:35:01 -0700479 note = " (flat tree)";
480 printf("Test: %s: %s%s\n", test_name, fname, note);
Simon Glass5517edb2021-03-07 17:35:00 -0700481
Simon Glass4066d8d2021-03-07 17:35:04 -0700482 /* Allow access to test state from drivers */
Simon Glasse6411372025-01-20 14:25:24 -0700483 ut_set_state(uts);
Simon Glass4066d8d2021-03-07 17:35:04 -0700484
Simon Glass5517edb2021-03-07 17:35:00 -0700485 ret = test_pre_run(uts, test);
486 if (ret == -EAGAIN)
Simon Glass816fe6c2022-10-20 18:22:48 -0600487 return skip_test(uts);
Simon Glass5517edb2021-03-07 17:35:00 -0700488 if (ret)
489 return ret;
490
Simon Glass816fe6c2022-10-20 18:22:48 -0600491 ret = test->func(uts);
492 if (ret == -EAGAIN)
493 skip_test(uts);
Simon Glass5517edb2021-03-07 17:35:00 -0700494
495 ret = test_post_run(uts, test);
496 if (ret)
497 return ret;
498
Simon Glasse6411372025-01-20 14:25:24 -0700499 ut_set_state(NULL);
Simon Glass4066d8d2021-03-07 17:35:04 -0700500
Simon Glass5517edb2021-03-07 17:35:00 -0700501 return 0;
502}
503
Simon Glassbb2b1732021-03-07 17:35:05 -0700504/**
505 * ut_run_test_live_flat() - Run a test with both live and flat tree
506 *
507 * This calls ut_run_test() with livetree enabled, which is the standard setup
508 * for runnig tests. Then, for driver model test, it calls it again with
509 * livetree disabled. This allows checking of flattree being used when OF_LIVE
510 * is enabled, as is the case in U-Boot proper before relocation, as well as in
511 * SPL.
512 *
513 * @uts: Test state to update. The caller should ensure that this is zeroed for
Simon Glassef69dbe2025-01-20 14:25:59 -0700514 * the first call to this function. On exit, @uts->cur.fail_count is
Simon Glassbb2b1732021-03-07 17:35:05 -0700515 * incremented by the number of failures (0, one hopes)
516 * @test: Test to run
Simon Glass598a1a52025-02-07 11:30:37 -0700517 * @leaf: Part of the name to show, or NULL to use test->name
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100518 * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
Simon Glassbb2b1732021-03-07 17:35:05 -0700519 * any failed
520 */
521static int ut_run_test_live_flat(struct unit_test_state *uts,
Simon Glass598a1a52025-02-07 11:30:37 -0700522 struct unit_test *test, const char *leaf)
Simon Glass0d32ec22021-03-07 17:35:03 -0700523{
Simon Glass94b35832024-10-09 18:28:59 -0600524 int runs, ret;
Simon Glass0d32ec22021-03-07 17:35:03 -0700525
Simon Glass1a92f832024-08-22 07:57:48 -0600526 if ((test->flags & UTF_OTHER_FDT) && !IS_ENABLED(CONFIG_SANDBOX))
Simon Glass816fe6c2022-10-20 18:22:48 -0600527 return skip_test(uts);
Simon Glassb9958072022-09-06 20:27:11 -0600528
Simon Glass0d32ec22021-03-07 17:35:03 -0700529 /* Run with the live tree if possible */
530 runs = 0;
531 if (CONFIG_IS_ENABLED(OF_LIVE)) {
Simon Glass1a92f832024-08-22 07:57:48 -0600532 if (!(test->flags & UTF_FLAT_TREE)) {
Simon Glass0d32ec22021-03-07 17:35:03 -0700533 uts->of_live = true;
Simon Glass598a1a52025-02-07 11:30:37 -0700534 ret = ut_run_test(uts, test, leaf ?: test->name);
Simon Glass94b35832024-10-09 18:28:59 -0600535 if (ret != -EAGAIN) {
536 ut_assertok(ret);
537 runs++;
538 }
Simon Glass0d32ec22021-03-07 17:35:03 -0700539 }
540 }
541
542 /*
Simon Glassb9958072022-09-06 20:27:11 -0600543 * Run with the flat tree if:
544 * - it is not marked for live tree only
545 * - it doesn't require the 'other' FDT when OFNODE_MULTI_TREE_MAX is
546 * not enabled (since flat tree can only support a single FDT in that
547 * case
548 * - we couldn't run it with live tree,
549 * - it is a core test (dm tests except video)
550 * - the FDT is still valid and has not been updated by an earlier test
551 * (for sandbox we handle this by copying the tree, but not for other
552 * boards)
Simon Glass0d32ec22021-03-07 17:35:03 -0700553 */
Sean Anderson5765fb12023-09-29 12:06:54 -0400554 if ((!CONFIG_IS_ENABLED(OF_LIVE) ||
Simon Glass1a92f832024-08-22 07:57:48 -0600555 (test->flags & UTF_SCAN_FDT)) &&
556 !(test->flags & UTF_LIVE_TREE) &&
Simon Glassb9958072022-09-06 20:27:11 -0600557 (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) ||
Simon Glass1a92f832024-08-22 07:57:48 -0600558 !(test->flags & UTF_OTHER_FDT)) &&
Simon Glass60f08992022-09-06 20:27:06 -0600559 (!runs || ut_test_run_on_flattree(test)) &&
560 !(gd->flags & GD_FLG_FDT_CHANGED)) {
Simon Glass0d32ec22021-03-07 17:35:03 -0700561 uts->of_live = false;
Simon Glass598a1a52025-02-07 11:30:37 -0700562 ret = ut_run_test(uts, test, leaf ?: test->name);
Simon Glass94b35832024-10-09 18:28:59 -0600563 if (ret != -EAGAIN) {
564 ut_assertok(ret);
565 runs++;
566 }
Simon Glass0d32ec22021-03-07 17:35:03 -0700567 }
568
569 return 0;
570}
571
Simon Glassbb2b1732021-03-07 17:35:05 -0700572/**
573 * ut_run_tests() - Run a set of tests
574 *
575 * This runs the tests, handling any preparation and clean-up needed. It prints
576 * the name of each test before running it.
577 *
578 * @uts: Test state to update. The caller should ensure that this is zeroed for
Simon Glassef69dbe2025-01-20 14:25:59 -0700579 * the first call to this function. On exit, @uts->cur.fail_count is
Simon Glassbb2b1732021-03-07 17:35:05 -0700580 * incremented by the number of failures (0, one hopes)
581 * @prefix: String prefix for the tests. Any tests that have this prefix will be
582 * printed without the prefix, so that it is easier to see the unique part
583 * of the test name. If NULL, no prefix processing is done
584 * @tests: List of tests to run
585 * @count: Number of tests to run
586 * @select_name: Name of a single test to run (from the list provided). If NULL
587 * then all tests are run
Simon Glass798b5c52024-10-19 09:21:56 -0600588 * @test_insert: String describing a test to run after n other tests run, in the
589 * format n:name where n is the number of tests to run before this one and
590 * name is the name of the test to run
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100591 * Return: 0 if all tests passed, -ENOENT if test @select_name was not found,
Simon Glassbb2b1732021-03-07 17:35:05 -0700592 * -EBADF if any failed
593 */
594static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
595 struct unit_test *tests, int count,
Simon Glass85ba7c32022-10-29 19:47:13 -0600596 const char *select_name, const char *test_insert)
Simon Glass5722fb22021-03-07 17:34:47 -0700597{
Simon Glass598a1a52025-02-07 11:30:37 -0700598 int prefix_len = prefix ? strlen(prefix) : 0;
Simon Glass85ba7c32022-10-29 19:47:13 -0600599 struct unit_test *test, *one;
Simon Glass5722fb22021-03-07 17:34:47 -0700600 int found = 0;
Simon Glass85ba7c32022-10-29 19:47:13 -0600601 int pos = 0;
602 int upto;
Simon Glass5722fb22021-03-07 17:34:47 -0700603
Simon Glass85ba7c32022-10-29 19:47:13 -0600604 one = NULL;
605 if (test_insert) {
606 char *p;
607
608 pos = dectoul(test_insert, NULL);
609 p = strchr(test_insert, ':');
610 if (p)
611 p++;
612
613 for (test = tests; test < tests + count; test++) {
614 if (!strcmp(p, test->name))
615 one = test;
616 }
617 }
618
619 for (upto = 0, test = tests; test < tests + count; test++, upto++) {
Simon Glass5722fb22021-03-07 17:34:47 -0700620 const char *test_name = test->name;
Simon Glass91a187b2022-08-01 07:58:45 -0600621 int ret, i, old_fail_count;
Simon Glass5722fb22021-03-07 17:34:47 -0700622
Simon Glass5c9a1242025-02-07 11:30:38 -0700623 if (!(test->flags & (UTF_INIT | UTF_UNINIT)) &&
624 !test_matches(prefix, test_name, select_name))
Simon Glass5722fb22021-03-07 17:34:47 -0700625 continue;
Simon Glass1f1614b2022-10-20 18:22:50 -0600626
Simon Glass1a92f832024-08-22 07:57:48 -0600627 if (test->flags & UTF_MANUAL) {
Simon Glass1f1614b2022-10-20 18:22:50 -0600628 int len;
629
630 /*
631 * manual tests must have a name ending "_norun" as this
632 * is how pytest knows to skip them. See
633 * generate_ut_subtest() for this check.
634 */
635 len = strlen(test_name);
636 if (len < 6 || strcmp(test_name + len - 6, "_norun")) {
Simon Glass4c4bf3e2024-11-02 13:37:05 -0600637 printf("Test '%s' is manual so must have a name ending in _norun\n",
Simon Glass1f1614b2022-10-20 18:22:50 -0600638 test_name);
Simon Glassef69dbe2025-01-20 14:25:59 -0700639 uts->cur.fail_count++;
Simon Glass1f1614b2022-10-20 18:22:50 -0600640 return -EBADF;
641 }
642 if (!uts->force_run) {
Simon Glass2939e892025-01-20 14:25:31 -0700643 printf("Test: %s: skipped as it is manual (use -f to run it)\n",
644 test_name);
Simon Glass1f1614b2022-10-20 18:22:50 -0600645 continue;
646 }
647 }
Simon Glassef69dbe2025-01-20 14:25:59 -0700648 old_fail_count = uts->cur.fail_count;
Simon Glass85ba7c32022-10-29 19:47:13 -0600649
Simon Glass4f621e52025-01-20 14:26:00 -0700650 uts->cur.test_count++;
Simon Glass85ba7c32022-10-29 19:47:13 -0600651 if (one && upto == pos) {
Simon Glass598a1a52025-02-07 11:30:37 -0700652 ret = ut_run_test_live_flat(uts, one, NULL);
Simon Glassef69dbe2025-01-20 14:25:59 -0700653 if (uts->cur.fail_count != old_fail_count) {
Simon Glass4c4bf3e2024-11-02 13:37:05 -0600654 printf("Test '%s' failed %d times (position %d)\n",
Simon Glass85ba7c32022-10-29 19:47:13 -0600655 one->name,
Simon Glassef69dbe2025-01-20 14:25:59 -0700656 uts->cur.fail_count - old_fail_count,
657 pos);
Simon Glass85ba7c32022-10-29 19:47:13 -0600658 }
659 return -EBADF;
660 }
661
Simon Glass598a1a52025-02-07 11:30:37 -0700662 if (prefix_len && !strncmp(test_name, prefix, prefix_len))
663 test_name = test_name + prefix_len;
664
Simon Glass91a187b2022-08-01 07:58:45 -0600665 for (i = 0; i < uts->runs_per_test; i++)
Simon Glass598a1a52025-02-07 11:30:37 -0700666 ret = ut_run_test_live_flat(uts, test, test_name);
Simon Glassef69dbe2025-01-20 14:25:59 -0700667 if (uts->cur.fail_count != old_fail_count) {
Simon Glass4c4bf3e2024-11-02 13:37:05 -0600668 printf("Test '%s' failed %d times\n", test_name,
Simon Glassef69dbe2025-01-20 14:25:59 -0700669 uts->cur.fail_count - old_fail_count);
Simon Glass91a187b2022-08-01 07:58:45 -0600670 }
Simon Glass5722fb22021-03-07 17:34:47 -0700671 found++;
Simon Glassd93dc7d2021-03-07 17:34:48 -0700672 if (ret == -EAGAIN)
673 continue;
674 if (ret)
675 return ret;
Simon Glass5722fb22021-03-07 17:34:47 -0700676 }
677 if (select_name && !found)
678 return -ENOENT;
679
Simon Glassef69dbe2025-01-20 14:25:59 -0700680 return uts->cur.fail_count ? -EBADF : 0;
Simon Glass5722fb22021-03-07 17:34:47 -0700681}
682
Simon Glass1f271dc2025-01-20 14:26:01 -0700683void ut_report(struct ut_stats *stats, int run_count)
684{
685 if (run_count > 1)
Simon Glass70737152025-02-07 11:30:57 -0700686 printf("Suites run: %d, total tests", run_count);
Simon Glass1f271dc2025-01-20 14:26:01 -0700687 else
688 printf("Tests");
689 printf(" run: %d, ", stats->test_count);
Simon Glassbd9b1512025-02-07 11:30:36 -0700690 if (stats && stats->test_count) {
691 ulong dur = stats->duration_ms;
692
693 printf("%ld ms, average: %ld ms, ", dur,
694 dur ? dur / stats->test_count : 0);
695 }
Simon Glass1f271dc2025-01-20 14:26:01 -0700696 if (stats->skip_count)
697 printf("skipped: %d, ", stats->skip_count);
698 printf("failures: %d\n", stats->fail_count);
699}
700
Simon Glass3869eb52025-01-20 14:25:26 -0700701int ut_run_list(struct unit_test_state *uts, const char *category,
702 const char *prefix, struct unit_test *tests, int count,
703 const char *select_name, int runs_per_test, bool force_run,
704 const char *test_insert)
Simon Glass5722fb22021-03-07 17:34:47 -0700705{
Simon Glass3869eb52025-01-20 14:25:26 -0700706 ;
Simon Glass53d1b192021-03-07 17:35:08 -0700707 bool has_dm_tests = false;
Simon Glass5fa92d62025-02-07 11:30:35 -0700708 ulong start_offset = 0;
709 ulong test_offset = 0;
Simon Glass5722fb22021-03-07 17:34:47 -0700710 int ret;
711
Simon Glassfb82a042025-01-20 14:26:02 -0700712 memset(&uts->cur, '\0', sizeof(struct ut_stats));
Simon Glass5fa92d62025-02-07 11:30:35 -0700713 if (CONFIG_IS_ENABLED(UNIT_TEST_DURATION)) {
714 uts->cur.start = get_timer(0);
715 start_offset = timer_test_get_offset();
716 }
Simon Glassfb82a042025-01-20 14:26:02 -0700717
Simon Glass1899e132021-03-07 17:35:07 -0700718 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
Simon Glass798b5c52024-10-19 09:21:56 -0600719 ut_list_has_dm_tests(tests, count, prefix, select_name)) {
Simon Glass53d1b192021-03-07 17:35:08 -0700720 has_dm_tests = true;
Simon Glass1899e132021-03-07 17:35:07 -0700721 /*
722 * If we have no device tree, or it only has a root node, then
Simon Glass21cc05e2025-02-07 11:30:33 -0700723 * these tests clearly aren't going to work...
Simon Glass1899e132021-03-07 17:35:07 -0700724 */
725 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
726 puts("Please run with test device tree:\n"
727 " ./u-boot -d arch/sandbox/dts/test.dtb\n");
728 return CMD_RET_FAILURE;
729 }
730 }
731
Simon Glass5722fb22021-03-07 17:34:47 -0700732 if (!select_name)
733 printf("Running %d %s tests\n", count, category);
734
Simon Glass3869eb52025-01-20 14:25:26 -0700735 uts->of_root = gd_of_root();
736 uts->runs_per_test = runs_per_test;
Simon Glass3ba76752022-09-06 20:27:05 -0600737 if (fdt_action() == FDTCHK_COPY && gd->fdt_blob) {
Simon Glass3869eb52025-01-20 14:25:26 -0700738 uts->fdt_size = fdt_totalsize(gd->fdt_blob);
739 uts->fdt_copy = os_malloc(uts->fdt_size);
740 if (!uts->fdt_copy) {
Simon Glass3ba76752022-09-06 20:27:05 -0600741 printf("Out of memory for device tree copy\n");
742 return -ENOMEM;
743 }
Simon Glass3869eb52025-01-20 14:25:26 -0700744 memcpy(uts->fdt_copy, gd->fdt_blob, uts->fdt_size);
Simon Glass3ba76752022-09-06 20:27:05 -0600745 }
Simon Glass3869eb52025-01-20 14:25:26 -0700746 uts->force_run = force_run;
747 ret = ut_run_tests(uts, prefix, tests, count, select_name,
Simon Glass85ba7c32022-10-29 19:47:13 -0600748 test_insert);
Simon Glass5722fb22021-03-07 17:34:47 -0700749
Simon Glass3ba76752022-09-06 20:27:05 -0600750 /* Best efforts only...ignore errors */
751 if (has_dm_tests)
Simon Glass3869eb52025-01-20 14:25:26 -0700752 dm_test_restore(uts->of_root);
Simon Glass3ba76752022-09-06 20:27:05 -0600753
Simon Glass5722fb22021-03-07 17:34:47 -0700754 if (ret == -ENOENT)
755 printf("Test '%s' not found\n", select_name);
Simon Glass5fa92d62025-02-07 11:30:35 -0700756 if (CONFIG_IS_ENABLED(UNIT_TEST_DURATION)) {
757 test_offset = timer_test_get_offset() - start_offset;
758
759 uts->cur.duration_ms = get_timer(uts->cur.start) - test_offset;
760 }
761 ut_report(&uts->cur, 1);
Simon Glass5722fb22021-03-07 17:34:47 -0700762
Simon Glassfb82a042025-01-20 14:26:02 -0700763 uts->total.skip_count += uts->cur.skip_count;
764 uts->total.fail_count += uts->cur.fail_count;
765 uts->total.test_count += uts->cur.test_count;
Simon Glass5fa92d62025-02-07 11:30:35 -0700766 uts->total.duration_ms += uts->cur.duration_ms;
Simon Glassfb82a042025-01-20 14:26:02 -0700767 uts->run_count++;
768
Simon Glass5722fb22021-03-07 17:34:47 -0700769 return ret;
770}