test.h 64 KB

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