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 = -1; 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 < 0) { 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 if (kunit_suite_num_test_cases(suite) > 0) 190 return ((void *) suite->test_cases[0].module_name); 191 else 192 return (void *) ""; 193 } 194 195 /* List of all Test Attributes */ 196 197 static struct kunit_attr kunit_attr_list[] = { 198 { 199 .name = "speed", 200 .get_attr = attr_speed_get, 201 .to_string = attr_speed_to_string, 202 .filter = attr_speed_filter, 203 .attr_default = (void *)KUNIT_SPEED_NORMAL, 204 .print = PRINT_ALWAYS, 205 }, 206 { 207 .name = "module", 208 .get_attr = attr_module_get, 209 .to_string = attr_string_to_string, 210 .filter = attr_string_filter, 211 .attr_default = (void *)"", 212 .print = PRINT_SUITE, 213 } 214 }; 215 216 /* Helper Functions to Access Attributes */ 217 218 const char *kunit_attr_filter_name(struct kunit_attr_filter filter) 219 { 220 return filter.attr->name; 221 } 222 223 void kunit_print_attr(void *test_or_suite, bool is_test, unsigned int test_level) 224 { 225 int i; 226 bool to_free = false; 227 void *attr; 228 const char *attr_name, *attr_str; 229 struct kunit_suite *suite = is_test ? NULL : test_or_suite; 230 struct kunit_case *test = is_test ? test_or_suite : NULL; 231 232 for (i = 0; i < ARRAY_SIZE(kunit_attr_list); i++) { 233 if (kunit_attr_list[i].print == PRINT_NEVER || 234 (test && kunit_attr_list[i].print == PRINT_SUITE)) 235 continue; 236 attr = kunit_attr_list[i].get_attr(test_or_suite, is_test); 237 if (attr) { 238 attr_name = kunit_attr_list[i].name; 239 attr_str = kunit_attr_list[i].to_string(attr, &to_free); 240 if (test) { 241 kunit_log(KERN_INFO, test, "%*s# %s.%s: %s", 242 KUNIT_INDENT_LEN * test_level, "", test->name, 243 attr_name, attr_str); 244 } else { 245 kunit_log(KERN_INFO, suite, "%*s# %s: %s", 246 KUNIT_INDENT_LEN * test_level, "", attr_name, attr_str); 247 } 248 249 /* Free to_string of attribute if needed */ 250 if (to_free) 251 kfree(attr_str); 252 } 253 } 254 } 255 256 /* Helper Functions to Filter Attributes */ 257 258 int kunit_get_filter_count(char *input) 259 { 260 int i, comma_index = 0, count = 0; 261 262 for (i = 0; input[i]; i++) { 263 if (input[i] == ',') { 264 if ((i - comma_index) > 1) 265 count++; 266 comma_index = i; 267 } 268 } 269 if ((i - comma_index) > 0) 270 count++; 271 return count; 272 } 273 274 struct kunit_attr_filter kunit_next_attr_filter(char **filters, int *err) 275 { 276 struct kunit_attr_filter filter = {}; 277 int i, j, comma_index = 0, new_start_index = 0; 278 int op_index = -1, attr_index = -1; 279 char op; 280 char *input = *filters; 281 282 /* Parse input until operation */ 283 for (i = 0; input[i]; i++) { 284 if (op_index < 0 && strchr(op_list, input[i])) { 285 op_index = i; 286 } else if (!comma_index && input[i] == ',') { 287 comma_index = i; 288 } else if (comma_index && input[i] != ' ') { 289 new_start_index = i; 290 break; 291 } 292 } 293 294 if (op_index <= 0) { 295 *err = -EINVAL; 296 pr_err("kunit executor: filter operation not found: %s\n", input); 297 return filter; 298 } 299 300 /* Temporarily set operator to \0 character. */ 301 op = input[op_index]; 302 input[op_index] = '\0'; 303 304 /* Find associated kunit_attr object */ 305 for (j = 0; j < ARRAY_SIZE(kunit_attr_list); j++) { 306 if (!strcmp(input, kunit_attr_list[j].name)) { 307 attr_index = j; 308 break; 309 } 310 } 311 312 input[op_index] = op; 313 314 if (attr_index < 0) { 315 *err = -EINVAL; 316 pr_err("kunit executor: attribute not found: %s\n", input); 317 } else { 318 filter.attr = &kunit_attr_list[attr_index]; 319 } 320 321 if (comma_index > 0) { 322 input[comma_index] = '\0'; 323 filter.input = input + op_index; 324 input = input + new_start_index; 325 } else { 326 filter.input = input + op_index; 327 input = NULL; 328 } 329 330 *filters = input; 331 332 return filter; 333 } 334 335 struct kunit_suite *kunit_filter_attr_tests(const struct kunit_suite *const suite, 336 struct kunit_attr_filter filter, char *action, int *err) 337 { 338 int n = 0; 339 struct kunit_case *filtered, *test_case; 340 struct kunit_suite *copy; 341 void *suite_val, *test_val; 342 bool suite_result, test_result, default_result, result; 343 344 /* Allocate memory for new copy of suite and list of test cases */ 345 copy = kmemdup(suite, sizeof(*copy), GFP_KERNEL); 346 if (!copy) 347 return ERR_PTR(-ENOMEM); 348 349 kunit_suite_for_each_test_case(suite, test_case) { n++; } 350 351 filtered = kcalloc(n + 1, sizeof(*filtered), GFP_KERNEL); 352 if (!filtered) { 353 kfree(copy); 354 return ERR_PTR(-ENOMEM); 355 } 356 357 n = 0; 358 359 /* Save filtering result on default value */ 360 default_result = filter.attr->filter(filter.attr->attr_default, filter.input, err); 361 if (*err) 362 goto err; 363 364 /* Save suite attribute value and filtering result on that value */ 365 suite_val = filter.attr->get_attr((void *)suite, false); 366 suite_result = filter.attr->filter(suite_val, filter.input, err); 367 if (*err) 368 goto err; 369 370 /* For each test case, save test case if passes filtering. */ 371 kunit_suite_for_each_test_case(suite, test_case) { 372 test_val = filter.attr->get_attr((void *) test_case, true); 373 test_result = filter.attr->filter(filter.attr->get_attr(test_case, true), 374 filter.input, err); 375 if (*err) 376 goto err; 377 378 /* 379 * If attribute value of test case is set, filter on that value. 380 * If not, filter on suite value if set. If not, filter on 381 * default value. 382 */ 383 result = false; 384 if (test_val) { 385 if (test_result) 386 result = true; 387 } else if (suite_val) { 388 if (suite_result) 389 result = true; 390 } else if (default_result) { 391 result = true; 392 } 393 394 if (result) { 395 filtered[n++] = *test_case; 396 } else if (action && strcmp(action, "skip") == 0) { 397 test_case->status = KUNIT_SKIPPED; 398 filtered[n++] = *test_case; 399 } 400 } 401 402 err: 403 if (n == 0 || *err) { 404 kfree(copy); 405 kfree(filtered); 406 return NULL; 407 } 408 409 copy->test_cases = filtered; 410 411 return copy; 412 } 413