David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4 | * |
| 5 | * kselftest_harness.h: simple C unit test helper. |
| 6 | * |
| 7 | * See documentation in Documentation/dev-tools/kselftest.rst |
| 8 | * |
| 9 | * API inspired by code.google.com/p/googletest |
| 10 | */ |
| 11 | |
| 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 | |
| 50 | #ifndef __KSELFTEST_HARNESS_H |
| 51 | #define __KSELFTEST_HARNESS_H |
| 52 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 53 | #ifndef _GNU_SOURCE |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 54 | #define _GNU_SOURCE |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 55 | #endif |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 56 | #include <asm/types.h> |
| 57 | #include <errno.h> |
| 58 | #include <stdbool.h> |
| 59 | #include <stdint.h> |
| 60 | #include <stdio.h> |
| 61 | #include <stdlib.h> |
| 62 | #include <string.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 63 | #include <sys/mman.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 64 | #include <sys/types.h> |
| 65 | #include <sys/wait.h> |
| 66 | #include <unistd.h> |
| 67 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 68 | #include "kselftest.h" |
| 69 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 70 | #define TEST_TIMEOUT_DEFAULT 30 |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 71 | |
| 72 | /* Utilities exposed to the test definitions */ |
| 73 | #ifndef TH_LOG_STREAM |
| 74 | # define TH_LOG_STREAM stderr |
| 75 | #endif |
| 76 | |
| 77 | #ifndef TH_LOG_ENABLED |
| 78 | # define TH_LOG_ENABLED 1 |
| 79 | #endif |
| 80 | |
| 81 | /** |
| 82 | * TH_LOG(fmt, ...) |
| 83 | * |
| 84 | * @fmt: format string |
| 85 | * @...: optional arguments |
| 86 | * |
| 87 | * .. code-block:: c |
| 88 | * |
| 89 | * TH_LOG(format, ...) |
| 90 | * |
| 91 | * Optional debug logging function available for use in tests. |
| 92 | * Logging may be enabled or disabled by defining TH_LOG_ENABLED. |
| 93 | * E.g., #define TH_LOG_ENABLED 1 |
| 94 | * |
| 95 | * If no definition is provided, logging is enabled by default. |
| 96 | * |
| 97 | * If there is no way to print an error message for the process running the |
| 98 | * test (e.g. not allowed to write to stderr), it is still possible to get the |
| 99 | * ASSERT_* number for which the test failed. This behavior can be enabled by |
| 100 | * writing `_metadata->no_print = true;` before the check sequence that is |
| 101 | * unable to print. When an error occur, instead of printing an error message |
| 102 | * and calling `abort(3)`, the test process call `_exit(2)` with the assert |
| 103 | * number as argument, which is then printed by the parent process. |
| 104 | */ |
| 105 | #define TH_LOG(fmt, ...) do { \ |
| 106 | if (TH_LOG_ENABLED) \ |
| 107 | __TH_LOG(fmt, ##__VA_ARGS__); \ |
| 108 | } while (0) |
| 109 | |
| 110 | /* Unconditional logger for internal use. */ |
| 111 | #define __TH_LOG(fmt, ...) \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 112 | fprintf(TH_LOG_STREAM, "# %s:%d:%s:" fmt "\n", \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 113 | __FILE__, __LINE__, _metadata->name, ##__VA_ARGS__) |
| 114 | |
| 115 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 116 | * SKIP(statement, fmt, ...) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 117 | * |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 118 | * @statement: statement to run after reporting SKIP |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 119 | * @fmt: format string |
| 120 | * @...: optional arguments |
| 121 | * |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 122 | * This forces a "pass" after reporting why something is being skipped |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 123 | * and runs "statement", which is usually "return" or "goto skip". |
| 124 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 125 | #define SKIP(statement, fmt, ...) do { \ |
| 126 | snprintf(_metadata->results->reason, \ |
| 127 | sizeof(_metadata->results->reason), fmt, ##__VA_ARGS__); \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 128 | if (TH_LOG_ENABLED) { \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 129 | fprintf(TH_LOG_STREAM, "# SKIP %s\n", \ |
| 130 | _metadata->results->reason); \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 131 | } \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 132 | _metadata->passed = 1; \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 133 | _metadata->skip = 1; \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 134 | _metadata->trigger = 0; \ |
| 135 | statement; \ |
| 136 | } while (0) |
| 137 | |
| 138 | /** |
| 139 | * TEST(test_name) - Defines the test function and creates the registration |
| 140 | * stub |
| 141 | * |
| 142 | * @test_name: test name |
| 143 | * |
| 144 | * .. code-block:: c |
| 145 | * |
| 146 | * TEST(name) { implementation } |
| 147 | * |
| 148 | * Defines a test by name. |
| 149 | * Names must be unique and tests must not be run in parallel. The |
| 150 | * implementation containing block is a function and scoping should be treated |
| 151 | * as such. Returning early may be performed with a bare "return;" statement. |
| 152 | * |
| 153 | * EXPECT_* and ASSERT_* are valid in a TEST() { } context. |
| 154 | */ |
| 155 | #define TEST(test_name) __TEST_IMPL(test_name, -1) |
| 156 | |
| 157 | /** |
| 158 | * TEST_SIGNAL(test_name, signal) |
| 159 | * |
| 160 | * @test_name: test name |
| 161 | * @signal: signal number |
| 162 | * |
| 163 | * .. code-block:: c |
| 164 | * |
| 165 | * TEST_SIGNAL(name, signal) { implementation } |
| 166 | * |
| 167 | * Defines a test by name and the expected term signal. |
| 168 | * Names must be unique and tests must not be run in parallel. The |
| 169 | * implementation containing block is a function and scoping should be treated |
| 170 | * as such. Returning early may be performed with a bare "return;" statement. |
| 171 | * |
| 172 | * EXPECT_* and ASSERT_* are valid in a TEST() { } context. |
| 173 | */ |
| 174 | #define TEST_SIGNAL(test_name, signal) __TEST_IMPL(test_name, signal) |
| 175 | |
| 176 | #define __TEST_IMPL(test_name, _signal) \ |
| 177 | static void test_name(struct __test_metadata *_metadata); \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 178 | static inline void wrapper_##test_name( \ |
| 179 | struct __test_metadata *_metadata, \ |
| 180 | struct __fixture_variant_metadata *variant) \ |
| 181 | { \ |
| 182 | test_name(_metadata); \ |
| 183 | } \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 184 | static struct __test_metadata _##test_name##_object = \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 185 | { .name = #test_name, \ |
| 186 | .fn = &wrapper_##test_name, \ |
| 187 | .fixture = &_fixture_global, \ |
| 188 | .termsig = _signal, \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 189 | .timeout = TEST_TIMEOUT_DEFAULT, }; \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 190 | static void __attribute__((constructor)) _register_##test_name(void) \ |
| 191 | { \ |
| 192 | __register_test(&_##test_name##_object); \ |
| 193 | } \ |
| 194 | static void test_name( \ |
| 195 | struct __test_metadata __attribute__((unused)) *_metadata) |
| 196 | |
| 197 | /** |
| 198 | * FIXTURE_DATA(datatype_name) - Wraps the struct name so we have one less |
| 199 | * argument to pass around |
| 200 | * |
| 201 | * @datatype_name: datatype name |
| 202 | * |
| 203 | * .. code-block:: c |
| 204 | * |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 205 | * FIXTURE_DATA(datatype_name) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 206 | * |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 207 | * Almost always, you want just FIXTURE() instead (see below). |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 208 | * This call may be used when the type of the fixture data |
| 209 | * is needed. In general, this should not be needed unless |
| 210 | * the *self* is being passed to a helper directly. |
| 211 | */ |
| 212 | #define FIXTURE_DATA(datatype_name) struct _test_data_##datatype_name |
| 213 | |
| 214 | /** |
| 215 | * FIXTURE(fixture_name) - Called once per fixture to setup the data and |
| 216 | * register |
| 217 | * |
| 218 | * @fixture_name: fixture name |
| 219 | * |
| 220 | * .. code-block:: c |
| 221 | * |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 222 | * FIXTURE(fixture_name) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 223 | * type property1; |
| 224 | * ... |
| 225 | * }; |
| 226 | * |
| 227 | * Defines the data provided to TEST_F()-defined tests as *self*. It should be |
| 228 | * populated and cleaned up using FIXTURE_SETUP() and FIXTURE_TEARDOWN(). |
| 229 | */ |
| 230 | #define FIXTURE(fixture_name) \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 231 | FIXTURE_VARIANT(fixture_name); \ |
| 232 | static struct __fixture_metadata _##fixture_name##_fixture_object = \ |
| 233 | { .name = #fixture_name, }; \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 234 | static void __attribute__((constructor)) \ |
| 235 | _register_##fixture_name##_data(void) \ |
| 236 | { \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 237 | __register_fixture(&_##fixture_name##_fixture_object); \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 238 | } \ |
| 239 | FIXTURE_DATA(fixture_name) |
| 240 | |
| 241 | /** |
| 242 | * FIXTURE_SETUP(fixture_name) - Prepares the setup function for the fixture. |
| 243 | * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly. |
| 244 | * |
| 245 | * @fixture_name: fixture name |
| 246 | * |
| 247 | * .. code-block:: c |
| 248 | * |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 249 | * FIXTURE_SETUP(fixture_name) { implementation } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 250 | * |
| 251 | * Populates the required "setup" function for a fixture. An instance of the |
| 252 | * datatype defined with FIXTURE_DATA() will be exposed as *self* for the |
| 253 | * implementation. |
| 254 | * |
| 255 | * ASSERT_* are valid for use in this context and will prempt the execution |
| 256 | * of any dependent fixture tests. |
| 257 | * |
| 258 | * A bare "return;" statement may be used to return early. |
| 259 | */ |
| 260 | #define FIXTURE_SETUP(fixture_name) \ |
| 261 | void fixture_name##_setup( \ |
| 262 | struct __test_metadata __attribute__((unused)) *_metadata, \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 263 | FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \ |
| 264 | const FIXTURE_VARIANT(fixture_name) \ |
| 265 | __attribute__((unused)) *variant) |
| 266 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 267 | /** |
| 268 | * FIXTURE_TEARDOWN(fixture_name) |
| 269 | * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly. |
| 270 | * |
| 271 | * @fixture_name: fixture name |
| 272 | * |
| 273 | * .. code-block:: c |
| 274 | * |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 275 | * FIXTURE_TEARDOWN(fixture_name) { implementation } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 276 | * |
| 277 | * Populates the required "teardown" function for a fixture. An instance of the |
| 278 | * datatype defined with FIXTURE_DATA() will be exposed as *self* for the |
| 279 | * implementation to clean up. |
| 280 | * |
| 281 | * A bare "return;" statement may be used to return early. |
| 282 | */ |
| 283 | #define FIXTURE_TEARDOWN(fixture_name) \ |
| 284 | void fixture_name##_teardown( \ |
| 285 | struct __test_metadata __attribute__((unused)) *_metadata, \ |
| 286 | FIXTURE_DATA(fixture_name) __attribute__((unused)) *self) |
| 287 | |
| 288 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 289 | * FIXTURE_VARIANT(fixture_name) - Optionally called once per fixture |
| 290 | * to declare fixture variant |
| 291 | * |
| 292 | * @fixture_name: fixture name |
| 293 | * |
| 294 | * .. code-block:: c |
| 295 | * |
| 296 | * FIXTURE_VARIANT(fixture_name) { |
| 297 | * type property1; |
| 298 | * ... |
| 299 | * }; |
| 300 | * |
| 301 | * Defines type of constant parameters provided to FIXTURE_SETUP() and TEST_F() |
| 302 | * as *variant*. Variants allow the same tests to be run with different |
| 303 | * arguments. |
| 304 | */ |
| 305 | #define FIXTURE_VARIANT(fixture_name) struct _fixture_variant_##fixture_name |
| 306 | |
| 307 | /** |
| 308 | * FIXTURE_VARIANT_ADD(fixture_name, variant_name) - Called once per fixture |
| 309 | * variant to setup and register the data |
| 310 | * |
| 311 | * @fixture_name: fixture name |
| 312 | * @variant_name: name of the parameter set |
| 313 | * |
| 314 | * .. code-block:: c |
| 315 | * |
| 316 | * FIXTURE_VARIANT_ADD(fixture_name, variant_name) { |
| 317 | * .property1 = val1, |
| 318 | * ... |
| 319 | * }; |
| 320 | * |
| 321 | * Defines a variant of the test fixture, provided to FIXTURE_SETUP() and |
| 322 | * TEST_F() as *variant*. Tests of each fixture will be run once for each |
| 323 | * variant. |
| 324 | */ |
| 325 | #define FIXTURE_VARIANT_ADD(fixture_name, variant_name) \ |
| 326 | extern FIXTURE_VARIANT(fixture_name) \ |
| 327 | _##fixture_name##_##variant_name##_variant; \ |
| 328 | static struct __fixture_variant_metadata \ |
| 329 | _##fixture_name##_##variant_name##_object = \ |
| 330 | { .name = #variant_name, \ |
| 331 | .data = &_##fixture_name##_##variant_name##_variant}; \ |
| 332 | static void __attribute__((constructor)) \ |
| 333 | _register_##fixture_name##_##variant_name(void) \ |
| 334 | { \ |
| 335 | __register_fixture_variant(&_##fixture_name##_fixture_object, \ |
| 336 | &_##fixture_name##_##variant_name##_object); \ |
| 337 | } \ |
| 338 | FIXTURE_VARIANT(fixture_name) \ |
| 339 | _##fixture_name##_##variant_name##_variant = |
| 340 | |
| 341 | /** |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 342 | * TEST_F(fixture_name, test_name) - Emits test registration and helpers for |
| 343 | * fixture-based test cases |
| 344 | * |
| 345 | * @fixture_name: fixture name |
| 346 | * @test_name: test name |
| 347 | * |
| 348 | * .. code-block:: c |
| 349 | * |
| 350 | * TEST_F(fixture, name) { implementation } |
| 351 | * |
| 352 | * Defines a test that depends on a fixture (e.g., is part of a test case). |
| 353 | * Very similar to TEST() except that *self* is the setup instance of fixture's |
| 354 | * datatype exposed for use by the implementation. |
| 355 | * |
| 356 | * Warning: use of ASSERT_* here will skip TEARDOWN. |
| 357 | */ |
| 358 | /* TODO(wad) register fixtures on dedicated test lists. */ |
| 359 | #define TEST_F(fixture_name, test_name) \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 360 | __TEST_F_IMPL(fixture_name, test_name, -1, TEST_TIMEOUT_DEFAULT) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 361 | |
| 362 | #define TEST_F_SIGNAL(fixture_name, test_name, signal) \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 363 | __TEST_F_IMPL(fixture_name, test_name, signal, TEST_TIMEOUT_DEFAULT) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 364 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 365 | #define TEST_F_TIMEOUT(fixture_name, test_name, timeout) \ |
| 366 | __TEST_F_IMPL(fixture_name, test_name, -1, timeout) |
| 367 | |
| 368 | #define __TEST_F_IMPL(fixture_name, test_name, signal, tmout) \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 369 | static void fixture_name##_##test_name( \ |
| 370 | struct __test_metadata *_metadata, \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 371 | FIXTURE_DATA(fixture_name) *self, \ |
| 372 | const FIXTURE_VARIANT(fixture_name) *variant); \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 373 | static inline void wrapper_##fixture_name##_##test_name( \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 374 | struct __test_metadata *_metadata, \ |
| 375 | struct __fixture_variant_metadata *variant) \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 376 | { \ |
| 377 | /* fixture data is alloced, setup, and torn down per call. */ \ |
| 378 | FIXTURE_DATA(fixture_name) self; \ |
| 379 | memset(&self, 0, sizeof(FIXTURE_DATA(fixture_name))); \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 380 | fixture_name##_setup(_metadata, &self, variant->data); \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 381 | /* Let setup failure terminate early. */ \ |
| 382 | if (!_metadata->passed) \ |
| 383 | return; \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 384 | fixture_name##_##test_name(_metadata, &self, variant->data); \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 385 | fixture_name##_teardown(_metadata, &self); \ |
| 386 | } \ |
| 387 | static struct __test_metadata \ |
| 388 | _##fixture_name##_##test_name##_object = { \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 389 | .name = #test_name, \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 390 | .fn = &wrapper_##fixture_name##_##test_name, \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 391 | .fixture = &_##fixture_name##_fixture_object, \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 392 | .termsig = signal, \ |
| 393 | .timeout = tmout, \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 394 | }; \ |
| 395 | static void __attribute__((constructor)) \ |
| 396 | _register_##fixture_name##_##test_name(void) \ |
| 397 | { \ |
| 398 | __register_test(&_##fixture_name##_##test_name##_object); \ |
| 399 | } \ |
| 400 | static void fixture_name##_##test_name( \ |
| 401 | struct __test_metadata __attribute__((unused)) *_metadata, \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 402 | FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \ |
| 403 | const FIXTURE_VARIANT(fixture_name) \ |
| 404 | __attribute__((unused)) *variant) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 405 | |
| 406 | /** |
| 407 | * TEST_HARNESS_MAIN - Simple wrapper to run the test harness |
| 408 | * |
| 409 | * .. code-block:: c |
| 410 | * |
| 411 | * TEST_HARNESS_MAIN |
| 412 | * |
| 413 | * Use once to append a main() to the test file. |
| 414 | */ |
| 415 | #define TEST_HARNESS_MAIN \ |
| 416 | static void __attribute__((constructor)) \ |
| 417 | __constructor_order_last(void) \ |
| 418 | { \ |
| 419 | if (!__constructor_order) \ |
| 420 | __constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; \ |
| 421 | } \ |
| 422 | int main(int argc, char **argv) { \ |
| 423 | return test_harness_run(argc, argv); \ |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * DOC: operators |
| 428 | * |
| 429 | * Operators for use in TEST() and TEST_F(). |
| 430 | * ASSERT_* calls will stop test execution immediately. |
| 431 | * EXPECT_* calls will emit a failure warning, note it, and continue. |
| 432 | */ |
| 433 | |
| 434 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 435 | * ASSERT_EQ() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 436 | * |
| 437 | * @expected: expected value |
| 438 | * @seen: measured value |
| 439 | * |
| 440 | * ASSERT_EQ(expected, measured): expected == measured |
| 441 | */ |
| 442 | #define ASSERT_EQ(expected, seen) \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 443 | __EXPECT(expected, #expected, seen, #seen, ==, 1) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 444 | |
| 445 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 446 | * ASSERT_NE() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 447 | * |
| 448 | * @expected: expected value |
| 449 | * @seen: measured value |
| 450 | * |
| 451 | * ASSERT_NE(expected, measured): expected != measured |
| 452 | */ |
| 453 | #define ASSERT_NE(expected, seen) \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 454 | __EXPECT(expected, #expected, seen, #seen, !=, 1) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 455 | |
| 456 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 457 | * ASSERT_LT() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 458 | * |
| 459 | * @expected: expected value |
| 460 | * @seen: measured value |
| 461 | * |
| 462 | * ASSERT_LT(expected, measured): expected < measured |
| 463 | */ |
| 464 | #define ASSERT_LT(expected, seen) \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 465 | __EXPECT(expected, #expected, seen, #seen, <, 1) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 466 | |
| 467 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 468 | * ASSERT_LE() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 469 | * |
| 470 | * @expected: expected value |
| 471 | * @seen: measured value |
| 472 | * |
| 473 | * ASSERT_LE(expected, measured): expected <= measured |
| 474 | */ |
| 475 | #define ASSERT_LE(expected, seen) \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 476 | __EXPECT(expected, #expected, seen, #seen, <=, 1) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 477 | |
| 478 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 479 | * ASSERT_GT() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 480 | * |
| 481 | * @expected: expected value |
| 482 | * @seen: measured value |
| 483 | * |
| 484 | * ASSERT_GT(expected, measured): expected > measured |
| 485 | */ |
| 486 | #define ASSERT_GT(expected, seen) \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 487 | __EXPECT(expected, #expected, seen, #seen, >, 1) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 488 | |
| 489 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 490 | * ASSERT_GE() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 491 | * |
| 492 | * @expected: expected value |
| 493 | * @seen: measured value |
| 494 | * |
| 495 | * ASSERT_GE(expected, measured): expected >= measured |
| 496 | */ |
| 497 | #define ASSERT_GE(expected, seen) \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 498 | __EXPECT(expected, #expected, seen, #seen, >=, 1) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 499 | |
| 500 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 501 | * ASSERT_NULL() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 502 | * |
| 503 | * @seen: measured value |
| 504 | * |
| 505 | * ASSERT_NULL(measured): NULL == measured |
| 506 | */ |
| 507 | #define ASSERT_NULL(seen) \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 508 | __EXPECT(NULL, "NULL", seen, #seen, ==, 1) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 509 | |
| 510 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 511 | * ASSERT_TRUE() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 512 | * |
| 513 | * @seen: measured value |
| 514 | * |
| 515 | * ASSERT_TRUE(measured): measured != 0 |
| 516 | */ |
| 517 | #define ASSERT_TRUE(seen) \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 518 | __EXPECT(0, "0", seen, #seen, !=, 1) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 519 | |
| 520 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 521 | * ASSERT_FALSE() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 522 | * |
| 523 | * @seen: measured value |
| 524 | * |
| 525 | * ASSERT_FALSE(measured): measured == 0 |
| 526 | */ |
| 527 | #define ASSERT_FALSE(seen) \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 528 | __EXPECT(0, "0", seen, #seen, ==, 1) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 529 | |
| 530 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 531 | * ASSERT_STREQ() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 532 | * |
| 533 | * @expected: expected value |
| 534 | * @seen: measured value |
| 535 | * |
| 536 | * ASSERT_STREQ(expected, measured): !strcmp(expected, measured) |
| 537 | */ |
| 538 | #define ASSERT_STREQ(expected, seen) \ |
| 539 | __EXPECT_STR(expected, seen, ==, 1) |
| 540 | |
| 541 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 542 | * ASSERT_STRNE() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 543 | * |
| 544 | * @expected: expected value |
| 545 | * @seen: measured value |
| 546 | * |
| 547 | * ASSERT_STRNE(expected, measured): strcmp(expected, measured) |
| 548 | */ |
| 549 | #define ASSERT_STRNE(expected, seen) \ |
| 550 | __EXPECT_STR(expected, seen, !=, 1) |
| 551 | |
| 552 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 553 | * EXPECT_EQ() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 554 | * |
| 555 | * @expected: expected value |
| 556 | * @seen: measured value |
| 557 | * |
| 558 | * EXPECT_EQ(expected, measured): expected == measured |
| 559 | */ |
| 560 | #define EXPECT_EQ(expected, seen) \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 561 | __EXPECT(expected, #expected, seen, #seen, ==, 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 562 | |
| 563 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 564 | * EXPECT_NE() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 565 | * |
| 566 | * @expected: expected value |
| 567 | * @seen: measured value |
| 568 | * |
| 569 | * EXPECT_NE(expected, measured): expected != measured |
| 570 | */ |
| 571 | #define EXPECT_NE(expected, seen) \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 572 | __EXPECT(expected, #expected, seen, #seen, !=, 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 573 | |
| 574 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 575 | * EXPECT_LT() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 576 | * |
| 577 | * @expected: expected value |
| 578 | * @seen: measured value |
| 579 | * |
| 580 | * EXPECT_LT(expected, measured): expected < measured |
| 581 | */ |
| 582 | #define EXPECT_LT(expected, seen) \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 583 | __EXPECT(expected, #expected, seen, #seen, <, 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 584 | |
| 585 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 586 | * EXPECT_LE() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 587 | * |
| 588 | * @expected: expected value |
| 589 | * @seen: measured value |
| 590 | * |
| 591 | * EXPECT_LE(expected, measured): expected <= measured |
| 592 | */ |
| 593 | #define EXPECT_LE(expected, seen) \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 594 | __EXPECT(expected, #expected, seen, #seen, <=, 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 595 | |
| 596 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 597 | * EXPECT_GT() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 598 | * |
| 599 | * @expected: expected value |
| 600 | * @seen: measured value |
| 601 | * |
| 602 | * EXPECT_GT(expected, measured): expected > measured |
| 603 | */ |
| 604 | #define EXPECT_GT(expected, seen) \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 605 | __EXPECT(expected, #expected, seen, #seen, >, 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 606 | |
| 607 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 608 | * EXPECT_GE() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 609 | * |
| 610 | * @expected: expected value |
| 611 | * @seen: measured value |
| 612 | * |
| 613 | * EXPECT_GE(expected, measured): expected >= measured |
| 614 | */ |
| 615 | #define EXPECT_GE(expected, seen) \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 616 | __EXPECT(expected, #expected, seen, #seen, >=, 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 617 | |
| 618 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 619 | * EXPECT_NULL() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 620 | * |
| 621 | * @seen: measured value |
| 622 | * |
| 623 | * EXPECT_NULL(measured): NULL == measured |
| 624 | */ |
| 625 | #define EXPECT_NULL(seen) \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 626 | __EXPECT(NULL, "NULL", seen, #seen, ==, 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 627 | |
| 628 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 629 | * EXPECT_TRUE() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 630 | * |
| 631 | * @seen: measured value |
| 632 | * |
| 633 | * EXPECT_TRUE(measured): 0 != measured |
| 634 | */ |
| 635 | #define EXPECT_TRUE(seen) \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 636 | __EXPECT(0, "0", seen, #seen, !=, 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 637 | |
| 638 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 639 | * EXPECT_FALSE() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 640 | * |
| 641 | * @seen: measured value |
| 642 | * |
| 643 | * EXPECT_FALSE(measured): 0 == measured |
| 644 | */ |
| 645 | #define EXPECT_FALSE(seen) \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 646 | __EXPECT(0, "0", seen, #seen, ==, 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 647 | |
| 648 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 649 | * EXPECT_STREQ() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 650 | * |
| 651 | * @expected: expected value |
| 652 | * @seen: measured value |
| 653 | * |
| 654 | * EXPECT_STREQ(expected, measured): !strcmp(expected, measured) |
| 655 | */ |
| 656 | #define EXPECT_STREQ(expected, seen) \ |
| 657 | __EXPECT_STR(expected, seen, ==, 0) |
| 658 | |
| 659 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 660 | * EXPECT_STRNE() |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 661 | * |
| 662 | * @expected: expected value |
| 663 | * @seen: measured value |
| 664 | * |
| 665 | * EXPECT_STRNE(expected, measured): strcmp(expected, measured) |
| 666 | */ |
| 667 | #define EXPECT_STRNE(expected, seen) \ |
| 668 | __EXPECT_STR(expected, seen, !=, 0) |
| 669 | |
| 670 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 671 | |
| 672 | /* Support an optional handler after and ASSERT_* or EXPECT_*. The approach is |
| 673 | * not thread-safe, but it should be fine in most sane test scenarios. |
| 674 | * |
| 675 | * Using __bail(), which optionally abort()s, is the easiest way to early |
| 676 | * return while still providing an optional block to the API consumer. |
| 677 | */ |
| 678 | #define OPTIONAL_HANDLER(_assert) \ |
| 679 | for (; _metadata->trigger; _metadata->trigger = \ |
| 680 | __bail(_assert, _metadata->no_print, _metadata->step)) |
| 681 | |
| 682 | #define __INC_STEP(_metadata) \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 683 | /* Keep "step" below 255 (which is used for "SKIP" reporting). */ \ |
| 684 | if (_metadata->passed && _metadata->step < 253) \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 685 | _metadata->step++; |
| 686 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 687 | #define is_signed_type(var) (!!(((__typeof__(var))(-1)) < (__typeof__(var))1)) |
| 688 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 689 | #define __EXPECT(_expected, _expected_str, _seen, _seen_str, _t, _assert) do { \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 690 | /* Avoid multiple evaluation of the cases */ \ |
| 691 | __typeof__(_expected) __exp = (_expected); \ |
| 692 | __typeof__(_seen) __seen = (_seen); \ |
| 693 | if (_assert) __INC_STEP(_metadata); \ |
| 694 | if (!(__exp _t __seen)) { \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 695 | /* Report with actual signedness to avoid weird output. */ \ |
| 696 | switch (is_signed_type(__exp) * 2 + is_signed_type(__seen)) { \ |
| 697 | case 0: { \ |
| 698 | unsigned long long __exp_print = (uintptr_t)__exp; \ |
| 699 | unsigned long long __seen_print = (uintptr_t)__seen; \ |
| 700 | __TH_LOG("Expected %s (%llu) %s %s (%llu)", \ |
| 701 | _expected_str, __exp_print, #_t, \ |
| 702 | _seen_str, __seen_print); \ |
| 703 | break; \ |
| 704 | } \ |
| 705 | case 1: { \ |
| 706 | unsigned long long __exp_print = (uintptr_t)__exp; \ |
| 707 | long long __seen_print = (intptr_t)__seen; \ |
| 708 | __TH_LOG("Expected %s (%llu) %s %s (%lld)", \ |
| 709 | _expected_str, __exp_print, #_t, \ |
| 710 | _seen_str, __seen_print); \ |
| 711 | break; \ |
| 712 | } \ |
| 713 | case 2: { \ |
| 714 | long long __exp_print = (intptr_t)__exp; \ |
| 715 | unsigned long long __seen_print = (uintptr_t)__seen; \ |
| 716 | __TH_LOG("Expected %s (%lld) %s %s (%llu)", \ |
| 717 | _expected_str, __exp_print, #_t, \ |
| 718 | _seen_str, __seen_print); \ |
| 719 | break; \ |
| 720 | } \ |
| 721 | case 3: { \ |
| 722 | long long __exp_print = (intptr_t)__exp; \ |
| 723 | long long __seen_print = (intptr_t)__seen; \ |
| 724 | __TH_LOG("Expected %s (%lld) %s %s (%lld)", \ |
| 725 | _expected_str, __exp_print, #_t, \ |
| 726 | _seen_str, __seen_print); \ |
| 727 | break; \ |
| 728 | } \ |
| 729 | } \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 730 | _metadata->passed = 0; \ |
| 731 | /* Ensure the optional handler is triggered */ \ |
| 732 | _metadata->trigger = 1; \ |
| 733 | } \ |
| 734 | } while (0); OPTIONAL_HANDLER(_assert) |
| 735 | |
| 736 | #define __EXPECT_STR(_expected, _seen, _t, _assert) do { \ |
| 737 | const char *__exp = (_expected); \ |
| 738 | const char *__seen = (_seen); \ |
| 739 | if (_assert) __INC_STEP(_metadata); \ |
| 740 | if (!(strcmp(__exp, __seen) _t 0)) { \ |
| 741 | __TH_LOG("Expected '%s' %s '%s'.", __exp, #_t, __seen); \ |
| 742 | _metadata->passed = 0; \ |
| 743 | _metadata->trigger = 1; \ |
| 744 | } \ |
| 745 | } while (0); OPTIONAL_HANDLER(_assert) |
| 746 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 747 | /* List helpers */ |
| 748 | #define __LIST_APPEND(head, item) \ |
| 749 | { \ |
| 750 | /* Circular linked list where only prev is circular. */ \ |
| 751 | if (head == NULL) { \ |
| 752 | head = item; \ |
| 753 | item->next = NULL; \ |
| 754 | item->prev = item; \ |
| 755 | return; \ |
| 756 | } \ |
| 757 | if (__constructor_order == _CONSTRUCTOR_ORDER_FORWARD) { \ |
| 758 | item->next = NULL; \ |
| 759 | item->prev = head->prev; \ |
| 760 | item->prev->next = item; \ |
| 761 | head->prev = item; \ |
| 762 | } else { \ |
| 763 | item->next = head; \ |
| 764 | item->next->prev = item; \ |
| 765 | item->prev = item; \ |
| 766 | head = item; \ |
| 767 | } \ |
| 768 | } |
| 769 | |
| 770 | struct __test_results { |
| 771 | char reason[1024]; /* Reason for test result */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 772 | }; |
| 773 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 774 | struct __test_metadata; |
| 775 | struct __fixture_variant_metadata; |
| 776 | |
| 777 | /* Contains all the information about a fixture. */ |
| 778 | struct __fixture_metadata { |
| 779 | const char *name; |
| 780 | struct __test_metadata *tests; |
| 781 | struct __fixture_variant_metadata *variant; |
| 782 | struct __fixture_metadata *prev, *next; |
| 783 | } _fixture_global __attribute__((unused)) = { |
| 784 | .name = "global", |
| 785 | .prev = &_fixture_global, |
| 786 | }; |
| 787 | |
| 788 | static struct __fixture_metadata *__fixture_list = &_fixture_global; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 789 | static int __constructor_order; |
| 790 | |
| 791 | #define _CONSTRUCTOR_ORDER_FORWARD 1 |
| 792 | #define _CONSTRUCTOR_ORDER_BACKWARD -1 |
| 793 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 794 | static inline void __register_fixture(struct __fixture_metadata *f) |
| 795 | { |
| 796 | __LIST_APPEND(__fixture_list, f); |
| 797 | } |
| 798 | |
| 799 | struct __fixture_variant_metadata { |
| 800 | const char *name; |
| 801 | const void *data; |
| 802 | struct __fixture_variant_metadata *prev, *next; |
| 803 | }; |
| 804 | |
| 805 | static inline void |
| 806 | __register_fixture_variant(struct __fixture_metadata *f, |
| 807 | struct __fixture_variant_metadata *variant) |
| 808 | { |
| 809 | __LIST_APPEND(f->variant, variant); |
| 810 | } |
| 811 | |
| 812 | /* Contains all the information for test execution and status checking. */ |
| 813 | struct __test_metadata { |
| 814 | const char *name; |
| 815 | void (*fn)(struct __test_metadata *, |
| 816 | struct __fixture_variant_metadata *); |
| 817 | pid_t pid; /* pid of test when being run */ |
| 818 | struct __fixture_metadata *fixture; |
| 819 | int termsig; |
| 820 | int passed; |
| 821 | int skip; /* did SKIP get used? */ |
| 822 | int trigger; /* extra handler after the evaluation */ |
| 823 | int timeout; /* seconds to wait for test timeout */ |
| 824 | bool timed_out; /* did this test timeout instead of exiting? */ |
| 825 | __u8 step; |
| 826 | bool no_print; /* manual trigger when TH_LOG_STREAM is not available */ |
| 827 | struct __test_results *results; |
| 828 | struct __test_metadata *prev, *next; |
| 829 | }; |
| 830 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 831 | /* |
| 832 | * Since constructors are called in reverse order, reverse the test |
| 833 | * list so tests are run in source declaration order. |
| 834 | * https://gcc.gnu.org/onlinedocs/gccint/Initialization.html |
| 835 | * However, it seems not all toolchains do this correctly, so use |
| 836 | * __constructor_order to detect which direction is called first |
| 837 | * and adjust list building logic to get things running in the right |
| 838 | * direction. |
| 839 | */ |
| 840 | static inline void __register_test(struct __test_metadata *t) |
| 841 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 842 | __LIST_APPEND(t->fixture->tests, t); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | static inline int __bail(int for_realz, bool no_print, __u8 step) |
| 846 | { |
| 847 | if (for_realz) { |
| 848 | if (no_print) |
| 849 | _exit(step); |
| 850 | abort(); |
| 851 | } |
| 852 | return 0; |
| 853 | } |
| 854 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 855 | struct __test_metadata *__active_test; |
| 856 | static void __timeout_handler(int sig, siginfo_t *info, void *ucontext) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 857 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 858 | struct __test_metadata *t = __active_test; |
| 859 | |
| 860 | /* Sanity check handler execution environment. */ |
| 861 | if (!t) { |
| 862 | fprintf(TH_LOG_STREAM, |
| 863 | "# no active test in SIGALRM handler!?\n"); |
| 864 | abort(); |
| 865 | } |
| 866 | if (sig != SIGALRM || sig != info->si_signo) { |
| 867 | fprintf(TH_LOG_STREAM, |
| 868 | "# %s: SIGALRM handler caught signal %d!?\n", |
| 869 | t->name, sig != SIGALRM ? sig : info->si_signo); |
| 870 | abort(); |
| 871 | } |
| 872 | |
| 873 | t->timed_out = true; |
| 874 | // signal process group |
| 875 | kill(-(t->pid), SIGKILL); |
| 876 | } |
| 877 | |
| 878 | void __wait_for_test(struct __test_metadata *t) |
| 879 | { |
| 880 | struct sigaction action = { |
| 881 | .sa_sigaction = __timeout_handler, |
| 882 | .sa_flags = SA_SIGINFO, |
| 883 | }; |
| 884 | struct sigaction saved_action; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 885 | int status; |
| 886 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 887 | if (sigaction(SIGALRM, &action, &saved_action)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 888 | t->passed = 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 889 | fprintf(TH_LOG_STREAM, |
| 890 | "# %s: unable to install SIGALRM handler\n", |
| 891 | t->name); |
| 892 | return; |
| 893 | } |
| 894 | __active_test = t; |
| 895 | t->timed_out = false; |
| 896 | alarm(t->timeout); |
| 897 | waitpid(t->pid, &status, 0); |
| 898 | alarm(0); |
| 899 | if (sigaction(SIGALRM, &saved_action, NULL)) { |
| 900 | t->passed = 0; |
| 901 | fprintf(TH_LOG_STREAM, |
| 902 | "# %s: unable to uninstall SIGALRM handler\n", |
| 903 | t->name); |
| 904 | return; |
| 905 | } |
| 906 | __active_test = NULL; |
| 907 | |
| 908 | if (t->timed_out) { |
| 909 | t->passed = 0; |
| 910 | fprintf(TH_LOG_STREAM, |
| 911 | "# %s: Test terminated by timeout\n", t->name); |
| 912 | } else if (WIFEXITED(status)) { |
| 913 | if (t->termsig != -1) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 914 | t->passed = 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 915 | fprintf(TH_LOG_STREAM, |
| 916 | "# %s: Test exited normally instead of by signal (code: %d)\n", |
| 917 | t->name, |
| 918 | WEXITSTATUS(status)); |
| 919 | } else { |
| 920 | switch (WEXITSTATUS(status)) { |
| 921 | /* Success */ |
| 922 | case 0: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 923 | t->passed = 1; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 924 | break; |
| 925 | /* SKIP */ |
| 926 | case 255: |
| 927 | t->passed = 1; |
| 928 | t->skip = 1; |
| 929 | break; |
| 930 | /* Other failure, assume step report. */ |
| 931 | default: |
| 932 | t->passed = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 933 | fprintf(TH_LOG_STREAM, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 934 | "# %s: Test failed at step #%d\n", |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 935 | t->name, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 936 | WEXITSTATUS(status)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 937 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 938 | } |
| 939 | } else if (WIFSIGNALED(status)) { |
| 940 | t->passed = 0; |
| 941 | if (WTERMSIG(status) == SIGABRT) { |
| 942 | fprintf(TH_LOG_STREAM, |
| 943 | "# %s: Test terminated by assertion\n", |
| 944 | t->name); |
| 945 | } else if (WTERMSIG(status) == t->termsig) { |
| 946 | t->passed = 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 947 | } else { |
| 948 | fprintf(TH_LOG_STREAM, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 949 | "# %s: Test terminated unexpectedly by signal %d\n", |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 950 | t->name, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 951 | WTERMSIG(status)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 952 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 953 | } else { |
| 954 | fprintf(TH_LOG_STREAM, |
| 955 | "# %s: Test ended in some other way [%u]\n", |
| 956 | t->name, |
| 957 | status); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 958 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 959 | } |
| 960 | |
| 961 | void __run_test(struct __fixture_metadata *f, |
| 962 | struct __fixture_variant_metadata *variant, |
| 963 | struct __test_metadata *t) |
| 964 | { |
| 965 | /* reset test struct */ |
| 966 | t->passed = 1; |
| 967 | t->skip = 0; |
| 968 | t->trigger = 0; |
| 969 | t->step = 1; |
| 970 | t->no_print = 0; |
| 971 | memset(t->results->reason, 0, sizeof(t->results->reason)); |
| 972 | |
| 973 | ksft_print_msg(" RUN %s%s%s.%s ...\n", |
| 974 | f->name, variant->name[0] ? "." : "", variant->name, t->name); |
| 975 | |
| 976 | /* Make sure output buffers are flushed before fork */ |
| 977 | fflush(stdout); |
| 978 | fflush(stderr); |
| 979 | |
| 980 | t->pid = fork(); |
| 981 | if (t->pid < 0) { |
| 982 | ksft_print_msg("ERROR SPAWNING TEST CHILD\n"); |
| 983 | t->passed = 0; |
| 984 | } else if (t->pid == 0) { |
| 985 | setpgrp(); |
| 986 | t->fn(t, variant); |
| 987 | if (t->skip) |
| 988 | _exit(255); |
| 989 | /* Pass is exit 0 */ |
| 990 | if (t->passed) |
| 991 | _exit(0); |
| 992 | /* Something else happened, report the step. */ |
| 993 | _exit(t->step); |
| 994 | } else { |
| 995 | __wait_for_test(t); |
| 996 | } |
| 997 | ksft_print_msg(" %4s %s%s%s.%s\n", t->passed ? "OK" : "FAIL", |
| 998 | f->name, variant->name[0] ? "." : "", variant->name, t->name); |
| 999 | |
| 1000 | if (t->skip) |
| 1001 | ksft_test_result_skip("%s\n", t->results->reason[0] ? |
| 1002 | t->results->reason : "unknown"); |
| 1003 | else |
| 1004 | ksft_test_result(t->passed, "%s%s%s.%s\n", |
| 1005 | f->name, variant->name[0] ? "." : "", variant->name, t->name); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | static int test_harness_run(int __attribute__((unused)) argc, |
| 1009 | char __attribute__((unused)) **argv) |
| 1010 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1011 | struct __fixture_variant_metadata no_variant = { .name = "", }; |
| 1012 | struct __fixture_variant_metadata *v; |
| 1013 | struct __fixture_metadata *f; |
| 1014 | struct __test_results *results; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1015 | struct __test_metadata *t; |
| 1016 | int ret = 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1017 | unsigned int case_count = 0, test_count = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1018 | unsigned int count = 0; |
| 1019 | unsigned int pass_count = 0; |
| 1020 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1021 | for (f = __fixture_list; f; f = f->next) { |
| 1022 | for (v = f->variant ?: &no_variant; v; v = v->next) { |
| 1023 | case_count++; |
| 1024 | for (t = f->tests; t; t = t->next) |
| 1025 | test_count++; |
| 1026 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1027 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1028 | |
| 1029 | results = mmap(NULL, sizeof(*results), PROT_READ | PROT_WRITE, |
| 1030 | MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
| 1031 | |
| 1032 | ksft_print_header(); |
| 1033 | ksft_set_plan(test_count); |
| 1034 | ksft_print_msg("Starting %u tests from %u test cases.\n", |
| 1035 | test_count, case_count); |
| 1036 | for (f = __fixture_list; f; f = f->next) { |
| 1037 | for (v = f->variant ?: &no_variant; v; v = v->next) { |
| 1038 | for (t = f->tests; t; t = t->next) { |
| 1039 | count++; |
| 1040 | t->results = results; |
| 1041 | __run_test(f, v, t); |
| 1042 | t->results = NULL; |
| 1043 | if (t->passed) |
| 1044 | pass_count++; |
| 1045 | else |
| 1046 | ret = 1; |
| 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 | munmap(results, sizeof(*results)); |
| 1051 | |
| 1052 | ksft_print_msg("%s: %u / %u tests passed.\n", ret ? "FAILED" : "PASSED", |
| 1053 | pass_count, count); |
| 1054 | ksft_exit(ret == 0); |
| 1055 | |
| 1056 | /* unreachable */ |
| 1057 | return KSFT_FAIL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | static void __attribute__((constructor)) __constructor_order_first(void) |
| 1061 | { |
| 1062 | if (!__constructor_order) |
| 1063 | __constructor_order = _CONSTRUCTOR_ORDER_FORWARD; |
| 1064 | } |
| 1065 | |
| 1066 | #endif /* __KSELFTEST_HARNESS_H */ |