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> |
Simon Glass | d18f739 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 10 | #include <asm/state.h> |
Simon Glass | d18f739 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 11 | #include <dm/root.h> |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 12 | #include <dm/test.h> |
Simon Glass | 5b7e55b | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 13 | #include <dm/uclass-internal.h> |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 14 | #include <test/test.h> |
Simon Glass | d18f739 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 15 | #include <test/ut.h> |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 16 | |
Simon Glass | 0f8f677 | 2021-03-07 17:34:49 -0700 | [diff] [blame] | 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 19 | /* This is valid when a test is running, NULL otherwise */ |
| 20 | static struct unit_test_state *cur_test_state; |
| 21 | |
| 22 | struct unit_test_state *test_get_state(void) |
| 23 | { |
| 24 | return cur_test_state; |
| 25 | } |
| 26 | |
| 27 | void test_set_state(struct unit_test_state *uts) |
| 28 | { |
| 29 | cur_test_state = uts; |
| 30 | } |
| 31 | |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 32 | /** |
| 33 | * dm_test_pre_run() - Get ready to run a driver model test |
| 34 | * |
| 35 | * This clears out the driver model data structures. For sandbox it resets the |
| 36 | * state structure |
| 37 | * |
| 38 | * @uts: Test state |
| 39 | */ |
| 40 | static int dm_test_pre_run(struct unit_test_state *uts) |
| 41 | { |
| 42 | bool of_live = uts->of_live; |
| 43 | |
| 44 | uts->root = NULL; |
| 45 | uts->testdev = NULL; |
| 46 | uts->force_fail_alloc = false; |
| 47 | uts->skip_post_probe = false; |
| 48 | gd->dm_root = NULL; |
| 49 | if (!CONFIG_IS_ENABLED(OF_PLATDATA)) |
| 50 | memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count)); |
| 51 | state_reset_for_test(state_get_current()); |
| 52 | |
| 53 | /* Determine whether to make the live tree available */ |
| 54 | gd_set_of_root(of_live ? uts->of_root : NULL); |
| 55 | ut_assertok(dm_init(of_live)); |
| 56 | uts->root = dm_root(); |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
Simon Glass | 5b7e55b | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 61 | static int dm_test_post_run(struct unit_test_state *uts) |
| 62 | { |
| 63 | int id; |
| 64 | |
| 65 | for (id = 0; id < UCLASS_COUNT; id++) { |
| 66 | struct uclass *uc; |
| 67 | |
| 68 | /* |
| 69 | * If the uclass doesn't exist we don't want to create it. So |
| 70 | * check that here before we call uclass_find_device(). |
| 71 | */ |
| 72 | uc = uclass_find(id); |
| 73 | if (!uc) |
| 74 | continue; |
| 75 | ut_assertok(uclass_destroy(uc)); |
| 76 | } |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
Simon Glass | 242357c | 2021-03-07 17:34:51 -0700 | [diff] [blame] | 81 | /* Ensure all the test devices are probed */ |
| 82 | static int do_autoprobe(struct unit_test_state *uts) |
| 83 | { |
| 84 | struct udevice *dev; |
| 85 | int ret; |
| 86 | |
| 87 | /* Scanning the uclass is enough to probe all the devices */ |
| 88 | for (ret = uclass_first_device(UCLASS_TEST, &dev); |
| 89 | dev; |
| 90 | ret = uclass_next_device(&dev)) |
| 91 | ; |
| 92 | |
| 93 | return ret; |
| 94 | } |
| 95 | |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 96 | /* |
| 97 | * ut_test_run_on_flattree() - Check if we should run a test with flat DT |
| 98 | * |
| 99 | * This skips long/slow tests where there is not much value in running a flat |
| 100 | * DT test in addition to a live DT test. |
| 101 | * |
| 102 | * @return true to run the given test on the flat device tree |
| 103 | */ |
| 104 | static bool ut_test_run_on_flattree(struct unit_test *test) |
| 105 | { |
| 106 | const char *fname = strrchr(test->file, '/') + 1; |
| 107 | |
| 108 | if (!(test->flags & UT_TESTF_DM)) |
| 109 | return false; |
| 110 | |
| 111 | return !strstr(fname, "video") || strstr(test->name, "video_base"); |
| 112 | } |
| 113 | |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 114 | /** |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame^] | 115 | * test_matches() - Check if a test should be run |
| 116 | * |
| 117 | * This checks if the a test should be run. In the normal case of running all |
| 118 | * tests, @select_name is NULL. |
| 119 | * |
| 120 | * @prefix: String prefix for the tests. Any tests that have this prefix will be |
| 121 | * printed without the prefix, so that it is easier to see the unique part |
| 122 | * of the test name. If NULL, no prefix processing is done |
| 123 | * @test_name: Name of current test |
| 124 | * @select_name: Name of test to run (or NULL for all) |
| 125 | * @return true to run this test, false to skip it |
| 126 | */ |
| 127 | static bool test_matches(const char *prefix, const char *test_name, |
| 128 | const char *select_name) |
| 129 | { |
| 130 | if (!select_name) |
| 131 | return true; |
| 132 | |
| 133 | if (!strcmp(test_name, select_name)) |
| 134 | return true; |
| 135 | |
| 136 | /* All tests have this prefix */ |
| 137 | if (prefix && !strncmp(test_name, prefix, strlen(prefix))) |
| 138 | test_name += strlen(prefix); |
| 139 | |
| 140 | if (!strcmp(test_name, select_name)) |
| 141 | return true; |
| 142 | |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | /** |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 147 | * test_pre_run() - Handle any preparation needed to run a test |
| 148 | * |
| 149 | * @uts: Test state |
| 150 | * @test: Test to prepare for |
| 151 | * @return 0 if OK, -EAGAIN to skip this test since some required feature is not |
| 152 | * available, other -ve on error (meaning that testing cannot likely |
| 153 | * continue) |
| 154 | */ |
| 155 | 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] | 156 | { |
Simon Glass | b2890a1 | 2021-03-07 17:34:56 -0700 | [diff] [blame] | 157 | if (test->flags & UT_TESTF_DM) |
Simon Glass | 2dba476 | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 158 | ut_assertok(dm_test_pre_run(uts)); |
Simon Glass | b2890a1 | 2021-03-07 17:34:56 -0700 | [diff] [blame] | 159 | |
Simon Glass | 59cad96 | 2021-03-07 17:34:55 -0700 | [diff] [blame] | 160 | ut_set_skip_delays(uts, false); |
| 161 | |
Simon Glass | 86a5bd0 | 2021-03-07 17:34:53 -0700 | [diff] [blame] | 162 | uts->start = mallinfo(); |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 163 | |
Simon Glass | 177e0fd | 2021-03-07 17:34:52 -0700 | [diff] [blame] | 164 | if (test->flags & UT_TESTF_SCAN_PDATA) |
| 165 | ut_assertok(dm_scan_plat(false)); |
| 166 | |
Simon Glass | 242357c | 2021-03-07 17:34:51 -0700 | [diff] [blame] | 167 | if (test->flags & UT_TESTF_PROBE_TEST) |
| 168 | ut_assertok(do_autoprobe(uts)); |
| 169 | |
Simon Glass | d18f739 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 170 | if (!CONFIG_IS_ENABLED(OF_PLATDATA) && |
| 171 | (test->flags & UT_TESTF_SCAN_FDT)) |
| 172 | ut_assertok(dm_extended_scan(false)); |
| 173 | |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 174 | if (test->flags & UT_TESTF_CONSOLE_REC) { |
| 175 | int ret = console_record_reset_enable(); |
| 176 | |
| 177 | if (ret) { |
| 178 | printf("Skipping: Console recording disabled\n"); |
| 179 | return -EAGAIN; |
| 180 | } |
| 181 | } |
Simon Glass | 2b566b9 | 2021-03-07 17:34:54 -0700 | [diff] [blame] | 182 | ut_silence_console(uts); |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 187 | /** |
| 188 | * test_post_run() - Handle cleaning up after a test |
| 189 | * |
| 190 | * @uts: Test state |
| 191 | * @test: Test to clean up after |
| 192 | * @return 0 if OK, -ve on error (meaning that testing cannot likely continue) |
| 193 | */ |
| 194 | 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] | 195 | { |
Simon Glass | 2b566b9 | 2021-03-07 17:34:54 -0700 | [diff] [blame] | 196 | ut_unsilence_console(uts); |
Simon Glass | 5b7e55b | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 197 | if (test->flags & UT_TESTF_DM) |
| 198 | ut_assertok(dm_test_post_run(uts)); |
Simon Glass | 0f8f677 | 2021-03-07 17:34:49 -0700 | [diff] [blame] | 199 | |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 203 | /** |
| 204 | * ut_run_test() - Run a single test |
| 205 | * |
| 206 | * This runs the test, handling any preparation and clean-up needed. It prints |
| 207 | * the name of each test before running it. |
| 208 | * |
| 209 | * @uts: Test state to update. The caller should ensure that this is zeroed for |
| 210 | * the first call to this function. On exit, @uts->fail_count is |
| 211 | * incremented by the number of failures (0, one hopes) |
| 212 | * @test_name: Test to run |
| 213 | * @name: Name of test, possibly skipping a prefix that should not be displayed |
| 214 | * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if |
| 215 | * any failed |
| 216 | */ |
| 217 | static int ut_run_test(struct unit_test_state *uts, struct unit_test *test, |
| 218 | const char *test_name) |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 219 | { |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 220 | const char *fname = strrchr(test->file, '/') + 1; |
| 221 | const char *note = ""; |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 222 | int ret; |
| 223 | |
Simon Glass | 436687e | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 224 | if ((test->flags & UT_TESTF_DM) && !uts->of_live) |
| 225 | note = " (flat tree)"; |
| 226 | printf("Test: %s: %s%s\n", test_name, fname, note); |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 227 | |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 228 | /* Allow access to test state from drivers */ |
| 229 | test_set_state(uts); |
| 230 | |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 231 | ret = test_pre_run(uts, test); |
| 232 | if (ret == -EAGAIN) |
| 233 | return -EAGAIN; |
| 234 | if (ret) |
| 235 | return ret; |
| 236 | |
| 237 | test->func(uts); |
| 238 | |
| 239 | ret = test_post_run(uts, test); |
| 240 | if (ret) |
| 241 | return ret; |
| 242 | |
Simon Glass | 4066d8d | 2021-03-07 17:35:04 -0700 | [diff] [blame] | 243 | test_set_state( NULL); |
| 244 | |
Simon Glass | 5517edb | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame^] | 248 | /** |
| 249 | * ut_run_test_live_flat() - Run a test with both live and flat tree |
| 250 | * |
| 251 | * This calls ut_run_test() with livetree enabled, which is the standard setup |
| 252 | * for runnig tests. Then, for driver model test, it calls it again with |
| 253 | * livetree disabled. This allows checking of flattree being used when OF_LIVE |
| 254 | * is enabled, as is the case in U-Boot proper before relocation, as well as in |
| 255 | * SPL. |
| 256 | * |
| 257 | * @uts: Test state to update. The caller should ensure that this is zeroed for |
| 258 | * the first call to this function. On exit, @uts->fail_count is |
| 259 | * incremented by the number of failures (0, one hopes) |
| 260 | * @test: Test to run |
| 261 | * @name: Name of test, possibly skipping a prefix that should not be displayed |
| 262 | * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if |
| 263 | * any failed |
| 264 | */ |
| 265 | static int ut_run_test_live_flat(struct unit_test_state *uts, |
| 266 | struct unit_test *test, const char *name) |
Simon Glass | 0d32ec2 | 2021-03-07 17:35:03 -0700 | [diff] [blame] | 267 | { |
| 268 | int runs; |
| 269 | |
| 270 | /* Run with the live tree if possible */ |
| 271 | runs = 0; |
| 272 | if (CONFIG_IS_ENABLED(OF_LIVE)) { |
| 273 | if (!(test->flags & UT_TESTF_FLAT_TREE)) { |
| 274 | uts->of_live = true; |
| 275 | ut_assertok(ut_run_test(uts, test, test->name)); |
| 276 | runs++; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * Run with the flat tree if we couldn't run it with live tree, |
| 282 | * or it is a core test. |
| 283 | */ |
| 284 | if (!(test->flags & UT_TESTF_LIVE_TREE) && |
| 285 | (!runs || ut_test_run_on_flattree(test))) { |
| 286 | uts->of_live = false; |
| 287 | ut_assertok(ut_run_test(uts, test, test->name)); |
| 288 | runs++; |
| 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame^] | 294 | /** |
| 295 | * ut_run_tests() - Run a set of tests |
| 296 | * |
| 297 | * This runs the tests, handling any preparation and clean-up needed. It prints |
| 298 | * the name of each test before running it. |
| 299 | * |
| 300 | * @uts: Test state to update. The caller should ensure that this is zeroed for |
| 301 | * the first call to this function. On exit, @uts->fail_count is |
| 302 | * incremented by the number of failures (0, one hopes) |
| 303 | * @prefix: String prefix for the tests. Any tests that have this prefix will be |
| 304 | * printed without the prefix, so that it is easier to see the unique part |
| 305 | * of the test name. If NULL, no prefix processing is done |
| 306 | * @tests: List of tests to run |
| 307 | * @count: Number of tests to run |
| 308 | * @select_name: Name of a single test to run (from the list provided). If NULL |
| 309 | * then all tests are run |
| 310 | * @return 0 if all tests passed, -ENOENT if test @select_name was not found, |
| 311 | * -EBADF if any failed |
| 312 | */ |
| 313 | static int ut_run_tests(struct unit_test_state *uts, const char *prefix, |
| 314 | struct unit_test *tests, int count, |
| 315 | const char *select_name) |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 316 | { |
| 317 | struct unit_test *test; |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 318 | int found = 0; |
| 319 | |
| 320 | for (test = tests; test < tests + count; test++) { |
| 321 | const char *test_name = test->name; |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 322 | int ret; |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 323 | |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame^] | 324 | if (!test_matches(prefix, test_name, select_name)) |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 325 | continue; |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame^] | 326 | ret = ut_run_test_live_flat(uts, test, select_name); |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 327 | found++; |
Simon Glass | d93dc7d | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 328 | if (ret == -EAGAIN) |
| 329 | continue; |
| 330 | if (ret) |
| 331 | return ret; |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 332 | } |
| 333 | if (select_name && !found) |
| 334 | return -ENOENT; |
| 335 | |
| 336 | return uts->fail_count ? -EBADF : 0; |
| 337 | } |
| 338 | |
| 339 | int ut_run_list(const char *category, const char *prefix, |
| 340 | struct unit_test *tests, int count, const char *select_name) |
| 341 | { |
| 342 | struct unit_test_state uts = { .fail_count = 0 }; |
| 343 | int ret; |
| 344 | |
| 345 | if (!select_name) |
| 346 | printf("Running %d %s tests\n", count, category); |
| 347 | |
Simon Glass | bb2b173 | 2021-03-07 17:35:05 -0700 | [diff] [blame^] | 348 | uts.of_root = gd_of_root(); |
Simon Glass | 5722fb2 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 349 | ret = ut_run_tests(&uts, prefix, tests, count, select_name); |
| 350 | |
| 351 | if (ret == -ENOENT) |
| 352 | printf("Test '%s' not found\n", select_name); |
| 353 | else |
| 354 | printf("Failures: %d\n", uts.fail_count); |
| 355 | |
| 356 | return ret; |
| 357 | } |