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.
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, ...);
21 * and finally report the pass/fail/skip/xfail/xpass state of the test
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, ...);
34 * When all tests are finished, clean up and exit the program with one of:
37 * ksft_exit(condition);
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):
45 * ksft_exit_fail_msg(fmt, ...);
46 * ksft_exit_fail_perror(msg);
59 #include <sys/utsname.h>
63 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
66 #if defined(__i386__) || defined(__x86_64__) /* arch */
68 * gcc cpuid.h provides __cpuid_count() since v4.4.
69 * Clang/LLVM cpuid.h provides __cpuid_count() since v3.4.0.
71 * Provide local define for tests needing __cpuid_count() because
72 * selftests need to work in older environments that do not yet
73 * have __cpuid_count().
76 #define __cpuid_count(level, count, a, b, c, d) \
77 __asm__ __volatile__ ("cpuid\n\t" \
78 : "=a" (a), "=b" (b), "=c" (c), "=d" (d) \
79 : "0" (level), "2" (count))
83 /* define kselftest exit codes */
91 #define __noreturn __attribute__((__noreturn__))
93 #define __printf(a, b) __attribute__((format(printf, a, b)))
97 unsigned int ksft_pass;
98 unsigned int ksft_fail;
99 unsigned int ksft_xfail;
100 unsigned int ksft_xpass;
101 unsigned int ksft_xskip;
102 unsigned int ksft_error;
105 static struct ksft_count ksft_cnt;
106 static unsigned int ksft_plan;
108 static inline unsigned int ksft_test_num(void)
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;
115 static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; }
116 static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; }
117 static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; }
118 static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; }
119 static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; }
120 static inline void ksft_inc_error_cnt(void) { ksft_cnt.ksft_error++; }
122 static inline int ksft_get_pass_cnt(void) { return ksft_cnt.ksft_pass; }
123 static inline int ksft_get_fail_cnt(void) { return ksft_cnt.ksft_fail; }
124 static inline int ksft_get_xfail_cnt(void) { return ksft_cnt.ksft_xfail; }
125 static inline int ksft_get_xpass_cnt(void) { return ksft_cnt.ksft_xpass; }
126 static inline int ksft_get_xskip_cnt(void) { return ksft_cnt.ksft_xskip; }
127 static inline int ksft_get_error_cnt(void) { return ksft_cnt.ksft_error; }
129 static inline void ksft_print_header(void)
132 * Force line buffering; If stdout is not connected to a terminal, it
133 * will otherwise default to fully buffered, which can cause output
134 * duplication if there is content in the buffer when fork()ing. If
135 * there is a crash, line buffering also means the most recent output
136 * line will be visible.
138 setvbuf(stdout, NULL, _IOLBF, 0);
140 if (!(getenv("KSFT_TAP_LEVEL")))
141 printf("TAP version 13\n");
144 static inline void ksft_set_plan(unsigned int plan)
147 printf("1..%u\n", ksft_plan);
150 static inline void ksft_print_cnts(void)
152 if (ksft_cnt.ksft_xskip > 0)
154 "# %u skipped test(s) detected. Consider enabling relevant config options to improve coverage.\n",
157 if (ksft_plan != ksft_test_num())
158 printf("# Planned tests != run tests (%u != %u)\n",
159 ksft_plan, ksft_test_num());
160 printf("# Totals: pass:%u fail:%u xfail:%u xpass:%u skip:%u error:%u\n",
161 ksft_cnt.ksft_pass, ksft_cnt.ksft_fail,
162 ksft_cnt.ksft_xfail, ksft_cnt.ksft_xpass,
163 ksft_cnt.ksft_xskip, ksft_cnt.ksft_error);
166 static inline __printf(1, 2) void ksft_print_msg(const char *msg, ...)
168 int saved_errno = errno;
178 static inline void ksft_perror(const char *msg)
180 ksft_print_msg("%s: %s (%d)\n", msg, strerror(errno), errno);
183 static inline __printf(1, 2) void ksft_test_result_pass(const char *msg, ...)
185 int saved_errno = errno;
188 ksft_cnt.ksft_pass++;
191 printf("ok %u ", ksft_test_num());
197 static inline __printf(1, 2) void ksft_test_result_fail(const char *msg, ...)
199 int saved_errno = errno;
202 ksft_cnt.ksft_fail++;
205 printf("not ok %u ", ksft_test_num());
212 * ksft_test_result() - Report test success based on truth of condition
214 * @condition: if true, report test success, otherwise failure.
216 #define ksft_test_result(condition, fmt, ...) do { \
218 ksft_test_result_pass(fmt, ##__VA_ARGS__);\
220 ksft_test_result_fail(fmt, ##__VA_ARGS__);\
223 static inline __printf(1, 2) void ksft_test_result_xfail(const char *msg, ...)
225 int saved_errno = errno;
228 ksft_cnt.ksft_xfail++;
231 printf("ok %u # XFAIL ", ksft_test_num());
237 static inline __printf(1, 2) void ksft_test_result_xpass(const char *msg, ...)
239 int saved_errno = errno;
242 ksft_cnt.ksft_xpass++;
245 printf("ok %u # XPASS ", ksft_test_num());
251 static inline __printf(1, 2) void ksft_test_result_skip(const char *msg, ...)
253 int saved_errno = errno;
256 ksft_cnt.ksft_xskip++;
259 printf("ok %u # SKIP ", ksft_test_num());
265 /* TODO: how does "error" differ from "fail" or "skip"? */
266 static inline __printf(1, 2) void ksft_test_result_error(const char *msg, ...)
268 int saved_errno = errno;
271 ksft_cnt.ksft_error++;
274 printf("not ok %u # error ", ksft_test_num());
280 static inline __printf(3, 4)
281 void ksft_test_result_code(int exit_code, const char *test_name,
282 const char *msg, ...)
284 const char *tap_code = "ok";
285 const char *directive = "";
286 int saved_errno = errno;
291 ksft_cnt.ksft_pass++;
294 directive = " # XFAIL ";
295 ksft_cnt.ksft_xfail++;
298 directive = " # XPASS ";
299 ksft_cnt.ksft_xpass++;
302 directive = " # SKIP ";
303 ksft_cnt.ksft_xskip++;
308 ksft_cnt.ksft_fail++;
312 /* Docs seem to call for double space if directive is absent */
313 if (!directive[0] && msg)
316 printf("%s %u %s%s", tap_code, ksft_test_num(), test_name, directive);
327 * ksft_test_result() - Report test success based on truth of condition
329 * @condition: if true, report test success, otherwise failure.
331 #define ksft_test_result_report(result, fmt, ...) do { \
334 ksft_test_result_pass(fmt, ##__VA_ARGS__); \
337 ksft_test_result_fail(fmt, ##__VA_ARGS__); \
340 ksft_test_result_xfail(fmt, ##__VA_ARGS__); \
343 ksft_test_result_xpass(fmt, ##__VA_ARGS__); \
346 ksft_test_result_skip(fmt, ##__VA_ARGS__); \
350 static inline __noreturn void ksft_exit_pass(void)
356 static inline __noreturn void ksft_exit_fail(void)
363 * ksft_exit() - Exit selftest based on truth of condition
365 * @condition: if true, exit self test with success, otherwise fail.
367 #define ksft_exit(condition) do { \
375 * ksft_finished() - Exit selftest with success if all tests passed
377 #define ksft_finished() \
378 ksft_exit(ksft_plan == \
379 ksft_cnt.ksft_pass + \
380 ksft_cnt.ksft_xfail + \
383 static inline __noreturn __printf(1, 2) void ksft_exit_fail_msg(const char *msg, ...)
385 int saved_errno = errno;
389 printf("Bail out! ");
398 static inline __noreturn void ksft_exit_fail_perror(const char *msg)
400 ksft_exit_fail_msg("%s: %s (%d)\n", msg, strerror(errno), errno);
403 static inline __noreturn void ksft_exit_xfail(void)
409 static inline __noreturn void ksft_exit_xpass(void)
415 static inline __noreturn __printf(1, 2) void ksft_exit_skip(const char *msg, ...)
417 int saved_errno = errno;
423 * FIXME: several tests misuse ksft_exit_skip so produce
424 * something sensible if some tests have already been run
425 * or a plan has been printed. Those tests should use
426 * ksft_test_result_skip or ksft_exit_fail_msg instead.
428 if (ksft_plan || ksft_test_num()) {
429 ksft_cnt.ksft_xskip++;
430 printf("ok %u # SKIP ", 1 + ksft_test_num());
432 printf("1..0 # SKIP ");
444 static inline int ksft_min_kernel_version(unsigned int min_major,
445 unsigned int min_minor)
448 ksft_print_msg("NOLIBC: Can't check kernel version: Function not implemented\n");
451 unsigned int major, minor;
454 if (uname(&info) || sscanf(info.release, "%u.%u.", &major, &minor) != 2)
455 ksft_exit_fail_msg("Can't parse kernel version\n");
457 return major > min_major || (major == min_major && minor >= min_minor);
461 #endif /* __KSELFTEST_H */