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 | 1880b8a | 2024-09-01 16:26:20 -0600 | [diff] [blame] | 17 | #include <usb.h> |
Simon Glass | ba8457b | 2022-09-06 20:27:19 -0600 | [diff] [blame] | 18 | #include <dm/ofnode.h> |
Simon Glass | d18f739 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 19 | #include <dm/root.h> |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 20 | #include <dm/test.h> |
Simon Glass | 5b7e55b | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 21 | #include <dm/uclass-internal.h> |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 22 | #include <test/test.h> |
Simon Glass | d18f739 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 23 | #include <test/ut.h> |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 24 | #include <u-boot/crc.h> |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 25 | |
Simon Glass | 0f8f677 | 2021-03-07 17:34:49 -0700 | [diff] [blame] | 26 | DECLARE_GLOBAL_DATA_PTR; |
| 27 | |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 28 | /** |
| 29 | * enum fdtchk_t - what to do with the device tree (gd->fdt_blob) |
| 30 | * |
| 31 | * This affects what happens with the device tree before and after a test |
| 32 | * |
| 33 | * @FDTCHK_NONE: Do nothing |
| 34 | * @FDTCHK_CHECKSUM: Take a checksum of the FDT before the test runs and |
| 35 | * compare it afterwards to detect any changes |
| 36 | * @FDTCHK_COPY: Make a copy of the FDT and restore it afterwards |
| 37 | */ |
| 38 | enum fdtchk_t { |
| 39 | FDTCHK_NONE, |
| 40 | FDTCHK_CHECKSUM, |
| 41 | FDTCHK_COPY, |
| 42 | }; |
| 43 | |
| 44 | /** |
| 45 | * fdt_action() - get the required action for the FDT |
| 46 | * |
| 47 | * @return the action that should be taken for this build |
| 48 | */ |
| 49 | static enum fdtchk_t fdt_action(void) |
| 50 | { |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 51 | /* For sandbox SPL builds, do nothing */ |
Simon Glass | 0e84d96 | 2024-09-29 19:49:50 -0600 | [diff] [blame] | 52 | if (IS_ENABLED(CONFIG_SANDBOX) && IS_ENABLED(CONFIG_XPL_BUILD)) |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 53 | return FDTCHK_NONE; |
| 54 | |
Simon Glass | 234f05b | 2023-02-22 09:34:12 -0700 | [diff] [blame] | 55 | /* Do a copy for sandbox (but only the U-Boot build, not SPL) */ |
| 56 | if (IS_ENABLED(CONFIG_SANDBOX)) |
| 57 | return FDTCHK_COPY; |
| 58 | |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 59 | /* For all other boards, do a checksum */ |
| 60 | return FDTCHK_CHECKSUM; |
| 61 | } |
| 62 | |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 63 | /* This is valid when a test is running, NULL otherwise */ |
| 64 | static struct unit_test_state *cur_test_state; |
| 65 | |
| 66 | struct unit_test_state *test_get_state(void) |
| 67 | { |
| 68 | return cur_test_state; |
| 69 | } |
| 70 | |
| 71 | void test_set_state(struct unit_test_state *uts) |
| 72 | { |
| 73 | cur_test_state = uts; |
| 74 | } |
| 75 | |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 76 | /** |
| 77 | * dm_test_pre_run() - Get ready to run a driver model test |
| 78 | * |
| 79 | * This clears out the driver model data structures. For sandbox it resets the |
| 80 | * state structure |
| 81 | * |
| 82 | * @uts: Test state |
| 83 | */ |
| 84 | static int dm_test_pre_run(struct unit_test_state *uts) |
| 85 | { |
| 86 | bool of_live = uts->of_live; |
| 87 | |
Simon Glass | 60f0899 | 2022-09-06 20:27:06 -0600 | [diff] [blame] | 88 | if (of_live && (gd->flags & GD_FLG_FDT_CHANGED)) { |
| 89 | printf("Cannot run live tree test as device tree changed\n"); |
| 90 | return -EFAULT; |
| 91 | } |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 92 | uts->root = NULL; |
| 93 | uts->testdev = NULL; |
| 94 | uts->force_fail_alloc = false; |
| 95 | uts->skip_post_probe = false; |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 96 | if (fdt_action() == FDTCHK_CHECKSUM) |
| 97 | uts->fdt_chksum = crc8(0, gd->fdt_blob, |
| 98 | fdt_totalsize(gd->fdt_blob)); |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 99 | gd->dm_root = NULL; |
Simon Glass | 1a3e39b | 2022-09-06 20:27:00 -0600 | [diff] [blame] | 100 | malloc_disable_testing(); |
Simon Glass | f700504 | 2021-07-05 16:32:43 -0600 | [diff] [blame] | 101 | if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA)) |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 102 | memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count)); |
Simon Glass | 45b807d | 2021-03-25 10:44:33 +1300 | [diff] [blame] | 103 | arch_reset_for_test(); |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 104 | |
| 105 | /* Determine whether to make the live tree available */ |
| 106 | gd_set_of_root(of_live ? uts->of_root : NULL); |
Simon Glass | ba8457b | 2022-09-06 20:27:19 -0600 | [diff] [blame] | 107 | oftree_reset(); |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 108 | ut_assertok(dm_init(of_live)); |
| 109 | uts->root = dm_root(); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
Simon Glass | 5b7e55b | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 114 | static int dm_test_post_run(struct unit_test_state *uts) |
| 115 | { |
| 116 | int id; |
| 117 | |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 118 | if (gd->fdt_blob) { |
| 119 | switch (fdt_action()) { |
| 120 | case FDTCHK_COPY: |
| 121 | memcpy((void *)gd->fdt_blob, uts->fdt_copy, uts->fdt_size); |
| 122 | break; |
| 123 | case FDTCHK_CHECKSUM: { |
| 124 | uint chksum; |
| 125 | |
| 126 | chksum = crc8(0, gd->fdt_blob, fdt_totalsize(gd->fdt_blob)); |
Simon Glass | 60f0899 | 2022-09-06 20:27:06 -0600 | [diff] [blame] | 127 | if (chksum != uts->fdt_chksum) { |
| 128 | /* |
| 129 | * We cannot run any more tests that need the |
| 130 | * live tree, since its strings point into the |
| 131 | * flat tree, which has changed. This likely |
| 132 | * means that at least some of the pointers from |
| 133 | * the live tree point to different things |
| 134 | */ |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 135 | printf("Device tree changed: cannot run live tree tests\n"); |
Simon Glass | 60f0899 | 2022-09-06 20:27:06 -0600 | [diff] [blame] | 136 | gd->flags |= GD_FLG_FDT_CHANGED; |
| 137 | } |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 138 | break; |
| 139 | } |
| 140 | case FDTCHK_NONE: |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | |
Simon Glass | 96113c1 | 2021-03-15 17:25:21 +1300 | [diff] [blame] | 145 | /* |
| 146 | * With of-platdata-inst the uclasses are created at build time. If we |
| 147 | * destroy them we cannot get them back since uclass_add() is not |
| 148 | * supported. So skip this. |
| 149 | */ |
| 150 | if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) { |
| 151 | for (id = 0; id < UCLASS_COUNT; id++) { |
| 152 | struct uclass *uc; |
Simon Glass | 5b7e55b | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 153 | |
Simon Glass | 96113c1 | 2021-03-15 17:25:21 +1300 | [diff] [blame] | 154 | /* |
| 155 | * If the uclass doesn't exist we don't want to create |
| 156 | * it. So check that here before we call |
| 157 | * uclass_find_device(). |
| 158 | */ |
| 159 | uc = uclass_find(id); |
| 160 | if (!uc) |
| 161 | continue; |
| 162 | ut_assertok(uclass_destroy(uc)); |
| 163 | } |
Simon Glass | 5b7e55b | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
Simon Glass | 242357c | 2021-03-07 17:34:51 -0700 | [diff] [blame] | 169 | /* Ensure all the test devices are probed */ |
| 170 | static int do_autoprobe(struct unit_test_state *uts) |
| 171 | { |
Michal Suchanek | 44322b5 | 2022-10-12 21:57:51 +0200 | [diff] [blame] | 172 | return uclass_probe_all(UCLASS_TEST); |
Simon Glass | 242357c | 2021-03-07 17:34:51 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 175 | /* |
| 176 | * ut_test_run_on_flattree() - Check if we should run a test with flat DT |
| 177 | * |
| 178 | * This skips long/slow tests where there is not much value in running a flat |
| 179 | * DT test in addition to a live DT test. |
| 180 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 181 | * Return: true to run the given test on the flat device tree |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 182 | */ |
| 183 | static bool ut_test_run_on_flattree(struct unit_test *test) |
| 184 | { |
| 185 | const char *fname = strrchr(test->file, '/') + 1; |
| 186 | |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 187 | if (!(test->flags & UTF_DM)) |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 188 | return false; |
| 189 | |
| 190 | return !strstr(fname, "video") || strstr(test->name, "video_base"); |
| 191 | } |
| 192 | |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 193 | /** |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 194 | * test_matches() - Check if a test should be run |
| 195 | * |
| 196 | * This checks if the a test should be run. In the normal case of running all |
| 197 | * tests, @select_name is NULL. |
| 198 | * |
| 199 | * @prefix: String prefix for the tests. Any tests that have this prefix will be |
| 200 | * 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] | 201 | * of the test name. If NULL, any suite name (xxx_test) is considered to be |
| 202 | * a prefix. |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 203 | * @test_name: Name of current test |
| 204 | * @select_name: Name of test to run (or NULL for all) |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 205 | * Return: true to run this test, false to skip it |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 206 | */ |
| 207 | static bool test_matches(const char *prefix, const char *test_name, |
| 208 | const char *select_name) |
| 209 | { |
Andy Shevchenko | d91cfbb | 2021-02-11 16:40:10 +0200 | [diff] [blame] | 210 | size_t len; |
| 211 | |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 212 | if (!select_name) |
| 213 | return true; |
| 214 | |
Andy Shevchenko | d91cfbb | 2021-02-11 16:40:10 +0200 | [diff] [blame] | 215 | /* Allow glob expansion in the test name */ |
| 216 | len = select_name[strlen(select_name) - 1] == '*' ? strlen(select_name) : 0; |
| 217 | if (len-- == 1) |
| 218 | return true; |
| 219 | |
| 220 | if (!strncmp(test_name, select_name, len)) |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 221 | return true; |
| 222 | |
Andy Shevchenko | 00d4290 | 2021-02-11 16:40:11 +0200 | [diff] [blame] | 223 | if (prefix) { |
| 224 | /* All tests have this prefix */ |
| 225 | if (!strncmp(test_name, prefix, strlen(prefix))) |
| 226 | test_name += strlen(prefix); |
| 227 | } else { |
Simon Glass | 1ef74ab | 2021-03-07 17:35:12 -0700 | [diff] [blame] | 228 | const char *p = strstr(test_name, "_test_"); |
| 229 | |
| 230 | /* convert xxx_test_yyy to yyy, i.e. remove the suite name */ |
| 231 | if (p) |
Andy Shevchenko | 00d4290 | 2021-02-11 16:40:11 +0200 | [diff] [blame] | 232 | test_name = p + strlen("_test_"); |
Simon Glass | 1ef74ab | 2021-03-07 17:35:12 -0700 | [diff] [blame] | 233 | } |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 234 | |
Andy Shevchenko | d91cfbb | 2021-02-11 16:40:10 +0200 | [diff] [blame] | 235 | if (!strncmp(test_name, select_name, len)) |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 236 | return true; |
| 237 | |
| 238 | return false; |
| 239 | } |
| 240 | |
Simon Glass | 53d1b19 | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 241 | /** |
Simon Glass | 1899e13 | 2021-03-07 17:35:07 -0700 | [diff] [blame] | 242 | * ut_list_has_dm_tests() - Check if a list of tests has driver model ones |
| 243 | * |
| 244 | * @tests: List of tests to run |
Simon Glass | 798b5c5 | 2024-10-19 09:21:56 -0600 | [diff] [blame] | 245 | * @count: Number of tests to run |
| 246 | * @prefix: String prefix for the tests. Any tests that have this prefix will be |
| 247 | * printed without the prefix, so that it is easier to see the unique part |
| 248 | * of the test name. If NULL, no prefix processing is done |
| 249 | * @select_name: Name of a single test being run (from the list provided). If |
| 250 | * NULL all tests are being run |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 251 | * Return: true if any of the tests have the UTF_DM flag |
Simon Glass | 1899e13 | 2021-03-07 17:35:07 -0700 | [diff] [blame] | 252 | */ |
Simon Glass | 798b5c5 | 2024-10-19 09:21:56 -0600 | [diff] [blame] | 253 | static bool ut_list_has_dm_tests(struct unit_test *tests, int count, |
| 254 | const char *prefix, const char *select_name) |
Simon Glass | 1899e13 | 2021-03-07 17:35:07 -0700 | [diff] [blame] | 255 | { |
| 256 | struct unit_test *test; |
| 257 | |
| 258 | for (test = tests; test < tests + count; test++) { |
Simon Glass | 798b5c5 | 2024-10-19 09:21:56 -0600 | [diff] [blame] | 259 | if (test_matches(prefix, test->name, select_name) && |
| 260 | (test->flags & UTF_DM)) |
Simon Glass | 1899e13 | 2021-03-07 17:35:07 -0700 | [diff] [blame] | 261 | return true; |
| 262 | } |
| 263 | |
| 264 | return false; |
| 265 | } |
| 266 | |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 267 | /** |
Simon Glass | 53d1b19 | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 268 | * dm_test_restore() Put things back to normal so sandbox works as expected |
| 269 | * |
| 270 | * @of_root: Value to set for of_root |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 271 | * Return: 0 if OK, -ve on error |
Simon Glass | 53d1b19 | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 272 | */ |
| 273 | static int dm_test_restore(struct device_node *of_root) |
| 274 | { |
| 275 | int ret; |
| 276 | |
| 277 | gd_set_of_root(of_root); |
| 278 | gd->dm_root = NULL; |
| 279 | ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE)); |
| 280 | if (ret) |
| 281 | return ret; |
| 282 | dm_scan_plat(false); |
| 283 | if (!CONFIG_IS_ENABLED(OF_PLATDATA)) |
AKASHI Takahiro | 16f5147 | 2023-06-08 09:55:59 +0900 | [diff] [blame] | 284 | dm_extended_scan(false); |
Simon Glass | 53d1b19 | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | /** |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 290 | * test_pre_run() - Handle any preparation needed to run a test |
| 291 | * |
| 292 | * @uts: Test state |
| 293 | * @test: Test to prepare for |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 294 | * 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] | 295 | * available, other -ve on error (meaning that testing cannot likely |
| 296 | * continue) |
| 297 | */ |
| 298 | 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] | 299 | { |
Simon Glass | a128b1b | 2022-03-04 08:43:01 -0700 | [diff] [blame] | 300 | ut_assertok(event_init()); |
| 301 | |
Simon Glass | 1880b8a | 2024-09-01 16:26:20 -0600 | [diff] [blame] | 302 | /* |
| 303 | * Remove any USB keyboard, so that we can add and remove USB devices |
| 304 | * in tests. |
| 305 | * |
Simon Glass | b0fd7a2 | 2024-10-14 14:17:53 -0600 | [diff] [blame] | 306 | * For UTF_DM tests, the old driver model state is saved and |
Simon Glass | 1880b8a | 2024-09-01 16:26:20 -0600 | [diff] [blame] | 307 | * restored across each test. Within in each test there is therefore a |
| 308 | * new driver model state, which means that any USB keyboard device in |
| 309 | * stdio points to the old state. |
| 310 | * |
Simon Glass | b0fd7a2 | 2024-10-14 14:17:53 -0600 | [diff] [blame] | 311 | * 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] | 312 | * USB (thus creating a stdio record pointing to the USB keyboard |
| 313 | * device) then when the test finishes, the new driver model state is |
| 314 | * freed, meaning that there is now a stale pointer in stdio. |
| 315 | * |
Simon Glass | b0fd7a2 | 2024-10-14 14:17:53 -0600 | [diff] [blame] | 316 | * This means that any future UTF_DM test which uses stdin will |
Simon Glass | 1880b8a | 2024-09-01 16:26:20 -0600 | [diff] [blame] | 317 | * cause the console system to call tstc() on the stale device pointer, |
| 318 | * causing a crash. |
| 319 | * |
Simon Glass | b0fd7a2 | 2024-10-14 14:17:53 -0600 | [diff] [blame] | 320 | * 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] | 321 | * this causes other problems. For example, bootflow_efi relies on |
| 322 | * U-Boot going through a proper init - without that we don't have the |
| 323 | * TCG measurement working and get an error |
| 324 | * 'tcg2 measurement fails(0x8000000000000007)'. Once we tidy up how EFI |
| 325 | * 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] | 326 | * potentially make the bootstd tests set UTF_DM, but other tests |
Simon Glass | 1880b8a | 2024-09-01 16:26:20 -0600 | [diff] [blame] | 327 | * might do the same thing. |
| 328 | * |
| 329 | * We could add a test flag to declare that USB is being used, but that |
| 330 | * seems unnecessary, at least for now. We could detect USB being used |
| 331 | * in a test, but there is no obvious drawback to clearing out stale |
| 332 | * pointers always. |
| 333 | * |
| 334 | * So just remove any USB keyboards from the console tables. This allows |
Simon Glass | b0fd7a2 | 2024-10-14 14:17:53 -0600 | [diff] [blame] | 335 | * UTF_DM and non-UTF_DM tests to coexist happily. |
Simon Glass | 1880b8a | 2024-09-01 16:26:20 -0600 | [diff] [blame] | 336 | */ |
| 337 | usb_kbd_remove_for_test(); |
| 338 | |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 339 | if (test->flags & UTF_DM) |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 340 | ut_assertok(dm_test_pre_run(uts)); |
Simon Glass | b2890a1 | 2021-03-07 17:34:56 -0700 | [diff] [blame] | 341 | |
Simon Glass | 59cad96 | 2021-03-07 17:34:55 -0700 | [diff] [blame] | 342 | ut_set_skip_delays(uts, false); |
| 343 | |
Simon Glass | 86a5bd0 | 2021-03-07 17:34:53 -0700 | [diff] [blame] | 344 | uts->start = mallinfo(); |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 345 | |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 346 | if (test->flags & UTF_SCAN_PDATA) |
Simon Glass | 177e0fd | 2021-03-07 17:34:52 -0700 | [diff] [blame] | 347 | ut_assertok(dm_scan_plat(false)); |
| 348 | |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 349 | if (test->flags & UTF_PROBE_TEST) |
Simon Glass | 242357c | 2021-03-07 17:34:51 -0700 | [diff] [blame] | 350 | ut_assertok(do_autoprobe(uts)); |
| 351 | |
Sean Anderson | 7bf2f8c | 2023-10-14 16:47:46 -0400 | [diff] [blame] | 352 | if (CONFIG_IS_ENABLED(OF_REAL) && |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 353 | (test->flags & UTF_SCAN_FDT)) { |
Simon Glass | b490f5f | 2023-01-17 10:47:28 -0700 | [diff] [blame] | 354 | /* |
| 355 | * only set this if we know the ethernet uclass will be created |
| 356 | */ |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 357 | eth_set_enable_bootdevs(test->flags & UTF_ETH_BOOTDEV); |
| 358 | test_sf_set_enable_bootdevs(test->flags & UTF_SF_BOOTDEV); |
Simon Glass | d18f739 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 359 | ut_assertok(dm_extended_scan(false)); |
Simon Glass | b490f5f | 2023-01-17 10:47:28 -0700 | [diff] [blame] | 360 | } |
Simon Glass | d18f739 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 361 | |
Simon Glass | b9ff648 | 2023-01-17 10:47:23 -0700 | [diff] [blame] | 362 | /* |
| 363 | * Do this after FDT scan since dm_scan_other() in bootstd-uclass.c |
| 364 | * checks for the existence of bootstd |
| 365 | */ |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 366 | if (test->flags & UTF_SCAN_PDATA) |
Simon Glass | b9ff648 | 2023-01-17 10:47:23 -0700 | [diff] [blame] | 367 | ut_assertok(dm_scan_other(false)); |
| 368 | |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 369 | if (IS_ENABLED(CONFIG_SANDBOX) && (test->flags & UTF_OTHER_FDT)) { |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 370 | /* make sure the other FDT is available */ |
| 371 | ut_assertok(test_load_other_fdt(uts)); |
| 372 | |
| 373 | /* |
| 374 | * create a new live tree with it for every test, in case a |
| 375 | * test modifies the tree |
| 376 | */ |
| 377 | if (of_live_active()) { |
| 378 | ut_assertok(unflatten_device_tree(uts->other_fdt, |
| 379 | &uts->of_other)); |
| 380 | } |
| 381 | } |
| 382 | |
Simon Glass | 11fcfa3 | 2024-08-22 07:57:50 -0600 | [diff] [blame] | 383 | if (test->flags & UTF_CONSOLE) { |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 384 | int ret = console_record_reset_enable(); |
| 385 | |
| 386 | if (ret) { |
| 387 | printf("Skipping: Console recording disabled\n"); |
| 388 | return -EAGAIN; |
| 389 | } |
| 390 | } |
Simon Glass | be277ee | 2024-10-28 13:47:53 +0100 | [diff] [blame^] | 391 | if (test->flags & UFT_BLOBLIST) { |
| 392 | log_debug("save bloblist %p\n", gd_bloblist()); |
| 393 | uts->old_bloblist = gd_bloblist(); |
| 394 | gd_set_bloblist(NULL); |
| 395 | } |
| 396 | |
Simon Glass | 2b566b9 | 2021-03-07 17:34:54 -0700 | [diff] [blame] | 397 | ut_silence_console(uts); |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 402 | /** |
| 403 | * test_post_run() - Handle cleaning up after a test |
| 404 | * |
| 405 | * @uts: Test state |
| 406 | * @test: Test to clean up after |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 407 | * 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] | 408 | */ |
| 409 | 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] | 410 | { |
Simon Glass | 2b566b9 | 2021-03-07 17:34:54 -0700 | [diff] [blame] | 411 | ut_unsilence_console(uts); |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 412 | if (test->flags & UTF_DM) |
Simon Glass | 5b7e55b | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 413 | ut_assertok(dm_test_post_run(uts)); |
Rasmus Villemoes | c794e49 | 2022-10-28 13:50:54 +0200 | [diff] [blame] | 414 | ut_assertok(cyclic_unregister_all()); |
Simon Glass | a128b1b | 2022-03-04 08:43:01 -0700 | [diff] [blame] | 415 | ut_assertok(event_uninit()); |
Simon Glass | 0f8f677 | 2021-03-07 17:34:49 -0700 | [diff] [blame] | 416 | |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 417 | free(uts->of_other); |
| 418 | uts->of_other = NULL; |
| 419 | |
Simon Glass | be277ee | 2024-10-28 13:47:53 +0100 | [diff] [blame^] | 420 | if (test->flags & UFT_BLOBLIST) { |
| 421 | gd_set_bloblist(uts->old_bloblist); |
| 422 | log_debug("restore bloblist %p\n", gd_bloblist()); |
| 423 | } |
| 424 | |
Simon Glass | 76c6269 | 2022-10-29 19:47:08 -0600 | [diff] [blame] | 425 | blkcache_free(); |
| 426 | |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 427 | return 0; |
| 428 | } |
| 429 | |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 430 | /** |
Simon Glass | 816fe6c | 2022-10-20 18:22:48 -0600 | [diff] [blame] | 431 | * skip_test() - Handle skipping a test |
| 432 | * |
| 433 | * @uts: Test state to update |
| 434 | * @return -EAGAIN (always) |
| 435 | */ |
| 436 | static int skip_test(struct unit_test_state *uts) |
| 437 | { |
| 438 | uts->skip_count++; |
| 439 | |
| 440 | return -EAGAIN; |
| 441 | } |
| 442 | |
| 443 | /** |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 444 | * ut_run_test() - Run a single test |
| 445 | * |
| 446 | * This runs the test, handling any preparation and clean-up needed. It prints |
| 447 | * the name of each test before running it. |
| 448 | * |
| 449 | * @uts: Test state to update. The caller should ensure that this is zeroed for |
| 450 | * the first call to this function. On exit, @uts->fail_count is |
| 451 | * incremented by the number of failures (0, one hopes) |
| 452 | * @test_name: Test to run |
| 453 | * @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] | 454 | * 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] | 455 | * any failed |
| 456 | */ |
| 457 | static int ut_run_test(struct unit_test_state *uts, struct unit_test *test, |
| 458 | const char *test_name) |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 459 | { |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 460 | const char *fname = strrchr(test->file, '/') + 1; |
| 461 | const char *note = ""; |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 462 | int ret; |
| 463 | |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 464 | if ((test->flags & UTF_DM) && !uts->of_live) |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 465 | note = " (flat tree)"; |
| 466 | printf("Test: %s: %s%s\n", test_name, fname, note); |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 467 | |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 468 | /* Allow access to test state from drivers */ |
| 469 | test_set_state(uts); |
| 470 | |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 471 | ret = test_pre_run(uts, test); |
| 472 | if (ret == -EAGAIN) |
Simon Glass | 816fe6c | 2022-10-20 18:22:48 -0600 | [diff] [blame] | 473 | return skip_test(uts); |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 474 | if (ret) |
| 475 | return ret; |
| 476 | |
Simon Glass | 816fe6c | 2022-10-20 18:22:48 -0600 | [diff] [blame] | 477 | ret = test->func(uts); |
| 478 | if (ret == -EAGAIN) |
| 479 | skip_test(uts); |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 480 | |
| 481 | ret = test_post_run(uts, test); |
| 482 | if (ret) |
| 483 | return ret; |
| 484 | |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 485 | test_set_state( NULL); |
| 486 | |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 487 | return 0; |
| 488 | } |
| 489 | |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 490 | /** |
| 491 | * ut_run_test_live_flat() - Run a test with both live and flat tree |
| 492 | * |
| 493 | * This calls ut_run_test() with livetree enabled, which is the standard setup |
| 494 | * for runnig tests. Then, for driver model test, it calls it again with |
| 495 | * livetree disabled. This allows checking of flattree being used when OF_LIVE |
| 496 | * is enabled, as is the case in U-Boot proper before relocation, as well as in |
| 497 | * SPL. |
| 498 | * |
| 499 | * @uts: Test state to update. The caller should ensure that this is zeroed for |
| 500 | * the first call to this function. On exit, @uts->fail_count is |
| 501 | * incremented by the number of failures (0, one hopes) |
| 502 | * @test: Test to run |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 503 | * 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] | 504 | * any failed |
| 505 | */ |
| 506 | static int ut_run_test_live_flat(struct unit_test_state *uts, |
Simon Glass | 0f7eb63 | 2022-10-29 19:47:09 -0600 | [diff] [blame] | 507 | struct unit_test *test) |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 508 | { |
Simon Glass | 94b3583 | 2024-10-09 18:28:59 -0600 | [diff] [blame] | 509 | int runs, ret; |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 510 | |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 511 | if ((test->flags & UTF_OTHER_FDT) && !IS_ENABLED(CONFIG_SANDBOX)) |
Simon Glass | 816fe6c | 2022-10-20 18:22:48 -0600 | [diff] [blame] | 512 | return skip_test(uts); |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 513 | |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 514 | /* Run with the live tree if possible */ |
| 515 | runs = 0; |
| 516 | if (CONFIG_IS_ENABLED(OF_LIVE)) { |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 517 | if (!(test->flags & UTF_FLAT_TREE)) { |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 518 | uts->of_live = true; |
Simon Glass | 94b3583 | 2024-10-09 18:28:59 -0600 | [diff] [blame] | 519 | ret = ut_run_test(uts, test, test->name); |
| 520 | if (ret != -EAGAIN) { |
| 521 | ut_assertok(ret); |
| 522 | runs++; |
| 523 | } |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
| 527 | /* |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 528 | * Run with the flat tree if: |
| 529 | * - it is not marked for live tree only |
| 530 | * - it doesn't require the 'other' FDT when OFNODE_MULTI_TREE_MAX is |
| 531 | * not enabled (since flat tree can only support a single FDT in that |
| 532 | * case |
| 533 | * - we couldn't run it with live tree, |
| 534 | * - it is a core test (dm tests except video) |
| 535 | * - the FDT is still valid and has not been updated by an earlier test |
| 536 | * (for sandbox we handle this by copying the tree, but not for other |
| 537 | * boards) |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 538 | */ |
Sean Anderson | 5765fb1 | 2023-09-29 12:06:54 -0400 | [diff] [blame] | 539 | if ((!CONFIG_IS_ENABLED(OF_LIVE) || |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 540 | (test->flags & UTF_SCAN_FDT)) && |
| 541 | !(test->flags & UTF_LIVE_TREE) && |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 542 | (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) || |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 543 | !(test->flags & UTF_OTHER_FDT)) && |
Simon Glass | 60f0899 | 2022-09-06 20:27:06 -0600 | [diff] [blame] | 544 | (!runs || ut_test_run_on_flattree(test)) && |
| 545 | !(gd->flags & GD_FLG_FDT_CHANGED)) { |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 546 | uts->of_live = false; |
Simon Glass | 94b3583 | 2024-10-09 18:28:59 -0600 | [diff] [blame] | 547 | ret = ut_run_test(uts, test, test->name); |
| 548 | if (ret != -EAGAIN) { |
| 549 | ut_assertok(ret); |
| 550 | runs++; |
| 551 | } |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 557 | /** |
| 558 | * ut_run_tests() - Run a set of tests |
| 559 | * |
| 560 | * This runs the tests, handling any preparation and clean-up needed. It prints |
| 561 | * the name of each test before running it. |
| 562 | * |
| 563 | * @uts: Test state to update. The caller should ensure that this is zeroed for |
| 564 | * the first call to this function. On exit, @uts->fail_count is |
| 565 | * incremented by the number of failures (0, one hopes) |
| 566 | * @prefix: String prefix for the tests. Any tests that have this prefix will be |
| 567 | * printed without the prefix, so that it is easier to see the unique part |
| 568 | * of the test name. If NULL, no prefix processing is done |
| 569 | * @tests: List of tests to run |
| 570 | * @count: Number of tests to run |
| 571 | * @select_name: Name of a single test to run (from the list provided). If NULL |
| 572 | * then all tests are run |
Simon Glass | 798b5c5 | 2024-10-19 09:21:56 -0600 | [diff] [blame] | 573 | * @test_insert: String describing a test to run after n other tests run, in the |
| 574 | * format n:name where n is the number of tests to run before this one and |
| 575 | * name is the name of the test to run |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 576 | * 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] | 577 | * -EBADF if any failed |
| 578 | */ |
| 579 | static int ut_run_tests(struct unit_test_state *uts, const char *prefix, |
| 580 | struct unit_test *tests, int count, |
Simon Glass | 85ba7c3 | 2022-10-29 19:47:13 -0600 | [diff] [blame] | 581 | const char *select_name, const char *test_insert) |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 582 | { |
Simon Glass | 85ba7c3 | 2022-10-29 19:47:13 -0600 | [diff] [blame] | 583 | struct unit_test *test, *one; |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 584 | int found = 0; |
Simon Glass | 85ba7c3 | 2022-10-29 19:47:13 -0600 | [diff] [blame] | 585 | int pos = 0; |
| 586 | int upto; |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 587 | |
Simon Glass | 85ba7c3 | 2022-10-29 19:47:13 -0600 | [diff] [blame] | 588 | one = NULL; |
| 589 | if (test_insert) { |
| 590 | char *p; |
| 591 | |
| 592 | pos = dectoul(test_insert, NULL); |
| 593 | p = strchr(test_insert, ':'); |
| 594 | if (p) |
| 595 | p++; |
| 596 | |
| 597 | for (test = tests; test < tests + count; test++) { |
| 598 | if (!strcmp(p, test->name)) |
| 599 | one = test; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | for (upto = 0, test = tests; test < tests + count; test++, upto++) { |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 604 | const char *test_name = test->name; |
Simon Glass | 91a187b | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 605 | int ret, i, old_fail_count; |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 606 | |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 607 | if (!test_matches(prefix, test_name, select_name)) |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 608 | continue; |
Simon Glass | 1f1614b | 2022-10-20 18:22:50 -0600 | [diff] [blame] | 609 | |
Simon Glass | 1a92f83 | 2024-08-22 07:57:48 -0600 | [diff] [blame] | 610 | if (test->flags & UTF_MANUAL) { |
Simon Glass | 1f1614b | 2022-10-20 18:22:50 -0600 | [diff] [blame] | 611 | int len; |
| 612 | |
| 613 | /* |
| 614 | * manual tests must have a name ending "_norun" as this |
| 615 | * is how pytest knows to skip them. See |
| 616 | * generate_ut_subtest() for this check. |
| 617 | */ |
| 618 | len = strlen(test_name); |
| 619 | if (len < 6 || strcmp(test_name + len - 6, "_norun")) { |
| 620 | printf("Test %s is manual so must have a name ending in _norun\n", |
| 621 | test_name); |
| 622 | uts->fail_count++; |
| 623 | return -EBADF; |
| 624 | } |
| 625 | if (!uts->force_run) { |
| 626 | if (select_name) { |
| 627 | printf("Test %s skipped as it is manual (use -f to run it)\n", |
| 628 | test_name); |
| 629 | } |
| 630 | continue; |
| 631 | } |
| 632 | } |
Simon Glass | 91a187b | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 633 | old_fail_count = uts->fail_count; |
Simon Glass | 85ba7c3 | 2022-10-29 19:47:13 -0600 | [diff] [blame] | 634 | |
| 635 | if (one && upto == pos) { |
| 636 | ret = ut_run_test_live_flat(uts, one); |
| 637 | if (uts->fail_count != old_fail_count) { |
| 638 | printf("Test %s failed %d times (position %d)\n", |
| 639 | one->name, |
| 640 | uts->fail_count - old_fail_count, pos); |
| 641 | } |
| 642 | return -EBADF; |
| 643 | } |
| 644 | |
Simon Glass | 91a187b | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 645 | for (i = 0; i < uts->runs_per_test; i++) |
Simon Glass | 0f7eb63 | 2022-10-29 19:47:09 -0600 | [diff] [blame] | 646 | ret = ut_run_test_live_flat(uts, test); |
Simon Glass | 91a187b | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 647 | if (uts->fail_count != old_fail_count) { |
| 648 | printf("Test %s failed %d times\n", select_name, |
| 649 | uts->fail_count - old_fail_count); |
| 650 | } |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 651 | found++; |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 652 | if (ret == -EAGAIN) |
| 653 | continue; |
| 654 | if (ret) |
| 655 | return ret; |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 656 | } |
| 657 | if (select_name && !found) |
| 658 | return -ENOENT; |
| 659 | |
| 660 | return uts->fail_count ? -EBADF : 0; |
| 661 | } |
| 662 | |
| 663 | int ut_run_list(const char *category, const char *prefix, |
Simon Glass | 91a187b | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 664 | struct unit_test *tests, int count, const char *select_name, |
Simon Glass | 85ba7c3 | 2022-10-29 19:47:13 -0600 | [diff] [blame] | 665 | int runs_per_test, bool force_run, const char *test_insert) |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 666 | { |
| 667 | struct unit_test_state uts = { .fail_count = 0 }; |
Simon Glass | 53d1b19 | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 668 | bool has_dm_tests = false; |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 669 | int ret; |
| 670 | |
Simon Glass | 1899e13 | 2021-03-07 17:35:07 -0700 | [diff] [blame] | 671 | if (!CONFIG_IS_ENABLED(OF_PLATDATA) && |
Simon Glass | 798b5c5 | 2024-10-19 09:21:56 -0600 | [diff] [blame] | 672 | ut_list_has_dm_tests(tests, count, prefix, select_name)) { |
Simon Glass | 53d1b19 | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 673 | has_dm_tests = true; |
Simon Glass | 1899e13 | 2021-03-07 17:35:07 -0700 | [diff] [blame] | 674 | /* |
| 675 | * If we have no device tree, or it only has a root node, then |
| 676 | * these * tests clearly aren't going to work... |
| 677 | */ |
| 678 | if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) { |
| 679 | puts("Please run with test device tree:\n" |
| 680 | " ./u-boot -d arch/sandbox/dts/test.dtb\n"); |
| 681 | return CMD_RET_FAILURE; |
| 682 | } |
| 683 | } |
| 684 | |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 685 | if (!select_name) |
| 686 | printf("Running %d %s tests\n", count, category); |
| 687 | |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 688 | uts.of_root = gd_of_root(); |
Simon Glass | 91a187b | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 689 | uts.runs_per_test = runs_per_test; |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 690 | if (fdt_action() == FDTCHK_COPY && gd->fdt_blob) { |
| 691 | uts.fdt_size = fdt_totalsize(gd->fdt_blob); |
| 692 | uts.fdt_copy = os_malloc(uts.fdt_size); |
| 693 | if (!uts.fdt_copy) { |
| 694 | printf("Out of memory for device tree copy\n"); |
| 695 | return -ENOMEM; |
| 696 | } |
| 697 | memcpy(uts.fdt_copy, gd->fdt_blob, uts.fdt_size); |
| 698 | } |
Simon Glass | 1f1614b | 2022-10-20 18:22:50 -0600 | [diff] [blame] | 699 | uts.force_run = force_run; |
Simon Glass | 85ba7c3 | 2022-10-29 19:47:13 -0600 | [diff] [blame] | 700 | ret = ut_run_tests(&uts, prefix, tests, count, select_name, |
| 701 | test_insert); |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 702 | |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 703 | /* Best efforts only...ignore errors */ |
| 704 | if (has_dm_tests) |
| 705 | dm_test_restore(uts.of_root); |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 706 | if (IS_ENABLED(CONFIG_SANDBOX)) { |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 707 | os_free(uts.fdt_copy); |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 708 | os_free(uts.other_fdt); |
| 709 | } |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 710 | |
Simon Glass | 816fe6c | 2022-10-20 18:22:48 -0600 | [diff] [blame] | 711 | if (uts.skip_count) |
| 712 | printf("Skipped: %d, ", uts.skip_count); |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 713 | if (ret == -ENOENT) |
| 714 | printf("Test '%s' not found\n", select_name); |
| 715 | else |
| 716 | printf("Failures: %d\n", uts.fail_count); |
| 717 | |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 718 | return ret; |
| 719 | } |