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 <kunit/attributes.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/panic.h>
17 #include <linux/sched/debug.h>
18 #include <linux/sched.h>
21 #include "hooks-impl.h"
22 #include "string-stream.h"
23 #include "try-catch-impl.h"
26 * Hook to fail the current test and print an error message to the log.
28 void __printf(3, 4) __kunit_fail_current_test_impl(const char *file, int line, const char *fmt, ...)
34 if (!current->kunit_test)
37 kunit_set_failure(current->kunit_test);
39 /* kunit_err() only accepts literals, so evaluate the args first. */
41 len = vsnprintf(NULL, 0, fmt, args) + 1;
44 buffer = kunit_kmalloc(current->kunit_test, len, GFP_KERNEL);
49 vsnprintf(buffer, len, fmt, args);
52 kunit_err(current->kunit_test, "%s:%d: %s", file, line, buffer);
53 kunit_kfree(current->kunit_test, buffer);
57 * Enable KUnit tests to run.
59 #ifdef CONFIG_KUNIT_DEFAULT_ENABLED
60 static bool enable_param = true;
62 static bool enable_param;
64 module_param_named(enable, enable_param, bool, 0);
65 MODULE_PARM_DESC(enable, "Enable KUnit tests");
68 * KUnit statistic mode:
70 * 1 - only when there is more than one subtest
73 static int kunit_stats_enabled = 1;
74 module_param_named(stats_enabled, kunit_stats_enabled, int, 0644);
75 MODULE_PARM_DESC(stats_enabled,
76 "Print test stats: never (0), only for multiple subtests (1), or always (2)");
78 struct kunit_result_stats {
80 unsigned long skipped;
85 static bool kunit_should_print_stats(struct kunit_result_stats stats)
87 if (kunit_stats_enabled == 0)
90 if (kunit_stats_enabled == 2)
93 return (stats.total > 1);
96 static void kunit_print_test_stats(struct kunit *test,
97 struct kunit_result_stats stats)
99 if (!kunit_should_print_stats(stats))
102 kunit_log(KERN_INFO, test,
104 "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
112 /* Append formatted message to log. */
113 void kunit_log_append(struct string_stream *log, const char *fmt, ...)
121 string_stream_vadd(log, fmt, args);
124 EXPORT_SYMBOL_GPL(kunit_log_append);
126 size_t kunit_suite_num_test_cases(struct kunit_suite *suite)
128 struct kunit_case *test_case;
131 kunit_suite_for_each_test_case(suite, test_case)
136 EXPORT_SYMBOL_GPL(kunit_suite_num_test_cases);
138 /* Currently supported test levels */
140 KUNIT_LEVEL_SUITE = 0,
142 KUNIT_LEVEL_CASE_PARAM,
145 static void kunit_print_suite_start(struct kunit_suite *suite)
148 * We do not log the test suite header as doing so would
149 * mean debugfs display would consist of the test suite
150 * header prior to individual test results.
151 * Hence directly printk the suite status, and we will
152 * separately seq_printf() the suite header for the debugfs
155 pr_info(KUNIT_SUBTEST_INDENT "KTAP version 1\n");
156 pr_info(KUNIT_SUBTEST_INDENT "# Subtest: %s\n",
158 kunit_print_attr((void *)suite, false, KUNIT_LEVEL_CASE);
159 pr_info(KUNIT_SUBTEST_INDENT "1..%zd\n",
160 kunit_suite_num_test_cases(suite));
163 static void kunit_print_ok_not_ok(struct kunit *test,
164 unsigned int test_level,
165 enum kunit_status status,
167 const char *description,
168 const char *directive)
170 const char *directive_header = (status == KUNIT_SKIPPED) ? " # SKIP " : "";
171 const char *directive_body = (status == KUNIT_SKIPPED) ? directive : "";
174 * When test is NULL assume that results are from the suite
175 * and today suite results are expected at level 0 only.
177 WARN(!test && test_level, "suite test level can't be %u!\n", test_level);
180 * We do not log the test suite results as doing so would
181 * mean debugfs display would consist of an incorrect test
182 * number. Hence directly printk the suite result, and we will
183 * separately seq_printf() the suite results for the debugfs
187 pr_info("%s %zd %s%s%s\n",
188 kunit_status_to_ok_not_ok(status),
189 test_number, description, directive_header,
192 kunit_log(KERN_INFO, test,
194 KUNIT_INDENT_LEN * test_level, "",
195 kunit_status_to_ok_not_ok(status),
196 test_number, description, directive_header,
200 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite)
202 const struct kunit_case *test_case;
203 enum kunit_status status = KUNIT_SKIPPED;
205 if (suite->suite_init_err)
206 return KUNIT_FAILURE;
208 kunit_suite_for_each_test_case(suite, test_case) {
209 if (test_case->status == KUNIT_FAILURE)
210 return KUNIT_FAILURE;
211 else if (test_case->status == KUNIT_SUCCESS)
212 status = KUNIT_SUCCESS;
217 EXPORT_SYMBOL_GPL(kunit_suite_has_succeeded);
219 static size_t kunit_suite_counter = 1;
221 static void kunit_print_suite_end(struct kunit_suite *suite)
223 kunit_print_ok_not_ok(NULL, KUNIT_LEVEL_SUITE,
224 kunit_suite_has_succeeded(suite),
225 kunit_suite_counter++,
227 suite->status_comment);
230 unsigned int kunit_test_case_num(struct kunit_suite *suite,
231 struct kunit_case *test_case)
233 struct kunit_case *tc;
236 kunit_suite_for_each_test_case(suite, tc) {
244 EXPORT_SYMBOL_GPL(kunit_test_case_num);
246 static void kunit_print_string_stream(struct kunit *test,
247 struct string_stream *stream)
249 struct string_stream_fragment *fragment;
252 if (string_stream_is_empty(stream))
255 buf = string_stream_get_string(stream);
258 "Could not allocate buffer, dumping stream:\n");
259 list_for_each_entry(fragment, &stream->fragments, node) {
260 kunit_err(test, "%s", fragment->fragment);
262 kunit_err(test, "\n");
264 kunit_err(test, "%s", buf);
269 static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
270 enum kunit_assert_type type, const struct kunit_assert *assert,
271 assert_format_t assert_format, const struct va_format *message)
273 struct string_stream *stream;
275 kunit_set_failure(test);
277 stream = kunit_alloc_string_stream(test, GFP_KERNEL);
278 if (IS_ERR(stream)) {
280 "Could not allocate stream to print failed assertion in %s:%d\n",
286 kunit_assert_prologue(loc, type, stream);
287 assert_format(assert, message, stream);
289 kunit_print_string_stream(test, stream);
291 kunit_free_string_stream(test, stream);
294 void __noreturn __kunit_abort(struct kunit *test)
296 kunit_try_catch_throw(&test->try_catch); /* Does not return. */
299 * Throw could not abort from test.
301 * XXX: we should never reach this line! As kunit_try_catch_throw is
304 WARN_ONCE(true, "Throw could not abort from test!\n");
306 EXPORT_SYMBOL_GPL(__kunit_abort);
308 void __kunit_do_failed_assertion(struct kunit *test,
309 const struct kunit_loc *loc,
310 enum kunit_assert_type type,
311 const struct kunit_assert *assert,
312 assert_format_t assert_format,
313 const char *fmt, ...)
316 struct va_format message;
322 kunit_fail(test, loc, type, assert, assert_format, &message);
326 EXPORT_SYMBOL_GPL(__kunit_do_failed_assertion);
328 void kunit_init_test(struct kunit *test, const char *name, struct string_stream *log)
330 spin_lock_init(&test->lock);
331 INIT_LIST_HEAD(&test->resources);
335 string_stream_clear(log);
336 test->status = KUNIT_SUCCESS;
337 test->status_comment[0] = '\0';
339 EXPORT_SYMBOL_GPL(kunit_init_test);
342 * Initializes and runs test case. Does not clean up or do post validations.
344 static void kunit_run_case_internal(struct kunit *test,
345 struct kunit_suite *suite,
346 struct kunit_case *test_case)
351 ret = suite->init(test);
353 kunit_err(test, "failed to initialize: %d\n", ret);
354 kunit_set_failure(test);
359 test_case->run_case(test);
362 static void kunit_case_internal_cleanup(struct kunit *test)
368 * Performs post validations and cleanup after a test case was run.
369 * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
371 static void kunit_run_case_cleanup(struct kunit *test,
372 struct kunit_suite *suite)
377 kunit_case_internal_cleanup(test);
380 struct kunit_try_catch_context {
382 struct kunit_suite *suite;
383 struct kunit_case *test_case;
386 static void kunit_try_run_case(void *data)
388 struct kunit_try_catch_context *ctx = data;
389 struct kunit *test = ctx->test;
390 struct kunit_suite *suite = ctx->suite;
391 struct kunit_case *test_case = ctx->test_case;
393 current->kunit_test = test;
396 * kunit_run_case_internal may encounter a fatal error; if it does,
397 * abort will be called, this thread will exit, and finally the parent
398 * thread will resume control and handle any necessary clean up.
400 kunit_run_case_internal(test, suite, test_case);
403 static void kunit_try_run_case_cleanup(void *data)
405 struct kunit_try_catch_context *ctx = data;
406 struct kunit *test = ctx->test;
407 struct kunit_suite *suite = ctx->suite;
409 current->kunit_test = test;
411 kunit_run_case_cleanup(test, suite);
414 static void kunit_catch_run_case_cleanup(void *data)
416 struct kunit_try_catch_context *ctx = data;
417 struct kunit *test = ctx->test;
418 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
420 /* It is always a failure if cleanup aborts. */
421 kunit_set_failure(test);
425 * Test case could not finish, we have no idea what state it is
426 * in, so don't do clean up.
428 if (try_exit_code == -ETIMEDOUT) {
429 kunit_err(test, "test case cleanup timed out\n");
431 * Unknown internal error occurred preventing test case from
432 * running, so there is nothing to clean up.
435 kunit_err(test, "internal error occurred during test case cleanup: %d\n",
441 kunit_err(test, "test aborted during cleanup. continuing without cleaning up\n");
445 static void kunit_catch_run_case(void *data)
447 struct kunit_try_catch_context *ctx = data;
448 struct kunit *test = ctx->test;
449 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
452 kunit_set_failure(test);
454 * Test case could not finish, we have no idea what state it is
455 * in, so don't do clean up.
457 if (try_exit_code == -ETIMEDOUT) {
458 kunit_err(test, "test case timed out\n");
460 * Unknown internal error occurred preventing test case from
461 * running, so there is nothing to clean up.
464 kunit_err(test, "internal error occurred preventing test case from running: %d\n",
472 * Performs all logic to run a test case. It also catches most errors that
473 * occur in a test case and reports them as failures.
475 static void kunit_run_case_catch_errors(struct kunit_suite *suite,
476 struct kunit_case *test_case,
479 struct kunit_try_catch_context context;
480 struct kunit_try_catch *try_catch;
482 try_catch = &test->try_catch;
484 kunit_try_catch_init(try_catch,
487 kunit_catch_run_case);
489 context.suite = suite;
490 context.test_case = test_case;
491 kunit_try_catch_run(try_catch, &context);
493 /* Now run the cleanup */
494 kunit_try_catch_init(try_catch,
496 kunit_try_run_case_cleanup,
497 kunit_catch_run_case_cleanup);
498 kunit_try_catch_run(try_catch, &context);
500 /* Propagate the parameter result to the test case. */
501 if (test->status == KUNIT_FAILURE)
502 test_case->status = KUNIT_FAILURE;
503 else if (test_case->status != KUNIT_FAILURE && test->status == KUNIT_SUCCESS)
504 test_case->status = KUNIT_SUCCESS;
507 static void kunit_print_suite_stats(struct kunit_suite *suite,
508 struct kunit_result_stats suite_stats,
509 struct kunit_result_stats param_stats)
511 if (kunit_should_print_stats(suite_stats)) {
512 kunit_log(KERN_INFO, suite,
513 "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
521 if (kunit_should_print_stats(param_stats)) {
522 kunit_log(KERN_INFO, suite,
523 "# Totals: pass:%lu fail:%lu skip:%lu total:%lu",
531 static void kunit_update_stats(struct kunit_result_stats *stats,
532 enum kunit_status status)
549 static void kunit_accumulate_stats(struct kunit_result_stats *total,
550 struct kunit_result_stats add)
552 total->passed += add.passed;
553 total->skipped += add.skipped;
554 total->failed += add.failed;
555 total->total += add.total;
558 int kunit_run_tests(struct kunit_suite *suite)
560 char param_desc[KUNIT_PARAM_DESC_SIZE];
561 struct kunit_case *test_case;
562 struct kunit_result_stats suite_stats = { 0 };
563 struct kunit_result_stats total_stats = { 0 };
565 /* Taint the kernel so we know we've run tests. */
566 add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
568 if (suite->suite_init) {
569 suite->suite_init_err = suite->suite_init(suite);
570 if (suite->suite_init_err) {
571 kunit_err(suite, KUNIT_SUBTEST_INDENT
572 "# failed to initialize (%d)", suite->suite_init_err);
577 kunit_print_suite_start(suite);
579 kunit_suite_for_each_test_case(suite, test_case) {
580 struct kunit test = { .param_value = NULL, .param_index = 0 };
581 struct kunit_result_stats param_stats = { 0 };
583 kunit_init_test(&test, test_case->name, test_case->log);
584 if (test_case->status == KUNIT_SKIPPED) {
585 /* Test marked as skip */
586 test.status = KUNIT_SKIPPED;
587 kunit_update_stats(¶m_stats, test.status);
588 } else if (!test_case->generate_params) {
589 /* Non-parameterised test. */
590 test_case->status = KUNIT_SKIPPED;
591 kunit_run_case_catch_errors(suite, test_case, &test);
592 kunit_update_stats(¶m_stats, test.status);
594 /* Get initial param. */
595 param_desc[0] = '\0';
596 test.param_value = test_case->generate_params(NULL, param_desc);
597 test_case->status = KUNIT_SKIPPED;
598 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
600 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
601 "# Subtest: %s", test_case->name);
603 while (test.param_value) {
604 kunit_run_case_catch_errors(suite, test_case, &test);
606 if (param_desc[0] == '\0') {
607 snprintf(param_desc, sizeof(param_desc),
608 "param-%d", test.param_index);
611 kunit_print_ok_not_ok(&test, KUNIT_LEVEL_CASE_PARAM,
613 test.param_index + 1,
615 test.status_comment);
617 kunit_update_stats(¶m_stats, test.status);
619 /* Get next param. */
620 param_desc[0] = '\0';
621 test.param_value = test_case->generate_params(test.param_value, param_desc);
623 test.status = KUNIT_SUCCESS;
624 test.status_comment[0] = '\0';
628 kunit_print_attr((void *)test_case, true, KUNIT_LEVEL_CASE);
630 kunit_print_test_stats(&test, param_stats);
632 kunit_print_ok_not_ok(&test, KUNIT_LEVEL_CASE, test_case->status,
633 kunit_test_case_num(suite, test_case),
635 test.status_comment);
637 kunit_update_stats(&suite_stats, test_case->status);
638 kunit_accumulate_stats(&total_stats, param_stats);
641 if (suite->suite_exit)
642 suite->suite_exit(suite);
644 kunit_print_suite_stats(suite, suite_stats, total_stats);
646 kunit_print_suite_end(suite);
650 EXPORT_SYMBOL_GPL(kunit_run_tests);
652 static void kunit_init_suite(struct kunit_suite *suite)
654 kunit_debugfs_create_suite(suite);
655 suite->status_comment[0] = '\0';
656 suite->suite_init_err = 0;
659 bool kunit_enabled(void)
664 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites)
668 if (!kunit_enabled() && num_suites > 0) {
669 pr_info("kunit: disabled\n");
673 static_branch_inc(&kunit_running);
675 for (i = 0; i < num_suites; i++) {
676 kunit_init_suite(suites[i]);
677 kunit_run_tests(suites[i]);
680 static_branch_dec(&kunit_running);
683 EXPORT_SYMBOL_GPL(__kunit_test_suites_init);
685 static void kunit_exit_suite(struct kunit_suite *suite)
687 kunit_debugfs_destroy_suite(suite);
690 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites)
694 if (!kunit_enabled())
697 for (i = 0; i < num_suites; i++)
698 kunit_exit_suite(suites[i]);
700 kunit_suite_counter = 1;
702 EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
704 #ifdef CONFIG_MODULES
705 static void kunit_module_init(struct module *mod)
707 struct kunit_suite_set suite_set = {
708 mod->kunit_suites, mod->kunit_suites + mod->num_kunit_suites,
710 const char *action = kunit_action();
713 suite_set = kunit_filter_suites(&suite_set,
714 kunit_filter_glob() ?: "*.*",
715 kunit_filter(), kunit_filter_action(),
718 pr_err("kunit module: error filtering suites: %d\n", err);
720 mod->kunit_suites = (struct kunit_suite **)suite_set.start;
721 mod->num_kunit_suites = suite_set.end - suite_set.start;
724 kunit_exec_run_tests(&suite_set, false);
725 else if (!strcmp(action, "list"))
726 kunit_exec_list_tests(&suite_set, false);
727 else if (!strcmp(action, "list_attr"))
728 kunit_exec_list_tests(&suite_set, true);
730 pr_err("kunit: unknown action '%s'\n", action);
733 static void kunit_module_exit(struct module *mod)
735 struct kunit_suite_set suite_set = {
736 mod->kunit_suites, mod->kunit_suites + mod->num_kunit_suites,
738 const char *action = kunit_action();
741 __kunit_test_suites_exit(mod->kunit_suites,
742 mod->num_kunit_suites);
745 kunit_free_suite_set(suite_set);
748 static int kunit_module_notify(struct notifier_block *nb, unsigned long val,
751 struct module *mod = data;
754 case MODULE_STATE_LIVE:
756 case MODULE_STATE_GOING:
757 kunit_module_exit(mod);
759 case MODULE_STATE_COMING:
760 kunit_module_init(mod);
762 case MODULE_STATE_UNFORMED:
769 static struct notifier_block kunit_mod_nb = {
770 .notifier_call = kunit_module_notify,
775 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp)
779 data = kmalloc_array(n, size, gfp);
784 if (kunit_add_action_or_reset(test, (kunit_action_t *)kfree, data) != 0)
789 EXPORT_SYMBOL_GPL(kunit_kmalloc_array);
791 void kunit_kfree(struct kunit *test, const void *ptr)
796 kunit_release_action(test, (kunit_action_t *)kfree, (void *)ptr);
798 EXPORT_SYMBOL_GPL(kunit_kfree);
800 void kunit_cleanup(struct kunit *test)
802 struct kunit_resource *res;
806 * test->resources is a stack - each allocation must be freed in the
807 * reverse order from which it was added since one resource may depend
808 * on another for its entire lifetime.
809 * Also, we cannot use the normal list_for_each constructs, even the
810 * safe ones because *arbitrary* nodes may be deleted when
811 * kunit_resource_free is called; the list_for_each_safe variants only
812 * protect against the current node being deleted, not the next.
815 spin_lock_irqsave(&test->lock, flags);
816 if (list_empty(&test->resources)) {
817 spin_unlock_irqrestore(&test->lock, flags);
820 res = list_last_entry(&test->resources,
821 struct kunit_resource,
824 * Need to unlock here as a resource may remove another
825 * resource, and this can't happen if the test->lock
828 spin_unlock_irqrestore(&test->lock, flags);
829 kunit_remove_resource(test, res);
831 current->kunit_test = NULL;
833 EXPORT_SYMBOL_GPL(kunit_cleanup);
835 static int __init kunit_init(void)
837 /* Install the KUnit hook functions. */
838 kunit_install_hooks();
840 kunit_debugfs_init();
841 #ifdef CONFIG_MODULES
842 return register_module_notifier(&kunit_mod_nb);
847 late_initcall(kunit_init);
849 static void __exit kunit_exit(void)
851 memset(&kunit_hooks, 0, sizeof(kunit_hooks));
852 #ifdef CONFIG_MODULES
853 unregister_module_notifier(&kunit_mod_nb);
855 kunit_debugfs_cleanup();
857 module_exit(kunit_exit);
859 MODULE_LICENSE("GPL v2");