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/moduleparam.h>
14 #include <linux/panic.h>
15 #include <linux/sched/debug.h>
16 #include <linux/sched.h>
19 #include "string-stream.h"
20 #include "try-catch-impl.h"
22 #if IS_BUILTIN(CONFIG_KUNIT)
24 * Fail the current test and print an error message to the log.
26 void __kunit_fail_current_test(const char *file, int line, const char *fmt, ...)
32 if (!current->kunit_test)
35 kunit_set_failure(current->kunit_test);
37 /* kunit_err() only accepts literals, so evaluate the args first. */
39 len = vsnprintf(NULL, 0, fmt, args) + 1;
42 buffer = kunit_kmalloc(current->kunit_test, len, GFP_KERNEL);
47 vsnprintf(buffer, len, fmt, args);
50 kunit_err(current->kunit_test, "%s:%d: %s", file, line, buffer);
51 kunit_kfree(current->kunit_test, buffer);
53 EXPORT_SYMBOL_GPL(__kunit_fail_current_test);
57 * KUnit statistic mode:
59 * 1 - only when there is more than one subtest
62 static int kunit_stats_enabled = 1;
63 module_param_named(stats_enabled, kunit_stats_enabled, int, 0644);
64 MODULE_PARM_DESC(stats_enabled,
65 "Print test stats: never (0), only for multiple subtests (1), or always (2)");
67 struct kunit_result_stats {
69 unsigned long skipped;
74 static bool kunit_should_print_stats(struct kunit_result_stats stats)
76 if (kunit_stats_enabled == 0)
79 if (kunit_stats_enabled == 2)
82 return (stats.total > 1);
85 static void kunit_print_test_stats(struct kunit *test,
86 struct kunit_result_stats stats)
88 if (!kunit_should_print_stats(stats))
91 kunit_log(KERN_INFO, test,
93 "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
102 * Append formatted message to log, size of which is limited to
103 * KUNIT_LOG_SIZE bytes (including null terminating byte).
105 void kunit_log_append(char *log, const char *fmt, ...)
107 char line[KUNIT_LOG_SIZE];
114 len_left = KUNIT_LOG_SIZE - strlen(log) - 1;
119 vsnprintf(line, sizeof(line), fmt, args);
122 strncat(log, line, len_left);
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 static void kunit_print_suite_start(struct kunit_suite *suite)
140 kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "# Subtest: %s",
142 kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "1..%zd",
143 kunit_suite_num_test_cases(suite));
146 static void kunit_print_ok_not_ok(void *test_or_suite,
148 enum kunit_status status,
150 const char *description,
151 const char *directive)
153 struct kunit_suite *suite = is_test ? NULL : test_or_suite;
154 struct kunit *test = is_test ? test_or_suite : NULL;
155 const char *directive_header = (status == KUNIT_SKIPPED) ? " # SKIP " : "";
158 * We do not log the test suite results as doing so would
159 * mean debugfs display would consist of the test suite
160 * description and status prior to individual test results.
161 * Hence directly printk the suite status, and we will
162 * separately seq_printf() the suite status for the debugfs
166 pr_info("%s %zd - %s%s%s\n",
167 kunit_status_to_ok_not_ok(status),
168 test_number, description, directive_header,
169 (status == KUNIT_SKIPPED) ? directive : "");
171 kunit_log(KERN_INFO, test,
172 KUNIT_SUBTEST_INDENT "%s %zd - %s%s%s",
173 kunit_status_to_ok_not_ok(status),
174 test_number, description, directive_header,
175 (status == KUNIT_SKIPPED) ? directive : "");
178 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite)
180 const struct kunit_case *test_case;
181 enum kunit_status status = KUNIT_SKIPPED;
183 if (suite->suite_init_err)
184 return KUNIT_FAILURE;
186 kunit_suite_for_each_test_case(suite, test_case) {
187 if (test_case->status == KUNIT_FAILURE)
188 return KUNIT_FAILURE;
189 else if (test_case->status == KUNIT_SUCCESS)
190 status = KUNIT_SUCCESS;
195 EXPORT_SYMBOL_GPL(kunit_suite_has_succeeded);
197 static size_t kunit_suite_counter = 1;
199 static void kunit_print_suite_end(struct kunit_suite *suite)
201 kunit_print_ok_not_ok((void *)suite, false,
202 kunit_suite_has_succeeded(suite),
203 kunit_suite_counter++,
205 suite->status_comment);
208 unsigned int kunit_test_case_num(struct kunit_suite *suite,
209 struct kunit_case *test_case)
211 struct kunit_case *tc;
214 kunit_suite_for_each_test_case(suite, tc) {
222 EXPORT_SYMBOL_GPL(kunit_test_case_num);
224 static void kunit_print_string_stream(struct kunit *test,
225 struct string_stream *stream)
227 struct string_stream_fragment *fragment;
230 if (string_stream_is_empty(stream))
233 buf = string_stream_get_string(stream);
236 "Could not allocate buffer, dumping stream:\n");
237 list_for_each_entry(fragment, &stream->fragments, node) {
238 kunit_err(test, "%s", fragment->fragment);
240 kunit_err(test, "\n");
242 kunit_err(test, "%s", buf);
243 kunit_kfree(test, buf);
247 static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
248 enum kunit_assert_type type, const struct kunit_assert *assert,
249 const struct va_format *message)
251 struct string_stream *stream;
253 kunit_set_failure(test);
255 stream = alloc_string_stream(test, GFP_KERNEL);
258 "Could not allocate stream to print failed assertion in %s:%d\n",
264 kunit_assert_prologue(loc, type, stream);
265 assert->format(assert, message, stream);
267 kunit_print_string_stream(test, stream);
269 WARN_ON(string_stream_destroy(stream));
272 static void __noreturn kunit_abort(struct kunit *test)
274 kunit_try_catch_throw(&test->try_catch); /* Does not return. */
277 * Throw could not abort from test.
279 * XXX: we should never reach this line! As kunit_try_catch_throw is
282 WARN_ONCE(true, "Throw could not abort from test!\n");
285 void kunit_do_failed_assertion(struct kunit *test,
286 const struct kunit_loc *loc,
287 enum kunit_assert_type type,
288 const struct kunit_assert *assert,
289 const char *fmt, ...)
292 struct va_format message;
298 kunit_fail(test, loc, type, assert, &message);
302 if (type == KUNIT_ASSERTION)
305 EXPORT_SYMBOL_GPL(kunit_do_failed_assertion);
307 void kunit_init_test(struct kunit *test, const char *name, char *log)
309 spin_lock_init(&test->lock);
310 INIT_LIST_HEAD(&test->resources);
315 test->status = KUNIT_SUCCESS;
316 test->status_comment[0] = '\0';
318 EXPORT_SYMBOL_GPL(kunit_init_test);
321 * Initializes and runs test case. Does not clean up or do post validations.
323 static void kunit_run_case_internal(struct kunit *test,
324 struct kunit_suite *suite,
325 struct kunit_case *test_case)
330 ret = suite->init(test);
332 kunit_err(test, "failed to initialize: %d\n", ret);
333 kunit_set_failure(test);
338 test_case->run_case(test);
341 static void kunit_case_internal_cleanup(struct kunit *test)
347 * Performs post validations and cleanup after a test case was run.
348 * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
350 static void kunit_run_case_cleanup(struct kunit *test,
351 struct kunit_suite *suite)
356 kunit_case_internal_cleanup(test);
359 struct kunit_try_catch_context {
361 struct kunit_suite *suite;
362 struct kunit_case *test_case;
365 static void kunit_try_run_case(void *data)
367 struct kunit_try_catch_context *ctx = data;
368 struct kunit *test = ctx->test;
369 struct kunit_suite *suite = ctx->suite;
370 struct kunit_case *test_case = ctx->test_case;
372 current->kunit_test = test;
375 * kunit_run_case_internal may encounter a fatal error; if it does,
376 * abort will be called, this thread will exit, and finally the parent
377 * thread will resume control and handle any necessary clean up.
379 kunit_run_case_internal(test, suite, test_case);
380 /* This line may never be reached. */
381 kunit_run_case_cleanup(test, suite);
384 static void kunit_catch_run_case(void *data)
386 struct kunit_try_catch_context *ctx = data;
387 struct kunit *test = ctx->test;
388 struct kunit_suite *suite = ctx->suite;
389 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
392 kunit_set_failure(test);
394 * Test case could not finish, we have no idea what state it is
395 * in, so don't do clean up.
397 if (try_exit_code == -ETIMEDOUT) {
398 kunit_err(test, "test case timed out\n");
400 * Unknown internal error occurred preventing test case from
401 * running, so there is nothing to clean up.
404 kunit_err(test, "internal error occurred preventing test case from running: %d\n",
411 * Test case was run, but aborted. It is the test case's business as to
412 * whether it failed or not, we just need to clean up.
414 kunit_run_case_cleanup(test, suite);
418 * Performs all logic to run a test case. It also catches most errors that
419 * occur in a test case and reports them as failures.
421 static void kunit_run_case_catch_errors(struct kunit_suite *suite,
422 struct kunit_case *test_case,
425 struct kunit_try_catch_context context;
426 struct kunit_try_catch *try_catch;
428 kunit_init_test(test, test_case->name, test_case->log);
429 try_catch = &test->try_catch;
431 kunit_try_catch_init(try_catch,
434 kunit_catch_run_case);
436 context.suite = suite;
437 context.test_case = test_case;
438 kunit_try_catch_run(try_catch, &context);
440 /* Propagate the parameter result to the test case. */
441 if (test->status == KUNIT_FAILURE)
442 test_case->status = KUNIT_FAILURE;
443 else if (test_case->status != KUNIT_FAILURE && test->status == KUNIT_SUCCESS)
444 test_case->status = KUNIT_SUCCESS;
447 static void kunit_print_suite_stats(struct kunit_suite *suite,
448 struct kunit_result_stats suite_stats,
449 struct kunit_result_stats param_stats)
451 if (kunit_should_print_stats(suite_stats)) {
452 kunit_log(KERN_INFO, suite,
453 "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
461 if (kunit_should_print_stats(param_stats)) {
462 kunit_log(KERN_INFO, suite,
463 "# Totals: pass:%lu fail:%lu skip:%lu total:%lu",
471 static void kunit_update_stats(struct kunit_result_stats *stats,
472 enum kunit_status status)
489 static void kunit_accumulate_stats(struct kunit_result_stats *total,
490 struct kunit_result_stats add)
492 total->passed += add.passed;
493 total->skipped += add.skipped;
494 total->failed += add.failed;
495 total->total += add.total;
498 int kunit_run_tests(struct kunit_suite *suite)
500 char param_desc[KUNIT_PARAM_DESC_SIZE];
501 struct kunit_case *test_case;
502 struct kunit_result_stats suite_stats = { 0 };
503 struct kunit_result_stats total_stats = { 0 };
505 /* Taint the kernel so we know we've run tests. */
506 add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
508 if (suite->suite_init) {
509 suite->suite_init_err = suite->suite_init(suite);
510 if (suite->suite_init_err) {
511 kunit_err(suite, KUNIT_SUBTEST_INDENT
512 "# failed to initialize (%d)", suite->suite_init_err);
517 kunit_print_suite_start(suite);
519 kunit_suite_for_each_test_case(suite, test_case) {
520 struct kunit test = { .param_value = NULL, .param_index = 0 };
521 struct kunit_result_stats param_stats = { 0 };
522 test_case->status = KUNIT_SKIPPED;
524 if (!test_case->generate_params) {
525 /* Non-parameterised test. */
526 kunit_run_case_catch_errors(suite, test_case, &test);
527 kunit_update_stats(¶m_stats, test.status);
529 /* Get initial param. */
530 param_desc[0] = '\0';
531 test.param_value = test_case->generate_params(NULL, param_desc);
532 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
533 "# Subtest: %s", test_case->name);
535 while (test.param_value) {
536 kunit_run_case_catch_errors(suite, test_case, &test);
538 if (param_desc[0] == '\0') {
539 snprintf(param_desc, sizeof(param_desc),
540 "param-%d", test.param_index);
543 kunit_log(KERN_INFO, &test,
544 KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
546 kunit_status_to_ok_not_ok(test.status),
547 test.param_index + 1, param_desc);
549 /* Get next param. */
550 param_desc[0] = '\0';
551 test.param_value = test_case->generate_params(test.param_value, param_desc);
554 kunit_update_stats(¶m_stats, test.status);
559 kunit_print_test_stats(&test, param_stats);
561 kunit_print_ok_not_ok(&test, true, test_case->status,
562 kunit_test_case_num(suite, test_case),
564 test.status_comment);
566 kunit_update_stats(&suite_stats, test_case->status);
567 kunit_accumulate_stats(&total_stats, param_stats);
570 if (suite->suite_exit)
571 suite->suite_exit(suite);
573 kunit_print_suite_stats(suite, suite_stats, total_stats);
575 kunit_print_suite_end(suite);
579 EXPORT_SYMBOL_GPL(kunit_run_tests);
581 static void kunit_init_suite(struct kunit_suite *suite)
583 kunit_debugfs_create_suite(suite);
584 suite->status_comment[0] = '\0';
585 suite->suite_init_err = 0;
588 int __kunit_test_suites_init(struct kunit_suite * const * const suites)
592 for (i = 0; suites[i] != NULL; i++) {
593 kunit_init_suite(suites[i]);
594 kunit_run_tests(suites[i]);
598 EXPORT_SYMBOL_GPL(__kunit_test_suites_init);
600 static void kunit_exit_suite(struct kunit_suite *suite)
602 kunit_debugfs_destroy_suite(suite);
605 void __kunit_test_suites_exit(struct kunit_suite **suites)
609 for (i = 0; suites[i] != NULL; i++)
610 kunit_exit_suite(suites[i]);
612 kunit_suite_counter = 1;
614 EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
616 struct kunit_kmalloc_array_params {
622 static int kunit_kmalloc_array_init(struct kunit_resource *res, void *context)
624 struct kunit_kmalloc_array_params *params = context;
626 res->data = kmalloc_array(params->n, params->size, params->gfp);
633 static void kunit_kmalloc_array_free(struct kunit_resource *res)
638 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp)
640 struct kunit_kmalloc_array_params params = {
646 return kunit_alloc_resource(test,
647 kunit_kmalloc_array_init,
648 kunit_kmalloc_array_free,
652 EXPORT_SYMBOL_GPL(kunit_kmalloc_array);
654 void kunit_kfree(struct kunit *test, const void *ptr)
656 struct kunit_resource *res;
658 res = kunit_find_resource(test, kunit_resource_instance_match,
662 * Removing the resource from the list of resources drops the
663 * reference count to 1; the final put will trigger the free.
665 kunit_remove_resource(test, res);
667 kunit_put_resource(res);
670 EXPORT_SYMBOL_GPL(kunit_kfree);
672 void kunit_cleanup(struct kunit *test)
674 struct kunit_resource *res;
678 * test->resources is a stack - each allocation must be freed in the
679 * reverse order from which it was added since one resource may depend
680 * on another for its entire lifetime.
681 * Also, we cannot use the normal list_for_each constructs, even the
682 * safe ones because *arbitrary* nodes may be deleted when
683 * kunit_resource_free is called; the list_for_each_safe variants only
684 * protect against the current node being deleted, not the next.
687 spin_lock_irqsave(&test->lock, flags);
688 if (list_empty(&test->resources)) {
689 spin_unlock_irqrestore(&test->lock, flags);
692 res = list_last_entry(&test->resources,
693 struct kunit_resource,
696 * Need to unlock here as a resource may remove another
697 * resource, and this can't happen if the test->lock
700 spin_unlock_irqrestore(&test->lock, flags);
701 kunit_remove_resource(test, res);
703 current->kunit_test = NULL;
705 EXPORT_SYMBOL_GPL(kunit_cleanup);
707 static int __init kunit_init(void)
709 kunit_debugfs_init();
713 late_initcall(kunit_init);
715 static void __exit kunit_exit(void)
717 kunit_debugfs_cleanup();
719 module_exit(kunit_exit);
721 MODULE_LICENSE("GPL v2");