kselftest.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 <[email protected]>
  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. *
  20. * and finally report the pass/fail/skip/xfail state of the test with one of:
  21. *
  22. * ksft_test_result(condition, fmt, ...);
  23. * ksft_test_result_pass(fmt, ...);
  24. * ksft_test_result_fail(fmt, ...);
  25. * ksft_test_result_skip(fmt, ...);
  26. * ksft_test_result_xfail(fmt, ...);
  27. * ksft_test_result_error(fmt, ...);
  28. *
  29. * When all tests are finished, clean up and exit the program with one of:
  30. *
  31. * ksft_finished();
  32. * ksft_exit(condition);
  33. * ksft_exit_pass();
  34. * ksft_exit_fail();
  35. *
  36. * If the program wants to report details on why the entire program has
  37. * failed, it can instead exit with a message (this is usually done when
  38. * the program is aborting before finishing all tests):
  39. *
  40. * ksft_exit_fail_msg(fmt, ...);
  41. *
  42. */
  43. #ifndef __KSELFTEST_H
  44. #define __KSELFTEST_H
  45. #include <errno.h>
  46. #include <stdlib.h>
  47. #include <unistd.h>
  48. #include <stdarg.h>
  49. #include <stdio.h>
  50. #ifndef ARRAY_SIZE
  51. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  52. #endif
  53. /*
  54. * gcc cpuid.h provides __cpuid_count() since v4.4.
  55. * Clang/LLVM cpuid.h provides __cpuid_count() since v3.4.0.
  56. *
  57. * Provide local define for tests needing __cpuid_count() because
  58. * selftests need to work in older environments that do not yet
  59. * have __cpuid_count().
  60. */
  61. #ifndef __cpuid_count
  62. #define __cpuid_count(level, count, a, b, c, d) \
  63. __asm__ __volatile__ ("cpuid\n\t" \
  64. : "=a" (a), "=b" (b), "=c" (c), "=d" (d) \
  65. : "0" (level), "2" (count))
  66. #endif
  67. /* define kselftest exit codes */
  68. #define KSFT_PASS 0
  69. #define KSFT_FAIL 1
  70. #define KSFT_XFAIL 2
  71. #define KSFT_XPASS 3
  72. #define KSFT_SKIP 4
  73. /* counters */
  74. struct ksft_count {
  75. unsigned int ksft_pass;
  76. unsigned int ksft_fail;
  77. unsigned int ksft_xfail;
  78. unsigned int ksft_xpass;
  79. unsigned int ksft_xskip;
  80. unsigned int ksft_error;
  81. };
  82. static struct ksft_count ksft_cnt;
  83. static unsigned int ksft_plan;
  84. static inline unsigned int ksft_test_num(void)
  85. {
  86. return ksft_cnt.ksft_pass + ksft_cnt.ksft_fail +
  87. ksft_cnt.ksft_xfail + ksft_cnt.ksft_xpass +
  88. ksft_cnt.ksft_xskip + ksft_cnt.ksft_error;
  89. }
  90. static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; }
  91. static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; }
  92. static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; }
  93. static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; }
  94. static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; }
  95. static inline void ksft_inc_error_cnt(void) { ksft_cnt.ksft_error++; }
  96. static inline int ksft_get_pass_cnt(void) { return ksft_cnt.ksft_pass; }
  97. static inline int ksft_get_fail_cnt(void) { return ksft_cnt.ksft_fail; }
  98. static inline int ksft_get_xfail_cnt(void) { return ksft_cnt.ksft_xfail; }
  99. static inline int ksft_get_xpass_cnt(void) { return ksft_cnt.ksft_xpass; }
  100. static inline int ksft_get_xskip_cnt(void) { return ksft_cnt.ksft_xskip; }
  101. static inline int ksft_get_error_cnt(void) { return ksft_cnt.ksft_error; }
  102. static inline void ksft_print_header(void)
  103. {
  104. if (!(getenv("KSFT_TAP_LEVEL")))
  105. printf("TAP version 13\n");
  106. }
  107. static inline void ksft_set_plan(unsigned int plan)
  108. {
  109. ksft_plan = plan;
  110. printf("1..%d\n", ksft_plan);
  111. }
  112. static inline void ksft_print_cnts(void)
  113. {
  114. if (ksft_plan != ksft_test_num())
  115. printf("# Planned tests != run tests (%u != %u)\n",
  116. ksft_plan, ksft_test_num());
  117. printf("# Totals: pass:%d fail:%d xfail:%d xpass:%d skip:%d error:%d\n",
  118. ksft_cnt.ksft_pass, ksft_cnt.ksft_fail,
  119. ksft_cnt.ksft_xfail, ksft_cnt.ksft_xpass,
  120. ksft_cnt.ksft_xskip, ksft_cnt.ksft_error);
  121. }
  122. static inline void ksft_print_msg(const char *msg, ...)
  123. {
  124. int saved_errno = errno;
  125. va_list args;
  126. va_start(args, msg);
  127. printf("# ");
  128. errno = saved_errno;
  129. vprintf(msg, args);
  130. va_end(args);
  131. }
  132. static inline void ksft_test_result_pass(const char *msg, ...)
  133. {
  134. int saved_errno = errno;
  135. va_list args;
  136. ksft_cnt.ksft_pass++;
  137. va_start(args, msg);
  138. printf("ok %d ", ksft_test_num());
  139. errno = saved_errno;
  140. vprintf(msg, args);
  141. va_end(args);
  142. }
  143. static inline void ksft_test_result_fail(const char *msg, ...)
  144. {
  145. int saved_errno = errno;
  146. va_list args;
  147. ksft_cnt.ksft_fail++;
  148. va_start(args, msg);
  149. printf("not ok %d ", ksft_test_num());
  150. errno = saved_errno;
  151. vprintf(msg, args);
  152. va_end(args);
  153. }
  154. /**
  155. * ksft_test_result() - Report test success based on truth of condition
  156. *
  157. * @condition: if true, report test success, otherwise failure.
  158. */
  159. #define ksft_test_result(condition, fmt, ...) do { \
  160. if (!!(condition)) \
  161. ksft_test_result_pass(fmt, ##__VA_ARGS__);\
  162. else \
  163. ksft_test_result_fail(fmt, ##__VA_ARGS__);\
  164. } while (0)
  165. static inline void ksft_test_result_xfail(const char *msg, ...)
  166. {
  167. int saved_errno = errno;
  168. va_list args;
  169. ksft_cnt.ksft_xfail++;
  170. va_start(args, msg);
  171. printf("ok %d # XFAIL ", ksft_test_num());
  172. errno = saved_errno;
  173. vprintf(msg, args);
  174. va_end(args);
  175. }
  176. static inline void ksft_test_result_skip(const char *msg, ...)
  177. {
  178. int saved_errno = errno;
  179. va_list args;
  180. ksft_cnt.ksft_xskip++;
  181. va_start(args, msg);
  182. printf("ok %d # SKIP ", ksft_test_num());
  183. errno = saved_errno;
  184. vprintf(msg, args);
  185. va_end(args);
  186. }
  187. /* TODO: how does "error" differ from "fail" or "skip"? */
  188. static inline void ksft_test_result_error(const char *msg, ...)
  189. {
  190. int saved_errno = errno;
  191. va_list args;
  192. ksft_cnt.ksft_error++;
  193. va_start(args, msg);
  194. printf("not ok %d # error ", ksft_test_num());
  195. errno = saved_errno;
  196. vprintf(msg, args);
  197. va_end(args);
  198. }
  199. static inline int ksft_exit_pass(void)
  200. {
  201. ksft_print_cnts();
  202. exit(KSFT_PASS);
  203. }
  204. static inline int ksft_exit_fail(void)
  205. {
  206. ksft_print_cnts();
  207. exit(KSFT_FAIL);
  208. }
  209. /**
  210. * ksft_exit() - Exit selftest based on truth of condition
  211. *
  212. * @condition: if true, exit self test with success, otherwise fail.
  213. */
  214. #define ksft_exit(condition) do { \
  215. if (!!(condition)) \
  216. ksft_exit_pass(); \
  217. else \
  218. ksft_exit_fail(); \
  219. } while (0)
  220. /**
  221. * ksft_finished() - Exit selftest with success if all tests passed
  222. */
  223. #define ksft_finished() \
  224. ksft_exit(ksft_plan == \
  225. ksft_cnt.ksft_pass + \
  226. ksft_cnt.ksft_xfail + \
  227. ksft_cnt.ksft_xskip)
  228. static inline int ksft_exit_fail_msg(const char *msg, ...)
  229. {
  230. int saved_errno = errno;
  231. va_list args;
  232. va_start(args, msg);
  233. printf("Bail out! ");
  234. errno = saved_errno;
  235. vprintf(msg, args);
  236. va_end(args);
  237. ksft_print_cnts();
  238. exit(KSFT_FAIL);
  239. }
  240. static inline int ksft_exit_xfail(void)
  241. {
  242. ksft_print_cnts();
  243. exit(KSFT_XFAIL);
  244. }
  245. static inline int ksft_exit_xpass(void)
  246. {
  247. ksft_print_cnts();
  248. exit(KSFT_XPASS);
  249. }
  250. static inline int ksft_exit_skip(const char *msg, ...)
  251. {
  252. int saved_errno = errno;
  253. va_list args;
  254. va_start(args, msg);
  255. /*
  256. * FIXME: several tests misuse ksft_exit_skip so produce
  257. * something sensible if some tests have already been run
  258. * or a plan has been printed. Those tests should use
  259. * ksft_test_result_skip or ksft_exit_fail_msg instead.
  260. */
  261. if (ksft_plan || ksft_test_num()) {
  262. ksft_cnt.ksft_xskip++;
  263. printf("ok %d # SKIP ", 1 + ksft_test_num());
  264. } else {
  265. printf("1..0 # SKIP ");
  266. }
  267. if (msg) {
  268. errno = saved_errno;
  269. vprintf(msg, args);
  270. va_end(args);
  271. }
  272. if (ksft_test_num())
  273. ksft_print_cnts();
  274. exit(KSFT_SKIP);
  275. }
  276. #endif /* __KSELFTEST_H */