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 #define _GNU_SOURCE
54 #include <asm/types.h>
55 #include <errno.h>
56 #include <stdbool.h>
57 #include <stdint.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <sys/types.h>
62 #include <sys/wait.h>
63 #include <unistd.h>
64 
65 #define TEST_TIMEOUT_DEFAULT 30
66 
67 /* Utilities exposed to the test definitions */
68 #ifndef TH_LOG_STREAM
69 #  define TH_LOG_STREAM stderr
70 #endif
71 
72 #ifndef TH_LOG_ENABLED
73 #  define TH_LOG_ENABLED 1
74 #endif
75 
76 /**
77  * TH_LOG(fmt, ...)
78  *
79  * @fmt: format string
80  * @...: optional arguments
81  *
82  * .. code-block:: c
83  *
84  *     TH_LOG(format, ...)
85  *
86  * Optional debug logging function available for use in tests.
87  * Logging may be enabled or disabled by defining TH_LOG_ENABLED.
88  * E.g., #define TH_LOG_ENABLED 1
89  *
90  * If no definition is provided, logging is enabled by default.
91  *
92  * If there is no way to print an error message for the process running the
93  * test (e.g. not allowed to write to stderr), it is still possible to get the
94  * ASSERT_* number for which the test failed.  This behavior can be enabled by
95  * writing `_metadata->no_print = true;` before the check sequence that is
96  * unable to print.  When an error occur, instead of printing an error message
97  * and calling `abort(3)`, the test process call `_exit(2)` with the assert
98  * number as argument, which is then printed by the parent process.
99  */
100 #define TH_LOG(fmt, ...) do { \
101 	if (TH_LOG_ENABLED) \
102 		__TH_LOG(fmt, ##__VA_ARGS__); \
103 } while (0)
104 
105 /* Unconditional logger for internal use. */
106 #define __TH_LOG(fmt, ...) \
107 		fprintf(TH_LOG_STREAM, "%s:%d:%s:" fmt "\n", \
108 			__FILE__, __LINE__, _metadata->name, ##__VA_ARGS__)
109 
110 /**
111  * XFAIL(statement, fmt, ...)
112  *
113  * @statement: statement to run after reporting XFAIL
114  * @fmt: format string
115  * @...: optional arguments
116  *
117  * This forces a "pass" after reporting a failure with an XFAIL prefix,
118  * and runs "statement", which is usually "return" or "goto skip".
119  */
120 #define XFAIL(statement, fmt, ...) do { \
121 	if (TH_LOG_ENABLED) { \
122 		fprintf(TH_LOG_STREAM, "[  XFAIL!  ] " fmt "\n", \
123 			##__VA_ARGS__); \
124 	} \
125 	/* TODO: find a way to pass xfail to test runner process. */ \
126 	_metadata->passed = 1; \
127 	_metadata->trigger = 0; \
128 	statement; \
129 } while (0)
130 
131 /**
132  * TEST(test_name) - Defines the test function and creates the registration
133  * stub
134  *
135  * @test_name: test name
136  *
137  * .. code-block:: c
138  *
139  *     TEST(name) { implementation }
140  *
141  * Defines a test by name.
142  * Names must be unique and tests must not be run in parallel.  The
143  * implementation containing block is a function and scoping should be treated
144  * as such.  Returning early may be performed with a bare "return;" statement.
145  *
146  * EXPECT_* and ASSERT_* are valid in a TEST() { } context.
147  */
148 #define TEST(test_name) __TEST_IMPL(test_name, -1)
149 
150 /**
151  * TEST_SIGNAL(test_name, signal)
152  *
153  * @test_name: test name
154  * @signal: signal number
155  *
156  * .. code-block:: c
157  *
158  *     TEST_SIGNAL(name, signal) { implementation }
159  *
160  * Defines a test by name and the expected term signal.
161  * Names must be unique and tests must not be run in parallel.  The
162  * implementation containing block is a function and scoping should be treated
163  * as such.  Returning early may be performed with a bare "return;" statement.
164  *
165  * EXPECT_* and ASSERT_* are valid in a TEST() { } context.
166  */
167 #define TEST_SIGNAL(test_name, signal) __TEST_IMPL(test_name, signal)
168 
169 #define __TEST_IMPL(test_name, _signal) \
170 	static void test_name(struct __test_metadata *_metadata); \
171 	static inline void wrapper_##test_name( \
172 		struct __test_metadata *_metadata, \
173 		struct __fixture_variant_metadata *variant) \
174 	{ \
175 		test_name(_metadata); \
176 	} \
177 	static struct __test_metadata _##test_name##_object = \
178 		{ .name = #test_name, \
179 		  .fn = &wrapper_##test_name, \
180 		  .fixture = &_fixture_global, \
181 		  .termsig = _signal, \
182 		  .timeout = TEST_TIMEOUT_DEFAULT, }; \
183 	static void __attribute__((constructor)) _register_##test_name(void) \
184 	{ \
185 		__register_test(&_##test_name##_object); \
186 	} \
187 	static void test_name( \
188 		struct __test_metadata __attribute__((unused)) *_metadata)
189 
190 /**
191  * FIXTURE_DATA(datatype_name) - Wraps the struct name so we have one less
192  * argument to pass around
193  *
194  * @datatype_name: datatype name
195  *
196  * .. code-block:: c
197  *
198  *     FIXTURE_DATA(datatype_name)
199  *
200  * Almost always, you want just FIXTURE() instead (see below).
201  * This call may be used when the type of the fixture data
202  * is needed.  In general, this should not be needed unless
203  * the *self* is being passed to a helper directly.
204  */
205 #define FIXTURE_DATA(datatype_name) struct _test_data_##datatype_name
206 
207 /**
208  * FIXTURE(fixture_name) - Called once per fixture to setup the data and
209  * register
210  *
211  * @fixture_name: fixture name
212  *
213  * .. code-block:: c
214  *
215  *     FIXTURE(fixture_name) {
216  *       type property1;
217  *       ...
218  *     };
219  *
220  * Defines the data provided to TEST_F()-defined tests as *self*.  It should be
221  * populated and cleaned up using FIXTURE_SETUP() and FIXTURE_TEARDOWN().
222  */
223 #define FIXTURE(fixture_name) \
224 	FIXTURE_VARIANT(fixture_name); \
225 	static struct __fixture_metadata _##fixture_name##_fixture_object = \
226 		{ .name =  #fixture_name, }; \
227 	static void __attribute__((constructor)) \
228 	_register_##fixture_name##_data(void) \
229 	{ \
230 		__register_fixture(&_##fixture_name##_fixture_object); \
231 	} \
232 	FIXTURE_DATA(fixture_name)
233 
234 /**
235  * FIXTURE_SETUP(fixture_name) - Prepares the setup function for the fixture.
236  * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly.
237  *
238  * @fixture_name: fixture name
239  *
240  * .. code-block:: c
241  *
242  *     FIXTURE_SETUP(fixture_name) { implementation }
243  *
244  * Populates the required "setup" function for a fixture.  An instance of the
245  * datatype defined with FIXTURE_DATA() will be exposed as *self* for the
246  * implementation.
247  *
248  * ASSERT_* are valid for use in this context and will prempt the execution
249  * of any dependent fixture tests.
250  *
251  * A bare "return;" statement may be used to return early.
252  */
253 #define FIXTURE_SETUP(fixture_name) \
254 	void fixture_name##_setup( \
255 		struct __test_metadata __attribute__((unused)) *_metadata, \
256 		FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
257 		const FIXTURE_VARIANT(fixture_name) \
258 			__attribute__((unused)) *variant)
259 
260 /**
261  * FIXTURE_TEARDOWN(fixture_name)
262  * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly.
263  *
264  * @fixture_name: fixture name
265  *
266  * .. code-block:: c
267  *
268  *     FIXTURE_TEARDOWN(fixture_name) { implementation }
269  *
270  * Populates the required "teardown" function for a fixture.  An instance of the
271  * datatype defined with FIXTURE_DATA() will be exposed as *self* for the
272  * implementation to clean up.
273  *
274  * A bare "return;" statement may be used to return early.
275  */
276 #define FIXTURE_TEARDOWN(fixture_name) \
277 	void fixture_name##_teardown( \
278 		struct __test_metadata __attribute__((unused)) *_metadata, \
279 		FIXTURE_DATA(fixture_name) __attribute__((unused)) *self)
280 
281 /**
282  * FIXTURE_VARIANT(fixture_name) - Optionally called once per fixture
283  * to declare fixture variant
284  *
285  * @fixture_name: fixture name
286  *
287  * .. code-block:: c
288  *
289  *     FIXTURE_VARIANT(fixture_name) {
290  *       type property1;
291  *       ...
292  *     };
293  *
294  * Defines type of constant parameters provided to FIXTURE_SETUP() and TEST_F()
295  * as *variant*. Variants allow the same tests to be run with different
296  * arguments.
297  */
298 #define FIXTURE_VARIANT(fixture_name) struct _fixture_variant_##fixture_name
299 
300 /**
301  * FIXTURE_VARIANT_ADD(fixture_name, variant_name) - Called once per fixture
302  * variant to setup and register the data
303  *
304  * @fixture_name: fixture name
305  * @variant_name: name of the parameter set
306  *
307  * .. code-block:: c
308  *
309  *     FIXTURE_VARIANT_ADD(fixture_name, variant_name) {
310  *       .property1 = val1,
311  *       ...
312  *     };
313  *
314  * Defines a variant of the test fixture, provided to FIXTURE_SETUP() and
315  * TEST_F() as *variant*. Tests of each fixture will be run once for each
316  * variant.
317  */
318 #define FIXTURE_VARIANT_ADD(fixture_name, variant_name) \
319 	extern FIXTURE_VARIANT(fixture_name) \
320 		_##fixture_name##_##variant_name##_variant; \
321 	static struct __fixture_variant_metadata \
322 		_##fixture_name##_##variant_name##_object = \
323 		{ .name = #variant_name, \
324 		  .data = &_##fixture_name##_##variant_name##_variant}; \
325 	static void __attribute__((constructor)) \
326 		_register_##fixture_name##_##variant_name(void) \
327 	{ \
328 		__register_fixture_variant(&_##fixture_name##_fixture_object, \
329 			&_##fixture_name##_##variant_name##_object);	\
330 	} \
331 	FIXTURE_VARIANT(fixture_name) \
332 		_##fixture_name##_##variant_name##_variant =
333 
334 /**
335  * TEST_F(fixture_name, test_name) - Emits test registration and helpers for
336  * fixture-based test cases
337  *
338  * @fixture_name: fixture name
339  * @test_name: test name
340  *
341  * .. code-block:: c
342  *
343  *     TEST_F(fixture, name) { implementation }
344  *
345  * Defines a test that depends on a fixture (e.g., is part of a test case).
346  * Very similar to TEST() except that *self* is the setup instance of fixture's
347  * datatype exposed for use by the implementation.
348  *
349  * Warning: use of ASSERT_* here will skip TEARDOWN.
350  */
351 /* TODO(wad) register fixtures on dedicated test lists. */
352 #define TEST_F(fixture_name, test_name) \
353 	__TEST_F_IMPL(fixture_name, test_name, -1, TEST_TIMEOUT_DEFAULT)
354 
355 #define TEST_F_SIGNAL(fixture_name, test_name, signal) \
356 	__TEST_F_IMPL(fixture_name, test_name, signal, TEST_TIMEOUT_DEFAULT)
357 
358 #define TEST_F_TIMEOUT(fixture_name, test_name, timeout) \
359 	__TEST_F_IMPL(fixture_name, test_name, -1, timeout)
360 
361 #define __TEST_F_IMPL(fixture_name, test_name, signal, tmout) \
362 	static void fixture_name##_##test_name( \
363 		struct __test_metadata *_metadata, \
364 		FIXTURE_DATA(fixture_name) *self, \
365 		const FIXTURE_VARIANT(fixture_name) *variant); \
366 	static inline void wrapper_##fixture_name##_##test_name( \
367 		struct __test_metadata *_metadata, \
368 		struct __fixture_variant_metadata *variant) \
369 	{ \
370 		/* fixture data is alloced, setup, and torn down per call. */ \
371 		FIXTURE_DATA(fixture_name) self; \
372 		memset(&self, 0, sizeof(FIXTURE_DATA(fixture_name))); \
373 		fixture_name##_setup(_metadata, &self, variant->data); \
374 		/* Let setup failure terminate early. */ \
375 		if (!_metadata->passed) \
376 			return; \
377 		fixture_name##_##test_name(_metadata, &self, variant->data); \
378 		fixture_name##_teardown(_metadata, &self); \
379 	} \
380 	static struct __test_metadata \
381 		      _##fixture_name##_##test_name##_object = { \
382 		.name = #test_name, \
383 		.fn = &wrapper_##fixture_name##_##test_name, \
384 		.fixture = &_##fixture_name##_fixture_object, \
385 		.termsig = signal, \
386 		.timeout = tmout, \
387 	 }; \
388 	static void __attribute__((constructor)) \
389 			_register_##fixture_name##_##test_name(void) \
390 	{ \
391 		__register_test(&_##fixture_name##_##test_name##_object); \
392 	} \
393 	static void fixture_name##_##test_name( \
394 		struct __test_metadata __attribute__((unused)) *_metadata, \
395 		FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
396 		const FIXTURE_VARIANT(fixture_name) \
397 			__attribute__((unused)) *variant)
398 
399 /**
400  * TEST_HARNESS_MAIN - Simple wrapper to run the test harness
401  *
402  * .. code-block:: c
403  *
404  *     TEST_HARNESS_MAIN
405  *
406  * Use once to append a main() to the test file.
407  */
408 #define TEST_HARNESS_MAIN \
409 	static void __attribute__((constructor)) \
410 	__constructor_order_last(void) \
411 	{ \
412 		if (!__constructor_order) \
413 			__constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; \
414 	} \
415 	int main(int argc, char **argv) { \
416 		return test_harness_run(argc, argv); \
417 	}
418 
419 /**
420  * DOC: operators
421  *
422  * Operators for use in TEST() and TEST_F().
423  * ASSERT_* calls will stop test execution immediately.
424  * EXPECT_* calls will emit a failure warning, note it, and continue.
425  */
426 
427 /**
428  * ASSERT_EQ(expected, seen)
429  *
430  * @expected: expected value
431  * @seen: measured value
432  *
433  * ASSERT_EQ(expected, measured): expected == measured
434  */
435 #define ASSERT_EQ(expected, seen) \
436 	__EXPECT(expected, #expected, seen, #seen, ==, 1)
437 
438 /**
439  * ASSERT_NE(expected, seen)
440  *
441  * @expected: expected value
442  * @seen: measured value
443  *
444  * ASSERT_NE(expected, measured): expected != measured
445  */
446 #define ASSERT_NE(expected, seen) \
447 	__EXPECT(expected, #expected, seen, #seen, !=, 1)
448 
449 /**
450  * ASSERT_LT(expected, seen)
451  *
452  * @expected: expected value
453  * @seen: measured value
454  *
455  * ASSERT_LT(expected, measured): expected < measured
456  */
457 #define ASSERT_LT(expected, seen) \
458 	__EXPECT(expected, #expected, seen, #seen, <, 1)
459 
460 /**
461  * ASSERT_LE(expected, seen)
462  *
463  * @expected: expected value
464  * @seen: measured value
465  *
466  * ASSERT_LE(expected, measured): expected <= measured
467  */
468 #define ASSERT_LE(expected, seen) \
469 	__EXPECT(expected, #expected, seen, #seen, <=, 1)
470 
471 /**
472  * ASSERT_GT(expected, seen)
473  *
474  * @expected: expected value
475  * @seen: measured value
476  *
477  * ASSERT_GT(expected, measured): expected > measured
478  */
479 #define ASSERT_GT(expected, seen) \
480 	__EXPECT(expected, #expected, seen, #seen, >, 1)
481 
482 /**
483  * ASSERT_GE(expected, seen)
484  *
485  * @expected: expected value
486  * @seen: measured value
487  *
488  * ASSERT_GE(expected, measured): expected >= measured
489  */
490 #define ASSERT_GE(expected, seen) \
491 	__EXPECT(expected, #expected, seen, #seen, >=, 1)
492 
493 /**
494  * ASSERT_NULL(seen)
495  *
496  * @seen: measured value
497  *
498  * ASSERT_NULL(measured): NULL == measured
499  */
500 #define ASSERT_NULL(seen) \
501 	__EXPECT(NULL, "NULL", seen, #seen, ==, 1)
502 
503 /**
504  * ASSERT_TRUE(seen)
505  *
506  * @seen: measured value
507  *
508  * ASSERT_TRUE(measured): measured != 0
509  */
510 #define ASSERT_TRUE(seen) \
511 	__EXPECT(0, "0", seen, #seen, !=, 1)
512 
513 /**
514  * ASSERT_FALSE(seen)
515  *
516  * @seen: measured value
517  *
518  * ASSERT_FALSE(measured): measured == 0
519  */
520 #define ASSERT_FALSE(seen) \
521 	__EXPECT(0, "0", seen, #seen, ==, 1)
522 
523 /**
524  * ASSERT_STREQ(expected, seen)
525  *
526  * @expected: expected value
527  * @seen: measured value
528  *
529  * ASSERT_STREQ(expected, measured): !strcmp(expected, measured)
530  */
531 #define ASSERT_STREQ(expected, seen) \
532 	__EXPECT_STR(expected, seen, ==, 1)
533 
534 /**
535  * ASSERT_STRNE(expected, seen)
536  *
537  * @expected: expected value
538  * @seen: measured value
539  *
540  * ASSERT_STRNE(expected, measured): strcmp(expected, measured)
541  */
542 #define ASSERT_STRNE(expected, seen) \
543 	__EXPECT_STR(expected, seen, !=, 1)
544 
545 /**
546  * EXPECT_EQ(expected, seen)
547  *
548  * @expected: expected value
549  * @seen: measured value
550  *
551  * EXPECT_EQ(expected, measured): expected == measured
552  */
553 #define EXPECT_EQ(expected, seen) \
554 	__EXPECT(expected, #expected, seen, #seen, ==, 0)
555 
556 /**
557  * EXPECT_NE(expected, seen)
558  *
559  * @expected: expected value
560  * @seen: measured value
561  *
562  * EXPECT_NE(expected, measured): expected != measured
563  */
564 #define EXPECT_NE(expected, seen) \
565 	__EXPECT(expected, #expected, seen, #seen, !=, 0)
566 
567 /**
568  * EXPECT_LT(expected, seen)
569  *
570  * @expected: expected value
571  * @seen: measured value
572  *
573  * EXPECT_LT(expected, measured): expected < measured
574  */
575 #define EXPECT_LT(expected, seen) \
576 	__EXPECT(expected, #expected, seen, #seen, <, 0)
577 
578 /**
579  * EXPECT_LE(expected, seen)
580  *
581  * @expected: expected value
582  * @seen: measured value
583  *
584  * EXPECT_LE(expected, measured): expected <= measured
585  */
586 #define EXPECT_LE(expected, seen) \
587 	__EXPECT(expected, #expected, seen, #seen, <=, 0)
588 
589 /**
590  * EXPECT_GT(expected, seen)
591  *
592  * @expected: expected value
593  * @seen: measured value
594  *
595  * EXPECT_GT(expected, measured): expected > measured
596  */
597 #define EXPECT_GT(expected, seen) \
598 	__EXPECT(expected, #expected, seen, #seen, >, 0)
599 
600 /**
601  * EXPECT_GE(expected, seen)
602  *
603  * @expected: expected value
604  * @seen: measured value
605  *
606  * EXPECT_GE(expected, measured): expected >= measured
607  */
608 #define EXPECT_GE(expected, seen) \
609 	__EXPECT(expected, #expected, seen, #seen, >=, 0)
610 
611 /**
612  * EXPECT_NULL(seen)
613  *
614  * @seen: measured value
615  *
616  * EXPECT_NULL(measured): NULL == measured
617  */
618 #define EXPECT_NULL(seen) \
619 	__EXPECT(NULL, "NULL", seen, #seen, ==, 0)
620 
621 /**
622  * EXPECT_TRUE(seen)
623  *
624  * @seen: measured value
625  *
626  * EXPECT_TRUE(measured): 0 != measured
627  */
628 #define EXPECT_TRUE(seen) \
629 	__EXPECT(0, "0", seen, #seen, !=, 0)
630 
631 /**
632  * EXPECT_FALSE(seen)
633  *
634  * @seen: measured value
635  *
636  * EXPECT_FALSE(measured): 0 == measured
637  */
638 #define EXPECT_FALSE(seen) \
639 	__EXPECT(0, "0", seen, #seen, ==, 0)
640 
641 /**
642  * EXPECT_STREQ(expected, seen)
643  *
644  * @expected: expected value
645  * @seen: measured value
646  *
647  * EXPECT_STREQ(expected, measured): !strcmp(expected, measured)
648  */
649 #define EXPECT_STREQ(expected, seen) \
650 	__EXPECT_STR(expected, seen, ==, 0)
651 
652 /**
653  * EXPECT_STRNE(expected, seen)
654  *
655  * @expected: expected value
656  * @seen: measured value
657  *
658  * EXPECT_STRNE(expected, measured): strcmp(expected, measured)
659  */
660 #define EXPECT_STRNE(expected, seen) \
661 	__EXPECT_STR(expected, seen, !=, 0)
662 
663 #define ARRAY_SIZE(a)	(sizeof(a) / sizeof(a[0]))
664 
665 /* Support an optional handler after and ASSERT_* or EXPECT_*.  The approach is
666  * not thread-safe, but it should be fine in most sane test scenarios.
667  *
668  * Using __bail(), which optionally abort()s, is the easiest way to early
669  * return while still providing an optional block to the API consumer.
670  */
671 #define OPTIONAL_HANDLER(_assert) \
672 	for (; _metadata->trigger; _metadata->trigger = \
673 			__bail(_assert, _metadata->no_print, _metadata->step))
674 
675 #define __INC_STEP(_metadata) \
676 	if (_metadata->passed && _metadata->step < 255) \
677 		_metadata->step++;
678 
679 #define __EXPECT(_expected, _expected_str, _seen, _seen_str, _t, _assert) do { \
680 	/* Avoid multiple evaluation of the cases */ \
681 	__typeof__(_expected) __exp = (_expected); \
682 	__typeof__(_seen) __seen = (_seen); \
683 	if (_assert) __INC_STEP(_metadata); \
684 	if (!(__exp _t __seen)) { \
685 		unsigned long long __exp_print = (uintptr_t)__exp; \
686 		unsigned long long __seen_print = (uintptr_t)__seen; \
687 		__TH_LOG("Expected %s (%llu) %s %s (%llu)", \
688 			 _expected_str, __exp_print, #_t, \
689 			 _seen_str, __seen_print); \
690 		_metadata->passed = 0; \
691 		/* Ensure the optional handler is triggered */ \
692 		_metadata->trigger = 1; \
693 	} \
694 } while (0); OPTIONAL_HANDLER(_assert)
695 
696 #define __EXPECT_STR(_expected, _seen, _t, _assert) do { \
697 	const char *__exp = (_expected); \
698 	const char *__seen = (_seen); \
699 	if (_assert) __INC_STEP(_metadata); \
700 	if (!(strcmp(__exp, __seen) _t 0))  { \
701 		__TH_LOG("Expected '%s' %s '%s'.", __exp, #_t, __seen); \
702 		_metadata->passed = 0; \
703 		_metadata->trigger = 1; \
704 	} \
705 } while (0); OPTIONAL_HANDLER(_assert)
706 
707 /* List helpers */
708 #define __LIST_APPEND(head, item) \
709 { \
710 	/* Circular linked list where only prev is circular. */ \
711 	if (head == NULL) { \
712 		head = item; \
713 		item->next = NULL; \
714 		item->prev = item; \
715 		return;	\
716 	} \
717 	if (__constructor_order == _CONSTRUCTOR_ORDER_FORWARD) { \
718 		item->next = NULL; \
719 		item->prev = head->prev; \
720 		item->prev->next = item; \
721 		head->prev = item; \
722 	} else { \
723 		item->next = head; \
724 		item->next->prev = item; \
725 		item->prev = item; \
726 		head = item; \
727 	} \
728 }
729 
730 struct __test_metadata;
731 struct __fixture_variant_metadata;
732 
733 /* Contains all the information about a fixture. */
734 struct __fixture_metadata {
735 	const char *name;
736 	struct __test_metadata *tests;
737 	struct __fixture_variant_metadata *variant;
738 	struct __fixture_metadata *prev, *next;
739 } _fixture_global __attribute__((unused)) = {
740 	.name = "global",
741 	.prev = &_fixture_global,
742 };
743 
744 static struct __fixture_metadata *__fixture_list = &_fixture_global;
745 static int __constructor_order;
746 
747 #define _CONSTRUCTOR_ORDER_FORWARD   1
748 #define _CONSTRUCTOR_ORDER_BACKWARD -1
749 
750 static inline void __register_fixture(struct __fixture_metadata *f)
751 {
752 	__LIST_APPEND(__fixture_list, f);
753 }
754 
755 struct __fixture_variant_metadata {
756 	const char *name;
757 	const void *data;
758 	struct __fixture_variant_metadata *prev, *next;
759 };
760 
761 static inline void
762 __register_fixture_variant(struct __fixture_metadata *f,
763 			   struct __fixture_variant_metadata *variant)
764 {
765 	__LIST_APPEND(f->variant, variant);
766 }
767 
768 /* Contains all the information for test execution and status checking. */
769 struct __test_metadata {
770 	const char *name;
771 	void (*fn)(struct __test_metadata *,
772 		   struct __fixture_variant_metadata *);
773 	pid_t pid;	/* pid of test when being run */
774 	struct __fixture_metadata *fixture;
775 	int termsig;
776 	int passed;
777 	int trigger; /* extra handler after the evaluation */
778 	int timeout;	/* seconds to wait for test timeout */
779 	bool timed_out;	/* did this test timeout instead of exiting? */
780 	__u8 step;
781 	bool no_print; /* manual trigger when TH_LOG_STREAM is not available */
782 	struct __test_metadata *prev, *next;
783 };
784 
785 /*
786  * Since constructors are called in reverse order, reverse the test
787  * list so tests are run in source declaration order.
788  * https://gcc.gnu.org/onlinedocs/gccint/Initialization.html
789  * However, it seems not all toolchains do this correctly, so use
790  * __constructor_order to detect which direction is called first
791  * and adjust list building logic to get things running in the right
792  * direction.
793  */
794 static inline void __register_test(struct __test_metadata *t)
795 {
796 	__LIST_APPEND(t->fixture->tests, t);
797 }
798 
799 static inline int __bail(int for_realz, bool no_print, __u8 step)
800 {
801 	if (for_realz) {
802 		if (no_print)
803 			_exit(step);
804 		abort();
805 	}
806 	return 0;
807 }
808 
809 struct __test_metadata *__active_test;
810 static void __timeout_handler(int sig, siginfo_t *info, void *ucontext)
811 {
812 	struct __test_metadata *t = __active_test;
813 
814 	/* Sanity check handler execution environment. */
815 	if (!t) {
816 		fprintf(TH_LOG_STREAM,
817 			"no active test in SIGALRM handler!?\n");
818 		abort();
819 	}
820 	if (sig != SIGALRM || sig != info->si_signo) {
821 		fprintf(TH_LOG_STREAM,
822 			"%s: SIGALRM handler caught signal %d!?\n",
823 			t->name, sig != SIGALRM ? sig : info->si_signo);
824 		abort();
825 	}
826 
827 	t->timed_out = true;
828 	kill(t->pid, SIGKILL);
829 }
830 
831 void __wait_for_test(struct __test_metadata *t)
832 {
833 	struct sigaction action = {
834 		.sa_sigaction = __timeout_handler,
835 		.sa_flags = SA_SIGINFO,
836 	};
837 	struct sigaction saved_action;
838 	int status;
839 
840 	if (sigaction(SIGALRM, &action, &saved_action)) {
841 		t->passed = 0;
842 		fprintf(TH_LOG_STREAM,
843 			"%s: unable to install SIGALRM handler\n",
844 			t->name);
845 		return;
846 	}
847 	__active_test = t;
848 	t->timed_out = false;
849 	alarm(t->timeout);
850 	waitpid(t->pid, &status, 0);
851 	alarm(0);
852 	if (sigaction(SIGALRM, &saved_action, NULL)) {
853 		t->passed = 0;
854 		fprintf(TH_LOG_STREAM,
855 			"%s: unable to uninstall SIGALRM handler\n",
856 			t->name);
857 		return;
858 	}
859 	__active_test = NULL;
860 
861 	if (t->timed_out) {
862 		t->passed = 0;
863 		fprintf(TH_LOG_STREAM,
864 			"%s: Test terminated by timeout\n", t->name);
865 	} else if (WIFEXITED(status)) {
866 		t->passed = t->termsig == -1 ? !WEXITSTATUS(status) : 0;
867 		if (t->termsig != -1) {
868 			fprintf(TH_LOG_STREAM,
869 				"%s: Test exited normally "
870 				"instead of by signal (code: %d)\n",
871 				t->name,
872 				WEXITSTATUS(status));
873 		} else if (!t->passed) {
874 			fprintf(TH_LOG_STREAM,
875 				"%s: Test failed at step #%d\n",
876 				t->name,
877 				WEXITSTATUS(status));
878 		}
879 	} else if (WIFSIGNALED(status)) {
880 		t->passed = 0;
881 		if (WTERMSIG(status) == SIGABRT) {
882 			fprintf(TH_LOG_STREAM,
883 				"%s: Test terminated by assertion\n",
884 				t->name);
885 		} else if (WTERMSIG(status) == t->termsig) {
886 			t->passed = 1;
887 		} else {
888 			fprintf(TH_LOG_STREAM,
889 				"%s: Test terminated unexpectedly "
890 				"by signal %d\n",
891 				t->name,
892 				WTERMSIG(status));
893 		}
894 	} else {
895 		fprintf(TH_LOG_STREAM,
896 			"%s: Test ended in some other way [%u]\n",
897 			t->name,
898 			status);
899 	}
900 }
901 
902 void __run_test(struct __fixture_metadata *f,
903 		struct __fixture_variant_metadata *variant,
904 		struct __test_metadata *t)
905 {
906 	/* reset test struct */
907 	t->passed = 1;
908 	t->trigger = 0;
909 	t->step = 0;
910 	t->no_print = 0;
911 
912 	printf("[ RUN      ] %s%s%s.%s\n",
913 	       f->name, variant->name[0] ? "." : "", variant->name, t->name);
914 	t->pid = fork();
915 	if (t->pid < 0) {
916 		printf("ERROR SPAWNING TEST CHILD\n");
917 		t->passed = 0;
918 	} else if (t->pid == 0) {
919 		t->fn(t, variant);
920 		/* return the step that failed or 0 */
921 		_exit(t->passed ? 0 : t->step);
922 	} else {
923 		__wait_for_test(t);
924 	}
925 	printf("[     %4s ] %s%s%s.%s\n", (t->passed ? "OK" : "FAIL"),
926 	       f->name, variant->name[0] ? "." : "", variant->name, t->name);
927 }
928 
929 static int test_harness_run(int __attribute__((unused)) argc,
930 			    char __attribute__((unused)) **argv)
931 {
932 	struct __fixture_variant_metadata no_variant = { .name = "", };
933 	struct __fixture_variant_metadata *v;
934 	struct __fixture_metadata *f;
935 	struct __test_metadata *t;
936 	int ret = 0;
937 	unsigned int case_count = 0, test_count = 0;
938 	unsigned int count = 0;
939 	unsigned int pass_count = 0;
940 
941 	for (f = __fixture_list; f; f = f->next) {
942 		for (v = f->variant ?: &no_variant; v; v = v->next) {
943 			case_count++;
944 			for (t = f->tests; t; t = t->next)
945 				test_count++;
946 		}
947 	}
948 
949 	/* TODO(wad) add optional arguments similar to gtest. */
950 	printf("[==========] Running %u tests from %u test cases.\n",
951 	       test_count, case_count);
952 	for (f = __fixture_list; f; f = f->next) {
953 		for (v = f->variant ?: &no_variant; v; v = v->next) {
954 			for (t = f->tests; t; t = t->next) {
955 				count++;
956 				__run_test(f, v, t);
957 				if (t->passed)
958 					pass_count++;
959 				else
960 					ret = 1;
961 			}
962 		}
963 	}
964 	printf("[==========] %u / %u tests passed.\n", pass_count, count);
965 	printf("[  %s  ]\n", (ret ? "FAILED" : "PASSED"));
966 	return ret;
967 }
968 
969 static void __attribute__((constructor)) __constructor_order_first(void)
970 {
971 	if (!__constructor_order)
972 		__constructor_order = _CONSTRUCTOR_ORDER_FORWARD;
973 }
974 
975 #endif  /* __KSELFTEST_HARNESS_H */
976