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