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