mock-test.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * KUnit test for mock.h.
  4. *
  5. * Copyright (C) 2020, Google LLC.
  6. * Author: Brendan Higgins <[email protected]>
  7. */
  8. #include <kunit/test.h>
  9. #include <kunit/mock.h>
  10. // A simple class for unit-testing/example purposes.
  11. struct adder {
  12. int (*add)(struct adder *adder, int x, int y);
  13. };
  14. static int real_add(struct adder *adder, int x, int y)
  15. {
  16. return x + y;
  17. }
  18. static void adder_real_init(struct adder *adder)
  19. {
  20. adder->add = real_add;
  21. }
  22. DECLARE_STRUCT_CLASS_MOCK_PREREQS(adder);
  23. DEFINE_STRUCT_CLASS_MOCK(METHOD(mock_add), CLASS(adder), RETURNS(int),
  24. PARAMS(struct adder*, int, int));
  25. DECLARE_STRUCT_CLASS_MOCK_INIT(adder);
  26. // This would normally live in the .c file.
  27. static int adder_mock_init(struct kunit *test, struct MOCK(adder) *mock_adder)
  28. {
  29. struct adder *real = mock_get_trgt(mock_adder);
  30. adder_real_init(real);
  31. real->add = mock_add;
  32. mock_set_default_action(mock_get_ctrl(mock_adder),
  33. "mock_add",
  34. mock_add,
  35. kunit_int_return(mock_get_test(mock_adder), 0));
  36. return 0;
  37. }
  38. DEFINE_STRUCT_CLASS_MOCK_INIT(adder, adder_mock_init);
  39. /*
  40. * Note: we create a new `failing_test` so we can validate that failed mock
  41. * expectations mark tests as failed.
  42. * Marking the real `test` as failed is obviously problematic.
  43. *
  44. * See mock_test_failed_expect_call_fails_test for an example.
  45. */
  46. struct mock_test_context {
  47. struct kunit *failing_test;
  48. struct mock *mock;
  49. };
  50. static void mock_test_do_expect_basic(struct kunit *test)
  51. {
  52. struct mock_test_context *ctx = test->priv;
  53. struct mock *mock = ctx->mock;
  54. int param0 = 5, param1 = -4;
  55. static const char * const two_param_types[] = {"int", "int"};
  56. const void *two_params[] = {&param0, &param1};
  57. struct mock_param_matcher *matchers_any_two[] = {
  58. kunit_any(test), kunit_any(test)
  59. };
  60. struct mock_expectation *expectation;
  61. const void *ret;
  62. expectation = mock_add_matcher(mock,
  63. "",
  64. NULL,
  65. matchers_any_two,
  66. ARRAY_SIZE(matchers_any_two));
  67. expectation->action = kunit_int_return(test, 5);
  68. KUNIT_EXPECT_EQ(test, 0, expectation->times_called);
  69. ret = mock->do_expect(mock,
  70. "",
  71. NULL,
  72. two_param_types,
  73. two_params,
  74. ARRAY_SIZE(two_params));
  75. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ret);
  76. KUNIT_EXPECT_EQ(test, 5, *((int *) ret));
  77. KUNIT_EXPECT_EQ(test, 1, expectation->times_called);
  78. }
  79. static void mock_test_ptr_eq(struct kunit *test)
  80. {
  81. struct mock_test_context *ctx = test->priv;
  82. struct kunit *failing_test = ctx->failing_test;
  83. struct mock *mock = ctx->mock;
  84. void *param0 = ctx, *param1 = failing_test;
  85. static const char * const two_param_types[] = {"void *", "void *"};
  86. const void *two_params[] = {&param0, &param1};
  87. struct mock_param_matcher *matchers_two_ptrs[] = {
  88. kunit_ptr_eq(test, param0), kunit_ptr_eq(test, param1)
  89. };
  90. struct mock_expectation *expectation;
  91. const void *ret;
  92. expectation = mock_add_matcher(mock,
  93. "",
  94. NULL,
  95. matchers_two_ptrs,
  96. ARRAY_SIZE(matchers_two_ptrs));
  97. expectation->action = kunit_int_return(test, 0);
  98. KUNIT_EXPECT_EQ(test, 0, expectation->times_called);
  99. ret = mock->do_expect(mock,
  100. "",
  101. NULL,
  102. two_param_types,
  103. two_params,
  104. ARRAY_SIZE(two_params));
  105. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ret);
  106. KUNIT_EXPECT_EQ(test, 1, expectation->times_called);
  107. }
  108. static void mock_test_ptr_eq_not_equal(struct kunit *test)
  109. {
  110. struct mock_test_context *ctx = test->priv;
  111. struct kunit *failing_test = ctx->failing_test;
  112. struct mock *mock = ctx->mock;
  113. /* Pick some two pointers, but pass in different values. */
  114. void *param0 = test, *param1 = failing_test;
  115. static const char * const two_param_types[] = {"void *", "void *"};
  116. const void *two_params[] = {&param0, &param1};
  117. struct mock_param_matcher *matchers_two_ptrs[] = {
  118. kunit_ptr_eq(failing_test, param0),
  119. kunit_ptr_eq(failing_test, param1 - 1)
  120. };
  121. struct mock_expectation *expectation;
  122. const void *ret;
  123. expectation = mock_add_matcher(mock,
  124. "",
  125. NULL,
  126. matchers_two_ptrs,
  127. ARRAY_SIZE(matchers_two_ptrs));
  128. expectation->action = kunit_int_return(failing_test, 0);
  129. KUNIT_EXPECT_EQ(test, 0, expectation->times_called);
  130. ret = mock->do_expect(mock,
  131. "",
  132. NULL,
  133. two_param_types,
  134. two_params,
  135. ARRAY_SIZE(two_params));
  136. KUNIT_EXPECT_FALSE(test, ret);
  137. KUNIT_EXPECT_EQ(test, 0, expectation->times_called);
  138. KUNIT_EXPECT_EQ(test, failing_test->status, KUNIT_FAILURE);
  139. }
  140. /*
  141. * In order for us to be able to rely on KUNIT_EXPECT_CALL to validate other
  142. * behavior, we need to test that unsatisfied KUNIT_EXPECT_CALL causes a test
  143. * failure.
  144. *
  145. * In order to understand what this test is testing we must first understand how
  146. * KUNIT_EXPECT_CALL() works conceptually. In theory, a test specifies that it
  147. * expects some function to be called some number of times (can be zero), with
  148. * some particular arguments. Hence, KUNIT_EXPECT_CALL() must do two things:
  149. *
  150. * 1) Determine whether a function call matches the expectation.
  151. *
  152. * 2) Fail if there are too many or too few matches.
  153. */
  154. static void mock_test_failed_expect_call_fails_test(struct kunit *test)
  155. {
  156. /*
  157. * We do not want to fail the real `test` object used to run this test.
  158. * So we use a separate `failing_test` for KUNIT_EXPECT_CALL().
  159. */
  160. struct mock_test_context *ctx = test->priv;
  161. struct kunit *failing_test = ctx->failing_test;
  162. struct mock *mock = ctx->mock;
  163. /*
  164. * Put an expectation on mock, which we won't satisify.
  165. *
  166. * NOTE: it does not actually matter what function we expect here.
  167. * `mock` does not represent an actual mock on anything; we just need to
  168. * create some expectation, that we won't satisfy.
  169. */
  170. KUNIT_EXPECT_CALL(mock_add(mock,
  171. kunit_any(failing_test),
  172. kunit_any(failing_test)));
  173. /*
  174. * Validate the unsatisfied expectation that we just created. This
  175. * should cause `failing_test` to fail.
  176. */
  177. mock_validate_expectations(mock);
  178. /* Verify that `failing_test` has actually failed. */
  179. KUNIT_EXPECT_EQ(test, failing_test->status, KUNIT_FAILURE);
  180. }
  181. static void mock_test_do_expect_default_return(struct kunit *test)
  182. {
  183. struct mock_test_context *ctx = test->priv;
  184. struct mock *mock = ctx->mock;
  185. int param0 = 5, param1 = -5;
  186. static const char * const two_param_types[] = {"int", "int"};
  187. const void *two_params[] = {&param0, &param1};
  188. struct mock_param_matcher *matchers[] = {
  189. kunit_int_eq(test, 5),
  190. kunit_int_eq(test, -4)
  191. };
  192. struct mock_expectation *expectation;
  193. const void *ret;
  194. expectation = mock_add_matcher(mock,
  195. "add",
  196. mock_add,
  197. matchers,
  198. ARRAY_SIZE(matchers));
  199. expectation->action = kunit_int_return(test, 5);
  200. KUNIT_EXPECT_EQ(test, 0, expectation->times_called);
  201. KUNIT_EXPECT_FALSE(test,
  202. mock_set_default_action(mock,
  203. "add",
  204. mock_add,
  205. kunit_int_return(test, -4)));
  206. ret = mock->do_expect(mock,
  207. "add",
  208. mock_add,
  209. two_param_types,
  210. two_params,
  211. ARRAY_SIZE(two_params));
  212. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ret);
  213. KUNIT_EXPECT_EQ(test, -4, *((int *) ret));
  214. KUNIT_EXPECT_EQ(test, 0, expectation->times_called);
  215. }
  216. /**
  217. * DOC: Testing the failure condition of different mock types.
  218. *
  219. * The following tests will test the behaviour of expectations under different
  220. * conditions. For example, what happens when an expectation:
  221. * - is not satisfied at the end of the test
  222. * - is fulfilled but the expected function is called again
  223. * - a function is called without expectations set on it
  224. *
  225. * For each of these conditions, there may be variations between the different
  226. * types of mocks: nice mocks, naggy mocks (the default) and strict mocks.
  227. *
  228. * More information about these mocks can be found in the kernel documentation
  229. * under Documentation/test/api/class-and-function-mocking
  230. */
  231. /* Method called on strict mock with no expectations will fail */
  232. static void mock_test_strict_no_expectations_will_fail(struct kunit *test)
  233. {
  234. struct mock_test_context *ctx = test->priv;
  235. struct kunit *failing_test = ctx->failing_test;
  236. struct mock *mock = ctx->mock;
  237. int param0 = 5, param1 = -5;
  238. static const char * const two_param_types[] = {"int", "int"};
  239. const void *two_params[] = {&param0, &param1};
  240. mock->type = MOCK_TYPE_STRICT;
  241. mock_set_default_action(mock,
  242. "add",
  243. mock_add,
  244. kunit_int_return(failing_test, -4));
  245. mock->do_expect(mock,
  246. "add",
  247. mock_add,
  248. two_param_types,
  249. two_params,
  250. ARRAY_SIZE(two_params));
  251. mock_validate_expectations(mock);
  252. KUNIT_EXPECT_EQ(test, failing_test->status, KUNIT_FAILURE);
  253. }
  254. /*
  255. * Method called on naggy mock with no expectations will not fail, but will show
  256. * a warning message
  257. */
  258. static void mock_test_naggy_no_expectations_no_fail(struct kunit *test)
  259. {
  260. struct mock_test_context *ctx = test->priv;
  261. struct kunit *failing_test = ctx->failing_test;
  262. struct mock *mock = ctx->mock;
  263. struct mock_expectation *expectation;
  264. int param0 = 5, param1 = -5;
  265. static const char * const two_param_types[] = {"int", "int"};
  266. const void *two_params[] = {&param0, &param1};
  267. mock->type = MOCK_TYPE_NAGGY;
  268. mock_set_default_action(mock,
  269. "add",
  270. real_add,
  271. kunit_int_return(failing_test, -4));
  272. expectation = Never(KUNIT_EXPECT_CALL(mock_add(mock,
  273. kunit_any(failing_test),
  274. kunit_any(failing_test))));
  275. KUNIT_EXPECT_CALL(mock_add(
  276. mock,
  277. kunit_any(failing_test),
  278. kunit_va_format_cmp(failing_test,
  279. kunit_str_contains(failing_test,
  280. "Method was called with no expectations declared"),
  281. kunit_any(failing_test))));
  282. mock->do_expect(mock,
  283. "add",
  284. real_add,
  285. two_param_types,
  286. two_params,
  287. ARRAY_SIZE(two_params));
  288. mock_validate_expectations(mock);
  289. KUNIT_EXPECT_EQ(test, failing_test->status, KUNIT_FAILURE);
  290. }
  291. /* Method called on nice mock with no expectations will do nothing. */
  292. static void mock_test_nice_no_expectations_do_nothing(struct kunit *test)
  293. {
  294. struct mock_test_context *ctx = test->priv;
  295. struct kunit *failing_test = ctx->failing_test;
  296. struct mock *mock = ctx->mock;
  297. int param0 = 5, param1 = -5;
  298. static const char * const two_param_types[] = {"int", "int"};
  299. const void *two_params[] = {&param0, &param1};
  300. mock->type = MOCK_TYPE_NICE;
  301. mock->do_expect(mock,
  302. "add",
  303. mock_add,
  304. two_param_types,
  305. two_params,
  306. ARRAY_SIZE(two_params));
  307. mock_validate_expectations(mock);
  308. KUNIT_EXPECT_EQ(test, failing_test->status, KUNIT_SUCCESS);
  309. }
  310. /* Test that method called on a mock (of any type) with no matching expectations
  311. * will fail test and print all the tried expectations.
  312. */
  313. static void
  314. run_method_called_but_no_matching_expectation_test(struct kunit *test,
  315. enum mock_type mock_type)
  316. {
  317. struct mock_test_context *ctx = test->priv;
  318. struct kunit *failing_test = ctx->failing_test;
  319. struct mock *mock = ctx->mock;
  320. int param0 = 5, param1 = -5;
  321. static const char * const two_param_types[] = {"int", "int"};
  322. const void *two_params[] = {&param0, &param1};
  323. struct mock_param_matcher *two_matchers[] = {
  324. kunit_int_eq(failing_test, 100),
  325. kunit_int_eq(failing_test, 100)
  326. };
  327. mock_add_matcher(mock, "add", mock_add, two_matchers,
  328. ARRAY_SIZE(two_matchers));
  329. mock->type = mock_type;
  330. mock->do_expect(mock, "add", mock_add, two_param_types, two_params,
  331. ARRAY_SIZE(two_params));
  332. /* Even nice mocks should fail if there's an unmet expectation. */
  333. KUNIT_EXPECT_EQ(test, failing_test->status, KUNIT_FAILURE);
  334. }
  335. static void mock_test_naggy_no_matching_expectations_fail(struct kunit *test)
  336. {
  337. run_method_called_but_no_matching_expectation_test(test,
  338. MOCK_TYPE_NAGGY);
  339. }
  340. static void mock_test_strict_no_matching_expectations_fail(struct kunit *test)
  341. {
  342. run_method_called_but_no_matching_expectation_test(test,
  343. MOCK_TYPE_STRICT);
  344. }
  345. static void mock_test_nice_no_matching_expectations_fail(struct kunit *test)
  346. {
  347. run_method_called_but_no_matching_expectation_test(test,
  348. MOCK_TYPE_NICE);
  349. }
  350. static void mock_test_mock_validate_expectations(struct kunit *test)
  351. {
  352. struct mock_test_context *ctx = test->priv;
  353. struct kunit *failing_test = ctx->failing_test;
  354. struct mock *mock = ctx->mock;
  355. struct mock_param_matcher *matchers[] = {
  356. kunit_int_eq(failing_test, 5),
  357. kunit_int_eq(failing_test, -4)
  358. };
  359. struct mock_expectation *expectation;
  360. expectation = mock_add_matcher(mock,
  361. "add",
  362. mock_add,
  363. matchers,
  364. ARRAY_SIZE(matchers));
  365. expectation->times_called = 0;
  366. expectation->min_calls_expected = 1;
  367. expectation->max_calls_expected = 1;
  368. mock_validate_expectations(mock);
  369. KUNIT_EXPECT_EQ(test, failing_test->status, KUNIT_FAILURE);
  370. }
  371. static void mock_test_validate_clears_expectations(struct kunit *test)
  372. {
  373. struct mock_test_context *ctx = test->priv;
  374. struct kunit *failing_test = ctx->failing_test;
  375. struct mock *mock = ctx->mock;
  376. struct mock_param_matcher *matchers[] = {
  377. kunit_int_eq(failing_test, 5),
  378. kunit_int_eq(failing_test, -4)
  379. };
  380. int param0 = 5, param1 = -4;
  381. static const char * const two_param_types[] = {"int", "int"};
  382. const void *two_params[] = {&param0, &param1};
  383. struct mock_expectation *expectation;
  384. mock->type = MOCK_TYPE_STRICT;
  385. expectation = Never(KUNIT_EXPECT_CALL(mock_add(mock,
  386. kunit_any(failing_test),
  387. kunit_any(failing_test))));
  388. /* Add an arbitrary matcher for 0 calls */
  389. expectation = mock_add_matcher(mock, "add", mock_add,
  390. matchers, ARRAY_SIZE(matchers));
  391. expectation->times_called = 0;
  392. expectation->min_calls_expected = 0;
  393. expectation->max_calls_expected = 0;
  394. /* Should have 0 calls and should clear the previous expectation */
  395. mock_validate_expectations(mock);
  396. /* Add a new matcher for 1 call */
  397. expectation = mock_add_matcher(mock, "add", mock_add,
  398. matchers, ARRAY_SIZE(matchers));
  399. expectation->times_called = 0;
  400. expectation->min_calls_expected = 1;
  401. expectation->max_calls_expected = 1;
  402. /* Satisfy previous matcher */
  403. mock->do_expect(mock, "add", mock_add, two_param_types, two_params,
  404. ARRAY_SIZE(two_params));
  405. /*
  406. * Validate previous satisfy; if we didn't clear the previous
  407. * expectation, it would fail the mock_test.
  408. */
  409. mock_validate_expectations(mock);
  410. /* If all goes well, shouldn't fail the test. */
  411. KUNIT_EXPECT_EQ(test, failing_test->status, KUNIT_SUCCESS);
  412. }
  413. static void mock_stub(int a) { }
  414. /* Common references for InSequence tests */
  415. static int param_len = 1;
  416. static const char * const param_type[] = {"int"};
  417. static const void *a_params[] = { &(int){1} };
  418. static const void *b_params[] = { &(int){2} };
  419. static const void *c_params[] = { &(int){3} };
  420. /* Simple test of InSequence, a -> b -> c */
  421. static void mock_test_in_sequence_simple_pass(struct kunit *test)
  422. {
  423. struct mock_test_context *ctx = test->priv;
  424. struct kunit *failing_test = ctx->failing_test;
  425. struct mock *mock = ctx->mock;
  426. struct mock_param_matcher *a_matchers[] = { kunit_int_eq(failing_test, 1) };
  427. struct mock_param_matcher *b_matchers[] = { kunit_int_eq(failing_test, 2) };
  428. struct mock_param_matcher *c_matchers[] = { kunit_int_eq(failing_test, 3) };
  429. struct mock_expectation *c = mock_add_matcher(mock, "c", mock_stub,
  430. c_matchers, param_len);
  431. struct mock_expectation *b = mock_add_matcher(mock, "b", mock_stub,
  432. b_matchers, param_len);
  433. struct mock_expectation *a = mock_add_matcher(mock, "a", mock_stub,
  434. a_matchers, param_len);
  435. InSequence(test, a, b, c);
  436. Never(KUNIT_EXPECT_CALL(mock_add(mock, kunit_any(failing_test),
  437. kunit_any(failing_test))));
  438. mock->do_expect(mock, "a", mock_stub, param_type, a_params, param_len);
  439. mock->do_expect(mock, "b", mock_stub, param_type, b_params, param_len);
  440. mock->do_expect(mock, "c", mock_stub, param_type, c_params, param_len);
  441. mock_validate_expectations(mock);
  442. /* If all goes well, shouldn't fail the test. */
  443. KUNIT_EXPECT_EQ(test, failing_test->status, KUNIT_SUCCESS);
  444. }
  445. static void mock_test_in_sequence_simple_fail(struct kunit *test)
  446. {
  447. struct mock_test_context *ctx = test->priv;
  448. struct kunit *failing_test = ctx->failing_test;
  449. struct mock *mock = ctx->mock;
  450. struct mock_param_matcher *a_matchers[] = { kunit_int_eq(failing_test, 1) };
  451. struct mock_param_matcher *b_matchers[] = { kunit_int_eq(failing_test, 2) };
  452. struct mock_param_matcher *c_matchers[] = { kunit_int_eq(failing_test, 3) };
  453. struct mock_expectation *c = mock_add_matcher(mock, "c", mock_stub,
  454. c_matchers, param_len);
  455. struct mock_expectation *b = mock_add_matcher(mock, "b", mock_stub,
  456. b_matchers, param_len);
  457. struct mock_expectation *a = mock_add_matcher(mock, "a", mock_stub,
  458. a_matchers, param_len);
  459. InSequence(test, a, b, c);
  460. mock->do_expect(mock, "a", mock_stub, param_type, a_params, param_len);
  461. mock->do_expect(mock, "c", mock_stub, param_type, c_params, param_len);
  462. mock->do_expect(mock, "b", mock_stub, param_type, b_params, param_len);
  463. KUNIT_EXPECT_EQ(test, failing_test->status, KUNIT_FAILURE);
  464. }
  465. /* More complex test of InSequence on two chains in v formation:
  466. * a -> c
  467. * b -> c
  468. */
  469. static void mock_test_in_sequence_abc_success(struct kunit *test)
  470. {
  471. struct mock_test_context *ctx = test->priv;
  472. struct kunit *failing_test = ctx->failing_test;
  473. struct mock *mock = ctx->mock;
  474. struct mock_param_matcher *a_matchers[] = { kunit_int_eq(failing_test, 1) };
  475. struct mock_param_matcher *b_matchers[] = { kunit_int_eq(failing_test, 2) };
  476. struct mock_param_matcher *c_matchers[] = { kunit_int_eq(failing_test, 3) };
  477. struct mock_expectation *c = mock_add_matcher(mock, "c", mock_stub,
  478. c_matchers, param_len);
  479. struct mock_expectation *b = mock_add_matcher(mock, "b", mock_stub,
  480. b_matchers, param_len);
  481. struct mock_expectation *a = mock_add_matcher(mock, "a", mock_stub,
  482. a_matchers, param_len);
  483. InSequence(test, a, c);
  484. InSequence(test, b, c);
  485. Never(KUNIT_EXPECT_CALL(mock_add(mock, kunit_any(failing_test),
  486. kunit_any(failing_test))));
  487. mock->do_expect(mock, "a", mock_stub, param_type, a_params, param_len);
  488. mock->do_expect(mock, "b", mock_stub, param_type, b_params, param_len);
  489. mock->do_expect(mock, "c", mock_stub, param_type, c_params, param_len);
  490. /* If all goes well, shouldn't fail the test. */
  491. KUNIT_EXPECT_EQ(test, failing_test->status, KUNIT_SUCCESS);
  492. }
  493. static void mock_test_in_sequence_bac_success(struct kunit *test)
  494. {
  495. struct mock_test_context *ctx = test->priv;
  496. struct kunit *failing_test = ctx->failing_test;
  497. struct mock *mock = ctx->mock;
  498. struct mock_param_matcher *a_matchers[] = { kunit_int_eq(failing_test, 1) };
  499. struct mock_param_matcher *b_matchers[] = { kunit_int_eq(failing_test, 2) };
  500. struct mock_param_matcher *c_matchers[] = { kunit_int_eq(failing_test, 3) };
  501. struct mock_expectation *c = mock_add_matcher(mock, "c", mock_stub,
  502. c_matchers, param_len);
  503. struct mock_expectation *b = mock_add_matcher(mock, "b", mock_stub,
  504. b_matchers, param_len);
  505. struct mock_expectation *a = mock_add_matcher(mock, "a", mock_stub,
  506. a_matchers, param_len);
  507. InSequence(test, a, c);
  508. InSequence(test, b, c);
  509. Never(KUNIT_EXPECT_CALL(mock_add(mock, kunit_any(failing_test),
  510. kunit_any(failing_test))));
  511. mock->do_expect(mock, "b", mock_stub, param_type, b_params, param_len);
  512. mock->do_expect(mock, "a", mock_stub, param_type, a_params, param_len);
  513. mock->do_expect(mock, "c", mock_stub, param_type, c_params, param_len);
  514. /* If all goes well, shouldn't fail the test. */
  515. KUNIT_EXPECT_EQ(test, failing_test->status, KUNIT_SUCCESS);
  516. }
  517. static void mock_test_in_sequence_no_a_fail(struct kunit *test)
  518. {
  519. struct mock_test_context *ctx = test->priv;
  520. struct kunit *failing_test = ctx->failing_test;
  521. struct mock *mock = ctx->mock;
  522. struct mock_param_matcher *a_matchers[] = { kunit_int_eq(failing_test, 1) };
  523. struct mock_param_matcher *b_matchers[] = { kunit_int_eq(failing_test, 2) };
  524. struct mock_param_matcher *c_matchers[] = { kunit_int_eq(failing_test, 3) };
  525. struct mock_expectation *c = mock_add_matcher(mock, "c", mock_stub,
  526. c_matchers, param_len);
  527. struct mock_expectation *b = mock_add_matcher(mock, "b", mock_stub,
  528. b_matchers, param_len);
  529. struct mock_expectation *a = mock_add_matcher(mock, "a", mock_stub,
  530. a_matchers, param_len);
  531. InSequence(test, a, c);
  532. InSequence(test, b, c);
  533. mock->do_expect(mock, "b", mock_stub, param_type, b_params, param_len);
  534. mock->do_expect(mock, "c", mock_stub, param_type, c_params, param_len);
  535. KUNIT_EXPECT_EQ(test, failing_test->status, KUNIT_FAILURE);
  536. }
  537. static void mock_test_in_sequence_retire_on_saturation(struct kunit *test)
  538. {
  539. struct mock_test_context *ctx = test->priv;
  540. struct kunit *failing_test = ctx->failing_test;
  541. struct mock *mock = ctx->mock;
  542. struct mock_param_matcher *a_matchers[] = { kunit_int_eq(failing_test, 1) };
  543. struct mock_param_matcher *b_matchers[] = { kunit_int_eq(failing_test, 2) };
  544. struct mock_param_matcher *c_matchers[] = { kunit_int_eq(failing_test, 3) };
  545. struct mock_expectation *c = mock_add_matcher(mock, "c", mock_stub,
  546. c_matchers, param_len);
  547. struct mock_expectation *b = mock_add_matcher(mock, "b", mock_stub,
  548. b_matchers, param_len);
  549. struct mock_expectation *a_1 = mock_add_matcher(mock, "a", mock_stub,
  550. a_matchers, param_len);
  551. struct mock_expectation *a_2 = mock_add_matcher(mock, "a", mock_stub,
  552. a_matchers, param_len);
  553. InSequence(test, a_1, b, a_2, c);
  554. Never(KUNIT_EXPECT_CALL(mock_add(mock, kunit_any(failing_test),
  555. kunit_any(failing_test))));
  556. mock->do_expect(mock, "a", mock_stub, param_type, a_params, param_len);
  557. mock->do_expect(mock, "b", mock_stub, param_type, b_params, param_len);
  558. mock->do_expect(mock, "a", mock_stub, param_type, a_params, param_len);
  559. mock->do_expect(mock, "c", mock_stub, param_type, c_params, param_len);
  560. mock_validate_expectations(mock);
  561. /* If all goes well, shouldn't fail the test. */
  562. KUNIT_EXPECT_EQ(test, failing_test->status, KUNIT_SUCCESS);
  563. }
  564. static void mock_test_atleast(struct kunit *test)
  565. {
  566. struct mock_test_context *ctx = test->priv;
  567. struct kunit *failing_test = ctx->failing_test;
  568. struct mock *mock = ctx->mock;
  569. struct mock_param_matcher *a_matchers[] = { kunit_int_eq(failing_test, 1) };
  570. struct mock_param_matcher *b_matchers[] = { kunit_int_eq(failing_test, 2) };
  571. struct mock_expectation *a = mock_add_matcher(mock, "a", mock_stub,
  572. a_matchers, param_len);
  573. struct mock_expectation *b = mock_add_matcher(mock, "b", mock_stub,
  574. b_matchers, param_len);
  575. AtLeast(2, a);
  576. AtLeast(1, b);
  577. Never(KUNIT_EXPECT_CALL(mock_add(mock, kunit_any(failing_test),
  578. kunit_any(failing_test))));
  579. mock->do_expect(mock, "a", mock_stub, param_type, a_params, param_len);
  580. mock->do_expect(mock, "b", mock_stub, param_type, b_params, param_len);
  581. mock->do_expect(mock, "a", mock_stub, param_type, a_params, param_len);
  582. mock->do_expect(mock, "a", mock_stub, param_type, a_params, param_len);
  583. mock_validate_expectations(mock);
  584. /* If all goes well, shouldn't fail the test. */
  585. KUNIT_EXPECT_EQ(test, failing_test->status, KUNIT_SUCCESS);
  586. }
  587. static void mock_test_atleast_fail(struct kunit *test)
  588. {
  589. struct mock_test_context *ctx = test->priv;
  590. struct kunit *failing_test = ctx->failing_test;
  591. struct mock *mock = ctx->mock;
  592. struct mock_param_matcher *b_matchers[] = { kunit_int_eq(failing_test, 2) };
  593. struct mock_expectation *b = mock_add_matcher(mock, "b", mock_stub,
  594. b_matchers, param_len);
  595. AtLeast(2, b);
  596. mock->do_expect(mock, "b", mock_stub, param_type, b_params, param_len);
  597. mock_validate_expectations(mock);
  598. KUNIT_EXPECT_EQ(test, failing_test->status, KUNIT_FAILURE);
  599. }
  600. static void mock_test_atmost(struct kunit *test)
  601. {
  602. struct mock_test_context *ctx = test->priv;
  603. struct kunit *failing_test = ctx->failing_test;
  604. struct mock *mock = ctx->mock;
  605. struct mock_param_matcher *a_matchers[] = { kunit_int_eq(failing_test, 1) };
  606. struct mock_param_matcher *b_matchers[] = { kunit_int_eq(failing_test, 2) };
  607. struct mock_param_matcher *c_matchers[] = { kunit_int_eq(failing_test, 3) };
  608. struct mock_expectation *a = mock_add_matcher(mock, "a", mock_stub,
  609. a_matchers, param_len);
  610. struct mock_expectation *b = mock_add_matcher(mock, "b", mock_stub,
  611. b_matchers, param_len);
  612. struct mock_expectation *c = mock_add_matcher(mock, "c", mock_stub,
  613. c_matchers, param_len);
  614. AtMost(2, a);
  615. AtMost(1, b);
  616. AtMost(2, c);
  617. Never(KUNIT_EXPECT_CALL(mock_add(mock, kunit_any(failing_test),
  618. kunit_any(failing_test))));
  619. mock->do_expect(mock, "a", mock_stub, param_type, a_params, param_len);
  620. mock->do_expect(mock, "a", mock_stub, param_type, a_params, param_len);
  621. mock->do_expect(mock, "c", mock_stub, param_type, c_params, param_len);
  622. mock_validate_expectations(mock);
  623. /* If all goes well, shouldn't fail the test. */
  624. KUNIT_EXPECT_EQ(test, failing_test->status, KUNIT_SUCCESS);
  625. }
  626. static void mock_test_atmost_fail(struct kunit *test)
  627. {
  628. struct mock_test_context *ctx = test->priv;
  629. struct kunit *failing_test = ctx->failing_test;
  630. struct mock *mock = ctx->mock;
  631. struct mock_param_matcher *b_matchers[] = { kunit_int_eq(failing_test, 2) };
  632. struct mock_expectation *b = mock_add_matcher(mock, "b", mock_stub,
  633. b_matchers, param_len);
  634. AtMost(2, b);
  635. mock->do_expect(mock, "b", mock_stub, param_type, b_params, param_len);
  636. mock->do_expect(mock, "b", mock_stub, param_type, b_params, param_len);
  637. mock->do_expect(mock, "b", mock_stub, param_type, b_params, param_len);
  638. mock_validate_expectations(mock);
  639. KUNIT_EXPECT_EQ(test, failing_test->status, KUNIT_FAILURE);
  640. }
  641. static void mock_test_between(struct kunit *test)
  642. {
  643. struct mock_test_context *ctx = test->priv;
  644. struct kunit *failing_test = ctx->failing_test;
  645. struct mock *mock = ctx->mock;
  646. struct mock_param_matcher *b_matchers[] = { kunit_int_eq(failing_test, 2) };
  647. struct mock_expectation *b = mock_add_matcher(mock, "b", mock_stub,
  648. b_matchers, param_len);
  649. Between(2, 4, b);
  650. Never(KUNIT_EXPECT_CALL(mock_add(mock, kunit_any(failing_test),
  651. kunit_any(failing_test))));
  652. mock->do_expect(mock, "b", mock_stub, param_type, b_params, param_len);
  653. mock->do_expect(mock, "b", mock_stub, param_type, b_params, param_len);
  654. mock->do_expect(mock, "b", mock_stub, param_type, b_params, param_len);
  655. mock_validate_expectations(mock);
  656. /* If all goes well, shouldn't fail the test. */
  657. KUNIT_EXPECT_EQ(test, failing_test->status, KUNIT_SUCCESS);
  658. }
  659. static void mock_test_between_fail(struct kunit *test)
  660. {
  661. struct mock_test_context *ctx = test->priv;
  662. struct kunit *failing_test = ctx->failing_test;
  663. struct mock *mock = ctx->mock;
  664. struct mock_param_matcher *a_matchers[] = { kunit_int_eq(failing_test, 1) };
  665. struct mock_param_matcher *b_matchers[] = { kunit_int_eq(failing_test, 2) };
  666. struct mock_expectation *a = mock_add_matcher(mock, "a", mock_stub,
  667. a_matchers, param_len);
  668. struct mock_expectation *b = mock_add_matcher(mock, "b", mock_stub,
  669. b_matchers, param_len);
  670. Between(2, 3, a);
  671. Between(1, 2, b);
  672. Times(2, KUNIT_EXPECT_CALL(mock_add(mock,
  673. kunit_any(failing_test),
  674. kunit_any(failing_test))));
  675. mock->do_expect(mock, "a", mock_stub, param_type, a_params, param_len);
  676. mock->do_expect(mock, "b", mock_stub, param_type, b_params, param_len);
  677. mock->do_expect(mock, "b", mock_stub, param_type, b_params, param_len);
  678. mock->do_expect(mock, "b", mock_stub, param_type, b_params, param_len);
  679. mock_validate_expectations(mock);
  680. KUNIT_EXPECT_EQ(test, failing_test->status, KUNIT_FAILURE);
  681. }
  682. static int mock_test_init(struct kunit *test)
  683. {
  684. struct mock_test_context *ctx;
  685. ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
  686. if (!ctx)
  687. return -ENOMEM;
  688. test->priv = ctx;
  689. ctx->failing_test = kunit_kzalloc(test, sizeof(*ctx->failing_test),
  690. GFP_KERNEL);
  691. if (!ctx->failing_test)
  692. return -EINVAL;
  693. kunit_init_test(ctx->failing_test, NULL, NULL);
  694. ctx->mock = kunit_kzalloc(test, sizeof(*ctx->mock), GFP_KERNEL);
  695. if (!ctx->mock)
  696. return -ENOMEM;
  697. mock_init_ctrl(ctx->failing_test, ctx->mock);
  698. return 0;
  699. }
  700. static void mock_test_exit(struct kunit *test)
  701. {
  702. struct mock_test_context *ctx = test->priv;
  703. kunit_cleanup(ctx->failing_test);
  704. }
  705. static struct kunit_case mock_test_cases[] = {
  706. KUNIT_CASE(mock_test_do_expect_basic),
  707. KUNIT_CASE(mock_test_ptr_eq),
  708. KUNIT_CASE(mock_test_ptr_eq_not_equal),
  709. KUNIT_CASE(mock_test_failed_expect_call_fails_test),
  710. KUNIT_CASE(mock_test_do_expect_default_return),
  711. KUNIT_CASE(mock_test_mock_validate_expectations),
  712. KUNIT_CASE(mock_test_strict_no_expectations_will_fail),
  713. KUNIT_CASE(mock_test_naggy_no_expectations_no_fail),
  714. KUNIT_CASE(mock_test_nice_no_expectations_do_nothing),
  715. KUNIT_CASE(mock_test_strict_no_matching_expectations_fail),
  716. KUNIT_CASE(mock_test_naggy_no_matching_expectations_fail),
  717. KUNIT_CASE(mock_test_nice_no_matching_expectations_fail),
  718. KUNIT_CASE(mock_test_validate_clears_expectations),
  719. KUNIT_CASE(mock_test_in_sequence_simple_pass),
  720. KUNIT_CASE(mock_test_in_sequence_simple_fail),
  721. KUNIT_CASE(mock_test_in_sequence_abc_success),
  722. KUNIT_CASE(mock_test_in_sequence_bac_success),
  723. KUNIT_CASE(mock_test_in_sequence_no_a_fail),
  724. KUNIT_CASE(mock_test_in_sequence_retire_on_saturation),
  725. KUNIT_CASE(mock_test_atleast),
  726. KUNIT_CASE(mock_test_atleast_fail),
  727. KUNIT_CASE(mock_test_atmost),
  728. KUNIT_CASE(mock_test_atmost_fail),
  729. KUNIT_CASE(mock_test_between),
  730. KUNIT_CASE(mock_test_between_fail),
  731. {}
  732. };
  733. static struct kunit_suite mock_test_suite = {
  734. .name = "mock-test",
  735. .init = mock_test_init,
  736. .exit = mock_test_exit,
  737. .test_cases = mock_test_cases,
  738. };
  739. kunit_test_suite(mock_test_suite);