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;
20 * dm_test_pre_run() - Get ready to run a driver model test
22 * This clears out the driver model data structures. For sandbox it resets the
27 static int dm_test_pre_run(struct unit_test_state *uts)
29 bool of_live = uts->of_live;
33 uts->force_fail_alloc = false;
34 uts->skip_post_probe = false;
36 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
37 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
38 state_reset_for_test(state_get_current());
40 /* Determine whether to make the live tree available */
41 gd_set_of_root(of_live ? uts->of_root : NULL);
42 ut_assertok(dm_init(of_live));
43 uts->root = dm_root();
48 static int dm_test_post_run(struct unit_test_state *uts)
52 for (id = 0; id < UCLASS_COUNT; id++) {
56 * If the uclass doesn't exist we don't want to create it. So
57 * check that here before we call uclass_find_device().
62 ut_assertok(uclass_destroy(uc));
68 /* Ensure all the test devices are probed */
69 static int do_autoprobe(struct unit_test_state *uts)
74 /* Scanning the uclass is enough to probe all the devices */
75 for (ret = uclass_first_device(UCLASS_TEST, &dev);
77 ret = uclass_next_device(&dev))
84 * test_pre_run() - Handle any preparation needed to run a test
87 * @test: Test to prepare for
88 * @return 0 if OK, -EAGAIN to skip this test since some required feature is not
89 * available, other -ve on error (meaning that testing cannot likely
92 static int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
94 if (test->flags & UT_TESTF_DM)
95 ut_assertok(dm_test_pre_run(uts));
97 ut_set_skip_delays(uts, false);
99 uts->start = mallinfo();
101 if (test->flags & UT_TESTF_SCAN_PDATA)
102 ut_assertok(dm_scan_plat(false));
104 if (test->flags & UT_TESTF_PROBE_TEST)
105 ut_assertok(do_autoprobe(uts));
107 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
108 (test->flags & UT_TESTF_SCAN_FDT))
109 ut_assertok(dm_extended_scan(false));
111 if (test->flags & UT_TESTF_CONSOLE_REC) {
112 int ret = console_record_reset_enable();
115 printf("Skipping: Console recording disabled\n");
119 ut_silence_console(uts);
125 * test_post_run() - Handle cleaning up after a test
128 * @test: Test to clean up after
129 * @return 0 if OK, -ve on error (meaning that testing cannot likely continue)
131 static int test_post_run(struct unit_test_state *uts, struct unit_test *test)
133 ut_unsilence_console(uts);
134 if (test->flags & UT_TESTF_DM)
135 ut_assertok(dm_test_post_run(uts));
140 int ut_run_test(struct unit_test_state *uts, struct unit_test *test,
141 const char *test_name)
143 const char *fname = strrchr(test->file, '/') + 1;
144 const char *note = "";
147 if ((test->flags & UT_TESTF_DM) && !uts->of_live)
148 note = " (flat tree)";
149 printf("Test: %s: %s%s\n", test_name, fname, note);
151 ret = test_pre_run(uts, test);
159 ret = test_post_run(uts, test);
166 int ut_run_tests(struct unit_test_state *uts, const char *prefix,
167 struct unit_test *tests, int count, const char *select_name)
169 struct unit_test *test;
170 int prefix_len = prefix ? strlen(prefix) : 0;
173 for (test = tests; test < tests + count; test++) {
174 const char *test_name = test->name;
177 /* Remove the prefix */
178 if (prefix && !strncmp(test_name, prefix, prefix_len))
179 test_name += prefix_len;
181 if (select_name && strcmp(select_name, test_name))
183 ret = ut_run_test(uts, test, test_name);
190 if (select_name && !found)
193 return uts->fail_count ? -EBADF : 0;
196 int ut_run_list(const char *category, const char *prefix,
197 struct unit_test *tests, int count, const char *select_name)
199 struct unit_test_state uts = { .fail_count = 0 };
203 printf("Running %d %s tests\n", count, category);
205 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
208 printf("Test '%s' not found\n", select_name);
210 printf("Failures: %d\n", uts.fail_count);