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 <test/test.h>
16 DECLARE_GLOBAL_DATA_PTR;
19 * dm_test_pre_run() - Get ready to run a driver model test
21 * This clears out the driver model data structures. For sandbox it resets the
26 static int dm_test_pre_run(struct unit_test_state *uts)
28 bool of_live = uts->of_live;
32 uts->force_fail_alloc = false;
33 uts->skip_post_probe = false;
35 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
36 memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
37 state_reset_for_test(state_get_current());
39 /* Determine whether to make the live tree available */
40 gd_set_of_root(of_live ? uts->of_root : NULL);
41 ut_assertok(dm_init(of_live));
42 uts->root = dm_root();
47 /* Ensure all the test devices are probed */
48 static int do_autoprobe(struct unit_test_state *uts)
53 /* Scanning the uclass is enough to probe all the devices */
54 for (ret = uclass_first_device(UCLASS_TEST, &dev);
56 ret = uclass_next_device(&dev))
62 int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
64 if (test->flags & UT_TESTF_DM)
65 ut_assertok(dm_test_pre_run(uts));
67 ut_set_skip_delays(uts, false);
69 uts->start = mallinfo();
71 if (test->flags & UT_TESTF_SCAN_PDATA)
72 ut_assertok(dm_scan_plat(false));
74 if (test->flags & UT_TESTF_PROBE_TEST)
75 ut_assertok(do_autoprobe(uts));
77 if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
78 (test->flags & UT_TESTF_SCAN_FDT))
79 ut_assertok(dm_extended_scan(false));
81 if (test->flags & UT_TESTF_CONSOLE_REC) {
82 int ret = console_record_reset_enable();
85 printf("Skipping: Console recording disabled\n");
89 ut_silence_console(uts);
94 int test_post_run(struct unit_test_state *uts, struct unit_test *test)
96 ut_unsilence_console(uts);
101 int ut_run_tests(struct unit_test_state *uts, const char *prefix,
102 struct unit_test *tests, int count, const char *select_name)
104 struct unit_test *test;
105 int prefix_len = prefix ? strlen(prefix) : 0;
108 for (test = tests; test < tests + count; test++) {
109 const char *test_name = test->name;
112 /* Remove the prefix */
113 if (prefix && !strncmp(test_name, prefix, prefix_len))
114 test_name += prefix_len;
116 if (select_name && strcmp(select_name, test_name))
118 printf("Test: %s\n", test_name);
121 ret = test_pre_run(uts, test);
129 ret = test_post_run(uts, test);
133 if (select_name && !found)
136 return uts->fail_count ? -EBADF : 0;
139 int ut_run_list(const char *category, const char *prefix,
140 struct unit_test *tests, int count, const char *select_name)
142 struct unit_test_state uts = { .fail_count = 0 };
146 printf("Running %d %s tests\n", count, category);
148 ret = ut_run_tests(&uts, prefix, tests, count, select_name);
151 printf("Test '%s' not found\n", select_name);
153 printf("Failures: %d\n", uts.fail_count);