18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * An API to allow a function, that may fail, to be executed, and recover in a
48c2ecf20Sopenharmony_ci * controlled manner.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2019, Google LLC.
78c2ecf20Sopenharmony_ci * Author: Brendan Higgins <brendanhiggins@google.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#ifndef _KUNIT_TRY_CATCH_H
118c2ecf20Sopenharmony_ci#define _KUNIT_TRY_CATCH_H
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/types.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_citypedef void (*kunit_try_catch_func_t)(void *);
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistruct completion;
188c2ecf20Sopenharmony_cistruct kunit;
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/**
218c2ecf20Sopenharmony_ci * struct kunit_try_catch - provides a generic way to run code which might fail.
228c2ecf20Sopenharmony_ci * @test: The test case that is currently being executed.
238c2ecf20Sopenharmony_ci * @try_completion: Completion that the control thread waits on while test runs.
248c2ecf20Sopenharmony_ci * @try_result: Contains any errno obtained while running test case.
258c2ecf20Sopenharmony_ci * @try: The function, the test case, to attempt to run.
268c2ecf20Sopenharmony_ci * @catch: The function called if @try bails out.
278c2ecf20Sopenharmony_ci * @context: used to pass user data to the try and catch functions.
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci * kunit_try_catch provides a generic, architecture independent way to execute
308c2ecf20Sopenharmony_ci * an arbitrary function of type kunit_try_catch_func_t which may bail out by
318c2ecf20Sopenharmony_ci * calling kunit_try_catch_throw(). If kunit_try_catch_throw() is called, @try
328c2ecf20Sopenharmony_ci * is stopped at the site of invocation and @catch is called.
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * struct kunit_try_catch provides a generic interface for the functionality
358c2ecf20Sopenharmony_ci * needed to implement kunit->abort() which in turn is needed for implementing
368c2ecf20Sopenharmony_ci * assertions. Assertions allow stating a precondition for a test simplifying
378c2ecf20Sopenharmony_ci * how test cases are written and presented.
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci * Assertions are like expectations, except they abort (call
408c2ecf20Sopenharmony_ci * kunit_try_catch_throw()) when the specified condition is not met. This is
418c2ecf20Sopenharmony_ci * useful when you look at a test case as a logical statement about some piece
428c2ecf20Sopenharmony_ci * of code, where assertions are the premises for the test case, and the
438c2ecf20Sopenharmony_ci * conclusion is a set of predicates, rather expectations, that must all be
448c2ecf20Sopenharmony_ci * true. If your premises are violated, it does not makes sense to continue.
458c2ecf20Sopenharmony_ci */
468c2ecf20Sopenharmony_cistruct kunit_try_catch {
478c2ecf20Sopenharmony_ci	/* private: internal use only. */
488c2ecf20Sopenharmony_ci	struct kunit *test;
498c2ecf20Sopenharmony_ci	struct completion *try_completion;
508c2ecf20Sopenharmony_ci	int try_result;
518c2ecf20Sopenharmony_ci	kunit_try_catch_func_t try;
528c2ecf20Sopenharmony_ci	kunit_try_catch_func_t catch;
538c2ecf20Sopenharmony_ci	void *context;
548c2ecf20Sopenharmony_ci};
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_civoid kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_civoid __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic inline int kunit_try_catch_get_result(struct kunit_try_catch *try_catch)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	return try_catch->try_result;
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#endif /* _KUNIT_TRY_CATCH_H */
66