kselftest.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * kselftest.h: low-level kselftest framework to include from
  4. * selftest programs. When possible, please use
  5. * kselftest_harness.h instead.
  6. *
  7. * Copyright (c) 2014 Shuah Khan <shuahkh@osg.samsung.com>
  8. * Copyright (c) 2014 Samsung Electronics Co., Ltd.
  9. *
  10. * Using this API consists of first counting how many tests your code
  11. * has to run, and then starting up the reporting:
  12. *
  13. * ksft_print_header();
  14. * ksft_set_plan(total_number_of_tests);
  15. *
  16. * For each test, report any progress, debugging, etc with:
  17. *
  18. * ksft_print_msg(fmt, ...);
  19. * ksft_perror(msg);
  20. *
  21. * and finally report the pass/fail/skip/xfail/xpass state of the test
  22. * with one of:
  23. *
  24. * ksft_test_result(condition, fmt, ...);
  25. * ksft_test_result_report(result, fmt, ...);
  26. * ksft_test_result_pass(fmt, ...);
  27. * ksft_test_result_fail(fmt, ...);
  28. * ksft_test_result_skip(fmt, ...);
  29. * ksft_test_result_xfail(fmt, ...);
  30. * ksft_test_result_xpass(fmt, ...);
  31. * ksft_test_result_error(fmt, ...);
  32. * ksft_test_result_code(exit_code, test_name, fmt, ...);
  33. *
  34. * When all tests are finished, clean up and exit the program with one of:
  35. *
  36. * ksft_finished();
  37. * ksft_exit(condition);
  38. * ksft_exit_pass();
  39. * ksft_exit_fail();
  40. *
  41. * If the program wants to report details on why the entire program has
  42. * failed, it can instead exit with a message (this is usually done when
  43. * the program is aborting before finishing all tests):
  44. *
  45. * ksft_exit_fail_msg(fmt, ...);
  46. * ksft_exit_fail_perror(msg);
  47. *
  48. */
  49. #ifndef __KSELFTEST_H
  50. #define __KSELFTEST_H
  51. #ifndef NOLIBC
  52. #include <errno.h>
  53. #include <stdlib.h>
  54. #include <unistd.h>
  55. #include <stdarg.h>
  56. #include <stdbool.h>
  57. #include <string.h>
  58. #include <stdio.h>
  59. #include <sys/utsname.h>
  60. #endif
  61. #ifndef ARRAY_SIZE
  62. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  63. #endif
  64. #if defined(__i386__) || defined(__x86_64__) /* arch */
  65. /*
  66. * gcc cpuid.h provides __cpuid_count() since v4.4.
  67. * Clang/LLVM cpuid.h provides __cpuid_count() since v3.4.0.
  68. *
  69. * Provide local define for tests needing __cpuid_count() because
  70. * selftests need to work in older environments that do not yet
  71. * have __cpuid_count().
  72. */
  73. #ifndef __cpuid_count
  74. #define __cpuid_count(level, count, a, b, c, d) \
  75. __asm__ __volatile__ ("cpuid\n\t" \
  76. : "=a" (a), "=b" (b), "=c" (c), "=d" (d) \
  77. : "0" (level), "2" (count))
  78. #endif
  79. #endif /* end arch */
  80. /* define kselftest exit codes */
  81. #define KSFT_PASS 0
  82. #define KSFT_FAIL 1
  83. #define KSFT_XFAIL 2
  84. #define KSFT_XPASS 3
  85. #define KSFT_SKIP 4
  86. #ifndef __noreturn
  87. #define __noreturn __attribute__((__noreturn__))
  88. #endif
  89. #define __printf(a, b) __attribute__((format(printf, a, b)))
  90. #ifndef __always_unused
  91. #define __always_unused __attribute__((__unused__))
  92. #endif
  93. #ifndef __maybe_unused
  94. #define __maybe_unused __attribute__((__unused__))
  95. #endif
  96. /* counters */
  97. struct ksft_count {
  98. unsigned int ksft_pass;
  99. unsigned int ksft_fail;
  100. unsigned int ksft_xfail;
  101. unsigned int ksft_xpass;
  102. unsigned int ksft_xskip;
  103. unsigned int ksft_error;
  104. };
  105. static struct ksft_count ksft_cnt;
  106. static unsigned int ksft_plan;
  107. static bool ksft_debug_enabled;
  108. static inline unsigned int ksft_test_num(void)
  109. {
  110. return ksft_cnt.ksft_pass + ksft_cnt.ksft_fail +
  111. ksft_cnt.ksft_xfail + ksft_cnt.ksft_xpass +
  112. ksft_cnt.ksft_xskip + ksft_cnt.ksft_error;
  113. }
  114. static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; }
  115. static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; }
  116. static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; }
  117. static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; }
  118. static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; }
  119. static inline void ksft_inc_error_cnt(void) { ksft_cnt.ksft_error++; }
  120. static inline int ksft_get_pass_cnt(void) { return ksft_cnt.ksft_pass; }
  121. static inline int ksft_get_fail_cnt(void) { return ksft_cnt.ksft_fail; }
  122. static inline int ksft_get_xfail_cnt(void) { return ksft_cnt.ksft_xfail; }
  123. static inline int ksft_get_xpass_cnt(void) { return ksft_cnt.ksft_xpass; }
  124. static inline int ksft_get_xskip_cnt(void) { return ksft_cnt.ksft_xskip; }
  125. static inline int ksft_get_error_cnt(void) { return ksft_cnt.ksft_error; }
  126. static inline void ksft_print_header(void)
  127. {
  128. /*
  129. * Force line buffering; If stdout is not connected to a terminal, it
  130. * will otherwise default to fully buffered, which can cause output
  131. * duplication if there is content in the buffer when fork()ing. If
  132. * there is a crash, line buffering also means the most recent output
  133. * line will be visible.
  134. */
  135. setvbuf(stdout, NULL, _IOLBF, 0);
  136. if (!(getenv("KSFT_TAP_LEVEL")))
  137. printf("TAP version 13\n");
  138. }
  139. static inline void ksft_set_plan(unsigned int plan)
  140. {
  141. ksft_plan = plan;
  142. printf("1..%u\n", ksft_plan);
  143. }
  144. static inline void ksft_print_cnts(void)
  145. {
  146. if (ksft_cnt.ksft_xskip > 0)
  147. printf(
  148. "# %u skipped test(s) detected. Consider enabling relevant config options to improve coverage.\n",
  149. ksft_cnt.ksft_xskip
  150. );
  151. if (ksft_plan != ksft_test_num())
  152. printf("# Planned tests != run tests (%u != %u)\n",
  153. ksft_plan, ksft_test_num());
  154. printf("# Totals: pass:%u fail:%u xfail:%u xpass:%u skip:%u error:%u\n",
  155. ksft_cnt.ksft_pass, ksft_cnt.ksft_fail,
  156. ksft_cnt.ksft_xfail, ksft_cnt.ksft_xpass,
  157. ksft_cnt.ksft_xskip, ksft_cnt.ksft_error);
  158. }
  159. static inline __printf(1, 2) void ksft_print_msg(const char *msg, ...)
  160. {
  161. int saved_errno = errno;
  162. va_list args;
  163. va_start(args, msg);
  164. printf("# ");
  165. errno = saved_errno;
  166. vprintf(msg, args);
  167. va_end(args);
  168. }
  169. static inline void ksft_print_dbg_msg(const char *msg, ...)
  170. {
  171. va_list args;
  172. if (!ksft_debug_enabled)
  173. return;
  174. va_start(args, msg);
  175. ksft_print_msg(msg, args);
  176. va_end(args);
  177. }
  178. static inline void ksft_perror(const char *msg)
  179. {
  180. ksft_print_msg("%s: %s (%d)\n", msg, strerror(errno), errno);
  181. }
  182. static inline __printf(1, 2) void ksft_test_result_pass(const char *msg, ...)
  183. {
  184. int saved_errno = errno;
  185. va_list args;
  186. ksft_cnt.ksft_pass++;
  187. va_start(args, msg);
  188. printf("ok %u ", ksft_test_num());
  189. errno = saved_errno;
  190. vprintf(msg, args);
  191. va_end(args);
  192. }
  193. static inline __printf(1, 2) void ksft_test_result_fail(const char *msg, ...)
  194. {
  195. int saved_errno = errno;
  196. va_list args;
  197. ksft_cnt.ksft_fail++;
  198. va_start(args, msg);
  199. printf("not ok %u ", ksft_test_num());
  200. errno = saved_errno;
  201. vprintf(msg, args);
  202. va_end(args);
  203. }
  204. /**
  205. * ksft_test_result() - Report test success based on truth of condition
  206. *
  207. * @condition: if true, report test success, otherwise failure.
  208. */
  209. #define ksft_test_result(condition, fmt, ...) do { \
  210. if (!!(condition)) \
  211. ksft_test_result_pass(fmt, ##__VA_ARGS__);\
  212. else \
  213. ksft_test_result_fail(fmt, ##__VA_ARGS__);\
  214. } while (0)
  215. static inline __printf(1, 2) void ksft_test_result_xfail(const char *msg, ...)
  216. {
  217. int saved_errno = errno;
  218. va_list args;
  219. ksft_cnt.ksft_xfail++;
  220. va_start(args, msg);
  221. printf("ok %u # XFAIL ", ksft_test_num());
  222. errno = saved_errno;
  223. vprintf(msg, args);
  224. va_end(args);
  225. }
  226. static inline __printf(1, 2) void ksft_test_result_xpass(const char *msg, ...)
  227. {
  228. int saved_errno = errno;
  229. va_list args;
  230. ksft_cnt.ksft_xpass++;
  231. va_start(args, msg);
  232. printf("ok %u # XPASS ", ksft_test_num());
  233. errno = saved_errno;
  234. vprintf(msg, args);
  235. va_end(args);
  236. }
  237. static inline __printf(1, 2) void ksft_test_result_skip(const char *msg, ...)
  238. {
  239. int saved_errno = errno;
  240. va_list args;
  241. ksft_cnt.ksft_xskip++;
  242. va_start(args, msg);
  243. printf("ok %u # SKIP ", ksft_test_num());
  244. errno = saved_errno;
  245. vprintf(msg, args);
  246. va_end(args);
  247. }
  248. /* TODO: how does "error" differ from "fail" or "skip"? */
  249. static inline __printf(1, 2) void ksft_test_result_error(const char *msg, ...)
  250. {
  251. int saved_errno = errno;
  252. va_list args;
  253. ksft_cnt.ksft_error++;
  254. va_start(args, msg);
  255. printf("not ok %u # error ", ksft_test_num());
  256. errno = saved_errno;
  257. vprintf(msg, args);
  258. va_end(args);
  259. }
  260. static inline __printf(3, 4)
  261. void ksft_test_result_code(int exit_code, const char *test_name,
  262. const char *msg, ...)
  263. {
  264. const char *tap_code = "ok";
  265. const char *directive = "";
  266. int saved_errno = errno;
  267. va_list args;
  268. switch (exit_code) {
  269. case KSFT_PASS:
  270. ksft_cnt.ksft_pass++;
  271. break;
  272. case KSFT_XFAIL:
  273. directive = " # XFAIL ";
  274. ksft_cnt.ksft_xfail++;
  275. break;
  276. case KSFT_XPASS:
  277. directive = " # XPASS ";
  278. ksft_cnt.ksft_xpass++;
  279. break;
  280. case KSFT_SKIP:
  281. directive = " # SKIP ";
  282. ksft_cnt.ksft_xskip++;
  283. break;
  284. case KSFT_FAIL:
  285. default:
  286. tap_code = "not ok";
  287. ksft_cnt.ksft_fail++;
  288. break;
  289. }
  290. /* Docs seem to call for double space if directive is absent */
  291. if (!directive[0] && msg)
  292. directive = " # ";
  293. printf("%s %u %s%s", tap_code, ksft_test_num(), test_name, directive);
  294. errno = saved_errno;
  295. if (msg) {
  296. va_start(args, msg);
  297. vprintf(msg, args);
  298. va_end(args);
  299. }
  300. printf("\n");
  301. }
  302. /**
  303. * ksft_test_result() - Report test success based on truth of condition
  304. *
  305. * @condition: if true, report test success, otherwise failure.
  306. */
  307. #define ksft_test_result_report(result, fmt, ...) do { \
  308. switch (result) { \
  309. case KSFT_PASS: \
  310. ksft_test_result_pass(fmt, ##__VA_ARGS__); \
  311. break; \
  312. case KSFT_FAIL: \
  313. ksft_test_result_fail(fmt, ##__VA_ARGS__); \
  314. break; \
  315. case KSFT_XFAIL: \
  316. ksft_test_result_xfail(fmt, ##__VA_ARGS__); \
  317. break; \
  318. case KSFT_XPASS: \
  319. ksft_test_result_xpass(fmt, ##__VA_ARGS__); \
  320. break; \
  321. case KSFT_SKIP: \
  322. ksft_test_result_skip(fmt, ##__VA_ARGS__); \
  323. break; \
  324. } } while (0)
  325. static inline __noreturn void ksft_exit_pass(void)
  326. {
  327. ksft_print_cnts();
  328. exit(KSFT_PASS);
  329. }
  330. static inline __noreturn void ksft_exit_fail(void)
  331. {
  332. ksft_print_cnts();
  333. exit(KSFT_FAIL);
  334. }
  335. /**
  336. * ksft_exit() - Exit selftest based on truth of condition
  337. *
  338. * @condition: if true, exit self test with success, otherwise fail.
  339. */
  340. #define ksft_exit(condition) do { \
  341. if (!!(condition)) \
  342. ksft_exit_pass(); \
  343. else \
  344. ksft_exit_fail(); \
  345. } while (0)
  346. /**
  347. * ksft_finished() - Exit selftest with success if all tests passed
  348. */
  349. #define ksft_finished() \
  350. ksft_exit(ksft_plan == \
  351. ksft_cnt.ksft_pass + \
  352. ksft_cnt.ksft_xfail + \
  353. ksft_cnt.ksft_xskip)
  354. static inline __noreturn __printf(1, 2) void ksft_exit_fail_msg(const char *msg, ...)
  355. {
  356. int saved_errno = errno;
  357. va_list args;
  358. va_start(args, msg);
  359. printf("Bail out! ");
  360. errno = saved_errno;
  361. vprintf(msg, args);
  362. va_end(args);
  363. ksft_print_cnts();
  364. exit(KSFT_FAIL);
  365. }
  366. static inline __noreturn void ksft_exit_fail_perror(const char *msg)
  367. {
  368. ksft_exit_fail_msg("%s: %s (%d)\n", msg, strerror(errno), errno);
  369. }
  370. static inline __noreturn void ksft_exit_xfail(void)
  371. {
  372. ksft_print_cnts();
  373. exit(KSFT_XFAIL);
  374. }
  375. static inline __noreturn void ksft_exit_xpass(void)
  376. {
  377. ksft_print_cnts();
  378. exit(KSFT_XPASS);
  379. }
  380. static inline __noreturn __printf(1, 2) void ksft_exit_skip(const char *msg, ...)
  381. {
  382. int saved_errno = errno;
  383. va_list args;
  384. va_start(args, msg);
  385. /*
  386. * FIXME: several tests misuse ksft_exit_skip so produce
  387. * something sensible if some tests have already been run
  388. * or a plan has been printed. Those tests should use
  389. * ksft_test_result_skip or ksft_exit_fail_msg instead.
  390. */
  391. if (ksft_plan || ksft_test_num()) {
  392. ksft_cnt.ksft_xskip++;
  393. printf("ok %u # SKIP ", 1 + ksft_test_num());
  394. } else {
  395. printf("1..0 # SKIP ");
  396. }
  397. if (msg) {
  398. errno = saved_errno;
  399. vprintf(msg, args);
  400. va_end(args);
  401. }
  402. if (ksft_test_num())
  403. ksft_print_cnts();
  404. exit(KSFT_SKIP);
  405. }
  406. static inline int ksft_min_kernel_version(unsigned int min_major,
  407. unsigned int min_minor)
  408. {
  409. unsigned int major, minor;
  410. struct utsname info;
  411. if (uname(&info) || sscanf(info.release, "%u.%u.", &major, &minor) != 2)
  412. ksft_exit_fail_msg("Can't parse kernel version\n");
  413. return major > min_major || (major == min_major && minor >= min_minor);
  414. }
  415. #endif /* __KSELFTEST_H */