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 * 5 * test_harness.h: simple C unit test helper. 6 * 7 * Usage: 8 * #include "test_harness.h" 9 * TEST(standalone_test) { 10 * do_some_stuff; 11 * EXPECT_GT(10, stuff) { 12 * stuff_state_t state; 13 * enumerate_stuff_state(&state); 14 * TH_LOG("expectation failed with state: %s", state.msg); 15 * } 16 * more_stuff; 17 * ASSERT_NE(some_stuff, NULL) TH_LOG("how did it happen?!"); 18 * last_stuff; 19 * EXPECT_EQ(0, last_stuff); 20 * } 21 * 22 * FIXTURE(my_fixture) { 23 * mytype_t *data; 24 * int awesomeness_level; 25 * }; 26 * FIXTURE_SETUP(my_fixture) { 27 * self->data = mytype_new(); 28 * ASSERT_NE(NULL, self->data); 29 * } 30 * FIXTURE_TEARDOWN(my_fixture) { 31 * mytype_free(self->data); 32 * } 33 * TEST_F(my_fixture, data_is_good) { 34 * EXPECT_EQ(1, is_my_data_good(self->data)); 35 * } 36 * 37 * TEST_HARNESS_MAIN 38 * 39 * API inspired by code.google.com/p/googletest 40 */ 41 #ifndef TEST_HARNESS_H_ 42 #define TEST_HARNESS_H_ 43 44 #define _GNU_SOURCE 45 #include <stdint.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <sys/types.h> 50 #include <sys/wait.h> 51 #include <unistd.h> 52 53 /* All exported functionality should be declared through this macro. */ 54 #define TEST_API(x) _##x 55 56 /* 57 * Exported APIs 58 */ 59 60 /* TEST(name) { implementation } 61 * Defines a test by name. 62 * Names must be unique and tests must not be run in parallel. The 63 * implementation containing block is a function and scoping should be treated 64 * as such. Returning early may be performed with a bare "return;" statement. 65 * 66 * EXPECT_* and ASSERT_* are valid in a TEST() { } context. 67 */ 68 #define TEST TEST_API(TEST) 69 70 /* TEST_SIGNAL(name, signal) { implementation } 71 * Defines a test by name and the expected term signal. 72 * Names must be unique and tests must not be run in parallel. The 73 * implementation containing block is a function and scoping should be treated 74 * as such. Returning early may be performed with a bare "return;" statement. 75 * 76 * EXPECT_* and ASSERT_* are valid in a TEST() { } context. 77 */ 78 #define TEST_SIGNAL TEST_API(TEST_SIGNAL) 79 80 /* FIXTURE(datatype name) { 81 * type property1; 82 * ... 83 * }; 84 * Defines the data provided to TEST_F()-defined tests as |self|. It should be 85 * populated and cleaned up using FIXTURE_SETUP and FIXTURE_TEARDOWN. 86 */ 87 #define FIXTURE TEST_API(FIXTURE) 88 89 /* FIXTURE_DATA(datatype name) 90 * This call may be used when the type of the fixture data 91 * is needed. In general, this should not be needed unless 92 * the |self| is being passed to a helper directly. 93 */ 94 #define FIXTURE_DATA TEST_API(FIXTURE_DATA) 95 96 /* FIXTURE_SETUP(fixture name) { implementation } 97 * Populates the required "setup" function for a fixture. An instance of the 98 * datatype defined with _FIXTURE_DATA will be exposed as |self| for the 99 * implementation. 100 * 101 * ASSERT_* are valid for use in this context and will prempt the execution 102 * of any dependent fixture tests. 103 * 104 * A bare "return;" statement may be used to return early. 105 */ 106 #define FIXTURE_SETUP TEST_API(FIXTURE_SETUP) 107 108 /* FIXTURE_TEARDOWN(fixture name) { implementation } 109 * Populates the required "teardown" function for a fixture. An instance of the 110 * datatype defined with _FIXTURE_DATA will be exposed as |self| for the 111 * implementation to clean up. 112 * 113 * A bare "return;" statement may be used to return early. 114 */ 115 #define FIXTURE_TEARDOWN TEST_API(FIXTURE_TEARDOWN) 116 117 /* TEST_F(fixture, name) { implementation } 118 * Defines a test that depends on a fixture (e.g., is part of a test case). 119 * Very similar to TEST() except that |self| is the setup instance of fixture's 120 * datatype exposed for use by the implementation. 121 */ 122 #define TEST_F TEST_API(TEST_F) 123 124 #define TEST_F_SIGNAL TEST_API(TEST_F_SIGNAL) 125 126 /* Use once to append a main() to the test file. E.g., 127 * TEST_HARNESS_MAIN 128 */ 129 #define TEST_HARNESS_MAIN TEST_API(TEST_HARNESS_MAIN) 130 131 /* 132 * Operators for use in TEST and TEST_F. 133 * ASSERT_* calls will stop test execution immediately. 134 * EXPECT_* calls will emit a failure warning, note it, and continue. 135 */ 136 137 /* ASSERT_EQ(expected, measured): expected == measured */ 138 #define ASSERT_EQ TEST_API(ASSERT_EQ) 139 /* ASSERT_NE(expected, measured): expected != measured */ 140 #define ASSERT_NE TEST_API(ASSERT_NE) 141 /* ASSERT_LT(expected, measured): expected < measured */ 142 #define ASSERT_LT TEST_API(ASSERT_LT) 143 /* ASSERT_LE(expected, measured): expected <= measured */ 144 #define ASSERT_LE TEST_API(ASSERT_LE) 145 /* ASSERT_GT(expected, measured): expected > measured */ 146 #define ASSERT_GT TEST_API(ASSERT_GT) 147 /* ASSERT_GE(expected, measured): expected >= measured */ 148 #define ASSERT_GE TEST_API(ASSERT_GE) 149 /* ASSERT_NULL(measured): NULL == measured */ 150 #define ASSERT_NULL TEST_API(ASSERT_NULL) 151 /* ASSERT_TRUE(measured): measured != 0 */ 152 #define ASSERT_TRUE TEST_API(ASSERT_TRUE) 153 /* ASSERT_FALSE(measured): measured == 0 */ 154 #define ASSERT_FALSE TEST_API(ASSERT_FALSE) 155 /* ASSERT_STREQ(expected, measured): !strcmp(expected, measured) */ 156 #define ASSERT_STREQ TEST_API(ASSERT_STREQ) 157 /* ASSERT_STRNE(expected, measured): strcmp(expected, measured) */ 158 #define ASSERT_STRNE TEST_API(ASSERT_STRNE) 159 /* EXPECT_EQ(expected, measured): expected == measured */ 160 #define EXPECT_EQ TEST_API(EXPECT_EQ) 161 /* EXPECT_NE(expected, measured): expected != measured */ 162 #define EXPECT_NE TEST_API(EXPECT_NE) 163 /* EXPECT_LT(expected, measured): expected < measured */ 164 #define EXPECT_LT TEST_API(EXPECT_LT) 165 /* EXPECT_LE(expected, measured): expected <= measured */ 166 #define EXPECT_LE TEST_API(EXPECT_LE) 167 /* EXPECT_GT(expected, measured): expected > measured */ 168 #define EXPECT_GT TEST_API(EXPECT_GT) 169 /* EXPECT_GE(expected, measured): expected >= measured */ 170 #define EXPECT_GE TEST_API(EXPECT_GE) 171 /* EXPECT_NULL(measured): NULL == measured */ 172 #define EXPECT_NULL TEST_API(EXPECT_NULL) 173 /* EXPECT_TRUE(measured): 0 != measured */ 174 #define EXPECT_TRUE TEST_API(EXPECT_TRUE) 175 /* EXPECT_FALSE(measured): 0 == measured */ 176 #define EXPECT_FALSE TEST_API(EXPECT_FALSE) 177 /* EXPECT_STREQ(expected, measured): !strcmp(expected, measured) */ 178 #define EXPECT_STREQ TEST_API(EXPECT_STREQ) 179 /* EXPECT_STRNE(expected, measured): strcmp(expected, measured) */ 180 #define EXPECT_STRNE TEST_API(EXPECT_STRNE) 181 182 /* TH_LOG(format, ...) 183 * Optional debug logging function available for use in tests. 184 * Logging may be enabled or disabled by defining TH_LOG_ENABLED. 185 * E.g., #define TH_LOG_ENABLED 1 186 * If no definition is provided, logging is enabled by default. 187 */ 188 #define TH_LOG TEST_API(TH_LOG) 189 190 /* 191 * Internal implementation. 192 * 193 */ 194 195 /* Utilities exposed to the test definitions */ 196 #ifndef TH_LOG_STREAM 197 # define TH_LOG_STREAM stderr 198 #endif 199 200 #ifndef TH_LOG_ENABLED 201 # define TH_LOG_ENABLED 1 202 #endif 203 204 #define _TH_LOG(fmt, ...) do { \ 205 if (TH_LOG_ENABLED) \ 206 __TH_LOG(fmt, ##__VA_ARGS__); \ 207 } while (0) 208 209 /* Unconditional logger for internal use. */ 210 #define __TH_LOG(fmt, ...) \ 211 fprintf(TH_LOG_STREAM, "%s:%d:%s:" fmt "\n", \ 212 __FILE__, __LINE__, _metadata->name, ##__VA_ARGS__) 213 214 /* Defines the test function and creates the registration stub. */ 215 #define _TEST(test_name) __TEST_IMPL(test_name, -1) 216 217 #define _TEST_SIGNAL(test_name, signal) __TEST_IMPL(test_name, signal) 218 219 #define __TEST_IMPL(test_name, _signal) \ 220 static void test_name(struct __test_metadata *_metadata); \ 221 static struct __test_metadata _##test_name##_object = \ 222 { name: "global." #test_name, \ 223 fn: &test_name, termsig: _signal }; \ 224 static void __attribute__((constructor)) _register_##test_name(void) \ 225 { \ 226 __register_test(&_##test_name##_object); \ 227 } \ 228 static void test_name( \ 229 struct __test_metadata __attribute__((unused)) *_metadata) 230 231 /* Wraps the struct name so we have one less argument to pass around. */ 232 #define _FIXTURE_DATA(fixture_name) struct _test_data_##fixture_name 233 234 /* Called once per fixture to setup the data and register. */ 235 #define _FIXTURE(fixture_name) \ 236 static void __attribute__((constructor)) \ 237 _register_##fixture_name##_data(void) \ 238 { \ 239 __fixture_count++; \ 240 } \ 241 _FIXTURE_DATA(fixture_name) 242 243 /* Prepares the setup function for the fixture. |_metadata| is included 244 * so that ASSERT_* work as a convenience. 245 */ 246 #define _FIXTURE_SETUP(fixture_name) \ 247 void fixture_name##_setup( \ 248 struct __test_metadata __attribute__((unused)) *_metadata, \ 249 _FIXTURE_DATA(fixture_name) __attribute__((unused)) *self) 250 #define _FIXTURE_TEARDOWN(fixture_name) \ 251 void fixture_name##_teardown( \ 252 struct __test_metadata __attribute__((unused)) *_metadata, \ 253 _FIXTURE_DATA(fixture_name) __attribute__((unused)) *self) 254 255 /* Emits test registration and helpers for fixture-based test 256 * cases. 257 * TODO(wad) register fixtures on dedicated test lists. 258 */ 259 #define _TEST_F(fixture_name, test_name) \ 260 __TEST_F_IMPL(fixture_name, test_name, -1) 261 262 #define _TEST_F_SIGNAL(fixture_name, test_name, signal) \ 263 __TEST_F_IMPL(fixture_name, test_name, signal) 264 265 #define __TEST_F_IMPL(fixture_name, test_name, signal) \ 266 static void fixture_name##_##test_name( \ 267 struct __test_metadata *_metadata, \ 268 _FIXTURE_DATA(fixture_name) *self); \ 269 static inline void wrapper_##fixture_name##_##test_name( \ 270 struct __test_metadata *_metadata) \ 271 { \ 272 /* fixture data is alloced, setup, and torn down per call. */ \ 273 _FIXTURE_DATA(fixture_name) self; \ 274 memset(&self, 0, sizeof(_FIXTURE_DATA(fixture_name))); \ 275 fixture_name##_setup(_metadata, &self); \ 276 /* Let setup failure terminate early. */ \ 277 if (!_metadata->passed) \ 278 return; \ 279 fixture_name##_##test_name(_metadata, &self); \ 280 fixture_name##_teardown(_metadata, &self); \ 281 } \ 282 static struct __test_metadata \ 283 _##fixture_name##_##test_name##_object = { \ 284 name: #fixture_name "." #test_name, \ 285 fn: &wrapper_##fixture_name##_##test_name, \ 286 termsig: signal, \ 287 }; \ 288 static void __attribute__((constructor)) \ 289 _register_##fixture_name##_##test_name(void) \ 290 { \ 291 __register_test(&_##fixture_name##_##test_name##_object); \ 292 } \ 293 static void fixture_name##_##test_name( \ 294 struct __test_metadata __attribute__((unused)) *_metadata, \ 295 _FIXTURE_DATA(fixture_name) __attribute__((unused)) *self) 296 297 /* Exports a simple wrapper to run the test harness. */ 298 #define _TEST_HARNESS_MAIN \ 299 static void __attribute__((constructor)) \ 300 __constructor_order_last(void) \ 301 { \ 302 if (!__constructor_order) \ 303 __constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; \ 304 } \ 305 int main(int argc, char **argv) { \ 306 return test_harness_run(argc, argv); \ 307 } 308 309 #define _ASSERT_EQ(_expected, _seen) \ 310 __EXPECT(_expected, _seen, ==, 1) 311 #define _ASSERT_NE(_expected, _seen) \ 312 __EXPECT(_expected, _seen, !=, 1) 313 #define _ASSERT_LT(_expected, _seen) \ 314 __EXPECT(_expected, _seen, <, 1) 315 #define _ASSERT_LE(_expected, _seen) \ 316 __EXPECT(_expected, _seen, <=, 1) 317 #define _ASSERT_GT(_expected, _seen) \ 318 __EXPECT(_expected, _seen, >, 1) 319 #define _ASSERT_GE(_expected, _seen) \ 320 __EXPECT(_expected, _seen, >=, 1) 321 #define _ASSERT_NULL(_seen) \ 322 __EXPECT(NULL, _seen, ==, 1) 323 324 #define _ASSERT_TRUE(_seen) \ 325 _ASSERT_NE(0, _seen) 326 #define _ASSERT_FALSE(_seen) \ 327 _ASSERT_EQ(0, _seen) 328 #define _ASSERT_STREQ(_expected, _seen) \ 329 __EXPECT_STR(_expected, _seen, ==, 1) 330 #define _ASSERT_STRNE(_expected, _seen) \ 331 __EXPECT_STR(_expected, _seen, !=, 1) 332 333 #define _EXPECT_EQ(_expected, _seen) \ 334 __EXPECT(_expected, _seen, ==, 0) 335 #define _EXPECT_NE(_expected, _seen) \ 336 __EXPECT(_expected, _seen, !=, 0) 337 #define _EXPECT_LT(_expected, _seen) \ 338 __EXPECT(_expected, _seen, <, 0) 339 #define _EXPECT_LE(_expected, _seen) \ 340 __EXPECT(_expected, _seen, <=, 0) 341 #define _EXPECT_GT(_expected, _seen) \ 342 __EXPECT(_expected, _seen, >, 0) 343 #define _EXPECT_GE(_expected, _seen) \ 344 __EXPECT(_expected, _seen, >=, 0) 345 346 #define _EXPECT_NULL(_seen) \ 347 __EXPECT(NULL, _seen, ==, 0) 348 #define _EXPECT_TRUE(_seen) \ 349 _EXPECT_NE(0, _seen) 350 #define _EXPECT_FALSE(_seen) \ 351 _EXPECT_EQ(0, _seen) 352 353 #define _EXPECT_STREQ(_expected, _seen) \ 354 __EXPECT_STR(_expected, _seen, ==, 0) 355 #define _EXPECT_STRNE(_expected, _seen) \ 356 __EXPECT_STR(_expected, _seen, !=, 0) 357 358 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) 359 360 /* Support an optional handler after and ASSERT_* or EXPECT_*. The approach is 361 * not thread-safe, but it should be fine in most sane test scenarios. 362 * 363 * Using __bail(), which optionally abort()s, is the easiest way to early 364 * return while still providing an optional block to the API consumer. 365 */ 366 #define OPTIONAL_HANDLER(_assert) \ 367 for (; _metadata->trigger; _metadata->trigger = __bail(_assert)) 368 369 #define __EXPECT(_expected, _seen, _t, _assert) do { \ 370 /* Avoid multiple evaluation of the cases */ \ 371 __typeof__(_expected) __exp = (_expected); \ 372 __typeof__(_seen) __seen = (_seen); \ 373 if (!(__exp _t __seen)) { \ 374 unsigned long long __exp_print = (uintptr_t)__exp; \ 375 unsigned long long __seen_print = (uintptr_t)__seen; \ 376 __TH_LOG("Expected %s (%llu) %s %s (%llu)", \ 377 #_expected, __exp_print, #_t, \ 378 #_seen, __seen_print); \ 379 _metadata->passed = 0; \ 380 /* Ensure the optional handler is triggered */ \ 381 _metadata->trigger = 1; \ 382 } \ 383 } while (0); OPTIONAL_HANDLER(_assert) 384 385 #define __EXPECT_STR(_expected, _seen, _t, _assert) do { \ 386 const char *__exp = (_expected); \ 387 const char *__seen = (_seen); \ 388 if (!(strcmp(__exp, __seen) _t 0)) { \ 389 __TH_LOG("Expected '%s' %s '%s'.", __exp, #_t, __seen); \ 390 _metadata->passed = 0; \ 391 _metadata->trigger = 1; \ 392 } \ 393 } while (0); OPTIONAL_HANDLER(_assert) 394 395 /* Contains all the information for test execution and status checking. */ 396 struct __test_metadata { 397 const char *name; 398 void (*fn)(struct __test_metadata *); 399 int termsig; 400 int passed; 401 int trigger; /* extra handler after the evaluation */ 402 struct __test_metadata *prev, *next; 403 }; 404 405 /* Storage for the (global) tests to be run. */ 406 static struct __test_metadata *__test_list; 407 static unsigned int __test_count; 408 static unsigned int __fixture_count; 409 static int __constructor_order; 410 411 #define _CONSTRUCTOR_ORDER_FORWARD 1 412 #define _CONSTRUCTOR_ORDER_BACKWARD -1 413 414 /* 415 * Since constructors are called in reverse order, reverse the test 416 * list so tests are run in source declaration order. 417 * https://gcc.gnu.org/onlinedocs/gccint/Initialization.html 418 * However, it seems not all toolchains do this correctly, so use 419 * __constructor_order to detect which direction is called first 420 * and adjust list building logic to get things running in the right 421 * direction. 422 */ 423 static inline void __register_test(struct __test_metadata *t) 424 { 425 __test_count++; 426 /* Circular linked list where only prev is circular. */ 427 if (__test_list == NULL) { 428 __test_list = t; 429 t->next = NULL; 430 t->prev = t; 431 return; 432 } 433 if (__constructor_order == _CONSTRUCTOR_ORDER_FORWARD) { 434 t->next = NULL; 435 t->prev = __test_list->prev; 436 t->prev->next = t; 437 __test_list->prev = t; 438 } else { 439 t->next = __test_list; 440 t->next->prev = t; 441 t->prev = t; 442 __test_list = t; 443 } 444 } 445 446 static inline int __bail(int for_realz) 447 { 448 if (for_realz) 449 abort(); 450 return 0; 451 } 452 453 void __run_test(struct __test_metadata *t) 454 { 455 pid_t child_pid; 456 int status; 457 458 t->passed = 1; 459 t->trigger = 0; 460 printf("[ RUN ] %s\n", t->name); 461 child_pid = fork(); 462 if (child_pid < 0) { 463 printf("ERROR SPAWNING TEST CHILD\n"); 464 t->passed = 0; 465 } else if (child_pid == 0) { 466 t->fn(t); 467 _exit(t->passed); 468 } else { 469 /* TODO(wad) add timeout support. */ 470 waitpid(child_pid, &status, 0); 471 if (WIFEXITED(status)) { 472 t->passed = t->termsig == -1 ? WEXITSTATUS(status) : 0; 473 if (t->termsig != -1) { 474 fprintf(TH_LOG_STREAM, 475 "%s: Test exited normally " 476 "instead of by signal (code: %d)\n", 477 t->name, 478 WEXITSTATUS(status)); 479 } 480 } else if (WIFSIGNALED(status)) { 481 t->passed = 0; 482 if (WTERMSIG(status) == SIGABRT) { 483 fprintf(TH_LOG_STREAM, 484 "%s: Test terminated by assertion\n", 485 t->name); 486 } else if (WTERMSIG(status) == t->termsig) { 487 t->passed = 1; 488 } else { 489 fprintf(TH_LOG_STREAM, 490 "%s: Test terminated unexpectedly " 491 "by signal %d\n", 492 t->name, 493 WTERMSIG(status)); 494 } 495 } else { 496 fprintf(TH_LOG_STREAM, 497 "%s: Test ended in some other way [%u]\n", 498 t->name, 499 status); 500 } 501 } 502 printf("[ %4s ] %s\n", (t->passed ? "OK" : "FAIL"), t->name); 503 } 504 505 static int test_harness_run(int __attribute__((unused)) argc, 506 char __attribute__((unused)) **argv) 507 { 508 struct __test_metadata *t; 509 int ret = 0; 510 unsigned int count = 0; 511 unsigned int pass_count = 0; 512 513 /* TODO(wad) add optional arguments similar to gtest. */ 514 printf("[==========] Running %u tests from %u test cases.\n", 515 __test_count, __fixture_count + 1); 516 for (t = __test_list; t; t = t->next) { 517 count++; 518 __run_test(t); 519 if (t->passed) 520 pass_count++; 521 else 522 ret = 1; 523 } 524 printf("[==========] %u / %u tests passed.\n", pass_count, count); 525 printf("[ %s ]\n", (ret ? "FAILED" : "PASSED")); 526 return ret; 527 } 528 529 static void __attribute__((constructor)) __constructor_order_first(void) 530 { 531 if (!__constructor_order) 532 __constructor_order = _CONSTRUCTOR_ORDER_FORWARD; 533 } 534 535 #endif /* TEST_HARNESS_H_ */ 536
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.