Simon Glass | 1c72175 | 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 | |
| 7 | #include <common.h> |
Simon Glass | 5ea894a | 2022-10-29 19:47:08 -0600 | [diff] [blame] | 8 | #include <blk.h> |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 9 | #include <console.h> |
Stefan Roese | af042c2 | 2022-09-02 13:57:54 +0200 | [diff] [blame] | 10 | #include <cyclic.h> |
Simon Glass | d8ed234 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 11 | #include <dm.h> |
Simon Glass | 7d02645 | 2022-03-04 08:43:01 -0700 | [diff] [blame] | 12 | #include <event.h> |
Simon Glass | 70dd886 | 2023-01-17 10:47:28 -0700 | [diff] [blame] | 13 | #include <net.h> |
Simon Glass | 8d468a1 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 14 | #include <of_live.h> |
Simon Glass | 0e4b697 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 15 | #include <os.h> |
Simon Glass | ee88ba7 | 2022-09-06 20:27:19 -0600 | [diff] [blame] | 16 | #include <dm/ofnode.h> |
Simon Glass | d8ed234 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 17 | #include <dm/root.h> |
Simon Glass | c79705e | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 18 | #include <dm/test.h> |
Simon Glass | e77615d | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 19 | #include <dm/uclass-internal.h> |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 20 | #include <test/test.h> |
Simon Glass | d8ed234 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 21 | #include <test/ut.h> |
Simon Glass | 0e4b697 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 22 | #include <u-boot/crc.h> |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 23 | |
Simon Glass | 30a0d20 | 2021-03-07 17:34:49 -0700 | [diff] [blame] | 24 | DECLARE_GLOBAL_DATA_PTR; |
| 25 | |
Simon Glass | 0e4b697 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 26 | /** |
| 27 | * enum fdtchk_t - what to do with the device tree (gd->fdt_blob) |
| 28 | * |
| 29 | * This affects what happens with the device tree before and after a test |
| 30 | * |
| 31 | * @FDTCHK_NONE: Do nothing |
| 32 | * @FDTCHK_CHECKSUM: Take a checksum of the FDT before the test runs and |
| 33 | * compare it afterwards to detect any changes |
| 34 | * @FDTCHK_COPY: Make a copy of the FDT and restore it afterwards |
| 35 | */ |
| 36 | enum fdtchk_t { |
| 37 | FDTCHK_NONE, |
| 38 | FDTCHK_CHECKSUM, |
| 39 | FDTCHK_COPY, |
| 40 | }; |
| 41 | |
| 42 | /** |
| 43 | * fdt_action() - get the required action for the FDT |
| 44 | * |
| 45 | * @return the action that should be taken for this build |
| 46 | */ |
| 47 | static enum fdtchk_t fdt_action(void) |
| 48 | { |
Simon Glass | 0e4b697 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 49 | /* For sandbox SPL builds, do nothing */ |
Simon Glass | d577459 | 2023-02-22 09:34:12 -0700 | [diff] [blame] | 50 | if (IS_ENABLED(CONFIG_SANDBOX) && IS_ENABLED(CONFIG_SPL_BUILD)) |
Simon Glass | 0e4b697 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 51 | return FDTCHK_NONE; |
| 52 | |
Simon Glass | d577459 | 2023-02-22 09:34:12 -0700 | [diff] [blame] | 53 | /* Do a copy for sandbox (but only the U-Boot build, not SPL) */ |
| 54 | if (IS_ENABLED(CONFIG_SANDBOX)) |
| 55 | return FDTCHK_COPY; |
| 56 | |
Simon Glass | 0e4b697 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 57 | /* For all other boards, do a checksum */ |
| 58 | return FDTCHK_CHECKSUM; |
| 59 | } |
| 60 | |
Simon Glass | fe80686 | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 61 | /* This is valid when a test is running, NULL otherwise */ |
| 62 | static struct unit_test_state *cur_test_state; |
| 63 | |
| 64 | struct unit_test_state *test_get_state(void) |
| 65 | { |
| 66 | return cur_test_state; |
| 67 | } |
| 68 | |
| 69 | void test_set_state(struct unit_test_state *uts) |
| 70 | { |
| 71 | cur_test_state = uts; |
| 72 | } |
| 73 | |
Simon Glass | c79705e | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 74 | /** |
| 75 | * dm_test_pre_run() - Get ready to run a driver model test |
| 76 | * |
| 77 | * This clears out the driver model data structures. For sandbox it resets the |
| 78 | * state structure |
| 79 | * |
| 80 | * @uts: Test state |
| 81 | */ |
| 82 | static int dm_test_pre_run(struct unit_test_state *uts) |
| 83 | { |
| 84 | bool of_live = uts->of_live; |
| 85 | |
Simon Glass | eb6e903 | 2022-09-06 20:27:06 -0600 | [diff] [blame] | 86 | if (of_live && (gd->flags & GD_FLG_FDT_CHANGED)) { |
| 87 | printf("Cannot run live tree test as device tree changed\n"); |
| 88 | return -EFAULT; |
| 89 | } |
Simon Glass | c79705e | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 90 | uts->root = NULL; |
| 91 | uts->testdev = NULL; |
| 92 | uts->force_fail_alloc = false; |
| 93 | uts->skip_post_probe = false; |
Simon Glass | 0e4b697 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 94 | if (fdt_action() == FDTCHK_CHECKSUM) |
| 95 | uts->fdt_chksum = crc8(0, gd->fdt_blob, |
| 96 | fdt_totalsize(gd->fdt_blob)); |
Simon Glass | c79705e | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 97 | gd->dm_root = NULL; |
Simon Glass | 62d6383 | 2022-09-06 20:27:00 -0600 | [diff] [blame] | 98 | malloc_disable_testing(); |
Simon Glass | 719d286 | 2021-07-05 16:32:43 -0600 | [diff] [blame] | 99 | if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA)) |
Simon Glass | c79705e | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 100 | memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count)); |
Simon Glass | d4a1592 | 2021-03-25 10:44:33 +1300 | [diff] [blame] | 101 | arch_reset_for_test(); |
Simon Glass | c79705e | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 102 | |
| 103 | /* Determine whether to make the live tree available */ |
| 104 | gd_set_of_root(of_live ? uts->of_root : NULL); |
Simon Glass | ee88ba7 | 2022-09-06 20:27:19 -0600 | [diff] [blame] | 105 | oftree_reset(); |
Simon Glass | c79705e | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 106 | ut_assertok(dm_init(of_live)); |
| 107 | uts->root = dm_root(); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
Simon Glass | e77615d | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 112 | static int dm_test_post_run(struct unit_test_state *uts) |
| 113 | { |
| 114 | int id; |
| 115 | |
Simon Glass | 0e4b697 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 116 | if (gd->fdt_blob) { |
| 117 | switch (fdt_action()) { |
| 118 | case FDTCHK_COPY: |
| 119 | memcpy((void *)gd->fdt_blob, uts->fdt_copy, uts->fdt_size); |
| 120 | break; |
| 121 | case FDTCHK_CHECKSUM: { |
| 122 | uint chksum; |
| 123 | |
| 124 | chksum = crc8(0, gd->fdt_blob, fdt_totalsize(gd->fdt_blob)); |
Simon Glass | eb6e903 | 2022-09-06 20:27:06 -0600 | [diff] [blame] | 125 | if (chksum != uts->fdt_chksum) { |
| 126 | /* |
| 127 | * We cannot run any more tests that need the |
| 128 | * live tree, since its strings point into the |
| 129 | * flat tree, which has changed. This likely |
| 130 | * means that at least some of the pointers from |
| 131 | * the live tree point to different things |
| 132 | */ |
Simon Glass | 0e4b697 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 133 | printf("Device tree changed: cannot run live tree tests\n"); |
Simon Glass | eb6e903 | 2022-09-06 20:27:06 -0600 | [diff] [blame] | 134 | gd->flags |= GD_FLG_FDT_CHANGED; |
| 135 | } |
Simon Glass | 0e4b697 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 136 | break; |
| 137 | } |
| 138 | case FDTCHK_NONE: |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | |
Simon Glass | e8c023c | 2021-03-15 17:25:21 +1300 | [diff] [blame] | 143 | /* |
| 144 | * With of-platdata-inst the uclasses are created at build time. If we |
| 145 | * destroy them we cannot get them back since uclass_add() is not |
| 146 | * supported. So skip this. |
| 147 | */ |
| 148 | if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) { |
| 149 | for (id = 0; id < UCLASS_COUNT; id++) { |
| 150 | struct uclass *uc; |
Simon Glass | e77615d | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 151 | |
Simon Glass | e8c023c | 2021-03-15 17:25:21 +1300 | [diff] [blame] | 152 | /* |
| 153 | * If the uclass doesn't exist we don't want to create |
| 154 | * it. So check that here before we call |
| 155 | * uclass_find_device(). |
| 156 | */ |
| 157 | uc = uclass_find(id); |
| 158 | if (!uc) |
| 159 | continue; |
| 160 | ut_assertok(uclass_destroy(uc)); |
| 161 | } |
Simon Glass | e77615d | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
Simon Glass | 4b8b27e | 2021-03-07 17:34:51 -0700 | [diff] [blame] | 167 | /* Ensure all the test devices are probed */ |
| 168 | static int do_autoprobe(struct unit_test_state *uts) |
| 169 | { |
Michal Suchanek | c0648b7 | 2022-10-12 21:57:51 +0200 | [diff] [blame] | 170 | return uclass_probe_all(UCLASS_TEST); |
Simon Glass | 4b8b27e | 2021-03-07 17:34:51 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Simon Glass | d2281bb | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 173 | /* |
| 174 | * ut_test_run_on_flattree() - Check if we should run a test with flat DT |
| 175 | * |
| 176 | * This skips long/slow tests where there is not much value in running a flat |
| 177 | * DT test in addition to a live DT test. |
| 178 | * |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 179 | * Return: true to run the given test on the flat device tree |
Simon Glass | d2281bb | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 180 | */ |
| 181 | static bool ut_test_run_on_flattree(struct unit_test *test) |
| 182 | { |
| 183 | const char *fname = strrchr(test->file, '/') + 1; |
| 184 | |
| 185 | if (!(test->flags & UT_TESTF_DM)) |
| 186 | return false; |
| 187 | |
| 188 | return !strstr(fname, "video") || strstr(test->name, "video_base"); |
| 189 | } |
| 190 | |
Simon Glass | ca44ca0 | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 191 | /** |
Simon Glass | f97f85e | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 192 | * test_matches() - Check if a test should be run |
| 193 | * |
| 194 | * This checks if the a test should be run. In the normal case of running all |
| 195 | * tests, @select_name is NULL. |
| 196 | * |
| 197 | * @prefix: String prefix for the tests. Any tests that have this prefix will be |
| 198 | * printed without the prefix, so that it is easier to see the unique part |
Simon Glass | 8482356 | 2021-03-07 17:35:12 -0700 | [diff] [blame] | 199 | * of the test name. If NULL, any suite name (xxx_test) is considered to be |
| 200 | * a prefix. |
Simon Glass | f97f85e | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 201 | * @test_name: Name of current test |
| 202 | * @select_name: Name of test to run (or NULL for all) |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 203 | * Return: true to run this test, false to skip it |
Simon Glass | f97f85e | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 204 | */ |
| 205 | static bool test_matches(const char *prefix, const char *test_name, |
| 206 | const char *select_name) |
| 207 | { |
Andy Shevchenko | ff232a7 | 2021-02-11 16:40:10 +0200 | [diff] [blame] | 208 | size_t len; |
| 209 | |
Simon Glass | f97f85e | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 210 | if (!select_name) |
| 211 | return true; |
| 212 | |
Andy Shevchenko | ff232a7 | 2021-02-11 16:40:10 +0200 | [diff] [blame] | 213 | /* Allow glob expansion in the test name */ |
| 214 | len = select_name[strlen(select_name) - 1] == '*' ? strlen(select_name) : 0; |
| 215 | if (len-- == 1) |
| 216 | return true; |
| 217 | |
| 218 | if (!strncmp(test_name, select_name, len)) |
Simon Glass | f97f85e | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 219 | return true; |
| 220 | |
Andy Shevchenko | 494a5e1 | 2021-02-11 16:40:11 +0200 | [diff] [blame] | 221 | if (prefix) { |
| 222 | /* All tests have this prefix */ |
| 223 | if (!strncmp(test_name, prefix, strlen(prefix))) |
| 224 | test_name += strlen(prefix); |
| 225 | } else { |
Simon Glass | 8482356 | 2021-03-07 17:35:12 -0700 | [diff] [blame] | 226 | const char *p = strstr(test_name, "_test_"); |
| 227 | |
| 228 | /* convert xxx_test_yyy to yyy, i.e. remove the suite name */ |
| 229 | if (p) |
Andy Shevchenko | 494a5e1 | 2021-02-11 16:40:11 +0200 | [diff] [blame] | 230 | test_name = p + strlen("_test_"); |
Simon Glass | 8482356 | 2021-03-07 17:35:12 -0700 | [diff] [blame] | 231 | } |
Simon Glass | f97f85e | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 232 | |
Andy Shevchenko | ff232a7 | 2021-02-11 16:40:10 +0200 | [diff] [blame] | 233 | if (!strncmp(test_name, select_name, len)) |
Simon Glass | f97f85e | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 234 | return true; |
| 235 | |
| 236 | return false; |
| 237 | } |
| 238 | |
Simon Glass | 664277f | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 239 | /** |
Simon Glass | 1fc9c12 | 2021-03-07 17:35:07 -0700 | [diff] [blame] | 240 | * ut_list_has_dm_tests() - Check if a list of tests has driver model ones |
| 241 | * |
| 242 | * @tests: List of tests to run |
| 243 | * @count: Number of tests to ru |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 244 | * Return: true if any of the tests have the UT_TESTF_DM flag |
Simon Glass | 1fc9c12 | 2021-03-07 17:35:07 -0700 | [diff] [blame] | 245 | */ |
| 246 | static bool ut_list_has_dm_tests(struct unit_test *tests, int count) |
| 247 | { |
| 248 | struct unit_test *test; |
| 249 | |
| 250 | for (test = tests; test < tests + count; test++) { |
| 251 | if (test->flags & UT_TESTF_DM) |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | return false; |
| 256 | } |
| 257 | |
Simon Glass | f97f85e | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 258 | /** |
Simon Glass | 664277f | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 259 | * dm_test_restore() Put things back to normal so sandbox works as expected |
| 260 | * |
| 261 | * @of_root: Value to set for of_root |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 262 | * Return: 0 if OK, -ve on error |
Simon Glass | 664277f | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 263 | */ |
| 264 | static int dm_test_restore(struct device_node *of_root) |
| 265 | { |
| 266 | int ret; |
| 267 | |
| 268 | gd_set_of_root(of_root); |
| 269 | gd->dm_root = NULL; |
| 270 | ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE)); |
| 271 | if (ret) |
| 272 | return ret; |
| 273 | dm_scan_plat(false); |
| 274 | if (!CONFIG_IS_ENABLED(OF_PLATDATA)) |
AKASHI Takahiro | 230038f | 2023-06-08 09:55:59 +0900 | [diff] [blame] | 275 | dm_extended_scan(false); |
Simon Glass | 664277f | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | /** |
Simon Glass | ca44ca0 | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 281 | * test_pre_run() - Handle any preparation needed to run a test |
| 282 | * |
| 283 | * @uts: Test state |
| 284 | * @test: Test to prepare for |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 285 | * Return: 0 if OK, -EAGAIN to skip this test since some required feature is not |
Simon Glass | ca44ca0 | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 286 | * available, other -ve on error (meaning that testing cannot likely |
| 287 | * continue) |
| 288 | */ |
| 289 | static int test_pre_run(struct unit_test_state *uts, struct unit_test *test) |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 290 | { |
Simon Glass | 7d02645 | 2022-03-04 08:43:01 -0700 | [diff] [blame] | 291 | ut_assertok(event_init()); |
| 292 | |
Simon Glass | 72b524c | 2021-03-07 17:34:56 -0700 | [diff] [blame] | 293 | if (test->flags & UT_TESTF_DM) |
Simon Glass | c79705e | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 294 | ut_assertok(dm_test_pre_run(uts)); |
Simon Glass | 72b524c | 2021-03-07 17:34:56 -0700 | [diff] [blame] | 295 | |
Simon Glass | 47ec3ed | 2021-03-07 17:34:55 -0700 | [diff] [blame] | 296 | ut_set_skip_delays(uts, false); |
| 297 | |
Simon Glass | 19fb3db | 2021-03-07 17:34:53 -0700 | [diff] [blame] | 298 | uts->start = mallinfo(); |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 299 | |
Simon Glass | ecb274c | 2023-01-17 10:47:23 -0700 | [diff] [blame] | 300 | if (test->flags & UT_TESTF_SCAN_PDATA) |
Simon Glass | 5a986f3 | 2021-03-07 17:34:52 -0700 | [diff] [blame] | 301 | ut_assertok(dm_scan_plat(false)); |
| 302 | |
Simon Glass | 4b8b27e | 2021-03-07 17:34:51 -0700 | [diff] [blame] | 303 | if (test->flags & UT_TESTF_PROBE_TEST) |
| 304 | ut_assertok(do_autoprobe(uts)); |
| 305 | |
Simon Glass | d8ed234 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 306 | if (!CONFIG_IS_ENABLED(OF_PLATDATA) && |
Simon Glass | 70dd886 | 2023-01-17 10:47:28 -0700 | [diff] [blame] | 307 | (test->flags & UT_TESTF_SCAN_FDT)) { |
| 308 | /* |
| 309 | * only set this if we know the ethernet uclass will be created |
| 310 | */ |
| 311 | eth_set_enable_bootdevs(test->flags & UT_TESTF_ETH_BOOTDEV); |
Simon Glass | 081bdc5 | 2023-01-17 10:48:02 -0700 | [diff] [blame] | 312 | test_sf_set_enable_bootdevs(test->flags & UT_TESTF_SF_BOOTDEV); |
Simon Glass | d8ed234 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 313 | ut_assertok(dm_extended_scan(false)); |
Simon Glass | 70dd886 | 2023-01-17 10:47:28 -0700 | [diff] [blame] | 314 | } |
Simon Glass | d8ed234 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 315 | |
Simon Glass | ecb274c | 2023-01-17 10:47:23 -0700 | [diff] [blame] | 316 | /* |
| 317 | * Do this after FDT scan since dm_scan_other() in bootstd-uclass.c |
| 318 | * checks for the existence of bootstd |
| 319 | */ |
| 320 | if (test->flags & UT_TESTF_SCAN_PDATA) |
| 321 | ut_assertok(dm_scan_other(false)); |
| 322 | |
Simon Glass | 8d468a1 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 323 | if (IS_ENABLED(CONFIG_SANDBOX) && (test->flags & UT_TESTF_OTHER_FDT)) { |
| 324 | /* make sure the other FDT is available */ |
| 325 | ut_assertok(test_load_other_fdt(uts)); |
| 326 | |
| 327 | /* |
| 328 | * create a new live tree with it for every test, in case a |
| 329 | * test modifies the tree |
| 330 | */ |
| 331 | if (of_live_active()) { |
| 332 | ut_assertok(unflatten_device_tree(uts->other_fdt, |
| 333 | &uts->of_other)); |
| 334 | } |
| 335 | } |
| 336 | |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 337 | if (test->flags & UT_TESTF_CONSOLE_REC) { |
| 338 | int ret = console_record_reset_enable(); |
| 339 | |
| 340 | if (ret) { |
| 341 | printf("Skipping: Console recording disabled\n"); |
| 342 | return -EAGAIN; |
| 343 | } |
| 344 | } |
Simon Glass | 7452471 | 2021-03-07 17:34:54 -0700 | [diff] [blame] | 345 | ut_silence_console(uts); |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
Simon Glass | ca44ca0 | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 350 | /** |
| 351 | * test_post_run() - Handle cleaning up after a test |
| 352 | * |
| 353 | * @uts: Test state |
| 354 | * @test: Test to clean up after |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 355 | * Return: 0 if OK, -ve on error (meaning that testing cannot likely continue) |
Simon Glass | ca44ca0 | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 356 | */ |
| 357 | static int test_post_run(struct unit_test_state *uts, struct unit_test *test) |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 358 | { |
Simon Glass | 7452471 | 2021-03-07 17:34:54 -0700 | [diff] [blame] | 359 | ut_unsilence_console(uts); |
Simon Glass | e77615d | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 360 | if (test->flags & UT_TESTF_DM) |
| 361 | ut_assertok(dm_test_post_run(uts)); |
Rasmus Villemoes | 50128ae | 2022-10-28 13:50:54 +0200 | [diff] [blame] | 362 | ut_assertok(cyclic_unregister_all()); |
Simon Glass | 7d02645 | 2022-03-04 08:43:01 -0700 | [diff] [blame] | 363 | ut_assertok(event_uninit()); |
Simon Glass | 30a0d20 | 2021-03-07 17:34:49 -0700 | [diff] [blame] | 364 | |
Simon Glass | 8d468a1 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 365 | free(uts->of_other); |
| 366 | uts->of_other = NULL; |
| 367 | |
Simon Glass | 5ea894a | 2022-10-29 19:47:08 -0600 | [diff] [blame] | 368 | blkcache_free(); |
| 369 | |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 370 | return 0; |
| 371 | } |
| 372 | |
Simon Glass | d2281bb | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 373 | /** |
Simon Glass | 1facaad | 2022-10-20 18:22:48 -0600 | [diff] [blame] | 374 | * skip_test() - Handle skipping a test |
| 375 | * |
| 376 | * @uts: Test state to update |
| 377 | * @return -EAGAIN (always) |
| 378 | */ |
| 379 | static int skip_test(struct unit_test_state *uts) |
| 380 | { |
| 381 | uts->skip_count++; |
| 382 | |
| 383 | return -EAGAIN; |
| 384 | } |
| 385 | |
| 386 | /** |
Simon Glass | d2281bb | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 387 | * ut_run_test() - Run a single test |
| 388 | * |
| 389 | * This runs the test, handling any preparation and clean-up needed. It prints |
| 390 | * the name of each test before running it. |
| 391 | * |
| 392 | * @uts: Test state to update. The caller should ensure that this is zeroed for |
| 393 | * the first call to this function. On exit, @uts->fail_count is |
| 394 | * incremented by the number of failures (0, one hopes) |
| 395 | * @test_name: Test to run |
| 396 | * @name: Name of test, possibly skipping a prefix that should not be displayed |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 397 | * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if |
Simon Glass | d2281bb | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 398 | * any failed |
| 399 | */ |
| 400 | static int ut_run_test(struct unit_test_state *uts, struct unit_test *test, |
| 401 | const char *test_name) |
Simon Glass | 99a88fe | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 402 | { |
Simon Glass | ca44ca0 | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 403 | const char *fname = strrchr(test->file, '/') + 1; |
| 404 | const char *note = ""; |
Simon Glass | 99a88fe | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 405 | int ret; |
| 406 | |
Simon Glass | ca44ca0 | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 407 | if ((test->flags & UT_TESTF_DM) && !uts->of_live) |
| 408 | note = " (flat tree)"; |
| 409 | printf("Test: %s: %s%s\n", test_name, fname, note); |
Simon Glass | 99a88fe | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 410 | |
Simon Glass | fe80686 | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 411 | /* Allow access to test state from drivers */ |
| 412 | test_set_state(uts); |
| 413 | |
Simon Glass | 99a88fe | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 414 | ret = test_pre_run(uts, test); |
| 415 | if (ret == -EAGAIN) |
Simon Glass | 1facaad | 2022-10-20 18:22:48 -0600 | [diff] [blame] | 416 | return skip_test(uts); |
Simon Glass | 99a88fe | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 417 | if (ret) |
| 418 | return ret; |
| 419 | |
Simon Glass | 1facaad | 2022-10-20 18:22:48 -0600 | [diff] [blame] | 420 | ret = test->func(uts); |
| 421 | if (ret == -EAGAIN) |
| 422 | skip_test(uts); |
Simon Glass | 99a88fe | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 423 | |
| 424 | ret = test_post_run(uts, test); |
| 425 | if (ret) |
| 426 | return ret; |
| 427 | |
Simon Glass | fe80686 | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 428 | test_set_state( NULL); |
| 429 | |
Simon Glass | 99a88fe | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 430 | return 0; |
| 431 | } |
| 432 | |
Simon Glass | f97f85e | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 433 | /** |
| 434 | * ut_run_test_live_flat() - Run a test with both live and flat tree |
| 435 | * |
| 436 | * This calls ut_run_test() with livetree enabled, which is the standard setup |
| 437 | * for runnig tests. Then, for driver model test, it calls it again with |
| 438 | * livetree disabled. This allows checking of flattree being used when OF_LIVE |
| 439 | * is enabled, as is the case in U-Boot proper before relocation, as well as in |
| 440 | * SPL. |
| 441 | * |
| 442 | * @uts: Test state to update. The caller should ensure that this is zeroed for |
| 443 | * the first call to this function. On exit, @uts->fail_count is |
| 444 | * incremented by the number of failures (0, one hopes) |
| 445 | * @test: Test to run |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 446 | * Return: 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if |
Simon Glass | f97f85e | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 447 | * any failed |
| 448 | */ |
| 449 | static int ut_run_test_live_flat(struct unit_test_state *uts, |
Simon Glass | f7a68d2 | 2022-10-29 19:47:09 -0600 | [diff] [blame] | 450 | struct unit_test *test) |
Simon Glass | d2281bb | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 451 | { |
| 452 | int runs; |
| 453 | |
Simon Glass | 8d468a1 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 454 | if ((test->flags & UT_TESTF_OTHER_FDT) && !IS_ENABLED(CONFIG_SANDBOX)) |
Simon Glass | 1facaad | 2022-10-20 18:22:48 -0600 | [diff] [blame] | 455 | return skip_test(uts); |
Simon Glass | 8d468a1 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 456 | |
Simon Glass | d2281bb | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 457 | /* Run with the live tree if possible */ |
| 458 | runs = 0; |
| 459 | if (CONFIG_IS_ENABLED(OF_LIVE)) { |
Simon Glass | 7c14dc7 | 2022-09-06 20:26:59 -0600 | [diff] [blame] | 460 | if (!(test->flags & UT_TESTF_FLAT_TREE)) { |
Simon Glass | d2281bb | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 461 | uts->of_live = true; |
| 462 | ut_assertok(ut_run_test(uts, test, test->name)); |
| 463 | runs++; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | /* |
Simon Glass | 8d468a1 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 468 | * Run with the flat tree if: |
| 469 | * - it is not marked for live tree only |
| 470 | * - it doesn't require the 'other' FDT when OFNODE_MULTI_TREE_MAX is |
| 471 | * not enabled (since flat tree can only support a single FDT in that |
| 472 | * case |
| 473 | * - we couldn't run it with live tree, |
| 474 | * - it is a core test (dm tests except video) |
| 475 | * - the FDT is still valid and has not been updated by an earlier test |
| 476 | * (for sandbox we handle this by copying the tree, but not for other |
| 477 | * boards) |
Simon Glass | d2281bb | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 478 | */ |
Simon Glass | 6ec5178 | 2023-07-12 09:04:30 -0600 | [diff] [blame^] | 479 | if ((test->flags & UT_TESTF_SCAN_FDT) && |
| 480 | !(test->flags & UT_TESTF_LIVE_TREE) && |
Simon Glass | 8d468a1 | 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 | eb6e903 | 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 | d2281bb | 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 | f97f85e | 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 | 185f812 | 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 | f97f85e | 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 | d1b4659 | 2022-10-29 19:47:13 -0600 | [diff] [blame] | 514 | const char *select_name, const char *test_insert) |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 515 | { |
Simon Glass | d1b4659 | 2022-10-29 19:47:13 -0600 | [diff] [blame] | 516 | struct unit_test *test, *one; |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 517 | int found = 0; |
Simon Glass | d1b4659 | 2022-10-29 19:47:13 -0600 | [diff] [blame] | 518 | int pos = 0; |
| 519 | int upto; |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 520 | |
Simon Glass | d1b4659 | 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 | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 537 | const char *test_name = test->name; |
Simon Glass | ea94d05 | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 538 | int ret, i, old_fail_count; |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 539 | |
Simon Glass | f97f85e | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 540 | if (!test_matches(prefix, test_name, select_name)) |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 541 | continue; |
Simon Glass | cbd71fa | 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 | ea94d05 | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 566 | old_fail_count = uts->fail_count; |
Simon Glass | d1b4659 | 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 | ea94d05 | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 578 | for (i = 0; i < uts->runs_per_test; i++) |
Simon Glass | f7a68d2 | 2022-10-29 19:47:09 -0600 | [diff] [blame] | 579 | ret = ut_run_test_live_flat(uts, test); |
Simon Glass | ea94d05 | 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 | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 584 | found++; |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 585 | if (ret == -EAGAIN) |
| 586 | continue; |
| 587 | if (ret) |
| 588 | return ret; |
Simon Glass | 1c72175 | 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 | ea94d05 | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 597 | struct unit_test *tests, int count, const char *select_name, |
Simon Glass | d1b4659 | 2022-10-29 19:47:13 -0600 | [diff] [blame] | 598 | int runs_per_test, bool force_run, const char *test_insert) |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 599 | { |
| 600 | struct unit_test_state uts = { .fail_count = 0 }; |
Simon Glass | 664277f | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 601 | bool has_dm_tests = false; |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 602 | int ret; |
| 603 | |
Simon Glass | 1fc9c12 | 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 | 664277f | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 606 | has_dm_tests = true; |
Simon Glass | 1fc9c12 | 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 | 1c72175 | 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 | f97f85e | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 621 | uts.of_root = gd_of_root(); |
Simon Glass | ea94d05 | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 622 | uts.runs_per_test = runs_per_test; |
Simon Glass | 0e4b697 | 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 | cbd71fa | 2022-10-20 18:22:50 -0600 | [diff] [blame] | 632 | uts.force_run = force_run; |
Simon Glass | d1b4659 | 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 | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 635 | |
Simon Glass | 0e4b697 | 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 | 8d468a1 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 639 | if (IS_ENABLED(CONFIG_SANDBOX)) { |
Simon Glass | 0e4b697 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 640 | os_free(uts.fdt_copy); |
Simon Glass | 8d468a1 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 641 | os_free(uts.other_fdt); |
| 642 | } |
Simon Glass | 0e4b697 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 643 | |
Simon Glass | 1facaad | 2022-10-20 18:22:48 -0600 | [diff] [blame] | 644 | if (uts.skip_count) |
| 645 | printf("Skipped: %d, ", uts.skip_count); |
Simon Glass | 1c72175 | 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 | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 651 | return ret; |
| 652 | } |