Linux Audio

Check our new training course

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