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>
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->passed = 1; \
131 _metadata->skip = 1; \
132 _metadata->trigger = 0; \
137 * TEST() - Defines the test function and creates the registration
140 * @test_name: test name
144 * TEST(name) { implementation }
146 * Defines a test by name.
147 * Names must be unique and tests must not be run in parallel. The
148 * implementation containing block is a function and scoping should be treated
149 * as such. Returning early may be performed with a bare "return;" statement.
151 * EXPECT_* and ASSERT_* are valid in a TEST() { } context.
153 #define TEST(test_name) __TEST_IMPL(test_name, -1)
158 * @test_name: test name
159 * @signal: signal number
163 * TEST_SIGNAL(name, signal) { implementation }
165 * Defines a test by name and the expected term signal.
166 * Names must be unique and tests must not be run in parallel. The
167 * implementation containing block is a function and scoping should be treated
168 * as such. Returning early may be performed with a bare "return;" statement.
170 * EXPECT_* and ASSERT_* are valid in a TEST() { } context.
172 #define TEST_SIGNAL(test_name, signal) __TEST_IMPL(test_name, signal)
174 #define __TEST_IMPL(test_name, _signal) \
175 static void test_name(struct __test_metadata *_metadata); \
176 static inline void wrapper_##test_name( \
177 struct __test_metadata *_metadata, \
178 struct __fixture_variant_metadata *variant) \
180 _metadata->setup_completed = true; \
181 if (setjmp(_metadata->env) == 0) \
182 test_name(_metadata); \
183 __test_check_assert(_metadata); \
185 static struct __test_metadata _##test_name##_object = \
186 { .name = #test_name, \
187 .fn = &wrapper_##test_name, \
188 .fixture = &_fixture_global, \
189 .termsig = _signal, \
190 .timeout = TEST_TIMEOUT_DEFAULT, }; \
191 static void __attribute__((constructor)) _register_##test_name(void) \
193 __register_test(&_##test_name##_object); \
195 static void test_name( \
196 struct __test_metadata __attribute__((unused)) *_metadata)
199 * FIXTURE_DATA() - Wraps the struct name so we have one less
200 * argument to pass around
202 * @datatype_name: datatype name
206 * FIXTURE_DATA(datatype_name)
208 * Almost always, you want just FIXTURE() instead (see below).
209 * This call may be used when the type of the fixture data
210 * is needed. In general, this should not be needed unless
211 * the *self* is being passed to a helper directly.
213 #define FIXTURE_DATA(datatype_name) struct _test_data_##datatype_name
216 * FIXTURE() - Called once per fixture to setup the data and
219 * @fixture_name: fixture name
223 * FIXTURE(fixture_name) {
228 * Defines the data provided to TEST_F()-defined tests as *self*. It should be
229 * populated and cleaned up using FIXTURE_SETUP() and FIXTURE_TEARDOWN().
231 #define FIXTURE(fixture_name) \
232 FIXTURE_VARIANT(fixture_name); \
233 static struct __fixture_metadata _##fixture_name##_fixture_object = \
234 { .name = #fixture_name, }; \
235 static void __attribute__((constructor)) \
236 _register_##fixture_name##_data(void) \
238 __register_fixture(&_##fixture_name##_fixture_object); \
240 FIXTURE_DATA(fixture_name)
243 * FIXTURE_SETUP() - Prepares the setup function for the fixture.
244 * *_metadata* is included so that EXPECT_*, ASSERT_* etc. work correctly.
246 * @fixture_name: fixture name
250 * FIXTURE_SETUP(fixture_name) { implementation }
252 * Populates the required "setup" function for a fixture. An instance of the
253 * datatype defined with FIXTURE_DATA() will be exposed as *self* for the
256 * ASSERT_* are valid for use in this context and will prempt the execution
257 * of any dependent fixture tests.
259 * A bare "return;" statement may be used to return early.
261 #define FIXTURE_SETUP(fixture_name) \
262 void fixture_name##_setup( \
263 struct __test_metadata __attribute__((unused)) *_metadata, \
264 FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
265 const FIXTURE_VARIANT(fixture_name) \
266 __attribute__((unused)) *variant)
270 * *_metadata* is included so that EXPECT_*, ASSERT_* etc. work correctly.
272 * @fixture_name: fixture name
276 * FIXTURE_TEARDOWN(fixture_name) { implementation }
278 * Populates the required "teardown" function for a fixture. An instance of the
279 * datatype defined with FIXTURE_DATA() will be exposed as *self* for the
280 * implementation to clean up.
282 * A bare "return;" statement may be used to return early.
284 #define FIXTURE_TEARDOWN(fixture_name) \
285 void fixture_name##_teardown( \
286 struct __test_metadata __attribute__((unused)) *_metadata, \
287 FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
288 const FIXTURE_VARIANT(fixture_name) \
289 __attribute__((unused)) *variant)
292 * FIXTURE_VARIANT() - Optionally called once per fixture
293 * to declare fixture variant
295 * @fixture_name: fixture name
299 * FIXTURE_VARIANT(fixture_name) {
304 * Defines type of constant parameters provided to FIXTURE_SETUP(), TEST_F() and
305 * FIXTURE_TEARDOWN as *variant*. Variants allow the same tests to be run with
306 * different arguments.
308 #define FIXTURE_VARIANT(fixture_name) struct _fixture_variant_##fixture_name
311 * FIXTURE_VARIANT_ADD() - Called once per fixture
312 * variant to setup and register the data
314 * @fixture_name: fixture name
315 * @variant_name: name of the parameter set
319 * FIXTURE_VARIANT_ADD(fixture_name, variant_name) {
324 * Defines a variant of the test fixture, provided to FIXTURE_SETUP() and
325 * TEST_F() as *variant*. Tests of each fixture will be run once for each
328 #define FIXTURE_VARIANT_ADD(fixture_name, variant_name) \
329 extern FIXTURE_VARIANT(fixture_name) \
330 _##fixture_name##_##variant_name##_variant; \
331 static struct __fixture_variant_metadata \
332 _##fixture_name##_##variant_name##_object = \
333 { .name = #variant_name, \
334 .data = &_##fixture_name##_##variant_name##_variant}; \
335 static void __attribute__((constructor)) \
336 _register_##fixture_name##_##variant_name(void) \
338 __register_fixture_variant(&_##fixture_name##_fixture_object, \
339 &_##fixture_name##_##variant_name##_object); \
341 FIXTURE_VARIANT(fixture_name) \
342 _##fixture_name##_##variant_name##_variant =
345 * TEST_F() - Emits test registration and helpers for
346 * fixture-based test cases
348 * @fixture_name: fixture name
349 * @test_name: test name
353 * TEST_F(fixture, name) { implementation }
355 * Defines a test that depends on a fixture (e.g., is part of a test case).
356 * Very similar to TEST() except that *self* is the setup instance of fixture's
357 * datatype exposed for use by the implementation.
359 * The @test_name code is run in a separate process sharing the same memory
360 * (i.e. vfork), which means that the test process can update its privileges
361 * without impacting the related FIXTURE_TEARDOWN() (e.g. to remove files from
362 * a directory where write access was dropped).
364 #define TEST_F(fixture_name, test_name) \
365 __TEST_F_IMPL(fixture_name, test_name, -1, TEST_TIMEOUT_DEFAULT)
367 #define TEST_F_SIGNAL(fixture_name, test_name, signal) \
368 __TEST_F_IMPL(fixture_name, test_name, signal, TEST_TIMEOUT_DEFAULT)
370 #define TEST_F_TIMEOUT(fixture_name, test_name, timeout) \
371 __TEST_F_IMPL(fixture_name, test_name, -1, timeout)
373 #define __TEST_F_IMPL(fixture_name, test_name, signal, tmout) \
374 static void fixture_name##_##test_name( \
375 struct __test_metadata *_metadata, \
376 FIXTURE_DATA(fixture_name) *self, \
377 const FIXTURE_VARIANT(fixture_name) *variant); \
378 static inline void wrapper_##fixture_name##_##test_name( \
379 struct __test_metadata *_metadata, \
380 struct __fixture_variant_metadata *variant) \
382 /* fixture data is alloced, setup, and torn down per call. */ \
383 FIXTURE_DATA(fixture_name) self; \
385 memset(&self, 0, sizeof(FIXTURE_DATA(fixture_name))); \
386 if (setjmp(_metadata->env) == 0) { \
387 fixture_name##_setup(_metadata, &self, variant->data); \
388 /* Let setup failure terminate early. */ \
389 if (!_metadata->passed || _metadata->skip) \
391 _metadata->setup_completed = true; \
392 /* Use the same _metadata. */ \
395 fixture_name##_##test_name(_metadata, &self, variant->data); \
399 ksft_print_msg("ERROR SPAWNING TEST GRANDCHILD\n"); \
400 _metadata->passed = 0; \
404 /* Child failed and updated the shared _metadata. */ \
406 if (_metadata->setup_completed) \
407 fixture_name##_teardown(_metadata, &self, variant->data); \
408 __test_check_assert(_metadata); \
410 static struct __test_metadata \
411 _##fixture_name##_##test_name##_object = { \
412 .name = #test_name, \
413 .fn = &wrapper_##fixture_name##_##test_name, \
414 .fixture = &_##fixture_name##_fixture_object, \
418 static void __attribute__((constructor)) \
419 _register_##fixture_name##_##test_name(void) \
421 __register_test(&_##fixture_name##_##test_name##_object); \
423 static void fixture_name##_##test_name( \
424 struct __test_metadata __attribute__((unused)) *_metadata, \
425 FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
426 const FIXTURE_VARIANT(fixture_name) \
427 __attribute__((unused)) *variant)
430 * TEST_HARNESS_MAIN - Simple wrapper to run the test harness
436 * Use once to append a main() to the test file.
438 #define TEST_HARNESS_MAIN \
439 static void __attribute__((constructor)) \
440 __constructor_order_last(void) \
442 if (!__constructor_order) \
443 __constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; \
445 int main(int argc, char **argv) { \
446 return test_harness_run(argc, argv); \
452 * Operators for use in TEST() and TEST_F().
453 * ASSERT_* calls will stop test execution immediately.
454 * EXPECT_* calls will emit a failure warning, note it, and continue.
460 * @expected: expected value
461 * @seen: measured value
463 * ASSERT_EQ(expected, measured): expected == measured
465 #define ASSERT_EQ(expected, seen) \
466 __EXPECT(expected, #expected, seen, #seen, ==, 1)
471 * @expected: expected value
472 * @seen: measured value
474 * ASSERT_NE(expected, measured): expected != measured
476 #define ASSERT_NE(expected, seen) \
477 __EXPECT(expected, #expected, seen, #seen, !=, 1)
482 * @expected: expected value
483 * @seen: measured value
485 * ASSERT_LT(expected, measured): expected < measured
487 #define ASSERT_LT(expected, seen) \
488 __EXPECT(expected, #expected, seen, #seen, <, 1)
493 * @expected: expected value
494 * @seen: measured value
496 * ASSERT_LE(expected, measured): expected <= measured
498 #define ASSERT_LE(expected, seen) \
499 __EXPECT(expected, #expected, seen, #seen, <=, 1)
504 * @expected: expected value
505 * @seen: measured value
507 * ASSERT_GT(expected, measured): expected > measured
509 #define ASSERT_GT(expected, seen) \
510 __EXPECT(expected, #expected, seen, #seen, >, 1)
515 * @expected: expected value
516 * @seen: measured value
518 * ASSERT_GE(expected, measured): expected >= measured
520 #define ASSERT_GE(expected, seen) \
521 __EXPECT(expected, #expected, seen, #seen, >=, 1)
526 * @seen: measured value
528 * ASSERT_NULL(measured): NULL == measured
530 #define ASSERT_NULL(seen) \
531 __EXPECT(NULL, "NULL", seen, #seen, ==, 1)
536 * @seen: measured value
538 * ASSERT_TRUE(measured): measured != 0
540 #define ASSERT_TRUE(seen) \
541 __EXPECT(0, "0", seen, #seen, !=, 1)
546 * @seen: measured value
548 * ASSERT_FALSE(measured): measured == 0
550 #define ASSERT_FALSE(seen) \
551 __EXPECT(0, "0", seen, #seen, ==, 1)
556 * @expected: expected value
557 * @seen: measured value
559 * ASSERT_STREQ(expected, measured): !strcmp(expected, measured)
561 #define ASSERT_STREQ(expected, seen) \
562 __EXPECT_STR(expected, seen, ==, 1)
567 * @expected: expected value
568 * @seen: measured value
570 * ASSERT_STRNE(expected, measured): strcmp(expected, measured)
572 #define ASSERT_STRNE(expected, seen) \
573 __EXPECT_STR(expected, seen, !=, 1)
578 * @expected: expected value
579 * @seen: measured value
581 * EXPECT_EQ(expected, measured): expected == measured
583 #define EXPECT_EQ(expected, seen) \
584 __EXPECT(expected, #expected, seen, #seen, ==, 0)
589 * @expected: expected value
590 * @seen: measured value
592 * EXPECT_NE(expected, measured): expected != measured
594 #define EXPECT_NE(expected, seen) \
595 __EXPECT(expected, #expected, seen, #seen, !=, 0)
600 * @expected: expected value
601 * @seen: measured value
603 * EXPECT_LT(expected, measured): expected < measured
605 #define EXPECT_LT(expected, seen) \
606 __EXPECT(expected, #expected, seen, #seen, <, 0)
611 * @expected: expected value
612 * @seen: measured value
614 * EXPECT_LE(expected, measured): expected <= measured
616 #define EXPECT_LE(expected, seen) \
617 __EXPECT(expected, #expected, seen, #seen, <=, 0)
622 * @expected: expected value
623 * @seen: measured value
625 * EXPECT_GT(expected, measured): expected > measured
627 #define EXPECT_GT(expected, seen) \
628 __EXPECT(expected, #expected, seen, #seen, >, 0)
633 * @expected: expected value
634 * @seen: measured value
636 * EXPECT_GE(expected, measured): expected >= measured
638 #define EXPECT_GE(expected, seen) \
639 __EXPECT(expected, #expected, seen, #seen, >=, 0)
644 * @seen: measured value
646 * EXPECT_NULL(measured): NULL == measured
648 #define EXPECT_NULL(seen) \
649 __EXPECT(NULL, "NULL", seen, #seen, ==, 0)
654 * @seen: measured value
656 * EXPECT_TRUE(measured): 0 != measured
658 #define EXPECT_TRUE(seen) \
659 __EXPECT(0, "0", seen, #seen, !=, 0)
664 * @seen: measured value
666 * EXPECT_FALSE(measured): 0 == measured
668 #define EXPECT_FALSE(seen) \
669 __EXPECT(0, "0", seen, #seen, ==, 0)
674 * @expected: expected value
675 * @seen: measured value
677 * EXPECT_STREQ(expected, measured): !strcmp(expected, measured)
679 #define EXPECT_STREQ(expected, seen) \
680 __EXPECT_STR(expected, seen, ==, 0)
685 * @expected: expected value
686 * @seen: measured value
688 * EXPECT_STRNE(expected, measured): strcmp(expected, measured)
690 #define EXPECT_STRNE(expected, seen) \
691 __EXPECT_STR(expected, seen, !=, 0)
694 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
697 /* Support an optional handler after and ASSERT_* or EXPECT_*. The approach is
698 * not thread-safe, but it should be fine in most sane test scenarios.
700 * Using __bail(), which optionally abort()s, is the easiest way to early
701 * return while still providing an optional block to the API consumer.
703 #define OPTIONAL_HANDLER(_assert) \
704 for (; _metadata->trigger; _metadata->trigger = \
705 __bail(_assert, _metadata))
707 #define is_signed_type(var) (!!(((__typeof__(var))(-1)) < (__typeof__(var))1))
709 #define __EXPECT(_expected, _expected_str, _seen, _seen_str, _t, _assert) do { \
710 /* Avoid multiple evaluation of the cases */ \
711 __typeof__(_expected) __exp = (_expected); \
712 __typeof__(_seen) __seen = (_seen); \
713 if (!(__exp _t __seen)) { \
714 /* Report with actual signedness to avoid weird output. */ \
715 switch (is_signed_type(__exp) * 2 + is_signed_type(__seen)) { \
717 unsigned long long __exp_print = (uintptr_t)__exp; \
718 unsigned long long __seen_print = (uintptr_t)__seen; \
719 __TH_LOG("Expected %s (%llu) %s %s (%llu)", \
720 _expected_str, __exp_print, #_t, \
721 _seen_str, __seen_print); \
725 unsigned long long __exp_print = (uintptr_t)__exp; \
726 long long __seen_print = (intptr_t)__seen; \
727 __TH_LOG("Expected %s (%llu) %s %s (%lld)", \
728 _expected_str, __exp_print, #_t, \
729 _seen_str, __seen_print); \
733 long long __exp_print = (intptr_t)__exp; \
734 unsigned long long __seen_print = (uintptr_t)__seen; \
735 __TH_LOG("Expected %s (%lld) %s %s (%llu)", \
736 _expected_str, __exp_print, #_t, \
737 _seen_str, __seen_print); \
741 long long __exp_print = (intptr_t)__exp; \
742 long long __seen_print = (intptr_t)__seen; \
743 __TH_LOG("Expected %s (%lld) %s %s (%lld)", \
744 _expected_str, __exp_print, #_t, \
745 _seen_str, __seen_print); \
749 _metadata->passed = 0; \
750 /* Ensure the optional handler is triggered */ \
751 _metadata->trigger = 1; \
753 } while (0); OPTIONAL_HANDLER(_assert)
755 #define __EXPECT_STR(_expected, _seen, _t, _assert) do { \
756 const char *__exp = (_expected); \
757 const char *__seen = (_seen); \
758 if (!(strcmp(__exp, __seen) _t 0)) { \
759 __TH_LOG("Expected '%s' %s '%s'.", __exp, #_t, __seen); \
760 _metadata->passed = 0; \
761 _metadata->trigger = 1; \
763 } while (0); OPTIONAL_HANDLER(_assert)
766 #define __LIST_APPEND(head, item) \
768 /* Circular linked list where only prev is circular. */ \
769 if (head == NULL) { \
775 if (__constructor_order == _CONSTRUCTOR_ORDER_FORWARD) { \
777 item->prev = head->prev; \
778 item->prev->next = item; \
782 item->next->prev = item; \
788 struct __test_results {
789 char reason[1024]; /* Reason for test result */
792 struct __test_metadata;
793 struct __fixture_variant_metadata;
795 /* Contains all the information about a fixture. */
796 struct __fixture_metadata {
798 struct __test_metadata *tests;
799 struct __fixture_variant_metadata *variant;
800 struct __fixture_metadata *prev, *next;
801 } _fixture_global __attribute__((unused)) = {
803 .prev = &_fixture_global,
806 static struct __fixture_metadata *__fixture_list = &_fixture_global;
807 static int __constructor_order;
809 #define _CONSTRUCTOR_ORDER_FORWARD 1
810 #define _CONSTRUCTOR_ORDER_BACKWARD -1
812 static inline void __register_fixture(struct __fixture_metadata *f)
814 __LIST_APPEND(__fixture_list, f);
817 struct __fixture_variant_metadata {
820 struct __fixture_variant_metadata *prev, *next;
824 __register_fixture_variant(struct __fixture_metadata *f,
825 struct __fixture_variant_metadata *variant)
827 __LIST_APPEND(f->variant, variant);
830 /* Contains all the information for test execution and status checking. */
831 struct __test_metadata {
833 void (*fn)(struct __test_metadata *,
834 struct __fixture_variant_metadata *);
835 pid_t pid; /* pid of test when being run */
836 struct __fixture_metadata *fixture;
839 int skip; /* did SKIP get used? */
840 int trigger; /* extra handler after the evaluation */
841 int timeout; /* seconds to wait for test timeout */
842 bool timed_out; /* did this test timeout instead of exiting? */
843 bool aborted; /* stopped test due to failed ASSERT */
844 bool setup_completed; /* did setup finish? */
845 jmp_buf env; /* for exiting out of test early */
846 struct __test_results *results;
847 struct __test_metadata *prev, *next;
851 * Since constructors are called in reverse order, reverse the test
852 * list so tests are run in source declaration order.
853 * https://gcc.gnu.org/onlinedocs/gccint/Initialization.html
854 * However, it seems not all toolchains do this correctly, so use
855 * __constructor_order to detect which direction is called first
856 * and adjust list building logic to get things running in the right
859 static inline void __register_test(struct __test_metadata *t)
861 __LIST_APPEND(t->fixture->tests, t);
864 static inline int __bail(int for_realz, struct __test_metadata *t)
866 /* if this is ASSERT, return immediately. */
871 /* otherwise, end the for loop and continue. */
875 static inline void __test_check_assert(struct __test_metadata *t)
881 struct __test_metadata *__active_test;
882 static void __timeout_handler(int sig, siginfo_t *info, void *ucontext)
884 struct __test_metadata *t = __active_test;
886 /* Sanity check handler execution environment. */
888 fprintf(TH_LOG_STREAM,
889 "# no active test in SIGALRM handler!?\n");
892 if (sig != SIGALRM || sig != info->si_signo) {
893 fprintf(TH_LOG_STREAM,
894 "# %s: SIGALRM handler caught signal %d!?\n",
895 t->name, sig != SIGALRM ? sig : info->si_signo);
900 // signal process group
901 kill(-(t->pid), SIGKILL);
904 void __wait_for_test(struct __test_metadata *t)
906 struct sigaction action = {
907 .sa_sigaction = __timeout_handler,
908 .sa_flags = SA_SIGINFO,
910 struct sigaction saved_action;
913 if (sigaction(SIGALRM, &action, &saved_action)) {
915 fprintf(TH_LOG_STREAM,
916 "# %s: unable to install SIGALRM handler\n",
921 t->timed_out = false;
923 waitpid(t->pid, &status, 0);
925 if (sigaction(SIGALRM, &saved_action, NULL)) {
927 fprintf(TH_LOG_STREAM,
928 "# %s: unable to uninstall SIGALRM handler\n",
932 __active_test = NULL;
936 fprintf(TH_LOG_STREAM,
937 "# %s: Test terminated by timeout\n", t->name);
938 } else if (WIFEXITED(status)) {
939 if (WEXITSTATUS(status) == KSFT_SKIP) {
943 } else if (t->termsig != -1) {
945 fprintf(TH_LOG_STREAM,
946 "# %s: Test exited normally instead of by signal (code: %d)\n",
948 WEXITSTATUS(status));
950 switch (WEXITSTATUS(status)) {
958 fprintf(TH_LOG_STREAM,
959 "# %s: Test failed\n",
963 } else if (WIFSIGNALED(status)) {
965 if (WTERMSIG(status) == SIGABRT) {
966 fprintf(TH_LOG_STREAM,
967 "# %s: Test terminated by assertion\n",
969 } else if (WTERMSIG(status) == t->termsig) {
972 fprintf(TH_LOG_STREAM,
973 "# %s: Test terminated unexpectedly by signal %d\n",
978 fprintf(TH_LOG_STREAM,
979 "# %s: Test ended in some other way [%u]\n",
985 static void test_harness_list_tests(void)
987 struct __fixture_variant_metadata *v;
988 struct __fixture_metadata *f;
989 struct __test_metadata *t;
991 for (f = __fixture_list; f; f = f->next) {
995 if (f == __fixture_list)
996 fprintf(stderr, "%-20s %-25s %s\n",
997 "# FIXTURE", "VARIANT", "TEST");
999 fprintf(stderr, "--------------------------------------------------------------------------------\n");
1002 fprintf(stderr, "%-20s %-25s %s\n",
1003 t == f->tests ? f->name : "",
1007 v = v ? v->next : NULL;
1008 t = t ? t->next : NULL;
1013 static int test_harness_argv_check(int argc, char **argv)
1017 while ((opt = getopt(argc, argv, "hlF:f:V:v:t:T:r:")) != -1) {
1028 test_harness_list_tests();
1033 "Usage: %s [-h|-l] [-t|-T|-v|-V|-f|-F|-r name]\n"
1035 "\t-l list all tests\n"
1037 "\t-t name include test\n"
1038 "\t-T name exclude test\n"
1039 "\t-v name include variant\n"
1040 "\t-V name exclude variant\n"
1041 "\t-f name include fixture\n"
1042 "\t-F name exclude fixture\n"
1043 "\t-r name run specified test\n"
1045 "Test filter options can be specified "
1046 "multiple times. The filtering stops\n"
1047 "at the first match. For example to "
1048 "include all tests from variant 'bla'\n"
1049 "but not test 'foo' specify '-T foo -v bla'.\n"
1051 return opt == 'h' ? KSFT_SKIP : KSFT_FAIL;
1058 static bool test_enabled(int argc, char **argv,
1059 struct __fixture_metadata *f,
1060 struct __fixture_variant_metadata *v,
1061 struct __test_metadata *t)
1063 unsigned int flen = 0, vlen = 0, tlen = 0;
1064 bool has_positive = false;
1068 while ((opt = getopt(argc, argv, "F:f:V:v:t:T:r:")) != -1) {
1069 has_positive |= islower(opt);
1071 switch (tolower(opt)) {
1073 if (!strcmp(t->name, optarg))
1074 return islower(opt);
1077 if (!strcmp(f->name, optarg))
1078 return islower(opt);
1081 if (!strcmp(v->name, optarg))
1082 return islower(opt);
1086 flen = strlen(f->name);
1087 vlen = strlen(v->name);
1088 tlen = strlen(t->name);
1090 if (strlen(optarg) == flen + 1 + vlen + !!vlen + tlen &&
1091 !strncmp(f->name, &optarg[0], flen) &&
1092 !strncmp(v->name, &optarg[flen + 1], vlen) &&
1093 !strncmp(t->name, &optarg[flen + 1 + vlen + !!vlen], tlen))
1100 * If there are no positive tests then we assume user just wants
1101 * exclusions and everything else is a pass.
1103 return !has_positive;
1106 void __run_test(struct __fixture_metadata *f,
1107 struct __fixture_variant_metadata *variant,
1108 struct __test_metadata *t)
1110 /* reset test struct */
1114 memset(t->results->reason, 0, sizeof(t->results->reason));
1116 ksft_print_msg(" RUN %s%s%s.%s ...\n",
1117 f->name, variant->name[0] ? "." : "", variant->name, t->name);
1119 /* Make sure output buffers are flushed before fork */
1125 ksft_print_msg("ERROR SPAWNING TEST CHILD\n");
1127 } else if (t->pid == 0) {
1138 ksft_print_msg(" %4s %s%s%s.%s\n", t->passed ? "OK" : "FAIL",
1139 f->name, variant->name[0] ? "." : "", variant->name, t->name);
1142 ksft_test_result_skip("%s\n", t->results->reason[0] ?
1143 t->results->reason : "unknown");
1145 ksft_test_result(t->passed, "%s%s%s.%s\n",
1146 f->name, variant->name[0] ? "." : "", variant->name, t->name);
1149 static int test_harness_run(int argc, char **argv)
1151 struct __fixture_variant_metadata no_variant = { .name = "", };
1152 struct __fixture_variant_metadata *v;
1153 struct __fixture_metadata *f;
1154 struct __test_results *results;
1155 struct __test_metadata *t;
1157 unsigned int case_count = 0, test_count = 0;
1158 unsigned int count = 0;
1159 unsigned int pass_count = 0;
1161 ret = test_harness_argv_check(argc, argv);
1162 if (ret != KSFT_PASS)
1165 for (f = __fixture_list; f; f = f->next) {
1166 for (v = f->variant ?: &no_variant; v; v = v->next) {
1167 unsigned int old_tests = test_count;
1169 for (t = f->tests; t; t = t->next)
1170 if (test_enabled(argc, argv, f, v, t))
1173 if (old_tests != test_count)
1178 results = mmap(NULL, sizeof(*results), PROT_READ | PROT_WRITE,
1179 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1181 ksft_print_header();
1182 ksft_set_plan(test_count);
1183 ksft_print_msg("Starting %u tests from %u test cases.\n",
1184 test_count, case_count);
1185 for (f = __fixture_list; f; f = f->next) {
1186 for (v = f->variant ?: &no_variant; v; v = v->next) {
1187 for (t = f->tests; t; t = t->next) {
1188 if (!test_enabled(argc, argv, f, v, t))
1191 t->results = results;
1192 __run_test(f, v, t);
1201 munmap(results, sizeof(*results));
1203 ksft_print_msg("%s: %u / %u tests passed.\n", ret ? "FAILED" : "PASSED",
1205 ksft_exit(ret == 0);
1211 static void __attribute__((constructor)) __constructor_order_first(void)
1213 if (!__constructor_order)
1214 __constructor_order = _CONSTRUCTOR_ORDER_FORWARD;
1217 #endif /* __KSELFTEST_HARNESS_H */