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 <linux/kernel.h>
11 #include <linux/sched/debug.h>
13 #include "string-stream.h"
14 #include "try-catch-impl.h"
16 static void kunit_set_failure(struct kunit
*test
)
18 WRITE_ONCE(test
->success
, false);
21 static void kunit_print_tap_version(void)
23 static bool kunit_has_printed_tap_version
;
25 if (!kunit_has_printed_tap_version
) {
26 pr_info("TAP version 14\n");
27 kunit_has_printed_tap_version
= true;
31 static size_t kunit_test_cases_len(struct kunit_case
*test_cases
)
33 struct kunit_case
*test_case
;
36 for (test_case
= test_cases
; test_case
->run_case
; test_case
++)
42 static void kunit_print_subtest_start(struct kunit_suite
*suite
)
44 kunit_print_tap_version();
45 pr_info("\t# Subtest: %s\n", suite
->name
);
46 pr_info("\t1..%zd\n", kunit_test_cases_len(suite
->test_cases
));
49 static void kunit_print_ok_not_ok(bool should_indent
,
52 const char *description
)
54 const char *indent
, *ok_not_ok
;
66 pr_info("%s%s %zd - %s\n", indent
, ok_not_ok
, test_number
, description
);
69 static bool kunit_suite_has_succeeded(struct kunit_suite
*suite
)
71 const struct kunit_case
*test_case
;
73 for (test_case
= suite
->test_cases
; test_case
->run_case
; test_case
++)
74 if (!test_case
->success
)
80 static void kunit_print_subtest_end(struct kunit_suite
*suite
)
82 static size_t kunit_suite_counter
= 1;
84 kunit_print_ok_not_ok(false,
85 kunit_suite_has_succeeded(suite
),
86 kunit_suite_counter
++,
90 static void kunit_print_test_case_ok_not_ok(struct kunit_case
*test_case
,
93 kunit_print_ok_not_ok(true,
99 static void kunit_print_string_stream(struct kunit
*test
,
100 struct string_stream
*stream
)
102 struct string_stream_fragment
*fragment
;
105 buf
= string_stream_get_string(stream
);
108 "Could not allocate buffer, dumping stream:\n");
109 list_for_each_entry(fragment
, &stream
->fragments
, node
) {
110 kunit_err(test
, "%s", fragment
->fragment
);
112 kunit_err(test
, "\n");
114 kunit_err(test
, "%s", buf
);
115 kunit_kfree(test
, buf
);
119 static void kunit_fail(struct kunit
*test
, struct kunit_assert
*assert)
121 struct string_stream
*stream
;
123 kunit_set_failure(test
);
125 stream
= alloc_string_stream(test
, GFP_KERNEL
);
128 "Could not allocate stream to print failed assertion in %s:%d\n",
134 assert->format(assert, stream
);
136 kunit_print_string_stream(test
, stream
);
138 WARN_ON(string_stream_destroy(stream
));
141 static void __noreturn
kunit_abort(struct kunit
*test
)
143 kunit_try_catch_throw(&test
->try_catch
); /* Does not return. */
146 * Throw could not abort from test.
148 * XXX: we should never reach this line! As kunit_try_catch_throw is
151 WARN_ONCE(true, "Throw could not abort from test!\n");
154 void kunit_do_assertion(struct kunit
*test
,
155 struct kunit_assert
*assert,
157 const char *fmt
, ...)
166 assert->message
.fmt
= fmt
;
167 assert->message
.va
= &args
;
169 kunit_fail(test
, assert);
173 if (assert->type
== KUNIT_ASSERTION
)
176 EXPORT_SYMBOL_GPL(kunit_do_assertion
);
178 void kunit_init_test(struct kunit
*test
, const char *name
)
180 spin_lock_init(&test
->lock
);
181 INIT_LIST_HEAD(&test
->resources
);
183 test
->success
= true;
185 EXPORT_SYMBOL_GPL(kunit_init_test
);
188 * Initializes and runs test case. Does not clean up or do post validations.
190 static void kunit_run_case_internal(struct kunit
*test
,
191 struct kunit_suite
*suite
,
192 struct kunit_case
*test_case
)
197 ret
= suite
->init(test
);
199 kunit_err(test
, "failed to initialize: %d\n", ret
);
200 kunit_set_failure(test
);
205 test_case
->run_case(test
);
208 static void kunit_case_internal_cleanup(struct kunit
*test
)
214 * Performs post validations and cleanup after a test case was run.
215 * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
217 static void kunit_run_case_cleanup(struct kunit
*test
,
218 struct kunit_suite
*suite
)
223 kunit_case_internal_cleanup(test
);
226 struct kunit_try_catch_context
{
228 struct kunit_suite
*suite
;
229 struct kunit_case
*test_case
;
232 static void kunit_try_run_case(void *data
)
234 struct kunit_try_catch_context
*ctx
= data
;
235 struct kunit
*test
= ctx
->test
;
236 struct kunit_suite
*suite
= ctx
->suite
;
237 struct kunit_case
*test_case
= ctx
->test_case
;
240 * kunit_run_case_internal may encounter a fatal error; if it does,
241 * abort will be called, this thread will exit, and finally the parent
242 * thread will resume control and handle any necessary clean up.
244 kunit_run_case_internal(test
, suite
, test_case
);
245 /* This line may never be reached. */
246 kunit_run_case_cleanup(test
, suite
);
249 static void kunit_catch_run_case(void *data
)
251 struct kunit_try_catch_context
*ctx
= data
;
252 struct kunit
*test
= ctx
->test
;
253 struct kunit_suite
*suite
= ctx
->suite
;
254 int try_exit_code
= kunit_try_catch_get_result(&test
->try_catch
);
257 kunit_set_failure(test
);
259 * Test case could not finish, we have no idea what state it is
260 * in, so don't do clean up.
262 if (try_exit_code
== -ETIMEDOUT
) {
263 kunit_err(test
, "test case timed out\n");
265 * Unknown internal error occurred preventing test case from
266 * running, so there is nothing to clean up.
269 kunit_err(test
, "internal error occurred preventing test case from running: %d\n",
276 * Test case was run, but aborted. It is the test case's business as to
277 * whether it failed or not, we just need to clean up.
279 kunit_run_case_cleanup(test
, suite
);
283 * Performs all logic to run a test case. It also catches most errors that
284 * occur in a test case and reports them as failures.
286 static void kunit_run_case_catch_errors(struct kunit_suite
*suite
,
287 struct kunit_case
*test_case
)
289 struct kunit_try_catch_context context
;
290 struct kunit_try_catch
*try_catch
;
293 kunit_init_test(&test
, test_case
->name
);
294 try_catch
= &test
.try_catch
;
296 kunit_try_catch_init(try_catch
,
299 kunit_catch_run_case
);
300 context
.test
= &test
;
301 context
.suite
= suite
;
302 context
.test_case
= test_case
;
303 kunit_try_catch_run(try_catch
, &context
);
305 test_case
->success
= test
.success
;
308 int kunit_run_tests(struct kunit_suite
*suite
)
310 struct kunit_case
*test_case
;
311 size_t test_case_count
= 1;
313 kunit_print_subtest_start(suite
);
315 for (test_case
= suite
->test_cases
; test_case
->run_case
; test_case
++) {
316 kunit_run_case_catch_errors(suite
, test_case
);
317 kunit_print_test_case_ok_not_ok(test_case
, test_case_count
++);
320 kunit_print_subtest_end(suite
);
324 EXPORT_SYMBOL_GPL(kunit_run_tests
);
326 struct kunit_resource
*kunit_alloc_and_get_resource(struct kunit
*test
,
327 kunit_resource_init_t init
,
328 kunit_resource_free_t free
,
332 struct kunit_resource
*res
;
335 res
= kzalloc(sizeof(*res
), internal_gfp
);
339 ret
= init(res
, context
);
344 spin_lock(&test
->lock
);
345 list_add_tail(&res
->node
, &test
->resources
);
346 spin_unlock(&test
->lock
);
350 EXPORT_SYMBOL_GPL(kunit_alloc_and_get_resource
);
352 static void kunit_resource_free(struct kunit
*test
, struct kunit_resource
*res
)
358 static struct kunit_resource
*kunit_resource_find(struct kunit
*test
,
359 kunit_resource_match_t match
,
360 kunit_resource_free_t free
,
363 struct kunit_resource
*resource
;
365 lockdep_assert_held(&test
->lock
);
367 list_for_each_entry_reverse(resource
, &test
->resources
, node
) {
368 if (resource
->free
!= free
)
370 if (match(test
, resource
->allocation
, match_data
))
377 static struct kunit_resource
*kunit_resource_remove(
379 kunit_resource_match_t match
,
380 kunit_resource_free_t free
,
383 struct kunit_resource
*resource
;
385 spin_lock(&test
->lock
);
386 resource
= kunit_resource_find(test
, match
, free
, match_data
);
388 list_del(&resource
->node
);
389 spin_unlock(&test
->lock
);
394 int kunit_resource_destroy(struct kunit
*test
,
395 kunit_resource_match_t match
,
396 kunit_resource_free_t free
,
399 struct kunit_resource
*resource
;
401 resource
= kunit_resource_remove(test
, match
, free
, match_data
);
406 kunit_resource_free(test
, resource
);
409 EXPORT_SYMBOL_GPL(kunit_resource_destroy
);
411 struct kunit_kmalloc_params
{
416 static int kunit_kmalloc_init(struct kunit_resource
*res
, void *context
)
418 struct kunit_kmalloc_params
*params
= context
;
420 res
->allocation
= kmalloc(params
->size
, params
->gfp
);
421 if (!res
->allocation
)
427 static void kunit_kmalloc_free(struct kunit_resource
*res
)
429 kfree(res
->allocation
);
432 void *kunit_kmalloc(struct kunit
*test
, size_t size
, gfp_t gfp
)
434 struct kunit_kmalloc_params params
= {
439 return kunit_alloc_resource(test
,
445 EXPORT_SYMBOL_GPL(kunit_kmalloc
);
447 void kunit_kfree(struct kunit
*test
, const void *ptr
)
451 rc
= kunit_resource_destroy(test
,
452 kunit_resource_instance_match
,
458 EXPORT_SYMBOL_GPL(kunit_kfree
);
460 void kunit_cleanup(struct kunit
*test
)
462 struct kunit_resource
*resource
;
465 * test->resources is a stack - each allocation must be freed in the
466 * reverse order from which it was added since one resource may depend
467 * on another for its entire lifetime.
468 * Also, we cannot use the normal list_for_each constructs, even the
469 * safe ones because *arbitrary* nodes may be deleted when
470 * kunit_resource_free is called; the list_for_each_safe variants only
471 * protect against the current node being deleted, not the next.
474 spin_lock(&test
->lock
);
475 if (list_empty(&test
->resources
)) {
476 spin_unlock(&test
->lock
);
479 resource
= list_last_entry(&test
->resources
,
480 struct kunit_resource
,
482 list_del(&resource
->node
);
483 spin_unlock(&test
->lock
);
485 kunit_resource_free(test
, resource
);
488 EXPORT_SYMBOL_GPL(kunit_cleanup
);
490 static int __init
kunit_init(void)
494 late_initcall(kunit_init
);
496 static void __exit
kunit_exit(void)
499 module_exit(kunit_exit
);
501 MODULE_LICENSE("GPL v2");