kunit-test.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * KUnit test for core test infrastructure.
  4. *
  5. * Copyright (C) 2019, Google LLC.
  6. * Author: Brendan Higgins <brendanhiggins@google.com>
  7. */
  8. #include "linux/gfp_types.h"
  9. #include <kunit/test.h>
  10. #include <kunit/test-bug.h>
  11. #include <kunit/static_stub.h>
  12. #include <linux/device.h>
  13. #include <kunit/device.h>
  14. #include "string-stream.h"
  15. #include "try-catch-impl.h"
  16. struct kunit_try_catch_test_context {
  17. struct kunit_try_catch *try_catch;
  18. bool function_called;
  19. };
  20. static void kunit_test_successful_try(void *data)
  21. {
  22. struct kunit *test = data;
  23. struct kunit_try_catch_test_context *ctx = test->priv;
  24. ctx->function_called = true;
  25. }
  26. static void kunit_test_no_catch(void *data)
  27. {
  28. struct kunit *test = data;
  29. KUNIT_FAIL(test, "Catch should not be called\n");
  30. }
  31. static void kunit_test_try_catch_successful_try_no_catch(struct kunit *test)
  32. {
  33. struct kunit_try_catch_test_context *ctx = test->priv;
  34. struct kunit_try_catch *try_catch = ctx->try_catch;
  35. kunit_try_catch_init(try_catch,
  36. test,
  37. kunit_test_successful_try,
  38. kunit_test_no_catch,
  39. 300 * msecs_to_jiffies(MSEC_PER_SEC));
  40. kunit_try_catch_run(try_catch, test);
  41. KUNIT_EXPECT_TRUE(test, ctx->function_called);
  42. }
  43. static void kunit_test_unsuccessful_try(void *data)
  44. {
  45. struct kunit *test = data;
  46. struct kunit_try_catch_test_context *ctx = test->priv;
  47. struct kunit_try_catch *try_catch = ctx->try_catch;
  48. kunit_try_catch_throw(try_catch);
  49. KUNIT_FAIL(test, "This line should never be reached\n");
  50. }
  51. static void kunit_test_catch(void *data)
  52. {
  53. struct kunit *test = data;
  54. struct kunit_try_catch_test_context *ctx = test->priv;
  55. ctx->function_called = true;
  56. }
  57. static void kunit_test_try_catch_unsuccessful_try_does_catch(struct kunit *test)
  58. {
  59. struct kunit_try_catch_test_context *ctx = test->priv;
  60. struct kunit_try_catch *try_catch = ctx->try_catch;
  61. kunit_try_catch_init(try_catch,
  62. test,
  63. kunit_test_unsuccessful_try,
  64. kunit_test_catch,
  65. 300 * msecs_to_jiffies(MSEC_PER_SEC));
  66. kunit_try_catch_run(try_catch, test);
  67. KUNIT_EXPECT_TRUE(test, ctx->function_called);
  68. }
  69. static int kunit_try_catch_test_init(struct kunit *test)
  70. {
  71. struct kunit_try_catch_test_context *ctx;
  72. ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
  73. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
  74. test->priv = ctx;
  75. ctx->try_catch = kunit_kmalloc(test,
  76. sizeof(*ctx->try_catch),
  77. GFP_KERNEL);
  78. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->try_catch);
  79. return 0;
  80. }
  81. static struct kunit_case kunit_try_catch_test_cases[] = {
  82. KUNIT_CASE(kunit_test_try_catch_successful_try_no_catch),
  83. KUNIT_CASE(kunit_test_try_catch_unsuccessful_try_does_catch),
  84. {}
  85. };
  86. static struct kunit_suite kunit_try_catch_test_suite = {
  87. .name = "kunit-try-catch-test",
  88. .init = kunit_try_catch_test_init,
  89. .test_cases = kunit_try_catch_test_cases,
  90. };
  91. #if IS_ENABLED(CONFIG_KUNIT_FAULT_TEST)
  92. static void kunit_test_null_dereference(void *data)
  93. {
  94. struct kunit *test = data;
  95. int *null = NULL;
  96. *null = 0;
  97. KUNIT_FAIL(test, "This line should never be reached\n");
  98. }
  99. static void kunit_test_fault_null_dereference(struct kunit *test)
  100. {
  101. struct kunit_try_catch_test_context *ctx = test->priv;
  102. struct kunit_try_catch *try_catch = ctx->try_catch;
  103. kunit_try_catch_init(try_catch,
  104. test,
  105. kunit_test_null_dereference,
  106. kunit_test_catch,
  107. 300 * msecs_to_jiffies(MSEC_PER_SEC));
  108. kunit_try_catch_run(try_catch, test);
  109. KUNIT_EXPECT_EQ(test, try_catch->try_result, -EINTR);
  110. KUNIT_EXPECT_TRUE(test, ctx->function_called);
  111. }
  112. #endif /* CONFIG_KUNIT_FAULT_TEST */
  113. static struct kunit_case kunit_fault_test_cases[] = {
  114. #if IS_ENABLED(CONFIG_KUNIT_FAULT_TEST)
  115. KUNIT_CASE(kunit_test_fault_null_dereference),
  116. #endif /* CONFIG_KUNIT_FAULT_TEST */
  117. {}
  118. };
  119. static struct kunit_suite kunit_fault_test_suite = {
  120. .name = "kunit_fault",
  121. .init = kunit_try_catch_test_init,
  122. .test_cases = kunit_fault_test_cases,
  123. };
  124. /*
  125. * Context for testing test managed resources
  126. * is_resource_initialized is used to test arbitrary resources
  127. */
  128. struct kunit_test_resource_context {
  129. struct kunit test;
  130. bool is_resource_initialized;
  131. int allocate_order[2];
  132. int free_order[4];
  133. };
  134. static int fake_resource_init(struct kunit_resource *res, void *context)
  135. {
  136. struct kunit_test_resource_context *ctx = context;
  137. res->data = &ctx->is_resource_initialized;
  138. ctx->is_resource_initialized = true;
  139. return 0;
  140. }
  141. static void fake_resource_free(struct kunit_resource *res)
  142. {
  143. bool *is_resource_initialized = res->data;
  144. *is_resource_initialized = false;
  145. }
  146. static void kunit_resource_test_init_resources(struct kunit *test)
  147. {
  148. struct kunit_test_resource_context *ctx = test->priv;
  149. kunit_init_test(&ctx->test, "testing_test_init_test", NULL);
  150. KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
  151. }
  152. static void kunit_resource_test_alloc_resource(struct kunit *test)
  153. {
  154. struct kunit_test_resource_context *ctx = test->priv;
  155. struct kunit_resource *res;
  156. kunit_resource_free_t free = fake_resource_free;
  157. res = kunit_alloc_and_get_resource(&ctx->test,
  158. fake_resource_init,
  159. fake_resource_free,
  160. GFP_KERNEL,
  161. ctx);
  162. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res);
  163. KUNIT_EXPECT_PTR_EQ(test,
  164. &ctx->is_resource_initialized,
  165. (bool *)res->data);
  166. KUNIT_EXPECT_TRUE(test, list_is_last(&res->node, &ctx->test.resources));
  167. KUNIT_EXPECT_PTR_EQ(test, free, res->free);
  168. kunit_put_resource(res);
  169. }
  170. static inline bool kunit_resource_instance_match(struct kunit *test,
  171. struct kunit_resource *res,
  172. void *match_data)
  173. {
  174. return res->data == match_data;
  175. }
  176. /*
  177. * Note: tests below use kunit_alloc_and_get_resource(), so as a consequence
  178. * they have a reference to the associated resource that they must release
  179. * via kunit_put_resource(). In normal operation, users will only
  180. * have to do this for cases where they use kunit_find_resource(), and the
  181. * kunit_alloc_resource() function will be used (which does not take a
  182. * resource reference).
  183. */
  184. static void kunit_resource_test_destroy_resource(struct kunit *test)
  185. {
  186. struct kunit_test_resource_context *ctx = test->priv;
  187. struct kunit_resource *res = kunit_alloc_and_get_resource(
  188. &ctx->test,
  189. fake_resource_init,
  190. fake_resource_free,
  191. GFP_KERNEL,
  192. ctx);
  193. kunit_put_resource(res);
  194. KUNIT_ASSERT_FALSE(test,
  195. kunit_destroy_resource(&ctx->test,
  196. kunit_resource_instance_match,
  197. res->data));
  198. KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
  199. KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
  200. }
  201. static void kunit_resource_test_remove_resource(struct kunit *test)
  202. {
  203. struct kunit_test_resource_context *ctx = test->priv;
  204. struct kunit_resource *res = kunit_alloc_and_get_resource(
  205. &ctx->test,
  206. fake_resource_init,
  207. fake_resource_free,
  208. GFP_KERNEL,
  209. ctx);
  210. /* The resource is in the list */
  211. KUNIT_EXPECT_FALSE(test, list_empty(&ctx->test.resources));
  212. /* Remove the resource. The pointer is still valid, but it can't be
  213. * found.
  214. */
  215. kunit_remove_resource(test, res);
  216. KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
  217. /* We haven't been freed yet. */
  218. KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
  219. /* Removing the resource multiple times is valid. */
  220. kunit_remove_resource(test, res);
  221. KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
  222. /* Despite having been removed twice (from only one reference), the
  223. * resource still has not been freed.
  224. */
  225. KUNIT_EXPECT_TRUE(test, ctx->is_resource_initialized);
  226. /* Free the resource. */
  227. kunit_put_resource(res);
  228. KUNIT_EXPECT_FALSE(test, ctx->is_resource_initialized);
  229. }
  230. static void kunit_resource_test_cleanup_resources(struct kunit *test)
  231. {
  232. int i;
  233. struct kunit_test_resource_context *ctx = test->priv;
  234. struct kunit_resource *resources[5];
  235. for (i = 0; i < ARRAY_SIZE(resources); i++) {
  236. resources[i] = kunit_alloc_and_get_resource(&ctx->test,
  237. fake_resource_init,
  238. fake_resource_free,
  239. GFP_KERNEL,
  240. ctx);
  241. kunit_put_resource(resources[i]);
  242. }
  243. kunit_cleanup(&ctx->test);
  244. KUNIT_EXPECT_TRUE(test, list_empty(&ctx->test.resources));
  245. }
  246. static void kunit_resource_test_mark_order(int order_array[],
  247. size_t order_size,
  248. int key)
  249. {
  250. int i;
  251. for (i = 0; i < order_size && order_array[i]; i++)
  252. ;
  253. order_array[i] = key;
  254. }
  255. #define KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, order_field, key) \
  256. kunit_resource_test_mark_order(ctx->order_field, \
  257. ARRAY_SIZE(ctx->order_field), \
  258. key)
  259. static int fake_resource_2_init(struct kunit_resource *res, void *context)
  260. {
  261. struct kunit_test_resource_context *ctx = context;
  262. KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 2);
  263. res->data = ctx;
  264. return 0;
  265. }
  266. static void fake_resource_2_free(struct kunit_resource *res)
  267. {
  268. struct kunit_test_resource_context *ctx = res->data;
  269. KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 2);
  270. }
  271. static int fake_resource_1_init(struct kunit_resource *res, void *context)
  272. {
  273. struct kunit_test_resource_context *ctx = context;
  274. struct kunit_resource *res2;
  275. res2 = kunit_alloc_and_get_resource(&ctx->test,
  276. fake_resource_2_init,
  277. fake_resource_2_free,
  278. GFP_KERNEL,
  279. ctx);
  280. KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, allocate_order, 1);
  281. res->data = ctx;
  282. kunit_put_resource(res2);
  283. return 0;
  284. }
  285. static void fake_resource_1_free(struct kunit_resource *res)
  286. {
  287. struct kunit_test_resource_context *ctx = res->data;
  288. KUNIT_RESOURCE_TEST_MARK_ORDER(ctx, free_order, 1);
  289. }
  290. /*
  291. * TODO(brendanhiggins@google.com): replace the arrays that keep track of the
  292. * order of allocation and freeing with strict mocks using the IN_SEQUENCE macro
  293. * to assert allocation and freeing order when the feature becomes available.
  294. */
  295. static void kunit_resource_test_proper_free_ordering(struct kunit *test)
  296. {
  297. struct kunit_test_resource_context *ctx = test->priv;
  298. struct kunit_resource *res;
  299. /* fake_resource_1 allocates a fake_resource_2 in its init. */
  300. res = kunit_alloc_and_get_resource(&ctx->test,
  301. fake_resource_1_init,
  302. fake_resource_1_free,
  303. GFP_KERNEL,
  304. ctx);
  305. /*
  306. * Since fake_resource_2_init calls KUNIT_RESOURCE_TEST_MARK_ORDER
  307. * before returning to fake_resource_1_init, it should be the first to
  308. * put its key in the allocate_order array.
  309. */
  310. KUNIT_EXPECT_EQ(test, ctx->allocate_order[0], 2);
  311. KUNIT_EXPECT_EQ(test, ctx->allocate_order[1], 1);
  312. kunit_put_resource(res);
  313. kunit_cleanup(&ctx->test);
  314. /*
  315. * Because fake_resource_2 finishes allocation before fake_resource_1,
  316. * fake_resource_1 should be freed first since it could depend on
  317. * fake_resource_2.
  318. */
  319. KUNIT_EXPECT_EQ(test, ctx->free_order[0], 1);
  320. KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
  321. }
  322. static void kunit_resource_test_static(struct kunit *test)
  323. {
  324. struct kunit_test_resource_context ctx;
  325. struct kunit_resource res;
  326. KUNIT_EXPECT_EQ(test, kunit_add_resource(test, NULL, NULL, &res, &ctx),
  327. 0);
  328. KUNIT_EXPECT_PTR_EQ(test, res.data, (void *)&ctx);
  329. kunit_cleanup(test);
  330. KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
  331. }
  332. static void kunit_resource_test_named(struct kunit *test)
  333. {
  334. struct kunit_resource res1, res2, *found = NULL;
  335. struct kunit_test_resource_context ctx;
  336. KUNIT_EXPECT_EQ(test,
  337. kunit_add_named_resource(test, NULL, NULL, &res1,
  338. "resource_1", &ctx),
  339. 0);
  340. KUNIT_EXPECT_PTR_EQ(test, res1.data, (void *)&ctx);
  341. KUNIT_EXPECT_EQ(test,
  342. kunit_add_named_resource(test, NULL, NULL, &res1,
  343. "resource_1", &ctx),
  344. -EEXIST);
  345. KUNIT_EXPECT_EQ(test,
  346. kunit_add_named_resource(test, NULL, NULL, &res2,
  347. "resource_2", &ctx),
  348. 0);
  349. found = kunit_find_named_resource(test, "resource_1");
  350. KUNIT_EXPECT_PTR_EQ(test, found, &res1);
  351. if (found)
  352. kunit_put_resource(&res1);
  353. KUNIT_EXPECT_EQ(test, kunit_destroy_named_resource(test, "resource_2"),
  354. 0);
  355. kunit_cleanup(test);
  356. KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
  357. }
  358. static void increment_int(void *ctx)
  359. {
  360. int *i = (int *)ctx;
  361. (*i)++;
  362. }
  363. static void kunit_resource_test_action(struct kunit *test)
  364. {
  365. int num_actions = 0;
  366. kunit_add_action(test, increment_int, &num_actions);
  367. KUNIT_EXPECT_EQ(test, num_actions, 0);
  368. kunit_cleanup(test);
  369. KUNIT_EXPECT_EQ(test, num_actions, 1);
  370. /* Once we've cleaned up, the action queue is empty. */
  371. kunit_cleanup(test);
  372. KUNIT_EXPECT_EQ(test, num_actions, 1);
  373. /* Check the same function can be deferred multiple times. */
  374. kunit_add_action(test, increment_int, &num_actions);
  375. kunit_add_action(test, increment_int, &num_actions);
  376. kunit_cleanup(test);
  377. KUNIT_EXPECT_EQ(test, num_actions, 3);
  378. }
  379. static void kunit_resource_test_remove_action(struct kunit *test)
  380. {
  381. int num_actions = 0;
  382. kunit_add_action(test, increment_int, &num_actions);
  383. KUNIT_EXPECT_EQ(test, num_actions, 0);
  384. kunit_remove_action(test, increment_int, &num_actions);
  385. kunit_cleanup(test);
  386. KUNIT_EXPECT_EQ(test, num_actions, 0);
  387. }
  388. static void kunit_resource_test_release_action(struct kunit *test)
  389. {
  390. int num_actions = 0;
  391. kunit_add_action(test, increment_int, &num_actions);
  392. KUNIT_EXPECT_EQ(test, num_actions, 0);
  393. /* Runs immediately on trigger. */
  394. kunit_release_action(test, increment_int, &num_actions);
  395. KUNIT_EXPECT_EQ(test, num_actions, 1);
  396. /* Doesn't run again on test exit. */
  397. kunit_cleanup(test);
  398. KUNIT_EXPECT_EQ(test, num_actions, 1);
  399. }
  400. static void action_order_1(void *ctx)
  401. {
  402. struct kunit_test_resource_context *res_ctx = (struct kunit_test_resource_context *)ctx;
  403. KUNIT_RESOURCE_TEST_MARK_ORDER(res_ctx, free_order, 1);
  404. kunit_log(KERN_INFO, current->kunit_test, "action_order_1");
  405. }
  406. static void action_order_2(void *ctx)
  407. {
  408. struct kunit_test_resource_context *res_ctx = (struct kunit_test_resource_context *)ctx;
  409. KUNIT_RESOURCE_TEST_MARK_ORDER(res_ctx, free_order, 2);
  410. kunit_log(KERN_INFO, current->kunit_test, "action_order_2");
  411. }
  412. static void kunit_resource_test_action_ordering(struct kunit *test)
  413. {
  414. struct kunit_test_resource_context *ctx = test->priv;
  415. kunit_add_action(test, action_order_1, ctx);
  416. kunit_add_action(test, action_order_2, ctx);
  417. kunit_add_action(test, action_order_1, ctx);
  418. kunit_add_action(test, action_order_2, ctx);
  419. kunit_remove_action(test, action_order_1, ctx);
  420. kunit_release_action(test, action_order_2, ctx);
  421. kunit_cleanup(test);
  422. /* [2 is triggered] [2], [(1 is cancelled)] [1] */
  423. KUNIT_EXPECT_EQ(test, ctx->free_order[0], 2);
  424. KUNIT_EXPECT_EQ(test, ctx->free_order[1], 2);
  425. KUNIT_EXPECT_EQ(test, ctx->free_order[2], 1);
  426. }
  427. static int kunit_resource_test_init(struct kunit *test)
  428. {
  429. struct kunit_test_resource_context *ctx = kzalloc_obj(*ctx);
  430. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
  431. test->priv = ctx;
  432. kunit_init_test(&ctx->test, "test_test_context", NULL);
  433. return 0;
  434. }
  435. static void kunit_resource_test_exit(struct kunit *test)
  436. {
  437. struct kunit_test_resource_context *ctx = test->priv;
  438. kunit_cleanup(&ctx->test);
  439. kfree(ctx);
  440. }
  441. static struct kunit_case kunit_resource_test_cases[] = {
  442. KUNIT_CASE(kunit_resource_test_init_resources),
  443. KUNIT_CASE(kunit_resource_test_alloc_resource),
  444. KUNIT_CASE(kunit_resource_test_destroy_resource),
  445. KUNIT_CASE(kunit_resource_test_remove_resource),
  446. KUNIT_CASE(kunit_resource_test_cleanup_resources),
  447. KUNIT_CASE(kunit_resource_test_proper_free_ordering),
  448. KUNIT_CASE(kunit_resource_test_static),
  449. KUNIT_CASE(kunit_resource_test_named),
  450. KUNIT_CASE(kunit_resource_test_action),
  451. KUNIT_CASE(kunit_resource_test_remove_action),
  452. KUNIT_CASE(kunit_resource_test_release_action),
  453. KUNIT_CASE(kunit_resource_test_action_ordering),
  454. {}
  455. };
  456. static struct kunit_suite kunit_resource_test_suite = {
  457. .name = "kunit-resource-test",
  458. .init = kunit_resource_test_init,
  459. .exit = kunit_resource_test_exit,
  460. .test_cases = kunit_resource_test_cases,
  461. };
  462. /*
  463. * Log tests call string_stream functions, which aren't exported. So only
  464. * build this code if this test is built-in.
  465. */
  466. #if IS_BUILTIN(CONFIG_KUNIT_TEST)
  467. /* This avoids a cast warning if kfree() is passed direct to kunit_add_action(). */
  468. KUNIT_DEFINE_ACTION_WRAPPER(kfree_wrapper, kfree, const void *);
  469. static void kunit_log_test(struct kunit *test)
  470. {
  471. struct kunit_suite suite;
  472. #ifdef CONFIG_KUNIT_DEBUGFS
  473. char *full_log;
  474. #endif
  475. suite.log = kunit_alloc_string_stream(test, GFP_KERNEL);
  476. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, suite.log);
  477. string_stream_set_append_newlines(suite.log, true);
  478. kunit_log(KERN_INFO, test, "put this in log.");
  479. kunit_log(KERN_INFO, test, "this too.");
  480. kunit_log(KERN_INFO, &suite, "add to suite log.");
  481. kunit_log(KERN_INFO, &suite, "along with this.");
  482. #ifdef CONFIG_KUNIT_DEBUGFS
  483. KUNIT_EXPECT_TRUE(test, test->log->append_newlines);
  484. full_log = string_stream_get_string(test->log);
  485. kunit_add_action(test, kfree_wrapper, full_log);
  486. KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
  487. strstr(full_log, "put this in log."));
  488. KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
  489. strstr(full_log, "this too."));
  490. full_log = string_stream_get_string(suite.log);
  491. kunit_add_action(test, kfree_wrapper, full_log);
  492. KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
  493. strstr(full_log, "add to suite log."));
  494. KUNIT_EXPECT_NOT_ERR_OR_NULL(test,
  495. strstr(full_log, "along with this."));
  496. #else
  497. KUNIT_EXPECT_NULL(test, test->log);
  498. #endif
  499. }
  500. static void kunit_log_newline_test(struct kunit *test)
  501. {
  502. char *full_log;
  503. kunit_info(test, "Add newline\n");
  504. if (test->log) {
  505. full_log = string_stream_get_string(test->log);
  506. kunit_add_action(test, kfree_wrapper, full_log);
  507. KUNIT_ASSERT_NOT_NULL_MSG(test, strstr(full_log, "Add newline\n"),
  508. "Missing log line, full log:\n%s", full_log);
  509. KUNIT_EXPECT_NULL(test, strstr(full_log, "Add newline\n\n"));
  510. } else {
  511. kunit_skip(test, "only useful when debugfs is enabled");
  512. }
  513. }
  514. #else
  515. static void kunit_log_test(struct kunit *test)
  516. {
  517. kunit_skip(test, "Log tests only run when built-in");
  518. }
  519. static void kunit_log_newline_test(struct kunit *test)
  520. {
  521. kunit_skip(test, "Log tests only run when built-in");
  522. }
  523. #endif /* IS_BUILTIN(CONFIG_KUNIT_TEST) */
  524. static struct kunit_case kunit_log_test_cases[] = {
  525. KUNIT_CASE(kunit_log_test),
  526. KUNIT_CASE(kunit_log_newline_test),
  527. {}
  528. };
  529. static struct kunit_suite kunit_log_test_suite = {
  530. .name = "kunit-log-test",
  531. .test_cases = kunit_log_test_cases,
  532. };
  533. static void kunit_status_set_failure_test(struct kunit *test)
  534. {
  535. struct kunit fake;
  536. kunit_init_test(&fake, "fake test", NULL);
  537. KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SUCCESS);
  538. kunit_set_failure(&fake);
  539. KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
  540. }
  541. static void kunit_status_mark_skipped_test(struct kunit *test)
  542. {
  543. struct kunit fake;
  544. kunit_init_test(&fake, "fake test", NULL);
  545. /* Before: Should be SUCCESS with no comment. */
  546. KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
  547. KUNIT_EXPECT_STREQ(test, fake.status_comment, "");
  548. /* Mark the test as skipped. */
  549. kunit_mark_skipped(&fake, "Accepts format string: %s", "YES");
  550. /* After: Should be SKIPPED with our comment. */
  551. KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_SKIPPED);
  552. KUNIT_EXPECT_STREQ(test, fake.status_comment, "Accepts format string: YES");
  553. }
  554. static struct kunit_case kunit_status_test_cases[] = {
  555. KUNIT_CASE(kunit_status_set_failure_test),
  556. KUNIT_CASE(kunit_status_mark_skipped_test),
  557. {}
  558. };
  559. static struct kunit_suite kunit_status_test_suite = {
  560. .name = "kunit_status",
  561. .test_cases = kunit_status_test_cases,
  562. };
  563. static void kunit_current_test(struct kunit *test)
  564. {
  565. /* Check results of both current->kunit_test and
  566. * kunit_get_current_test() are equivalent to current test.
  567. */
  568. KUNIT_EXPECT_PTR_EQ(test, test, current->kunit_test);
  569. KUNIT_EXPECT_PTR_EQ(test, test, kunit_get_current_test());
  570. }
  571. static void kunit_current_fail_test(struct kunit *test)
  572. {
  573. struct kunit fake;
  574. kunit_init_test(&fake, "fake test", NULL);
  575. KUNIT_EXPECT_EQ(test, fake.status, KUNIT_SUCCESS);
  576. /* Set current->kunit_test to fake test. */
  577. current->kunit_test = &fake;
  578. kunit_fail_current_test("This should make `fake` test fail.");
  579. KUNIT_EXPECT_EQ(test, fake.status, (enum kunit_status)KUNIT_FAILURE);
  580. kunit_cleanup(&fake);
  581. /* Reset current->kunit_test to current test. */
  582. current->kunit_test = test;
  583. }
  584. static struct kunit_case kunit_current_test_cases[] = {
  585. KUNIT_CASE(kunit_current_test),
  586. KUNIT_CASE(kunit_current_fail_test),
  587. {}
  588. };
  589. static void test_dev_action(void *priv)
  590. {
  591. *(long *)priv = 1;
  592. }
  593. static void kunit_device_test(struct kunit *test)
  594. {
  595. struct device *test_device;
  596. long action_was_run = 0;
  597. test_device = kunit_device_register(test, "my_device");
  598. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
  599. // Add an action to verify cleanup.
  600. devm_add_action(test_device, test_dev_action, &action_was_run);
  601. KUNIT_EXPECT_EQ(test, action_was_run, 0);
  602. kunit_device_unregister(test, test_device);
  603. KUNIT_EXPECT_EQ(test, action_was_run, 1);
  604. }
  605. static void kunit_device_cleanup_test(struct kunit *test)
  606. {
  607. struct device *test_device;
  608. long action_was_run = 0;
  609. test_device = kunit_device_register(test, "my_device");
  610. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
  611. /* Add an action to verify cleanup. */
  612. devm_add_action(test_device, test_dev_action, &action_was_run);
  613. KUNIT_EXPECT_EQ(test, action_was_run, 0);
  614. /* Force KUnit to run cleanup early. */
  615. kunit_cleanup(test);
  616. KUNIT_EXPECT_EQ(test, action_was_run, 1);
  617. }
  618. struct driver_test_state {
  619. bool driver_device_probed;
  620. bool driver_device_removed;
  621. long action_was_run;
  622. };
  623. static int driver_probe_hook(struct device *dev)
  624. {
  625. struct kunit *test = kunit_get_current_test();
  626. struct driver_test_state *state = (struct driver_test_state *)test->priv;
  627. state->driver_device_probed = true;
  628. return 0;
  629. }
  630. static int driver_remove_hook(struct device *dev)
  631. {
  632. struct kunit *test = kunit_get_current_test();
  633. struct driver_test_state *state = (struct driver_test_state *)test->priv;
  634. state->driver_device_removed = true;
  635. return 0;
  636. }
  637. static void kunit_device_driver_test(struct kunit *test)
  638. {
  639. struct device_driver *test_driver;
  640. struct device *test_device;
  641. struct driver_test_state *test_state = kunit_kzalloc(test, sizeof(*test_state), GFP_KERNEL);
  642. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_state);
  643. test->priv = test_state;
  644. test_driver = kunit_driver_create(test, "my_driver");
  645. // This can fail with an error pointer.
  646. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_driver);
  647. test_driver->probe = driver_probe_hook;
  648. test_driver->remove = driver_remove_hook;
  649. test_device = kunit_device_register_with_driver(test, "my_device", test_driver);
  650. // This can fail with an error pointer.
  651. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
  652. // Make sure the probe function was called.
  653. KUNIT_ASSERT_TRUE(test, test_state->driver_device_probed);
  654. // Add an action to verify cleanup.
  655. devm_add_action(test_device, test_dev_action, &test_state->action_was_run);
  656. KUNIT_EXPECT_EQ(test, test_state->action_was_run, 0);
  657. kunit_device_unregister(test, test_device);
  658. test_device = NULL;
  659. // Make sure the remove hook was called.
  660. KUNIT_ASSERT_TRUE(test, test_state->driver_device_removed);
  661. // We're going to test this again.
  662. test_state->driver_device_probed = false;
  663. // The driver should not automatically be destroyed by
  664. // kunit_device_unregister, so we can re-use it.
  665. test_device = kunit_device_register_with_driver(test, "my_device", test_driver);
  666. // This can fail with an error pointer.
  667. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
  668. // Probe was called again.
  669. KUNIT_ASSERT_TRUE(test, test_state->driver_device_probed);
  670. // Everything is automatically freed here.
  671. }
  672. static struct kunit_case kunit_device_test_cases[] = {
  673. KUNIT_CASE(kunit_device_test),
  674. KUNIT_CASE(kunit_device_cleanup_test),
  675. KUNIT_CASE(kunit_device_driver_test),
  676. {}
  677. };
  678. static struct kunit_suite kunit_device_test_suite = {
  679. .name = "kunit_device",
  680. .test_cases = kunit_device_test_cases,
  681. };
  682. static struct kunit_suite kunit_current_test_suite = {
  683. .name = "kunit_current",
  684. .test_cases = kunit_current_test_cases,
  685. };
  686. static void kunit_stub_test(struct kunit *test)
  687. {
  688. struct kunit fake_test;
  689. const unsigned long fake_real_fn_addr = 0x1234;
  690. const unsigned long fake_replacement_addr = 0x5678;
  691. struct kunit_resource *res;
  692. struct {
  693. void *real_fn_addr;
  694. void *replacement_addr;
  695. } *stub_ctx;
  696. kunit_init_test(&fake_test, "kunit_stub_fake_test", NULL);
  697. KUNIT_ASSERT_EQ(test, fake_test.status, KUNIT_SUCCESS);
  698. KUNIT_ASSERT_EQ(test, list_count_nodes(&fake_test.resources), 0);
  699. __kunit_activate_static_stub(&fake_test, (void *)fake_real_fn_addr,
  700. (void *)fake_replacement_addr);
  701. KUNIT_ASSERT_EQ(test, fake_test.status, KUNIT_SUCCESS);
  702. KUNIT_ASSERT_EQ(test, list_count_nodes(&fake_test.resources), 1);
  703. res = list_first_entry(&fake_test.resources, struct kunit_resource, node);
  704. KUNIT_EXPECT_NOT_NULL(test, res);
  705. stub_ctx = res->data;
  706. KUNIT_EXPECT_NOT_NULL(test, stub_ctx);
  707. KUNIT_EXPECT_EQ(test, (unsigned long)stub_ctx->real_fn_addr, fake_real_fn_addr);
  708. KUNIT_EXPECT_EQ(test, (unsigned long)stub_ctx->replacement_addr, fake_replacement_addr);
  709. __kunit_activate_static_stub(&fake_test, (void *)fake_real_fn_addr, NULL);
  710. KUNIT_ASSERT_EQ(test, fake_test.status, KUNIT_SUCCESS);
  711. KUNIT_ASSERT_EQ(test, list_count_nodes(&fake_test.resources), 0);
  712. }
  713. static struct kunit_case kunit_stub_test_cases[] = {
  714. KUNIT_CASE(kunit_stub_test),
  715. {}
  716. };
  717. static struct kunit_suite kunit_stub_test_suite = {
  718. .name = "kunit_stub",
  719. .test_cases = kunit_stub_test_cases,
  720. };
  721. kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite,
  722. &kunit_log_test_suite, &kunit_status_test_suite,
  723. &kunit_current_test_suite, &kunit_device_test_suite,
  724. &kunit_fault_test_suite, &kunit_stub_test_suite);
  725. MODULE_DESCRIPTION("KUnit test for core test infrastructure");
  726. MODULE_LICENSE("GPL v2");