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 <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 * Enable KUnit tests to run.
60 #ifdef CONFIG_KUNIT_DEFAULT_ENABLED
61 static bool enable_param = true;
63 static bool enable_param;
65 module_param_named(enable, enable_param, bool, 0);
66 MODULE_PARM_DESC(enable, "Enable KUnit tests");
69 * KUnit statistic mode:
71 * 1 - only when there is more than one subtest
74 static int kunit_stats_enabled = 1;
75 module_param_named(stats_enabled, kunit_stats_enabled, int, 0644);
76 MODULE_PARM_DESC(stats_enabled,
77 "Print test stats: never (0), only for multiple subtests (1), or always (2)");
79 struct kunit_result_stats {
81 unsigned long skipped;
86 static bool kunit_should_print_stats(struct kunit_result_stats stats)
88 if (kunit_stats_enabled == 0)
91 if (kunit_stats_enabled == 2)
94 return (stats.total > 1);
97 static void kunit_print_test_stats(struct kunit *test,
98 struct kunit_result_stats stats)
100 if (!kunit_should_print_stats(stats))
103 kunit_log(KERN_INFO, test,
105 "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
114 * Append formatted message to log, size of which is limited to
115 * KUNIT_LOG_SIZE bytes (including null terminating byte).
117 void kunit_log_append(char *log, const char *fmt, ...)
119 char line[KUNIT_LOG_SIZE];
126 len_left = KUNIT_LOG_SIZE - strlen(log) - 1;
131 vsnprintf(line, sizeof(line), fmt, args);
134 strncat(log, line, len_left);
136 EXPORT_SYMBOL_GPL(kunit_log_append);
138 size_t kunit_suite_num_test_cases(struct kunit_suite *suite)
140 struct kunit_case *test_case;
143 kunit_suite_for_each_test_case(suite, test_case)
148 EXPORT_SYMBOL_GPL(kunit_suite_num_test_cases);
150 static void kunit_print_suite_start(struct kunit_suite *suite)
152 kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "KTAP version 1\n");
153 kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "# Subtest: %s",
155 kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "1..%zd",
156 kunit_suite_num_test_cases(suite));
159 static void kunit_print_ok_not_ok(void *test_or_suite,
161 enum kunit_status status,
163 const char *description,
164 const char *directive)
166 struct kunit_suite *suite = is_test ? NULL : test_or_suite;
167 struct kunit *test = is_test ? test_or_suite : NULL;
168 const char *directive_header = (status == KUNIT_SKIPPED) ? " # SKIP " : "";
171 * We do not log the test suite results as doing so would
172 * mean debugfs display would consist of the test suite
173 * description and status prior to individual test results.
174 * Hence directly printk the suite status, and we will
175 * separately seq_printf() the suite status for the debugfs
179 pr_info("%s %zd %s%s%s\n",
180 kunit_status_to_ok_not_ok(status),
181 test_number, description, directive_header,
182 (status == KUNIT_SKIPPED) ? directive : "");
184 kunit_log(KERN_INFO, test,
185 KUNIT_SUBTEST_INDENT "%s %zd %s%s%s",
186 kunit_status_to_ok_not_ok(status),
187 test_number, description, directive_header,
188 (status == KUNIT_SKIPPED) ? directive : "");
191 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite)
193 const struct kunit_case *test_case;
194 enum kunit_status status = KUNIT_SKIPPED;
196 if (suite->suite_init_err)
197 return KUNIT_FAILURE;
199 kunit_suite_for_each_test_case(suite, test_case) {
200 if (test_case->status == KUNIT_FAILURE)
201 return KUNIT_FAILURE;
202 else if (test_case->status == KUNIT_SUCCESS)
203 status = KUNIT_SUCCESS;
208 EXPORT_SYMBOL_GPL(kunit_suite_has_succeeded);
210 static size_t kunit_suite_counter = 1;
212 static void kunit_print_suite_end(struct kunit_suite *suite)
214 kunit_print_ok_not_ok((void *)suite, false,
215 kunit_suite_has_succeeded(suite),
216 kunit_suite_counter++,
218 suite->status_comment);
221 unsigned int kunit_test_case_num(struct kunit_suite *suite,
222 struct kunit_case *test_case)
224 struct kunit_case *tc;
227 kunit_suite_for_each_test_case(suite, tc) {
235 EXPORT_SYMBOL_GPL(kunit_test_case_num);
237 static void kunit_print_string_stream(struct kunit *test,
238 struct string_stream *stream)
240 struct string_stream_fragment *fragment;
243 if (string_stream_is_empty(stream))
246 buf = string_stream_get_string(stream);
249 "Could not allocate buffer, dumping stream:\n");
250 list_for_each_entry(fragment, &stream->fragments, node) {
251 kunit_err(test, "%s", fragment->fragment);
253 kunit_err(test, "\n");
255 kunit_err(test, "%s", buf);
256 kunit_kfree(test, buf);
260 static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
261 enum kunit_assert_type type, const struct kunit_assert *assert,
262 assert_format_t assert_format, const struct va_format *message)
264 struct string_stream *stream;
266 kunit_set_failure(test);
268 stream = alloc_string_stream(test, GFP_KERNEL);
269 if (IS_ERR(stream)) {
271 "Could not allocate stream to print failed assertion in %s:%d\n",
277 kunit_assert_prologue(loc, type, stream);
278 assert_format(assert, message, stream);
280 kunit_print_string_stream(test, stream);
282 string_stream_destroy(stream);
285 static void __noreturn kunit_abort(struct kunit *test)
287 kunit_try_catch_throw(&test->try_catch); /* Does not return. */
290 * Throw could not abort from test.
292 * XXX: we should never reach this line! As kunit_try_catch_throw is
295 WARN_ONCE(true, "Throw could not abort from test!\n");
298 void kunit_do_failed_assertion(struct kunit *test,
299 const struct kunit_loc *loc,
300 enum kunit_assert_type type,
301 const struct kunit_assert *assert,
302 assert_format_t assert_format,
303 const char *fmt, ...)
306 struct va_format message;
312 kunit_fail(test, loc, type, assert, assert_format, &message);
316 if (type == KUNIT_ASSERTION)
319 EXPORT_SYMBOL_GPL(kunit_do_failed_assertion);
321 void kunit_init_test(struct kunit *test, const char *name, char *log)
323 spin_lock_init(&test->lock);
324 INIT_LIST_HEAD(&test->resources);
329 test->status = KUNIT_SUCCESS;
330 test->status_comment[0] = '\0';
332 EXPORT_SYMBOL_GPL(kunit_init_test);
335 * Initializes and runs test case. Does not clean up or do post validations.
337 static void kunit_run_case_internal(struct kunit *test,
338 struct kunit_suite *suite,
339 struct kunit_case *test_case)
344 ret = suite->init(test);
346 kunit_err(test, "failed to initialize: %d\n", ret);
347 kunit_set_failure(test);
352 test_case->run_case(test);
355 static void kunit_case_internal_cleanup(struct kunit *test)
361 * Performs post validations and cleanup after a test case was run.
362 * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
364 static void kunit_run_case_cleanup(struct kunit *test,
365 struct kunit_suite *suite)
370 kunit_case_internal_cleanup(test);
373 struct kunit_try_catch_context {
375 struct kunit_suite *suite;
376 struct kunit_case *test_case;
379 static void kunit_try_run_case(void *data)
381 struct kunit_try_catch_context *ctx = data;
382 struct kunit *test = ctx->test;
383 struct kunit_suite *suite = ctx->suite;
384 struct kunit_case *test_case = ctx->test_case;
386 current->kunit_test = test;
389 * kunit_run_case_internal may encounter a fatal error; if it does,
390 * abort will be called, this thread will exit, and finally the parent
391 * thread will resume control and handle any necessary clean up.
393 kunit_run_case_internal(test, suite, test_case);
394 /* This line may never be reached. */
395 kunit_run_case_cleanup(test, suite);
398 static void kunit_catch_run_case(void *data)
400 struct kunit_try_catch_context *ctx = data;
401 struct kunit *test = ctx->test;
402 struct kunit_suite *suite = ctx->suite;
403 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
406 kunit_set_failure(test);
408 * Test case could not finish, we have no idea what state it is
409 * in, so don't do clean up.
411 if (try_exit_code == -ETIMEDOUT) {
412 kunit_err(test, "test case timed out\n");
414 * Unknown internal error occurred preventing test case from
415 * running, so there is nothing to clean up.
418 kunit_err(test, "internal error occurred preventing test case from running: %d\n",
425 * Test case was run, but aborted. It is the test case's business as to
426 * whether it failed or not, we just need to clean up.
428 kunit_run_case_cleanup(test, suite);
432 * Performs all logic to run a test case. It also catches most errors that
433 * occur in a test case and reports them as failures.
435 static void kunit_run_case_catch_errors(struct kunit_suite *suite,
436 struct kunit_case *test_case,
439 struct kunit_try_catch_context context;
440 struct kunit_try_catch *try_catch;
442 kunit_init_test(test, test_case->name, test_case->log);
443 try_catch = &test->try_catch;
445 kunit_try_catch_init(try_catch,
448 kunit_catch_run_case);
450 context.suite = suite;
451 context.test_case = test_case;
452 kunit_try_catch_run(try_catch, &context);
454 /* Propagate the parameter result to the test case. */
455 if (test->status == KUNIT_FAILURE)
456 test_case->status = KUNIT_FAILURE;
457 else if (test_case->status != KUNIT_FAILURE && test->status == KUNIT_SUCCESS)
458 test_case->status = KUNIT_SUCCESS;
461 static void kunit_print_suite_stats(struct kunit_suite *suite,
462 struct kunit_result_stats suite_stats,
463 struct kunit_result_stats param_stats)
465 if (kunit_should_print_stats(suite_stats)) {
466 kunit_log(KERN_INFO, suite,
467 "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
475 if (kunit_should_print_stats(param_stats)) {
476 kunit_log(KERN_INFO, suite,
477 "# Totals: pass:%lu fail:%lu skip:%lu total:%lu",
485 static void kunit_update_stats(struct kunit_result_stats *stats,
486 enum kunit_status status)
503 static void kunit_accumulate_stats(struct kunit_result_stats *total,
504 struct kunit_result_stats add)
506 total->passed += add.passed;
507 total->skipped += add.skipped;
508 total->failed += add.failed;
509 total->total += add.total;
512 int kunit_run_tests(struct kunit_suite *suite)
514 char param_desc[KUNIT_PARAM_DESC_SIZE];
515 struct kunit_case *test_case;
516 struct kunit_result_stats suite_stats = { 0 };
517 struct kunit_result_stats total_stats = { 0 };
519 /* Taint the kernel so we know we've run tests. */
520 add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
522 if (suite->suite_init) {
523 suite->suite_init_err = suite->suite_init(suite);
524 if (suite->suite_init_err) {
525 kunit_err(suite, KUNIT_SUBTEST_INDENT
526 "# failed to initialize (%d)", suite->suite_init_err);
531 kunit_print_suite_start(suite);
533 kunit_suite_for_each_test_case(suite, test_case) {
534 struct kunit test = { .param_value = NULL, .param_index = 0 };
535 struct kunit_result_stats param_stats = { 0 };
536 test_case->status = KUNIT_SKIPPED;
538 if (!test_case->generate_params) {
539 /* Non-parameterised test. */
540 kunit_run_case_catch_errors(suite, test_case, &test);
541 kunit_update_stats(¶m_stats, test.status);
543 /* Get initial param. */
544 param_desc[0] = '\0';
545 test.param_value = test_case->generate_params(NULL, param_desc);
546 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
548 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
549 "# Subtest: %s", test_case->name);
551 while (test.param_value) {
552 kunit_run_case_catch_errors(suite, test_case, &test);
554 if (param_desc[0] == '\0') {
555 snprintf(param_desc, sizeof(param_desc),
556 "param-%d", test.param_index);
559 kunit_log(KERN_INFO, &test,
560 KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
562 kunit_status_to_ok_not_ok(test.status),
563 test.param_index + 1, param_desc);
565 /* Get next param. */
566 param_desc[0] = '\0';
567 test.param_value = test_case->generate_params(test.param_value, param_desc);
570 kunit_update_stats(¶m_stats, test.status);
575 kunit_print_test_stats(&test, param_stats);
577 kunit_print_ok_not_ok(&test, true, test_case->status,
578 kunit_test_case_num(suite, test_case),
580 test.status_comment);
582 kunit_update_stats(&suite_stats, test_case->status);
583 kunit_accumulate_stats(&total_stats, param_stats);
586 if (suite->suite_exit)
587 suite->suite_exit(suite);
589 kunit_print_suite_stats(suite, suite_stats, total_stats);
591 kunit_print_suite_end(suite);
595 EXPORT_SYMBOL_GPL(kunit_run_tests);
597 static void kunit_init_suite(struct kunit_suite *suite)
599 kunit_debugfs_create_suite(suite);
600 suite->status_comment[0] = '\0';
601 suite->suite_init_err = 0;
604 bool kunit_enabled(void)
609 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites)
613 if (!kunit_enabled() && num_suites > 0) {
614 pr_info("kunit: disabled\n");
618 for (i = 0; i < num_suites; i++) {
619 kunit_init_suite(suites[i]);
620 kunit_run_tests(suites[i]);
624 EXPORT_SYMBOL_GPL(__kunit_test_suites_init);
626 static void kunit_exit_suite(struct kunit_suite *suite)
628 kunit_debugfs_destroy_suite(suite);
631 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites)
635 if (!kunit_enabled())
638 for (i = 0; i < num_suites; i++)
639 kunit_exit_suite(suites[i]);
641 kunit_suite_counter = 1;
643 EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
645 #ifdef CONFIG_MODULES
646 static void kunit_module_init(struct module *mod)
648 __kunit_test_suites_init(mod->kunit_suites, mod->num_kunit_suites);
651 static void kunit_module_exit(struct module *mod)
653 __kunit_test_suites_exit(mod->kunit_suites, mod->num_kunit_suites);
656 static int kunit_module_notify(struct notifier_block *nb, unsigned long val,
659 struct module *mod = data;
662 case MODULE_STATE_LIVE:
663 kunit_module_init(mod);
665 case MODULE_STATE_GOING:
666 kunit_module_exit(mod);
668 case MODULE_STATE_COMING:
669 case MODULE_STATE_UNFORMED:
676 static struct notifier_block kunit_mod_nb = {
677 .notifier_call = kunit_module_notify,
682 struct kunit_kmalloc_array_params {
688 static int kunit_kmalloc_array_init(struct kunit_resource *res, void *context)
690 struct kunit_kmalloc_array_params *params = context;
692 res->data = kmalloc_array(params->n, params->size, params->gfp);
699 static void kunit_kmalloc_array_free(struct kunit_resource *res)
704 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp)
706 struct kunit_kmalloc_array_params params = {
712 return kunit_alloc_resource(test,
713 kunit_kmalloc_array_init,
714 kunit_kmalloc_array_free,
718 EXPORT_SYMBOL_GPL(kunit_kmalloc_array);
720 static inline bool kunit_kfree_match(struct kunit *test,
721 struct kunit_resource *res, void *match_data)
723 /* Only match resources allocated with kunit_kmalloc() and friends. */
724 return res->free == kunit_kmalloc_array_free && res->data == match_data;
727 void kunit_kfree(struct kunit *test, const void *ptr)
732 if (kunit_destroy_resource(test, kunit_kfree_match, (void *)ptr))
733 KUNIT_FAIL(test, "kunit_kfree: %px already freed or not allocated by kunit", ptr);
735 EXPORT_SYMBOL_GPL(kunit_kfree);
737 void kunit_cleanup(struct kunit *test)
739 struct kunit_resource *res;
743 * test->resources is a stack - each allocation must be freed in the
744 * reverse order from which it was added since one resource may depend
745 * on another for its entire lifetime.
746 * Also, we cannot use the normal list_for_each constructs, even the
747 * safe ones because *arbitrary* nodes may be deleted when
748 * kunit_resource_free is called; the list_for_each_safe variants only
749 * protect against the current node being deleted, not the next.
752 spin_lock_irqsave(&test->lock, flags);
753 if (list_empty(&test->resources)) {
754 spin_unlock_irqrestore(&test->lock, flags);
757 res = list_last_entry(&test->resources,
758 struct kunit_resource,
761 * Need to unlock here as a resource may remove another
762 * resource, and this can't happen if the test->lock
765 spin_unlock_irqrestore(&test->lock, flags);
766 kunit_remove_resource(test, res);
768 current->kunit_test = NULL;
770 EXPORT_SYMBOL_GPL(kunit_cleanup);
772 static int __init kunit_init(void)
774 kunit_debugfs_init();
775 #ifdef CONFIG_MODULES
776 return register_module_notifier(&kunit_mod_nb);
781 late_initcall(kunit_init);
783 static void __exit kunit_exit(void)
785 #ifdef CONFIG_MODULES
786 unregister_module_notifier(&kunit_mod_nb);
788 kunit_debugfs_cleanup();
790 module_exit(kunit_exit);
792 MODULE_LICENSE("GPL v2");