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 <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 "hooks-impl.h"
21 #include "string-stream.h"
22 #include "try-catch-impl.h"
25 * Hook to fail the current test and print an error message to the log.
27 void __printf(3, 4) __kunit_fail_current_test_impl(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);
56 * Enable KUnit tests to run.
58 #ifdef CONFIG_KUNIT_DEFAULT_ENABLED
59 static bool enable_param = true;
61 static bool enable_param;
63 module_param_named(enable, enable_param, bool, 0);
64 MODULE_PARM_DESC(enable, "Enable KUnit tests");
67 * KUnit statistic mode:
69 * 1 - only when there is more than one subtest
72 static int kunit_stats_enabled = 1;
73 module_param_named(stats_enabled, kunit_stats_enabled, int, 0644);
74 MODULE_PARM_DESC(stats_enabled,
75 "Print test stats: never (0), only for multiple subtests (1), or always (2)");
77 struct kunit_result_stats {
79 unsigned long skipped;
84 static bool kunit_should_print_stats(struct kunit_result_stats stats)
86 if (kunit_stats_enabled == 0)
89 if (kunit_stats_enabled == 2)
92 return (stats.total > 1);
95 static void kunit_print_test_stats(struct kunit *test,
96 struct kunit_result_stats stats)
98 if (!kunit_should_print_stats(stats))
101 kunit_log(KERN_INFO, test,
103 "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
112 * kunit_log_newline() - Add newline to the end of log if one is not
114 * @log: The log to add the newline to.
116 static void kunit_log_newline(char *log)
118 int log_len, len_left;
120 log_len = strlen(log);
121 len_left = KUNIT_LOG_SIZE - log_len - 1;
123 if (log_len > 0 && log[log_len - 1] != '\n')
124 strncat(log, "\n", len_left);
128 * Append formatted message to log, size of which is limited to
129 * KUNIT_LOG_SIZE bytes (including null terminating byte).
131 void kunit_log_append(char *log, const char *fmt, ...)
134 int len, log_len, len_left;
139 log_len = strlen(log);
140 len_left = KUNIT_LOG_SIZE - log_len - 1;
144 /* Evaluate length of line to add to log */
146 len = vsnprintf(NULL, 0, fmt, args) + 1;
149 /* Print formatted line to the log */
151 vsnprintf(log + log_len, min(len, len_left), fmt, args);
154 /* Add newline to end of log if not already present. */
155 kunit_log_newline(log);
157 EXPORT_SYMBOL_GPL(kunit_log_append);
159 size_t kunit_suite_num_test_cases(struct kunit_suite *suite)
161 struct kunit_case *test_case;
164 kunit_suite_for_each_test_case(suite, test_case)
169 EXPORT_SYMBOL_GPL(kunit_suite_num_test_cases);
171 static void kunit_print_suite_start(struct kunit_suite *suite)
174 * We do not log the test suite header as doing so would
175 * mean debugfs display would consist of the test suite
176 * header prior to individual test results.
177 * Hence directly printk the suite status, and we will
178 * separately seq_printf() the suite header for the debugfs
181 pr_info(KUNIT_SUBTEST_INDENT "KTAP version 1\n");
182 pr_info(KUNIT_SUBTEST_INDENT "# Subtest: %s\n",
184 pr_info(KUNIT_SUBTEST_INDENT "1..%zd\n",
185 kunit_suite_num_test_cases(suite));
188 static void kunit_print_ok_not_ok(void *test_or_suite,
190 enum kunit_status status,
192 const char *description,
193 const char *directive)
195 struct kunit_suite *suite = is_test ? NULL : test_or_suite;
196 struct kunit *test = is_test ? test_or_suite : NULL;
197 const char *directive_header = (status == KUNIT_SKIPPED) ? " # SKIP " : "";
200 * We do not log the test suite results as doing so would
201 * mean debugfs display would consist of an incorrect test
202 * number. Hence directly printk the suite result, and we will
203 * separately seq_printf() the suite results for the debugfs
207 pr_info("%s %zd %s%s%s\n",
208 kunit_status_to_ok_not_ok(status),
209 test_number, description, directive_header,
210 (status == KUNIT_SKIPPED) ? directive : "");
212 kunit_log(KERN_INFO, test,
213 KUNIT_SUBTEST_INDENT "%s %zd %s%s%s",
214 kunit_status_to_ok_not_ok(status),
215 test_number, description, directive_header,
216 (status == KUNIT_SKIPPED) ? directive : "");
219 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite)
221 const struct kunit_case *test_case;
222 enum kunit_status status = KUNIT_SKIPPED;
224 if (suite->suite_init_err)
225 return KUNIT_FAILURE;
227 kunit_suite_for_each_test_case(suite, test_case) {
228 if (test_case->status == KUNIT_FAILURE)
229 return KUNIT_FAILURE;
230 else if (test_case->status == KUNIT_SUCCESS)
231 status = KUNIT_SUCCESS;
236 EXPORT_SYMBOL_GPL(kunit_suite_has_succeeded);
238 static size_t kunit_suite_counter = 1;
240 static void kunit_print_suite_end(struct kunit_suite *suite)
242 kunit_print_ok_not_ok((void *)suite, false,
243 kunit_suite_has_succeeded(suite),
244 kunit_suite_counter++,
246 suite->status_comment);
249 unsigned int kunit_test_case_num(struct kunit_suite *suite,
250 struct kunit_case *test_case)
252 struct kunit_case *tc;
255 kunit_suite_for_each_test_case(suite, tc) {
263 EXPORT_SYMBOL_GPL(kunit_test_case_num);
265 static void kunit_print_string_stream(struct kunit *test,
266 struct string_stream *stream)
268 struct string_stream_fragment *fragment;
271 if (string_stream_is_empty(stream))
274 buf = string_stream_get_string(stream);
277 "Could not allocate buffer, dumping stream:\n");
278 list_for_each_entry(fragment, &stream->fragments, node) {
279 kunit_err(test, "%s", fragment->fragment);
281 kunit_err(test, "\n");
283 kunit_err(test, "%s", buf);
284 kunit_kfree(test, buf);
288 static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
289 enum kunit_assert_type type, const struct kunit_assert *assert,
290 assert_format_t assert_format, const struct va_format *message)
292 struct string_stream *stream;
294 kunit_set_failure(test);
296 stream = alloc_string_stream(test, GFP_KERNEL);
297 if (IS_ERR(stream)) {
299 "Could not allocate stream to print failed assertion in %s:%d\n",
305 kunit_assert_prologue(loc, type, stream);
306 assert_format(assert, message, stream);
308 kunit_print_string_stream(test, stream);
310 string_stream_destroy(stream);
313 static void __noreturn kunit_abort(struct kunit *test)
315 kunit_try_catch_throw(&test->try_catch); /* Does not return. */
318 * Throw could not abort from test.
320 * XXX: we should never reach this line! As kunit_try_catch_throw is
323 WARN_ONCE(true, "Throw could not abort from test!\n");
326 void kunit_do_failed_assertion(struct kunit *test,
327 const struct kunit_loc *loc,
328 enum kunit_assert_type type,
329 const struct kunit_assert *assert,
330 assert_format_t assert_format,
331 const char *fmt, ...)
334 struct va_format message;
340 kunit_fail(test, loc, type, assert, assert_format, &message);
344 if (type == KUNIT_ASSERTION)
347 EXPORT_SYMBOL_GPL(kunit_do_failed_assertion);
349 void kunit_init_test(struct kunit *test, const char *name, char *log)
351 spin_lock_init(&test->lock);
352 INIT_LIST_HEAD(&test->resources);
357 test->status = KUNIT_SUCCESS;
358 test->status_comment[0] = '\0';
360 EXPORT_SYMBOL_GPL(kunit_init_test);
363 * Initializes and runs test case. Does not clean up or do post validations.
365 static void kunit_run_case_internal(struct kunit *test,
366 struct kunit_suite *suite,
367 struct kunit_case *test_case)
372 ret = suite->init(test);
374 kunit_err(test, "failed to initialize: %d\n", ret);
375 kunit_set_failure(test);
380 test_case->run_case(test);
383 static void kunit_case_internal_cleanup(struct kunit *test)
389 * Performs post validations and cleanup after a test case was run.
390 * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
392 static void kunit_run_case_cleanup(struct kunit *test,
393 struct kunit_suite *suite)
398 kunit_case_internal_cleanup(test);
401 struct kunit_try_catch_context {
403 struct kunit_suite *suite;
404 struct kunit_case *test_case;
407 static void kunit_try_run_case(void *data)
409 struct kunit_try_catch_context *ctx = data;
410 struct kunit *test = ctx->test;
411 struct kunit_suite *suite = ctx->suite;
412 struct kunit_case *test_case = ctx->test_case;
414 current->kunit_test = test;
417 * kunit_run_case_internal may encounter a fatal error; if it does,
418 * abort will be called, this thread will exit, and finally the parent
419 * thread will resume control and handle any necessary clean up.
421 kunit_run_case_internal(test, suite, test_case);
422 /* This line may never be reached. */
423 kunit_run_case_cleanup(test, suite);
426 static void kunit_catch_run_case(void *data)
428 struct kunit_try_catch_context *ctx = data;
429 struct kunit *test = ctx->test;
430 struct kunit_suite *suite = ctx->suite;
431 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
434 kunit_set_failure(test);
436 * Test case could not finish, we have no idea what state it is
437 * in, so don't do clean up.
439 if (try_exit_code == -ETIMEDOUT) {
440 kunit_err(test, "test case timed out\n");
442 * Unknown internal error occurred preventing test case from
443 * running, so there is nothing to clean up.
446 kunit_err(test, "internal error occurred preventing test case from running: %d\n",
453 * Test case was run, but aborted. It is the test case's business as to
454 * whether it failed or not, we just need to clean up.
456 kunit_run_case_cleanup(test, suite);
460 * Performs all logic to run a test case. It also catches most errors that
461 * occur in a test case and reports them as failures.
463 static void kunit_run_case_catch_errors(struct kunit_suite *suite,
464 struct kunit_case *test_case,
467 struct kunit_try_catch_context context;
468 struct kunit_try_catch *try_catch;
470 try_catch = &test->try_catch;
472 kunit_try_catch_init(try_catch,
475 kunit_catch_run_case);
477 context.suite = suite;
478 context.test_case = test_case;
479 kunit_try_catch_run(try_catch, &context);
481 /* Propagate the parameter result to the test case. */
482 if (test->status == KUNIT_FAILURE)
483 test_case->status = KUNIT_FAILURE;
484 else if (test_case->status != KUNIT_FAILURE && test->status == KUNIT_SUCCESS)
485 test_case->status = KUNIT_SUCCESS;
488 static void kunit_print_suite_stats(struct kunit_suite *suite,
489 struct kunit_result_stats suite_stats,
490 struct kunit_result_stats param_stats)
492 if (kunit_should_print_stats(suite_stats)) {
493 kunit_log(KERN_INFO, suite,
494 "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
502 if (kunit_should_print_stats(param_stats)) {
503 kunit_log(KERN_INFO, suite,
504 "# Totals: pass:%lu fail:%lu skip:%lu total:%lu",
512 static void kunit_update_stats(struct kunit_result_stats *stats,
513 enum kunit_status status)
530 static void kunit_accumulate_stats(struct kunit_result_stats *total,
531 struct kunit_result_stats add)
533 total->passed += add.passed;
534 total->skipped += add.skipped;
535 total->failed += add.failed;
536 total->total += add.total;
539 int kunit_run_tests(struct kunit_suite *suite)
541 char param_desc[KUNIT_PARAM_DESC_SIZE];
542 struct kunit_case *test_case;
543 struct kunit_result_stats suite_stats = { 0 };
544 struct kunit_result_stats total_stats = { 0 };
546 /* Taint the kernel so we know we've run tests. */
547 add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
549 if (suite->suite_init) {
550 suite->suite_init_err = suite->suite_init(suite);
551 if (suite->suite_init_err) {
552 kunit_err(suite, KUNIT_SUBTEST_INDENT
553 "# failed to initialize (%d)", suite->suite_init_err);
558 kunit_print_suite_start(suite);
560 kunit_suite_for_each_test_case(suite, test_case) {
561 struct kunit test = { .param_value = NULL, .param_index = 0 };
562 struct kunit_result_stats param_stats = { 0 };
563 test_case->status = KUNIT_SKIPPED;
565 kunit_init_test(&test, test_case->name, test_case->log);
567 if (!test_case->generate_params) {
568 /* Non-parameterised test. */
569 kunit_run_case_catch_errors(suite, test_case, &test);
570 kunit_update_stats(¶m_stats, test.status);
572 /* Get initial param. */
573 param_desc[0] = '\0';
574 test.param_value = test_case->generate_params(NULL, param_desc);
575 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
577 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
578 "# Subtest: %s", test_case->name);
580 while (test.param_value) {
581 kunit_run_case_catch_errors(suite, test_case, &test);
583 if (param_desc[0] == '\0') {
584 snprintf(param_desc, sizeof(param_desc),
585 "param-%d", test.param_index);
588 kunit_log(KERN_INFO, &test,
589 KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
591 kunit_status_to_ok_not_ok(test.status),
592 test.param_index + 1, param_desc);
594 /* Get next param. */
595 param_desc[0] = '\0';
596 test.param_value = test_case->generate_params(test.param_value, param_desc);
599 kunit_update_stats(¶m_stats, test.status);
604 kunit_print_test_stats(&test, param_stats);
606 kunit_print_ok_not_ok(&test, true, test_case->status,
607 kunit_test_case_num(suite, test_case),
609 test.status_comment);
611 kunit_update_stats(&suite_stats, test_case->status);
612 kunit_accumulate_stats(&total_stats, param_stats);
615 if (suite->suite_exit)
616 suite->suite_exit(suite);
618 kunit_print_suite_stats(suite, suite_stats, total_stats);
620 kunit_print_suite_end(suite);
624 EXPORT_SYMBOL_GPL(kunit_run_tests);
626 static void kunit_init_suite(struct kunit_suite *suite)
628 kunit_debugfs_create_suite(suite);
629 suite->status_comment[0] = '\0';
630 suite->suite_init_err = 0;
633 bool kunit_enabled(void)
638 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites)
642 if (!kunit_enabled() && num_suites > 0) {
643 pr_info("kunit: disabled\n");
647 static_branch_inc(&kunit_running);
649 for (i = 0; i < num_suites; i++) {
650 kunit_init_suite(suites[i]);
651 kunit_run_tests(suites[i]);
654 static_branch_dec(&kunit_running);
657 EXPORT_SYMBOL_GPL(__kunit_test_suites_init);
659 static void kunit_exit_suite(struct kunit_suite *suite)
661 kunit_debugfs_destroy_suite(suite);
664 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites)
668 if (!kunit_enabled())
671 for (i = 0; i < num_suites; i++)
672 kunit_exit_suite(suites[i]);
674 kunit_suite_counter = 1;
676 EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
678 #ifdef CONFIG_MODULES
679 static void kunit_module_init(struct module *mod)
681 __kunit_test_suites_init(mod->kunit_suites, mod->num_kunit_suites);
684 static void kunit_module_exit(struct module *mod)
686 __kunit_test_suites_exit(mod->kunit_suites, mod->num_kunit_suites);
689 static int kunit_module_notify(struct notifier_block *nb, unsigned long val,
692 struct module *mod = data;
695 case MODULE_STATE_LIVE:
696 kunit_module_init(mod);
698 case MODULE_STATE_GOING:
699 kunit_module_exit(mod);
701 case MODULE_STATE_COMING:
702 case MODULE_STATE_UNFORMED:
709 static struct notifier_block kunit_mod_nb = {
710 .notifier_call = kunit_module_notify,
715 struct kunit_kmalloc_array_params {
721 static int kunit_kmalloc_array_init(struct kunit_resource *res, void *context)
723 struct kunit_kmalloc_array_params *params = context;
725 res->data = kmalloc_array(params->n, params->size, params->gfp);
732 static void kunit_kmalloc_array_free(struct kunit_resource *res)
737 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp)
739 struct kunit_kmalloc_array_params params = {
745 return kunit_alloc_resource(test,
746 kunit_kmalloc_array_init,
747 kunit_kmalloc_array_free,
751 EXPORT_SYMBOL_GPL(kunit_kmalloc_array);
753 static inline bool kunit_kfree_match(struct kunit *test,
754 struct kunit_resource *res, void *match_data)
756 /* Only match resources allocated with kunit_kmalloc() and friends. */
757 return res->free == kunit_kmalloc_array_free && res->data == match_data;
760 void kunit_kfree(struct kunit *test, const void *ptr)
765 if (kunit_destroy_resource(test, kunit_kfree_match, (void *)ptr))
766 KUNIT_FAIL(test, "kunit_kfree: %px already freed or not allocated by kunit", ptr);
768 EXPORT_SYMBOL_GPL(kunit_kfree);
770 void kunit_cleanup(struct kunit *test)
772 struct kunit_resource *res;
776 * test->resources is a stack - each allocation must be freed in the
777 * reverse order from which it was added since one resource may depend
778 * on another for its entire lifetime.
779 * Also, we cannot use the normal list_for_each constructs, even the
780 * safe ones because *arbitrary* nodes may be deleted when
781 * kunit_resource_free is called; the list_for_each_safe variants only
782 * protect against the current node being deleted, not the next.
785 spin_lock_irqsave(&test->lock, flags);
786 if (list_empty(&test->resources)) {
787 spin_unlock_irqrestore(&test->lock, flags);
790 res = list_last_entry(&test->resources,
791 struct kunit_resource,
794 * Need to unlock here as a resource may remove another
795 * resource, and this can't happen if the test->lock
798 spin_unlock_irqrestore(&test->lock, flags);
799 kunit_remove_resource(test, res);
801 current->kunit_test = NULL;
803 EXPORT_SYMBOL_GPL(kunit_cleanup);
805 static int __init kunit_init(void)
807 /* Install the KUnit hook functions. */
808 kunit_install_hooks();
810 kunit_debugfs_init();
811 #ifdef CONFIG_MODULES
812 return register_module_notifier(&kunit_mod_nb);
817 late_initcall(kunit_init);
819 static void __exit kunit_exit(void)
821 memset(&kunit_hooks, 0, sizeof(kunit_hooks));
822 #ifdef CONFIG_MODULES
823 unregister_module_notifier(&kunit_mod_nb);
825 kunit_debugfs_cleanup();
827 module_exit(kunit_exit);
829 MODULE_LICENSE("GPL v2");