kunit-example-test.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 <[email protected]>
  7. */
  8. #include <kunit/test.h>
  9. #include <kunit/mock.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. struct example_ops;
  31. struct example {
  32. struct example_ops *ops;
  33. };
  34. /*
  35. * A lot of times, we embed "ops structs", which acts an abstraction over
  36. * hardware, a file system implementation, or some other subsystem that you
  37. * want to reason about in a generic way.
  38. */
  39. struct example_ops {
  40. int (*foo)(struct example *example, int num);
  41. };
  42. static int example_bar(struct example *example, int num)
  43. {
  44. return example->ops->foo(example, num);
  45. }
  46. /*
  47. * KUnit allows such a class to be "mocked out" with the following:
  48. */
  49. /*
  50. * This macro creates a mock subclass of the specified class.
  51. */
  52. DECLARE_STRUCT_CLASS_MOCK_PREREQS(example);
  53. /*
  54. * This macro creates a mock implementation of the specified method of the
  55. * specified class.
  56. */
  57. DEFINE_STRUCT_CLASS_MOCK(METHOD(foo), CLASS(example),
  58. RETURNS(int),
  59. PARAMS(struct example *, int));
  60. /*
  61. * This tells KUnit how to initialize the parts of the mock that come from the
  62. * parent. In this example, all we have to do is populate the member functions
  63. * of the parent class with the mock versions we defined.
  64. */
  65. static int example_init(struct kunit *test, struct MOCK(example) *mock_example)
  66. {
  67. /* This is how you get a pointer to the parent class of a mock. */
  68. struct example *example = mock_get_trgt(mock_example);
  69. /*
  70. * Here we create an ops struct containing our mock method instead.
  71. */
  72. example->ops = kunit_kzalloc(test, sizeof(*example->ops), GFP_KERNEL);
  73. example->ops->foo = foo;
  74. return 0;
  75. }
  76. /*
  77. * This registers our parent init function above, allowing KUnit to create a
  78. * constructor for the mock.
  79. */
  80. DEFINE_STRUCT_CLASS_MOCK_INIT(example, example_init);
  81. /*
  82. * This is a test case where we use our mock.
  83. */
  84. static void example_mock_test(struct kunit *test)
  85. {
  86. struct MOCK(example) *mock_example = test->priv;
  87. struct example *example = mock_get_trgt(mock_example);
  88. struct mock_expectation *handle;
  89. /*
  90. * Here we make an expectation that our mock method will be called with
  91. * a parameter equal to 5 passed in.
  92. */
  93. handle = KUNIT_EXPECT_CALL(foo(mock_get_ctrl(mock_example),
  94. kunit_int_eq(test, 5)));
  95. /*
  96. * We specify that when our mock is called in this way, we want it to
  97. * return 2.
  98. */
  99. handle->action = kunit_int_return(test, 2);
  100. KUNIT_EXPECT_EQ(test, 2, example_bar(example, 5));
  101. }
  102. /*
  103. * This is run once before each test case, see the comment on
  104. * example_test_suite for more information.
  105. */
  106. static int example_test_init(struct kunit *test)
  107. {
  108. kunit_info(test, "initializing\n");
  109. /*
  110. * Here we construct the mock and store it in test's `priv` field; this
  111. * field is for KUnit users. You can put whatever you want here, but
  112. * most often it is a place that the init function can put stuff to be
  113. * used by test cases.
  114. */
  115. test->priv = CONSTRUCT_MOCK(example, test);
  116. if (!test->priv)
  117. return -EINVAL;
  118. return 0;
  119. }
  120. /*
  121. * This is run once before all test cases in the suite.
  122. * See the comment on example_test_suite for more information.
  123. */
  124. static int example_test_init_suite(struct kunit_suite *suite)
  125. {
  126. kunit_info(suite, "initializing suite\n");
  127. return 0;
  128. }
  129. /*
  130. * This test should always be skipped.
  131. */
  132. static void example_skip_test(struct kunit *test)
  133. {
  134. /* This line should run */
  135. kunit_info(test, "You should not see a line below.");
  136. /* Skip (and abort) the test */
  137. kunit_skip(test, "this test should be skipped");
  138. /* This line should not execute */
  139. KUNIT_FAIL(test, "You should not see this line.");
  140. }
  141. /*
  142. * This test should always be marked skipped.
  143. */
  144. static void example_mark_skipped_test(struct kunit *test)
  145. {
  146. /* This line should run */
  147. kunit_info(test, "You should see a line below.");
  148. /* Skip (but do not abort) the test */
  149. kunit_mark_skipped(test, "this test should be skipped");
  150. /* This line should run */
  151. kunit_info(test, "You should see this line.");
  152. }
  153. /*
  154. * This test shows off all the types of KUNIT_EXPECT macros.
  155. */
  156. static void example_all_expect_macros_test(struct kunit *test)
  157. {
  158. /* Boolean assertions */
  159. KUNIT_EXPECT_TRUE(test, true);
  160. KUNIT_EXPECT_FALSE(test, false);
  161. /* Integer assertions */
  162. KUNIT_EXPECT_EQ(test, 1, 1); /* check == */
  163. KUNIT_EXPECT_GE(test, 1, 1); /* check >= */
  164. KUNIT_EXPECT_LE(test, 1, 1); /* check <= */
  165. KUNIT_EXPECT_NE(test, 1, 0); /* check != */
  166. KUNIT_EXPECT_GT(test, 1, 0); /* check > */
  167. KUNIT_EXPECT_LT(test, 0, 1); /* check < */
  168. /* Pointer assertions */
  169. KUNIT_EXPECT_NOT_ERR_OR_NULL(test, test);
  170. KUNIT_EXPECT_PTR_EQ(test, NULL, NULL);
  171. KUNIT_EXPECT_PTR_NE(test, test, NULL);
  172. KUNIT_EXPECT_NULL(test, NULL);
  173. KUNIT_EXPECT_NOT_NULL(test, test);
  174. /* String assertions */
  175. KUNIT_EXPECT_STREQ(test, "hi", "hi");
  176. KUNIT_EXPECT_STRNEQ(test, "hi", "bye");
  177. /*
  178. * There are also ASSERT variants of all of the above that abort test
  179. * execution if they fail. Useful for memory allocations, etc.
  180. */
  181. KUNIT_ASSERT_GT(test, sizeof(char), 0);
  182. /*
  183. * There are also _MSG variants of all of the above that let you include
  184. * additional text on failure.
  185. */
  186. KUNIT_EXPECT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
  187. KUNIT_ASSERT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
  188. }
  189. /*
  190. * Here we make a list of all the test cases we want to add to the test suite
  191. * below.
  192. */
  193. static struct kunit_case example_test_cases[] = {
  194. /*
  195. * This is a helper to create a test case object from a test case
  196. * function; its exact function is not important to understand how to
  197. * use KUnit, just know that this is how you associate test cases with a
  198. * test suite.
  199. */
  200. KUNIT_CASE(example_simple_test),
  201. KUNIT_CASE(example_mock_test),
  202. KUNIT_CASE(example_skip_test),
  203. KUNIT_CASE(example_mark_skipped_test),
  204. KUNIT_CASE(example_all_expect_macros_test),
  205. {}
  206. };
  207. /*
  208. * This defines a suite or grouping of tests.
  209. *
  210. * Test cases are defined as belonging to the suite by adding them to
  211. * `kunit_cases`.
  212. *
  213. * Often it is desirable to run some function which will set up things which
  214. * will be used by every test; this is accomplished with an `init` function
  215. * which runs before each test case is invoked. Similarly, an `exit` function
  216. * may be specified which runs after every test case and can be used to for
  217. * cleanup. For clarity, running tests in a test suite would behave as follows:
  218. *
  219. * suite.suite_init(suite);
  220. * suite.init(test);
  221. * suite.test_case[0](test);
  222. * suite.exit(test);
  223. * suite.init(test);
  224. * suite.test_case[1](test);
  225. * suite.exit(test);
  226. * suite.suite_exit(suite);
  227. * ...;
  228. */
  229. static struct kunit_suite example_test_suite = {
  230. .name = "example",
  231. .init = example_test_init,
  232. .suite_init = example_test_init_suite,
  233. .test_cases = example_test_cases,
  234. };
  235. /*
  236. * This registers the above test suite telling KUnit that this is a suite of
  237. * tests that need to be run.
  238. */
  239. kunit_test_suites(&example_test_suite);
  240. MODULE_LICENSE("GPL v2");