test: Move dm_test_init() into test-main.c
[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 <test/test.h>
14 #include <test/ut.h>
15
16 DECLARE_GLOBAL_DATA_PTR;
17
18 /**
19  * dm_test_pre_run() - Get ready to run a driver model test
20  *
21  * This clears out the driver model data structures. For sandbox it resets the
22  * state structure
23  *
24  * @uts: Test state
25  */
26 static int dm_test_pre_run(struct unit_test_state *uts)
27 {
28         bool of_live = uts->of_live;
29
30         uts->root = NULL;
31         uts->testdev = NULL;
32         uts->force_fail_alloc = false;
33         uts->skip_post_probe = false;
34         gd->dm_root = NULL;
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());
38
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();
43
44         return 0;
45 }
46
47 /* Ensure all the test devices are probed */
48 static int do_autoprobe(struct unit_test_state *uts)
49 {
50         struct udevice *dev;
51         int ret;
52
53         /* Scanning the uclass is enough to probe all the devices */
54         for (ret = uclass_first_device(UCLASS_TEST, &dev);
55              dev;
56              ret = uclass_next_device(&dev))
57                 ;
58
59         return ret;
60 }
61
62 int test_pre_run(struct unit_test_state *uts, struct unit_test *test)
63 {
64         if (test->flags & UT_TESTF_DM)
65                 ut_assertok(dm_test_pre_run(uts));
66
67         ut_set_skip_delays(uts, false);
68
69         uts->start = mallinfo();
70
71         if (test->flags & UT_TESTF_SCAN_PDATA)
72                 ut_assertok(dm_scan_plat(false));
73
74         if (test->flags & UT_TESTF_PROBE_TEST)
75                 ut_assertok(do_autoprobe(uts));
76
77         if (!CONFIG_IS_ENABLED(OF_PLATDATA) &&
78             (test->flags & UT_TESTF_SCAN_FDT))
79                 ut_assertok(dm_extended_scan(false));
80
81         if (test->flags & UT_TESTF_CONSOLE_REC) {
82                 int ret = console_record_reset_enable();
83
84                 if (ret) {
85                         printf("Skipping: Console recording disabled\n");
86                         return -EAGAIN;
87                 }
88         }
89         ut_silence_console(uts);
90
91         return 0;
92 }
93
94 int test_post_run(struct unit_test_state *uts, struct unit_test *test)
95 {
96         ut_unsilence_console(uts);
97
98         return 0;
99 }
100
101 int ut_run_tests(struct unit_test_state *uts, const char *prefix,
102                  struct unit_test *tests, int count, const char *select_name)
103 {
104         struct unit_test *test;
105         int prefix_len = prefix ? strlen(prefix) : 0;
106         int found = 0;
107
108         for (test = tests; test < tests + count; test++) {
109                 const char *test_name = test->name;
110                 int ret;
111
112                 /* Remove the prefix */
113                 if (prefix && !strncmp(test_name, prefix, prefix_len))
114                         test_name += prefix_len;
115
116                 if (select_name && strcmp(select_name, test_name))
117                         continue;
118                 printf("Test: %s\n", test_name);
119                 found++;
120
121                 ret = test_pre_run(uts, test);
122                 if (ret == -EAGAIN)
123                         continue;
124                 if (ret)
125                         return ret;
126
127                 test->func(uts);
128
129                 ret = test_post_run(uts, test);
130                 if (ret)
131                         return ret;
132         }
133         if (select_name && !found)
134                 return -ENOENT;
135
136         return uts->fail_count ? -EBADF : 0;
137 }
138
139 int ut_run_list(const char *category, const char *prefix,
140                 struct unit_test *tests, int count, const char *select_name)
141 {
142         struct unit_test_state uts = { .fail_count = 0 };
143         int ret;
144
145         if (!select_name)
146                 printf("Running %d %s tests\n", count, category);
147
148         ret = ut_run_tests(&uts, prefix, tests, count, select_name);
149
150         if (ret == -ENOENT)
151                 printf("Test '%s' not found\n", select_name);
152         else
153                 printf("Failures: %d\n", uts.fail_count);
154
155         return ret;
156 }