test: Move the devicetree check into ut_run_list()
[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 <asm/state.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(OF_PLATDATA))
50                 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
51         state_reset_for_test(state_get_current());
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         for (id = 0; id < UCLASS_COUNT; id++) {
66                 struct uclass *uc;
67
68                 /*
69                  * If the uclass doesn't exist we don't want to create it. So
70                  * check that here before we call uclass_find_device().
71                  */
72                 uc = uclass_find(id);
73                 if (!uc)
74                         continue;
75                 ut_assertok(uclass_destroy(uc));
76         }
77
78         return 0;
79 }
80
81 /* Ensure all the test devices are probed */
82 static int do_autoprobe(struct unit_test_state *uts)
83 {
84         struct udevice *dev;
85         int ret;
86
87         /* Scanning the uclass is enough to probe all the devices */
88         for (ret = uclass_first_device(UCLASS_TEST, &dev);
89              dev;
90              ret = uclass_next_device(&dev))
91                 ;
92
93         return ret;
94 }
95
96 /*
97  * ut_test_run_on_flattree() - Check if we should run a test with flat DT
98  *
99  * This skips long/slow tests where there is not much value in running a flat
100  * DT test in addition to a live DT test.
101  *
102  * @return true to run the given test on the flat device tree
103  */
104 static bool ut_test_run_on_flattree(struct unit_test *test)
105 {
106         const char *fname = strrchr(test->file, '/') + 1;
107
108         if (!(test->flags & UT_TESTF_DM))
109                 return false;
110
111         return !strstr(fname, "video") || strstr(test->name, "video_base");
112 }
113
114 /**
115  * test_matches() - Check if a test should be run
116  *
117  * This checks if the a test should be run. In the normal case of running all
118  * tests, @select_name is NULL.
119  *
120  * @prefix: String prefix for the tests. Any tests that have this prefix will be
121  *      printed without the prefix, so that it is easier to see the unique part
122  *      of the test name. If NULL, no prefix processing is done
123  * @test_name: Name of current test
124  * @select_name: Name of test to run (or NULL for all)
125  * @return true to run this test, false to skip it
126  */
127 static bool test_matches(const char *prefix, const char *test_name,
128                          const char *select_name)
129 {
130         if (!select_name)
131                 return true;
132
133         if (!strcmp(test_name, select_name))
134                 return true;
135
136         /* All tests have this prefix */
137         if (prefix && !strncmp(test_name, prefix, strlen(prefix)))
138                 test_name += strlen(prefix);
139
140         if (!strcmp(test_name, select_name))
141                 return true;
142
143         return false;
144 }
145
146 /*
147  * ut_list_has_dm_tests() - Check if a list of tests has driver model ones
148  *
149  * @tests: List of tests to run
150  * @count: Number of tests to ru
151  * @return true if any of the tests have the UT_TESTF_DM flag
152  */
153 static bool ut_list_has_dm_tests(struct unit_test *tests, int count)
154 {
155         struct unit_test *test;
156
157         for (test = tests; test < tests + count; test++) {
158                 if (test->flags & UT_TESTF_DM)
159                         return true;
160         }
161
162         return false;
163 }
164
165 /**
166  * test_pre_run() - Handle any preparation needed to run a test
167  *
168  * @uts: Test state
169  * @test: Test to prepare for
170  * @return 0 if OK, -EAGAIN to skip this test since some required feature is not
171  *      available, other -ve on error (meaning that testing cannot likely
172  *      continue)
173  */
174 static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
175 {
176         if (test->flags & UT_TESTF_DM)
177                 ut_assertok(dm_test_pre_run(uts));
178
179         ut_set_skip_delays(uts, false);
180
181         uts->start = mallinfo();
182
183         if (test->flags & UT_TESTF_SCAN_PDATA)
184                 ut_assertok(dm_scan_plat(false));
185
186         if (test->flags & UT_TESTF_PROBE_TEST)
187                 ut_assertok(do_autoprobe(uts));
188
189         if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
190             (test->flags & UT_TESTF_SCAN_FDT))
191                 ut_assertok(dm_extended_scan(false));
192
193         if (test->flags & UT_TESTF_CONSOLE_REC) {
194                 int ret = console_record_reset_enable();
195
196                 if (ret) {
197                         printf("Skipping: Console recording disabled\n");
198                         return -EAGAIN;
199                 }
200         }
201         ut_silence_console(uts);
202
203         return 0;
204 }
205
206 /**
207  * test_post_run() - Handle cleaning up after a test
208  *
209  * @uts: Test state
210  * @test: Test to clean up after
211  * @return 0 if OK, -ve on error (meaning that testing cannot likely continue)
212  */
213 static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
214 {
215         ut_unsilence_console(uts);
216         if (test->flags & UT_TESTF_DM)
217                 ut_assertok(dm_test_post_run(uts));
218
219         return 0;
220 }
221
222 /**
223  * ut_run_test() - Run a single test
224  *
225  * This runs the test, handling any preparation and clean-up needed. It prints
226  * the name of each test before running it.
227  *
228  * @uts: Test state to update. The caller should ensure that this is zeroed for
229  *      the first call to this function. On exit, @uts->fail_count is
230  *      incremented by the number of failures (0, one hopes)
231  * @test_name: Test to run
232  * @name: Name of test, possibly skipping a prefix that should not be displayed
233  * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
234  *      any failed
235  */
236 static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
237                        const char *test_name)
238 {
239         const char *fname = strrchr(test->file, '/') + 1;
240         const char *note = "";
241         int ret;
242
243         if ((test->flags & UT_TESTF_DM) && !uts->of_live)
244                 note = " (flat tree)";
245         printf("Test: %s: %s%s\n", test_name, fname, note);
246
247         /* Allow access to test state from drivers */
248         test_set_state(uts);
249
250         ret = test_pre_run(uts, test);
251         if (ret == -EAGAIN)
252                 return -EAGAIN;
253         if (ret)
254                 return ret;
255
256         test->func(uts);
257
258         ret = test_post_run(uts, test);
259         if (ret)
260                 return ret;
261
262         test_set_state( NULL);
263
264         return 0;
265 }
266
267 /**
268  * ut_run_test_live_flat() - Run a test with both live and flat tree
269  *
270  * This calls ut_run_test() with livetree enabled, which is the standard setup
271  * for runnig tests. Then, for driver model test, it calls it again with
272  * livetree disabled. This allows checking of flattree being used when OF_LIVE
273  * is enabled, as is the case in U-Boot proper before relocation, as well as in
274  * SPL.
275  *
276  * @uts: Test state to update. The caller should ensure that this is zeroed for
277  *      the first call to this function. On exit, @uts->fail_count is
278  *      incremented by the number of failures (0, one hopes)
279  * @test: Test to run
280  * @name: Name of test, possibly skipping a prefix that should not be displayed
281  * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
282  *      any failed
283  */
284 static int ut_run_test_live_flat(struct unit_test_state *uts,
285                                  struct unit_test *test, const char *name)
286 {
287         int runs;
288
289         /* Run with the live tree if possible */
290         runs = 0;
291         if (CONFIG_IS_ENABLED(OF_LIVE)) {
292                 if (!(test->flags & UT_TESTF_FLAT_TREE)) {
293                         uts->of_live = true;
294                         ut_assertok(ut_run_test(uts, test, test->name));
295                         runs++;
296                 }
297         }
298
299         /*
300          * Run with the flat tree if we couldn't run it with live tree,
301          * or it is a core test.
302          */
303         if (!(test->flags & UT_TESTF_LIVE_TREE) &&
304             (!runs || ut_test_run_on_flattree(test))) {
305                 uts->of_live = false;
306                 ut_assertok(ut_run_test(uts, test, test->name));
307                 runs++;
308         }
309
310         return 0;
311 }
312
313 /**
314  * ut_run_tests() - Run a set of tests
315  *
316  * This runs the tests, handling any preparation and clean-up needed. It prints
317  * the name of each test before running it.
318  *
319  * @uts: Test state to update. The caller should ensure that this is zeroed for
320  *      the first call to this function. On exit, @uts->fail_count is
321  *      incremented by the number of failures (0, one hopes)
322  * @prefix: String prefix for the tests. Any tests that have this prefix will be
323  *      printed without the prefix, so that it is easier to see the unique part
324  *      of the test name. If NULL, no prefix processing is done
325  * @tests: List of tests to run
326  * @count: Number of tests to run
327  * @select_name: Name of a single test to run (from the list provided). If NULL
328  *      then all tests are run
329  * @return 0 if all tests passed, -ENOENT if test @select_name was not found,
330  *      -EBADF if any failed
331  */
332 static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
333                         struct unit_test *tests, int count,
334                         const char *select_name)
335 {
336         struct unit_test *test;
337         int found = 0;
338
339         for (test = tests; test < tests + count; test++) {
340                 const char *test_name = test->name;
341                 int ret;
342
343                 if (!test_matches(prefix, test_name, select_name))
344                         continue;
345                 ret = ut_run_test_live_flat(uts, test, select_name);
346                 found++;
347                 if (ret == -EAGAIN)
348                         continue;
349                 if (ret)
350                         return ret;
351         }
352         if (select_name && !found)
353                 return -ENOENT;
354
355         return uts->fail_count ? -EBADF : 0;
356 }
357
358 int ut_run_list(const char *category, const char *prefix,
359                 struct unit_test *tests, int count, const char *select_name)
360 {
361         struct unit_test_state uts = { .fail_count = 0 };
362         int ret;
363
364         if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
365             ut_list_has_dm_tests(tests, count)) {
366                 /*
367                  * If we have no device tree, or it only has a root node, then
368                  * these * tests clearly aren't going to work...
369                  */
370                 if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
371                         puts("Please run with test device tree:\n"
372                              "    ./u-boot -d arch/sandbox/dts/test.dtb\n");
373                         return CMD_RET_FAILURE;
374                 }
375         }
376
377         if (!select_name)
378                 printf("Running %d %s tests\n", count, category);
379
380         uts.of_root = gd_of_root();
381         ret = ut_run_tests(&uts, prefix, tests, count, select_name);
382
383         if (ret == -ENOENT)
384                 printf("Test '%s' not found\n", select_name);
385         else
386                 printf("Failures: %d\n", uts.fail_count);
387
388         return ret;
389 }