1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Base unit test (KUnit) API. 4 * 5 * Copyright (C) 2019, Google LLC. 6 * Author: Brendan Higgins <brendanhiggins@google.com> 7 */ 8 9 #ifndef _KUNIT_TEST_H 10 #define _KUNIT_TEST_H 11 12 #include <kunit/assert.h> 13 #include <kunit/try-catch.h> 14 15 #include <linux/args.h> 16 #include <linux/compiler.h> 17 #include <linux/container_of.h> 18 #include <linux/err.h> 19 #include <linux/init.h> 20 #include <linux/jump_label.h> 21 #include <linux/kconfig.h> 22 #include <linux/kref.h> 23 #include <linux/list.h> 24 #include <linux/module.h> 25 #include <linux/slab.h> 26 #include <linux/spinlock.h> 27 #include <linux/string.h> 28 #include <linux/types.h> 29 30 #include <asm/rwonce.h> 31 #include <asm/sections.h> 32 33 /* Static key: true if any KUnit tests are currently running */ 34 DECLARE_STATIC_KEY_FALSE(kunit_running); 35 36 struct kunit; 37 struct string_stream; 38 39 /* Maximum size of parameter description string. */ 40 #define KUNIT_PARAM_DESC_SIZE 128 41 42 /* Maximum size of a status comment. */ 43 #define KUNIT_STATUS_COMMENT_SIZE 256 44 45 /* 46 * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a 47 * sub-subtest. See the "Subtests" section in 48 * https://node-tap.org/tap-protocol/ 49 */ 50 #define KUNIT_INDENT_LEN 4 51 #define KUNIT_SUBTEST_INDENT " " 52 #define KUNIT_SUBSUBTEST_INDENT " " 53 54 /** 55 * enum kunit_status - Type of result for a test or test suite 56 * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped 57 * @KUNIT_FAILURE: Denotes the test has failed. 58 * @KUNIT_SKIPPED: Denotes the test has been skipped. 59 */ 60 enum kunit_status { 61 KUNIT_SUCCESS, 62 KUNIT_FAILURE, 63 KUNIT_SKIPPED, 64 }; 65 66 /* Attribute struct/enum definitions */ 67 68 /* 69 * Speed Attribute is stored as an enum and separated into categories of 70 * speed: very_slow, slow, and normal. These speeds are relative to 71 * other KUnit tests. 72 * 73 * Note: unset speed attribute acts as default of KUNIT_SPEED_NORMAL. 74 */ 75 enum kunit_speed { 76 KUNIT_SPEED_UNSET, 77 KUNIT_SPEED_VERY_SLOW, 78 KUNIT_SPEED_SLOW, 79 KUNIT_SPEED_NORMAL, 80 KUNIT_SPEED_MAX = KUNIT_SPEED_NORMAL, 81 }; 82 83 /* Holds attributes for each test case and suite */ 84 struct kunit_attributes { 85 enum kunit_speed speed; 86 }; 87 88 /** 89 * struct kunit_case - represents an individual test case. 90 * 91 * @run_case: the function representing the actual test case. 92 * @name: the name of the test case. 93 * @generate_params: the generator function for parameterized tests. 94 * @attr: the attributes associated with the test 95 * @param_init: The init function to run before a parameterized test. 96 * @param_exit: The exit function to run after a parameterized test. 97 * 98 * A test case is a function with the signature, 99 * ``void (*)(struct kunit *)`` 100 * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and 101 * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated 102 * with a &struct kunit_suite and will be run after the suite's init 103 * function and followed by the suite's exit function. 104 * 105 * A test case should be static and should only be created with the 106 * KUNIT_CASE() macro; additionally, every array of test cases should be 107 * terminated with an empty test case. 108 * 109 * Example: 110 * 111 * .. code-block:: c 112 * 113 * void add_test_basic(struct kunit *test) 114 * { 115 * KUNIT_EXPECT_EQ(test, 1, add(1, 0)); 116 * KUNIT_EXPECT_EQ(test, 2, add(1, 1)); 117 * KUNIT_EXPECT_EQ(test, 0, add(-1, 1)); 118 * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX)); 119 * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN)); 120 * } 121 * 122 * static struct kunit_case example_test_cases[] = { 123 * KUNIT_CASE(add_test_basic), 124 * {} 125 * }; 126 * 127 */ 128 struct kunit_case { 129 void (*run_case)(struct kunit *test); 130 const char *name; 131 const void* (*generate_params)(struct kunit *test, 132 const void *prev, char *desc); 133 struct kunit_attributes attr; 134 int (*param_init)(struct kunit *test); 135 void (*param_exit)(struct kunit *test); 136 137 /* private: internal use only. */ 138 enum kunit_status status; 139 char *module_name; 140 struct string_stream *log; 141 }; 142 143 static inline char *kunit_status_to_ok_not_ok(enum kunit_status status) 144 { 145 switch (status) { 146 case KUNIT_SKIPPED: 147 case KUNIT_SUCCESS: 148 return "ok"; 149 case KUNIT_FAILURE: 150 return "not ok"; 151 } 152 return "invalid"; 153 } 154 155 /** 156 * KUNIT_CASE - A helper for creating a &struct kunit_case 157 * 158 * @test_name: a reference to a test case function. 159 * 160 * Takes a symbol for a function representing a test case and creates a 161 * &struct kunit_case object from it. See the documentation for 162 * &struct kunit_case for an example on how to use it. 163 */ 164 #define KUNIT_CASE(test_name) \ 165 { .run_case = test_name, .name = #test_name, \ 166 .module_name = KBUILD_MODNAME} 167 168 /** 169 * KUNIT_CASE_ATTR - A helper for creating a &struct kunit_case 170 * with attributes 171 * 172 * @test_name: a reference to a test case function. 173 * @attributes: a reference to a struct kunit_attributes object containing 174 * test attributes 175 */ 176 #define KUNIT_CASE_ATTR(test_name, attributes) \ 177 { .run_case = test_name, .name = #test_name, \ 178 .attr = attributes, .module_name = KBUILD_MODNAME} 179 180 /** 181 * KUNIT_CASE_SLOW - A helper for creating a &struct kunit_case 182 * with the slow attribute 183 * 184 * @test_name: a reference to a test case function. 185 */ 186 187 #define KUNIT_CASE_SLOW(test_name) \ 188 { .run_case = test_name, .name = #test_name, \ 189 .attr.speed = KUNIT_SPEED_SLOW, .module_name = KBUILD_MODNAME} 190 191 /** 192 * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case 193 * 194 * @test_name: a reference to a test case function. 195 * @gen_params: a reference to a parameter generator function. 196 * 197 * The generator function:: 198 * 199 * const void* gen_params(const void *prev, char *desc) 200 * 201 * is used to lazily generate a series of arbitrarily typed values that fit into 202 * a void*. The argument @prev is the previously returned value, which should be 203 * used to derive the next value; @prev is set to NULL on the initial generator 204 * call. When no more values are available, the generator must return NULL. 205 * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE) 206 * describing the parameter. 207 */ 208 #define KUNIT_CASE_PARAM(test_name, gen_params) \ 209 { .run_case = test_name, .name = #test_name, \ 210 .generate_params = gen_params, .module_name = KBUILD_MODNAME} 211 212 /** 213 * KUNIT_CASE_PARAM_ATTR - A helper for creating a parameterized &struct 214 * kunit_case with attributes 215 * 216 * @test_name: a reference to a test case function. 217 * @gen_params: a reference to a parameter generator function. 218 * @attributes: a reference to a struct kunit_attributes object containing 219 * test attributes 220 */ 221 #define KUNIT_CASE_PARAM_ATTR(test_name, gen_params, attributes) \ 222 { .run_case = test_name, .name = #test_name, \ 223 .generate_params = gen_params, \ 224 .attr = attributes, .module_name = KBUILD_MODNAME} 225 226 /** 227 * KUNIT_CASE_PARAM_WITH_INIT - Define a parameterized KUnit test case with custom 228 * param_init() and param_exit() functions. 229 * @test_name: The function implementing the test case. 230 * @gen_params: The function to generate parameters for the test case. 231 * @init: A reference to the param_init() function to run before a parameterized test. 232 * @exit: A reference to the param_exit() function to run after a parameterized test. 233 * 234 * Provides the option to register param_init() and param_exit() functions. 235 * param_init/exit will be passed the parameterized test context and run once 236 * before and once after the parameterized test. The init function can be used 237 * to add resources to share between parameter runs, pass parameter arrays, 238 * and any other setup logic. The exit function can be used to clean up resources 239 * that were not managed by the parameterized test, and any other teardown logic. 240 * 241 * Note: If you are registering a parameter array in param_init() with 242 * kunit_register_param_array() then you need to pass kunit_array_gen_params() 243 * to this as the generator function. 244 */ 245 #define KUNIT_CASE_PARAM_WITH_INIT(test_name, gen_params, init, exit) \ 246 { .run_case = test_name, .name = #test_name, \ 247 .generate_params = gen_params, \ 248 .param_init = init, .param_exit = exit, \ 249 .module_name = KBUILD_MODNAME} 250 251 /** 252 * struct kunit_suite - describes a related collection of &struct kunit_case 253 * 254 * @name: the name of the test. Purely informational. 255 * @suite_init: called once per test suite before the test cases. 256 * @suite_exit: called once per test suite after all test cases. 257 * @init: called before every test case. 258 * @exit: called after every test case. 259 * @test_cases: a null terminated array of test cases. 260 * @attr: the attributes associated with the test suite 261 * 262 * A kunit_suite is a collection of related &struct kunit_case s, such that 263 * @init is called before every test case and @exit is called after every 264 * test case, similar to the notion of a *test fixture* or a *test class* 265 * in other unit testing frameworks like JUnit or Googletest. 266 * 267 * Note that @exit and @suite_exit will run even if @init or @suite_init 268 * fail: make sure they can handle any inconsistent state which may result. 269 * 270 * Every &struct kunit_case must be associated with a kunit_suite for KUnit 271 * to run it. 272 */ 273 struct kunit_suite { 274 const char name[256]; 275 int (*suite_init)(struct kunit_suite *suite); 276 void (*suite_exit)(struct kunit_suite *suite); 277 int (*init)(struct kunit *test); 278 void (*exit)(struct kunit *test); 279 struct kunit_case *test_cases; 280 struct kunit_attributes attr; 281 282 /* private: internal use only */ 283 char status_comment[KUNIT_STATUS_COMMENT_SIZE]; 284 struct dentry *debugfs; 285 struct string_stream *log; 286 int suite_init_err; 287 bool is_init; 288 }; 289 290 /* Stores an array of suites, end points one past the end */ 291 struct kunit_suite_set { 292 struct kunit_suite * const *start; 293 struct kunit_suite * const *end; 294 }; 295 296 /* Stores the pointer to the parameter array and its metadata. */ 297 struct kunit_params { 298 /* 299 * Reference to the parameter array for a parameterized test. This 300 * is NULL if a parameter array wasn't directly passed to the 301 * parameterized test context struct kunit via kunit_register_params_array(). 302 */ 303 const void *params; 304 /* Reference to a function that gets the description of a parameter. */ 305 void (*get_description)(struct kunit *test, const void *param, char *desc); 306 size_t num_params; 307 size_t elem_size; 308 }; 309 310 /** 311 * struct kunit - represents a running instance of a test. 312 * 313 * @priv: for user to store arbitrary data. Commonly used to pass data 314 * created in the init function (see &struct kunit_suite). 315 * @parent: reference to the parent context of type struct kunit that can 316 * be used for storing shared resources. 317 * @params_array: for storing the parameter array. 318 * 319 * Used to store information about the current context under which the test 320 * is running. Most of this data is private and should only be accessed 321 * indirectly via public functions; the exceptions are @priv, @parent and 322 * @params_array which can be used by the test writer to store arbitrary data, 323 * access the parent context, and to store the parameter array, respectively. 324 */ 325 struct kunit { 326 void *priv; 327 struct kunit *parent; 328 struct kunit_params params_array; 329 330 /* private: internal use only. */ 331 const char *name; /* Read only after initialization! */ 332 struct string_stream *log; /* Points at case log after initialization */ 333 struct kunit_try_catch try_catch; 334 /* param_value is the current parameter value for a test case. */ 335 const void *param_value; 336 /* param_index stores the index of the parameter in parameterized tests. */ 337 int param_index; 338 /* 339 * success starts as true, and may only be set to false during a 340 * test case; thus, it is safe to update this across multiple 341 * threads using WRITE_ONCE; however, as a consequence, it may only 342 * be read after the test case finishes once all threads associated 343 * with the test case have terminated. 344 */ 345 spinlock_t lock; /* Guards all mutable test state. */ 346 enum kunit_status status; /* Read only after test_case finishes! */ 347 /* 348 * Because resources is a list that may be updated multiple times (with 349 * new resources) from any thread associated with a test case, we must 350 * protect it with some type of lock. 351 */ 352 struct list_head resources; /* Protected by lock. */ 353 354 char status_comment[KUNIT_STATUS_COMMENT_SIZE]; 355 /* Saves the last seen test. Useful to help with faults. */ 356 struct kunit_loc last_seen; 357 }; 358 359 static inline void kunit_set_failure(struct kunit *test) 360 { 361 WRITE_ONCE(test->status, KUNIT_FAILURE); 362 } 363 364 bool kunit_enabled(void); 365 bool kunit_autorun(void); 366 const char *kunit_action(void); 367 const char *kunit_filter_glob(void); 368 char *kunit_filter(void); 369 char *kunit_filter_action(void); 370 371 void kunit_init_test(struct kunit *test, const char *name, struct string_stream *log); 372 373 int kunit_run_tests(struct kunit_suite *suite); 374 375 size_t kunit_suite_num_test_cases(struct kunit_suite *suite); 376 377 unsigned int kunit_test_case_num(struct kunit_suite *suite, 378 struct kunit_case *test_case); 379 380 struct kunit_suite_set 381 kunit_filter_suites(const struct kunit_suite_set *suite_set, 382 const char *filter_glob, 383 char *filters, 384 char *filter_action, 385 int *err); 386 void kunit_free_suite_set(struct kunit_suite_set suite_set); 387 388 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites, 389 bool run_tests); 390 391 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites); 392 393 void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin); 394 void kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr); 395 396 struct kunit_suite_set kunit_merge_suite_sets(struct kunit_suite_set init_suite_set, 397 struct kunit_suite_set suite_set); 398 399 const void *kunit_array_gen_params(struct kunit *test, const void *prev, char *desc); 400 401 #if IS_BUILTIN(CONFIG_KUNIT) 402 int kunit_run_all_tests(void); 403 #else 404 static inline int kunit_run_all_tests(void) 405 { 406 return 0; 407 } 408 #endif /* IS_BUILTIN(CONFIG_KUNIT) */ 409 410 #define __kunit_test_suites(unique_array, ...) \ 411 static struct kunit_suite *unique_array[] \ 412 __aligned(sizeof(struct kunit_suite *)) \ 413 __used __section(".kunit_test_suites") = { __VA_ARGS__ } 414 415 /** 416 * kunit_test_suites() - used to register one or more &struct kunit_suite 417 * with KUnit. 418 * 419 * @__suites: a statically allocated list of &struct kunit_suite. 420 * 421 * Registers @suites with the test framework. 422 * This is done by placing the array of struct kunit_suite * in the 423 * .kunit_test_suites ELF section. 424 * 425 * When builtin, KUnit tests are all run via the executor at boot, and when 426 * built as a module, they run on module load. 427 * 428 */ 429 #define kunit_test_suites(__suites...) \ 430 __kunit_test_suites(__UNIQUE_ID(array), \ 431 ##__suites) 432 433 #define kunit_test_suite(suite) kunit_test_suites(&suite) 434 435 #define __kunit_init_test_suites(unique_array, ...) \ 436 static struct kunit_suite *unique_array[] \ 437 __aligned(sizeof(struct kunit_suite *)) \ 438 __used __section(".kunit_init_test_suites") = { __VA_ARGS__ } 439 440 /** 441 * kunit_test_init_section_suites() - used to register one or more &struct 442 * kunit_suite containing init functions or 443 * init data. 444 * 445 * @__suites: a statically allocated list of &struct kunit_suite. 446 * 447 * This functions similar to kunit_test_suites() except that it compiles the 448 * list of suites during init phase. 449 * 450 * This macro also suffixes the array and suite declarations it makes with 451 * _probe; so that modpost suppresses warnings about referencing init data 452 * for symbols named in this manner. 453 * 454 * Note: these init tests are not able to be run after boot so there is no 455 * "run" debugfs file generated for these tests. 456 * 457 * Also, do not mark the suite or test case structs with __initdata because 458 * they will be used after the init phase with debugfs. 459 */ 460 #define kunit_test_init_section_suites(__suites...) \ 461 __kunit_init_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \ 462 ##__suites) 463 464 #define kunit_test_init_section_suite(suite) \ 465 kunit_test_init_section_suites(&suite) 466 467 #define kunit_suite_for_each_test_case(suite, test_case) \ 468 for (test_case = suite->test_cases; test_case->run_case; test_case++) 469 470 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite); 471 472 /** 473 * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*. 474 * @test: The test context object. 475 * @n: number of elements. 476 * @size: The size in bytes of the desired memory. 477 * @gfp: flags passed to underlying kmalloc(). 478 * 479 * Just like `kmalloc_array(...)`, except the allocation is managed by the test case 480 * and is automatically cleaned up after the test case concludes. See kunit_add_action() 481 * for more information. 482 * 483 * Note that some internal context data is also allocated with GFP_KERNEL, 484 * regardless of the gfp passed in. 485 */ 486 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp); 487 488 /** 489 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*. 490 * @test: The test context object. 491 * @size: The size in bytes of the desired memory. 492 * @gfp: flags passed to underlying kmalloc(). 493 * 494 * See kmalloc() and kunit_kmalloc_array() for more information. 495 * 496 * Note that some internal context data is also allocated with GFP_KERNEL, 497 * regardless of the gfp passed in. 498 */ 499 static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp) 500 { 501 return kunit_kmalloc_array(test, 1, size, gfp); 502 } 503 504 /** 505 * kunit_kfree() - Like kfree except for allocations managed by KUnit. 506 * @test: The test case to which the resource belongs. 507 * @ptr: The memory allocation to free. 508 */ 509 void kunit_kfree(struct kunit *test, const void *ptr); 510 511 /** 512 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation. 513 * @test: The test context object. 514 * @size: The size in bytes of the desired memory. 515 * @gfp: flags passed to underlying kmalloc(). 516 * 517 * See kzalloc() and kunit_kmalloc_array() for more information. 518 */ 519 static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp) 520 { 521 return kunit_kmalloc(test, size, gfp | __GFP_ZERO); 522 } 523 524 /** 525 * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation. 526 * @test: The test context object. 527 * @n: number of elements. 528 * @size: The size in bytes of the desired memory. 529 * @gfp: flags passed to underlying kmalloc(). 530 * 531 * See kcalloc() and kunit_kmalloc_array() for more information. 532 */ 533 static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp) 534 { 535 return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO); 536 } 537 538 539 /** 540 * kunit_kfree_const() - conditionally free test managed memory 541 * @test: The test context object. 542 * @x: pointer to the memory 543 * 544 * Calls kunit_kfree() only if @x is not in .rodata section. 545 * See kunit_kstrdup_const() for more information. 546 */ 547 void kunit_kfree_const(struct kunit *test, const void *x); 548 549 /** 550 * kunit_kstrdup() - Duplicates a string into a test managed allocation. 551 * 552 * @test: The test context object. 553 * @str: The NULL-terminated string to duplicate. 554 * @gfp: flags passed to underlying kmalloc(). 555 * 556 * See kstrdup() and kunit_kmalloc_array() for more information. 557 */ 558 static inline char *kunit_kstrdup(struct kunit *test, const char *str, gfp_t gfp) 559 { 560 size_t len; 561 char *buf; 562 563 if (!str) 564 return NULL; 565 566 len = strlen(str) + 1; 567 buf = kunit_kmalloc(test, len, gfp); 568 if (buf) 569 memcpy(buf, str, len); 570 return buf; 571 } 572 573 /** 574 * kunit_kstrdup_const() - Conditionally duplicates a string into a test managed allocation. 575 * 576 * @test: The test context object. 577 * @str: The NULL-terminated string to duplicate. 578 * @gfp: flags passed to underlying kmalloc(). 579 * 580 * Calls kunit_kstrdup() only if @str is not in the rodata section. Must be freed with 581 * kunit_kfree_const() -- not kunit_kfree(). 582 * See kstrdup_const() and kunit_kmalloc_array() for more information. 583 */ 584 const char *kunit_kstrdup_const(struct kunit *test, const char *str, gfp_t gfp); 585 586 /** 587 * kunit_attach_mm() - Create and attach a new mm if it doesn't already exist. 588 * 589 * Allocates a &struct mm_struct and attaches it to @current. In most cases, call 590 * kunit_vm_mmap() without calling kunit_attach_mm() directly. Only necessary when 591 * code under test accesses the mm before executing the mmap (e.g., to perform 592 * additional initialization beforehand). 593 * 594 * Return: 0 on success, -errno on failure. 595 */ 596 int kunit_attach_mm(void); 597 598 /** 599 * kunit_vm_mmap() - Allocate KUnit-tracked vm_mmap() area 600 * @test: The test context object. 601 * @file: struct file pointer to map from, if any 602 * @addr: desired address, if any 603 * @len: how many bytes to allocate 604 * @prot: mmap PROT_* bits 605 * @flag: mmap flags 606 * @offset: offset into @file to start mapping from. 607 * 608 * See vm_mmap() for more information. 609 */ 610 unsigned long kunit_vm_mmap(struct kunit *test, struct file *file, 611 unsigned long addr, unsigned long len, 612 unsigned long prot, unsigned long flag, 613 unsigned long offset); 614 615 void kunit_cleanup(struct kunit *test); 616 617 void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt, ...); 618 619 /** 620 * kunit_mark_skipped() - Marks @test as skipped 621 * 622 * @test: The test context object. 623 * @fmt: A printk() style format string. 624 * 625 * Marks the test as skipped. @fmt is given output as the test status 626 * comment, typically the reason the test was skipped. 627 * 628 * Test execution continues after kunit_mark_skipped() is called. 629 */ 630 #define kunit_mark_skipped(test, fmt, ...) \ 631 do { \ 632 WRITE_ONCE((test)->status, KUNIT_SKIPPED); \ 633 scnprintf((test)->status_comment, \ 634 KUNIT_STATUS_COMMENT_SIZE, \ 635 fmt, ##__VA_ARGS__); \ 636 } while (0) 637 638 /** 639 * kunit_skip() - Marks @test as skipped 640 * 641 * @test: The test context object. 642 * @fmt: A printk() style format string. 643 * 644 * Skips the test. @fmt is given output as the test status 645 * comment, typically the reason the test was skipped. 646 * 647 * Test execution is halted after kunit_skip() is called. 648 */ 649 #define kunit_skip(test, fmt, ...) \ 650 do { \ 651 kunit_mark_skipped((test), fmt, ##__VA_ARGS__); \ 652 kunit_try_catch_throw(&((test)->try_catch)); \ 653 } while (0) 654 655 /* 656 * printk and log to per-test or per-suite log buffer. Logging only done 657 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used. 658 */ 659 #define kunit_log(lvl, test_or_suite, fmt, ...) \ 660 do { \ 661 printk(lvl fmt, ##__VA_ARGS__); \ 662 kunit_log_append((test_or_suite)->log, fmt, \ 663 ##__VA_ARGS__); \ 664 } while (0) 665 666 #define kunit_printk(lvl, test, fmt, ...) \ 667 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \ 668 (test)->name, ##__VA_ARGS__) 669 670 /** 671 * kunit_info() - Prints an INFO level message associated with @test. 672 * 673 * @test: The test context object. 674 * @fmt: A printk() style format string. 675 * 676 * Prints an info level message associated with the test suite being run. 677 * Takes a variable number of format parameters just like printk(). 678 */ 679 #define kunit_info(test, fmt, ...) \ 680 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__) 681 682 /** 683 * kunit_warn() - Prints a WARN level message associated with @test. 684 * 685 * @test: The test context object. 686 * @fmt: A printk() style format string. 687 * 688 * Prints a warning level message. 689 */ 690 #define kunit_warn(test, fmt, ...) \ 691 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__) 692 693 /** 694 * kunit_err() - Prints an ERROR level message associated with @test. 695 * 696 * @test: The test context object. 697 * @fmt: A printk() style format string. 698 * 699 * Prints an error level message. 700 */ 701 #define kunit_err(test, fmt, ...) \ 702 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__) 703 704 /* 705 * Must be called at the beginning of each KUNIT_*_ASSERTION(). 706 * Cf. KUNIT_CURRENT_LOC. 707 */ 708 #define _KUNIT_SAVE_LOC(test) do { \ 709 WRITE_ONCE(test->last_seen.file, __FILE__); \ 710 WRITE_ONCE(test->last_seen.line, __LINE__); \ 711 } while (0) 712 713 /** 714 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity. 715 * @test: The test context object. 716 * 717 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other 718 * words, it does nothing and only exists for code clarity. See 719 * KUNIT_EXPECT_TRUE() for more information. 720 */ 721 #define KUNIT_SUCCEED(test) _KUNIT_SAVE_LOC(test) 722 723 void __noreturn __kunit_abort(struct kunit *test); 724 725 void __printf(6, 7) __kunit_do_failed_assertion(struct kunit *test, 726 const struct kunit_loc *loc, 727 enum kunit_assert_type type, 728 const struct kunit_assert *assert, 729 assert_format_t assert_format, 730 const char *fmt, ...); 731 732 #define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \ 733 static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \ 734 const struct assert_class __assertion = INITIALIZER; \ 735 __kunit_do_failed_assertion(test, \ 736 &__loc, \ 737 assert_type, \ 738 &__assertion.assert, \ 739 assert_format, \ 740 fmt, \ 741 ##__VA_ARGS__); \ 742 if (assert_type == KUNIT_ASSERTION) \ 743 __kunit_abort(test); \ 744 } while (0) 745 746 747 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) do { \ 748 _KUNIT_SAVE_LOC(test); \ 749 _KUNIT_FAILED(test, \ 750 assert_type, \ 751 kunit_fail_assert, \ 752 kunit_fail_assert_format, \ 753 {}, \ 754 fmt, \ 755 ##__VA_ARGS__); \ 756 } while (0) 757 758 /** 759 * KUNIT_FAIL() - Always causes a test to fail when evaluated. 760 * @test: The test context object. 761 * @fmt: an informational message to be printed when the assertion is made. 762 * @...: string format arguments. 763 * 764 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In 765 * other words, it always results in a failed expectation, and consequently 766 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE() 767 * for more information. 768 */ 769 #define KUNIT_FAIL(test, fmt, ...) \ 770 KUNIT_FAIL_ASSERTION(test, \ 771 KUNIT_EXPECTATION, \ 772 fmt, \ 773 ##__VA_ARGS__) 774 775 /* Helper to safely pass around an initializer list to other macros. */ 776 #define KUNIT_INIT_ASSERT(initializers...) { initializers } 777 778 #define KUNIT_UNARY_ASSERTION(test, \ 779 assert_type, \ 780 condition_, \ 781 expected_true_, \ 782 fmt, \ 783 ...) \ 784 do { \ 785 _KUNIT_SAVE_LOC(test); \ 786 if (likely(!!(condition_) == !!expected_true_)) \ 787 break; \ 788 \ 789 _KUNIT_FAILED(test, \ 790 assert_type, \ 791 kunit_unary_assert, \ 792 kunit_unary_assert_format, \ 793 KUNIT_INIT_ASSERT(.condition = #condition_, \ 794 .expected_true = expected_true_), \ 795 fmt, \ 796 ##__VA_ARGS__); \ 797 } while (0) 798 799 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \ 800 KUNIT_UNARY_ASSERTION(test, \ 801 assert_type, \ 802 condition, \ 803 true, \ 804 fmt, \ 805 ##__VA_ARGS__) 806 807 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \ 808 KUNIT_UNARY_ASSERTION(test, \ 809 assert_type, \ 810 condition, \ 811 false, \ 812 fmt, \ 813 ##__VA_ARGS__) 814 815 /* 816 * A factory macro for defining the assertions and expectations for the basic 817 * comparisons defined for the built in types. 818 * 819 * Unfortunately, there is no common type that all types can be promoted to for 820 * which all the binary operators behave the same way as for the actual types 821 * (for example, there is no type that long long and unsigned long long can 822 * both be cast to where the comparison result is preserved for all values). So 823 * the best we can do is do the comparison in the original types and then coerce 824 * everything to long long for printing; this way, the comparison behaves 825 * correctly and the printed out value usually makes sense without 826 * interpretation, but can always be interpreted to figure out the actual 827 * value. 828 */ 829 #define KUNIT_BASE_BINARY_ASSERTION(test, \ 830 assert_class, \ 831 format_func, \ 832 assert_type, \ 833 left, \ 834 op, \ 835 right, \ 836 fmt, \ 837 ...) \ 838 do { \ 839 const typeof(left) __left = (left); \ 840 const typeof(right) __right = (right); \ 841 static const struct kunit_binary_assert_text __text = { \ 842 .operation = #op, \ 843 .left_text = #left, \ 844 .right_text = #right, \ 845 }; \ 846 \ 847 _KUNIT_SAVE_LOC(test); \ 848 if (likely(__left op __right)) \ 849 break; \ 850 \ 851 _KUNIT_FAILED(test, \ 852 assert_type, \ 853 assert_class, \ 854 format_func, \ 855 KUNIT_INIT_ASSERT(.text = &__text, \ 856 .left_value = __left, \ 857 .right_value = __right), \ 858 fmt, \ 859 ##__VA_ARGS__); \ 860 } while (0) 861 862 #define KUNIT_BINARY_INT_ASSERTION(test, \ 863 assert_type, \ 864 left, \ 865 op, \ 866 right, \ 867 fmt, \ 868 ...) \ 869 KUNIT_BASE_BINARY_ASSERTION(test, \ 870 kunit_binary_assert, \ 871 kunit_binary_assert_format, \ 872 assert_type, \ 873 left, op, right, \ 874 fmt, \ 875 ##__VA_ARGS__) 876 877 #define KUNIT_BINARY_PTR_ASSERTION(test, \ 878 assert_type, \ 879 left, \ 880 op, \ 881 right, \ 882 fmt, \ 883 ...) \ 884 KUNIT_BASE_BINARY_ASSERTION(test, \ 885 kunit_binary_ptr_assert, \ 886 kunit_binary_ptr_assert_format, \ 887 assert_type, \ 888 left, op, right, \ 889 fmt, \ 890 ##__VA_ARGS__) 891 892 #define KUNIT_BINARY_STR_ASSERTION(test, \ 893 assert_type, \ 894 left, \ 895 op, \ 896 right, \ 897 fmt, \ 898 ...) \ 899 do { \ 900 const char *__left = (left); \ 901 const char *__right = (right); \ 902 static const struct kunit_binary_assert_text __text = { \ 903 .operation = #op, \ 904 .left_text = #left, \ 905 .right_text = #right, \ 906 }; \ 907 \ 908 _KUNIT_SAVE_LOC(test); \ 909 if (likely(!IS_ERR_OR_NULL(__left) && !IS_ERR_OR_NULL(__right) && \ 910 (strcmp(__left, __right) op 0))) \ 911 break; \ 912 \ 913 \ 914 _KUNIT_FAILED(test, \ 915 assert_type, \ 916 kunit_binary_str_assert, \ 917 kunit_binary_str_assert_format, \ 918 KUNIT_INIT_ASSERT(.text = &__text, \ 919 .left_value = __left, \ 920 .right_value = __right), \ 921 fmt, \ 922 ##__VA_ARGS__); \ 923 } while (0) 924 925 #define KUNIT_MEM_ASSERTION(test, \ 926 assert_type, \ 927 left, \ 928 op, \ 929 right, \ 930 size_, \ 931 fmt, \ 932 ...) \ 933 do { \ 934 const void *__left = (left); \ 935 const void *__right = (right); \ 936 const size_t __size = (size_); \ 937 static const struct kunit_binary_assert_text __text = { \ 938 .operation = #op, \ 939 .left_text = #left, \ 940 .right_text = #right, \ 941 }; \ 942 \ 943 _KUNIT_SAVE_LOC(test); \ 944 if (likely(__left && __right)) \ 945 if (likely(memcmp(__left, __right, __size) op 0)) \ 946 break; \ 947 \ 948 _KUNIT_FAILED(test, \ 949 assert_type, \ 950 kunit_mem_assert, \ 951 kunit_mem_assert_format, \ 952 KUNIT_INIT_ASSERT(.text = &__text, \ 953 .left_value = __left, \ 954 .right_value = __right, \ 955 .size = __size), \ 956 fmt, \ 957 ##__VA_ARGS__); \ 958 } while (0) 959 960 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \ 961 assert_type, \ 962 ptr, \ 963 fmt, \ 964 ...) \ 965 do { \ 966 const typeof(ptr) __ptr = (ptr); \ 967 \ 968 _KUNIT_SAVE_LOC(test); \ 969 if (!IS_ERR_OR_NULL(__ptr)) \ 970 break; \ 971 \ 972 _KUNIT_FAILED(test, \ 973 assert_type, \ 974 kunit_ptr_not_err_assert, \ 975 kunit_ptr_not_err_assert_format, \ 976 KUNIT_INIT_ASSERT(.text = #ptr, .value = __ptr), \ 977 fmt, \ 978 ##__VA_ARGS__); \ 979 } while (0) 980 981 /** 982 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true. 983 * @test: The test context object. 984 * @condition: an arbitrary boolean expression. The test fails when this does 985 * not evaluate to true. 986 * 987 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case 988 * to fail when the specified condition is not met; however, it will not prevent 989 * the test case from continuing to run; this is otherwise known as an 990 * *expectation failure*. 991 */ 992 #define KUNIT_EXPECT_TRUE(test, condition) \ 993 KUNIT_EXPECT_TRUE_MSG(test, condition, NULL) 994 995 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \ 996 KUNIT_TRUE_MSG_ASSERTION(test, \ 997 KUNIT_EXPECTATION, \ 998 condition, \ 999 fmt, \ 1000 ##__VA_ARGS__) 1001 1002 /** 1003 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false. 1004 * @test: The test context object. 1005 * @condition: an arbitrary boolean expression. The test fails when this does 1006 * not evaluate to false. 1007 * 1008 * Sets an expectation that @condition evaluates to false. See 1009 * KUNIT_EXPECT_TRUE() for more information. 1010 */ 1011 #define KUNIT_EXPECT_FALSE(test, condition) \ 1012 KUNIT_EXPECT_FALSE_MSG(test, condition, NULL) 1013 1014 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \ 1015 KUNIT_FALSE_MSG_ASSERTION(test, \ 1016 KUNIT_EXPECTATION, \ 1017 condition, \ 1018 fmt, \ 1019 ##__VA_ARGS__) 1020 1021 /** 1022 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal. 1023 * @test: The test context object. 1024 * @left: an arbitrary expression that evaluates to a primitive C type. 1025 * @right: an arbitrary expression that evaluates to a primitive C type. 1026 * 1027 * Sets an expectation that the values that @left and @right evaluate to are 1028 * equal. This is semantically equivalent to 1029 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for 1030 * more information. 1031 */ 1032 #define KUNIT_EXPECT_EQ(test, left, right) \ 1033 KUNIT_EXPECT_EQ_MSG(test, left, right, NULL) 1034 1035 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \ 1036 KUNIT_BINARY_INT_ASSERTION(test, \ 1037 KUNIT_EXPECTATION, \ 1038 left, ==, right, \ 1039 fmt, \ 1040 ##__VA_ARGS__) 1041 1042 /** 1043 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal. 1044 * @test: The test context object. 1045 * @left: an arbitrary expression that evaluates to a pointer. 1046 * @right: an arbitrary expression that evaluates to a pointer. 1047 * 1048 * Sets an expectation that the values that @left and @right evaluate to are 1049 * equal. This is semantically equivalent to 1050 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for 1051 * more information. 1052 */ 1053 #define KUNIT_EXPECT_PTR_EQ(test, left, right) \ 1054 KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL) 1055 1056 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \ 1057 KUNIT_BINARY_PTR_ASSERTION(test, \ 1058 KUNIT_EXPECTATION, \ 1059 left, ==, right, \ 1060 fmt, \ 1061 ##__VA_ARGS__) 1062 1063 /** 1064 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal. 1065 * @test: The test context object. 1066 * @left: an arbitrary expression that evaluates to a primitive C type. 1067 * @right: an arbitrary expression that evaluates to a primitive C type. 1068 * 1069 * Sets an expectation that the values that @left and @right evaluate to are not 1070 * equal. This is semantically equivalent to 1071 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for 1072 * more information. 1073 */ 1074 #define KUNIT_EXPECT_NE(test, left, right) \ 1075 KUNIT_EXPECT_NE_MSG(test, left, right, NULL) 1076 1077 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \ 1078 KUNIT_BINARY_INT_ASSERTION(test, \ 1079 KUNIT_EXPECTATION, \ 1080 left, !=, right, \ 1081 fmt, \ 1082 ##__VA_ARGS__) 1083 1084 /** 1085 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal. 1086 * @test: The test context object. 1087 * @left: an arbitrary expression that evaluates to a pointer. 1088 * @right: an arbitrary expression that evaluates to a pointer. 1089 * 1090 * Sets an expectation that the values that @left and @right evaluate to are not 1091 * equal. This is semantically equivalent to 1092 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for 1093 * more information. 1094 */ 1095 #define KUNIT_EXPECT_PTR_NE(test, left, right) \ 1096 KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL) 1097 1098 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \ 1099 KUNIT_BINARY_PTR_ASSERTION(test, \ 1100 KUNIT_EXPECTATION, \ 1101 left, !=, right, \ 1102 fmt, \ 1103 ##__VA_ARGS__) 1104 1105 /** 1106 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right. 1107 * @test: The test context object. 1108 * @left: an arbitrary expression that evaluates to a primitive C type. 1109 * @right: an arbitrary expression that evaluates to a primitive C type. 1110 * 1111 * Sets an expectation that the value that @left evaluates to is less than the 1112 * value that @right evaluates to. This is semantically equivalent to 1113 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for 1114 * more information. 1115 */ 1116 #define KUNIT_EXPECT_LT(test, left, right) \ 1117 KUNIT_EXPECT_LT_MSG(test, left, right, NULL) 1118 1119 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \ 1120 KUNIT_BINARY_INT_ASSERTION(test, \ 1121 KUNIT_EXPECTATION, \ 1122 left, <, right, \ 1123 fmt, \ 1124 ##__VA_ARGS__) 1125 1126 /** 1127 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right. 1128 * @test: The test context object. 1129 * @left: an arbitrary expression that evaluates to a primitive C type. 1130 * @right: an arbitrary expression that evaluates to a primitive C type. 1131 * 1132 * Sets an expectation that the value that @left evaluates to is less than or 1133 * equal to the value that @right evaluates to. Semantically this is equivalent 1134 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for 1135 * more information. 1136 */ 1137 #define KUNIT_EXPECT_LE(test, left, right) \ 1138 KUNIT_EXPECT_LE_MSG(test, left, right, NULL) 1139 1140 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \ 1141 KUNIT_BINARY_INT_ASSERTION(test, \ 1142 KUNIT_EXPECTATION, \ 1143 left, <=, right, \ 1144 fmt, \ 1145 ##__VA_ARGS__) 1146 1147 /** 1148 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right. 1149 * @test: The test context object. 1150 * @left: an arbitrary expression that evaluates to a primitive C type. 1151 * @right: an arbitrary expression that evaluates to a primitive C type. 1152 * 1153 * Sets an expectation that the value that @left evaluates to is greater than 1154 * the value that @right evaluates to. This is semantically equivalent to 1155 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for 1156 * more information. 1157 */ 1158 #define KUNIT_EXPECT_GT(test, left, right) \ 1159 KUNIT_EXPECT_GT_MSG(test, left, right, NULL) 1160 1161 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \ 1162 KUNIT_BINARY_INT_ASSERTION(test, \ 1163 KUNIT_EXPECTATION, \ 1164 left, >, right, \ 1165 fmt, \ 1166 ##__VA_ARGS__) 1167 1168 /** 1169 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right. 1170 * @test: The test context object. 1171 * @left: an arbitrary expression that evaluates to a primitive C type. 1172 * @right: an arbitrary expression that evaluates to a primitive C type. 1173 * 1174 * Sets an expectation that the value that @left evaluates to is greater than 1175 * the value that @right evaluates to. This is semantically equivalent to 1176 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for 1177 * more information. 1178 */ 1179 #define KUNIT_EXPECT_GE(test, left, right) \ 1180 KUNIT_EXPECT_GE_MSG(test, left, right, NULL) 1181 1182 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \ 1183 KUNIT_BINARY_INT_ASSERTION(test, \ 1184 KUNIT_EXPECTATION, \ 1185 left, >=, right, \ 1186 fmt, \ 1187 ##__VA_ARGS__) 1188 1189 /** 1190 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal. 1191 * @test: The test context object. 1192 * @left: an arbitrary expression that evaluates to a null terminated string. 1193 * @right: an arbitrary expression that evaluates to a null terminated string. 1194 * 1195 * Sets an expectation that the values that @left and @right evaluate to are 1196 * equal. This is semantically equivalent to 1197 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE() 1198 * for more information. 1199 */ 1200 #define KUNIT_EXPECT_STREQ(test, left, right) \ 1201 KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL) 1202 1203 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \ 1204 KUNIT_BINARY_STR_ASSERTION(test, \ 1205 KUNIT_EXPECTATION, \ 1206 left, ==, right, \ 1207 fmt, \ 1208 ##__VA_ARGS__) 1209 1210 /** 1211 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal. 1212 * @test: The test context object. 1213 * @left: an arbitrary expression that evaluates to a null terminated string. 1214 * @right: an arbitrary expression that evaluates to a null terminated string. 1215 * 1216 * Sets an expectation that the values that @left and @right evaluate to are 1217 * not equal. This is semantically equivalent to 1218 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE() 1219 * for more information. 1220 */ 1221 #define KUNIT_EXPECT_STRNEQ(test, left, right) \ 1222 KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL) 1223 1224 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \ 1225 KUNIT_BINARY_STR_ASSERTION(test, \ 1226 KUNIT_EXPECTATION, \ 1227 left, !=, right, \ 1228 fmt, \ 1229 ##__VA_ARGS__) 1230 1231 /** 1232 * KUNIT_EXPECT_MEMEQ() - Expects that the first @size bytes of @left and @right are equal. 1233 * @test: The test context object. 1234 * @left: An arbitrary expression that evaluates to the specified size. 1235 * @right: An arbitrary expression that evaluates to the specified size. 1236 * @size: Number of bytes compared. 1237 * 1238 * Sets an expectation that the values that @left and @right evaluate to are 1239 * equal. This is semantically equivalent to 1240 * KUNIT_EXPECT_TRUE(@test, !memcmp((@left), (@right), (@size))). See 1241 * KUNIT_EXPECT_TRUE() for more information. 1242 * 1243 * Although this expectation works for any memory block, it is not recommended 1244 * for comparing more structured data, such as structs. This expectation is 1245 * recommended for comparing, for example, data arrays. 1246 */ 1247 #define KUNIT_EXPECT_MEMEQ(test, left, right, size) \ 1248 KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, NULL) 1249 1250 #define KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, fmt, ...) \ 1251 KUNIT_MEM_ASSERTION(test, \ 1252 KUNIT_EXPECTATION, \ 1253 left, ==, right, \ 1254 size, \ 1255 fmt, \ 1256 ##__VA_ARGS__) 1257 1258 /** 1259 * KUNIT_EXPECT_MEMNEQ() - Expects that the first @size bytes of @left and @right are not equal. 1260 * @test: The test context object. 1261 * @left: An arbitrary expression that evaluates to the specified size. 1262 * @right: An arbitrary expression that evaluates to the specified size. 1263 * @size: Number of bytes compared. 1264 * 1265 * Sets an expectation that the values that @left and @right evaluate to are 1266 * not equal. This is semantically equivalent to 1267 * KUNIT_EXPECT_TRUE(@test, memcmp((@left), (@right), (@size))). See 1268 * KUNIT_EXPECT_TRUE() for more information. 1269 * 1270 * Although this expectation works for any memory block, it is not recommended 1271 * for comparing more structured data, such as structs. This expectation is 1272 * recommended for comparing, for example, data arrays. 1273 */ 1274 #define KUNIT_EXPECT_MEMNEQ(test, left, right, size) \ 1275 KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, NULL) 1276 1277 #define KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \ 1278 KUNIT_MEM_ASSERTION(test, \ 1279 KUNIT_EXPECTATION, \ 1280 left, !=, right, \ 1281 size, \ 1282 fmt, \ 1283 ##__VA_ARGS__) 1284 1285 /** 1286 * KUNIT_EXPECT_NULL() - Expects that @ptr is null. 1287 * @test: The test context object. 1288 * @ptr: an arbitrary pointer. 1289 * 1290 * Sets an expectation that the value that @ptr evaluates to is null. This is 1291 * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL). 1292 * See KUNIT_EXPECT_TRUE() for more information. 1293 */ 1294 #define KUNIT_EXPECT_NULL(test, ptr) \ 1295 KUNIT_EXPECT_NULL_MSG(test, \ 1296 ptr, \ 1297 NULL) 1298 1299 #define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \ 1300 KUNIT_BINARY_PTR_ASSERTION(test, \ 1301 KUNIT_EXPECTATION, \ 1302 ptr, ==, NULL, \ 1303 fmt, \ 1304 ##__VA_ARGS__) 1305 1306 /** 1307 * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null. 1308 * @test: The test context object. 1309 * @ptr: an arbitrary pointer. 1310 * 1311 * Sets an expectation that the value that @ptr evaluates to is not null. This 1312 * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL). 1313 * See KUNIT_EXPECT_TRUE() for more information. 1314 */ 1315 #define KUNIT_EXPECT_NOT_NULL(test, ptr) \ 1316 KUNIT_EXPECT_NOT_NULL_MSG(test, \ 1317 ptr, \ 1318 NULL) 1319 1320 #define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \ 1321 KUNIT_BINARY_PTR_ASSERTION(test, \ 1322 KUNIT_EXPECTATION, \ 1323 ptr, !=, NULL, \ 1324 fmt, \ 1325 ##__VA_ARGS__) 1326 1327 /** 1328 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err. 1329 * @test: The test context object. 1330 * @ptr: an arbitrary pointer. 1331 * 1332 * Sets an expectation that the value that @ptr evaluates to is not null and not 1333 * an errno stored in a pointer. This is semantically equivalent to 1334 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for 1335 * more information. 1336 */ 1337 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \ 1338 KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL) 1339 1340 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \ 1341 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \ 1342 KUNIT_EXPECTATION, \ 1343 ptr, \ 1344 fmt, \ 1345 ##__VA_ARGS__) 1346 1347 /** 1348 * KUNIT_FAIL_AND_ABORT() - Always causes a test to fail and abort when evaluated. 1349 * @test: The test context object. 1350 * @fmt: an informational message to be printed when the assertion is made. 1351 * @...: string format arguments. 1352 * 1353 * The opposite of KUNIT_SUCCEED(), it is an assertion that always fails. In 1354 * other words, it always results in a failed assertion, and consequently 1355 * always causes the test case to fail and abort when evaluated. 1356 * See KUNIT_ASSERT_TRUE() for more information. 1357 */ 1358 #define KUNIT_FAIL_AND_ABORT(test, fmt, ...) \ 1359 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__) 1360 1361 /** 1362 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true. 1363 * @test: The test context object. 1364 * @condition: an arbitrary boolean expression. The test fails and aborts when 1365 * this does not evaluate to true. 1366 * 1367 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to 1368 * fail *and immediately abort* when the specified condition is not met. Unlike 1369 * an expectation failure, it will prevent the test case from continuing to run; 1370 * this is otherwise known as an *assertion failure*. 1371 */ 1372 #define KUNIT_ASSERT_TRUE(test, condition) \ 1373 KUNIT_ASSERT_TRUE_MSG(test, condition, NULL) 1374 1375 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \ 1376 KUNIT_TRUE_MSG_ASSERTION(test, \ 1377 KUNIT_ASSERTION, \ 1378 condition, \ 1379 fmt, \ 1380 ##__VA_ARGS__) 1381 1382 /** 1383 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false. 1384 * @test: The test context object. 1385 * @condition: an arbitrary boolean expression. 1386 * 1387 * Sets an assertion that the value that @condition evaluates to is false. This 1388 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure 1389 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1390 */ 1391 #define KUNIT_ASSERT_FALSE(test, condition) \ 1392 KUNIT_ASSERT_FALSE_MSG(test, condition, NULL) 1393 1394 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \ 1395 KUNIT_FALSE_MSG_ASSERTION(test, \ 1396 KUNIT_ASSERTION, \ 1397 condition, \ 1398 fmt, \ 1399 ##__VA_ARGS__) 1400 1401 /** 1402 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal. 1403 * @test: The test context object. 1404 * @left: an arbitrary expression that evaluates to a primitive C type. 1405 * @right: an arbitrary expression that evaluates to a primitive C type. 1406 * 1407 * Sets an assertion that the values that @left and @right evaluate to are 1408 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion 1409 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1410 */ 1411 #define KUNIT_ASSERT_EQ(test, left, right) \ 1412 KUNIT_ASSERT_EQ_MSG(test, left, right, NULL) 1413 1414 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \ 1415 KUNIT_BINARY_INT_ASSERTION(test, \ 1416 KUNIT_ASSERTION, \ 1417 left, ==, right, \ 1418 fmt, \ 1419 ##__VA_ARGS__) 1420 1421 /** 1422 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal. 1423 * @test: The test context object. 1424 * @left: an arbitrary expression that evaluates to a pointer. 1425 * @right: an arbitrary expression that evaluates to a pointer. 1426 * 1427 * Sets an assertion that the values that @left and @right evaluate to are 1428 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion 1429 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1430 */ 1431 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \ 1432 KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL) 1433 1434 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \ 1435 KUNIT_BINARY_PTR_ASSERTION(test, \ 1436 KUNIT_ASSERTION, \ 1437 left, ==, right, \ 1438 fmt, \ 1439 ##__VA_ARGS__) 1440 1441 /** 1442 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal. 1443 * @test: The test context object. 1444 * @left: an arbitrary expression that evaluates to a primitive C type. 1445 * @right: an arbitrary expression that evaluates to a primitive C type. 1446 * 1447 * Sets an assertion that the values that @left and @right evaluate to are not 1448 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion 1449 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1450 */ 1451 #define KUNIT_ASSERT_NE(test, left, right) \ 1452 KUNIT_ASSERT_NE_MSG(test, left, right, NULL) 1453 1454 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \ 1455 KUNIT_BINARY_INT_ASSERTION(test, \ 1456 KUNIT_ASSERTION, \ 1457 left, !=, right, \ 1458 fmt, \ 1459 ##__VA_ARGS__) 1460 1461 /** 1462 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal. 1463 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal. 1464 * @test: The test context object. 1465 * @left: an arbitrary expression that evaluates to a pointer. 1466 * @right: an arbitrary expression that evaluates to a pointer. 1467 * 1468 * Sets an assertion that the values that @left and @right evaluate to are not 1469 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion 1470 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1471 */ 1472 #define KUNIT_ASSERT_PTR_NE(test, left, right) \ 1473 KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL) 1474 1475 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \ 1476 KUNIT_BINARY_PTR_ASSERTION(test, \ 1477 KUNIT_ASSERTION, \ 1478 left, !=, right, \ 1479 fmt, \ 1480 ##__VA_ARGS__) 1481 /** 1482 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right. 1483 * @test: The test context object. 1484 * @left: an arbitrary expression that evaluates to a primitive C type. 1485 * @right: an arbitrary expression that evaluates to a primitive C type. 1486 * 1487 * Sets an assertion that the value that @left evaluates to is less than the 1488 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except 1489 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion 1490 * is not met. 1491 */ 1492 #define KUNIT_ASSERT_LT(test, left, right) \ 1493 KUNIT_ASSERT_LT_MSG(test, left, right, NULL) 1494 1495 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \ 1496 KUNIT_BINARY_INT_ASSERTION(test, \ 1497 KUNIT_ASSERTION, \ 1498 left, <, right, \ 1499 fmt, \ 1500 ##__VA_ARGS__) 1501 /** 1502 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right. 1503 * @test: The test context object. 1504 * @left: an arbitrary expression that evaluates to a primitive C type. 1505 * @right: an arbitrary expression that evaluates to a primitive C type. 1506 * 1507 * Sets an assertion that the value that @left evaluates to is less than or 1508 * equal to the value that @right evaluates to. This is the same as 1509 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see 1510 * KUNIT_ASSERT_TRUE()) when the assertion is not met. 1511 */ 1512 #define KUNIT_ASSERT_LE(test, left, right) \ 1513 KUNIT_ASSERT_LE_MSG(test, left, right, NULL) 1514 1515 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \ 1516 KUNIT_BINARY_INT_ASSERTION(test, \ 1517 KUNIT_ASSERTION, \ 1518 left, <=, right, \ 1519 fmt, \ 1520 ##__VA_ARGS__) 1521 1522 /** 1523 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right. 1524 * @test: The test context object. 1525 * @left: an arbitrary expression that evaluates to a primitive C type. 1526 * @right: an arbitrary expression that evaluates to a primitive C type. 1527 * 1528 * Sets an assertion that the value that @left evaluates to is greater than the 1529 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except 1530 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion 1531 * is not met. 1532 */ 1533 #define KUNIT_ASSERT_GT(test, left, right) \ 1534 KUNIT_ASSERT_GT_MSG(test, left, right, NULL) 1535 1536 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \ 1537 KUNIT_BINARY_INT_ASSERTION(test, \ 1538 KUNIT_ASSERTION, \ 1539 left, >, right, \ 1540 fmt, \ 1541 ##__VA_ARGS__) 1542 1543 /** 1544 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right. 1545 * @test: The test context object. 1546 * @left: an arbitrary expression that evaluates to a primitive C type. 1547 * @right: an arbitrary expression that evaluates to a primitive C type. 1548 * 1549 * Sets an assertion that the value that @left evaluates to is greater than the 1550 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except 1551 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion 1552 * is not met. 1553 */ 1554 #define KUNIT_ASSERT_GE(test, left, right) \ 1555 KUNIT_ASSERT_GE_MSG(test, left, right, NULL) 1556 1557 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \ 1558 KUNIT_BINARY_INT_ASSERTION(test, \ 1559 KUNIT_ASSERTION, \ 1560 left, >=, right, \ 1561 fmt, \ 1562 ##__VA_ARGS__) 1563 1564 /** 1565 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal. 1566 * @test: The test context object. 1567 * @left: an arbitrary expression that evaluates to a null terminated string. 1568 * @right: an arbitrary expression that evaluates to a null terminated string. 1569 * 1570 * Sets an assertion that the values that @left and @right evaluate to are 1571 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an 1572 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1573 */ 1574 #define KUNIT_ASSERT_STREQ(test, left, right) \ 1575 KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL) 1576 1577 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \ 1578 KUNIT_BINARY_STR_ASSERTION(test, \ 1579 KUNIT_ASSERTION, \ 1580 left, ==, right, \ 1581 fmt, \ 1582 ##__VA_ARGS__) 1583 1584 /** 1585 * KUNIT_ASSERT_STRNEQ() - An assertion that strings @left and @right are not equal. 1586 * @test: The test context object. 1587 * @left: an arbitrary expression that evaluates to a null terminated string. 1588 * @right: an arbitrary expression that evaluates to a null terminated string. 1589 * 1590 * Sets an assertion that the values that @left and @right evaluate to are 1591 * not equal. This is semantically equivalent to 1592 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE() 1593 * for more information. 1594 */ 1595 #define KUNIT_ASSERT_STRNEQ(test, left, right) \ 1596 KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL) 1597 1598 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \ 1599 KUNIT_BINARY_STR_ASSERTION(test, \ 1600 KUNIT_ASSERTION, \ 1601 left, !=, right, \ 1602 fmt, \ 1603 ##__VA_ARGS__) 1604 1605 /** 1606 * KUNIT_ASSERT_MEMEQ() - Asserts that the first @size bytes of @left and @right are equal. 1607 * @test: The test context object. 1608 * @left: An arbitrary expression that evaluates to the specified size. 1609 * @right: An arbitrary expression that evaluates to the specified size. 1610 * @size: Number of bytes compared. 1611 * 1612 * Sets an assertion that the values that @left and @right evaluate to are 1613 * equal. This is semantically equivalent to 1614 * KUNIT_ASSERT_TRUE(@test, !memcmp((@left), (@right), (@size))). See 1615 * KUNIT_ASSERT_TRUE() for more information. 1616 * 1617 * Although this assertion works for any memory block, it is not recommended 1618 * for comparing more structured data, such as structs. This assertion is 1619 * recommended for comparing, for example, data arrays. 1620 */ 1621 #define KUNIT_ASSERT_MEMEQ(test, left, right, size) \ 1622 KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, NULL) 1623 1624 #define KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, fmt, ...) \ 1625 KUNIT_MEM_ASSERTION(test, \ 1626 KUNIT_ASSERTION, \ 1627 left, ==, right, \ 1628 size, \ 1629 fmt, \ 1630 ##__VA_ARGS__) 1631 1632 /** 1633 * KUNIT_ASSERT_MEMNEQ() - Asserts that the first @size bytes of @left and @right are not equal. 1634 * @test: The test context object. 1635 * @left: An arbitrary expression that evaluates to the specified size. 1636 * @right: An arbitrary expression that evaluates to the specified size. 1637 * @size: Number of bytes compared. 1638 * 1639 * Sets an assertion that the values that @left and @right evaluate to are 1640 * not equal. This is semantically equivalent to 1641 * KUNIT_ASSERT_TRUE(@test, memcmp((@left), (@right), (@size))). See 1642 * KUNIT_ASSERT_TRUE() for more information. 1643 * 1644 * Although this assertion works for any memory block, it is not recommended 1645 * for comparing more structured data, such as structs. This assertion is 1646 * recommended for comparing, for example, data arrays. 1647 */ 1648 #define KUNIT_ASSERT_MEMNEQ(test, left, right, size) \ 1649 KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, NULL) 1650 1651 #define KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \ 1652 KUNIT_MEM_ASSERTION(test, \ 1653 KUNIT_ASSERTION, \ 1654 left, !=, right, \ 1655 size, \ 1656 fmt, \ 1657 ##__VA_ARGS__) 1658 1659 /** 1660 * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null. 1661 * @test: The test context object. 1662 * @ptr: an arbitrary pointer. 1663 * 1664 * Sets an assertion that the values that @ptr evaluates to is null. This is 1665 * the same as KUNIT_EXPECT_NULL(), except it causes an assertion 1666 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1667 */ 1668 #define KUNIT_ASSERT_NULL(test, ptr) \ 1669 KUNIT_ASSERT_NULL_MSG(test, \ 1670 ptr, \ 1671 NULL) 1672 1673 #define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \ 1674 KUNIT_BINARY_PTR_ASSERTION(test, \ 1675 KUNIT_ASSERTION, \ 1676 ptr, ==, NULL, \ 1677 fmt, \ 1678 ##__VA_ARGS__) 1679 1680 /** 1681 * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null. 1682 * @test: The test context object. 1683 * @ptr: an arbitrary pointer. 1684 * 1685 * Sets an assertion that the values that @ptr evaluates to is not null. This 1686 * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion 1687 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met. 1688 */ 1689 #define KUNIT_ASSERT_NOT_NULL(test, ptr) \ 1690 KUNIT_ASSERT_NOT_NULL_MSG(test, \ 1691 ptr, \ 1692 NULL) 1693 1694 #define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \ 1695 KUNIT_BINARY_PTR_ASSERTION(test, \ 1696 KUNIT_ASSERTION, \ 1697 ptr, !=, NULL, \ 1698 fmt, \ 1699 ##__VA_ARGS__) 1700 1701 /** 1702 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err. 1703 * @test: The test context object. 1704 * @ptr: an arbitrary pointer. 1705 * 1706 * Sets an assertion that the value that @ptr evaluates to is not null and not 1707 * an errno stored in a pointer. This is the same as 1708 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see 1709 * KUNIT_ASSERT_TRUE()) when the assertion is not met. 1710 */ 1711 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \ 1712 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL) 1713 1714 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \ 1715 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \ 1716 KUNIT_ASSERTION, \ 1717 ptr, \ 1718 fmt, \ 1719 ##__VA_ARGS__) 1720 1721 /** 1722 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array. 1723 * @name: prefix for the test parameter generator function. 1724 * @array: array of test parameters. 1725 * @get_desc: function to convert param to description; NULL to use default 1726 * 1727 * Define function @name_gen_params which uses @array to generate parameters. 1728 */ 1729 #define KUNIT_ARRAY_PARAM(name, array, get_desc) \ 1730 static const void *name##_gen_params(struct kunit *test, \ 1731 const void *prev, char *desc) \ 1732 { \ 1733 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \ 1734 if (!prev) \ 1735 kunit_register_params_array(test, array, ARRAY_SIZE(array), NULL); \ 1736 if (__next - (array) < ARRAY_SIZE((array))) { \ 1737 void (*__get_desc)(typeof(__next), char *) = get_desc; \ 1738 if (__get_desc) \ 1739 __get_desc(__next, desc); \ 1740 return __next; \ 1741 } \ 1742 return NULL; \ 1743 } 1744 1745 /** 1746 * KUNIT_ARRAY_PARAM_DESC() - Define test parameter generator from an array. 1747 * @name: prefix for the test parameter generator function. 1748 * @array: array of test parameters. 1749 * @desc_member: structure member from array element to use as description 1750 * 1751 * Define function @name_gen_params which uses @array to generate parameters. 1752 */ 1753 #define KUNIT_ARRAY_PARAM_DESC(name, array, desc_member) \ 1754 static const void *name##_gen_params(struct kunit *test, \ 1755 const void *prev, char *desc) \ 1756 { \ 1757 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \ 1758 if (!prev) \ 1759 kunit_register_params_array(test, array, ARRAY_SIZE(array), NULL); \ 1760 if (__next - (array) < ARRAY_SIZE((array))) { \ 1761 strscpy(desc, __next->desc_member, KUNIT_PARAM_DESC_SIZE); \ 1762 return __next; \ 1763 } \ 1764 return NULL; \ 1765 } 1766 1767 /** 1768 * kunit_register_params_array() - Register parameter array for a KUnit test. 1769 * @test: The KUnit test structure to which parameters will be added. 1770 * @array: An array of test parameters. 1771 * @param_count: Number of parameters. 1772 * @get_desc: Function that generates a string description for a given parameter 1773 * element. 1774 * 1775 * This macro initializes the @test's parameter array data, storing information 1776 * including the parameter array, its count, the element size, and the parameter 1777 * description function within `test->params_array`. 1778 * 1779 * Note: If using this macro in param_init(), kunit_array_gen_params() 1780 * will then need to be manually provided as the parameter generator function to 1781 * KUNIT_CASE_PARAM_WITH_INIT(). kunit_array_gen_params() is a KUnit 1782 * function that uses the registered array to generate parameters 1783 */ 1784 #define kunit_register_params_array(test, array, param_count, get_desc) \ 1785 do { \ 1786 struct kunit *_test = (test); \ 1787 const typeof((array)[0]) * _params_ptr = &(array)[0]; \ 1788 _test->params_array.params = _params_ptr; \ 1789 _test->params_array.num_params = (param_count); \ 1790 _test->params_array.elem_size = sizeof(*_params_ptr); \ 1791 _test->params_array.get_description = (get_desc); \ 1792 } while (0) 1793 1794 // TODO(dlatypov@google.com): consider eventually migrating users to explicitly 1795 // include resource.h themselves if they need it. 1796 #include <kunit/resource.h> 1797 1798 #endif /* _KUNIT_TEST_H */ 1799