64
64
#include <sys/types.h>
65
65
#include <sys/wait.h>
66
66
#include <unistd.h>
67
+ #include <setjmp.h>
67
68
68
69
#include "kselftest.h"
69
70
183
184
struct __test_metadata *_metadata, \
184
185
struct __fixture_variant_metadata *variant) \
185
186
{ \
186
- test_name(_metadata); \
187
+ _metadata->setup_completed = true; \
188
+ if (setjmp(_metadata->env) == 0) \
189
+ test_name(_metadata); \
190
+ __test_check_assert(_metadata); \
187
191
} \
188
192
static struct __test_metadata _##test_name##_object = \
189
193
{ .name = #test_name, \
287
291
#define FIXTURE_TEARDOWN (fixture_name ) \
288
292
void fixture_name##_teardown( \
289
293
struct __test_metadata __attribute__((unused)) *_metadata, \
290
- FIXTURE_DATA(fixture_name) __attribute__((unused)) *self)
294
+ FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
295
+ const FIXTURE_VARIANT(fixture_name) \
296
+ __attribute__((unused)) *variant)
291
297
292
298
/**
293
299
* FIXTURE_VARIANT() - Optionally called once per fixture
302
308
* ...
303
309
* };
304
310
*
305
- * Defines type of constant parameters provided to FIXTURE_SETUP() and TEST_F()
306
- * as *variant*. Variants allow the same tests to be run with different
307
- * arguments.
311
+ * Defines type of constant parameters provided to FIXTURE_SETUP(), TEST_F() and
312
+ * FIXTURE_TEARDOWN as *variant*. Variants allow the same tests to be run with
313
+ * different arguments.
308
314
*/
309
315
#define FIXTURE_VARIANT (fixture_name ) struct _fixture_variant_##fixture_name
310
316
356
362
* Defines a test that depends on a fixture (e.g., is part of a test case).
357
363
* Very similar to TEST() except that *self* is the setup instance of fixture's
358
364
* datatype exposed for use by the implementation.
359
- *
360
- * Warning: use of ASSERT_* here will skip TEARDOWN.
361
365
*/
362
- /* TODO(wad) register fixtures on dedicated test lists. */
363
366
#define TEST_F (fixture_name , test_name ) \
364
367
__TEST_F_IMPL(fixture_name, test_name, -1, TEST_TIMEOUT_DEFAULT)
365
368
381
384
/* fixture data is alloced, setup, and torn down per call. */ \
382
385
FIXTURE_DATA (fixture_name ) self ; \
383
386
memset (& self , 0 , sizeof (FIXTURE_DATA (fixture_name ))); \
384
- fixture_name ##_setup (_metadata , & self , variant -> data ); \
385
- /* Let setup failure terminate early. */ \
386
- if (!_metadata -> passed ) \
387
- return ; \
388
- fixture_name ##_ ##test_name (_metadata, &self, variant->data); \
389
- fixture_name##_teardown(_metadata, &self); \
387
+ if (setjmp (_metadata -> env ) == 0 ) { \
388
+ fixture_name ##_setup (_metadata , & self , variant -> data ); \
389
+ /* Let setup failure terminate early. */ \
390
+ if (!_metadata -> passed ) \
391
+ return ; \
392
+ _metadata -> setup_completed = true; \
393
+ fixture_name ##_ ##test_name (_metadata, &self, variant->data); \
394
+ } \
395
+ if (_metadata->setup_completed) \
396
+ fixture_name##_teardown(_metadata, &self, variant->data); \
397
+ __test_check_assert(_metadata); \
390
398
} \
391
399
static struct __test_metadata \
392
400
_##fixture_name##_##test_name##_object = { \
683
691
*/
684
692
#define OPTIONAL_HANDLER (_assert ) \
685
693
for (; _metadata->trigger; _metadata->trigger = \
686
- __bail(_assert, _metadata->no_print, _metadata->step ))
694
+ __bail(_assert, _metadata))
687
695
688
696
#define __INC_STEP (_metadata ) \
689
697
/* Keep "step" below 255 (which is used for "SKIP" reporting). */ \
@@ -830,6 +838,9 @@ struct __test_metadata {
830
838
bool timed_out ; /* did this test timeout instead of exiting? */
831
839
__u8 step ;
832
840
bool no_print ; /* manual trigger when TH_LOG_STREAM is not available */
841
+ bool aborted ; /* stopped test due to failed ASSERT */
842
+ bool setup_completed ; /* did setup finish? */
843
+ jmp_buf env ; /* for exiting out of test early */
833
844
struct __test_results * results ;
834
845
struct __test_metadata * prev , * next ;
835
846
};
@@ -848,16 +859,26 @@ static inline void __register_test(struct __test_metadata *t)
848
859
__LIST_APPEND (t -> fixture -> tests , t );
849
860
}
850
861
851
- static inline int __bail (int for_realz , bool no_print , __u8 step )
862
+ static inline int __bail (int for_realz , struct __test_metadata * t )
852
863
{
864
+ /* if this is ASSERT, return immediately. */
853
865
if (for_realz ) {
854
- if (no_print )
855
- _exit (step );
856
- abort ();
866
+ t -> aborted = true;
867
+ longjmp (t -> env , 1 );
857
868
}
869
+ /* otherwise, end the for loop and continue. */
858
870
return 0 ;
859
871
}
860
872
873
+ static inline void __test_check_assert (struct __test_metadata * t )
874
+ {
875
+ if (t -> aborted ) {
876
+ if (t -> no_print )
877
+ _exit (t -> step );
878
+ abort ();
879
+ }
880
+ }
881
+
861
882
struct __test_metadata * __active_test ;
862
883
static void __timeout_handler (int sig , siginfo_t * info , void * ucontext )
863
884
{
0 commit comments