xref: /linux-6.15/include/kunit/try-catch.h (revision 3a35c130)
1*5f3e0620SBrendan Higgins /* SPDX-License-Identifier: GPL-2.0 */
2*5f3e0620SBrendan Higgins /*
3*5f3e0620SBrendan Higgins  * An API to allow a function, that may fail, to be executed, and recover in a
4*5f3e0620SBrendan Higgins  * controlled manner.
5*5f3e0620SBrendan Higgins  *
6*5f3e0620SBrendan Higgins  * Copyright (C) 2019, Google LLC.
7*5f3e0620SBrendan Higgins  * Author: Brendan Higgins <[email protected]>
8*5f3e0620SBrendan Higgins  */
9*5f3e0620SBrendan Higgins 
10*5f3e0620SBrendan Higgins #ifndef _KUNIT_TRY_CATCH_H
11*5f3e0620SBrendan Higgins #define _KUNIT_TRY_CATCH_H
12*5f3e0620SBrendan Higgins 
13*5f3e0620SBrendan Higgins #include <linux/types.h>
14*5f3e0620SBrendan Higgins 
15*5f3e0620SBrendan Higgins typedef void (*kunit_try_catch_func_t)(void *);
16*5f3e0620SBrendan Higgins 
17*5f3e0620SBrendan Higgins struct kunit;
18*5f3e0620SBrendan Higgins 
19*5f3e0620SBrendan Higgins /**
20*5f3e0620SBrendan Higgins  * struct kunit_try_catch - provides a generic way to run code which might fail.
21*5f3e0620SBrendan Higgins  * @test: The test case that is currently being executed.
22*5f3e0620SBrendan Higgins  * @try_result: Contains any errno obtained while running test case.
23*5f3e0620SBrendan Higgins  * @try: The function, the test case, to attempt to run.
24*5f3e0620SBrendan Higgins  * @catch: The function called if @try bails out.
25*5f3e0620SBrendan Higgins  * @context: used to pass user data to the try and catch functions.
26*5f3e0620SBrendan Higgins  *
27*5f3e0620SBrendan Higgins  * kunit_try_catch provides a generic, architecture independent way to execute
28*5f3e0620SBrendan Higgins  * an arbitrary function of type kunit_try_catch_func_t which may bail out by
29*5f3e0620SBrendan Higgins  * calling kunit_try_catch_throw(). If kunit_try_catch_throw() is called, @try
30*5f3e0620SBrendan Higgins  * is stopped at the site of invocation and @catch is called.
31*5f3e0620SBrendan Higgins  *
32*5f3e0620SBrendan Higgins  * struct kunit_try_catch provides a generic interface for the functionality
33*5f3e0620SBrendan Higgins  * needed to implement kunit->abort() which in turn is needed for implementing
34*5f3e0620SBrendan Higgins  * assertions. Assertions allow stating a precondition for a test simplifying
35*5f3e0620SBrendan Higgins  * how test cases are written and presented.
36*5f3e0620SBrendan Higgins  *
37*5f3e0620SBrendan Higgins  * Assertions are like expectations, except they abort (call
38*5f3e0620SBrendan Higgins  * kunit_try_catch_throw()) when the specified condition is not met. This is
39*5f3e0620SBrendan Higgins  * useful when you look at a test case as a logical statement about some piece
40*5f3e0620SBrendan Higgins  * of code, where assertions are the premises for the test case, and the
41*5f3e0620SBrendan Higgins  * conclusion is a set of predicates, rather expectations, that must all be
42*5f3e0620SBrendan Higgins  * true. If your premises are violated, it does not makes sense to continue.
43*5f3e0620SBrendan Higgins  */
44*5f3e0620SBrendan Higgins struct kunit_try_catch {
45*5f3e0620SBrendan Higgins 	/* private: internal use only. */
46*5f3e0620SBrendan Higgins 	struct kunit *test;
47*5f3e0620SBrendan Higgins 	int try_result;
48*5f3e0620SBrendan Higgins 	kunit_try_catch_func_t try;
49*5f3e0620SBrendan Higgins 	kunit_try_catch_func_t catch;
50*5f3e0620SBrendan Higgins 	void *context;
51*5f3e0620SBrendan Higgins };
52*5f3e0620SBrendan Higgins 
53*5f3e0620SBrendan Higgins void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context);
54*5f3e0620SBrendan Higgins 
55*5f3e0620SBrendan Higgins void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch);
56*5f3e0620SBrendan Higgins 
kunit_try_catch_get_result(struct kunit_try_catch * try_catch)57*5f3e0620SBrendan Higgins static inline int kunit_try_catch_get_result(struct kunit_try_catch *try_catch)
58*5f3e0620SBrendan Higgins {
59*5f3e0620SBrendan Higgins 	return try_catch->try_result;
60*5f3e0620SBrendan Higgins }
61*5f3e0620SBrendan Higgins 
62*5f3e0620SBrendan Higgins #endif /* _KUNIT_TRY_CATCH_H */
63