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__)
118 * @statement: statement to run after reporting SKIP
119 * @fmt: format string
120 * @...: optional arguments
124 * SKIP(statement, fmt, ...);
126 * This forces a "pass" after reporting why something is being skipped
127 * and runs "statement", which is usually "return" or "goto skip".
129 #define SKIP(statement, fmt, ...) do { \
130 snprintf(_metadata->results->reason, \
131 sizeof(_metadata->results->reason), fmt, ##__VA_ARGS__); \
132 if (TH_LOG_ENABLED) { \
133 fprintf(TH_LOG_STREAM, "# SKIP %s\n", \
134 _metadata->results->reason); \
136 _metadata->passed = 1; \
137 _metadata->skip = 1; \
138 _metadata->trigger = 0; \
143 * TEST() - Defines the test function and creates the registration
146 * @test_name: test name
150 * TEST(name) { implementation }
152 * Defines a test by name.
153 * Names must be unique and tests must not be run in parallel. The
154 * implementation containing block is a function and scoping should be treated
155 * as such. Returning early may be performed with a bare "return;" statement.
157 * EXPECT_* and ASSERT_* are valid in a TEST() { } context.
159 #define TEST(test_name) __TEST_IMPL(test_name, -1)
164 * @test_name: test name
165 * @signal: signal number
169 * TEST_SIGNAL(name, signal) { implementation }
171 * Defines a test by name and the expected term signal.
172 * Names must be unique and tests must not be run in parallel. The
173 * implementation containing block is a function and scoping should be treated
174 * as such. Returning early may be performed with a bare "return;" statement.
176 * EXPECT_* and ASSERT_* are valid in a TEST() { } context.
178 #define TEST_SIGNAL(test_name, signal) __TEST_IMPL(test_name, signal)
180 #define __TEST_IMPL(test_name, _signal) \
181 static void test_name(struct __test_metadata *_metadata); \
182 static inline void wrapper_##test_name( \
183 struct __test_metadata *_metadata, \
184 struct __fixture_variant_metadata *variant) \
186 test_name(_metadata); \
188 static struct __test_metadata _##test_name##_object = \
189 { .name = #test_name, \
190 .fn = &wrapper_##test_name, \
191 .fixture = &_fixture_global, \
192 .termsig = _signal, \
193 .timeout = TEST_TIMEOUT_DEFAULT, }; \
194 static void __attribute__((constructor)) _register_##test_name(void) \
196 __register_test(&_##test_name##_object); \
198 static void test_name( \
199 struct __test_metadata __attribute__((unused)) *_metadata)
202 * FIXTURE_DATA() - Wraps the struct name so we have one less
203 * argument to pass around
205 * @datatype_name: datatype name
209 * FIXTURE_DATA(datatype_name)
211 * Almost always, you want just FIXTURE() instead (see below).
212 * This call may be used when the type of the fixture data
213 * is needed. In general, this should not be needed unless
214 * the *self* is being passed to a helper directly.
216 #define FIXTURE_DATA(datatype_name) struct _test_data_##datatype_name
219 * FIXTURE() - Called once per fixture to setup the data and
222 * @fixture_name: fixture name
226 * FIXTURE(fixture_name) {
231 * Defines the data provided to TEST_F()-defined tests as *self*. It should be
232 * populated and cleaned up using FIXTURE_SETUP() and FIXTURE_TEARDOWN().
234 #define FIXTURE(fixture_name) \
235 FIXTURE_VARIANT(fixture_name); \
236 static struct __fixture_metadata _##fixture_name##_fixture_object = \
237 { .name = #fixture_name, }; \
238 static void __attribute__((constructor)) \
239 _register_##fixture_name##_data(void) \
241 __register_fixture(&_##fixture_name##_fixture_object); \
243 FIXTURE_DATA(fixture_name)
246 * FIXTURE_SETUP() - Prepares the setup function for the fixture.
247 * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly.
249 * @fixture_name: fixture name
253 * FIXTURE_SETUP(fixture_name) { implementation }
255 * Populates the required "setup" function for a fixture. An instance of the
256 * datatype defined with FIXTURE_DATA() will be exposed as *self* for the
259 * ASSERT_* are valid for use in this context and will prempt the execution
260 * of any dependent fixture tests.
262 * A bare "return;" statement may be used to return early.
264 #define FIXTURE_SETUP(fixture_name) \
265 void fixture_name##_setup( \
266 struct __test_metadata __attribute__((unused)) *_metadata, \
267 FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
268 const FIXTURE_VARIANT(fixture_name) \
269 __attribute__((unused)) *variant)
273 * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly.
275 * @fixture_name: fixture name
279 * FIXTURE_TEARDOWN(fixture_name) { implementation }
281 * Populates the required "teardown" function for a fixture. An instance of the
282 * datatype defined with FIXTURE_DATA() will be exposed as *self* for the
283 * implementation to clean up.
285 * A bare "return;" statement may be used to return early.
287 #define FIXTURE_TEARDOWN(fixture_name) \
288 void fixture_name##_teardown( \
289 struct __test_metadata __attribute__((unused)) *_metadata, \
290 FIXTURE_DATA(fixture_name) __attribute__((unused)) *self)
293 * FIXTURE_VARIANT() - Optionally called once per fixture
294 * to declare fixture variant
296 * @fixture_name: fixture name
300 * FIXTURE_VARIANT(fixture_name) {
305 * Defines type of constant parameters provided to FIXTURE_SETUP() and TEST_F()
306 * as *variant*. Variants allow the same tests to be run with different
309 #define FIXTURE_VARIANT(fixture_name) struct _fixture_variant_##fixture_name
312 * FIXTURE_VARIANT_ADD() - Called once per fixture
313 * variant to setup and register the data
315 * @fixture_name: fixture name
316 * @variant_name: name of the parameter set
320 * FIXTURE_VARIANT_ADD(fixture_name, variant_name) {
325 * Defines a variant of the test fixture, provided to FIXTURE_SETUP() and
326 * TEST_F() as *variant*. Tests of each fixture will be run once for each
329 #define FIXTURE_VARIANT_ADD(fixture_name, variant_name) \
330 extern FIXTURE_VARIANT(fixture_name) \
331 _##fixture_name##_##variant_name##_variant; \
332 static struct __fixture_variant_metadata \
333 _##fixture_name##_##variant_name##_object = \
334 { .name = #variant_name, \
335 .data = &_##fixture_name##_##variant_name##_variant}; \
336 static void __attribute__((constructor)) \
337 _register_##fixture_name##_##variant_name(void) \
339 __register_fixture_variant(&_##fixture_name##_fixture_object, \
340 &_##fixture_name##_##variant_name##_object); \
342 FIXTURE_VARIANT(fixture_name) \
343 _##fixture_name##_##variant_name##_variant =
346 * TEST_F() - Emits test registration and helpers for
347 * fixture-based test cases
349 * @fixture_name: fixture name
350 * @test_name: test name
354 * TEST_F(fixture, name) { implementation }
356 * Defines a test that depends on a fixture (e.g., is part of a test case).
357 * Very similar to TEST() except that *self* is the setup instance of fixture's
358 * datatype exposed for use by the implementation.
360 * Warning: use of ASSERT_* here will skip TEARDOWN.
362 /* TODO(wad) register fixtures on dedicated test lists. */
363 #define TEST_F(fixture_name, test_name) \
364 __TEST_F_IMPL(fixture_name, test_name, -1, TEST_TIMEOUT_DEFAULT)
366 #define TEST_F_SIGNAL(fixture_name, test_name, signal) \
367 __TEST_F_IMPL(fixture_name, test_name, signal, TEST_TIMEOUT_DEFAULT)
369 #define TEST_F_TIMEOUT(fixture_name, test_name, timeout) \
370 __TEST_F_IMPL(fixture_name, test_name, -1, timeout)
372 #define __TEST_F_IMPL(fixture_name, test_name, signal, tmout) \
373 static void fixture_name##_##test_name( \
374 struct __test_metadata *_metadata, \
375 FIXTURE_DATA(fixture_name) *self, \
376 const FIXTURE_VARIANT(fixture_name) *variant); \
377 static inline void wrapper_##fixture_name##_##test_name( \
378 struct __test_metadata *_metadata, \
379 struct __fixture_variant_metadata *variant) \
381 /* fixture data is alloced, setup, and torn down per call. */ \
382 FIXTURE_DATA(fixture_name) self; \
383 memset(&self, 0, sizeof(FIXTURE_DATA(fixture_name))); \
384 fixture_name##_setup(_metadata, &self, variant->data); \
385 /* Let setup failure terminate early. */ \
386 if (!_metadata->passed) \
388 fixture_name##_##test_name(_metadata, &self, variant->data); \
389 fixture_name##_teardown(_metadata, &self); \
391 static struct __test_metadata \
392 _##fixture_name##_##test_name##_object = { \
393 .name = #test_name, \
394 .fn = &wrapper_##fixture_name##_##test_name, \
395 .fixture = &_##fixture_name##_fixture_object, \
399 static void __attribute__((constructor)) \
400 _register_##fixture_name##_##test_name(void) \
402 __register_test(&_##fixture_name##_##test_name##_object); \
404 static void fixture_name##_##test_name( \
405 struct __test_metadata __attribute__((unused)) *_metadata, \
406 FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
407 const FIXTURE_VARIANT(fixture_name) \
408 __attribute__((unused)) *variant)
411 * TEST_HARNESS_MAIN - Simple wrapper to run the test harness
417 * Use once to append a main() to the test file.
419 #define TEST_HARNESS_MAIN \
420 static void __attribute__((constructor)) \
421 __constructor_order_last(void) \
423 if (!__constructor_order) \
424 __constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; \
426 int main(int argc, char **argv) { \
427 return test_harness_run(argc, argv); \
433 * Operators for use in TEST() and TEST_F().
434 * ASSERT_* calls will stop test execution immediately.
435 * EXPECT_* calls will emit a failure warning, note it, and continue.
441 * @expected: expected value
442 * @seen: measured value
444 * ASSERT_EQ(expected, measured): expected == measured
446 #define ASSERT_EQ(expected, seen) \
447 __EXPECT(expected, #expected, seen, #seen, ==, 1)
452 * @expected: expected value
453 * @seen: measured value
455 * ASSERT_NE(expected, measured): expected != measured
457 #define ASSERT_NE(expected, seen) \
458 __EXPECT(expected, #expected, seen, #seen, !=, 1)
463 * @expected: expected value
464 * @seen: measured value
466 * ASSERT_LT(expected, measured): expected < measured
468 #define ASSERT_LT(expected, seen) \
469 __EXPECT(expected, #expected, seen, #seen, <, 1)
474 * @expected: expected value
475 * @seen: measured value
477 * ASSERT_LE(expected, measured): expected <= measured
479 #define ASSERT_LE(expected, seen) \
480 __EXPECT(expected, #expected, seen, #seen, <=, 1)
485 * @expected: expected value
486 * @seen: measured value
488 * ASSERT_GT(expected, measured): expected > measured
490 #define ASSERT_GT(expected, seen) \
491 __EXPECT(expected, #expected, seen, #seen, >, 1)
496 * @expected: expected value
497 * @seen: measured value
499 * ASSERT_GE(expected, measured): expected >= measured
501 #define ASSERT_GE(expected, seen) \
502 __EXPECT(expected, #expected, seen, #seen, >=, 1)
507 * @seen: measured value
509 * ASSERT_NULL(measured): NULL == measured
511 #define ASSERT_NULL(seen) \
512 __EXPECT(NULL, "NULL", seen, #seen, ==, 1)
517 * @seen: measured value
519 * ASSERT_TRUE(measured): measured != 0
521 #define ASSERT_TRUE(seen) \
522 __EXPECT(0, "0", seen, #seen, !=, 1)
527 * @seen: measured value
529 * ASSERT_FALSE(measured): measured == 0
531 #define ASSERT_FALSE(seen) \
532 __EXPECT(0, "0", seen, #seen, ==, 1)
537 * @expected: expected value
538 * @seen: measured value
540 * ASSERT_STREQ(expected, measured): !strcmp(expected, measured)
542 #define ASSERT_STREQ(expected, seen) \
543 __EXPECT_STR(expected, seen, ==, 1)
548 * @expected: expected value
549 * @seen: measured value
551 * ASSERT_STRNE(expected, measured): strcmp(expected, measured)
553 #define ASSERT_STRNE(expected, seen) \
554 __EXPECT_STR(expected, seen, !=, 1)
559 * @expected: expected value
560 * @seen: measured value
562 * EXPECT_EQ(expected, measured): expected == measured
564 #define EXPECT_EQ(expected, seen) \
565 __EXPECT(expected, #expected, seen, #seen, ==, 0)
570 * @expected: expected value
571 * @seen: measured value
573 * EXPECT_NE(expected, measured): expected != measured
575 #define EXPECT_NE(expected, seen) \
576 __EXPECT(expected, #expected, seen, #seen, !=, 0)
581 * @expected: expected value
582 * @seen: measured value
584 * EXPECT_LT(expected, measured): expected < measured
586 #define EXPECT_LT(expected, seen) \
587 __EXPECT(expected, #expected, seen, #seen, <, 0)
592 * @expected: expected value
593 * @seen: measured value
595 * EXPECT_LE(expected, measured): expected <= measured
597 #define EXPECT_LE(expected, seen) \
598 __EXPECT(expected, #expected, seen, #seen, <=, 0)
603 * @expected: expected value
604 * @seen: measured value
606 * EXPECT_GT(expected, measured): expected > measured
608 #define EXPECT_GT(expected, seen) \
609 __EXPECT(expected, #expected, seen, #seen, >, 0)
614 * @expected: expected value
615 * @seen: measured value
617 * EXPECT_GE(expected, measured): expected >= measured
619 #define EXPECT_GE(expected, seen) \
620 __EXPECT(expected, #expected, seen, #seen, >=, 0)
625 * @seen: measured value
627 * EXPECT_NULL(measured): NULL == measured
629 #define EXPECT_NULL(seen) \
630 __EXPECT(NULL, "NULL", seen, #seen, ==, 0)
635 * @seen: measured value
637 * EXPECT_TRUE(measured): 0 != measured
639 #define EXPECT_TRUE(seen) \
640 __EXPECT(0, "0", seen, #seen, !=, 0)
645 * @seen: measured value
647 * EXPECT_FALSE(measured): 0 == measured
649 #define EXPECT_FALSE(seen) \
650 __EXPECT(0, "0", seen, #seen, ==, 0)
655 * @expected: expected value
656 * @seen: measured value
658 * EXPECT_STREQ(expected, measured): !strcmp(expected, measured)
660 #define EXPECT_STREQ(expected, seen) \
661 __EXPECT_STR(expected, seen, ==, 0)
666 * @expected: expected value
667 * @seen: measured value
669 * EXPECT_STRNE(expected, measured): strcmp(expected, measured)
671 #define EXPECT_STRNE(expected, seen) \
672 __EXPECT_STR(expected, seen, !=, 0)
675 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
678 /* Support an optional handler after and ASSERT_* or EXPECT_*. The approach is
679 * not thread-safe, but it should be fine in most sane test scenarios.
681 * Using __bail(), which optionally abort()s, is the easiest way to early
682 * return while still providing an optional block to the API consumer.
684 #define OPTIONAL_HANDLER(_assert) \
685 for (; _metadata->trigger; _metadata->trigger = \
686 __bail(_assert, _metadata->no_print, _metadata->step))
688 #define __INC_STEP(_metadata) \
689 /* Keep "step" below 255 (which is used for "SKIP" reporting). */ \
690 if (_metadata->passed && _metadata->step < 253) \
693 #define is_signed_type(var) (!!(((__typeof__(var))(-1)) < (__typeof__(var))1))
695 #define __EXPECT(_expected, _expected_str, _seen, _seen_str, _t, _assert) do { \
696 /* Avoid multiple evaluation of the cases */ \
697 __typeof__(_expected) __exp = (_expected); \
698 __typeof__(_seen) __seen = (_seen); \
699 if (_assert) __INC_STEP(_metadata); \
700 if (!(__exp _t __seen)) { \
701 /* Report with actual signedness to avoid weird output. */ \
702 switch (is_signed_type(__exp) * 2 + is_signed_type(__seen)) { \
704 unsigned long long __exp_print = (uintptr_t)__exp; \
705 unsigned long long __seen_print = (uintptr_t)__seen; \
706 __TH_LOG("Expected %s (%llu) %s %s (%llu)", \
707 _expected_str, __exp_print, #_t, \
708 _seen_str, __seen_print); \
712 unsigned long long __exp_print = (uintptr_t)__exp; \
713 long long __seen_print = (intptr_t)__seen; \
714 __TH_LOG("Expected %s (%llu) %s %s (%lld)", \
715 _expected_str, __exp_print, #_t, \
716 _seen_str, __seen_print); \
720 long long __exp_print = (intptr_t)__exp; \
721 unsigned long long __seen_print = (uintptr_t)__seen; \
722 __TH_LOG("Expected %s (%lld) %s %s (%llu)", \
723 _expected_str, __exp_print, #_t, \
724 _seen_str, __seen_print); \
728 long long __exp_print = (intptr_t)__exp; \
729 long long __seen_print = (intptr_t)__seen; \
730 __TH_LOG("Expected %s (%lld) %s %s (%lld)", \
731 _expected_str, __exp_print, #_t, \
732 _seen_str, __seen_print); \
736 _metadata->passed = 0; \
737 /* Ensure the optional handler is triggered */ \
738 _metadata->trigger = 1; \
740 } while (0); OPTIONAL_HANDLER(_assert)
742 #define __EXPECT_STR(_expected, _seen, _t, _assert) do { \
743 const char *__exp = (_expected); \
744 const char *__seen = (_seen); \
745 if (_assert) __INC_STEP(_metadata); \
746 if (!(strcmp(__exp, __seen) _t 0)) { \
747 __TH_LOG("Expected '%s' %s '%s'.", __exp, #_t, __seen); \
748 _metadata->passed = 0; \
749 _metadata->trigger = 1; \
751 } while (0); OPTIONAL_HANDLER(_assert)
754 #define __LIST_APPEND(head, item) \
756 /* Circular linked list where only prev is circular. */ \
757 if (head == NULL) { \
763 if (__constructor_order == _CONSTRUCTOR_ORDER_FORWARD) { \
765 item->prev = head->prev; \
766 item->prev->next = item; \
770 item->next->prev = item; \
776 struct __test_results {
777 char reason[1024]; /* Reason for test result */
780 struct __test_metadata;
781 struct __fixture_variant_metadata;
783 /* Contains all the information about a fixture. */
784 struct __fixture_metadata {
786 struct __test_metadata *tests;
787 struct __fixture_variant_metadata *variant;
788 struct __fixture_metadata *prev, *next;
789 } _fixture_global __attribute__((unused)) = {
791 .prev = &_fixture_global,
794 static struct __fixture_metadata *__fixture_list = &_fixture_global;
795 static int __constructor_order;
797 #define _CONSTRUCTOR_ORDER_FORWARD 1
798 #define _CONSTRUCTOR_ORDER_BACKWARD -1
800 static inline void __register_fixture(struct __fixture_metadata *f)
802 __LIST_APPEND(__fixture_list, f);
805 struct __fixture_variant_metadata {
808 struct __fixture_variant_metadata *prev, *next;
812 __register_fixture_variant(struct __fixture_metadata *f,
813 struct __fixture_variant_metadata *variant)
815 __LIST_APPEND(f->variant, variant);
818 /* Contains all the information for test execution and status checking. */
819 struct __test_metadata {
821 void (*fn)(struct __test_metadata *,
822 struct __fixture_variant_metadata *);
823 pid_t pid; /* pid of test when being run */
824 struct __fixture_metadata *fixture;
827 int skip; /* did SKIP get used? */
828 int trigger; /* extra handler after the evaluation */
829 int timeout; /* seconds to wait for test timeout */
830 bool timed_out; /* did this test timeout instead of exiting? */
832 bool no_print; /* manual trigger when TH_LOG_STREAM is not available */
833 struct __test_results *results;
834 struct __test_metadata *prev, *next;
838 * Since constructors are called in reverse order, reverse the test
839 * list so tests are run in source declaration order.
840 * https://gcc.gnu.org/onlinedocs/gccint/Initialization.html
841 * However, it seems not all toolchains do this correctly, so use
842 * __constructor_order to detect which direction is called first
843 * and adjust list building logic to get things running in the right
846 static inline void __register_test(struct __test_metadata *t)
848 __LIST_APPEND(t->fixture->tests, t);
851 static inline int __bail(int for_realz, bool no_print, __u8 step)
861 struct __test_metadata *__active_test;
862 static void __timeout_handler(int sig, siginfo_t *info, void *ucontext)
864 struct __test_metadata *t = __active_test;
866 /* Sanity check handler execution environment. */
868 fprintf(TH_LOG_STREAM,
869 "# no active test in SIGALRM handler!?\n");
872 if (sig != SIGALRM || sig != info->si_signo) {
873 fprintf(TH_LOG_STREAM,
874 "# %s: SIGALRM handler caught signal %d!?\n",
875 t->name, sig != SIGALRM ? sig : info->si_signo);
880 // signal process group
881 kill(-(t->pid), SIGKILL);
884 void __wait_for_test(struct __test_metadata *t)
886 struct sigaction action = {
887 .sa_sigaction = __timeout_handler,
888 .sa_flags = SA_SIGINFO,
890 struct sigaction saved_action;
893 if (sigaction(SIGALRM, &action, &saved_action)) {
895 fprintf(TH_LOG_STREAM,
896 "# %s: unable to install SIGALRM handler\n",
901 t->timed_out = false;
903 waitpid(t->pid, &status, 0);
905 if (sigaction(SIGALRM, &saved_action, NULL)) {
907 fprintf(TH_LOG_STREAM,
908 "# %s: unable to uninstall SIGALRM handler\n",
912 __active_test = NULL;
916 fprintf(TH_LOG_STREAM,
917 "# %s: Test terminated by timeout\n", t->name);
918 } else if (WIFEXITED(status)) {
919 if (t->termsig != -1) {
921 fprintf(TH_LOG_STREAM,
922 "# %s: Test exited normally instead of by signal (code: %d)\n",
924 WEXITSTATUS(status));
926 switch (WEXITSTATUS(status)) {
936 /* Other failure, assume step report. */
939 fprintf(TH_LOG_STREAM,
940 "# %s: Test failed at step #%d\n",
942 WEXITSTATUS(status));
945 } else if (WIFSIGNALED(status)) {
947 if (WTERMSIG(status) == SIGABRT) {
948 fprintf(TH_LOG_STREAM,
949 "# %s: Test terminated by assertion\n",
951 } else if (WTERMSIG(status) == t->termsig) {
954 fprintf(TH_LOG_STREAM,
955 "# %s: Test terminated unexpectedly by signal %d\n",
960 fprintf(TH_LOG_STREAM,
961 "# %s: Test ended in some other way [%u]\n",
967 void __run_test(struct __fixture_metadata *f,
968 struct __fixture_variant_metadata *variant,
969 struct __test_metadata *t)
971 /* reset test struct */
977 memset(t->results->reason, 0, sizeof(t->results->reason));
979 ksft_print_msg(" RUN %s%s%s.%s ...\n",
980 f->name, variant->name[0] ? "." : "", variant->name, t->name);
982 /* Make sure output buffers are flushed before fork */
988 ksft_print_msg("ERROR SPAWNING TEST CHILD\n");
990 } else if (t->pid == 0) {
998 /* Something else happened, report the step. */
1003 ksft_print_msg(" %4s %s%s%s.%s\n", t->passed ? "OK" : "FAIL",
1004 f->name, variant->name[0] ? "." : "", variant->name, t->name);
1007 ksft_test_result_skip("%s\n", t->results->reason[0] ?
1008 t->results->reason : "unknown");
1010 ksft_test_result(t->passed, "%s%s%s.%s\n",
1011 f->name, variant->name[0] ? "." : "", variant->name, t->name);
1014 static int test_harness_run(int __attribute__((unused)) argc,
1015 char __attribute__((unused)) **argv)
1017 struct __fixture_variant_metadata no_variant = { .name = "", };
1018 struct __fixture_variant_metadata *v;
1019 struct __fixture_metadata *f;
1020 struct __test_results *results;
1021 struct __test_metadata *t;
1023 unsigned int case_count = 0, test_count = 0;
1024 unsigned int count = 0;
1025 unsigned int pass_count = 0;
1027 for (f = __fixture_list; f; f = f->next) {
1028 for (v = f->variant ?: &no_variant; v; v = v->next) {
1030 for (t = f->tests; t; t = t->next)
1035 results = mmap(NULL, sizeof(*results), PROT_READ | PROT_WRITE,
1036 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1038 ksft_print_header();
1039 ksft_set_plan(test_count);
1040 ksft_print_msg("Starting %u tests from %u test cases.\n",
1041 test_count, case_count);
1042 for (f = __fixture_list; f; f = f->next) {
1043 for (v = f->variant ?: &no_variant; v; v = v->next) {
1044 for (t = f->tests; t; t = t->next) {
1046 t->results = results;
1047 __run_test(f, v, t);
1056 munmap(results, sizeof(*results));
1058 ksft_print_msg("%s: %u / %u tests passed.\n", ret ? "FAILED" : "PASSED",
1060 ksft_exit(ret == 0);
1066 static void __attribute__((constructor)) __constructor_order_first(void)
1068 if (!__constructor_order)
1069 __constructor_order = _CONSTRUCTOR_ORDER_FORWARD;
1072 #endif /* __KSELFTEST_HARNESS_H */