77 */
88
99#include <kunit/test.h>
10+ #include <kunit/try-catch.h>
1011#include <linux/kernel.h>
12+ #include <linux/sched/debug.h>
1113
1214static void kunit_set_failure (struct kunit * test )
1315{
@@ -162,6 +164,19 @@ static void kunit_fail(struct kunit *test, struct kunit_assert *assert)
162164 WARN_ON (string_stream_destroy (stream ));
163165}
164166
167+ static void __noreturn kunit_abort (struct kunit * test )
168+ {
169+ kunit_try_catch_throw (& test -> try_catch ); /* Does not return. */
170+
171+ /*
172+ * Throw could not abort from test.
173+ *
174+ * XXX: we should never reach this line! As kunit_try_catch_throw is
175+ * marked __noreturn.
176+ */
177+ WARN_ONCE (true, "Throw could not abort from test!\n" );
178+ }
179+
165180void kunit_do_assertion (struct kunit * test ,
166181 struct kunit_assert * assert ,
167182 bool pass ,
@@ -180,6 +195,9 @@ void kunit_do_assertion(struct kunit *test,
180195 kunit_fail (test , assert );
181196
182197 va_end (args );
198+
199+ if (assert -> type == KUNIT_ASSERTION )
200+ kunit_abort (test );
183201}
184202
185203void kunit_init_test (struct kunit * test , const char * name )
@@ -191,33 +209,122 @@ void kunit_init_test(struct kunit *test, const char *name)
191209}
192210
193211/*
194- * Performs all logic to run a test case .
212+ * Initializes and runs test case. Does not clean up or do post validations .
195213 */
196- static void kunit_run_case (struct kunit_suite * suite ,
197- struct kunit_case * test_case )
214+ static void kunit_run_case_internal (struct kunit * test ,
215+ struct kunit_suite * suite ,
216+ struct kunit_case * test_case )
198217{
199- struct kunit test ;
200-
201- kunit_init_test (& test , test_case -> name );
202-
203218 if (suite -> init ) {
204219 int ret ;
205220
206- ret = suite -> init (& test );
221+ ret = suite -> init (test );
207222 if (ret ) {
208- kunit_err (& test , "failed to initialize: %d\n" , ret );
209- kunit_set_failure (& test );
210- test_case -> success = test .success ;
223+ kunit_err (test , "failed to initialize: %d\n" , ret );
224+ kunit_set_failure (test );
211225 return ;
212226 }
213227 }
214228
215- test_case -> run_case (& test );
229+ test_case -> run_case (test );
230+ }
216231
232+ static void kunit_case_internal_cleanup (struct kunit * test )
233+ {
234+ kunit_cleanup (test );
235+ }
236+
237+ /*
238+ * Performs post validations and cleanup after a test case was run.
239+ * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
240+ */
241+ static void kunit_run_case_cleanup (struct kunit * test ,
242+ struct kunit_suite * suite )
243+ {
217244 if (suite -> exit )
218- suite -> exit (& test );
245+ suite -> exit (test );
246+
247+ kunit_case_internal_cleanup (test );
248+ }
249+
250+ struct kunit_try_catch_context {
251+ struct kunit * test ;
252+ struct kunit_suite * suite ;
253+ struct kunit_case * test_case ;
254+ };
255+
256+ static void kunit_try_run_case (void * data )
257+ {
258+ struct kunit_try_catch_context * ctx = data ;
259+ struct kunit * test = ctx -> test ;
260+ struct kunit_suite * suite = ctx -> suite ;
261+ struct kunit_case * test_case = ctx -> test_case ;
262+
263+ /*
264+ * kunit_run_case_internal may encounter a fatal error; if it does,
265+ * abort will be called, this thread will exit, and finally the parent
266+ * thread will resume control and handle any necessary clean up.
267+ */
268+ kunit_run_case_internal (test , suite , test_case );
269+ /* This line may never be reached. */
270+ kunit_run_case_cleanup (test , suite );
271+ }
272+
273+ static void kunit_catch_run_case (void * data )
274+ {
275+ struct kunit_try_catch_context * ctx = data ;
276+ struct kunit * test = ctx -> test ;
277+ struct kunit_suite * suite = ctx -> suite ;
278+ int try_exit_code = kunit_try_catch_get_result (& test -> try_catch );
279+
280+ if (try_exit_code ) {
281+ kunit_set_failure (test );
282+ /*
283+ * Test case could not finish, we have no idea what state it is
284+ * in, so don't do clean up.
285+ */
286+ if (try_exit_code == - ETIMEDOUT ) {
287+ kunit_err (test , "test case timed out\n" );
288+ /*
289+ * Unknown internal error occurred preventing test case from
290+ * running, so there is nothing to clean up.
291+ */
292+ } else {
293+ kunit_err (test , "internal error occurred preventing test case from running: %d\n" ,
294+ try_exit_code );
295+ }
296+ return ;
297+ }
298+
299+ /*
300+ * Test case was run, but aborted. It is the test case's business as to
301+ * whether it failed or not, we just need to clean up.
302+ */
303+ kunit_run_case_cleanup (test , suite );
304+ }
305+
306+ /*
307+ * Performs all logic to run a test case. It also catches most errors that
308+ * occur in a test case and reports them as failures.
309+ */
310+ static void kunit_run_case_catch_errors (struct kunit_suite * suite ,
311+ struct kunit_case * test_case )
312+ {
313+ struct kunit_try_catch_context context ;
314+ struct kunit_try_catch * try_catch ;
315+ struct kunit test ;
316+
317+ kunit_init_test (& test , test_case -> name );
318+ try_catch = & test .try_catch ;
219319
220- kunit_cleanup (& test );
320+ kunit_try_catch_init (try_catch ,
321+ & test ,
322+ kunit_try_run_case ,
323+ kunit_catch_run_case );
324+ context .test = & test ;
325+ context .suite = suite ;
326+ context .test_case = test_case ;
327+ kunit_try_catch_run (try_catch , & context );
221328
222329 test_case -> success = test .success ;
223330}
@@ -230,7 +337,7 @@ int kunit_run_tests(struct kunit_suite *suite)
230337 kunit_print_subtest_start (suite );
231338
232339 for (test_case = suite -> test_cases ; test_case -> run_case ; test_case ++ ) {
233- kunit_run_case (suite , test_case );
340+ kunit_run_case_catch_errors (suite , test_case );
234341 kunit_print_test_case_ok_not_ok (test_case , test_case_count ++ );
235342 }
236343
0 commit comments