kselftest_harness.h 35 KB

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