1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2021 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
10 #include <asm/state.h>
13 #include <dm/uclass-internal.h>
14 #include <test/test.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 /* This is valid when a test is running, NULL otherwise */
20 static struct unit_test_state *cur_test_state;
22 struct unit_test_state *test_get_state(void)
24 return cur_test_state;
27 void test_set_state(struct unit_test_state *uts)
33 * dm_test_pre_run() - Get ready to run a driver model test
35 * This clears out the driver model data structures. For sandbox it resets the
40 static int dm_test_pre_run(struct unit_test_state *uts)
42 bool of_live = uts->of_live;
46 uts->force_fail_alloc = false;
47 uts->skip_post_probe = false;
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());
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();
61 static int dm_test_post_run(struct unit_test_state *uts)
65 for (id = 0; id < UCLASS_COUNT; id++) {
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().
75 ut_assertok(uclass_destroy(uc));
81 /* Ensure all the test devices are probed */
82 static int do_autoprobe(struct unit_test_state *uts)
87 /* Scanning the uclass is enough to probe all the devices */
88 for (ret = uclass_first_device(UCLASS_TEST, &dev);
90 ret = uclass_next_device(&dev))
97 * ut_test_run_on_flattree() - Check if we should run a test with flat DT
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.
102 * @return true to run the given test on the flat device tree
104 static bool ut_test_run_on_flattree(struct unit_test *test)
106 const char *fname = strrchr(test->file, '/') + 1;
108 if (!(test->flags & UT_TESTF_DM))
111 return !strstr(fname, "video") || strstr(test->name, "video_base");
115 * test_pre_run() - Handle any preparation needed to run a test
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
123 static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
125 if (test->flags & UT_TESTF_DM)
126 ut_assertok(dm_test_pre_run(uts));
128 ut_set_skip_delays(uts, false);
130 uts->start = mallinfo();
132 if (test->flags & UT_TESTF_SCAN_PDATA)
133 ut_assertok(dm_scan_plat(false));
135 if (test->flags & UT_TESTF_PROBE_TEST)
136 ut_assertok(do_autoprobe(uts));
138 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
139 (test->flags & UT_TESTF_SCAN_FDT))
140 ut_assertok(dm_extended_scan(false));
142 if (test->flags & UT_TESTF_CONSOLE_REC) {
143 int ret = console_record_reset_enable();
146 printf("Skipping: Console recording disabled\n");
150 ut_silence_console(uts);
156 * test_post_run() - Handle cleaning up after a test
159 * @test: Test to clean up after
160 * @return 0 if OK, -ve on error (meaning that testing cannot likely continue)
162 static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
164 ut_unsilence_console(uts);
165 if (test->flags & UT_TESTF_DM)
166 ut_assertok(dm_test_post_run(uts));
172 * ut_run_test() - Run a single test
174 * This runs the test, handling any preparation and clean-up needed. It prints
175 * the name of each test before running it.
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
185 static int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
186 const char *test_name)
188 const char *fname = strrchr(test->file, '/') + 1;
189 const char *note = "";
192 if ((test->flags & UT_TESTF_DM) && !uts->of_live)
193 note = " (flat tree)";
194 printf("Test: %s: %s%s\n", test_name, fname, note);
196 /* Allow access to test state from drivers */
199 ret = test_pre_run(uts, test);
207 ret = test_post_run(uts, test);
211 test_set_state( NULL);
216 int ut_run_test_live_flat(struct unit_test_state *uts, struct unit_test *test,
221 /* Run with the live tree if possible */
223 if (CONFIG_IS_ENABLED(OF_LIVE)) {
224 if (!(test->flags & UT_TESTF_FLAT_TREE)) {
226 ut_assertok(ut_run_test(uts, test, test->name));
232 * Run with the flat tree if we couldn't run it with live tree,
233 * or it is a core test.
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));
245 int ut_run_tests(struct unit_test_state *uts, const char *prefix,
246 struct unit_test *tests, int count, const char *select_name)
248 struct unit_test *test;
249 int prefix_len = prefix ? strlen(prefix) : 0;
252 for (test = tests; test < tests + count; test++) {
253 const char *test_name = test->name;
256 /* Remove the prefix */
257 if (prefix && !strncmp(test_name, prefix, prefix_len))
258 test_name += prefix_len;
260 if (select_name && strcmp(select_name, test_name))
262 ret = ut_run_test_live_flat(uts, test, test_name);
269 if (select_name && !found)
272 return uts->fail_count ? -EBADF : 0;
275 int ut_run_list(const char *category, const char *prefix,
276 struct unit_test *tests, int count, const char *select_name)
278 struct unit_test_state uts = { .fail_count = 0 };
282 printf("Running %d %s tests\n", count, category);
284 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
287 printf("Test '%s' not found\n", select_name);
289 printf("Failures: %d\n", uts.fail_count);