test.h 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  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. #ifdef CONFIG_SEC_KUNIT
  12. #include <kunit/kunit-stream.h>
  13. #endif /* CONFIG_SEC_KUNIT */
  14. #include <kunit/try-catch.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/kconfig.h>
  20. #include <linux/kref.h>
  21. #include <linux/list.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/string.h>
  26. #include <linux/types.h>
  27. #include <asm/rwonce.h>
  28. struct kunit;
  29. /* Size of log associated with test. */
  30. #define KUNIT_LOG_SIZE 512
  31. /* Maximum size of parameter description string. */
  32. #define KUNIT_PARAM_DESC_SIZE 128
  33. /* Maximum size of a status comment. */
  34. #define KUNIT_STATUS_COMMENT_SIZE 256
  35. /*
  36. * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
  37. * sub-subtest. See the "Subtests" section in
  38. * https://node-tap.org/tap-protocol/
  39. */
  40. #define KUNIT_SUBTEST_INDENT " "
  41. #define KUNIT_SUBSUBTEST_INDENT " "
  42. /**
  43. * enum kunit_status - Type of result for a test or test suite
  44. * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped
  45. * @KUNIT_FAILURE: Denotes the test has failed.
  46. * @KUNIT_SKIPPED: Denotes the test has been skipped.
  47. */
  48. enum kunit_status {
  49. KUNIT_SUCCESS,
  50. KUNIT_FAILURE,
  51. KUNIT_SKIPPED,
  52. };
  53. /**
  54. * struct kunit_case - represents an individual test case.
  55. *
  56. * @run_case: the function representing the actual test case.
  57. * @name: the name of the test case.
  58. * @generate_params: the generator function for parameterized tests.
  59. *
  60. * A test case is a function with the signature,
  61. * ``void (*)(struct kunit *)``
  62. * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
  63. * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
  64. * with a &struct kunit_suite and will be run after the suite's init
  65. * function and followed by the suite's exit function.
  66. *
  67. * A test case should be static and should only be created with the
  68. * KUNIT_CASE() macro; additionally, every array of test cases should be
  69. * terminated with an empty test case.
  70. *
  71. * Example:
  72. *
  73. * .. code-block:: c
  74. *
  75. * void add_test_basic(struct kunit *test)
  76. * {
  77. * KUNIT_EXPECT_EQ(test, 1, add(1, 0));
  78. * KUNIT_EXPECT_EQ(test, 2, add(1, 1));
  79. * KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
  80. * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
  81. * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
  82. * }
  83. *
  84. * static struct kunit_case example_test_cases[] = {
  85. * KUNIT_CASE(add_test_basic),
  86. * {}
  87. * };
  88. *
  89. */
  90. struct kunit_case {
  91. void (*run_case)(struct kunit *test);
  92. const char *name;
  93. const void* (*generate_params)(const void *prev, char *desc);
  94. /* private: internal use only. */
  95. enum kunit_status status;
  96. char *log;
  97. };
  98. static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
  99. {
  100. switch (status) {
  101. case KUNIT_SKIPPED:
  102. case KUNIT_SUCCESS:
  103. return "ok";
  104. case KUNIT_FAILURE:
  105. return "not ok";
  106. }
  107. return "invalid";
  108. }
  109. /**
  110. * KUNIT_CASE - A helper for creating a &struct kunit_case
  111. *
  112. * @test_name: a reference to a test case function.
  113. *
  114. * Takes a symbol for a function representing a test case and creates a
  115. * &struct kunit_case object from it. See the documentation for
  116. * &struct kunit_case for an example on how to use it.
  117. */
  118. #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
  119. /**
  120. * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
  121. *
  122. * @test_name: a reference to a test case function.
  123. * @gen_params: a reference to a parameter generator function.
  124. *
  125. * The generator function::
  126. *
  127. * const void* gen_params(const void *prev, char *desc)
  128. *
  129. * is used to lazily generate a series of arbitrarily typed values that fit into
  130. * a void*. The argument @prev is the previously returned value, which should be
  131. * used to derive the next value; @prev is set to NULL on the initial generator
  132. * call. When no more values are available, the generator must return NULL.
  133. * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)
  134. * describing the parameter.
  135. */
  136. #define KUNIT_CASE_PARAM(test_name, gen_params) \
  137. { .run_case = test_name, .name = #test_name, \
  138. .generate_params = gen_params }
  139. /**
  140. * struct kunit_suite - describes a related collection of &struct kunit_case
  141. *
  142. * @name: the name of the test. Purely informational.
  143. * @suite_init: called once per test suite before the test cases.
  144. * @suite_exit: called once per test suite after all test cases.
  145. * @init: called before every test case.
  146. * @exit: called after every test case.
  147. * @test_cases: a null terminated array of test cases.
  148. *
  149. * A kunit_suite is a collection of related &struct kunit_case s, such that
  150. * @init is called before every test case and @exit is called after every
  151. * test case, similar to the notion of a *test fixture* or a *test class*
  152. * in other unit testing frameworks like JUnit or Googletest.
  153. *
  154. * Every &struct kunit_case must be associated with a kunit_suite for KUnit
  155. * to run it.
  156. */
  157. struct kunit_suite {
  158. const char name[256];
  159. int (*suite_init)(struct kunit_suite *suite);
  160. void (*suite_exit)(struct kunit_suite *suite);
  161. int (*init)(struct kunit *test);
  162. void (*exit)(struct kunit *test);
  163. struct kunit_case *test_cases;
  164. /* private: internal use only */
  165. char status_comment[KUNIT_STATUS_COMMENT_SIZE];
  166. struct dentry *debugfs;
  167. char *log;
  168. int suite_init_err;
  169. };
  170. /**
  171. * struct kunit - represents a running instance of a test.
  172. *
  173. * @priv: for user to store arbitrary data. Commonly used to pass data
  174. * created in the init function (see &struct kunit_suite).
  175. *
  176. * Used to store information about the current context under which the test
  177. * is running. Most of this data is private and should only be accessed
  178. * indirectly via public functions; the one exception is @priv which can be
  179. * used by the test writer to store arbitrary data.
  180. */
  181. struct kunit {
  182. void *priv;
  183. /* private: internal use only. */
  184. const char *name; /* Read only after initialization! */
  185. char *log; /* Points at case log after initialization */
  186. struct kunit_try_catch try_catch;
  187. /* param_value is the current parameter value for a test case. */
  188. const void *param_value;
  189. /* param_index stores the index of the parameter in parameterized tests. */
  190. int param_index;
  191. /*
  192. * success starts as true, and may only be set to false during a
  193. * test case; thus, it is safe to update this across multiple
  194. * threads using WRITE_ONCE; however, as a consequence, it may only
  195. * be read after the test case finishes once all threads associated
  196. * with the test case have terminated.
  197. */
  198. spinlock_t lock; /* Guards all mutable test state. */
  199. enum kunit_status status; /* Read only after test_case finishes! */
  200. /*
  201. * Because resources is a list that may be updated multiple times (with
  202. * new resources) from any thread associated with a test case, we must
  203. * protect it with some type of lock.
  204. */
  205. struct list_head resources; /* Protected by lock. */
  206. #ifdef CONFIG_SEC_KUNIT
  207. struct list_head post_conditions;
  208. #endif /* CONFIG_SEC_KUNIT */
  209. char status_comment[KUNIT_STATUS_COMMENT_SIZE];
  210. };
  211. static inline void kunit_set_failure(struct kunit *test)
  212. {
  213. WRITE_ONCE(test->status, KUNIT_FAILURE);
  214. }
  215. bool kunit_enabled(void);
  216. void kunit_init_test(struct kunit *test, const char *name, char *log);
  217. int kunit_run_tests(struct kunit_suite *suite);
  218. size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
  219. unsigned int kunit_test_case_num(struct kunit_suite *suite,
  220. struct kunit_case *test_case);
  221. int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites);
  222. void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);
  223. #if IS_BUILTIN(CONFIG_KUNIT)
  224. int kunit_run_all_tests(void);
  225. #else
  226. static inline int kunit_run_all_tests(void)
  227. {
  228. return 0;
  229. }
  230. #endif /* IS_BUILTIN(CONFIG_KUNIT) */
  231. #define __kunit_test_suites(unique_array, ...) \
  232. static struct kunit_suite *unique_array[] \
  233. __aligned(sizeof(struct kunit_suite *)) \
  234. __used __section(".kunit_test_suites") = { __VA_ARGS__ }
  235. /**
  236. * kunit_test_suites() - used to register one or more &struct kunit_suite
  237. * with KUnit.
  238. *
  239. * @__suites: a statically allocated list of &struct kunit_suite.
  240. *
  241. * Registers @suites with the test framework.
  242. * This is done by placing the array of struct kunit_suite * in the
  243. * .kunit_test_suites ELF section.
  244. *
  245. * When builtin, KUnit tests are all run via the executor at boot, and when
  246. * built as a module, they run on module load.
  247. *
  248. */
  249. #define kunit_test_suites(__suites...) \
  250. __kunit_test_suites(__UNIQUE_ID(array), \
  251. ##__suites)
  252. #define kunit_test_suite(suite) kunit_test_suites(&suite)
  253. /**
  254. * kunit_test_init_section_suites() - used to register one or more &struct
  255. * kunit_suite containing init functions or
  256. * init data.
  257. *
  258. * @__suites: a statically allocated list of &struct kunit_suite.
  259. *
  260. * This functions identically as kunit_test_suites() except that it suppresses
  261. * modpost warnings for referencing functions marked __init or data marked
  262. * __initdata; this is OK because currently KUnit only runs tests upon boot
  263. * during the init phase or upon loading a module during the init phase.
  264. *
  265. * NOTE TO KUNIT DEVS: If we ever allow KUnit tests to be run after boot, these
  266. * tests must be excluded.
  267. *
  268. * The only thing this macro does that's different from kunit_test_suites is
  269. * that it suffixes the array and suite declarations it makes with _probe;
  270. * modpost suppresses warnings about referencing init data for symbols named in
  271. * this manner.
  272. */
  273. #define kunit_test_init_section_suites(__suites...) \
  274. __kunit_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \
  275. ##__suites)
  276. #define kunit_test_init_section_suite(suite) \
  277. kunit_test_init_section_suites(&suite)
  278. #define kunit_suite_for_each_test_case(suite, test_case) \
  279. for (test_case = suite->test_cases; test_case->run_case; test_case++)
  280. enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
  281. /**
  282. * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
  283. * @test: The test context object.
  284. * @n: number of elements.
  285. * @size: The size in bytes of the desired memory.
  286. * @gfp: flags passed to underlying kmalloc().
  287. *
  288. * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
  289. * and is automatically cleaned up after the test case concludes. See &struct
  290. * kunit_resource for more information.
  291. */
  292. void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
  293. /**
  294. * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
  295. * @test: The test context object.
  296. * @size: The size in bytes of the desired memory.
  297. * @gfp: flags passed to underlying kmalloc().
  298. *
  299. * See kmalloc() and kunit_kmalloc_array() for more information.
  300. */
  301. static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
  302. {
  303. return kunit_kmalloc_array(test, 1, size, gfp);
  304. }
  305. /**
  306. * kunit_kfree() - Like kfree except for allocations managed by KUnit.
  307. * @test: The test case to which the resource belongs.
  308. * @ptr: The memory allocation to free.
  309. */
  310. void kunit_kfree(struct kunit *test, const void *ptr);
  311. /**
  312. * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
  313. * @test: The test context object.
  314. * @size: The size in bytes of the desired memory.
  315. * @gfp: flags passed to underlying kmalloc().
  316. *
  317. * See kzalloc() and kunit_kmalloc_array() for more information.
  318. */
  319. static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
  320. {
  321. return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
  322. }
  323. /**
  324. * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
  325. * @test: The test context object.
  326. * @n: number of elements.
  327. * @size: The size in bytes of the desired memory.
  328. * @gfp: flags passed to underlying kmalloc().
  329. *
  330. * See kcalloc() and kunit_kmalloc_array() for more information.
  331. */
  332. static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
  333. {
  334. return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
  335. }
  336. void kunit_cleanup(struct kunit *test);
  337. void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
  338. /**
  339. * kunit_mark_skipped() - Marks @test_or_suite as skipped
  340. *
  341. * @test_or_suite: The test context object.
  342. * @fmt: A printk() style format string.
  343. *
  344. * Marks the test as skipped. @fmt is given output as the test status
  345. * comment, typically the reason the test was skipped.
  346. *
  347. * Test execution continues after kunit_mark_skipped() is called.
  348. */
  349. #define kunit_mark_skipped(test_or_suite, fmt, ...) \
  350. do { \
  351. WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED); \
  352. scnprintf((test_or_suite)->status_comment, \
  353. KUNIT_STATUS_COMMENT_SIZE, \
  354. fmt, ##__VA_ARGS__); \
  355. } while (0)
  356. /**
  357. * kunit_skip() - Marks @test_or_suite as skipped
  358. *
  359. * @test_or_suite: The test context object.
  360. * @fmt: A printk() style format string.
  361. *
  362. * Skips the test. @fmt is given output as the test status
  363. * comment, typically the reason the test was skipped.
  364. *
  365. * Test execution is halted after kunit_skip() is called.
  366. */
  367. #define kunit_skip(test_or_suite, fmt, ...) \
  368. do { \
  369. kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
  370. kunit_try_catch_throw(&((test_or_suite)->try_catch)); \
  371. } while (0)
  372. /*
  373. * printk and log to per-test or per-suite log buffer. Logging only done
  374. * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
  375. */
  376. #define kunit_log(lvl, test_or_suite, fmt, ...) \
  377. do { \
  378. printk(lvl fmt, ##__VA_ARGS__); \
  379. kunit_log_append((test_or_suite)->log, fmt "\n", \
  380. ##__VA_ARGS__); \
  381. } while (0)
  382. #define kunit_printk(lvl, test, fmt, ...) \
  383. kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
  384. (test)->name, ##__VA_ARGS__)
  385. /**
  386. * kunit_info() - Prints an INFO level message associated with @test.
  387. *
  388. * @test: The test context object.
  389. * @fmt: A printk() style format string.
  390. *
  391. * Prints an info level message associated with the test suite being run.
  392. * Takes a variable number of format parameters just like printk().
  393. */
  394. #define kunit_info(test, fmt, ...) \
  395. kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
  396. /**
  397. * kunit_warn() - Prints a WARN level message associated with @test.
  398. *
  399. * @test: The test context object.
  400. * @fmt: A printk() style format string.
  401. *
  402. * Prints a warning level message.
  403. */
  404. #define kunit_warn(test, fmt, ...) \
  405. kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
  406. /**
  407. * kunit_err() - Prints an ERROR level message associated with @test.
  408. *
  409. * @test: The test context object.
  410. * @fmt: A printk() style format string.
  411. *
  412. * Prints an error level message.
  413. */
  414. #define kunit_err(test, fmt, ...) \
  415. kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
  416. /**
  417. * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
  418. * @test: The test context object.
  419. *
  420. * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
  421. * words, it does nothing and only exists for code clarity. See
  422. * KUNIT_EXPECT_TRUE() for more information.
  423. */
  424. #define KUNIT_SUCCEED(test) do {} while (0)
  425. void kunit_do_failed_assertion(struct kunit *test,
  426. const struct kunit_loc *loc,
  427. enum kunit_assert_type type,
  428. const struct kunit_assert *assert,
  429. assert_format_t assert_format,
  430. const char *fmt, ...);
  431. #define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
  432. static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
  433. const struct assert_class __assertion = INITIALIZER; \
  434. kunit_do_failed_assertion(test, \
  435. &__loc, \
  436. assert_type, \
  437. &__assertion.assert, \
  438. assert_format, \
  439. fmt, \
  440. ##__VA_ARGS__); \
  441. } while (0)
  442. #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
  443. _KUNIT_FAILED(test, \
  444. assert_type, \
  445. kunit_fail_assert, \
  446. kunit_fail_assert_format, \
  447. {}, \
  448. fmt, \
  449. ##__VA_ARGS__)
  450. /**
  451. * KUNIT_FAIL() - Always causes a test to fail when evaluated.
  452. * @test: The test context object.
  453. * @fmt: an informational message to be printed when the assertion is made.
  454. * @...: string format arguments.
  455. *
  456. * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
  457. * other words, it always results in a failed expectation, and consequently
  458. * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
  459. * for more information.
  460. */
  461. #define KUNIT_FAIL(test, fmt, ...) \
  462. KUNIT_FAIL_ASSERTION(test, \
  463. KUNIT_EXPECTATION, \
  464. fmt, \
  465. ##__VA_ARGS__)
  466. #define KUNIT_UNARY_ASSERTION(test, \
  467. assert_type, \
  468. condition, \
  469. expected_true, \
  470. fmt, \
  471. ...) \
  472. do { \
  473. if (likely(!!(condition) == !!expected_true)) \
  474. break; \
  475. \
  476. _KUNIT_FAILED(test, \
  477. assert_type, \
  478. kunit_unary_assert, \
  479. kunit_unary_assert_format, \
  480. KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \
  481. expected_true), \
  482. fmt, \
  483. ##__VA_ARGS__); \
  484. } while (0)
  485. #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
  486. KUNIT_UNARY_ASSERTION(test, \
  487. assert_type, \
  488. condition, \
  489. true, \
  490. fmt, \
  491. ##__VA_ARGS__)
  492. #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
  493. KUNIT_UNARY_ASSERTION(test, \
  494. assert_type, \
  495. condition, \
  496. false, \
  497. fmt, \
  498. ##__VA_ARGS__)
  499. /*
  500. * A factory macro for defining the assertions and expectations for the basic
  501. * comparisons defined for the built in types.
  502. *
  503. * Unfortunately, there is no common type that all types can be promoted to for
  504. * which all the binary operators behave the same way as for the actual types
  505. * (for example, there is no type that long long and unsigned long long can
  506. * both be cast to where the comparison result is preserved for all values). So
  507. * the best we can do is do the comparison in the original types and then coerce
  508. * everything to long long for printing; this way, the comparison behaves
  509. * correctly and the printed out value usually makes sense without
  510. * interpretation, but can always be interpreted to figure out the actual
  511. * value.
  512. */
  513. #define KUNIT_BASE_BINARY_ASSERTION(test, \
  514. assert_class, \
  515. format_func, \
  516. assert_type, \
  517. left, \
  518. op, \
  519. right, \
  520. fmt, \
  521. ...) \
  522. do { \
  523. const typeof(left) __left = (left); \
  524. const typeof(right) __right = (right); \
  525. static const struct kunit_binary_assert_text __text = { \
  526. .operation = #op, \
  527. .left_text = #left, \
  528. .right_text = #right, \
  529. }; \
  530. \
  531. if (likely(__left op __right)) \
  532. break; \
  533. \
  534. _KUNIT_FAILED(test, \
  535. assert_type, \
  536. assert_class, \
  537. format_func, \
  538. KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \
  539. __left, \
  540. __right), \
  541. fmt, \
  542. ##__VA_ARGS__); \
  543. } while (0)
  544. #define KUNIT_BINARY_INT_ASSERTION(test, \
  545. assert_type, \
  546. left, \
  547. op, \
  548. right, \
  549. fmt, \
  550. ...) \
  551. KUNIT_BASE_BINARY_ASSERTION(test, \
  552. kunit_binary_assert, \
  553. kunit_binary_assert_format, \
  554. assert_type, \
  555. left, op, right, \
  556. fmt, \
  557. ##__VA_ARGS__)
  558. #define KUNIT_BINARY_PTR_ASSERTION(test, \
  559. assert_type, \
  560. left, \
  561. op, \
  562. right, \
  563. fmt, \
  564. ...) \
  565. KUNIT_BASE_BINARY_ASSERTION(test, \
  566. kunit_binary_ptr_assert, \
  567. kunit_binary_ptr_assert_format, \
  568. assert_type, \
  569. left, op, right, \
  570. fmt, \
  571. ##__VA_ARGS__)
  572. #define KUNIT_BINARY_STR_ASSERTION(test, \
  573. assert_type, \
  574. left, \
  575. op, \
  576. right, \
  577. fmt, \
  578. ...) \
  579. do { \
  580. const char *__left = (left); \
  581. const char *__right = (right); \
  582. static const struct kunit_binary_assert_text __text = { \
  583. .operation = #op, \
  584. .left_text = #left, \
  585. .right_text = #right, \
  586. }; \
  587. \
  588. if (likely(strcmp(__left, __right) op 0)) \
  589. break; \
  590. \
  591. \
  592. _KUNIT_FAILED(test, \
  593. assert_type, \
  594. kunit_binary_str_assert, \
  595. kunit_binary_str_assert_format, \
  596. KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \
  597. __left, \
  598. __right), \
  599. fmt, \
  600. ##__VA_ARGS__); \
  601. } while (0)
  602. #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
  603. assert_type, \
  604. ptr, \
  605. fmt, \
  606. ...) \
  607. do { \
  608. const typeof(ptr) __ptr = (ptr); \
  609. \
  610. if (!IS_ERR_OR_NULL(__ptr)) \
  611. break; \
  612. \
  613. _KUNIT_FAILED(test, \
  614. assert_type, \
  615. kunit_ptr_not_err_assert, \
  616. kunit_ptr_not_err_assert_format, \
  617. KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, __ptr), \
  618. fmt, \
  619. ##__VA_ARGS__); \
  620. } while (0)
  621. /**
  622. * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
  623. * @test: The test context object.
  624. * @condition: an arbitrary boolean expression. The test fails when this does
  625. * not evaluate to true.
  626. *
  627. * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
  628. * to fail when the specified condition is not met; however, it will not prevent
  629. * the test case from continuing to run; this is otherwise known as an
  630. * *expectation failure*.
  631. */
  632. #define KUNIT_EXPECT_TRUE(test, condition) \
  633. KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
  634. #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
  635. KUNIT_TRUE_MSG_ASSERTION(test, \
  636. KUNIT_EXPECTATION, \
  637. condition, \
  638. fmt, \
  639. ##__VA_ARGS__)
  640. /**
  641. * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
  642. * @test: The test context object.
  643. * @condition: an arbitrary boolean expression. The test fails when this does
  644. * not evaluate to false.
  645. *
  646. * Sets an expectation that @condition evaluates to false. See
  647. * KUNIT_EXPECT_TRUE() for more information.
  648. */
  649. #define KUNIT_EXPECT_FALSE(test, condition) \
  650. KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
  651. #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
  652. KUNIT_FALSE_MSG_ASSERTION(test, \
  653. KUNIT_EXPECTATION, \
  654. condition, \
  655. fmt, \
  656. ##__VA_ARGS__)
  657. /**
  658. * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
  659. * @test: The test context object.
  660. * @left: an arbitrary expression that evaluates to a primitive C type.
  661. * @right: an arbitrary expression that evaluates to a primitive C type.
  662. *
  663. * Sets an expectation that the values that @left and @right evaluate to are
  664. * equal. This is semantically equivalent to
  665. * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
  666. * more information.
  667. */
  668. #define KUNIT_EXPECT_EQ(test, left, right) \
  669. KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
  670. #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
  671. KUNIT_BINARY_INT_ASSERTION(test, \
  672. KUNIT_EXPECTATION, \
  673. left, ==, right, \
  674. fmt, \
  675. ##__VA_ARGS__)
  676. /**
  677. * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
  678. * @test: The test context object.
  679. * @left: an arbitrary expression that evaluates to a pointer.
  680. * @right: an arbitrary expression that evaluates to a pointer.
  681. *
  682. * Sets an expectation that the values that @left and @right evaluate to are
  683. * equal. This is semantically equivalent to
  684. * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
  685. * more information.
  686. */
  687. #define KUNIT_EXPECT_PTR_EQ(test, left, right) \
  688. KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
  689. #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
  690. KUNIT_BINARY_PTR_ASSERTION(test, \
  691. KUNIT_EXPECTATION, \
  692. left, ==, right, \
  693. fmt, \
  694. ##__VA_ARGS__)
  695. /**
  696. * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
  697. * @test: The test context object.
  698. * @left: an arbitrary expression that evaluates to a primitive C type.
  699. * @right: an arbitrary expression that evaluates to a primitive C type.
  700. *
  701. * Sets an expectation that the values that @left and @right evaluate to are not
  702. * equal. This is semantically equivalent to
  703. * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
  704. * more information.
  705. */
  706. #define KUNIT_EXPECT_NE(test, left, right) \
  707. KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
  708. #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
  709. KUNIT_BINARY_INT_ASSERTION(test, \
  710. KUNIT_EXPECTATION, \
  711. left, !=, right, \
  712. fmt, \
  713. ##__VA_ARGS__)
  714. /**
  715. * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
  716. * @test: The test context object.
  717. * @left: an arbitrary expression that evaluates to a pointer.
  718. * @right: an arbitrary expression that evaluates to a pointer.
  719. *
  720. * Sets an expectation that the values that @left and @right evaluate to are not
  721. * equal. This is semantically equivalent to
  722. * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
  723. * more information.
  724. */
  725. #define KUNIT_EXPECT_PTR_NE(test, left, right) \
  726. KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
  727. #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
  728. KUNIT_BINARY_PTR_ASSERTION(test, \
  729. KUNIT_EXPECTATION, \
  730. left, !=, right, \
  731. fmt, \
  732. ##__VA_ARGS__)
  733. /**
  734. * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
  735. * @test: The test context object.
  736. * @left: an arbitrary expression that evaluates to a primitive C type.
  737. * @right: an arbitrary expression that evaluates to a primitive C type.
  738. *
  739. * Sets an expectation that the value that @left evaluates to is less than the
  740. * value that @right evaluates to. This is semantically equivalent to
  741. * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
  742. * more information.
  743. */
  744. #define KUNIT_EXPECT_LT(test, left, right) \
  745. KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
  746. #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
  747. KUNIT_BINARY_INT_ASSERTION(test, \
  748. KUNIT_EXPECTATION, \
  749. left, <, right, \
  750. fmt, \
  751. ##__VA_ARGS__)
  752. /**
  753. * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
  754. * @test: The test context object.
  755. * @left: an arbitrary expression that evaluates to a primitive C type.
  756. * @right: an arbitrary expression that evaluates to a primitive C type.
  757. *
  758. * Sets an expectation that the value that @left evaluates to is less than or
  759. * equal to the value that @right evaluates to. Semantically this is equivalent
  760. * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
  761. * more information.
  762. */
  763. #define KUNIT_EXPECT_LE(test, left, right) \
  764. KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
  765. #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
  766. KUNIT_BINARY_INT_ASSERTION(test, \
  767. KUNIT_EXPECTATION, \
  768. left, <=, right, \
  769. fmt, \
  770. ##__VA_ARGS__)
  771. /**
  772. * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
  773. * @test: The test context object.
  774. * @left: an arbitrary expression that evaluates to a primitive C type.
  775. * @right: an arbitrary expression that evaluates to a primitive C type.
  776. *
  777. * Sets an expectation that the value that @left evaluates to is greater than
  778. * the value that @right evaluates to. This is semantically equivalent to
  779. * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
  780. * more information.
  781. */
  782. #define KUNIT_EXPECT_GT(test, left, right) \
  783. KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
  784. #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
  785. KUNIT_BINARY_INT_ASSERTION(test, \
  786. KUNIT_EXPECTATION, \
  787. left, >, right, \
  788. fmt, \
  789. ##__VA_ARGS__)
  790. /**
  791. * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
  792. * @test: The test context object.
  793. * @left: an arbitrary expression that evaluates to a primitive C type.
  794. * @right: an arbitrary expression that evaluates to a primitive C type.
  795. *
  796. * Sets an expectation that the value that @left evaluates to is greater than
  797. * the value that @right evaluates to. This is semantically equivalent to
  798. * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
  799. * more information.
  800. */
  801. #define KUNIT_EXPECT_GE(test, left, right) \
  802. KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
  803. #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
  804. KUNIT_BINARY_INT_ASSERTION(test, \
  805. KUNIT_EXPECTATION, \
  806. left, >=, right, \
  807. fmt, \
  808. ##__VA_ARGS__)
  809. /**
  810. * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
  811. * @test: The test context object.
  812. * @left: an arbitrary expression that evaluates to a null terminated string.
  813. * @right: an arbitrary expression that evaluates to a null terminated string.
  814. *
  815. * Sets an expectation that the values that @left and @right evaluate to are
  816. * equal. This is semantically equivalent to
  817. * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
  818. * for more information.
  819. */
  820. #define KUNIT_EXPECT_STREQ(test, left, right) \
  821. KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
  822. #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
  823. KUNIT_BINARY_STR_ASSERTION(test, \
  824. KUNIT_EXPECTATION, \
  825. left, ==, right, \
  826. fmt, \
  827. ##__VA_ARGS__)
  828. /**
  829. * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
  830. * @test: The test context object.
  831. * @left: an arbitrary expression that evaluates to a null terminated string.
  832. * @right: an arbitrary expression that evaluates to a null terminated string.
  833. *
  834. * Sets an expectation that the values that @left and @right evaluate to are
  835. * not equal. This is semantically equivalent to
  836. * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
  837. * for more information.
  838. */
  839. #define KUNIT_EXPECT_STRNEQ(test, left, right) \
  840. KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
  841. #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
  842. KUNIT_BINARY_STR_ASSERTION(test, \
  843. KUNIT_EXPECTATION, \
  844. left, !=, right, \
  845. fmt, \
  846. ##__VA_ARGS__)
  847. /**
  848. * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
  849. * @test: The test context object.
  850. * @ptr: an arbitrary pointer.
  851. *
  852. * Sets an expectation that the value that @ptr evaluates to is null. This is
  853. * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
  854. * See KUNIT_EXPECT_TRUE() for more information.
  855. */
  856. #define KUNIT_EXPECT_NULL(test, ptr) \
  857. KUNIT_EXPECT_NULL_MSG(test, \
  858. ptr, \
  859. NULL)
  860. #define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \
  861. KUNIT_BINARY_PTR_ASSERTION(test, \
  862. KUNIT_EXPECTATION, \
  863. ptr, ==, NULL, \
  864. fmt, \
  865. ##__VA_ARGS__)
  866. /**
  867. * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
  868. * @test: The test context object.
  869. * @ptr: an arbitrary pointer.
  870. *
  871. * Sets an expectation that the value that @ptr evaluates to is not null. This
  872. * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
  873. * See KUNIT_EXPECT_TRUE() for more information.
  874. */
  875. #define KUNIT_EXPECT_NOT_NULL(test, ptr) \
  876. KUNIT_EXPECT_NOT_NULL_MSG(test, \
  877. ptr, \
  878. NULL)
  879. #define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \
  880. KUNIT_BINARY_PTR_ASSERTION(test, \
  881. KUNIT_EXPECTATION, \
  882. ptr, !=, NULL, \
  883. fmt, \
  884. ##__VA_ARGS__)
  885. /**
  886. * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
  887. * @test: The test context object.
  888. * @ptr: an arbitrary pointer.
  889. *
  890. * Sets an expectation that the value that @ptr evaluates to is not null and not
  891. * an errno stored in a pointer. This is semantically equivalent to
  892. * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
  893. * more information.
  894. */
  895. #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
  896. KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
  897. #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
  898. KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
  899. KUNIT_EXPECTATION, \
  900. ptr, \
  901. fmt, \
  902. ##__VA_ARGS__)
  903. #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
  904. KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
  905. /**
  906. * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
  907. * @test: The test context object.
  908. * @condition: an arbitrary boolean expression. The test fails and aborts when
  909. * this does not evaluate to true.
  910. *
  911. * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
  912. * fail *and immediately abort* when the specified condition is not met. Unlike
  913. * an expectation failure, it will prevent the test case from continuing to run;
  914. * this is otherwise known as an *assertion failure*.
  915. */
  916. #define KUNIT_ASSERT_TRUE(test, condition) \
  917. KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
  918. #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
  919. KUNIT_TRUE_MSG_ASSERTION(test, \
  920. KUNIT_ASSERTION, \
  921. condition, \
  922. fmt, \
  923. ##__VA_ARGS__)
  924. /**
  925. * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
  926. * @test: The test context object.
  927. * @condition: an arbitrary boolean expression.
  928. *
  929. * Sets an assertion that the value that @condition evaluates to is false. This
  930. * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
  931. * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
  932. */
  933. #define KUNIT_ASSERT_FALSE(test, condition) \
  934. KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
  935. #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
  936. KUNIT_FALSE_MSG_ASSERTION(test, \
  937. KUNIT_ASSERTION, \
  938. condition, \
  939. fmt, \
  940. ##__VA_ARGS__)
  941. /**
  942. * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
  943. * @test: The test context object.
  944. * @left: an arbitrary expression that evaluates to a primitive C type.
  945. * @right: an arbitrary expression that evaluates to a primitive C type.
  946. *
  947. * Sets an assertion that the values that @left and @right evaluate to are
  948. * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
  949. * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
  950. */
  951. #define KUNIT_ASSERT_EQ(test, left, right) \
  952. KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
  953. #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
  954. KUNIT_BINARY_INT_ASSERTION(test, \
  955. KUNIT_ASSERTION, \
  956. left, ==, right, \
  957. fmt, \
  958. ##__VA_ARGS__)
  959. /**
  960. * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
  961. * @test: The test context object.
  962. * @left: an arbitrary expression that evaluates to a pointer.
  963. * @right: an arbitrary expression that evaluates to a pointer.
  964. *
  965. * Sets an assertion that the values that @left and @right evaluate to are
  966. * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
  967. * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
  968. */
  969. #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
  970. KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
  971. #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
  972. KUNIT_BINARY_PTR_ASSERTION(test, \
  973. KUNIT_ASSERTION, \
  974. left, ==, right, \
  975. fmt, \
  976. ##__VA_ARGS__)
  977. /**
  978. * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
  979. * @test: The test context object.
  980. * @left: an arbitrary expression that evaluates to a primitive C type.
  981. * @right: an arbitrary expression that evaluates to a primitive C type.
  982. *
  983. * Sets an assertion that the values that @left and @right evaluate to are not
  984. * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
  985. * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
  986. */
  987. #define KUNIT_ASSERT_NE(test, left, right) \
  988. KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
  989. #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
  990. KUNIT_BINARY_INT_ASSERTION(test, \
  991. KUNIT_ASSERTION, \
  992. left, !=, right, \
  993. fmt, \
  994. ##__VA_ARGS__)
  995. /**
  996. * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
  997. * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
  998. * @test: The test context object.
  999. * @left: an arbitrary expression that evaluates to a pointer.
  1000. * @right: an arbitrary expression that evaluates to a pointer.
  1001. *
  1002. * Sets an assertion that the values that @left and @right evaluate to are not
  1003. * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
  1004. * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1005. */
  1006. #define KUNIT_ASSERT_PTR_NE(test, left, right) \
  1007. KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
  1008. #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
  1009. KUNIT_BINARY_PTR_ASSERTION(test, \
  1010. KUNIT_ASSERTION, \
  1011. left, !=, right, \
  1012. fmt, \
  1013. ##__VA_ARGS__)
  1014. /**
  1015. * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
  1016. * @test: The test context object.
  1017. * @left: an arbitrary expression that evaluates to a primitive C type.
  1018. * @right: an arbitrary expression that evaluates to a primitive C type.
  1019. *
  1020. * Sets an assertion that the value that @left evaluates to is less than the
  1021. * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
  1022. * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
  1023. * is not met.
  1024. */
  1025. #define KUNIT_ASSERT_LT(test, left, right) \
  1026. KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
  1027. #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
  1028. KUNIT_BINARY_INT_ASSERTION(test, \
  1029. KUNIT_ASSERTION, \
  1030. left, <, right, \
  1031. fmt, \
  1032. ##__VA_ARGS__)
  1033. /**
  1034. * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
  1035. * @test: The test context object.
  1036. * @left: an arbitrary expression that evaluates to a primitive C type.
  1037. * @right: an arbitrary expression that evaluates to a primitive C type.
  1038. *
  1039. * Sets an assertion that the value that @left evaluates to is less than or
  1040. * equal to the value that @right evaluates to. This is the same as
  1041. * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
  1042. * KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1043. */
  1044. #define KUNIT_ASSERT_LE(test, left, right) \
  1045. KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
  1046. #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
  1047. KUNIT_BINARY_INT_ASSERTION(test, \
  1048. KUNIT_ASSERTION, \
  1049. left, <=, right, \
  1050. fmt, \
  1051. ##__VA_ARGS__)
  1052. /**
  1053. * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
  1054. * @test: The test context object.
  1055. * @left: an arbitrary expression that evaluates to a primitive C type.
  1056. * @right: an arbitrary expression that evaluates to a primitive C type.
  1057. *
  1058. * Sets an assertion that the value that @left evaluates to is greater than the
  1059. * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
  1060. * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
  1061. * is not met.
  1062. */
  1063. #define KUNIT_ASSERT_GT(test, left, right) \
  1064. KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
  1065. #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
  1066. KUNIT_BINARY_INT_ASSERTION(test, \
  1067. KUNIT_ASSERTION, \
  1068. left, >, right, \
  1069. fmt, \
  1070. ##__VA_ARGS__)
  1071. /**
  1072. * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
  1073. * @test: The test context object.
  1074. * @left: an arbitrary expression that evaluates to a primitive C type.
  1075. * @right: an arbitrary expression that evaluates to a primitive C type.
  1076. *
  1077. * Sets an assertion that the value that @left evaluates to is greater than the
  1078. * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
  1079. * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
  1080. * is not met.
  1081. */
  1082. #define KUNIT_ASSERT_GE(test, left, right) \
  1083. KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
  1084. #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
  1085. KUNIT_BINARY_INT_ASSERTION(test, \
  1086. KUNIT_ASSERTION, \
  1087. left, >=, right, \
  1088. fmt, \
  1089. ##__VA_ARGS__)
  1090. /**
  1091. * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
  1092. * @test: The test context object.
  1093. * @left: an arbitrary expression that evaluates to a null terminated string.
  1094. * @right: an arbitrary expression that evaluates to a null terminated string.
  1095. *
  1096. * Sets an assertion that the values that @left and @right evaluate to are
  1097. * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
  1098. * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1099. */
  1100. #define KUNIT_ASSERT_STREQ(test, left, right) \
  1101. KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
  1102. #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
  1103. KUNIT_BINARY_STR_ASSERTION(test, \
  1104. KUNIT_ASSERTION, \
  1105. left, ==, right, \
  1106. fmt, \
  1107. ##__VA_ARGS__)
  1108. /**
  1109. * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
  1110. * @test: The test context object.
  1111. * @left: an arbitrary expression that evaluates to a null terminated string.
  1112. * @right: an arbitrary expression that evaluates to a null terminated string.
  1113. *
  1114. * Sets an expectation that the values that @left and @right evaluate to are
  1115. * not equal. This is semantically equivalent to
  1116. * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
  1117. * for more information.
  1118. */
  1119. #define KUNIT_ASSERT_STRNEQ(test, left, right) \
  1120. KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
  1121. #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
  1122. KUNIT_BINARY_STR_ASSERTION(test, \
  1123. KUNIT_ASSERTION, \
  1124. left, !=, right, \
  1125. fmt, \
  1126. ##__VA_ARGS__)
  1127. /**
  1128. * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
  1129. * @test: The test context object.
  1130. * @ptr: an arbitrary pointer.
  1131. *
  1132. * Sets an assertion that the values that @ptr evaluates to is null. This is
  1133. * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
  1134. * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1135. */
  1136. #define KUNIT_ASSERT_NULL(test, ptr) \
  1137. KUNIT_ASSERT_NULL_MSG(test, \
  1138. ptr, \
  1139. NULL)
  1140. #define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
  1141. KUNIT_BINARY_PTR_ASSERTION(test, \
  1142. KUNIT_ASSERTION, \
  1143. ptr, ==, NULL, \
  1144. fmt, \
  1145. ##__VA_ARGS__)
  1146. /**
  1147. * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
  1148. * @test: The test context object.
  1149. * @ptr: an arbitrary pointer.
  1150. *
  1151. * Sets an assertion that the values that @ptr evaluates to is not null. This
  1152. * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
  1153. * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1154. */
  1155. #define KUNIT_ASSERT_NOT_NULL(test, ptr) \
  1156. KUNIT_ASSERT_NOT_NULL_MSG(test, \
  1157. ptr, \
  1158. NULL)
  1159. #define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
  1160. KUNIT_BINARY_PTR_ASSERTION(test, \
  1161. KUNIT_ASSERTION, \
  1162. ptr, !=, NULL, \
  1163. fmt, \
  1164. ##__VA_ARGS__)
  1165. /**
  1166. * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
  1167. * @test: The test context object.
  1168. * @ptr: an arbitrary pointer.
  1169. *
  1170. * Sets an assertion that the value that @ptr evaluates to is not null and not
  1171. * an errno stored in a pointer. This is the same as
  1172. * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
  1173. * KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1174. */
  1175. #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
  1176. KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
  1177. #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
  1178. KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
  1179. KUNIT_ASSERTION, \
  1180. ptr, \
  1181. fmt, \
  1182. ##__VA_ARGS__)
  1183. /**
  1184. * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
  1185. * @name: prefix for the test parameter generator function.
  1186. * @array: array of test parameters.
  1187. * @get_desc: function to convert param to description; NULL to use default
  1188. *
  1189. * Define function @name_gen_params which uses @array to generate parameters.
  1190. */
  1191. #define KUNIT_ARRAY_PARAM(name, array, get_desc) \
  1192. static const void *name##_gen_params(const void *prev, char *desc) \
  1193. { \
  1194. typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
  1195. if (__next - (array) < ARRAY_SIZE((array))) { \
  1196. void (*__get_desc)(typeof(__next), char *) = get_desc; \
  1197. if (__get_desc) \
  1198. __get_desc(__next, desc); \
  1199. return __next; \
  1200. } \
  1201. return NULL; \
  1202. }
  1203. #ifdef CONFIG_SEC_KUNIT
  1204. /*
  1205. * for mock feature from kunit/alpha/master
  1206. */
  1207. struct test_initcall {
  1208. struct list_head node;
  1209. int (*init)(struct test_initcall *this, struct kunit *test);
  1210. void (*exit)(struct test_initcall *this);
  1211. };
  1212. struct kunit_post_condition {
  1213. struct list_head node;
  1214. void (*validate)(struct kunit_post_condition *condition);
  1215. };
  1216. void test_install_initcall(struct test_initcall *initcall);
  1217. #define test_pure_initcall(fn) postcore_initcall(fn)
  1218. #define test_register_initcall(initcall) \
  1219. static int register_test_initcall_##initcall(void) \
  1220. { \
  1221. test_install_initcall(&initcall); \
  1222. \
  1223. return 0; \
  1224. } \
  1225. test_pure_initcall(register_test_initcall_##initcall)
  1226. #endif /* CONFIG_SEC_KUNIT */
  1227. // TODO(dlatypov@google.com): consider eventually migrating users to explicitly
  1228. // include resource.h themselves if they need it.
  1229. #include <kunit/resource.h>
  1230. #endif /* _KUNIT_TEST_H */