kselftest_harness.h 20 KB

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