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