mock.h 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Mocking API for KUnit.
  4. *
  5. * Copyright (C) 2020, Google LLC.
  6. * Author: Brendan Higgins <[email protected]>
  7. */
  8. #ifndef _KUNIT_MOCK_H
  9. #define _KUNIT_MOCK_H
  10. #include <linux/types.h>
  11. #include <linux/tracepoint.h> /* For PARAMS(...) */
  12. #include <kunit/test.h>
  13. #include <kunit/kunit-stream.h>
  14. #include <kunit/params.h>
  15. /**
  16. * struct mock_param_matcher - represents a matcher used in a *call expectation*
  17. * @match: the function that performs the matching
  18. *
  19. * The matching function takes a couple of parameters:
  20. *
  21. * - ``this``: refers to the parent struct
  22. * - ``stream``: a &kunit_stream to which a detailed message should be added as
  23. * to why the parameter matches or not
  24. * - ``param``: a pointer to the parameter to check for a match
  25. *
  26. * The matching function should return whether or not the passed parameter
  27. * matches.
  28. */
  29. struct mock_param_matcher {
  30. bool (*match)(struct mock_param_matcher *this,
  31. struct kunit_stream *stream,
  32. const void *param);
  33. };
  34. #define MOCK_MAX_PARAMS 255
  35. struct mock_matcher {
  36. struct mock_param_matcher *matchers[MOCK_MAX_PARAMS];
  37. int num;
  38. };
  39. /**
  40. * struct mock_action - Represents an action that a mock performs when
  41. * expectation is matched
  42. * @do_action: the action to perform
  43. *
  44. * The action function is given some parameters:
  45. *
  46. * - ``this``: refers to the parent struct
  47. * - ``params``: an array of pointers to the params passed into the mocked
  48. * method or function. **The class argument is excluded for a mocked class
  49. * method.**
  50. * - ``len``: size of ``params``
  51. *
  52. * The action function returns a pointer to the value that the mocked method
  53. * or function should be returning.
  54. */
  55. struct mock_action {
  56. void *(*do_action)(struct mock_action *this,
  57. const void **params,
  58. int len);
  59. };
  60. /**
  61. * struct mock_expectation - represents a *call expectation* on a function.
  62. * @action: A &struct mock_action to perform when the function is called.
  63. * @max_calls_expected: maximum number of times an expectation may be called.
  64. * @min_calls_expected: minimum number of times an expectation may be called.
  65. * @retire_on_saturation: no longer match once ``max_calls_expected`` is
  66. * reached.
  67. *
  68. * Represents a *call expectation* on a function created with
  69. * KUNIT_EXPECT_CALL().
  70. */
  71. struct mock_expectation {
  72. struct mock_action *action;
  73. int max_calls_expected;
  74. int min_calls_expected;
  75. bool retire_on_saturation;
  76. /* private: internal use only. */
  77. const char *expectation_name;
  78. struct list_head node;
  79. struct mock_matcher *matcher;
  80. int times_called;
  81. /* internal list of prerequisites */
  82. struct list_head prerequisites;
  83. };
  84. struct mock_expectation_prereq_entry {
  85. struct mock_expectation *expectation;
  86. struct list_head node;
  87. };
  88. struct mock_method {
  89. struct list_head node;
  90. const char *method_name;
  91. const void *method_ptr;
  92. struct mock_action *default_action;
  93. struct list_head expectations;
  94. };
  95. enum mock_type {
  96. MOCK_TYPE_NICE,
  97. MOCK_TYPE_NAGGY,
  98. MOCK_TYPE_STRICT
  99. };
  100. struct mock {
  101. #ifdef CONFIG_SEC_KUNIT
  102. struct kunit_post_condition parent;
  103. #endif
  104. struct kunit *test;
  105. struct list_head methods;
  106. enum mock_type type;
  107. /* TODO([email protected]): add locking to do_expect. */
  108. const void *(*do_expect)(struct mock *mock,
  109. const char *method_name,
  110. const void *method_ptr,
  111. const char * const *param_types,
  112. const void **params,
  113. int len);
  114. };
  115. #define DEFAULT_MOCK_TYPE MOCK_TYPE_NAGGY
  116. void mock_init_ctrl(struct kunit *test, struct mock *mock);
  117. void mock_validate_expectations(struct mock *mock);
  118. int mock_set_default_action(struct mock *mock,
  119. const char *method_name,
  120. const void *method_ptr,
  121. struct mock_action *action);
  122. struct mock_expectation *mock_add_matcher(struct mock *mock,
  123. const char *method_name,
  124. const void *method_ptr,
  125. struct mock_param_matcher *matchers[],
  126. int len);
  127. struct mock_param_formatter {
  128. struct list_head node;
  129. const char *type_name;
  130. void (*format)(struct mock_param_formatter *formatter,
  131. struct kunit_stream *stream,
  132. const void *param);
  133. };
  134. void mock_register_formatter(struct mock_param_formatter *formatter);
  135. void mock_unregister_formatter(struct mock_param_formatter *formatter);
  136. #define MOCK(name) name##_mock
  137. struct mock *mock_get_global_mock(void);
  138. /**
  139. * STRICT_MOCK() - sets the mock to be strict and returns the mock
  140. * @mock: the mock
  141. *
  142. * For an example, see ``The Nice, the Strict, and the Naggy`` under
  143. * ``Using KUnit``.
  144. */
  145. #define STRICT_MOCK(mock) \
  146. ({ \
  147. mock_get_ctrl(mock)->type = MOCK_TYPE_STRICT; \
  148. mock; \
  149. })
  150. static inline bool is_strict_mock(struct mock *mock)
  151. {
  152. return mock->type == MOCK_TYPE_STRICT;
  153. }
  154. /**
  155. * NICE_MOCK() - sets the mock to be nice and returns the mock
  156. * @mock: the mock
  157. *
  158. * For an example, see ``The Nice, the Strict, and the Naggy`` under
  159. * ``Using KUnit``.
  160. */
  161. #define NICE_MOCK(mock) \
  162. ({ \
  163. mock_get_ctrl(mock)->type = MOCK_TYPE_NICE; \
  164. mock; \
  165. })
  166. static inline bool is_nice_mock(struct mock *mock)
  167. {
  168. return mock->type == MOCK_TYPE_NICE;
  169. }
  170. /**
  171. * NAGGY_MOCK() - sets the mock to be naggy and returns the mock
  172. * @mock: the mock
  173. *
  174. * For an example, see ``The Nice, the Strict, and the Naggy`` under
  175. * ``Using KUnit``.
  176. */
  177. #define NAGGY_MOCK(mock) \
  178. ({ \
  179. mock_get_ctrl(mock)->type = MOCK_TYPE_NAGGY; \
  180. mock; \
  181. })
  182. static inline bool is_naggy_mock(struct mock *mock)
  183. {
  184. return mock->type == MOCK_TYPE_NAGGY;
  185. }
  186. /**
  187. * KUNIT_EXPECT_CALL() - Declares a *call expectation* on a mock function.
  188. * @expectation_call: a mocked method or function with parameters replaced with
  189. * matchers.
  190. *
  191. * Example:
  192. *
  193. * .. code-block:: c
  194. *
  195. * // Class to mock.
  196. * struct example {
  197. * int (*foo)(struct example *, int);
  198. * };
  199. *
  200. * // Define the mock.
  201. * DECLARE_STRUCT_CLASS_MOCK_PREREQS(example);
  202. *
  203. * DEFINE_STRUCT_CLASS_MOCK(METHOD(foo), CLASS(example),
  204. * RETURNS(int),
  205. * PARAMS(struct example *, int));
  206. *
  207. * static int example_init(struct MOCK(example) *mock_example)
  208. * {
  209. * struct example *example = mock_get_trgt(mock_example);
  210. *
  211. * example->foo = foo;
  212. * return 0;
  213. * }
  214. *
  215. * DEFINE_STRUCT_CLASS_MOCK_INIT(example, example_init);
  216. *
  217. * static void foo_example_test_success(struct kunit *test)
  218. * {
  219. * struct MOCK(example) *mock_example;
  220. * struct example *example = mock_get_trgt(mock_example);
  221. * struct mock_expectation *handle;
  222. *
  223. * mock_example = CONSTRUCT_MOCK(example, test);
  224. *
  225. * handle = KUNIT_EXPECT_CALL(foo(mock_get_ctrl(mock_example),
  226. * kunit_int_eq(test, 5)));
  227. * handle->action = int_return(test, 2);
  228. *
  229. * KUNIT_EXPECT_EQ(test, 2, example_bar(example, 5));
  230. * }
  231. *
  232. * Return:
  233. * A &struct mock_expectation representing the call expectation.
  234. * allowing additional conditions and actions to be specified.
  235. */
  236. #define KUNIT_EXPECT_CALL(expectation_call) mock_master_##expectation_call
  237. /**
  238. * Times() - sets the number of times a method is expected be called with the
  239. * matching parameters
  240. * @times: the number of times expected
  241. * @expectation: the expectation to set
  242. *
  243. * Return:
  244. * the same &struct mock_expectation passed in
  245. */
  246. static inline struct mock_expectation *Times(
  247. int times,
  248. struct mock_expectation *expectation)
  249. {
  250. expectation->min_calls_expected = times;
  251. expectation->max_calls_expected = times;
  252. return expectation;
  253. }
  254. /**
  255. * AtLeast() - sets the minimum number of times a method is expected to be
  256. * called with matching parameters
  257. * @times: the minimum number of times expected
  258. * @expectation: the expectation to set
  259. *
  260. * Return:
  261. * the same &struct mock_expectation passed in
  262. */
  263. static inline struct mock_expectation *AtLeast(
  264. int times,
  265. struct mock_expectation *expectation)
  266. {
  267. expectation->min_calls_expected = times;
  268. expectation->max_calls_expected = INT_MAX;
  269. return expectation;
  270. }
  271. /**
  272. * AtMost() - sets the maximum number of times a method is expected to be
  273. * called with matching parameters
  274. * @times: the maximum number of times expected
  275. * @expectation: the expectation to set
  276. *
  277. * Return:
  278. * the same &struct mock_expectation passed in
  279. */
  280. static inline struct mock_expectation *AtMost(
  281. int times,
  282. struct mock_expectation *expectation)
  283. {
  284. expectation->min_calls_expected = 0;
  285. expectation->max_calls_expected = times;
  286. return expectation;
  287. }
  288. /**
  289. * Between() - sets the minimum and maximum number of times a method is
  290. * expected to be called with matching parameters
  291. * @min_times: the minimum number of times expected
  292. * @max_times: the maximum number of times expected
  293. * @expectation: the expectation to set
  294. *
  295. * Return:
  296. * the same &struct mock_expectation passed in
  297. */
  298. static inline struct mock_expectation *Between(
  299. int min_times,
  300. int max_times,
  301. struct mock_expectation *expectation)
  302. {
  303. expectation->min_calls_expected = min_times;
  304. expectation->max_calls_expected = max_times;
  305. return expectation;
  306. }
  307. /**
  308. * Never() - alias for Times(0)
  309. * @expectation: the expectation to set
  310. *
  311. * Return:
  312. * the same &struct mock_expectation passed in
  313. */
  314. static inline struct mock_expectation *Never(
  315. struct mock_expectation *expectation)
  316. {
  317. return Times(0, expectation);
  318. }
  319. /**
  320. * RetireOnSaturation() - sets the expectation to retire on saturation
  321. * @expectation: the expectation to set
  322. *
  323. * Return:
  324. * the same &struct mock_expectation passed in
  325. */
  326. static inline struct mock_expectation *RetireOnSaturation(
  327. struct mock_expectation *expectation)
  328. {
  329. expectation->retire_on_saturation = true;
  330. return expectation;
  331. }
  332. /**
  333. * ActionOnMatch() - sets a action of the expectation when matched
  334. * @expectation: the expectation to set the action of
  335. * @action: action to perform when expectation matches
  336. *
  337. * Example:
  338. *
  339. * .. code-block:: c
  340. *
  341. * ActionOnMatch(EXPECT_CALL(...), INVOKE_REAL(test, ...));
  342. *
  343. * Return:
  344. * the same &struct mock_expectation passed in
  345. */
  346. static inline struct mock_expectation *ActionOnMatch(
  347. struct mock_expectation *expectation,
  348. struct mock_action *action)
  349. {
  350. expectation->action = action;
  351. return expectation;
  352. }
  353. /**
  354. * InSequence() - defines an order for expectations to be matched
  355. * @test: the test, used for internal resource allocations
  356. * @first: the first &struct mock_expectation in the sequence
  357. * @...: the rest of the expectations in order following
  358. *
  359. * Example:
  360. *
  361. * .. code-block:: c
  362. *
  363. * struct mock_expectation *a = EXPECT_CALL(...);
  364. * struct mock_expectation *b = EXPECT_CALL(...);
  365. * struct mock_expectation *c = EXPECT_CALL(...);
  366. *
  367. * InSequence(test, a, b, c);
  368. *
  369. * Return:
  370. * 0 if everything was successful, otherwise a memory allocation error
  371. */
  372. #define InSequence(test, first, ...) \
  373. mock_in_sequence((struct kunit *)test, first, __VA_ARGS__, 0)
  374. int mock_in_sequence(struct kunit *test, struct mock_expectation *first, ...);
  375. #define mock_get_ctrl_internal(mock_object) (&(mock_object)->ctrl)
  376. #define mock_get_ctrl(mock_object) mock_get_ctrl_internal(mock_object)
  377. #define mock_get_trgt_internal(mock_object) (&(mock_object)->trgt)
  378. #define mock_get_trgt(mock_object) mock_get_trgt_internal(mock_object)
  379. #define mock_get_test(mock_object) (mock_get_ctrl(mock_object)->test)
  380. #define CLASS(struct_name) struct_name
  381. #define HANDLE_INDEX(index) index
  382. #define METHOD(method_name) method_name
  383. #define RETURNS(return_type) return_type
  384. /* #define PARAMS(...) __VA_ARGS__ included by linux/tracepoint.h */
  385. #define MOCK_INIT_ID(struct_name) struct_name##mock_init
  386. #define REAL_ID(func_name) __real__##func_name
  387. #define INVOKE_ID(func_name) __invoke__##func_name
  388. #define DECLARE_MOCK_CLIENT(name, return_type, param_types...) \
  389. return_type name(PARAM_LIST_FROM_TYPES(param_types))
  390. #define DECLARE_MOCK_MASTER(name, ctrl_index, param_types...) \
  391. struct mock_expectation *mock_master_##name( \
  392. MATCHER_PARAM_LIST_FROM_TYPES(ctrl_index, \
  393. param_types))
  394. #define DECLARE_MOCK_COMMON(name, handle_index, return_type, param_types...) \
  395. DECLARE_MOCK_CLIENT(name, return_type, param_types); \
  396. DECLARE_MOCK_MASTER(name, handle_index, param_types)
  397. #define DECLARE_REDIRECT_MOCKABLE(name, return_type, param_types...) \
  398. return_type REAL_ID(name)(param_types); \
  399. return_type name(param_types); \
  400. void *INVOKE_ID(name)(struct kunit *test, \
  401. const void *params[], \
  402. int len)
  403. #define DECLARE_MOCK_FUNC_CLIENT(name, return_type, param_types...) \
  404. DECLARE_MOCK_CLIENT(name, return_type, param_types)
  405. #define DECLARE_MOCK_FUNC_MASTER(name, param_types...) \
  406. DECLARE_MOCK_MASTER(name, MOCK_MAX_PARAMS, param_types)
  407. #define DECLARE_STRUCT_CLASS_MOCK_STRUCT(struct_name) \
  408. struct MOCK(struct_name) { \
  409. struct mock ctrl; \
  410. struct struct_name trgt; \
  411. }
  412. #define DECLARE_STRUCT_CLASS_MOCK_CONVERTER(struct_name) \
  413. static inline struct mock *from_##struct_name##_to_mock( \
  414. const struct struct_name *trgt) \
  415. { \
  416. return mock_get_ctrl( \
  417. container_of(trgt, \
  418. struct MOCK(struct_name), \
  419. trgt)); \
  420. }
  421. /**
  422. * DECLARE_STRUCT_CLASS_MOCK_PREREQS() - Create a mock child class
  423. * @struct_name: name of the class/struct to be mocked
  424. *
  425. * Creates a mock child class of ``struct_name`` named
  426. * ``struct MOCK(struct_name)`` along with supporting internally used methods.
  427. *
  428. * See KUNIT_EXPECT_CALL() for example usages.
  429. */
  430. #define DECLARE_STRUCT_CLASS_MOCK_PREREQS(struct_name) \
  431. DECLARE_STRUCT_CLASS_MOCK_STRUCT(struct_name); \
  432. DECLARE_STRUCT_CLASS_MOCK_CONVERTER(struct_name)
  433. #define DECLARE_STRUCT_CLASS_MOCK_HANDLE_INDEX_INTERNAL(name, \
  434. struct_name, \
  435. handle_index, \
  436. return_type, \
  437. param_types...) \
  438. DECLARE_MOCK_COMMON(name, \
  439. handle_index, \
  440. return_type, \
  441. param_types)
  442. #define DECLARE_STRUCT_CLASS_MOCK_HANDLE_INDEX(name, \
  443. struct_name, \
  444. handle_index, \
  445. return_type, \
  446. param_types...) \
  447. DECLARE_STRUCT_CLASS_MOCK_HANDLE_INDEX_INTERNAL(name, \
  448. struct_name, \
  449. handle_index, \
  450. return_type, \
  451. param_types)
  452. /**
  453. * DECLARE_STRUCT_CLASS_MOCK()
  454. * @name: method name
  455. * @struct_name: name of the class/struct
  456. * @return_type: return type of the method
  457. * @param_types: parameters of the method
  458. *
  459. * Same as DEFINE_STRUCT_CLASS_MOCK(), but only makes header compatible
  460. * declarations.
  461. */
  462. #define DECLARE_STRUCT_CLASS_MOCK(name, \
  463. struct_name, \
  464. return_type, \
  465. param_types...) \
  466. DECLARE_STRUCT_CLASS_MOCK_HANDLE_INDEX(name, \
  467. struct_name, \
  468. 0, \
  469. return_type, \
  470. param_types)
  471. /**
  472. * DECLARE_STRUCT_CLASS_MOCK_VOID_RETURN()
  473. * @name: method name
  474. * @struct_name: name of the class/struct
  475. * @param_types: parameters of the method
  476. *
  477. * Same as DEFINE_STRUCT_CLASS_MOCK_VOID_RETURN(), but only makes header
  478. * compatible declarations.
  479. */
  480. #define DECLARE_STRUCT_CLASS_MOCK_VOID_RETURN(name, \
  481. struct_name, \
  482. param_types...) \
  483. DECLARE_STRUCT_CLASS_MOCK_HANDLE_INDEX(name, \
  484. struct_name, \
  485. 0, \
  486. void, \
  487. param_types)
  488. /**
  489. * DECLARE_STRUCT_CLASS_MOCK_INIT()
  490. * @struct_name: name of the class/struct
  491. *
  492. * Same as DEFINE_STRUCT_CLASS_MOCK_INIT(), but only makes header compatible
  493. * declarations.
  494. */
  495. #define DECLARE_STRUCT_CLASS_MOCK_INIT(struct_name) \
  496. struct MOCK(struct_name) *MOCK_INIT_ID(struct_name)( \
  497. struct kunit *test)
  498. #define DECLARE_VOID_CLASS_MOCK_HANDLE_INDEX_INTERNAL(name, \
  499. handle_index, \
  500. return_type, \
  501. param_types...) \
  502. DECLARE_MOCK_COMMON(name, \
  503. handle_index, \
  504. return_type, \
  505. param_types)
  506. #define DECLARE_VOID_CLASS_MOCK_HANDLE_INDEX(name, \
  507. handle_index, \
  508. return_type, \
  509. param_types...) \
  510. DECLARE_VOID_CLASS_MOCK_HANDLE_INDEX_INTERNAL(name, \
  511. handle_index, \
  512. return_type, \
  513. param_types)
  514. /**
  515. * CONSTRUCT_MOCK()
  516. * @struct_name: name of the class
  517. * @test: associated test
  518. *
  519. * Constructs and allocates a test managed ``struct MOCK(struct_name)`` given
  520. * the name of the class for which the mock is defined and a test object.
  521. *
  522. * See KUNIT_EXPECT_CALL() for example usage.
  523. */
  524. #define CONSTRUCT_MOCK(struct_name, test) MOCK_INIT_ID(struct_name)((struct kunit *)test)
  525. #define DECLARE_FUNCTION_MOCK_INTERNAL(name, return_type, param_types...) \
  526. DECLARE_MOCK_FUNC_CLIENT(name, return_type, param_types); \
  527. DECLARE_MOCK_FUNC_MASTER(name, param_types);
  528. #define DECLARE_FUNCTION_MOCK(name, return_type, param_types...) \
  529. DECLARE_FUNCTION_MOCK_INTERNAL(name, return_type, param_types)
  530. #define DECLARE_FUNCTION_MOCK_VOID_RETURN(name, param_types...) \
  531. DECLARE_FUNCTION_MOCK(name, void, param_types)
  532. #define DEFINE_MOCK_CLIENT_COMMON(name, \
  533. handle_index, \
  534. MOCK_SOURCE, \
  535. mock_source_ctx, \
  536. return_type, \
  537. RETURN, \
  538. param_types...) \
  539. return_type name(PARAM_LIST_FROM_TYPES(param_types)) \
  540. { \
  541. struct mock *mock = MOCK_SOURCE(mock_source_ctx, \
  542. handle_index); \
  543. static const char * const param_type_names[] = { \
  544. TYPE_NAMES_FROM_TYPES(handle_index, \
  545. param_types) \
  546. }; \
  547. const void *params[] = { \
  548. PTR_TO_ARG_FROM_TYPES(handle_index, \
  549. param_types) \
  550. }; \
  551. const void *retval; \
  552. \
  553. retval = mock->do_expect(mock, \
  554. #name, \
  555. name, \
  556. param_type_names, \
  557. params, \
  558. ARRAY_SIZE(params)); \
  559. KUNIT_ASSERT_NOT_ERR_OR_NULL(mock->test, retval); \
  560. if (!retval) { \
  561. kunit_info(mock->test, \
  562. "no action installed for "#name"\n");\
  563. BUG(); \
  564. } \
  565. RETURN(return_type, retval); \
  566. }
  567. #if IS_ENABLED(CONFIG_KUNIT)
  568. #define DEFINE_INVOKABLE(name, return_type, RETURN_ASSIGN, param_types...) \
  569. void *INVOKE_ID(name)(struct kunit *test, \
  570. const void *params[], \
  571. int len) { \
  572. return_type *retval; \
  573. \
  574. KUNIT_ASSERT_EQ(test, NUM_VA_ARGS(param_types), len); \
  575. retval = kunit_kzalloc(test, \
  576. sizeof(*retval), \
  577. GFP_KERNEL); \
  578. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, retval); \
  579. RETURN_ASSIGN() REAL_ID(name)( \
  580. ARRAY_ACCESSORS_FROM_TYPES( \
  581. param_types)); \
  582. return retval; \
  583. }
  584. #else
  585. #define DEFINE_INVOKABLE(name, return_type, RETURN_ASSIGN, param_types...)
  586. #endif
  587. #define DEFINE_REDIRECT_MOCKABLE_COMMON(name, \
  588. return_type, \
  589. RETURN_ASSIGN, \
  590. param_types...) \
  591. return_type REAL_ID(name)(param_types); \
  592. return_type name(param_types) __mockable_alias(REAL_ID(name)); \
  593. DEFINE_INVOKABLE(name, return_type, RETURN_ASSIGN, param_types);
  594. #define ASSIGN() *retval =
  595. /**
  596. * DEFINE_REDIRECT_MOCKABLE()
  597. * @name: name of the function
  598. * @return_type: return type of the function
  599. * @param_types: parameter types of the function
  600. *
  601. * Used to define a function which is *redirect-mockable*, which allows the
  602. * function to be mocked and refer to the original definition via
  603. * INVOKE_REAL().
  604. *
  605. * Example:
  606. *
  607. * .. code-block:: c
  608. *
  609. * DEFINE_REDIRECT_MOCKABLE(i2c_add_adapter,
  610. * RETURNS(int), PARAMS(struct i2c_adapter *));
  611. * int REAL_ID(i2c_add_adapter)(struct i2c_adapter *adapter)
  612. * {
  613. * ...
  614. * }
  615. *
  616. * static int aspeed_i2c_test_init(struct kunit *test)
  617. * {
  618. * struct mock_param_capturer *adap_capturer;
  619. * struct mock_expectation *handle;
  620. * struct aspeed_i2c_test *ctx;
  621. * int ret;
  622. *
  623. * ctx = test_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
  624. * if (!ctx)
  625. * return -ENOMEM;
  626. * test->priv = ctx;
  627. *
  628. * handle = EXPECT_CALL(
  629. * i2c_add_adapter(capturer_to_matcher(
  630. * adap_capturer)));
  631. * handle->action = INVOKE_REAL(test, i2c_add_adapter);
  632. * ret = of_fake_probe_platform_by_name(test,
  633. * "aspeed-i2c-bus",
  634. * "test-i2c-bus");
  635. * if (ret < 0)
  636. * return ret;
  637. *
  638. * ASSERT_PARAM_CAPTURED(test, adap_capturer);
  639. * ctx->adap = mock_capturer_get(adap_capturer,
  640. * struct i2c_adapter *);
  641. *
  642. * return 0;
  643. * }
  644. */
  645. #define DEFINE_REDIRECT_MOCKABLE(name, return_type, param_types...) \
  646. DEFINE_REDIRECT_MOCKABLE_COMMON(name, \
  647. return_type, \
  648. ASSIGN, \
  649. param_types)
  650. #define NO_ASSIGN()
  651. #define DEFINE_REDIRECT_MOCKABLE_VOID_RETURN(name, param_types) \
  652. DEFINE_REDIRECT_MOCKABLE_COMMON(name, \
  653. void, \
  654. NO_ASSIGN, \
  655. param_types)
  656. #define CLASS_MOCK_CLIENT_SOURCE(ctx, handle_index) ctx(arg##handle_index)
  657. #define DEFINE_MOCK_METHOD_CLIENT_COMMON(name, \
  658. handle_index, \
  659. mock_converter, \
  660. return_type, \
  661. RETURN, \
  662. param_types...) \
  663. DEFINE_MOCK_CLIENT_COMMON(name, \
  664. handle_index, \
  665. CLASS_MOCK_CLIENT_SOURCE, \
  666. mock_converter, \
  667. return_type, \
  668. RETURN, \
  669. param_types)
  670. #define CAST_AND_RETURN(return_type, retval) return *((return_type *) retval)
  671. #define NO_RETURN(return_type, retval)
  672. #define DEFINE_MOCK_METHOD_CLIENT(name, \
  673. handle_index, \
  674. mock_converter, \
  675. return_type, \
  676. param_types...) \
  677. DEFINE_MOCK_METHOD_CLIENT_COMMON(name, \
  678. handle_index, \
  679. mock_converter, \
  680. return_type, \
  681. CAST_AND_RETURN, \
  682. param_types)
  683. #define DEFINE_MOCK_METHOD_CLIENT_VOID_RETURN(name, \
  684. handle_index, \
  685. mock_converter, \
  686. param_types...) \
  687. DEFINE_MOCK_METHOD_CLIENT_COMMON(name, \
  688. handle_index, \
  689. mock_converter, \
  690. void, \
  691. NO_RETURN, \
  692. param_types)
  693. #define FUNC_MOCK_SOURCE(ctx, handle_index) mock_get_global_mock()
  694. #define DEFINE_MOCK_FUNC_CLIENT_COMMON(name, \
  695. return_type, \
  696. RETURN, \
  697. param_types...) \
  698. DEFINE_MOCK_CLIENT_COMMON(name, \
  699. MOCK_MAX_PARAMS, \
  700. FUNC_MOCK_SOURCE, \
  701. name, \
  702. return_type, \
  703. RETURN, \
  704. param_types)
  705. #define DEFINE_MOCK_FUNC_CLIENT(name, return_type, param_types...) \
  706. DEFINE_MOCK_FUNC_CLIENT_COMMON(name, \
  707. return_type, \
  708. CAST_AND_RETURN, \
  709. param_types)
  710. #define DEFINE_MOCK_FUNC_CLIENT_VOID_RETURN(name, param_types...) \
  711. DEFINE_MOCK_FUNC_CLIENT_COMMON(name, \
  712. void, \
  713. NO_RETURN, \
  714. param_types)
  715. #define DEFINE_MOCK_MASTER_COMMON_INTERNAL(name, \
  716. ctrl_index, \
  717. MOCK_SOURCE, \
  718. param_types...) \
  719. struct mock_expectation *mock_master_##name( \
  720. MATCHER_PARAM_LIST_FROM_TYPES(ctrl_index, \
  721. param_types)) \
  722. { \
  723. struct mock_param_matcher *matchers[] = { \
  724. ARG_NAMES_FROM_TYPES(ctrl_index, param_types) \
  725. }; \
  726. \
  727. return mock_add_matcher(MOCK_SOURCE(ctrl_index), \
  728. #name, \
  729. (const void *) name, \
  730. matchers, \
  731. ARRAY_SIZE(matchers)); \
  732. }
  733. #define DEFINE_MOCK_MASTER_COMMON(name, \
  734. ctrl_index, \
  735. MOCK_SOURCE, \
  736. param_types...) \
  737. DEFINE_MOCK_MASTER_COMMON_INTERNAL(name, \
  738. ctrl_index, \
  739. MOCK_SOURCE, \
  740. param_types)
  741. #define CLASS_MOCK_MASTER_SOURCE(ctrl_index) arg##ctrl_index
  742. #define DEFINE_MOCK_METHOD_MASTER(name, ctrl_index, param_types...) \
  743. DEFINE_MOCK_MASTER_COMMON(name, \
  744. ctrl_index, \
  745. CLASS_MOCK_MASTER_SOURCE, \
  746. param_types)
  747. #define FUNC_MOCK_CLIENT_SOURCE(ctrl_index) mock_get_global_mock()
  748. #define DEFINE_MOCK_FUNC_MASTER(name, param_types...) \
  749. DEFINE_MOCK_MASTER_COMMON(name, \
  750. MOCK_MAX_PARAMS, \
  751. FUNC_MOCK_CLIENT_SOURCE, \
  752. param_types)
  753. #define DEFINE_MOCK_COMMON(name, \
  754. handle_index, \
  755. mock_converter, \
  756. return_type, \
  757. param_types...) \
  758. DEFINE_MOCK_METHOD_CLIENT(name, \
  759. handle_index, \
  760. mock_converter, \
  761. return_type, \
  762. param_types); \
  763. DEFINE_MOCK_METHOD_MASTER(name, handle_index, param_types)
  764. #define DEFINE_MOCK_COMMON_VOID_RETURN(name, \
  765. handle_index, \
  766. mock_converter, \
  767. param_types...) \
  768. DEFINE_MOCK_METHOD_CLIENT_VOID_RETURN(name, \
  769. handle_index, \
  770. mock_converter, \
  771. param_types); \
  772. DEFINE_MOCK_METHOD_MASTER(name, handle_index, param_types)
  773. #define DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX_INTERNAL(name, \
  774. struct_name, \
  775. handle_index, \
  776. return_type, \
  777. param_types...) \
  778. DEFINE_MOCK_COMMON(name, \
  779. handle_index, \
  780. from_##struct_name##_to_mock, \
  781. return_type, param_types)
  782. #define DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX(name, \
  783. struct_name, \
  784. handle_index, \
  785. return_type, \
  786. param_types...) \
  787. DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX_INTERNAL(name, \
  788. struct_name, \
  789. handle_index, \
  790. return_type, \
  791. param_types)
  792. #define DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX_VOID_RETURN_INTERNAL( \
  793. name, \
  794. struct_name, \
  795. handle_index, \
  796. param_types...) \
  797. DEFINE_MOCK_COMMON_VOID_RETURN(name, \
  798. handle_index, \
  799. from_##struct_name##_to_mock, \
  800. param_types)
  801. #define DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX_VOID_RETURN(name, \
  802. struct_name, \
  803. handle_index, \
  804. param_types...) \
  805. DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX_VOID_RETURN_INTERNAL( \
  806. name, \
  807. struct_name, \
  808. handle_index, \
  809. param_types)
  810. /**
  811. * DEFINE_STRUCT_CLASS_MOCK()
  812. * @name: name of the method
  813. * @struct_name: name of the class of which the method belongs
  814. * @return_type: return type of the method to be created. **Must not be void.**
  815. * @param_types: parameters to method to be created.
  816. *
  817. * See KUNIT_EXPECT_CALL() for example usage.
  818. */
  819. #define DEFINE_STRUCT_CLASS_MOCK(name, \
  820. struct_name, \
  821. return_type, \
  822. param_types...) \
  823. DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX(name, \
  824. struct_name, \
  825. 0, \
  826. return_type, \
  827. param_types)
  828. /**
  829. * DEFINE_STRUCT_CLASS_MOCK_VOID_RETURN()
  830. * @name: name of the method
  831. * @struct_name: name of the class of which the method belongs
  832. * @param_types: parameters to method to be created.
  833. *
  834. * Same as DEFINE_STRUCT_CLASS_MOCK() except the method has a ``void`` return
  835. * type.
  836. */
  837. #define DEFINE_STRUCT_CLASS_MOCK_VOID_RETURN(name, struct_name, param_types...)\
  838. DEFINE_STRUCT_CLASS_MOCK_HANDLE_INDEX_VOID_RETURN(name, \
  839. struct_name, \
  840. 0, \
  841. param_types)
  842. /**
  843. * DEFINE_STRUCT_CLASS_MOCK_INIT()
  844. * @struct_name: name of the class
  845. * @init_func: a function of type ``int (*)(struct kunit *, struct MOCK(struct_name) *)``.
  846. * The function is passed a pointer to an allocated, *but not
  847. * initialized*, ``struct MOCK(struct_name)``. The job of this user
  848. * provided function is to perform remaining initialization. Usually
  849. * this entails assigning mock methods to the function pointers in
  850. * the parent struct.
  851. *
  852. * See KUNIT_EXPECT_CALL() for example usage.
  853. */
  854. #define DEFINE_STRUCT_CLASS_MOCK_INIT(struct_name, init_func) \
  855. struct MOCK(struct_name) *MOCK_INIT_ID(struct_name)( \
  856. struct kunit *test) \
  857. { \
  858. struct MOCK(struct_name) *mock_obj; \
  859. \
  860. mock_obj = kunit_kzalloc(test, \
  861. sizeof(*mock_obj), \
  862. GFP_KERNEL); \
  863. if (!mock_obj) \
  864. return NULL; \
  865. \
  866. mock_init_ctrl(test, mock_get_ctrl(mock_obj)); \
  867. \
  868. if (init_func(test, mock_obj)) \
  869. return NULL; \
  870. \
  871. return mock_obj; \
  872. }
  873. struct MOCK(void) {
  874. struct mock ctrl;
  875. void *trgt;
  876. };
  877. static inline struct mock *from_void_ptr_to_mock(const void *ptr)
  878. {
  879. struct MOCK(void) *mock_void_ptr = (void *)ptr;
  880. return mock_get_ctrl(mock_void_ptr);
  881. }
  882. #define DEFINE_VOID_CLASS_MOCK_HANDLE_INDEX_INTERNAL(name, \
  883. handle_index, \
  884. return_type, \
  885. param_types...) \
  886. DEFINE_MOCK_COMMON(name, \
  887. handle_index, \
  888. from_void_ptr_to_mock, \
  889. return_type, \
  890. param_types)
  891. #define DEFINE_VOID_CLASS_MOCK_HANDLE_INDEX(name, \
  892. handle_index, \
  893. return_type, \
  894. param_types...) \
  895. DEFINE_VOID_CLASS_MOCK_HANDLE_INDEX_INTERNAL(name, \
  896. handle_index, \
  897. return_type, \
  898. param_types)
  899. DECLARE_STRUCT_CLASS_MOCK_INIT(void);
  900. #define DEFINE_FUNCTION_MOCK_INTERNAL(name, return_type, param_types...) \
  901. DEFINE_MOCK_FUNC_CLIENT(name, return_type, param_types); \
  902. DEFINE_MOCK_FUNC_MASTER(name, param_types)
  903. /**
  904. * DEFINE_FUNCTION_MOCK()
  905. * @name: name of the function
  906. * @return_type: return type of the function
  907. * @...: parameter types of the function
  908. *
  909. * Same as DEFINE_STRUCT_CLASS_MOCK() except can be used to mock any function
  910. * declared %__mockable or DEFINE_REDIRECT_MOCKABLE()
  911. */
  912. #define DEFINE_FUNCTION_MOCK(name, return_type, param_types...) \
  913. DEFINE_FUNCTION_MOCK_INTERNAL(name, return_type, param_types)
  914. #define DEFINE_FUNCTION_MOCK_VOID_RETURN_INTERNAL(name, param_types...) \
  915. DEFINE_MOCK_FUNC_CLIENT_VOID_RETURN(name, param_types); \
  916. DEFINE_MOCK_FUNC_MASTER(name, param_types)
  917. /**
  918. * DEFINE_FUNCTION_MOCK_VOID_RETURN()
  919. * @name: name of the function
  920. * @...: parameter types of the function
  921. *
  922. * Same as DEFINE_FUNCTION_MOCK() except the method has a ``void`` return
  923. * type.
  924. */
  925. #define DEFINE_FUNCTION_MOCK_VOID_RETURN(name, param_types...) \
  926. DEFINE_FUNCTION_MOCK_VOID_RETURN_INTERNAL(name, param_types)
  927. #if IS_ENABLED(CONFIG_KUNIT)
  928. /**
  929. * __mockable - A function decorator that allows the function to be mocked.
  930. *
  931. * Example:
  932. *
  933. * .. code-block:: c
  934. *
  935. * int __mockable example(int arg) { ... }
  936. */
  937. #define __mockable __weak
  938. #define __mockable_alias(id) __weak __alias(id)
  939. /**
  940. * __visible_for_testing - Makes a static function visible when testing.
  941. *
  942. * A macro that replaces the `static` specifier on functions and global
  943. * variables that is static when compiled normally and visible when compiled for
  944. * tests.
  945. */
  946. #define __visible_for_testing
  947. #else
  948. #define __mockable
  949. #define __mockable_alias(id) __alias(id)
  950. #define __visible_for_testing static
  951. #endif
  952. #define CONVERT_TO_ACTUAL_TYPE(type, ptr) (*((type *) ptr))
  953. /**
  954. * DOC: Built In Matchers
  955. *
  956. * These are the matchers that can be used when matching arguments in
  957. * :c:func:`KUNIT_EXPECT_CALL` (more can be defined manually).
  958. *
  959. * For example, there's a matcher that matches any arguments:
  960. *
  961. * .. code-block:: c
  962. *
  963. * struct mock_param_matcher *any(struct kunit *test);
  964. *
  965. * There are matchers for integers based on the binary condition:
  966. *
  967. * * eq: equals to
  968. * * ne: not equal to
  969. * * lt: less than
  970. * * le: less than or equal to
  971. * * gt: greater than
  972. * * ge: greater than or equal to
  973. *
  974. * .. code-block:: c
  975. *
  976. * struct mock_param_matcher *kunit_int_eq(struct kunit *test, int expected);
  977. * struct mock_param_matcher *kunit_int_ne(struct kunit *test, int expected);
  978. * struct mock_param_matcher *kunit_int_lt(struct kunit *test, int expected);
  979. * struct mock_param_matcher *kunit_int_le(struct kunit *test, int expected);
  980. * struct mock_param_matcher *kunit_int_gt(struct kunit *test, int expected);
  981. * struct mock_param_matcher *kunit_int_ge(struct kunit *test, int expected);
  982. *
  983. * For a detailed list, please see
  984. * ``include/linux/mock.h``.
  985. */
  986. /* Matches any argument */
  987. struct mock_param_matcher *kunit_any(struct kunit *test);
  988. /*
  989. * Matches different types of integers, the argument is compared to the
  990. * `expected` field, based on the comparison defined.
  991. */
  992. struct mock_param_matcher *kunit_u8_eq(struct kunit *test, u8 expected);
  993. struct mock_param_matcher *kunit_u8_ne(struct kunit *test, u8 expected);
  994. struct mock_param_matcher *kunit_u8_le(struct kunit *test, u8 expected);
  995. struct mock_param_matcher *kunit_u8_lt(struct kunit *test, u8 expected);
  996. struct mock_param_matcher *kunit_u8_ge(struct kunit *test, u8 expected);
  997. struct mock_param_matcher *kunit_u8_gt(struct kunit *test, u8 expected);
  998. struct mock_param_matcher *kunit_u16_eq(struct kunit *test, u16 expected);
  999. struct mock_param_matcher *kunit_u16_ne(struct kunit *test, u16 expected);
  1000. struct mock_param_matcher *kunit_u16_le(struct kunit *test, u16 expected);
  1001. struct mock_param_matcher *kunit_u16_lt(struct kunit *test, u16 expected);
  1002. struct mock_param_matcher *kunit_u16_ge(struct kunit *test, u16 expected);
  1003. struct mock_param_matcher *kunit_u16_gt(struct kunit *test, u16 expected);
  1004. struct mock_param_matcher *kunit_u32_eq(struct kunit *test, u32 expected);
  1005. struct mock_param_matcher *kunit_u32_ne(struct kunit *test, u32 expected);
  1006. struct mock_param_matcher *kunit_u32_le(struct kunit *test, u32 expected);
  1007. struct mock_param_matcher *kunit_u32_lt(struct kunit *test, u32 expected);
  1008. struct mock_param_matcher *kunit_u32_ge(struct kunit *test, u32 expected);
  1009. struct mock_param_matcher *kunit_u32_gt(struct kunit *test, u32 expected);
  1010. struct mock_param_matcher *kunit_u64_eq(struct kunit *test, u64 expected);
  1011. struct mock_param_matcher *kunit_u64_ne(struct kunit *test, u64 expected);
  1012. struct mock_param_matcher *kunit_u64_le(struct kunit *test, u64 expected);
  1013. struct mock_param_matcher *kunit_u64_lt(struct kunit *test, u64 expected);
  1014. struct mock_param_matcher *kunit_u64_ge(struct kunit *test, u64 expected);
  1015. struct mock_param_matcher *kunit_u64_gt(struct kunit *test, u64 expected);
  1016. struct mock_param_matcher *kunit_char_eq(struct kunit *test, char expected);
  1017. struct mock_param_matcher *kunit_char_ne(struct kunit *test, char expected);
  1018. struct mock_param_matcher *kunit_char_le(struct kunit *test, char expected);
  1019. struct mock_param_matcher *kunit_char_lt(struct kunit *test, char expected);
  1020. struct mock_param_matcher *kunit_char_ge(struct kunit *test, char expected);
  1021. struct mock_param_matcher *kunit_char_gt(struct kunit *test, char expected);
  1022. struct mock_param_matcher *kunit_uchar_eq(struct kunit *test,
  1023. unsigned char expected);
  1024. struct mock_param_matcher *kunit_uchar_ne(struct kunit *test,
  1025. unsigned char expected);
  1026. struct mock_param_matcher *kunit_uchar_le(struct kunit *test,
  1027. unsigned char expected);
  1028. struct mock_param_matcher *kunit_uchar_lt(struct kunit *test,
  1029. unsigned char expected);
  1030. struct mock_param_matcher *kunit_uchar_ge(struct kunit *test,
  1031. unsigned char expected);
  1032. struct mock_param_matcher *kunit_uchar_gt(struct kunit *test,
  1033. unsigned char expected);
  1034. struct mock_param_matcher *kunit_schar_eq(struct kunit *test,
  1035. signed char expected);
  1036. struct mock_param_matcher *kunit_schar_ne(struct kunit *test,
  1037. signed char expected);
  1038. struct mock_param_matcher *kunit_schar_le(struct kunit *test,
  1039. signed char expected);
  1040. struct mock_param_matcher *kunit_schar_lt(struct kunit *test,
  1041. signed char expected);
  1042. struct mock_param_matcher *kunit_schar_ge(struct kunit *test,
  1043. signed char expected);
  1044. struct mock_param_matcher *kunit_schar_gt(struct kunit *test,
  1045. signed char expected);
  1046. struct mock_param_matcher *kunit_short_eq(struct kunit *test, short expected);
  1047. struct mock_param_matcher *kunit_short_ne(struct kunit *test, short expected);
  1048. struct mock_param_matcher *kunit_short_le(struct kunit *test, short expected);
  1049. struct mock_param_matcher *kunit_short_lt(struct kunit *test, short expected);
  1050. struct mock_param_matcher *kunit_short_ge(struct kunit *test, short expected);
  1051. struct mock_param_matcher *kunit_short_gt(struct kunit *test, short expected);
  1052. struct mock_param_matcher *kunit_ushort_eq(struct kunit *test,
  1053. unsigned short expected);
  1054. struct mock_param_matcher *kunit_ushort_ne(struct kunit *test,
  1055. unsigned short expected);
  1056. struct mock_param_matcher *kunit_ushort_le(struct kunit *test,
  1057. unsigned short expected);
  1058. struct mock_param_matcher *kunit_ushort_lt(struct kunit *test,
  1059. unsigned short expected);
  1060. struct mock_param_matcher *kunit_ushort_ge(struct kunit *test,
  1061. unsigned short expected);
  1062. struct mock_param_matcher *kunit_ushort_gt(struct kunit *test,
  1063. unsigned short expected);
  1064. struct mock_param_matcher *kunit_int_eq(struct kunit *test, int expected);
  1065. struct mock_param_matcher *kunit_int_ne(struct kunit *test, int expected);
  1066. struct mock_param_matcher *kunit_int_lt(struct kunit *test, int expected);
  1067. struct mock_param_matcher *kunit_int_le(struct kunit *test, int expected);
  1068. struct mock_param_matcher *kunit_int_gt(struct kunit *test, int expected);
  1069. struct mock_param_matcher *kunit_int_ge(struct kunit *test, int expected);
  1070. struct mock_param_matcher *kunit_uint_eq(struct kunit *test,
  1071. unsigned int expected);
  1072. struct mock_param_matcher *kunit_uint_ne(struct kunit *test,
  1073. unsigned int expected);
  1074. struct mock_param_matcher *kunit_uint_lt(struct kunit *test,
  1075. unsigned int expected);
  1076. struct mock_param_matcher *kunit_uint_le(struct kunit *test,
  1077. unsigned int expected);
  1078. struct mock_param_matcher *kunit_uint_gt(struct kunit *test,
  1079. unsigned int expected);
  1080. struct mock_param_matcher *kunit_uint_ge(struct kunit *test,
  1081. unsigned int expected);
  1082. struct mock_param_matcher *kunit_long_eq(struct kunit *test, long expected);
  1083. struct mock_param_matcher *kunit_long_ne(struct kunit *test, long expected);
  1084. struct mock_param_matcher *kunit_long_le(struct kunit *test, long expected);
  1085. struct mock_param_matcher *kunit_long_lt(struct kunit *test, long expected);
  1086. struct mock_param_matcher *kunit_long_ge(struct kunit *test, long expected);
  1087. struct mock_param_matcher *kunit_long_gt(struct kunit *test, long expected);
  1088. struct mock_param_matcher *kunit_ulong_eq(struct kunit *test,
  1089. unsigned long expected);
  1090. struct mock_param_matcher *kunit_ulong_ne(struct kunit *test,
  1091. unsigned long expected);
  1092. struct mock_param_matcher *kunit_ulong_le(struct kunit *test,
  1093. unsigned long expected);
  1094. struct mock_param_matcher *kunit_ulong_lt(struct kunit *test,
  1095. unsigned long expected);
  1096. struct mock_param_matcher *kunit_ulong_ge(struct kunit *test,
  1097. unsigned long expected);
  1098. struct mock_param_matcher *kunit_ulong_gt(struct kunit *test,
  1099. unsigned long expected);
  1100. struct mock_param_matcher *kunit_longlong_eq(struct kunit *test,
  1101. long long expected);
  1102. struct mock_param_matcher *kunit_longlong_ne(struct kunit *test,
  1103. long long expected);
  1104. struct mock_param_matcher *kunit_longlong_le(struct kunit *test,
  1105. long long expected);
  1106. struct mock_param_matcher *kunit_longlong_lt(struct kunit *test,
  1107. long long expected);
  1108. struct mock_param_matcher *kunit_longlong_ge(struct kunit *test,
  1109. long long expected);
  1110. struct mock_param_matcher *kunit_longlong_gt(struct kunit *test,
  1111. long long expected);
  1112. struct mock_param_matcher *kunit_ulonglong_eq(struct kunit *test,
  1113. unsigned long long expected);
  1114. struct mock_param_matcher *kunit_ulonglong_ne(struct kunit *test,
  1115. unsigned long long expected);
  1116. struct mock_param_matcher *kunit_ulonglong_le(struct kunit *test,
  1117. unsigned long long expected);
  1118. struct mock_param_matcher *kunit_ulonglong_lt(struct kunit *test,
  1119. unsigned long long expected);
  1120. struct mock_param_matcher *kunit_ulonglong_ge(struct kunit *test,
  1121. unsigned long long expected);
  1122. struct mock_param_matcher *kunit_ulonglong_gt(struct kunit *test,
  1123. unsigned long long expected);
  1124. /* Matches pointers. */
  1125. struct mock_param_matcher *kunit_ptr_eq(struct kunit *test, void *expected);
  1126. struct mock_param_matcher *kunit_ptr_ne(struct kunit *test, void *expected);
  1127. struct mock_param_matcher *kunit_ptr_lt(struct kunit *test, void *expected);
  1128. struct mock_param_matcher *kunit_ptr_le(struct kunit *test, void *expected);
  1129. struct mock_param_matcher *kunit_ptr_gt(struct kunit *test, void *expected);
  1130. struct mock_param_matcher *kunit_ptr_ge(struct kunit *test, void *expected);
  1131. /* Matches memory sections and strings. */
  1132. struct mock_param_matcher *kunit_memeq(struct kunit *test,
  1133. const void *buf,
  1134. size_t size);
  1135. struct mock_param_matcher *kunit_streq(struct kunit *test, const char *str);
  1136. struct mock_param_matcher *kunit_str_contains(struct kunit *test,
  1137. const char *needle);
  1138. /* Matches var-arg arguments. */
  1139. struct mock_param_matcher *kunit_va_format_cmp(
  1140. struct kunit *test,
  1141. struct mock_param_matcher *fmt_matcher,
  1142. struct mock_param_matcher *va_matcher);
  1143. struct mock_action *kunit_bool_return(struct kunit *test, bool ret);
  1144. struct mock_action *kunit_u8_return(struct kunit *test, u8 ret);
  1145. struct mock_action *kunit_u16_return(struct kunit *test, u16 ret);
  1146. struct mock_action *kunit_u32_return(struct kunit *test, u32 ret);
  1147. struct mock_action *kunit_u64_return(struct kunit *test, u64 ret);
  1148. struct mock_action *kunit_char_return(struct kunit *test, char ret);
  1149. struct mock_action *kunit_uchar_return(struct kunit *test, unsigned char ret);
  1150. struct mock_action *kunit_schar_return(struct kunit *test, signed char ret);
  1151. struct mock_action *kunit_short_return(struct kunit *test, short ret);
  1152. struct mock_action *kunit_ushort_return(struct kunit *test, unsigned short ret);
  1153. struct mock_action *kunit_int_return(struct kunit *test, int ret);
  1154. struct mock_action *kunit_uint_return(struct kunit *test, unsigned int ret);
  1155. struct mock_action *kunit_long_return(struct kunit *test, long ret);
  1156. struct mock_action *kunit_ulong_return(struct kunit *test, unsigned long ret);
  1157. struct mock_action *kunit_longlong_return(struct kunit *test, long long ret);
  1158. struct mock_action *kunit_ulonglong_return(struct kunit *test,
  1159. unsigned long long ret);
  1160. struct mock_action *kunit_ptr_return(struct kunit *test, void *ret);
  1161. /**
  1162. * struct mock_struct_matcher_entry - composed with other &struct
  1163. * mock_struct_matcher_entry to make a
  1164. * &struct struct_matcher
  1165. * @member_offset: offset of this member
  1166. * @matcher: matcher for this particular member
  1167. *
  1168. * This is used for struct_cmp() matchers.
  1169. */
  1170. struct mock_struct_matcher_entry {
  1171. size_t member_offset;
  1172. struct mock_param_matcher *matcher;
  1173. };
  1174. static inline void init_mock_struct_matcher_entry_internal(
  1175. struct mock_struct_matcher_entry *entry,
  1176. size_t offset,
  1177. struct mock_param_matcher *matcher)
  1178. {
  1179. entry->member_offset = offset;
  1180. entry->matcher = matcher;
  1181. }
  1182. /**
  1183. * INIT_MOCK_STRUCT_MATCHER_ENTRY()
  1184. * @entry: the &struct mock_struct_matcher_entry to initialize
  1185. * @type: the struct being matched
  1186. * @member: the member of the struct being matched, used to calculate the offset
  1187. * @matcher: matcher to match that member
  1188. *
  1189. * Initializes ``entry`` to match ``type->member`` with ``matcher``.
  1190. */
  1191. #define INIT_MOCK_STRUCT_MATCHER_ENTRY(entry, type, member, matcher) \
  1192. init_mock_struct_matcher_entry_internal(entry, \
  1193. offsetof(type, member),\
  1194. matcher)
  1195. static inline void INIT_MOCK_STRUCT_MATCHER_ENTRY_LAST(
  1196. struct mock_struct_matcher_entry *entry)
  1197. {
  1198. entry->matcher = NULL;
  1199. }
  1200. struct mock_param_matcher *kunit_struct_cmp(
  1201. struct kunit *test,
  1202. const char *struct_name,
  1203. struct mock_struct_matcher_entry *entries);
  1204. /**
  1205. * struct mock_param_capturer - used to capture parameter when matching
  1206. *
  1207. * Use the associated helper macros to access relevant fields.
  1208. * Example:
  1209. *
  1210. * .. code-block::c
  1211. *
  1212. * static int some_test(struct kunit *test)
  1213. * {
  1214. * // imagine a mocked function: int add(int a, int b)
  1215. * struct mock_param_capturer *capturer =
  1216. * mock_int_capturer_create(test, any(test));
  1217. * EXPECT_CALL(add(any(test), capturer_to_matcher(capturer)));
  1218. * ASSERT_PARAM_CAPTURED(test, capturer);
  1219. *
  1220. * int captured_value = mock_capturer_get(capturer, int);
  1221. * }
  1222. */
  1223. struct mock_param_capturer {
  1224. /* private: internal use only. */
  1225. struct mock_param_matcher matcher;
  1226. struct mock_param_matcher *child_matcher;
  1227. void *(*capture_param)(struct kunit *test, const void *param);
  1228. void *captured_param;
  1229. };
  1230. struct mock_param_capturer *mock_param_capturer_create(
  1231. struct kunit *test,
  1232. struct mock_param_matcher *child_matcher,
  1233. void *(*capture_param)(struct kunit *, const void *));
  1234. /**
  1235. * mock_int_capturer_create() - creates a int parameter capturer
  1236. * @test: associated test
  1237. * @child_matcher: matcher used to match the integer
  1238. *
  1239. * The capturer will capture the value if the matcher is satisfied.
  1240. */
  1241. struct mock_param_capturer *mock_int_capturer_create(
  1242. struct kunit *test, struct mock_param_matcher *child_matcher);
  1243. /**
  1244. * mock_int_capturer_create() - creates a generic pointer parameter capturer
  1245. * @test: associated test
  1246. * @child_matcher: matcher used to match the pointer
  1247. *
  1248. * The capturer will capture the value if the matcher is satisfied
  1249. */
  1250. struct mock_param_capturer *mock_ptr_capturer_create(
  1251. struct kunit *test, struct mock_param_matcher *child_matcher);
  1252. /**
  1253. * capturer_to_matcher()
  1254. * @capturer: the param capturer
  1255. *
  1256. * Use this function when passing a capturer into an EXPECT_CALL() where a
  1257. * matcher would be expected. See the example for &struct mock_param_capturer.
  1258. */
  1259. #define capturer_to_matcher(capturer) (&(capturer)->matcher)
  1260. /**
  1261. * ASSERT_PARAM_CAPTURED(): Asserts that the capturer has captured a parameter.
  1262. * @test: the associated test
  1263. * @capturer: the param capturer
  1264. *
  1265. * See &struct mock_param_capturer for an example.
  1266. */
  1267. #define ASSERT_PARAM_CAPTURED(test, capturer) \
  1268. ASSERT((struct kunit *)test, \
  1269. !IS_ERR_OR_NULL((capturer)->captured_param), \
  1270. "Asserted " #capturer " captured param, but did not.")
  1271. /**
  1272. * mock_capturer_get(): Returns the value captured by ``capturer``
  1273. * @capturer: the param capturer
  1274. * @type: the type of the value
  1275. *
  1276. * See &struct mock_param_capturer for an example.
  1277. */
  1278. #define mock_capturer_get(capturer, type) \
  1279. CONVERT_TO_ACTUAL_TYPE(type, (capturer)->captured_param)
  1280. struct mock_action *invoke(struct kunit *test,
  1281. void *(*invokable)(struct kunit *,
  1282. const void *params[],
  1283. int len));
  1284. /**
  1285. * INVOKE_REAL()
  1286. * @test: associated test
  1287. * @func_name: name of the function
  1288. *
  1289. * See DEFINE_REDIRECT_MOCKABLE() for an example.
  1290. *
  1291. * Return: &struct mock_action that makes the associated mock method or function
  1292. * call the original function definition of a redirect-mockable
  1293. * function.
  1294. */
  1295. #define INVOKE_REAL(test, func_name) invoke((struct kunit *)test, INVOKE_ID(func_name))
  1296. struct mock_struct_formatter_entry {
  1297. size_t member_offset;
  1298. struct mock_param_formatter *formatter;
  1299. };
  1300. static inline void init_mock_struct_formatter_entry_internal(
  1301. struct mock_struct_formatter_entry *entry,
  1302. size_t offset,
  1303. struct mock_param_formatter *formatter)
  1304. {
  1305. entry->member_offset = offset;
  1306. entry->formatter = formatter;
  1307. }
  1308. #define INIT_MOCK_STRUCT_FORMATTER_ENTRY(entry, type, member, formatter) \
  1309. init_mock_struct_formatter_entry_internal(entry, \
  1310. offsetof(type, \
  1311. member), \
  1312. formatter)
  1313. static inline void INIT_MOCK_STRUCT_FORMATTER_ENTRY_LAST(
  1314. struct mock_struct_formatter_entry *entry)
  1315. {
  1316. entry->formatter = NULL;
  1317. }
  1318. #define EXPECT_CALL(expectation_call) KUNIT_EXPECT_CALL(expectation_call)
  1319. #define any(test) kunit_any((struct kunit *)test)
  1320. /*
  1321. * Matches different types of integers, the argument is compared to the
  1322. * `expected` field, based on the comparison defined.
  1323. */
  1324. #define u8_eq(test, ...) kunit_u8_eq((struct kunit *)test, ##__VA_ARGS__)
  1325. #define u8_ne(test, ...) kunit_u8_ne((struct kunit *)test, ##__VA_ARGS__)
  1326. #define u8_le(test, ...) kunit_u8_le((struct kunit *)test, ##__VA_ARGS__)
  1327. #define u8_lt(test, ...) kunit_u8_lt((struct kunit *)test, ##__VA_ARGS__)
  1328. #define u8_ge(test, ...) kunit_u8_ge((struct kunit *)test, ##__VA_ARGS__)
  1329. #define u8_gt(test, ...) kunit_u8_gt((struct kunit *)test, ##__VA_ARGS__)
  1330. #define u16_eq(test, ...) kunit_u16_eq((struct kunit *)test, ##__VA_ARGS__)
  1331. #define u16_ne(test, ...) kunit_u16_ne((struct kunit *)test, ##__VA_ARGS__)
  1332. #define u16_le(test, ...) kunit_u16_le((struct kunit *)test, ##__VA_ARGS__)
  1333. #define u16_lt(test, ...) kunit_u16_lt((struct kunit *)test, ##__VA_ARGS__)
  1334. #define u16_ge(test, ...) kunit_u16_ge((struct kunit *)test, ##__VA_ARGS__)
  1335. #define u16_gt(test, ...) kunit_u16_gt((struct kunit *)test, ##__VA_ARGS__)
  1336. #define u32_eq(test, ...) kunit_u32_eq((struct kunit *)test, ##__VA_ARGS__)
  1337. #define u32_ne(test, ...) kunit_u32_ne((struct kunit *)test, ##__VA_ARGS__)
  1338. #define u32_le(test, ...) kunit_u32_le((struct kunit *)test, ##__VA_ARGS__)
  1339. #define u32_lt(test, ...) kunit_u32_lt((struct kunit *)test, ##__VA_ARGS__)
  1340. #define u32_ge(test, ...) kunit_u32_ge((struct kunit *)test, ##__VA_ARGS__)
  1341. #define u32_gt(test, ...) kunit_u32_gt((struct kunit *)test, ##__VA_ARGS__)
  1342. #define u64_eq(test, ...) kunit_u64_eq((struct kunit *)test, ##__VA_ARGS__)
  1343. #define u64_ne(test, ...) kunit_u64_ne((struct kunit *)test, ##__VA_ARGS__)
  1344. #define u64_le(test, ...) kunit_u64_le((struct kunit *)test, ##__VA_ARGS__)
  1345. #define u64_lt(test, ...) kunit_u64_lt((struct kunit *)test, ##__VA_ARGS__)
  1346. #define u64_ge(test, ...) kunit_u64_ge((struct kunit *)test, ##__VA_ARGS__)
  1347. #define u64_gt(test, ...) kunit_u64_gt((struct kunit *)test, ##__VA_ARGS__)
  1348. #define char_eq(test, ...) kunit_char_eq((struct kunit *)test, ##__VA_ARGS__)
  1349. #define char_ne(test, ...) kunit_char_ne((struct kunit *)test, ##__VA_ARGS__)
  1350. #define char_le(test, ...) kunit_char_le((struct kunit *)test, ##__VA_ARGS__)
  1351. #define char_lt(test, ...) kunit_char_lt((struct kunit *)test, ##__VA_ARGS__)
  1352. #define char_ge(test, ...) kunit_char_ge((struct kunit *)test, ##__VA_ARGS__)
  1353. #define char_gt(test, ...) kunit_char_gt((struct kunit *)test, ##__VA_ARGS__)
  1354. #define uchar_eq(test, ...) kunit_uchar_eq((struct kunit *)test, ##__VA_ARGS__)
  1355. #define uchar_ne(test, ...) kunit_uchar_ne((struct kunit *)test, ##__VA_ARGS__)
  1356. #define uchar_le(test, ...) kunit_uchar_le((struct kunit *)test, ##__VA_ARGS__)
  1357. #define uchar_lt(test, ...) kunit_uchar_lt((struct kunit *)test, ##__VA_ARGS__)
  1358. #define uchar_ge(test, ...) kunit_uchar_ge((struct kunit *)test, ##__VA_ARGS__)
  1359. #define uchar_gt(test, ...) kunit_uchar_gt((struct kunit *)test, ##__VA_ARGS__)
  1360. #define schar_eq(test, ...) kunit_schar_eq((struct kunit *)test, ##__VA_ARGS__)
  1361. #define schar_ne(test, ...) kunit_schar_ne((struct kunit *)test, ##__VA_ARGS__)
  1362. #define schar_le(test, ...) kunit_schar_le((struct kunit *)test, ##__VA_ARGS__)
  1363. #define schar_lt(test, ...) kunit_schar_lt((struct kunit *)test, ##__VA_ARGS__)
  1364. #define schar_ge(test, ...) kunit_schar_ge((struct kunit *)test, ##__VA_ARGS__)
  1365. #define schar_gt(test, ...) kunit_schar_gt((struct kunit *)test, ##__VA_ARGS__)
  1366. #define short_eq(test, ...) kunit_short_eq((struct kunit *)test, ##__VA_ARGS__)
  1367. #define short_ne(test, ...) kunit_short_ne((struct kunit *)test, ##__VA_ARGS__)
  1368. #define short_le(test, ...) kunit_short_le((struct kunit *)test, ##__VA_ARGS__)
  1369. #define short_lt(test, ...) kunit_short_lt((struct kunit *)test, ##__VA_ARGS__)
  1370. #define short_ge(test, ...) kunit_short_ge((struct kunit *)test, ##__VA_ARGS__)
  1371. #define short_gt(test, ...) kunit_short_gt((struct kunit *)test, ##__VA_ARGS__)
  1372. #define ushort_eq(test, ...) kunit_ushort_eq((struct kunit *)test, ##__VA_ARGS__)
  1373. #define ushort_ne(test, ...) kunit_ushort_ne((struct kunit *)test, ##__VA_ARGS__)
  1374. #define ushort_le(test, ...) kunit_ushort_le((struct kunit *)test, ##__VA_ARGS__)
  1375. #define ushort_lt(test, ...) kunit_ushort_lt((struct kunit *)test, ##__VA_ARGS__)
  1376. #define ushort_ge(test, ...) kunit_ushort_ge((struct kunit *)test, ##__VA_ARGS__)
  1377. #define ushort_gt(test, ...) kunit_ushort_gt((struct kunit *)test, ##__VA_ARGS__)
  1378. #define int_eq(test, ...) kunit_int_eq((struct kunit *)test, ##__VA_ARGS__)
  1379. #define int_ne(test, ...) kunit_int_ne((struct kunit *)test, ##__VA_ARGS__)
  1380. #define int_lt(test, ...) kunit_int_lt((struct kunit *)test, ##__VA_ARGS__)
  1381. #define int_le(test, ...) kunit_int_le((struct kunit *)test, ##__VA_ARGS__)
  1382. #define int_gt(test, ...) kunit_int_gt((struct kunit *)test, ##__VA_ARGS__)
  1383. #define int_ge(test, ...) kunit_int_ge((struct kunit *)test, ##__VA_ARGS__)
  1384. #define uint_eq(test, ...) kunit_uint_eq((struct kunit *)test, ##__VA_ARGS__)
  1385. #define uint_ne(test, ...) kunit_uint_ne((struct kunit *)test, ##__VA_ARGS__)
  1386. #define uint_lt(test, ...) kunit_uint_lt((struct kunit *)test, ##__VA_ARGS__)
  1387. #define uint_le(test, ...) kunit_uint_le((struct kunit *)test, ##__VA_ARGS__)
  1388. #define uint_gt(test, ...) kunit_uint_gt((struct kunit *)test, ##__VA_ARGS__)
  1389. #define uint_ge(test, ...) kunit_uint_ge((struct kunit *)test, ##__VA_ARGS__)
  1390. #define long_eq(test, ...) kunit_long_eq((struct kunit *)test, ##__VA_ARGS__)
  1391. #define long_ne(test, ...) kunit_long_ne((struct kunit *)test, ##__VA_ARGS__)
  1392. #define long_le(test, ...) kunit_long_le((struct kunit *)test, ##__VA_ARGS__)
  1393. #define long_lt(test, ...) kunit_long_lt((struct kunit *)test, ##__VA_ARGS__)
  1394. #define long_ge(test, ...) kunit_long_ge((struct kunit *)test, ##__VA_ARGS__)
  1395. #define long_gt(test, ...) kunit_long_gt((struct kunit *)test, ##__VA_ARGS__)
  1396. #define ulong_eq(test, ...) kunit_ulong_eq((struct kunit *)test, ##__VA_ARGS__)
  1397. #define ulong_ne(test, ...) kunit_ulong_ne((struct kunit *)test, ##__VA_ARGS__)
  1398. #define ulong_le(test, ...) kunit_ulong_le((struct kunit *)test, ##__VA_ARGS__)
  1399. #define ulong_lt(test, ...) kunit_ulong_lt((struct kunit *)test, ##__VA_ARGS__)
  1400. #define ulong_ge(test, ...) kunit_ulong_ge((struct kunit *)test, ##__VA_ARGS__)
  1401. #define ulong_gt(test, ...) kunit_ulong_gt((struct kunit *)test, ##__VA_ARGS__)
  1402. #define longlong_eq(test, ...) kunit_longlong_eq((struct kunit *)test, ##__VA_ARGS__)
  1403. #define longlong_ne(test, ...) kunit_longlong_ne((struct kunit *)test, ##__VA_ARGS__)
  1404. #define longlong_le(test, ...) kunit_longlong_le((struct kunit *)test, ##__VA_ARGS__)
  1405. #define longlong_lt(test, ...) kunit_longlong_lt((struct kunit *)test, ##__VA_ARGS__)
  1406. #define longlong_ge(test, ...) kunit_longlong_ge((struct kunit *)test, ##__VA_ARGS__)
  1407. #define longlong_gt(test, ...) kunit_longlong_gt((struct kunit *)test, ##__VA_ARGS__)
  1408. #define ulonglong_eq(test, ...) kunit_ulonglong_eq((struct kunit *)test, ##__VA_ARGS__)
  1409. #define ulonglong_ne(test, ...) kunit_ulonglong_ne((struct kunit *)test, ##__VA_ARGS__)
  1410. #define ulonglong_le(test, ...) kunit_ulonglong_le((struct kunit *)test, ##__VA_ARGS__)
  1411. #define ulonglong_lt(test, ...) kunit_ulonglong_lt((struct kunit *)test, ##__VA_ARGS__)
  1412. #define ulonglong_ge(test, ...) kunit_ulonglong_ge((struct kunit *)test, ##__VA_ARGS__)
  1413. #define ulonglong_gt(test, ...) kunit_ulonglong_gt((struct kunit *)test, ##__VA_ARGS__)
  1414. /* matches booleans. */
  1415. #define bool_eq(test, ...) do {} while (0)
  1416. #define bool_ne(test, ...) do {} while (0)
  1417. /* Matches pointers. */
  1418. #define ptr_eq(test, ...) kunit_ptr_eq((struct kunit *)test, ##__VA_ARGS__)
  1419. #define ptr_ne(test, ...) kunit_ptr_ne((struct kunit *)test, ##__VA_ARGS__)
  1420. #define ptr_lt(test, ...) kunit_ptr_lt((struct kunit *)test, ##__VA_ARGS__)
  1421. #define ptr_le(test, ...) kunit_ptr_le((struct kunit *)test, ##__VA_ARGS__)
  1422. #define ptr_gt(test, ...) kunit_ptr_gt((struct kunit *)test, ##__VA_ARGS__)
  1423. #define ptr_ge(test, ...) kunit_ptr_ge((struct kunit *)test, ##__VA_ARGS__)
  1424. /* Matches memory sections and strings. */
  1425. #define memeq(test, ...) kunit_memeq((struct kunit *)test, ##__VA_ARGS__)
  1426. #define streq(test, ...) kunit_streq((struct kunit *)test, ##__VA_ARGS__)
  1427. #define str_contains(test, ...) kunit_str_contains((struct kunit *)test, ##__VA_ARGS__)
  1428. /* Matches var-arg arguments. */
  1429. #define va_format_cmp(test, ...) kunit_va_format_cmp((struct kunit *)test, ##__VA_ARGS__)
  1430. /* Compound matchers */
  1431. #define and(test, ...) do {} while (0)
  1432. #define or(test, ...) do {} while (0)
  1433. #define not(test, ...) do {} while (0)
  1434. #define bool_return(test, ...) kunit_bool_return((struct kunit *)test, ##__VA_ARGS__)
  1435. #define u8_return(test, ...) kunit_u8_return((struct kunit *)test, ##__VA_ARGS__)
  1436. #define u16_return(test, ...) kunit_u16_return((struct kunit *)test, ##__VA_ARGS__)
  1437. #define u32_return(test, ...) kunit_u32_return((struct kunit *)test, ##__VA_ARGS__)
  1438. #define u64_return(test, ...) kunit_u64_return((struct kunit *)test, ##__VA_ARGS__)
  1439. #define char_return(test, ...) kunit_char_return((struct kunit *)test, ##__VA_ARGS__)
  1440. #define uchar_return(test, ...) kunit_uchar_return((struct kunit *)test, ##__VA_ARGS__)
  1441. #define schar_return(test, ...) kunit_schar_return((struct kunit *)test, ##__VA_ARGS__)
  1442. #define short_return(test, ...) kunit_short_return((struct kunit *)test, ##__VA_ARGS__)
  1443. #define ushort_return(test, ...) kunit_ushort_return((struct kunit *)test, ##__VA_ARGS__)
  1444. #define int_return(test, ...) kunit_int_return((struct kunit *)test, ##__VA_ARGS__)
  1445. #define uint_return(test, ...) kunit_uint_return((struct kunit *)test, ##__VA_ARGS__)
  1446. #define long_return(test, ...) kunit_long_return((struct kunit *)test, ##__VA_ARGS__)
  1447. #define ulong_return(test, ...) kunit_ulong_return((struct kunit *)test, ##__VA_ARGS__)
  1448. #define longlong_return(test, ...) kunit_longlong_return((struct kunit *)test, ##__VA_ARGS__)
  1449. #define ulonglong_return(test, ...) kunit_ulonglong_return((struct kunit *)test, ##__VA_ARGS__)
  1450. #define ptr_return(test, ...) kunit_ptr_return((struct kunit *)test, ##__VA_ARGS__)
  1451. #endif /* _KUNIT_MOCK_H */