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