Thomas Gleixner | e500db3 | 2019-06-04 10:11:14 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 4 | * |
Mickaël Salaün | dfa47d3 | 2017-05-26 20:43:57 +0200 | [diff] [blame] | 5 | * kselftest_harness.h: simple C unit test helper. |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 6 | * |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 7 | * See documentation in Documentation/dev-tools/kselftest.rst |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 8 | * |
| 9 | * API inspired by code.google.com/p/googletest |
| 10 | */ |
Mickaël Salaün | dfa47d3 | 2017-05-26 20:43:57 +0200 | [diff] [blame] | 11 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 12 | /** |
| 13 | * DOC: example |
| 14 | * |
| 15 | * .. code-block:: c |
| 16 | * |
| 17 | * #include "../kselftest_harness.h" |
| 18 | * |
| 19 | * TEST(standalone_test) { |
| 20 | * do_some_stuff; |
| 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); |
| 25 | * } |
| 26 | * more_stuff; |
| 27 | * ASSERT_NE(some_stuff, NULL) TH_LOG("how did it happen?!"); |
| 28 | * last_stuff; |
| 29 | * EXPECT_EQ(0, last_stuff); |
| 30 | * } |
| 31 | * |
| 32 | * FIXTURE(my_fixture) { |
| 33 | * mytype_t *data; |
| 34 | * int awesomeness_level; |
| 35 | * }; |
| 36 | * FIXTURE_SETUP(my_fixture) { |
| 37 | * self->data = mytype_new(); |
| 38 | * ASSERT_NE(NULL, self->data); |
| 39 | * } |
| 40 | * FIXTURE_TEARDOWN(my_fixture) { |
| 41 | * mytype_free(self->data); |
| 42 | * } |
| 43 | * TEST_F(my_fixture, data_is_good) { |
| 44 | * EXPECT_EQ(1, is_my_data_good(self->data)); |
| 45 | * } |
| 46 | * |
| 47 | * TEST_HARNESS_MAIN |
| 48 | */ |
| 49 | |
Mickaël Salaün | dfa47d3 | 2017-05-26 20:43:57 +0200 | [diff] [blame] | 50 | #ifndef __KSELFTEST_HARNESS_H |
| 51 | #define __KSELFTEST_HARNESS_H |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 52 | |
| 53 | #define _GNU_SOURCE |
Mickaël Salaün | 369130b | 2017-08-07 01:23:37 +0200 | [diff] [blame] | 54 | #include <asm/types.h> |
| 55 | #include <errno.h> |
| 56 | #include <stdbool.h> |
Kees Cook | b5bb6d3 | 2015-12-10 14:50:25 -0800 | [diff] [blame] | 57 | #include <stdint.h> |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 58 | #include <stdio.h> |
| 59 | #include <stdlib.h> |
| 60 | #include <string.h> |
| 61 | #include <sys/types.h> |
| 62 | #include <sys/wait.h> |
| 63 | #include <unistd.h> |
| 64 | |
Alexandre Belloni | d51f1f1 | 2019-05-24 00:42:22 +0200 | [diff] [blame] | 65 | #define TEST_TIMEOUT_DEFAULT 30 |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 66 | |
| 67 | /* Utilities exposed to the test definitions */ |
| 68 | #ifndef TH_LOG_STREAM |
| 69 | # define TH_LOG_STREAM stderr |
| 70 | #endif |
| 71 | |
| 72 | #ifndef TH_LOG_ENABLED |
| 73 | # define TH_LOG_ENABLED 1 |
| 74 | #endif |
| 75 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 76 | /** |
| 77 | * TH_LOG(fmt, ...) |
| 78 | * |
| 79 | * @fmt: format string |
| 80 | * @...: optional arguments |
| 81 | * |
| 82 | * .. code-block:: c |
| 83 | * |
| 84 | * TH_LOG(format, ...) |
| 85 | * |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 86 | * Optional debug logging function available for use in tests. |
| 87 | * Logging may be enabled or disabled by defining TH_LOG_ENABLED. |
| 88 | * E.g., #define TH_LOG_ENABLED 1 |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 89 | * |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 90 | * If no definition is provided, logging is enabled by default. |
Mickaël Salaün | 369130b | 2017-08-07 01:23:37 +0200 | [diff] [blame] | 91 | * |
| 92 | * If there is no way to print an error message for the process running the |
| 93 | * test (e.g. not allowed to write to stderr), it is still possible to get the |
| 94 | * ASSERT_* number for which the test failed. This behavior can be enabled by |
| 95 | * writing `_metadata->no_print = true;` before the check sequence that is |
| 96 | * unable to print. When an error occur, instead of printing an error message |
| 97 | * and calling `abort(3)`, the test process call `_exit(2)` with the assert |
| 98 | * number as argument, which is then printed by the parent process. |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 99 | */ |
| 100 | #define TH_LOG(fmt, ...) do { \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 101 | if (TH_LOG_ENABLED) \ |
| 102 | __TH_LOG(fmt, ##__VA_ARGS__); \ |
| 103 | } while (0) |
| 104 | |
| 105 | /* Unconditional logger for internal use. */ |
| 106 | #define __TH_LOG(fmt, ...) \ |
| 107 | fprintf(TH_LOG_STREAM, "%s:%d:%s:" fmt "\n", \ |
| 108 | __FILE__, __LINE__, _metadata->name, ##__VA_ARGS__) |
| 109 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 110 | /** |
Kees Cook | 6c3b6d5 | 2018-03-15 09:59:16 -0700 | [diff] [blame] | 111 | * XFAIL(statement, fmt, ...) |
| 112 | * |
| 113 | * @statement: statement to run after reporting XFAIL |
| 114 | * @fmt: format string |
| 115 | * @...: optional arguments |
| 116 | * |
| 117 | * This forces a "pass" after reporting a failure with an XFAIL prefix, |
| 118 | * and runs "statement", which is usually "return" or "goto skip". |
| 119 | */ |
| 120 | #define XFAIL(statement, fmt, ...) do { \ |
| 121 | if (TH_LOG_ENABLED) { \ |
| 122 | fprintf(TH_LOG_STREAM, "[ XFAIL! ] " fmt "\n", \ |
| 123 | ##__VA_ARGS__); \ |
| 124 | } \ |
| 125 | /* TODO: find a way to pass xfail to test runner process. */ \ |
| 126 | _metadata->passed = 1; \ |
| 127 | _metadata->trigger = 0; \ |
| 128 | statement; \ |
| 129 | } while (0) |
| 130 | |
| 131 | /** |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 132 | * TEST(test_name) - Defines the test function and creates the registration |
| 133 | * stub |
| 134 | * |
| 135 | * @test_name: test name |
| 136 | * |
| 137 | * .. code-block:: c |
| 138 | * |
| 139 | * TEST(name) { implementation } |
| 140 | * |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 141 | * Defines a test by name. |
| 142 | * Names must be unique and tests must not be run in parallel. The |
| 143 | * implementation containing block is a function and scoping should be treated |
| 144 | * as such. Returning early may be performed with a bare "return;" statement. |
| 145 | * |
| 146 | * EXPECT_* and ASSERT_* are valid in a TEST() { } context. |
| 147 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 148 | #define TEST(test_name) __TEST_IMPL(test_name, -1) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 149 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 150 | /** |
| 151 | * TEST_SIGNAL(test_name, signal) |
| 152 | * |
| 153 | * @test_name: test name |
| 154 | * @signal: signal number |
| 155 | * |
| 156 | * .. code-block:: c |
| 157 | * |
| 158 | * TEST_SIGNAL(name, signal) { implementation } |
| 159 | * |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 160 | * Defines a test by name and the expected term signal. |
| 161 | * Names must be unique and tests must not be run in parallel. The |
| 162 | * implementation containing block is a function and scoping should be treated |
| 163 | * as such. Returning early may be performed with a bare "return;" statement. |
| 164 | * |
| 165 | * EXPECT_* and ASSERT_* are valid in a TEST() { } context. |
| 166 | */ |
| 167 | #define TEST_SIGNAL(test_name, signal) __TEST_IMPL(test_name, signal) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 168 | |
| 169 | #define __TEST_IMPL(test_name, _signal) \ |
| 170 | static void test_name(struct __test_metadata *_metadata); \ |
| 171 | static struct __test_metadata _##test_name##_object = \ |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame^] | 172 | { .name = #test_name, \ |
| 173 | .fn = &test_name, \ |
| 174 | .fixture = &_fixture_global, \ |
| 175 | .termsig = _signal, \ |
Alexandre Belloni | d51f1f1 | 2019-05-24 00:42:22 +0200 | [diff] [blame] | 176 | .timeout = TEST_TIMEOUT_DEFAULT, }; \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 177 | static void __attribute__((constructor)) _register_##test_name(void) \ |
| 178 | { \ |
| 179 | __register_test(&_##test_name##_object); \ |
| 180 | } \ |
| 181 | static void test_name( \ |
| 182 | struct __test_metadata __attribute__((unused)) *_metadata) |
| 183 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 184 | /** |
| 185 | * FIXTURE_DATA(datatype_name) - Wraps the struct name so we have one less |
| 186 | * argument to pass around |
| 187 | * |
| 188 | * @datatype_name: datatype name |
| 189 | * |
| 190 | * .. code-block:: c |
| 191 | * |
| 192 | * FIXTURE_DATA(datatype name) |
| 193 | * |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 194 | * This call may be used when the type of the fixture data |
| 195 | * is needed. In general, this should not be needed unless |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 196 | * the *self* is being passed to a helper directly. |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 197 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 198 | #define FIXTURE_DATA(datatype_name) struct _test_data_##datatype_name |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 199 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 200 | /** |
| 201 | * FIXTURE(fixture_name) - Called once per fixture to setup the data and |
| 202 | * register |
| 203 | * |
| 204 | * @fixture_name: fixture name |
| 205 | * |
| 206 | * .. code-block:: c |
| 207 | * |
| 208 | * FIXTURE(datatype name) { |
| 209 | * type property1; |
| 210 | * ... |
| 211 | * }; |
| 212 | * |
| 213 | * Defines the data provided to TEST_F()-defined tests as *self*. It should be |
| 214 | * populated and cleaned up using FIXTURE_SETUP() and FIXTURE_TEARDOWN(). |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 215 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 216 | #define FIXTURE(fixture_name) \ |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame^] | 217 | static struct __fixture_metadata _##fixture_name##_fixture_object = \ |
| 218 | { .name = #fixture_name, }; \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 219 | static void __attribute__((constructor)) \ |
| 220 | _register_##fixture_name##_data(void) \ |
| 221 | { \ |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame^] | 222 | __register_fixture(&_##fixture_name##_fixture_object); \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 223 | } \ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 224 | FIXTURE_DATA(fixture_name) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 225 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 226 | /** |
| 227 | * FIXTURE_SETUP(fixture_name) - Prepares the setup function for the fixture. |
Kees Cook | 6c3b6d5 | 2018-03-15 09:59:16 -0700 | [diff] [blame] | 228 | * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly. |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 229 | * |
| 230 | * @fixture_name: fixture name |
| 231 | * |
| 232 | * .. code-block:: c |
| 233 | * |
| 234 | * FIXTURE_SETUP(fixture name) { implementation } |
| 235 | * |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 236 | * Populates the required "setup" function for a fixture. An instance of the |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 237 | * datatype defined with FIXTURE_DATA() will be exposed as *self* for the |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 238 | * implementation. |
| 239 | * |
| 240 | * ASSERT_* are valid for use in this context and will prempt the execution |
| 241 | * of any dependent fixture tests. |
| 242 | * |
| 243 | * A bare "return;" statement may be used to return early. |
| 244 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 245 | #define FIXTURE_SETUP(fixture_name) \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 246 | void fixture_name##_setup( \ |
| 247 | struct __test_metadata __attribute__((unused)) *_metadata, \ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 248 | FIXTURE_DATA(fixture_name) __attribute__((unused)) *self) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 249 | /** |
| 250 | * FIXTURE_TEARDOWN(fixture_name) |
Kees Cook | 6c3b6d5 | 2018-03-15 09:59:16 -0700 | [diff] [blame] | 251 | * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly. |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 252 | * |
| 253 | * @fixture_name: fixture name |
| 254 | * |
| 255 | * .. code-block:: c |
| 256 | * |
| 257 | * FIXTURE_TEARDOWN(fixture name) { implementation } |
| 258 | * |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 259 | * Populates the required "teardown" function for a fixture. An instance of the |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 260 | * datatype defined with FIXTURE_DATA() will be exposed as *self* for the |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 261 | * implementation to clean up. |
| 262 | * |
| 263 | * A bare "return;" statement may be used to return early. |
| 264 | */ |
| 265 | #define FIXTURE_TEARDOWN(fixture_name) \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 266 | void fixture_name##_teardown( \ |
| 267 | struct __test_metadata __attribute__((unused)) *_metadata, \ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 268 | FIXTURE_DATA(fixture_name) __attribute__((unused)) *self) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 269 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 270 | /** |
| 271 | * TEST_F(fixture_name, test_name) - Emits test registration and helpers for |
| 272 | * fixture-based test cases |
| 273 | * |
| 274 | * @fixture_name: fixture name |
| 275 | * @test_name: test name |
| 276 | * |
| 277 | * .. code-block:: c |
| 278 | * |
| 279 | * TEST_F(fixture, name) { implementation } |
| 280 | * |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 281 | * Defines a test that depends on a fixture (e.g., is part of a test case). |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 282 | * Very similar to TEST() except that *self* is the setup instance of fixture's |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 283 | * datatype exposed for use by the implementation. |
Kees Cook | 6c3b6d5 | 2018-03-15 09:59:16 -0700 | [diff] [blame] | 284 | * |
| 285 | * Warning: use of ASSERT_* here will skip TEARDOWN. |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 286 | */ |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 287 | /* TODO(wad) register fixtures on dedicated test lists. */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 288 | #define TEST_F(fixture_name, test_name) \ |
Alexandre Belloni | d51f1f1 | 2019-05-24 00:42:22 +0200 | [diff] [blame] | 289 | __TEST_F_IMPL(fixture_name, test_name, -1, TEST_TIMEOUT_DEFAULT) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 290 | |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 291 | #define TEST_F_SIGNAL(fixture_name, test_name, signal) \ |
Alexandre Belloni | d51f1f1 | 2019-05-24 00:42:22 +0200 | [diff] [blame] | 292 | __TEST_F_IMPL(fixture_name, test_name, signal, TEST_TIMEOUT_DEFAULT) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 293 | |
Alexandre Belloni | d51f1f1 | 2019-05-24 00:42:22 +0200 | [diff] [blame] | 294 | #define TEST_F_TIMEOUT(fixture_name, test_name, timeout) \ |
| 295 | __TEST_F_IMPL(fixture_name, test_name, -1, timeout) |
| 296 | |
| 297 | #define __TEST_F_IMPL(fixture_name, test_name, signal, tmout) \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 298 | static void fixture_name##_##test_name( \ |
| 299 | struct __test_metadata *_metadata, \ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 300 | FIXTURE_DATA(fixture_name) *self); \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 301 | static inline void wrapper_##fixture_name##_##test_name( \ |
| 302 | struct __test_metadata *_metadata) \ |
| 303 | { \ |
| 304 | /* fixture data is alloced, setup, and torn down per call. */ \ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 305 | FIXTURE_DATA(fixture_name) self; \ |
| 306 | memset(&self, 0, sizeof(FIXTURE_DATA(fixture_name))); \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 307 | fixture_name##_setup(_metadata, &self); \ |
| 308 | /* Let setup failure terminate early. */ \ |
| 309 | if (!_metadata->passed) \ |
| 310 | return; \ |
| 311 | fixture_name##_##test_name(_metadata, &self); \ |
| 312 | fixture_name##_teardown(_metadata, &self); \ |
| 313 | } \ |
| 314 | static struct __test_metadata \ |
| 315 | _##fixture_name##_##test_name##_object = { \ |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame^] | 316 | .name = #test_name, \ |
Kees Cook | 121e357 | 2019-01-27 01:42:51 -0800 | [diff] [blame] | 317 | .fn = &wrapper_##fixture_name##_##test_name, \ |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame^] | 318 | .fixture = &_##fixture_name##_fixture_object, \ |
Kees Cook | 121e357 | 2019-01-27 01:42:51 -0800 | [diff] [blame] | 319 | .termsig = signal, \ |
Alexandre Belloni | d51f1f1 | 2019-05-24 00:42:22 +0200 | [diff] [blame] | 320 | .timeout = tmout, \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 321 | }; \ |
| 322 | static void __attribute__((constructor)) \ |
| 323 | _register_##fixture_name##_##test_name(void) \ |
| 324 | { \ |
| 325 | __register_test(&_##fixture_name##_##test_name##_object); \ |
| 326 | } \ |
| 327 | static void fixture_name##_##test_name( \ |
| 328 | struct __test_metadata __attribute__((unused)) *_metadata, \ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 329 | FIXTURE_DATA(fixture_name) __attribute__((unused)) *self) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 330 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 331 | /** |
| 332 | * TEST_HARNESS_MAIN - Simple wrapper to run the test harness |
| 333 | * |
| 334 | * .. code-block:: c |
| 335 | * |
| 336 | * TEST_HARNESS_MAIN |
| 337 | * |
| 338 | * Use once to append a main() to the test file. |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 339 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 340 | #define TEST_HARNESS_MAIN \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 341 | static void __attribute__((constructor)) \ |
| 342 | __constructor_order_last(void) \ |
| 343 | { \ |
| 344 | if (!__constructor_order) \ |
| 345 | __constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; \ |
| 346 | } \ |
| 347 | int main(int argc, char **argv) { \ |
| 348 | return test_harness_run(argc, argv); \ |
| 349 | } |
| 350 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 351 | /** |
| 352 | * DOC: operators |
| 353 | * |
| 354 | * Operators for use in TEST() and TEST_F(). |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 355 | * ASSERT_* calls will stop test execution immediately. |
| 356 | * EXPECT_* calls will emit a failure warning, note it, and continue. |
| 357 | */ |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 358 | |
| 359 | /** |
| 360 | * ASSERT_EQ(expected, seen) |
| 361 | * |
| 362 | * @expected: expected value |
| 363 | * @seen: measured value |
| 364 | * |
| 365 | * ASSERT_EQ(expected, measured): expected == measured |
| 366 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 367 | #define ASSERT_EQ(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 368 | __EXPECT(expected, #expected, seen, #seen, ==, 1) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 369 | |
| 370 | /** |
| 371 | * ASSERT_NE(expected, seen) |
| 372 | * |
| 373 | * @expected: expected value |
| 374 | * @seen: measured value |
| 375 | * |
| 376 | * ASSERT_NE(expected, measured): expected != measured |
| 377 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 378 | #define ASSERT_NE(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 379 | __EXPECT(expected, #expected, seen, #seen, !=, 1) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 380 | |
| 381 | /** |
| 382 | * ASSERT_LT(expected, seen) |
| 383 | * |
| 384 | * @expected: expected value |
| 385 | * @seen: measured value |
| 386 | * |
| 387 | * ASSERT_LT(expected, measured): expected < measured |
| 388 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 389 | #define ASSERT_LT(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 390 | __EXPECT(expected, #expected, seen, #seen, <, 1) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 391 | |
| 392 | /** |
| 393 | * ASSERT_LE(expected, seen) |
| 394 | * |
| 395 | * @expected: expected value |
| 396 | * @seen: measured value |
| 397 | * |
| 398 | * ASSERT_LE(expected, measured): expected <= measured |
| 399 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 400 | #define ASSERT_LE(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 401 | __EXPECT(expected, #expected, seen, #seen, <=, 1) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 402 | |
| 403 | /** |
| 404 | * ASSERT_GT(expected, seen) |
| 405 | * |
| 406 | * @expected: expected value |
| 407 | * @seen: measured value |
| 408 | * |
| 409 | * ASSERT_GT(expected, measured): expected > measured |
| 410 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 411 | #define ASSERT_GT(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 412 | __EXPECT(expected, #expected, seen, #seen, >, 1) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 413 | |
| 414 | /** |
| 415 | * ASSERT_GE(expected, seen) |
| 416 | * |
| 417 | * @expected: expected value |
| 418 | * @seen: measured value |
| 419 | * |
| 420 | * ASSERT_GE(expected, measured): expected >= measured |
| 421 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 422 | #define ASSERT_GE(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 423 | __EXPECT(expected, #expected, seen, #seen, >=, 1) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 424 | |
| 425 | /** |
| 426 | * ASSERT_NULL(seen) |
| 427 | * |
| 428 | * @seen: measured value |
| 429 | * |
| 430 | * ASSERT_NULL(measured): NULL == measured |
| 431 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 432 | #define ASSERT_NULL(seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 433 | __EXPECT(NULL, "NULL", seen, #seen, ==, 1) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 434 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 435 | /** |
| 436 | * ASSERT_TRUE(seen) |
| 437 | * |
| 438 | * @seen: measured value |
| 439 | * |
| 440 | * ASSERT_TRUE(measured): measured != 0 |
| 441 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 442 | #define ASSERT_TRUE(seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 443 | __EXPECT(0, "0", seen, #seen, !=, 1) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 444 | |
| 445 | /** |
| 446 | * ASSERT_FALSE(seen) |
| 447 | * |
| 448 | * @seen: measured value |
| 449 | * |
| 450 | * ASSERT_FALSE(measured): measured == 0 |
| 451 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 452 | #define ASSERT_FALSE(seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 453 | __EXPECT(0, "0", seen, #seen, ==, 1) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 454 | |
| 455 | /** |
| 456 | * ASSERT_STREQ(expected, seen) |
| 457 | * |
| 458 | * @expected: expected value |
| 459 | * @seen: measured value |
| 460 | * |
| 461 | * ASSERT_STREQ(expected, measured): !strcmp(expected, measured) |
| 462 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 463 | #define ASSERT_STREQ(expected, seen) \ |
| 464 | __EXPECT_STR(expected, seen, ==, 1) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 465 | |
| 466 | /** |
| 467 | * ASSERT_STRNE(expected, seen) |
| 468 | * |
| 469 | * @expected: expected value |
| 470 | * @seen: measured value |
| 471 | * |
| 472 | * ASSERT_STRNE(expected, measured): strcmp(expected, measured) |
| 473 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 474 | #define ASSERT_STRNE(expected, seen) \ |
| 475 | __EXPECT_STR(expected, seen, !=, 1) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 476 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 477 | /** |
| 478 | * EXPECT_EQ(expected, seen) |
| 479 | * |
| 480 | * @expected: expected value |
| 481 | * @seen: measured value |
| 482 | * |
| 483 | * EXPECT_EQ(expected, measured): expected == measured |
| 484 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 485 | #define EXPECT_EQ(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 486 | __EXPECT(expected, #expected, seen, #seen, ==, 0) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 487 | |
| 488 | /** |
| 489 | * EXPECT_NE(expected, seen) |
| 490 | * |
| 491 | * @expected: expected value |
| 492 | * @seen: measured value |
| 493 | * |
| 494 | * EXPECT_NE(expected, measured): expected != measured |
| 495 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 496 | #define EXPECT_NE(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 497 | __EXPECT(expected, #expected, seen, #seen, !=, 0) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 498 | |
| 499 | /** |
| 500 | * EXPECT_LT(expected, seen) |
| 501 | * |
| 502 | * @expected: expected value |
| 503 | * @seen: measured value |
| 504 | * |
| 505 | * EXPECT_LT(expected, measured): expected < measured |
| 506 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 507 | #define EXPECT_LT(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 508 | __EXPECT(expected, #expected, seen, #seen, <, 0) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 509 | |
| 510 | /** |
| 511 | * EXPECT_LE(expected, seen) |
| 512 | * |
| 513 | * @expected: expected value |
| 514 | * @seen: measured value |
| 515 | * |
| 516 | * EXPECT_LE(expected, measured): expected <= measured |
| 517 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 518 | #define EXPECT_LE(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 519 | __EXPECT(expected, #expected, seen, #seen, <=, 0) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 520 | |
| 521 | /** |
| 522 | * EXPECT_GT(expected, seen) |
| 523 | * |
| 524 | * @expected: expected value |
| 525 | * @seen: measured value |
| 526 | * |
| 527 | * EXPECT_GT(expected, measured): expected > measured |
| 528 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 529 | #define EXPECT_GT(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 530 | __EXPECT(expected, #expected, seen, #seen, >, 0) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 531 | |
| 532 | /** |
| 533 | * EXPECT_GE(expected, seen) |
| 534 | * |
| 535 | * @expected: expected value |
| 536 | * @seen: measured value |
| 537 | * |
| 538 | * EXPECT_GE(expected, measured): expected >= measured |
| 539 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 540 | #define EXPECT_GE(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 541 | __EXPECT(expected, #expected, seen, #seen, >=, 0) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 542 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 543 | /** |
| 544 | * EXPECT_NULL(seen) |
| 545 | * |
| 546 | * @seen: measured value |
| 547 | * |
| 548 | * EXPECT_NULL(measured): NULL == measured |
| 549 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 550 | #define EXPECT_NULL(seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 551 | __EXPECT(NULL, "NULL", seen, #seen, ==, 0) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 552 | |
| 553 | /** |
| 554 | * EXPECT_TRUE(seen) |
| 555 | * |
| 556 | * @seen: measured value |
| 557 | * |
| 558 | * EXPECT_TRUE(measured): 0 != measured |
| 559 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 560 | #define EXPECT_TRUE(seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 561 | __EXPECT(0, "0", seen, #seen, !=, 0) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 562 | |
| 563 | /** |
| 564 | * EXPECT_FALSE(seen) |
| 565 | * |
| 566 | * @seen: measured value |
| 567 | * |
| 568 | * EXPECT_FALSE(measured): 0 == measured |
| 569 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 570 | #define EXPECT_FALSE(seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 571 | __EXPECT(0, "0", seen, #seen, ==, 0) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 572 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 573 | /** |
| 574 | * EXPECT_STREQ(expected, seen) |
| 575 | * |
| 576 | * @expected: expected value |
| 577 | * @seen: measured value |
| 578 | * |
| 579 | * EXPECT_STREQ(expected, measured): !strcmp(expected, measured) |
| 580 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 581 | #define EXPECT_STREQ(expected, seen) \ |
| 582 | __EXPECT_STR(expected, seen, ==, 0) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 583 | |
| 584 | /** |
| 585 | * EXPECT_STRNE(expected, seen) |
| 586 | * |
| 587 | * @expected: expected value |
| 588 | * @seen: measured value |
| 589 | * |
| 590 | * EXPECT_STRNE(expected, measured): strcmp(expected, measured) |
| 591 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 592 | #define EXPECT_STRNE(expected, seen) \ |
| 593 | __EXPECT_STR(expected, seen, !=, 0) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 594 | |
| 595 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 596 | |
| 597 | /* Support an optional handler after and ASSERT_* or EXPECT_*. The approach is |
| 598 | * not thread-safe, but it should be fine in most sane test scenarios. |
| 599 | * |
| 600 | * Using __bail(), which optionally abort()s, is the easiest way to early |
| 601 | * return while still providing an optional block to the API consumer. |
| 602 | */ |
| 603 | #define OPTIONAL_HANDLER(_assert) \ |
Mickaël Salaün | 369130b | 2017-08-07 01:23:37 +0200 | [diff] [blame] | 604 | for (; _metadata->trigger; _metadata->trigger = \ |
| 605 | __bail(_assert, _metadata->no_print, _metadata->step)) |
| 606 | |
| 607 | #define __INC_STEP(_metadata) \ |
| 608 | if (_metadata->passed && _metadata->step < 255) \ |
| 609 | _metadata->step++; |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 610 | |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 611 | #define __EXPECT(_expected, _expected_str, _seen, _seen_str, _t, _assert) do { \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 612 | /* Avoid multiple evaluation of the cases */ \ |
| 613 | __typeof__(_expected) __exp = (_expected); \ |
| 614 | __typeof__(_seen) __seen = (_seen); \ |
Mickaël Salaün | 369130b | 2017-08-07 01:23:37 +0200 | [diff] [blame] | 615 | if (_assert) __INC_STEP(_metadata); \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 616 | if (!(__exp _t __seen)) { \ |
Kees Cook | b5bb6d3 | 2015-12-10 14:50:25 -0800 | [diff] [blame] | 617 | unsigned long long __exp_print = (uintptr_t)__exp; \ |
| 618 | unsigned long long __seen_print = (uintptr_t)__seen; \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 619 | __TH_LOG("Expected %s (%llu) %s %s (%llu)", \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 620 | _expected_str, __exp_print, #_t, \ |
| 621 | _seen_str, __seen_print); \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 622 | _metadata->passed = 0; \ |
| 623 | /* Ensure the optional handler is triggered */ \ |
| 624 | _metadata->trigger = 1; \ |
| 625 | } \ |
| 626 | } while (0); OPTIONAL_HANDLER(_assert) |
| 627 | |
| 628 | #define __EXPECT_STR(_expected, _seen, _t, _assert) do { \ |
| 629 | const char *__exp = (_expected); \ |
| 630 | const char *__seen = (_seen); \ |
Mickaël Salaün | 369130b | 2017-08-07 01:23:37 +0200 | [diff] [blame] | 631 | if (_assert) __INC_STEP(_metadata); \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 632 | if (!(strcmp(__exp, __seen) _t 0)) { \ |
| 633 | __TH_LOG("Expected '%s' %s '%s'.", __exp, #_t, __seen); \ |
| 634 | _metadata->passed = 0; \ |
| 635 | _metadata->trigger = 1; \ |
| 636 | } \ |
| 637 | } while (0); OPTIONAL_HANDLER(_assert) |
| 638 | |
Jakub Kicinski | 1a89595 | 2020-04-27 18:03:47 -0700 | [diff] [blame] | 639 | /* List helpers */ |
| 640 | #define __LIST_APPEND(head, item) \ |
| 641 | { \ |
| 642 | /* Circular linked list where only prev is circular. */ \ |
| 643 | if (head == NULL) { \ |
| 644 | head = item; \ |
| 645 | item->next = NULL; \ |
| 646 | item->prev = item; \ |
| 647 | return; \ |
| 648 | } \ |
| 649 | if (__constructor_order == _CONSTRUCTOR_ORDER_FORWARD) { \ |
| 650 | item->next = NULL; \ |
| 651 | item->prev = head->prev; \ |
| 652 | item->prev->next = item; \ |
| 653 | head->prev = item; \ |
| 654 | } else { \ |
| 655 | item->next = head; \ |
| 656 | item->next->prev = item; \ |
| 657 | item->prev = item; \ |
| 658 | head = item; \ |
| 659 | } \ |
| 660 | } |
| 661 | |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame^] | 662 | /* Contains all the information about a fixture. */ |
| 663 | struct __fixture_metadata { |
| 664 | const char *name; |
| 665 | struct __fixture_metadata *prev, *next; |
| 666 | } _fixture_global __attribute__((unused)) = { |
| 667 | .name = "global", |
| 668 | .prev = &_fixture_global, |
| 669 | }; |
| 670 | |
| 671 | static struct __fixture_metadata *__fixture_list = &_fixture_global; |
| 672 | static unsigned int __fixture_count; |
| 673 | static int __constructor_order; |
| 674 | |
| 675 | #define _CONSTRUCTOR_ORDER_FORWARD 1 |
| 676 | #define _CONSTRUCTOR_ORDER_BACKWARD -1 |
| 677 | |
| 678 | static inline void __register_fixture(struct __fixture_metadata *f) |
| 679 | { |
| 680 | __fixture_count++; |
| 681 | __LIST_APPEND(__fixture_list, f); |
| 682 | } |
| 683 | |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 684 | /* Contains all the information for test execution and status checking. */ |
| 685 | struct __test_metadata { |
| 686 | const char *name; |
| 687 | void (*fn)(struct __test_metadata *); |
Kees Cook | f46f576 | 2020-03-13 16:12:51 -0700 | [diff] [blame] | 688 | pid_t pid; /* pid of test when being run */ |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame^] | 689 | struct __fixture_metadata *fixture; |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 690 | int termsig; |
| 691 | int passed; |
| 692 | int trigger; /* extra handler after the evaluation */ |
Kees Cook | c31801d | 2020-03-13 16:12:52 -0700 | [diff] [blame] | 693 | int timeout; /* seconds to wait for test timeout */ |
| 694 | bool timed_out; /* did this test timeout instead of exiting? */ |
Mickaël Salaün | 369130b | 2017-08-07 01:23:37 +0200 | [diff] [blame] | 695 | __u8 step; |
| 696 | bool no_print; /* manual trigger when TH_LOG_STREAM is not available */ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 697 | struct __test_metadata *prev, *next; |
| 698 | }; |
| 699 | |
| 700 | /* Storage for the (global) tests to be run. */ |
| 701 | static struct __test_metadata *__test_list; |
| 702 | static unsigned int __test_count; |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 703 | |
| 704 | /* |
| 705 | * Since constructors are called in reverse order, reverse the test |
| 706 | * list so tests are run in source declaration order. |
| 707 | * https://gcc.gnu.org/onlinedocs/gccint/Initialization.html |
| 708 | * However, it seems not all toolchains do this correctly, so use |
| 709 | * __constructor_order to detect which direction is called first |
| 710 | * and adjust list building logic to get things running in the right |
| 711 | * direction. |
| 712 | */ |
| 713 | static inline void __register_test(struct __test_metadata *t) |
| 714 | { |
| 715 | __test_count++; |
Jakub Kicinski | 1a89595 | 2020-04-27 18:03:47 -0700 | [diff] [blame] | 716 | __LIST_APPEND(__test_list, t); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Mickaël Salaün | 369130b | 2017-08-07 01:23:37 +0200 | [diff] [blame] | 719 | static inline int __bail(int for_realz, bool no_print, __u8 step) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 720 | { |
Mickaël Salaün | 369130b | 2017-08-07 01:23:37 +0200 | [diff] [blame] | 721 | if (for_realz) { |
| 722 | if (no_print) |
| 723 | _exit(step); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 724 | abort(); |
Mickaël Salaün | 369130b | 2017-08-07 01:23:37 +0200 | [diff] [blame] | 725 | } |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 726 | return 0; |
| 727 | } |
| 728 | |
Kees Cook | c31801d | 2020-03-13 16:12:52 -0700 | [diff] [blame] | 729 | struct __test_metadata *__active_test; |
| 730 | static void __timeout_handler(int sig, siginfo_t *info, void *ucontext) |
| 731 | { |
| 732 | struct __test_metadata *t = __active_test; |
| 733 | |
| 734 | /* Sanity check handler execution environment. */ |
| 735 | if (!t) { |
| 736 | fprintf(TH_LOG_STREAM, |
Colin Ian King | d925c89 | 2020-03-27 09:06:48 +0000 | [diff] [blame] | 737 | "no active test in SIGALRM handler!?\n"); |
Kees Cook | c31801d | 2020-03-13 16:12:52 -0700 | [diff] [blame] | 738 | abort(); |
| 739 | } |
| 740 | if (sig != SIGALRM || sig != info->si_signo) { |
| 741 | fprintf(TH_LOG_STREAM, |
| 742 | "%s: SIGALRM handler caught signal %d!?\n", |
| 743 | t->name, sig != SIGALRM ? sig : info->si_signo); |
| 744 | abort(); |
| 745 | } |
| 746 | |
| 747 | t->timed_out = true; |
| 748 | kill(t->pid, SIGKILL); |
| 749 | } |
| 750 | |
Kees Cook | f46f576 | 2020-03-13 16:12:51 -0700 | [diff] [blame] | 751 | void __wait_for_test(struct __test_metadata *t) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 752 | { |
Kees Cook | c31801d | 2020-03-13 16:12:52 -0700 | [diff] [blame] | 753 | struct sigaction action = { |
| 754 | .sa_sigaction = __timeout_handler, |
| 755 | .sa_flags = SA_SIGINFO, |
| 756 | }; |
| 757 | struct sigaction saved_action; |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 758 | int status; |
| 759 | |
Kees Cook | c31801d | 2020-03-13 16:12:52 -0700 | [diff] [blame] | 760 | if (sigaction(SIGALRM, &action, &saved_action)) { |
| 761 | t->passed = 0; |
| 762 | fprintf(TH_LOG_STREAM, |
Colin Ian King | d925c89 | 2020-03-27 09:06:48 +0000 | [diff] [blame] | 763 | "%s: unable to install SIGALRM handler\n", |
Kees Cook | c31801d | 2020-03-13 16:12:52 -0700 | [diff] [blame] | 764 | t->name); |
| 765 | return; |
| 766 | } |
| 767 | __active_test = t; |
| 768 | t->timed_out = false; |
Kees Cook | f46f576 | 2020-03-13 16:12:51 -0700 | [diff] [blame] | 769 | alarm(t->timeout); |
| 770 | waitpid(t->pid, &status, 0); |
| 771 | alarm(0); |
Kees Cook | c31801d | 2020-03-13 16:12:52 -0700 | [diff] [blame] | 772 | if (sigaction(SIGALRM, &saved_action, NULL)) { |
| 773 | t->passed = 0; |
| 774 | fprintf(TH_LOG_STREAM, |
Colin Ian King | d925c89 | 2020-03-27 09:06:48 +0000 | [diff] [blame] | 775 | "%s: unable to uninstall SIGALRM handler\n", |
Kees Cook | c31801d | 2020-03-13 16:12:52 -0700 | [diff] [blame] | 776 | t->name); |
| 777 | return; |
| 778 | } |
| 779 | __active_test = NULL; |
Kees Cook | f46f576 | 2020-03-13 16:12:51 -0700 | [diff] [blame] | 780 | |
Kees Cook | c31801d | 2020-03-13 16:12:52 -0700 | [diff] [blame] | 781 | if (t->timed_out) { |
| 782 | t->passed = 0; |
| 783 | fprintf(TH_LOG_STREAM, |
| 784 | "%s: Test terminated by timeout\n", t->name); |
| 785 | } else if (WIFEXITED(status)) { |
Kees Cook | f46f576 | 2020-03-13 16:12:51 -0700 | [diff] [blame] | 786 | t->passed = t->termsig == -1 ? !WEXITSTATUS(status) : 0; |
| 787 | if (t->termsig != -1) { |
| 788 | fprintf(TH_LOG_STREAM, |
| 789 | "%s: Test exited normally " |
| 790 | "instead of by signal (code: %d)\n", |
| 791 | t->name, |
| 792 | WEXITSTATUS(status)); |
| 793 | } else if (!t->passed) { |
| 794 | fprintf(TH_LOG_STREAM, |
| 795 | "%s: Test failed at step #%d\n", |
| 796 | t->name, |
| 797 | WEXITSTATUS(status)); |
| 798 | } |
| 799 | } else if (WIFSIGNALED(status)) { |
| 800 | t->passed = 0; |
| 801 | if (WTERMSIG(status) == SIGABRT) { |
| 802 | fprintf(TH_LOG_STREAM, |
| 803 | "%s: Test terminated by assertion\n", |
| 804 | t->name); |
| 805 | } else if (WTERMSIG(status) == t->termsig) { |
| 806 | t->passed = 1; |
| 807 | } else { |
| 808 | fprintf(TH_LOG_STREAM, |
| 809 | "%s: Test terminated unexpectedly " |
| 810 | "by signal %d\n", |
| 811 | t->name, |
| 812 | WTERMSIG(status)); |
| 813 | } |
| 814 | } else { |
| 815 | fprintf(TH_LOG_STREAM, |
| 816 | "%s: Test ended in some other way [%u]\n", |
| 817 | t->name, |
| 818 | status); |
| 819 | } |
| 820 | } |
| 821 | |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame^] | 822 | void __run_test(struct __fixture_metadata *f, |
| 823 | struct __test_metadata *t) |
Kees Cook | f46f576 | 2020-03-13 16:12:51 -0700 | [diff] [blame] | 824 | { |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 825 | t->passed = 1; |
| 826 | t->trigger = 0; |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame^] | 827 | printf("[ RUN ] %s.%s\n", f->name, t->name); |
Kees Cook | f46f576 | 2020-03-13 16:12:51 -0700 | [diff] [blame] | 828 | t->pid = fork(); |
| 829 | if (t->pid < 0) { |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 830 | printf("ERROR SPAWNING TEST CHILD\n"); |
| 831 | t->passed = 0; |
Kees Cook | f46f576 | 2020-03-13 16:12:51 -0700 | [diff] [blame] | 832 | } else if (t->pid == 0) { |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 833 | t->fn(t); |
Mickaël Salaün | 369130b | 2017-08-07 01:23:37 +0200 | [diff] [blame] | 834 | /* return the step that failed or 0 */ |
| 835 | _exit(t->passed ? 0 : t->step); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 836 | } else { |
Kees Cook | f46f576 | 2020-03-13 16:12:51 -0700 | [diff] [blame] | 837 | __wait_for_test(t); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 838 | } |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame^] | 839 | printf("[ %4s ] %s.%s\n", (t->passed ? "OK" : "FAIL"), |
| 840 | f->name, t->name); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | static int test_harness_run(int __attribute__((unused)) argc, |
| 844 | char __attribute__((unused)) **argv) |
| 845 | { |
| 846 | struct __test_metadata *t; |
| 847 | int ret = 0; |
| 848 | unsigned int count = 0; |
| 849 | unsigned int pass_count = 0; |
| 850 | |
| 851 | /* TODO(wad) add optional arguments similar to gtest. */ |
| 852 | printf("[==========] Running %u tests from %u test cases.\n", |
| 853 | __test_count, __fixture_count + 1); |
| 854 | for (t = __test_list; t; t = t->next) { |
| 855 | count++; |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame^] | 856 | __run_test(t->fixture, t); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 857 | if (t->passed) |
| 858 | pass_count++; |
| 859 | else |
| 860 | ret = 1; |
| 861 | } |
| 862 | printf("[==========] %u / %u tests passed.\n", pass_count, count); |
| 863 | printf("[ %s ]\n", (ret ? "FAILED" : "PASSED")); |
| 864 | return ret; |
| 865 | } |
| 866 | |
| 867 | static void __attribute__((constructor)) __constructor_order_first(void) |
| 868 | { |
| 869 | if (!__constructor_order) |
| 870 | __constructor_order = _CONSTRUCTOR_ORDER_FORWARD; |
| 871 | } |
| 872 | |
Mickaël Salaün | dfa47d3 | 2017-05-26 20:43:57 +0200 | [diff] [blame] | 873 | #endif /* __KSELFTEST_HARNESS_H */ |