xref: /linux/tools/testing/selftests/kselftest_harness.h (revision 53ed0af4964229595b60594b35334d006d411ef0)
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 		memset(&self, 0, sizeof(FIXTURE_DATA(fixture_name))); \
386 		if (setjmp(_metadata->env) == 0) { \
387 			/* Use the same _metadata. */ \
388 			child = vfork(); \
389 			if (child == 0) { \
390 				fixture_name##_setup(_metadata, &self, variant->data); \
391 				/* Let setup failure terminate early. */ \
392 				if (_metadata->exit_code) \
393 					_exit(0); \
394 				_metadata->setup_completed = true; \
395 				fixture_name##_##test_name(_metadata, &self, variant->data); \
396 			} else if (child < 0 || child != waitpid(child, &status, 0)) { \
397 				ksft_print_msg("ERROR SPAWNING TEST GRANDCHILD\n"); \
398 				_metadata->exit_code = KSFT_FAIL; \
399 			} \
400 		} \
401 		if (child == 0) { \
402 			if (_metadata->setup_completed && !_metadata->teardown_parent) \
403 				fixture_name##_teardown(_metadata, &self, variant->data); \
404 			_exit(0); \
405 		} \
406 		if (_metadata->setup_completed && _metadata->teardown_parent) \
407 			fixture_name##_teardown(_metadata, &self, variant->data); \
408 		if (!WIFEXITED(status) && WIFSIGNALED(status)) \
409 			/* Forward signal to __wait_for_test(). */ \
410 			kill(getpid(), WTERMSIG(status)); \
411 		__test_check_assert(_metadata); \
412 	} \
413 	static struct __test_metadata \
414 		      _##fixture_name##_##test_name##_object = { \
415 		.name = #test_name, \
416 		.fn = &wrapper_##fixture_name##_##test_name, \
417 		.fixture = &_##fixture_name##_fixture_object, \
418 		.termsig = signal, \
419 		.timeout = tmout, \
420 		.teardown_parent = false, \
421 	 }; \
422 	static void __attribute__((constructor)) \
423 			_register_##fixture_name##_##test_name(void) \
424 	{ \
425 		__register_test(&_##fixture_name##_##test_name##_object); \
426 	} \
427 	static void fixture_name##_##test_name( \
428 		struct __test_metadata __attribute__((unused)) *_metadata, \
429 		FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
430 		const FIXTURE_VARIANT(fixture_name) \
431 			__attribute__((unused)) *variant)
432 
433 /**
434  * TEST_HARNESS_MAIN - Simple wrapper to run the test harness
435  *
436  * .. code-block:: c
437  *
438  *     TEST_HARNESS_MAIN
439  *
440  * Use once to append a main() to the test file.
441  */
442 #define TEST_HARNESS_MAIN \
443 	static void __attribute__((constructor)) \
444 	__constructor_order_last(void) \
445 	{ \
446 		if (!__constructor_order) \
447 			__constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; \
448 	} \
449 	int main(int argc, char **argv) { \
450 		return test_harness_run(argc, argv); \
451 	}
452 
453 /**
454  * DOC: operators
455  *
456  * Operators for use in TEST() and TEST_F().
457  * ASSERT_* calls will stop test execution immediately.
458  * EXPECT_* calls will emit a failure warning, note it, and continue.
459  */
460 
461 /**
462  * ASSERT_EQ()
463  *
464  * @expected: expected value
465  * @seen: measured value
466  *
467  * ASSERT_EQ(expected, measured): expected == measured
468  */
469 #define ASSERT_EQ(expected, seen) \
470 	__EXPECT(expected, #expected, seen, #seen, ==, 1)
471 
472 /**
473  * ASSERT_NE()
474  *
475  * @expected: expected value
476  * @seen: measured value
477  *
478  * ASSERT_NE(expected, measured): expected != measured
479  */
480 #define ASSERT_NE(expected, seen) \
481 	__EXPECT(expected, #expected, seen, #seen, !=, 1)
482 
483 /**
484  * ASSERT_LT()
485  *
486  * @expected: expected value
487  * @seen: measured value
488  *
489  * ASSERT_LT(expected, measured): expected < measured
490  */
491 #define ASSERT_LT(expected, seen) \
492 	__EXPECT(expected, #expected, seen, #seen, <, 1)
493 
494 /**
495  * ASSERT_LE()
496  *
497  * @expected: expected value
498  * @seen: measured value
499  *
500  * ASSERT_LE(expected, measured): expected <= measured
501  */
502 #define ASSERT_LE(expected, seen) \
503 	__EXPECT(expected, #expected, seen, #seen, <=, 1)
504 
505 /**
506  * ASSERT_GT()
507  *
508  * @expected: expected value
509  * @seen: measured value
510  *
511  * ASSERT_GT(expected, measured): expected > measured
512  */
513 #define ASSERT_GT(expected, seen) \
514 	__EXPECT(expected, #expected, seen, #seen, >, 1)
515 
516 /**
517  * ASSERT_GE()
518  *
519  * @expected: expected value
520  * @seen: measured value
521  *
522  * ASSERT_GE(expected, measured): expected >= measured
523  */
524 #define ASSERT_GE(expected, seen) \
525 	__EXPECT(expected, #expected, seen, #seen, >=, 1)
526 
527 /**
528  * ASSERT_NULL()
529  *
530  * @seen: measured value
531  *
532  * ASSERT_NULL(measured): NULL == measured
533  */
534 #define ASSERT_NULL(seen) \
535 	__EXPECT(NULL, "NULL", seen, #seen, ==, 1)
536 
537 /**
538  * ASSERT_TRUE()
539  *
540  * @seen: measured value
541  *
542  * ASSERT_TRUE(measured): measured != 0
543  */
544 #define ASSERT_TRUE(seen) \
545 	__EXPECT(0, "0", seen, #seen, !=, 1)
546 
547 /**
548  * ASSERT_FALSE()
549  *
550  * @seen: measured value
551  *
552  * ASSERT_FALSE(measured): measured == 0
553  */
554 #define ASSERT_FALSE(seen) \
555 	__EXPECT(0, "0", seen, #seen, ==, 1)
556 
557 /**
558  * ASSERT_STREQ()
559  *
560  * @expected: expected value
561  * @seen: measured value
562  *
563  * ASSERT_STREQ(expected, measured): !strcmp(expected, measured)
564  */
565 #define ASSERT_STREQ(expected, seen) \
566 	__EXPECT_STR(expected, seen, ==, 1)
567 
568 /**
569  * ASSERT_STRNE()
570  *
571  * @expected: expected value
572  * @seen: measured value
573  *
574  * ASSERT_STRNE(expected, measured): strcmp(expected, measured)
575  */
576 #define ASSERT_STRNE(expected, seen) \
577 	__EXPECT_STR(expected, seen, !=, 1)
578 
579 /**
580  * EXPECT_EQ()
581  *
582  * @expected: expected value
583  * @seen: measured value
584  *
585  * EXPECT_EQ(expected, measured): expected == measured
586  */
587 #define EXPECT_EQ(expected, seen) \
588 	__EXPECT(expected, #expected, seen, #seen, ==, 0)
589 
590 /**
591  * EXPECT_NE()
592  *
593  * @expected: expected value
594  * @seen: measured value
595  *
596  * EXPECT_NE(expected, measured): expected != measured
597  */
598 #define EXPECT_NE(expected, seen) \
599 	__EXPECT(expected, #expected, seen, #seen, !=, 0)
600 
601 /**
602  * EXPECT_LT()
603  *
604  * @expected: expected value
605  * @seen: measured value
606  *
607  * EXPECT_LT(expected, measured): expected < measured
608  */
609 #define EXPECT_LT(expected, seen) \
610 	__EXPECT(expected, #expected, seen, #seen, <, 0)
611 
612 /**
613  * EXPECT_LE()
614  *
615  * @expected: expected value
616  * @seen: measured value
617  *
618  * EXPECT_LE(expected, measured): expected <= measured
619  */
620 #define EXPECT_LE(expected, seen) \
621 	__EXPECT(expected, #expected, seen, #seen, <=, 0)
622 
623 /**
624  * EXPECT_GT()
625  *
626  * @expected: expected value
627  * @seen: measured value
628  *
629  * EXPECT_GT(expected, measured): expected > measured
630  */
631 #define EXPECT_GT(expected, seen) \
632 	__EXPECT(expected, #expected, seen, #seen, >, 0)
633 
634 /**
635  * EXPECT_GE()
636  *
637  * @expected: expected value
638  * @seen: measured value
639  *
640  * EXPECT_GE(expected, measured): expected >= measured
641  */
642 #define EXPECT_GE(expected, seen) \
643 	__EXPECT(expected, #expected, seen, #seen, >=, 0)
644 
645 /**
646  * EXPECT_NULL()
647  *
648  * @seen: measured value
649  *
650  * EXPECT_NULL(measured): NULL == measured
651  */
652 #define EXPECT_NULL(seen) \
653 	__EXPECT(NULL, "NULL", seen, #seen, ==, 0)
654 
655 /**
656  * EXPECT_TRUE()
657  *
658  * @seen: measured value
659  *
660  * EXPECT_TRUE(measured): 0 != measured
661  */
662 #define EXPECT_TRUE(seen) \
663 	__EXPECT(0, "0", seen, #seen, !=, 0)
664 
665 /**
666  * EXPECT_FALSE()
667  *
668  * @seen: measured value
669  *
670  * EXPECT_FALSE(measured): 0 == measured
671  */
672 #define EXPECT_FALSE(seen) \
673 	__EXPECT(0, "0", seen, #seen, ==, 0)
674 
675 /**
676  * EXPECT_STREQ()
677  *
678  * @expected: expected value
679  * @seen: measured value
680  *
681  * EXPECT_STREQ(expected, measured): !strcmp(expected, measured)
682  */
683 #define EXPECT_STREQ(expected, seen) \
684 	__EXPECT_STR(expected, seen, ==, 0)
685 
686 /**
687  * EXPECT_STRNE()
688  *
689  * @expected: expected value
690  * @seen: measured value
691  *
692  * EXPECT_STRNE(expected, measured): strcmp(expected, measured)
693  */
694 #define EXPECT_STRNE(expected, seen) \
695 	__EXPECT_STR(expected, seen, !=, 0)
696 
697 #ifndef ARRAY_SIZE
698 #define ARRAY_SIZE(a)	(sizeof(a) / sizeof(a[0]))
699 #endif
700 
701 /* Support an optional handler after and ASSERT_* or EXPECT_*.  The approach is
702  * not thread-safe, but it should be fine in most sane test scenarios.
703  *
704  * Using __bail(), which optionally abort()s, is the easiest way to early
705  * return while still providing an optional block to the API consumer.
706  */
707 #define OPTIONAL_HANDLER(_assert) \
708 	for (; _metadata->trigger; _metadata->trigger = \
709 			__bail(_assert, _metadata))
710 
711 #define is_signed_type(var)       (!!(((__typeof__(var))(-1)) < (__typeof__(var))1))
712 
713 #define __EXPECT(_expected, _expected_str, _seen, _seen_str, _t, _assert) do { \
714 	/* Avoid multiple evaluation of the cases */ \
715 	__typeof__(_expected) __exp = (_expected); \
716 	__typeof__(_seen) __seen = (_seen); \
717 	if (!(__exp _t __seen)) { \
718 		/* Report with actual signedness to avoid weird output. */ \
719 		switch (is_signed_type(__exp) * 2 + is_signed_type(__seen)) { \
720 		case 0: { \
721 			unsigned long long __exp_print = (uintptr_t)__exp; \
722 			unsigned long long __seen_print = (uintptr_t)__seen; \
723 			__TH_LOG("Expected %s (%llu) %s %s (%llu)", \
724 				 _expected_str, __exp_print, #_t, \
725 				 _seen_str, __seen_print); \
726 			break; \
727 			} \
728 		case 1: { \
729 			unsigned long long __exp_print = (uintptr_t)__exp; \
730 			long long __seen_print = (intptr_t)__seen; \
731 			__TH_LOG("Expected %s (%llu) %s %s (%lld)", \
732 				 _expected_str, __exp_print, #_t, \
733 				 _seen_str, __seen_print); \
734 			break; \
735 			} \
736 		case 2: { \
737 			long long __exp_print = (intptr_t)__exp; \
738 			unsigned long long __seen_print = (uintptr_t)__seen; \
739 			__TH_LOG("Expected %s (%lld) %s %s (%llu)", \
740 				 _expected_str, __exp_print, #_t, \
741 				 _seen_str, __seen_print); \
742 			break; \
743 			} \
744 		case 3: { \
745 			long long __exp_print = (intptr_t)__exp; \
746 			long long __seen_print = (intptr_t)__seen; \
747 			__TH_LOG("Expected %s (%lld) %s %s (%lld)", \
748 				 _expected_str, __exp_print, #_t, \
749 				 _seen_str, __seen_print); \
750 			break; \
751 			} \
752 		} \
753 		_metadata->exit_code = KSFT_FAIL; \
754 		/* Ensure the optional handler is triggered */ \
755 		_metadata->trigger = 1; \
756 	} \
757 } while (0); OPTIONAL_HANDLER(_assert)
758 
759 #define __EXPECT_STR(_expected, _seen, _t, _assert) do { \
760 	const char *__exp = (_expected); \
761 	const char *__seen = (_seen); \
762 	if (!(strcmp(__exp, __seen) _t 0))  { \
763 		__TH_LOG("Expected '%s' %s '%s'.", __exp, #_t, __seen); \
764 		_metadata->exit_code = KSFT_FAIL; \
765 		_metadata->trigger = 1; \
766 	} \
767 } while (0); OPTIONAL_HANDLER(_assert)
768 
769 /* List helpers */
770 #define __LIST_APPEND(head, item) \
771 { \
772 	/* Circular linked list where only prev is circular. */ \
773 	if (head == NULL) { \
774 		head = item; \
775 		item->next = NULL; \
776 		item->prev = item; \
777 		return;	\
778 	} \
779 	if (__constructor_order == _CONSTRUCTOR_ORDER_FORWARD) { \
780 		item->next = NULL; \
781 		item->prev = head->prev; \
782 		item->prev->next = item; \
783 		head->prev = item; \
784 	} else { \
785 		item->next = head; \
786 		item->next->prev = item; \
787 		item->prev = item; \
788 		head = item; \
789 	} \
790 }
791 
792 struct __test_results {
793 	char reason[1024];	/* Reason for test result */
794 };
795 
796 struct __test_metadata;
797 struct __fixture_variant_metadata;
798 
799 /* Contains all the information about a fixture. */
800 struct __fixture_metadata {
801 	const char *name;
802 	struct __test_metadata *tests;
803 	struct __fixture_variant_metadata *variant;
804 	struct __fixture_metadata *prev, *next;
805 } _fixture_global __attribute__((unused)) = {
806 	.name = "global",
807 	.prev = &_fixture_global,
808 };
809 
810 struct __test_xfail {
811 	struct __fixture_metadata *fixture;
812 	struct __fixture_variant_metadata *variant;
813 	struct __test_metadata *test;
814 	struct __test_xfail *prev, *next;
815 };
816 
817 /**
818  * XFAIL_ADD() - mark variant + test case combination as expected to fail
819  * @fixture_name: name of the fixture
820  * @variant_name: name of the variant
821  * @test_name: name of the test case
822  *
823  * Mark a combination of variant + test case for a given fixture as expected
824  * to fail. Tests marked this way will report XPASS / XFAIL return codes,
825  * instead of PASS / FAIL,and use respective counters.
826  */
827 #define XFAIL_ADD(fixture_name, variant_name, test_name) \
828 	static struct __test_xfail \
829 		_##fixture_name##_##variant_name##_##test_name##_xfail = \
830 	{ \
831 		.fixture = &_##fixture_name##_fixture_object, \
832 		.variant = &_##fixture_name##_##variant_name##_object, \
833 		.test = &_##fixture_name##_##test_name##_object, \
834 	}; \
835 	static void __attribute__((constructor)) \
836 		_register_##fixture_name##_##variant_name##_##test_name##_xfail(void) \
837 	{ \
838 		__register_xfail(&_##fixture_name##_##variant_name##_##test_name##_xfail); \
839 	}
840 
841 static struct __fixture_metadata *__fixture_list = &_fixture_global;
842 static int __constructor_order;
843 
844 #define _CONSTRUCTOR_ORDER_FORWARD   1
845 #define _CONSTRUCTOR_ORDER_BACKWARD -1
846 
847 static inline void __register_fixture(struct __fixture_metadata *f)
848 {
849 	__LIST_APPEND(__fixture_list, f);
850 }
851 
852 struct __fixture_variant_metadata {
853 	const char *name;
854 	const void *data;
855 	struct __test_xfail *xfails;
856 	struct __fixture_variant_metadata *prev, *next;
857 };
858 
859 static inline void
860 __register_fixture_variant(struct __fixture_metadata *f,
861 			   struct __fixture_variant_metadata *variant)
862 {
863 	__LIST_APPEND(f->variant, variant);
864 }
865 
866 /* Contains all the information for test execution and status checking. */
867 struct __test_metadata {
868 	const char *name;
869 	void (*fn)(struct __test_metadata *,
870 		   struct __fixture_variant_metadata *);
871 	pid_t pid;	/* pid of test when being run */
872 	struct __fixture_metadata *fixture;
873 	int termsig;
874 	int exit_code;
875 	int trigger; /* extra handler after the evaluation */
876 	int timeout;	/* seconds to wait for test timeout */
877 	bool timed_out;	/* did this test timeout instead of exiting? */
878 	bool aborted;	/* stopped test due to failed ASSERT */
879 	bool setup_completed; /* did setup finish? */
880 	bool teardown_parent; /* run teardown in a parent process */
881 	jmp_buf env;	/* for exiting out of test early */
882 	struct __test_results *results;
883 	struct __test_metadata *prev, *next;
884 };
885 
886 static inline bool __test_passed(struct __test_metadata *metadata)
887 {
888 	return metadata->exit_code != KSFT_FAIL &&
889 	       metadata->exit_code <= KSFT_SKIP;
890 }
891 
892 /*
893  * Since constructors are called in reverse order, reverse the test
894  * list so tests are run in source declaration order.
895  * https://gcc.gnu.org/onlinedocs/gccint/Initialization.html
896  * However, it seems not all toolchains do this correctly, so use
897  * __constructor_order to detect which direction is called first
898  * and adjust list building logic to get things running in the right
899  * direction.
900  */
901 static inline void __register_test(struct __test_metadata *t)
902 {
903 	__LIST_APPEND(t->fixture->tests, t);
904 }
905 
906 static inline void __register_xfail(struct __test_xfail *xf)
907 {
908 	__LIST_APPEND(xf->variant->xfails, xf);
909 }
910 
911 static inline int __bail(int for_realz, struct __test_metadata *t)
912 {
913 	/* if this is ASSERT, return immediately. */
914 	if (for_realz) {
915 		t->aborted = true;
916 		longjmp(t->env, 1);
917 	}
918 	/* otherwise, end the for loop and continue. */
919 	return 0;
920 }
921 
922 static inline void __test_check_assert(struct __test_metadata *t)
923 {
924 	if (t->aborted)
925 		abort();
926 }
927 
928 struct __test_metadata *__active_test;
929 static void __timeout_handler(int sig, siginfo_t *info, void *ucontext)
930 {
931 	struct __test_metadata *t = __active_test;
932 
933 	/* Sanity check handler execution environment. */
934 	if (!t) {
935 		fprintf(TH_LOG_STREAM,
936 			"# no active test in SIGALRM handler!?\n");
937 		abort();
938 	}
939 	if (sig != SIGALRM || sig != info->si_signo) {
940 		fprintf(TH_LOG_STREAM,
941 			"# %s: SIGALRM handler caught signal %d!?\n",
942 			t->name, sig != SIGALRM ? sig : info->si_signo);
943 		abort();
944 	}
945 
946 	t->timed_out = true;
947 	// signal process group
948 	kill(-(t->pid), SIGKILL);
949 }
950 
951 void __wait_for_test(struct __test_metadata *t)
952 {
953 	struct sigaction action = {
954 		.sa_sigaction = __timeout_handler,
955 		.sa_flags = SA_SIGINFO,
956 	};
957 	struct sigaction saved_action;
958 	int status;
959 
960 	if (sigaction(SIGALRM, &action, &saved_action)) {
961 		t->exit_code = KSFT_FAIL;
962 		fprintf(TH_LOG_STREAM,
963 			"# %s: unable to install SIGALRM handler\n",
964 			t->name);
965 		return;
966 	}
967 	__active_test = t;
968 	t->timed_out = false;
969 	alarm(t->timeout);
970 	waitpid(t->pid, &status, 0);
971 	alarm(0);
972 	if (sigaction(SIGALRM, &saved_action, NULL)) {
973 		t->exit_code = KSFT_FAIL;
974 		fprintf(TH_LOG_STREAM,
975 			"# %s: unable to uninstall SIGALRM handler\n",
976 			t->name);
977 		return;
978 	}
979 	__active_test = NULL;
980 
981 	if (t->timed_out) {
982 		t->exit_code = KSFT_FAIL;
983 		fprintf(TH_LOG_STREAM,
984 			"# %s: Test terminated by timeout\n", t->name);
985 	} else if (WIFEXITED(status)) {
986 		if (WEXITSTATUS(status) == KSFT_SKIP ||
987 		    WEXITSTATUS(status) == KSFT_XPASS ||
988 		    WEXITSTATUS(status) == KSFT_XFAIL) {
989 			t->exit_code = WEXITSTATUS(status);
990 		} else if (t->termsig != -1) {
991 			t->exit_code = KSFT_FAIL;
992 			fprintf(TH_LOG_STREAM,
993 				"# %s: Test exited normally instead of by signal (code: %d)\n",
994 				t->name,
995 				WEXITSTATUS(status));
996 		} else {
997 			switch (WEXITSTATUS(status)) {
998 			/* Success */
999 			case KSFT_PASS:
1000 				t->exit_code = KSFT_PASS;
1001 				break;
1002 			/* Failure */
1003 			default:
1004 				t->exit_code = KSFT_FAIL;
1005 				fprintf(TH_LOG_STREAM,
1006 					"# %s: Test failed\n",
1007 					t->name);
1008 			}
1009 		}
1010 	} else if (WIFSIGNALED(status)) {
1011 		t->exit_code = KSFT_FAIL;
1012 		if (WTERMSIG(status) == SIGABRT) {
1013 			fprintf(TH_LOG_STREAM,
1014 				"# %s: Test terminated by assertion\n",
1015 				t->name);
1016 		} else if (WTERMSIG(status) == t->termsig) {
1017 			t->exit_code = KSFT_PASS;
1018 		} else {
1019 			fprintf(TH_LOG_STREAM,
1020 				"# %s: Test terminated unexpectedly by signal %d\n",
1021 				t->name,
1022 				WTERMSIG(status));
1023 		}
1024 	} else {
1025 		fprintf(TH_LOG_STREAM,
1026 			"# %s: Test ended in some other way [%u]\n",
1027 			t->name,
1028 			status);
1029 	}
1030 }
1031 
1032 static void test_harness_list_tests(void)
1033 {
1034 	struct __fixture_variant_metadata *v;
1035 	struct __fixture_metadata *f;
1036 	struct __test_metadata *t;
1037 
1038 	for (f = __fixture_list; f; f = f->next) {
1039 		v = f->variant;
1040 		t = f->tests;
1041 
1042 		if (f == __fixture_list)
1043 			fprintf(stderr, "%-20s %-25s %s\n",
1044 				"# FIXTURE", "VARIANT", "TEST");
1045 		else
1046 			fprintf(stderr, "--------------------------------------------------------------------------------\n");
1047 
1048 		do {
1049 			fprintf(stderr, "%-20s %-25s %s\n",
1050 				t == f->tests ? f->name : "",
1051 				v ? v->name : "",
1052 				t ? t->name : "");
1053 
1054 			v = v ? v->next : NULL;
1055 			t = t ? t->next : NULL;
1056 		} while (v || t);
1057 	}
1058 }
1059 
1060 static int test_harness_argv_check(int argc, char **argv)
1061 {
1062 	int opt;
1063 
1064 	while ((opt = getopt(argc, argv, "hlF:f:V:v:t:T:r:")) != -1) {
1065 		switch (opt) {
1066 		case 'f':
1067 		case 'F':
1068 		case 'v':
1069 		case 'V':
1070 		case 't':
1071 		case 'T':
1072 		case 'r':
1073 			break;
1074 		case 'l':
1075 			test_harness_list_tests();
1076 			return KSFT_SKIP;
1077 		case 'h':
1078 		default:
1079 			fprintf(stderr,
1080 				"Usage: %s [-h|-l] [-t|-T|-v|-V|-f|-F|-r name]\n"
1081 				"\t-h       print help\n"
1082 				"\t-l       list all tests\n"
1083 				"\n"
1084 				"\t-t name  include test\n"
1085 				"\t-T name  exclude test\n"
1086 				"\t-v name  include variant\n"
1087 				"\t-V name  exclude variant\n"
1088 				"\t-f name  include fixture\n"
1089 				"\t-F name  exclude fixture\n"
1090 				"\t-r name  run specified test\n"
1091 				"\n"
1092 				"Test filter options can be specified "
1093 				"multiple times. The filtering stops\n"
1094 				"at the first match. For example to "
1095 				"include all tests from variant 'bla'\n"
1096 				"but not test 'foo' specify '-T foo -v bla'.\n"
1097 				"", argv[0]);
1098 			return opt == 'h' ? KSFT_SKIP : KSFT_FAIL;
1099 		}
1100 	}
1101 
1102 	return KSFT_PASS;
1103 }
1104 
1105 static bool test_enabled(int argc, char **argv,
1106 			 struct __fixture_metadata *f,
1107 			 struct __fixture_variant_metadata *v,
1108 			 struct __test_metadata *t)
1109 {
1110 	unsigned int flen = 0, vlen = 0, tlen = 0;
1111 	bool has_positive = false;
1112 	int opt;
1113 
1114 	optind = 1;
1115 	while ((opt = getopt(argc, argv, "F:f:V:v:t:T:r:")) != -1) {
1116 		has_positive |= islower(opt);
1117 
1118 		switch (tolower(opt)) {
1119 		case 't':
1120 			if (!strcmp(t->name, optarg))
1121 				return islower(opt);
1122 			break;
1123 		case 'f':
1124 			if (!strcmp(f->name, optarg))
1125 				return islower(opt);
1126 			break;
1127 		case 'v':
1128 			if (!strcmp(v->name, optarg))
1129 				return islower(opt);
1130 			break;
1131 		case 'r':
1132 			if (!tlen) {
1133 				flen = strlen(f->name);
1134 				vlen = strlen(v->name);
1135 				tlen = strlen(t->name);
1136 			}
1137 			if (strlen(optarg) == flen + 1 + vlen + !!vlen + tlen &&
1138 			    !strncmp(f->name, &optarg[0], flen) &&
1139 			    !strncmp(v->name, &optarg[flen + 1], vlen) &&
1140 			    !strncmp(t->name, &optarg[flen + 1 + vlen + !!vlen], tlen))
1141 				return true;
1142 			break;
1143 		}
1144 	}
1145 
1146 	/*
1147 	 * If there are no positive tests then we assume user just wants
1148 	 * exclusions and everything else is a pass.
1149 	 */
1150 	return !has_positive;
1151 }
1152 
1153 void __run_test(struct __fixture_metadata *f,
1154 		struct __fixture_variant_metadata *variant,
1155 		struct __test_metadata *t)
1156 {
1157 	struct __test_xfail *xfail;
1158 	char *test_name;
1159 	const char *diagnostic;
1160 
1161 	/* reset test struct */
1162 	t->exit_code = KSFT_PASS;
1163 	t->trigger = 0;
1164 	memset(t->results->reason, 0, sizeof(t->results->reason));
1165 
1166 	if (asprintf(&test_name, "%s%s%s.%s", f->name,
1167 		variant->name[0] ? "." : "", variant->name, t->name) == -1) {
1168 		ksft_print_msg("ERROR ALLOCATING MEMORY\n");
1169 		t->exit_code = KSFT_FAIL;
1170 		_exit(t->exit_code);
1171 	}
1172 
1173 	ksft_print_msg(" RUN           %s ...\n", test_name);
1174 
1175 	/* Make sure output buffers are flushed before fork */
1176 	fflush(stdout);
1177 	fflush(stderr);
1178 
1179 	t->pid = fork();
1180 	if (t->pid < 0) {
1181 		ksft_print_msg("ERROR SPAWNING TEST CHILD\n");
1182 		t->exit_code = KSFT_FAIL;
1183 	} else if (t->pid == 0) {
1184 		setpgrp();
1185 		t->fn(t, variant);
1186 		_exit(t->exit_code);
1187 	} else {
1188 		__wait_for_test(t);
1189 	}
1190 	ksft_print_msg("         %4s  %s\n",
1191 		       __test_passed(t) ? "OK" : "FAIL", test_name);
1192 
1193 	/* Check if we're expecting this test to fail */
1194 	for (xfail = variant->xfails; xfail; xfail = xfail->next)
1195 		if (xfail->test == t)
1196 			break;
1197 	if (xfail)
1198 		t->exit_code = __test_passed(t) ? KSFT_XPASS : KSFT_XFAIL;
1199 
1200 	if (t->results->reason[0])
1201 		diagnostic = t->results->reason;
1202 	else if (t->exit_code == KSFT_PASS || t->exit_code == KSFT_FAIL)
1203 		diagnostic = NULL;
1204 	else
1205 		diagnostic = "unknown";
1206 
1207 	ksft_test_result_code(t->exit_code, test_name,
1208 			      diagnostic ? "%s" : "", diagnostic);
1209 	free(test_name);
1210 }
1211 
1212 static int test_harness_run(int argc, char **argv)
1213 {
1214 	struct __fixture_variant_metadata no_variant = { .name = "", };
1215 	struct __fixture_variant_metadata *v;
1216 	struct __fixture_metadata *f;
1217 	struct __test_results *results;
1218 	struct __test_metadata *t;
1219 	int ret;
1220 	unsigned int case_count = 0, test_count = 0;
1221 	unsigned int count = 0;
1222 	unsigned int pass_count = 0;
1223 
1224 	ret = test_harness_argv_check(argc, argv);
1225 	if (ret != KSFT_PASS)
1226 		return ret;
1227 
1228 	for (f = __fixture_list; f; f = f->next) {
1229 		for (v = f->variant ?: &no_variant; v; v = v->next) {
1230 			unsigned int old_tests = test_count;
1231 
1232 			for (t = f->tests; t; t = t->next)
1233 				if (test_enabled(argc, argv, f, v, t))
1234 					test_count++;
1235 
1236 			if (old_tests != test_count)
1237 				case_count++;
1238 		}
1239 	}
1240 
1241 	results = mmap(NULL, sizeof(*results), PROT_READ | PROT_WRITE,
1242 		       MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1243 
1244 	ksft_print_header();
1245 	ksft_set_plan(test_count);
1246 	ksft_print_msg("Starting %u tests from %u test cases.\n",
1247 	       test_count, case_count);
1248 	for (f = __fixture_list; f; f = f->next) {
1249 		for (v = f->variant ?: &no_variant; v; v = v->next) {
1250 			for (t = f->tests; t; t = t->next) {
1251 				if (!test_enabled(argc, argv, f, v, t))
1252 					continue;
1253 				count++;
1254 				t->results = results;
1255 				__run_test(f, v, t);
1256 				t->results = NULL;
1257 				if (__test_passed(t))
1258 					pass_count++;
1259 				else
1260 					ret = 1;
1261 			}
1262 		}
1263 	}
1264 	munmap(results, sizeof(*results));
1265 
1266 	ksft_print_msg("%s: %u / %u tests passed.\n", ret ? "FAILED" : "PASSED",
1267 			pass_count, count);
1268 	ksft_exit(ret == 0);
1269 
1270 	/* unreachable */
1271 	return KSFT_FAIL;
1272 }
1273 
1274 static void __attribute__((constructor)) __constructor_order_first(void)
1275 {
1276 	if (!__constructor_order)
1277 		__constructor_order = _CONSTRUCTOR_ORDER_FORWARD;
1278 }
1279 
1280 #endif  /* __KSELFTEST_HARNESS_H */
1281