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