/* SPDX-License-Identifier: GPL-2.0 */1/*2* Base unit test (KUnit) API.3*4* Copyright (C) 2019, Google LLC.5* Author: Brendan Higgins <[email protected]>6*/78#ifndef _KUNIT_TEST_H9#define _KUNIT_TEST_H1011#include <kunit/assert.h>12#include <kunit/try-catch.h>1314#include <linux/args.h>15#include <linux/compiler.h>16#include <linux/container_of.h>17#include <linux/err.h>18#include <linux/init.h>19#include <linux/jump_label.h>20#include <linux/kconfig.h>21#include <linux/kref.h>22#include <linux/list.h>23#include <linux/module.h>24#include <linux/slab.h>25#include <linux/spinlock.h>26#include <linux/string.h>27#include <linux/types.h>2829#include <asm/rwonce.h>30#include <asm/sections.h>3132/* Static key: true if any KUnit tests are currently running */33DECLARE_STATIC_KEY_FALSE(kunit_running);3435struct kunit;36struct string_stream;3738/* Maximum size of parameter description string. */39#define KUNIT_PARAM_DESC_SIZE 1284041/* Maximum size of a status comment. */42#define KUNIT_STATUS_COMMENT_SIZE 2564344/*45* TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a46* sub-subtest. See the "Subtests" section in47* https://node-tap.org/tap-protocol/48*/49#define KUNIT_INDENT_LEN 450#define KUNIT_SUBTEST_INDENT " "51#define KUNIT_SUBSUBTEST_INDENT " "5253/**54* enum kunit_status - Type of result for a test or test suite55* @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped56* @KUNIT_FAILURE: Denotes the test has failed.57* @KUNIT_SKIPPED: Denotes the test has been skipped.58*/59enum kunit_status {60KUNIT_SUCCESS,61KUNIT_FAILURE,62KUNIT_SKIPPED,63};6465/* Attribute struct/enum definitions */6667/*68* Speed Attribute is stored as an enum and separated into categories of69* speed: very_slow, slow, and normal. These speeds are relative to70* other KUnit tests.71*72* Note: unset speed attribute acts as default of KUNIT_SPEED_NORMAL.73*/74enum kunit_speed {75KUNIT_SPEED_UNSET,76KUNIT_SPEED_VERY_SLOW,77KUNIT_SPEED_SLOW,78KUNIT_SPEED_NORMAL,79KUNIT_SPEED_MAX = KUNIT_SPEED_NORMAL,80};8182/* Holds attributes for each test case and suite */83struct kunit_attributes {84enum kunit_speed speed;85};8687/**88* struct kunit_case - represents an individual test case.89*90* @run_case: the function representing the actual test case.91* @name: the name of the test case.92* @generate_params: the generator function for parameterized tests.93* @attr: the attributes associated with the test94* @param_init: The init function to run before a parameterized test.95* @param_exit: The exit function to run after a parameterized test.96*97* A test case is a function with the signature,98* ``void (*)(struct kunit *)``99* that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and100* KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated101* with a &struct kunit_suite and will be run after the suite's init102* function and followed by the suite's exit function.103*104* A test case should be static and should only be created with the105* KUNIT_CASE() macro; additionally, every array of test cases should be106* terminated with an empty test case.107*108* Example:109*110* .. code-block:: c111*112* void add_test_basic(struct kunit *test)113* {114* KUNIT_EXPECT_EQ(test, 1, add(1, 0));115* KUNIT_EXPECT_EQ(test, 2, add(1, 1));116* KUNIT_EXPECT_EQ(test, 0, add(-1, 1));117* KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));118* KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));119* }120*121* static struct kunit_case example_test_cases[] = {122* KUNIT_CASE(add_test_basic),123* {}124* };125*126*/127struct kunit_case {128void (*run_case)(struct kunit *test);129const char *name;130const void* (*generate_params)(struct kunit *test,131const void *prev, char *desc);132struct kunit_attributes attr;133int (*param_init)(struct kunit *test);134void (*param_exit)(struct kunit *test);135136/* private: internal use only. */137enum kunit_status status;138char *module_name;139struct string_stream *log;140};141142static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)143{144switch (status) {145case KUNIT_SKIPPED:146case KUNIT_SUCCESS:147return "ok";148case KUNIT_FAILURE:149return "not ok";150}151return "invalid";152}153154/**155* KUNIT_CASE - A helper for creating a &struct kunit_case156*157* @test_name: a reference to a test case function.158*159* Takes a symbol for a function representing a test case and creates a160* &struct kunit_case object from it. See the documentation for161* &struct kunit_case for an example on how to use it.162*/163#define KUNIT_CASE(test_name) \164{ .run_case = test_name, .name = #test_name, \165.module_name = KBUILD_MODNAME}166167/**168* KUNIT_CASE_ATTR - A helper for creating a &struct kunit_case169* with attributes170*171* @test_name: a reference to a test case function.172* @attributes: a reference to a struct kunit_attributes object containing173* test attributes174*/175#define KUNIT_CASE_ATTR(test_name, attributes) \176{ .run_case = test_name, .name = #test_name, \177.attr = attributes, .module_name = KBUILD_MODNAME}178179/**180* KUNIT_CASE_SLOW - A helper for creating a &struct kunit_case181* with the slow attribute182*183* @test_name: a reference to a test case function.184*/185186#define KUNIT_CASE_SLOW(test_name) \187{ .run_case = test_name, .name = #test_name, \188.attr.speed = KUNIT_SPEED_SLOW, .module_name = KBUILD_MODNAME}189190/**191* KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case192*193* @test_name: a reference to a test case function.194* @gen_params: a reference to a parameter generator function.195*196* The generator function::197*198* const void* gen_params(const void *prev, char *desc)199*200* is used to lazily generate a series of arbitrarily typed values that fit into201* a void*. The argument @prev is the previously returned value, which should be202* used to derive the next value; @prev is set to NULL on the initial generator203* call. When no more values are available, the generator must return NULL.204* Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)205* describing the parameter.206*/207#define KUNIT_CASE_PARAM(test_name, gen_params) \208{ .run_case = test_name, .name = #test_name, \209.generate_params = gen_params, .module_name = KBUILD_MODNAME}210211/**212* KUNIT_CASE_PARAM_ATTR - A helper for creating a parameterized &struct213* kunit_case with attributes214*215* @test_name: a reference to a test case function.216* @gen_params: a reference to a parameter generator function.217* @attributes: a reference to a struct kunit_attributes object containing218* test attributes219*/220#define KUNIT_CASE_PARAM_ATTR(test_name, gen_params, attributes) \221{ .run_case = test_name, .name = #test_name, \222.generate_params = gen_params, \223.attr = attributes, .module_name = KBUILD_MODNAME}224225/**226* KUNIT_CASE_PARAM_WITH_INIT - Define a parameterized KUnit test case with custom227* param_init() and param_exit() functions.228* @test_name: The function implementing the test case.229* @gen_params: The function to generate parameters for the test case.230* @init: A reference to the param_init() function to run before a parameterized test.231* @exit: A reference to the param_exit() function to run after a parameterized test.232*233* Provides the option to register param_init() and param_exit() functions.234* param_init/exit will be passed the parameterized test context and run once235* before and once after the parameterized test. The init function can be used236* to add resources to share between parameter runs, pass parameter arrays,237* and any other setup logic. The exit function can be used to clean up resources238* that were not managed by the parameterized test, and any other teardown logic.239*240* Note: If you are registering a parameter array in param_init() with241* kunit_register_param_array() then you need to pass kunit_array_gen_params()242* to this as the generator function.243*/244#define KUNIT_CASE_PARAM_WITH_INIT(test_name, gen_params, init, exit) \245{ .run_case = test_name, .name = #test_name, \246.generate_params = gen_params, \247.param_init = init, .param_exit = exit, \248.module_name = KBUILD_MODNAME}249250/**251* struct kunit_suite - describes a related collection of &struct kunit_case252*253* @name: the name of the test. Purely informational.254* @suite_init: called once per test suite before the test cases.255* @suite_exit: called once per test suite after all test cases.256* @init: called before every test case.257* @exit: called after every test case.258* @test_cases: a null terminated array of test cases.259* @attr: the attributes associated with the test suite260*261* A kunit_suite is a collection of related &struct kunit_case s, such that262* @init is called before every test case and @exit is called after every263* test case, similar to the notion of a *test fixture* or a *test class*264* in other unit testing frameworks like JUnit or Googletest.265*266* Note that @exit and @suite_exit will run even if @init or @suite_init267* fail: make sure they can handle any inconsistent state which may result.268*269* Every &struct kunit_case must be associated with a kunit_suite for KUnit270* to run it.271*/272struct kunit_suite {273const char name[256];274int (*suite_init)(struct kunit_suite *suite);275void (*suite_exit)(struct kunit_suite *suite);276int (*init)(struct kunit *test);277void (*exit)(struct kunit *test);278struct kunit_case *test_cases;279struct kunit_attributes attr;280281/* private: internal use only */282char status_comment[KUNIT_STATUS_COMMENT_SIZE];283struct dentry *debugfs;284struct string_stream *log;285int suite_init_err;286bool is_init;287};288289/* Stores an array of suites, end points one past the end */290struct kunit_suite_set {291struct kunit_suite * const *start;292struct kunit_suite * const *end;293};294295/* Stores the pointer to the parameter array and its metadata. */296struct kunit_params {297/*298* Reference to the parameter array for a parameterized test. This299* is NULL if a parameter array wasn't directly passed to the300* parameterized test context struct kunit via kunit_register_params_array().301*/302const void *params;303/* Reference to a function that gets the description of a parameter. */304void (*get_description)(struct kunit *test, const void *param, char *desc);305size_t num_params;306size_t elem_size;307};308309/**310* struct kunit - represents a running instance of a test.311*312* @priv: for user to store arbitrary data. Commonly used to pass data313* created in the init function (see &struct kunit_suite).314* @parent: reference to the parent context of type struct kunit that can315* be used for storing shared resources.316* @params_array: for storing the parameter array.317*318* Used to store information about the current context under which the test319* is running. Most of this data is private and should only be accessed320* indirectly via public functions; the exceptions are @priv, @parent and321* @params_array which can be used by the test writer to store arbitrary data,322* access the parent context, and to store the parameter array, respectively.323*/324struct kunit {325void *priv;326struct kunit *parent;327struct kunit_params params_array;328329/* private: internal use only. */330const char *name; /* Read only after initialization! */331struct string_stream *log; /* Points at case log after initialization */332struct kunit_try_catch try_catch;333/* param_value is the current parameter value for a test case. */334const void *param_value;335/* param_index stores the index of the parameter in parameterized tests. */336int param_index;337/*338* success starts as true, and may only be set to false during a339* test case; thus, it is safe to update this across multiple340* threads using WRITE_ONCE; however, as a consequence, it may only341* be read after the test case finishes once all threads associated342* with the test case have terminated.343*/344spinlock_t lock; /* Guards all mutable test state. */345enum kunit_status status; /* Read only after test_case finishes! */346/*347* Because resources is a list that may be updated multiple times (with348* new resources) from any thread associated with a test case, we must349* protect it with some type of lock.350*/351struct list_head resources; /* Protected by lock. */352353char status_comment[KUNIT_STATUS_COMMENT_SIZE];354/* Saves the last seen test. Useful to help with faults. */355struct kunit_loc last_seen;356};357358static inline void kunit_set_failure(struct kunit *test)359{360WRITE_ONCE(test->status, KUNIT_FAILURE);361}362363bool kunit_enabled(void);364bool kunit_autorun(void);365const char *kunit_action(void);366const char *kunit_filter_glob(void);367char *kunit_filter(void);368char *kunit_filter_action(void);369370void kunit_init_test(struct kunit *test, const char *name, struct string_stream *log);371372int kunit_run_tests(struct kunit_suite *suite);373374size_t kunit_suite_num_test_cases(struct kunit_suite *suite);375376unsigned int kunit_test_case_num(struct kunit_suite *suite,377struct kunit_case *test_case);378379struct kunit_suite_set380kunit_filter_suites(const struct kunit_suite_set *suite_set,381const char *filter_glob,382char *filters,383char *filter_action,384int *err);385void kunit_free_suite_set(struct kunit_suite_set suite_set);386387int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites,388bool run_tests);389390void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);391392void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin);393void kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr);394395struct kunit_suite_set kunit_merge_suite_sets(struct kunit_suite_set init_suite_set,396struct kunit_suite_set suite_set);397398const void *kunit_array_gen_params(struct kunit *test, const void *prev, char *desc);399400#if IS_BUILTIN(CONFIG_KUNIT)401int kunit_run_all_tests(void);402#else403static inline int kunit_run_all_tests(void)404{405return 0;406}407#endif /* IS_BUILTIN(CONFIG_KUNIT) */408409#define __kunit_test_suites(unique_array, ...) \410static struct kunit_suite *unique_array[] \411__aligned(sizeof(struct kunit_suite *)) \412__used __section(".kunit_test_suites") = { __VA_ARGS__ }413414/**415* kunit_test_suites() - used to register one or more &struct kunit_suite416* with KUnit.417*418* @__suites: a statically allocated list of &struct kunit_suite.419*420* Registers @suites with the test framework.421* This is done by placing the array of struct kunit_suite * in the422* .kunit_test_suites ELF section.423*424* When builtin, KUnit tests are all run via the executor at boot, and when425* built as a module, they run on module load.426*427*/428#define kunit_test_suites(__suites...) \429__kunit_test_suites(__UNIQUE_ID(array), \430##__suites)431432#define kunit_test_suite(suite) kunit_test_suites(&suite)433434#define __kunit_init_test_suites(unique_array, ...) \435static struct kunit_suite *unique_array[] \436__aligned(sizeof(struct kunit_suite *)) \437__used __section(".kunit_init_test_suites") = { __VA_ARGS__ }438439/**440* kunit_test_init_section_suites() - used to register one or more &struct441* kunit_suite containing init functions or442* init data.443*444* @__suites: a statically allocated list of &struct kunit_suite.445*446* This functions similar to kunit_test_suites() except that it compiles the447* list of suites during init phase.448*449* This macro also suffixes the array and suite declarations it makes with450* _probe; so that modpost suppresses warnings about referencing init data451* for symbols named in this manner.452*453* Note: these init tests are not able to be run after boot so there is no454* "run" debugfs file generated for these tests.455*456* Also, do not mark the suite or test case structs with __initdata because457* they will be used after the init phase with debugfs.458*/459#define kunit_test_init_section_suites(__suites...) \460__kunit_init_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \461##__suites)462463#define kunit_test_init_section_suite(suite) \464kunit_test_init_section_suites(&suite)465466#define kunit_suite_for_each_test_case(suite, test_case) \467for (test_case = suite->test_cases; test_case->run_case; test_case++)468469enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);470471/**472* kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.473* @test: The test context object.474* @n: number of elements.475* @size: The size in bytes of the desired memory.476* @gfp: flags passed to underlying kmalloc().477*478* Just like `kmalloc_array(...)`, except the allocation is managed by the test case479* and is automatically cleaned up after the test case concludes. See kunit_add_action()480* for more information.481*482* Note that some internal context data is also allocated with GFP_KERNEL,483* regardless of the gfp passed in.484*/485void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);486487/**488* kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.489* @test: The test context object.490* @size: The size in bytes of the desired memory.491* @gfp: flags passed to underlying kmalloc().492*493* See kmalloc() and kunit_kmalloc_array() for more information.494*495* Note that some internal context data is also allocated with GFP_KERNEL,496* regardless of the gfp passed in.497*/498static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)499{500return kunit_kmalloc_array(test, 1, size, gfp);501}502503/**504* kunit_kfree() - Like kfree except for allocations managed by KUnit.505* @test: The test case to which the resource belongs.506* @ptr: The memory allocation to free.507*/508void kunit_kfree(struct kunit *test, const void *ptr);509510/**511* kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.512* @test: The test context object.513* @size: The size in bytes of the desired memory.514* @gfp: flags passed to underlying kmalloc().515*516* See kzalloc() and kunit_kmalloc_array() for more information.517*/518static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)519{520return kunit_kmalloc(test, size, gfp | __GFP_ZERO);521}522523/**524* kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.525* @test: The test context object.526* @n: number of elements.527* @size: The size in bytes of the desired memory.528* @gfp: flags passed to underlying kmalloc().529*530* See kcalloc() and kunit_kmalloc_array() for more information.531*/532static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)533{534return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);535}536537538/**539* kunit_kfree_const() - conditionally free test managed memory540* @test: The test context object.541* @x: pointer to the memory542*543* Calls kunit_kfree() only if @x is not in .rodata section.544* See kunit_kstrdup_const() for more information.545*/546void kunit_kfree_const(struct kunit *test, const void *x);547548/**549* kunit_kstrdup() - Duplicates a string into a test managed allocation.550*551* @test: The test context object.552* @str: The NULL-terminated string to duplicate.553* @gfp: flags passed to underlying kmalloc().554*555* See kstrdup() and kunit_kmalloc_array() for more information.556*/557static inline char *kunit_kstrdup(struct kunit *test, const char *str, gfp_t gfp)558{559size_t len;560char *buf;561562if (!str)563return NULL;564565len = strlen(str) + 1;566buf = kunit_kmalloc(test, len, gfp);567if (buf)568memcpy(buf, str, len);569return buf;570}571572/**573* kunit_kstrdup_const() - Conditionally duplicates a string into a test managed allocation.574*575* @test: The test context object.576* @str: The NULL-terminated string to duplicate.577* @gfp: flags passed to underlying kmalloc().578*579* Calls kunit_kstrdup() only if @str is not in the rodata section. Must be freed with580* kunit_kfree_const() -- not kunit_kfree().581* See kstrdup_const() and kunit_kmalloc_array() for more information.582*/583const char *kunit_kstrdup_const(struct kunit *test, const char *str, gfp_t gfp);584585/**586* kunit_attach_mm() - Create and attach a new mm if it doesn't already exist.587*588* Allocates a &struct mm_struct and attaches it to @current. In most cases, call589* kunit_vm_mmap() without calling kunit_attach_mm() directly. Only necessary when590* code under test accesses the mm before executing the mmap (e.g., to perform591* additional initialization beforehand).592*593* Return: 0 on success, -errno on failure.594*/595int kunit_attach_mm(void);596597/**598* kunit_vm_mmap() - Allocate KUnit-tracked vm_mmap() area599* @test: The test context object.600* @file: struct file pointer to map from, if any601* @addr: desired address, if any602* @len: how many bytes to allocate603* @prot: mmap PROT_* bits604* @flag: mmap flags605* @offset: offset into @file to start mapping from.606*607* See vm_mmap() for more information.608*/609unsigned long kunit_vm_mmap(struct kunit *test, struct file *file,610unsigned long addr, unsigned long len,611unsigned long prot, unsigned long flag,612unsigned long offset);613614void kunit_cleanup(struct kunit *test);615616void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt, ...);617618/**619* kunit_mark_skipped() - Marks @test as skipped620*621* @test: The test context object.622* @fmt: A printk() style format string.623*624* Marks the test as skipped. @fmt is given output as the test status625* comment, typically the reason the test was skipped.626*627* Test execution continues after kunit_mark_skipped() is called.628*/629#define kunit_mark_skipped(test, fmt, ...) \630do { \631WRITE_ONCE((test)->status, KUNIT_SKIPPED); \632scnprintf((test)->status_comment, \633KUNIT_STATUS_COMMENT_SIZE, \634fmt, ##__VA_ARGS__); \635} while (0)636637/**638* kunit_skip() - Marks @test as skipped639*640* @test: The test context object.641* @fmt: A printk() style format string.642*643* Skips the test. @fmt is given output as the test status644* comment, typically the reason the test was skipped.645*646* Test execution is halted after kunit_skip() is called.647*/648#define kunit_skip(test, fmt, ...) \649do { \650kunit_mark_skipped((test), fmt, ##__VA_ARGS__); \651kunit_try_catch_throw(&((test)->try_catch)); \652} while (0)653654/*655* printk and log to per-test or per-suite log buffer. Logging only done656* if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.657*/658#define kunit_log(lvl, test_or_suite, fmt, ...) \659do { \660printk(lvl fmt, ##__VA_ARGS__); \661kunit_log_append((test_or_suite)->log, fmt, \662##__VA_ARGS__); \663} while (0)664665#define kunit_printk(lvl, test, fmt, ...) \666kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \667(test)->name, ##__VA_ARGS__)668669/**670* kunit_info() - Prints an INFO level message associated with @test.671*672* @test: The test context object.673* @fmt: A printk() style format string.674*675* Prints an info level message associated with the test suite being run.676* Takes a variable number of format parameters just like printk().677*/678#define kunit_info(test, fmt, ...) \679kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)680681/**682* kunit_warn() - Prints a WARN level message associated with @test.683*684* @test: The test context object.685* @fmt: A printk() style format string.686*687* Prints a warning level message.688*/689#define kunit_warn(test, fmt, ...) \690kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)691692/**693* kunit_err() - Prints an ERROR level message associated with @test.694*695* @test: The test context object.696* @fmt: A printk() style format string.697*698* Prints an error level message.699*/700#define kunit_err(test, fmt, ...) \701kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)702703/*704* Must be called at the beginning of each KUNIT_*_ASSERTION().705* Cf. KUNIT_CURRENT_LOC.706*/707#define _KUNIT_SAVE_LOC(test) do { \708WRITE_ONCE(test->last_seen.file, __FILE__); \709WRITE_ONCE(test->last_seen.line, __LINE__); \710} while (0)711712/**713* KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.714* @test: The test context object.715*716* The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other717* words, it does nothing and only exists for code clarity. See718* KUNIT_EXPECT_TRUE() for more information.719*/720#define KUNIT_SUCCEED(test) _KUNIT_SAVE_LOC(test)721722void __noreturn __kunit_abort(struct kunit *test);723724void __printf(6, 7) __kunit_do_failed_assertion(struct kunit *test,725const struct kunit_loc *loc,726enum kunit_assert_type type,727const struct kunit_assert *assert,728assert_format_t assert_format,729const char *fmt, ...);730731#define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \732static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \733const struct assert_class __assertion = INITIALIZER; \734__kunit_do_failed_assertion(test, \735&__loc, \736assert_type, \737&__assertion.assert, \738assert_format, \739fmt, \740##__VA_ARGS__); \741if (assert_type == KUNIT_ASSERTION) \742__kunit_abort(test); \743} while (0)744745746#define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) do { \747_KUNIT_SAVE_LOC(test); \748_KUNIT_FAILED(test, \749assert_type, \750kunit_fail_assert, \751kunit_fail_assert_format, \752{}, \753fmt, \754##__VA_ARGS__); \755} while (0)756757/**758* KUNIT_FAIL() - Always causes a test to fail when evaluated.759* @test: The test context object.760* @fmt: an informational message to be printed when the assertion is made.761* @...: string format arguments.762*763* The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In764* other words, it always results in a failed expectation, and consequently765* always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()766* for more information.767*/768#define KUNIT_FAIL(test, fmt, ...) \769KUNIT_FAIL_ASSERTION(test, \770KUNIT_EXPECTATION, \771fmt, \772##__VA_ARGS__)773774/* Helper to safely pass around an initializer list to other macros. */775#define KUNIT_INIT_ASSERT(initializers...) { initializers }776777#define KUNIT_UNARY_ASSERTION(test, \778assert_type, \779condition_, \780expected_true_, \781fmt, \782...) \783do { \784_KUNIT_SAVE_LOC(test); \785if (likely(!!(condition_) == !!expected_true_)) \786break; \787\788_KUNIT_FAILED(test, \789assert_type, \790kunit_unary_assert, \791kunit_unary_assert_format, \792KUNIT_INIT_ASSERT(.condition = #condition_, \793.expected_true = expected_true_), \794fmt, \795##__VA_ARGS__); \796} while (0)797798#define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \799KUNIT_UNARY_ASSERTION(test, \800assert_type, \801condition, \802true, \803fmt, \804##__VA_ARGS__)805806#define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \807KUNIT_UNARY_ASSERTION(test, \808assert_type, \809condition, \810false, \811fmt, \812##__VA_ARGS__)813814/*815* A factory macro for defining the assertions and expectations for the basic816* comparisons defined for the built in types.817*818* Unfortunately, there is no common type that all types can be promoted to for819* which all the binary operators behave the same way as for the actual types820* (for example, there is no type that long long and unsigned long long can821* both be cast to where the comparison result is preserved for all values). So822* the best we can do is do the comparison in the original types and then coerce823* everything to long long for printing; this way, the comparison behaves824* correctly and the printed out value usually makes sense without825* interpretation, but can always be interpreted to figure out the actual826* value.827*/828#define KUNIT_BASE_BINARY_ASSERTION(test, \829assert_class, \830format_func, \831assert_type, \832left, \833op, \834right, \835fmt, \836...) \837do { \838const typeof(left) __left = (left); \839const typeof(right) __right = (right); \840static const struct kunit_binary_assert_text __text = { \841.operation = #op, \842.left_text = #left, \843.right_text = #right, \844}; \845\846_KUNIT_SAVE_LOC(test); \847if (likely(__left op __right)) \848break; \849\850_KUNIT_FAILED(test, \851assert_type, \852assert_class, \853format_func, \854KUNIT_INIT_ASSERT(.text = &__text, \855.left_value = __left, \856.right_value = __right), \857fmt, \858##__VA_ARGS__); \859} while (0)860861#define KUNIT_BINARY_INT_ASSERTION(test, \862assert_type, \863left, \864op, \865right, \866fmt, \867...) \868KUNIT_BASE_BINARY_ASSERTION(test, \869kunit_binary_assert, \870kunit_binary_assert_format, \871assert_type, \872left, op, right, \873fmt, \874##__VA_ARGS__)875876#define KUNIT_BINARY_PTR_ASSERTION(test, \877assert_type, \878left, \879op, \880right, \881fmt, \882...) \883KUNIT_BASE_BINARY_ASSERTION(test, \884kunit_binary_ptr_assert, \885kunit_binary_ptr_assert_format, \886assert_type, \887left, op, right, \888fmt, \889##__VA_ARGS__)890891#define KUNIT_BINARY_STR_ASSERTION(test, \892assert_type, \893left, \894op, \895right, \896fmt, \897...) \898do { \899const char *__left = (left); \900const char *__right = (right); \901static const struct kunit_binary_assert_text __text = { \902.operation = #op, \903.left_text = #left, \904.right_text = #right, \905}; \906\907_KUNIT_SAVE_LOC(test); \908if (likely((__left) && (__right) && (strcmp(__left, __right) op 0))) \909break; \910\911\912_KUNIT_FAILED(test, \913assert_type, \914kunit_binary_str_assert, \915kunit_binary_str_assert_format, \916KUNIT_INIT_ASSERT(.text = &__text, \917.left_value = __left, \918.right_value = __right), \919fmt, \920##__VA_ARGS__); \921} while (0)922923#define KUNIT_MEM_ASSERTION(test, \924assert_type, \925left, \926op, \927right, \928size_, \929fmt, \930...) \931do { \932const void *__left = (left); \933const void *__right = (right); \934const size_t __size = (size_); \935static const struct kunit_binary_assert_text __text = { \936.operation = #op, \937.left_text = #left, \938.right_text = #right, \939}; \940\941_KUNIT_SAVE_LOC(test); \942if (likely(__left && __right)) \943if (likely(memcmp(__left, __right, __size) op 0)) \944break; \945\946_KUNIT_FAILED(test, \947assert_type, \948kunit_mem_assert, \949kunit_mem_assert_format, \950KUNIT_INIT_ASSERT(.text = &__text, \951.left_value = __left, \952.right_value = __right, \953.size = __size), \954fmt, \955##__VA_ARGS__); \956} while (0)957958#define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \959assert_type, \960ptr, \961fmt, \962...) \963do { \964const typeof(ptr) __ptr = (ptr); \965\966_KUNIT_SAVE_LOC(test); \967if (!IS_ERR_OR_NULL(__ptr)) \968break; \969\970_KUNIT_FAILED(test, \971assert_type, \972kunit_ptr_not_err_assert, \973kunit_ptr_not_err_assert_format, \974KUNIT_INIT_ASSERT(.text = #ptr, .value = __ptr), \975fmt, \976##__VA_ARGS__); \977} while (0)978979/**980* KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.981* @test: The test context object.982* @condition: an arbitrary boolean expression. The test fails when this does983* not evaluate to true.984*985* This and expectations of the form `KUNIT_EXPECT_*` will cause the test case986* to fail when the specified condition is not met; however, it will not prevent987* the test case from continuing to run; this is otherwise known as an988* *expectation failure*.989*/990#define KUNIT_EXPECT_TRUE(test, condition) \991KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)992993#define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \994KUNIT_TRUE_MSG_ASSERTION(test, \995KUNIT_EXPECTATION, \996condition, \997fmt, \998##__VA_ARGS__)9991000/**1001* KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.1002* @test: The test context object.1003* @condition: an arbitrary boolean expression. The test fails when this does1004* not evaluate to false.1005*1006* Sets an expectation that @condition evaluates to false. See1007* KUNIT_EXPECT_TRUE() for more information.1008*/1009#define KUNIT_EXPECT_FALSE(test, condition) \1010KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)10111012#define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \1013KUNIT_FALSE_MSG_ASSERTION(test, \1014KUNIT_EXPECTATION, \1015condition, \1016fmt, \1017##__VA_ARGS__)10181019/**1020* KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.1021* @test: The test context object.1022* @left: an arbitrary expression that evaluates to a primitive C type.1023* @right: an arbitrary expression that evaluates to a primitive C type.1024*1025* Sets an expectation that the values that @left and @right evaluate to are1026* equal. This is semantically equivalent to1027* KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for1028* more information.1029*/1030#define KUNIT_EXPECT_EQ(test, left, right) \1031KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)10321033#define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \1034KUNIT_BINARY_INT_ASSERTION(test, \1035KUNIT_EXPECTATION, \1036left, ==, right, \1037fmt, \1038##__VA_ARGS__)10391040/**1041* KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.1042* @test: The test context object.1043* @left: an arbitrary expression that evaluates to a pointer.1044* @right: an arbitrary expression that evaluates to a pointer.1045*1046* Sets an expectation that the values that @left and @right evaluate to are1047* equal. This is semantically equivalent to1048* KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for1049* more information.1050*/1051#define KUNIT_EXPECT_PTR_EQ(test, left, right) \1052KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)10531054#define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \1055KUNIT_BINARY_PTR_ASSERTION(test, \1056KUNIT_EXPECTATION, \1057left, ==, right, \1058fmt, \1059##__VA_ARGS__)10601061/**1062* KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.1063* @test: The test context object.1064* @left: an arbitrary expression that evaluates to a primitive C type.1065* @right: an arbitrary expression that evaluates to a primitive C type.1066*1067* Sets an expectation that the values that @left and @right evaluate to are not1068* equal. This is semantically equivalent to1069* KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for1070* more information.1071*/1072#define KUNIT_EXPECT_NE(test, left, right) \1073KUNIT_EXPECT_NE_MSG(test, left, right, NULL)10741075#define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \1076KUNIT_BINARY_INT_ASSERTION(test, \1077KUNIT_EXPECTATION, \1078left, !=, right, \1079fmt, \1080##__VA_ARGS__)10811082/**1083* KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.1084* @test: The test context object.1085* @left: an arbitrary expression that evaluates to a pointer.1086* @right: an arbitrary expression that evaluates to a pointer.1087*1088* Sets an expectation that the values that @left and @right evaluate to are not1089* equal. This is semantically equivalent to1090* KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for1091* more information.1092*/1093#define KUNIT_EXPECT_PTR_NE(test, left, right) \1094KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)10951096#define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \1097KUNIT_BINARY_PTR_ASSERTION(test, \1098KUNIT_EXPECTATION, \1099left, !=, right, \1100fmt, \1101##__VA_ARGS__)11021103/**1104* KUNIT_EXPECT_LT() - An expectation that @left is less than @right.1105* @test: The test context object.1106* @left: an arbitrary expression that evaluates to a primitive C type.1107* @right: an arbitrary expression that evaluates to a primitive C type.1108*1109* Sets an expectation that the value that @left evaluates to is less than the1110* value that @right evaluates to. This is semantically equivalent to1111* KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for1112* more information.1113*/1114#define KUNIT_EXPECT_LT(test, left, right) \1115KUNIT_EXPECT_LT_MSG(test, left, right, NULL)11161117#define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \1118KUNIT_BINARY_INT_ASSERTION(test, \1119KUNIT_EXPECTATION, \1120left, <, right, \1121fmt, \1122##__VA_ARGS__)11231124/**1125* KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.1126* @test: The test context object.1127* @left: an arbitrary expression that evaluates to a primitive C type.1128* @right: an arbitrary expression that evaluates to a primitive C type.1129*1130* Sets an expectation that the value that @left evaluates to is less than or1131* equal to the value that @right evaluates to. Semantically this is equivalent1132* to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for1133* more information.1134*/1135#define KUNIT_EXPECT_LE(test, left, right) \1136KUNIT_EXPECT_LE_MSG(test, left, right, NULL)11371138#define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \1139KUNIT_BINARY_INT_ASSERTION(test, \1140KUNIT_EXPECTATION, \1141left, <=, right, \1142fmt, \1143##__VA_ARGS__)11441145/**1146* KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.1147* @test: The test context object.1148* @left: an arbitrary expression that evaluates to a primitive C type.1149* @right: an arbitrary expression that evaluates to a primitive C type.1150*1151* Sets an expectation that the value that @left evaluates to is greater than1152* the value that @right evaluates to. This is semantically equivalent to1153* KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for1154* more information.1155*/1156#define KUNIT_EXPECT_GT(test, left, right) \1157KUNIT_EXPECT_GT_MSG(test, left, right, NULL)11581159#define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \1160KUNIT_BINARY_INT_ASSERTION(test, \1161KUNIT_EXPECTATION, \1162left, >, right, \1163fmt, \1164##__VA_ARGS__)11651166/**1167* KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.1168* @test: The test context object.1169* @left: an arbitrary expression that evaluates to a primitive C type.1170* @right: an arbitrary expression that evaluates to a primitive C type.1171*1172* Sets an expectation that the value that @left evaluates to is greater than1173* the value that @right evaluates to. This is semantically equivalent to1174* KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for1175* more information.1176*/1177#define KUNIT_EXPECT_GE(test, left, right) \1178KUNIT_EXPECT_GE_MSG(test, left, right, NULL)11791180#define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \1181KUNIT_BINARY_INT_ASSERTION(test, \1182KUNIT_EXPECTATION, \1183left, >=, right, \1184fmt, \1185##__VA_ARGS__)11861187/**1188* KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.1189* @test: The test context object.1190* @left: an arbitrary expression that evaluates to a null terminated string.1191* @right: an arbitrary expression that evaluates to a null terminated string.1192*1193* Sets an expectation that the values that @left and @right evaluate to are1194* equal. This is semantically equivalent to1195* KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()1196* for more information.1197*/1198#define KUNIT_EXPECT_STREQ(test, left, right) \1199KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)12001201#define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \1202KUNIT_BINARY_STR_ASSERTION(test, \1203KUNIT_EXPECTATION, \1204left, ==, right, \1205fmt, \1206##__VA_ARGS__)12071208/**1209* KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.1210* @test: The test context object.1211* @left: an arbitrary expression that evaluates to a null terminated string.1212* @right: an arbitrary expression that evaluates to a null terminated string.1213*1214* Sets an expectation that the values that @left and @right evaluate to are1215* not equal. This is semantically equivalent to1216* KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()1217* for more information.1218*/1219#define KUNIT_EXPECT_STRNEQ(test, left, right) \1220KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)12211222#define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \1223KUNIT_BINARY_STR_ASSERTION(test, \1224KUNIT_EXPECTATION, \1225left, !=, right, \1226fmt, \1227##__VA_ARGS__)12281229/**1230* KUNIT_EXPECT_MEMEQ() - Expects that the first @size bytes of @left and @right are equal.1231* @test: The test context object.1232* @left: An arbitrary expression that evaluates to the specified size.1233* @right: An arbitrary expression that evaluates to the specified size.1234* @size: Number of bytes compared.1235*1236* Sets an expectation that the values that @left and @right evaluate to are1237* equal. This is semantically equivalent to1238* KUNIT_EXPECT_TRUE(@test, !memcmp((@left), (@right), (@size))). See1239* KUNIT_EXPECT_TRUE() for more information.1240*1241* Although this expectation works for any memory block, it is not recommended1242* for comparing more structured data, such as structs. This expectation is1243* recommended for comparing, for example, data arrays.1244*/1245#define KUNIT_EXPECT_MEMEQ(test, left, right, size) \1246KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, NULL)12471248#define KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, fmt, ...) \1249KUNIT_MEM_ASSERTION(test, \1250KUNIT_EXPECTATION, \1251left, ==, right, \1252size, \1253fmt, \1254##__VA_ARGS__)12551256/**1257* KUNIT_EXPECT_MEMNEQ() - Expects that the first @size bytes of @left and @right are not equal.1258* @test: The test context object.1259* @left: An arbitrary expression that evaluates to the specified size.1260* @right: An arbitrary expression that evaluates to the specified size.1261* @size: Number of bytes compared.1262*1263* Sets an expectation that the values that @left and @right evaluate to are1264* not equal. This is semantically equivalent to1265* KUNIT_EXPECT_TRUE(@test, memcmp((@left), (@right), (@size))). See1266* KUNIT_EXPECT_TRUE() for more information.1267*1268* Although this expectation works for any memory block, it is not recommended1269* for comparing more structured data, such as structs. This expectation is1270* recommended for comparing, for example, data arrays.1271*/1272#define KUNIT_EXPECT_MEMNEQ(test, left, right, size) \1273KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, NULL)12741275#define KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \1276KUNIT_MEM_ASSERTION(test, \1277KUNIT_EXPECTATION, \1278left, !=, right, \1279size, \1280fmt, \1281##__VA_ARGS__)12821283/**1284* KUNIT_EXPECT_NULL() - Expects that @ptr is null.1285* @test: The test context object.1286* @ptr: an arbitrary pointer.1287*1288* Sets an expectation that the value that @ptr evaluates to is null. This is1289* semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).1290* See KUNIT_EXPECT_TRUE() for more information.1291*/1292#define KUNIT_EXPECT_NULL(test, ptr) \1293KUNIT_EXPECT_NULL_MSG(test, \1294ptr, \1295NULL)12961297#define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \1298KUNIT_BINARY_PTR_ASSERTION(test, \1299KUNIT_EXPECTATION, \1300ptr, ==, NULL, \1301fmt, \1302##__VA_ARGS__)13031304/**1305* KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.1306* @test: The test context object.1307* @ptr: an arbitrary pointer.1308*1309* Sets an expectation that the value that @ptr evaluates to is not null. This1310* is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).1311* See KUNIT_EXPECT_TRUE() for more information.1312*/1313#define KUNIT_EXPECT_NOT_NULL(test, ptr) \1314KUNIT_EXPECT_NOT_NULL_MSG(test, \1315ptr, \1316NULL)13171318#define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \1319KUNIT_BINARY_PTR_ASSERTION(test, \1320KUNIT_EXPECTATION, \1321ptr, !=, NULL, \1322fmt, \1323##__VA_ARGS__)13241325/**1326* KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.1327* @test: The test context object.1328* @ptr: an arbitrary pointer.1329*1330* Sets an expectation that the value that @ptr evaluates to is not null and not1331* an errno stored in a pointer. This is semantically equivalent to1332* KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for1333* more information.1334*/1335#define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \1336KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)13371338#define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \1339KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \1340KUNIT_EXPECTATION, \1341ptr, \1342fmt, \1343##__VA_ARGS__)13441345/**1346* KUNIT_FAIL_AND_ABORT() - Always causes a test to fail and abort when evaluated.1347* @test: The test context object.1348* @fmt: an informational message to be printed when the assertion is made.1349* @...: string format arguments.1350*1351* The opposite of KUNIT_SUCCEED(), it is an assertion that always fails. In1352* other words, it always results in a failed assertion, and consequently1353* always causes the test case to fail and abort when evaluated.1354* See KUNIT_ASSERT_TRUE() for more information.1355*/1356#define KUNIT_FAIL_AND_ABORT(test, fmt, ...) \1357KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)13581359/**1360* KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.1361* @test: The test context object.1362* @condition: an arbitrary boolean expression. The test fails and aborts when1363* this does not evaluate to true.1364*1365* This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to1366* fail *and immediately abort* when the specified condition is not met. Unlike1367* an expectation failure, it will prevent the test case from continuing to run;1368* this is otherwise known as an *assertion failure*.1369*/1370#define KUNIT_ASSERT_TRUE(test, condition) \1371KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)13721373#define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \1374KUNIT_TRUE_MSG_ASSERTION(test, \1375KUNIT_ASSERTION, \1376condition, \1377fmt, \1378##__VA_ARGS__)13791380/**1381* KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.1382* @test: The test context object.1383* @condition: an arbitrary boolean expression.1384*1385* Sets an assertion that the value that @condition evaluates to is false. This1386* is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure1387* (see KUNIT_ASSERT_TRUE()) when the assertion is not met.1388*/1389#define KUNIT_ASSERT_FALSE(test, condition) \1390KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)13911392#define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \1393KUNIT_FALSE_MSG_ASSERTION(test, \1394KUNIT_ASSERTION, \1395condition, \1396fmt, \1397##__VA_ARGS__)13981399/**1400* KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.1401* @test: The test context object.1402* @left: an arbitrary expression that evaluates to a primitive C type.1403* @right: an arbitrary expression that evaluates to a primitive C type.1404*1405* Sets an assertion that the values that @left and @right evaluate to are1406* equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion1407* failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.1408*/1409#define KUNIT_ASSERT_EQ(test, left, right) \1410KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)14111412#define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \1413KUNIT_BINARY_INT_ASSERTION(test, \1414KUNIT_ASSERTION, \1415left, ==, right, \1416fmt, \1417##__VA_ARGS__)14181419/**1420* KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.1421* @test: The test context object.1422* @left: an arbitrary expression that evaluates to a pointer.1423* @right: an arbitrary expression that evaluates to a pointer.1424*1425* Sets an assertion that the values that @left and @right evaluate to are1426* equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion1427* failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.1428*/1429#define KUNIT_ASSERT_PTR_EQ(test, left, right) \1430KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)14311432#define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \1433KUNIT_BINARY_PTR_ASSERTION(test, \1434KUNIT_ASSERTION, \1435left, ==, right, \1436fmt, \1437##__VA_ARGS__)14381439/**1440* KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.1441* @test: The test context object.1442* @left: an arbitrary expression that evaluates to a primitive C type.1443* @right: an arbitrary expression that evaluates to a primitive C type.1444*1445* Sets an assertion that the values that @left and @right evaluate to are not1446* equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion1447* failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.1448*/1449#define KUNIT_ASSERT_NE(test, left, right) \1450KUNIT_ASSERT_NE_MSG(test, left, right, NULL)14511452#define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \1453KUNIT_BINARY_INT_ASSERTION(test, \1454KUNIT_ASSERTION, \1455left, !=, right, \1456fmt, \1457##__VA_ARGS__)14581459/**1460* KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.1461* KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.1462* @test: The test context object.1463* @left: an arbitrary expression that evaluates to a pointer.1464* @right: an arbitrary expression that evaluates to a pointer.1465*1466* Sets an assertion that the values that @left and @right evaluate to are not1467* equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion1468* failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.1469*/1470#define KUNIT_ASSERT_PTR_NE(test, left, right) \1471KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)14721473#define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \1474KUNIT_BINARY_PTR_ASSERTION(test, \1475KUNIT_ASSERTION, \1476left, !=, right, \1477fmt, \1478##__VA_ARGS__)1479/**1480* KUNIT_ASSERT_LT() - An assertion that @left is less than @right.1481* @test: The test context object.1482* @left: an arbitrary expression that evaluates to a primitive C type.1483* @right: an arbitrary expression that evaluates to a primitive C type.1484*1485* Sets an assertion that the value that @left evaluates to is less than the1486* value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except1487* it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion1488* is not met.1489*/1490#define KUNIT_ASSERT_LT(test, left, right) \1491KUNIT_ASSERT_LT_MSG(test, left, right, NULL)14921493#define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \1494KUNIT_BINARY_INT_ASSERTION(test, \1495KUNIT_ASSERTION, \1496left, <, right, \1497fmt, \1498##__VA_ARGS__)1499/**1500* KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.1501* @test: The test context object.1502* @left: an arbitrary expression that evaluates to a primitive C type.1503* @right: an arbitrary expression that evaluates to a primitive C type.1504*1505* Sets an assertion that the value that @left evaluates to is less than or1506* equal to the value that @right evaluates to. This is the same as1507* KUNIT_EXPECT_LE(), except it causes an assertion failure (see1508* KUNIT_ASSERT_TRUE()) when the assertion is not met.1509*/1510#define KUNIT_ASSERT_LE(test, left, right) \1511KUNIT_ASSERT_LE_MSG(test, left, right, NULL)15121513#define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \1514KUNIT_BINARY_INT_ASSERTION(test, \1515KUNIT_ASSERTION, \1516left, <=, right, \1517fmt, \1518##__VA_ARGS__)15191520/**1521* KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.1522* @test: The test context object.1523* @left: an arbitrary expression that evaluates to a primitive C type.1524* @right: an arbitrary expression that evaluates to a primitive C type.1525*1526* Sets an assertion that the value that @left evaluates to is greater than the1527* value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except1528* it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion1529* is not met.1530*/1531#define KUNIT_ASSERT_GT(test, left, right) \1532KUNIT_ASSERT_GT_MSG(test, left, right, NULL)15331534#define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \1535KUNIT_BINARY_INT_ASSERTION(test, \1536KUNIT_ASSERTION, \1537left, >, right, \1538fmt, \1539##__VA_ARGS__)15401541/**1542* KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.1543* @test: The test context object.1544* @left: an arbitrary expression that evaluates to a primitive C type.1545* @right: an arbitrary expression that evaluates to a primitive C type.1546*1547* Sets an assertion that the value that @left evaluates to is greater than the1548* value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except1549* it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion1550* is not met.1551*/1552#define KUNIT_ASSERT_GE(test, left, right) \1553KUNIT_ASSERT_GE_MSG(test, left, right, NULL)15541555#define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \1556KUNIT_BINARY_INT_ASSERTION(test, \1557KUNIT_ASSERTION, \1558left, >=, right, \1559fmt, \1560##__VA_ARGS__)15611562/**1563* KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.1564* @test: The test context object.1565* @left: an arbitrary expression that evaluates to a null terminated string.1566* @right: an arbitrary expression that evaluates to a null terminated string.1567*1568* Sets an assertion that the values that @left and @right evaluate to are1569* equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an1570* assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.1571*/1572#define KUNIT_ASSERT_STREQ(test, left, right) \1573KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)15741575#define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \1576KUNIT_BINARY_STR_ASSERTION(test, \1577KUNIT_ASSERTION, \1578left, ==, right, \1579fmt, \1580##__VA_ARGS__)15811582/**1583* KUNIT_ASSERT_STRNEQ() - An assertion that strings @left and @right are not equal.1584* @test: The test context object.1585* @left: an arbitrary expression that evaluates to a null terminated string.1586* @right: an arbitrary expression that evaluates to a null terminated string.1587*1588* Sets an assertion that the values that @left and @right evaluate to are1589* not equal. This is semantically equivalent to1590* KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()1591* for more information.1592*/1593#define KUNIT_ASSERT_STRNEQ(test, left, right) \1594KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)15951596#define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \1597KUNIT_BINARY_STR_ASSERTION(test, \1598KUNIT_ASSERTION, \1599left, !=, right, \1600fmt, \1601##__VA_ARGS__)16021603/**1604* KUNIT_ASSERT_MEMEQ() - Asserts that the first @size bytes of @left and @right are equal.1605* @test: The test context object.1606* @left: An arbitrary expression that evaluates to the specified size.1607* @right: An arbitrary expression that evaluates to the specified size.1608* @size: Number of bytes compared.1609*1610* Sets an assertion that the values that @left and @right evaluate to are1611* equal. This is semantically equivalent to1612* KUNIT_ASSERT_TRUE(@test, !memcmp((@left), (@right), (@size))). See1613* KUNIT_ASSERT_TRUE() for more information.1614*1615* Although this assertion works for any memory block, it is not recommended1616* for comparing more structured data, such as structs. This assertion is1617* recommended for comparing, for example, data arrays.1618*/1619#define KUNIT_ASSERT_MEMEQ(test, left, right, size) \1620KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, NULL)16211622#define KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, fmt, ...) \1623KUNIT_MEM_ASSERTION(test, \1624KUNIT_ASSERTION, \1625left, ==, right, \1626size, \1627fmt, \1628##__VA_ARGS__)16291630/**1631* KUNIT_ASSERT_MEMNEQ() - Asserts that the first @size bytes of @left and @right are not equal.1632* @test: The test context object.1633* @left: An arbitrary expression that evaluates to the specified size.1634* @right: An arbitrary expression that evaluates to the specified size.1635* @size: Number of bytes compared.1636*1637* Sets an assertion that the values that @left and @right evaluate to are1638* not equal. This is semantically equivalent to1639* KUNIT_ASSERT_TRUE(@test, memcmp((@left), (@right), (@size))). See1640* KUNIT_ASSERT_TRUE() for more information.1641*1642* Although this assertion works for any memory block, it is not recommended1643* for comparing more structured data, such as structs. This assertion is1644* recommended for comparing, for example, data arrays.1645*/1646#define KUNIT_ASSERT_MEMNEQ(test, left, right, size) \1647KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, NULL)16481649#define KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \1650KUNIT_MEM_ASSERTION(test, \1651KUNIT_ASSERTION, \1652left, !=, right, \1653size, \1654fmt, \1655##__VA_ARGS__)16561657/**1658* KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.1659* @test: The test context object.1660* @ptr: an arbitrary pointer.1661*1662* Sets an assertion that the values that @ptr evaluates to is null. This is1663* the same as KUNIT_EXPECT_NULL(), except it causes an assertion1664* failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.1665*/1666#define KUNIT_ASSERT_NULL(test, ptr) \1667KUNIT_ASSERT_NULL_MSG(test, \1668ptr, \1669NULL)16701671#define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \1672KUNIT_BINARY_PTR_ASSERTION(test, \1673KUNIT_ASSERTION, \1674ptr, ==, NULL, \1675fmt, \1676##__VA_ARGS__)16771678/**1679* KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.1680* @test: The test context object.1681* @ptr: an arbitrary pointer.1682*1683* Sets an assertion that the values that @ptr evaluates to is not null. This1684* is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion1685* failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.1686*/1687#define KUNIT_ASSERT_NOT_NULL(test, ptr) \1688KUNIT_ASSERT_NOT_NULL_MSG(test, \1689ptr, \1690NULL)16911692#define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \1693KUNIT_BINARY_PTR_ASSERTION(test, \1694KUNIT_ASSERTION, \1695ptr, !=, NULL, \1696fmt, \1697##__VA_ARGS__)16981699/**1700* KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.1701* @test: The test context object.1702* @ptr: an arbitrary pointer.1703*1704* Sets an assertion that the value that @ptr evaluates to is not null and not1705* an errno stored in a pointer. This is the same as1706* KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see1707* KUNIT_ASSERT_TRUE()) when the assertion is not met.1708*/1709#define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \1710KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)17111712#define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \1713KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \1714KUNIT_ASSERTION, \1715ptr, \1716fmt, \1717##__VA_ARGS__)17181719/**1720* KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.1721* @name: prefix for the test parameter generator function.1722* @array: array of test parameters.1723* @get_desc: function to convert param to description; NULL to use default1724*1725* Define function @name_gen_params which uses @array to generate parameters.1726*/1727#define KUNIT_ARRAY_PARAM(name, array, get_desc) \1728static const void *name##_gen_params(struct kunit *test, \1729const void *prev, char *desc) \1730{ \1731typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \1732if (!prev) \1733kunit_register_params_array(test, array, ARRAY_SIZE(array), NULL); \1734if (__next - (array) < ARRAY_SIZE((array))) { \1735void (*__get_desc)(typeof(__next), char *) = get_desc; \1736if (__get_desc) \1737__get_desc(__next, desc); \1738return __next; \1739} \1740return NULL; \1741}17421743/**1744* KUNIT_ARRAY_PARAM_DESC() - Define test parameter generator from an array.1745* @name: prefix for the test parameter generator function.1746* @array: array of test parameters.1747* @desc_member: structure member from array element to use as description1748*1749* Define function @name_gen_params which uses @array to generate parameters.1750*/1751#define KUNIT_ARRAY_PARAM_DESC(name, array, desc_member) \1752static const void *name##_gen_params(struct kunit *test, \1753const void *prev, char *desc) \1754{ \1755typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \1756if (!prev) \1757kunit_register_params_array(test, array, ARRAY_SIZE(array), NULL); \1758if (__next - (array) < ARRAY_SIZE((array))) { \1759strscpy(desc, __next->desc_member, KUNIT_PARAM_DESC_SIZE); \1760return __next; \1761} \1762return NULL; \1763}17641765/**1766* kunit_register_params_array() - Register parameter array for a KUnit test.1767* @test: The KUnit test structure to which parameters will be added.1768* @array: An array of test parameters.1769* @param_count: Number of parameters.1770* @get_desc: Function that generates a string description for a given parameter1771* element.1772*1773* This macro initializes the @test's parameter array data, storing information1774* including the parameter array, its count, the element size, and the parameter1775* description function within `test->params_array`.1776*1777* Note: If using this macro in param_init(), kunit_array_gen_params()1778* will then need to be manually provided as the parameter generator function to1779* KUNIT_CASE_PARAM_WITH_INIT(). kunit_array_gen_params() is a KUnit1780* function that uses the registered array to generate parameters1781*/1782#define kunit_register_params_array(test, array, param_count, get_desc) \1783do { \1784struct kunit *_test = (test); \1785const typeof((array)[0]) * _params_ptr = &(array)[0]; \1786_test->params_array.params = _params_ptr; \1787_test->params_array.num_params = (param_count); \1788_test->params_array.elem_size = sizeof(*_params_ptr); \1789_test->params_array.get_description = (get_desc); \1790} while (0)17911792// TODO([email protected]): consider eventually migrating users to explicitly1793// include resource.h themselves if they need it.1794#include <kunit/resource.h>17951796#endif /* _KUNIT_TEST_H */179717981799