|
7 | 7 | */
|
8 | 8 |
|
9 | 9 | #include <kunit/test.h>
|
| 10 | +#include <kunit/test-bug.h> |
10 | 11 | #include <linux/kernel.h>
|
11 | 12 | #include <linux/kref.h>
|
12 | 13 | #include <linux/sched/debug.h>
|
|
16 | 17 | #include "string-stream.h"
|
17 | 18 | #include "try-catch-impl.h"
|
18 | 19 |
|
| 20 | +#if IS_BUILTIN(CONFIG_KUNIT) |
| 21 | +/* |
| 22 | + * Fail the current test and print an error message to the log. |
| 23 | + */ |
| 24 | +void __kunit_fail_current_test(const char *file, int line, const char *fmt, ...) |
| 25 | +{ |
| 26 | + va_list args; |
| 27 | + int len; |
| 28 | + char *buffer; |
| 29 | + |
| 30 | + if (!current->kunit_test) |
| 31 | + return; |
| 32 | + |
| 33 | + kunit_set_failure(current->kunit_test); |
| 34 | + |
| 35 | + /* kunit_err() only accepts literals, so evaluate the args first. */ |
| 36 | + va_start(args, fmt); |
| 37 | + len = vsnprintf(NULL, 0, fmt, args) + 1; |
| 38 | + va_end(args); |
| 39 | + |
| 40 | + buffer = kunit_kmalloc(current->kunit_test, len, GFP_KERNEL); |
| 41 | + if (!buffer) |
| 42 | + return; |
| 43 | + |
| 44 | + va_start(args, fmt); |
| 45 | + vsnprintf(buffer, len, fmt, args); |
| 46 | + va_end(args); |
| 47 | + |
| 48 | + kunit_err(current->kunit_test, "%s:%d: %s", file, line, buffer); |
| 49 | + kunit_kfree(current->kunit_test, buffer); |
| 50 | +} |
| 51 | +EXPORT_SYMBOL_GPL(__kunit_fail_current_test); |
| 52 | +#endif |
| 53 | + |
19 | 54 | /*
|
20 | 55 | * Append formatted message to log, size of which is limited to
|
21 | 56 | * KUNIT_LOG_SIZE bytes (including null terminating byte).
|
@@ -273,9 +308,7 @@ static void kunit_try_run_case(void *data)
|
273 | 308 | struct kunit_suite *suite = ctx->suite;
|
274 | 309 | struct kunit_case *test_case = ctx->test_case;
|
275 | 310 |
|
276 |
| -#if (IS_ENABLED(CONFIG_KASAN) && IS_ENABLED(CONFIG_KUNIT)) |
277 | 311 | current->kunit_test = test;
|
278 |
| -#endif /* IS_ENABLED(CONFIG_KASAN) && IS_ENABLED(CONFIG_KUNIT) */ |
279 | 312 |
|
280 | 313 | /*
|
281 | 314 | * kunit_run_case_internal may encounter a fatal error; if it does,
|
@@ -624,9 +657,7 @@ void kunit_cleanup(struct kunit *test)
|
624 | 657 | spin_unlock(&test->lock);
|
625 | 658 | kunit_remove_resource(test, res);
|
626 | 659 | }
|
627 |
| -#if (IS_ENABLED(CONFIG_KASAN) && IS_ENABLED(CONFIG_KUNIT)) |
628 | 660 | current->kunit_test = NULL;
|
629 |
| -#endif /* IS_ENABLED(CONFIG_KASAN) && IS_ENABLED(CONFIG_KUNIT)*/ |
630 | 661 | }
|
631 | 662 | EXPORT_SYMBOL_GPL(kunit_cleanup);
|
632 | 663 |
|
|
0 commit comments