1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/reboot.h>
4 #include <kunit/test.h>
5 #include <linux/glob.h>
6 #include <linux/moduleparam.h>
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.
12 extern struct kunit_suite * const * const __kunit_suites_start[];
13 extern struct kunit_suite * const * const __kunit_suites_end[];
15 #if IS_BUILTIN(CONFIG_KUNIT)
17 static char *filter_glob_param;
18 module_param_named(filter_glob, filter_glob_param, charp, 0);
19 MODULE_PARM_DESC(filter_glob,
20 "Filter which KUnit test suites run at boot-time, e.g. list*");
22 static char *kunit_shutdown;
23 core_param(kunit_shutdown, kunit_shutdown, charp, 0644);
25 static struct kunit_suite * const *
26 kunit_filter_subsuite(struct kunit_suite * const * const subsuite,
27 const char *filter_glob)
30 struct kunit_suite **filtered;
33 for (i = 0; subsuite[i] != NULL; ++i) {
34 if (glob_match(filter_glob, subsuite[i]->name))
41 filtered = kmalloc_array(n + 1, sizeof(*filtered), GFP_KERNEL);
46 for (i = 0; subsuite[i] != NULL; ++i) {
47 if (glob_match(filter_glob, subsuite[i]->name))
48 filtered[n++] = subsuite[i];
56 struct kunit_suite * const * const *start;
57 struct kunit_suite * const * const *end;
60 static struct suite_set kunit_filter_suites(const struct suite_set *suite_set,
61 const char *filter_glob)
64 struct kunit_suite * const **copy, * const *filtered_subsuite;
65 struct suite_set filtered;
67 const size_t max = suite_set->end - suite_set->start;
69 copy = kmalloc_array(max, sizeof(*filtered.start), GFP_KERNEL);
70 filtered.start = copy;
71 if (!copy) { /* won't be able to run anything, return an empty set */
76 for (i = 0; i < max; ++i) {
77 filtered_subsuite = kunit_filter_subsuite(suite_set->start[i], filter_glob);
78 if (filtered_subsuite)
79 *copy++ = filtered_subsuite;
85 static void kunit_handle_shutdown(void)
90 if (!strcmp(kunit_shutdown, "poweroff"))
92 else if (!strcmp(kunit_shutdown, "halt"))
94 else if (!strcmp(kunit_shutdown, "reboot"))
99 static void kunit_print_tap_header(struct suite_set *suite_set)
101 struct kunit_suite * const * const *suites, * const *subsuite;
102 int num_of_suites = 0;
104 for (suites = suite_set->start; suites < suite_set->end; suites++)
105 for (subsuite = *suites; *subsuite != NULL; subsuite++)
108 pr_info("TAP version 14\n");
109 pr_info("1..%d\n", num_of_suites);
112 int kunit_run_all_tests(void)
114 struct kunit_suite * const * const *suites;
115 struct suite_set suite_set = {
116 .start = __kunit_suites_start,
117 .end = __kunit_suites_end,
120 if (filter_glob_param)
121 suite_set = kunit_filter_suites(&suite_set, filter_glob_param);
123 kunit_print_tap_header(&suite_set);
125 for (suites = suite_set.start; suites < suite_set.end; suites++)
126 __kunit_test_suites_init(*suites);
128 if (filter_glob_param) { /* a copy was made of each array */
129 for (suites = suite_set.start; suites < suite_set.end; suites++)
131 kfree(suite_set.start);
134 kunit_handle_shutdown();
139 #if IS_BUILTIN(CONFIG_KUNIT_TEST)
140 #include "executor_test.c"
143 #endif /* IS_BUILTIN(CONFIG_KUNIT) */