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