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 state of the test with one of:
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, ...);
32 * When all tests are finished, clean up and exit the program with one of:
35 * ksft_exit(condition);
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):
43 * ksft_exit_fail_msg(fmt, ...);
44 * ksft_exit_fail_perror(msg);
57 #include <sys/utsname.h>
61 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
65 * gcc cpuid.h provides __cpuid_count() since v4.4.
66 * Clang/LLVM cpuid.h provides __cpuid_count() since v3.4.0.
68 * Provide local define for tests needing __cpuid_count() because
69 * selftests need to work in older environments that do not yet
70 * have __cpuid_count().
73 #define __cpuid_count(level, count, a, b, c, d) \
74 __asm__ __volatile__ ("cpuid\n\t" \
75 : "=a" (a), "=b" (b), "=c" (c), "=d" (d) \
76 : "0" (level), "2" (count))
79 /* define kselftest exit codes */
87 #define __noreturn __attribute__((__noreturn__))
89 #define __printf(a, b) __attribute__((format(printf, a, b)))
93 unsigned int ksft_pass;
94 unsigned int ksft_fail;
95 unsigned int ksft_xfail;
96 unsigned int ksft_xpass;
97 unsigned int ksft_xskip;
98 unsigned int ksft_error;
101 static struct ksft_count ksft_cnt;
102 static unsigned int ksft_plan;
104 static inline unsigned int ksft_test_num(void)
106 return ksft_cnt.ksft_pass + ksft_cnt.ksft_fail +
107 ksft_cnt.ksft_xfail + ksft_cnt.ksft_xpass +
108 ksft_cnt.ksft_xskip + ksft_cnt.ksft_error;
111 static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; }
112 static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; }
113 static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; }
114 static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; }
115 static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; }
116 static inline void ksft_inc_error_cnt(void) { ksft_cnt.ksft_error++; }
118 static inline int ksft_get_pass_cnt(void) { return ksft_cnt.ksft_pass; }
119 static inline int ksft_get_fail_cnt(void) { return ksft_cnt.ksft_fail; }
120 static inline int ksft_get_xfail_cnt(void) { return ksft_cnt.ksft_xfail; }
121 static inline int ksft_get_xpass_cnt(void) { return ksft_cnt.ksft_xpass; }
122 static inline int ksft_get_xskip_cnt(void) { return ksft_cnt.ksft_xskip; }
123 static inline int ksft_get_error_cnt(void) { return ksft_cnt.ksft_error; }
125 static inline void ksft_print_header(void)
128 * Force line buffering; If stdout is not connected to a terminal, it
129 * will otherwise default to fully buffered, which can cause output
130 * duplication if there is content in the buffer when fork()ing. If
131 * there is a crash, line buffering also means the most recent output
132 * line will be visible.
134 setvbuf(stdout, NULL, _IOLBF, 0);
136 if (!(getenv("KSFT_TAP_LEVEL")))
137 printf("TAP version 13\n");
140 static inline void ksft_set_plan(unsigned int plan)
143 printf("1..%u\n", ksft_plan);
146 static inline void ksft_print_cnts(void)
148 if (ksft_plan != ksft_test_num())
149 printf("# Planned tests != run tests (%u != %u)\n",
150 ksft_plan, ksft_test_num());
151 printf("# Totals: pass:%u fail:%u xfail:%u xpass:%u skip:%u error:%u\n",
152 ksft_cnt.ksft_pass, ksft_cnt.ksft_fail,
153 ksft_cnt.ksft_xfail, ksft_cnt.ksft_xpass,
154 ksft_cnt.ksft_xskip, ksft_cnt.ksft_error);
157 static inline __printf(1, 2) void ksft_print_msg(const char *msg, ...)
159 int saved_errno = errno;
169 static inline void ksft_perror(const char *msg)
171 ksft_print_msg("%s: %s (%d)\n", msg, strerror(errno), errno);
174 static inline __printf(1, 2) void ksft_test_result_pass(const char *msg, ...)
176 int saved_errno = errno;
179 ksft_cnt.ksft_pass++;
182 printf("ok %u ", ksft_test_num());
188 static inline __printf(1, 2) void ksft_test_result_fail(const char *msg, ...)
190 int saved_errno = errno;
193 ksft_cnt.ksft_fail++;
196 printf("not ok %u ", ksft_test_num());
203 * ksft_test_result() - Report test success based on truth of condition
205 * @condition: if true, report test success, otherwise failure.
207 #define ksft_test_result(condition, fmt, ...) do { \
209 ksft_test_result_pass(fmt, ##__VA_ARGS__);\
211 ksft_test_result_fail(fmt, ##__VA_ARGS__);\
214 static inline __printf(1, 2) void ksft_test_result_xfail(const char *msg, ...)
216 int saved_errno = errno;
219 ksft_cnt.ksft_xfail++;
222 printf("ok %u # XFAIL ", ksft_test_num());
228 static inline __printf(1, 2) void ksft_test_result_skip(const char *msg, ...)
230 int saved_errno = errno;
233 ksft_cnt.ksft_xskip++;
236 printf("ok %u # SKIP ", ksft_test_num());
242 /* TODO: how does "error" differ from "fail" or "skip"? */
243 static inline __printf(1, 2) void ksft_test_result_error(const char *msg, ...)
245 int saved_errno = errno;
248 ksft_cnt.ksft_error++;
251 printf("not ok %u # error ", ksft_test_num());
257 static inline __printf(3, 4)
258 void ksft_test_result_code(int exit_code, const char *test_name,
259 const char *msg, ...)
261 const char *tap_code = "ok";
262 const char *directive = "";
263 int saved_errno = errno;
268 ksft_cnt.ksft_pass++;
271 directive = " # XFAIL ";
272 ksft_cnt.ksft_xfail++;
275 directive = " # XPASS ";
276 ksft_cnt.ksft_xpass++;
279 directive = " # SKIP ";
280 ksft_cnt.ksft_xskip++;
285 ksft_cnt.ksft_fail++;
289 /* Docs seem to call for double space if directive is absent */
290 if (!directive[0] && msg)
293 printf("%s %u %s%s", tap_code, ksft_test_num(), test_name, directive);
304 * ksft_test_result() - Report test success based on truth of condition
306 * @condition: if true, report test success, otherwise failure.
308 #define ksft_test_result_report(result, fmt, ...) do { \
311 ksft_test_result_pass(fmt, ##__VA_ARGS__); \
314 ksft_test_result_fail(fmt, ##__VA_ARGS__); \
317 ksft_test_result_xfail(fmt, ##__VA_ARGS__); \
320 ksft_test_result_skip(fmt, ##__VA_ARGS__); \
324 static inline __noreturn void ksft_exit_pass(void)
330 static inline __noreturn void ksft_exit_fail(void)
337 * ksft_exit() - Exit selftest based on truth of condition
339 * @condition: if true, exit self test with success, otherwise fail.
341 #define ksft_exit(condition) do { \
349 * ksft_finished() - Exit selftest with success if all tests passed
351 #define ksft_finished() \
352 ksft_exit(ksft_plan == \
353 ksft_cnt.ksft_pass + \
354 ksft_cnt.ksft_xfail + \
357 static inline __noreturn __printf(1, 2) void ksft_exit_fail_msg(const char *msg, ...)
359 int saved_errno = errno;
363 printf("Bail out! ");
372 static inline __noreturn void ksft_exit_fail_perror(const char *msg)
375 ksft_exit_fail_msg("%s: %s (%d)\n", msg, strerror(errno), errno);
378 * nolibc doesn't provide strerror() and it seems
379 * inappropriate to add one, just print the errno.
381 ksft_exit_fail_msg("%s: %d)\n", msg, errno);
385 static inline __noreturn void ksft_exit_xfail(void)
391 static inline __noreturn void ksft_exit_xpass(void)
397 static inline __noreturn __printf(1, 2) void ksft_exit_skip(const char *msg, ...)
399 int saved_errno = errno;
405 * FIXME: several tests misuse ksft_exit_skip so produce
406 * something sensible if some tests have already been run
407 * or a plan has been printed. Those tests should use
408 * ksft_test_result_skip or ksft_exit_fail_msg instead.
410 if (ksft_plan || ksft_test_num()) {
411 ksft_cnt.ksft_xskip++;
412 printf("ok %d # SKIP ", 1 + ksft_test_num());
414 printf("1..0 # SKIP ");
426 static inline int ksft_min_kernel_version(unsigned int min_major,
427 unsigned int min_minor)
430 ksft_print_msg("NOLIBC: Can't check kernel version: Function not implemented\n");
433 unsigned int major, minor;
436 if (uname(&info) || sscanf(info.release, "%u.%u.", &major, &minor) != 2)
437 ksft_exit_fail_msg("Can't parse kernel version\n");
439 return major > min_major || (major == min_major && minor >= min_minor);
443 #endif /* __KSELFTEST_H */