1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
5 * kselftest_harness.h: simple C unit test helper.
7 * See documentation in Documentation/dev-tools/kselftest.rst
9 * API inspired by code.google.com/p/googletest
17 * #include "../kselftest_harness.h"
19 * TEST(standalone_test) {
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);
27 * ASSERT_NE(some_stuff, NULL) TH_LOG("how did it happen?!");
29 * EXPECT_EQ(0, last_stuff);
32 * FIXTURE(my_fixture) {
34 * int awesomeness_level;
36 * FIXTURE_SETUP(my_fixture) {
37 * self->data = mytype_new();
38 * ASSERT_NE(NULL, self->data);
40 * FIXTURE_TEARDOWN(my_fixture) {
41 * mytype_free(self->data);
43 * TEST_F(my_fixture, data_is_good) {
44 * EXPECT_EQ(1, is_my_data_good(self->data));
50 #ifndef __KSELFTEST_HARNESS_H
51 #define __KSELFTEST_HARNESS_H
56 #include <asm/types.h>
64 #include <sys/types.h>
68 #include "kselftest.h"
70 #define TEST_TIMEOUT_DEFAULT 30
72 /* Utilities exposed to the test definitions */
74 # define TH_LOG_STREAM stderr
77 #ifndef TH_LOG_ENABLED
78 # define TH_LOG_ENABLED 1
85 * @...: optional arguments
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
95 * If no definition is provided, logging is enabled by default.
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.
105 #define TH_LOG(fmt, ...) do { \
106 if (TH_LOG_ENABLED) \
107 __TH_LOG(fmt, ##__VA_ARGS__); \
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__)
116 * SKIP(statement, fmt, ...)
118 * @statement: statement to run after reporting SKIP
119 * @fmt: format string
120 * @...: optional arguments
122 * This forces a "pass" after reporting why something is being skipped
123 * and runs "statement", which is usually "return" or "goto skip".
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); \
132 _metadata->passed = 1; \
133 _metadata->skip = 1; \
134 _metadata->trigger = 0; \
139 * TEST(test_name) - Defines the test function and creates the registration
142 * @test_name: test name
146 * TEST(name) { implementation }
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.
153 * EXPECT_* and ASSERT_* are valid in a TEST() { } context.
155 #define TEST(test_name) __TEST_IMPL(test_name, -1)
158 * TEST_SIGNAL(test_name, signal)
160 * @test_name: test name
161 * @signal: signal number
165 * TEST_SIGNAL(name, signal) { implementation }
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.
172 * EXPECT_* and ASSERT_* are valid in a TEST() { } context.
174 #define TEST_SIGNAL(test_name, signal) __TEST_IMPL(test_name, signal)
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) \
182 test_name(_metadata); \
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) \
192 __register_test(&_##test_name##_object); \
194 static void test_name( \
195 struct __test_metadata __attribute__((unused)) *_metadata)
198 * FIXTURE_DATA(datatype_name) - Wraps the struct name so we have one less
199 * argument to pass around
201 * @datatype_name: datatype name
205 * FIXTURE_DATA(datatype_name)
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.
212 #define FIXTURE_DATA(datatype_name) struct _test_data_##datatype_name
215 * FIXTURE(fixture_name) - Called once per fixture to setup the data and
218 * @fixture_name: fixture name
222 * FIXTURE(fixture_name) {
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().
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) \
237 __register_fixture(&_##fixture_name##_fixture_object); \
239 FIXTURE_DATA(fixture_name)
242 * FIXTURE_SETUP(fixture_name) - Prepares the setup function for the fixture.
243 * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly.
245 * @fixture_name: fixture name
249 * FIXTURE_SETUP(fixture_name) { implementation }
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
255 * ASSERT_* are valid for use in this context and will prempt the execution
256 * of any dependent fixture tests.
258 * A bare "return;" statement may be used to return early.
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)
268 * FIXTURE_TEARDOWN(fixture_name)
269 * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly.
271 * @fixture_name: fixture name
275 * FIXTURE_TEARDOWN(fixture_name) { implementation }
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.
281 * A bare "return;" statement may be used to return early.
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)
289 * FIXTURE_VARIANT(fixture_name) - Optionally called once per fixture
290 * to declare fixture variant
292 * @fixture_name: fixture name
296 * FIXTURE_VARIANT(fixture_name) {
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
305 #define FIXTURE_VARIANT(fixture_name) struct _fixture_variant_##fixture_name
308 * FIXTURE_VARIANT_ADD(fixture_name, variant_name) - Called once per fixture
309 * variant to setup and register the data
311 * @fixture_name: fixture name
312 * @variant_name: name of the parameter set
316 * FIXTURE_VARIANT_ADD(fixture_name, variant_name) {
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
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) \
335 __register_fixture_variant(&_##fixture_name##_fixture_object, \
336 &_##fixture_name##_##variant_name##_object); \
338 FIXTURE_VARIANT(fixture_name) \
339 _##fixture_name##_##variant_name##_variant =
342 * TEST_F(fixture_name, test_name) - Emits test registration and helpers for
343 * fixture-based test cases
345 * @fixture_name: fixture name
346 * @test_name: test name
350 * TEST_F(fixture, name) { implementation }
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.
356 * Warning: use of ASSERT_* here will skip TEARDOWN.
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)
362 #define TEST_F_SIGNAL(fixture_name, test_name, signal) \
363 __TEST_F_IMPL(fixture_name, test_name, signal, TEST_TIMEOUT_DEFAULT)
365 #define TEST_F_TIMEOUT(fixture_name, test_name, timeout) \
366 __TEST_F_IMPL(fixture_name, test_name, -1, timeout)
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) \
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) \
384 fixture_name##_##test_name(_metadata, &self, variant->data); \
385 fixture_name##_teardown(_metadata, &self); \
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, \
395 static void __attribute__((constructor)) \
396 _register_##fixture_name##_##test_name(void) \
398 __register_test(&_##fixture_name##_##test_name##_object); \
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)
407 * TEST_HARNESS_MAIN - Simple wrapper to run the test harness
413 * Use once to append a main() to the test file.
415 #define TEST_HARNESS_MAIN \
416 static void __attribute__((constructor)) \
417 __constructor_order_last(void) \
419 if (!__constructor_order) \
420 __constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; \
422 int main(int argc, char **argv) { \
423 return test_harness_run(argc, argv); \
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.
437 * @expected: expected value
438 * @seen: measured value
440 * ASSERT_EQ(expected, measured): expected == measured
442 #define ASSERT_EQ(expected, seen) \
443 __EXPECT(expected, #expected, seen, #seen, ==, 1)
448 * @expected: expected value
449 * @seen: measured value
451 * ASSERT_NE(expected, measured): expected != measured
453 #define ASSERT_NE(expected, seen) \
454 __EXPECT(expected, #expected, seen, #seen, !=, 1)
459 * @expected: expected value
460 * @seen: measured value
462 * ASSERT_LT(expected, measured): expected < measured
464 #define ASSERT_LT(expected, seen) \
465 __EXPECT(expected, #expected, seen, #seen, <, 1)
470 * @expected: expected value
471 * @seen: measured value
473 * ASSERT_LE(expected, measured): expected <= measured
475 #define ASSERT_LE(expected, seen) \
476 __EXPECT(expected, #expected, seen, #seen, <=, 1)
481 * @expected: expected value
482 * @seen: measured value
484 * ASSERT_GT(expected, measured): expected > measured
486 #define ASSERT_GT(expected, seen) \
487 __EXPECT(expected, #expected, seen, #seen, >, 1)
492 * @expected: expected value
493 * @seen: measured value
495 * ASSERT_GE(expected, measured): expected >= measured
497 #define ASSERT_GE(expected, seen) \
498 __EXPECT(expected, #expected, seen, #seen, >=, 1)
503 * @seen: measured value
505 * ASSERT_NULL(measured): NULL == measured
507 #define ASSERT_NULL(seen) \
508 __EXPECT(NULL, "NULL", seen, #seen, ==, 1)
513 * @seen: measured value
515 * ASSERT_TRUE(measured): measured != 0
517 #define ASSERT_TRUE(seen) \
518 __EXPECT(0, "0", seen, #seen, !=, 1)
523 * @seen: measured value
525 * ASSERT_FALSE(measured): measured == 0
527 #define ASSERT_FALSE(seen) \
528 __EXPECT(0, "0", seen, #seen, ==, 1)
533 * @expected: expected value
534 * @seen: measured value
536 * ASSERT_STREQ(expected, measured): !strcmp(expected, measured)
538 #define ASSERT_STREQ(expected, seen) \
539 __EXPECT_STR(expected, seen, ==, 1)
544 * @expected: expected value
545 * @seen: measured value
547 * ASSERT_STRNE(expected, measured): strcmp(expected, measured)
549 #define ASSERT_STRNE(expected, seen) \
550 __EXPECT_STR(expected, seen, !=, 1)
555 * @expected: expected value
556 * @seen: measured value
558 * EXPECT_EQ(expected, measured): expected == measured
560 #define EXPECT_EQ(expected, seen) \
561 __EXPECT(expected, #expected, seen, #seen, ==, 0)
566 * @expected: expected value
567 * @seen: measured value
569 * EXPECT_NE(expected, measured): expected != measured
571 #define EXPECT_NE(expected, seen) \
572 __EXPECT(expected, #expected, seen, #seen, !=, 0)
577 * @expected: expected value
578 * @seen: measured value
580 * EXPECT_LT(expected, measured): expected < measured
582 #define EXPECT_LT(expected, seen) \
583 __EXPECT(expected, #expected, seen, #seen, <, 0)
588 * @expected: expected value
589 * @seen: measured value
591 * EXPECT_LE(expected, measured): expected <= measured
593 #define EXPECT_LE(expected, seen) \
594 __EXPECT(expected, #expected, seen, #seen, <=, 0)
599 * @expected: expected value
600 * @seen: measured value
602 * EXPECT_GT(expected, measured): expected > measured
604 #define EXPECT_GT(expected, seen) \
605 __EXPECT(expected, #expected, seen, #seen, >, 0)
610 * @expected: expected value
611 * @seen: measured value
613 * EXPECT_GE(expected, measured): expected >= measured
615 #define EXPECT_GE(expected, seen) \
616 __EXPECT(expected, #expected, seen, #seen, >=, 0)
621 * @seen: measured value
623 * EXPECT_NULL(measured): NULL == measured
625 #define EXPECT_NULL(seen) \
626 __EXPECT(NULL, "NULL", seen, #seen, ==, 0)
631 * @seen: measured value
633 * EXPECT_TRUE(measured): 0 != measured
635 #define EXPECT_TRUE(seen) \
636 __EXPECT(0, "0", seen, #seen, !=, 0)
641 * @seen: measured value
643 * EXPECT_FALSE(measured): 0 == measured
645 #define EXPECT_FALSE(seen) \
646 __EXPECT(0, "0", seen, #seen, ==, 0)
651 * @expected: expected value
652 * @seen: measured value
654 * EXPECT_STREQ(expected, measured): !strcmp(expected, measured)
656 #define EXPECT_STREQ(expected, seen) \
657 __EXPECT_STR(expected, seen, ==, 0)
662 * @expected: expected value
663 * @seen: measured value
665 * EXPECT_STRNE(expected, measured): strcmp(expected, measured)
667 #define EXPECT_STRNE(expected, seen) \
668 __EXPECT_STR(expected, seen, !=, 0)
670 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
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.
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.
678 #define OPTIONAL_HANDLER(_assert) \
679 for (; _metadata->trigger; _metadata->trigger = \
680 __bail(_assert, _metadata->no_print, _metadata->step))
682 #define __INC_STEP(_metadata) \
683 /* Keep "step" below 255 (which is used for "SKIP" reporting). */ \
684 if (_metadata->passed && _metadata->step < 253) \
687 #define is_signed_type(var) (!!(((__typeof__(var))(-1)) < (__typeof__(var))1))
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)) { \
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); \
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); \
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); \
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); \
730 _metadata->passed = 0; \
731 /* Ensure the optional handler is triggered */ \
732 _metadata->trigger = 1; \
734 } while (0); OPTIONAL_HANDLER(_assert)
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; \
745 } while (0); OPTIONAL_HANDLER(_assert)
748 #define __LIST_APPEND(head, item) \
750 /* Circular linked list where only prev is circular. */ \
751 if (head == NULL) { \
757 if (__constructor_order == _CONSTRUCTOR_ORDER_FORWARD) { \
759 item->prev = head->prev; \
760 item->prev->next = item; \
764 item->next->prev = item; \
770 struct __test_results
{
771 char reason
[1024]; /* Reason for test result */
774 struct __test_metadata
;
775 struct __fixture_variant_metadata
;
777 /* Contains all the information about a fixture. */
778 struct __fixture_metadata
{
780 struct __test_metadata
*tests
;
781 struct __fixture_variant_metadata
*variant
;
782 struct __fixture_metadata
*prev
, *next
;
783 } _fixture_global
__attribute__((unused
)) = {
785 .prev
= &_fixture_global
,
788 static struct __fixture_metadata
*__fixture_list
= &_fixture_global
;
789 static int __constructor_order
;
791 #define _CONSTRUCTOR_ORDER_FORWARD 1
792 #define _CONSTRUCTOR_ORDER_BACKWARD -1
794 static inline void __register_fixture(struct __fixture_metadata
*f
)
796 __LIST_APPEND(__fixture_list
, f
);
799 struct __fixture_variant_metadata
{
802 struct __fixture_variant_metadata
*prev
, *next
;
806 __register_fixture_variant(struct __fixture_metadata
*f
,
807 struct __fixture_variant_metadata
*variant
)
809 __LIST_APPEND(f
->variant
, variant
);
812 /* Contains all the information for test execution and status checking. */
813 struct __test_metadata
{
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
;
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? */
826 bool no_print
; /* manual trigger when TH_LOG_STREAM is not available */
827 struct __test_results
*results
;
828 struct __test_metadata
*prev
, *next
;
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
840 static inline void __register_test(struct __test_metadata
*t
)
842 __LIST_APPEND(t
->fixture
->tests
, t
);
845 static inline int __bail(int for_realz
, bool no_print
, __u8 step
)
855 struct __test_metadata
*__active_test
;
856 static void __timeout_handler(int sig
, siginfo_t
*info
, void *ucontext
)
858 struct __test_metadata
*t
= __active_test
;
860 /* Sanity check handler execution environment. */
862 fprintf(TH_LOG_STREAM
,
863 "# no active test in SIGALRM handler!?\n");
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
);
874 kill(t
->pid
, SIGKILL
);
877 void __wait_for_test(struct __test_metadata
*t
)
879 struct sigaction action
= {
880 .sa_sigaction
= __timeout_handler
,
881 .sa_flags
= SA_SIGINFO
,
883 struct sigaction saved_action
;
886 if (sigaction(SIGALRM
, &action
, &saved_action
)) {
888 fprintf(TH_LOG_STREAM
,
889 "# %s: unable to install SIGALRM handler\n",
894 t
->timed_out
= false;
896 waitpid(t
->pid
, &status
, 0);
898 if (sigaction(SIGALRM
, &saved_action
, NULL
)) {
900 fprintf(TH_LOG_STREAM
,
901 "# %s: unable to uninstall SIGALRM handler\n",
905 __active_test
= NULL
;
909 fprintf(TH_LOG_STREAM
,
910 "# %s: Test terminated by timeout\n", t
->name
);
911 } else if (WIFEXITED(status
)) {
912 if (t
->termsig
!= -1) {
914 fprintf(TH_LOG_STREAM
,
915 "# %s: Test exited normally instead of by signal (code: %d)\n",
917 WEXITSTATUS(status
));
919 switch (WEXITSTATUS(status
)) {
929 /* Other failure, assume step report. */
932 fprintf(TH_LOG_STREAM
,
933 "# %s: Test failed at step #%d\n",
935 WEXITSTATUS(status
));
938 } else if (WIFSIGNALED(status
)) {
940 if (WTERMSIG(status
) == SIGABRT
) {
941 fprintf(TH_LOG_STREAM
,
942 "# %s: Test terminated by assertion\n",
944 } else if (WTERMSIG(status
) == t
->termsig
) {
947 fprintf(TH_LOG_STREAM
,
948 "# %s: Test terminated unexpectedly by signal %d\n",
953 fprintf(TH_LOG_STREAM
,
954 "# %s: Test ended in some other way [%u]\n",
960 void __run_test(struct __fixture_metadata
*f
,
961 struct __fixture_variant_metadata
*variant
,
962 struct __test_metadata
*t
)
964 /* reset test struct */
970 memset(t
->results
->reason
, 0, sizeof(t
->results
->reason
));
972 ksft_print_msg(" RUN %s%s%s.%s ...\n",
973 f
->name
, variant
->name
[0] ? "." : "", variant
->name
, t
->name
);
975 /* Make sure output buffers are flushed before fork */
981 ksft_print_msg("ERROR SPAWNING TEST CHILD\n");
983 } else if (t
->pid
== 0) {
990 /* Something else happened, report the step. */
995 ksft_print_msg(" %4s %s%s%s.%s\n", t
->passed
? "OK" : "FAIL",
996 f
->name
, variant
->name
[0] ? "." : "", variant
->name
, t
->name
);
999 ksft_test_result_skip("%s\n", t
->results
->reason
[0] ?
1000 t
->results
->reason
: "unknown");
1002 ksft_test_result(t
->passed
, "%s%s%s.%s\n",
1003 f
->name
, variant
->name
[0] ? "." : "", variant
->name
, t
->name
);
1006 static int test_harness_run(int __attribute__((unused
)) argc
,
1007 char __attribute__((unused
)) **argv
)
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
;
1015 unsigned int case_count
= 0, test_count
= 0;
1016 unsigned int count
= 0;
1017 unsigned int pass_count
= 0;
1019 for (f
= __fixture_list
; f
; f
= f
->next
) {
1020 for (v
= f
->variant
?: &no_variant
; v
; v
= v
->next
) {
1022 for (t
= f
->tests
; t
; t
= t
->next
)
1027 results
= mmap(NULL
, sizeof(*results
), PROT_READ
| PROT_WRITE
,
1028 MAP_SHARED
| MAP_ANONYMOUS
, -1, 0);
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
) {
1038 t
->results
= results
;
1039 __run_test(f
, v
, t
);
1048 munmap(results
, sizeof(*results
));
1050 ksft_print_msg("%s: %u / %u tests passed.\n", ret
? "FAILED" : "PASSED",
1052 ksft_exit(ret
== 0);
1058 static void __attribute__((constructor
)) __constructor_order_first(void)
1060 if (!__constructor_order
)
1061 __constructor_order
= _CONSTRUCTOR_ORDER_FORWARD
;
1064 #endif /* __KSELFTEST_HARNESS_H */