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