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
54 static_assert(0, "kselftest harness requires _GNU_SOURCE to be defined");
56 #include <asm/types.h>
65 #include <sys/types.h>
70 #include "kselftest.h"
72 #define TEST_TIMEOUT_DEFAULT 30
74 /* Utilities exposed to the test definitions */
76 # define TH_LOG_STREAM stderr
79 #ifndef TH_LOG_ENABLED
80 # define TH_LOG_ENABLED 1
87 * @...: optional arguments
93 * Optional debug logging function available for use in tests.
94 * Logging may be enabled or disabled by defining TH_LOG_ENABLED.
95 * E.g., #define TH_LOG_ENABLED 1
97 * If no definition is provided, logging is enabled by default.
99 #define TH_LOG(fmt, ...) do { \
100 if (TH_LOG_ENABLED) \
101 __TH_LOG(fmt, ##__VA_ARGS__); \
104 /* Unconditional logger for internal use. */
105 #define __TH_LOG(fmt, ...) \
106 fprintf(TH_LOG_STREAM, "# %s:%d:%s:" fmt "\n", \
107 __FILE__, __LINE__, _metadata->name, ##__VA_ARGS__)
112 * @statement: statement to run after reporting SKIP
113 * @fmt: format string
114 * @...: optional arguments
118 * SKIP(statement, fmt, ...);
120 * This forces a "pass" after reporting why something is being skipped
121 * and runs "statement", which is usually "return" or "goto skip".
123 #define SKIP(statement, fmt, ...) do { \
124 snprintf(_metadata->results->reason, \
125 sizeof(_metadata->results->reason), fmt, ##__VA_ARGS__); \
126 if (TH_LOG_ENABLED) { \
127 fprintf(TH_LOG_STREAM, "# SKIP %s\n", \
128 _metadata->results->reason); \
130 _metadata->exit_code = KSFT_SKIP; \
131 _metadata->trigger = 0; \
136 * TEST() - 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)
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 _metadata->setup_completed = true; \
180 if (setjmp(_metadata->env) == 0) \
181 test_name(_metadata); \
182 __test_check_assert(_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() - 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() - 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() - Prepares the setup function for the fixture.
243 * *_metadata* is included so that EXPECT_*, ASSERT_* etc. 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)
269 * *_metadata* is included so that EXPECT_*, ASSERT_* etc. 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, \
287 const FIXTURE_VARIANT(fixture_name) \
288 __attribute__((unused)) *variant)
291 * FIXTURE_VARIANT() - Optionally called once per fixture
292 * to declare fixture variant
294 * @fixture_name: fixture name
298 * FIXTURE_VARIANT(fixture_name) {
303 * Defines type of constant parameters provided to FIXTURE_SETUP(), TEST_F() and
304 * FIXTURE_TEARDOWN as *variant*. Variants allow the same tests to be run with
305 * different arguments.
307 #define FIXTURE_VARIANT(fixture_name) struct _fixture_variant_##fixture_name
310 * FIXTURE_VARIANT_ADD() - Called once per fixture
311 * variant to setup and register the data
313 * @fixture_name: fixture name
314 * @variant_name: name of the parameter set
318 * FIXTURE_VARIANT_ADD(fixture_name, variant_name) {
323 * Defines a variant of the test fixture, provided to FIXTURE_SETUP() and
324 * TEST_F() as *variant*. Tests of each fixture will be run once for each
327 #define FIXTURE_VARIANT_ADD(fixture_name, variant_name) \
328 extern FIXTURE_VARIANT(fixture_name) \
329 _##fixture_name##_##variant_name##_variant; \
330 static struct __fixture_variant_metadata \
331 _##fixture_name##_##variant_name##_object = \
332 { .name = #variant_name, \
333 .data = &_##fixture_name##_##variant_name##_variant}; \
334 static void __attribute__((constructor)) \
335 _register_##fixture_name##_##variant_name(void) \
337 __register_fixture_variant(&_##fixture_name##_fixture_object, \
338 &_##fixture_name##_##variant_name##_object); \
340 FIXTURE_VARIANT(fixture_name) \
341 _##fixture_name##_##variant_name##_variant =
344 * TEST_F() - Emits test registration and helpers for
345 * fixture-based test cases
347 * @fixture_name: fixture name
348 * @test_name: test name
352 * TEST_F(fixture, name) { implementation }
354 * Defines a test that depends on a fixture (e.g., is part of a test case).
355 * Very similar to TEST() except that *self* is the setup instance of fixture's
356 * datatype exposed for use by the implementation.
358 * The @test_name code is run in a separate process sharing the same memory
359 * (i.e. vfork), which means that the test process can update its privileges
360 * without impacting the related FIXTURE_TEARDOWN() (e.g. to remove files from
361 * a directory where write access was dropped).
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; \
386 memset(&self, 0, sizeof(FIXTURE_DATA(fixture_name))); \
387 if (setjmp(_metadata->env) == 0) { \
388 /* Use the same _metadata. */ \
391 fixture_name##_setup(_metadata, &self, variant->data); \
392 /* Let setup failure terminate early. */ \
393 if (_metadata->exit_code) \
395 _metadata->setup_completed = true; \
396 fixture_name##_##test_name(_metadata, &self, variant->data); \
397 } else if (child < 0 || child != waitpid(child, &status, 0)) { \
398 ksft_print_msg("ERROR SPAWNING TEST GRANDCHILD\n"); \
399 _metadata->exit_code = KSFT_FAIL; \
405 if (_metadata->setup_completed && !_metadata->teardown_parent && !jmp) \
406 fixture_name##_teardown(_metadata, &self, variant->data); \
409 if (_metadata->setup_completed && _metadata->teardown_parent) \
410 fixture_name##_teardown(_metadata, &self, variant->data); \
411 if (!WIFEXITED(status) && WIFSIGNALED(status)) \
412 /* Forward signal to __wait_for_test(). */ \
413 kill(getpid(), WTERMSIG(status)); \
414 __test_check_assert(_metadata); \
416 static struct __test_metadata \
417 _##fixture_name##_##test_name##_object = { \
418 .name = #test_name, \
419 .fn = &wrapper_##fixture_name##_##test_name, \
420 .fixture = &_##fixture_name##_fixture_object, \
423 .teardown_parent = false, \
425 static void __attribute__((constructor)) \
426 _register_##fixture_name##_##test_name(void) \
428 __register_test(&_##fixture_name##_##test_name##_object); \
430 static void fixture_name##_##test_name( \
431 struct __test_metadata __attribute__((unused)) *_metadata, \
432 FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
433 const FIXTURE_VARIANT(fixture_name) \
434 __attribute__((unused)) *variant)
437 * TEST_HARNESS_MAIN - Simple wrapper to run the test harness
443 * Use once to append a main() to the test file.
445 #define TEST_HARNESS_MAIN \
446 static void __attribute__((constructor)) \
447 __constructor_order_last(void) \
449 if (!__constructor_order) \
450 __constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; \
452 int main(int argc, char **argv) { \
453 return test_harness_run(argc, argv); \
459 * Operators for use in TEST() and TEST_F().
460 * ASSERT_* calls will stop test execution immediately.
461 * EXPECT_* calls will emit a failure warning, note it, and continue.
467 * @expected: expected value
468 * @seen: measured value
470 * ASSERT_EQ(expected, measured): expected == measured
472 #define ASSERT_EQ(expected, seen) \
473 __EXPECT(expected, #expected, seen, #seen, ==, 1)
478 * @expected: expected value
479 * @seen: measured value
481 * ASSERT_NE(expected, measured): expected != measured
483 #define ASSERT_NE(expected, seen) \
484 __EXPECT(expected, #expected, seen, #seen, !=, 1)
489 * @expected: expected value
490 * @seen: measured value
492 * ASSERT_LT(expected, measured): expected < measured
494 #define ASSERT_LT(expected, seen) \
495 __EXPECT(expected, #expected, seen, #seen, <, 1)
500 * @expected: expected value
501 * @seen: measured value
503 * ASSERT_LE(expected, measured): expected <= measured
505 #define ASSERT_LE(expected, seen) \
506 __EXPECT(expected, #expected, seen, #seen, <=, 1)
511 * @expected: expected value
512 * @seen: measured value
514 * ASSERT_GT(expected, measured): expected > measured
516 #define ASSERT_GT(expected, seen) \
517 __EXPECT(expected, #expected, seen, #seen, >, 1)
522 * @expected: expected value
523 * @seen: measured value
525 * ASSERT_GE(expected, measured): expected >= measured
527 #define ASSERT_GE(expected, seen) \
528 __EXPECT(expected, #expected, seen, #seen, >=, 1)
533 * @seen: measured value
535 * ASSERT_NULL(measured): NULL == measured
537 #define ASSERT_NULL(seen) \
538 __EXPECT(NULL, "NULL", seen, #seen, ==, 1)
543 * @seen: measured value
545 * ASSERT_TRUE(measured): measured != 0
547 #define ASSERT_TRUE(seen) \
548 __EXPECT(0, "0", seen, #seen, !=, 1)
553 * @seen: measured value
555 * ASSERT_FALSE(measured): measured == 0
557 #define ASSERT_FALSE(seen) \
558 __EXPECT(0, "0", seen, #seen, ==, 1)
563 * @expected: expected value
564 * @seen: measured value
566 * ASSERT_STREQ(expected, measured): !strcmp(expected, measured)
568 #define ASSERT_STREQ(expected, seen) \
569 __EXPECT_STR(expected, seen, ==, 1)
574 * @expected: expected value
575 * @seen: measured value
577 * ASSERT_STRNE(expected, measured): strcmp(expected, measured)
579 #define ASSERT_STRNE(expected, seen) \
580 __EXPECT_STR(expected, seen, !=, 1)
585 * @expected: expected value
586 * @seen: measured value
588 * EXPECT_EQ(expected, measured): expected == measured
590 #define EXPECT_EQ(expected, seen) \
591 __EXPECT(expected, #expected, seen, #seen, ==, 0)
596 * @expected: expected value
597 * @seen: measured value
599 * EXPECT_NE(expected, measured): expected != measured
601 #define EXPECT_NE(expected, seen) \
602 __EXPECT(expected, #expected, seen, #seen, !=, 0)
607 * @expected: expected value
608 * @seen: measured value
610 * EXPECT_LT(expected, measured): expected < measured
612 #define EXPECT_LT(expected, seen) \
613 __EXPECT(expected, #expected, seen, #seen, <, 0)
618 * @expected: expected value
619 * @seen: measured value
621 * EXPECT_LE(expected, measured): expected <= measured
623 #define EXPECT_LE(expected, seen) \
624 __EXPECT(expected, #expected, seen, #seen, <=, 0)
629 * @expected: expected value
630 * @seen: measured value
632 * EXPECT_GT(expected, measured): expected > measured
634 #define EXPECT_GT(expected, seen) \
635 __EXPECT(expected, #expected, seen, #seen, >, 0)
640 * @expected: expected value
641 * @seen: measured value
643 * EXPECT_GE(expected, measured): expected >= measured
645 #define EXPECT_GE(expected, seen) \
646 __EXPECT(expected, #expected, seen, #seen, >=, 0)
651 * @seen: measured value
653 * EXPECT_NULL(measured): NULL == measured
655 #define EXPECT_NULL(seen) \
656 __EXPECT(NULL, "NULL", seen, #seen, ==, 0)
661 * @seen: measured value
663 * EXPECT_TRUE(measured): 0 != measured
665 #define EXPECT_TRUE(seen) \
666 __EXPECT(0, "0", seen, #seen, !=, 0)
671 * @seen: measured value
673 * EXPECT_FALSE(measured): 0 == measured
675 #define EXPECT_FALSE(seen) \
676 __EXPECT(0, "0", seen, #seen, ==, 0)
681 * @expected: expected value
682 * @seen: measured value
684 * EXPECT_STREQ(expected, measured): !strcmp(expected, measured)
686 #define EXPECT_STREQ(expected, seen) \
687 __EXPECT_STR(expected, seen, ==, 0)
692 * @expected: expected value
693 * @seen: measured value
695 * EXPECT_STRNE(expected, measured): strcmp(expected, measured)
697 #define EXPECT_STRNE(expected, seen) \
698 __EXPECT_STR(expected, seen, !=, 0)
701 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
704 /* Support an optional handler after and ASSERT_* or EXPECT_*. The approach is
705 * not thread-safe, but it should be fine in most sane test scenarios.
707 * Using __bail(), which optionally abort()s, is the easiest way to early
708 * return while still providing an optional block to the API consumer.
710 #define OPTIONAL_HANDLER(_assert) \
711 for (; _metadata->trigger; _metadata->trigger = \
712 __bail(_assert, _metadata))
714 #define is_signed_type(var) (!!(((__typeof__(var))(-1)) < (__typeof__(var))1))
716 #define __EXPECT(_expected, _expected_str, _seen, _seen_str, _t, _assert) do { \
717 /* Avoid multiple evaluation of the cases */ \
718 __typeof__(_expected) __exp = (_expected); \
719 __typeof__(_seen) __seen = (_seen); \
720 if (!(__exp _t __seen)) { \
721 /* Report with actual signedness to avoid weird output. */ \
722 switch (is_signed_type(__exp) * 2 + is_signed_type(__seen)) { \
724 unsigned long long __exp_print = (uintptr_t)__exp; \
725 unsigned long long __seen_print = (uintptr_t)__seen; \
726 __TH_LOG("Expected %s (%llu) %s %s (%llu)", \
727 _expected_str, __exp_print, #_t, \
728 _seen_str, __seen_print); \
732 unsigned long long __exp_print = (uintptr_t)__exp; \
733 long long __seen_print = (intptr_t)__seen; \
734 __TH_LOG("Expected %s (%llu) %s %s (%lld)", \
735 _expected_str, __exp_print, #_t, \
736 _seen_str, __seen_print); \
740 long long __exp_print = (intptr_t)__exp; \
741 unsigned long long __seen_print = (uintptr_t)__seen; \
742 __TH_LOG("Expected %s (%lld) %s %s (%llu)", \
743 _expected_str, __exp_print, #_t, \
744 _seen_str, __seen_print); \
748 long long __exp_print = (intptr_t)__exp; \
749 long long __seen_print = (intptr_t)__seen; \
750 __TH_LOG("Expected %s (%lld) %s %s (%lld)", \
751 _expected_str, __exp_print, #_t, \
752 _seen_str, __seen_print); \
756 _metadata->exit_code = KSFT_FAIL; \
757 /* Ensure the optional handler is triggered */ \
758 _metadata->trigger = 1; \
760 } while (0); OPTIONAL_HANDLER(_assert)
762 #define __EXPECT_STR(_expected, _seen, _t, _assert) do { \
763 const char *__exp = (_expected); \
764 const char *__seen = (_seen); \
765 if (!(strcmp(__exp, __seen) _t 0)) { \
766 __TH_LOG("Expected '%s' %s '%s'.", __exp, #_t, __seen); \
767 _metadata->exit_code = KSFT_FAIL; \
768 _metadata->trigger = 1; \
770 } while (0); OPTIONAL_HANDLER(_assert)
773 #define __LIST_APPEND(head, item) \
775 /* Circular linked list where only prev is circular. */ \
776 if (head == NULL) { \
782 if (__constructor_order == _CONSTRUCTOR_ORDER_FORWARD) { \
784 item->prev = head->prev; \
785 item->prev->next = item; \
789 item->next->prev = item; \
795 struct __test_results {
796 char reason[1024]; /* Reason for test result */
799 struct __test_metadata;
800 struct __fixture_variant_metadata;
802 /* Contains all the information about a fixture. */
803 struct __fixture_metadata {
805 struct __test_metadata *tests;
806 struct __fixture_variant_metadata *variant;
807 struct __fixture_metadata *prev, *next;
808 } _fixture_global __attribute__((unused)) = {
810 .prev = &_fixture_global,
813 struct __test_xfail {
814 struct __fixture_metadata *fixture;
815 struct __fixture_variant_metadata *variant;
816 struct __test_metadata *test;
817 struct __test_xfail *prev, *next;
821 * XFAIL_ADD() - mark variant + test case combination as expected to fail
822 * @fixture_name: name of the fixture
823 * @variant_name: name of the variant
824 * @test_name: name of the test case
826 * Mark a combination of variant + test case for a given fixture as expected
827 * to fail. Tests marked this way will report XPASS / XFAIL return codes,
828 * instead of PASS / FAIL,and use respective counters.
830 #define XFAIL_ADD(fixture_name, variant_name, test_name) \
831 static struct __test_xfail \
832 _##fixture_name##_##variant_name##_##test_name##_xfail = \
834 .fixture = &_##fixture_name##_fixture_object, \
835 .variant = &_##fixture_name##_##variant_name##_object, \
836 .test = &_##fixture_name##_##test_name##_object, \
838 static void __attribute__((constructor)) \
839 _register_##fixture_name##_##variant_name##_##test_name##_xfail(void) \
841 __register_xfail(&_##fixture_name##_##variant_name##_##test_name##_xfail); \
844 static struct __fixture_metadata *__fixture_list = &_fixture_global;
845 static int __constructor_order;
847 #define _CONSTRUCTOR_ORDER_FORWARD 1
848 #define _CONSTRUCTOR_ORDER_BACKWARD -1
850 static inline void __register_fixture(struct __fixture_metadata *f)
852 __LIST_APPEND(__fixture_list, f);
855 struct __fixture_variant_metadata {
858 struct __test_xfail *xfails;
859 struct __fixture_variant_metadata *prev, *next;
863 __register_fixture_variant(struct __fixture_metadata *f,
864 struct __fixture_variant_metadata *variant)
866 __LIST_APPEND(f->variant, variant);
869 /* Contains all the information for test execution and status checking. */
870 struct __test_metadata {
872 void (*fn)(struct __test_metadata *,
873 struct __fixture_variant_metadata *);
874 pid_t pid; /* pid of test when being run */
875 struct __fixture_metadata *fixture;
878 int trigger; /* extra handler after the evaluation */
879 int timeout; /* seconds to wait for test timeout */
880 bool timed_out; /* did this test timeout instead of exiting? */
881 bool aborted; /* stopped test due to failed ASSERT */
882 bool setup_completed; /* did setup finish? */
883 bool teardown_parent; /* run teardown in a parent process */
884 jmp_buf env; /* for exiting out of test early */
885 struct __test_results *results;
886 struct __test_metadata *prev, *next;
889 static inline bool __test_passed(struct __test_metadata *metadata)
891 return metadata->exit_code != KSFT_FAIL &&
892 metadata->exit_code <= KSFT_SKIP;
896 * Since constructors are called in reverse order, reverse the test
897 * list so tests are run in source declaration order.
898 * https://gcc.gnu.org/onlinedocs/gccint/Initialization.html
899 * However, it seems not all toolchains do this correctly, so use
900 * __constructor_order to detect which direction is called first
901 * and adjust list building logic to get things running in the right
904 static inline void __register_test(struct __test_metadata *t)
906 __LIST_APPEND(t->fixture->tests, t);
909 static inline void __register_xfail(struct __test_xfail *xf)
911 __LIST_APPEND(xf->variant->xfails, xf);
914 static inline int __bail(int for_realz, struct __test_metadata *t)
916 /* if this is ASSERT, return immediately. */
921 /* otherwise, end the for loop and continue. */
925 static inline void __test_check_assert(struct __test_metadata *t)
931 struct __test_metadata *__active_test;
932 static void __timeout_handler(int sig, siginfo_t *info, void *ucontext)
934 struct __test_metadata *t = __active_test;
936 /* Sanity check handler execution environment. */
938 fprintf(TH_LOG_STREAM,
939 "# no active test in SIGALRM handler!?\n");
942 if (sig != SIGALRM || sig != info->si_signo) {
943 fprintf(TH_LOG_STREAM,
944 "# %s: SIGALRM handler caught signal %d!?\n",
945 t->name, sig != SIGALRM ? sig : info->si_signo);
950 // signal process group
951 kill(-(t->pid), SIGKILL);
954 void __wait_for_test(struct __test_metadata *t)
956 struct sigaction action = {
957 .sa_sigaction = __timeout_handler,
958 .sa_flags = SA_SIGINFO,
960 struct sigaction saved_action;
963 if (sigaction(SIGALRM, &action, &saved_action)) {
964 t->exit_code = KSFT_FAIL;
965 fprintf(TH_LOG_STREAM,
966 "# %s: unable to install SIGALRM handler\n",
971 t->timed_out = false;
973 waitpid(t->pid, &status, 0);
975 if (sigaction(SIGALRM, &saved_action, NULL)) {
976 t->exit_code = KSFT_FAIL;
977 fprintf(TH_LOG_STREAM,
978 "# %s: unable to uninstall SIGALRM handler\n",
982 __active_test = NULL;
985 t->exit_code = KSFT_FAIL;
986 fprintf(TH_LOG_STREAM,
987 "# %s: Test terminated by timeout\n", t->name);
988 } else if (WIFEXITED(status)) {
989 if (WEXITSTATUS(status) == KSFT_SKIP ||
990 WEXITSTATUS(status) == KSFT_XPASS ||
991 WEXITSTATUS(status) == KSFT_XFAIL) {
992 t->exit_code = WEXITSTATUS(status);
993 } else if (t->termsig != -1) {
994 t->exit_code = KSFT_FAIL;
995 fprintf(TH_LOG_STREAM,
996 "# %s: Test exited normally instead of by signal (code: %d)\n",
998 WEXITSTATUS(status));
1000 switch (WEXITSTATUS(status)) {
1003 t->exit_code = KSFT_PASS;
1007 t->exit_code = KSFT_FAIL;
1008 fprintf(TH_LOG_STREAM,
1009 "# %s: Test failed\n",
1013 } else if (WIFSIGNALED(status)) {
1014 t->exit_code = KSFT_FAIL;
1015 if (WTERMSIG(status) == SIGABRT) {
1016 fprintf(TH_LOG_STREAM,
1017 "# %s: Test terminated by assertion\n",
1019 } else if (WTERMSIG(status) == t->termsig) {
1020 t->exit_code = KSFT_PASS;
1022 fprintf(TH_LOG_STREAM,
1023 "# %s: Test terminated unexpectedly by signal %d\n",
1028 fprintf(TH_LOG_STREAM,
1029 "# %s: Test ended in some other way [%u]\n",
1035 static void test_harness_list_tests(void)
1037 struct __fixture_variant_metadata *v;
1038 struct __fixture_metadata *f;
1039 struct __test_metadata *t;
1041 for (f = __fixture_list; f; f = f->next) {
1045 if (f == __fixture_list)
1046 fprintf(stderr, "%-20s %-25s %s\n",
1047 "# FIXTURE", "VARIANT", "TEST");
1049 fprintf(stderr, "--------------------------------------------------------------------------------\n");
1052 fprintf(stderr, "%-20s %-25s %s\n",
1053 t == f->tests ? f->name : "",
1057 v = v ? v->next : NULL;
1058 t = t ? t->next : NULL;
1063 static int test_harness_argv_check(int argc, char **argv)
1067 while ((opt = getopt(argc, argv, "hlF:f:V:v:t:T:r:")) != -1) {
1078 test_harness_list_tests();
1083 "Usage: %s [-h|-l] [-t|-T|-v|-V|-f|-F|-r name]\n"
1085 "\t-l list all tests\n"
1087 "\t-t name include test\n"
1088 "\t-T name exclude test\n"
1089 "\t-v name include variant\n"
1090 "\t-V name exclude variant\n"
1091 "\t-f name include fixture\n"
1092 "\t-F name exclude fixture\n"
1093 "\t-r name run specified test\n"
1095 "Test filter options can be specified "
1096 "multiple times. The filtering stops\n"
1097 "at the first match. For example to "
1098 "include all tests from variant 'bla'\n"
1099 "but not test 'foo' specify '-T foo -v bla'.\n"
1101 return opt == 'h' ? KSFT_SKIP : KSFT_FAIL;
1108 static bool test_enabled(int argc, char **argv,
1109 struct __fixture_metadata *f,
1110 struct __fixture_variant_metadata *v,
1111 struct __test_metadata *t)
1113 unsigned int flen = 0, vlen = 0, tlen = 0;
1114 bool has_positive = false;
1118 while ((opt = getopt(argc, argv, "F:f:V:v:t:T:r:")) != -1) {
1119 has_positive |= islower(opt);
1121 switch (tolower(opt)) {
1123 if (!strcmp(t->name, optarg))
1124 return islower(opt);
1127 if (!strcmp(f->name, optarg))
1128 return islower(opt);
1131 if (!strcmp(v->name, optarg))
1132 return islower(opt);
1136 flen = strlen(f->name);
1137 vlen = strlen(v->name);
1138 tlen = strlen(t->name);
1140 if (strlen(optarg) == flen + 1 + vlen + !!vlen + tlen &&
1141 !strncmp(f->name, &optarg[0], flen) &&
1142 !strncmp(v->name, &optarg[flen + 1], vlen) &&
1143 !strncmp(t->name, &optarg[flen + 1 + vlen + !!vlen], tlen))
1150 * If there are no positive tests then we assume user just wants
1151 * exclusions and everything else is a pass.
1153 return !has_positive;
1156 void __run_test(struct __fixture_metadata *f,
1157 struct __fixture_variant_metadata *variant,
1158 struct __test_metadata *t)
1160 struct __test_xfail *xfail;
1162 const char *diagnostic;
1164 /* reset test struct */
1165 t->exit_code = KSFT_PASS;
1167 memset(t->results->reason, 0, sizeof(t->results->reason));
1169 if (asprintf(&test_name, "%s%s%s.%s", f->name,
1170 variant->name[0] ? "." : "", variant->name, t->name) == -1) {
1171 ksft_print_msg("ERROR ALLOCATING MEMORY\n");
1172 t->exit_code = KSFT_FAIL;
1173 _exit(t->exit_code);
1176 ksft_print_msg(" RUN %s ...\n", test_name);
1178 /* Make sure output buffers are flushed before fork */
1184 ksft_print_msg("ERROR SPAWNING TEST CHILD\n");
1185 t->exit_code = KSFT_FAIL;
1186 } else if (t->pid == 0) {
1189 _exit(t->exit_code);
1193 ksft_print_msg(" %4s %s\n",
1194 __test_passed(t) ? "OK" : "FAIL", test_name);
1196 /* Check if we're expecting this test to fail */
1197 for (xfail = variant->xfails; xfail; xfail = xfail->next)
1198 if (xfail->test == t)
1201 t->exit_code = __test_passed(t) ? KSFT_XPASS : KSFT_XFAIL;
1203 if (t->results->reason[0])
1204 diagnostic = t->results->reason;
1205 else if (t->exit_code == KSFT_PASS || t->exit_code == KSFT_FAIL)
1208 diagnostic = "unknown";
1210 ksft_test_result_code(t->exit_code, test_name,
1211 diagnostic ? "%s" : NULL, diagnostic);
1215 static int test_harness_run(int argc, char **argv)
1217 struct __fixture_variant_metadata no_variant = { .name = "", };
1218 struct __fixture_variant_metadata *v;
1219 struct __fixture_metadata *f;
1220 struct __test_results *results;
1221 struct __test_metadata *t;
1223 unsigned int case_count = 0, test_count = 0;
1224 unsigned int count = 0;
1225 unsigned int pass_count = 0;
1227 ret = test_harness_argv_check(argc, argv);
1228 if (ret != KSFT_PASS)
1231 for (f = __fixture_list; f; f = f->next) {
1232 for (v = f->variant ?: &no_variant; v; v = v->next) {
1233 unsigned int old_tests = test_count;
1235 for (t = f->tests; t; t = t->next)
1236 if (test_enabled(argc, argv, f, v, t))
1239 if (old_tests != test_count)
1244 results = mmap(NULL, sizeof(*results), PROT_READ | PROT_WRITE,
1245 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1247 ksft_print_header();
1248 ksft_set_plan(test_count);
1249 ksft_print_msg("Starting %u tests from %u test cases.\n",
1250 test_count, case_count);
1251 for (f = __fixture_list; f; f = f->next) {
1252 for (v = f->variant ?: &no_variant; v; v = v->next) {
1253 for (t = f->tests; t; t = t->next) {
1254 if (!test_enabled(argc, argv, f, v, t))
1257 t->results = results;
1258 __run_test(f, v, t);
1260 if (__test_passed(t))
1267 munmap(results, sizeof(*results));
1269 ksft_print_msg("%s: %u / %u tests passed.\n", ret ? "FAILED" : "PASSED",
1271 ksft_exit(ret == 0);
1277 static void __attribute__((constructor)) __constructor_order_first(void)
1279 if (!__constructor_order)
1280 __constructor_order = _CONSTRUCTOR_ORDER_FORWARD;
1283 #endif /* __KSELFTEST_HARNESS_H */