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 <linux/kernel.h>
11 #include <linux/sched/debug.h>
14 #include "string-stream.h"
15 #include "try-catch-impl.h"
17 static void kunit_set_failure(struct kunit *test)
19 WRITE_ONCE(test->success, false);
22 static void kunit_print_tap_version(void)
24 static bool kunit_has_printed_tap_version;
26 if (!kunit_has_printed_tap_version) {
27 pr_info("TAP version 14\n");
28 kunit_has_printed_tap_version = true;
33 * Append formatted message to log, size of which is limited to
34 * KUNIT_LOG_SIZE bytes (including null terminating byte).
36 void kunit_log_append(char *log, const char *fmt, ...)
38 char line[KUNIT_LOG_SIZE];
45 len_left = KUNIT_LOG_SIZE - strlen(log) - 1;
50 vsnprintf(line, sizeof(line), fmt, args);
53 strncat(log, line, len_left);
55 EXPORT_SYMBOL_GPL(kunit_log_append);
57 size_t kunit_suite_num_test_cases(struct kunit_suite *suite)
59 struct kunit_case *test_case;
62 kunit_suite_for_each_test_case(suite, test_case)
67 EXPORT_SYMBOL_GPL(kunit_suite_num_test_cases);
69 static void kunit_print_subtest_start(struct kunit_suite *suite)
71 kunit_print_tap_version();
72 kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "# Subtest: %s",
74 kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "1..%zd",
75 kunit_suite_num_test_cases(suite));
78 static void kunit_print_ok_not_ok(void *test_or_suite,
82 const char *description)
84 struct kunit_suite *suite = is_test ? NULL : test_or_suite;
85 struct kunit *test = is_test ? test_or_suite : NULL;
88 * We do not log the test suite results as doing so would
89 * mean debugfs display would consist of the test suite
90 * description and status prior to individual test results.
91 * Hence directly printk the suite status, and we will
92 * separately seq_printf() the suite status for the debugfs
96 pr_info("%s %zd - %s",
97 kunit_status_to_string(is_ok),
98 test_number, description);
100 kunit_log(KERN_INFO, test, KUNIT_SUBTEST_INDENT "%s %zd - %s",
101 kunit_status_to_string(is_ok),
102 test_number, description);
105 bool kunit_suite_has_succeeded(struct kunit_suite *suite)
107 const struct kunit_case *test_case;
109 kunit_suite_for_each_test_case(suite, test_case) {
110 if (!test_case->success)
116 EXPORT_SYMBOL_GPL(kunit_suite_has_succeeded);
118 static void kunit_print_subtest_end(struct kunit_suite *suite)
120 static size_t kunit_suite_counter = 1;
122 kunit_print_ok_not_ok((void *)suite, false,
123 kunit_suite_has_succeeded(suite),
124 kunit_suite_counter++,
128 unsigned int kunit_test_case_num(struct kunit_suite *suite,
129 struct kunit_case *test_case)
131 struct kunit_case *tc;
134 kunit_suite_for_each_test_case(suite, tc) {
142 EXPORT_SYMBOL_GPL(kunit_test_case_num);
144 static void kunit_print_string_stream(struct kunit *test,
145 struct string_stream *stream)
147 struct string_stream_fragment *fragment;
150 if (string_stream_is_empty(stream))
153 buf = string_stream_get_string(stream);
156 "Could not allocate buffer, dumping stream:\n");
157 list_for_each_entry(fragment, &stream->fragments, node) {
158 kunit_err(test, "%s", fragment->fragment);
160 kunit_err(test, "\n");
162 kunit_err(test, "%s", buf);
163 kunit_kfree(test, buf);
167 static void kunit_fail(struct kunit *test, struct kunit_assert *assert)
169 struct string_stream *stream;
171 kunit_set_failure(test);
173 stream = alloc_string_stream(test, GFP_KERNEL);
176 "Could not allocate stream to print failed assertion in %s:%d\n",
182 assert->format(assert, stream);
184 kunit_print_string_stream(test, stream);
186 WARN_ON(string_stream_destroy(stream));
189 static void __noreturn kunit_abort(struct kunit *test)
191 kunit_try_catch_throw(&test->try_catch); /* Does not return. */
194 * Throw could not abort from test.
196 * XXX: we should never reach this line! As kunit_try_catch_throw is
199 WARN_ONCE(true, "Throw could not abort from test!\n");
202 void kunit_do_assertion(struct kunit *test,
203 struct kunit_assert *assert,
205 const char *fmt, ...)
214 assert->message.fmt = fmt;
215 assert->message.va = &args;
217 kunit_fail(test, assert);
221 if (assert->type == KUNIT_ASSERTION)
224 EXPORT_SYMBOL_GPL(kunit_do_assertion);
226 void kunit_init_test(struct kunit *test, const char *name, char *log)
228 spin_lock_init(&test->lock);
229 INIT_LIST_HEAD(&test->resources);
234 test->success = true;
236 EXPORT_SYMBOL_GPL(kunit_init_test);
239 * Initializes and runs test case. Does not clean up or do post validations.
241 static void kunit_run_case_internal(struct kunit *test,
242 struct kunit_suite *suite,
243 struct kunit_case *test_case)
248 ret = suite->init(test);
250 kunit_err(test, "failed to initialize: %d\n", ret);
251 kunit_set_failure(test);
256 test_case->run_case(test);
259 static void kunit_case_internal_cleanup(struct kunit *test)
265 * Performs post validations and cleanup after a test case was run.
266 * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
268 static void kunit_run_case_cleanup(struct kunit *test,
269 struct kunit_suite *suite)
274 kunit_case_internal_cleanup(test);
277 struct kunit_try_catch_context {
279 struct kunit_suite *suite;
280 struct kunit_case *test_case;
283 static void kunit_try_run_case(void *data)
285 struct kunit_try_catch_context *ctx = data;
286 struct kunit *test = ctx->test;
287 struct kunit_suite *suite = ctx->suite;
288 struct kunit_case *test_case = ctx->test_case;
291 * kunit_run_case_internal may encounter a fatal error; if it does,
292 * abort will be called, this thread will exit, and finally the parent
293 * thread will resume control and handle any necessary clean up.
295 kunit_run_case_internal(test, suite, test_case);
296 /* This line may never be reached. */
297 kunit_run_case_cleanup(test, suite);
300 static void kunit_catch_run_case(void *data)
302 struct kunit_try_catch_context *ctx = data;
303 struct kunit *test = ctx->test;
304 struct kunit_suite *suite = ctx->suite;
305 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
308 kunit_set_failure(test);
310 * Test case could not finish, we have no idea what state it is
311 * in, so don't do clean up.
313 if (try_exit_code == -ETIMEDOUT) {
314 kunit_err(test, "test case timed out\n");
316 * Unknown internal error occurred preventing test case from
317 * running, so there is nothing to clean up.
320 kunit_err(test, "internal error occurred preventing test case from running: %d\n",
327 * Test case was run, but aborted. It is the test case's business as to
328 * whether it failed or not, we just need to clean up.
330 kunit_run_case_cleanup(test, suite);
334 * Performs all logic to run a test case. It also catches most errors that
335 * occur in a test case and reports them as failures.
337 static void kunit_run_case_catch_errors(struct kunit_suite *suite,
338 struct kunit_case *test_case)
340 struct kunit_try_catch_context context;
341 struct kunit_try_catch *try_catch;
344 kunit_init_test(&test, test_case->name, test_case->log);
345 try_catch = &test.try_catch;
347 kunit_try_catch_init(try_catch,
350 kunit_catch_run_case);
351 context.test = &test;
352 context.suite = suite;
353 context.test_case = test_case;
354 kunit_try_catch_run(try_catch, &context);
356 test_case->success = test.success;
358 kunit_print_ok_not_ok(&test, true, test_case->success,
359 kunit_test_case_num(suite, test_case),
363 int kunit_run_tests(struct kunit_suite *suite)
365 struct kunit_case *test_case;
367 kunit_print_subtest_start(suite);
369 kunit_suite_for_each_test_case(suite, test_case)
370 kunit_run_case_catch_errors(suite, test_case);
372 kunit_print_subtest_end(suite);
376 EXPORT_SYMBOL_GPL(kunit_run_tests);
378 static void kunit_init_suite(struct kunit_suite *suite)
380 kunit_debugfs_create_suite(suite);
383 int __kunit_test_suites_init(struct kunit_suite **suites)
387 for (i = 0; suites[i] != NULL; i++) {
388 kunit_init_suite(suites[i]);
389 kunit_run_tests(suites[i]);
393 EXPORT_SYMBOL_GPL(__kunit_test_suites_init);
395 static void kunit_exit_suite(struct kunit_suite *suite)
397 kunit_debugfs_destroy_suite(suite);
400 void __kunit_test_suites_exit(struct kunit_suite **suites)
404 for (i = 0; suites[i] != NULL; i++)
405 kunit_exit_suite(suites[i]);
407 EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
409 struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
410 kunit_resource_init_t init,
411 kunit_resource_free_t free,
415 struct kunit_resource *res;
418 res = kzalloc(sizeof(*res), internal_gfp);
422 ret = init(res, context);
427 spin_lock(&test->lock);
428 list_add_tail(&res->node, &test->resources);
429 spin_unlock(&test->lock);
433 EXPORT_SYMBOL_GPL(kunit_alloc_and_get_resource);
435 static void kunit_resource_free(struct kunit *test, struct kunit_resource *res)
441 static struct kunit_resource *kunit_resource_find(struct kunit *test,
442 kunit_resource_match_t match,
443 kunit_resource_free_t free,
446 struct kunit_resource *resource;
448 lockdep_assert_held(&test->lock);
450 list_for_each_entry_reverse(resource, &test->resources, node) {
451 if (resource->free != free)
453 if (match(test, resource->allocation, match_data))
460 static struct kunit_resource *kunit_resource_remove(
462 kunit_resource_match_t match,
463 kunit_resource_free_t free,
466 struct kunit_resource *resource;
468 spin_lock(&test->lock);
469 resource = kunit_resource_find(test, match, free, match_data);
471 list_del(&resource->node);
472 spin_unlock(&test->lock);
477 int kunit_resource_destroy(struct kunit *test,
478 kunit_resource_match_t match,
479 kunit_resource_free_t free,
482 struct kunit_resource *resource;
484 resource = kunit_resource_remove(test, match, free, match_data);
489 kunit_resource_free(test, resource);
492 EXPORT_SYMBOL_GPL(kunit_resource_destroy);
494 struct kunit_kmalloc_params {
499 static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
501 struct kunit_kmalloc_params *params = context;
503 res->allocation = kmalloc(params->size, params->gfp);
504 if (!res->allocation)
510 static void kunit_kmalloc_free(struct kunit_resource *res)
512 kfree(res->allocation);
515 void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
517 struct kunit_kmalloc_params params = {
522 return kunit_alloc_resource(test,
528 EXPORT_SYMBOL_GPL(kunit_kmalloc);
530 void kunit_kfree(struct kunit *test, const void *ptr)
534 rc = kunit_resource_destroy(test,
535 kunit_resource_instance_match,
541 EXPORT_SYMBOL_GPL(kunit_kfree);
543 void kunit_cleanup(struct kunit *test)
545 struct kunit_resource *resource;
548 * test->resources is a stack - each allocation must be freed in the
549 * reverse order from which it was added since one resource may depend
550 * on another for its entire lifetime.
551 * Also, we cannot use the normal list_for_each constructs, even the
552 * safe ones because *arbitrary* nodes may be deleted when
553 * kunit_resource_free is called; the list_for_each_safe variants only
554 * protect against the current node being deleted, not the next.
557 spin_lock(&test->lock);
558 if (list_empty(&test->resources)) {
559 spin_unlock(&test->lock);
562 resource = list_last_entry(&test->resources,
563 struct kunit_resource,
565 list_del(&resource->node);
566 spin_unlock(&test->lock);
568 kunit_resource_free(test, resource);
571 EXPORT_SYMBOL_GPL(kunit_cleanup);
573 static int __init kunit_init(void)
575 kunit_debugfs_init();
579 late_initcall(kunit_init);
581 static void __exit kunit_exit(void)
583 kunit_debugfs_cleanup();
585 module_exit(kunit_exit);
587 MODULE_LICENSE("GPL v2");