xref: /linux/tools/testing/selftests/kselftest_harness.h (revision cbc7afffc5ec581d3781c49fe9c8e8c661e5217b)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
4  *
5  * kselftest_harness.h: simple C unit test helper.
6  *
7  * See documentation in Documentation/dev-tools/kselftest.rst
8  *
9  * API inspired by code.google.com/p/googletest
10  */
11 
12 /**
13  * DOC: example
14  *
15  * .. code-block:: c
16  *
17  *    #include "../kselftest_harness.h"
18  *
19  *    TEST(standalone_test) {
20  *      do_some_stuff;
21  *      EXPECT_GT(10, stuff) {
22  *         stuff_state_t state;
23  *         enumerate_stuff_state(&state);
24  *         TH_LOG("expectation failed with state: %s", state.msg);
25  *      }
26  *      more_stuff;
27  *      ASSERT_NE(some_stuff, NULL) TH_LOG("how did it happen?!");
28  *      last_stuff;
29  *      EXPECT_EQ(0, last_stuff);
30  *    }
31  *
32  *    FIXTURE(my_fixture) {
33  *      mytype_t *data;
34  *      int awesomeness_level;
35  *    };
36  *    FIXTURE_SETUP(my_fixture) {
37  *      self->data = mytype_new();
38  *      ASSERT_NE(NULL, self->data);
39  *    }
40  *    FIXTURE_TEARDOWN(my_fixture) {
41  *      mytype_free(self->data);
42  *    }
43  *    TEST_F(my_fixture, data_is_good) {
44  *      EXPECT_EQ(1, is_my_data_good(self->data));
45  *    }
46  *
47  *    TEST_HARNESS_MAIN
48  */
49 
50 #ifndef __KSELFTEST_HARNESS_H
51 #define __KSELFTEST_HARNESS_H
52 
53 #ifndef _GNU_SOURCE
54 #define _GNU_SOURCE
55 #endif
56 #include <asm/types.h>
57 #include <ctype.h>
58 #include <errno.h>
59 #include <stdbool.h>
60 #include <stdint.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <sys/mman.h>
65 #include <sys/types.h>
66 #include <sys/wait.h>
67 #include <unistd.h>
68 #include <setjmp.h>
69 
70 #include "kselftest.h"
71 
72 #define TEST_TIMEOUT_DEFAULT 30
73 
74 /* Utilities exposed to the test definitions */
75 #ifndef TH_LOG_STREAM
76 #  define TH_LOG_STREAM stderr
77 #endif
78 
79 #ifndef TH_LOG_ENABLED
80 #  define TH_LOG_ENABLED 1
81 #endif
82 
83 /**
84  * TH_LOG()
85  *
86  * @fmt: format string
87  * @...: optional arguments
88  *
89  * .. code-block:: c
90  *
91  *     TH_LOG(format, ...)
92  *
93  * Optional debug logging function available for use in tests.
94  * Logging may be enabled or disabled by defining TH_LOG_ENABLED.
95  * E.g., #define TH_LOG_ENABLED 1
96  *
97  * If no definition is provided, logging is enabled by default.
98  */
99 #define TH_LOG(fmt, ...) do { \
100 	if (TH_LOG_ENABLED) \
101 		__TH_LOG(fmt, ##__VA_ARGS__); \
102 } while (0)
103 
104 /* Unconditional logger for internal use. */
105 #define __TH_LOG(fmt, ...) \
106 		fprintf(TH_LOG_STREAM, "# %s:%d:%s:" fmt "\n", \
107 			__FILE__, __LINE__, _metadata->name, ##__VA_ARGS__)
108 
109 /**
110  * SKIP()
111  *
112  * @statement: statement to run after reporting SKIP
113  * @fmt: format string
114  * @...: optional arguments
115  *
116  * .. code-block:: c
117  *
118  *     SKIP(statement, fmt, ...);
119  *
120  * This forces a "pass" after reporting why something is being skipped
121  * and runs "statement", which is usually "return" or "goto skip".
122  */
123 #define SKIP(statement, fmt, ...) do { \
124 	snprintf(_metadata->results->reason, \
125 		 sizeof(_metadata->results->reason), fmt, ##__VA_ARGS__); \
126 	if (TH_LOG_ENABLED) { \
127 		fprintf(TH_LOG_STREAM, "#      SKIP      %s\n", \
128 			_metadata->results->reason); \
129 	} \
130 	_metadata->exit_code = KSFT_SKIP; \
131 	_metadata->trigger = 0; \
132 	statement; \
133 } while (0)
134 
135 /**
136  * TEST() - Defines the test function and creates the registration
137  * stub
138  *
139  * @test_name: test name
140  *
141  * .. code-block:: c
142  *
143  *     TEST(name) { implementation }
144  *
145  * Defines a test by name.
146  * Names must be unique and tests must not be run in parallel.  The
147  * implementation containing block is a function and scoping should be treated
148  * as such.  Returning early may be performed with a bare "return;" statement.
149  *
150  * EXPECT_* and ASSERT_* are valid in a TEST() { } context.
151  */
152 #define TEST(test_name) __TEST_IMPL(test_name, -1)
153 
154 /**
155  * TEST_SIGNAL()
156  *
157  * @test_name: test name
158  * @signal: signal number
159  *
160  * .. code-block:: c
161  *
162  *     TEST_SIGNAL(name, signal) { implementation }
163  *
164  * Defines a test by name and the expected term signal.
165  * Names must be unique and tests must not be run in parallel.  The
166  * implementation containing block is a function and scoping should be treated
167  * as such.  Returning early may be performed with a bare "return;" statement.
168  *
169  * EXPECT_* and ASSERT_* are valid in a TEST() { } context.
170  */
171 #define TEST_SIGNAL(test_name, signal) __TEST_IMPL(test_name, signal)
172 
173 #define __TEST_IMPL(test_name, _signal) \
174 	static void test_name(struct __test_metadata *_metadata); \
175 	static inline void wrapper_##test_name( \
176 		struct __test_metadata *_metadata, \
177 		struct __fixture_variant_metadata *variant) \
178 	{ \
179 		_metadata->setup_completed = true; \
180 		if (setjmp(_metadata->env) == 0) \
181 			test_name(_metadata); \
182 		__test_check_assert(_metadata); \
183 	} \
184 	static struct __test_metadata _##test_name##_object = \
185 		{ .name = #test_name, \
186 		  .fn = &wrapper_##test_name, \
187 		  .fixture = &_fixture_global, \
188 		  .termsig = _signal, \
189 		  .timeout = TEST_TIMEOUT_DEFAULT, }; \
190 	static void __attribute__((constructor)) _register_##test_name(void) \
191 	{ \
192 		__register_test(&_##test_name##_object); \
193 	} \
194 	static void test_name( \
195 		struct __test_metadata __attribute__((unused)) *_metadata)
196 
197 /**
198  * FIXTURE_DATA() - Wraps the struct name so we have one less
199  * argument to pass around
200  *
201  * @datatype_name: datatype name
202  *
203  * .. code-block:: c
204  *
205  *     FIXTURE_DATA(datatype_name)
206  *
207  * Almost always, you want just FIXTURE() instead (see below).
208  * This call may be used when the type of the fixture data
209  * is needed.  In general, this should not be needed unless
210  * the *self* is being passed to a helper directly.
211  */
212 #define FIXTURE_DATA(datatype_name) struct _test_data_##datatype_name
213 
214 /**
215  * FIXTURE() - Called once per fixture to setup the data and
216  * register
217  *
218  * @fixture_name: fixture name
219  *
220  * .. code-block:: c
221  *
222  *     FIXTURE(fixture_name) {
223  *       type property1;
224  *       ...
225  *     };
226  *
227  * Defines the data provided to TEST_F()-defined tests as *self*.  It should be
228  * populated and cleaned up using FIXTURE_SETUP() and FIXTURE_TEARDOWN().
229  */
230 #define FIXTURE(fixture_name) \
231 	FIXTURE_VARIANT(fixture_name); \
232 	static struct __fixture_metadata _##fixture_name##_fixture_object = \
233 		{ .name =  #fixture_name, }; \
234 	static void __attribute__((constructor)) \
235 	_register_##fixture_name##_data(void) \
236 	{ \
237 		__register_fixture(&_##fixture_name##_fixture_object); \
238 	} \
239 	FIXTURE_DATA(fixture_name)
240 
241 /**
242  * FIXTURE_SETUP() - Prepares the setup function for the fixture.
243  * *_metadata* is included so that EXPECT_*, ASSERT_* etc. work correctly.
244  *
245  * @fixture_name: fixture name
246  *
247  * .. code-block:: c
248  *
249  *     FIXTURE_SETUP(fixture_name) { implementation }
250  *
251  * Populates the required "setup" function for a fixture.  An instance of the
252  * datatype defined with FIXTURE_DATA() will be exposed as *self* for the
253  * implementation.
254  *
255  * ASSERT_* are valid for use in this context and will prempt the execution
256  * of any dependent fixture tests.
257  *
258  * A bare "return;" statement may be used to return early.
259  */
260 #define FIXTURE_SETUP(fixture_name) \
261 	void fixture_name##_setup( \
262 		struct __test_metadata __attribute__((unused)) *_metadata, \
263 		FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
264 		const FIXTURE_VARIANT(fixture_name) \
265 			__attribute__((unused)) *variant)
266 
267 /**
268  * FIXTURE_TEARDOWN()
269  * *_metadata* is included so that EXPECT_*, ASSERT_* etc. work correctly.
270  *
271  * @fixture_name: fixture name
272  *
273  * .. code-block:: c
274  *
275  *     FIXTURE_TEARDOWN(fixture_name) { implementation }
276  *
277  * Populates the required "teardown" function for a fixture.  An instance of the
278  * datatype defined with FIXTURE_DATA() will be exposed as *self* for the
279  * implementation to clean up.
280  *
281  * A bare "return;" statement may be used to return early.
282  */
283 #define FIXTURE_TEARDOWN(fixture_name) \
284 	void fixture_name##_teardown( \
285 		struct __test_metadata __attribute__((unused)) *_metadata, \
286 		FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
287 		const FIXTURE_VARIANT(fixture_name) \
288 			__attribute__((unused)) *variant)
289 
290 /**
291  * FIXTURE_VARIANT() - Optionally called once per fixture
292  * to declare fixture variant
293  *
294  * @fixture_name: fixture name
295  *
296  * .. code-block:: c
297  *
298  *     FIXTURE_VARIANT(fixture_name) {
299  *       type property1;
300  *       ...
301  *     };
302  *
303  * Defines type of constant parameters provided to FIXTURE_SETUP(), TEST_F() and
304  * FIXTURE_TEARDOWN as *variant*. Variants allow the same tests to be run with
305  * different arguments.
306  */
307 #define FIXTURE_VARIANT(fixture_name) struct _fixture_variant_##fixture_name
308 
309 /**
310  * FIXTURE_VARIANT_ADD() - Called once per fixture
311  * variant to setup and register the data
312  *
313  * @fixture_name: fixture name
314  * @variant_name: name of the parameter set
315  *
316  * .. code-block:: c
317  *
318  *     FIXTURE_VARIANT_ADD(fixture_name, variant_name) {
319  *       .property1 = val1,
320  *       ...
321  *     };
322  *
323  * Defines a variant of the test fixture, provided to FIXTURE_SETUP() and
324  * TEST_F() as *variant*. Tests of each fixture will be run once for each
325  * variant.
326  */
327 #define FIXTURE_VARIANT_ADD(fixture_name, variant_name) \
328 	extern FIXTURE_VARIANT(fixture_name) \
329 		_##fixture_name##_##variant_name##_variant; \
330 	static struct __fixture_variant_metadata \
331 		_##fixture_name##_##variant_name##_object = \
332 		{ .name = #variant_name, \
333 		  .data = &_##fixture_name##_##variant_name##_variant}; \
334 	static void __attribute__((constructor)) \
335 		_register_##fixture_name##_##variant_name(void) \
336 	{ \
337 		__register_fixture_variant(&_##fixture_name##_fixture_object, \
338 			&_##fixture_name##_##variant_name##_object);	\
339 	} \
340 	FIXTURE_VARIANT(fixture_name) \
341 		_##fixture_name##_##variant_name##_variant =
342 
343 /**
344  * TEST_F() - Emits test registration and helpers for
345  * fixture-based test cases
346  *
347  * @fixture_name: fixture name
348  * @test_name: test name
349  *
350  * .. code-block:: c
351  *
352  *     TEST_F(fixture, name) { implementation }
353  *
354  * Defines a test that depends on a fixture (e.g., is part of a test case).
355  * Very similar to TEST() except that *self* is the setup instance of fixture's
356  * datatype exposed for use by the implementation.
357  *
358  * The @test_name code is run in a separate process sharing the same memory
359  * (i.e. vfork), which means that the test process can update its privileges
360  * without impacting the related FIXTURE_TEARDOWN() (e.g. to remove files from
361  * a directory where write access was dropped).
362  */
363 #define TEST_F(fixture_name, test_name) \
364 	__TEST_F_IMPL(fixture_name, test_name, -1, TEST_TIMEOUT_DEFAULT)
365 
366 #define TEST_F_SIGNAL(fixture_name, test_name, signal) \
367 	__TEST_F_IMPL(fixture_name, test_name, signal, TEST_TIMEOUT_DEFAULT)
368 
369 #define TEST_F_TIMEOUT(fixture_name, test_name, timeout) \
370 	__TEST_F_IMPL(fixture_name, test_name, -1, timeout)
371 
372 #define __TEST_F_IMPL(fixture_name, test_name, signal, tmout) \
373 	static void fixture_name##_##test_name( \
374 		struct __test_metadata *_metadata, \
375 		FIXTURE_DATA(fixture_name) *self, \
376 		const FIXTURE_VARIANT(fixture_name) *variant); \
377 	static inline void wrapper_##fixture_name##_##test_name( \
378 		struct __test_metadata *_metadata, \
379 		struct __fixture_variant_metadata *variant) \
380 	{ \
381 		/* fixture data is alloced, setup, and torn down per call. */ \
382 		FIXTURE_DATA(fixture_name) self; \
383 		pid_t child = 1; \
384 		int status = 0; \
385 		bool jmp = false; \
386 		memset(&self, 0, sizeof(FIXTURE_DATA(fixture_name))); \
387 		if (setjmp(_metadata->env) == 0) { \
388 			/* Use the same _metadata. */ \
389 			child = vfork(); \
390 			if (child == 0) { \
391 				fixture_name##_setup(_metadata, &self, variant->data); \
392 				/* Let setup failure terminate early. */ \
393 				if (_metadata->exit_code) \
394 					_exit(0); \
395 				_metadata->setup_completed = true; \
396 				fixture_name##_##test_name(_metadata, &self, variant->data); \
397 			} else if (child < 0 || child != waitpid(child, &status, 0)) { \
398 				ksft_print_msg("ERROR SPAWNING TEST GRANDCHILD\n"); \
399 				_metadata->exit_code = KSFT_FAIL; \
400 			} \
401 		} \
402 		else \
403 			jmp = true; \
404 		if (child == 0) { \
405 			if (_metadata->setup_completed && !_metadata->teardown_parent && !jmp) \
406 				fixture_name##_teardown(_metadata, &self, variant->data); \
407 			_exit(0); \
408 		} \
409 		if (_metadata->setup_completed && _metadata->teardown_parent) \
410 			fixture_name##_teardown(_metadata, &self, variant->data); \
411 		if (!WIFEXITED(status) && WIFSIGNALED(status)) \
412 			/* Forward signal to __wait_for_test(). */ \
413 			kill(getpid(), WTERMSIG(status)); \
414 		__test_check_assert(_metadata); \
415 	} \
416 	static struct __test_metadata \
417 		      _##fixture_name##_##test_name##_object = { \
418 		.name = #test_name, \
419 		.fn = &wrapper_##fixture_name##_##test_name, \
420 		.fixture = &_##fixture_name##_fixture_object, \
421 		.termsig = signal, \
422 		.timeout = tmout, \
423 		.teardown_parent = false, \
424 	 }; \
425 	static void __attribute__((constructor)) \
426 			_register_##fixture_name##_##test_name(void) \
427 	{ \
428 		__register_test(&_##fixture_name##_##test_name##_object); \
429 	} \
430 	static void fixture_name##_##test_name( \
431 		struct __test_metadata __attribute__((unused)) *_metadata, \
432 		FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
433 		const FIXTURE_VARIANT(fixture_name) \
434 			__attribute__((unused)) *variant)
435 
436 /**
437  * TEST_HARNESS_MAIN - Simple wrapper to run the test harness
438  *
439  * .. code-block:: c
440  *
441  *     TEST_HARNESS_MAIN
442  *
443  * Use once to append a main() to the test file.
444  */
445 #define TEST_HARNESS_MAIN \
446 	static void __attribute__((constructor)) \
447 	__constructor_order_last(void) \
448 	{ \
449 		if (!__constructor_order) \
450 			__constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; \
451 	} \
452 	int main(int argc, char **argv) { \
453 		return test_harness_run(argc, argv); \
454 	}
455 
456 /**
457  * DOC: operators
458  *
459  * Operators for use in TEST() and TEST_F().
460  * ASSERT_* calls will stop test execution immediately.
461  * EXPECT_* calls will emit a failure warning, note it, and continue.
462  */
463 
464 /**
465  * ASSERT_EQ()
466  *
467  * @expected: expected value
468  * @seen: measured value
469  *
470  * ASSERT_EQ(expected, measured): expected == measured
471  */
472 #define ASSERT_EQ(expected, seen) \
473 	__EXPECT(expected, #expected, seen, #seen, ==, 1)
474 
475 /**
476  * ASSERT_NE()
477  *
478  * @expected: expected value
479  * @seen: measured value
480  *
481  * ASSERT_NE(expected, measured): expected != measured
482  */
483 #define ASSERT_NE(expected, seen) \
484 	__EXPECT(expected, #expected, seen, #seen, !=, 1)
485 
486 /**
487  * ASSERT_LT()
488  *
489  * @expected: expected value
490  * @seen: measured value
491  *
492  * ASSERT_LT(expected, measured): expected < measured
493  */
494 #define ASSERT_LT(expected, seen) \
495 	__EXPECT(expected, #expected, seen, #seen, <, 1)
496 
497 /**
498  * ASSERT_LE()
499  *
500  * @expected: expected value
501  * @seen: measured value
502  *
503  * ASSERT_LE(expected, measured): expected <= measured
504  */
505 #define ASSERT_LE(expected, seen) \
506 	__EXPECT(expected, #expected, seen, #seen, <=, 1)
507 
508 /**
509  * ASSERT_GT()
510  *
511  * @expected: expected value
512  * @seen: measured value
513  *
514  * ASSERT_GT(expected, measured): expected > measured
515  */
516 #define ASSERT_GT(expected, seen) \
517 	__EXPECT(expected, #expected, seen, #seen, >, 1)
518 
519 /**
520  * ASSERT_GE()
521  *
522  * @expected: expected value
523  * @seen: measured value
524  *
525  * ASSERT_GE(expected, measured): expected >= measured
526  */
527 #define ASSERT_GE(expected, seen) \
528 	__EXPECT(expected, #expected, seen, #seen, >=, 1)
529 
530 /**
531  * ASSERT_NULL()
532  *
533  * @seen: measured value
534  *
535  * ASSERT_NULL(measured): NULL == measured
536  */
537 #define ASSERT_NULL(seen) \
538 	__EXPECT(NULL, "NULL", seen, #seen, ==, 1)
539 
540 /**
541  * ASSERT_TRUE()
542  *
543  * @seen: measured value
544  *
545  * ASSERT_TRUE(measured): measured != 0
546  */
547 #define ASSERT_TRUE(seen) \
548 	__EXPECT(0, "0", seen, #seen, !=, 1)
549 
550 /**
551  * ASSERT_FALSE()
552  *
553  * @seen: measured value
554  *
555  * ASSERT_FALSE(measured): measured == 0
556  */
557 #define ASSERT_FALSE(seen) \
558 	__EXPECT(0, "0", seen, #seen, ==, 1)
559 
560 /**
561  * ASSERT_STREQ()
562  *
563  * @expected: expected value
564  * @seen: measured value
565  *
566  * ASSERT_STREQ(expected, measured): !strcmp(expected, measured)
567  */
568 #define ASSERT_STREQ(expected, seen) \
569 	__EXPECT_STR(expected, seen, ==, 1)
570 
571 /**
572  * ASSERT_STRNE()
573  *
574  * @expected: expected value
575  * @seen: measured value
576  *
577  * ASSERT_STRNE(expected, measured): strcmp(expected, measured)
578  */
579 #define ASSERT_STRNE(expected, seen) \
580 	__EXPECT_STR(expected, seen, !=, 1)
581 
582 /**
583  * EXPECT_EQ()
584  *
585  * @expected: expected value
586  * @seen: measured value
587  *
588  * EXPECT_EQ(expected, measured): expected == measured
589  */
590 #define EXPECT_EQ(expected, seen) \
591 	__EXPECT(expected, #expected, seen, #seen, ==, 0)
592 
593 /**
594  * EXPECT_NE()
595  *
596  * @expected: expected value
597  * @seen: measured value
598  *
599  * EXPECT_NE(expected, measured): expected != measured
600  */
601 #define EXPECT_NE(expected, seen) \
602 	__EXPECT(expected, #expected, seen, #seen, !=, 0)
603 
604 /**
605  * EXPECT_LT()
606  *
607  * @expected: expected value
608  * @seen: measured value
609  *
610  * EXPECT_LT(expected, measured): expected < measured
611  */
612 #define EXPECT_LT(expected, seen) \
613 	__EXPECT(expected, #expected, seen, #seen, <, 0)
614 
615 /**
616  * EXPECT_LE()
617  *
618  * @expected: expected value
619  * @seen: measured value
620  *
621  * EXPECT_LE(expected, measured): expected <= measured
622  */
623 #define EXPECT_LE(expected, seen) \
624 	__EXPECT(expected, #expected, seen, #seen, <=, 0)
625 
626 /**
627  * EXPECT_GT()
628  *
629  * @expected: expected value
630  * @seen: measured value
631  *
632  * EXPECT_GT(expected, measured): expected > measured
633  */
634 #define EXPECT_GT(expected, seen) \
635 	__EXPECT(expected, #expected, seen, #seen, >, 0)
636 
637 /**
638  * EXPECT_GE()
639  *
640  * @expected: expected value
641  * @seen: measured value
642  *
643  * EXPECT_GE(expected, measured): expected >= measured
644  */
645 #define EXPECT_GE(expected, seen) \
646 	__EXPECT(expected, #expected, seen, #seen, >=, 0)
647 
648 /**
649  * EXPECT_NULL()
650  *
651  * @seen: measured value
652  *
653  * EXPECT_NULL(measured): NULL == measured
654  */
655 #define EXPECT_NULL(seen) \
656 	__EXPECT(NULL, "NULL", seen, #seen, ==, 0)
657 
658 /**
659  * EXPECT_TRUE()
660  *
661  * @seen: measured value
662  *
663  * EXPECT_TRUE(measured): 0 != measured
664  */
665 #define EXPECT_TRUE(seen) \
666 	__EXPECT(0, "0", seen, #seen, !=, 0)
667 
668 /**
669  * EXPECT_FALSE()
670  *
671  * @seen: measured value
672  *
673  * EXPECT_FALSE(measured): 0 == measured
674  */
675 #define EXPECT_FALSE(seen) \
676 	__EXPECT(0, "0", seen, #seen, ==, 0)
677 
678 /**
679  * EXPECT_STREQ()
680  *
681  * @expected: expected value
682  * @seen: measured value
683  *
684  * EXPECT_STREQ(expected, measured): !strcmp(expected, measured)
685  */
686 #define EXPECT_STREQ(expected, seen) \
687 	__EXPECT_STR(expected, seen, ==, 0)
688 
689 /**
690  * EXPECT_STRNE()
691  *
692  * @expected: expected value
693  * @seen: measured value
694  *
695  * EXPECT_STRNE(expected, measured): strcmp(expected, measured)
696  */
697 #define EXPECT_STRNE(expected, seen) \
698 	__EXPECT_STR(expected, seen, !=, 0)
699 
700 #ifndef ARRAY_SIZE
701 #define ARRAY_SIZE(a)	(sizeof(a) / sizeof(a[0]))
702 #endif
703 
704 /* Support an optional handler after and ASSERT_* or EXPECT_*.  The approach is
705  * not thread-safe, but it should be fine in most sane test scenarios.
706  *
707  * Using __bail(), which optionally abort()s, is the easiest way to early
708  * return while still providing an optional block to the API consumer.
709  */
710 #define OPTIONAL_HANDLER(_assert) \
711 	for (; _metadata->trigger; _metadata->trigger = \
712 			__bail(_assert, _metadata))
713 
714 #define is_signed_type(var)       (!!(((__typeof__(var))(-1)) < (__typeof__(var))1))
715 
716 #define __EXPECT(_expected, _expected_str, _seen, _seen_str, _t, _assert) do { \
717 	/* Avoid multiple evaluation of the cases */ \
718 	__typeof__(_expected) __exp = (_expected); \
719 	__typeof__(_seen) __seen = (_seen); \
720 	if (!(__exp _t __seen)) { \
721 		/* Report with actual signedness to avoid weird output. */ \
722 		switch (is_signed_type(__exp) * 2 + is_signed_type(__seen)) { \
723 		case 0: { \
724 			unsigned long long __exp_print = (uintptr_t)__exp; \
725 			unsigned long long __seen_print = (uintptr_t)__seen; \
726 			__TH_LOG("Expected %s (%llu) %s %s (%llu)", \
727 				 _expected_str, __exp_print, #_t, \
728 				 _seen_str, __seen_print); \
729 			break; \
730 			} \
731 		case 1: { \
732 			unsigned long long __exp_print = (uintptr_t)__exp; \
733 			long long __seen_print = (intptr_t)__seen; \
734 			__TH_LOG("Expected %s (%llu) %s %s (%lld)", \
735 				 _expected_str, __exp_print, #_t, \
736 				 _seen_str, __seen_print); \
737 			break; \
738 			} \
739 		case 2: { \
740 			long long __exp_print = (intptr_t)__exp; \
741 			unsigned long long __seen_print = (uintptr_t)__seen; \
742 			__TH_LOG("Expected %s (%lld) %s %s (%llu)", \
743 				 _expected_str, __exp_print, #_t, \
744 				 _seen_str, __seen_print); \
745 			break; \
746 			} \
747 		case 3: { \
748 			long long __exp_print = (intptr_t)__exp; \
749 			long long __seen_print = (intptr_t)__seen; \
750 			__TH_LOG("Expected %s (%lld) %s %s (%lld)", \
751 				 _expected_str, __exp_print, #_t, \
752 				 _seen_str, __seen_print); \
753 			break; \
754 			} \
755 		} \
756 		_metadata->exit_code = KSFT_FAIL; \
757 		/* Ensure the optional handler is triggered */ \
758 		_metadata->trigger = 1; \
759 	} \
760 } while (0); OPTIONAL_HANDLER(_assert)
761 
762 #define __EXPECT_STR(_expected, _seen, _t, _assert) do { \
763 	const char *__exp = (_expected); \
764 	const char *__seen = (_seen); \
765 	if (!(strcmp(__exp, __seen) _t 0))  { \
766 		__TH_LOG("Expected '%s' %s '%s'.", __exp, #_t, __seen); \
767 		_metadata->exit_code = KSFT_FAIL; \
768 		_metadata->trigger = 1; \
769 	} \
770 } while (0); OPTIONAL_HANDLER(_assert)
771 
772 /* List helpers */
773 #define __LIST_APPEND(head, item) \
774 { \
775 	/* Circular linked list where only prev is circular. */ \
776 	if (head == NULL) { \
777 		head = item; \
778 		item->next = NULL; \
779 		item->prev = item; \
780 		return;	\
781 	} \
782 	if (__constructor_order == _CONSTRUCTOR_ORDER_FORWARD) { \
783 		item->next = NULL; \
784 		item->prev = head->prev; \
785 		item->prev->next = item; \
786 		head->prev = item; \
787 	} else { \
788 		item->next = head; \
789 		item->next->prev = item; \
790 		item->prev = item; \
791 		head = item; \
792 	} \
793 }
794 
795 struct __test_results {
796 	char reason[1024];	/* Reason for test result */
797 };
798 
799 struct __test_metadata;
800 struct __fixture_variant_metadata;
801 
802 /* Contains all the information about a fixture. */
803 struct __fixture_metadata {
804 	const char *name;
805 	struct __test_metadata *tests;
806 	struct __fixture_variant_metadata *variant;
807 	struct __fixture_metadata *prev, *next;
808 } _fixture_global __attribute__((unused)) = {
809 	.name = "global",
810 	.prev = &_fixture_global,
811 };
812 
813 struct __test_xfail {
814 	struct __fixture_metadata *fixture;
815 	struct __fixture_variant_metadata *variant;
816 	struct __test_metadata *test;
817 	struct __test_xfail *prev, *next;
818 };
819 
820 /**
821  * XFAIL_ADD() - mark variant + test case combination as expected to fail
822  * @fixture_name: name of the fixture
823  * @variant_name: name of the variant
824  * @test_name: name of the test case
825  *
826  * Mark a combination of variant + test case for a given fixture as expected
827  * to fail. Tests marked this way will report XPASS / XFAIL return codes,
828  * instead of PASS / FAIL,and use respective counters.
829  */
830 #define XFAIL_ADD(fixture_name, variant_name, test_name) \
831 	static struct __test_xfail \
832 		_##fixture_name##_##variant_name##_##test_name##_xfail = \
833 	{ \
834 		.fixture = &_##fixture_name##_fixture_object, \
835 		.variant = &_##fixture_name##_##variant_name##_object, \
836 		.test = &_##fixture_name##_##test_name##_object, \
837 	}; \
838 	static void __attribute__((constructor)) \
839 		_register_##fixture_name##_##variant_name##_##test_name##_xfail(void) \
840 	{ \
841 		__register_xfail(&_##fixture_name##_##variant_name##_##test_name##_xfail); \
842 	}
843 
844 static struct __fixture_metadata *__fixture_list = &_fixture_global;
845 static int __constructor_order;
846 
847 #define _CONSTRUCTOR_ORDER_FORWARD   1
848 #define _CONSTRUCTOR_ORDER_BACKWARD -1
849 
850 static inline void __register_fixture(struct __fixture_metadata *f)
851 {
852 	__LIST_APPEND(__fixture_list, f);
853 }
854 
855 struct __fixture_variant_metadata {
856 	const char *name;
857 	const void *data;
858 	struct __test_xfail *xfails;
859 	struct __fixture_variant_metadata *prev, *next;
860 };
861 
862 static inline void
863 __register_fixture_variant(struct __fixture_metadata *f,
864 			   struct __fixture_variant_metadata *variant)
865 {
866 	__LIST_APPEND(f->variant, variant);
867 }
868 
869 /* Contains all the information for test execution and status checking. */
870 struct __test_metadata {
871 	const char *name;
872 	void (*fn)(struct __test_metadata *,
873 		   struct __fixture_variant_metadata *);
874 	pid_t pid;	/* pid of test when being run */
875 	struct __fixture_metadata *fixture;
876 	int termsig;
877 	int exit_code;
878 	int trigger; /* extra handler after the evaluation */
879 	int timeout;	/* seconds to wait for test timeout */
880 	bool timed_out;	/* did this test timeout instead of exiting? */
881 	bool aborted;	/* stopped test due to failed ASSERT */
882 	bool setup_completed; /* did setup finish? */
883 	bool teardown_parent; /* run teardown in a parent process */
884 	jmp_buf env;	/* for exiting out of test early */
885 	struct __test_results *results;
886 	struct __test_metadata *prev, *next;
887 };
888 
889 static inline bool __test_passed(struct __test_metadata *metadata)
890 {
891 	return metadata->exit_code != KSFT_FAIL &&
892 	       metadata->exit_code <= KSFT_SKIP;
893 }
894 
895 /*
896  * Since constructors are called in reverse order, reverse the test
897  * list so tests are run in source declaration order.
898  * https://gcc.gnu.org/onlinedocs/gccint/Initialization.html
899  * However, it seems not all toolchains do this correctly, so use
900  * __constructor_order to detect which direction is called first
901  * and adjust list building logic to get things running in the right
902  * direction.
903  */
904 static inline void __register_test(struct __test_metadata *t)
905 {
906 	__LIST_APPEND(t->fixture->tests, t);
907 }
908 
909 static inline void __register_xfail(struct __test_xfail *xf)
910 {
911 	__LIST_APPEND(xf->variant->xfails, xf);
912 }
913 
914 static inline int __bail(int for_realz, struct __test_metadata *t)
915 {
916 	/* if this is ASSERT, return immediately. */
917 	if (for_realz) {
918 		t->aborted = true;
919 		longjmp(t->env, 1);
920 	}
921 	/* otherwise, end the for loop and continue. */
922 	return 0;
923 }
924 
925 static inline void __test_check_assert(struct __test_metadata *t)
926 {
927 	if (t->aborted)
928 		abort();
929 }
930 
931 struct __test_metadata *__active_test;
932 static void __timeout_handler(int sig, siginfo_t *info, void *ucontext)
933 {
934 	struct __test_metadata *t = __active_test;
935 
936 	/* Sanity check handler execution environment. */
937 	if (!t) {
938 		fprintf(TH_LOG_STREAM,
939 			"# no active test in SIGALRM handler!?\n");
940 		abort();
941 	}
942 	if (sig != SIGALRM || sig != info->si_signo) {
943 		fprintf(TH_LOG_STREAM,
944 			"# %s: SIGALRM handler caught signal %d!?\n",
945 			t->name, sig != SIGALRM ? sig : info->si_signo);
946 		abort();
947 	}
948 
949 	t->timed_out = true;
950 	// signal process group
951 	kill(-(t->pid), SIGKILL);
952 }
953 
954 void __wait_for_test(struct __test_metadata *t)
955 {
956 	struct sigaction action = {
957 		.sa_sigaction = __timeout_handler,
958 		.sa_flags = SA_SIGINFO,
959 	};
960 	struct sigaction saved_action;
961 	int status;
962 
963 	if (sigaction(SIGALRM, &action, &saved_action)) {
964 		t->exit_code = KSFT_FAIL;
965 		fprintf(TH_LOG_STREAM,
966 			"# %s: unable to install SIGALRM handler\n",
967 			t->name);
968 		return;
969 	}
970 	__active_test = t;
971 	t->timed_out = false;
972 	alarm(t->timeout);
973 	waitpid(t->pid, &status, 0);
974 	alarm(0);
975 	if (sigaction(SIGALRM, &saved_action, NULL)) {
976 		t->exit_code = KSFT_FAIL;
977 		fprintf(TH_LOG_STREAM,
978 			"# %s: unable to uninstall SIGALRM handler\n",
979 			t->name);
980 		return;
981 	}
982 	__active_test = NULL;
983 
984 	if (t->timed_out) {
985 		t->exit_code = KSFT_FAIL;
986 		fprintf(TH_LOG_STREAM,
987 			"# %s: Test terminated by timeout\n", t->name);
988 	} else if (WIFEXITED(status)) {
989 		if (WEXITSTATUS(status) == KSFT_SKIP ||
990 		    WEXITSTATUS(status) == KSFT_XPASS ||
991 		    WEXITSTATUS(status) == KSFT_XFAIL) {
992 			t->exit_code = WEXITSTATUS(status);
993 		} else if (t->termsig != -1) {
994 			t->exit_code = KSFT_FAIL;
995 			fprintf(TH_LOG_STREAM,
996 				"# %s: Test exited normally instead of by signal (code: %d)\n",
997 				t->name,
998 				WEXITSTATUS(status));
999 		} else {
1000 			switch (WEXITSTATUS(status)) {
1001 			/* Success */
1002 			case KSFT_PASS:
1003 				t->exit_code = KSFT_PASS;
1004 				break;
1005 			/* Failure */
1006 			default:
1007 				t->exit_code = KSFT_FAIL;
1008 				fprintf(TH_LOG_STREAM,
1009 					"# %s: Test failed\n",
1010 					t->name);
1011 			}
1012 		}
1013 	} else if (WIFSIGNALED(status)) {
1014 		t->exit_code = KSFT_FAIL;
1015 		if (WTERMSIG(status) == SIGABRT) {
1016 			fprintf(TH_LOG_STREAM,
1017 				"# %s: Test terminated by assertion\n",
1018 				t->name);
1019 		} else if (WTERMSIG(status) == t->termsig) {
1020 			t->exit_code = KSFT_PASS;
1021 		} else {
1022 			fprintf(TH_LOG_STREAM,
1023 				"# %s: Test terminated unexpectedly by signal %d\n",
1024 				t->name,
1025 				WTERMSIG(status));
1026 		}
1027 	} else {
1028 		fprintf(TH_LOG_STREAM,
1029 			"# %s: Test ended in some other way [%u]\n",
1030 			t->name,
1031 			status);
1032 	}
1033 }
1034 
1035 static void test_harness_list_tests(void)
1036 {
1037 	struct __fixture_variant_metadata *v;
1038 	struct __fixture_metadata *f;
1039 	struct __test_metadata *t;
1040 
1041 	for (f = __fixture_list; f; f = f->next) {
1042 		v = f->variant;
1043 		t = f->tests;
1044 
1045 		if (f == __fixture_list)
1046 			fprintf(stderr, "%-20s %-25s %s\n",
1047 				"# FIXTURE", "VARIANT", "TEST");
1048 		else
1049 			fprintf(stderr, "--------------------------------------------------------------------------------\n");
1050 
1051 		do {
1052 			fprintf(stderr, "%-20s %-25s %s\n",
1053 				t == f->tests ? f->name : "",
1054 				v ? v->name : "",
1055 				t ? t->name : "");
1056 
1057 			v = v ? v->next : NULL;
1058 			t = t ? t->next : NULL;
1059 		} while (v || t);
1060 	}
1061 }
1062 
1063 static int test_harness_argv_check(int argc, char **argv)
1064 {
1065 	int opt;
1066 
1067 	while ((opt = getopt(argc, argv, "hlF:f:V:v:t:T:r:")) != -1) {
1068 		switch (opt) {
1069 		case 'f':
1070 		case 'F':
1071 		case 'v':
1072 		case 'V':
1073 		case 't':
1074 		case 'T':
1075 		case 'r':
1076 			break;
1077 		case 'l':
1078 			test_harness_list_tests();
1079 			return KSFT_SKIP;
1080 		case 'h':
1081 		default:
1082 			fprintf(stderr,
1083 				"Usage: %s [-h|-l] [-t|-T|-v|-V|-f|-F|-r name]\n"
1084 				"\t-h       print help\n"
1085 				"\t-l       list all tests\n"
1086 				"\n"
1087 				"\t-t name  include test\n"
1088 				"\t-T name  exclude test\n"
1089 				"\t-v name  include variant\n"
1090 				"\t-V name  exclude variant\n"
1091 				"\t-f name  include fixture\n"
1092 				"\t-F name  exclude fixture\n"
1093 				"\t-r name  run specified test\n"
1094 				"\n"
1095 				"Test filter options can be specified "
1096 				"multiple times. The filtering stops\n"
1097 				"at the first match. For example to "
1098 				"include all tests from variant 'bla'\n"
1099 				"but not test 'foo' specify '-T foo -v bla'.\n"
1100 				"", argv[0]);
1101 			return opt == 'h' ? KSFT_SKIP : KSFT_FAIL;
1102 		}
1103 	}
1104 
1105 	return KSFT_PASS;
1106 }
1107 
1108 static bool test_enabled(int argc, char **argv,
1109 			 struct __fixture_metadata *f,
1110 			 struct __fixture_variant_metadata *v,
1111 			 struct __test_metadata *t)
1112 {
1113 	unsigned int flen = 0, vlen = 0, tlen = 0;
1114 	bool has_positive = false;
1115 	int opt;
1116 
1117 	optind = 1;
1118 	while ((opt = getopt(argc, argv, "F:f:V:v:t:T:r:")) != -1) {
1119 		has_positive |= islower(opt);
1120 
1121 		switch (tolower(opt)) {
1122 		case 't':
1123 			if (!strcmp(t->name, optarg))
1124 				return islower(opt);
1125 			break;
1126 		case 'f':
1127 			if (!strcmp(f->name, optarg))
1128 				return islower(opt);
1129 			break;
1130 		case 'v':
1131 			if (!strcmp(v->name, optarg))
1132 				return islower(opt);
1133 			break;
1134 		case 'r':
1135 			if (!tlen) {
1136 				flen = strlen(f->name);
1137 				vlen = strlen(v->name);
1138 				tlen = strlen(t->name);
1139 			}
1140 			if (strlen(optarg) == flen + 1 + vlen + !!vlen + tlen &&
1141 			    !strncmp(f->name, &optarg[0], flen) &&
1142 			    !strncmp(v->name, &optarg[flen + 1], vlen) &&
1143 			    !strncmp(t->name, &optarg[flen + 1 + vlen + !!vlen], tlen))
1144 				return true;
1145 			break;
1146 		}
1147 	}
1148 
1149 	/*
1150 	 * If there are no positive tests then we assume user just wants
1151 	 * exclusions and everything else is a pass.
1152 	 */
1153 	return !has_positive;
1154 }
1155 
1156 void __run_test(struct __fixture_metadata *f,
1157 		struct __fixture_variant_metadata *variant,
1158 		struct __test_metadata *t)
1159 {
1160 	struct __test_xfail *xfail;
1161 	char *test_name;
1162 	const char *diagnostic;
1163 
1164 	/* reset test struct */
1165 	t->exit_code = KSFT_PASS;
1166 	t->trigger = 0;
1167 	memset(t->results->reason, 0, sizeof(t->results->reason));
1168 
1169 	if (asprintf(&test_name, "%s%s%s.%s", f->name,
1170 		variant->name[0] ? "." : "", variant->name, t->name) == -1) {
1171 		ksft_print_msg("ERROR ALLOCATING MEMORY\n");
1172 		t->exit_code = KSFT_FAIL;
1173 		_exit(t->exit_code);
1174 	}
1175 
1176 	ksft_print_msg(" RUN           %s ...\n", test_name);
1177 
1178 	/* Make sure output buffers are flushed before fork */
1179 	fflush(stdout);
1180 	fflush(stderr);
1181 
1182 	t->pid = fork();
1183 	if (t->pid < 0) {
1184 		ksft_print_msg("ERROR SPAWNING TEST CHILD\n");
1185 		t->exit_code = KSFT_FAIL;
1186 	} else if (t->pid == 0) {
1187 		setpgrp();
1188 		t->fn(t, variant);
1189 		_exit(t->exit_code);
1190 	} else {
1191 		__wait_for_test(t);
1192 	}
1193 	ksft_print_msg("         %4s  %s\n",
1194 		       __test_passed(t) ? "OK" : "FAIL", test_name);
1195 
1196 	/* Check if we're expecting this test to fail */
1197 	for (xfail = variant->xfails; xfail; xfail = xfail->next)
1198 		if (xfail->test == t)
1199 			break;
1200 	if (xfail)
1201 		t->exit_code = __test_passed(t) ? KSFT_XPASS : KSFT_XFAIL;
1202 
1203 	if (t->results->reason[0])
1204 		diagnostic = t->results->reason;
1205 	else if (t->exit_code == KSFT_PASS || t->exit_code == KSFT_FAIL)
1206 		diagnostic = NULL;
1207 	else
1208 		diagnostic = "unknown";
1209 
1210 	ksft_test_result_code(t->exit_code, test_name,
1211 			      diagnostic ? "%s" : NULL, diagnostic);
1212 	free(test_name);
1213 }
1214 
1215 static int test_harness_run(int argc, char **argv)
1216 {
1217 	struct __fixture_variant_metadata no_variant = { .name = "", };
1218 	struct __fixture_variant_metadata *v;
1219 	struct __fixture_metadata *f;
1220 	struct __test_results *results;
1221 	struct __test_metadata *t;
1222 	int ret;
1223 	unsigned int case_count = 0, test_count = 0;
1224 	unsigned int count = 0;
1225 	unsigned int pass_count = 0;
1226 
1227 	ret = test_harness_argv_check(argc, argv);
1228 	if (ret != KSFT_PASS)
1229 		return ret;
1230 
1231 	for (f = __fixture_list; f; f = f->next) {
1232 		for (v = f->variant ?: &no_variant; v; v = v->next) {
1233 			unsigned int old_tests = test_count;
1234 
1235 			for (t = f->tests; t; t = t->next)
1236 				if (test_enabled(argc, argv, f, v, t))
1237 					test_count++;
1238 
1239 			if (old_tests != test_count)
1240 				case_count++;
1241 		}
1242 	}
1243 
1244 	results = mmap(NULL, sizeof(*results), PROT_READ | PROT_WRITE,
1245 		       MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1246 
1247 	ksft_print_header();
1248 	ksft_set_plan(test_count);
1249 	ksft_print_msg("Starting %u tests from %u test cases.\n",
1250 	       test_count, case_count);
1251 	for (f = __fixture_list; f; f = f->next) {
1252 		for (v = f->variant ?: &no_variant; v; v = v->next) {
1253 			for (t = f->tests; t; t = t->next) {
1254 				if (!test_enabled(argc, argv, f, v, t))
1255 					continue;
1256 				count++;
1257 				t->results = results;
1258 				__run_test(f, v, t);
1259 				t->results = NULL;
1260 				if (__test_passed(t))
1261 					pass_count++;
1262 				else
1263 					ret = 1;
1264 			}
1265 		}
1266 	}
1267 	munmap(results, sizeof(*results));
1268 
1269 	ksft_print_msg("%s: %u / %u tests passed.\n", ret ? "FAILED" : "PASSED",
1270 			pass_count, count);
1271 	ksft_exit(ret == 0);
1272 
1273 	/* unreachable */
1274 	return KSFT_FAIL;
1275 }
1276 
1277 static void __attribute__((constructor)) __constructor_order_first(void)
1278 {
1279 	if (!__constructor_order)
1280 		__constructor_order = _CONSTRUCTOR_ORDER_FORWARD;
1281 }
1282 
1283 #endif  /* __KSELFTEST_HARNESS_H */
1284