1 // SPDX-License-Identifier: GPL-2.0
3 * KUnit API to save and access test attributes
5 * Copyright (C) 2023, Google LLC.
6 * Author: Rae Moar <rmoar@google.com>
9 #include <kunit/test.h>
10 #include <kunit/attributes.h>
12 /* Options for printing attributes:
13 * PRINT_ALWAYS - attribute is printed for every test case and suite if set
14 * PRINT_SUITE - attribute is printed for every suite if set but not for test cases
15 * PRINT_NEVER - attribute is never printed
24 * struct kunit_attr - represents a test attribute and holds flexible
25 * helper functions to interact with attribute.
27 * @name: name of test attribute, eg. speed
28 * @get_attr: function to return attribute value given a test
29 * @to_string: function to return string representation of given
31 * @filter: function to indicate whether a given attribute value passes a
33 * @attr_default: default attribute value used during filtering
34 * @print: value of enum print_ops to indicate when to print attribute
38 void *(*get_attr)(void *test_or_suite, bool is_test);
39 const char *(*to_string)(void *attr, bool *to_free);
40 int (*filter)(void *attr, const char *input, int *err);
45 /* String Lists for enum Attributes */
47 static const char * const speed_str_list[] = {"unset", "very_slow", "slow", "normal"};
49 /* To String Methods */
51 static const char *attr_enum_to_string(void *attr, const char * const str_list[], bool *to_free)
53 long val = (long)attr;
61 static const char *attr_speed_to_string(void *attr, bool *to_free)
63 return attr_enum_to_string(attr, speed_str_list, to_free);
66 static const char *attr_string_to_string(void *attr, bool *to_free)
74 static const char op_list[] = "<>!=";
77 * Returns whether the inputted integer value matches the filter given
78 * by the operation string and inputted integer.
80 static int int_filter(long val, const char *op, int input, int *err)
82 if (!strncmp(op, "<=", 2))
83 return (val <= input);
84 else if (!strncmp(op, ">=", 2))
85 return (val >= input);
86 else if (!strncmp(op, "!=", 2))
87 return (val != input);
88 else if (!strncmp(op, ">", 1))
90 else if (!strncmp(op, "<", 1))
92 else if (!strncmp(op, "=", 1))
93 return (val == input);
95 pr_err("kunit executor: invalid filter operation: %s\n", op);
100 * Returns whether the inputted enum value "attr" matches the filter given
101 * by the input string. Note: the str_list includes the corresponding string
102 * list to the enum values.
104 static int attr_enum_filter(void *attr, const char *input, int *err,
105 const char * const str_list[], int max)
107 int i, j, input_int = -1;
108 long test_val = (long)attr;
109 const char *input_val = NULL;
111 for (i = 0; input[i]; i++) {
112 if (!strchr(op_list, input[i])) {
113 input_val = input + i;
120 pr_err("kunit executor: filter value not found: %s\n", input);
124 for (j = 0; j <= max; j++) {
125 if (!strcmp(input_val, str_list[j]))
131 pr_err("kunit executor: invalid filter input: %s\n", input);
135 return int_filter(test_val, input, input_int, err);
138 static int attr_speed_filter(void *attr, const char *input, int *err)
140 return attr_enum_filter(attr, input, err, speed_str_list, KUNIT_SPEED_MAX);
144 * Returns whether the inputted string value (attr) matches the filter given
145 * by the input string.
147 static int attr_string_filter(void *attr, const char *input, int *err)
151 if (!strncmp(input, "<", 1)) {
153 pr_err("kunit executor: invalid filter input: %s\n", input);
155 } else if (!strncmp(input, ">", 1)) {
157 pr_err("kunit executor: invalid filter input: %s\n", input);
159 } else if (!strncmp(input, "!=", 2)) {
160 return (strcmp(input + 2, str) != 0);
161 } else if (!strncmp(input, "=", 1)) {
162 return (strcmp(input + 1, str) == 0);
165 pr_err("kunit executor: invalid filter operation: %s\n", input);
170 /* Get Attribute Methods */
172 static void *attr_speed_get(void *test_or_suite, bool is_test)
174 struct kunit_suite *suite = is_test ? NULL : test_or_suite;
175 struct kunit_case *test = is_test ? test_or_suite : NULL;
178 return ((void *) test->attr.speed);
180 return ((void *) suite->attr.speed);
183 static void *attr_module_get(void *test_or_suite, bool is_test)
185 struct kunit_suite *suite = is_test ? NULL : test_or_suite;
186 struct kunit_case *test = is_test ? test_or_suite : NULL;
188 // Suites get their module attribute from their first test_case
190 return ((void *) test->module_name);
191 else if (kunit_suite_num_test_cases(suite) > 0)
192 return ((void *) suite->test_cases[0].module_name);
197 /* List of all Test Attributes */
199 static struct kunit_attr kunit_attr_list[] = {
202 .get_attr = attr_speed_get,
203 .to_string = attr_speed_to_string,
204 .filter = attr_speed_filter,
205 .attr_default = (void *)KUNIT_SPEED_NORMAL,
206 .print = PRINT_ALWAYS,
210 .get_attr = attr_module_get,
211 .to_string = attr_string_to_string,
212 .filter = attr_string_filter,
213 .attr_default = (void *)"",
214 .print = PRINT_SUITE,
218 /* Helper Functions to Access Attributes */
220 const char *kunit_attr_filter_name(struct kunit_attr_filter filter)
222 return filter.attr->name;
225 void kunit_print_attr(void *test_or_suite, bool is_test, unsigned int test_level)
228 bool to_free = false;
230 const char *attr_name, *attr_str;
231 struct kunit_suite *suite = is_test ? NULL : test_or_suite;
232 struct kunit_case *test = is_test ? test_or_suite : NULL;
234 for (i = 0; i < ARRAY_SIZE(kunit_attr_list); i++) {
235 if (kunit_attr_list[i].print == PRINT_NEVER ||
236 (test && kunit_attr_list[i].print == PRINT_SUITE))
238 attr = kunit_attr_list[i].get_attr(test_or_suite, is_test);
240 attr_name = kunit_attr_list[i].name;
241 attr_str = kunit_attr_list[i].to_string(attr, &to_free);
243 kunit_log(KERN_INFO, test, "%*s# %s.%s: %s",
244 KUNIT_INDENT_LEN * test_level, "", test->name,
245 attr_name, attr_str);
247 kunit_log(KERN_INFO, suite, "%*s# %s: %s",
248 KUNIT_INDENT_LEN * test_level, "", attr_name, attr_str);
251 /* Free to_string of attribute if needed */
258 /* Helper Functions to Filter Attributes */
260 int kunit_get_filter_count(char *input)
262 int i, comma_index = 0, count = 0;
264 for (i = 0; input[i]; i++) {
265 if (input[i] == ',') {
266 if ((i - comma_index) > 1)
271 if ((i - comma_index) > 0)
276 struct kunit_attr_filter kunit_next_attr_filter(char **filters, int *err)
278 struct kunit_attr_filter filter = {};
279 int i, j, comma_index = 0, new_start_index = 0;
280 int op_index = -1, attr_index = -1;
282 char *input = *filters;
284 /* Parse input until operation */
285 for (i = 0; input[i]; i++) {
286 if (op_index < 0 && strchr(op_list, input[i])) {
288 } else if (!comma_index && input[i] == ',') {
290 } else if (comma_index && input[i] != ' ') {
298 pr_err("kunit executor: filter operation not found: %s\n", input);
302 /* Temporarily set operator to \0 character. */
303 op = input[op_index];
304 input[op_index] = '\0';
306 /* Find associated kunit_attr object */
307 for (j = 0; j < ARRAY_SIZE(kunit_attr_list); j++) {
308 if (!strcmp(input, kunit_attr_list[j].name)) {
314 input[op_index] = op;
316 if (attr_index < 0) {
318 pr_err("kunit executor: attribute not found: %s\n", input);
320 filter.attr = &kunit_attr_list[attr_index];
323 if (comma_index > 0) {
324 input[comma_index] = '\0';
325 filter.input = input + op_index;
326 input = input + new_start_index;
328 filter.input = input + op_index;
337 struct kunit_suite *kunit_filter_attr_tests(const struct kunit_suite *const suite,
338 struct kunit_attr_filter filter, char *action, int *err)
341 struct kunit_case *filtered, *test_case;
342 struct kunit_suite *copy;
343 void *suite_val, *test_val;
344 bool suite_result, test_result, default_result, result;
346 /* Allocate memory for new copy of suite and list of test cases */
347 copy = kmemdup(suite, sizeof(*copy), GFP_KERNEL);
349 return ERR_PTR(-ENOMEM);
351 kunit_suite_for_each_test_case(suite, test_case) { n++; }
353 filtered = kcalloc(n + 1, sizeof(*filtered), GFP_KERNEL);
356 return ERR_PTR(-ENOMEM);
361 /* Save filtering result on default value */
362 default_result = filter.attr->filter(filter.attr->attr_default, filter.input, err);
366 /* Save suite attribute value and filtering result on that value */
367 suite_val = filter.attr->get_attr((void *)suite, false);
368 suite_result = filter.attr->filter(suite_val, filter.input, err);
372 /* For each test case, save test case if passes filtering. */
373 kunit_suite_for_each_test_case(suite, test_case) {
374 test_val = filter.attr->get_attr((void *) test_case, true);
375 test_result = filter.attr->filter(filter.attr->get_attr(test_case, true),
381 * If attribute value of test case is set, filter on that value.
382 * If not, filter on suite value if set. If not, filter on
389 } else if (suite_val) {
392 } else if (default_result) {
397 filtered[n++] = *test_case;
398 } else if (action && strcmp(action, "skip") == 0) {
399 test_case->status = KUNIT_SKIPPED;
400 filtered[n++] = *test_case;
405 if (n == 0 || *err) {
411 copy->test_cases = filtered;