| .. | .. |
|---|
| 1 | +/* SPDX-License-Identifier: GPL-2.0-only */ |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
|---|
| 3 | | - * Use of this source code is governed by the GPLv2 license. |
|---|
| 4 | 4 | * |
|---|
| 5 | 5 | * kselftest_harness.h: simple C unit test helper. |
|---|
| 6 | 6 | * |
|---|
| .. | .. |
|---|
| 50 | 50 | #ifndef __KSELFTEST_HARNESS_H |
|---|
| 51 | 51 | #define __KSELFTEST_HARNESS_H |
|---|
| 52 | 52 | |
|---|
| 53 | +#ifndef _GNU_SOURCE |
|---|
| 53 | 54 | #define _GNU_SOURCE |
|---|
| 55 | +#endif |
|---|
| 54 | 56 | #include <asm/types.h> |
|---|
| 55 | 57 | #include <errno.h> |
|---|
| 56 | 58 | #include <stdbool.h> |
|---|
| .. | .. |
|---|
| 58 | 60 | #include <stdio.h> |
|---|
| 59 | 61 | #include <stdlib.h> |
|---|
| 60 | 62 | #include <string.h> |
|---|
| 63 | +#include <sys/mman.h> |
|---|
| 61 | 64 | #include <sys/types.h> |
|---|
| 62 | 65 | #include <sys/wait.h> |
|---|
| 63 | 66 | #include <unistd.h> |
|---|
| 64 | 67 | |
|---|
| 68 | +#include "kselftest.h" |
|---|
| 69 | + |
|---|
| 70 | +#define TEST_TIMEOUT_DEFAULT 30 |
|---|
| 65 | 71 | |
|---|
| 66 | 72 | /* Utilities exposed to the test definitions */ |
|---|
| 67 | 73 | #ifndef TH_LOG_STREAM |
|---|
| .. | .. |
|---|
| 103 | 109 | |
|---|
| 104 | 110 | /* Unconditional logger for internal use. */ |
|---|
| 105 | 111 | #define __TH_LOG(fmt, ...) \ |
|---|
| 106 | | - fprintf(TH_LOG_STREAM, "%s:%d:%s:" fmt "\n", \ |
|---|
| 112 | + fprintf(TH_LOG_STREAM, "# %s:%d:%s:" fmt "\n", \ |
|---|
| 107 | 113 | __FILE__, __LINE__, _metadata->name, ##__VA_ARGS__) |
|---|
| 108 | 114 | |
|---|
| 109 | 115 | /** |
|---|
| 110 | | - * XFAIL(statement, fmt, ...) |
|---|
| 116 | + * SKIP(statement, fmt, ...) |
|---|
| 111 | 117 | * |
|---|
| 112 | | - * @statement: statement to run after reporting XFAIL |
|---|
| 118 | + * @statement: statement to run after reporting SKIP |
|---|
| 113 | 119 | * @fmt: format string |
|---|
| 114 | 120 | * @...: optional arguments |
|---|
| 115 | 121 | * |
|---|
| 116 | | - * This forces a "pass" after reporting a failure with an XFAIL prefix, |
|---|
| 122 | + * This forces a "pass" after reporting why something is being skipped |
|---|
| 117 | 123 | * and runs "statement", which is usually "return" or "goto skip". |
|---|
| 118 | 124 | */ |
|---|
| 119 | | -#define XFAIL(statement, fmt, ...) do { \ |
|---|
| 125 | +#define SKIP(statement, fmt, ...) do { \ |
|---|
| 126 | + snprintf(_metadata->results->reason, \ |
|---|
| 127 | + sizeof(_metadata->results->reason), fmt, ##__VA_ARGS__); \ |
|---|
| 120 | 128 | if (TH_LOG_ENABLED) { \ |
|---|
| 121 | | - fprintf(TH_LOG_STREAM, "[ XFAIL! ] " fmt "\n", \ |
|---|
| 122 | | - ##__VA_ARGS__); \ |
|---|
| 129 | + fprintf(TH_LOG_STREAM, "# SKIP %s\n", \ |
|---|
| 130 | + _metadata->results->reason); \ |
|---|
| 123 | 131 | } \ |
|---|
| 124 | | - /* TODO: find a way to pass xfail to test runner process. */ \ |
|---|
| 125 | 132 | _metadata->passed = 1; \ |
|---|
| 133 | + _metadata->skip = 1; \ |
|---|
| 126 | 134 | _metadata->trigger = 0; \ |
|---|
| 127 | 135 | statement; \ |
|---|
| 128 | 136 | } while (0) |
|---|
| .. | .. |
|---|
| 167 | 175 | |
|---|
| 168 | 176 | #define __TEST_IMPL(test_name, _signal) \ |
|---|
| 169 | 177 | static void test_name(struct __test_metadata *_metadata); \ |
|---|
| 178 | + static inline void wrapper_##test_name( \ |
|---|
| 179 | + struct __test_metadata *_metadata, \ |
|---|
| 180 | + struct __fixture_variant_metadata *variant) \ |
|---|
| 181 | + { \ |
|---|
| 182 | + test_name(_metadata); \ |
|---|
| 183 | + } \ |
|---|
| 170 | 184 | static struct __test_metadata _##test_name##_object = \ |
|---|
| 171 | | - { name: "global." #test_name, \ |
|---|
| 172 | | - fn: &test_name, termsig: _signal }; \ |
|---|
| 185 | + { .name = #test_name, \ |
|---|
| 186 | + .fn = &wrapper_##test_name, \ |
|---|
| 187 | + .fixture = &_fixture_global, \ |
|---|
| 188 | + .termsig = _signal, \ |
|---|
| 189 | + .timeout = TEST_TIMEOUT_DEFAULT, }; \ |
|---|
| 173 | 190 | static void __attribute__((constructor)) _register_##test_name(void) \ |
|---|
| 174 | 191 | { \ |
|---|
| 175 | 192 | __register_test(&_##test_name##_object); \ |
|---|
| .. | .. |
|---|
| 185 | 202 | * |
|---|
| 186 | 203 | * .. code-block:: c |
|---|
| 187 | 204 | * |
|---|
| 188 | | - * FIXTURE_DATA(datatype name) |
|---|
| 205 | + * FIXTURE_DATA(datatype_name) |
|---|
| 189 | 206 | * |
|---|
| 207 | + * Almost always, you want just FIXTURE() instead (see below). |
|---|
| 190 | 208 | * This call may be used when the type of the fixture data |
|---|
| 191 | 209 | * is needed. In general, this should not be needed unless |
|---|
| 192 | 210 | * the *self* is being passed to a helper directly. |
|---|
| .. | .. |
|---|
| 201 | 219 | * |
|---|
| 202 | 220 | * .. code-block:: c |
|---|
| 203 | 221 | * |
|---|
| 204 | | - * FIXTURE(datatype name) { |
|---|
| 222 | + * FIXTURE(fixture_name) { |
|---|
| 205 | 223 | * type property1; |
|---|
| 206 | 224 | * ... |
|---|
| 207 | 225 | * }; |
|---|
| .. | .. |
|---|
| 210 | 228 | * populated and cleaned up using FIXTURE_SETUP() and FIXTURE_TEARDOWN(). |
|---|
| 211 | 229 | */ |
|---|
| 212 | 230 | #define FIXTURE(fixture_name) \ |
|---|
| 231 | + FIXTURE_VARIANT(fixture_name); \ |
|---|
| 232 | + static struct __fixture_metadata _##fixture_name##_fixture_object = \ |
|---|
| 233 | + { .name = #fixture_name, }; \ |
|---|
| 213 | 234 | static void __attribute__((constructor)) \ |
|---|
| 214 | 235 | _register_##fixture_name##_data(void) \ |
|---|
| 215 | 236 | { \ |
|---|
| 216 | | - __fixture_count++; \ |
|---|
| 237 | + __register_fixture(&_##fixture_name##_fixture_object); \ |
|---|
| 217 | 238 | } \ |
|---|
| 218 | 239 | FIXTURE_DATA(fixture_name) |
|---|
| 219 | 240 | |
|---|
| .. | .. |
|---|
| 225 | 246 | * |
|---|
| 226 | 247 | * .. code-block:: c |
|---|
| 227 | 248 | * |
|---|
| 228 | | - * FIXTURE_SETUP(fixture name) { implementation } |
|---|
| 249 | + * FIXTURE_SETUP(fixture_name) { implementation } |
|---|
| 229 | 250 | * |
|---|
| 230 | 251 | * Populates the required "setup" function for a fixture. An instance of the |
|---|
| 231 | 252 | * datatype defined with FIXTURE_DATA() will be exposed as *self* for the |
|---|
| .. | .. |
|---|
| 239 | 260 | #define FIXTURE_SETUP(fixture_name) \ |
|---|
| 240 | 261 | void fixture_name##_setup( \ |
|---|
| 241 | 262 | struct __test_metadata __attribute__((unused)) *_metadata, \ |
|---|
| 242 | | - FIXTURE_DATA(fixture_name) __attribute__((unused)) *self) |
|---|
| 263 | + FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \ |
|---|
| 264 | + const FIXTURE_VARIANT(fixture_name) \ |
|---|
| 265 | + __attribute__((unused)) *variant) |
|---|
| 266 | + |
|---|
| 243 | 267 | /** |
|---|
| 244 | 268 | * FIXTURE_TEARDOWN(fixture_name) |
|---|
| 245 | 269 | * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly. |
|---|
| .. | .. |
|---|
| 248 | 272 | * |
|---|
| 249 | 273 | * .. code-block:: c |
|---|
| 250 | 274 | * |
|---|
| 251 | | - * FIXTURE_TEARDOWN(fixture name) { implementation } |
|---|
| 275 | + * FIXTURE_TEARDOWN(fixture_name) { implementation } |
|---|
| 252 | 276 | * |
|---|
| 253 | 277 | * Populates the required "teardown" function for a fixture. An instance of the |
|---|
| 254 | 278 | * datatype defined with FIXTURE_DATA() will be exposed as *self* for the |
|---|
| .. | .. |
|---|
| 260 | 284 | void fixture_name##_teardown( \ |
|---|
| 261 | 285 | struct __test_metadata __attribute__((unused)) *_metadata, \ |
|---|
| 262 | 286 | FIXTURE_DATA(fixture_name) __attribute__((unused)) *self) |
|---|
| 287 | + |
|---|
| 288 | +/** |
|---|
| 289 | + * FIXTURE_VARIANT(fixture_name) - Optionally called once per fixture |
|---|
| 290 | + * to declare fixture variant |
|---|
| 291 | + * |
|---|
| 292 | + * @fixture_name: fixture name |
|---|
| 293 | + * |
|---|
| 294 | + * .. code-block:: c |
|---|
| 295 | + * |
|---|
| 296 | + * FIXTURE_VARIANT(fixture_name) { |
|---|
| 297 | + * type property1; |
|---|
| 298 | + * ... |
|---|
| 299 | + * }; |
|---|
| 300 | + * |
|---|
| 301 | + * Defines type of constant parameters provided to FIXTURE_SETUP() and TEST_F() |
|---|
| 302 | + * as *variant*. Variants allow the same tests to be run with different |
|---|
| 303 | + * arguments. |
|---|
| 304 | + */ |
|---|
| 305 | +#define FIXTURE_VARIANT(fixture_name) struct _fixture_variant_##fixture_name |
|---|
| 306 | + |
|---|
| 307 | +/** |
|---|
| 308 | + * FIXTURE_VARIANT_ADD(fixture_name, variant_name) - Called once per fixture |
|---|
| 309 | + * variant to setup and register the data |
|---|
| 310 | + * |
|---|
| 311 | + * @fixture_name: fixture name |
|---|
| 312 | + * @variant_name: name of the parameter set |
|---|
| 313 | + * |
|---|
| 314 | + * .. code-block:: c |
|---|
| 315 | + * |
|---|
| 316 | + * FIXTURE_VARIANT_ADD(fixture_name, variant_name) { |
|---|
| 317 | + * .property1 = val1, |
|---|
| 318 | + * ... |
|---|
| 319 | + * }; |
|---|
| 320 | + * |
|---|
| 321 | + * Defines a variant of the test fixture, provided to FIXTURE_SETUP() and |
|---|
| 322 | + * TEST_F() as *variant*. Tests of each fixture will be run once for each |
|---|
| 323 | + * variant. |
|---|
| 324 | + */ |
|---|
| 325 | +#define FIXTURE_VARIANT_ADD(fixture_name, variant_name) \ |
|---|
| 326 | + extern FIXTURE_VARIANT(fixture_name) \ |
|---|
| 327 | + _##fixture_name##_##variant_name##_variant; \ |
|---|
| 328 | + static struct __fixture_variant_metadata \ |
|---|
| 329 | + _##fixture_name##_##variant_name##_object = \ |
|---|
| 330 | + { .name = #variant_name, \ |
|---|
| 331 | + .data = &_##fixture_name##_##variant_name##_variant}; \ |
|---|
| 332 | + static void __attribute__((constructor)) \ |
|---|
| 333 | + _register_##fixture_name##_##variant_name(void) \ |
|---|
| 334 | + { \ |
|---|
| 335 | + __register_fixture_variant(&_##fixture_name##_fixture_object, \ |
|---|
| 336 | + &_##fixture_name##_##variant_name##_object); \ |
|---|
| 337 | + } \ |
|---|
| 338 | + FIXTURE_VARIANT(fixture_name) \ |
|---|
| 339 | + _##fixture_name##_##variant_name##_variant = |
|---|
| 263 | 340 | |
|---|
| 264 | 341 | /** |
|---|
| 265 | 342 | * TEST_F(fixture_name, test_name) - Emits test registration and helpers for |
|---|
| .. | .. |
|---|
| 280 | 357 | */ |
|---|
| 281 | 358 | /* TODO(wad) register fixtures on dedicated test lists. */ |
|---|
| 282 | 359 | #define TEST_F(fixture_name, test_name) \ |
|---|
| 283 | | - __TEST_F_IMPL(fixture_name, test_name, -1) |
|---|
| 360 | + __TEST_F_IMPL(fixture_name, test_name, -1, TEST_TIMEOUT_DEFAULT) |
|---|
| 284 | 361 | |
|---|
| 285 | 362 | #define TEST_F_SIGNAL(fixture_name, test_name, signal) \ |
|---|
| 286 | | - __TEST_F_IMPL(fixture_name, test_name, signal) |
|---|
| 363 | + __TEST_F_IMPL(fixture_name, test_name, signal, TEST_TIMEOUT_DEFAULT) |
|---|
| 287 | 364 | |
|---|
| 288 | | -#define __TEST_F_IMPL(fixture_name, test_name, signal) \ |
|---|
| 365 | +#define TEST_F_TIMEOUT(fixture_name, test_name, timeout) \ |
|---|
| 366 | + __TEST_F_IMPL(fixture_name, test_name, -1, timeout) |
|---|
| 367 | + |
|---|
| 368 | +#define __TEST_F_IMPL(fixture_name, test_name, signal, tmout) \ |
|---|
| 289 | 369 | static void fixture_name##_##test_name( \ |
|---|
| 290 | 370 | struct __test_metadata *_metadata, \ |
|---|
| 291 | | - FIXTURE_DATA(fixture_name) *self); \ |
|---|
| 371 | + FIXTURE_DATA(fixture_name) *self, \ |
|---|
| 372 | + const FIXTURE_VARIANT(fixture_name) *variant); \ |
|---|
| 292 | 373 | static inline void wrapper_##fixture_name##_##test_name( \ |
|---|
| 293 | | - struct __test_metadata *_metadata) \ |
|---|
| 374 | + struct __test_metadata *_metadata, \ |
|---|
| 375 | + struct __fixture_variant_metadata *variant) \ |
|---|
| 294 | 376 | { \ |
|---|
| 295 | 377 | /* fixture data is alloced, setup, and torn down per call. */ \ |
|---|
| 296 | 378 | FIXTURE_DATA(fixture_name) self; \ |
|---|
| 297 | 379 | memset(&self, 0, sizeof(FIXTURE_DATA(fixture_name))); \ |
|---|
| 298 | | - fixture_name##_setup(_metadata, &self); \ |
|---|
| 380 | + fixture_name##_setup(_metadata, &self, variant->data); \ |
|---|
| 299 | 381 | /* Let setup failure terminate early. */ \ |
|---|
| 300 | 382 | if (!_metadata->passed) \ |
|---|
| 301 | 383 | return; \ |
|---|
| 302 | | - fixture_name##_##test_name(_metadata, &self); \ |
|---|
| 384 | + fixture_name##_##test_name(_metadata, &self, variant->data); \ |
|---|
| 303 | 385 | fixture_name##_teardown(_metadata, &self); \ |
|---|
| 304 | 386 | } \ |
|---|
| 305 | 387 | static struct __test_metadata \ |
|---|
| 306 | 388 | _##fixture_name##_##test_name##_object = { \ |
|---|
| 307 | | - name: #fixture_name "." #test_name, \ |
|---|
| 308 | | - fn: &wrapper_##fixture_name##_##test_name, \ |
|---|
| 309 | | - termsig: signal, \ |
|---|
| 389 | + .name = #test_name, \ |
|---|
| 390 | + .fn = &wrapper_##fixture_name##_##test_name, \ |
|---|
| 391 | + .fixture = &_##fixture_name##_fixture_object, \ |
|---|
| 392 | + .termsig = signal, \ |
|---|
| 393 | + .timeout = tmout, \ |
|---|
| 310 | 394 | }; \ |
|---|
| 311 | 395 | static void __attribute__((constructor)) \ |
|---|
| 312 | 396 | _register_##fixture_name##_##test_name(void) \ |
|---|
| .. | .. |
|---|
| 315 | 399 | } \ |
|---|
| 316 | 400 | static void fixture_name##_##test_name( \ |
|---|
| 317 | 401 | struct __test_metadata __attribute__((unused)) *_metadata, \ |
|---|
| 318 | | - FIXTURE_DATA(fixture_name) __attribute__((unused)) *self) |
|---|
| 402 | + FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \ |
|---|
| 403 | + const FIXTURE_VARIANT(fixture_name) \ |
|---|
| 404 | + __attribute__((unused)) *variant) |
|---|
| 319 | 405 | |
|---|
| 320 | 406 | /** |
|---|
| 321 | 407 | * TEST_HARNESS_MAIN - Simple wrapper to run the test harness |
|---|
| .. | .. |
|---|
| 346 | 432 | */ |
|---|
| 347 | 433 | |
|---|
| 348 | 434 | /** |
|---|
| 349 | | - * ASSERT_EQ(expected, seen) |
|---|
| 435 | + * ASSERT_EQ() |
|---|
| 350 | 436 | * |
|---|
| 351 | 437 | * @expected: expected value |
|---|
| 352 | 438 | * @seen: measured value |
|---|
| .. | .. |
|---|
| 357 | 443 | __EXPECT(expected, #expected, seen, #seen, ==, 1) |
|---|
| 358 | 444 | |
|---|
| 359 | 445 | /** |
|---|
| 360 | | - * ASSERT_NE(expected, seen) |
|---|
| 446 | + * ASSERT_NE() |
|---|
| 361 | 447 | * |
|---|
| 362 | 448 | * @expected: expected value |
|---|
| 363 | 449 | * @seen: measured value |
|---|
| .. | .. |
|---|
| 368 | 454 | __EXPECT(expected, #expected, seen, #seen, !=, 1) |
|---|
| 369 | 455 | |
|---|
| 370 | 456 | /** |
|---|
| 371 | | - * ASSERT_LT(expected, seen) |
|---|
| 457 | + * ASSERT_LT() |
|---|
| 372 | 458 | * |
|---|
| 373 | 459 | * @expected: expected value |
|---|
| 374 | 460 | * @seen: measured value |
|---|
| .. | .. |
|---|
| 379 | 465 | __EXPECT(expected, #expected, seen, #seen, <, 1) |
|---|
| 380 | 466 | |
|---|
| 381 | 467 | /** |
|---|
| 382 | | - * ASSERT_LE(expected, seen) |
|---|
| 468 | + * ASSERT_LE() |
|---|
| 383 | 469 | * |
|---|
| 384 | 470 | * @expected: expected value |
|---|
| 385 | 471 | * @seen: measured value |
|---|
| .. | .. |
|---|
| 390 | 476 | __EXPECT(expected, #expected, seen, #seen, <=, 1) |
|---|
| 391 | 477 | |
|---|
| 392 | 478 | /** |
|---|
| 393 | | - * ASSERT_GT(expected, seen) |
|---|
| 479 | + * ASSERT_GT() |
|---|
| 394 | 480 | * |
|---|
| 395 | 481 | * @expected: expected value |
|---|
| 396 | 482 | * @seen: measured value |
|---|
| .. | .. |
|---|
| 401 | 487 | __EXPECT(expected, #expected, seen, #seen, >, 1) |
|---|
| 402 | 488 | |
|---|
| 403 | 489 | /** |
|---|
| 404 | | - * ASSERT_GE(expected, seen) |
|---|
| 490 | + * ASSERT_GE() |
|---|
| 405 | 491 | * |
|---|
| 406 | 492 | * @expected: expected value |
|---|
| 407 | 493 | * @seen: measured value |
|---|
| .. | .. |
|---|
| 412 | 498 | __EXPECT(expected, #expected, seen, #seen, >=, 1) |
|---|
| 413 | 499 | |
|---|
| 414 | 500 | /** |
|---|
| 415 | | - * ASSERT_NULL(seen) |
|---|
| 501 | + * ASSERT_NULL() |
|---|
| 416 | 502 | * |
|---|
| 417 | 503 | * @seen: measured value |
|---|
| 418 | 504 | * |
|---|
| .. | .. |
|---|
| 422 | 508 | __EXPECT(NULL, "NULL", seen, #seen, ==, 1) |
|---|
| 423 | 509 | |
|---|
| 424 | 510 | /** |
|---|
| 425 | | - * ASSERT_TRUE(seen) |
|---|
| 511 | + * ASSERT_TRUE() |
|---|
| 426 | 512 | * |
|---|
| 427 | 513 | * @seen: measured value |
|---|
| 428 | 514 | * |
|---|
| .. | .. |
|---|
| 432 | 518 | __EXPECT(0, "0", seen, #seen, !=, 1) |
|---|
| 433 | 519 | |
|---|
| 434 | 520 | /** |
|---|
| 435 | | - * ASSERT_FALSE(seen) |
|---|
| 521 | + * ASSERT_FALSE() |
|---|
| 436 | 522 | * |
|---|
| 437 | 523 | * @seen: measured value |
|---|
| 438 | 524 | * |
|---|
| .. | .. |
|---|
| 442 | 528 | __EXPECT(0, "0", seen, #seen, ==, 1) |
|---|
| 443 | 529 | |
|---|
| 444 | 530 | /** |
|---|
| 445 | | - * ASSERT_STREQ(expected, seen) |
|---|
| 531 | + * ASSERT_STREQ() |
|---|
| 446 | 532 | * |
|---|
| 447 | 533 | * @expected: expected value |
|---|
| 448 | 534 | * @seen: measured value |
|---|
| .. | .. |
|---|
| 453 | 539 | __EXPECT_STR(expected, seen, ==, 1) |
|---|
| 454 | 540 | |
|---|
| 455 | 541 | /** |
|---|
| 456 | | - * ASSERT_STRNE(expected, seen) |
|---|
| 542 | + * ASSERT_STRNE() |
|---|
| 457 | 543 | * |
|---|
| 458 | 544 | * @expected: expected value |
|---|
| 459 | 545 | * @seen: measured value |
|---|
| .. | .. |
|---|
| 464 | 550 | __EXPECT_STR(expected, seen, !=, 1) |
|---|
| 465 | 551 | |
|---|
| 466 | 552 | /** |
|---|
| 467 | | - * EXPECT_EQ(expected, seen) |
|---|
| 553 | + * EXPECT_EQ() |
|---|
| 468 | 554 | * |
|---|
| 469 | 555 | * @expected: expected value |
|---|
| 470 | 556 | * @seen: measured value |
|---|
| .. | .. |
|---|
| 475 | 561 | __EXPECT(expected, #expected, seen, #seen, ==, 0) |
|---|
| 476 | 562 | |
|---|
| 477 | 563 | /** |
|---|
| 478 | | - * EXPECT_NE(expected, seen) |
|---|
| 564 | + * EXPECT_NE() |
|---|
| 479 | 565 | * |
|---|
| 480 | 566 | * @expected: expected value |
|---|
| 481 | 567 | * @seen: measured value |
|---|
| .. | .. |
|---|
| 486 | 572 | __EXPECT(expected, #expected, seen, #seen, !=, 0) |
|---|
| 487 | 573 | |
|---|
| 488 | 574 | /** |
|---|
| 489 | | - * EXPECT_LT(expected, seen) |
|---|
| 575 | + * EXPECT_LT() |
|---|
| 490 | 576 | * |
|---|
| 491 | 577 | * @expected: expected value |
|---|
| 492 | 578 | * @seen: measured value |
|---|
| .. | .. |
|---|
| 497 | 583 | __EXPECT(expected, #expected, seen, #seen, <, 0) |
|---|
| 498 | 584 | |
|---|
| 499 | 585 | /** |
|---|
| 500 | | - * EXPECT_LE(expected, seen) |
|---|
| 586 | + * EXPECT_LE() |
|---|
| 501 | 587 | * |
|---|
| 502 | 588 | * @expected: expected value |
|---|
| 503 | 589 | * @seen: measured value |
|---|
| .. | .. |
|---|
| 508 | 594 | __EXPECT(expected, #expected, seen, #seen, <=, 0) |
|---|
| 509 | 595 | |
|---|
| 510 | 596 | /** |
|---|
| 511 | | - * EXPECT_GT(expected, seen) |
|---|
| 597 | + * EXPECT_GT() |
|---|
| 512 | 598 | * |
|---|
| 513 | 599 | * @expected: expected value |
|---|
| 514 | 600 | * @seen: measured value |
|---|
| .. | .. |
|---|
| 519 | 605 | __EXPECT(expected, #expected, seen, #seen, >, 0) |
|---|
| 520 | 606 | |
|---|
| 521 | 607 | /** |
|---|
| 522 | | - * EXPECT_GE(expected, seen) |
|---|
| 608 | + * EXPECT_GE() |
|---|
| 523 | 609 | * |
|---|
| 524 | 610 | * @expected: expected value |
|---|
| 525 | 611 | * @seen: measured value |
|---|
| .. | .. |
|---|
| 530 | 616 | __EXPECT(expected, #expected, seen, #seen, >=, 0) |
|---|
| 531 | 617 | |
|---|
| 532 | 618 | /** |
|---|
| 533 | | - * EXPECT_NULL(seen) |
|---|
| 619 | + * EXPECT_NULL() |
|---|
| 534 | 620 | * |
|---|
| 535 | 621 | * @seen: measured value |
|---|
| 536 | 622 | * |
|---|
| .. | .. |
|---|
| 540 | 626 | __EXPECT(NULL, "NULL", seen, #seen, ==, 0) |
|---|
| 541 | 627 | |
|---|
| 542 | 628 | /** |
|---|
| 543 | | - * EXPECT_TRUE(seen) |
|---|
| 629 | + * EXPECT_TRUE() |
|---|
| 544 | 630 | * |
|---|
| 545 | 631 | * @seen: measured value |
|---|
| 546 | 632 | * |
|---|
| .. | .. |
|---|
| 550 | 636 | __EXPECT(0, "0", seen, #seen, !=, 0) |
|---|
| 551 | 637 | |
|---|
| 552 | 638 | /** |
|---|
| 553 | | - * EXPECT_FALSE(seen) |
|---|
| 639 | + * EXPECT_FALSE() |
|---|
| 554 | 640 | * |
|---|
| 555 | 641 | * @seen: measured value |
|---|
| 556 | 642 | * |
|---|
| .. | .. |
|---|
| 560 | 646 | __EXPECT(0, "0", seen, #seen, ==, 0) |
|---|
| 561 | 647 | |
|---|
| 562 | 648 | /** |
|---|
| 563 | | - * EXPECT_STREQ(expected, seen) |
|---|
| 649 | + * EXPECT_STREQ() |
|---|
| 564 | 650 | * |
|---|
| 565 | 651 | * @expected: expected value |
|---|
| 566 | 652 | * @seen: measured value |
|---|
| .. | .. |
|---|
| 571 | 657 | __EXPECT_STR(expected, seen, ==, 0) |
|---|
| 572 | 658 | |
|---|
| 573 | 659 | /** |
|---|
| 574 | | - * EXPECT_STRNE(expected, seen) |
|---|
| 660 | + * EXPECT_STRNE() |
|---|
| 575 | 661 | * |
|---|
| 576 | 662 | * @expected: expected value |
|---|
| 577 | 663 | * @seen: measured value |
|---|
| .. | .. |
|---|
| 594 | 680 | __bail(_assert, _metadata->no_print, _metadata->step)) |
|---|
| 595 | 681 | |
|---|
| 596 | 682 | #define __INC_STEP(_metadata) \ |
|---|
| 597 | | - if (_metadata->passed && _metadata->step < 255) \ |
|---|
| 683 | + /* Keep "step" below 255 (which is used for "SKIP" reporting). */ \ |
|---|
| 684 | + if (_metadata->passed && _metadata->step < 253) \ |
|---|
| 598 | 685 | _metadata->step++; |
|---|
| 686 | + |
|---|
| 687 | +#define is_signed_type(var) (!!(((__typeof__(var))(-1)) < (__typeof__(var))1)) |
|---|
| 599 | 688 | |
|---|
| 600 | 689 | #define __EXPECT(_expected, _expected_str, _seen, _seen_str, _t, _assert) do { \ |
|---|
| 601 | 690 | /* Avoid multiple evaluation of the cases */ \ |
|---|
| .. | .. |
|---|
| 603 | 692 | __typeof__(_seen) __seen = (_seen); \ |
|---|
| 604 | 693 | if (_assert) __INC_STEP(_metadata); \ |
|---|
| 605 | 694 | if (!(__exp _t __seen)) { \ |
|---|
| 606 | | - unsigned long long __exp_print = (uintptr_t)__exp; \ |
|---|
| 607 | | - unsigned long long __seen_print = (uintptr_t)__seen; \ |
|---|
| 608 | | - __TH_LOG("Expected %s (%llu) %s %s (%llu)", \ |
|---|
| 609 | | - _expected_str, __exp_print, #_t, \ |
|---|
| 610 | | - _seen_str, __seen_print); \ |
|---|
| 695 | + /* Report with actual signedness to avoid weird output. */ \ |
|---|
| 696 | + switch (is_signed_type(__exp) * 2 + is_signed_type(__seen)) { \ |
|---|
| 697 | + case 0: { \ |
|---|
| 698 | + unsigned long long __exp_print = (uintptr_t)__exp; \ |
|---|
| 699 | + unsigned long long __seen_print = (uintptr_t)__seen; \ |
|---|
| 700 | + __TH_LOG("Expected %s (%llu) %s %s (%llu)", \ |
|---|
| 701 | + _expected_str, __exp_print, #_t, \ |
|---|
| 702 | + _seen_str, __seen_print); \ |
|---|
| 703 | + break; \ |
|---|
| 704 | + } \ |
|---|
| 705 | + case 1: { \ |
|---|
| 706 | + unsigned long long __exp_print = (uintptr_t)__exp; \ |
|---|
| 707 | + long long __seen_print = (intptr_t)__seen; \ |
|---|
| 708 | + __TH_LOG("Expected %s (%llu) %s %s (%lld)", \ |
|---|
| 709 | + _expected_str, __exp_print, #_t, \ |
|---|
| 710 | + _seen_str, __seen_print); \ |
|---|
| 711 | + break; \ |
|---|
| 712 | + } \ |
|---|
| 713 | + case 2: { \ |
|---|
| 714 | + long long __exp_print = (intptr_t)__exp; \ |
|---|
| 715 | + unsigned long long __seen_print = (uintptr_t)__seen; \ |
|---|
| 716 | + __TH_LOG("Expected %s (%lld) %s %s (%llu)", \ |
|---|
| 717 | + _expected_str, __exp_print, #_t, \ |
|---|
| 718 | + _seen_str, __seen_print); \ |
|---|
| 719 | + break; \ |
|---|
| 720 | + } \ |
|---|
| 721 | + case 3: { \ |
|---|
| 722 | + long long __exp_print = (intptr_t)__exp; \ |
|---|
| 723 | + long long __seen_print = (intptr_t)__seen; \ |
|---|
| 724 | + __TH_LOG("Expected %s (%lld) %s %s (%lld)", \ |
|---|
| 725 | + _expected_str, __exp_print, #_t, \ |
|---|
| 726 | + _seen_str, __seen_print); \ |
|---|
| 727 | + break; \ |
|---|
| 728 | + } \ |
|---|
| 729 | + } \ |
|---|
| 611 | 730 | _metadata->passed = 0; \ |
|---|
| 612 | 731 | /* Ensure the optional handler is triggered */ \ |
|---|
| 613 | 732 | _metadata->trigger = 1; \ |
|---|
| .. | .. |
|---|
| 625 | 744 | } \ |
|---|
| 626 | 745 | } while (0); OPTIONAL_HANDLER(_assert) |
|---|
| 627 | 746 | |
|---|
| 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 */ |
|---|
| 635 | | - __u8 step; |
|---|
| 636 | | - bool no_print; /* manual trigger when TH_LOG_STREAM is not available */ |
|---|
| 637 | | - struct __test_metadata *prev, *next; |
|---|
| 747 | +/* List helpers */ |
|---|
| 748 | +#define __LIST_APPEND(head, item) \ |
|---|
| 749 | +{ \ |
|---|
| 750 | + /* Circular linked list where only prev is circular. */ \ |
|---|
| 751 | + if (head == NULL) { \ |
|---|
| 752 | + head = item; \ |
|---|
| 753 | + item->next = NULL; \ |
|---|
| 754 | + item->prev = item; \ |
|---|
| 755 | + return; \ |
|---|
| 756 | + } \ |
|---|
| 757 | + if (__constructor_order == _CONSTRUCTOR_ORDER_FORWARD) { \ |
|---|
| 758 | + item->next = NULL; \ |
|---|
| 759 | + item->prev = head->prev; \ |
|---|
| 760 | + item->prev->next = item; \ |
|---|
| 761 | + head->prev = item; \ |
|---|
| 762 | + } else { \ |
|---|
| 763 | + item->next = head; \ |
|---|
| 764 | + item->next->prev = item; \ |
|---|
| 765 | + item->prev = item; \ |
|---|
| 766 | + head = item; \ |
|---|
| 767 | + } \ |
|---|
| 768 | +} |
|---|
| 769 | + |
|---|
| 770 | +struct __test_results { |
|---|
| 771 | + char reason[1024]; /* Reason for test result */ |
|---|
| 638 | 772 | }; |
|---|
| 639 | 773 | |
|---|
| 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; |
|---|
| 774 | +struct __test_metadata; |
|---|
| 775 | +struct __fixture_variant_metadata; |
|---|
| 776 | + |
|---|
| 777 | +/* Contains all the information about a fixture. */ |
|---|
| 778 | +struct __fixture_metadata { |
|---|
| 779 | + const char *name; |
|---|
| 780 | + struct __test_metadata *tests; |
|---|
| 781 | + struct __fixture_variant_metadata *variant; |
|---|
| 782 | + struct __fixture_metadata *prev, *next; |
|---|
| 783 | +} _fixture_global __attribute__((unused)) = { |
|---|
| 784 | + .name = "global", |
|---|
| 785 | + .prev = &_fixture_global, |
|---|
| 786 | +}; |
|---|
| 787 | + |
|---|
| 788 | +static struct __fixture_metadata *__fixture_list = &_fixture_global; |
|---|
| 644 | 789 | static int __constructor_order; |
|---|
| 645 | 790 | |
|---|
| 646 | 791 | #define _CONSTRUCTOR_ORDER_FORWARD 1 |
|---|
| 647 | 792 | #define _CONSTRUCTOR_ORDER_BACKWARD -1 |
|---|
| 793 | + |
|---|
| 794 | +static inline void __register_fixture(struct __fixture_metadata *f) |
|---|
| 795 | +{ |
|---|
| 796 | + __LIST_APPEND(__fixture_list, f); |
|---|
| 797 | +} |
|---|
| 798 | + |
|---|
| 799 | +struct __fixture_variant_metadata { |
|---|
| 800 | + const char *name; |
|---|
| 801 | + const void *data; |
|---|
| 802 | + struct __fixture_variant_metadata *prev, *next; |
|---|
| 803 | +}; |
|---|
| 804 | + |
|---|
| 805 | +static inline void |
|---|
| 806 | +__register_fixture_variant(struct __fixture_metadata *f, |
|---|
| 807 | + struct __fixture_variant_metadata *variant) |
|---|
| 808 | +{ |
|---|
| 809 | + __LIST_APPEND(f->variant, variant); |
|---|
| 810 | +} |
|---|
| 811 | + |
|---|
| 812 | +/* Contains all the information for test execution and status checking. */ |
|---|
| 813 | +struct __test_metadata { |
|---|
| 814 | + const char *name; |
|---|
| 815 | + void (*fn)(struct __test_metadata *, |
|---|
| 816 | + struct __fixture_variant_metadata *); |
|---|
| 817 | + pid_t pid; /* pid of test when being run */ |
|---|
| 818 | + struct __fixture_metadata *fixture; |
|---|
| 819 | + int termsig; |
|---|
| 820 | + int passed; |
|---|
| 821 | + int skip; /* did SKIP get used? */ |
|---|
| 822 | + int trigger; /* extra handler after the evaluation */ |
|---|
| 823 | + int timeout; /* seconds to wait for test timeout */ |
|---|
| 824 | + bool timed_out; /* did this test timeout instead of exiting? */ |
|---|
| 825 | + __u8 step; |
|---|
| 826 | + bool no_print; /* manual trigger when TH_LOG_STREAM is not available */ |
|---|
| 827 | + struct __test_results *results; |
|---|
| 828 | + struct __test_metadata *prev, *next; |
|---|
| 829 | +}; |
|---|
| 648 | 830 | |
|---|
| 649 | 831 | /* |
|---|
| 650 | 832 | * Since constructors are called in reverse order, reverse the test |
|---|
| .. | .. |
|---|
| 657 | 839 | */ |
|---|
| 658 | 840 | static inline void __register_test(struct __test_metadata *t) |
|---|
| 659 | 841 | { |
|---|
| 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 | | - } |
|---|
| 842 | + __LIST_APPEND(t->fixture->tests, t); |
|---|
| 679 | 843 | } |
|---|
| 680 | 844 | |
|---|
| 681 | 845 | static inline int __bail(int for_realz, bool no_print, __u8 step) |
|---|
| .. | .. |
|---|
| 688 | 852 | return 0; |
|---|
| 689 | 853 | } |
|---|
| 690 | 854 | |
|---|
| 691 | | -void __run_test(struct __test_metadata *t) |
|---|
| 855 | +struct __test_metadata *__active_test; |
|---|
| 856 | +static void __timeout_handler(int sig, siginfo_t *info, void *ucontext) |
|---|
| 692 | 857 | { |
|---|
| 693 | | - pid_t child_pid; |
|---|
| 858 | + struct __test_metadata *t = __active_test; |
|---|
| 859 | + |
|---|
| 860 | + /* Sanity check handler execution environment. */ |
|---|
| 861 | + if (!t) { |
|---|
| 862 | + fprintf(TH_LOG_STREAM, |
|---|
| 863 | + "# no active test in SIGALRM handler!?\n"); |
|---|
| 864 | + abort(); |
|---|
| 865 | + } |
|---|
| 866 | + if (sig != SIGALRM || sig != info->si_signo) { |
|---|
| 867 | + fprintf(TH_LOG_STREAM, |
|---|
| 868 | + "# %s: SIGALRM handler caught signal %d!?\n", |
|---|
| 869 | + t->name, sig != SIGALRM ? sig : info->si_signo); |
|---|
| 870 | + abort(); |
|---|
| 871 | + } |
|---|
| 872 | + |
|---|
| 873 | + t->timed_out = true; |
|---|
| 874 | + // signal process group |
|---|
| 875 | + kill(-(t->pid), SIGKILL); |
|---|
| 876 | +} |
|---|
| 877 | + |
|---|
| 878 | +void __wait_for_test(struct __test_metadata *t) |
|---|
| 879 | +{ |
|---|
| 880 | + struct sigaction action = { |
|---|
| 881 | + .sa_sigaction = __timeout_handler, |
|---|
| 882 | + .sa_flags = SA_SIGINFO, |
|---|
| 883 | + }; |
|---|
| 884 | + struct sigaction saved_action; |
|---|
| 694 | 885 | int status; |
|---|
| 695 | 886 | |
|---|
| 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"); |
|---|
| 887 | + if (sigaction(SIGALRM, &action, &saved_action)) { |
|---|
| 702 | 888 | t->passed = 0; |
|---|
| 703 | | - } else if (child_pid == 0) { |
|---|
| 704 | | - t->fn(t); |
|---|
| 705 | | - /* return the step that failed or 0 */ |
|---|
| 706 | | - _exit(t->passed ? 0 : t->step); |
|---|
| 707 | | - } else { |
|---|
| 708 | | - /* TODO(wad) add timeout support. */ |
|---|
| 709 | | - waitpid(child_pid, &status, 0); |
|---|
| 710 | | - if (WIFEXITED(status)) { |
|---|
| 711 | | - t->passed = t->termsig == -1 ? !WEXITSTATUS(status) : 0; |
|---|
| 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)); |
|---|
| 718 | | - } else if (!t->passed) { |
|---|
| 719 | | - fprintf(TH_LOG_STREAM, |
|---|
| 720 | | - "%s: Test failed at step #%d\n", |
|---|
| 721 | | - t->name, |
|---|
| 722 | | - WEXITSTATUS(status)); |
|---|
| 723 | | - } |
|---|
| 724 | | - } else if (WIFSIGNALED(status)) { |
|---|
| 889 | + fprintf(TH_LOG_STREAM, |
|---|
| 890 | + "# %s: unable to install SIGALRM handler\n", |
|---|
| 891 | + t->name); |
|---|
| 892 | + return; |
|---|
| 893 | + } |
|---|
| 894 | + __active_test = t; |
|---|
| 895 | + t->timed_out = false; |
|---|
| 896 | + alarm(t->timeout); |
|---|
| 897 | + waitpid(t->pid, &status, 0); |
|---|
| 898 | + alarm(0); |
|---|
| 899 | + if (sigaction(SIGALRM, &saved_action, NULL)) { |
|---|
| 900 | + t->passed = 0; |
|---|
| 901 | + fprintf(TH_LOG_STREAM, |
|---|
| 902 | + "# %s: unable to uninstall SIGALRM handler\n", |
|---|
| 903 | + t->name); |
|---|
| 904 | + return; |
|---|
| 905 | + } |
|---|
| 906 | + __active_test = NULL; |
|---|
| 907 | + |
|---|
| 908 | + if (t->timed_out) { |
|---|
| 909 | + t->passed = 0; |
|---|
| 910 | + fprintf(TH_LOG_STREAM, |
|---|
| 911 | + "# %s: Test terminated by timeout\n", t->name); |
|---|
| 912 | + } else if (WIFEXITED(status)) { |
|---|
| 913 | + if (t->termsig != -1) { |
|---|
| 725 | 914 | 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) { |
|---|
| 915 | + fprintf(TH_LOG_STREAM, |
|---|
| 916 | + "# %s: Test exited normally instead of by signal (code: %d)\n", |
|---|
| 917 | + t->name, |
|---|
| 918 | + WEXITSTATUS(status)); |
|---|
| 919 | + } else { |
|---|
| 920 | + switch (WEXITSTATUS(status)) { |
|---|
| 921 | + /* Success */ |
|---|
| 922 | + case 0: |
|---|
| 731 | 923 | t->passed = 1; |
|---|
| 732 | | - } else { |
|---|
| 924 | + break; |
|---|
| 925 | + /* SKIP */ |
|---|
| 926 | + case 255: |
|---|
| 927 | + t->passed = 1; |
|---|
| 928 | + t->skip = 1; |
|---|
| 929 | + break; |
|---|
| 930 | + /* Other failure, assume step report. */ |
|---|
| 931 | + default: |
|---|
| 932 | + t->passed = 0; |
|---|
| 733 | 933 | fprintf(TH_LOG_STREAM, |
|---|
| 734 | | - "%s: Test terminated unexpectedly " |
|---|
| 735 | | - "by signal %d\n", |
|---|
| 934 | + "# %s: Test failed at step #%d\n", |
|---|
| 736 | 935 | t->name, |
|---|
| 737 | | - WTERMSIG(status)); |
|---|
| 936 | + WEXITSTATUS(status)); |
|---|
| 738 | 937 | } |
|---|
| 938 | + } |
|---|
| 939 | + } else if (WIFSIGNALED(status)) { |
|---|
| 940 | + t->passed = 0; |
|---|
| 941 | + if (WTERMSIG(status) == SIGABRT) { |
|---|
| 942 | + fprintf(TH_LOG_STREAM, |
|---|
| 943 | + "# %s: Test terminated by assertion\n", |
|---|
| 944 | + t->name); |
|---|
| 945 | + } else if (WTERMSIG(status) == t->termsig) { |
|---|
| 946 | + t->passed = 1; |
|---|
| 739 | 947 | } else { |
|---|
| 740 | 948 | fprintf(TH_LOG_STREAM, |
|---|
| 741 | | - "%s: Test ended in some other way [%u]\n", |
|---|
| 949 | + "# %s: Test terminated unexpectedly by signal %d\n", |
|---|
| 742 | 950 | t->name, |
|---|
| 743 | | - status); |
|---|
| 951 | + WTERMSIG(status)); |
|---|
| 744 | 952 | } |
|---|
| 953 | + } else { |
|---|
| 954 | + fprintf(TH_LOG_STREAM, |
|---|
| 955 | + "# %s: Test ended in some other way [%u]\n", |
|---|
| 956 | + t->name, |
|---|
| 957 | + status); |
|---|
| 745 | 958 | } |
|---|
| 746 | | - printf("[ %4s ] %s\n", (t->passed ? "OK" : "FAIL"), t->name); |
|---|
| 959 | +} |
|---|
| 960 | + |
|---|
| 961 | +void __run_test(struct __fixture_metadata *f, |
|---|
| 962 | + struct __fixture_variant_metadata *variant, |
|---|
| 963 | + struct __test_metadata *t) |
|---|
| 964 | +{ |
|---|
| 965 | + /* reset test struct */ |
|---|
| 966 | + t->passed = 1; |
|---|
| 967 | + t->skip = 0; |
|---|
| 968 | + t->trigger = 0; |
|---|
| 969 | + t->step = 1; |
|---|
| 970 | + t->no_print = 0; |
|---|
| 971 | + memset(t->results->reason, 0, sizeof(t->results->reason)); |
|---|
| 972 | + |
|---|
| 973 | + ksft_print_msg(" RUN %s%s%s.%s ...\n", |
|---|
| 974 | + f->name, variant->name[0] ? "." : "", variant->name, t->name); |
|---|
| 975 | + |
|---|
| 976 | + /* Make sure output buffers are flushed before fork */ |
|---|
| 977 | + fflush(stdout); |
|---|
| 978 | + fflush(stderr); |
|---|
| 979 | + |
|---|
| 980 | + t->pid = fork(); |
|---|
| 981 | + if (t->pid < 0) { |
|---|
| 982 | + ksft_print_msg("ERROR SPAWNING TEST CHILD\n"); |
|---|
| 983 | + t->passed = 0; |
|---|
| 984 | + } else if (t->pid == 0) { |
|---|
| 985 | + setpgrp(); |
|---|
| 986 | + t->fn(t, variant); |
|---|
| 987 | + if (t->skip) |
|---|
| 988 | + _exit(255); |
|---|
| 989 | + /* Pass is exit 0 */ |
|---|
| 990 | + if (t->passed) |
|---|
| 991 | + _exit(0); |
|---|
| 992 | + /* Something else happened, report the step. */ |
|---|
| 993 | + _exit(t->step); |
|---|
| 994 | + } else { |
|---|
| 995 | + __wait_for_test(t); |
|---|
| 996 | + } |
|---|
| 997 | + ksft_print_msg(" %4s %s%s%s.%s\n", t->passed ? "OK" : "FAIL", |
|---|
| 998 | + f->name, variant->name[0] ? "." : "", variant->name, t->name); |
|---|
| 999 | + |
|---|
| 1000 | + if (t->skip) |
|---|
| 1001 | + ksft_test_result_skip("%s\n", t->results->reason[0] ? |
|---|
| 1002 | + t->results->reason : "unknown"); |
|---|
| 1003 | + else |
|---|
| 1004 | + ksft_test_result(t->passed, "%s%s%s.%s\n", |
|---|
| 1005 | + f->name, variant->name[0] ? "." : "", variant->name, t->name); |
|---|
| 747 | 1006 | } |
|---|
| 748 | 1007 | |
|---|
| 749 | 1008 | static int test_harness_run(int __attribute__((unused)) argc, |
|---|
| 750 | 1009 | char __attribute__((unused)) **argv) |
|---|
| 751 | 1010 | { |
|---|
| 1011 | + struct __fixture_variant_metadata no_variant = { .name = "", }; |
|---|
| 1012 | + struct __fixture_variant_metadata *v; |
|---|
| 1013 | + struct __fixture_metadata *f; |
|---|
| 1014 | + struct __test_results *results; |
|---|
| 752 | 1015 | struct __test_metadata *t; |
|---|
| 753 | 1016 | int ret = 0; |
|---|
| 1017 | + unsigned int case_count = 0, test_count = 0; |
|---|
| 754 | 1018 | unsigned int count = 0; |
|---|
| 755 | 1019 | unsigned int pass_count = 0; |
|---|
| 756 | 1020 | |
|---|
| 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; |
|---|
| 1021 | + for (f = __fixture_list; f; f = f->next) { |
|---|
| 1022 | + for (v = f->variant ?: &no_variant; v; v = v->next) { |
|---|
| 1023 | + case_count++; |
|---|
| 1024 | + for (t = f->tests; t; t = t->next) |
|---|
| 1025 | + test_count++; |
|---|
| 1026 | + } |
|---|
| 767 | 1027 | } |
|---|
| 768 | | - printf("[==========] %u / %u tests passed.\n", pass_count, count); |
|---|
| 769 | | - printf("[ %s ]\n", (ret ? "FAILED" : "PASSED")); |
|---|
| 770 | | - return ret; |
|---|
| 1028 | + |
|---|
| 1029 | + results = mmap(NULL, sizeof(*results), PROT_READ | PROT_WRITE, |
|---|
| 1030 | + MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
|---|
| 1031 | + |
|---|
| 1032 | + ksft_print_header(); |
|---|
| 1033 | + ksft_set_plan(test_count); |
|---|
| 1034 | + ksft_print_msg("Starting %u tests from %u test cases.\n", |
|---|
| 1035 | + test_count, case_count); |
|---|
| 1036 | + for (f = __fixture_list; f; f = f->next) { |
|---|
| 1037 | + for (v = f->variant ?: &no_variant; v; v = v->next) { |
|---|
| 1038 | + for (t = f->tests; t; t = t->next) { |
|---|
| 1039 | + count++; |
|---|
| 1040 | + t->results = results; |
|---|
| 1041 | + __run_test(f, v, t); |
|---|
| 1042 | + t->results = NULL; |
|---|
| 1043 | + if (t->passed) |
|---|
| 1044 | + pass_count++; |
|---|
| 1045 | + else |
|---|
| 1046 | + ret = 1; |
|---|
| 1047 | + } |
|---|
| 1048 | + } |
|---|
| 1049 | + } |
|---|
| 1050 | + munmap(results, sizeof(*results)); |
|---|
| 1051 | + |
|---|
| 1052 | + ksft_print_msg("%s: %u / %u tests passed.\n", ret ? "FAILED" : "PASSED", |
|---|
| 1053 | + pass_count, count); |
|---|
| 1054 | + ksft_exit(ret == 0); |
|---|
| 1055 | + |
|---|
| 1056 | + /* unreachable */ |
|---|
| 1057 | + return KSFT_FAIL; |
|---|
| 771 | 1058 | } |
|---|
| 772 | 1059 | |
|---|
| 773 | 1060 | static void __attribute__((constructor)) __constructor_order_first(void) |
|---|