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