1 /* SPDX-License-Identifier: GPL-2.0 */
3 * kselftest.h: low-level kselftest framework to include from
4 * selftest programs. When possible, please use
5 * kselftest_harness.h instead.
7 * Copyright (c) 2014 Shuah Khan <shuahkh@osg.samsung.com>
8 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
10 * Using this API consists of first counting how many tests your code
11 * has to run, and then starting up the reporting:
13 * ksft_print_header();
14 * ksft_set_plan(total_number_of_tests);
16 * For each test, report any progress, debugging, etc with:
18 * ksft_print_msg(fmt, ...);
20 * and finally report the pass/fail/skip/xfail state of the test with one of:
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 * ksft_test_result_code(exit_code, test_name, fmt, ...);
30 * When all tests are finished, clean up and exit the program with one of:
33 * ksft_exit(condition);
37 * If the program wants to report details on why the entire program has
38 * failed, it can instead exit with a message (this is usually done when
39 * the program is aborting before finishing all tests):
41 * ksft_exit_fail_msg(fmt, ...);
54 #include <sys/utsname.h>
58 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
62 * gcc cpuid.h provides __cpuid_count() since v4.4.
63 * Clang/LLVM cpuid.h provides __cpuid_count() since v3.4.0.
65 * Provide local define for tests needing __cpuid_count() because
66 * selftests need to work in older environments that do not yet
67 * have __cpuid_count().
70 #define __cpuid_count(level, count, a, b, c, d) \
71 __asm__ __volatile__ ("cpuid\n\t" \
72 : "=a" (a), "=b" (b), "=c" (c), "=d" (d) \
73 : "0" (level), "2" (count))
76 /* define kselftest exit codes */
84 #define __noreturn __attribute__((__noreturn__))
86 #define __printf(a, b) __attribute__((format(printf, a, b)))
90 unsigned int ksft_pass;
91 unsigned int ksft_fail;
92 unsigned int ksft_xfail;
93 unsigned int ksft_xpass;
94 unsigned int ksft_xskip;
95 unsigned int ksft_error;
98 static struct ksft_count ksft_cnt;
99 static unsigned int ksft_plan;
101 static inline unsigned int ksft_test_num(void)
103 return ksft_cnt.ksft_pass + ksft_cnt.ksft_fail +
104 ksft_cnt.ksft_xfail + ksft_cnt.ksft_xpass +
105 ksft_cnt.ksft_xskip + ksft_cnt.ksft_error;
108 static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; }
109 static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; }
110 static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; }
111 static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; }
112 static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; }
113 static inline void ksft_inc_error_cnt(void) { ksft_cnt.ksft_error++; }
115 static inline int ksft_get_pass_cnt(void) { return ksft_cnt.ksft_pass; }
116 static inline int ksft_get_fail_cnt(void) { return ksft_cnt.ksft_fail; }
117 static inline int ksft_get_xfail_cnt(void) { return ksft_cnt.ksft_xfail; }
118 static inline int ksft_get_xpass_cnt(void) { return ksft_cnt.ksft_xpass; }
119 static inline int ksft_get_xskip_cnt(void) { return ksft_cnt.ksft_xskip; }
120 static inline int ksft_get_error_cnt(void) { return ksft_cnt.ksft_error; }
122 static inline void ksft_print_header(void)
125 * Force line buffering; If stdout is not connected to a terminal, it
126 * will otherwise default to fully buffered, which can cause output
127 * duplication if there is content in the buffer when fork()ing. If
128 * there is a crash, line buffering also means the most recent output
129 * line will be visible.
131 setvbuf(stdout, NULL, _IOLBF, 0);
133 if (!(getenv("KSFT_TAP_LEVEL")))
134 printf("TAP version 13\n");
137 static inline void ksft_set_plan(unsigned int plan)
140 printf("1..%u\n", ksft_plan);
143 static inline void ksft_print_cnts(void)
145 if (ksft_plan != ksft_test_num())
146 printf("# Planned tests != run tests (%u != %u)\n",
147 ksft_plan, ksft_test_num());
148 printf("# Totals: pass:%u fail:%u xfail:%u xpass:%u skip:%u error:%u\n",
149 ksft_cnt.ksft_pass, ksft_cnt.ksft_fail,
150 ksft_cnt.ksft_xfail, ksft_cnt.ksft_xpass,
151 ksft_cnt.ksft_xskip, ksft_cnt.ksft_error);
154 static inline __printf(1, 2) void ksft_print_msg(const char *msg, ...)
156 int saved_errno = errno;
166 static inline void ksft_perror(const char *msg)
169 ksft_print_msg("%s: %s (%d)\n", msg, strerror(errno), errno);
172 * nolibc doesn't provide strerror() and it seems
173 * inappropriate to add one, just print the errno.
175 ksft_print_msg("%s: %d)\n", msg, errno);
179 static inline __printf(1, 2) void ksft_test_result_pass(const char *msg, ...)
181 int saved_errno = errno;
184 ksft_cnt.ksft_pass++;
187 printf("ok %u ", ksft_test_num());
193 static inline __printf(1, 2) void ksft_test_result_fail(const char *msg, ...)
195 int saved_errno = errno;
198 ksft_cnt.ksft_fail++;
201 printf("not ok %u ", ksft_test_num());
208 * ksft_test_result() - Report test success based on truth of condition
210 * @condition: if true, report test success, otherwise failure.
212 #define ksft_test_result(condition, fmt, ...) do { \
214 ksft_test_result_pass(fmt, ##__VA_ARGS__);\
216 ksft_test_result_fail(fmt, ##__VA_ARGS__);\
219 static inline __printf(1, 2) void ksft_test_result_xfail(const char *msg, ...)
221 int saved_errno = errno;
224 ksft_cnt.ksft_xfail++;
227 printf("ok %u # XFAIL ", ksft_test_num());
233 static inline __printf(1, 2) void ksft_test_result_skip(const char *msg, ...)
235 int saved_errno = errno;
238 ksft_cnt.ksft_xskip++;
241 printf("ok %u # SKIP ", ksft_test_num());
247 /* TODO: how does "error" differ from "fail" or "skip"? */
248 static inline __printf(1, 2) void ksft_test_result_error(const char *msg, ...)
250 int saved_errno = errno;
253 ksft_cnt.ksft_error++;
256 printf("not ok %u # error ", ksft_test_num());
262 static inline __printf(3, 4)
263 void ksft_test_result_code(int exit_code, const char *test_name,
264 const char *msg, ...)
266 const char *tap_code = "ok";
267 const char *directive = "";
268 int saved_errno = errno;
273 ksft_cnt.ksft_pass++;
276 directive = " # XFAIL ";
277 ksft_cnt.ksft_xfail++;
280 directive = " # XPASS ";
281 ksft_cnt.ksft_xpass++;
284 directive = " # SKIP ";
285 ksft_cnt.ksft_xskip++;
290 ksft_cnt.ksft_fail++;
294 /* Docs seem to call for double space if directive is absent */
295 if (!directive[0] && msg)
298 printf("%s %u %s%s", tap_code, ksft_test_num(), test_name, directive);
308 static inline __noreturn int ksft_exit_pass(void)
314 static inline __noreturn int ksft_exit_fail(void)
321 * ksft_exit() - Exit selftest based on truth of condition
323 * @condition: if true, exit self test with success, otherwise fail.
325 #define ksft_exit(condition) do { \
333 * ksft_finished() - Exit selftest with success if all tests passed
335 #define ksft_finished() \
336 ksft_exit(ksft_plan == \
337 ksft_cnt.ksft_pass + \
338 ksft_cnt.ksft_xfail + \
341 static inline __noreturn __printf(1, 2) int ksft_exit_fail_msg(const char *msg, ...)
343 int saved_errno = errno;
347 printf("Bail out! ");
356 static inline __noreturn int ksft_exit_xfail(void)
362 static inline __noreturn int ksft_exit_xpass(void)
368 static inline __noreturn __printf(1, 2) int ksft_exit_skip(const char *msg, ...)
370 int saved_errno = errno;
376 * FIXME: several tests misuse ksft_exit_skip so produce
377 * something sensible if some tests have already been run
378 * or a plan has been printed. Those tests should use
379 * ksft_test_result_skip or ksft_exit_fail_msg instead.
381 if (ksft_plan || ksft_test_num()) {
382 ksft_cnt.ksft_xskip++;
383 printf("ok %d # SKIP ", 1 + ksft_test_num());
385 printf("1..0 # SKIP ");
397 static inline int ksft_min_kernel_version(unsigned int min_major,
398 unsigned int min_minor)
401 ksft_print_msg("NOLIBC: Can't check kernel version: Function not implemented\n");
404 unsigned int major, minor;
407 if (uname(&info) || sscanf(info.release, "%u.%u.", &major, &minor) != 2)
408 ksft_exit_fail_msg("Can't parse kernel version\n");
410 return major > min_major || (major == min_major && minor >= min_minor);
414 #endif /* __KSELFTEST_H */