1 // SPDX-License-Identifier: GPL-2.0 2 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> 8 9 /* 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. 12 */ 13 extern struct kunit_suite * const __kunit_suites_start[]; 14 extern struct kunit_suite * const __kunit_suites_end[]; 15 16 #if IS_BUILTIN(CONFIG_KUNIT) 17 18 static char *filter_glob_param; 19 static char *action_param; 20 static char *filter_param; 21 static char *filter_action_param; 22 23 module_param_named(filter_glob, filter_glob_param, charp, 0); 24 MODULE_PARM_DESC(filter_glob, 25 "Filter which KUnit test suites/tests run at boot-time, e.g. list* or list*.*del_test"); 26 module_param_named(action, action_param, charp, 0); 27 MODULE_PARM_DESC(action, 28 "Changes KUnit executor behavior, valid values are:\n" 29 "<none>: run the tests like normal\n" 30 "'list' to list test names instead of running them.\n" 31 "'list_attr' to list test names and attributes instead of running them.\n"); 32 module_param_named(filter, filter_param, charp, 0); 33 MODULE_PARM_DESC(filter, 34 "Filter which KUnit test suites/tests run at boot-time using attributes, e.g. speed>slow"); 35 module_param_named(filter_action, filter_action_param, charp, 0); 36 MODULE_PARM_DESC(filter_action, 37 "Changes behavior of filtered tests using attributes, valid values are:\n" 38 "<none>: do not run filtered tests as normal\n" 39 "'skip': skip all filtered tests instead so tests will appear in output\n"); 40 41 /* glob_match() needs NULL terminated strings, so we need a copy of filter_glob_param. */ 42 struct kunit_glob_filter { 43 char *suite_glob; 44 char *test_glob; 45 }; 46 47 /* Split "suite_glob.test_glob" into two. Assumes filter_glob is not empty. */ 48 static void kunit_parse_glob_filter(struct kunit_glob_filter *parsed, 49 const char *filter_glob) 50 { 51 const int len = strlen(filter_glob); 52 const char *period = strchr(filter_glob, '.'); 53 54 if (!period) { 55 parsed->suite_glob = kzalloc(len + 1, GFP_KERNEL); 56 parsed->test_glob = NULL; 57 strcpy(parsed->suite_glob, filter_glob); 58 return; 59 } 60 61 parsed->suite_glob = kzalloc(period - filter_glob + 1, GFP_KERNEL); 62 parsed->test_glob = kzalloc(len - (period - filter_glob) + 1, GFP_KERNEL); 63 64 strncpy(parsed->suite_glob, filter_glob, period - filter_glob); 65 strncpy(parsed->test_glob, period + 1, len - (period - filter_glob)); 66 } 67 68 /* Create a copy of suite with only tests that match test_glob. */ 69 static struct kunit_suite * 70 kunit_filter_glob_tests(const struct kunit_suite *const suite, const char *test_glob) 71 { 72 int n = 0; 73 struct kunit_case *filtered, *test_case; 74 struct kunit_suite *copy; 75 76 kunit_suite_for_each_test_case(suite, test_case) { 77 if (!test_glob || glob_match(test_glob, test_case->name)) 78 ++n; 79 } 80 81 if (n == 0) 82 return NULL; 83 84 copy = kmemdup(suite, sizeof(*copy), GFP_KERNEL); 85 if (!copy) 86 return ERR_PTR(-ENOMEM); 87 88 filtered = kcalloc(n + 1, sizeof(*filtered), GFP_KERNEL); 89 if (!filtered) { 90 kfree(copy); 91 return ERR_PTR(-ENOMEM); 92 } 93 94 n = 0; 95 kunit_suite_for_each_test_case(suite, test_case) { 96 if (!test_glob || glob_match(test_glob, test_case->name)) 97 filtered[n++] = *test_case; 98 } 99 100 copy->test_cases = filtered; 101 return copy; 102 } 103 104 static char *kunit_shutdown; 105 core_param(kunit_shutdown, kunit_shutdown, charp, 0644); 106 107 /* Stores an array of suites, end points one past the end */ 108 struct suite_set { 109 struct kunit_suite * const *start; 110 struct kunit_suite * const *end; 111 }; 112 113 static void kunit_free_suite_set(struct suite_set suite_set) 114 { 115 struct kunit_suite * const *suites; 116 117 for (suites = suite_set.start; suites < suite_set.end; suites++) 118 kfree(*suites); 119 kfree(suite_set.start); 120 } 121 122 static struct suite_set kunit_filter_suites(const struct suite_set *suite_set, 123 const char *filter_glob, 124 char *filters, 125 char *filter_action, 126 int *err) 127 { 128 int i, j, k, filter_count; 129 struct kunit_suite **copy, *filtered_suite, *new_filtered_suite; 130 struct suite_set filtered; 131 struct kunit_glob_filter parsed_glob; 132 struct kunit_attr_filter *parsed_filters; 133 134 const size_t max = suite_set->end - suite_set->start; 135 136 copy = kmalloc_array(max, sizeof(*filtered.start), GFP_KERNEL); 137 filtered.start = copy; 138 if (!copy) { /* won't be able to run anything, return an empty set */ 139 filtered.end = copy; 140 return filtered; 141 } 142 143 if (filter_glob) 144 kunit_parse_glob_filter(&parsed_glob, filter_glob); 145 146 /* Parse attribute filters */ 147 if (filters) { 148 filter_count = kunit_get_filter_count(filters); 149 parsed_filters = kcalloc(filter_count + 1, sizeof(*parsed_filters), GFP_KERNEL); 150 for (j = 0; j < filter_count; j++) 151 parsed_filters[j] = kunit_next_attr_filter(&filters, err); 152 if (*err) 153 return filtered; 154 } 155 156 for (i = 0; &suite_set->start[i] != suite_set->end; i++) { 157 filtered_suite = suite_set->start[i]; 158 if (filter_glob) { 159 if (!glob_match(parsed_glob.suite_glob, filtered_suite->name)) 160 continue; 161 filtered_suite = kunit_filter_glob_tests(filtered_suite, 162 parsed_glob.test_glob); 163 if (IS_ERR(filtered_suite)) { 164 *err = PTR_ERR(filtered_suite); 165 return filtered; 166 } 167 } 168 if (filter_count) { 169 for (k = 0; k < filter_count; k++) { 170 new_filtered_suite = kunit_filter_attr_tests(filtered_suite, 171 parsed_filters[k], filter_action, err); 172 173 /* Free previous copy of suite */ 174 if (k > 0 || filter_glob) 175 kfree(filtered_suite); 176 filtered_suite = new_filtered_suite; 177 178 if (*err) 179 return filtered; 180 if (IS_ERR(filtered_suite)) { 181 *err = PTR_ERR(filtered_suite); 182 return filtered; 183 } 184 if (!filtered_suite) 185 break; 186 } 187 } 188 189 if (!filtered_suite) 190 continue; 191 192 *copy++ = filtered_suite; 193 } 194 filtered.end = copy; 195 196 if (filter_glob) { 197 kfree(parsed_glob.suite_glob); 198 kfree(parsed_glob.test_glob); 199 } 200 201 if (filter_count) 202 kfree(parsed_filters); 203 204 return filtered; 205 } 206 207 static void kunit_handle_shutdown(void) 208 { 209 if (!kunit_shutdown) 210 return; 211 212 if (!strcmp(kunit_shutdown, "poweroff")) 213 kernel_power_off(); 214 else if (!strcmp(kunit_shutdown, "halt")) 215 kernel_halt(); 216 else if (!strcmp(kunit_shutdown, "reboot")) 217 kernel_restart(NULL); 218 219 } 220 221 static void kunit_exec_run_tests(struct suite_set *suite_set) 222 { 223 size_t num_suites = suite_set->end - suite_set->start; 224 225 pr_info("KTAP version 1\n"); 226 pr_info("1..%zu\n", num_suites); 227 228 __kunit_test_suites_init(suite_set->start, num_suites); 229 } 230 231 static void kunit_exec_list_tests(struct suite_set *suite_set, bool include_attr) 232 { 233 struct kunit_suite * const *suites; 234 struct kunit_case *test_case; 235 236 /* Hack: print a ktap header so kunit.py can find the start of KUnit output. */ 237 pr_info("KTAP version 1\n"); 238 239 for (suites = suite_set->start; suites < suite_set->end; suites++) { 240 /* Print suite name and suite attributes */ 241 pr_info("%s\n", (*suites)->name); 242 if (include_attr) 243 kunit_print_attr((void *)(*suites), false, 0); 244 245 /* Print test case name and attributes in suite */ 246 kunit_suite_for_each_test_case((*suites), test_case) { 247 pr_info("%s.%s\n", (*suites)->name, test_case->name); 248 if (include_attr) 249 kunit_print_attr((void *)test_case, true, 0); 250 } 251 } 252 } 253 254 int kunit_run_all_tests(void) 255 { 256 struct suite_set suite_set = {__kunit_suites_start, __kunit_suites_end}; 257 int err = 0; 258 if (!kunit_enabled()) { 259 pr_info("kunit: disabled\n"); 260 goto out; 261 } 262 263 if (filter_glob_param || filter_param) { 264 suite_set = kunit_filter_suites(&suite_set, filter_glob_param, 265 filter_param, filter_action_param, &err); 266 if (err) { 267 pr_err("kunit executor: error filtering suites: %d\n", err); 268 goto out; 269 } 270 } 271 272 if (!action_param) 273 kunit_exec_run_tests(&suite_set); 274 else if (strcmp(action_param, "list") == 0) 275 kunit_exec_list_tests(&suite_set, false); 276 else if (strcmp(action_param, "list_attr") == 0) 277 kunit_exec_list_tests(&suite_set, true); 278 else 279 pr_err("kunit executor: unknown action '%s'\n", action_param); 280 281 if (filter_glob_param || filter_param) { /* a copy was made of each suite */ 282 kunit_free_suite_set(suite_set); 283 } 284 285 out: 286 kunit_handle_shutdown(); 287 return err; 288 } 289 290 #if IS_BUILTIN(CONFIG_KUNIT_TEST) 291 #include "executor_test.c" 292 #endif 293 294 #endif /* IS_BUILTIN(CONFIG_KUNIT) */ 295