Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2021 Google LLC |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | */ |
| 6 | |
Simon Glass | be277ee | 2024-10-28 13:47:53 +0100 | [diff] [blame] | 7 | #define LOG_CATEGORY LOGC_TEST |
| 8 | |
Simon Glass | 76c6269 | 2022-10-29 19:47:08 -0600 | [diff] [blame] | 9 | #include <blk.h> |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 10 | #include <console.h> |
Stefan Roese | 5be8f37 | 2022-09-02 13:57:54 +0200 | [diff] [blame] | 11 | #include <cyclic.h> |
Simon Glass | d18f739 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | a128b1b | 2022-03-04 08:43:01 -0700 | [diff] [blame] | 13 | #include <event.h> |
Simon Glass | b490f5f | 2023-01-17 10:47:28 -0700 | [diff] [blame] | 14 | #include <net.h> |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 15 | #include <of_live.h> |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 16 | #include <os.h> |
Simon Glass | 5fa92d6 | 2025-02-07 11:30:35 -0700 | [diff] [blame] | 17 | #include <spl.h> |
Simon Glass | 1880b8a | 2024-09-01 16:26:20 -0600 | [diff] [blame] | 18 | #include <usb.h> |
Simon Glass | ba8457b | 2022-09-06 20:27:19 -0600 | [diff] [blame] | 19 | #include <dm/ofnode.h> |
Simon Glass | d18f739 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 20 | #include <dm/root.h> |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 21 | #include <dm/test.h> |
Simon Glass | 5b7e55b | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 22 | #include <dm/uclass-internal.h> |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 23 | #include <test/test.h> |
Simon Glass | d18f739 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 24 | #include <test/ut.h> |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 25 | #include <u-boot/crc.h> |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 26 | |
Simon Glass | 0f8f677 | 2021-03-07 17:34:49 -0700 | [diff] [blame] | 27 | DECLARE_GLOBAL_DATA_PTR; |
| 28 | |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 29 | /** |
| 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 | */ |
| 39 | enum 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 | */ |
| 50 | static enum fdtchk_t fdt_action(void) |
| 51 | { |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 52 | /* For sandbox SPL builds, do nothing */ |
Simon Glass | 0e84d96 | 2024-09-29 19:49:50 -0600 | [diff] [blame] | 53 | if (IS_ENABLED(CONFIG_SANDBOX) && IS_ENABLED(CONFIG_XPL_BUILD)) |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 54 | return FDTCHK_NONE; |
| 55 | |
Simon Glass | 234f05b | 2023-02-22 09:34:12 -0700 | [diff] [blame] | 56 | /* 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 Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 60 | /* For all other boards, do a checksum */ |
| 61 | return FDTCHK_CHECKSUM; |
| 62 | } |
| 63 | |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 64 | /* This is valid when a test is running, NULL otherwise */ |
| 65 | static struct unit_test_state *cur_test_state; |
| 66 | |
Simon Glass | e641137 | 2025-01-20 14:25:24 -0700 | [diff] [blame] | 67 | struct unit_test_state *ut_get_state(void) |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 68 | { |
| 69 | return cur_test_state; |
| 70 | } |
| 71 | |
Simon Glass | e641137 | 2025-01-20 14:25:24 -0700 | [diff] [blame] | 72 | void ut_set_state(struct unit_test_state *uts) |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 73 | { |
| 74 | cur_test_state = uts; |
| 75 | } |
| 76 | |
Simon Glass | 2d989e8 | 2025-01-20 14:25:25 -0700 | [diff] [blame] | 77 | void ut_init_state(struct unit_test_state *uts) |
| 78 | { |
| 79 | memset(uts, '\0', sizeof(*uts)); |
| 80 | } |
| 81 | |
| 82 | void 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 Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 90 | /** |
| 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 | */ |
| 98 | static int dm_test_pre_run(struct unit_test_state *uts) |
| 99 | { |
| 100 | bool of_live = uts->of_live; |
| 101 | |
Simon Glass | 60f0899 | 2022-09-06 20:27:06 -0600 | [diff] [blame] | 102 | 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 Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 106 | uts->root = NULL; |
| 107 | uts->testdev = NULL; |
| 108 | uts->force_fail_alloc = false; |
| 109 | uts->skip_post_probe = false; |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 110 | if (fdt_action() == FDTCHK_CHECKSUM) |
| 111 | uts->fdt_chksum = crc8(0, gd->fdt_blob, |
| 112 | fdt_totalsize(gd->fdt_blob)); |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 113 | gd->dm_root = NULL; |
Simon Glass | 1a3e39b | 2022-09-06 20:27:00 -0600 | [diff] [blame] | 114 | malloc_disable_testing(); |
Simon Glass | f700504 | 2021-07-05 16:32:43 -0600 | [diff] [blame] | 115 | if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA)) |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 116 | memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count)); |
Simon Glass | 45b807d | 2021-03-25 10:44:33 +1300 | [diff] [blame] | 117 | arch_reset_for_test(); |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 118 | |
| 119 | /* Determine whether to make the live tree available */ |
| 120 | gd_set_of_root(of_live ? uts->of_root : NULL); |
Simon Glass | ba8457b | 2022-09-06 20:27:19 -0600 | [diff] [blame] | 121 | oftree_reset(); |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 122 | ut_assertok(dm_init(of_live)); |
| 123 | uts->root = dm_root(); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
Simon Glass | 5b7e55b | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 128 | static int dm_test_post_run(struct unit_test_state *uts) |
| 129 | { |
| 130 | int id; |
| 131 | |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 132 | 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 Glass | 60f0899 | 2022-09-06 20:27:06 -0600 | [diff] [blame] | 141 | 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 Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 149 | printf("Device tree changed: cannot run live tree tests\n"); |
Simon Glass | 60f0899 | 2022-09-06 20:27:06 -0600 | [diff] [blame] | 150 | gd->flags |= GD_FLG_FDT_CHANGED; |
| 151 | } |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 152 | break; |
| 153 | } |
| 154 | case FDTCHK_NONE: |
| 155 | break; |
| 156 | } |
| 157 | } |
| 158 | |
Simon Glass | 96113c1 | 2021-03-15 17:25:21 +1300 | [diff] [blame] | 159 | /* |
| 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 Glass | 5b7e55b | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 167 | |
Simon Glass | 96113c1 | 2021-03-15 17:25:21 +1300 | [diff] [blame] | 168 | /* |
| 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 Glass | 5b7e55b | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
Simon Glass | 242357c | 2021-03-07 17:34:51 -0700 | [diff] [blame] | 183 | /* Ensure all the test devices are probed */ |
| 184 | static int do_autoprobe(struct unit_test_state *uts) |
| 185 | { |
Michal Suchanek | 44322b5 | 2022-10-12 21:57:51 +0200 | [diff] [blame] | 186 | return uclass_probe_all(UCLASS_TEST); |
Simon Glass | 242357c | 2021-03-07 17:34:51 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 189 | /* |
| 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 195 | * Return: true to run the given test on the flat device tree |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 196 | */ |
| 197 | static bool ut_test_run_on_flattree(struct unit_test *test) |
| 198 | { |
| 199 | const char *fname = strrchr(test->file, '/') + 1; |
| 200 | |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 201 | if (!(test->flags & UTF_DM)) |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 202 | return false; |
| 203 | |
| 204 | return !strstr(fname, "video") || strstr(test->name, "video_base"); |
| 205 | } |
| 206 | |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 207 | /** |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 208 | * 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 Glass | 1ef74ab | 2021-03-07 17:35:12 -0700 | [diff] [blame] | 215 | * of the test name. If NULL, any suite name (xxx_test) is considered to be |
| 216 | * a prefix. |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 217 | * @test_name: Name of current test |
| 218 | * @select_name: Name of test to run (or NULL for all) |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 219 | * Return: true to run this test, false to skip it |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 220 | */ |
| 221 | static bool test_matches(const char *prefix, const char *test_name, |
| 222 | const char *select_name) |
| 223 | { |
Andy Shevchenko | d91cfbb | 2021-02-11 16:40:10 +0200 | [diff] [blame] | 224 | size_t len; |
| 225 | |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 226 | if (!select_name) |
| 227 | return true; |
| 228 | |
Andy Shevchenko | d91cfbb | 2021-02-11 16:40:10 +0200 | [diff] [blame] | 229 | /* 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 Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 235 | return true; |
| 236 | |
Andy Shevchenko | 00d4290 | 2021-02-11 16:40:11 +0200 | [diff] [blame] | 237 | if (prefix) { |
| 238 | /* All tests have this prefix */ |
| 239 | if (!strncmp(test_name, prefix, strlen(prefix))) |
| 240 | test_name += strlen(prefix); |
| 241 | } else { |
Simon Glass | 1ef74ab | 2021-03-07 17:35:12 -0700 | [diff] [blame] | 242 | 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 Shevchenko | 00d4290 | 2021-02-11 16:40:11 +0200 | [diff] [blame] | 246 | test_name = p + strlen("_test_"); |
Simon Glass | 1ef74ab | 2021-03-07 17:35:12 -0700 | [diff] [blame] | 247 | } |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 248 | |
Andy Shevchenko | d91cfbb | 2021-02-11 16:40:10 +0200 | [diff] [blame] | 249 | if (!strncmp(test_name, select_name, len)) |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 250 | return true; |
| 251 | |
| 252 | return false; |
| 253 | } |
| 254 | |
Simon Glass | 53d1b19 | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 255 | /** |
Simon Glass | 1899e13 | 2021-03-07 17:35:07 -0700 | [diff] [blame] | 256 | * ut_list_has_dm_tests() - Check if a list of tests has driver model ones |
| 257 | * |
| 258 | * @tests: List of tests to run |
Simon Glass | 798b5c5 | 2024-10-19 09:21:56 -0600 | [diff] [blame] | 259 | * @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 Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 265 | * Return: true if any of the tests have the UTF_DM flag |
Simon Glass | 1899e13 | 2021-03-07 17:35:07 -0700 | [diff] [blame] | 266 | */ |
Simon Glass | 798b5c5 | 2024-10-19 09:21:56 -0600 | [diff] [blame] | 267 | static bool ut_list_has_dm_tests(struct unit_test *tests, int count, |
| 268 | const char *prefix, const char *select_name) |
Simon Glass | 1899e13 | 2021-03-07 17:35:07 -0700 | [diff] [blame] | 269 | { |
| 270 | struct unit_test *test; |
| 271 | |
| 272 | for (test = tests; test < tests + count; test++) { |
Simon Glass | 798b5c5 | 2024-10-19 09:21:56 -0600 | [diff] [blame] | 273 | if (test_matches(prefix, test->name, select_name) && |
| 274 | (test->flags & UTF_DM)) |
Simon Glass | 1899e13 | 2021-03-07 17:35:07 -0700 | [diff] [blame] | 275 | return true; |
| 276 | } |
| 277 | |
| 278 | return false; |
| 279 | } |
| 280 | |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 281 | /** |
Simon Glass | 53d1b19 | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 282 | * dm_test_restore() Put things back to normal so sandbox works as expected |
| 283 | * |
| 284 | * @of_root: Value to set for of_root |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 285 | * Return: 0 if OK, -ve on error |
Simon Glass | 53d1b19 | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 286 | */ |
| 287 | static 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 Takahiro | 16f5147 | 2023-06-08 09:55:59 +0900 | [diff] [blame] | 298 | dm_extended_scan(false); |
Simon Glass | 53d1b19 | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | /** |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 304 | * test_pre_run() - Handle any preparation needed to run a test |
| 305 | * |
| 306 | * @uts: Test state |
| 307 | * @test: Test to prepare for |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 308 | * Return: 0 if OK, -EAGAIN to skip this test since some required feature is not |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 309 | * available, other -ve on error (meaning that testing cannot likely |
| 310 | * continue) |
| 311 | */ |
| 312 | static int test_pre_run(struct unit_test_state *uts, struct unit_test *test) |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 313 | { |
Simon Glass | a128b1b | 2022-03-04 08:43:01 -0700 | [diff] [blame] | 314 | ut_assertok(event_init()); |
| 315 | |
Simon Glass | 1880b8a | 2024-09-01 16:26:20 -0600 | [diff] [blame] | 316 | /* |
| 317 | * Remove any USB keyboard, so that we can add and remove USB devices |
| 318 | * in tests. |
| 319 | * |
Simon Glass | b0fd7a2 | 2024-10-14 14:17:53 -0600 | [diff] [blame] | 320 | * For UTF_DM tests, the old driver model state is saved and |
Simon Glass | 1880b8a | 2024-09-01 16:26:20 -0600 | [diff] [blame] | 321 | * 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 Glass | b0fd7a2 | 2024-10-14 14:17:53 -0600 | [diff] [blame] | 325 | * This is fine in most cases. But if a non-UTF_DM test starts up |
Simon Glass | 1880b8a | 2024-09-01 16:26:20 -0600 | [diff] [blame] | 326 | * 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 Glass | b0fd7a2 | 2024-10-14 14:17:53 -0600 | [diff] [blame] | 330 | * This means that any future UTF_DM test which uses stdin will |
Simon Glass | 1880b8a | 2024-09-01 16:26:20 -0600 | [diff] [blame] | 331 | * cause the console system to call tstc() on the stale device pointer, |
| 332 | * causing a crash. |
| 333 | * |
Simon Glass | b0fd7a2 | 2024-10-14 14:17:53 -0600 | [diff] [blame] | 334 | * We don't want to fix this by enabling UTF_DM for all tests as |
Simon Glass | 1880b8a | 2024-09-01 16:26:20 -0600 | [diff] [blame] | 335 | * 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 Glass | b0fd7a2 | 2024-10-14 14:17:53 -0600 | [diff] [blame] | 340 | * potentially make the bootstd tests set UTF_DM, but other tests |
Simon Glass | 1880b8a | 2024-09-01 16:26:20 -0600 | [diff] [blame] | 341 | * 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 Glass | b0fd7a2 | 2024-10-14 14:17:53 -0600 | [diff] [blame] | 349 | * UTF_DM and non-UTF_DM tests to coexist happily. |
Simon Glass | 1880b8a | 2024-09-01 16:26:20 -0600 | [diff] [blame] | 350 | */ |
| 351 | usb_kbd_remove_for_test(); |
| 352 | |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 353 | if (test->flags & UTF_DM) |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 354 | ut_assertok(dm_test_pre_run(uts)); |
Simon Glass | b2890a1 | 2021-03-07 17:34:56 -0700 | [diff] [blame] | 355 | |
Simon Glass | 59cad96 | 2021-03-07 17:34:55 -0700 | [diff] [blame] | 356 | ut_set_skip_delays(uts, false); |
| 357 | |
Simon Glass | 86a5bd0 | 2021-03-07 17:34:53 -0700 | [diff] [blame] | 358 | uts->start = mallinfo(); |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 359 | |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 360 | if (test->flags & UTF_SCAN_PDATA) |
Simon Glass | 177e0fd | 2021-03-07 17:34:52 -0700 | [diff] [blame] | 361 | ut_assertok(dm_scan_plat(false)); |
| 362 | |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 363 | if (test->flags & UTF_PROBE_TEST) |
Simon Glass | 242357c | 2021-03-07 17:34:51 -0700 | [diff] [blame] | 364 | ut_assertok(do_autoprobe(uts)); |
| 365 | |
Sean Anderson | 7bf2f8c | 2023-10-14 16:47:46 -0400 | [diff] [blame] | 366 | if (CONFIG_IS_ENABLED(OF_REAL) && |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 367 | (test->flags & UTF_SCAN_FDT)) { |
Simon Glass | b490f5f | 2023-01-17 10:47:28 -0700 | [diff] [blame] | 368 | /* |
| 369 | * only set this if we know the ethernet uclass will be created |
| 370 | */ |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 371 | eth_set_enable_bootdevs(test->flags & UTF_ETH_BOOTDEV); |
| 372 | test_sf_set_enable_bootdevs(test->flags & UTF_SF_BOOTDEV); |
Simon Glass | d18f739 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 373 | ut_assertok(dm_extended_scan(false)); |
Simon Glass | b490f5f | 2023-01-17 10:47:28 -0700 | [diff] [blame] | 374 | } |
Simon Glass | d18f739 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 375 | |
Simon Glass | b9ff648 | 2023-01-17 10:47:23 -0700 | [diff] [blame] | 376 | /* |
| 377 | * Do this after FDT scan since dm_scan_other() in bootstd-uclass.c |
| 378 | * checks for the existence of bootstd |
| 379 | */ |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 380 | if (test->flags & UTF_SCAN_PDATA) |
Simon Glass | b9ff648 | 2023-01-17 10:47:23 -0700 | [diff] [blame] | 381 | ut_assertok(dm_scan_other(false)); |
| 382 | |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 383 | if (IS_ENABLED(CONFIG_SANDBOX) && (test->flags & UTF_OTHER_FDT)) { |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 384 | /* 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 Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 397 | if (test->flags & UTF_CONSOLE) { |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 398 | int ret = console_record_reset_enable(); |
| 399 | |
| 400 | if (ret) { |
| 401 | printf("Skipping: Console recording disabled\n"); |
| 402 | return -EAGAIN; |
| 403 | } |
| 404 | } |
Simon Glass | be277ee | 2024-10-28 13:47:53 +0100 | [diff] [blame] | 405 | 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 Glass | 2b566b9 | 2021-03-07 17:34:54 -0700 | [diff] [blame] | 411 | ut_silence_console(uts); |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 416 | /** |
| 417 | * test_post_run() - Handle cleaning up after a test |
| 418 | * |
| 419 | * @uts: Test state |
| 420 | * @test: Test to clean up after |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 421 | * Return: 0 if OK, -ve on error (meaning that testing cannot likely continue) |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 422 | */ |
| 423 | static int test_post_run(struct unit_test_state *uts, struct unit_test *test) |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 424 | { |
Simon Glass | 2b566b9 | 2021-03-07 17:34:54 -0700 | [diff] [blame] | 425 | ut_unsilence_console(uts); |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 426 | if (test->flags & UTF_DM) |
Simon Glass | 5b7e55b | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 427 | ut_assertok(dm_test_post_run(uts)); |
Rasmus Villemoes | c794e49 | 2022-10-28 13:50:54 +0200 | [diff] [blame] | 428 | ut_assertok(cyclic_unregister_all()); |
Simon Glass | a128b1b | 2022-03-04 08:43:01 -0700 | [diff] [blame] | 429 | ut_assertok(event_uninit()); |
Simon Glass | 0f8f677 | 2021-03-07 17:34:49 -0700 | [diff] [blame] | 430 | |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 431 | free(uts->of_other); |
| 432 | uts->of_other = NULL; |
| 433 | |
Simon Glass | be277ee | 2024-10-28 13:47:53 +0100 | [diff] [blame] | 434 | if (test->flags & UFT_BLOBLIST) { |
| 435 | gd_set_bloblist(uts->old_bloblist); |
| 436 | log_debug("restore bloblist %p\n", gd_bloblist()); |
| 437 | } |
| 438 | |
Simon Glass | 76c6269 | 2022-10-29 19:47:08 -0600 | [diff] [blame] | 439 | blkcache_free(); |
| 440 | |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 444 | /** |
Simon Glass | 816fe6c | 2022-10-20 18:22:48 -0600 | [diff] [blame] | 445 | * skip_test() - Handle skipping a test |
| 446 | * |
| 447 | * @uts: Test state to update |
| 448 | * @return -EAGAIN (always) |
| 449 | */ |
| 450 | static int skip_test(struct unit_test_state *uts) |
| 451 | { |
Simon Glass | ef69dbe | 2025-01-20 14:25:59 -0700 | [diff] [blame] | 452 | uts->cur.skip_count++; |
Simon Glass | 816fe6c | 2022-10-20 18:22:48 -0600 | [diff] [blame] | 453 | |
| 454 | return -EAGAIN; |
| 455 | } |
| 456 | |
| 457 | /** |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 458 | * 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 Glass | ef69dbe | 2025-01-20 14:25:59 -0700 | [diff] [blame] | 464 | * the first call to this function. On exit, @uts->cur.fail_count is |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 465 | * 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 468 | * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 469 | * any failed |
| 470 | */ |
| 471 | static int ut_run_test(struct unit_test_state *uts, struct unit_test *test, |
| 472 | const char *test_name) |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 473 | { |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 474 | const char *fname = strrchr(test->file, '/') + 1; |
| 475 | const char *note = ""; |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 476 | int ret; |
| 477 | |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 478 | if ((test->flags & UTF_DM) && !uts->of_live) |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 479 | note = " (flat tree)"; |
| 480 | printf("Test: %s: %s%s\n", test_name, fname, note); |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 481 | |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 482 | /* Allow access to test state from drivers */ |
Simon Glass | e641137 | 2025-01-20 14:25:24 -0700 | [diff] [blame] | 483 | ut_set_state(uts); |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 484 | |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 485 | ret = test_pre_run(uts, test); |
| 486 | if (ret == -EAGAIN) |
Simon Glass | 816fe6c | 2022-10-20 18:22:48 -0600 | [diff] [blame] | 487 | return skip_test(uts); |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 488 | if (ret) |
| 489 | return ret; |
| 490 | |
Simon Glass | 816fe6c | 2022-10-20 18:22:48 -0600 | [diff] [blame] | 491 | ret = test->func(uts); |
| 492 | if (ret == -EAGAIN) |
| 493 | skip_test(uts); |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 494 | |
| 495 | ret = test_post_run(uts, test); |
| 496 | if (ret) |
| 497 | return ret; |
| 498 | |
Simon Glass | e641137 | 2025-01-20 14:25:24 -0700 | [diff] [blame] | 499 | ut_set_state(NULL); |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 500 | |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 501 | return 0; |
| 502 | } |
| 503 | |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 504 | /** |
| 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 Glass | ef69dbe | 2025-01-20 14:25:59 -0700 | [diff] [blame] | 514 | * the first call to this function. On exit, @uts->cur.fail_count is |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 515 | * incremented by the number of failures (0, one hopes) |
| 516 | * @test: Test to run |
Simon Glass | 598a1a5 | 2025-02-07 11:30:37 -0700 | [diff] [blame] | 517 | * @leaf: Part of the name to show, or NULL to use test->name |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 518 | * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 519 | * any failed |
| 520 | */ |
| 521 | static int ut_run_test_live_flat(struct unit_test_state *uts, |
Simon Glass | 598a1a5 | 2025-02-07 11:30:37 -0700 | [diff] [blame] | 522 | struct unit_test *test, const char *leaf) |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 523 | { |
Simon Glass | 94b3583 | 2024-10-09 18:28:59 -0600 | [diff] [blame] | 524 | int runs, ret; |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 525 | |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 526 | if ((test->flags & UTF_OTHER_FDT) && !IS_ENABLED(CONFIG_SANDBOX)) |
Simon Glass | 816fe6c | 2022-10-20 18:22:48 -0600 | [diff] [blame] | 527 | return skip_test(uts); |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 528 | |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 529 | /* Run with the live tree if possible */ |
| 530 | runs = 0; |
| 531 | if (CONFIG_IS_ENABLED(OF_LIVE)) { |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 532 | if (!(test->flags & UTF_FLAT_TREE)) { |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 533 | uts->of_live = true; |
Simon Glass | 598a1a5 | 2025-02-07 11:30:37 -0700 | [diff] [blame] | 534 | ret = ut_run_test(uts, test, leaf ?: test->name); |
Simon Glass | 94b3583 | 2024-10-09 18:28:59 -0600 | [diff] [blame] | 535 | if (ret != -EAGAIN) { |
| 536 | ut_assertok(ret); |
| 537 | runs++; |
| 538 | } |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
| 542 | /* |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 543 | * 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 Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 553 | */ |
Sean Anderson | 5765fb1 | 2023-09-29 12:06:54 -0400 | [diff] [blame] | 554 | if ((!CONFIG_IS_ENABLED(OF_LIVE) || |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 555 | (test->flags & UTF_SCAN_FDT)) && |
| 556 | !(test->flags & UTF_LIVE_TREE) && |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 557 | (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) || |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 558 | !(test->flags & UTF_OTHER_FDT)) && |
Simon Glass | 60f0899 | 2022-09-06 20:27:06 -0600 | [diff] [blame] | 559 | (!runs || ut_test_run_on_flattree(test)) && |
| 560 | !(gd->flags & GD_FLG_FDT_CHANGED)) { |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 561 | uts->of_live = false; |
Simon Glass | 598a1a5 | 2025-02-07 11:30:37 -0700 | [diff] [blame] | 562 | ret = ut_run_test(uts, test, leaf ?: test->name); |
Simon Glass | 94b3583 | 2024-10-09 18:28:59 -0600 | [diff] [blame] | 563 | if (ret != -EAGAIN) { |
| 564 | ut_assertok(ret); |
| 565 | runs++; |
| 566 | } |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | return 0; |
| 570 | } |
| 571 | |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 572 | /** |
| 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 Glass | ef69dbe | 2025-01-20 14:25:59 -0700 | [diff] [blame] | 579 | * the first call to this function. On exit, @uts->cur.fail_count is |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 580 | * 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 Glass | 798b5c5 | 2024-10-19 09:21:56 -0600 | [diff] [blame] | 588 | * @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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 591 | * Return: 0 if all tests passed, -ENOENT if test @select_name was not found, |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 592 | * -EBADF if any failed |
| 593 | */ |
| 594 | static int ut_run_tests(struct unit_test_state *uts, const char *prefix, |
| 595 | struct unit_test *tests, int count, |
Simon Glass | 85ba7c3 | 2022-10-29 19:47:13 -0600 | [diff] [blame] | 596 | const char *select_name, const char *test_insert) |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 597 | { |
Simon Glass | 598a1a5 | 2025-02-07 11:30:37 -0700 | [diff] [blame] | 598 | int prefix_len = prefix ? strlen(prefix) : 0; |
Simon Glass | 85ba7c3 | 2022-10-29 19:47:13 -0600 | [diff] [blame] | 599 | struct unit_test *test, *one; |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 600 | int found = 0; |
Simon Glass | 85ba7c3 | 2022-10-29 19:47:13 -0600 | [diff] [blame] | 601 | int pos = 0; |
| 602 | int upto; |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 603 | |
Simon Glass | 85ba7c3 | 2022-10-29 19:47:13 -0600 | [diff] [blame] | 604 | 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 Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 620 | const char *test_name = test->name; |
Simon Glass | 91a187b | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 621 | int ret, i, old_fail_count; |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 622 | |
Simon Glass | 5c9a124 | 2025-02-07 11:30:38 -0700 | [diff] [blame] | 623 | if (!(test->flags & (UTF_INIT | UTF_UNINIT)) && |
| 624 | !test_matches(prefix, test_name, select_name)) |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 625 | continue; |
Simon Glass | 1f1614b | 2022-10-20 18:22:50 -0600 | [diff] [blame] | 626 | |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 627 | if (test->flags & UTF_MANUAL) { |
Simon Glass | 1f1614b | 2022-10-20 18:22:50 -0600 | [diff] [blame] | 628 | 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 Glass | 4c4bf3e | 2024-11-02 13:37:05 -0600 | [diff] [blame] | 637 | printf("Test '%s' is manual so must have a name ending in _norun\n", |
Simon Glass | 1f1614b | 2022-10-20 18:22:50 -0600 | [diff] [blame] | 638 | test_name); |
Simon Glass | ef69dbe | 2025-01-20 14:25:59 -0700 | [diff] [blame] | 639 | uts->cur.fail_count++; |
Simon Glass | 1f1614b | 2022-10-20 18:22:50 -0600 | [diff] [blame] | 640 | return -EBADF; |
| 641 | } |
| 642 | if (!uts->force_run) { |
Simon Glass | 2939e89 | 2025-01-20 14:25:31 -0700 | [diff] [blame] | 643 | printf("Test: %s: skipped as it is manual (use -f to run it)\n", |
| 644 | test_name); |
Simon Glass | 1f1614b | 2022-10-20 18:22:50 -0600 | [diff] [blame] | 645 | continue; |
| 646 | } |
| 647 | } |
Simon Glass | ef69dbe | 2025-01-20 14:25:59 -0700 | [diff] [blame] | 648 | old_fail_count = uts->cur.fail_count; |
Simon Glass | 85ba7c3 | 2022-10-29 19:47:13 -0600 | [diff] [blame] | 649 | |
Simon Glass | 4f621e5 | 2025-01-20 14:26:00 -0700 | [diff] [blame] | 650 | uts->cur.test_count++; |
Simon Glass | 85ba7c3 | 2022-10-29 19:47:13 -0600 | [diff] [blame] | 651 | if (one && upto == pos) { |
Simon Glass | 598a1a5 | 2025-02-07 11:30:37 -0700 | [diff] [blame] | 652 | ret = ut_run_test_live_flat(uts, one, NULL); |
Simon Glass | ef69dbe | 2025-01-20 14:25:59 -0700 | [diff] [blame] | 653 | if (uts->cur.fail_count != old_fail_count) { |
Simon Glass | 4c4bf3e | 2024-11-02 13:37:05 -0600 | [diff] [blame] | 654 | printf("Test '%s' failed %d times (position %d)\n", |
Simon Glass | 85ba7c3 | 2022-10-29 19:47:13 -0600 | [diff] [blame] | 655 | one->name, |
Simon Glass | ef69dbe | 2025-01-20 14:25:59 -0700 | [diff] [blame] | 656 | uts->cur.fail_count - old_fail_count, |
| 657 | pos); |
Simon Glass | 85ba7c3 | 2022-10-29 19:47:13 -0600 | [diff] [blame] | 658 | } |
| 659 | return -EBADF; |
| 660 | } |
| 661 | |
Simon Glass | 598a1a5 | 2025-02-07 11:30:37 -0700 | [diff] [blame] | 662 | if (prefix_len && !strncmp(test_name, prefix, prefix_len)) |
| 663 | test_name = test_name + prefix_len; |
| 664 | |
Simon Glass | 91a187b | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 665 | for (i = 0; i < uts->runs_per_test; i++) |
Simon Glass | 598a1a5 | 2025-02-07 11:30:37 -0700 | [diff] [blame] | 666 | ret = ut_run_test_live_flat(uts, test, test_name); |
Simon Glass | ef69dbe | 2025-01-20 14:25:59 -0700 | [diff] [blame] | 667 | if (uts->cur.fail_count != old_fail_count) { |
Simon Glass | 4c4bf3e | 2024-11-02 13:37:05 -0600 | [diff] [blame] | 668 | printf("Test '%s' failed %d times\n", test_name, |
Simon Glass | ef69dbe | 2025-01-20 14:25:59 -0700 | [diff] [blame] | 669 | uts->cur.fail_count - old_fail_count); |
Simon Glass | 91a187b | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 670 | } |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 671 | found++; |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 672 | if (ret == -EAGAIN) |
| 673 | continue; |
| 674 | if (ret) |
| 675 | return ret; |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 676 | } |
| 677 | if (select_name && !found) |
| 678 | return -ENOENT; |
| 679 | |
Simon Glass | ef69dbe | 2025-01-20 14:25:59 -0700 | [diff] [blame] | 680 | return uts->cur.fail_count ? -EBADF : 0; |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Simon Glass | 1f271dc | 2025-01-20 14:26:01 -0700 | [diff] [blame] | 683 | void ut_report(struct ut_stats *stats, int run_count) |
| 684 | { |
| 685 | if (run_count > 1) |
Simon Glass | 7073715 | 2025-02-07 11:30:57 -0700 | [diff] [blame] | 686 | printf("Suites run: %d, total tests", run_count); |
Simon Glass | 1f271dc | 2025-01-20 14:26:01 -0700 | [diff] [blame] | 687 | else |
| 688 | printf("Tests"); |
| 689 | printf(" run: %d, ", stats->test_count); |
Simon Glass | bd9b151 | 2025-02-07 11:30:36 -0700 | [diff] [blame] | 690 | 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 Glass | 1f271dc | 2025-01-20 14:26:01 -0700 | [diff] [blame] | 696 | if (stats->skip_count) |
| 697 | printf("skipped: %d, ", stats->skip_count); |
| 698 | printf("failures: %d\n", stats->fail_count); |
| 699 | } |
| 700 | |
Simon Glass | 3869eb5 | 2025-01-20 14:25:26 -0700 | [diff] [blame] | 701 | int 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 Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 705 | { |
Simon Glass | 3869eb5 | 2025-01-20 14:25:26 -0700 | [diff] [blame] | 706 | ; |
Simon Glass | 53d1b19 | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 707 | bool has_dm_tests = false; |
Simon Glass | 5fa92d6 | 2025-02-07 11:30:35 -0700 | [diff] [blame] | 708 | ulong start_offset = 0; |
| 709 | ulong test_offset = 0; |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 710 | int ret; |
| 711 | |
Simon Glass | fb82a04 | 2025-01-20 14:26:02 -0700 | [diff] [blame] | 712 | memset(&uts->cur, '\0', sizeof(struct ut_stats)); |
Simon Glass | 5fa92d6 | 2025-02-07 11:30:35 -0700 | [diff] [blame] | 713 | if (CONFIG_IS_ENABLED(UNIT_TEST_DURATION)) { |
| 714 | uts->cur.start = get_timer(0); |
| 715 | start_offset = timer_test_get_offset(); |
| 716 | } |
Simon Glass | fb82a04 | 2025-01-20 14:26:02 -0700 | [diff] [blame] | 717 | |
Simon Glass | 1899e13 | 2021-03-07 17:35:07 -0700 | [diff] [blame] | 718 | if (!CONFIG_IS_ENABLED(OF_PLATDATA) && |
Simon Glass | 798b5c5 | 2024-10-19 09:21:56 -0600 | [diff] [blame] | 719 | ut_list_has_dm_tests(tests, count, prefix, select_name)) { |
Simon Glass | 53d1b19 | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 720 | has_dm_tests = true; |
Simon Glass | 1899e13 | 2021-03-07 17:35:07 -0700 | [diff] [blame] | 721 | /* |
| 722 | * If we have no device tree, or it only has a root node, then |
Simon Glass | 21cc05e | 2025-02-07 11:30:33 -0700 | [diff] [blame] | 723 | * these tests clearly aren't going to work... |
Simon Glass | 1899e13 | 2021-03-07 17:35:07 -0700 | [diff] [blame] | 724 | */ |
| 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 Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 732 | if (!select_name) |
| 733 | printf("Running %d %s tests\n", count, category); |
| 734 | |
Simon Glass | 3869eb5 | 2025-01-20 14:25:26 -0700 | [diff] [blame] | 735 | uts->of_root = gd_of_root(); |
| 736 | uts->runs_per_test = runs_per_test; |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 737 | if (fdt_action() == FDTCHK_COPY && gd->fdt_blob) { |
Simon Glass | 3869eb5 | 2025-01-20 14:25:26 -0700 | [diff] [blame] | 738 | uts->fdt_size = fdt_totalsize(gd->fdt_blob); |
| 739 | uts->fdt_copy = os_malloc(uts->fdt_size); |
| 740 | if (!uts->fdt_copy) { |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 741 | printf("Out of memory for device tree copy\n"); |
| 742 | return -ENOMEM; |
| 743 | } |
Simon Glass | 3869eb5 | 2025-01-20 14:25:26 -0700 | [diff] [blame] | 744 | memcpy(uts->fdt_copy, gd->fdt_blob, uts->fdt_size); |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 745 | } |
Simon Glass | 3869eb5 | 2025-01-20 14:25:26 -0700 | [diff] [blame] | 746 | uts->force_run = force_run; |
| 747 | ret = ut_run_tests(uts, prefix, tests, count, select_name, |
Simon Glass | 85ba7c3 | 2022-10-29 19:47:13 -0600 | [diff] [blame] | 748 | test_insert); |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 749 | |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 750 | /* Best efforts only...ignore errors */ |
| 751 | if (has_dm_tests) |
Simon Glass | 3869eb5 | 2025-01-20 14:25:26 -0700 | [diff] [blame] | 752 | dm_test_restore(uts->of_root); |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 753 | |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 754 | if (ret == -ENOENT) |
| 755 | printf("Test '%s' not found\n", select_name); |
Simon Glass | 5fa92d6 | 2025-02-07 11:30:35 -0700 | [diff] [blame] | 756 | 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 Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 762 | |
Simon Glass | fb82a04 | 2025-01-20 14:26:02 -0700 | [diff] [blame] | 763 | 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 Glass | 5fa92d6 | 2025-02-07 11:30:35 -0700 | [diff] [blame] | 766 | uts->total.duration_ms += uts->cur.duration_ms; |
Simon Glass | fb82a04 | 2025-01-20 14:26:02 -0700 | [diff] [blame] | 767 | uts->run_count++; |
| 768 | |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 769 | return ret; |
| 770 | } |