PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/kunit/test.c

https://github.com/tytso/ext4
C | 614 lines | 424 code | 106 blank | 84 comment | 38 complexity | 59a526a8281e88d1097cbdb5afb23592 MD5 | raw file
Possible License(s): GPL-2.0
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Base unit test (KUnit) API.
  4. *
  5. * Copyright (C) 2019, Google LLC.
  6. * Author: Brendan Higgins <brendanhiggins@google.com>
  7. */
  8. #include <kunit/test.h>
  9. #include <linux/kernel.h>
  10. #include <linux/kref.h>
  11. #include <linux/sched/debug.h>
  12. #include <linux/sched.h>
  13. #include "debugfs.h"
  14. #include "string-stream.h"
  15. #include "try-catch-impl.h"
  16. /*
  17. * Append formatted message to log, size of which is limited to
  18. * KUNIT_LOG_SIZE bytes (including null terminating byte).
  19. */
  20. void kunit_log_append(char *log, const char *fmt, ...)
  21. {
  22. char line[KUNIT_LOG_SIZE];
  23. va_list args;
  24. int len_left;
  25. if (!log)
  26. return;
  27. len_left = KUNIT_LOG_SIZE - strlen(log) - 1;
  28. if (len_left <= 0)
  29. return;
  30. va_start(args, fmt);
  31. vsnprintf(line, sizeof(line), fmt, args);
  32. va_end(args);
  33. strncat(log, line, len_left);
  34. }
  35. EXPORT_SYMBOL_GPL(kunit_log_append);
  36. size_t kunit_suite_num_test_cases(struct kunit_suite *suite)
  37. {
  38. struct kunit_case *test_case;
  39. size_t len = 0;
  40. kunit_suite_for_each_test_case(suite, test_case)
  41. len++;
  42. return len;
  43. }
  44. EXPORT_SYMBOL_GPL(kunit_suite_num_test_cases);
  45. static void kunit_print_subtest_start(struct kunit_suite *suite)
  46. {
  47. kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "# Subtest: %s",
  48. suite->name);
  49. kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "1..%zd",
  50. kunit_suite_num_test_cases(suite));
  51. }
  52. static void kunit_print_ok_not_ok(void *test_or_suite,
  53. bool is_test,
  54. bool is_ok,
  55. size_t test_number,
  56. const char *description)
  57. {
  58. struct kunit_suite *suite = is_test ? NULL : test_or_suite;
  59. struct kunit *test = is_test ? test_or_suite : NULL;
  60. /*
  61. * We do not log the test suite results as doing so would
  62. * mean debugfs display would consist of the test suite
  63. * description and status prior to individual test results.
  64. * Hence directly printk the suite status, and we will
  65. * separately seq_printf() the suite status for the debugfs
  66. * representation.
  67. */
  68. if (suite)
  69. pr_info("%s %zd - %s\n",
  70. kunit_status_to_string(is_ok),
  71. test_number, description);
  72. else
  73. kunit_log(KERN_INFO, test, KUNIT_SUBTEST_INDENT "%s %zd - %s",
  74. kunit_status_to_string(is_ok),
  75. test_number, description);
  76. }
  77. bool kunit_suite_has_succeeded(struct kunit_suite *suite)
  78. {
  79. const struct kunit_case *test_case;
  80. kunit_suite_for_each_test_case(suite, test_case) {
  81. if (!test_case->success)
  82. return false;
  83. }
  84. return true;
  85. }
  86. EXPORT_SYMBOL_GPL(kunit_suite_has_succeeded);
  87. static void kunit_print_subtest_end(struct kunit_suite *suite)
  88. {
  89. static size_t kunit_suite_counter = 1;
  90. kunit_print_ok_not_ok((void *)suite, false,
  91. kunit_suite_has_succeeded(suite),
  92. kunit_suite_counter++,
  93. suite->name);
  94. }
  95. unsigned int kunit_test_case_num(struct kunit_suite *suite,
  96. struct kunit_case *test_case)
  97. {
  98. struct kunit_case *tc;
  99. unsigned int i = 1;
  100. kunit_suite_for_each_test_case(suite, tc) {
  101. if (tc == test_case)
  102. return i;
  103. i++;
  104. }
  105. return 0;
  106. }
  107. EXPORT_SYMBOL_GPL(kunit_test_case_num);
  108. static void kunit_print_string_stream(struct kunit *test,
  109. struct string_stream *stream)
  110. {
  111. struct string_stream_fragment *fragment;
  112. char *buf;
  113. if (string_stream_is_empty(stream))
  114. return;
  115. buf = string_stream_get_string(stream);
  116. if (!buf) {
  117. kunit_err(test,
  118. "Could not allocate buffer, dumping stream:\n");
  119. list_for_each_entry(fragment, &stream->fragments, node) {
  120. kunit_err(test, "%s", fragment->fragment);
  121. }
  122. kunit_err(test, "\n");
  123. } else {
  124. kunit_err(test, "%s", buf);
  125. kunit_kfree(test, buf);
  126. }
  127. }
  128. static void kunit_fail(struct kunit *test, struct kunit_assert *assert)
  129. {
  130. struct string_stream *stream;
  131. kunit_set_failure(test);
  132. stream = alloc_string_stream(test, GFP_KERNEL);
  133. if (!stream) {
  134. WARN(true,
  135. "Could not allocate stream to print failed assertion in %s:%d\n",
  136. assert->file,
  137. assert->line);
  138. return;
  139. }
  140. assert->format(assert, stream);
  141. kunit_print_string_stream(test, stream);
  142. WARN_ON(string_stream_destroy(stream));
  143. }
  144. static void __noreturn kunit_abort(struct kunit *test)
  145. {
  146. kunit_try_catch_throw(&test->try_catch); /* Does not return. */
  147. /*
  148. * Throw could not abort from test.
  149. *
  150. * XXX: we should never reach this line! As kunit_try_catch_throw is
  151. * marked __noreturn.
  152. */
  153. WARN_ONCE(true, "Throw could not abort from test!\n");
  154. }
  155. void kunit_do_assertion(struct kunit *test,
  156. struct kunit_assert *assert,
  157. bool pass,
  158. const char *fmt, ...)
  159. {
  160. va_list args;
  161. if (pass)
  162. return;
  163. va_start(args, fmt);
  164. assert->message.fmt = fmt;
  165. assert->message.va = &args;
  166. kunit_fail(test, assert);
  167. va_end(args);
  168. if (assert->type == KUNIT_ASSERTION)
  169. kunit_abort(test);
  170. }
  171. EXPORT_SYMBOL_GPL(kunit_do_assertion);
  172. void kunit_init_test(struct kunit *test, const char *name, char *log)
  173. {
  174. spin_lock_init(&test->lock);
  175. INIT_LIST_HEAD(&test->resources);
  176. test->name = name;
  177. test->log = log;
  178. if (test->log)
  179. test->log[0] = '\0';
  180. test->success = true;
  181. }
  182. EXPORT_SYMBOL_GPL(kunit_init_test);
  183. /*
  184. * Initializes and runs test case. Does not clean up or do post validations.
  185. */
  186. static void kunit_run_case_internal(struct kunit *test,
  187. struct kunit_suite *suite,
  188. struct kunit_case *test_case)
  189. {
  190. if (suite->init) {
  191. int ret;
  192. ret = suite->init(test);
  193. if (ret) {
  194. kunit_err(test, "failed to initialize: %d\n", ret);
  195. kunit_set_failure(test);
  196. return;
  197. }
  198. }
  199. test_case->run_case(test);
  200. }
  201. static void kunit_case_internal_cleanup(struct kunit *test)
  202. {
  203. kunit_cleanup(test);
  204. }
  205. /*
  206. * Performs post validations and cleanup after a test case was run.
  207. * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
  208. */
  209. static void kunit_run_case_cleanup(struct kunit *test,
  210. struct kunit_suite *suite)
  211. {
  212. if (suite->exit)
  213. suite->exit(test);
  214. kunit_case_internal_cleanup(test);
  215. }
  216. struct kunit_try_catch_context {
  217. struct kunit *test;
  218. struct kunit_suite *suite;
  219. struct kunit_case *test_case;
  220. };
  221. static void kunit_try_run_case(void *data)
  222. {
  223. struct kunit_try_catch_context *ctx = data;
  224. struct kunit *test = ctx->test;
  225. struct kunit_suite *suite = ctx->suite;
  226. struct kunit_case *test_case = ctx->test_case;
  227. #if (IS_ENABLED(CONFIG_KASAN) && IS_ENABLED(CONFIG_KUNIT))
  228. current->kunit_test = test;
  229. #endif /* IS_ENABLED(CONFIG_KASAN) && IS_ENABLED(CONFIG_KUNIT) */
  230. /*
  231. * kunit_run_case_internal may encounter a fatal error; if it does,
  232. * abort will be called, this thread will exit, and finally the parent
  233. * thread will resume control and handle any necessary clean up.
  234. */
  235. kunit_run_case_internal(test, suite, test_case);
  236. /* This line may never be reached. */
  237. kunit_run_case_cleanup(test, suite);
  238. }
  239. static void kunit_catch_run_case(void *data)
  240. {
  241. struct kunit_try_catch_context *ctx = data;
  242. struct kunit *test = ctx->test;
  243. struct kunit_suite *suite = ctx->suite;
  244. int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
  245. if (try_exit_code) {
  246. kunit_set_failure(test);
  247. /*
  248. * Test case could not finish, we have no idea what state it is
  249. * in, so don't do clean up.
  250. */
  251. if (try_exit_code == -ETIMEDOUT) {
  252. kunit_err(test, "test case timed out\n");
  253. /*
  254. * Unknown internal error occurred preventing test case from
  255. * running, so there is nothing to clean up.
  256. */
  257. } else {
  258. kunit_err(test, "internal error occurred preventing test case from running: %d\n",
  259. try_exit_code);
  260. }
  261. return;
  262. }
  263. /*
  264. * Test case was run, but aborted. It is the test case's business as to
  265. * whether it failed or not, we just need to clean up.
  266. */
  267. kunit_run_case_cleanup(test, suite);
  268. }
  269. /*
  270. * Performs all logic to run a test case. It also catches most errors that
  271. * occur in a test case and reports them as failures.
  272. */
  273. static void kunit_run_case_catch_errors(struct kunit_suite *suite,
  274. struct kunit_case *test_case)
  275. {
  276. struct kunit_try_catch_context context;
  277. struct kunit_try_catch *try_catch;
  278. struct kunit test;
  279. kunit_init_test(&test, test_case->name, test_case->log);
  280. try_catch = &test.try_catch;
  281. kunit_try_catch_init(try_catch,
  282. &test,
  283. kunit_try_run_case,
  284. kunit_catch_run_case);
  285. context.test = &test;
  286. context.suite = suite;
  287. context.test_case = test_case;
  288. kunit_try_catch_run(try_catch, &context);
  289. test_case->success = test.success;
  290. kunit_print_ok_not_ok(&test, true, test_case->success,
  291. kunit_test_case_num(suite, test_case),
  292. test_case->name);
  293. }
  294. int kunit_run_tests(struct kunit_suite *suite)
  295. {
  296. struct kunit_case *test_case;
  297. kunit_print_subtest_start(suite);
  298. kunit_suite_for_each_test_case(suite, test_case)
  299. kunit_run_case_catch_errors(suite, test_case);
  300. kunit_print_subtest_end(suite);
  301. return 0;
  302. }
  303. EXPORT_SYMBOL_GPL(kunit_run_tests);
  304. static void kunit_init_suite(struct kunit_suite *suite)
  305. {
  306. kunit_debugfs_create_suite(suite);
  307. }
  308. int __kunit_test_suites_init(struct kunit_suite * const * const suites)
  309. {
  310. unsigned int i;
  311. for (i = 0; suites[i] != NULL; i++) {
  312. kunit_init_suite(suites[i]);
  313. kunit_run_tests(suites[i]);
  314. }
  315. return 0;
  316. }
  317. EXPORT_SYMBOL_GPL(__kunit_test_suites_init);
  318. static void kunit_exit_suite(struct kunit_suite *suite)
  319. {
  320. kunit_debugfs_destroy_suite(suite);
  321. }
  322. void __kunit_test_suites_exit(struct kunit_suite **suites)
  323. {
  324. unsigned int i;
  325. for (i = 0; suites[i] != NULL; i++)
  326. kunit_exit_suite(suites[i]);
  327. }
  328. EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
  329. /*
  330. * Used for static resources and when a kunit_resource * has been created by
  331. * kunit_alloc_resource(). When an init function is supplied, @data is passed
  332. * into the init function; otherwise, we simply set the resource data field to
  333. * the data value passed in.
  334. */
  335. int kunit_add_resource(struct kunit *test,
  336. kunit_resource_init_t init,
  337. kunit_resource_free_t free,
  338. struct kunit_resource *res,
  339. void *data)
  340. {
  341. int ret = 0;
  342. res->free = free;
  343. kref_init(&res->refcount);
  344. if (init) {
  345. ret = init(res, data);
  346. if (ret)
  347. return ret;
  348. } else {
  349. res->data = data;
  350. }
  351. spin_lock(&test->lock);
  352. list_add_tail(&res->node, &test->resources);
  353. /* refcount for list is established by kref_init() */
  354. spin_unlock(&test->lock);
  355. return ret;
  356. }
  357. EXPORT_SYMBOL_GPL(kunit_add_resource);
  358. int kunit_add_named_resource(struct kunit *test,
  359. kunit_resource_init_t init,
  360. kunit_resource_free_t free,
  361. struct kunit_resource *res,
  362. const char *name,
  363. void *data)
  364. {
  365. struct kunit_resource *existing;
  366. if (!name)
  367. return -EINVAL;
  368. existing = kunit_find_named_resource(test, name);
  369. if (existing) {
  370. kunit_put_resource(existing);
  371. return -EEXIST;
  372. }
  373. res->name = name;
  374. return kunit_add_resource(test, init, free, res, data);
  375. }
  376. EXPORT_SYMBOL_GPL(kunit_add_named_resource);
  377. struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
  378. kunit_resource_init_t init,
  379. kunit_resource_free_t free,
  380. gfp_t internal_gfp,
  381. void *data)
  382. {
  383. struct kunit_resource *res;
  384. int ret;
  385. res = kzalloc(sizeof(*res), internal_gfp);
  386. if (!res)
  387. return NULL;
  388. ret = kunit_add_resource(test, init, free, res, data);
  389. if (!ret) {
  390. /*
  391. * bump refcount for get; kunit_resource_put() should be called
  392. * when done.
  393. */
  394. kunit_get_resource(res);
  395. return res;
  396. }
  397. return NULL;
  398. }
  399. EXPORT_SYMBOL_GPL(kunit_alloc_and_get_resource);
  400. void kunit_remove_resource(struct kunit *test, struct kunit_resource *res)
  401. {
  402. spin_lock(&test->lock);
  403. list_del(&res->node);
  404. spin_unlock(&test->lock);
  405. kunit_put_resource(res);
  406. }
  407. EXPORT_SYMBOL_GPL(kunit_remove_resource);
  408. int kunit_destroy_resource(struct kunit *test, kunit_resource_match_t match,
  409. void *match_data)
  410. {
  411. struct kunit_resource *res = kunit_find_resource(test, match,
  412. match_data);
  413. if (!res)
  414. return -ENOENT;
  415. kunit_remove_resource(test, res);
  416. /* We have a reference also via _find(); drop it. */
  417. kunit_put_resource(res);
  418. return 0;
  419. }
  420. EXPORT_SYMBOL_GPL(kunit_destroy_resource);
  421. struct kunit_kmalloc_params {
  422. size_t size;
  423. gfp_t gfp;
  424. };
  425. static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
  426. {
  427. struct kunit_kmalloc_params *params = context;
  428. res->data = kmalloc(params->size, params->gfp);
  429. if (!res->data)
  430. return -ENOMEM;
  431. return 0;
  432. }
  433. static void kunit_kmalloc_free(struct kunit_resource *res)
  434. {
  435. kfree(res->data);
  436. }
  437. void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
  438. {
  439. struct kunit_kmalloc_params params = {
  440. .size = size,
  441. .gfp = gfp
  442. };
  443. return kunit_alloc_resource(test,
  444. kunit_kmalloc_init,
  445. kunit_kmalloc_free,
  446. gfp,
  447. &params);
  448. }
  449. EXPORT_SYMBOL_GPL(kunit_kmalloc);
  450. void kunit_kfree(struct kunit *test, const void *ptr)
  451. {
  452. struct kunit_resource *res;
  453. res = kunit_find_resource(test, kunit_resource_instance_match,
  454. (void *)ptr);
  455. /*
  456. * Removing the resource from the list of resources drops the
  457. * reference count to 1; the final put will trigger the free.
  458. */
  459. kunit_remove_resource(test, res);
  460. kunit_put_resource(res);
  461. }
  462. EXPORT_SYMBOL_GPL(kunit_kfree);
  463. void kunit_cleanup(struct kunit *test)
  464. {
  465. struct kunit_resource *res;
  466. /*
  467. * test->resources is a stack - each allocation must be freed in the
  468. * reverse order from which it was added since one resource may depend
  469. * on another for its entire lifetime.
  470. * Also, we cannot use the normal list_for_each constructs, even the
  471. * safe ones because *arbitrary* nodes may be deleted when
  472. * kunit_resource_free is called; the list_for_each_safe variants only
  473. * protect against the current node being deleted, not the next.
  474. */
  475. while (true) {
  476. spin_lock(&test->lock);
  477. if (list_empty(&test->resources)) {
  478. spin_unlock(&test->lock);
  479. break;
  480. }
  481. res = list_last_entry(&test->resources,
  482. struct kunit_resource,
  483. node);
  484. /*
  485. * Need to unlock here as a resource may remove another
  486. * resource, and this can't happen if the test->lock
  487. * is held.
  488. */
  489. spin_unlock(&test->lock);
  490. kunit_remove_resource(test, res);
  491. }
  492. #if (IS_ENABLED(CONFIG_KASAN) && IS_ENABLED(CONFIG_KUNIT))
  493. current->kunit_test = NULL;
  494. #endif /* IS_ENABLED(CONFIG_KASAN) && IS_ENABLED(CONFIG_KUNIT)*/
  495. }
  496. EXPORT_SYMBOL_GPL(kunit_cleanup);
  497. static int __init kunit_init(void)
  498. {
  499. kunit_debugfs_init();
  500. return 0;
  501. }
  502. late_initcall(kunit_init);
  503. static void __exit kunit_exit(void)
  504. {
  505. kunit_debugfs_cleanup();
  506. }
  507. module_exit(kunit_exit);
  508. MODULE_LICENSE("GPL v2");