1 // SPDX-License-Identifier: GPL-2.0
3 * Base unit test (KUnit) API.
5 * Copyright (C) 2019, Google LLC.
9 #include <kunit/resource.h>
10 #include <kunit/test.h>
11 #include <kunit/test-bug.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/panic.h>
16 #include <linux/sched/debug.h>
17 #include <linux/sched.h>
20 #include "string-stream.h"
21 #include "try-catch-impl.h"
23 #if IS_BUILTIN(CONFIG_KUNIT)
25 * Fail the current test and print an error message to the log.
27 void __kunit_fail_current_test(const char *file, int line, const char *fmt, ...)
33 if (!current->kunit_test)
36 kunit_set_failure(current->kunit_test);
38 /* kunit_err() only accepts literals, so evaluate the args first. */
40 len = vsnprintf(NULL, 0, fmt, args) + 1;
43 buffer = kunit_kmalloc(current->kunit_test, len, GFP_KERNEL);
48 vsnprintf(buffer, len, fmt, args);
51 kunit_err(current->kunit_test, "%s:%d: %s", file, line, buffer);
52 kunit_kfree(current->kunit_test, buffer);
54 EXPORT_SYMBOL_GPL(__kunit_fail_current_test);
58 * KUnit statistic mode:
60 * 1 - only when there is more than one subtest
63 static int kunit_stats_enabled = 1;
64 module_param_named(stats_enabled, kunit_stats_enabled, int, 0644);
65 MODULE_PARM_DESC(stats_enabled,
66 "Print test stats: never (0), only for multiple subtests (1), or always (2)");
68 struct kunit_result_stats {
70 unsigned long skipped;
75 static bool kunit_should_print_stats(struct kunit_result_stats stats)
77 if (kunit_stats_enabled == 0)
80 if (kunit_stats_enabled == 2)
83 return (stats.total > 1);
86 static void kunit_print_test_stats(struct kunit *test,
87 struct kunit_result_stats stats)
89 if (!kunit_should_print_stats(stats))
92 kunit_log(KERN_INFO, test,
94 "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
103 * Append formatted message to log, size of which is limited to
104 * KUNIT_LOG_SIZE bytes (including null terminating byte).
106 void kunit_log_append(char *log, const char *fmt, ...)
108 char line[KUNIT_LOG_SIZE];
115 len_left = KUNIT_LOG_SIZE - strlen(log) - 1;
120 vsnprintf(line, sizeof(line), fmt, args);
123 strncat(log, line, len_left);
125 EXPORT_SYMBOL_GPL(kunit_log_append);
127 size_t kunit_suite_num_test_cases(struct kunit_suite *suite)
129 struct kunit_case *test_case;
132 kunit_suite_for_each_test_case(suite, test_case)
137 EXPORT_SYMBOL_GPL(kunit_suite_num_test_cases);
139 static void kunit_print_suite_start(struct kunit_suite *suite)
141 kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "# Subtest: %s",
143 kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "1..%zd",
144 kunit_suite_num_test_cases(suite));
147 static void kunit_print_ok_not_ok(void *test_or_suite,
149 enum kunit_status status,
151 const char *description,
152 const char *directive)
154 struct kunit_suite *suite = is_test ? NULL : test_or_suite;
155 struct kunit *test = is_test ? test_or_suite : NULL;
156 const char *directive_header = (status == KUNIT_SKIPPED) ? " # SKIP " : "";
159 * We do not log the test suite results as doing so would
160 * mean debugfs display would consist of the test suite
161 * description and status prior to individual test results.
162 * Hence directly printk the suite status, and we will
163 * separately seq_printf() the suite status for the debugfs
167 pr_info("%s %zd - %s%s%s\n",
168 kunit_status_to_ok_not_ok(status),
169 test_number, description, directive_header,
170 (status == KUNIT_SKIPPED) ? directive : "");
172 kunit_log(KERN_INFO, test,
173 KUNIT_SUBTEST_INDENT "%s %zd - %s%s%s",
174 kunit_status_to_ok_not_ok(status),
175 test_number, description, directive_header,
176 (status == KUNIT_SKIPPED) ? directive : "");
179 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite)
181 const struct kunit_case *test_case;
182 enum kunit_status status = KUNIT_SKIPPED;
184 if (suite->suite_init_err)
185 return KUNIT_FAILURE;
187 kunit_suite_for_each_test_case(suite, test_case) {
188 if (test_case->status == KUNIT_FAILURE)
189 return KUNIT_FAILURE;
190 else if (test_case->status == KUNIT_SUCCESS)
191 status = KUNIT_SUCCESS;
196 EXPORT_SYMBOL_GPL(kunit_suite_has_succeeded);
198 static size_t kunit_suite_counter = 1;
200 static void kunit_print_suite_end(struct kunit_suite *suite)
202 kunit_print_ok_not_ok((void *)suite, false,
203 kunit_suite_has_succeeded(suite),
204 kunit_suite_counter++,
206 suite->status_comment);
209 unsigned int kunit_test_case_num(struct kunit_suite *suite,
210 struct kunit_case *test_case)
212 struct kunit_case *tc;
215 kunit_suite_for_each_test_case(suite, tc) {
223 EXPORT_SYMBOL_GPL(kunit_test_case_num);
225 static void kunit_print_string_stream(struct kunit *test,
226 struct string_stream *stream)
228 struct string_stream_fragment *fragment;
231 if (string_stream_is_empty(stream))
234 buf = string_stream_get_string(stream);
237 "Could not allocate buffer, dumping stream:\n");
238 list_for_each_entry(fragment, &stream->fragments, node) {
239 kunit_err(test, "%s", fragment->fragment);
241 kunit_err(test, "\n");
243 kunit_err(test, "%s", buf);
244 kunit_kfree(test, buf);
248 static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
249 enum kunit_assert_type type, const struct kunit_assert *assert,
250 const struct va_format *message)
252 struct string_stream *stream;
254 kunit_set_failure(test);
256 stream = alloc_string_stream(test, GFP_KERNEL);
259 "Could not allocate stream to print failed assertion in %s:%d\n",
265 kunit_assert_prologue(loc, type, stream);
266 assert->format(assert, message, stream);
268 kunit_print_string_stream(test, stream);
270 WARN_ON(string_stream_destroy(stream));
273 static void __noreturn kunit_abort(struct kunit *test)
275 kunit_try_catch_throw(&test->try_catch); /* Does not return. */
278 * Throw could not abort from test.
280 * XXX: we should never reach this line! As kunit_try_catch_throw is
283 WARN_ONCE(true, "Throw could not abort from test!\n");
286 void kunit_do_failed_assertion(struct kunit *test,
287 const struct kunit_loc *loc,
288 enum kunit_assert_type type,
289 const struct kunit_assert *assert,
290 const char *fmt, ...)
293 struct va_format message;
299 kunit_fail(test, loc, type, assert, &message);
303 if (type == KUNIT_ASSERTION)
306 EXPORT_SYMBOL_GPL(kunit_do_failed_assertion);
308 void kunit_init_test(struct kunit *test, const char *name, char *log)
310 spin_lock_init(&test->lock);
311 INIT_LIST_HEAD(&test->resources);
316 test->status = KUNIT_SUCCESS;
317 test->status_comment[0] = '\0';
319 EXPORT_SYMBOL_GPL(kunit_init_test);
322 * Initializes and runs test case. Does not clean up or do post validations.
324 static void kunit_run_case_internal(struct kunit *test,
325 struct kunit_suite *suite,
326 struct kunit_case *test_case)
331 ret = suite->init(test);
333 kunit_err(test, "failed to initialize: %d\n", ret);
334 kunit_set_failure(test);
339 test_case->run_case(test);
342 static void kunit_case_internal_cleanup(struct kunit *test)
348 * Performs post validations and cleanup after a test case was run.
349 * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
351 static void kunit_run_case_cleanup(struct kunit *test,
352 struct kunit_suite *suite)
357 kunit_case_internal_cleanup(test);
360 struct kunit_try_catch_context {
362 struct kunit_suite *suite;
363 struct kunit_case *test_case;
366 static void kunit_try_run_case(void *data)
368 struct kunit_try_catch_context *ctx = data;
369 struct kunit *test = ctx->test;
370 struct kunit_suite *suite = ctx->suite;
371 struct kunit_case *test_case = ctx->test_case;
373 current->kunit_test = test;
376 * kunit_run_case_internal may encounter a fatal error; if it does,
377 * abort will be called, this thread will exit, and finally the parent
378 * thread will resume control and handle any necessary clean up.
380 kunit_run_case_internal(test, suite, test_case);
381 /* This line may never be reached. */
382 kunit_run_case_cleanup(test, suite);
385 static void kunit_catch_run_case(void *data)
387 struct kunit_try_catch_context *ctx = data;
388 struct kunit *test = ctx->test;
389 struct kunit_suite *suite = ctx->suite;
390 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
393 kunit_set_failure(test);
395 * Test case could not finish, we have no idea what state it is
396 * in, so don't do clean up.
398 if (try_exit_code == -ETIMEDOUT) {
399 kunit_err(test, "test case timed out\n");
401 * Unknown internal error occurred preventing test case from
402 * running, so there is nothing to clean up.
405 kunit_err(test, "internal error occurred preventing test case from running: %d\n",
412 * Test case was run, but aborted. It is the test case's business as to
413 * whether it failed or not, we just need to clean up.
415 kunit_run_case_cleanup(test, suite);
419 * Performs all logic to run a test case. It also catches most errors that
420 * occur in a test case and reports them as failures.
422 static void kunit_run_case_catch_errors(struct kunit_suite *suite,
423 struct kunit_case *test_case,
426 struct kunit_try_catch_context context;
427 struct kunit_try_catch *try_catch;
429 kunit_init_test(test, test_case->name, test_case->log);
430 try_catch = &test->try_catch;
432 kunit_try_catch_init(try_catch,
435 kunit_catch_run_case);
437 context.suite = suite;
438 context.test_case = test_case;
439 kunit_try_catch_run(try_catch, &context);
441 /* Propagate the parameter result to the test case. */
442 if (test->status == KUNIT_FAILURE)
443 test_case->status = KUNIT_FAILURE;
444 else if (test_case->status != KUNIT_FAILURE && test->status == KUNIT_SUCCESS)
445 test_case->status = KUNIT_SUCCESS;
448 static void kunit_print_suite_stats(struct kunit_suite *suite,
449 struct kunit_result_stats suite_stats,
450 struct kunit_result_stats param_stats)
452 if (kunit_should_print_stats(suite_stats)) {
453 kunit_log(KERN_INFO, suite,
454 "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
462 if (kunit_should_print_stats(param_stats)) {
463 kunit_log(KERN_INFO, suite,
464 "# Totals: pass:%lu fail:%lu skip:%lu total:%lu",
472 static void kunit_update_stats(struct kunit_result_stats *stats,
473 enum kunit_status status)
490 static void kunit_accumulate_stats(struct kunit_result_stats *total,
491 struct kunit_result_stats add)
493 total->passed += add.passed;
494 total->skipped += add.skipped;
495 total->failed += add.failed;
496 total->total += add.total;
499 int kunit_run_tests(struct kunit_suite *suite)
501 char param_desc[KUNIT_PARAM_DESC_SIZE];
502 struct kunit_case *test_case;
503 struct kunit_result_stats suite_stats = { 0 };
504 struct kunit_result_stats total_stats = { 0 };
506 /* Taint the kernel so we know we've run tests. */
507 add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
509 if (suite->suite_init) {
510 suite->suite_init_err = suite->suite_init(suite);
511 if (suite->suite_init_err) {
512 kunit_err(suite, KUNIT_SUBTEST_INDENT
513 "# failed to initialize (%d)", suite->suite_init_err);
518 kunit_print_suite_start(suite);
520 kunit_suite_for_each_test_case(suite, test_case) {
521 struct kunit test = { .param_value = NULL, .param_index = 0 };
522 struct kunit_result_stats param_stats = { 0 };
523 test_case->status = KUNIT_SKIPPED;
525 if (!test_case->generate_params) {
526 /* Non-parameterised test. */
527 kunit_run_case_catch_errors(suite, test_case, &test);
528 kunit_update_stats(¶m_stats, test.status);
530 /* Get initial param. */
531 param_desc[0] = '\0';
532 test.param_value = test_case->generate_params(NULL, param_desc);
533 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
534 "# Subtest: %s", test_case->name);
536 while (test.param_value) {
537 kunit_run_case_catch_errors(suite, test_case, &test);
539 if (param_desc[0] == '\0') {
540 snprintf(param_desc, sizeof(param_desc),
541 "param-%d", test.param_index);
544 kunit_log(KERN_INFO, &test,
545 KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
547 kunit_status_to_ok_not_ok(test.status),
548 test.param_index + 1, param_desc);
550 /* Get next param. */
551 param_desc[0] = '\0';
552 test.param_value = test_case->generate_params(test.param_value, param_desc);
555 kunit_update_stats(¶m_stats, test.status);
560 kunit_print_test_stats(&test, param_stats);
562 kunit_print_ok_not_ok(&test, true, test_case->status,
563 kunit_test_case_num(suite, test_case),
565 test.status_comment);
567 kunit_update_stats(&suite_stats, test_case->status);
568 kunit_accumulate_stats(&total_stats, param_stats);
571 if (suite->suite_exit)
572 suite->suite_exit(suite);
574 kunit_print_suite_stats(suite, suite_stats, total_stats);
576 kunit_print_suite_end(suite);
580 EXPORT_SYMBOL_GPL(kunit_run_tests);
582 static void kunit_init_suite(struct kunit_suite *suite)
584 kunit_debugfs_create_suite(suite);
585 suite->status_comment[0] = '\0';
586 suite->suite_init_err = 0;
589 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites)
593 for (i = 0; i < num_suites; i++) {
594 kunit_init_suite(suites[i]);
595 kunit_run_tests(suites[i]);
599 EXPORT_SYMBOL_GPL(__kunit_test_suites_init);
601 static void kunit_exit_suite(struct kunit_suite *suite)
603 kunit_debugfs_destroy_suite(suite);
606 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites)
610 for (i = 0; i < num_suites; i++)
611 kunit_exit_suite(suites[i]);
613 kunit_suite_counter = 1;
615 EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
617 #ifdef CONFIG_MODULES
618 static void kunit_module_init(struct module *mod)
620 __kunit_test_suites_init(mod->kunit_suites, mod->num_kunit_suites);
623 static void kunit_module_exit(struct module *mod)
625 __kunit_test_suites_exit(mod->kunit_suites, mod->num_kunit_suites);
628 static int kunit_module_notify(struct notifier_block *nb, unsigned long val,
631 struct module *mod = data;
634 case MODULE_STATE_LIVE:
635 kunit_module_init(mod);
637 case MODULE_STATE_GOING:
638 kunit_module_exit(mod);
640 case MODULE_STATE_COMING:
641 case MODULE_STATE_UNFORMED:
648 static struct notifier_block kunit_mod_nb = {
649 .notifier_call = kunit_module_notify,
654 struct kunit_kmalloc_array_params {
660 static int kunit_kmalloc_array_init(struct kunit_resource *res, void *context)
662 struct kunit_kmalloc_array_params *params = context;
664 res->data = kmalloc_array(params->n, params->size, params->gfp);
671 static void kunit_kmalloc_array_free(struct kunit_resource *res)
676 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp)
678 struct kunit_kmalloc_array_params params = {
684 return kunit_alloc_resource(test,
685 kunit_kmalloc_array_init,
686 kunit_kmalloc_array_free,
690 EXPORT_SYMBOL_GPL(kunit_kmalloc_array);
692 void kunit_kfree(struct kunit *test, const void *ptr)
694 struct kunit_resource *res;
696 res = kunit_find_resource(test, kunit_resource_instance_match,
700 * Removing the resource from the list of resources drops the
701 * reference count to 1; the final put will trigger the free.
703 kunit_remove_resource(test, res);
705 kunit_put_resource(res);
708 EXPORT_SYMBOL_GPL(kunit_kfree);
710 void kunit_cleanup(struct kunit *test)
712 struct kunit_resource *res;
716 * test->resources is a stack - each allocation must be freed in the
717 * reverse order from which it was added since one resource may depend
718 * on another for its entire lifetime.
719 * Also, we cannot use the normal list_for_each constructs, even the
720 * safe ones because *arbitrary* nodes may be deleted when
721 * kunit_resource_free is called; the list_for_each_safe variants only
722 * protect against the current node being deleted, not the next.
725 spin_lock_irqsave(&test->lock, flags);
726 if (list_empty(&test->resources)) {
727 spin_unlock_irqrestore(&test->lock, flags);
730 res = list_last_entry(&test->resources,
731 struct kunit_resource,
734 * Need to unlock here as a resource may remove another
735 * resource, and this can't happen if the test->lock
738 spin_unlock_irqrestore(&test->lock, flags);
739 kunit_remove_resource(test, res);
741 current->kunit_test = NULL;
743 EXPORT_SYMBOL_GPL(kunit_cleanup);
745 static int __init kunit_init(void)
747 kunit_debugfs_init();
748 #ifdef CONFIG_MODULES
749 return register_module_notifier(&kunit_mod_nb);
754 late_initcall(kunit_init);
756 static void __exit kunit_exit(void)
758 #ifdef CONFIG_MODULES
759 unregister_module_notifier(&kunit_mod_nb);
761 kunit_debugfs_cleanup();
763 module_exit(kunit_exit);
765 MODULE_LICENSE("GPL v2");