kunit-example-test.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Example KUnit test to show how to use KUnit.
  4. *
  5. * Copyright (C) 2019, Google LLC.
  6. * Author: Brendan Higgins <brendanhiggins@google.com>
  7. */
  8. #include <kunit/test.h>
  9. #include <kunit/static_stub.h>
  10. /*
  11. * This is the most fundamental element of KUnit, the test case. A test case
  12. * makes a set EXPECTATIONs and ASSERTIONs about the behavior of some code; if
  13. * any expectations or assertions are not met, the test fails; otherwise, the
  14. * test passes.
  15. *
  16. * In KUnit, a test case is just a function with the signature
  17. * `void (*)(struct kunit *)`. `struct kunit` is a context object that stores
  18. * information about the current test.
  19. */
  20. static void example_simple_test(struct kunit *test)
  21. {
  22. /*
  23. * This is an EXPECTATION; it is how KUnit tests things. When you want
  24. * to test a piece of code, you set some expectations about what the
  25. * code should do. KUnit then runs the test and verifies that the code's
  26. * behavior matched what was expected.
  27. */
  28. KUNIT_EXPECT_EQ(test, 1 + 1, 2);
  29. }
  30. /*
  31. * This is run once before each test case, see the comment on
  32. * example_test_suite for more information.
  33. */
  34. static int example_test_init(struct kunit *test)
  35. {
  36. kunit_info(test, "initializing\n");
  37. return 0;
  38. }
  39. /*
  40. * This is run once after each test case, see the comment on
  41. * example_test_suite for more information.
  42. */
  43. static void example_test_exit(struct kunit *test)
  44. {
  45. kunit_info(test, "cleaning up\n");
  46. }
  47. /*
  48. * This is run once before all test cases in the suite.
  49. * See the comment on example_test_suite for more information.
  50. */
  51. static int example_test_init_suite(struct kunit_suite *suite)
  52. {
  53. kunit_info(suite, "initializing suite\n");
  54. return 0;
  55. }
  56. /*
  57. * This is run once after all test cases in the suite.
  58. * See the comment on example_test_suite for more information.
  59. */
  60. static void example_test_exit_suite(struct kunit_suite *suite)
  61. {
  62. kunit_info(suite, "exiting suite\n");
  63. }
  64. /*
  65. * This test should always be skipped.
  66. */
  67. static void example_skip_test(struct kunit *test)
  68. {
  69. /* This line should run */
  70. kunit_info(test, "You should not see a line below.");
  71. /* Skip (and abort) the test */
  72. kunit_skip(test, "this test should be skipped");
  73. /* This line should not execute */
  74. KUNIT_FAIL(test, "You should not see this line.");
  75. }
  76. /*
  77. * This test should always be marked skipped.
  78. */
  79. static void example_mark_skipped_test(struct kunit *test)
  80. {
  81. /* This line should run */
  82. kunit_info(test, "You should see a line below.");
  83. /* Skip (but do not abort) the test */
  84. kunit_mark_skipped(test, "this test should be skipped");
  85. /* This line should run */
  86. kunit_info(test, "You should see this line.");
  87. }
  88. /*
  89. * This test shows off all the types of KUNIT_EXPECT macros.
  90. */
  91. static void example_all_expect_macros_test(struct kunit *test)
  92. {
  93. const u32 array1[] = { 0x0F, 0xFF };
  94. const u32 array2[] = { 0x1F, 0xFF };
  95. /* Boolean assertions */
  96. KUNIT_EXPECT_TRUE(test, true);
  97. KUNIT_EXPECT_FALSE(test, false);
  98. /* Integer assertions */
  99. KUNIT_EXPECT_EQ(test, 1, 1); /* check == */
  100. KUNIT_EXPECT_GE(test, 1, 1); /* check >= */
  101. KUNIT_EXPECT_LE(test, 1, 1); /* check <= */
  102. KUNIT_EXPECT_NE(test, 1, 0); /* check != */
  103. KUNIT_EXPECT_GT(test, 1, 0); /* check > */
  104. KUNIT_EXPECT_LT(test, 0, 1); /* check < */
  105. /* Pointer assertions */
  106. KUNIT_EXPECT_NOT_ERR_OR_NULL(test, test);
  107. KUNIT_EXPECT_PTR_EQ(test, NULL, NULL);
  108. KUNIT_EXPECT_PTR_NE(test, test, NULL);
  109. KUNIT_EXPECT_NULL(test, NULL);
  110. KUNIT_EXPECT_NOT_NULL(test, test);
  111. /* String assertions */
  112. KUNIT_EXPECT_STREQ(test, "hi", "hi");
  113. KUNIT_EXPECT_STRNEQ(test, "hi", "bye");
  114. /* Memory block assertions */
  115. KUNIT_EXPECT_MEMEQ(test, array1, array1, sizeof(array1));
  116. KUNIT_EXPECT_MEMNEQ(test, array1, array2, sizeof(array1));
  117. /*
  118. * There are also ASSERT variants of all of the above that abort test
  119. * execution if they fail. Useful for memory allocations, etc.
  120. */
  121. KUNIT_ASSERT_GT(test, sizeof(char), 0);
  122. /*
  123. * There are also _MSG variants of all of the above that let you include
  124. * additional text on failure.
  125. */
  126. KUNIT_EXPECT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
  127. KUNIT_ASSERT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
  128. }
  129. /* This is a function we'll replace with static stubs. */
  130. static int add_one(int i)
  131. {
  132. /* This will trigger the stub if active. */
  133. KUNIT_STATIC_STUB_REDIRECT(add_one, i);
  134. return i + 1;
  135. }
  136. /* This is used as a replacement for the above function. */
  137. static int subtract_one(int i)
  138. {
  139. /* We don't need to trigger the stub from the replacement. */
  140. return i - 1;
  141. }
  142. /*
  143. * If the function to be replaced is static within a module it is
  144. * useful to export a pointer to that function instead of having
  145. * to change the static function to a non-static exported function.
  146. *
  147. * This pointer simulates a module exporting a pointer to a static
  148. * function.
  149. */
  150. static int (* const add_one_fn_ptr)(int i) = add_one;
  151. /*
  152. * This test shows the use of static stubs.
  153. */
  154. static void example_static_stub_test(struct kunit *test)
  155. {
  156. /* By default, function is not stubbed. */
  157. KUNIT_EXPECT_EQ(test, add_one(1), 2);
  158. /* Replace add_one() with subtract_one(). */
  159. kunit_activate_static_stub(test, add_one, subtract_one);
  160. /* add_one() is now replaced. */
  161. KUNIT_EXPECT_EQ(test, add_one(1), 0);
  162. /* Return add_one() to normal. */
  163. kunit_deactivate_static_stub(test, add_one);
  164. KUNIT_EXPECT_EQ(test, add_one(1), 2);
  165. }
  166. /*
  167. * This test shows the use of static stubs when the function being
  168. * replaced is provided as a pointer-to-function instead of the
  169. * actual function. This is useful for providing access to static
  170. * functions in a module by exporting a pointer to that function
  171. * instead of having to change the static function to a non-static
  172. * exported function.
  173. */
  174. static void example_static_stub_using_fn_ptr_test(struct kunit *test)
  175. {
  176. /* By default, function is not stubbed. */
  177. KUNIT_EXPECT_EQ(test, add_one(1), 2);
  178. /* Replace add_one() with subtract_one(). */
  179. kunit_activate_static_stub(test, add_one_fn_ptr, subtract_one);
  180. /* add_one() is now replaced. */
  181. KUNIT_EXPECT_EQ(test, add_one(1), 0);
  182. /* Return add_one() to normal. */
  183. kunit_deactivate_static_stub(test, add_one_fn_ptr);
  184. KUNIT_EXPECT_EQ(test, add_one(1), 2);
  185. }
  186. static const struct example_param {
  187. int value;
  188. } example_params_array[] = {
  189. { .value = 3, },
  190. { .value = 2, },
  191. { .value = 1, },
  192. { .value = 0, },
  193. };
  194. static void example_param_get_desc(const struct example_param *p, char *desc)
  195. {
  196. snprintf(desc, KUNIT_PARAM_DESC_SIZE, "example value %d", p->value);
  197. }
  198. KUNIT_ARRAY_PARAM(example, example_params_array, example_param_get_desc);
  199. /*
  200. * This test shows the use of params.
  201. */
  202. static void example_params_test(struct kunit *test)
  203. {
  204. const struct example_param *param = test->param_value;
  205. /* By design, param pointer will not be NULL */
  206. KUNIT_ASSERT_NOT_NULL(test, param);
  207. /* Test can be skipped on unsupported param values */
  208. if (!is_power_of_2(param->value))
  209. kunit_skip(test, "unsupported param value %d", param->value);
  210. /* You can use param values for parameterized testing */
  211. KUNIT_EXPECT_EQ(test, param->value % param->value, 0);
  212. }
  213. /*
  214. * This test shows the use of test->priv.
  215. */
  216. static void example_priv_test(struct kunit *test)
  217. {
  218. /* unless setup in suite->init(), test->priv is NULL */
  219. KUNIT_ASSERT_NULL(test, test->priv);
  220. /* but can be used to pass arbitrary data to other functions */
  221. test->priv = kunit_kzalloc(test, 1, GFP_KERNEL);
  222. KUNIT_EXPECT_NOT_NULL(test, test->priv);
  223. KUNIT_ASSERT_PTR_EQ(test, test->priv, kunit_get_current_test()->priv);
  224. }
  225. /*
  226. * This test should always pass. Can be used to practice filtering attributes.
  227. */
  228. static void example_slow_test(struct kunit *test)
  229. {
  230. KUNIT_EXPECT_EQ(test, 1 + 1, 2);
  231. }
  232. /*
  233. * This custom function allocates memory and sets the information we want
  234. * stored in the kunit_resource->data field.
  235. */
  236. static int example_resource_init(struct kunit_resource *res, void *context)
  237. {
  238. int *info = kmalloc_obj(*info);
  239. if (!info)
  240. return -ENOMEM;
  241. *info = *(int *)context;
  242. res->data = info;
  243. return 0;
  244. }
  245. /*
  246. * This function deallocates memory for the kunit_resource->data field.
  247. */
  248. static void example_resource_free(struct kunit_resource *res)
  249. {
  250. kfree(res->data);
  251. }
  252. /*
  253. * This match function is invoked by kunit_find_resource() to locate
  254. * a test resource based on certain criteria.
  255. */
  256. static bool example_resource_alloc_match(struct kunit *test,
  257. struct kunit_resource *res,
  258. void *match_data)
  259. {
  260. return res->data && res->free == example_resource_free;
  261. }
  262. /*
  263. * This is an example of a function that provides a description for each of the
  264. * parameters in a parameterized test.
  265. */
  266. static void example_param_array_get_desc(struct kunit *test, const void *p, char *desc)
  267. {
  268. const struct example_param *param = p;
  269. snprintf(desc, KUNIT_PARAM_DESC_SIZE,
  270. "example check if %d is less than or equal to 3", param->value);
  271. }
  272. /*
  273. * This function gets passed in the parameterized test context i.e. the
  274. * struct kunit belonging to the parameterized test. You can use this function
  275. * to add resources you want shared across the whole parameterized test or
  276. * for additional setup.
  277. */
  278. static int example_param_init(struct kunit *test)
  279. {
  280. int ctx = 3; /* Data to be stored. */
  281. size_t arr_size = ARRAY_SIZE(example_params_array);
  282. /*
  283. * This allocates a struct kunit_resource, sets its data field to
  284. * ctx, and adds it to the struct kunit's resources list. Note that
  285. * this is parameterized test managed. So, it doesn't need to have
  286. * a custom exit function to deallocation as it will get cleaned up at
  287. * the end of the parameterized test.
  288. */
  289. void *data = kunit_alloc_resource(test, example_resource_init, example_resource_free,
  290. GFP_KERNEL, &ctx);
  291. if (!data)
  292. return -ENOMEM;
  293. /*
  294. * Pass the parameter array information to the parameterized test context
  295. * struct kunit. Note that you will need to provide kunit_array_gen_params()
  296. * as the generator function to KUNIT_CASE_PARAM_WITH_INIT() when registering
  297. * a parameter array this route.
  298. */
  299. kunit_register_params_array(test, example_params_array, arr_size,
  300. example_param_array_get_desc);
  301. return 0;
  302. }
  303. /*
  304. * This is an example of a test that uses shared resources available in the
  305. * parameterized test context.
  306. */
  307. static void example_params_test_with_init(struct kunit *test)
  308. {
  309. int threshold;
  310. struct kunit_resource *res;
  311. const struct example_param *param = test->param_value;
  312. /* By design, param pointer will not be NULL. */
  313. KUNIT_ASSERT_NOT_NULL(test, param);
  314. /*
  315. * Here we pass test->parent to search for shared resources in the
  316. * parameterized test context.
  317. */
  318. res = kunit_find_resource(test->parent, example_resource_alloc_match, NULL);
  319. KUNIT_ASSERT_NOT_NULL(test, res);
  320. /* Since kunit_resource->data is a void pointer we need to typecast it. */
  321. threshold = *((int *)res->data);
  322. /* Assert that the parameter is less than or equal to a certain threshold. */
  323. KUNIT_ASSERT_LE(test, param->value, threshold);
  324. /* This decreases the reference count after calling kunit_find_resource(). */
  325. kunit_put_resource(res);
  326. }
  327. /*
  328. * Helper function to create a parameter array of Fibonacci numbers. This example
  329. * highlights a parameter generation scenario that is:
  330. * 1. Not feasible to fully pre-generate at compile time.
  331. * 2. Challenging to implement with a standard generate_params() function,
  332. * as it only provides the previous parameter, while Fibonacci requires
  333. * access to two preceding values for calculation.
  334. */
  335. static void *make_fibonacci_params(struct kunit *test, size_t seq_size)
  336. {
  337. int *seq;
  338. if (seq_size <= 0)
  339. return NULL;
  340. /*
  341. * Using kunit_kmalloc_array here ties the lifetime of the array to
  342. * the parameterized test i.e. it will get automatically cleaned up
  343. * by KUnit after the parameterized test finishes.
  344. */
  345. seq = kunit_kmalloc_array(test, seq_size, sizeof(int), GFP_KERNEL);
  346. if (!seq)
  347. return NULL;
  348. if (seq_size >= 1)
  349. seq[0] = 0;
  350. if (seq_size >= 2)
  351. seq[1] = 1;
  352. for (int i = 2; i < seq_size; i++)
  353. seq[i] = seq[i - 1] + seq[i - 2];
  354. return seq;
  355. }
  356. /*
  357. * This is an example of a function that provides a description for each of the
  358. * parameters.
  359. */
  360. static void example_param_dynamic_arr_get_desc(struct kunit *test, const void *p, char *desc)
  361. {
  362. const int *fib_num = p;
  363. snprintf(desc, KUNIT_PARAM_DESC_SIZE, "fibonacci param: %d", *fib_num);
  364. }
  365. /*
  366. * Example of a parameterized test param_init() function that registers a dynamic
  367. * array of parameters.
  368. */
  369. static int example_param_init_dynamic_arr(struct kunit *test)
  370. {
  371. size_t seq_size;
  372. int *fibonacci_params;
  373. kunit_info(test, "initializing parameterized test\n");
  374. seq_size = 6;
  375. fibonacci_params = make_fibonacci_params(test, seq_size);
  376. if (!fibonacci_params)
  377. return -ENOMEM;
  378. /*
  379. * Passes the dynamic parameter array information to the parameterized test
  380. * context struct kunit. The array and its metadata will be stored in
  381. * test->parent->params_array. The array itself will be located in
  382. * params_data.params.
  383. *
  384. * Note that you will need to pass kunit_array_gen_params() as the
  385. * generator function to KUNIT_CASE_PARAM_WITH_INIT() when registering
  386. * a parameter array this route.
  387. */
  388. kunit_register_params_array(test, fibonacci_params, seq_size,
  389. example_param_dynamic_arr_get_desc);
  390. return 0;
  391. }
  392. /*
  393. * Example of a parameterized test param_exit() function that outputs a log
  394. * at the end of the parameterized test. It could also be used for any other
  395. * teardown logic.
  396. */
  397. static void example_param_exit_dynamic_arr(struct kunit *test)
  398. {
  399. kunit_info(test, "exiting parameterized test\n");
  400. }
  401. /*
  402. * Example of test that uses the registered dynamic array to perform assertions
  403. * and expectations.
  404. */
  405. static void example_params_test_with_init_dynamic_arr(struct kunit *test)
  406. {
  407. const int *param = test->param_value;
  408. int param_val;
  409. /* By design, param pointer will not be NULL. */
  410. KUNIT_ASSERT_NOT_NULL(test, param);
  411. param_val = *param;
  412. KUNIT_EXPECT_EQ(test, param_val - param_val, 0);
  413. }
  414. /*
  415. * Here we make a list of all the test cases we want to add to the test suite
  416. * below.
  417. */
  418. static struct kunit_case example_test_cases[] = {
  419. /*
  420. * This is a helper to create a test case object from a test case
  421. * function; its exact function is not important to understand how to
  422. * use KUnit, just know that this is how you associate test cases with a
  423. * test suite.
  424. */
  425. KUNIT_CASE(example_simple_test),
  426. KUNIT_CASE(example_skip_test),
  427. KUNIT_CASE(example_mark_skipped_test),
  428. KUNIT_CASE(example_all_expect_macros_test),
  429. KUNIT_CASE(example_static_stub_test),
  430. KUNIT_CASE(example_static_stub_using_fn_ptr_test),
  431. KUNIT_CASE(example_priv_test),
  432. KUNIT_CASE_PARAM(example_params_test, example_gen_params),
  433. KUNIT_CASE_PARAM_WITH_INIT(example_params_test_with_init, kunit_array_gen_params,
  434. example_param_init, NULL),
  435. KUNIT_CASE_PARAM_WITH_INIT(example_params_test_with_init_dynamic_arr,
  436. kunit_array_gen_params, example_param_init_dynamic_arr,
  437. example_param_exit_dynamic_arr),
  438. KUNIT_CASE_SLOW(example_slow_test),
  439. {}
  440. };
  441. /*
  442. * This defines a suite or grouping of tests.
  443. *
  444. * Test cases are defined as belonging to the suite by adding them to
  445. * `kunit_cases`.
  446. *
  447. * Often it is desirable to run some function which will set up things which
  448. * will be used by every test; this is accomplished with an `init` function
  449. * which runs before each test case is invoked. Similarly, an `exit` function
  450. * may be specified which runs after every test case and can be used to for
  451. * cleanup. For clarity, running tests in a test suite would behave as follows:
  452. *
  453. * suite.suite_init(suite);
  454. * suite.init(test);
  455. * suite.test_case[0](test);
  456. * suite.exit(test);
  457. * suite.init(test);
  458. * suite.test_case[1](test);
  459. * suite.exit(test);
  460. * suite.suite_exit(suite);
  461. * ...;
  462. */
  463. static struct kunit_suite example_test_suite = {
  464. .name = "example",
  465. .init = example_test_init,
  466. .exit = example_test_exit,
  467. .suite_init = example_test_init_suite,
  468. .suite_exit = example_test_exit_suite,
  469. .test_cases = example_test_cases,
  470. };
  471. /*
  472. * This registers the above test suite telling KUnit that this is a suite of
  473. * tests that need to be run.
  474. */
  475. kunit_test_suites(&example_test_suite);
  476. static int __init init_add(int x, int y)
  477. {
  478. return (x + y);
  479. }
  480. /*
  481. * This test should always pass. Can be used to test init suites.
  482. */
  483. static void __init example_init_test(struct kunit *test)
  484. {
  485. KUNIT_EXPECT_EQ(test, init_add(1, 1), 2);
  486. }
  487. /*
  488. * The kunit_case struct cannot be marked as __initdata as this will be
  489. * used in debugfs to retrieve results after test has run
  490. */
  491. static struct kunit_case __refdata example_init_test_cases[] = {
  492. KUNIT_CASE(example_init_test),
  493. {}
  494. };
  495. /*
  496. * The kunit_suite struct cannot be marked as __initdata as this will be
  497. * used in debugfs to retrieve results after test has run
  498. */
  499. static struct kunit_suite example_init_test_suite = {
  500. .name = "example_init",
  501. .test_cases = example_init_test_cases,
  502. };
  503. /*
  504. * This registers the test suite and marks the suite as using init data
  505. * and/or functions.
  506. */
  507. kunit_test_init_section_suites(&example_init_test_suite);
  508. MODULE_DESCRIPTION("Example KUnit test suite");
  509. MODULE_LICENSE("GPL v2");