xref: /linux-6.15/include/kunit/test-bug.h (revision df3cb7ac)
1359a3760SUriel Guajardo /* SPDX-License-Identifier: GPL-2.0 */
2359a3760SUriel Guajardo /*
37170b7edSDavid Gow  * KUnit API providing hooks for non-test code to interact with tests.
4359a3760SUriel Guajardo  *
5359a3760SUriel Guajardo  * Copyright (C) 2020, Google LLC.
6359a3760SUriel Guajardo  * Author: Uriel Guajardo <[email protected]>
7359a3760SUriel Guajardo  */
8359a3760SUriel Guajardo 
9359a3760SUriel Guajardo #ifndef _KUNIT_TEST_BUG_H
10359a3760SUriel Guajardo #define _KUNIT_TEST_BUG_H
11359a3760SUriel Guajardo 
12*df3cb7acSMiguel Ojeda #include <linux/stddef.h> /* for NULL */
13*df3cb7acSMiguel Ojeda 
147170b7edSDavid Gow #if IS_ENABLED(CONFIG_KUNIT)
15359a3760SUriel Guajardo 
1691e93592SDavid Gow #include <linux/jump_label.h> /* For static branch */
1791e93592SDavid Gow #include <linux/sched.h>
1891e93592SDavid Gow 
1991e93592SDavid Gow /* Static key if KUnit is running any tests. */
2091e93592SDavid Gow DECLARE_STATIC_KEY_FALSE(kunit_running);
2191e93592SDavid Gow 
227170b7edSDavid Gow /* Hooks table: a table of function pointers filled in when kunit loads */
237170b7edSDavid Gow extern struct kunit_hooks_table {
247170b7edSDavid Gow 	__printf(3, 4) void (*fail_current_test)(const char*, int, const char*, ...);
25e047c5eaSDavid Gow 	void *(*get_static_stub_address)(struct kunit *test, void *real_fn_addr);
267170b7edSDavid Gow } kunit_hooks;
277170b7edSDavid Gow 
2891e93592SDavid Gow /**
2991e93592SDavid Gow  * kunit_get_current_test() - Return a pointer to the currently running
3091e93592SDavid Gow  *			      KUnit test.
3191e93592SDavid Gow  *
3291e93592SDavid Gow  * If a KUnit test is running in the current task, returns a pointer to its
3391e93592SDavid Gow  * associated struct kunit. This pointer can then be passed to any KUnit
3491e93592SDavid Gow  * function or assertion. If no test is running (or a test is running in a
3591e93592SDavid Gow  * different task), returns NULL.
3691e93592SDavid Gow  *
3791e93592SDavid Gow  * This function is safe to call even when KUnit is disabled. If CONFIG_KUNIT
3891e93592SDavid Gow  * is not enabled, it will compile down to nothing and will return quickly no
3991e93592SDavid Gow  * test is running.
4091e93592SDavid Gow  */
kunit_get_current_test(void)4191e93592SDavid Gow static inline struct kunit *kunit_get_current_test(void)
4291e93592SDavid Gow {
4391e93592SDavid Gow 	if (!static_branch_unlikely(&kunit_running))
4491e93592SDavid Gow 		return NULL;
4591e93592SDavid Gow 
4691e93592SDavid Gow 	return current->kunit_test;
4791e93592SDavid Gow }
4891e93592SDavid Gow 
4991e93592SDavid Gow 
5091e93592SDavid Gow /**
5191e93592SDavid Gow  * kunit_fail_current_test() - If a KUnit test is running, fail it.
5291e93592SDavid Gow  *
5391e93592SDavid Gow  * If a KUnit test is running in the current task, mark that test as failed.
5491e93592SDavid Gow  */
5591e93592SDavid Gow #define kunit_fail_current_test(fmt, ...) do {					\
5691e93592SDavid Gow 		if (static_branch_unlikely(&kunit_running)) {			\
577170b7edSDavid Gow 			/* Guaranteed to be non-NULL when kunit_running true*/	\
587170b7edSDavid Gow 			kunit_hooks.fail_current_test(__FILE__, __LINE__,	\
5991e93592SDavid Gow 						  fmt, ##__VA_ARGS__);		\
6091e93592SDavid Gow 		}								\
6191e93592SDavid Gow 	} while (0)
6291e93592SDavid Gow 
63359a3760SUriel Guajardo #else
64359a3760SUriel Guajardo 
kunit_get_current_test(void)6591e93592SDavid Gow static inline struct kunit *kunit_get_current_test(void) { return NULL; }
6691e93592SDavid Gow 
677170b7edSDavid Gow #define kunit_fail_current_test(fmt, ...) do {} while (0)
68359a3760SUriel Guajardo 
69359a3760SUriel Guajardo #endif
70359a3760SUriel Guajardo 
71359a3760SUriel Guajardo #endif /* _KUNIT_TEST_BUG_H */
72