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/test.h>
10 #include <kunit/try-catch.h>
11 #include <linux/kernel.h>
12 #include <linux/sched/debug.h>
14 static void kunit_set_failure(struct kunit *test)
16 WRITE_ONCE(test->success, false);
19 static int kunit_vprintk_emit(int level, const char *fmt, va_list args)
21 return vprintk_emit(0, level, NULL, 0, fmt, args);
24 static int kunit_printk_emit(int level, const char *fmt, ...)
30 ret = kunit_vprintk_emit(level, fmt, args);
36 static void kunit_vprintk(const struct kunit *test,
38 struct va_format *vaf)
40 kunit_printk_emit(level[1] - '0', "\t# %s: %pV", test->name, vaf);
43 static void kunit_print_tap_version(void)
45 static bool kunit_has_printed_tap_version;
47 if (!kunit_has_printed_tap_version) {
48 kunit_printk_emit(LOGLEVEL_INFO, "TAP version 14\n");
49 kunit_has_printed_tap_version = true;
53 static size_t kunit_test_cases_len(struct kunit_case *test_cases)
55 struct kunit_case *test_case;
58 for (test_case = test_cases; test_case->run_case; test_case++)
64 static void kunit_print_subtest_start(struct kunit_suite *suite)
66 kunit_print_tap_version();
67 kunit_printk_emit(LOGLEVEL_INFO, "\t# Subtest: %s\n", suite->name);
68 kunit_printk_emit(LOGLEVEL_INFO,
70 kunit_test_cases_len(suite->test_cases));
73 static void kunit_print_ok_not_ok(bool should_indent,
76 const char *description)
78 const char *indent, *ok_not_ok;
90 kunit_printk_emit(LOGLEVEL_INFO,
92 indent, ok_not_ok, test_number, description);
95 static bool kunit_suite_has_succeeded(struct kunit_suite *suite)
97 const struct kunit_case *test_case;
99 for (test_case = suite->test_cases; test_case->run_case; test_case++)
100 if (!test_case->success)
106 static void kunit_print_subtest_end(struct kunit_suite *suite)
108 static size_t kunit_suite_counter = 1;
110 kunit_print_ok_not_ok(false,
111 kunit_suite_has_succeeded(suite),
112 kunit_suite_counter++,
116 static void kunit_print_test_case_ok_not_ok(struct kunit_case *test_case,
119 kunit_print_ok_not_ok(true,
125 static void kunit_print_string_stream(struct kunit *test,
126 struct string_stream *stream)
128 struct string_stream_fragment *fragment;
131 buf = string_stream_get_string(stream);
134 "Could not allocate buffer, dumping stream:\n");
135 list_for_each_entry(fragment, &stream->fragments, node) {
136 kunit_err(test, fragment->fragment);
138 kunit_err(test, "\n");
140 kunit_err(test, buf);
141 kunit_kfree(test, buf);
145 static void kunit_fail(struct kunit *test, struct kunit_assert *assert)
147 struct string_stream *stream;
149 kunit_set_failure(test);
151 stream = alloc_string_stream(test, GFP_KERNEL);
154 "Could not allocate stream to print failed assertion in %s:%d\n",
160 assert->format(assert, stream);
162 kunit_print_string_stream(test, stream);
164 WARN_ON(string_stream_destroy(stream));
167 static void __noreturn kunit_abort(struct kunit *test)
169 kunit_try_catch_throw(&test->try_catch); /* Does not return. */
172 * Throw could not abort from test.
174 * XXX: we should never reach this line! As kunit_try_catch_throw is
177 WARN_ONCE(true, "Throw could not abort from test!\n");
180 void kunit_do_assertion(struct kunit *test,
181 struct kunit_assert *assert,
183 const char *fmt, ...)
192 assert->message.fmt = fmt;
193 assert->message.va = &args;
195 kunit_fail(test, assert);
199 if (assert->type == KUNIT_ASSERTION)
203 void kunit_init_test(struct kunit *test, const char *name)
205 spin_lock_init(&test->lock);
206 INIT_LIST_HEAD(&test->resources);
208 test->success = true;
212 * Initializes and runs test case. Does not clean up or do post validations.
214 static void kunit_run_case_internal(struct kunit *test,
215 struct kunit_suite *suite,
216 struct kunit_case *test_case)
221 ret = suite->init(test);
223 kunit_err(test, "failed to initialize: %d\n", ret);
224 kunit_set_failure(test);
229 test_case->run_case(test);
232 static void kunit_case_internal_cleanup(struct kunit *test)
238 * Performs post validations and cleanup after a test case was run.
239 * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
241 static void kunit_run_case_cleanup(struct kunit *test,
242 struct kunit_suite *suite)
247 kunit_case_internal_cleanup(test);
250 struct kunit_try_catch_context {
252 struct kunit_suite *suite;
253 struct kunit_case *test_case;
256 static void kunit_try_run_case(void *data)
258 struct kunit_try_catch_context *ctx = data;
259 struct kunit *test = ctx->test;
260 struct kunit_suite *suite = ctx->suite;
261 struct kunit_case *test_case = ctx->test_case;
264 * kunit_run_case_internal may encounter a fatal error; if it does,
265 * abort will be called, this thread will exit, and finally the parent
266 * thread will resume control and handle any necessary clean up.
268 kunit_run_case_internal(test, suite, test_case);
269 /* This line may never be reached. */
270 kunit_run_case_cleanup(test, suite);
273 static void kunit_catch_run_case(void *data)
275 struct kunit_try_catch_context *ctx = data;
276 struct kunit *test = ctx->test;
277 struct kunit_suite *suite = ctx->suite;
278 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
281 kunit_set_failure(test);
283 * Test case could not finish, we have no idea what state it is
284 * in, so don't do clean up.
286 if (try_exit_code == -ETIMEDOUT) {
287 kunit_err(test, "test case timed out\n");
289 * Unknown internal error occurred preventing test case from
290 * running, so there is nothing to clean up.
293 kunit_err(test, "internal error occurred preventing test case from running: %d\n",
300 * Test case was run, but aborted. It is the test case's business as to
301 * whether it failed or not, we just need to clean up.
303 kunit_run_case_cleanup(test, suite);
307 * Performs all logic to run a test case. It also catches most errors that
308 * occur in a test case and reports them as failures.
310 static void kunit_run_case_catch_errors(struct kunit_suite *suite,
311 struct kunit_case *test_case)
313 struct kunit_try_catch_context context;
314 struct kunit_try_catch *try_catch;
317 kunit_init_test(&test, test_case->name);
318 try_catch = &test.try_catch;
320 kunit_try_catch_init(try_catch,
323 kunit_catch_run_case);
324 context.test = &test;
325 context.suite = suite;
326 context.test_case = test_case;
327 kunit_try_catch_run(try_catch, &context);
329 test_case->success = test.success;
332 int kunit_run_tests(struct kunit_suite *suite)
334 struct kunit_case *test_case;
335 size_t test_case_count = 1;
337 kunit_print_subtest_start(suite);
339 for (test_case = suite->test_cases; test_case->run_case; test_case++) {
340 kunit_run_case_catch_errors(suite, test_case);
341 kunit_print_test_case_ok_not_ok(test_case, test_case_count++);
344 kunit_print_subtest_end(suite);
349 struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
350 kunit_resource_init_t init,
351 kunit_resource_free_t free,
355 struct kunit_resource *res;
358 res = kzalloc(sizeof(*res), internal_gfp);
362 ret = init(res, context);
367 spin_lock(&test->lock);
368 list_add_tail(&res->node, &test->resources);
369 spin_unlock(&test->lock);
374 static void kunit_resource_free(struct kunit *test, struct kunit_resource *res)
380 static struct kunit_resource *kunit_resource_find(struct kunit *test,
381 kunit_resource_match_t match,
382 kunit_resource_free_t free,
385 struct kunit_resource *resource;
387 lockdep_assert_held(&test->lock);
389 list_for_each_entry_reverse(resource, &test->resources, node) {
390 if (resource->free != free)
392 if (match(test, resource->allocation, match_data))
399 static struct kunit_resource *kunit_resource_remove(
401 kunit_resource_match_t match,
402 kunit_resource_free_t free,
405 struct kunit_resource *resource;
407 spin_lock(&test->lock);
408 resource = kunit_resource_find(test, match, free, match_data);
410 list_del(&resource->node);
411 spin_unlock(&test->lock);
416 int kunit_resource_destroy(struct kunit *test,
417 kunit_resource_match_t match,
418 kunit_resource_free_t free,
421 struct kunit_resource *resource;
423 resource = kunit_resource_remove(test, match, free, match_data);
428 kunit_resource_free(test, resource);
432 struct kunit_kmalloc_params {
437 static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
439 struct kunit_kmalloc_params *params = context;
441 res->allocation = kmalloc(params->size, params->gfp);
442 if (!res->allocation)
448 static void kunit_kmalloc_free(struct kunit_resource *res)
450 kfree(res->allocation);
453 void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
455 struct kunit_kmalloc_params params = {
460 return kunit_alloc_resource(test,
467 void kunit_kfree(struct kunit *test, const void *ptr)
471 rc = kunit_resource_destroy(test,
472 kunit_resource_instance_match,
479 void kunit_cleanup(struct kunit *test)
481 struct kunit_resource *resource;
484 * test->resources is a stack - each allocation must be freed in the
485 * reverse order from which it was added since one resource may depend
486 * on another for its entire lifetime.
487 * Also, we cannot use the normal list_for_each constructs, even the
488 * safe ones because *arbitrary* nodes may be deleted when
489 * kunit_resource_free is called; the list_for_each_safe variants only
490 * protect against the current node being deleted, not the next.
493 spin_lock(&test->lock);
494 if (list_empty(&test->resources)) {
495 spin_unlock(&test->lock);
498 resource = list_last_entry(&test->resources,
499 struct kunit_resource,
501 list_del(&resource->node);
502 spin_unlock(&test->lock);
504 kunit_resource_free(test, resource);
508 void kunit_printk(const char *level,
509 const struct kunit *test,
510 const char *fmt, ...)
512 struct va_format vaf;
520 kunit_vprintk(test, level, &vaf);