1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/reboot.h>
4 #include <kunit/test.h>
5 #include <kunit/attributes.h>
6 #include <linux/glob.h>
7 #include <linux/moduleparam.h>
10 * These symbols point to the .kunit_test_suites section and are defined in
11 * include/asm-generic/vmlinux.lds.h, and consequently must be extern.
13 extern struct kunit_suite * const __kunit_suites_start[];
14 extern struct kunit_suite * const __kunit_suites_end[];
16 static char *action_param;
18 module_param_named(action, action_param, charp, 0400);
19 MODULE_PARM_DESC(action,
20 "Changes KUnit executor behavior, valid values are:\n"
21 "<none>: run the tests like normal\n"
22 "'list' to list test names instead of running them.\n"
23 "'list_attr' to list test names and attributes instead of running them.\n");
25 const char *kunit_action(void)
30 static char *filter_glob_param;
31 static char *filter_param;
32 static char *filter_action_param;
34 module_param_named(filter_glob, filter_glob_param, charp, 0400);
35 MODULE_PARM_DESC(filter_glob,
36 "Filter which KUnit test suites/tests run at boot-time, e.g. list* or list*.*del_test");
37 module_param_named(filter, filter_param, charp, 0400);
38 MODULE_PARM_DESC(filter,
39 "Filter which KUnit test suites/tests run at boot-time using attributes, e.g. speed>slow");
40 module_param_named(filter_action, filter_action_param, charp, 0400);
41 MODULE_PARM_DESC(filter_action,
42 "Changes behavior of filtered tests using attributes, valid values are:\n"
43 "<none>: do not run filtered tests as normal\n"
44 "'skip': skip all filtered tests instead so tests will appear in output\n");
46 const char *kunit_filter_glob(void)
48 return filter_glob_param;
51 char *kunit_filter(void)
56 char *kunit_filter_action(void)
58 return filter_action_param;
61 /* glob_match() needs NULL terminated strings, so we need a copy of filter_glob_param. */
62 struct kunit_glob_filter {
67 /* Split "suite_glob.test_glob" into two. Assumes filter_glob is not empty. */
68 static int kunit_parse_glob_filter(struct kunit_glob_filter *parsed,
69 const char *filter_glob)
71 const int len = strlen(filter_glob);
72 const char *period = strchr(filter_glob, '.');
75 parsed->suite_glob = kzalloc(len + 1, GFP_KERNEL);
76 if (!parsed->suite_glob)
79 parsed->test_glob = NULL;
80 strcpy(parsed->suite_glob, filter_glob);
84 parsed->suite_glob = kzalloc(period - filter_glob + 1, GFP_KERNEL);
85 if (!parsed->suite_glob)
88 parsed->test_glob = kzalloc(len - (period - filter_glob) + 1, GFP_KERNEL);
89 if (!parsed->test_glob) {
90 kfree(parsed->suite_glob);
94 strncpy(parsed->suite_glob, filter_glob, period - filter_glob);
95 strncpy(parsed->test_glob, period + 1, len - (period - filter_glob));
100 /* Create a copy of suite with only tests that match test_glob. */
101 static struct kunit_suite *
102 kunit_filter_glob_tests(const struct kunit_suite *const suite, const char *test_glob)
105 struct kunit_case *filtered, *test_case;
106 struct kunit_suite *copy;
108 kunit_suite_for_each_test_case(suite, test_case) {
109 if (!test_glob || glob_match(test_glob, test_case->name))
116 copy = kmemdup(suite, sizeof(*copy), GFP_KERNEL);
118 return ERR_PTR(-ENOMEM);
120 filtered = kcalloc(n + 1, sizeof(*filtered), GFP_KERNEL);
123 return ERR_PTR(-ENOMEM);
127 kunit_suite_for_each_test_case(suite, test_case) {
128 if (!test_glob || glob_match(test_glob, test_case->name))
129 filtered[n++] = *test_case;
132 copy->test_cases = filtered;
136 void kunit_free_suite_set(struct kunit_suite_set suite_set)
138 struct kunit_suite * const *suites;
140 for (suites = suite_set.start; suites < suite_set.end; suites++)
142 kfree(suite_set.start);
145 struct kunit_suite_set
146 kunit_filter_suites(const struct kunit_suite_set *suite_set,
147 const char *filter_glob,
153 int filter_count = 0;
154 struct kunit_suite **copy, **copy_start, *filtered_suite, *new_filtered_suite;
155 struct kunit_suite_set filtered = {NULL, NULL};
156 struct kunit_glob_filter parsed_glob;
157 struct kunit_attr_filter *parsed_filters = NULL;
159 const size_t max = suite_set->end - suite_set->start;
161 copy = kmalloc_array(max, sizeof(*filtered.start), GFP_KERNEL);
162 if (!copy) { /* won't be able to run anything, return an empty set */
168 *err = kunit_parse_glob_filter(&parsed_glob, filter_glob);
173 /* Parse attribute filters */
175 filter_count = kunit_get_filter_count(filters);
176 parsed_filters = kcalloc(filter_count, sizeof(*parsed_filters), GFP_KERNEL);
177 if (!parsed_filters) {
179 goto free_parsed_glob;
181 for (j = 0; j < filter_count; j++)
182 parsed_filters[j] = kunit_next_attr_filter(&filters, err);
184 goto free_parsed_filters;
187 for (i = 0; &suite_set->start[i] != suite_set->end; i++) {
188 filtered_suite = suite_set->start[i];
190 if (!glob_match(parsed_glob.suite_glob, filtered_suite->name))
192 filtered_suite = kunit_filter_glob_tests(filtered_suite,
193 parsed_glob.test_glob);
194 if (IS_ERR(filtered_suite)) {
195 *err = PTR_ERR(filtered_suite);
196 goto free_parsed_filters;
199 if (filter_count > 0 && parsed_filters != NULL) {
200 for (k = 0; k < filter_count; k++) {
201 new_filtered_suite = kunit_filter_attr_tests(filtered_suite,
202 parsed_filters[k], filter_action, err);
204 /* Free previous copy of suite */
205 if (k > 0 || filter_glob) {
206 kfree(filtered_suite->test_cases);
207 kfree(filtered_suite);
210 filtered_suite = new_filtered_suite;
213 goto free_parsed_filters;
215 if (IS_ERR(filtered_suite)) {
216 *err = PTR_ERR(filtered_suite);
217 goto free_parsed_filters;
227 *copy++ = filtered_suite;
229 filtered.start = copy_start;
234 kfree(parsed_filters);
238 kfree(parsed_glob.suite_glob);
239 kfree(parsed_glob.test_glob);
249 void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin)
251 size_t num_suites = suite_set->end - suite_set->start;
253 if (builtin || num_suites) {
254 pr_info("KTAP version 1\n");
255 pr_info("1..%zu\n", num_suites);
258 __kunit_test_suites_init(suite_set->start, num_suites);
261 void kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr)
263 struct kunit_suite * const *suites;
264 struct kunit_case *test_case;
266 /* Hack: print a ktap header so kunit.py can find the start of KUnit output. */
267 pr_info("KTAP version 1\n");
269 for (suites = suite_set->start; suites < suite_set->end; suites++) {
270 /* Print suite name and suite attributes */
271 pr_info("%s\n", (*suites)->name);
273 kunit_print_attr((void *)(*suites), false, 0);
275 /* Print test case name and attributes in suite */
276 kunit_suite_for_each_test_case((*suites), test_case) {
277 pr_info("%s.%s\n", (*suites)->name, test_case->name);
279 kunit_print_attr((void *)test_case, true, 0);
284 #if IS_BUILTIN(CONFIG_KUNIT)
286 static char *kunit_shutdown;
287 core_param(kunit_shutdown, kunit_shutdown, charp, 0644);
289 static void kunit_handle_shutdown(void)
294 if (!strcmp(kunit_shutdown, "poweroff"))
296 else if (!strcmp(kunit_shutdown, "halt"))
298 else if (!strcmp(kunit_shutdown, "reboot"))
299 kernel_restart(NULL);
303 int kunit_run_all_tests(void)
305 struct kunit_suite_set suite_set = {
306 __kunit_suites_start, __kunit_suites_end,
309 if (!kunit_enabled()) {
310 pr_info("kunit: disabled\n");
314 if (filter_glob_param || filter_param) {
315 suite_set = kunit_filter_suites(&suite_set, filter_glob_param,
316 filter_param, filter_action_param, &err);
318 pr_err("kunit executor: error filtering suites: %d\n", err);
324 kunit_exec_run_tests(&suite_set, true);
325 else if (strcmp(action_param, "list") == 0)
326 kunit_exec_list_tests(&suite_set, false);
327 else if (strcmp(action_param, "list_attr") == 0)
328 kunit_exec_list_tests(&suite_set, true);
330 pr_err("kunit executor: unknown action '%s'\n", action_param);
332 if (filter_glob_param || filter_param) { /* a copy was made of each suite */
333 kunit_free_suite_set(suite_set);
337 kunit_handle_shutdown();
341 #if IS_BUILTIN(CONFIG_KUNIT_TEST)
342 #include "executor_test.c"
345 #endif /* IS_BUILTIN(CONFIG_KUNIT) */