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/mutex.h>
17 #include <linux/panic.h>
18 #include <linux/sched/debug.h>
19 #include <linux/sched.h>
22 #include "device-impl.h"
23 #include "hooks-impl.h"
24 #include "string-stream.h"
25 #include "try-catch-impl.h"
27 static DEFINE_MUTEX(kunit_run_lock);
30 * Hook to fail the current test and print an error message to the log.
32 void __printf(3, 4) __kunit_fail_current_test_impl(const char *file, int line, const char *fmt, ...)
38 if (!current->kunit_test)
41 kunit_set_failure(current->kunit_test);
43 /* kunit_err() only accepts literals, so evaluate the args first. */
45 len = vsnprintf(NULL, 0, fmt, args) + 1;
48 buffer = kunit_kmalloc(current->kunit_test, len, GFP_KERNEL);
53 vsnprintf(buffer, len, fmt, args);
56 kunit_err(current->kunit_test, "%s:%d: %s", file, line, buffer);
57 kunit_kfree(current->kunit_test, buffer);
61 * Enable KUnit tests to run.
63 #ifdef CONFIG_KUNIT_DEFAULT_ENABLED
64 static bool enable_param = true;
66 static bool enable_param;
68 module_param_named(enable, enable_param, bool, 0);
69 MODULE_PARM_DESC(enable, "Enable KUnit tests");
72 * KUnit statistic mode:
74 * 1 - only when there is more than one subtest
77 static int kunit_stats_enabled = 1;
78 module_param_named(stats_enabled, kunit_stats_enabled, int, 0644);
79 MODULE_PARM_DESC(stats_enabled,
80 "Print test stats: never (0), only for multiple subtests (1), or always (2)");
82 struct kunit_result_stats {
84 unsigned long skipped;
89 static bool kunit_should_print_stats(struct kunit_result_stats stats)
91 if (kunit_stats_enabled == 0)
94 if (kunit_stats_enabled == 2)
97 return (stats.total > 1);
100 static void kunit_print_test_stats(struct kunit *test,
101 struct kunit_result_stats stats)
103 if (!kunit_should_print_stats(stats))
106 kunit_log(KERN_INFO, test,
108 "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
116 /* Append formatted message to log. */
117 void kunit_log_append(struct string_stream *log, const char *fmt, ...)
125 string_stream_vadd(log, fmt, args);
128 EXPORT_SYMBOL_GPL(kunit_log_append);
130 size_t kunit_suite_num_test_cases(struct kunit_suite *suite)
132 struct kunit_case *test_case;
135 kunit_suite_for_each_test_case(suite, test_case)
140 EXPORT_SYMBOL_GPL(kunit_suite_num_test_cases);
142 /* Currently supported test levels */
144 KUNIT_LEVEL_SUITE = 0,
146 KUNIT_LEVEL_CASE_PARAM,
149 static void kunit_print_suite_start(struct kunit_suite *suite)
152 * We do not log the test suite header as doing so would
153 * mean debugfs display would consist of the test suite
154 * header prior to individual test results.
155 * Hence directly printk the suite status, and we will
156 * separately seq_printf() the suite header for the debugfs
159 pr_info(KUNIT_SUBTEST_INDENT "KTAP version 1\n");
160 pr_info(KUNIT_SUBTEST_INDENT "# Subtest: %s\n",
162 kunit_print_attr((void *)suite, false, KUNIT_LEVEL_CASE);
163 pr_info(KUNIT_SUBTEST_INDENT "1..%zd\n",
164 kunit_suite_num_test_cases(suite));
167 static void kunit_print_ok_not_ok(struct kunit *test,
168 unsigned int test_level,
169 enum kunit_status status,
171 const char *description,
172 const char *directive)
174 const char *directive_header = (status == KUNIT_SKIPPED) ? " # SKIP " : "";
175 const char *directive_body = (status == KUNIT_SKIPPED) ? directive : "";
178 * When test is NULL assume that results are from the suite
179 * and today suite results are expected at level 0 only.
181 WARN(!test && test_level, "suite test level can't be %u!\n", test_level);
184 * We do not log the test suite results as doing so would
185 * mean debugfs display would consist of an incorrect test
186 * number. Hence directly printk the suite result, and we will
187 * separately seq_printf() the suite results for the debugfs
191 pr_info("%s %zd %s%s%s\n",
192 kunit_status_to_ok_not_ok(status),
193 test_number, description, directive_header,
196 kunit_log(KERN_INFO, test,
198 KUNIT_INDENT_LEN * test_level, "",
199 kunit_status_to_ok_not_ok(status),
200 test_number, description, directive_header,
204 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite)
206 const struct kunit_case *test_case;
207 enum kunit_status status = KUNIT_SKIPPED;
209 if (suite->suite_init_err)
210 return KUNIT_FAILURE;
212 kunit_suite_for_each_test_case(suite, test_case) {
213 if (test_case->status == KUNIT_FAILURE)
214 return KUNIT_FAILURE;
215 else if (test_case->status == KUNIT_SUCCESS)
216 status = KUNIT_SUCCESS;
221 EXPORT_SYMBOL_GPL(kunit_suite_has_succeeded);
223 static size_t kunit_suite_counter = 1;
225 static void kunit_print_suite_end(struct kunit_suite *suite)
227 kunit_print_ok_not_ok(NULL, KUNIT_LEVEL_SUITE,
228 kunit_suite_has_succeeded(suite),
229 kunit_suite_counter++,
231 suite->status_comment);
234 unsigned int kunit_test_case_num(struct kunit_suite *suite,
235 struct kunit_case *test_case)
237 struct kunit_case *tc;
240 kunit_suite_for_each_test_case(suite, tc) {
248 EXPORT_SYMBOL_GPL(kunit_test_case_num);
250 static void kunit_print_string_stream(struct kunit *test,
251 struct string_stream *stream)
253 struct string_stream_fragment *fragment;
256 if (string_stream_is_empty(stream))
259 buf = string_stream_get_string(stream);
262 "Could not allocate buffer, dumping stream:\n");
263 list_for_each_entry(fragment, &stream->fragments, node) {
264 kunit_err(test, "%s", fragment->fragment);
266 kunit_err(test, "\n");
268 kunit_err(test, "%s", buf);
273 static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
274 enum kunit_assert_type type, const struct kunit_assert *assert,
275 assert_format_t assert_format, const struct va_format *message)
277 struct string_stream *stream;
279 kunit_set_failure(test);
281 stream = kunit_alloc_string_stream(test, GFP_KERNEL);
282 if (IS_ERR(stream)) {
284 "Could not allocate stream to print failed assertion in %s:%d\n",
290 kunit_assert_prologue(loc, type, stream);
291 assert_format(assert, message, stream);
293 kunit_print_string_stream(test, stream);
295 kunit_free_string_stream(test, stream);
298 void __noreturn __kunit_abort(struct kunit *test)
300 kunit_try_catch_throw(&test->try_catch); /* Does not return. */
303 * Throw could not abort from test.
305 * XXX: we should never reach this line! As kunit_try_catch_throw is
308 WARN_ONCE(true, "Throw could not abort from test!\n");
310 EXPORT_SYMBOL_GPL(__kunit_abort);
312 void __kunit_do_failed_assertion(struct kunit *test,
313 const struct kunit_loc *loc,
314 enum kunit_assert_type type,
315 const struct kunit_assert *assert,
316 assert_format_t assert_format,
317 const char *fmt, ...)
320 struct va_format message;
326 kunit_fail(test, loc, type, assert, assert_format, &message);
330 EXPORT_SYMBOL_GPL(__kunit_do_failed_assertion);
332 void kunit_init_test(struct kunit *test, const char *name, struct string_stream *log)
334 spin_lock_init(&test->lock);
335 INIT_LIST_HEAD(&test->resources);
339 string_stream_clear(log);
340 test->status = KUNIT_SUCCESS;
341 test->status_comment[0] = '\0';
343 EXPORT_SYMBOL_GPL(kunit_init_test);
345 /* Only warn when a test takes more than twice the threshold */
346 #define KUNIT_SPEED_WARNING_MULTIPLIER 2
348 /* Slow tests are defined as taking more than 1s */
349 #define KUNIT_SPEED_SLOW_THRESHOLD_S 1
351 #define KUNIT_SPEED_SLOW_WARNING_THRESHOLD_S \
352 (KUNIT_SPEED_WARNING_MULTIPLIER * KUNIT_SPEED_SLOW_THRESHOLD_S)
354 #define s_to_timespec64(s) ns_to_timespec64((s) * NSEC_PER_SEC)
356 static void kunit_run_case_check_speed(struct kunit *test,
357 struct kunit_case *test_case,
358 struct timespec64 duration)
360 struct timespec64 slow_thr =
361 s_to_timespec64(KUNIT_SPEED_SLOW_WARNING_THRESHOLD_S);
362 enum kunit_speed speed = test_case->attr.speed;
364 if (timespec64_compare(&duration, &slow_thr) < 0)
367 if (speed == KUNIT_SPEED_VERY_SLOW || speed == KUNIT_SPEED_SLOW)
371 "Test should be marked slow (runtime: %lld.%09lds)",
372 duration.tv_sec, duration.tv_nsec);
376 * Initializes and runs test case. Does not clean up or do post validations.
378 static void kunit_run_case_internal(struct kunit *test,
379 struct kunit_suite *suite,
380 struct kunit_case *test_case)
382 struct timespec64 start, end;
387 ret = suite->init(test);
389 kunit_err(test, "failed to initialize: %d\n", ret);
390 kunit_set_failure(test);
395 ktime_get_ts64(&start);
397 test_case->run_case(test);
399 ktime_get_ts64(&end);
401 kunit_run_case_check_speed(test, test_case, timespec64_sub(end, start));
404 static void kunit_case_internal_cleanup(struct kunit *test)
410 * Performs post validations and cleanup after a test case was run.
411 * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
413 static void kunit_run_case_cleanup(struct kunit *test,
414 struct kunit_suite *suite)
419 kunit_case_internal_cleanup(test);
422 struct kunit_try_catch_context {
424 struct kunit_suite *suite;
425 struct kunit_case *test_case;
428 static void kunit_try_run_case(void *data)
430 struct kunit_try_catch_context *ctx = data;
431 struct kunit *test = ctx->test;
432 struct kunit_suite *suite = ctx->suite;
433 struct kunit_case *test_case = ctx->test_case;
435 current->kunit_test = test;
438 * kunit_run_case_internal may encounter a fatal error; if it does,
439 * abort will be called, this thread will exit, and finally the parent
440 * thread will resume control and handle any necessary clean up.
442 kunit_run_case_internal(test, suite, test_case);
445 static void kunit_try_run_case_cleanup(void *data)
447 struct kunit_try_catch_context *ctx = data;
448 struct kunit *test = ctx->test;
449 struct kunit_suite *suite = ctx->suite;
451 current->kunit_test = test;
453 kunit_run_case_cleanup(test, suite);
456 static void kunit_catch_run_case_cleanup(void *data)
458 struct kunit_try_catch_context *ctx = data;
459 struct kunit *test = ctx->test;
460 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
462 /* It is always a failure if cleanup aborts. */
463 kunit_set_failure(test);
467 * Test case could not finish, we have no idea what state it is
468 * in, so don't do clean up.
470 if (try_exit_code == -ETIMEDOUT) {
471 kunit_err(test, "test case cleanup timed out\n");
473 * Unknown internal error occurred preventing test case from
474 * running, so there is nothing to clean up.
477 kunit_err(test, "internal error occurred during test case cleanup: %d\n",
483 kunit_err(test, "test aborted during cleanup. continuing without cleaning up\n");
487 static void kunit_catch_run_case(void *data)
489 struct kunit_try_catch_context *ctx = data;
490 struct kunit *test = ctx->test;
491 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
494 kunit_set_failure(test);
496 * Test case could not finish, we have no idea what state it is
497 * in, so don't do clean up.
499 if (try_exit_code == -ETIMEDOUT) {
500 kunit_err(test, "test case timed out\n");
502 * Unknown internal error occurred preventing test case from
503 * running, so there is nothing to clean up.
506 kunit_err(test, "internal error occurred preventing test case from running: %d\n",
514 * Performs all logic to run a test case. It also catches most errors that
515 * occur in a test case and reports them as failures.
517 static void kunit_run_case_catch_errors(struct kunit_suite *suite,
518 struct kunit_case *test_case,
521 struct kunit_try_catch_context context;
522 struct kunit_try_catch *try_catch;
524 try_catch = &test->try_catch;
526 kunit_try_catch_init(try_catch,
529 kunit_catch_run_case);
531 context.suite = suite;
532 context.test_case = test_case;
533 kunit_try_catch_run(try_catch, &context);
535 /* Now run the cleanup */
536 kunit_try_catch_init(try_catch,
538 kunit_try_run_case_cleanup,
539 kunit_catch_run_case_cleanup);
540 kunit_try_catch_run(try_catch, &context);
542 /* Propagate the parameter result to the test case. */
543 if (test->status == KUNIT_FAILURE)
544 test_case->status = KUNIT_FAILURE;
545 else if (test_case->status != KUNIT_FAILURE && test->status == KUNIT_SUCCESS)
546 test_case->status = KUNIT_SUCCESS;
549 static void kunit_print_suite_stats(struct kunit_suite *suite,
550 struct kunit_result_stats suite_stats,
551 struct kunit_result_stats param_stats)
553 if (kunit_should_print_stats(suite_stats)) {
554 kunit_log(KERN_INFO, suite,
555 "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
563 if (kunit_should_print_stats(param_stats)) {
564 kunit_log(KERN_INFO, suite,
565 "# Totals: pass:%lu fail:%lu skip:%lu total:%lu",
573 static void kunit_update_stats(struct kunit_result_stats *stats,
574 enum kunit_status status)
591 static void kunit_accumulate_stats(struct kunit_result_stats *total,
592 struct kunit_result_stats add)
594 total->passed += add.passed;
595 total->skipped += add.skipped;
596 total->failed += add.failed;
597 total->total += add.total;
600 int kunit_run_tests(struct kunit_suite *suite)
602 char param_desc[KUNIT_PARAM_DESC_SIZE];
603 struct kunit_case *test_case;
604 struct kunit_result_stats suite_stats = { 0 };
605 struct kunit_result_stats total_stats = { 0 };
607 /* Taint the kernel so we know we've run tests. */
608 add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
610 if (suite->suite_init) {
611 suite->suite_init_err = suite->suite_init(suite);
612 if (suite->suite_init_err) {
613 kunit_err(suite, KUNIT_SUBTEST_INDENT
614 "# failed to initialize (%d)", suite->suite_init_err);
619 kunit_print_suite_start(suite);
621 kunit_suite_for_each_test_case(suite, test_case) {
622 struct kunit test = { .param_value = NULL, .param_index = 0 };
623 struct kunit_result_stats param_stats = { 0 };
625 kunit_init_test(&test, test_case->name, test_case->log);
626 if (test_case->status == KUNIT_SKIPPED) {
627 /* Test marked as skip */
628 test.status = KUNIT_SKIPPED;
629 kunit_update_stats(¶m_stats, test.status);
630 } else if (!test_case->generate_params) {
631 /* Non-parameterised test. */
632 test_case->status = KUNIT_SKIPPED;
633 kunit_run_case_catch_errors(suite, test_case, &test);
634 kunit_update_stats(¶m_stats, test.status);
636 /* Get initial param. */
637 param_desc[0] = '\0';
638 test.param_value = test_case->generate_params(NULL, param_desc);
639 test_case->status = KUNIT_SKIPPED;
640 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
642 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
643 "# Subtest: %s", test_case->name);
645 while (test.param_value) {
646 kunit_run_case_catch_errors(suite, test_case, &test);
648 if (param_desc[0] == '\0') {
649 snprintf(param_desc, sizeof(param_desc),
650 "param-%d", test.param_index);
653 kunit_print_ok_not_ok(&test, KUNIT_LEVEL_CASE_PARAM,
655 test.param_index + 1,
657 test.status_comment);
659 kunit_update_stats(¶m_stats, test.status);
661 /* Get next param. */
662 param_desc[0] = '\0';
663 test.param_value = test_case->generate_params(test.param_value, param_desc);
665 test.status = KUNIT_SUCCESS;
666 test.status_comment[0] = '\0';
671 kunit_print_attr((void *)test_case, true, KUNIT_LEVEL_CASE);
673 kunit_print_test_stats(&test, param_stats);
675 kunit_print_ok_not_ok(&test, KUNIT_LEVEL_CASE, test_case->status,
676 kunit_test_case_num(suite, test_case),
678 test.status_comment);
680 kunit_update_stats(&suite_stats, test_case->status);
681 kunit_accumulate_stats(&total_stats, param_stats);
684 if (suite->suite_exit)
685 suite->suite_exit(suite);
687 kunit_print_suite_stats(suite, suite_stats, total_stats);
689 kunit_print_suite_end(suite);
693 EXPORT_SYMBOL_GPL(kunit_run_tests);
695 static void kunit_init_suite(struct kunit_suite *suite)
697 kunit_debugfs_create_suite(suite);
698 suite->status_comment[0] = '\0';
699 suite->suite_init_err = 0;
702 string_stream_clear(suite->log);
705 bool kunit_enabled(void)
710 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites)
714 if (!kunit_enabled() && num_suites > 0) {
715 pr_info("kunit: disabled\n");
719 kunit_suite_counter = 1;
721 /* Use mutex lock to guard against running tests concurrently. */
722 if (mutex_lock_interruptible(&kunit_run_lock)) {
723 pr_err("kunit: test interrupted\n");
726 static_branch_inc(&kunit_running);
728 for (i = 0; i < num_suites; i++) {
729 kunit_init_suite(suites[i]);
730 kunit_run_tests(suites[i]);
733 static_branch_dec(&kunit_running);
734 mutex_unlock(&kunit_run_lock);
737 EXPORT_SYMBOL_GPL(__kunit_test_suites_init);
739 static void kunit_exit_suite(struct kunit_suite *suite)
741 kunit_debugfs_destroy_suite(suite);
744 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites)
748 if (!kunit_enabled())
751 for (i = 0; i < num_suites; i++)
752 kunit_exit_suite(suites[i]);
754 EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
756 #ifdef CONFIG_MODULES
757 static void kunit_module_init(struct module *mod)
759 struct kunit_suite_set suite_set, filtered_set;
760 struct kunit_suite_set normal_suite_set = {
761 mod->kunit_suites, mod->kunit_suites + mod->num_kunit_suites,
763 struct kunit_suite_set init_suite_set = {
764 mod->kunit_init_suites, mod->kunit_init_suites + mod->num_kunit_init_suites,
766 const char *action = kunit_action();
769 if (mod->num_kunit_init_suites > 0)
770 suite_set = kunit_merge_suite_sets(init_suite_set, normal_suite_set);
772 suite_set = normal_suite_set;
774 filtered_set = kunit_filter_suites(&suite_set,
775 kunit_filter_glob() ?: "*.*",
776 kunit_filter(), kunit_filter_action(),
779 pr_err("kunit module: error filtering suites: %d\n", err);
781 mod->kunit_suites = (struct kunit_suite **)filtered_set.start;
782 mod->num_kunit_suites = filtered_set.end - filtered_set.start;
784 if (mod->num_kunit_init_suites > 0)
785 kfree(suite_set.start);
788 kunit_exec_run_tests(&filtered_set, false);
789 else if (!strcmp(action, "list"))
790 kunit_exec_list_tests(&filtered_set, false);
791 else if (!strcmp(action, "list_attr"))
792 kunit_exec_list_tests(&filtered_set, true);
794 pr_err("kunit: unknown action '%s'\n", action);
797 static void kunit_module_exit(struct module *mod)
799 struct kunit_suite_set suite_set = {
800 mod->kunit_suites, mod->kunit_suites + mod->num_kunit_suites,
802 const char *action = kunit_action();
805 __kunit_test_suites_exit(mod->kunit_suites,
806 mod->num_kunit_suites);
809 kunit_free_suite_set(suite_set);
812 static int kunit_module_notify(struct notifier_block *nb, unsigned long val,
815 struct module *mod = data;
818 case MODULE_STATE_LIVE:
820 case MODULE_STATE_GOING:
821 kunit_module_exit(mod);
823 case MODULE_STATE_COMING:
824 kunit_module_init(mod);
826 case MODULE_STATE_UNFORMED:
833 static struct notifier_block kunit_mod_nb = {
834 .notifier_call = kunit_module_notify,
839 KUNIT_DEFINE_ACTION_WRAPPER(kfree_action_wrapper, kfree, const void *)
841 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp)
845 data = kmalloc_array(n, size, gfp);
850 if (kunit_add_action_or_reset(test, kfree_action_wrapper, data) != 0)
855 EXPORT_SYMBOL_GPL(kunit_kmalloc_array);
857 void kunit_kfree(struct kunit *test, const void *ptr)
862 kunit_release_action(test, kfree_action_wrapper, (void *)ptr);
864 EXPORT_SYMBOL_GPL(kunit_kfree);
866 void kunit_cleanup(struct kunit *test)
868 struct kunit_resource *res;
872 * test->resources is a stack - each allocation must be freed in the
873 * reverse order from which it was added since one resource may depend
874 * on another for its entire lifetime.
875 * Also, we cannot use the normal list_for_each constructs, even the
876 * safe ones because *arbitrary* nodes may be deleted when
877 * kunit_resource_free is called; the list_for_each_safe variants only
878 * protect against the current node being deleted, not the next.
881 spin_lock_irqsave(&test->lock, flags);
882 if (list_empty(&test->resources)) {
883 spin_unlock_irqrestore(&test->lock, flags);
886 res = list_last_entry(&test->resources,
887 struct kunit_resource,
890 * Need to unlock here as a resource may remove another
891 * resource, and this can't happen if the test->lock
894 spin_unlock_irqrestore(&test->lock, flags);
895 kunit_remove_resource(test, res);
897 current->kunit_test = NULL;
899 EXPORT_SYMBOL_GPL(kunit_cleanup);
901 static int __init kunit_init(void)
903 /* Install the KUnit hook functions. */
904 kunit_install_hooks();
906 kunit_debugfs_init();
909 #ifdef CONFIG_MODULES
910 return register_module_notifier(&kunit_mod_nb);
915 late_initcall(kunit_init);
917 static void __exit kunit_exit(void)
919 memset(&kunit_hooks, 0, sizeof(kunit_hooks));
920 #ifdef CONFIG_MODULES
921 unregister_module_notifier(&kunit_mod_nb);
923 kunit_debugfs_cleanup();
925 module_exit(kunit_exit);
927 MODULE_LICENSE("GPL v2");