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