1 // SPDX-License-Identifier: GPL-2.0
3 * Base unit test (KUnit) API.
5 * Copyright (C) 2019, Google LLC.
6 * Author: Brendan Higgins <brendanhiggins@google.com>
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);
341 /* Only warn when a test takes more than twice the threshold */
342 #define KUNIT_SPEED_WARNING_MULTIPLIER 2
344 /* Slow tests are defined as taking more than 1s */
345 #define KUNIT_SPEED_SLOW_THRESHOLD_S 1
347 #define KUNIT_SPEED_SLOW_WARNING_THRESHOLD_S \
348 (KUNIT_SPEED_WARNING_MULTIPLIER * KUNIT_SPEED_SLOW_THRESHOLD_S)
350 #define s_to_timespec64(s) ns_to_timespec64((s) * NSEC_PER_SEC)
352 static void kunit_run_case_check_speed(struct kunit *test,
353 struct kunit_case *test_case,
354 struct timespec64 duration)
356 struct timespec64 slow_thr =
357 s_to_timespec64(KUNIT_SPEED_SLOW_WARNING_THRESHOLD_S);
358 enum kunit_speed speed = test_case->attr.speed;
360 if (timespec64_compare(&duration, &slow_thr) < 0)
363 if (speed == KUNIT_SPEED_VERY_SLOW || speed == KUNIT_SPEED_SLOW)
367 "Test should be marked slow (runtime: %lld.%09lds)",
368 duration.tv_sec, duration.tv_nsec);
372 * Initializes and runs test case. Does not clean up or do post validations.
374 static void kunit_run_case_internal(struct kunit *test,
375 struct kunit_suite *suite,
376 struct kunit_case *test_case)
378 struct timespec64 start, end;
383 ret = suite->init(test);
385 kunit_err(test, "failed to initialize: %d\n", ret);
386 kunit_set_failure(test);
391 ktime_get_ts64(&start);
393 test_case->run_case(test);
395 ktime_get_ts64(&end);
397 kunit_run_case_check_speed(test, test_case, timespec64_sub(end, start));
400 static void kunit_case_internal_cleanup(struct kunit *test)
406 * Performs post validations and cleanup after a test case was run.
407 * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
409 static void kunit_run_case_cleanup(struct kunit *test,
410 struct kunit_suite *suite)
415 kunit_case_internal_cleanup(test);
418 struct kunit_try_catch_context {
420 struct kunit_suite *suite;
421 struct kunit_case *test_case;
424 static void kunit_try_run_case(void *data)
426 struct kunit_try_catch_context *ctx = data;
427 struct kunit *test = ctx->test;
428 struct kunit_suite *suite = ctx->suite;
429 struct kunit_case *test_case = ctx->test_case;
431 current->kunit_test = test;
434 * kunit_run_case_internal may encounter a fatal error; if it does,
435 * abort will be called, this thread will exit, and finally the parent
436 * thread will resume control and handle any necessary clean up.
438 kunit_run_case_internal(test, suite, test_case);
441 static void kunit_try_run_case_cleanup(void *data)
443 struct kunit_try_catch_context *ctx = data;
444 struct kunit *test = ctx->test;
445 struct kunit_suite *suite = ctx->suite;
447 current->kunit_test = test;
449 kunit_run_case_cleanup(test, suite);
452 static void kunit_catch_run_case_cleanup(void *data)
454 struct kunit_try_catch_context *ctx = data;
455 struct kunit *test = ctx->test;
456 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
458 /* It is always a failure if cleanup aborts. */
459 kunit_set_failure(test);
463 * Test case could not finish, we have no idea what state it is
464 * in, so don't do clean up.
466 if (try_exit_code == -ETIMEDOUT) {
467 kunit_err(test, "test case cleanup timed out\n");
469 * Unknown internal error occurred preventing test case from
470 * running, so there is nothing to clean up.
473 kunit_err(test, "internal error occurred during test case cleanup: %d\n",
479 kunit_err(test, "test aborted during cleanup. continuing without cleaning up\n");
483 static void kunit_catch_run_case(void *data)
485 struct kunit_try_catch_context *ctx = data;
486 struct kunit *test = ctx->test;
487 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
490 kunit_set_failure(test);
492 * Test case could not finish, we have no idea what state it is
493 * in, so don't do clean up.
495 if (try_exit_code == -ETIMEDOUT) {
496 kunit_err(test, "test case timed out\n");
498 * Unknown internal error occurred preventing test case from
499 * running, so there is nothing to clean up.
502 kunit_err(test, "internal error occurred preventing test case from running: %d\n",
510 * Performs all logic to run a test case. It also catches most errors that
511 * occur in a test case and reports them as failures.
513 static void kunit_run_case_catch_errors(struct kunit_suite *suite,
514 struct kunit_case *test_case,
517 struct kunit_try_catch_context context;
518 struct kunit_try_catch *try_catch;
520 try_catch = &test->try_catch;
522 kunit_try_catch_init(try_catch,
525 kunit_catch_run_case);
527 context.suite = suite;
528 context.test_case = test_case;
529 kunit_try_catch_run(try_catch, &context);
531 /* Now run the cleanup */
532 kunit_try_catch_init(try_catch,
534 kunit_try_run_case_cleanup,
535 kunit_catch_run_case_cleanup);
536 kunit_try_catch_run(try_catch, &context);
538 /* Propagate the parameter result to the test case. */
539 if (test->status == KUNIT_FAILURE)
540 test_case->status = KUNIT_FAILURE;
541 else if (test_case->status != KUNIT_FAILURE && test->status == KUNIT_SUCCESS)
542 test_case->status = KUNIT_SUCCESS;
545 static void kunit_print_suite_stats(struct kunit_suite *suite,
546 struct kunit_result_stats suite_stats,
547 struct kunit_result_stats param_stats)
549 if (kunit_should_print_stats(suite_stats)) {
550 kunit_log(KERN_INFO, suite,
551 "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
559 if (kunit_should_print_stats(param_stats)) {
560 kunit_log(KERN_INFO, suite,
561 "# Totals: pass:%lu fail:%lu skip:%lu total:%lu",
569 static void kunit_update_stats(struct kunit_result_stats *stats,
570 enum kunit_status status)
587 static void kunit_accumulate_stats(struct kunit_result_stats *total,
588 struct kunit_result_stats add)
590 total->passed += add.passed;
591 total->skipped += add.skipped;
592 total->failed += add.failed;
593 total->total += add.total;
596 int kunit_run_tests(struct kunit_suite *suite)
598 char param_desc[KUNIT_PARAM_DESC_SIZE];
599 struct kunit_case *test_case;
600 struct kunit_result_stats suite_stats = { 0 };
601 struct kunit_result_stats total_stats = { 0 };
603 /* Taint the kernel so we know we've run tests. */
604 add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
606 if (suite->suite_init) {
607 suite->suite_init_err = suite->suite_init(suite);
608 if (suite->suite_init_err) {
609 kunit_err(suite, KUNIT_SUBTEST_INDENT
610 "# failed to initialize (%d)", suite->suite_init_err);
615 kunit_print_suite_start(suite);
617 kunit_suite_for_each_test_case(suite, test_case) {
618 struct kunit test = { .param_value = NULL, .param_index = 0 };
619 struct kunit_result_stats param_stats = { 0 };
621 kunit_init_test(&test, test_case->name, test_case->log);
622 if (test_case->status == KUNIT_SKIPPED) {
623 /* Test marked as skip */
624 test.status = KUNIT_SKIPPED;
625 kunit_update_stats(¶m_stats, test.status);
626 } else if (!test_case->generate_params) {
627 /* Non-parameterised test. */
628 test_case->status = KUNIT_SKIPPED;
629 kunit_run_case_catch_errors(suite, test_case, &test);
630 kunit_update_stats(¶m_stats, test.status);
632 /* Get initial param. */
633 param_desc[0] = '\0';
634 test.param_value = test_case->generate_params(NULL, param_desc);
635 test_case->status = KUNIT_SKIPPED;
636 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
638 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
639 "# Subtest: %s", test_case->name);
641 while (test.param_value) {
642 kunit_run_case_catch_errors(suite, test_case, &test);
644 if (param_desc[0] == '\0') {
645 snprintf(param_desc, sizeof(param_desc),
646 "param-%d", test.param_index);
649 kunit_print_ok_not_ok(&test, KUNIT_LEVEL_CASE_PARAM,
651 test.param_index + 1,
653 test.status_comment);
655 kunit_update_stats(¶m_stats, test.status);
657 /* Get next param. */
658 param_desc[0] = '\0';
659 test.param_value = test_case->generate_params(test.param_value, param_desc);
661 test.status = KUNIT_SUCCESS;
662 test.status_comment[0] = '\0';
666 kunit_print_attr((void *)test_case, true, KUNIT_LEVEL_CASE);
668 kunit_print_test_stats(&test, param_stats);
670 kunit_print_ok_not_ok(&test, KUNIT_LEVEL_CASE, test_case->status,
671 kunit_test_case_num(suite, test_case),
673 test.status_comment);
675 kunit_update_stats(&suite_stats, test_case->status);
676 kunit_accumulate_stats(&total_stats, param_stats);
679 if (suite->suite_exit)
680 suite->suite_exit(suite);
682 kunit_print_suite_stats(suite, suite_stats, total_stats);
684 kunit_print_suite_end(suite);
688 EXPORT_SYMBOL_GPL(kunit_run_tests);
690 static void kunit_init_suite(struct kunit_suite *suite)
692 kunit_debugfs_create_suite(suite);
693 suite->status_comment[0] = '\0';
694 suite->suite_init_err = 0;
697 bool kunit_enabled(void)
702 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites)
706 if (!kunit_enabled() && num_suites > 0) {
707 pr_info("kunit: disabled\n");
711 kunit_suite_counter = 1;
713 static_branch_inc(&kunit_running);
715 for (i = 0; i < num_suites; i++) {
716 kunit_init_suite(suites[i]);
717 kunit_run_tests(suites[i]);
720 static_branch_dec(&kunit_running);
723 EXPORT_SYMBOL_GPL(__kunit_test_suites_init);
725 static void kunit_exit_suite(struct kunit_suite *suite)
727 kunit_debugfs_destroy_suite(suite);
730 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites)
734 if (!kunit_enabled())
737 for (i = 0; i < num_suites; i++)
738 kunit_exit_suite(suites[i]);
740 EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
742 #ifdef CONFIG_MODULES
743 static void kunit_module_init(struct module *mod)
745 struct kunit_suite_set suite_set = {
746 mod->kunit_suites, mod->kunit_suites + mod->num_kunit_suites,
748 const char *action = kunit_action();
751 suite_set = kunit_filter_suites(&suite_set,
752 kunit_filter_glob() ?: "*.*",
753 kunit_filter(), kunit_filter_action(),
756 pr_err("kunit module: error filtering suites: %d\n", err);
758 mod->kunit_suites = (struct kunit_suite **)suite_set.start;
759 mod->num_kunit_suites = suite_set.end - suite_set.start;
762 kunit_exec_run_tests(&suite_set, false);
763 else if (!strcmp(action, "list"))
764 kunit_exec_list_tests(&suite_set, false);
765 else if (!strcmp(action, "list_attr"))
766 kunit_exec_list_tests(&suite_set, true);
768 pr_err("kunit: unknown action '%s'\n", action);
771 static void kunit_module_exit(struct module *mod)
773 struct kunit_suite_set suite_set = {
774 mod->kunit_suites, mod->kunit_suites + mod->num_kunit_suites,
776 const char *action = kunit_action();
779 __kunit_test_suites_exit(mod->kunit_suites,
780 mod->num_kunit_suites);
783 kunit_free_suite_set(suite_set);
786 static int kunit_module_notify(struct notifier_block *nb, unsigned long val,
789 struct module *mod = data;
792 case MODULE_STATE_LIVE:
794 case MODULE_STATE_GOING:
795 kunit_module_exit(mod);
797 case MODULE_STATE_COMING:
798 kunit_module_init(mod);
800 case MODULE_STATE_UNFORMED:
807 static struct notifier_block kunit_mod_nb = {
808 .notifier_call = kunit_module_notify,
813 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp)
817 data = kmalloc_array(n, size, gfp);
822 if (kunit_add_action_or_reset(test, (kunit_action_t *)kfree, data) != 0)
827 EXPORT_SYMBOL_GPL(kunit_kmalloc_array);
829 void kunit_kfree(struct kunit *test, const void *ptr)
834 kunit_release_action(test, (kunit_action_t *)kfree, (void *)ptr);
836 EXPORT_SYMBOL_GPL(kunit_kfree);
838 void kunit_cleanup(struct kunit *test)
840 struct kunit_resource *res;
844 * test->resources is a stack - each allocation must be freed in the
845 * reverse order from which it was added since one resource may depend
846 * on another for its entire lifetime.
847 * Also, we cannot use the normal list_for_each constructs, even the
848 * safe ones because *arbitrary* nodes may be deleted when
849 * kunit_resource_free is called; the list_for_each_safe variants only
850 * protect against the current node being deleted, not the next.
853 spin_lock_irqsave(&test->lock, flags);
854 if (list_empty(&test->resources)) {
855 spin_unlock_irqrestore(&test->lock, flags);
858 res = list_last_entry(&test->resources,
859 struct kunit_resource,
862 * Need to unlock here as a resource may remove another
863 * resource, and this can't happen if the test->lock
866 spin_unlock_irqrestore(&test->lock, flags);
867 kunit_remove_resource(test, res);
869 current->kunit_test = NULL;
871 EXPORT_SYMBOL_GPL(kunit_cleanup);
873 static int __init kunit_init(void)
875 /* Install the KUnit hook functions. */
876 kunit_install_hooks();
878 kunit_debugfs_init();
879 #ifdef CONFIG_MODULES
880 return register_module_notifier(&kunit_mod_nb);
885 late_initcall(kunit_init);
887 static void __exit kunit_exit(void)
889 memset(&kunit_hooks, 0, sizeof(kunit_hooks));
890 #ifdef CONFIG_MODULES
891 unregister_module_notifier(&kunit_mod_nb);
893 kunit_debugfs_cleanup();
895 module_exit(kunit_exit);
897 MODULE_LICENSE("GPL v2");