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 void kunit_print_tap_version(void)
21 static bool kunit_has_printed_tap_version;
23 if (!kunit_has_printed_tap_version) {
24 pr_info("TAP version 14\n");
25 kunit_has_printed_tap_version = true;
29 static size_t kunit_test_cases_len(struct kunit_case *test_cases)
31 struct kunit_case *test_case;
34 for (test_case = test_cases; test_case->run_case; test_case++)
40 static void kunit_print_subtest_start(struct kunit_suite *suite)
42 kunit_print_tap_version();
43 pr_info("\t# Subtest: %s\n", suite->name);
44 pr_info("\t1..%zd\n", kunit_test_cases_len(suite->test_cases));
47 static void kunit_print_ok_not_ok(bool should_indent,
50 const char *description)
52 const char *indent, *ok_not_ok;
64 pr_info("%s%s %zd - %s\n", indent, ok_not_ok, test_number, description);
67 static bool kunit_suite_has_succeeded(struct kunit_suite *suite)
69 const struct kunit_case *test_case;
71 for (test_case = suite->test_cases; test_case->run_case; test_case++)
72 if (!test_case->success)
78 static void kunit_print_subtest_end(struct kunit_suite *suite)
80 static size_t kunit_suite_counter = 1;
82 kunit_print_ok_not_ok(false,
83 kunit_suite_has_succeeded(suite),
84 kunit_suite_counter++,
88 static void kunit_print_test_case_ok_not_ok(struct kunit_case *test_case,
91 kunit_print_ok_not_ok(true,
97 static void kunit_print_string_stream(struct kunit *test,
98 struct string_stream *stream)
100 struct string_stream_fragment *fragment;
103 buf = string_stream_get_string(stream);
106 "Could not allocate buffer, dumping stream:\n");
107 list_for_each_entry(fragment, &stream->fragments, node) {
108 kunit_err(test, "%s", fragment->fragment);
110 kunit_err(test, "\n");
112 kunit_err(test, "%s", buf);
113 kunit_kfree(test, buf);
117 static void kunit_fail(struct kunit *test, struct kunit_assert *assert)
119 struct string_stream *stream;
121 kunit_set_failure(test);
123 stream = alloc_string_stream(test, GFP_KERNEL);
126 "Could not allocate stream to print failed assertion in %s:%d\n",
132 assert->format(assert, stream);
134 kunit_print_string_stream(test, stream);
136 WARN_ON(string_stream_destroy(stream));
139 static void __noreturn kunit_abort(struct kunit *test)
141 kunit_try_catch_throw(&test->try_catch); /* Does not return. */
144 * Throw could not abort from test.
146 * XXX: we should never reach this line! As kunit_try_catch_throw is
149 WARN_ONCE(true, "Throw could not abort from test!\n");
152 void kunit_do_assertion(struct kunit *test,
153 struct kunit_assert *assert,
155 const char *fmt, ...)
164 assert->message.fmt = fmt;
165 assert->message.va = &args;
167 kunit_fail(test, assert);
171 if (assert->type == KUNIT_ASSERTION)
175 void kunit_init_test(struct kunit *test, const char *name)
177 spin_lock_init(&test->lock);
178 INIT_LIST_HEAD(&test->resources);
180 test->success = true;
184 * Initializes and runs test case. Does not clean up or do post validations.
186 static void kunit_run_case_internal(struct kunit *test,
187 struct kunit_suite *suite,
188 struct kunit_case *test_case)
193 ret = suite->init(test);
195 kunit_err(test, "failed to initialize: %d\n", ret);
196 kunit_set_failure(test);
201 test_case->run_case(test);
204 static void kunit_case_internal_cleanup(struct kunit *test)
210 * Performs post validations and cleanup after a test case was run.
211 * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
213 static void kunit_run_case_cleanup(struct kunit *test,
214 struct kunit_suite *suite)
219 kunit_case_internal_cleanup(test);
222 struct kunit_try_catch_context {
224 struct kunit_suite *suite;
225 struct kunit_case *test_case;
228 static void kunit_try_run_case(void *data)
230 struct kunit_try_catch_context *ctx = data;
231 struct kunit *test = ctx->test;
232 struct kunit_suite *suite = ctx->suite;
233 struct kunit_case *test_case = ctx->test_case;
236 * kunit_run_case_internal may encounter a fatal error; if it does,
237 * abort will be called, this thread will exit, and finally the parent
238 * thread will resume control and handle any necessary clean up.
240 kunit_run_case_internal(test, suite, test_case);
241 /* This line may never be reached. */
242 kunit_run_case_cleanup(test, suite);
245 static void kunit_catch_run_case(void *data)
247 struct kunit_try_catch_context *ctx = data;
248 struct kunit *test = ctx->test;
249 struct kunit_suite *suite = ctx->suite;
250 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
253 kunit_set_failure(test);
255 * Test case could not finish, we have no idea what state it is
256 * in, so don't do clean up.
258 if (try_exit_code == -ETIMEDOUT) {
259 kunit_err(test, "test case timed out\n");
261 * Unknown internal error occurred preventing test case from
262 * running, so there is nothing to clean up.
265 kunit_err(test, "internal error occurred preventing test case from running: %d\n",
272 * Test case was run, but aborted. It is the test case's business as to
273 * whether it failed or not, we just need to clean up.
275 kunit_run_case_cleanup(test, suite);
279 * Performs all logic to run a test case. It also catches most errors that
280 * occur in a test case and reports them as failures.
282 static void kunit_run_case_catch_errors(struct kunit_suite *suite,
283 struct kunit_case *test_case)
285 struct kunit_try_catch_context context;
286 struct kunit_try_catch *try_catch;
289 kunit_init_test(&test, test_case->name);
290 try_catch = &test.try_catch;
292 kunit_try_catch_init(try_catch,
295 kunit_catch_run_case);
296 context.test = &test;
297 context.suite = suite;
298 context.test_case = test_case;
299 kunit_try_catch_run(try_catch, &context);
301 test_case->success = test.success;
304 int kunit_run_tests(struct kunit_suite *suite)
306 struct kunit_case *test_case;
307 size_t test_case_count = 1;
309 kunit_print_subtest_start(suite);
311 for (test_case = suite->test_cases; test_case->run_case; test_case++) {
312 kunit_run_case_catch_errors(suite, test_case);
313 kunit_print_test_case_ok_not_ok(test_case, test_case_count++);
316 kunit_print_subtest_end(suite);
321 struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
322 kunit_resource_init_t init,
323 kunit_resource_free_t free,
327 struct kunit_resource *res;
330 res = kzalloc(sizeof(*res), internal_gfp);
334 ret = init(res, context);
339 spin_lock(&test->lock);
340 list_add_tail(&res->node, &test->resources);
341 spin_unlock(&test->lock);
346 static void kunit_resource_free(struct kunit *test, struct kunit_resource *res)
352 static struct kunit_resource *kunit_resource_find(struct kunit *test,
353 kunit_resource_match_t match,
354 kunit_resource_free_t free,
357 struct kunit_resource *resource;
359 lockdep_assert_held(&test->lock);
361 list_for_each_entry_reverse(resource, &test->resources, node) {
362 if (resource->free != free)
364 if (match(test, resource->allocation, match_data))
371 static struct kunit_resource *kunit_resource_remove(
373 kunit_resource_match_t match,
374 kunit_resource_free_t free,
377 struct kunit_resource *resource;
379 spin_lock(&test->lock);
380 resource = kunit_resource_find(test, match, free, match_data);
382 list_del(&resource->node);
383 spin_unlock(&test->lock);
388 int kunit_resource_destroy(struct kunit *test,
389 kunit_resource_match_t match,
390 kunit_resource_free_t free,
393 struct kunit_resource *resource;
395 resource = kunit_resource_remove(test, match, free, match_data);
400 kunit_resource_free(test, resource);
404 struct kunit_kmalloc_params {
409 static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
411 struct kunit_kmalloc_params *params = context;
413 res->allocation = kmalloc(params->size, params->gfp);
414 if (!res->allocation)
420 static void kunit_kmalloc_free(struct kunit_resource *res)
422 kfree(res->allocation);
425 void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
427 struct kunit_kmalloc_params params = {
432 return kunit_alloc_resource(test,
439 void kunit_kfree(struct kunit *test, const void *ptr)
443 rc = kunit_resource_destroy(test,
444 kunit_resource_instance_match,
451 void kunit_cleanup(struct kunit *test)
453 struct kunit_resource *resource;
456 * test->resources is a stack - each allocation must be freed in the
457 * reverse order from which it was added since one resource may depend
458 * on another for its entire lifetime.
459 * Also, we cannot use the normal list_for_each constructs, even the
460 * safe ones because *arbitrary* nodes may be deleted when
461 * kunit_resource_free is called; the list_for_each_safe variants only
462 * protect against the current node being deleted, not the next.
465 spin_lock(&test->lock);
466 if (list_empty(&test->resources)) {
467 spin_unlock(&test->lock);
470 resource = list_last_entry(&test->resources,
471 struct kunit_resource,
473 list_del(&resource->node);
474 spin_unlock(&test->lock);
476 kunit_resource_free(test, resource);