Merge tag 'mmc-2021-4-6' of https://source.denx.de/u-boot/custodians/u-boot-mmc
[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 <dm.h>
10 #include <dm/root.h>
11 #include <dm/test.h>
12 #include <dm/uclass-internal.h>
13 #include <test/test.h>
14 #include <test/ut.h>
15
16 DECLARE_GLOBAL_DATA_PTR;
17
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
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;
48         if (IS_ENABLED(CONFIG_UT_DM) && !CONFIG_IS_ENABLED(OF_PLATDATA))
49                 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
50         arch_reset_for_test();
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
60 static int dm_test_post_run(struct unit_test_state *uts)
61 {
62         int id;
63
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;
72
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                 }
83         }
84
85         return 0;
86 }
87
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
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
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
129  *      of the test name. If NULL, any suite name (xxx_test) is considered to be
130  *      a prefix.
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 {
138         if (!select_name)
139                 return true;
140
141         if (!strcmp(test_name, select_name))
142                 return true;
143
144         if (!prefix) {
145                 const char *p = strstr(test_name, "_test_");
146
147                 /* convert xxx_test_yyy to yyy, i.e. remove the suite name */
148                 if (p)
149                         test_name = p + 6;
150         } else {
151                 /* All tests have this prefix */
152                 if (!strncmp(test_name, prefix, strlen(prefix)))
153                         test_name += strlen(prefix);
154         }
155
156         if (!strcmp(test_name, select_name))
157                 return true;
158
159         return false;
160 }
161
162 /**
163  * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
164  *
165  * @tests: List of tests to run
166  * @count: Number of tests to ru
167  * @return true if any of the tests have the UT_TESTF_DM flag
168  */
169 static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
170 {
171         struct unit_test *test;
172
173         for (test = tests; test < tests + count; test++) {
174                 if (test->flags & UT_TESTF_DM)
175                         return true;
176         }
177
178         return false;
179 }
180
181 /**
182  * dm_test_restore() Put things back to normal so sandbox works as expected
183  *
184  * @of_root: Value to set for of_root
185  * @return 0 if OK, -ve on error
186  */
187 static int dm_test_restore(struct device_node *of_root)
188 {
189         int ret;
190
191         gd_set_of_root(of_root);
192         gd->dm_root = NULL;
193         ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
194         if (ret)
195                 return ret;
196         dm_scan_plat(false);
197         if (!CONFIG_IS_ENABLED(OF_PLATDATA))
198                 dm_scan_fdt(false);
199
200         return 0;
201 }
202
203 /**
204  * test_pre_run() - Handle any preparation needed to run a test
205  *
206  * @uts: Test state
207  * @test: Test to prepare for
208  * @return 0 if OK, -EAGAIN to skip this test since some required feature is not
209  *      available, other -ve on error (meaning that testing cannot likely
210  *      continue)
211  */
212 static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
213 {
214         if (test->flags & UT_TESTF_DM)
215                 ut_assertok(dm_test_pre_run(uts));
216
217         ut_set_skip_delays(uts, false);
218
219         uts->start = mallinfo();
220
221         if (test->flags & UT_TESTF_SCAN_PDATA)
222                 ut_assertok(dm_scan_plat(false));
223
224         if (test->flags & UT_TESTF_PROBE_TEST)
225                 ut_assertok(do_autoprobe(uts));
226
227         if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
228             (test->flags & UT_TESTF_SCAN_FDT))
229                 ut_assertok(dm_extended_scan(false));
230
231         if (test->flags & UT_TESTF_CONSOLE_REC) {
232                 int ret = console_record_reset_enable();
233
234                 if (ret) {
235                         printf("Skipping: Console recording disabled\n");
236                         return -EAGAIN;
237                 }
238         }
239         ut_silence_console(uts);
240
241         return 0;
242 }
243
244 /**
245  * test_post_run() - Handle cleaning up after a test
246  *
247  * @uts: Test state
248  * @test: Test to clean up after
249  * @return 0 if OK, -ve on error (meaning that testing cannot likely continue)
250  */
251 static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
252 {
253         ut_unsilence_console(uts);
254         if (test->flags & UT_TESTF_DM)
255                 ut_assertok(dm_test_post_run(uts));
256
257         return 0;
258 }
259
260 /**
261  * ut_run_test() - Run a single test
262  *
263  * This runs the test, handling any preparation and clean-up needed. It prints
264  * the name of each test before running it.
265  *
266  * @uts: Test state to update. The caller should ensure that this is zeroed for
267  *      the first call to this function. On exit, @uts->fail_count is
268  *      incremented by the number of failures (0, one hopes)
269  * @test_name: Test to run
270  * @name: Name of test, possibly skipping a prefix that should not be displayed
271  * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
272  *      any failed
273  */
274 static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
275                        const char *test_name)
276 {
277         const char *fname = strrchr(test->file, '/') + 1;
278         const char *note = "";
279         int ret;
280
281         if ((test->flags & UT_TESTF_DM) && !uts->of_live)
282                 note = " (flat tree)";
283         printf("Test: %s: %s%s\n", test_name, fname, note);
284
285         /* Allow access to test state from drivers */
286         test_set_state(uts);
287
288         ret = test_pre_run(uts, test);
289         if (ret == -EAGAIN)
290                 return -EAGAIN;
291         if (ret)
292                 return ret;
293
294         test->func(uts);
295
296         ret = test_post_run(uts, test);
297         if (ret)
298                 return ret;
299
300         test_set_state( NULL);
301
302         return 0;
303 }
304
305 /**
306  * ut_run_test_live_flat() - Run a test with both live and flat tree
307  *
308  * This calls ut_run_test() with livetree enabled, which is the standard setup
309  * for runnig tests. Then, for driver model test, it calls it again with
310  * livetree disabled. This allows checking of flattree being used when OF_LIVE
311  * is enabled, as is the case in U-Boot proper before relocation, as well as in
312  * SPL.
313  *
314  * @uts: Test state to update. The caller should ensure that this is zeroed for
315  *      the first call to this function. On exit, @uts->fail_count is
316  *      incremented by the number of failures (0, one hopes)
317  * @test: Test to run
318  * @name: Name of test, possibly skipping a prefix that should not be displayed
319  * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
320  *      any failed
321  */
322 static int ut_run_test_live_flat(struct unit_test_state *uts,
323                                  struct unit_test *test, const char *name)
324 {
325         int runs;
326
327         /* Run with the live tree if possible */
328         runs = 0;
329         if (CONFIG_IS_ENABLED(OF_LIVE)) {
330                 if (!(test->flags & UT_TESTF_FLAT_TREE)) {
331                         uts->of_live = true;
332                         ut_assertok(ut_run_test(uts, test, test->name));
333                         runs++;
334                 }
335         }
336
337         /*
338          * Run with the flat tree if we couldn't run it with live tree,
339          * or it is a core test.
340          */
341         if (!(test->flags & UT_TESTF_LIVE_TREE) &&
342             (!runs || ut_test_run_on_flattree(test))) {
343                 uts->of_live = false;
344                 ut_assertok(ut_run_test(uts, test, test->name));
345                 runs++;
346         }
347
348         return 0;
349 }
350
351 /**
352  * ut_run_tests() - Run a set of tests
353  *
354  * This runs the tests, handling any preparation and clean-up needed. It prints
355  * the name of each test before running it.
356  *
357  * @uts: Test state to update. The caller should ensure that this is zeroed for
358  *      the first call to this function. On exit, @uts->fail_count is
359  *      incremented by the number of failures (0, one hopes)
360  * @prefix: String prefix for the tests. Any tests that have this prefix will be
361  *      printed without the prefix, so that it is easier to see the unique part
362  *      of the test name. If NULL, no prefix processing is done
363  * @tests: List of tests to run
364  * @count: Number of tests to run
365  * @select_name: Name of a single test to run (from the list provided). If NULL
366  *      then all tests are run
367  * @return 0 if all tests passed, -ENOENT if test @select_name was not found,
368  *      -EBADF if any failed
369  */
370 static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
371                         struct unit_test *tests, int count,
372                         const char *select_name)
373 {
374         struct unit_test *test;
375         int found = 0;
376
377         for (test = tests; test < tests + count; test++) {
378                 const char *test_name = test->name;
379                 int ret;
380
381                 if (!test_matches(prefix, test_name, select_name))
382                         continue;
383                 ret = ut_run_test_live_flat(uts, test, select_name);
384                 found++;
385                 if (ret == -EAGAIN)
386                         continue;
387                 if (ret)
388                         return ret;
389         }
390         if (select_name && !found)
391                 return -ENOENT;
392
393         return uts->fail_count ? -EBADF : 0;
394 }
395
396 int ut_run_list(const char *category, const char *prefix,
397                 struct unit_test *tests, int count, const char *select_name)
398 {
399         struct unit_test_state uts = { .fail_count = 0 };
400         bool has_dm_tests = false;
401         int ret;
402
403         if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
404             ut_list_has_dm_tests(tests, count)) {
405                 has_dm_tests = true;
406                 /*
407                  * If we have no device tree, or it only has a root node, then
408                  * these * tests clearly aren't going to work...
409                  */
410                 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
411                         puts("Please run with test device tree:\n"
412                              "    ./u-boot -d arch/sandbox/dts/test.dtb\n");
413                         return CMD_RET_FAILURE;
414                 }
415         }
416
417         if (!select_name)
418                 printf("Running %d %s tests\n", count, category);
419
420         uts.of_root = gd_of_root();
421         ret = ut_run_tests(&uts, prefix, tests, count, select_name);
422
423         if (ret == -ENOENT)
424                 printf("Test '%s' not found\n", select_name);
425         else
426                 printf("Failures: %d\n", uts.fail_count);
427
428         /* Best efforts only...ignore errors */
429         if (has_dm_tests)
430                 dm_test_restore(uts.of_root);
431
432         return ret;
433 }