kselftest_harness.h 35 KB

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