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, ...);
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]))
64 #if defined(__i386__) || defined(__x86_64__) /* arch */
66 * gcc cpuid.h provides __cpuid_count() since v4.4.
67 * Clang/LLVM cpuid.h provides __cpuid_count() since v3.4.0.
69 * Provide local define for tests needing __cpuid_count() because
70 * selftests need to work in older environments that do not yet
71 * have __cpuid_count().
74 #define __cpuid_count(level, count, a, b, c, d) \
75 __asm__ __volatile__ ("cpuid\n\t" \
76 : "=a" (a), "=b" (b), "=c" (c), "=d" (d) \
77 : "0" (level), "2" (count))
81 /* define kselftest exit codes */
89 #define __noreturn __attribute__((__noreturn__))
91 #define __printf(a, b) __attribute__((format(printf, a, b)))
95 unsigned int ksft_pass
;
96 unsigned int ksft_fail
;
97 unsigned int ksft_xfail
;
98 unsigned int ksft_xpass
;
99 unsigned int ksft_xskip
;
100 unsigned int ksft_error
;
103 static struct ksft_count ksft_cnt
;
104 static unsigned int ksft_plan
;
106 static inline unsigned int ksft_test_num(void)
108 return ksft_cnt
.ksft_pass
+ ksft_cnt
.ksft_fail
+
109 ksft_cnt
.ksft_xfail
+ ksft_cnt
.ksft_xpass
+
110 ksft_cnt
.ksft_xskip
+ ksft_cnt
.ksft_error
;
113 static inline void ksft_inc_pass_cnt(void) { ksft_cnt
.ksft_pass
++; }
114 static inline void ksft_inc_fail_cnt(void) { ksft_cnt
.ksft_fail
++; }
115 static inline void ksft_inc_xfail_cnt(void) { ksft_cnt
.ksft_xfail
++; }
116 static inline void ksft_inc_xpass_cnt(void) { ksft_cnt
.ksft_xpass
++; }
117 static inline void ksft_inc_xskip_cnt(void) { ksft_cnt
.ksft_xskip
++; }
118 static inline void ksft_inc_error_cnt(void) { ksft_cnt
.ksft_error
++; }
120 static inline int ksft_get_pass_cnt(void) { return ksft_cnt
.ksft_pass
; }
121 static inline int ksft_get_fail_cnt(void) { return ksft_cnt
.ksft_fail
; }
122 static inline int ksft_get_xfail_cnt(void) { return ksft_cnt
.ksft_xfail
; }
123 static inline int ksft_get_xpass_cnt(void) { return ksft_cnt
.ksft_xpass
; }
124 static inline int ksft_get_xskip_cnt(void) { return ksft_cnt
.ksft_xskip
; }
125 static inline int ksft_get_error_cnt(void) { return ksft_cnt
.ksft_error
; }
127 static inline void ksft_print_header(void)
130 * Force line buffering; If stdout is not connected to a terminal, it
131 * will otherwise default to fully buffered, which can cause output
132 * duplication if there is content in the buffer when fork()ing. If
133 * there is a crash, line buffering also means the most recent output
134 * line will be visible.
136 setvbuf(stdout
, NULL
, _IOLBF
, 0);
138 if (!(getenv("KSFT_TAP_LEVEL")))
139 printf("TAP version 13\n");
142 static inline void ksft_set_plan(unsigned int plan
)
145 printf("1..%u\n", ksft_plan
);
148 static inline void ksft_print_cnts(void)
150 if (ksft_plan
!= ksft_test_num())
151 printf("# Planned tests != run tests (%u != %u)\n",
152 ksft_plan
, ksft_test_num());
153 printf("# Totals: pass:%u fail:%u xfail:%u xpass:%u skip:%u error:%u\n",
154 ksft_cnt
.ksft_pass
, ksft_cnt
.ksft_fail
,
155 ksft_cnt
.ksft_xfail
, ksft_cnt
.ksft_xpass
,
156 ksft_cnt
.ksft_xskip
, ksft_cnt
.ksft_error
);
159 static inline __printf(1, 2) void ksft_print_msg(const char *msg
, ...)
161 int saved_errno
= errno
;
171 static inline void ksft_perror(const char *msg
)
173 ksft_print_msg("%s: %s (%d)\n", msg
, strerror(errno
), errno
);
176 static inline __printf(1, 2) void ksft_test_result_pass(const char *msg
, ...)
178 int saved_errno
= errno
;
181 ksft_cnt
.ksft_pass
++;
184 printf("ok %u ", ksft_test_num());
190 static inline __printf(1, 2) void ksft_test_result_fail(const char *msg
, ...)
192 int saved_errno
= errno
;
195 ksft_cnt
.ksft_fail
++;
198 printf("not ok %u ", ksft_test_num());
205 * ksft_test_result() - Report test success based on truth of condition
207 * @condition: if true, report test success, otherwise failure.
209 #define ksft_test_result(condition, fmt, ...) do { \
211 ksft_test_result_pass(fmt, ##__VA_ARGS__);\
213 ksft_test_result_fail(fmt, ##__VA_ARGS__);\
216 static inline __printf(1, 2) void ksft_test_result_xfail(const char *msg
, ...)
218 int saved_errno
= errno
;
221 ksft_cnt
.ksft_xfail
++;
224 printf("ok %u # XFAIL ", ksft_test_num());
230 static inline __printf(1, 2) void ksft_test_result_skip(const char *msg
, ...)
232 int saved_errno
= errno
;
235 ksft_cnt
.ksft_xskip
++;
238 printf("ok %u # SKIP ", ksft_test_num());
244 /* TODO: how does "error" differ from "fail" or "skip"? */
245 static inline __printf(1, 2) void ksft_test_result_error(const char *msg
, ...)
247 int saved_errno
= errno
;
250 ksft_cnt
.ksft_error
++;
253 printf("not ok %u # error ", ksft_test_num());
259 static inline __printf(3, 4)
260 void ksft_test_result_code(int exit_code
, const char *test_name
,
261 const char *msg
, ...)
263 const char *tap_code
= "ok";
264 const char *directive
= "";
265 int saved_errno
= errno
;
270 ksft_cnt
.ksft_pass
++;
273 directive
= " # XFAIL ";
274 ksft_cnt
.ksft_xfail
++;
277 directive
= " # XPASS ";
278 ksft_cnt
.ksft_xpass
++;
281 directive
= " # SKIP ";
282 ksft_cnt
.ksft_xskip
++;
287 ksft_cnt
.ksft_fail
++;
291 /* Docs seem to call for double space if directive is absent */
292 if (!directive
[0] && msg
)
295 printf("%s %u %s%s", tap_code
, ksft_test_num(), test_name
, directive
);
306 * ksft_test_result() - Report test success based on truth of condition
308 * @condition: if true, report test success, otherwise failure.
310 #define ksft_test_result_report(result, fmt, ...) do { \
313 ksft_test_result_pass(fmt, ##__VA_ARGS__); \
316 ksft_test_result_fail(fmt, ##__VA_ARGS__); \
319 ksft_test_result_xfail(fmt, ##__VA_ARGS__); \
322 ksft_test_result_skip(fmt, ##__VA_ARGS__); \
326 static inline __noreturn
void ksft_exit_pass(void)
332 static inline __noreturn
void ksft_exit_fail(void)
339 * ksft_exit() - Exit selftest based on truth of condition
341 * @condition: if true, exit self test with success, otherwise fail.
343 #define ksft_exit(condition) do { \
351 * ksft_finished() - Exit selftest with success if all tests passed
353 #define ksft_finished() \
354 ksft_exit(ksft_plan == \
355 ksft_cnt.ksft_pass + \
356 ksft_cnt.ksft_xfail + \
359 static inline __noreturn
__printf(1, 2) void ksft_exit_fail_msg(const char *msg
, ...)
361 int saved_errno
= errno
;
365 printf("Bail out! ");
374 static inline __noreturn
void ksft_exit_fail_perror(const char *msg
)
376 ksft_exit_fail_msg("%s: %s (%d)\n", msg
, strerror(errno
), errno
);
379 static inline __noreturn
void ksft_exit_xfail(void)
385 static inline __noreturn
void ksft_exit_xpass(void)
391 static inline __noreturn
__printf(1, 2) void ksft_exit_skip(const char *msg
, ...)
393 int saved_errno
= errno
;
399 * FIXME: several tests misuse ksft_exit_skip so produce
400 * something sensible if some tests have already been run
401 * or a plan has been printed. Those tests should use
402 * ksft_test_result_skip or ksft_exit_fail_msg instead.
404 if (ksft_plan
|| ksft_test_num()) {
405 ksft_cnt
.ksft_xskip
++;
406 printf("ok %d # SKIP ", 1 + ksft_test_num());
408 printf("1..0 # SKIP ");
420 static inline int ksft_min_kernel_version(unsigned int min_major
,
421 unsigned int min_minor
)
424 ksft_print_msg("NOLIBC: Can't check kernel version: Function not implemented\n");
427 unsigned int major
, minor
;
430 if (uname(&info
) || sscanf(info
.release
, "%u.%u.", &major
, &minor
) != 2)
431 ksft_exit_fail_msg("Can't parse kernel version\n");
433 return major
> min_major
|| (major
== min_major
&& minor
>= min_minor
);
437 #endif /* __KSELFTEST_H */