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