kselftest.h 11 KB

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