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