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 | |
| 7 | #include <common.h> |
| 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 | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 12 | #include <of_live.h> |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 13 | #include <os.h> |
Simon Glass | ba8457b | 2022-09-06 20:27:19 -0600 | [diff] [blame] | 14 | #include <dm/ofnode.h> |
Simon Glass | d18f739 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 15 | #include <dm/root.h> |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 16 | #include <dm/test.h> |
Simon Glass | 5b7e55b | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 17 | #include <dm/uclass-internal.h> |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 18 | #include <test/test.h> |
Simon Glass | d18f739 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 19 | #include <test/ut.h> |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 20 | #include <u-boot/crc.h> |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 21 | |
Simon Glass | 0f8f677 | 2021-03-07 17:34:49 -0700 | [diff] [blame] | 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
Simon Glass | 3ba7675 | 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 | 4066d8d | 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 | 2dba476 | 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 | 60f0899 | 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 | 2dba476 | 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 | 3ba7675 | 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 | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 95 | gd->dm_root = NULL; |
Simon Glass | 1a3e39b | 2022-09-06 20:27:00 -0600 | [diff] [blame] | 96 | malloc_disable_testing(); |
Simon Glass | f700504 | 2021-07-05 16:32:43 -0600 | [diff] [blame] | 97 | if (CONFIG_IS_ENABLED(UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA)) |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 98 | memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count)); |
Simon Glass | 45b807d | 2021-03-25 10:44:33 +1300 | [diff] [blame] | 99 | arch_reset_for_test(); |
Simon Glass | 2dba476 | 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 | ba8457b | 2022-09-06 20:27:19 -0600 | [diff] [blame] | 103 | oftree_reset(); |
Simon Glass | 2dba476 | 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 | 5b7e55b | 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 | 3ba7675 | 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 | 60f0899 | 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 | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 131 | printf("Device tree changed: cannot run live tree tests\n"); |
Simon Glass | 60f0899 | 2022-09-06 20:27:06 -0600 | [diff] [blame] | 132 | gd->flags |= GD_FLG_FDT_CHANGED; |
| 133 | } |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 134 | break; |
| 135 | } |
| 136 | case FDTCHK_NONE: |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
Simon Glass | 96113c1 | 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 | 5b7e55b | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 149 | |
Simon Glass | 96113c1 | 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 | 5b7e55b | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
Simon Glass | 242357c | 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 | { |
Michal Suchanek | 44322b5 | 2022-10-12 21:57:51 +0200 | [diff] [blame] | 168 | return uclass_probe_all(UCLASS_TEST); |
Simon Glass | 242357c | 2021-03-07 17:34:51 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 171 | /* |
| 172 | * ut_test_run_on_flattree() - Check if we should run a test with flat DT |
| 173 | * |
| 174 | * This skips long/slow tests where there is not much value in running a flat |
| 175 | * DT test in addition to a live DT test. |
| 176 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 177 | * Return: true to run the given test on the flat device tree |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 178 | */ |
| 179 | static bool ut_test_run_on_flattree(struct unit_test *test) |
| 180 | { |
| 181 | const char *fname = strrchr(test->file, '/') + 1; |
| 182 | |
| 183 | if (!(test->flags & UT_TESTF_DM)) |
| 184 | return false; |
| 185 | |
| 186 | return !strstr(fname, "video") || strstr(test->name, "video_base"); |
| 187 | } |
| 188 | |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 189 | /** |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 190 | * test_matches() - Check if a test should be run |
| 191 | * |
| 192 | * This checks if the a test should be run. In the normal case of running all |
| 193 | * tests, @select_name is NULL. |
| 194 | * |
| 195 | * @prefix: String prefix for the tests. Any tests that have this prefix will be |
| 196 | * 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] | 197 | * of the test name. If NULL, any suite name (xxx_test) is considered to be |
| 198 | * a prefix. |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 199 | * @test_name: Name of current test |
| 200 | * @select_name: Name of test to run (or NULL for all) |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 201 | * Return: true to run this test, false to skip it |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 202 | */ |
| 203 | static bool test_matches(const char *prefix, const char *test_name, |
| 204 | const char *select_name) |
| 205 | { |
Andy Shevchenko | d91cfbb | 2021-02-11 16:40:10 +0200 | [diff] [blame] | 206 | size_t len; |
| 207 | |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 208 | if (!select_name) |
| 209 | return true; |
| 210 | |
Andy Shevchenko | d91cfbb | 2021-02-11 16:40:10 +0200 | [diff] [blame] | 211 | /* Allow glob expansion in the test name */ |
| 212 | len = select_name[strlen(select_name) - 1] == '*' ? strlen(select_name) : 0; |
| 213 | if (len-- == 1) |
| 214 | return true; |
| 215 | |
| 216 | if (!strncmp(test_name, select_name, len)) |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 217 | return true; |
| 218 | |
Andy Shevchenko | 00d4290 | 2021-02-11 16:40:11 +0200 | [diff] [blame] | 219 | if (prefix) { |
| 220 | /* All tests have this prefix */ |
| 221 | if (!strncmp(test_name, prefix, strlen(prefix))) |
| 222 | test_name += strlen(prefix); |
| 223 | } else { |
Simon Glass | 1ef74ab | 2021-03-07 17:35:12 -0700 | [diff] [blame] | 224 | const char *p = strstr(test_name, "_test_"); |
| 225 | |
| 226 | /* convert xxx_test_yyy to yyy, i.e. remove the suite name */ |
| 227 | if (p) |
Andy Shevchenko | 00d4290 | 2021-02-11 16:40:11 +0200 | [diff] [blame] | 228 | test_name = p + strlen("_test_"); |
Simon Glass | 1ef74ab | 2021-03-07 17:35:12 -0700 | [diff] [blame] | 229 | } |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 230 | |
Andy Shevchenko | d91cfbb | 2021-02-11 16:40:10 +0200 | [diff] [blame] | 231 | if (!strncmp(test_name, select_name, len)) |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 232 | return true; |
| 233 | |
| 234 | return false; |
| 235 | } |
| 236 | |
Simon Glass | 53d1b19 | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 237 | /** |
Simon Glass | 1899e13 | 2021-03-07 17:35:07 -0700 | [diff] [blame] | 238 | * ut_list_has_dm_tests() - Check if a list of tests has driver model ones |
| 239 | * |
| 240 | * @tests: List of tests to run |
| 241 | * @count: Number of tests to ru |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 242 | * 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] | 243 | */ |
| 244 | static bool ut_list_has_dm_tests(struct unit_test *tests, int count) |
| 245 | { |
| 246 | struct unit_test *test; |
| 247 | |
| 248 | for (test = tests; test < tests + count; test++) { |
| 249 | if (test->flags & UT_TESTF_DM) |
| 250 | return true; |
| 251 | } |
| 252 | |
| 253 | return false; |
| 254 | } |
| 255 | |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 256 | /** |
Simon Glass | 53d1b19 | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 257 | * dm_test_restore() Put things back to normal so sandbox works as expected |
| 258 | * |
| 259 | * @of_root: Value to set for of_root |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 260 | * Return: 0 if OK, -ve on error |
Simon Glass | 53d1b19 | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 261 | */ |
| 262 | static int dm_test_restore(struct device_node *of_root) |
| 263 | { |
| 264 | int ret; |
| 265 | |
| 266 | gd_set_of_root(of_root); |
| 267 | gd->dm_root = NULL; |
| 268 | ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE)); |
| 269 | if (ret) |
| 270 | return ret; |
| 271 | dm_scan_plat(false); |
| 272 | if (!CONFIG_IS_ENABLED(OF_PLATDATA)) |
| 273 | dm_scan_fdt(false); |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | /** |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 279 | * test_pre_run() - Handle any preparation needed to run a test |
| 280 | * |
| 281 | * @uts: Test state |
| 282 | * @test: Test to prepare for |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 283 | * 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] | 284 | * available, other -ve on error (meaning that testing cannot likely |
| 285 | * continue) |
| 286 | */ |
| 287 | 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] | 288 | { |
Simon Glass | a128b1b | 2022-03-04 08:43:01 -0700 | [diff] [blame] | 289 | ut_assertok(event_init()); |
| 290 | |
Simon Glass | b2890a1 | 2021-03-07 17:34:56 -0700 | [diff] [blame] | 291 | if (test->flags & UT_TESTF_DM) |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 292 | ut_assertok(dm_test_pre_run(uts)); |
Simon Glass | b2890a1 | 2021-03-07 17:34:56 -0700 | [diff] [blame] | 293 | |
Simon Glass | 59cad96 | 2021-03-07 17:34:55 -0700 | [diff] [blame] | 294 | ut_set_skip_delays(uts, false); |
| 295 | |
Simon Glass | 86a5bd0 | 2021-03-07 17:34:53 -0700 | [diff] [blame] | 296 | uts->start = mallinfo(); |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 297 | |
Simon Glass | 9c80e54 | 2022-07-30 15:52:26 -0600 | [diff] [blame] | 298 | if (test->flags & UT_TESTF_SCAN_PDATA) { |
Simon Glass | 177e0fd | 2021-03-07 17:34:52 -0700 | [diff] [blame] | 299 | ut_assertok(dm_scan_plat(false)); |
Simon Glass | 9c80e54 | 2022-07-30 15:52:26 -0600 | [diff] [blame] | 300 | ut_assertok(dm_scan_other(false)); |
| 301 | } |
Simon Glass | 177e0fd | 2021-03-07 17:34:52 -0700 | [diff] [blame] | 302 | |
Simon Glass | 242357c | 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 | d18f739 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 306 | if (!CONFIG_IS_ENABLED(OF_PLATDATA) && |
| 307 | (test->flags & UT_TESTF_SCAN_FDT)) |
| 308 | ut_assertok(dm_extended_scan(false)); |
| 309 | |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 310 | if (IS_ENABLED(CONFIG_SANDBOX) && (test->flags & UT_TESTF_OTHER_FDT)) { |
| 311 | /* make sure the other FDT is available */ |
| 312 | ut_assertok(test_load_other_fdt(uts)); |
| 313 | |
| 314 | /* |
| 315 | * create a new live tree with it for every test, in case a |
| 316 | * test modifies the tree |
| 317 | */ |
| 318 | if (of_live_active()) { |
| 319 | ut_assertok(unflatten_device_tree(uts->other_fdt, |
| 320 | &uts->of_other)); |
| 321 | } |
| 322 | } |
| 323 | |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 324 | if (test->flags & UT_TESTF_CONSOLE_REC) { |
| 325 | int ret = console_record_reset_enable(); |
| 326 | |
| 327 | if (ret) { |
| 328 | printf("Skipping: Console recording disabled\n"); |
| 329 | return -EAGAIN; |
| 330 | } |
| 331 | } |
Simon Glass | 2b566b9 | 2021-03-07 17:34:54 -0700 | [diff] [blame] | 332 | ut_silence_console(uts); |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 337 | /** |
| 338 | * test_post_run() - Handle cleaning up after a test |
| 339 | * |
| 340 | * @uts: Test state |
| 341 | * @test: Test to clean up after |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 342 | * 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] | 343 | */ |
| 344 | 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] | 345 | { |
Simon Glass | 2b566b9 | 2021-03-07 17:34:54 -0700 | [diff] [blame] | 346 | ut_unsilence_console(uts); |
Simon Glass | 5b7e55b | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 347 | if (test->flags & UT_TESTF_DM) |
| 348 | ut_assertok(dm_test_post_run(uts)); |
Rasmus Villemoes | c794e49 | 2022-10-28 13:50:54 +0200 | [diff] [blame] | 349 | ut_assertok(cyclic_unregister_all()); |
Simon Glass | a128b1b | 2022-03-04 08:43:01 -0700 | [diff] [blame] | 350 | ut_assertok(event_uninit()); |
Simon Glass | 0f8f677 | 2021-03-07 17:34:49 -0700 | [diff] [blame] | 351 | |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 352 | free(uts->of_other); |
| 353 | uts->of_other = NULL; |
| 354 | |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 355 | return 0; |
| 356 | } |
| 357 | |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 358 | /** |
Simon Glass | 816fe6c | 2022-10-20 18:22:48 -0600 | [diff] [blame] | 359 | * skip_test() - Handle skipping a test |
| 360 | * |
| 361 | * @uts: Test state to update |
| 362 | * @return -EAGAIN (always) |
| 363 | */ |
| 364 | static int skip_test(struct unit_test_state *uts) |
| 365 | { |
| 366 | uts->skip_count++; |
| 367 | |
| 368 | return -EAGAIN; |
| 369 | } |
| 370 | |
| 371 | /** |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 372 | * ut_run_test() - Run a single test |
| 373 | * |
| 374 | * This runs the test, handling any preparation and clean-up needed. It prints |
| 375 | * the name of each test before running it. |
| 376 | * |
| 377 | * @uts: Test state to update. The caller should ensure that this is zeroed for |
| 378 | * the first call to this function. On exit, @uts->fail_count is |
| 379 | * incremented by the number of failures (0, one hopes) |
| 380 | * @test_name: Test to run |
| 381 | * @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] | 382 | * 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] | 383 | * any failed |
| 384 | */ |
| 385 | static int ut_run_test(struct unit_test_state *uts, struct unit_test *test, |
| 386 | const char *test_name) |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 387 | { |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 388 | const char *fname = strrchr(test->file, '/') + 1; |
| 389 | const char *note = ""; |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 390 | int ret; |
| 391 | |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 392 | if ((test->flags & UT_TESTF_DM) && !uts->of_live) |
| 393 | note = " (flat tree)"; |
| 394 | printf("Test: %s: %s%s\n", test_name, fname, note); |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 395 | |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 396 | /* Allow access to test state from drivers */ |
| 397 | test_set_state(uts); |
| 398 | |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 399 | ret = test_pre_run(uts, test); |
| 400 | if (ret == -EAGAIN) |
Simon Glass | 816fe6c | 2022-10-20 18:22:48 -0600 | [diff] [blame] | 401 | return skip_test(uts); |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 402 | if (ret) |
| 403 | return ret; |
| 404 | |
Simon Glass | 816fe6c | 2022-10-20 18:22:48 -0600 | [diff] [blame] | 405 | ret = test->func(uts); |
| 406 | if (ret == -EAGAIN) |
| 407 | skip_test(uts); |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 408 | |
| 409 | ret = test_post_run(uts, test); |
| 410 | if (ret) |
| 411 | return ret; |
| 412 | |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 413 | test_set_state( NULL); |
| 414 | |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 418 | /** |
| 419 | * ut_run_test_live_flat() - Run a test with both live and flat tree |
| 420 | * |
| 421 | * This calls ut_run_test() with livetree enabled, which is the standard setup |
| 422 | * for runnig tests. Then, for driver model test, it calls it again with |
| 423 | * livetree disabled. This allows checking of flattree being used when OF_LIVE |
| 424 | * is enabled, as is the case in U-Boot proper before relocation, as well as in |
| 425 | * SPL. |
| 426 | * |
| 427 | * @uts: Test state to update. The caller should ensure that this is zeroed for |
| 428 | * the first call to this function. On exit, @uts->fail_count is |
| 429 | * incremented by the number of failures (0, one hopes) |
| 430 | * @test: Test to run |
| 431 | * @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] | 432 | * 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] | 433 | * any failed |
| 434 | */ |
| 435 | static int ut_run_test_live_flat(struct unit_test_state *uts, |
| 436 | struct unit_test *test, const char *name) |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 437 | { |
| 438 | int runs; |
| 439 | |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 440 | if ((test->flags & UT_TESTF_OTHER_FDT) && !IS_ENABLED(CONFIG_SANDBOX)) |
Simon Glass | 816fe6c | 2022-10-20 18:22:48 -0600 | [diff] [blame] | 441 | return skip_test(uts); |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 442 | |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 443 | /* Run with the live tree if possible */ |
| 444 | runs = 0; |
| 445 | if (CONFIG_IS_ENABLED(OF_LIVE)) { |
Simon Glass | 7f9b580 | 2022-09-06 20:26:59 -0600 | [diff] [blame] | 446 | if (!(test->flags & UT_TESTF_FLAT_TREE)) { |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 447 | uts->of_live = true; |
| 448 | ut_assertok(ut_run_test(uts, test, test->name)); |
| 449 | runs++; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /* |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 454 | * Run with the flat tree if: |
| 455 | * - it is not marked for live tree only |
| 456 | * - it doesn't require the 'other' FDT when OFNODE_MULTI_TREE_MAX is |
| 457 | * not enabled (since flat tree can only support a single FDT in that |
| 458 | * case |
| 459 | * - we couldn't run it with live tree, |
| 460 | * - it is a core test (dm tests except video) |
| 461 | * - the FDT is still valid and has not been updated by an earlier test |
| 462 | * (for sandbox we handle this by copying the tree, but not for other |
| 463 | * boards) |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 464 | */ |
| 465 | if (!(test->flags & UT_TESTF_LIVE_TREE) && |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 466 | (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) || |
| 467 | !(test->flags & UT_TESTF_OTHER_FDT)) && |
Simon Glass | 60f0899 | 2022-09-06 20:27:06 -0600 | [diff] [blame] | 468 | (!runs || ut_test_run_on_flattree(test)) && |
| 469 | !(gd->flags & GD_FLG_FDT_CHANGED)) { |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 470 | uts->of_live = false; |
| 471 | ut_assertok(ut_run_test(uts, test, test->name)); |
| 472 | runs++; |
| 473 | } |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 478 | /** |
| 479 | * ut_run_tests() - Run a set of tests |
| 480 | * |
| 481 | * This runs the tests, handling any preparation and clean-up needed. It prints |
| 482 | * the name of each test before running it. |
| 483 | * |
| 484 | * @uts: Test state to update. The caller should ensure that this is zeroed for |
| 485 | * the first call to this function. On exit, @uts->fail_count is |
| 486 | * incremented by the number of failures (0, one hopes) |
| 487 | * @prefix: String prefix for the tests. Any tests that have this prefix will be |
| 488 | * printed without the prefix, so that it is easier to see the unique part |
| 489 | * of the test name. If NULL, no prefix processing is done |
| 490 | * @tests: List of tests to run |
| 491 | * @count: Number of tests to run |
| 492 | * @select_name: Name of a single test to run (from the list provided). If NULL |
| 493 | * then all tests are run |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 494 | * 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] | 495 | * -EBADF if any failed |
| 496 | */ |
| 497 | static int ut_run_tests(struct unit_test_state *uts, const char *prefix, |
| 498 | struct unit_test *tests, int count, |
| 499 | const char *select_name) |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 500 | { |
| 501 | struct unit_test *test; |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 502 | int found = 0; |
| 503 | |
| 504 | for (test = tests; test < tests + count; test++) { |
| 505 | const char *test_name = test->name; |
Simon Glass | 91a187b | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 506 | int ret, i, old_fail_count; |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 507 | |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 508 | if (!test_matches(prefix, test_name, select_name)) |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 509 | continue; |
Simon Glass | 1f1614b | 2022-10-20 18:22:50 -0600 | [diff] [blame] | 510 | |
| 511 | if (test->flags & UT_TESTF_MANUAL) { |
| 512 | int len; |
| 513 | |
| 514 | /* |
| 515 | * manual tests must have a name ending "_norun" as this |
| 516 | * is how pytest knows to skip them. See |
| 517 | * generate_ut_subtest() for this check. |
| 518 | */ |
| 519 | len = strlen(test_name); |
| 520 | if (len < 6 || strcmp(test_name + len - 6, "_norun")) { |
| 521 | printf("Test %s is manual so must have a name ending in _norun\n", |
| 522 | test_name); |
| 523 | uts->fail_count++; |
| 524 | return -EBADF; |
| 525 | } |
| 526 | if (!uts->force_run) { |
| 527 | if (select_name) { |
| 528 | printf("Test %s skipped as it is manual (use -f to run it)\n", |
| 529 | test_name); |
| 530 | } |
| 531 | continue; |
| 532 | } |
| 533 | } |
Simon Glass | 91a187b | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 534 | old_fail_count = uts->fail_count; |
| 535 | for (i = 0; i < uts->runs_per_test; i++) |
| 536 | ret = ut_run_test_live_flat(uts, test, select_name); |
| 537 | if (uts->fail_count != old_fail_count) { |
| 538 | printf("Test %s failed %d times\n", select_name, |
| 539 | uts->fail_count - old_fail_count); |
| 540 | } |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 541 | found++; |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 542 | if (ret == -EAGAIN) |
| 543 | continue; |
| 544 | if (ret) |
| 545 | return ret; |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 546 | } |
| 547 | if (select_name && !found) |
| 548 | return -ENOENT; |
| 549 | |
| 550 | return uts->fail_count ? -EBADF : 0; |
| 551 | } |
| 552 | |
| 553 | int ut_run_list(const char *category, const char *prefix, |
Simon Glass | 91a187b | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 554 | struct unit_test *tests, int count, const char *select_name, |
Simon Glass | 1f1614b | 2022-10-20 18:22:50 -0600 | [diff] [blame] | 555 | int runs_per_test, bool force_run) |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 556 | { |
| 557 | struct unit_test_state uts = { .fail_count = 0 }; |
Simon Glass | 53d1b19 | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 558 | bool has_dm_tests = false; |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 559 | int ret; |
| 560 | |
Simon Glass | 1899e13 | 2021-03-07 17:35:07 -0700 | [diff] [blame] | 561 | if (!CONFIG_IS_ENABLED(OF_PLATDATA) && |
| 562 | ut_list_has_dm_tests(tests, count)) { |
Simon Glass | 53d1b19 | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 563 | has_dm_tests = true; |
Simon Glass | 1899e13 | 2021-03-07 17:35:07 -0700 | [diff] [blame] | 564 | /* |
| 565 | * If we have no device tree, or it only has a root node, then |
| 566 | * these * tests clearly aren't going to work... |
| 567 | */ |
| 568 | if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) { |
| 569 | puts("Please run with test device tree:\n" |
| 570 | " ./u-boot -d arch/sandbox/dts/test.dtb\n"); |
| 571 | return CMD_RET_FAILURE; |
| 572 | } |
| 573 | } |
| 574 | |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 575 | if (!select_name) |
| 576 | printf("Running %d %s tests\n", count, category); |
| 577 | |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame] | 578 | uts.of_root = gd_of_root(); |
Simon Glass | 91a187b | 2022-08-01 07:58:45 -0600 | [diff] [blame] | 579 | uts.runs_per_test = runs_per_test; |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 580 | if (fdt_action() == FDTCHK_COPY && gd->fdt_blob) { |
| 581 | uts.fdt_size = fdt_totalsize(gd->fdt_blob); |
| 582 | uts.fdt_copy = os_malloc(uts.fdt_size); |
| 583 | if (!uts.fdt_copy) { |
| 584 | printf("Out of memory for device tree copy\n"); |
| 585 | return -ENOMEM; |
| 586 | } |
| 587 | memcpy(uts.fdt_copy, gd->fdt_blob, uts.fdt_size); |
| 588 | } |
Simon Glass | 1f1614b | 2022-10-20 18:22:50 -0600 | [diff] [blame] | 589 | uts.force_run = force_run; |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 590 | ret = ut_run_tests(&uts, prefix, tests, count, select_name); |
| 591 | |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 592 | /* Best efforts only...ignore errors */ |
| 593 | if (has_dm_tests) |
| 594 | dm_test_restore(uts.of_root); |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 595 | if (IS_ENABLED(CONFIG_SANDBOX)) { |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 596 | os_free(uts.fdt_copy); |
Simon Glass | b995807 | 2022-09-06 20:27:11 -0600 | [diff] [blame] | 597 | os_free(uts.other_fdt); |
| 598 | } |
Simon Glass | 3ba7675 | 2022-09-06 20:27:05 -0600 | [diff] [blame] | 599 | |
Simon Glass | 816fe6c | 2022-10-20 18:22:48 -0600 | [diff] [blame] | 600 | if (uts.skip_count) |
| 601 | printf("Skipped: %d, ", uts.skip_count); |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 602 | if (ret == -ENOENT) |
| 603 | printf("Test '%s' not found\n", select_name); |
| 604 | else |
| 605 | printf("Failures: %d\n", uts.fail_count); |
| 606 | |
Simon Glass | 53d1b19 | 2021-03-07 17:35:08 -0700 | [diff] [blame] | 607 | /* Best efforts only...ignore errors */ |
| 608 | if (has_dm_tests) |
| 609 | dm_test_restore(uts.of_root); |
| 610 | |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 611 | return ret; |
| 612 | } |