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