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