blob: 139fc1f6f18385523411028b1792bc0c6d317806 [file] [log] [blame]
Simon Glass5722fb22021-03-07 17:34:47 -07001// 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>
Simon Glassd18f7392021-03-07 17:34:50 -07009#include <dm.h>
Simon Glass2dba4762021-03-07 17:34:58 -070010#include <asm/state.h>
Simon Glassd18f7392021-03-07 17:34:50 -070011#include <dm/root.h>
Simon Glass2dba4762021-03-07 17:34:58 -070012#include <dm/test.h>
Simon Glass5b7e55b2021-03-07 17:34:59 -070013#include <dm/uclass-internal.h>
Simon Glass5722fb22021-03-07 17:34:47 -070014#include <test/test.h>
Simon Glassd18f7392021-03-07 17:34:50 -070015#include <test/ut.h>
Simon Glass5722fb22021-03-07 17:34:47 -070016
Simon Glass0f8f6772021-03-07 17:34:49 -070017DECLARE_GLOBAL_DATA_PTR;
18
Simon Glass4066d8d2021-03-07 17:35:04 -070019/* This is valid when a test is running, NULL otherwise */
20static struct unit_test_state *cur_test_state;
21
22struct unit_test_state *test_get_state(void)
23{
24 return cur_test_state;
25}
26
27void test_set_state(struct unit_test_state *uts)
28{
29 cur_test_state = uts;
30}
31
Simon Glass2dba4762021-03-07 17:34:58 -070032/**
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 */
40static 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
Simon Glass5b7e55b2021-03-07 17:34:59 -070061static 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
Simon Glass242357c2021-03-07 17:34:51 -070081/* Ensure all the test devices are probed */
82static 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
Simon Glass0d32ec22021-03-07 17:35:03 -070096/*
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 */
104static 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
Simon Glass436687e2021-03-07 17:35:01 -0700114/**
115 * test_pre_run() - Handle any preparation needed to run a test
116 *
117 * @uts: Test state
118 * @test: Test to prepare for
119 * @return 0 if OK, -EAGAIN to skip this test since some required feature is not
120 * available, other -ve on error (meaning that testing cannot likely
121 * continue)
122 */
123static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd93dc7d2021-03-07 17:34:48 -0700124{
Simon Glassb2890a12021-03-07 17:34:56 -0700125 if (test->flags & UT_TESTF_DM)
Simon Glass2dba4762021-03-07 17:34:58 -0700126 ut_assertok(dm_test_pre_run(uts));
Simon Glassb2890a12021-03-07 17:34:56 -0700127
Simon Glass59cad962021-03-07 17:34:55 -0700128 ut_set_skip_delays(uts, false);
129
Simon Glass86a5bd02021-03-07 17:34:53 -0700130 uts->start = mallinfo();
Simon Glassd93dc7d2021-03-07 17:34:48 -0700131
Simon Glass177e0fd2021-03-07 17:34:52 -0700132 if (test->flags & UT_TESTF_SCAN_PDATA)
133 ut_assertok(dm_scan_plat(false));
134
Simon Glass242357c2021-03-07 17:34:51 -0700135 if (test->flags & UT_TESTF_PROBE_TEST)
136 ut_assertok(do_autoprobe(uts));
137
Simon Glassd18f7392021-03-07 17:34:50 -0700138 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
139 (test->flags & UT_TESTF_SCAN_FDT))
140 ut_assertok(dm_extended_scan(false));
141
Simon Glassd93dc7d2021-03-07 17:34:48 -0700142 if (test->flags & UT_TESTF_CONSOLE_REC) {
143 int ret = console_record_reset_enable();
144
145 if (ret) {
146 printf("Skipping: Console recording disabled\n");
147 return -EAGAIN;
148 }
149 }
Simon Glass2b566b92021-03-07 17:34:54 -0700150 ut_silence_console(uts);
Simon Glassd93dc7d2021-03-07 17:34:48 -0700151
152 return 0;
153}
154
Simon Glass436687e2021-03-07 17:35:01 -0700155/**
156 * test_post_run() - Handle cleaning up after a test
157 *
158 * @uts: Test state
159 * @test: Test to clean up after
160 * @return 0 if OK, -ve on error (meaning that testing cannot likely continue)
161 */
162static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
Simon Glassd93dc7d2021-03-07 17:34:48 -0700163{
Simon Glass2b566b92021-03-07 17:34:54 -0700164 ut_unsilence_console(uts);
Simon Glass5b7e55b2021-03-07 17:34:59 -0700165 if (test->flags & UT_TESTF_DM)
166 ut_assertok(dm_test_post_run(uts));
Simon Glass0f8f6772021-03-07 17:34:49 -0700167
Simon Glassd93dc7d2021-03-07 17:34:48 -0700168 return 0;
169}
170
Simon Glass0d32ec22021-03-07 17:35:03 -0700171/**
172 * ut_run_test() - Run a single test
173 *
174 * This runs the test, handling any preparation and clean-up needed. It prints
175 * the name of each test before running it.
176 *
177 * @uts: Test state to update. The caller should ensure that this is zeroed for
178 * the first call to this function. On exit, @uts->fail_count is
179 * incremented by the number of failures (0, one hopes)
180 * @test_name: Test to run
181 * @name: Name of test, possibly skipping a prefix that should not be displayed
182 * @return 0 if all tests passed, -EAGAIN if the test should be skipped, -1 if
183 * any failed
184 */
185static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
186 const char *test_name)
Simon Glass5517edb2021-03-07 17:35:00 -0700187{
Simon Glass436687e2021-03-07 17:35:01 -0700188 const char *fname = strrchr(test->file, '/') + 1;
189 const char *note = "";
Simon Glass5517edb2021-03-07 17:35:00 -0700190 int ret;
191
Simon Glass436687e2021-03-07 17:35:01 -0700192 if ((test->flags & UT_TESTF_DM) && !uts->of_live)
193 note = " (flat tree)";
194 printf("Test: %s: %s%s\n", test_name, fname, note);
Simon Glass5517edb2021-03-07 17:35:00 -0700195
Simon Glass4066d8d2021-03-07 17:35:04 -0700196 /* Allow access to test state from drivers */
197 test_set_state(uts);
198
Simon Glass5517edb2021-03-07 17:35:00 -0700199 ret = test_pre_run(uts, test);
200 if (ret == -EAGAIN)
201 return -EAGAIN;
202 if (ret)
203 return ret;
204
205 test->func(uts);
206
207 ret = test_post_run(uts, test);
208 if (ret)
209 return ret;
210
Simon Glass4066d8d2021-03-07 17:35:04 -0700211 test_set_state( NULL);
212
Simon Glass5517edb2021-03-07 17:35:00 -0700213 return 0;
214}
215
Simon Glass0d32ec22021-03-07 17:35:03 -0700216int ut_run_test_live_flat(struct unit_test_state *uts, struct unit_test *test,
217 const char *name)
218{
219 int runs;
220
221 /* Run with the live tree if possible */
222 runs = 0;
223 if (CONFIG_IS_ENABLED(OF_LIVE)) {
224 if (!(test->flags & UT_TESTF_FLAT_TREE)) {
225 uts->of_live = true;
226 ut_assertok(ut_run_test(uts, test, test->name));
227 runs++;
228 }
229 }
230
231 /*
232 * Run with the flat tree if we couldn't run it with live tree,
233 * or it is a core test.
234 */
235 if (!(test->flags & UT_TESTF_LIVE_TREE) &&
236 (!runs || ut_test_run_on_flattree(test))) {
237 uts->of_live = false;
238 ut_assertok(ut_run_test(uts, test, test->name));
239 runs++;
240 }
241
242 return 0;
243}
244
Simon Glass5722fb22021-03-07 17:34:47 -0700245int ut_run_tests(struct unit_test_state *uts, const char *prefix,
246 struct unit_test *tests, int count, const char *select_name)
247{
248 struct unit_test *test;
249 int prefix_len = prefix ? strlen(prefix) : 0;
250 int found = 0;
251
252 for (test = tests; test < tests + count; test++) {
253 const char *test_name = test->name;
Simon Glassd93dc7d2021-03-07 17:34:48 -0700254 int ret;
Simon Glass5722fb22021-03-07 17:34:47 -0700255
256 /* Remove the prefix */
257 if (prefix && !strncmp(test_name, prefix, prefix_len))
258 test_name += prefix_len;
259
260 if (select_name && strcmp(select_name, test_name))
261 continue;
Simon Glass0d32ec22021-03-07 17:35:03 -0700262 ret = ut_run_test_live_flat(uts, test, test_name);
Simon Glass5722fb22021-03-07 17:34:47 -0700263 found++;
Simon Glassd93dc7d2021-03-07 17:34:48 -0700264 if (ret == -EAGAIN)
265 continue;
266 if (ret)
267 return ret;
Simon Glass5722fb22021-03-07 17:34:47 -0700268 }
269 if (select_name && !found)
270 return -ENOENT;
271
272 return uts->fail_count ? -EBADF : 0;
273}
274
275int ut_run_list(const char *category, const char *prefix,
276 struct unit_test *tests, int count, const char *select_name)
277{
278 struct unit_test_state uts = { .fail_count = 0 };
279 int ret;
280
281 if (!select_name)
282 printf("Running %d %s tests\n", count, category);
283
284 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
285
286 if (ret == -ENOENT)
287 printf("Test '%s' not found\n", select_name);
288 else
289 printf("Failures: %d\n", uts.fail_count);
290
291 return ret;
292}