projects/linux·blob master · refs

1898 lines · 68.2 KiB

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 <[email protected]>
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 */
34DECLARE_STATIC_KEY_FALSE(kunit_running);
35
36struct kunit;
37struct 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 */
60enum 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 */
75enum 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 */
84struct 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 */
128struct 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
143static 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 */
273struct 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 enum kunit_status status;
289};
290
291/* Stores an array of suites, end points one past the end */
292struct kunit_suite_set {
293 struct kunit_suite * const *start;
294 struct kunit_suite * const *end;
295};
296
297/* Stores the pointer to the parameter array and its metadata. */
298struct kunit_params {
299 /*
300 * Reference to the parameter array for a parameterized test. This
301 * is NULL if a parameter array wasn't directly passed to the
302 * parameterized test context struct kunit via kunit_register_params_array().
303 */
304 const void *params;
305 /* Reference to a function that gets the description of a parameter. */
306 void (*get_description)(struct kunit *test, const void *param, char *desc);
307 size_t num_params;
308 size_t elem_size;
309};
310
311/**
312 * struct kunit - represents a running instance of a test.
313 *
314 * @priv: for user to store arbitrary data. Commonly used to pass data
315 * created in the init function (see &struct kunit_suite).
316 * @parent: reference to the parent context of type struct kunit that can
317 * be used for storing shared resources.
318 * @params_array: for storing the parameter array.
319 *
320 * Used to store information about the current context under which the test
321 * is running. Most of this data is private and should only be accessed
322 * indirectly via public functions; the exceptions are @priv, @parent and
323 * @params_array which can be used by the test writer to store arbitrary data,
324 * access the parent context, and to store the parameter array, respectively.
325 */
326struct kunit {
327 void *priv;
328 struct kunit *parent;
329 struct kunit_params params_array;
330
331 /* private: internal use only. */
332 const char *name; /* Read only after initialization! */
333 struct string_stream *log; /* Points at case log after initialization */
334 struct kunit_try_catch try_catch;
335 /* param_value is the current parameter value for a test case. */
336 const void *param_value;
337 /* param_index stores the index of the parameter in parameterized tests. */
338 int param_index;
339 /*
340 * success starts as true, and may only be set to false during a
341 * test case; thus, it is safe to update this across multiple
342 * threads using WRITE_ONCE; however, as a consequence, it may only
343 * be read after the test case finishes once all threads associated
344 * with the test case have terminated.
345 */
346 spinlock_t lock; /* Guards all mutable test state. */
347 enum kunit_status status; /* Read only after test_case finishes! */
348 /*
349 * Because resources is a list that may be updated multiple times (with
350 * new resources) from any thread associated with a test case, we must
351 * protect it with some type of lock.
352 */
353 struct list_head resources; /* Protected by lock. */
354
355 char status_comment[KUNIT_STATUS_COMMENT_SIZE];
356 /* Saves the last seen test. Useful to help with faults. */
357 struct kunit_loc last_seen;
358};
359
360static inline void kunit_set_failure(struct kunit *test)
361{
362 WRITE_ONCE(test->status, KUNIT_FAILURE);
363}
364
365bool kunit_enabled(void);
366bool kunit_autorun(void);
367const char *kunit_action(void);
368const char *kunit_filter_glob(void);
369char *kunit_filter(void);
370char *kunit_filter_action(void);
371
372void kunit_init_test(struct kunit *test, const char *name, struct string_stream *log);
373
374int kunit_run_tests(struct kunit_suite *suite);
375
376size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
377
378unsigned int kunit_test_case_num(struct kunit_suite *suite,
379 struct kunit_case *test_case);
380
381struct kunit_suite_set
382kunit_filter_suites(const struct kunit_suite_set *suite_set,
383 const char *filter_glob,
384 char *filters,
385 char *filter_action,
386 int *err);
387void kunit_free_suite_set(struct kunit_suite_set suite_set);
388
389int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites,
390 bool run_tests);
391
392void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);
393
394void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin);
395void kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr);
396
397struct kunit_suite_set kunit_merge_suite_sets(struct kunit_suite_set init_suite_set,
398 struct kunit_suite_set suite_set);
399
400const void *kunit_array_gen_params(struct kunit *test, const void *prev, char *desc);
401
402#if IS_BUILTIN(CONFIG_KUNIT)
403int kunit_run_all_tests(void);
404#else
405static inline int kunit_run_all_tests(void)
406{
407 return 0;
408}
409#endif /* IS_BUILTIN(CONFIG_KUNIT) */
410
411#define __kunit_test_suites(unique_array, ...) \
412 static struct kunit_suite *unique_array[] \
413 __aligned(sizeof(struct kunit_suite *)) \
414 __used __section(".kunit_test_suites") = { __VA_ARGS__ }
415
416/**
417 * kunit_test_suites() - used to register one or more &struct kunit_suite
418 * with KUnit.
419 *
420 * @__suites: a statically allocated list of &struct kunit_suite.
421 *
422 * Registers @suites with the test framework.
423 * This is done by placing the array of struct kunit_suite * in the
424 * .kunit_test_suites ELF section.
425 *
426 * When builtin, KUnit tests are all run via the executor at boot, and when
427 * built as a module, they run on module load.
428 *
429 */
430#define kunit_test_suites(__suites...) \
431 __kunit_test_suites(__UNIQUE_ID(array), \
432 ##__suites)
433
434#define kunit_test_suite(suite) kunit_test_suites(&suite)
435
436#define __kunit_init_test_suites(unique_array, ...) \
437 static struct kunit_suite *unique_array[] \
438 __aligned(sizeof(struct kunit_suite *)) \
439 __used __section(".kunit_init_test_suites") = { __VA_ARGS__ }
440
441/**
442 * kunit_test_init_section_suites() - used to register one or more &struct
443 * kunit_suite containing init functions or
444 * init data.
445 *
446 * @__suites: a statically allocated list of &struct kunit_suite.
447 *
448 * This functions similar to kunit_test_suites() except that it compiles the
449 * list of suites during init phase.
450 *
451 * This macro also suffixes the array and suite declarations it makes with
452 * _probe; so that modpost suppresses warnings about referencing init data
453 * for symbols named in this manner.
454 *
455 * Note: these init tests are not able to be run after boot so there is no
456 * "run" debugfs file generated for these tests.
457 *
458 * Also, do not mark the suite or test case structs with __initdata because
459 * they will be used after the init phase with debugfs.
460 */
461#define kunit_test_init_section_suites(__suites...) \
462 __kunit_init_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \
463 ##__suites)
464
465#define kunit_test_init_section_suite(suite) \
466 kunit_test_init_section_suites(&suite)
467
468#define kunit_suite_for_each_test_case(suite, test_case) \
469 for (test_case = suite->test_cases; test_case->run_case; test_case++)
470
471enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
472
473/**
474 * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
475 * @test: The test context object.
476 * @n: number of elements.
477 * @size: The size in bytes of the desired memory.
478 * @gfp: flags passed to underlying kmalloc().
479 *
480 * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
481 * and is automatically cleaned up after the test case concludes. See kunit_add_action()
482 * for more information.
483 *
484 * Note that some internal context data is also allocated with GFP_KERNEL,
485 * regardless of the gfp passed in.
486 */
487void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
488
489/**
490 * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
491 * @test: The test context object.
492 * @size: The size in bytes of the desired memory.
493 * @gfp: flags passed to underlying kmalloc().
494 *
495 * See kmalloc() and kunit_kmalloc_array() for more information.
496 *
497 * Note that some internal context data is also allocated with GFP_KERNEL,
498 * regardless of the gfp passed in.
499 */
500static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
501{
502 return kunit_kmalloc_array(test, 1, size, gfp);
503}
504
505/**
506 * kunit_kfree() - Like kfree except for allocations managed by KUnit.
507 * @test: The test case to which the resource belongs.
508 * @ptr: The memory allocation to free.
509 */
510void kunit_kfree(struct kunit *test, const void *ptr);
511
512/**
513 * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
514 * @test: The test context object.
515 * @size: The size in bytes of the desired memory.
516 * @gfp: flags passed to underlying kmalloc().
517 *
518 * See kzalloc() and kunit_kmalloc_array() for more information.
519 */
520static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
521{
522 return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
523}
524
525/**
526 * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
527 * @test: The test context object.
528 * @n: number of elements.
529 * @size: The size in bytes of the desired memory.
530 * @gfp: flags passed to underlying kmalloc().
531 *
532 * See kcalloc() and kunit_kmalloc_array() for more information.
533 */
534static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
535{
536 return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
537}
538
539
540/**
541 * kunit_kfree_const() - conditionally free test managed memory
542 * @test: The test context object.
543 * @x: pointer to the memory
544 *
545 * Calls kunit_kfree() only if @x is not in .rodata section.
546 * See kunit_kstrdup_const() for more information.
547 */
548void kunit_kfree_const(struct kunit *test, const void *x);
549
550/**
551 * kunit_kstrdup() - Duplicates a string into a test managed allocation.
552 *
553 * @test: The test context object.
554 * @str: The NULL-terminated string to duplicate.
555 * @gfp: flags passed to underlying kmalloc().
556 *
557 * See kstrdup() and kunit_kmalloc_array() for more information.
558 */
559static inline char *kunit_kstrdup(struct kunit *test, const char *str, gfp_t gfp)
560{
561 size_t len;
562 char *buf;
563
564 if (!str)
565 return NULL;
566
567 len = strlen(str) + 1;
568 buf = kunit_kmalloc(test, len, gfp);
569 if (buf)
570 memcpy(buf, str, len);
571 return buf;
572}
573
574/**
575 * kunit_kstrdup_const() - Conditionally duplicates a string into a test managed allocation.
576 *
577 * @test: The test context object.
578 * @str: The NULL-terminated string to duplicate.
579 * @gfp: flags passed to underlying kmalloc().
580 *
581 * Calls kunit_kstrdup() only if @str is not in the rodata section. Must be freed with
582 * kunit_kfree_const() -- not kunit_kfree().
583 * See kstrdup_const() and kunit_kmalloc_array() for more information.
584 */
585const char *kunit_kstrdup_const(struct kunit *test, const char *str, gfp_t gfp);
586
587/**
588 * kunit_attach_mm() - Create and attach a new mm if it doesn't already exist.
589 *
590 * Allocates a &struct mm_struct and attaches it to @current. In most cases, call
591 * kunit_vm_mmap() without calling kunit_attach_mm() directly. Only necessary when
592 * code under test accesses the mm before executing the mmap (e.g., to perform
593 * additional initialization beforehand).
594 *
595 * Return: 0 on success, -errno on failure.
596 */
597int kunit_attach_mm(void);
598
599/**
600 * kunit_vm_mmap() - Allocate KUnit-tracked vm_mmap() area
601 * @test: The test context object.
602 * @file: struct file pointer to map from, if any
603 * @addr: desired address, if any
604 * @len: how many bytes to allocate
605 * @prot: mmap PROT_* bits
606 * @flag: mmap flags
607 * @offset: offset into @file to start mapping from.
608 *
609 * See vm_mmap() for more information.
610 */
611unsigned long kunit_vm_mmap(struct kunit *test, struct file *file,
612 unsigned long addr, unsigned long len,
613 unsigned long prot, unsigned long flag,
614 unsigned long offset);
615
616void kunit_cleanup(struct kunit *test);
617void kunit_free_boot_suites(void);
618
619void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt, ...);
620
621/**
622 * kunit_mark_skipped() - Marks @test as skipped
623 *
624 * @test: The test context object.
625 * @fmt: A printk() style format string.
626 *
627 * Marks the test as skipped. @fmt is given output as the test status
628 * comment, typically the reason the test was skipped.
629 *
630 * Test execution continues after kunit_mark_skipped() is called.
631 */
632#define kunit_mark_skipped(test, fmt, ...) \
633 do { \
634 WRITE_ONCE((test)->status, KUNIT_SKIPPED); \
635 scnprintf((test)->status_comment, \
636 KUNIT_STATUS_COMMENT_SIZE, \
637 fmt, ##__VA_ARGS__); \
638 } while (0)
639
640/**
641 * kunit_skip() - Marks @test as skipped
642 *
643 * @test: The test context object.
644 * @fmt: A printk() style format string.
645 *
646 * Skips the test. @fmt is given output as the test status
647 * comment, typically the reason the test was skipped.
648 *
649 * Test execution is halted after kunit_skip() is called.
650 */
651#define kunit_skip(test, fmt, ...) \
652 do { \
653 kunit_mark_skipped((test), fmt, ##__VA_ARGS__); \
654 kunit_try_catch_throw(&((test)->try_catch)); \
655 } while (0)
656
657/*
658 * printk and log to per-test or per-suite log buffer. Logging only done
659 * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
660 */
661#define kunit_log(lvl, test_or_suite, fmt, ...) \
662 do { \
663 printk(lvl fmt, ##__VA_ARGS__); \
664 kunit_log_append((test_or_suite)->log, fmt, \
665 ##__VA_ARGS__); \
666 } while (0)
667
668#define kunit_printk(lvl, test, fmt, ...) \
669 kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
670 (test)->name, ##__VA_ARGS__)
671
672/**
673 * kunit_info() - Prints an INFO level message associated with @test.
674 *
675 * @test: The test context object.
676 * @fmt: A printk() style format string.
677 *
678 * Prints an info level message associated with the test suite being run.
679 * Takes a variable number of format parameters just like printk().
680 */
681#define kunit_info(test, fmt, ...) \
682 kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
683
684/**
685 * kunit_warn() - Prints a WARN level message associated with @test.
686 *
687 * @test: The test context object.
688 * @fmt: A printk() style format string.
689 *
690 * Prints a warning level message.
691 */
692#define kunit_warn(test, fmt, ...) \
693 kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
694
695/**
696 * kunit_err() - Prints an ERROR level message associated with @test.
697 *
698 * @test: The test context object.
699 * @fmt: A printk() style format string.
700 *
701 * Prints an error level message.
702 */
703#define kunit_err(test, fmt, ...) \
704 kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
705
706/*
707 * Must be called at the beginning of each KUNIT_*_ASSERTION().
708 * Cf. KUNIT_CURRENT_LOC.
709 */
710#define _KUNIT_SAVE_LOC(test) do { \
711 WRITE_ONCE(test->last_seen.file, __FILE__); \
712 WRITE_ONCE(test->last_seen.line, __LINE__); \
713} while (0)
714
715/**
716 * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
717 * @test: The test context object.
718 *
719 * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
720 * words, it does nothing and only exists for code clarity. See
721 * KUNIT_EXPECT_TRUE() for more information.
722 */
723#define KUNIT_SUCCEED(test) _KUNIT_SAVE_LOC(test)
724
725void __noreturn __kunit_abort(struct kunit *test);
726
727void __printf(6, 7) __kunit_do_failed_assertion(struct kunit *test,
728 const struct kunit_loc *loc,
729 enum kunit_assert_type type,
730 const struct kunit_assert *assert,
731 assert_format_t assert_format,
732 const char *fmt, ...);
733
734#define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
735 static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
736 const struct assert_class __assertion = INITIALIZER; \
737 __kunit_do_failed_assertion(test, \
738 &__loc, \
739 assert_type, \
740 &__assertion.assert, \
741 assert_format, \
742 fmt, \
743 ##__VA_ARGS__); \
744 if (assert_type == KUNIT_ASSERTION) \
745 __kunit_abort(test); \
746} while (0)
747
748
749#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) do { \
750 _KUNIT_SAVE_LOC(test); \
751 _KUNIT_FAILED(test, \
752 assert_type, \
753 kunit_fail_assert, \
754 kunit_fail_assert_format, \
755 {}, \
756 fmt, \
757 ##__VA_ARGS__); \
758} while (0)
759
760/**
761 * KUNIT_FAIL() - Always causes a test to fail when evaluated.
762 * @test: The test context object.
763 * @fmt: an informational message to be printed when the assertion is made.
764 * @...: string format arguments.
765 *
766 * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
767 * other words, it always results in a failed expectation, and consequently
768 * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
769 * for more information.
770 */
771#define KUNIT_FAIL(test, fmt, ...) \
772 KUNIT_FAIL_ASSERTION(test, \
773 KUNIT_EXPECTATION, \
774 fmt, \
775 ##__VA_ARGS__)
776
777/* Helper to safely pass around an initializer list to other macros. */
778#define KUNIT_INIT_ASSERT(initializers...) { initializers }
779
780#define KUNIT_UNARY_ASSERTION(test, \
781 assert_type, \
782 condition_, \
783 expected_true_, \
784 fmt, \
785 ...) \
786do { \
787 _KUNIT_SAVE_LOC(test); \
788 if (likely(!!(condition_) == !!expected_true_)) \
789 break; \
790 \
791 _KUNIT_FAILED(test, \
792 assert_type, \
793 kunit_unary_assert, \
794 kunit_unary_assert_format, \
795 KUNIT_INIT_ASSERT(.condition = #condition_, \
796 .expected_true = expected_true_), \
797 fmt, \
798 ##__VA_ARGS__); \
799} while (0)
800
801#define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
802 KUNIT_UNARY_ASSERTION(test, \
803 assert_type, \
804 condition, \
805 true, \
806 fmt, \
807 ##__VA_ARGS__)
808
809#define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
810 KUNIT_UNARY_ASSERTION(test, \
811 assert_type, \
812 condition, \
813 false, \
814 fmt, \
815 ##__VA_ARGS__)
816
817/*
818 * A factory macro for defining the assertions and expectations for the basic
819 * comparisons defined for the built in types.
820 *
821 * Unfortunately, there is no common type that all types can be promoted to for
822 * which all the binary operators behave the same way as for the actual types
823 * (for example, there is no type that long long and unsigned long long can
824 * both be cast to where the comparison result is preserved for all values). So
825 * the best we can do is do the comparison in the original types and then coerce
826 * everything to long long for printing; this way, the comparison behaves
827 * correctly and the printed out value usually makes sense without
828 * interpretation, but can always be interpreted to figure out the actual
829 * value.
830 */
831#define KUNIT_BASE_BINARY_ASSERTION(test, \
832 assert_class, \
833 format_func, \
834 assert_type, \
835 left, \
836 op, \
837 right, \
838 fmt, \
839 ...) \
840do { \
841 const typeof(left) __left = (left); \
842 const typeof(right) __right = (right); \
843 static const struct kunit_binary_assert_text __text = { \
844 .operation = #op, \
845 .left_text = #left, \
846 .right_text = #right, \
847 }; \
848 \
849 _KUNIT_SAVE_LOC(test); \
850 if (likely(__left op __right)) \
851 break; \
852 \
853 _KUNIT_FAILED(test, \
854 assert_type, \
855 assert_class, \
856 format_func, \
857 KUNIT_INIT_ASSERT(.text = &__text, \
858 .left_value = __left, \
859 .right_value = __right), \
860 fmt, \
861 ##__VA_ARGS__); \
862} while (0)
863
864#define KUNIT_BINARY_INT_ASSERTION(test, \
865 assert_type, \
866 left, \
867 op, \
868 right, \
869 fmt, \
870 ...) \
871 KUNIT_BASE_BINARY_ASSERTION(test, \
872 kunit_binary_assert, \
873 kunit_binary_assert_format, \
874 assert_type, \
875 left, op, right, \
876 fmt, \
877 ##__VA_ARGS__)
878
879#define KUNIT_BINARY_PTR_ASSERTION(test, \
880 assert_type, \
881 left, \
882 op, \
883 right, \
884 fmt, \
885 ...) \
886 KUNIT_BASE_BINARY_ASSERTION(test, \
887 kunit_binary_ptr_assert, \
888 kunit_binary_ptr_assert_format, \
889 assert_type, \
890 left, op, right, \
891 fmt, \
892 ##__VA_ARGS__)
893
894#define KUNIT_BINARY_STR_ASSERTION(test, \
895 assert_type, \
896 left, \
897 op, \
898 right, \
899 fmt, \
900 ...) \
901do { \
902 const char *__left = (left); \
903 const char *__right = (right); \
904 static const struct kunit_binary_assert_text __text = { \
905 .operation = #op, \
906 .left_text = #left, \
907 .right_text = #right, \
908 }; \
909 \
910 _KUNIT_SAVE_LOC(test); \
911 if (likely(!IS_ERR_OR_NULL(__left) && !IS_ERR_OR_NULL(__right) && \
912 (strcmp(__left, __right) op 0))) \
913 break; \
914 \
915 \
916 _KUNIT_FAILED(test, \
917 assert_type, \
918 kunit_binary_str_assert, \
919 kunit_binary_str_assert_format, \
920 KUNIT_INIT_ASSERT(.text = &__text, \
921 .left_value = __left, \
922 .right_value = __right), \
923 fmt, \
924 ##__VA_ARGS__); \
925} while (0)
926
927#define KUNIT_MEM_ASSERTION(test, \
928 assert_type, \
929 left, \
930 op, \
931 right, \
932 size_, \
933 fmt, \
934 ...) \
935do { \
936 const void *__left = (left); \
937 const void *__right = (right); \
938 const size_t __size = (size_); \
939 static const struct kunit_binary_assert_text __text = { \
940 .operation = #op, \
941 .left_text = #left, \
942 .right_text = #right, \
943 }; \
944 \
945 _KUNIT_SAVE_LOC(test); \
946 if (likely(__left && __right)) \
947 if (likely(memcmp(__left, __right, __size) op 0)) \
948 break; \
949 \
950 _KUNIT_FAILED(test, \
951 assert_type, \
952 kunit_mem_assert, \
953 kunit_mem_assert_format, \
954 KUNIT_INIT_ASSERT(.text = &__text, \
955 .left_value = __left, \
956 .right_value = __right, \
957 .size = __size), \
958 fmt, \
959 ##__VA_ARGS__); \
960} while (0)
961
962#define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
963 assert_type, \
964 ptr, \
965 fmt, \
966 ...) \
967do { \
968 const typeof(ptr) __ptr = (ptr); \
969 \
970 _KUNIT_SAVE_LOC(test); \
971 if (!IS_ERR_OR_NULL(__ptr)) \
972 break; \
973 \
974 _KUNIT_FAILED(test, \
975 assert_type, \
976 kunit_ptr_not_err_assert, \
977 kunit_ptr_not_err_assert_format, \
978 KUNIT_INIT_ASSERT(.text = #ptr, .value = __ptr), \
979 fmt, \
980 ##__VA_ARGS__); \
981} while (0)
982
983/**
984 * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
985 * @test: The test context object.
986 * @condition: an arbitrary boolean expression. The test fails when this does
987 * not evaluate to true.
988 *
989 * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
990 * to fail when the specified condition is not met; however, it will not prevent
991 * the test case from continuing to run; this is otherwise known as an
992 * *expectation failure*.
993 */
994#define KUNIT_EXPECT_TRUE(test, condition) \
995 KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
996
997#define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
998 KUNIT_TRUE_MSG_ASSERTION(test, \
999 KUNIT_EXPECTATION, \
1000 condition, \
1001 fmt, \
1002 ##__VA_ARGS__)
1003
1004/**
1005 * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
1006 * @test: The test context object.
1007 * @condition: an arbitrary boolean expression. The test fails when this does
1008 * not evaluate to false.
1009 *
1010 * Sets an expectation that @condition evaluates to false. See
1011 * KUNIT_EXPECT_TRUE() for more information.
1012 */
1013#define KUNIT_EXPECT_FALSE(test, condition) \
1014 KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
1015
1016#define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
1017 KUNIT_FALSE_MSG_ASSERTION(test, \
1018 KUNIT_EXPECTATION, \
1019 condition, \
1020 fmt, \
1021 ##__VA_ARGS__)
1022
1023/**
1024 * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
1025 * @test: The test context object.
1026 * @left: an arbitrary expression that evaluates to a primitive C type.
1027 * @right: an arbitrary expression that evaluates to a primitive C type.
1028 *
1029 * Sets an expectation that the values that @left and @right evaluate to are
1030 * equal. This is semantically equivalent to
1031 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
1032 * more information.
1033 */
1034#define KUNIT_EXPECT_EQ(test, left, right) \
1035 KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
1036
1037#define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
1038 KUNIT_BINARY_INT_ASSERTION(test, \
1039 KUNIT_EXPECTATION, \
1040 left, ==, right, \
1041 fmt, \
1042 ##__VA_ARGS__)
1043
1044/**
1045 * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
1046 * @test: The test context object.
1047 * @left: an arbitrary expression that evaluates to a pointer.
1048 * @right: an arbitrary expression that evaluates to a pointer.
1049 *
1050 * Sets an expectation that the values that @left and @right evaluate to are
1051 * equal. This is semantically equivalent to
1052 * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
1053 * more information.
1054 */
1055#define KUNIT_EXPECT_PTR_EQ(test, left, right) \
1056 KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
1057
1058#define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1059 KUNIT_BINARY_PTR_ASSERTION(test, \
1060 KUNIT_EXPECTATION, \
1061 left, ==, right, \
1062 fmt, \
1063 ##__VA_ARGS__)
1064
1065/**
1066 * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
1067 * @test: The test context object.
1068 * @left: an arbitrary expression that evaluates to a primitive C type.
1069 * @right: an arbitrary expression that evaluates to a primitive C type.
1070 *
1071 * Sets an expectation that the values that @left and @right evaluate to are not
1072 * equal. This is semantically equivalent to
1073 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1074 * more information.
1075 */
1076#define KUNIT_EXPECT_NE(test, left, right) \
1077 KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
1078
1079#define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
1080 KUNIT_BINARY_INT_ASSERTION(test, \
1081 KUNIT_EXPECTATION, \
1082 left, !=, right, \
1083 fmt, \
1084 ##__VA_ARGS__)
1085
1086/**
1087 * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
1088 * @test: The test context object.
1089 * @left: an arbitrary expression that evaluates to a pointer.
1090 * @right: an arbitrary expression that evaluates to a pointer.
1091 *
1092 * Sets an expectation that the values that @left and @right evaluate to are not
1093 * equal. This is semantically equivalent to
1094 * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
1095 * more information.
1096 */
1097#define KUNIT_EXPECT_PTR_NE(test, left, right) \
1098 KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
1099
1100#define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
1101 KUNIT_BINARY_PTR_ASSERTION(test, \
1102 KUNIT_EXPECTATION, \
1103 left, !=, right, \
1104 fmt, \
1105 ##__VA_ARGS__)
1106
1107/**
1108 * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
1109 * @test: The test context object.
1110 * @left: an arbitrary expression that evaluates to a primitive C type.
1111 * @right: an arbitrary expression that evaluates to a primitive C type.
1112 *
1113 * Sets an expectation that the value that @left evaluates to is less than the
1114 * value that @right evaluates to. This is semantically equivalent to
1115 * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
1116 * more information.
1117 */
1118#define KUNIT_EXPECT_LT(test, left, right) \
1119 KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
1120
1121#define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
1122 KUNIT_BINARY_INT_ASSERTION(test, \
1123 KUNIT_EXPECTATION, \
1124 left, <, right, \
1125 fmt, \
1126 ##__VA_ARGS__)
1127
1128/**
1129 * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
1130 * @test: The test context object.
1131 * @left: an arbitrary expression that evaluates to a primitive C type.
1132 * @right: an arbitrary expression that evaluates to a primitive C type.
1133 *
1134 * Sets an expectation that the value that @left evaluates to is less than or
1135 * equal to the value that @right evaluates to. Semantically this is equivalent
1136 * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
1137 * more information.
1138 */
1139#define KUNIT_EXPECT_LE(test, left, right) \
1140 KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
1141
1142#define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
1143 KUNIT_BINARY_INT_ASSERTION(test, \
1144 KUNIT_EXPECTATION, \
1145 left, <=, right, \
1146 fmt, \
1147 ##__VA_ARGS__)
1148
1149/**
1150 * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
1151 * @test: The test context object.
1152 * @left: an arbitrary expression that evaluates to a primitive C type.
1153 * @right: an arbitrary expression that evaluates to a primitive C type.
1154 *
1155 * Sets an expectation that the value that @left evaluates to is greater than
1156 * the value that @right evaluates to. This is semantically equivalent to
1157 * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
1158 * more information.
1159 */
1160#define KUNIT_EXPECT_GT(test, left, right) \
1161 KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
1162
1163#define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
1164 KUNIT_BINARY_INT_ASSERTION(test, \
1165 KUNIT_EXPECTATION, \
1166 left, >, right, \
1167 fmt, \
1168 ##__VA_ARGS__)
1169
1170/**
1171 * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
1172 * @test: The test context object.
1173 * @left: an arbitrary expression that evaluates to a primitive C type.
1174 * @right: an arbitrary expression that evaluates to a primitive C type.
1175 *
1176 * Sets an expectation that the value that @left evaluates to is greater than
1177 * the value that @right evaluates to. This is semantically equivalent to
1178 * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
1179 * more information.
1180 */
1181#define KUNIT_EXPECT_GE(test, left, right) \
1182 KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
1183
1184#define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
1185 KUNIT_BINARY_INT_ASSERTION(test, \
1186 KUNIT_EXPECTATION, \
1187 left, >=, right, \
1188 fmt, \
1189 ##__VA_ARGS__)
1190
1191/**
1192 * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
1193 * @test: The test context object.
1194 * @left: an arbitrary expression that evaluates to a null terminated string.
1195 * @right: an arbitrary expression that evaluates to a null terminated string.
1196 *
1197 * Sets an expectation that the values that @left and @right evaluate to are
1198 * equal. This is semantically equivalent to
1199 * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1200 * for more information.
1201 */
1202#define KUNIT_EXPECT_STREQ(test, left, right) \
1203 KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
1204
1205#define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
1206 KUNIT_BINARY_STR_ASSERTION(test, \
1207 KUNIT_EXPECTATION, \
1208 left, ==, right, \
1209 fmt, \
1210 ##__VA_ARGS__)
1211
1212/**
1213 * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
1214 * @test: The test context object.
1215 * @left: an arbitrary expression that evaluates to a null terminated string.
1216 * @right: an arbitrary expression that evaluates to a null terminated string.
1217 *
1218 * Sets an expectation that the values that @left and @right evaluate to are
1219 * not equal. This is semantically equivalent to
1220 * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
1221 * for more information.
1222 */
1223#define KUNIT_EXPECT_STRNEQ(test, left, right) \
1224 KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
1225
1226#define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
1227 KUNIT_BINARY_STR_ASSERTION(test, \
1228 KUNIT_EXPECTATION, \
1229 left, !=, right, \
1230 fmt, \
1231 ##__VA_ARGS__)
1232
1233/**
1234 * KUNIT_EXPECT_MEMEQ() - Expects that the first @size bytes of @left and @right are equal.
1235 * @test: The test context object.
1236 * @left: An arbitrary expression that evaluates to the specified size.
1237 * @right: An arbitrary expression that evaluates to the specified size.
1238 * @size: Number of bytes compared.
1239 *
1240 * Sets an expectation that the values that @left and @right evaluate to are
1241 * equal. This is semantically equivalent to
1242 * KUNIT_EXPECT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
1243 * KUNIT_EXPECT_TRUE() for more information.
1244 *
1245 * Although this expectation works for any memory block, it is not recommended
1246 * for comparing more structured data, such as structs. This expectation is
1247 * recommended for comparing, for example, data arrays.
1248 */
1249#define KUNIT_EXPECT_MEMEQ(test, left, right, size) \
1250 KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, NULL)
1251
1252#define KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, fmt, ...) \
1253 KUNIT_MEM_ASSERTION(test, \
1254 KUNIT_EXPECTATION, \
1255 left, ==, right, \
1256 size, \
1257 fmt, \
1258 ##__VA_ARGS__)
1259
1260/**
1261 * KUNIT_EXPECT_MEMNEQ() - Expects that the first @size bytes of @left and @right are not equal.
1262 * @test: The test context object.
1263 * @left: An arbitrary expression that evaluates to the specified size.
1264 * @right: An arbitrary expression that evaluates to the specified size.
1265 * @size: Number of bytes compared.
1266 *
1267 * Sets an expectation that the values that @left and @right evaluate to are
1268 * not equal. This is semantically equivalent to
1269 * KUNIT_EXPECT_TRUE(@test, memcmp((@left), (@right), (@size))). See
1270 * KUNIT_EXPECT_TRUE() for more information.
1271 *
1272 * Although this expectation works for any memory block, it is not recommended
1273 * for comparing more structured data, such as structs. This expectation is
1274 * recommended for comparing, for example, data arrays.
1275 */
1276#define KUNIT_EXPECT_MEMNEQ(test, left, right, size) \
1277 KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, NULL)
1278
1279#define KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \
1280 KUNIT_MEM_ASSERTION(test, \
1281 KUNIT_EXPECTATION, \
1282 left, !=, right, \
1283 size, \
1284 fmt, \
1285 ##__VA_ARGS__)
1286
1287/**
1288 * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
1289 * @test: The test context object.
1290 * @ptr: an arbitrary pointer.
1291 *
1292 * Sets an expectation that the value that @ptr evaluates to is null. This is
1293 * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
1294 * See KUNIT_EXPECT_TRUE() for more information.
1295 */
1296#define KUNIT_EXPECT_NULL(test, ptr) \
1297 KUNIT_EXPECT_NULL_MSG(test, \
1298 ptr, \
1299 NULL)
1300
1301#define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \
1302 KUNIT_BINARY_PTR_ASSERTION(test, \
1303 KUNIT_EXPECTATION, \
1304 ptr, ==, NULL, \
1305 fmt, \
1306 ##__VA_ARGS__)
1307
1308/**
1309 * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
1310 * @test: The test context object.
1311 * @ptr: an arbitrary pointer.
1312 *
1313 * Sets an expectation that the value that @ptr evaluates to is not null. This
1314 * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
1315 * See KUNIT_EXPECT_TRUE() for more information.
1316 */
1317#define KUNIT_EXPECT_NOT_NULL(test, ptr) \
1318 KUNIT_EXPECT_NOT_NULL_MSG(test, \
1319 ptr, \
1320 NULL)
1321
1322#define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1323 KUNIT_BINARY_PTR_ASSERTION(test, \
1324 KUNIT_EXPECTATION, \
1325 ptr, !=, NULL, \
1326 fmt, \
1327 ##__VA_ARGS__)
1328
1329/**
1330 * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
1331 * @test: The test context object.
1332 * @ptr: an arbitrary pointer.
1333 *
1334 * Sets an expectation that the value that @ptr evaluates to is not null and not
1335 * an errno stored in a pointer. This is semantically equivalent to
1336 * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
1337 * more information.
1338 */
1339#define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
1340 KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
1341
1342#define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1343 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1344 KUNIT_EXPECTATION, \
1345 ptr, \
1346 fmt, \
1347 ##__VA_ARGS__)
1348
1349/**
1350 * KUNIT_FAIL_AND_ABORT() - Always causes a test to fail and abort when evaluated.
1351 * @test: The test context object.
1352 * @fmt: an informational message to be printed when the assertion is made.
1353 * @...: string format arguments.
1354 *
1355 * The opposite of KUNIT_SUCCEED(), it is an assertion that always fails. In
1356 * other words, it always results in a failed assertion, and consequently
1357 * always causes the test case to fail and abort when evaluated.
1358 * See KUNIT_ASSERT_TRUE() for more information.
1359 */
1360#define KUNIT_FAIL_AND_ABORT(test, fmt, ...) \
1361 KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
1362
1363/**
1364 * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
1365 * @test: The test context object.
1366 * @condition: an arbitrary boolean expression. The test fails and aborts when
1367 * this does not evaluate to true.
1368 *
1369 * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
1370 * fail *and immediately abort* when the specified condition is not met. Unlike
1371 * an expectation failure, it will prevent the test case from continuing to run;
1372 * this is otherwise known as an *assertion failure*.
1373 */
1374#define KUNIT_ASSERT_TRUE(test, condition) \
1375 KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
1376
1377#define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
1378 KUNIT_TRUE_MSG_ASSERTION(test, \
1379 KUNIT_ASSERTION, \
1380 condition, \
1381 fmt, \
1382 ##__VA_ARGS__)
1383
1384/**
1385 * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1386 * @test: The test context object.
1387 * @condition: an arbitrary boolean expression.
1388 *
1389 * Sets an assertion that the value that @condition evaluates to is false. This
1390 * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1391 * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1392 */
1393#define KUNIT_ASSERT_FALSE(test, condition) \
1394 KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
1395
1396#define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
1397 KUNIT_FALSE_MSG_ASSERTION(test, \
1398 KUNIT_ASSERTION, \
1399 condition, \
1400 fmt, \
1401 ##__VA_ARGS__)
1402
1403/**
1404 * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1405 * @test: The test context object.
1406 * @left: an arbitrary expression that evaluates to a primitive C type.
1407 * @right: an arbitrary expression that evaluates to a primitive C type.
1408 *
1409 * Sets an assertion that the values that @left and @right evaluate to are
1410 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1411 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1412 */
1413#define KUNIT_ASSERT_EQ(test, left, right) \
1414 KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
1415
1416#define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
1417 KUNIT_BINARY_INT_ASSERTION(test, \
1418 KUNIT_ASSERTION, \
1419 left, ==, right, \
1420 fmt, \
1421 ##__VA_ARGS__)
1422
1423/**
1424 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1425 * @test: The test context object.
1426 * @left: an arbitrary expression that evaluates to a pointer.
1427 * @right: an arbitrary expression that evaluates to a pointer.
1428 *
1429 * Sets an assertion that the values that @left and @right evaluate to are
1430 * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1431 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1432 */
1433#define KUNIT_ASSERT_PTR_EQ(test, left, right) \
1434 KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
1435
1436#define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
1437 KUNIT_BINARY_PTR_ASSERTION(test, \
1438 KUNIT_ASSERTION, \
1439 left, ==, right, \
1440 fmt, \
1441 ##__VA_ARGS__)
1442
1443/**
1444 * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1445 * @test: The test context object.
1446 * @left: an arbitrary expression that evaluates to a primitive C type.
1447 * @right: an arbitrary expression that evaluates to a primitive C type.
1448 *
1449 * Sets an assertion that the values that @left and @right evaluate to are not
1450 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1451 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1452 */
1453#define KUNIT_ASSERT_NE(test, left, right) \
1454 KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
1455
1456#define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
1457 KUNIT_BINARY_INT_ASSERTION(test, \
1458 KUNIT_ASSERTION, \
1459 left, !=, right, \
1460 fmt, \
1461 ##__VA_ARGS__)
1462
1463/**
1464 * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1465 * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1466 * @test: The test context object.
1467 * @left: an arbitrary expression that evaluates to a pointer.
1468 * @right: an arbitrary expression that evaluates to a pointer.
1469 *
1470 * Sets an assertion that the values that @left and @right evaluate to are not
1471 * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1472 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1473 */
1474#define KUNIT_ASSERT_PTR_NE(test, left, right) \
1475 KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
1476
1477#define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
1478 KUNIT_BINARY_PTR_ASSERTION(test, \
1479 KUNIT_ASSERTION, \
1480 left, !=, right, \
1481 fmt, \
1482 ##__VA_ARGS__)
1483/**
1484 * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1485 * @test: The test context object.
1486 * @left: an arbitrary expression that evaluates to a primitive C type.
1487 * @right: an arbitrary expression that evaluates to a primitive C type.
1488 *
1489 * Sets an assertion that the value that @left evaluates to is less than the
1490 * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1491 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1492 * is not met.
1493 */
1494#define KUNIT_ASSERT_LT(test, left, right) \
1495 KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
1496
1497#define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
1498 KUNIT_BINARY_INT_ASSERTION(test, \
1499 KUNIT_ASSERTION, \
1500 left, <, right, \
1501 fmt, \
1502 ##__VA_ARGS__)
1503/**
1504 * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1505 * @test: The test context object.
1506 * @left: an arbitrary expression that evaluates to a primitive C type.
1507 * @right: an arbitrary expression that evaluates to a primitive C type.
1508 *
1509 * Sets an assertion that the value that @left evaluates to is less than or
1510 * equal to the value that @right evaluates to. This is the same as
1511 * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1512 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1513 */
1514#define KUNIT_ASSERT_LE(test, left, right) \
1515 KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
1516
1517#define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
1518 KUNIT_BINARY_INT_ASSERTION(test, \
1519 KUNIT_ASSERTION, \
1520 left, <=, right, \
1521 fmt, \
1522 ##__VA_ARGS__)
1523
1524/**
1525 * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1526 * @test: The test context object.
1527 * @left: an arbitrary expression that evaluates to a primitive C type.
1528 * @right: an arbitrary expression that evaluates to a primitive C type.
1529 *
1530 * Sets an assertion that the value that @left evaluates to is greater than the
1531 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1532 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1533 * is not met.
1534 */
1535#define KUNIT_ASSERT_GT(test, left, right) \
1536 KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
1537
1538#define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
1539 KUNIT_BINARY_INT_ASSERTION(test, \
1540 KUNIT_ASSERTION, \
1541 left, >, right, \
1542 fmt, \
1543 ##__VA_ARGS__)
1544
1545/**
1546 * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1547 * @test: The test context object.
1548 * @left: an arbitrary expression that evaluates to a primitive C type.
1549 * @right: an arbitrary expression that evaluates to a primitive C type.
1550 *
1551 * Sets an assertion that the value that @left evaluates to is greater than the
1552 * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1553 * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1554 * is not met.
1555 */
1556#define KUNIT_ASSERT_GE(test, left, right) \
1557 KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
1558
1559#define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
1560 KUNIT_BINARY_INT_ASSERTION(test, \
1561 KUNIT_ASSERTION, \
1562 left, >=, right, \
1563 fmt, \
1564 ##__VA_ARGS__)
1565
1566/**
1567 * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1568 * @test: The test context object.
1569 * @left: an arbitrary expression that evaluates to a null terminated string.
1570 * @right: an arbitrary expression that evaluates to a null terminated string.
1571 *
1572 * Sets an assertion that the values that @left and @right evaluate to are
1573 * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1574 * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1575 */
1576#define KUNIT_ASSERT_STREQ(test, left, right) \
1577 KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
1578
1579#define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
1580 KUNIT_BINARY_STR_ASSERTION(test, \
1581 KUNIT_ASSERTION, \
1582 left, ==, right, \
1583 fmt, \
1584 ##__VA_ARGS__)
1585
1586/**
1587 * KUNIT_ASSERT_STRNEQ() - An assertion that strings @left and @right are not equal.
1588 * @test: The test context object.
1589 * @left: an arbitrary expression that evaluates to a null terminated string.
1590 * @right: an arbitrary expression that evaluates to a null terminated string.
1591 *
1592 * Sets an assertion that the values that @left and @right evaluate to are
1593 * not equal. This is semantically equivalent to
1594 * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1595 * for more information.
1596 */
1597#define KUNIT_ASSERT_STRNEQ(test, left, right) \
1598 KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
1599
1600#define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
1601 KUNIT_BINARY_STR_ASSERTION(test, \
1602 KUNIT_ASSERTION, \
1603 left, !=, right, \
1604 fmt, \
1605 ##__VA_ARGS__)
1606
1607/**
1608 * KUNIT_ASSERT_MEMEQ() - Asserts that the first @size bytes of @left and @right are equal.
1609 * @test: The test context object.
1610 * @left: An arbitrary expression that evaluates to the specified size.
1611 * @right: An arbitrary expression that evaluates to the specified size.
1612 * @size: Number of bytes compared.
1613 *
1614 * Sets an assertion that the values that @left and @right evaluate to are
1615 * equal. This is semantically equivalent to
1616 * KUNIT_ASSERT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
1617 * KUNIT_ASSERT_TRUE() for more information.
1618 *
1619 * Although this assertion works for any memory block, it is not recommended
1620 * for comparing more structured data, such as structs. This assertion is
1621 * recommended for comparing, for example, data arrays.
1622 */
1623#define KUNIT_ASSERT_MEMEQ(test, left, right, size) \
1624 KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, NULL)
1625
1626#define KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, fmt, ...) \
1627 KUNIT_MEM_ASSERTION(test, \
1628 KUNIT_ASSERTION, \
1629 left, ==, right, \
1630 size, \
1631 fmt, \
1632 ##__VA_ARGS__)
1633
1634/**
1635 * KUNIT_ASSERT_MEMNEQ() - Asserts that the first @size bytes of @left and @right are not equal.
1636 * @test: The test context object.
1637 * @left: An arbitrary expression that evaluates to the specified size.
1638 * @right: An arbitrary expression that evaluates to the specified size.
1639 * @size: Number of bytes compared.
1640 *
1641 * Sets an assertion that the values that @left and @right evaluate to are
1642 * not equal. This is semantically equivalent to
1643 * KUNIT_ASSERT_TRUE(@test, memcmp((@left), (@right), (@size))). See
1644 * KUNIT_ASSERT_TRUE() for more information.
1645 *
1646 * Although this assertion works for any memory block, it is not recommended
1647 * for comparing more structured data, such as structs. This assertion is
1648 * recommended for comparing, for example, data arrays.
1649 */
1650#define KUNIT_ASSERT_MEMNEQ(test, left, right, size) \
1651 KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, NULL)
1652
1653#define KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \
1654 KUNIT_MEM_ASSERTION(test, \
1655 KUNIT_ASSERTION, \
1656 left, !=, right, \
1657 size, \
1658 fmt, \
1659 ##__VA_ARGS__)
1660
1661/**
1662 * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
1663 * @test: The test context object.
1664 * @ptr: an arbitrary pointer.
1665 *
1666 * Sets an assertion that the values that @ptr evaluates to is null. This is
1667 * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
1668 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1669 */
1670#define KUNIT_ASSERT_NULL(test, ptr) \
1671 KUNIT_ASSERT_NULL_MSG(test, \
1672 ptr, \
1673 NULL)
1674
1675#define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
1676 KUNIT_BINARY_PTR_ASSERTION(test, \
1677 KUNIT_ASSERTION, \
1678 ptr, ==, NULL, \
1679 fmt, \
1680 ##__VA_ARGS__)
1681
1682/**
1683 * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
1684 * @test: The test context object.
1685 * @ptr: an arbitrary pointer.
1686 *
1687 * Sets an assertion that the values that @ptr evaluates to is not null. This
1688 * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
1689 * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1690 */
1691#define KUNIT_ASSERT_NOT_NULL(test, ptr) \
1692 KUNIT_ASSERT_NOT_NULL_MSG(test, \
1693 ptr, \
1694 NULL)
1695
1696#define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1697 KUNIT_BINARY_PTR_ASSERTION(test, \
1698 KUNIT_ASSERTION, \
1699 ptr, !=, NULL, \
1700 fmt, \
1701 ##__VA_ARGS__)
1702
1703/**
1704 * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1705 * @test: The test context object.
1706 * @ptr: an arbitrary pointer.
1707 *
1708 * Sets an assertion that the value that @ptr evaluates to is not null and not
1709 * an errno stored in a pointer. This is the same as
1710 * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1711 * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1712 */
1713#define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
1714 KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
1715
1716#define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1717 KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1718 KUNIT_ASSERTION, \
1719 ptr, \
1720 fmt, \
1721 ##__VA_ARGS__)
1722
1723/**
1724 * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1725 * @name: prefix for the test parameter generator function.
1726 * @array: array of test parameters.
1727 * @get_desc: function to convert param to description; NULL to use default
1728 *
1729 * Define function @name_gen_params which uses @array to generate parameters.
1730 */
1731#define KUNIT_ARRAY_PARAM(name, array, get_desc) \
1732 static const void *name##_gen_params(struct kunit *test, \
1733 const void *prev, char *desc) \
1734 { \
1735 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1736 if (!prev) \
1737 kunit_register_params_array(test, array, ARRAY_SIZE(array), NULL); \
1738 if (__next - (array) < ARRAY_SIZE((array))) { \
1739 void (*__get_desc)(typeof(__next), char *) = get_desc; \
1740 if (__get_desc) \
1741 __get_desc(__next, desc); \
1742 return __next; \
1743 } \
1744 return NULL; \
1745 }
1746
1747/**
1748 * KUNIT_ARRAY_PARAM_DESC() - Define test parameter generator from an array.
1749 * @name: prefix for the test parameter generator function.
1750 * @array: array of test parameters.
1751 * @desc_member: structure member from array element to use as description
1752 *
1753 * Define function @name_gen_params which uses @array to generate parameters.
1754 */
1755#define KUNIT_ARRAY_PARAM_DESC(name, array, desc_member) \
1756 static const void *name##_gen_params(struct kunit *test, \
1757 const void *prev, char *desc) \
1758 { \
1759 typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1760 if (!prev) \
1761 kunit_register_params_array(test, array, ARRAY_SIZE(array), NULL); \
1762 if (__next - (array) < ARRAY_SIZE((array))) { \
1763 strscpy(desc, __next->desc_member, KUNIT_PARAM_DESC_SIZE); \
1764 return __next; \
1765 } \
1766 return NULL; \
1767 }
1768
1769/**
1770 * kunit_register_params_array() - Register parameter array for a KUnit test.
1771 * @test: The KUnit test structure to which parameters will be added.
1772 * @array: An array of test parameters.
1773 * @param_count: Number of parameters.
1774 * @get_desc: Function that generates a string description for a given parameter
1775 * element.
1776 *
1777 * This macro initializes the @test's parameter array data, storing information
1778 * including the parameter array, its count, the element size, and the parameter
1779 * description function within `test->params_array`.
1780 *
1781 * Note: If using this macro in param_init(), kunit_array_gen_params()
1782 * will then need to be manually provided as the parameter generator function to
1783 * KUNIT_CASE_PARAM_WITH_INIT(). kunit_array_gen_params() is a KUnit
1784 * function that uses the registered array to generate parameters
1785 */
1786#define kunit_register_params_array(test, array, param_count, get_desc) \
1787 do { \
1788 struct kunit *_test = (test); \
1789 const typeof((array)[0]) * _params_ptr = &(array)[0]; \
1790 _test->params_array.params = _params_ptr; \
1791 _test->params_array.num_params = (param_count); \
1792 _test->params_array.elem_size = sizeof(*_params_ptr); \
1793 _test->params_array.get_description = (get_desc); \
1794 } while (0)
1795
1796// TODO([email protected]): consider eventually migrating users to explicitly
1797// include resource.h themselves if they need it.
1798#include <kunit/resource.h>
1799
1800/*
1801 * Warning backtrace suppression API.
1802 *
1803 * Suppresses WARN*() backtraces on the current task while active. Two forms
1804 * are provided:
1805 *
1806 * - Scoped: kunit_warning_suppress(test) { ... }
1807 * Suppression is active for the duration of the block. On normal exit,
1808 * the for-loop increment deactivates suppression. On early exit (break,
1809 * return, goto), the __cleanup attribute fires. On kthread_exit() (e.g.,
1810 * a failed KUnit assertion), kunit_add_action() cleans up at test
1811 * teardown. The suppression handle is only accessible inside the block,
1812 * so warning counts must be checked before the block exits.
1813 *
1814 * - Direct: kunit_start_suppress_warning() / kunit_end_suppress_warning()
1815 * The underlying functions, returning an explicit handle pointer. Use
1816 * when the handle needs to be retained (e.g., for post-suppression
1817 * count checks) or passed across helper functions.
1818 */
1819struct kunit_suppressed_warning;
1820
1821struct kunit_suppressed_warning *
1822kunit_start_suppress_warning(struct kunit *test);
1823void kunit_end_suppress_warning(struct kunit *test,
1824 struct kunit_suppressed_warning *w);
1825int kunit_suppressed_warning_count(struct kunit_suppressed_warning *w);
1826void __kunit_suppress_auto_cleanup(struct kunit_suppressed_warning **wp);
1827bool kunit_has_active_suppress_warning(void);
1828
1829/**
1830 * kunit_warning_suppress() - Suppress WARN*() backtraces for the duration
1831 * of a block.
1832 * @test: The test context object.
1833 *
1834 * Scoped form of the suppression API. Suppression starts when the block is
1835 * entered and ends automatically when the block exits through any path. See
1836 * the section comment above for the cleanup guarantees on each exit path.
1837 * Fails the test if suppression is already active; nesting is not supported.
1838 *
1839 * The warning count can be checked inside the block via
1840 * KUNIT_EXPECT_SUPPRESSED_WARNING_COUNT(). The handle is not accessible
1841 * after the block exits.
1842 *
1843 * Example::
1844 *
1845 * kunit_warning_suppress(test) {
1846 * trigger_warning();
1847 * KUNIT_EXPECT_SUPPRESSED_WARNING_COUNT(test, 1);
1848 * }
1849 */
1850#define kunit_warning_suppress(test) \
1851 for (struct kunit_suppressed_warning *__kunit_suppress \
1852 __cleanup(__kunit_suppress_auto_cleanup) = \
1853 kunit_start_suppress_warning(test); \
1854 __kunit_suppress; \
1855 kunit_end_suppress_warning(test, __kunit_suppress), \
1856 __kunit_suppress = NULL)
1857
1858/**
1859 * KUNIT_SUPPRESSED_WARNING_COUNT() - Returns the suppressed warning count.
1860 *
1861 * Returns the number of WARN*() calls suppressed since the current
1862 * suppression block started, or 0 if the handle is NULL. Usable inside a
1863 * kunit_warning_suppress() block.
1864 */
1865#define KUNIT_SUPPRESSED_WARNING_COUNT() \
1866 kunit_suppressed_warning_count(__kunit_suppress)
1867
1868/**
1869 * KUNIT_EXPECT_SUPPRESSED_WARNING_COUNT() - Sets an expectation that the
1870 * suppressed warning count equals
1871 * @expected.
1872 * @test: The test context object.
1873 * @expected: an expression that evaluates to the expected warning count.
1874 *
1875 * Sets an expectation that the number of suppressed WARN*() calls equals
1876 * @expected. This is semantically equivalent to
1877 * KUNIT_EXPECT_EQ(@test, KUNIT_SUPPRESSED_WARNING_COUNT(), @expected).
1878 * See KUNIT_EXPECT_EQ() for more information.
1879 */
1880#define KUNIT_EXPECT_SUPPRESSED_WARNING_COUNT(test, expected) \
1881 KUNIT_EXPECT_EQ(test, KUNIT_SUPPRESSED_WARNING_COUNT(), expected)
1882
1883/**
1884 * KUNIT_ASSERT_SUPPRESSED_WARNING_COUNT() - Sets an assertion that the
1885 * suppressed warning count equals
1886 * @expected.
1887 * @test: The test context object.
1888 * @expected: an expression that evaluates to the expected warning count.
1889 *
1890 * Sets an assertion that the number of suppressed WARN*() calls equals
1891 * @expected. This is the same as KUNIT_EXPECT_SUPPRESSED_WARNING_COUNT(),
1892 * except it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the
1893 * assertion is not met.
1894 */
1895#define KUNIT_ASSERT_SUPPRESSED_WARNING_COUNT(test, expected) \
1896 KUNIT_ASSERT_EQ(test, KUNIT_SUPPRESSED_WARNING_COUNT(), expected)
1897
1898#endif /* _KUNIT_TEST_H */