Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0
2 :
3 : #include <linux/reboot.h>
4 : #include <kunit/test.h>
5 : #include <linux/glob.h>
6 : #include <linux/moduleparam.h>
7 :
8 : /*
9 : * These symbols point to the .kunit_test_suites section and are defined in
10 : * include/asm-generic/vmlinux.lds.h, and consequently must be extern.
11 : */
12 : extern struct kunit_suite * const __kunit_suites_start[];
13 : extern struct kunit_suite * const __kunit_suites_end[];
14 :
15 : #if IS_BUILTIN(CONFIG_KUNIT)
16 :
17 : static char *filter_glob_param;
18 : static char *action_param;
19 :
20 : module_param_named(filter_glob, filter_glob_param, charp, 0);
21 : MODULE_PARM_DESC(filter_glob,
22 : "Filter which KUnit test suites/tests run at boot-time, e.g. list* or list*.*del_test");
23 : module_param_named(action, action_param, charp, 0);
24 : MODULE_PARM_DESC(action,
25 : "Changes KUnit executor behavior, valid values are:\n"
26 : "<none>: run the tests like normal\n"
27 : "'list' to list test names instead of running them.\n");
28 :
29 : /* glob_match() needs NULL terminated strings, so we need a copy of filter_glob_param. */
30 : struct kunit_test_filter {
31 : char *suite_glob;
32 : char *test_glob;
33 : };
34 :
35 : /* Split "suite_glob.test_glob" into two. Assumes filter_glob is not empty. */
36 0 : static void kunit_parse_filter_glob(struct kunit_test_filter *parsed,
37 : const char *filter_glob)
38 : {
39 0 : const int len = strlen(filter_glob);
40 0 : const char *period = strchr(filter_glob, '.');
41 :
42 0 : if (!period) {
43 0 : parsed->suite_glob = kzalloc(len + 1, GFP_KERNEL);
44 0 : parsed->test_glob = NULL;
45 0 : strcpy(parsed->suite_glob, filter_glob);
46 0 : return;
47 : }
48 :
49 0 : parsed->suite_glob = kzalloc(period - filter_glob + 1, GFP_KERNEL);
50 0 : parsed->test_glob = kzalloc(len - (period - filter_glob) + 1, GFP_KERNEL);
51 :
52 0 : strncpy(parsed->suite_glob, filter_glob, period - filter_glob);
53 0 : strncpy(parsed->test_glob, period + 1, len - (period - filter_glob));
54 : }
55 :
56 : /* Create a copy of suite with only tests that match test_glob. */
57 : static struct kunit_suite *
58 0 : kunit_filter_tests(const struct kunit_suite *const suite, const char *test_glob)
59 : {
60 0 : int n = 0;
61 : struct kunit_case *filtered, *test_case;
62 : struct kunit_suite *copy;
63 :
64 0 : kunit_suite_for_each_test_case(suite, test_case) {
65 0 : if (!test_glob || glob_match(test_glob, test_case->name))
66 0 : ++n;
67 : }
68 :
69 0 : if (n == 0)
70 : return NULL;
71 :
72 0 : copy = kmemdup(suite, sizeof(*copy), GFP_KERNEL);
73 0 : if (!copy)
74 : return ERR_PTR(-ENOMEM);
75 :
76 0 : filtered = kcalloc(n + 1, sizeof(*filtered), GFP_KERNEL);
77 0 : if (!filtered)
78 : return ERR_PTR(-ENOMEM);
79 :
80 0 : n = 0;
81 0 : kunit_suite_for_each_test_case(suite, test_case) {
82 0 : if (!test_glob || glob_match(test_glob, test_case->name))
83 0 : filtered[n++] = *test_case;
84 : }
85 :
86 0 : copy->test_cases = filtered;
87 0 : return copy;
88 : }
89 :
90 : static char *kunit_shutdown;
91 : core_param(kunit_shutdown, kunit_shutdown, charp, 0644);
92 :
93 : /* Stores a NULL-terminated array of suites. */
94 : struct suite_set {
95 : struct kunit_suite * const *start;
96 : struct kunit_suite * const *end;
97 : };
98 :
99 0 : static void kunit_free_suite_set(struct suite_set suite_set)
100 : {
101 : struct kunit_suite * const *suites;
102 :
103 0 : for (suites = suite_set.start; suites < suite_set.end; suites++)
104 0 : kfree(*suites);
105 0 : kfree(suite_set.start);
106 0 : }
107 :
108 0 : static struct suite_set kunit_filter_suites(const struct suite_set *suite_set,
109 : const char *filter_glob,
110 : int *err)
111 : {
112 : int i;
113 : struct kunit_suite **copy, *filtered_suite;
114 : struct suite_set filtered;
115 : struct kunit_test_filter filter;
116 :
117 : /* Note: this includes space for the terminating NULL. */
118 0 : const size_t max = suite_set->end - suite_set->start;
119 :
120 0 : copy = kmalloc_array(max, sizeof(*filtered.start), GFP_KERNEL);
121 0 : filtered.start = copy;
122 0 : if (!copy) { /* won't be able to run anything, return an empty set */
123 0 : filtered.end = copy;
124 0 : return filtered;
125 : }
126 :
127 0 : kunit_parse_filter_glob(&filter, filter_glob);
128 :
129 0 : for (i = 0; suite_set->start[i] != NULL; i++) {
130 0 : if (!glob_match(filter.suite_glob, suite_set->start[i]->name))
131 0 : continue;
132 :
133 0 : filtered_suite = kunit_filter_tests(suite_set->start[i], filter.test_glob);
134 0 : if (IS_ERR(filtered_suite)) {
135 0 : *err = PTR_ERR(filtered_suite);
136 0 : return filtered;
137 : }
138 0 : if (!filtered_suite)
139 0 : continue;
140 :
141 0 : *copy++ = filtered_suite;
142 : }
143 0 : *copy = NULL;
144 0 : filtered.end = copy;
145 :
146 0 : kfree(filter.suite_glob);
147 0 : kfree(filter.test_glob);
148 0 : return filtered;
149 : }
150 :
151 1 : static void kunit_handle_shutdown(void)
152 : {
153 1 : if (!kunit_shutdown)
154 : return;
155 :
156 1 : if (!strcmp(kunit_shutdown, "poweroff"))
157 0 : kernel_power_off();
158 1 : else if (!strcmp(kunit_shutdown, "halt"))
159 1 : kernel_halt();
160 0 : else if (!strcmp(kunit_shutdown, "reboot"))
161 0 : kernel_restart(NULL);
162 :
163 : }
164 :
165 1 : static void kunit_exec_run_tests(struct suite_set *suite_set)
166 : {
167 1 : size_t num_suites = suite_set->end - suite_set->start;
168 :
169 1 : pr_info("TAP version 14\n");
170 1 : pr_info("1..%zu\n", num_suites);
171 :
172 1 : __kunit_test_suites_init(suite_set->start, num_suites);
173 1 : }
174 :
175 0 : static void kunit_exec_list_tests(struct suite_set *suite_set)
176 : {
177 : struct kunit_suite * const *suites;
178 : struct kunit_case *test_case;
179 :
180 : /* Hack: print a tap header so kunit.py can find the start of KUnit output. */
181 0 : pr_info("TAP version 14\n");
182 :
183 0 : for (suites = suite_set->start; suites < suite_set->end; suites++)
184 0 : kunit_suite_for_each_test_case((*suites), test_case) {
185 0 : pr_info("%s.%s\n", (*suites)->name, test_case->name);
186 : }
187 0 : }
188 :
189 1 : int kunit_run_all_tests(void)
190 : {
191 1 : struct suite_set suite_set = {__kunit_suites_start, __kunit_suites_end};
192 1 : int err = 0;
193 :
194 1 : if (filter_glob_param) {
195 0 : suite_set = kunit_filter_suites(&suite_set, filter_glob_param, &err);
196 0 : if (err) {
197 0 : pr_err("kunit executor: error filtering suites: %d\n", err);
198 0 : goto out;
199 : }
200 : }
201 :
202 1 : if (!action_param)
203 1 : kunit_exec_run_tests(&suite_set);
204 0 : else if (strcmp(action_param, "list") == 0)
205 0 : kunit_exec_list_tests(&suite_set);
206 : else
207 0 : pr_err("kunit executor: unknown action '%s'\n", action_param);
208 :
209 1 : if (filter_glob_param) { /* a copy was made of each suite */
210 0 : kunit_free_suite_set(suite_set);
211 : }
212 :
213 : out:
214 1 : kunit_handle_shutdown();
215 0 : return err;
216 : }
217 :
218 : #if IS_BUILTIN(CONFIG_KUNIT_TEST)
219 : #include "executor_test.c"
220 : #endif
221 :
222 : #endif /* IS_BUILTIN(CONFIG_KUNIT) */
|