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); \ |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 171 | static inline void wrapper_##test_name( \ |
| 172 | struct __test_metadata *_metadata, \ |
| 173 | struct __fixture_variant_metadata *variant) \ |
| 174 | { \ |
| 175 | test_name(_metadata); \ |
| 176 | } \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 177 | static struct __test_metadata _##test_name##_object = \ |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame] | 178 | { .name = #test_name, \ |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 179 | .fn = &wrapper_##test_name, \ |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame] | 180 | .fixture = &_fixture_global, \ |
| 181 | .termsig = _signal, \ |
Alexandre Belloni | d51f1f1 | 2019-05-24 00:42:22 +0200 | [diff] [blame] | 182 | .timeout = TEST_TIMEOUT_DEFAULT, }; \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 183 | static void __attribute__((constructor)) _register_##test_name(void) \ |
| 184 | { \ |
| 185 | __register_test(&_##test_name##_object); \ |
| 186 | } \ |
| 187 | static void test_name( \ |
| 188 | struct __test_metadata __attribute__((unused)) *_metadata) |
| 189 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 190 | /** |
| 191 | * FIXTURE_DATA(datatype_name) - Wraps the struct name so we have one less |
| 192 | * argument to pass around |
| 193 | * |
| 194 | * @datatype_name: datatype name |
| 195 | * |
| 196 | * .. code-block:: c |
| 197 | * |
| 198 | * FIXTURE_DATA(datatype name) |
| 199 | * |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 200 | * This call may be used when the type of the fixture data |
| 201 | * is needed. In general, this should not be needed unless |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 202 | * the *self* is being passed to a helper directly. |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 203 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 204 | #define FIXTURE_DATA(datatype_name) struct _test_data_##datatype_name |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 205 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 206 | /** |
| 207 | * FIXTURE(fixture_name) - Called once per fixture to setup the data and |
| 208 | * register |
| 209 | * |
| 210 | * @fixture_name: fixture name |
| 211 | * |
| 212 | * .. code-block:: c |
| 213 | * |
| 214 | * FIXTURE(datatype name) { |
| 215 | * type property1; |
| 216 | * ... |
| 217 | * }; |
| 218 | * |
| 219 | * Defines the data provided to TEST_F()-defined tests as *self*. It should be |
| 220 | * populated and cleaned up using FIXTURE_SETUP() and FIXTURE_TEARDOWN(). |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 221 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 222 | #define FIXTURE(fixture_name) \ |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 223 | FIXTURE_VARIANT(fixture_name); \ |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame] | 224 | static struct __fixture_metadata _##fixture_name##_fixture_object = \ |
| 225 | { .name = #fixture_name, }; \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 226 | static void __attribute__((constructor)) \ |
| 227 | _register_##fixture_name##_data(void) \ |
| 228 | { \ |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame] | 229 | __register_fixture(&_##fixture_name##_fixture_object); \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 230 | } \ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 231 | FIXTURE_DATA(fixture_name) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 232 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 233 | /** |
| 234 | * FIXTURE_SETUP(fixture_name) - Prepares the setup function for the fixture. |
Kees Cook | 6c3b6d5 | 2018-03-15 09:59:16 -0700 | [diff] [blame] | 235 | * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly. |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 236 | * |
| 237 | * @fixture_name: fixture name |
| 238 | * |
| 239 | * .. code-block:: c |
| 240 | * |
| 241 | * FIXTURE_SETUP(fixture name) { implementation } |
| 242 | * |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 243 | * 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] | 244 | * 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] | 245 | * implementation. |
| 246 | * |
| 247 | * ASSERT_* are valid for use in this context and will prempt the execution |
| 248 | * of any dependent fixture tests. |
| 249 | * |
| 250 | * A bare "return;" statement may be used to return early. |
| 251 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 252 | #define FIXTURE_SETUP(fixture_name) \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 253 | void fixture_name##_setup( \ |
| 254 | struct __test_metadata __attribute__((unused)) *_metadata, \ |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 255 | FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \ |
| 256 | const FIXTURE_VARIANT(fixture_name) \ |
| 257 | __attribute__((unused)) *variant) |
| 258 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 259 | /** |
| 260 | * FIXTURE_TEARDOWN(fixture_name) |
Kees Cook | 6c3b6d5 | 2018-03-15 09:59:16 -0700 | [diff] [blame] | 261 | * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly. |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 262 | * |
| 263 | * @fixture_name: fixture name |
| 264 | * |
| 265 | * .. code-block:: c |
| 266 | * |
| 267 | * FIXTURE_TEARDOWN(fixture name) { implementation } |
| 268 | * |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 269 | * 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] | 270 | * 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] | 271 | * implementation to clean up. |
| 272 | * |
| 273 | * A bare "return;" statement may be used to return early. |
| 274 | */ |
| 275 | #define FIXTURE_TEARDOWN(fixture_name) \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 276 | void fixture_name##_teardown( \ |
| 277 | struct __test_metadata __attribute__((unused)) *_metadata, \ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 278 | FIXTURE_DATA(fixture_name) __attribute__((unused)) *self) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 279 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 280 | /** |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 281 | * FIXTURE_VARIANT(fixture_name) - Optionally called once per fixture |
| 282 | * to declare fixture variant |
| 283 | * |
| 284 | * @fixture_name: fixture name |
| 285 | * |
| 286 | * .. code-block:: c |
| 287 | * |
| 288 | * FIXTURE_VARIANT(datatype name) { |
| 289 | * type property1; |
| 290 | * ... |
| 291 | * }; |
| 292 | * |
| 293 | * Defines type of constant parameters provided to FIXTURE_SETUP() and TEST_F() |
| 294 | * as *variant*. Variants allow the same tests to be run with different |
| 295 | * arguments. |
| 296 | */ |
| 297 | #define FIXTURE_VARIANT(fixture_name) struct _fixture_variant_##fixture_name |
| 298 | |
| 299 | /** |
| 300 | * FIXTURE_VARIANT_ADD(fixture_name, variant_name) - Called once per fixture |
| 301 | * variant to setup and register the data |
| 302 | * |
| 303 | * @fixture_name: fixture name |
| 304 | * @variant_name: name of the parameter set |
| 305 | * |
| 306 | * .. code-block:: c |
| 307 | * |
| 308 | * FIXTURE_ADD(datatype name) { |
| 309 | * .property1 = val1; |
| 310 | * ... |
| 311 | * }; |
| 312 | * |
| 313 | * Defines a variant of the test fixture, provided to FIXTURE_SETUP() and |
| 314 | * TEST_F() as *variant*. Tests of each fixture will be run once for each |
| 315 | * variant. |
| 316 | */ |
| 317 | #define FIXTURE_VARIANT_ADD(fixture_name, variant_name) \ |
| 318 | extern FIXTURE_VARIANT(fixture_name) \ |
| 319 | _##fixture_name##_##variant_name##_variant; \ |
| 320 | static struct __fixture_variant_metadata \ |
| 321 | _##fixture_name##_##variant_name##_object = \ |
| 322 | { .name = #variant_name, \ |
| 323 | .data = &_##fixture_name##_##variant_name##_variant}; \ |
| 324 | static void __attribute__((constructor)) \ |
| 325 | _register_##fixture_name##_##variant_name(void) \ |
| 326 | { \ |
| 327 | __register_fixture_variant(&_##fixture_name##_fixture_object, \ |
| 328 | &_##fixture_name##_##variant_name##_object); \ |
| 329 | } \ |
| 330 | FIXTURE_VARIANT(fixture_name) \ |
| 331 | _##fixture_name##_##variant_name##_variant = |
| 332 | |
| 333 | /** |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 334 | * TEST_F(fixture_name, test_name) - Emits test registration and helpers for |
| 335 | * fixture-based test cases |
| 336 | * |
| 337 | * @fixture_name: fixture name |
| 338 | * @test_name: test name |
| 339 | * |
| 340 | * .. code-block:: c |
| 341 | * |
| 342 | * TEST_F(fixture, name) { implementation } |
| 343 | * |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 344 | * 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] | 345 | * 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] | 346 | * datatype exposed for use by the implementation. |
Kees Cook | 6c3b6d5 | 2018-03-15 09:59:16 -0700 | [diff] [blame] | 347 | * |
| 348 | * Warning: use of ASSERT_* here will skip TEARDOWN. |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 349 | */ |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 350 | /* TODO(wad) register fixtures on dedicated test lists. */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 351 | #define TEST_F(fixture_name, test_name) \ |
Alexandre Belloni | d51f1f1 | 2019-05-24 00:42:22 +0200 | [diff] [blame] | 352 | __TEST_F_IMPL(fixture_name, test_name, -1, TEST_TIMEOUT_DEFAULT) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 353 | |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 354 | #define TEST_F_SIGNAL(fixture_name, test_name, signal) \ |
Alexandre Belloni | d51f1f1 | 2019-05-24 00:42:22 +0200 | [diff] [blame] | 355 | __TEST_F_IMPL(fixture_name, test_name, signal, TEST_TIMEOUT_DEFAULT) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 356 | |
Alexandre Belloni | d51f1f1 | 2019-05-24 00:42:22 +0200 | [diff] [blame] | 357 | #define TEST_F_TIMEOUT(fixture_name, test_name, timeout) \ |
| 358 | __TEST_F_IMPL(fixture_name, test_name, -1, timeout) |
| 359 | |
| 360 | #define __TEST_F_IMPL(fixture_name, test_name, signal, tmout) \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 361 | static void fixture_name##_##test_name( \ |
| 362 | struct __test_metadata *_metadata, \ |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 363 | FIXTURE_DATA(fixture_name) *self, \ |
| 364 | const FIXTURE_VARIANT(fixture_name) *variant); \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 365 | static inline void wrapper_##fixture_name##_##test_name( \ |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 366 | struct __test_metadata *_metadata, \ |
| 367 | struct __fixture_variant_metadata *variant) \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 368 | { \ |
| 369 | /* fixture data is alloced, setup, and torn down per call. */ \ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 370 | FIXTURE_DATA(fixture_name) self; \ |
| 371 | memset(&self, 0, sizeof(FIXTURE_DATA(fixture_name))); \ |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 372 | fixture_name##_setup(_metadata, &self, variant->data); \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 373 | /* Let setup failure terminate early. */ \ |
| 374 | if (!_metadata->passed) \ |
| 375 | return; \ |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 376 | fixture_name##_##test_name(_metadata, &self, variant->data); \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 377 | fixture_name##_teardown(_metadata, &self); \ |
| 378 | } \ |
| 379 | static struct __test_metadata \ |
| 380 | _##fixture_name##_##test_name##_object = { \ |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame] | 381 | .name = #test_name, \ |
Kees Cook | 121e357 | 2019-01-27 01:42:51 -0800 | [diff] [blame] | 382 | .fn = &wrapper_##fixture_name##_##test_name, \ |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame] | 383 | .fixture = &_##fixture_name##_fixture_object, \ |
Kees Cook | 121e357 | 2019-01-27 01:42:51 -0800 | [diff] [blame] | 384 | .termsig = signal, \ |
Alexandre Belloni | d51f1f1 | 2019-05-24 00:42:22 +0200 | [diff] [blame] | 385 | .timeout = tmout, \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 386 | }; \ |
| 387 | static void __attribute__((constructor)) \ |
| 388 | _register_##fixture_name##_##test_name(void) \ |
| 389 | { \ |
| 390 | __register_test(&_##fixture_name##_##test_name##_object); \ |
| 391 | } \ |
| 392 | static void fixture_name##_##test_name( \ |
| 393 | struct __test_metadata __attribute__((unused)) *_metadata, \ |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 394 | FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \ |
| 395 | const FIXTURE_VARIANT(fixture_name) \ |
| 396 | __attribute__((unused)) *variant) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 397 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 398 | /** |
| 399 | * TEST_HARNESS_MAIN - Simple wrapper to run the test harness |
| 400 | * |
| 401 | * .. code-block:: c |
| 402 | * |
| 403 | * TEST_HARNESS_MAIN |
| 404 | * |
| 405 | * Use once to append a main() to the test file. |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 406 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 407 | #define TEST_HARNESS_MAIN \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 408 | static void __attribute__((constructor)) \ |
| 409 | __constructor_order_last(void) \ |
| 410 | { \ |
| 411 | if (!__constructor_order) \ |
| 412 | __constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; \ |
| 413 | } \ |
| 414 | int main(int argc, char **argv) { \ |
| 415 | return test_harness_run(argc, argv); \ |
| 416 | } |
| 417 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 418 | /** |
| 419 | * DOC: operators |
| 420 | * |
| 421 | * Operators for use in TEST() and TEST_F(). |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 422 | * ASSERT_* calls will stop test execution immediately. |
| 423 | * EXPECT_* calls will emit a failure warning, note it, and continue. |
| 424 | */ |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 425 | |
| 426 | /** |
| 427 | * ASSERT_EQ(expected, seen) |
| 428 | * |
| 429 | * @expected: expected value |
| 430 | * @seen: measured value |
| 431 | * |
| 432 | * ASSERT_EQ(expected, measured): expected == measured |
| 433 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 434 | #define ASSERT_EQ(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 435 | __EXPECT(expected, #expected, seen, #seen, ==, 1) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 436 | |
| 437 | /** |
| 438 | * ASSERT_NE(expected, seen) |
| 439 | * |
| 440 | * @expected: expected value |
| 441 | * @seen: measured value |
| 442 | * |
| 443 | * ASSERT_NE(expected, measured): expected != measured |
| 444 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 445 | #define ASSERT_NE(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 446 | __EXPECT(expected, #expected, seen, #seen, !=, 1) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 447 | |
| 448 | /** |
| 449 | * ASSERT_LT(expected, seen) |
| 450 | * |
| 451 | * @expected: expected value |
| 452 | * @seen: measured value |
| 453 | * |
| 454 | * ASSERT_LT(expected, measured): expected < measured |
| 455 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 456 | #define ASSERT_LT(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 457 | __EXPECT(expected, #expected, seen, #seen, <, 1) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 458 | |
| 459 | /** |
| 460 | * ASSERT_LE(expected, seen) |
| 461 | * |
| 462 | * @expected: expected value |
| 463 | * @seen: measured value |
| 464 | * |
| 465 | * ASSERT_LE(expected, measured): expected <= measured |
| 466 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 467 | #define ASSERT_LE(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 468 | __EXPECT(expected, #expected, seen, #seen, <=, 1) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 469 | |
| 470 | /** |
| 471 | * ASSERT_GT(expected, seen) |
| 472 | * |
| 473 | * @expected: expected value |
| 474 | * @seen: measured value |
| 475 | * |
| 476 | * ASSERT_GT(expected, measured): expected > measured |
| 477 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 478 | #define ASSERT_GT(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 479 | __EXPECT(expected, #expected, seen, #seen, >, 1) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 480 | |
| 481 | /** |
| 482 | * ASSERT_GE(expected, seen) |
| 483 | * |
| 484 | * @expected: expected value |
| 485 | * @seen: measured value |
| 486 | * |
| 487 | * ASSERT_GE(expected, measured): expected >= measured |
| 488 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 489 | #define ASSERT_GE(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 490 | __EXPECT(expected, #expected, seen, #seen, >=, 1) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 491 | |
| 492 | /** |
| 493 | * ASSERT_NULL(seen) |
| 494 | * |
| 495 | * @seen: measured value |
| 496 | * |
| 497 | * ASSERT_NULL(measured): NULL == measured |
| 498 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 499 | #define ASSERT_NULL(seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 500 | __EXPECT(NULL, "NULL", seen, #seen, ==, 1) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 501 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 502 | /** |
| 503 | * ASSERT_TRUE(seen) |
| 504 | * |
| 505 | * @seen: measured value |
| 506 | * |
| 507 | * ASSERT_TRUE(measured): measured != 0 |
| 508 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 509 | #define ASSERT_TRUE(seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 510 | __EXPECT(0, "0", seen, #seen, !=, 1) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 511 | |
| 512 | /** |
| 513 | * ASSERT_FALSE(seen) |
| 514 | * |
| 515 | * @seen: measured value |
| 516 | * |
| 517 | * ASSERT_FALSE(measured): measured == 0 |
| 518 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 519 | #define ASSERT_FALSE(seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 520 | __EXPECT(0, "0", seen, #seen, ==, 1) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 521 | |
| 522 | /** |
| 523 | * ASSERT_STREQ(expected, seen) |
| 524 | * |
| 525 | * @expected: expected value |
| 526 | * @seen: measured value |
| 527 | * |
| 528 | * ASSERT_STREQ(expected, measured): !strcmp(expected, measured) |
| 529 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 530 | #define ASSERT_STREQ(expected, seen) \ |
| 531 | __EXPECT_STR(expected, seen, ==, 1) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 532 | |
| 533 | /** |
| 534 | * ASSERT_STRNE(expected, seen) |
| 535 | * |
| 536 | * @expected: expected value |
| 537 | * @seen: measured value |
| 538 | * |
| 539 | * ASSERT_STRNE(expected, measured): strcmp(expected, measured) |
| 540 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 541 | #define ASSERT_STRNE(expected, seen) \ |
| 542 | __EXPECT_STR(expected, seen, !=, 1) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 543 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 544 | /** |
| 545 | * EXPECT_EQ(expected, seen) |
| 546 | * |
| 547 | * @expected: expected value |
| 548 | * @seen: measured value |
| 549 | * |
| 550 | * EXPECT_EQ(expected, measured): expected == measured |
| 551 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 552 | #define EXPECT_EQ(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 553 | __EXPECT(expected, #expected, seen, #seen, ==, 0) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 554 | |
| 555 | /** |
| 556 | * EXPECT_NE(expected, seen) |
| 557 | * |
| 558 | * @expected: expected value |
| 559 | * @seen: measured value |
| 560 | * |
| 561 | * EXPECT_NE(expected, measured): expected != measured |
| 562 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 563 | #define EXPECT_NE(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 564 | __EXPECT(expected, #expected, seen, #seen, !=, 0) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 565 | |
| 566 | /** |
| 567 | * EXPECT_LT(expected, seen) |
| 568 | * |
| 569 | * @expected: expected value |
| 570 | * @seen: measured value |
| 571 | * |
| 572 | * EXPECT_LT(expected, measured): expected < measured |
| 573 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 574 | #define EXPECT_LT(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 575 | __EXPECT(expected, #expected, seen, #seen, <, 0) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 576 | |
| 577 | /** |
| 578 | * EXPECT_LE(expected, seen) |
| 579 | * |
| 580 | * @expected: expected value |
| 581 | * @seen: measured value |
| 582 | * |
| 583 | * EXPECT_LE(expected, measured): expected <= measured |
| 584 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 585 | #define EXPECT_LE(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 586 | __EXPECT(expected, #expected, seen, #seen, <=, 0) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 587 | |
| 588 | /** |
| 589 | * EXPECT_GT(expected, seen) |
| 590 | * |
| 591 | * @expected: expected value |
| 592 | * @seen: measured value |
| 593 | * |
| 594 | * EXPECT_GT(expected, measured): expected > measured |
| 595 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 596 | #define EXPECT_GT(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 597 | __EXPECT(expected, #expected, seen, #seen, >, 0) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 598 | |
| 599 | /** |
| 600 | * EXPECT_GE(expected, seen) |
| 601 | * |
| 602 | * @expected: expected value |
| 603 | * @seen: measured value |
| 604 | * |
| 605 | * EXPECT_GE(expected, measured): expected >= measured |
| 606 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 607 | #define EXPECT_GE(expected, seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 608 | __EXPECT(expected, #expected, seen, #seen, >=, 0) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 609 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 610 | /** |
| 611 | * EXPECT_NULL(seen) |
| 612 | * |
| 613 | * @seen: measured value |
| 614 | * |
| 615 | * EXPECT_NULL(measured): NULL == measured |
| 616 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 617 | #define EXPECT_NULL(seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 618 | __EXPECT(NULL, "NULL", seen, #seen, ==, 0) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 619 | |
| 620 | /** |
| 621 | * EXPECT_TRUE(seen) |
| 622 | * |
| 623 | * @seen: measured value |
| 624 | * |
| 625 | * EXPECT_TRUE(measured): 0 != measured |
| 626 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 627 | #define EXPECT_TRUE(seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 628 | __EXPECT(0, "0", seen, #seen, !=, 0) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 629 | |
| 630 | /** |
| 631 | * EXPECT_FALSE(seen) |
| 632 | * |
| 633 | * @seen: measured value |
| 634 | * |
| 635 | * EXPECT_FALSE(measured): 0 == measured |
| 636 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 637 | #define EXPECT_FALSE(seen) \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 638 | __EXPECT(0, "0", seen, #seen, ==, 0) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 639 | |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 640 | /** |
| 641 | * EXPECT_STREQ(expected, seen) |
| 642 | * |
| 643 | * @expected: expected value |
| 644 | * @seen: measured value |
| 645 | * |
| 646 | * EXPECT_STREQ(expected, measured): !strcmp(expected, measured) |
| 647 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 648 | #define EXPECT_STREQ(expected, seen) \ |
| 649 | __EXPECT_STR(expected, seen, ==, 0) |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame] | 650 | |
| 651 | /** |
| 652 | * EXPECT_STRNE(expected, seen) |
| 653 | * |
| 654 | * @expected: expected value |
| 655 | * @seen: measured value |
| 656 | * |
| 657 | * EXPECT_STRNE(expected, measured): strcmp(expected, measured) |
| 658 | */ |
Mickaël Salaün | 1256a52 | 2017-05-26 20:44:01 +0200 | [diff] [blame] | 659 | #define EXPECT_STRNE(expected, seen) \ |
| 660 | __EXPECT_STR(expected, seen, !=, 0) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 661 | |
| 662 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 663 | |
| 664 | /* Support an optional handler after and ASSERT_* or EXPECT_*. The approach is |
| 665 | * not thread-safe, but it should be fine in most sane test scenarios. |
| 666 | * |
| 667 | * Using __bail(), which optionally abort()s, is the easiest way to early |
| 668 | * return while still providing an optional block to the API consumer. |
| 669 | */ |
| 670 | #define OPTIONAL_HANDLER(_assert) \ |
Mickaël Salaün | 369130b | 2017-08-07 01:23:37 +0200 | [diff] [blame] | 671 | for (; _metadata->trigger; _metadata->trigger = \ |
| 672 | __bail(_assert, _metadata->no_print, _metadata->step)) |
| 673 | |
| 674 | #define __INC_STEP(_metadata) \ |
| 675 | if (_metadata->passed && _metadata->step < 255) \ |
| 676 | _metadata->step++; |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 677 | |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 678 | #define __EXPECT(_expected, _expected_str, _seen, _seen_str, _t, _assert) do { \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 679 | /* Avoid multiple evaluation of the cases */ \ |
| 680 | __typeof__(_expected) __exp = (_expected); \ |
| 681 | __typeof__(_seen) __seen = (_seen); \ |
Mickaël Salaün | 369130b | 2017-08-07 01:23:37 +0200 | [diff] [blame] | 682 | if (_assert) __INC_STEP(_metadata); \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 683 | if (!(__exp _t __seen)) { \ |
Kees Cook | b5bb6d3 | 2015-12-10 14:50:25 -0800 | [diff] [blame] | 684 | unsigned long long __exp_print = (uintptr_t)__exp; \ |
| 685 | unsigned long long __seen_print = (uintptr_t)__seen; \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 686 | __TH_LOG("Expected %s (%llu) %s %s (%llu)", \ |
Dmitry V. Levin | b708a3cc | 2018-12-10 02:00:47 +0300 | [diff] [blame] | 687 | _expected_str, __exp_print, #_t, \ |
| 688 | _seen_str, __seen_print); \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 689 | _metadata->passed = 0; \ |
| 690 | /* Ensure the optional handler is triggered */ \ |
| 691 | _metadata->trigger = 1; \ |
| 692 | } \ |
| 693 | } while (0); OPTIONAL_HANDLER(_assert) |
| 694 | |
| 695 | #define __EXPECT_STR(_expected, _seen, _t, _assert) do { \ |
| 696 | const char *__exp = (_expected); \ |
| 697 | const char *__seen = (_seen); \ |
Mickaël Salaün | 369130b | 2017-08-07 01:23:37 +0200 | [diff] [blame] | 698 | if (_assert) __INC_STEP(_metadata); \ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 699 | if (!(strcmp(__exp, __seen) _t 0)) { \ |
| 700 | __TH_LOG("Expected '%s' %s '%s'.", __exp, #_t, __seen); \ |
| 701 | _metadata->passed = 0; \ |
| 702 | _metadata->trigger = 1; \ |
| 703 | } \ |
| 704 | } while (0); OPTIONAL_HANDLER(_assert) |
| 705 | |
Jakub Kicinski | 1a89595 | 2020-04-27 18:03:47 -0700 | [diff] [blame] | 706 | /* List helpers */ |
| 707 | #define __LIST_APPEND(head, item) \ |
| 708 | { \ |
| 709 | /* Circular linked list where only prev is circular. */ \ |
| 710 | if (head == NULL) { \ |
| 711 | head = item; \ |
| 712 | item->next = NULL; \ |
| 713 | item->prev = item; \ |
| 714 | return; \ |
| 715 | } \ |
| 716 | if (__constructor_order == _CONSTRUCTOR_ORDER_FORWARD) { \ |
| 717 | item->next = NULL; \ |
| 718 | item->prev = head->prev; \ |
| 719 | item->prev->next = item; \ |
| 720 | head->prev = item; \ |
| 721 | } else { \ |
| 722 | item->next = head; \ |
| 723 | item->next->prev = item; \ |
| 724 | item->prev = item; \ |
| 725 | head = item; \ |
| 726 | } \ |
| 727 | } |
| 728 | |
Jakub Kicinski | e7f3046 | 2020-04-27 18:03:49 -0700 | [diff] [blame] | 729 | struct __test_metadata; |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 730 | struct __fixture_variant_metadata; |
Jakub Kicinski | e7f3046 | 2020-04-27 18:03:49 -0700 | [diff] [blame] | 731 | |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame] | 732 | /* Contains all the information about a fixture. */ |
| 733 | struct __fixture_metadata { |
| 734 | const char *name; |
Jakub Kicinski | e7f3046 | 2020-04-27 18:03:49 -0700 | [diff] [blame] | 735 | struct __test_metadata *tests; |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 736 | struct __fixture_variant_metadata *variant; |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame] | 737 | struct __fixture_metadata *prev, *next; |
| 738 | } _fixture_global __attribute__((unused)) = { |
| 739 | .name = "global", |
| 740 | .prev = &_fixture_global, |
| 741 | }; |
| 742 | |
| 743 | static struct __fixture_metadata *__fixture_list = &_fixture_global; |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame] | 744 | static int __constructor_order; |
| 745 | |
| 746 | #define _CONSTRUCTOR_ORDER_FORWARD 1 |
| 747 | #define _CONSTRUCTOR_ORDER_BACKWARD -1 |
| 748 | |
| 749 | static inline void __register_fixture(struct __fixture_metadata *f) |
| 750 | { |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame] | 751 | __LIST_APPEND(__fixture_list, f); |
| 752 | } |
| 753 | |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 754 | struct __fixture_variant_metadata { |
| 755 | const char *name; |
| 756 | const void *data; |
| 757 | struct __fixture_variant_metadata *prev, *next; |
| 758 | }; |
| 759 | |
| 760 | static inline void |
| 761 | __register_fixture_variant(struct __fixture_metadata *f, |
| 762 | struct __fixture_variant_metadata *variant) |
| 763 | { |
| 764 | __LIST_APPEND(f->variant, variant); |
| 765 | } |
| 766 | |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 767 | /* Contains all the information for test execution and status checking. */ |
| 768 | struct __test_metadata { |
| 769 | const char *name; |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 770 | void (*fn)(struct __test_metadata *, |
| 771 | struct __fixture_variant_metadata *); |
Kees Cook | f46f576 | 2020-03-13 16:12:51 -0700 | [diff] [blame] | 772 | pid_t pid; /* pid of test when being run */ |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame] | 773 | struct __fixture_metadata *fixture; |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 774 | int termsig; |
| 775 | int passed; |
| 776 | int trigger; /* extra handler after the evaluation */ |
Kees Cook | c31801d | 2020-03-13 16:12:52 -0700 | [diff] [blame] | 777 | int timeout; /* seconds to wait for test timeout */ |
| 778 | bool timed_out; /* did this test timeout instead of exiting? */ |
Mickaël Salaün | 369130b | 2017-08-07 01:23:37 +0200 | [diff] [blame] | 779 | __u8 step; |
| 780 | bool no_print; /* manual trigger when TH_LOG_STREAM is not available */ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 781 | struct __test_metadata *prev, *next; |
| 782 | }; |
| 783 | |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 784 | /* |
| 785 | * Since constructors are called in reverse order, reverse the test |
| 786 | * list so tests are run in source declaration order. |
| 787 | * https://gcc.gnu.org/onlinedocs/gccint/Initialization.html |
| 788 | * However, it seems not all toolchains do this correctly, so use |
| 789 | * __constructor_order to detect which direction is called first |
| 790 | * and adjust list building logic to get things running in the right |
| 791 | * direction. |
| 792 | */ |
| 793 | static inline void __register_test(struct __test_metadata *t) |
| 794 | { |
Jakub Kicinski | e7f3046 | 2020-04-27 18:03:49 -0700 | [diff] [blame] | 795 | __LIST_APPEND(t->fixture->tests, t); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Mickaël Salaün | 369130b | 2017-08-07 01:23:37 +0200 | [diff] [blame] | 798 | static inline int __bail(int for_realz, bool no_print, __u8 step) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 799 | { |
Mickaël Salaün | 369130b | 2017-08-07 01:23:37 +0200 | [diff] [blame] | 800 | if (for_realz) { |
| 801 | if (no_print) |
| 802 | _exit(step); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 803 | abort(); |
Mickaël Salaün | 369130b | 2017-08-07 01:23:37 +0200 | [diff] [blame] | 804 | } |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 805 | return 0; |
| 806 | } |
| 807 | |
Kees Cook | c31801d | 2020-03-13 16:12:52 -0700 | [diff] [blame] | 808 | struct __test_metadata *__active_test; |
| 809 | static void __timeout_handler(int sig, siginfo_t *info, void *ucontext) |
| 810 | { |
| 811 | struct __test_metadata *t = __active_test; |
| 812 | |
| 813 | /* Sanity check handler execution environment. */ |
| 814 | if (!t) { |
| 815 | fprintf(TH_LOG_STREAM, |
Colin Ian King | d925c89 | 2020-03-27 09:06:48 +0000 | [diff] [blame] | 816 | "no active test in SIGALRM handler!?\n"); |
Kees Cook | c31801d | 2020-03-13 16:12:52 -0700 | [diff] [blame] | 817 | abort(); |
| 818 | } |
| 819 | if (sig != SIGALRM || sig != info->si_signo) { |
| 820 | fprintf(TH_LOG_STREAM, |
| 821 | "%s: SIGALRM handler caught signal %d!?\n", |
| 822 | t->name, sig != SIGALRM ? sig : info->si_signo); |
| 823 | abort(); |
| 824 | } |
| 825 | |
| 826 | t->timed_out = true; |
| 827 | kill(t->pid, SIGKILL); |
| 828 | } |
| 829 | |
Kees Cook | f46f576 | 2020-03-13 16:12:51 -0700 | [diff] [blame] | 830 | void __wait_for_test(struct __test_metadata *t) |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 831 | { |
Kees Cook | c31801d | 2020-03-13 16:12:52 -0700 | [diff] [blame] | 832 | struct sigaction action = { |
| 833 | .sa_sigaction = __timeout_handler, |
| 834 | .sa_flags = SA_SIGINFO, |
| 835 | }; |
| 836 | struct sigaction saved_action; |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 837 | int status; |
| 838 | |
Kees Cook | c31801d | 2020-03-13 16:12:52 -0700 | [diff] [blame] | 839 | if (sigaction(SIGALRM, &action, &saved_action)) { |
| 840 | t->passed = 0; |
| 841 | fprintf(TH_LOG_STREAM, |
Colin Ian King | d925c89 | 2020-03-27 09:06:48 +0000 | [diff] [blame] | 842 | "%s: unable to install SIGALRM handler\n", |
Kees Cook | c31801d | 2020-03-13 16:12:52 -0700 | [diff] [blame] | 843 | t->name); |
| 844 | return; |
| 845 | } |
| 846 | __active_test = t; |
| 847 | t->timed_out = false; |
Kees Cook | f46f576 | 2020-03-13 16:12:51 -0700 | [diff] [blame] | 848 | alarm(t->timeout); |
| 849 | waitpid(t->pid, &status, 0); |
| 850 | alarm(0); |
Kees Cook | c31801d | 2020-03-13 16:12:52 -0700 | [diff] [blame] | 851 | if (sigaction(SIGALRM, &saved_action, NULL)) { |
| 852 | t->passed = 0; |
| 853 | fprintf(TH_LOG_STREAM, |
Colin Ian King | d925c89 | 2020-03-27 09:06:48 +0000 | [diff] [blame] | 854 | "%s: unable to uninstall SIGALRM handler\n", |
Kees Cook | c31801d | 2020-03-13 16:12:52 -0700 | [diff] [blame] | 855 | t->name); |
| 856 | return; |
| 857 | } |
| 858 | __active_test = NULL; |
Kees Cook | f46f576 | 2020-03-13 16:12:51 -0700 | [diff] [blame] | 859 | |
Kees Cook | c31801d | 2020-03-13 16:12:52 -0700 | [diff] [blame] | 860 | if (t->timed_out) { |
| 861 | t->passed = 0; |
| 862 | fprintf(TH_LOG_STREAM, |
| 863 | "%s: Test terminated by timeout\n", t->name); |
| 864 | } else if (WIFEXITED(status)) { |
Kees Cook | f46f576 | 2020-03-13 16:12:51 -0700 | [diff] [blame] | 865 | t->passed = t->termsig == -1 ? !WEXITSTATUS(status) : 0; |
| 866 | if (t->termsig != -1) { |
| 867 | fprintf(TH_LOG_STREAM, |
| 868 | "%s: Test exited normally " |
| 869 | "instead of by signal (code: %d)\n", |
| 870 | t->name, |
| 871 | WEXITSTATUS(status)); |
| 872 | } else if (!t->passed) { |
| 873 | fprintf(TH_LOG_STREAM, |
| 874 | "%s: Test failed at step #%d\n", |
| 875 | t->name, |
| 876 | WEXITSTATUS(status)); |
| 877 | } |
| 878 | } else if (WIFSIGNALED(status)) { |
| 879 | t->passed = 0; |
| 880 | if (WTERMSIG(status) == SIGABRT) { |
| 881 | fprintf(TH_LOG_STREAM, |
| 882 | "%s: Test terminated by assertion\n", |
| 883 | t->name); |
| 884 | } else if (WTERMSIG(status) == t->termsig) { |
| 885 | t->passed = 1; |
| 886 | } else { |
| 887 | fprintf(TH_LOG_STREAM, |
| 888 | "%s: Test terminated unexpectedly " |
| 889 | "by signal %d\n", |
| 890 | t->name, |
| 891 | WTERMSIG(status)); |
| 892 | } |
| 893 | } else { |
| 894 | fprintf(TH_LOG_STREAM, |
| 895 | "%s: Test ended in some other way [%u]\n", |
| 896 | t->name, |
| 897 | status); |
| 898 | } |
| 899 | } |
| 900 | |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame] | 901 | void __run_test(struct __fixture_metadata *f, |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 902 | struct __fixture_variant_metadata *variant, |
Jakub Kicinski | 142aca6 | 2020-04-27 18:03:48 -0700 | [diff] [blame] | 903 | struct __test_metadata *t) |
Kees Cook | f46f576 | 2020-03-13 16:12:51 -0700 | [diff] [blame] | 904 | { |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 905 | /* reset test struct */ |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 906 | t->passed = 1; |
| 907 | t->trigger = 0; |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 908 | t->step = 0; |
| 909 | t->no_print = 0; |
| 910 | |
| 911 | printf("[ RUN ] %s%s%s.%s\n", |
| 912 | f->name, variant->name[0] ? "." : "", variant->name, t->name); |
Kees Cook | f46f576 | 2020-03-13 16:12:51 -0700 | [diff] [blame] | 913 | t->pid = fork(); |
| 914 | if (t->pid < 0) { |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 915 | printf("ERROR SPAWNING TEST CHILD\n"); |
| 916 | t->passed = 0; |
Kees Cook | f46f576 | 2020-03-13 16:12:51 -0700 | [diff] [blame] | 917 | } else if (t->pid == 0) { |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 918 | t->fn(t, variant); |
Mickaël Salaün | 369130b | 2017-08-07 01:23:37 +0200 | [diff] [blame] | 919 | /* return the step that failed or 0 */ |
| 920 | _exit(t->passed ? 0 : t->step); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 921 | } else { |
Kees Cook | f46f576 | 2020-03-13 16:12:51 -0700 | [diff] [blame] | 922 | __wait_for_test(t); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 923 | } |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 924 | printf("[ %4s ] %s%s%s.%s\n", (t->passed ? "OK" : "FAIL"), |
| 925 | f->name, variant->name[0] ? "." : "", variant->name, t->name); |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | static int test_harness_run(int __attribute__((unused)) argc, |
| 929 | char __attribute__((unused)) **argv) |
| 930 | { |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 931 | struct __fixture_variant_metadata no_variant = { .name = "", }; |
| 932 | struct __fixture_variant_metadata *v; |
Jakub Kicinski | e7f3046 | 2020-04-27 18:03:49 -0700 | [diff] [blame] | 933 | struct __fixture_metadata *f; |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 934 | struct __test_metadata *t; |
| 935 | int ret = 0; |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 936 | unsigned int case_count = 0, test_count = 0; |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 937 | unsigned int count = 0; |
| 938 | unsigned int pass_count = 0; |
| 939 | |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 940 | for (f = __fixture_list; f; f = f->next) { |
| 941 | for (v = f->variant ?: &no_variant; v; v = v->next) { |
| 942 | case_count++; |
| 943 | for (t = f->tests; t; t = t->next) |
| 944 | test_count++; |
| 945 | } |
| 946 | } |
| 947 | |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 948 | /* TODO(wad) add optional arguments similar to gtest. */ |
| 949 | printf("[==========] Running %u tests from %u test cases.\n", |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 950 | test_count, case_count); |
Jakub Kicinski | e7f3046 | 2020-04-27 18:03:49 -0700 | [diff] [blame] | 951 | for (f = __fixture_list; f; f = f->next) { |
Jakub Kicinski | 74bc7c9 | 2020-04-27 18:03:50 -0700 | [diff] [blame] | 952 | for (v = f->variant ?: &no_variant; v; v = v->next) { |
| 953 | for (t = f->tests; t; t = t->next) { |
| 954 | count++; |
| 955 | __run_test(f, v, t); |
| 956 | if (t->passed) |
| 957 | pass_count++; |
| 958 | else |
| 959 | ret = 1; |
| 960 | } |
Jakub Kicinski | e7f3046 | 2020-04-27 18:03:49 -0700 | [diff] [blame] | 961 | } |
Kees Cook | c99ee51 | 2015-06-16 10:54:14 -0700 | [diff] [blame] | 962 | } |
| 963 | printf("[==========] %u / %u tests passed.\n", pass_count, count); |
| 964 | printf("[ %s ]\n", (ret ? "FAILED" : "PASSED")); |
| 965 | return ret; |
| 966 | } |
| 967 | |
| 968 | static void __attribute__((constructor)) __constructor_order_first(void) |
| 969 | { |
| 970 | if (!__constructor_order) |
| 971 | __constructor_order = _CONSTRUCTOR_ORDER_FORWARD; |
| 972 | } |
| 973 | |
Mickaël Salaün | dfa47d3 | 2017-05-26 20:43:57 +0200 | [diff] [blame] | 974 | #endif /* __KSELFTEST_HARNESS_H */ |