1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * KUnit API to save and access test attributes 4 * 5 * Copyright (C) 2023, Google LLC. 6 * Author: Rae Moar <rmoar@google.com> 7 */ 8 9 #include <kunit/test.h> 10 #include <kunit/attributes.h> 11 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 16 */ 17 enum print_ops { 18 PRINT_ALWAYS, 19 PRINT_SUITE, 20 PRINT_NEVER, 21 }; 22 23 /** 24 * struct kunit_attr - represents a test attribute and holds flexible 25 * helper functions to interact with attribute. 26 * 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 30 * attribute value 31 * @filter: function to indicate whether a given attribute value passes a 32 * filter 33 */ 34 struct kunit_attr { 35 const char *name; 36 void *(*get_attr)(void *test_or_suite, bool is_test); 37 const char *(*to_string)(void *attr, bool *to_free); 38 int (*filter)(void *attr, const char *input, int *err); 39 void *attr_default; 40 enum print_ops print; 41 }; 42 43 /* String Lists for enum Attributes */ 44 45 static const char * const speed_str_list[] = {"unset", "very_slow", "slow", "normal"}; 46 47 /* To String Methods */ 48 49 static const char *attr_enum_to_string(void *attr, const char * const str_list[], bool *to_free) 50 { 51 long val = (long)attr; 52 53 *to_free = false; 54 if (!val) 55 return NULL; 56 return str_list[val]; 57 } 58 59 static const char *attr_speed_to_string(void *attr, bool *to_free) 60 { 61 return attr_enum_to_string(attr, speed_str_list, to_free); 62 } 63 64 static const char *attr_string_to_string(void *attr, bool *to_free) 65 { 66 *to_free = false; 67 return (char *) attr; 68 } 69 70 /* Filter Methods */ 71 72 static const char op_list[] = "<>!="; 73 74 /* 75 * Returns whether the inputted integer value matches the filter given 76 * by the operation string and inputted integer. 77 */ 78 static int int_filter(long val, const char *op, int input, int *err) 79 { 80 if (!strncmp(op, "<=", 2)) 81 return (val <= input); 82 else if (!strncmp(op, ">=", 2)) 83 return (val >= input); 84 else if (!strncmp(op, "!=", 2)) 85 return (val != input); 86 else if (!strncmp(op, ">", 1)) 87 return (val > input); 88 else if (!strncmp(op, "<", 1)) 89 return (val < input); 90 else if (!strncmp(op, "=", 1)) 91 return (val == input); 92 *err = -EINVAL; 93 pr_err("kunit executor: invalid filter operation: %s\n", op); 94 return false; 95 } 96 97 /* 98 * Returns whether the inputted enum value "attr" matches the filter given 99 * by the input string. Note: the str_list includes the corresponding string 100 * list to the enum values. 101 */ 102 static int attr_enum_filter(void *attr, const char *input, int *err, 103 const char * const str_list[], int max) 104 { 105 int i, j, input_int; 106 long test_val = (long)attr; 107 const char *input_val = NULL; 108 109 for (i = 0; input[i]; i++) { 110 if (!strchr(op_list, input[i])) { 111 input_val = input + i; 112 break; 113 } 114 } 115 116 if (!input_val) { 117 *err = -EINVAL; 118 pr_err("kunit executor: filter value not found: %s\n", input); 119 return false; 120 } 121 122 for (j = 0; j <= max; j++) { 123 if (!strcmp(input_val, str_list[j])) 124 input_int = j; 125 } 126 127 if (!input_int) { 128 *err = -EINVAL; 129 pr_err("kunit executor: invalid filter input: %s\n", input); 130 return false; 131 } 132 133 return int_filter(test_val, input, input_int, err); 134 } 135 136 static int attr_speed_filter(void *attr, const char *input, int *err) 137 { 138 return attr_enum_filter(attr, input, err, speed_str_list, KUNIT_SPEED_MAX); 139 } 140 141 /* 142 * Returns whether the inputted string value (attr) matches the filter given 143 * by the input string. 144 */ 145 static int attr_string_filter(void *attr, const char *input, int *err) 146 { 147 char *str = attr; 148 149 if (!strncmp(input, "<", 1)) { 150 *err = -EINVAL; 151 pr_err("kunit executor: invalid filter input: %s\n", input); 152 return false; 153 } else if (!strncmp(input, ">", 1)) { 154 *err = -EINVAL; 155 pr_err("kunit executor: invalid filter input: %s\n", input); 156 return false; 157 } else if (!strncmp(input, "!=", 2)) { 158 return (strcmp(input + 2, str) != 0); 159 } else if (!strncmp(input, "=", 1)) { 160 return (strcmp(input + 1, str) == 0); 161 } 162 *err = -EINVAL; 163 pr_err("kunit executor: invalid filter operation: %s\n", input); 164 return false; 165 } 166 167 168 /* Get Attribute Methods */ 169 170 static void *attr_speed_get(void *test_or_suite, bool is_test) 171 { 172 struct kunit_suite *suite = is_test ? NULL : test_or_suite; 173 struct kunit_case *test = is_test ? test_or_suite : NULL; 174 175 if (test) 176 return ((void *) test->attr.speed); 177 else 178 return ((void *) suite->attr.speed); 179 } 180 181 static void *attr_module_get(void *test_or_suite, bool is_test) 182 { 183 struct kunit_suite *suite = is_test ? NULL : test_or_suite; 184 struct kunit_case *test = is_test ? test_or_suite : NULL; 185 186 // Suites get their module attribute from their first test_case 187 if (test) 188 return ((void *) test->module_name); 189 else 190 return ((void *) suite->test_cases[0].module_name); 191 } 192 193 /* List of all Test Attributes */ 194 195 static struct kunit_attr kunit_attr_list[] = { 196 { 197 .name = "speed", 198 .get_attr = attr_speed_get, 199 .to_string = attr_speed_to_string, 200 .filter = attr_speed_filter, 201 .attr_default = (void *)KUNIT_SPEED_NORMAL, 202 .print = PRINT_ALWAYS, 203 }, 204 { 205 .name = "module", 206 .get_attr = attr_module_get, 207 .to_string = attr_string_to_string, 208 .filter = attr_string_filter, 209 .attr_default = (void *)"", 210 .print = PRINT_SUITE, 211 } 212 }; 213 214 /* Helper Functions to Access Attributes */ 215 216 const char *kunit_attr_filter_name(struct kunit_attr_filter filter) 217 { 218 return filter.attr->name; 219 } 220 221 void kunit_print_attr(void *test_or_suite, bool is_test, unsigned int test_level) 222 { 223 int i; 224 bool to_free; 225 void *attr; 226 const char *attr_name, *attr_str; 227 struct kunit_suite *suite = is_test ? NULL : test_or_suite; 228 struct kunit_case *test = is_test ? test_or_suite : NULL; 229 230 for (i = 0; i < ARRAY_SIZE(kunit_attr_list); i++) { 231 if (kunit_attr_list[i].print == PRINT_NEVER || 232 (test && kunit_attr_list[i].print == PRINT_SUITE)) 233 continue; 234 attr = kunit_attr_list[i].get_attr(test_or_suite, is_test); 235 if (attr) { 236 attr_name = kunit_attr_list[i].name; 237 attr_str = kunit_attr_list[i].to_string(attr, &to_free); 238 if (test) { 239 kunit_log(KERN_INFO, test, "%*s# %s.%s: %s", 240 KUNIT_INDENT_LEN * test_level, "", test->name, 241 attr_name, attr_str); 242 } else { 243 kunit_log(KERN_INFO, suite, "%*s# %s: %s", 244 KUNIT_INDENT_LEN * test_level, "", attr_name, attr_str); 245 } 246 247 /* Free to_string of attribute if needed */ 248 if (to_free) 249 kfree(attr_str); 250 } 251 } 252 } 253 254 /* Helper Functions to Filter Attributes */ 255 256 int kunit_get_filter_count(char *input) 257 { 258 int i, comma_index, count = 0; 259 260 for (i = 0; input[i]; i++) { 261 if (input[i] == ',') { 262 if ((i - comma_index) > 1) 263 count++; 264 comma_index = i; 265 } 266 } 267 if ((i - comma_index) > 0) 268 count++; 269 return count; 270 } 271 272 struct kunit_attr_filter kunit_next_attr_filter(char **filters, int *err) 273 { 274 struct kunit_attr_filter filter = {}; 275 int i, j, comma_index, new_start_index; 276 int op_index = -1, attr_index = -1; 277 char op; 278 char *input = *filters; 279 280 /* Parse input until operation */ 281 for (i = 0; input[i]; i++) { 282 if (op_index < 0 && strchr(op_list, input[i])) { 283 op_index = i; 284 } else if (!comma_index && input[i] == ',') { 285 comma_index = i; 286 } else if (comma_index && input[i] != ' ') { 287 new_start_index = i; 288 break; 289 } 290 } 291 292 if (op_index <= 0) { 293 *err = -EINVAL; 294 pr_err("kunit executor: filter operation not found: %s\n", input); 295 return filter; 296 } 297 298 /* Temporarily set operator to \0 character. */ 299 op = input[op_index]; 300 input[op_index] = '\0'; 301 302 /* Find associated kunit_attr object */ 303 for (j = 0; j < ARRAY_SIZE(kunit_attr_list); j++) { 304 if (!strcmp(input, kunit_attr_list[j].name)) { 305 attr_index = j; 306 break; 307 } 308 } 309 310 input[op_index] = op; 311 312 if (attr_index < 0) { 313 *err = -EINVAL; 314 pr_err("kunit executor: attribute not found: %s\n", input); 315 } else { 316 filter.attr = &kunit_attr_list[attr_index]; 317 } 318 319 if (comma_index) { 320 input[comma_index] = '\0'; 321 filter.input = input + op_index; 322 input = input + new_start_index; 323 } else { 324 filter.input = input + op_index; 325 input = NULL; 326 } 327 328 *filters = input; 329 330 return filter; 331 } 332 333 struct kunit_suite *kunit_filter_attr_tests(const struct kunit_suite *const suite, 334 struct kunit_attr_filter filter, char *action, int *err) 335 { 336 int n = 0; 337 struct kunit_case *filtered, *test_case; 338 struct kunit_suite *copy; 339 void *suite_val, *test_val; 340 bool suite_result, test_result, default_result, result; 341 342 /* Allocate memory for new copy of suite and list of test cases */ 343 copy = kmemdup(suite, sizeof(*copy), GFP_KERNEL); 344 if (!copy) 345 return ERR_PTR(-ENOMEM); 346 347 kunit_suite_for_each_test_case(suite, test_case) { n++; } 348 349 filtered = kcalloc(n + 1, sizeof(*filtered), GFP_KERNEL); 350 if (!filtered) { 351 kfree(copy); 352 return ERR_PTR(-ENOMEM); 353 } 354 355 n = 0; 356 357 /* Save filtering result on default value */ 358 default_result = filter.attr->filter(filter.attr->attr_default, filter.input, err); 359 if (*err) { 360 kfree(copy); 361 kfree(filtered); 362 return NULL; 363 } 364 365 /* Save suite attribute value and filtering result on that value */ 366 suite_val = filter.attr->get_attr((void *)suite, false); 367 suite_result = filter.attr->filter(suite_val, filter.input, err); 368 if (*err) { 369 kfree(copy); 370 kfree(filtered); 371 return NULL; 372 } 373 374 /* For each test case, save test case if passes filtering. */ 375 kunit_suite_for_each_test_case(suite, test_case) { 376 test_val = filter.attr->get_attr((void *) test_case, true); 377 test_result = filter.attr->filter(filter.attr->get_attr(test_case, true), 378 filter.input, err); 379 if (*err) { 380 kfree(copy); 381 kfree(filtered); 382 return NULL; 383 } 384 385 /* 386 * If attribute value of test case is set, filter on that value. 387 * If not, filter on suite value if set. If not, filter on 388 * default value. 389 */ 390 result = false; 391 if (test_val) { 392 if (test_result) 393 result = true; 394 } else if (suite_val) { 395 if (suite_result) 396 result = true; 397 } else if (default_result) { 398 result = true; 399 } 400 401 if (result) { 402 filtered[n++] = *test_case; 403 } else if (action && strcmp(action, "skip") == 0) { 404 test_case->status = KUNIT_SKIPPED; 405 filtered[n++] = *test_case; 406 } 407 } 408 409 if (n == 0) { 410 kfree(copy); 411 kfree(filtered); 412 return NULL; 413 } 414 415 copy->test_cases = filtered; 416 417 return copy; 418 } 419