kselftest_harness.h 28 KB

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