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",
113 * kunit_log_newline() - Add newline to the end of log if one is not
115 * @log: The log to add the newline to.
117 static void kunit_log_newline(char *log)
119 int log_len, len_left;
121 log_len = strlen(log);
122 len_left = KUNIT_LOG_SIZE - log_len - 1;
124 if (log_len > 0 && log[log_len - 1] != '\n')
125 strncat(log, "\n", len_left);
129 * Append formatted message to log, size of which is limited to
130 * KUNIT_LOG_SIZE bytes (including null terminating byte).
132 void kunit_log_append(char *log, const char *fmt, ...)
135 int len, log_len, len_left;
140 log_len = strlen(log);
141 len_left = KUNIT_LOG_SIZE - log_len - 1;
145 /* Evaluate length of line to add to log */
147 len = vsnprintf(NULL, 0, fmt, args) + 1;
150 /* Print formatted line to the log */
152 vsnprintf(log + log_len, min(len, len_left), fmt, args);
155 /* Add newline to end of log if not already present. */
156 kunit_log_newline(log);
158 EXPORT_SYMBOL_GPL(kunit_log_append);
160 size_t kunit_suite_num_test_cases(struct kunit_suite *suite)
162 struct kunit_case *test_case;
165 kunit_suite_for_each_test_case(suite, test_case)
170 EXPORT_SYMBOL_GPL(kunit_suite_num_test_cases);
172 /* Currently supported test levels */
174 KUNIT_LEVEL_SUITE = 0,
176 KUNIT_LEVEL_CASE_PARAM,
179 static void kunit_print_suite_start(struct kunit_suite *suite)
182 * We do not log the test suite header as doing so would
183 * mean debugfs display would consist of the test suite
184 * header prior to individual test results.
185 * Hence directly printk the suite status, and we will
186 * separately seq_printf() the suite header for the debugfs
189 pr_info(KUNIT_SUBTEST_INDENT "KTAP version 1\n");
190 pr_info(KUNIT_SUBTEST_INDENT "# Subtest: %s\n",
192 kunit_print_attr((void *)suite, false, KUNIT_LEVEL_CASE);
193 pr_info(KUNIT_SUBTEST_INDENT "1..%zd\n",
194 kunit_suite_num_test_cases(suite));
197 static void kunit_print_ok_not_ok(struct kunit *test,
198 unsigned int test_level,
199 enum kunit_status status,
201 const char *description,
202 const char *directive)
204 const char *directive_header = (status == KUNIT_SKIPPED) ? " # SKIP " : "";
205 const char *directive_body = (status == KUNIT_SKIPPED) ? directive : "";
208 * When test is NULL assume that results are from the suite
209 * and today suite results are expected at level 0 only.
211 WARN(!test && test_level, "suite test level can't be %u!\n", test_level);
214 * We do not log the test suite results as doing so would
215 * mean debugfs display would consist of an incorrect test
216 * number. Hence directly printk the suite result, and we will
217 * separately seq_printf() the suite results for the debugfs
221 pr_info("%s %zd %s%s%s\n",
222 kunit_status_to_ok_not_ok(status),
223 test_number, description, directive_header,
226 kunit_log(KERN_INFO, test,
228 KUNIT_INDENT_LEN * test_level, "",
229 kunit_status_to_ok_not_ok(status),
230 test_number, description, directive_header,
234 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite)
236 const struct kunit_case *test_case;
237 enum kunit_status status = KUNIT_SKIPPED;
239 if (suite->suite_init_err)
240 return KUNIT_FAILURE;
242 kunit_suite_for_each_test_case(suite, test_case) {
243 if (test_case->status == KUNIT_FAILURE)
244 return KUNIT_FAILURE;
245 else if (test_case->status == KUNIT_SUCCESS)
246 status = KUNIT_SUCCESS;
251 EXPORT_SYMBOL_GPL(kunit_suite_has_succeeded);
253 static size_t kunit_suite_counter = 1;
255 static void kunit_print_suite_end(struct kunit_suite *suite)
257 kunit_print_ok_not_ok(NULL, KUNIT_LEVEL_SUITE,
258 kunit_suite_has_succeeded(suite),
259 kunit_suite_counter++,
261 suite->status_comment);
264 unsigned int kunit_test_case_num(struct kunit_suite *suite,
265 struct kunit_case *test_case)
267 struct kunit_case *tc;
270 kunit_suite_for_each_test_case(suite, tc) {
278 EXPORT_SYMBOL_GPL(kunit_test_case_num);
280 static void kunit_print_string_stream(struct kunit *test,
281 struct string_stream *stream)
283 struct string_stream_fragment *fragment;
286 if (string_stream_is_empty(stream))
289 buf = string_stream_get_string(stream);
292 "Could not allocate buffer, dumping stream:\n");
293 list_for_each_entry(fragment, &stream->fragments, node) {
294 kunit_err(test, "%s", fragment->fragment);
296 kunit_err(test, "\n");
298 kunit_err(test, "%s", buf);
299 kunit_kfree(test, buf);
303 static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
304 enum kunit_assert_type type, const struct kunit_assert *assert,
305 assert_format_t assert_format, const struct va_format *message)
307 struct string_stream *stream;
309 kunit_set_failure(test);
311 stream = alloc_string_stream(test, GFP_KERNEL);
312 if (IS_ERR(stream)) {
314 "Could not allocate stream to print failed assertion in %s:%d\n",
320 kunit_assert_prologue(loc, type, stream);
321 assert_format(assert, message, stream);
323 kunit_print_string_stream(test, stream);
325 string_stream_destroy(stream);
328 void __noreturn __kunit_abort(struct kunit *test)
330 kunit_try_catch_throw(&test->try_catch); /* Does not return. */
333 * Throw could not abort from test.
335 * XXX: we should never reach this line! As kunit_try_catch_throw is
338 WARN_ONCE(true, "Throw could not abort from test!\n");
340 EXPORT_SYMBOL_GPL(__kunit_abort);
342 void __kunit_do_failed_assertion(struct kunit *test,
343 const struct kunit_loc *loc,
344 enum kunit_assert_type type,
345 const struct kunit_assert *assert,
346 assert_format_t assert_format,
347 const char *fmt, ...)
350 struct va_format message;
356 kunit_fail(test, loc, type, assert, assert_format, &message);
360 EXPORT_SYMBOL_GPL(__kunit_do_failed_assertion);
362 void kunit_init_test(struct kunit *test, const char *name, char *log)
364 spin_lock_init(&test->lock);
365 INIT_LIST_HEAD(&test->resources);
370 test->status = KUNIT_SUCCESS;
371 test->status_comment[0] = '\0';
373 EXPORT_SYMBOL_GPL(kunit_init_test);
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)
385 ret = suite->init(test);
387 kunit_err(test, "failed to initialize: %d\n", ret);
388 kunit_set_failure(test);
393 test_case->run_case(test);
396 static void kunit_case_internal_cleanup(struct kunit *test)
402 * Performs post validations and cleanup after a test case was run.
403 * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
405 static void kunit_run_case_cleanup(struct kunit *test,
406 struct kunit_suite *suite)
411 kunit_case_internal_cleanup(test);
414 struct kunit_try_catch_context {
416 struct kunit_suite *suite;
417 struct kunit_case *test_case;
420 static void kunit_try_run_case(void *data)
422 struct kunit_try_catch_context *ctx = data;
423 struct kunit *test = ctx->test;
424 struct kunit_suite *suite = ctx->suite;
425 struct kunit_case *test_case = ctx->test_case;
427 current->kunit_test = test;
430 * kunit_run_case_internal may encounter a fatal error; if it does,
431 * abort will be called, this thread will exit, and finally the parent
432 * thread will resume control and handle any necessary clean up.
434 kunit_run_case_internal(test, suite, test_case);
437 static void kunit_try_run_case_cleanup(void *data)
439 struct kunit_try_catch_context *ctx = data;
440 struct kunit *test = ctx->test;
441 struct kunit_suite *suite = ctx->suite;
443 current->kunit_test = test;
445 kunit_run_case_cleanup(test, suite);
448 static void kunit_catch_run_case_cleanup(void *data)
450 struct kunit_try_catch_context *ctx = data;
451 struct kunit *test = ctx->test;
452 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
454 /* It is always a failure if cleanup aborts. */
455 kunit_set_failure(test);
459 * Test case could not finish, we have no idea what state it is
460 * in, so don't do clean up.
462 if (try_exit_code == -ETIMEDOUT) {
463 kunit_err(test, "test case cleanup timed out\n");
465 * Unknown internal error occurred preventing test case from
466 * running, so there is nothing to clean up.
469 kunit_err(test, "internal error occurred during test case cleanup: %d\n",
475 kunit_err(test, "test aborted during cleanup. continuing without cleaning up\n");
479 static void kunit_catch_run_case(void *data)
481 struct kunit_try_catch_context *ctx = data;
482 struct kunit *test = ctx->test;
483 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
486 kunit_set_failure(test);
488 * Test case could not finish, we have no idea what state it is
489 * in, so don't do clean up.
491 if (try_exit_code == -ETIMEDOUT) {
492 kunit_err(test, "test case timed out\n");
494 * Unknown internal error occurred preventing test case from
495 * running, so there is nothing to clean up.
498 kunit_err(test, "internal error occurred preventing test case from running: %d\n",
506 * Performs all logic to run a test case. It also catches most errors that
507 * occur in a test case and reports them as failures.
509 static void kunit_run_case_catch_errors(struct kunit_suite *suite,
510 struct kunit_case *test_case,
513 struct kunit_try_catch_context context;
514 struct kunit_try_catch *try_catch;
516 try_catch = &test->try_catch;
518 kunit_try_catch_init(try_catch,
521 kunit_catch_run_case);
523 context.suite = suite;
524 context.test_case = test_case;
525 kunit_try_catch_run(try_catch, &context);
527 /* Now run the cleanup */
528 kunit_try_catch_init(try_catch,
530 kunit_try_run_case_cleanup,
531 kunit_catch_run_case_cleanup);
532 kunit_try_catch_run(try_catch, &context);
534 /* Propagate the parameter result to the test case. */
535 if (test->status == KUNIT_FAILURE)
536 test_case->status = KUNIT_FAILURE;
537 else if (test_case->status != KUNIT_FAILURE && test->status == KUNIT_SUCCESS)
538 test_case->status = KUNIT_SUCCESS;
541 static void kunit_print_suite_stats(struct kunit_suite *suite,
542 struct kunit_result_stats suite_stats,
543 struct kunit_result_stats param_stats)
545 if (kunit_should_print_stats(suite_stats)) {
546 kunit_log(KERN_INFO, suite,
547 "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
555 if (kunit_should_print_stats(param_stats)) {
556 kunit_log(KERN_INFO, suite,
557 "# Totals: pass:%lu fail:%lu skip:%lu total:%lu",
565 static void kunit_update_stats(struct kunit_result_stats *stats,
566 enum kunit_status status)
583 static void kunit_accumulate_stats(struct kunit_result_stats *total,
584 struct kunit_result_stats add)
586 total->passed += add.passed;
587 total->skipped += add.skipped;
588 total->failed += add.failed;
589 total->total += add.total;
592 int kunit_run_tests(struct kunit_suite *suite)
594 char param_desc[KUNIT_PARAM_DESC_SIZE];
595 struct kunit_case *test_case;
596 struct kunit_result_stats suite_stats = { 0 };
597 struct kunit_result_stats total_stats = { 0 };
599 /* Taint the kernel so we know we've run tests. */
600 add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
602 if (suite->suite_init) {
603 suite->suite_init_err = suite->suite_init(suite);
604 if (suite->suite_init_err) {
605 kunit_err(suite, KUNIT_SUBTEST_INDENT
606 "# failed to initialize (%d)", suite->suite_init_err);
611 kunit_print_suite_start(suite);
613 kunit_suite_for_each_test_case(suite, test_case) {
614 struct kunit test = { .param_value = NULL, .param_index = 0 };
615 struct kunit_result_stats param_stats = { 0 };
617 kunit_init_test(&test, test_case->name, test_case->log);
618 if (test_case->status == KUNIT_SKIPPED) {
619 /* Test marked as skip */
620 test.status = KUNIT_SKIPPED;
621 kunit_update_stats(¶m_stats, test.status);
622 } else if (!test_case->generate_params) {
623 /* Non-parameterised test. */
624 test_case->status = KUNIT_SKIPPED;
625 kunit_run_case_catch_errors(suite, test_case, &test);
626 kunit_update_stats(¶m_stats, test.status);
628 /* Get initial param. */
629 param_desc[0] = '\0';
630 test.param_value = test_case->generate_params(NULL, param_desc);
631 test_case->status = KUNIT_SKIPPED;
632 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
634 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
635 "# Subtest: %s", test_case->name);
637 while (test.param_value) {
638 kunit_run_case_catch_errors(suite, test_case, &test);
640 if (param_desc[0] == '\0') {
641 snprintf(param_desc, sizeof(param_desc),
642 "param-%d", test.param_index);
645 kunit_print_ok_not_ok(&test, KUNIT_LEVEL_CASE_PARAM,
647 test.param_index + 1,
649 test.status_comment);
651 /* Get next param. */
652 param_desc[0] = '\0';
653 test.param_value = test_case->generate_params(test.param_value, param_desc);
656 kunit_update_stats(¶m_stats, test.status);
660 kunit_print_attr((void *)test_case, true, KUNIT_LEVEL_CASE);
662 kunit_print_test_stats(&test, param_stats);
664 kunit_print_ok_not_ok(&test, KUNIT_LEVEL_CASE, test_case->status,
665 kunit_test_case_num(suite, test_case),
667 test.status_comment);
669 kunit_update_stats(&suite_stats, test_case->status);
670 kunit_accumulate_stats(&total_stats, param_stats);
673 if (suite->suite_exit)
674 suite->suite_exit(suite);
676 kunit_print_suite_stats(suite, suite_stats, total_stats);
678 kunit_print_suite_end(suite);
682 EXPORT_SYMBOL_GPL(kunit_run_tests);
684 static void kunit_init_suite(struct kunit_suite *suite)
686 kunit_debugfs_create_suite(suite);
687 suite->status_comment[0] = '\0';
688 suite->suite_init_err = 0;
691 bool kunit_enabled(void)
696 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites)
700 if (!kunit_enabled() && num_suites > 0) {
701 pr_info("kunit: disabled\n");
705 static_branch_inc(&kunit_running);
707 for (i = 0; i < num_suites; i++) {
708 kunit_init_suite(suites[i]);
709 kunit_run_tests(suites[i]);
712 static_branch_dec(&kunit_running);
715 EXPORT_SYMBOL_GPL(__kunit_test_suites_init);
717 static void kunit_exit_suite(struct kunit_suite *suite)
719 kunit_debugfs_destroy_suite(suite);
722 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites)
726 if (!kunit_enabled())
729 for (i = 0; i < num_suites; i++)
730 kunit_exit_suite(suites[i]);
732 kunit_suite_counter = 1;
734 EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
736 #ifdef CONFIG_MODULES
737 static void kunit_module_init(struct module *mod)
739 struct kunit_suite_set suite_set = {
740 mod->kunit_suites, mod->kunit_suites + mod->num_kunit_suites,
742 const char *action = kunit_action();
745 suite_set = kunit_filter_suites(&suite_set,
746 kunit_filter_glob() ?: "*.*",
747 kunit_filter(), kunit_filter_action(),
750 pr_err("kunit module: error filtering suites: %d\n", err);
752 mod->kunit_suites = (struct kunit_suite **)suite_set.start;
753 mod->num_kunit_suites = suite_set.end - suite_set.start;
756 kunit_exec_run_tests(&suite_set, false);
757 else if (!strcmp(action, "list"))
758 kunit_exec_list_tests(&suite_set, false);
759 else if (!strcmp(action, "list_attr"))
760 kunit_exec_list_tests(&suite_set, true);
762 pr_err("kunit: unknown action '%s'\n", action);
765 static void kunit_module_exit(struct module *mod)
767 struct kunit_suite_set suite_set = {
768 mod->kunit_suites, mod->kunit_suites + mod->num_kunit_suites,
770 const char *action = kunit_action();
773 __kunit_test_suites_exit(mod->kunit_suites,
774 mod->num_kunit_suites);
777 kunit_free_suite_set(suite_set);
780 static int kunit_module_notify(struct notifier_block *nb, unsigned long val,
783 struct module *mod = data;
786 case MODULE_STATE_LIVE:
788 case MODULE_STATE_GOING:
789 kunit_module_exit(mod);
791 case MODULE_STATE_COMING:
792 kunit_module_init(mod);
794 case MODULE_STATE_UNFORMED:
801 static struct notifier_block kunit_mod_nb = {
802 .notifier_call = kunit_module_notify,
807 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp)
811 data = kmalloc_array(n, size, gfp);
816 if (kunit_add_action_or_reset(test, (kunit_action_t *)kfree, data) != 0)
821 EXPORT_SYMBOL_GPL(kunit_kmalloc_array);
823 void kunit_kfree(struct kunit *test, const void *ptr)
828 kunit_release_action(test, (kunit_action_t *)kfree, (void *)ptr);
830 EXPORT_SYMBOL_GPL(kunit_kfree);
832 void kunit_cleanup(struct kunit *test)
834 struct kunit_resource *res;
838 * test->resources is a stack - each allocation must be freed in the
839 * reverse order from which it was added since one resource may depend
840 * on another for its entire lifetime.
841 * Also, we cannot use the normal list_for_each constructs, even the
842 * safe ones because *arbitrary* nodes may be deleted when
843 * kunit_resource_free is called; the list_for_each_safe variants only
844 * protect against the current node being deleted, not the next.
847 spin_lock_irqsave(&test->lock, flags);
848 if (list_empty(&test->resources)) {
849 spin_unlock_irqrestore(&test->lock, flags);
852 res = list_last_entry(&test->resources,
853 struct kunit_resource,
856 * Need to unlock here as a resource may remove another
857 * resource, and this can't happen if the test->lock
860 spin_unlock_irqrestore(&test->lock, flags);
861 kunit_remove_resource(test, res);
863 current->kunit_test = NULL;
865 EXPORT_SYMBOL_GPL(kunit_cleanup);
867 static int __init kunit_init(void)
869 /* Install the KUnit hook functions. */
870 kunit_install_hooks();
872 kunit_debugfs_init();
873 #ifdef CONFIG_MODULES
874 return register_module_notifier(&kunit_mod_nb);
879 late_initcall(kunit_init);
881 static void __exit kunit_exit(void)
883 memset(&kunit_hooks, 0, sizeof(kunit_hooks));
884 #ifdef CONFIG_MODULES
885 unregister_module_notifier(&kunit_mod_nb);
887 kunit_debugfs_cleanup();
889 module_exit(kunit_exit);
891 MODULE_LICENSE("GPL v2");