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