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>
63 #include <sys/types.h>
67 #include "kselftest.h"
69 #define TEST_TIMEOUT_DEFAULT 30
71 /* Utilities exposed to the test definitions */
73 # define TH_LOG_STREAM stderr
76 #ifndef TH_LOG_ENABLED
77 # define TH_LOG_ENABLED 1
84 * @...: optional arguments
90 * Optional debug logging function available for use in tests.
91 * Logging may be enabled or disabled by defining TH_LOG_ENABLED.
92 * E.g., #define TH_LOG_ENABLED 1
94 * If no definition is provided, logging is enabled by default.
96 * If there is no way to print an error message for the process running the
97 * test (e.g. not allowed to write to stderr), it is still possible to get the
98 * ASSERT_* number for which the test failed. This behavior can be enabled by
99 * writing `_metadata->no_print = true;` before the check sequence that is
100 * unable to print. When an error occur, instead of printing an error message
101 * and calling `abort(3)`, the test process call `_exit(2)` with the assert
102 * number as argument, which is then printed by the parent process.
104 #define TH_LOG(fmt, ...) do { \
105 if (TH_LOG_ENABLED) \
106 __TH_LOG(fmt, ##__VA_ARGS__); \
109 /* Unconditional logger for internal use. */
110 #define __TH_LOG(fmt, ...) \
111 fprintf(TH_LOG_STREAM, "# %s:%d:%s:" fmt "\n", \
112 __FILE__, __LINE__, _metadata->name, ##__VA_ARGS__)
115 * SKIP(statement, fmt, ...)
117 * @statement: statement to run after reporting SKIP
118 * @fmt: format string
119 * @...: optional arguments
121 * This forces a "pass" after reporting why something is being skipped
122 * and runs "statement", which is usually "return" or "goto skip".
124 #define SKIP(statement, fmt, ...) do { \
125 if (TH_LOG_ENABLED) { \
126 fprintf(TH_LOG_STREAM, "# SKIP " fmt "\n", \
129 _metadata->passed = 1; \
130 _metadata->skip = 1; \
131 _metadata->trigger = 0; \
136 * TEST(test_name) - Defines the test function and creates the registration
139 * @test_name: test name
143 * TEST(name) { implementation }
145 * Defines a test by name.
146 * Names must be unique and tests must not be run in parallel. The
147 * implementation containing block is a function and scoping should be treated
148 * as such. Returning early may be performed with a bare "return;" statement.
150 * EXPECT_* and ASSERT_* are valid in a TEST() { } context.
152 #define TEST(test_name) __TEST_IMPL(test_name, -1)
155 * TEST_SIGNAL(test_name, signal)
157 * @test_name: test name
158 * @signal: signal number
162 * TEST_SIGNAL(name, signal) { implementation }
164 * Defines a test by name and the expected term signal.
165 * Names must be unique and tests must not be run in parallel. The
166 * implementation containing block is a function and scoping should be treated
167 * as such. Returning early may be performed with a bare "return;" statement.
169 * EXPECT_* and ASSERT_* are valid in a TEST() { } context.
171 #define TEST_SIGNAL(test_name, signal) __TEST_IMPL(test_name, signal)
173 #define __TEST_IMPL(test_name, _signal) \
174 static void test_name(struct __test_metadata *_metadata); \
175 static inline void wrapper_##test_name( \
176 struct __test_metadata *_metadata, \
177 struct __fixture_variant_metadata *variant) \
179 test_name(_metadata); \
181 static struct __test_metadata _##test_name##_object = \
182 { .name = #test_name, \
183 .fn = &wrapper_##test_name, \
184 .fixture = &_fixture_global, \
185 .termsig = _signal, \
186 .timeout = TEST_TIMEOUT_DEFAULT, }; \
187 static void __attribute__((constructor)) _register_##test_name(void) \
189 __register_test(&_##test_name##_object); \
191 static void test_name( \
192 struct __test_metadata __attribute__((unused)) *_metadata)
195 * FIXTURE_DATA(datatype_name) - Wraps the struct name so we have one less
196 * argument to pass around
198 * @datatype_name: datatype name
202 * FIXTURE_DATA(datatype name)
204 * This call may be used when the type of the fixture data
205 * is needed. In general, this should not be needed unless
206 * the *self* is being passed to a helper directly.
208 #define FIXTURE_DATA(datatype_name) struct _test_data_##datatype_name
211 * FIXTURE(fixture_name) - Called once per fixture to setup the data and
214 * @fixture_name: fixture name
218 * FIXTURE(datatype name) {
223 * Defines the data provided to TEST_F()-defined tests as *self*. It should be
224 * populated and cleaned up using FIXTURE_SETUP() and FIXTURE_TEARDOWN().
226 #define FIXTURE(fixture_name) \
227 FIXTURE_VARIANT(fixture_name); \
228 static struct __fixture_metadata _##fixture_name##_fixture_object = \
229 { .name = #fixture_name, }; \
230 static void __attribute__((constructor)) \
231 _register_##fixture_name##_data(void) \
233 __register_fixture(&_##fixture_name##_fixture_object); \
235 FIXTURE_DATA(fixture_name)
238 * FIXTURE_SETUP(fixture_name) - Prepares the setup function for the fixture.
239 * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly.
241 * @fixture_name: fixture name
245 * FIXTURE_SETUP(fixture name) { implementation }
247 * Populates the required "setup" function for a fixture. An instance of the
248 * datatype defined with FIXTURE_DATA() will be exposed as *self* for the
251 * ASSERT_* are valid for use in this context and will prempt the execution
252 * of any dependent fixture tests.
254 * A bare "return;" statement may be used to return early.
256 #define FIXTURE_SETUP(fixture_name) \
257 void fixture_name##_setup( \
258 struct __test_metadata __attribute__((unused)) *_metadata, \
259 FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
260 const FIXTURE_VARIANT(fixture_name) \
261 __attribute__((unused)) *variant)
264 * FIXTURE_TEARDOWN(fixture_name)
265 * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly.
267 * @fixture_name: fixture name
271 * FIXTURE_TEARDOWN(fixture name) { implementation }
273 * Populates the required "teardown" function for a fixture. An instance of the
274 * datatype defined with FIXTURE_DATA() will be exposed as *self* for the
275 * implementation to clean up.
277 * A bare "return;" statement may be used to return early.
279 #define FIXTURE_TEARDOWN(fixture_name) \
280 void fixture_name##_teardown( \
281 struct __test_metadata __attribute__((unused)) *_metadata, \
282 FIXTURE_DATA(fixture_name) __attribute__((unused)) *self)
285 * FIXTURE_VARIANT(fixture_name) - Optionally called once per fixture
286 * to declare fixture variant
288 * @fixture_name: fixture name
292 * FIXTURE_VARIANT(datatype name) {
297 * Defines type of constant parameters provided to FIXTURE_SETUP() and TEST_F()
298 * as *variant*. Variants allow the same tests to be run with different
301 #define FIXTURE_VARIANT(fixture_name) struct _fixture_variant_##fixture_name
304 * FIXTURE_VARIANT_ADD(fixture_name, variant_name) - Called once per fixture
305 * variant to setup and register the data
307 * @fixture_name: fixture name
308 * @variant_name: name of the parameter set
312 * FIXTURE_ADD(datatype name) {
317 * Defines a variant of the test fixture, provided to FIXTURE_SETUP() and
318 * TEST_F() as *variant*. Tests of each fixture will be run once for each
321 #define FIXTURE_VARIANT_ADD(fixture_name, variant_name) \
322 extern FIXTURE_VARIANT(fixture_name) \
323 _##fixture_name##_##variant_name##_variant; \
324 static struct __fixture_variant_metadata \
325 _##fixture_name##_##variant_name##_object = \
326 { .name = #variant_name, \
327 .data = &_##fixture_name##_##variant_name##_variant}; \
328 static void __attribute__((constructor)) \
329 _register_##fixture_name##_##variant_name(void) \
331 __register_fixture_variant(&_##fixture_name##_fixture_object, \
332 &_##fixture_name##_##variant_name##_object); \
334 FIXTURE_VARIANT(fixture_name) \
335 _##fixture_name##_##variant_name##_variant =
338 * TEST_F(fixture_name, test_name) - Emits test registration and helpers for
339 * fixture-based test cases
341 * @fixture_name: fixture name
342 * @test_name: test name
346 * TEST_F(fixture, name) { implementation }
348 * Defines a test that depends on a fixture (e.g., is part of a test case).
349 * Very similar to TEST() except that *self* is the setup instance of fixture's
350 * datatype exposed for use by the implementation.
352 * Warning: use of ASSERT_* here will skip TEARDOWN.
354 /* TODO(wad) register fixtures on dedicated test lists. */
355 #define TEST_F(fixture_name, test_name) \
356 __TEST_F_IMPL(fixture_name, test_name, -1, TEST_TIMEOUT_DEFAULT)
358 #define TEST_F_SIGNAL(fixture_name, test_name, signal) \
359 __TEST_F_IMPL(fixture_name, test_name, signal, TEST_TIMEOUT_DEFAULT)
361 #define TEST_F_TIMEOUT(fixture_name, test_name, timeout) \
362 __TEST_F_IMPL(fixture_name, test_name, -1, timeout)
364 #define __TEST_F_IMPL(fixture_name, test_name, signal, tmout) \
365 static void fixture_name##_##test_name( \
366 struct __test_metadata *_metadata, \
367 FIXTURE_DATA(fixture_name) *self, \
368 const FIXTURE_VARIANT(fixture_name) *variant); \
369 static inline void wrapper_##fixture_name##_##test_name( \
370 struct __test_metadata *_metadata, \
371 struct __fixture_variant_metadata *variant) \
373 /* fixture data is alloced, setup, and torn down per call. */ \
374 FIXTURE_DATA(fixture_name) self; \
375 memset(&self, 0, sizeof(FIXTURE_DATA(fixture_name))); \
376 fixture_name##_setup(_metadata, &self, variant->data); \
377 /* Let setup failure terminate early. */ \
378 if (!_metadata->passed) \
380 fixture_name##_##test_name(_metadata, &self, variant->data); \
381 fixture_name##_teardown(_metadata, &self); \
383 static struct __test_metadata \
384 _##fixture_name##_##test_name##_object = { \
385 .name = #test_name, \
386 .fn = &wrapper_##fixture_name##_##test_name, \
387 .fixture = &_##fixture_name##_fixture_object, \
391 static void __attribute__((constructor)) \
392 _register_##fixture_name##_##test_name(void) \
394 __register_test(&_##fixture_name##_##test_name##_object); \
396 static void fixture_name##_##test_name( \
397 struct __test_metadata __attribute__((unused)) *_metadata, \
398 FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
399 const FIXTURE_VARIANT(fixture_name) \
400 __attribute__((unused)) *variant)
403 * TEST_HARNESS_MAIN - Simple wrapper to run the test harness
409 * Use once to append a main() to the test file.
411 #define TEST_HARNESS_MAIN \
412 static void __attribute__((constructor)) \
413 __constructor_order_last(void) \
415 if (!__constructor_order) \
416 __constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; \
418 int main(int argc, char **argv) { \
419 return test_harness_run(argc, argv); \
425 * Operators for use in TEST() and TEST_F().
426 * ASSERT_* calls will stop test execution immediately.
427 * EXPECT_* calls will emit a failure warning, note it, and continue.
431 * ASSERT_EQ(expected, seen)
433 * @expected: expected value
434 * @seen: measured value
436 * ASSERT_EQ(expected, measured): expected == measured
438 #define ASSERT_EQ(expected, seen) \
439 __EXPECT(expected, #expected, seen, #seen, ==, 1)
442 * ASSERT_NE(expected, seen)
444 * @expected: expected value
445 * @seen: measured value
447 * ASSERT_NE(expected, measured): expected != measured
449 #define ASSERT_NE(expected, seen) \
450 __EXPECT(expected, #expected, seen, #seen, !=, 1)
453 * ASSERT_LT(expected, seen)
455 * @expected: expected value
456 * @seen: measured value
458 * ASSERT_LT(expected, measured): expected < measured
460 #define ASSERT_LT(expected, seen) \
461 __EXPECT(expected, #expected, seen, #seen, <, 1)
464 * ASSERT_LE(expected, seen)
466 * @expected: expected value
467 * @seen: measured value
469 * ASSERT_LE(expected, measured): expected <= measured
471 #define ASSERT_LE(expected, seen) \
472 __EXPECT(expected, #expected, seen, #seen, <=, 1)
475 * ASSERT_GT(expected, seen)
477 * @expected: expected value
478 * @seen: measured value
480 * ASSERT_GT(expected, measured): expected > measured
482 #define ASSERT_GT(expected, seen) \
483 __EXPECT(expected, #expected, seen, #seen, >, 1)
486 * ASSERT_GE(expected, seen)
488 * @expected: expected value
489 * @seen: measured value
491 * ASSERT_GE(expected, measured): expected >= measured
493 #define ASSERT_GE(expected, seen) \
494 __EXPECT(expected, #expected, seen, #seen, >=, 1)
499 * @seen: measured value
501 * ASSERT_NULL(measured): NULL == measured
503 #define ASSERT_NULL(seen) \
504 __EXPECT(NULL, "NULL", seen, #seen, ==, 1)
509 * @seen: measured value
511 * ASSERT_TRUE(measured): measured != 0
513 #define ASSERT_TRUE(seen) \
514 __EXPECT(0, "0", seen, #seen, !=, 1)
519 * @seen: measured value
521 * ASSERT_FALSE(measured): measured == 0
523 #define ASSERT_FALSE(seen) \
524 __EXPECT(0, "0", seen, #seen, ==, 1)
527 * ASSERT_STREQ(expected, seen)
529 * @expected: expected value
530 * @seen: measured value
532 * ASSERT_STREQ(expected, measured): !strcmp(expected, measured)
534 #define ASSERT_STREQ(expected, seen) \
535 __EXPECT_STR(expected, seen, ==, 1)
538 * ASSERT_STRNE(expected, seen)
540 * @expected: expected value
541 * @seen: measured value
543 * ASSERT_STRNE(expected, measured): strcmp(expected, measured)
545 #define ASSERT_STRNE(expected, seen) \
546 __EXPECT_STR(expected, seen, !=, 1)
549 * EXPECT_EQ(expected, seen)
551 * @expected: expected value
552 * @seen: measured value
554 * EXPECT_EQ(expected, measured): expected == measured
556 #define EXPECT_EQ(expected, seen) \
557 __EXPECT(expected, #expected, seen, #seen, ==, 0)
560 * EXPECT_NE(expected, seen)
562 * @expected: expected value
563 * @seen: measured value
565 * EXPECT_NE(expected, measured): expected != measured
567 #define EXPECT_NE(expected, seen) \
568 __EXPECT(expected, #expected, seen, #seen, !=, 0)
571 * EXPECT_LT(expected, seen)
573 * @expected: expected value
574 * @seen: measured value
576 * EXPECT_LT(expected, measured): expected < measured
578 #define EXPECT_LT(expected, seen) \
579 __EXPECT(expected, #expected, seen, #seen, <, 0)
582 * EXPECT_LE(expected, seen)
584 * @expected: expected value
585 * @seen: measured value
587 * EXPECT_LE(expected, measured): expected <= measured
589 #define EXPECT_LE(expected, seen) \
590 __EXPECT(expected, #expected, seen, #seen, <=, 0)
593 * EXPECT_GT(expected, seen)
595 * @expected: expected value
596 * @seen: measured value
598 * EXPECT_GT(expected, measured): expected > measured
600 #define EXPECT_GT(expected, seen) \
601 __EXPECT(expected, #expected, seen, #seen, >, 0)
604 * EXPECT_GE(expected, seen)
606 * @expected: expected value
607 * @seen: measured value
609 * EXPECT_GE(expected, measured): expected >= measured
611 #define EXPECT_GE(expected, seen) \
612 __EXPECT(expected, #expected, seen, #seen, >=, 0)
617 * @seen: measured value
619 * EXPECT_NULL(measured): NULL == measured
621 #define EXPECT_NULL(seen) \
622 __EXPECT(NULL, "NULL", seen, #seen, ==, 0)
627 * @seen: measured value
629 * EXPECT_TRUE(measured): 0 != measured
631 #define EXPECT_TRUE(seen) \
632 __EXPECT(0, "0", seen, #seen, !=, 0)
637 * @seen: measured value
639 * EXPECT_FALSE(measured): 0 == measured
641 #define EXPECT_FALSE(seen) \
642 __EXPECT(0, "0", seen, #seen, ==, 0)
645 * EXPECT_STREQ(expected, seen)
647 * @expected: expected value
648 * @seen: measured value
650 * EXPECT_STREQ(expected, measured): !strcmp(expected, measured)
652 #define EXPECT_STREQ(expected, seen) \
653 __EXPECT_STR(expected, seen, ==, 0)
656 * EXPECT_STRNE(expected, seen)
658 * @expected: expected value
659 * @seen: measured value
661 * EXPECT_STRNE(expected, measured): strcmp(expected, measured)
663 #define EXPECT_STRNE(expected, seen) \
664 __EXPECT_STR(expected, seen, !=, 0)
666 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
668 /* Support an optional handler after and ASSERT_* or EXPECT_*. The approach is
669 * not thread-safe, but it should be fine in most sane test scenarios.
671 * Using __bail(), which optionally abort()s, is the easiest way to early
672 * return while still providing an optional block to the API consumer.
674 #define OPTIONAL_HANDLER(_assert) \
675 for (; _metadata->trigger; _metadata->trigger = \
676 __bail(_assert, _metadata->no_print, _metadata->step))
678 #define __INC_STEP(_metadata) \
679 if (_metadata->passed && _metadata->step < 255) \
682 #define __EXPECT(_expected, _expected_str, _seen, _seen_str, _t, _assert) do { \
683 /* Avoid multiple evaluation of the cases */ \
684 __typeof__(_expected) __exp = (_expected); \
685 __typeof__(_seen) __seen = (_seen); \
686 if (_assert) __INC_STEP(_metadata); \
687 if (!(__exp _t __seen)) { \
688 unsigned long long __exp_print = (uintptr_t)__exp; \
689 unsigned long long __seen_print = (uintptr_t)__seen; \
690 __TH_LOG("Expected %s (%llu) %s %s (%llu)", \
691 _expected_str, __exp_print, #_t, \
692 _seen_str, __seen_print); \
693 _metadata->passed = 0; \
694 /* Ensure the optional handler is triggered */ \
695 _metadata->trigger = 1; \
697 } while (0); OPTIONAL_HANDLER(_assert)
699 #define __EXPECT_STR(_expected, _seen, _t, _assert) do { \
700 const char *__exp = (_expected); \
701 const char *__seen = (_seen); \
702 if (_assert) __INC_STEP(_metadata); \
703 if (!(strcmp(__exp, __seen) _t 0)) { \
704 __TH_LOG("Expected '%s' %s '%s'.", __exp, #_t, __seen); \
705 _metadata->passed = 0; \
706 _metadata->trigger = 1; \
708 } while (0); OPTIONAL_HANDLER(_assert)
711 #define __LIST_APPEND(head, item) \
713 /* Circular linked list where only prev is circular. */ \
714 if (head == NULL) { \
720 if (__constructor_order == _CONSTRUCTOR_ORDER_FORWARD) { \
722 item->prev = head->prev; \
723 item->prev->next = item; \
727 item->next->prev = item; \
733 struct __test_metadata;
734 struct __fixture_variant_metadata;
736 /* Contains all the information about a fixture. */
737 struct __fixture_metadata {
739 struct __test_metadata *tests;
740 struct __fixture_variant_metadata *variant;
741 struct __fixture_metadata *prev, *next;
742 } _fixture_global __attribute__((unused)) = {
744 .prev = &_fixture_global,
747 static struct __fixture_metadata *__fixture_list = &_fixture_global;
748 static int __constructor_order;
750 #define _CONSTRUCTOR_ORDER_FORWARD 1
751 #define _CONSTRUCTOR_ORDER_BACKWARD -1
753 static inline void __register_fixture(struct __fixture_metadata *f)
755 __LIST_APPEND(__fixture_list, f);
758 struct __fixture_variant_metadata {
761 struct __fixture_variant_metadata *prev, *next;
765 __register_fixture_variant(struct __fixture_metadata *f,
766 struct __fixture_variant_metadata *variant)
768 __LIST_APPEND(f->variant, variant);
771 /* Contains all the information for test execution and status checking. */
772 struct __test_metadata {
774 void (*fn)(struct __test_metadata *,
775 struct __fixture_variant_metadata *);
776 pid_t pid; /* pid of test when being run */
777 struct __fixture_metadata *fixture;
780 int skip; /* did SKIP get used? */
781 int trigger; /* extra handler after the evaluation */
782 int timeout; /* seconds to wait for test timeout */
783 bool timed_out; /* did this test timeout instead of exiting? */
785 bool no_print; /* manual trigger when TH_LOG_STREAM is not available */
786 struct __test_metadata *prev, *next;
790 * Since constructors are called in reverse order, reverse the test
791 * list so tests are run in source declaration order.
792 * https://gcc.gnu.org/onlinedocs/gccint/Initialization.html
793 * However, it seems not all toolchains do this correctly, so use
794 * __constructor_order to detect which direction is called first
795 * and adjust list building logic to get things running in the right
798 static inline void __register_test(struct __test_metadata *t)
800 __LIST_APPEND(t->fixture->tests, t);
803 static inline int __bail(int for_realz, bool no_print, __u8 step)
813 struct __test_metadata *__active_test;
814 static void __timeout_handler(int sig, siginfo_t *info, void *ucontext)
816 struct __test_metadata *t = __active_test;
818 /* Sanity check handler execution environment. */
820 fprintf(TH_LOG_STREAM,
821 "# no active test in SIGALRM handler!?\n");
824 if (sig != SIGALRM || sig != info->si_signo) {
825 fprintf(TH_LOG_STREAM,
826 "# %s: SIGALRM handler caught signal %d!?\n",
827 t->name, sig != SIGALRM ? sig : info->si_signo);
832 kill(t->pid, SIGKILL);
835 void __wait_for_test(struct __test_metadata *t)
837 struct sigaction action = {
838 .sa_sigaction = __timeout_handler,
839 .sa_flags = SA_SIGINFO,
841 struct sigaction saved_action;
844 if (sigaction(SIGALRM, &action, &saved_action)) {
846 fprintf(TH_LOG_STREAM,
847 "# %s: unable to install SIGALRM handler\n",
852 t->timed_out = false;
854 waitpid(t->pid, &status, 0);
856 if (sigaction(SIGALRM, &saved_action, NULL)) {
858 fprintf(TH_LOG_STREAM,
859 "# %s: unable to uninstall SIGALRM handler\n",
863 __active_test = NULL;
867 fprintf(TH_LOG_STREAM,
868 "# %s: Test terminated by timeout\n", t->name);
869 } else if (WIFEXITED(status)) {
870 if (t->termsig != -1) {
872 fprintf(TH_LOG_STREAM,
873 "# %s: Test exited normally instead of by signal (code: %d)\n",
875 WEXITSTATUS(status));
877 switch (WEXITSTATUS(status)) {
887 /* Other failure, assume step report. */
890 fprintf(TH_LOG_STREAM,
891 "# %s: Test failed at step #%d\n",
893 WEXITSTATUS(status));
896 } else if (WIFSIGNALED(status)) {
898 if (WTERMSIG(status) == SIGABRT) {
899 fprintf(TH_LOG_STREAM,
900 "# %s: Test terminated by assertion\n",
902 } else if (WTERMSIG(status) == t->termsig) {
905 fprintf(TH_LOG_STREAM,
906 "# %s: Test terminated unexpectedly by signal %d\n",
911 fprintf(TH_LOG_STREAM,
912 "# %s: Test ended in some other way [%u]\n",
918 void __run_test(struct __fixture_metadata *f,
919 struct __fixture_variant_metadata *variant,
920 struct __test_metadata *t)
922 /* reset test struct */
929 ksft_print_msg(" RUN %s%s%s.%s ...\n",
930 f->name, variant->name[0] ? "." : "", variant->name, t->name);
933 ksft_print_msg("ERROR SPAWNING TEST CHILD\n");
935 } else if (t->pid == 0) {
937 /* Make sure step doesn't get lost in reporting */
938 if (t->step >= 255) {
939 ksft_print_msg("Too many test steps (%u)!?\n", t->step);
942 /* Use 255 for SKIP */
948 /* Something else happened, report the step. */
953 ksft_print_msg(" %4s %s%s%s.%s\n", t->passed ? "OK" : "FAIL",
954 f->name, variant->name[0] ? "." : "", variant->name, t->name);
957 ksft_test_result_skip("%s%s%s.%s\n",
958 f->name, variant->name[0] ? "." : "", variant->name, t->name);
960 ksft_test_result(t->passed, "%s%s%s.%s\n",
961 f->name, variant->name[0] ? "." : "", variant->name, t->name);
964 static int test_harness_run(int __attribute__((unused)) argc,
965 char __attribute__((unused)) **argv)
967 struct __fixture_variant_metadata no_variant = { .name = "", };
968 struct __fixture_variant_metadata *v;
969 struct __fixture_metadata *f;
970 struct __test_metadata *t;
972 unsigned int case_count = 0, test_count = 0;
973 unsigned int count = 0;
974 unsigned int pass_count = 0;
976 for (f = __fixture_list; f; f = f->next) {
977 for (v = f->variant ?: &no_variant; v; v = v->next) {
979 for (t = f->tests; t; t = t->next)
985 ksft_set_plan(test_count);
986 ksft_print_msg("Starting %u tests from %u test cases.\n",
987 test_count, case_count);
988 for (f = __fixture_list; f; f = f->next) {
989 for (v = f->variant ?: &no_variant; v; v = v->next) {
990 for (t = f->tests; t; t = t->next) {
1000 ksft_print_msg("%s: %u / %u tests passed.\n", ret ? "FAILED" : "PASSED",
1002 ksft_exit(ret == 0);
1008 static void __attribute__((constructor)) __constructor_order_first(void)
1010 if (!__constructor_order)
1011 __constructor_order = _CONSTRUCTOR_ORDER_FORWARD;
1014 #endif /* __KSELFTEST_HARNESS_H */