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