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