Alan Maguire | aac3546 | 2020-08-04 13:47:42 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | |
David Gow | b6d5799 | 2021-05-26 14:24:04 -0700 | [diff] [blame] | 3 | #include <linux/reboot.h> |
Alan Maguire | aac3546 | 2020-08-04 13:47:42 -0700 | [diff] [blame] | 4 | #include <kunit/test.h> |
Daniel Latypov | 5d31f71 | 2021-02-05 16:08:52 -0800 | [diff] [blame] | 5 | #include <linux/glob.h> |
| 6 | #include <linux/moduleparam.h> |
Alan Maguire | aac3546 | 2020-08-04 13:47:42 -0700 | [diff] [blame] | 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 * const __kunit_suites_start[]; |
| 13 | extern struct kunit_suite * const * const __kunit_suites_end[]; |
| 14 | |
| 15 | #if IS_BUILTIN(CONFIG_KUNIT) |
| 16 | |
Daniel Latypov | 1d71307 | 2021-04-20 19:04:27 -0700 | [diff] [blame] | 17 | static char *filter_glob_param; |
| 18 | module_param_named(filter_glob, filter_glob_param, charp, 0); |
Daniel Latypov | 5d31f71 | 2021-02-05 16:08:52 -0800 | [diff] [blame] | 19 | MODULE_PARM_DESC(filter_glob, |
| 20 | "Filter which KUnit test suites run at boot-time, e.g. list*"); |
| 21 | |
David Gow | b6d5799 | 2021-05-26 14:24:04 -0700 | [diff] [blame] | 22 | static char *kunit_shutdown; |
| 23 | core_param(kunit_shutdown, kunit_shutdown, charp, 0644); |
| 24 | |
Daniel Latypov | 5d31f71 | 2021-02-05 16:08:52 -0800 | [diff] [blame] | 25 | static struct kunit_suite * const * |
Daniel Latypov | 1d71307 | 2021-04-20 19:04:27 -0700 | [diff] [blame] | 26 | kunit_filter_subsuite(struct kunit_suite * const * const subsuite, |
| 27 | const char *filter_glob) |
Daniel Latypov | 5d31f71 | 2021-02-05 16:08:52 -0800 | [diff] [blame] | 28 | { |
| 29 | int i, n = 0; |
| 30 | struct kunit_suite **filtered; |
| 31 | |
| 32 | n = 0; |
| 33 | for (i = 0; subsuite[i] != NULL; ++i) { |
| 34 | if (glob_match(filter_glob, subsuite[i]->name)) |
| 35 | ++n; |
| 36 | } |
| 37 | |
| 38 | if (n == 0) |
| 39 | return NULL; |
| 40 | |
| 41 | filtered = kmalloc_array(n + 1, sizeof(*filtered), GFP_KERNEL); |
| 42 | if (!filtered) |
| 43 | return NULL; |
| 44 | |
| 45 | n = 0; |
| 46 | for (i = 0; subsuite[i] != NULL; ++i) { |
| 47 | if (glob_match(filter_glob, subsuite[i]->name)) |
| 48 | filtered[n++] = subsuite[i]; |
| 49 | } |
| 50 | filtered[n] = NULL; |
| 51 | |
| 52 | return filtered; |
| 53 | } |
| 54 | |
| 55 | struct suite_set { |
| 56 | struct kunit_suite * const * const *start; |
| 57 | struct kunit_suite * const * const *end; |
| 58 | }; |
| 59 | |
Daniel Latypov | 1d71307 | 2021-04-20 19:04:27 -0700 | [diff] [blame] | 60 | static struct suite_set kunit_filter_suites(const struct suite_set *suite_set, |
| 61 | const char *filter_glob) |
Daniel Latypov | 5d31f71 | 2021-02-05 16:08:52 -0800 | [diff] [blame] | 62 | { |
| 63 | int i; |
| 64 | struct kunit_suite * const **copy, * const *filtered_subsuite; |
| 65 | struct suite_set filtered; |
| 66 | |
Daniel Latypov | 1d71307 | 2021-04-20 19:04:27 -0700 | [diff] [blame] | 67 | const size_t max = suite_set->end - suite_set->start; |
Daniel Latypov | 5d31f71 | 2021-02-05 16:08:52 -0800 | [diff] [blame] | 68 | |
| 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 */ |
| 72 | filtered.end = copy; |
| 73 | return filtered; |
| 74 | } |
| 75 | |
| 76 | for (i = 0; i < max; ++i) { |
Daniel Latypov | 1d71307 | 2021-04-20 19:04:27 -0700 | [diff] [blame] | 77 | filtered_subsuite = kunit_filter_subsuite(suite_set->start[i], filter_glob); |
Daniel Latypov | 5d31f71 | 2021-02-05 16:08:52 -0800 | [diff] [blame] | 78 | if (filtered_subsuite) |
| 79 | *copy++ = filtered_subsuite; |
| 80 | } |
| 81 | filtered.end = copy; |
| 82 | return filtered; |
| 83 | } |
| 84 | |
David Gow | b6d5799 | 2021-05-26 14:24:04 -0700 | [diff] [blame] | 85 | static void kunit_handle_shutdown(void) |
| 86 | { |
| 87 | if (!kunit_shutdown) |
| 88 | return; |
| 89 | |
| 90 | if (!strcmp(kunit_shutdown, "poweroff")) |
| 91 | kernel_power_off(); |
| 92 | else if (!strcmp(kunit_shutdown, "halt")) |
| 93 | kernel_halt(); |
| 94 | else if (!strcmp(kunit_shutdown, "reboot")) |
| 95 | kernel_restart(NULL); |
| 96 | |
| 97 | } |
| 98 | |
Daniel Latypov | 5d31f71 | 2021-02-05 16:08:52 -0800 | [diff] [blame] | 99 | static void kunit_print_tap_header(struct suite_set *suite_set) |
Brendan Higgins | 45dcbb6 | 2020-08-04 13:47:44 -0700 | [diff] [blame] | 100 | { |
| 101 | struct kunit_suite * const * const *suites, * const *subsuite; |
| 102 | int num_of_suites = 0; |
| 103 | |
Daniel Latypov | 5d31f71 | 2021-02-05 16:08:52 -0800 | [diff] [blame] | 104 | for (suites = suite_set->start; suites < suite_set->end; suites++) |
Brendan Higgins | 45dcbb6 | 2020-08-04 13:47:44 -0700 | [diff] [blame] | 105 | for (subsuite = *suites; *subsuite != NULL; subsuite++) |
| 106 | num_of_suites++; |
| 107 | |
| 108 | pr_info("TAP version 14\n"); |
| 109 | pr_info("1..%d\n", num_of_suites); |
| 110 | } |
| 111 | |
Brendan Higgins | 8c0d884 | 2020-08-04 13:47:43 -0700 | [diff] [blame] | 112 | int kunit_run_all_tests(void) |
Alan Maguire | aac3546 | 2020-08-04 13:47:42 -0700 | [diff] [blame] | 113 | { |
| 114 | struct kunit_suite * const * const *suites; |
Daniel Latypov | 1d71307 | 2021-04-20 19:04:27 -0700 | [diff] [blame] | 115 | struct suite_set suite_set = { |
| 116 | .start = __kunit_suites_start, |
| 117 | .end = __kunit_suites_end, |
| 118 | }; |
Alan Maguire | aac3546 | 2020-08-04 13:47:42 -0700 | [diff] [blame] | 119 | |
Daniel Latypov | 1d71307 | 2021-04-20 19:04:27 -0700 | [diff] [blame] | 120 | if (filter_glob_param) |
| 121 | suite_set = kunit_filter_suites(&suite_set, filter_glob_param); |
Brendan Higgins | 45dcbb6 | 2020-08-04 13:47:44 -0700 | [diff] [blame] | 122 | |
Daniel Latypov | 5d31f71 | 2021-02-05 16:08:52 -0800 | [diff] [blame] | 123 | kunit_print_tap_header(&suite_set); |
| 124 | |
| 125 | for (suites = suite_set.start; suites < suite_set.end; suites++) |
| 126 | __kunit_test_suites_init(*suites); |
| 127 | |
Daniel Latypov | 1d71307 | 2021-04-20 19:04:27 -0700 | [diff] [blame] | 128 | if (filter_glob_param) { /* a copy was made of each array */ |
Daniel Latypov | 5d31f71 | 2021-02-05 16:08:52 -0800 | [diff] [blame] | 129 | for (suites = suite_set.start; suites < suite_set.end; suites++) |
| 130 | kfree(*suites); |
| 131 | kfree(suite_set.start); |
| 132 | } |
Alan Maguire | aac3546 | 2020-08-04 13:47:42 -0700 | [diff] [blame] | 133 | |
David Gow | b6d5799 | 2021-05-26 14:24:04 -0700 | [diff] [blame] | 134 | kunit_handle_shutdown(); |
| 135 | |
Alan Maguire | aac3546 | 2020-08-04 13:47:42 -0700 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | |
Daniel Latypov | 1d71307 | 2021-04-20 19:04:27 -0700 | [diff] [blame] | 139 | #if IS_BUILTIN(CONFIG_KUNIT_TEST) |
| 140 | #include "executor_test.c" |
| 141 | #endif |
| 142 | |
Alan Maguire | aac3546 | 2020-08-04 13:47:42 -0700 | [diff] [blame] | 143 | #endif /* IS_BUILTIN(CONFIG_KUNIT) */ |