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>
69 #include "kselftest.h"
71 #define TEST_TIMEOUT_DEFAULT 30
73 /* Utilities exposed to the test definitions */
75 # define TH_LOG_STREAM stderr
78 #ifndef TH_LOG_ENABLED
79 # define TH_LOG_ENABLED 1
86 * @...: optional arguments
92 * Optional debug logging function available for use in tests.
93 * Logging may be enabled or disabled by defining TH_LOG_ENABLED.
94 * E.g., #define TH_LOG_ENABLED 1
96 * If no definition is provided, logging is enabled by default.
98 * If there is no way to print an error message for the process running the
99 * test (e.g. not allowed to write to stderr), it is still possible to get the
100 * ASSERT_* number for which the test failed. This behavior can be enabled by
101 * writing `_metadata->no_print = true;` before the check sequence that is
102 * unable to print. When an error occur, instead of printing an error message
103 * and calling `abort(3)`, the test process call `_exit(2)` with the assert
104 * number as argument, which is then printed by the parent process.
106 #define TH_LOG(fmt, ...) do { \
107 if (TH_LOG_ENABLED) \
108 __TH_LOG(fmt, ##__VA_ARGS__); \
111 /* Unconditional logger for internal use. */
112 #define __TH_LOG(fmt, ...) \
113 fprintf(TH_LOG_STREAM, "# %s:%d:%s:" fmt "\n", \
114 __FILE__, __LINE__, _metadata->name, ##__VA_ARGS__)
119 * @statement: statement to run after reporting SKIP
120 * @fmt: format string
121 * @...: optional arguments
125 * SKIP(statement, fmt, ...);
127 * This forces a "pass" after reporting why something is being skipped
128 * and runs "statement", which is usually "return" or "goto skip".
130 #define SKIP(statement, fmt, ...) do { \
131 snprintf(_metadata->results->reason, \
132 sizeof(_metadata->results->reason), fmt, ##__VA_ARGS__); \
133 if (TH_LOG_ENABLED) { \
134 fprintf(TH_LOG_STREAM, "# SKIP %s\n", \
135 _metadata->results->reason); \
137 _metadata->passed = 1; \
138 _metadata->skip = 1; \
139 _metadata->trigger = 0; \
144 * TEST() - Defines the test function and creates the registration
147 * @test_name: test name
151 * TEST(name) { implementation }
153 * Defines a test by name.
154 * Names must be unique and tests must not be run in parallel. The
155 * implementation containing block is a function and scoping should be treated
156 * as such. Returning early may be performed with a bare "return;" statement.
158 * EXPECT_* and ASSERT_* are valid in a TEST() { } context.
160 #define TEST(test_name) __TEST_IMPL(test_name, -1)
165 * @test_name: test name
166 * @signal: signal number
170 * TEST_SIGNAL(name, signal) { implementation }
172 * Defines a test by name and the expected term signal.
173 * Names must be unique and tests must not be run in parallel. The
174 * implementation containing block is a function and scoping should be treated
175 * as such. Returning early may be performed with a bare "return;" statement.
177 * EXPECT_* and ASSERT_* are valid in a TEST() { } context.
179 #define TEST_SIGNAL(test_name, signal) __TEST_IMPL(test_name, signal)
181 #define __TEST_IMPL(test_name, _signal) \
182 static void test_name(struct __test_metadata *_metadata); \
183 static inline void wrapper_##test_name( \
184 struct __test_metadata *_metadata, \
185 struct __fixture_variant_metadata *variant) \
187 _metadata->setup_completed = true; \
188 if (setjmp(_metadata->env) == 0) \
189 test_name(_metadata); \
190 __test_check_assert(_metadata); \
192 static struct __test_metadata _##test_name##_object = \
193 { .name = #test_name, \
194 .fn = &wrapper_##test_name, \
195 .fixture = &_fixture_global, \
196 .termsig = _signal, \
197 .timeout = TEST_TIMEOUT_DEFAULT, }; \
198 static void __attribute__((constructor)) _register_##test_name(void) \
200 __register_test(&_##test_name##_object); \
202 static void test_name( \
203 struct __test_metadata __attribute__((unused)) *_metadata)
206 * FIXTURE_DATA() - Wraps the struct name so we have one less
207 * argument to pass around
209 * @datatype_name: datatype name
213 * FIXTURE_DATA(datatype_name)
215 * Almost always, you want just FIXTURE() instead (see below).
216 * This call may be used when the type of the fixture data
217 * is needed. In general, this should not be needed unless
218 * the *self* is being passed to a helper directly.
220 #define FIXTURE_DATA(datatype_name) struct _test_data_##datatype_name
223 * FIXTURE() - Called once per fixture to setup the data and
226 * @fixture_name: fixture name
230 * FIXTURE(fixture_name) {
235 * Defines the data provided to TEST_F()-defined tests as *self*. It should be
236 * populated and cleaned up using FIXTURE_SETUP() and FIXTURE_TEARDOWN().
238 #define FIXTURE(fixture_name) \
239 FIXTURE_VARIANT(fixture_name); \
240 static struct __fixture_metadata _##fixture_name##_fixture_object = \
241 { .name = #fixture_name, }; \
242 static void __attribute__((constructor)) \
243 _register_##fixture_name##_data(void) \
245 __register_fixture(&_##fixture_name##_fixture_object); \
247 FIXTURE_DATA(fixture_name)
250 * FIXTURE_SETUP() - Prepares the setup function for the fixture.
251 * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly.
253 * @fixture_name: fixture name
257 * FIXTURE_SETUP(fixture_name) { implementation }
259 * Populates the required "setup" function for a fixture. An instance of the
260 * datatype defined with FIXTURE_DATA() will be exposed as *self* for the
263 * ASSERT_* are valid for use in this context and will prempt the execution
264 * of any dependent fixture tests.
266 * A bare "return;" statement may be used to return early.
268 #define FIXTURE_SETUP(fixture_name) \
269 void fixture_name##_setup( \
270 struct __test_metadata __attribute__((unused)) *_metadata, \
271 FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
272 const FIXTURE_VARIANT(fixture_name) \
273 __attribute__((unused)) *variant)
277 * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly.
279 * @fixture_name: fixture name
283 * FIXTURE_TEARDOWN(fixture_name) { implementation }
285 * Populates the required "teardown" function for a fixture. An instance of the
286 * datatype defined with FIXTURE_DATA() will be exposed as *self* for the
287 * implementation to clean up.
289 * A bare "return;" statement may be used to return early.
291 #define FIXTURE_TEARDOWN(fixture_name) \
292 void fixture_name##_teardown( \
293 struct __test_metadata __attribute__((unused)) *_metadata, \
294 FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
295 const FIXTURE_VARIANT(fixture_name) \
296 __attribute__((unused)) *variant)
299 * FIXTURE_VARIANT() - Optionally called once per fixture
300 * to declare fixture variant
302 * @fixture_name: fixture name
306 * FIXTURE_VARIANT(fixture_name) {
311 * Defines type of constant parameters provided to FIXTURE_SETUP(), TEST_F() and
312 * FIXTURE_TEARDOWN as *variant*. Variants allow the same tests to be run with
313 * different arguments.
315 #define FIXTURE_VARIANT(fixture_name) struct _fixture_variant_##fixture_name
318 * FIXTURE_VARIANT_ADD() - Called once per fixture
319 * variant to setup and register the data
321 * @fixture_name: fixture name
322 * @variant_name: name of the parameter set
326 * FIXTURE_VARIANT_ADD(fixture_name, variant_name) {
331 * Defines a variant of the test fixture, provided to FIXTURE_SETUP() and
332 * TEST_F() as *variant*. Tests of each fixture will be run once for each
335 #define FIXTURE_VARIANT_ADD(fixture_name, variant_name) \
336 extern FIXTURE_VARIANT(fixture_name) \
337 _##fixture_name##_##variant_name##_variant; \
338 static struct __fixture_variant_metadata \
339 _##fixture_name##_##variant_name##_object = \
340 { .name = #variant_name, \
341 .data = &_##fixture_name##_##variant_name##_variant}; \
342 static void __attribute__((constructor)) \
343 _register_##fixture_name##_##variant_name(void) \
345 __register_fixture_variant(&_##fixture_name##_fixture_object, \
346 &_##fixture_name##_##variant_name##_object); \
348 FIXTURE_VARIANT(fixture_name) \
349 _##fixture_name##_##variant_name##_variant =
352 * TEST_F() - Emits test registration and helpers for
353 * fixture-based test cases
355 * @fixture_name: fixture name
356 * @test_name: test name
360 * TEST_F(fixture, name) { implementation }
362 * Defines a test that depends on a fixture (e.g., is part of a test case).
363 * Very similar to TEST() except that *self* is the setup instance of fixture's
364 * datatype exposed for use by the implementation.
366 #define TEST_F(fixture_name, test_name) \
367 __TEST_F_IMPL(fixture_name, test_name, -1, TEST_TIMEOUT_DEFAULT)
369 #define TEST_F_SIGNAL(fixture_name, test_name, signal) \
370 __TEST_F_IMPL(fixture_name, test_name, signal, TEST_TIMEOUT_DEFAULT)
372 #define TEST_F_TIMEOUT(fixture_name, test_name, timeout) \
373 __TEST_F_IMPL(fixture_name, test_name, -1, timeout)
375 #define __TEST_F_IMPL(fixture_name, test_name, signal, tmout) \
376 static void fixture_name##_##test_name( \
377 struct __test_metadata *_metadata, \
378 FIXTURE_DATA(fixture_name) *self, \
379 const FIXTURE_VARIANT(fixture_name) *variant); \
380 static inline void wrapper_##fixture_name##_##test_name( \
381 struct __test_metadata *_metadata, \
382 struct __fixture_variant_metadata *variant) \
384 /* fixture data is alloced, setup, and torn down per call. */ \
385 FIXTURE_DATA(fixture_name) self; \
386 memset(&self, 0, sizeof(FIXTURE_DATA(fixture_name))); \
387 if (setjmp(_metadata->env) == 0) { \
388 fixture_name##_setup(_metadata, &self, variant->data); \
389 /* Let setup failure terminate early. */ \
390 if (!_metadata->passed) \
392 _metadata->setup_completed = true; \
393 fixture_name##_##test_name(_metadata, &self, variant->data); \
395 if (_metadata->setup_completed) \
396 fixture_name##_teardown(_metadata, &self, variant->data); \
397 __test_check_assert(_metadata); \
399 static struct __test_metadata \
400 _##fixture_name##_##test_name##_object = { \
401 .name = #test_name, \
402 .fn = &wrapper_##fixture_name##_##test_name, \
403 .fixture = &_##fixture_name##_fixture_object, \
407 static void __attribute__((constructor)) \
408 _register_##fixture_name##_##test_name(void) \
410 __register_test(&_##fixture_name##_##test_name##_object); \
412 static void fixture_name##_##test_name( \
413 struct __test_metadata __attribute__((unused)) *_metadata, \
414 FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
415 const FIXTURE_VARIANT(fixture_name) \
416 __attribute__((unused)) *variant)
419 * TEST_HARNESS_MAIN - Simple wrapper to run the test harness
425 * Use once to append a main() to the test file.
427 #define TEST_HARNESS_MAIN \
428 static void __attribute__((constructor)) \
429 __constructor_order_last(void) \
431 if (!__constructor_order) \
432 __constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; \
434 int main(int argc, char **argv) { \
435 return test_harness_run(argc, argv); \
441 * Operators for use in TEST() and TEST_F().
442 * ASSERT_* calls will stop test execution immediately.
443 * EXPECT_* calls will emit a failure warning, note it, and continue.
449 * @expected: expected value
450 * @seen: measured value
452 * ASSERT_EQ(expected, measured): expected == measured
454 #define ASSERT_EQ(expected, seen) \
455 __EXPECT(expected, #expected, seen, #seen, ==, 1)
460 * @expected: expected value
461 * @seen: measured value
463 * ASSERT_NE(expected, measured): expected != measured
465 #define ASSERT_NE(expected, seen) \
466 __EXPECT(expected, #expected, seen, #seen, !=, 1)
471 * @expected: expected value
472 * @seen: measured value
474 * ASSERT_LT(expected, measured): expected < measured
476 #define ASSERT_LT(expected, seen) \
477 __EXPECT(expected, #expected, seen, #seen, <, 1)
482 * @expected: expected value
483 * @seen: measured value
485 * ASSERT_LE(expected, measured): expected <= measured
487 #define ASSERT_LE(expected, seen) \
488 __EXPECT(expected, #expected, seen, #seen, <=, 1)
493 * @expected: expected value
494 * @seen: measured value
496 * ASSERT_GT(expected, measured): expected > measured
498 #define ASSERT_GT(expected, seen) \
499 __EXPECT(expected, #expected, seen, #seen, >, 1)
504 * @expected: expected value
505 * @seen: measured value
507 * ASSERT_GE(expected, measured): expected >= measured
509 #define ASSERT_GE(expected, seen) \
510 __EXPECT(expected, #expected, seen, #seen, >=, 1)
515 * @seen: measured value
517 * ASSERT_NULL(measured): NULL == measured
519 #define ASSERT_NULL(seen) \
520 __EXPECT(NULL, "NULL", seen, #seen, ==, 1)
525 * @seen: measured value
527 * ASSERT_TRUE(measured): measured != 0
529 #define ASSERT_TRUE(seen) \
530 __EXPECT(0, "0", seen, #seen, !=, 1)
535 * @seen: measured value
537 * ASSERT_FALSE(measured): measured == 0
539 #define ASSERT_FALSE(seen) \
540 __EXPECT(0, "0", seen, #seen, ==, 1)
545 * @expected: expected value
546 * @seen: measured value
548 * ASSERT_STREQ(expected, measured): !strcmp(expected, measured)
550 #define ASSERT_STREQ(expected, seen) \
551 __EXPECT_STR(expected, seen, ==, 1)
556 * @expected: expected value
557 * @seen: measured value
559 * ASSERT_STRNE(expected, measured): strcmp(expected, measured)
561 #define ASSERT_STRNE(expected, seen) \
562 __EXPECT_STR(expected, seen, !=, 1)
567 * @expected: expected value
568 * @seen: measured value
570 * EXPECT_EQ(expected, measured): expected == measured
572 #define EXPECT_EQ(expected, seen) \
573 __EXPECT(expected, #expected, seen, #seen, ==, 0)
578 * @expected: expected value
579 * @seen: measured value
581 * EXPECT_NE(expected, measured): expected != measured
583 #define EXPECT_NE(expected, seen) \
584 __EXPECT(expected, #expected, seen, #seen, !=, 0)
589 * @expected: expected value
590 * @seen: measured value
592 * EXPECT_LT(expected, measured): expected < measured
594 #define EXPECT_LT(expected, seen) \
595 __EXPECT(expected, #expected, seen, #seen, <, 0)
600 * @expected: expected value
601 * @seen: measured value
603 * EXPECT_LE(expected, measured): expected <= measured
605 #define EXPECT_LE(expected, seen) \
606 __EXPECT(expected, #expected, seen, #seen, <=, 0)
611 * @expected: expected value
612 * @seen: measured value
614 * EXPECT_GT(expected, measured): expected > measured
616 #define EXPECT_GT(expected, seen) \
617 __EXPECT(expected, #expected, seen, #seen, >, 0)
622 * @expected: expected value
623 * @seen: measured value
625 * EXPECT_GE(expected, measured): expected >= measured
627 #define EXPECT_GE(expected, seen) \
628 __EXPECT(expected, #expected, seen, #seen, >=, 0)
633 * @seen: measured value
635 * EXPECT_NULL(measured): NULL == measured
637 #define EXPECT_NULL(seen) \
638 __EXPECT(NULL, "NULL", seen, #seen, ==, 0)
643 * @seen: measured value
645 * EXPECT_TRUE(measured): 0 != measured
647 #define EXPECT_TRUE(seen) \
648 __EXPECT(0, "0", seen, #seen, !=, 0)
653 * @seen: measured value
655 * EXPECT_FALSE(measured): 0 == measured
657 #define EXPECT_FALSE(seen) \
658 __EXPECT(0, "0", seen, #seen, ==, 0)
663 * @expected: expected value
664 * @seen: measured value
666 * EXPECT_STREQ(expected, measured): !strcmp(expected, measured)
668 #define EXPECT_STREQ(expected, seen) \
669 __EXPECT_STR(expected, seen, ==, 0)
674 * @expected: expected value
675 * @seen: measured value
677 * EXPECT_STRNE(expected, measured): strcmp(expected, measured)
679 #define EXPECT_STRNE(expected, seen) \
680 __EXPECT_STR(expected, seen, !=, 0)
683 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
686 /* Support an optional handler after and ASSERT_* or EXPECT_*. The approach is
687 * not thread-safe, but it should be fine in most sane test scenarios.
689 * Using __bail(), which optionally abort()s, is the easiest way to early
690 * return while still providing an optional block to the API consumer.
692 #define OPTIONAL_HANDLER(_assert) \
693 for (; _metadata->trigger; _metadata->trigger = \
694 __bail(_assert, _metadata))
696 #define __INC_STEP(_metadata) \
697 /* Keep "step" below 255 (which is used for "SKIP" reporting). */ \
698 if (_metadata->passed && _metadata->step < 253) \
701 #define is_signed_type(var) (!!(((__typeof__(var))(-1)) < (__typeof__(var))1))
703 #define __EXPECT(_expected, _expected_str, _seen, _seen_str, _t, _assert) do { \
704 /* Avoid multiple evaluation of the cases */ \
705 __typeof__(_expected) __exp = (_expected); \
706 __typeof__(_seen) __seen = (_seen); \
707 if (_assert) __INC_STEP(_metadata); \
708 if (!(__exp _t __seen)) { \
709 /* Report with actual signedness to avoid weird output. */ \
710 switch (is_signed_type(__exp) * 2 + is_signed_type(__seen)) { \
712 unsigned long long __exp_print = (uintptr_t)__exp; \
713 unsigned long long __seen_print = (uintptr_t)__seen; \
714 __TH_LOG("Expected %s (%llu) %s %s (%llu)", \
715 _expected_str, __exp_print, #_t, \
716 _seen_str, __seen_print); \
720 unsigned long long __exp_print = (uintptr_t)__exp; \
721 long long __seen_print = (intptr_t)__seen; \
722 __TH_LOG("Expected %s (%llu) %s %s (%lld)", \
723 _expected_str, __exp_print, #_t, \
724 _seen_str, __seen_print); \
728 long long __exp_print = (intptr_t)__exp; \
729 unsigned long long __seen_print = (uintptr_t)__seen; \
730 __TH_LOG("Expected %s (%lld) %s %s (%llu)", \
731 _expected_str, __exp_print, #_t, \
732 _seen_str, __seen_print); \
736 long long __exp_print = (intptr_t)__exp; \
737 long long __seen_print = (intptr_t)__seen; \
738 __TH_LOG("Expected %s (%lld) %s %s (%lld)", \
739 _expected_str, __exp_print, #_t, \
740 _seen_str, __seen_print); \
744 _metadata->passed = 0; \
745 /* Ensure the optional handler is triggered */ \
746 _metadata->trigger = 1; \
748 } while (0); OPTIONAL_HANDLER(_assert)
750 #define __EXPECT_STR(_expected, _seen, _t, _assert) do { \
751 const char *__exp = (_expected); \
752 const char *__seen = (_seen); \
753 if (_assert) __INC_STEP(_metadata); \
754 if (!(strcmp(__exp, __seen) _t 0)) { \
755 __TH_LOG("Expected '%s' %s '%s'.", __exp, #_t, __seen); \
756 _metadata->passed = 0; \
757 _metadata->trigger = 1; \
759 } while (0); OPTIONAL_HANDLER(_assert)
762 #define __LIST_APPEND(head, item) \
764 /* Circular linked list where only prev is circular. */ \
765 if (head == NULL) { \
771 if (__constructor_order == _CONSTRUCTOR_ORDER_FORWARD) { \
773 item->prev = head->prev; \
774 item->prev->next = item; \
778 item->next->prev = item; \
784 struct __test_results {
785 char reason[1024]; /* Reason for test result */
788 struct __test_metadata;
789 struct __fixture_variant_metadata;
791 /* Contains all the information about a fixture. */
792 struct __fixture_metadata {
794 struct __test_metadata *tests;
795 struct __fixture_variant_metadata *variant;
796 struct __fixture_metadata *prev, *next;
797 } _fixture_global __attribute__((unused)) = {
799 .prev = &_fixture_global,
802 static struct __fixture_metadata *__fixture_list = &_fixture_global;
803 static int __constructor_order;
805 #define _CONSTRUCTOR_ORDER_FORWARD 1
806 #define _CONSTRUCTOR_ORDER_BACKWARD -1
808 static inline void __register_fixture(struct __fixture_metadata *f)
810 __LIST_APPEND(__fixture_list, f);
813 struct __fixture_variant_metadata {
816 struct __fixture_variant_metadata *prev, *next;
820 __register_fixture_variant(struct __fixture_metadata *f,
821 struct __fixture_variant_metadata *variant)
823 __LIST_APPEND(f->variant, variant);
826 /* Contains all the information for test execution and status checking. */
827 struct __test_metadata {
829 void (*fn)(struct __test_metadata *,
830 struct __fixture_variant_metadata *);
831 pid_t pid; /* pid of test when being run */
832 struct __fixture_metadata *fixture;
835 int skip; /* did SKIP get used? */
836 int trigger; /* extra handler after the evaluation */
837 int timeout; /* seconds to wait for test timeout */
838 bool timed_out; /* did this test timeout instead of exiting? */
840 bool no_print; /* manual trigger when TH_LOG_STREAM is not available */
841 bool aborted; /* stopped test due to failed ASSERT */
842 bool setup_completed; /* did setup finish? */
843 jmp_buf env; /* for exiting out of test early */
844 struct __test_results *results;
845 struct __test_metadata *prev, *next;
849 * Since constructors are called in reverse order, reverse the test
850 * list so tests are run in source declaration order.
851 * https://gcc.gnu.org/onlinedocs/gccint/Initialization.html
852 * However, it seems not all toolchains do this correctly, so use
853 * __constructor_order to detect which direction is called first
854 * and adjust list building logic to get things running in the right
857 static inline void __register_test(struct __test_metadata *t)
859 __LIST_APPEND(t->fixture->tests, t);
862 static inline int __bail(int for_realz, struct __test_metadata *t)
864 /* if this is ASSERT, return immediately. */
869 /* otherwise, end the for loop and continue. */
873 static inline void __test_check_assert(struct __test_metadata *t)
882 struct __test_metadata *__active_test;
883 static void __timeout_handler(int sig, siginfo_t *info, void *ucontext)
885 struct __test_metadata *t = __active_test;
887 /* Sanity check handler execution environment. */
889 fprintf(TH_LOG_STREAM,
890 "# no active test in SIGALRM handler!?\n");
893 if (sig != SIGALRM || sig != info->si_signo) {
894 fprintf(TH_LOG_STREAM,
895 "# %s: SIGALRM handler caught signal %d!?\n",
896 t->name, sig != SIGALRM ? sig : info->si_signo);
901 // signal process group
902 kill(-(t->pid), SIGKILL);
905 void __wait_for_test(struct __test_metadata *t)
907 struct sigaction action = {
908 .sa_sigaction = __timeout_handler,
909 .sa_flags = SA_SIGINFO,
911 struct sigaction saved_action;
914 if (sigaction(SIGALRM, &action, &saved_action)) {
916 fprintf(TH_LOG_STREAM,
917 "# %s: unable to install SIGALRM handler\n",
922 t->timed_out = false;
924 waitpid(t->pid, &status, 0);
926 if (sigaction(SIGALRM, &saved_action, NULL)) {
928 fprintf(TH_LOG_STREAM,
929 "# %s: unable to uninstall SIGALRM handler\n",
933 __active_test = NULL;
937 fprintf(TH_LOG_STREAM,
938 "# %s: Test terminated by timeout\n", t->name);
939 } else if (WIFEXITED(status)) {
940 if (t->termsig != -1) {
942 fprintf(TH_LOG_STREAM,
943 "# %s: Test exited normally instead of by signal (code: %d)\n",
945 WEXITSTATUS(status));
947 switch (WEXITSTATUS(status)) {
957 /* Other failure, assume step report. */
960 fprintf(TH_LOG_STREAM,
961 "# %s: Test failed at step #%d\n",
963 WEXITSTATUS(status));
966 } else if (WIFSIGNALED(status)) {
968 if (WTERMSIG(status) == SIGABRT) {
969 fprintf(TH_LOG_STREAM,
970 "# %s: Test terminated by assertion\n",
972 } else if (WTERMSIG(status) == t->termsig) {
975 fprintf(TH_LOG_STREAM,
976 "# %s: Test terminated unexpectedly by signal %d\n",
981 fprintf(TH_LOG_STREAM,
982 "# %s: Test ended in some other way [%u]\n",
988 void __run_test(struct __fixture_metadata *f,
989 struct __fixture_variant_metadata *variant,
990 struct __test_metadata *t)
992 /* reset test struct */
998 memset(t->results->reason, 0, sizeof(t->results->reason));
1000 ksft_print_msg(" RUN %s%s%s.%s ...\n",
1001 f->name, variant->name[0] ? "." : "", variant->name, t->name);
1003 /* Make sure output buffers are flushed before fork */
1009 ksft_print_msg("ERROR SPAWNING TEST CHILD\n");
1011 } else if (t->pid == 0) {
1016 /* Pass is exit 0 */
1019 /* Something else happened, report the step. */
1024 ksft_print_msg(" %4s %s%s%s.%s\n", t->passed ? "OK" : "FAIL",
1025 f->name, variant->name[0] ? "." : "", variant->name, t->name);
1028 ksft_test_result_skip("%s\n", t->results->reason[0] ?
1029 t->results->reason : "unknown");
1031 ksft_test_result(t->passed, "%s%s%s.%s\n",
1032 f->name, variant->name[0] ? "." : "", variant->name, t->name);
1035 static int test_harness_run(int __attribute__((unused)) argc,
1036 char __attribute__((unused)) **argv)
1038 struct __fixture_variant_metadata no_variant = { .name = "", };
1039 struct __fixture_variant_metadata *v;
1040 struct __fixture_metadata *f;
1041 struct __test_results *results;
1042 struct __test_metadata *t;
1044 unsigned int case_count = 0, test_count = 0;
1045 unsigned int count = 0;
1046 unsigned int pass_count = 0;
1048 for (f = __fixture_list; f; f = f->next) {
1049 for (v = f->variant ?: &no_variant; v; v = v->next) {
1051 for (t = f->tests; t; t = t->next)
1056 results = mmap(NULL, sizeof(*results), PROT_READ | PROT_WRITE,
1057 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1059 ksft_print_header();
1060 ksft_set_plan(test_count);
1061 ksft_print_msg("Starting %u tests from %u test cases.\n",
1062 test_count, case_count);
1063 for (f = __fixture_list; f; f = f->next) {
1064 for (v = f->variant ?: &no_variant; v; v = v->next) {
1065 for (t = f->tests; t; t = t->next) {
1067 t->results = results;
1068 __run_test(f, v, t);
1077 munmap(results, sizeof(*results));
1079 ksft_print_msg("%s: %u / %u tests passed.\n", ret ? "FAILED" : "PASSED",
1081 ksft_exit(ret == 0);
1087 static void __attribute__((constructor)) __constructor_order_first(void)
1089 if (!__constructor_order)
1090 __constructor_order = _CONSTRUCTOR_ORDER_FORWARD;
1093 #endif /* __KSELFTEST_HARNESS_H */