Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2021 Google LLC |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <console.h> |
Simon Glass | d8ed234 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | c79705e | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 10 | #include <asm/state.h> |
Simon Glass | d8ed234 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 11 | #include <dm/root.h> |
Simon Glass | c79705e | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 12 | #include <dm/test.h> |
Simon Glass | e77615d | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 13 | #include <dm/uclass-internal.h> |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 14 | #include <test/test.h> |
Simon Glass | d8ed234 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 15 | #include <test/ut.h> |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 16 | |
Simon Glass | 30a0d20 | 2021-03-07 17:34:49 -0700 | [diff] [blame] | 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
Simon Glass | c79705e | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 19 | /** |
| 20 | * dm_test_pre_run() - Get ready to run a driver model test |
| 21 | * |
| 22 | * This clears out the driver model data structures. For sandbox it resets the |
| 23 | * state structure |
| 24 | * |
| 25 | * @uts: Test state |
| 26 | */ |
| 27 | static int dm_test_pre_run(struct unit_test_state *uts) |
| 28 | { |
| 29 | bool of_live = uts->of_live; |
| 30 | |
| 31 | uts->root = NULL; |
| 32 | uts->testdev = NULL; |
| 33 | uts->force_fail_alloc = false; |
| 34 | uts->skip_post_probe = false; |
| 35 | gd->dm_root = NULL; |
| 36 | if (!CONFIG_IS_ENABLED(OF_PLATDATA)) |
| 37 | memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count)); |
| 38 | state_reset_for_test(state_get_current()); |
| 39 | |
| 40 | /* Determine whether to make the live tree available */ |
| 41 | gd_set_of_root(of_live ? uts->of_root : NULL); |
| 42 | ut_assertok(dm_init(of_live)); |
| 43 | uts->root = dm_root(); |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
Simon Glass | e77615d | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 48 | static int dm_test_post_run(struct unit_test_state *uts) |
| 49 | { |
| 50 | int id; |
| 51 | |
| 52 | for (id = 0; id < UCLASS_COUNT; id++) { |
| 53 | struct uclass *uc; |
| 54 | |
| 55 | /* |
| 56 | * If the uclass doesn't exist we don't want to create it. So |
| 57 | * check that here before we call uclass_find_device(). |
| 58 | */ |
| 59 | uc = uclass_find(id); |
| 60 | if (!uc) |
| 61 | continue; |
| 62 | ut_assertok(uclass_destroy(uc)); |
| 63 | } |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
Simon Glass | 4b8b27e | 2021-03-07 17:34:51 -0700 | [diff] [blame] | 68 | /* Ensure all the test devices are probed */ |
| 69 | static int do_autoprobe(struct unit_test_state *uts) |
| 70 | { |
| 71 | struct udevice *dev; |
| 72 | int ret; |
| 73 | |
| 74 | /* Scanning the uclass is enough to probe all the devices */ |
| 75 | for (ret = uclass_first_device(UCLASS_TEST, &dev); |
| 76 | dev; |
| 77 | ret = uclass_next_device(&dev)) |
| 78 | ; |
| 79 | |
| 80 | return ret; |
| 81 | } |
| 82 | |
Simon Glass | d2281bb | 2021-03-07 17:35:03 -0700 | [diff] [blame^] | 83 | /* |
| 84 | * ut_test_run_on_flattree() - Check if we should run a test with flat DT |
| 85 | * |
| 86 | * This skips long/slow tests where there is not much value in running a flat |
| 87 | * DT test in addition to a live DT test. |
| 88 | * |
| 89 | * @return true to run the given test on the flat device tree |
| 90 | */ |
| 91 | static bool ut_test_run_on_flattree(struct unit_test *test) |
| 92 | { |
| 93 | const char *fname = strrchr(test->file, '/') + 1; |
| 94 | |
| 95 | if (!(test->flags & UT_TESTF_DM)) |
| 96 | return false; |
| 97 | |
| 98 | return !strstr(fname, "video") || strstr(test->name, "video_base"); |
| 99 | } |
| 100 | |
Simon Glass | ca44ca0 | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 101 | /** |
| 102 | * test_pre_run() - Handle any preparation needed to run a test |
| 103 | * |
| 104 | * @uts: Test state |
| 105 | * @test: Test to prepare for |
| 106 | * @return 0 if OK, -EAGAIN to skip this test since some required feature is not |
| 107 | * available, other -ve on error (meaning that testing cannot likely |
| 108 | * continue) |
| 109 | */ |
| 110 | static int test_pre_run(struct unit_test_state *uts, struct unit_test *test) |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 111 | { |
Simon Glass | 72b524c | 2021-03-07 17:34:56 -0700 | [diff] [blame] | 112 | if (test->flags & UT_TESTF_DM) |
Simon Glass | c79705e | 2021-03-07 17:34:58 -0700 | [diff] [blame] | 113 | ut_assertok(dm_test_pre_run(uts)); |
Simon Glass | 72b524c | 2021-03-07 17:34:56 -0700 | [diff] [blame] | 114 | |
Simon Glass | 47ec3ed | 2021-03-07 17:34:55 -0700 | [diff] [blame] | 115 | ut_set_skip_delays(uts, false); |
| 116 | |
Simon Glass | 19fb3db | 2021-03-07 17:34:53 -0700 | [diff] [blame] | 117 | uts->start = mallinfo(); |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 118 | |
Simon Glass | 5a986f3 | 2021-03-07 17:34:52 -0700 | [diff] [blame] | 119 | if (test->flags & UT_TESTF_SCAN_PDATA) |
| 120 | ut_assertok(dm_scan_plat(false)); |
| 121 | |
Simon Glass | 4b8b27e | 2021-03-07 17:34:51 -0700 | [diff] [blame] | 122 | if (test->flags & UT_TESTF_PROBE_TEST) |
| 123 | ut_assertok(do_autoprobe(uts)); |
| 124 | |
Simon Glass | d8ed234 | 2021-03-07 17:34:50 -0700 | [diff] [blame] | 125 | if (!CONFIG_IS_ENABLED(OF_PLATDATA) && |
| 126 | (test->flags & UT_TESTF_SCAN_FDT)) |
| 127 | ut_assertok(dm_extended_scan(false)); |
| 128 | |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 129 | if (test->flags & UT_TESTF_CONSOLE_REC) { |
| 130 | int ret = console_record_reset_enable(); |
| 131 | |
| 132 | if (ret) { |
| 133 | printf("Skipping: Console recording disabled\n"); |
| 134 | return -EAGAIN; |
| 135 | } |
| 136 | } |
Simon Glass | 7452471 | 2021-03-07 17:34:54 -0700 | [diff] [blame] | 137 | ut_silence_console(uts); |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
Simon Glass | ca44ca0 | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 142 | /** |
| 143 | * test_post_run() - Handle cleaning up after a test |
| 144 | * |
| 145 | * @uts: Test state |
| 146 | * @test: Test to clean up after |
| 147 | * @return 0 if OK, -ve on error (meaning that testing cannot likely continue) |
| 148 | */ |
| 149 | static int test_post_run(struct unit_test_state *uts, struct unit_test *test) |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 150 | { |
Simon Glass | 7452471 | 2021-03-07 17:34:54 -0700 | [diff] [blame] | 151 | ut_unsilence_console(uts); |
Simon Glass | e77615d | 2021-03-07 17:34:59 -0700 | [diff] [blame] | 152 | if (test->flags & UT_TESTF_DM) |
| 153 | ut_assertok(dm_test_post_run(uts)); |
Simon Glass | 30a0d20 | 2021-03-07 17:34:49 -0700 | [diff] [blame] | 154 | |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
Simon Glass | d2281bb | 2021-03-07 17:35:03 -0700 | [diff] [blame^] | 158 | /** |
| 159 | * ut_run_test() - Run a single test |
| 160 | * |
| 161 | * This runs the test, handling any preparation and clean-up needed. It prints |
| 162 | * the name of each test before running it. |
| 163 | * |
| 164 | * @uts: Test state to update. The caller should ensure that this is zeroed for |
| 165 | * the first call to this function. On exit, @uts->fail_count is |
| 166 | * incremented by the number of failures (0, one hopes) |
| 167 | * @test_name: Test to run |
| 168 | * @name: Name of test, possibly skipping a prefix that should not be displayed |
| 169 | * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if |
| 170 | * any failed |
| 171 | */ |
| 172 | static int ut_run_test(struct unit_test_state *uts, struct unit_test *test, |
| 173 | const char *test_name) |
Simon Glass | 99a88fe | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 174 | { |
Simon Glass | ca44ca0 | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 175 | const char *fname = strrchr(test->file, '/') + 1; |
| 176 | const char *note = ""; |
Simon Glass | 99a88fe | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 177 | int ret; |
| 178 | |
Simon Glass | ca44ca0 | 2021-03-07 17:35:01 -0700 | [diff] [blame] | 179 | if ((test->flags & UT_TESTF_DM) && !uts->of_live) |
| 180 | note = " (flat tree)"; |
| 181 | printf("Test: %s: %s%s\n", test_name, fname, note); |
Simon Glass | 99a88fe | 2021-03-07 17:35:00 -0700 | [diff] [blame] | 182 | |
| 183 | ret = test_pre_run(uts, test); |
| 184 | if (ret == -EAGAIN) |
| 185 | return -EAGAIN; |
| 186 | if (ret) |
| 187 | return ret; |
| 188 | |
| 189 | test->func(uts); |
| 190 | |
| 191 | ret = test_post_run(uts, test); |
| 192 | if (ret) |
| 193 | return ret; |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
Simon Glass | d2281bb | 2021-03-07 17:35:03 -0700 | [diff] [blame^] | 198 | int ut_run_test_live_flat(struct unit_test_state *uts, struct unit_test *test, |
| 199 | const char *name) |
| 200 | { |
| 201 | int runs; |
| 202 | |
| 203 | /* Run with the live tree if possible */ |
| 204 | runs = 0; |
| 205 | if (CONFIG_IS_ENABLED(OF_LIVE)) { |
| 206 | if (!(test->flags & UT_TESTF_FLAT_TREE)) { |
| 207 | uts->of_live = true; |
| 208 | ut_assertok(ut_run_test(uts, test, test->name)); |
| 209 | runs++; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Run with the flat tree if we couldn't run it with live tree, |
| 215 | * or it is a core test. |
| 216 | */ |
| 217 | if (!(test->flags & UT_TESTF_LIVE_TREE) && |
| 218 | (!runs || ut_test_run_on_flattree(test))) { |
| 219 | uts->of_live = false; |
| 220 | ut_assertok(ut_run_test(uts, test, test->name)); |
| 221 | runs++; |
| 222 | } |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 227 | int ut_run_tests(struct unit_test_state *uts, const char *prefix, |
| 228 | struct unit_test *tests, int count, const char *select_name) |
| 229 | { |
| 230 | struct unit_test *test; |
| 231 | int prefix_len = prefix ? strlen(prefix) : 0; |
| 232 | int found = 0; |
| 233 | |
| 234 | for (test = tests; test < tests + count; test++) { |
| 235 | const char *test_name = test->name; |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 236 | int ret; |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 237 | |
| 238 | /* Remove the prefix */ |
| 239 | if (prefix && !strncmp(test_name, prefix, prefix_len)) |
| 240 | test_name += prefix_len; |
| 241 | |
| 242 | if (select_name && strcmp(select_name, test_name)) |
| 243 | continue; |
Simon Glass | d2281bb | 2021-03-07 17:35:03 -0700 | [diff] [blame^] | 244 | ret = ut_run_test_live_flat(uts, test, test_name); |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 245 | found++; |
Simon Glass | d002a27 | 2021-03-07 17:34:48 -0700 | [diff] [blame] | 246 | if (ret == -EAGAIN) |
| 247 | continue; |
| 248 | if (ret) |
| 249 | return ret; |
Simon Glass | 1c72175 | 2021-03-07 17:34:47 -0700 | [diff] [blame] | 250 | } |
| 251 | if (select_name && !found) |
| 252 | return -ENOENT; |
| 253 | |
| 254 | return uts->fail_count ? -EBADF : 0; |
| 255 | } |
| 256 | |
| 257 | int ut_run_list(const char *category, const char *prefix, |
| 258 | struct unit_test *tests, int count, const char *select_name) |
| 259 | { |
| 260 | struct unit_test_state uts = { .fail_count = 0 }; |
| 261 | int ret; |
| 262 | |
| 263 | if (!select_name) |
| 264 | printf("Running %d %s tests\n", count, category); |
| 265 | |
| 266 | ret = ut_run_tests(&uts, prefix, tests, count, select_name); |
| 267 | |
| 268 | if (ret == -ENOENT) |
| 269 | printf("Test '%s' not found\n", select_name); |
| 270 | else |
| 271 | printf("Failures: %d\n", uts.fail_count); |
| 272 | |
| 273 | return ret; |
| 274 | } |