1 // SPDX-License-Identifier: GPL-2.0
3 * Base unit test (KUnit) API.
5 * Copyright (C) 2019, Google LLC.
9 #include <kunit/test.h>
10 #include <kunit/test-bug.h>
11 #include <linux/kernel.h>
12 #include <linux/kref.h>
13 #include <linux/moduleparam.h>
14 #include <linux/sched/debug.h>
15 #include <linux/sched.h>
18 #include "string-stream.h"
19 #include "try-catch-impl.h"
21 #if IS_BUILTIN(CONFIG_KUNIT)
23 * Fail the current test and print an error message to the log.
25 void __kunit_fail_current_test(const char *file, int line, const char *fmt, ...)
31 if (!current->kunit_test)
34 kunit_set_failure(current->kunit_test);
36 /* kunit_err() only accepts literals, so evaluate the args first. */
38 len = vsnprintf(NULL, 0, fmt, args) + 1;
41 buffer = kunit_kmalloc(current->kunit_test, len, GFP_KERNEL);
46 vsnprintf(buffer, len, fmt, args);
49 kunit_err(current->kunit_test, "%s:%d: %s", file, line, buffer);
50 kunit_kfree(current->kunit_test, buffer);
52 EXPORT_SYMBOL_GPL(__kunit_fail_current_test);
56 * KUnit statistic mode:
58 * 1 - only when there is more than one subtest
61 static int kunit_stats_enabled = 1;
62 module_param_named(stats_enabled, kunit_stats_enabled, int, 0644);
63 MODULE_PARM_DESC(stats_enabled,
64 "Print test stats: never (0), only for multiple subtests (1), or always (2)");
66 struct kunit_result_stats {
68 unsigned long skipped;
73 static bool kunit_should_print_stats(struct kunit_result_stats stats)
75 if (kunit_stats_enabled == 0)
78 if (kunit_stats_enabled == 2)
81 return (stats.total > 1);
84 static void kunit_print_test_stats(struct kunit *test,
85 struct kunit_result_stats stats)
87 if (!kunit_should_print_stats(stats))
90 kunit_log(KERN_INFO, test,
92 "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
101 * Append formatted message to log, size of which is limited to
102 * KUNIT_LOG_SIZE bytes (including null terminating byte).
104 void kunit_log_append(char *log, const char *fmt, ...)
106 char line[KUNIT_LOG_SIZE];
113 len_left = KUNIT_LOG_SIZE - strlen(log) - 1;
118 vsnprintf(line, sizeof(line), fmt, args);
121 strncat(log, line, len_left);
123 EXPORT_SYMBOL_GPL(kunit_log_append);
125 size_t kunit_suite_num_test_cases(struct kunit_suite *suite)
127 struct kunit_case *test_case;
130 kunit_suite_for_each_test_case(suite, test_case)
135 EXPORT_SYMBOL_GPL(kunit_suite_num_test_cases);
137 static void kunit_print_subtest_start(struct kunit_suite *suite)
139 kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "# Subtest: %s",
141 kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "1..%zd",
142 kunit_suite_num_test_cases(suite));
145 static void kunit_print_ok_not_ok(void *test_or_suite,
147 enum kunit_status status,
149 const char *description,
150 const char *directive)
152 struct kunit_suite *suite = is_test ? NULL : test_or_suite;
153 struct kunit *test = is_test ? test_or_suite : NULL;
154 const char *directive_header = (status == KUNIT_SKIPPED) ? " # SKIP " : "";
157 * We do not log the test suite results as doing so would
158 * mean debugfs display would consist of the test suite
159 * description and status prior to individual test results.
160 * Hence directly printk the suite status, and we will
161 * separately seq_printf() the suite status for the debugfs
165 pr_info("%s %zd - %s%s%s\n",
166 kunit_status_to_ok_not_ok(status),
167 test_number, description, directive_header,
168 (status == KUNIT_SKIPPED) ? directive : "");
170 kunit_log(KERN_INFO, test,
171 KUNIT_SUBTEST_INDENT "%s %zd - %s%s%s",
172 kunit_status_to_ok_not_ok(status),
173 test_number, description, directive_header,
174 (status == KUNIT_SKIPPED) ? directive : "");
177 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite)
179 const struct kunit_case *test_case;
180 enum kunit_status status = KUNIT_SKIPPED;
182 kunit_suite_for_each_test_case(suite, test_case) {
183 if (test_case->status == KUNIT_FAILURE)
184 return KUNIT_FAILURE;
185 else if (test_case->status == KUNIT_SUCCESS)
186 status = KUNIT_SUCCESS;
191 EXPORT_SYMBOL_GPL(kunit_suite_has_succeeded);
193 static size_t kunit_suite_counter = 1;
195 static void kunit_print_subtest_end(struct kunit_suite *suite)
197 kunit_print_ok_not_ok((void *)suite, false,
198 kunit_suite_has_succeeded(suite),
199 kunit_suite_counter++,
201 suite->status_comment);
204 unsigned int kunit_test_case_num(struct kunit_suite *suite,
205 struct kunit_case *test_case)
207 struct kunit_case *tc;
210 kunit_suite_for_each_test_case(suite, tc) {
218 EXPORT_SYMBOL_GPL(kunit_test_case_num);
220 static void kunit_print_string_stream(struct kunit *test,
221 struct string_stream *stream)
223 struct string_stream_fragment *fragment;
226 if (string_stream_is_empty(stream))
229 buf = string_stream_get_string(stream);
232 "Could not allocate buffer, dumping stream:\n");
233 list_for_each_entry(fragment, &stream->fragments, node) {
234 kunit_err(test, "%s", fragment->fragment);
236 kunit_err(test, "\n");
238 kunit_err(test, "%s", buf);
239 kunit_kfree(test, buf);
243 static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
244 enum kunit_assert_type type, struct kunit_assert *assert,
245 const struct va_format *message)
247 struct string_stream *stream;
249 kunit_set_failure(test);
251 stream = alloc_string_stream(test, GFP_KERNEL);
254 "Could not allocate stream to print failed assertion in %s:%d\n",
260 kunit_assert_prologue(loc, type, stream);
261 assert->format(assert, message, stream);
263 kunit_print_string_stream(test, stream);
265 WARN_ON(string_stream_destroy(stream));
268 static void __noreturn kunit_abort(struct kunit *test)
270 kunit_try_catch_throw(&test->try_catch); /* Does not return. */
273 * Throw could not abort from test.
275 * XXX: we should never reach this line! As kunit_try_catch_throw is
278 WARN_ONCE(true, "Throw could not abort from test!\n");
281 void kunit_do_failed_assertion(struct kunit *test,
282 const struct kunit_loc *loc,
283 enum kunit_assert_type type,
284 struct kunit_assert *assert,
285 const char *fmt, ...)
288 struct va_format message;
294 kunit_fail(test, loc, type, assert, &message);
298 if (type == KUNIT_ASSERTION)
301 EXPORT_SYMBOL_GPL(kunit_do_failed_assertion);
303 void kunit_init_test(struct kunit *test, const char *name, char *log)
305 spin_lock_init(&test->lock);
306 INIT_LIST_HEAD(&test->resources);
311 test->status = KUNIT_SUCCESS;
312 test->status_comment[0] = '\0';
314 EXPORT_SYMBOL_GPL(kunit_init_test);
317 * Initializes and runs test case. Does not clean up or do post validations.
319 static void kunit_run_case_internal(struct kunit *test,
320 struct kunit_suite *suite,
321 struct kunit_case *test_case)
326 ret = suite->init(test);
328 kunit_err(test, "failed to initialize: %d\n", ret);
329 kunit_set_failure(test);
334 test_case->run_case(test);
337 static void kunit_case_internal_cleanup(struct kunit *test)
343 * Performs post validations and cleanup after a test case was run.
344 * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
346 static void kunit_run_case_cleanup(struct kunit *test,
347 struct kunit_suite *suite)
352 kunit_case_internal_cleanup(test);
355 struct kunit_try_catch_context {
357 struct kunit_suite *suite;
358 struct kunit_case *test_case;
361 static void kunit_try_run_case(void *data)
363 struct kunit_try_catch_context *ctx = data;
364 struct kunit *test = ctx->test;
365 struct kunit_suite *suite = ctx->suite;
366 struct kunit_case *test_case = ctx->test_case;
368 current->kunit_test = test;
371 * kunit_run_case_internal may encounter a fatal error; if it does,
372 * abort will be called, this thread will exit, and finally the parent
373 * thread will resume control and handle any necessary clean up.
375 kunit_run_case_internal(test, suite, test_case);
376 /* This line may never be reached. */
377 kunit_run_case_cleanup(test, suite);
380 static void kunit_catch_run_case(void *data)
382 struct kunit_try_catch_context *ctx = data;
383 struct kunit *test = ctx->test;
384 struct kunit_suite *suite = ctx->suite;
385 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
388 kunit_set_failure(test);
390 * Test case could not finish, we have no idea what state it is
391 * in, so don't do clean up.
393 if (try_exit_code == -ETIMEDOUT) {
394 kunit_err(test, "test case timed out\n");
396 * Unknown internal error occurred preventing test case from
397 * running, so there is nothing to clean up.
400 kunit_err(test, "internal error occurred preventing test case from running: %d\n",
407 * Test case was run, but aborted. It is the test case's business as to
408 * whether it failed or not, we just need to clean up.
410 kunit_run_case_cleanup(test, suite);
414 * Performs all logic to run a test case. It also catches most errors that
415 * occur in a test case and reports them as failures.
417 static void kunit_run_case_catch_errors(struct kunit_suite *suite,
418 struct kunit_case *test_case,
421 struct kunit_try_catch_context context;
422 struct kunit_try_catch *try_catch;
424 kunit_init_test(test, test_case->name, test_case->log);
425 try_catch = &test->try_catch;
427 kunit_try_catch_init(try_catch,
430 kunit_catch_run_case);
432 context.suite = suite;
433 context.test_case = test_case;
434 kunit_try_catch_run(try_catch, &context);
436 /* Propagate the parameter result to the test case. */
437 if (test->status == KUNIT_FAILURE)
438 test_case->status = KUNIT_FAILURE;
439 else if (test_case->status != KUNIT_FAILURE && test->status == KUNIT_SUCCESS)
440 test_case->status = KUNIT_SUCCESS;
443 static void kunit_print_suite_stats(struct kunit_suite *suite,
444 struct kunit_result_stats suite_stats,
445 struct kunit_result_stats param_stats)
447 if (kunit_should_print_stats(suite_stats)) {
448 kunit_log(KERN_INFO, suite,
449 "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
457 if (kunit_should_print_stats(param_stats)) {
458 kunit_log(KERN_INFO, suite,
459 "# Totals: pass:%lu fail:%lu skip:%lu total:%lu",
467 static void kunit_update_stats(struct kunit_result_stats *stats,
468 enum kunit_status status)
485 static void kunit_accumulate_stats(struct kunit_result_stats *total,
486 struct kunit_result_stats add)
488 total->passed += add.passed;
489 total->skipped += add.skipped;
490 total->failed += add.failed;
491 total->total += add.total;
494 int kunit_run_tests(struct kunit_suite *suite)
496 char param_desc[KUNIT_PARAM_DESC_SIZE];
497 struct kunit_case *test_case;
498 struct kunit_result_stats suite_stats = { 0 };
499 struct kunit_result_stats total_stats = { 0 };
501 kunit_print_subtest_start(suite);
503 kunit_suite_for_each_test_case(suite, test_case) {
504 struct kunit test = { .param_value = NULL, .param_index = 0 };
505 struct kunit_result_stats param_stats = { 0 };
506 test_case->status = KUNIT_SKIPPED;
508 if (!test_case->generate_params) {
509 /* Non-parameterised test. */
510 kunit_run_case_catch_errors(suite, test_case, &test);
511 kunit_update_stats(¶m_stats, test.status);
513 /* Get initial param. */
514 param_desc[0] = '\0';
515 test.param_value = test_case->generate_params(NULL, param_desc);
516 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
517 "# Subtest: %s", test_case->name);
519 while (test.param_value) {
520 kunit_run_case_catch_errors(suite, test_case, &test);
522 if (param_desc[0] == '\0') {
523 snprintf(param_desc, sizeof(param_desc),
524 "param-%d", test.param_index);
527 kunit_log(KERN_INFO, &test,
528 KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
530 kunit_status_to_ok_not_ok(test.status),
531 test.param_index + 1, param_desc);
533 /* Get next param. */
534 param_desc[0] = '\0';
535 test.param_value = test_case->generate_params(test.param_value, param_desc);
538 kunit_update_stats(¶m_stats, test.status);
543 kunit_print_test_stats(&test, param_stats);
545 kunit_print_ok_not_ok(&test, true, test_case->status,
546 kunit_test_case_num(suite, test_case),
548 test.status_comment);
550 kunit_update_stats(&suite_stats, test_case->status);
551 kunit_accumulate_stats(&total_stats, param_stats);
554 kunit_print_suite_stats(suite, suite_stats, total_stats);
555 kunit_print_subtest_end(suite);
559 EXPORT_SYMBOL_GPL(kunit_run_tests);
561 static void kunit_init_suite(struct kunit_suite *suite)
563 kunit_debugfs_create_suite(suite);
564 suite->status_comment[0] = '\0';
567 int __kunit_test_suites_init(struct kunit_suite * const * const suites)
571 for (i = 0; suites[i] != NULL; i++) {
572 kunit_init_suite(suites[i]);
573 kunit_run_tests(suites[i]);
577 EXPORT_SYMBOL_GPL(__kunit_test_suites_init);
579 static void kunit_exit_suite(struct kunit_suite *suite)
581 kunit_debugfs_destroy_suite(suite);
584 void __kunit_test_suites_exit(struct kunit_suite **suites)
588 for (i = 0; suites[i] != NULL; i++)
589 kunit_exit_suite(suites[i]);
591 kunit_suite_counter = 1;
593 EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
596 * Used for static resources and when a kunit_resource * has been created by
597 * kunit_alloc_resource(). When an init function is supplied, @data is passed
598 * into the init function; otherwise, we simply set the resource data field to
599 * the data value passed in.
601 int kunit_add_resource(struct kunit *test,
602 kunit_resource_init_t init,
603 kunit_resource_free_t free,
604 struct kunit_resource *res,
611 kref_init(&res->refcount);
614 ret = init(res, data);
621 spin_lock_irqsave(&test->lock, flags);
622 list_add_tail(&res->node, &test->resources);
623 /* refcount for list is established by kref_init() */
624 spin_unlock_irqrestore(&test->lock, flags);
628 EXPORT_SYMBOL_GPL(kunit_add_resource);
630 int kunit_add_named_resource(struct kunit *test,
631 kunit_resource_init_t init,
632 kunit_resource_free_t free,
633 struct kunit_resource *res,
637 struct kunit_resource *existing;
642 existing = kunit_find_named_resource(test, name);
644 kunit_put_resource(existing);
650 return kunit_add_resource(test, init, free, res, data);
652 EXPORT_SYMBOL_GPL(kunit_add_named_resource);
654 struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
655 kunit_resource_init_t init,
656 kunit_resource_free_t free,
660 struct kunit_resource *res;
663 res = kzalloc(sizeof(*res), internal_gfp);
667 ret = kunit_add_resource(test, init, free, res, data);
670 * bump refcount for get; kunit_resource_put() should be called
673 kunit_get_resource(res);
678 EXPORT_SYMBOL_GPL(kunit_alloc_and_get_resource);
680 void kunit_remove_resource(struct kunit *test, struct kunit_resource *res)
684 spin_lock_irqsave(&test->lock, flags);
685 list_del(&res->node);
686 spin_unlock_irqrestore(&test->lock, flags);
687 kunit_put_resource(res);
689 EXPORT_SYMBOL_GPL(kunit_remove_resource);
691 int kunit_destroy_resource(struct kunit *test, kunit_resource_match_t match,
694 struct kunit_resource *res = kunit_find_resource(test, match,
700 kunit_remove_resource(test, res);
702 /* We have a reference also via _find(); drop it. */
703 kunit_put_resource(res);
707 EXPORT_SYMBOL_GPL(kunit_destroy_resource);
709 struct kunit_kmalloc_array_params {
715 static int kunit_kmalloc_array_init(struct kunit_resource *res, void *context)
717 struct kunit_kmalloc_array_params *params = context;
719 res->data = kmalloc_array(params->n, params->size, params->gfp);
726 static void kunit_kmalloc_array_free(struct kunit_resource *res)
731 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp)
733 struct kunit_kmalloc_array_params params = {
739 return kunit_alloc_resource(test,
740 kunit_kmalloc_array_init,
741 kunit_kmalloc_array_free,
745 EXPORT_SYMBOL_GPL(kunit_kmalloc_array);
747 void kunit_kfree(struct kunit *test, const void *ptr)
749 struct kunit_resource *res;
751 res = kunit_find_resource(test, kunit_resource_instance_match,
755 * Removing the resource from the list of resources drops the
756 * reference count to 1; the final put will trigger the free.
758 kunit_remove_resource(test, res);
760 kunit_put_resource(res);
763 EXPORT_SYMBOL_GPL(kunit_kfree);
765 void kunit_cleanup(struct kunit *test)
767 struct kunit_resource *res;
771 * test->resources is a stack - each allocation must be freed in the
772 * reverse order from which it was added since one resource may depend
773 * on another for its entire lifetime.
774 * Also, we cannot use the normal list_for_each constructs, even the
775 * safe ones because *arbitrary* nodes may be deleted when
776 * kunit_resource_free is called; the list_for_each_safe variants only
777 * protect against the current node being deleted, not the next.
780 spin_lock_irqsave(&test->lock, flags);
781 if (list_empty(&test->resources)) {
782 spin_unlock_irqrestore(&test->lock, flags);
785 res = list_last_entry(&test->resources,
786 struct kunit_resource,
789 * Need to unlock here as a resource may remove another
790 * resource, and this can't happen if the test->lock
793 spin_unlock_irqrestore(&test->lock, flags);
794 kunit_remove_resource(test, res);
796 current->kunit_test = NULL;
798 EXPORT_SYMBOL_GPL(kunit_cleanup);
800 static int __init kunit_init(void)
802 kunit_debugfs_init();
806 late_initcall(kunit_init);
808 static void __exit kunit_exit(void)
810 kunit_debugfs_cleanup();
812 module_exit(kunit_exit);
814 MODULE_LICENSE("GPL v2");