]> Git Repo - linux.git/blob - tools/testing/selftests/kselftest.h
Linux 6.14-rc3
[linux.git] / tools / testing / selftests / kselftest.h
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  *     ksft_perror(msg);
20  *
21  * and finally report the pass/fail/skip/xfail/xpass state of the test
22  * with one of:
23  *
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, ...);
33  *
34  * When all tests are finished, clean up and exit the program with one of:
35  *
36  *    ksft_finished();
37  *    ksft_exit(condition);
38  *    ksft_exit_pass();
39  *    ksft_exit_fail();
40  *
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):
44  *
45  *    ksft_exit_fail_msg(fmt, ...);
46  *    ksft_exit_fail_perror(msg);
47  *
48  */
49 #ifndef __KSELFTEST_H
50 #define __KSELFTEST_H
51
52 #ifndef NOLIBC
53 #include <errno.h>
54 #include <stdlib.h>
55 #include <unistd.h>
56 #include <stdarg.h>
57 #include <string.h>
58 #include <stdio.h>
59 #include <sys/utsname.h>
60 #endif
61
62 #ifndef ARRAY_SIZE
63 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
64 #endif
65
66 #if defined(__i386__) || defined(__x86_64__) /* arch */
67 /*
68  * gcc cpuid.h provides __cpuid_count() since v4.4.
69  * Clang/LLVM cpuid.h provides  __cpuid_count() since v3.4.0.
70  *
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().
74  */
75 #ifndef __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))
80 #endif
81 #endif /* end arch */
82
83 /* define kselftest exit codes */
84 #define KSFT_PASS  0
85 #define KSFT_FAIL  1
86 #define KSFT_XFAIL 2
87 #define KSFT_XPASS 3
88 #define KSFT_SKIP  4
89
90 #ifndef __noreturn
91 #define __noreturn       __attribute__((__noreturn__))
92 #endif
93 #define __printf(a, b)   __attribute__((format(printf, a, b)))
94
95 /* counters */
96 struct ksft_count {
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;
103 };
104
105 static struct ksft_count ksft_cnt;
106 static unsigned int ksft_plan;
107
108 static inline unsigned int ksft_test_num(void)
109 {
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;
113 }
114
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++; }
121
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; }
128
129 static inline void ksft_print_header(void)
130 {
131         /*
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.
137          */
138         setvbuf(stdout, NULL, _IOLBF, 0);
139
140         if (!(getenv("KSFT_TAP_LEVEL")))
141                 printf("TAP version 13\n");
142 }
143
144 static inline void ksft_set_plan(unsigned int plan)
145 {
146         ksft_plan = plan;
147         printf("1..%u\n", ksft_plan);
148 }
149
150 static inline void ksft_print_cnts(void)
151 {
152         if (ksft_cnt.ksft_xskip > 0)
153                 printf(
154                         "# %u skipped test(s) detected. Consider enabling relevant config options to improve coverage.\n",
155                         ksft_cnt.ksft_xskip
156                 );
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);
164 }
165
166 static inline __printf(1, 2) void ksft_print_msg(const char *msg, ...)
167 {
168         int saved_errno = errno;
169         va_list args;
170
171         va_start(args, msg);
172         printf("# ");
173         errno = saved_errno;
174         vprintf(msg, args);
175         va_end(args);
176 }
177
178 static inline void ksft_perror(const char *msg)
179 {
180         ksft_print_msg("%s: %s (%d)\n", msg, strerror(errno), errno);
181 }
182
183 static inline __printf(1, 2) void ksft_test_result_pass(const char *msg, ...)
184 {
185         int saved_errno = errno;
186         va_list args;
187
188         ksft_cnt.ksft_pass++;
189
190         va_start(args, msg);
191         printf("ok %u ", ksft_test_num());
192         errno = saved_errno;
193         vprintf(msg, args);
194         va_end(args);
195 }
196
197 static inline __printf(1, 2) void ksft_test_result_fail(const char *msg, ...)
198 {
199         int saved_errno = errno;
200         va_list args;
201
202         ksft_cnt.ksft_fail++;
203
204         va_start(args, msg);
205         printf("not ok %u ", ksft_test_num());
206         errno = saved_errno;
207         vprintf(msg, args);
208         va_end(args);
209 }
210
211 /**
212  * ksft_test_result() - Report test success based on truth of condition
213  *
214  * @condition: if true, report test success, otherwise failure.
215  */
216 #define ksft_test_result(condition, fmt, ...) do {      \
217         if (!!(condition))                              \
218                 ksft_test_result_pass(fmt, ##__VA_ARGS__);\
219         else                                            \
220                 ksft_test_result_fail(fmt, ##__VA_ARGS__);\
221         } while (0)
222
223 static inline __printf(1, 2) void ksft_test_result_xfail(const char *msg, ...)
224 {
225         int saved_errno = errno;
226         va_list args;
227
228         ksft_cnt.ksft_xfail++;
229
230         va_start(args, msg);
231         printf("ok %u # XFAIL ", ksft_test_num());
232         errno = saved_errno;
233         vprintf(msg, args);
234         va_end(args);
235 }
236
237 static inline __printf(1, 2) void ksft_test_result_xpass(const char *msg, ...)
238 {
239         int saved_errno = errno;
240         va_list args;
241
242         ksft_cnt.ksft_xpass++;
243
244         va_start(args, msg);
245         printf("ok %u # XPASS ", ksft_test_num());
246         errno = saved_errno;
247         vprintf(msg, args);
248         va_end(args);
249 }
250
251 static inline __printf(1, 2) void ksft_test_result_skip(const char *msg, ...)
252 {
253         int saved_errno = errno;
254         va_list args;
255
256         ksft_cnt.ksft_xskip++;
257
258         va_start(args, msg);
259         printf("ok %u # SKIP ", ksft_test_num());
260         errno = saved_errno;
261         vprintf(msg, args);
262         va_end(args);
263 }
264
265 /* TODO: how does "error" differ from "fail" or "skip"? */
266 static inline __printf(1, 2) void ksft_test_result_error(const char *msg, ...)
267 {
268         int saved_errno = errno;
269         va_list args;
270
271         ksft_cnt.ksft_error++;
272
273         va_start(args, msg);
274         printf("not ok %u # error ", ksft_test_num());
275         errno = saved_errno;
276         vprintf(msg, args);
277         va_end(args);
278 }
279
280 static inline __printf(3, 4)
281 void ksft_test_result_code(int exit_code, const char *test_name,
282                            const char *msg, ...)
283 {
284         const char *tap_code = "ok";
285         const char *directive = "";
286         int saved_errno = errno;
287         va_list args;
288
289         switch (exit_code) {
290         case KSFT_PASS:
291                 ksft_cnt.ksft_pass++;
292                 break;
293         case KSFT_XFAIL:
294                 directive = " # XFAIL ";
295                 ksft_cnt.ksft_xfail++;
296                 break;
297         case KSFT_XPASS:
298                 directive = " # XPASS ";
299                 ksft_cnt.ksft_xpass++;
300                 break;
301         case KSFT_SKIP:
302                 directive = " # SKIP ";
303                 ksft_cnt.ksft_xskip++;
304                 break;
305         case KSFT_FAIL:
306         default:
307                 tap_code = "not ok";
308                 ksft_cnt.ksft_fail++;
309                 break;
310         }
311
312         /* Docs seem to call for double space if directive is absent */
313         if (!directive[0] && msg)
314                 directive = " #  ";
315
316         printf("%s %u %s%s", tap_code, ksft_test_num(), test_name, directive);
317         errno = saved_errno;
318         if (msg) {
319                 va_start(args, msg);
320                 vprintf(msg, args);
321                 va_end(args);
322         }
323         printf("\n");
324 }
325
326 /**
327  * ksft_test_result() - Report test success based on truth of condition
328  *
329  * @condition: if true, report test success, otherwise failure.
330  */
331 #define ksft_test_result_report(result, fmt, ...) do {          \
332         switch (result) {                                       \
333         case KSFT_PASS:                                         \
334                 ksft_test_result_pass(fmt, ##__VA_ARGS__);      \
335                 break;                                          \
336         case KSFT_FAIL:                                         \
337                 ksft_test_result_fail(fmt, ##__VA_ARGS__);      \
338                 break;                                          \
339         case KSFT_XFAIL:                                        \
340                 ksft_test_result_xfail(fmt, ##__VA_ARGS__);     \
341                 break;                                          \
342         case KSFT_XPASS:                                        \
343                 ksft_test_result_xpass(fmt, ##__VA_ARGS__);     \
344                 break;                                          \
345         case KSFT_SKIP:                                         \
346                 ksft_test_result_skip(fmt, ##__VA_ARGS__);      \
347                 break;                                          \
348         } } while (0)
349
350 static inline __noreturn void ksft_exit_pass(void)
351 {
352         ksft_print_cnts();
353         exit(KSFT_PASS);
354 }
355
356 static inline __noreturn void ksft_exit_fail(void)
357 {
358         ksft_print_cnts();
359         exit(KSFT_FAIL);
360 }
361
362 /**
363  * ksft_exit() - Exit selftest based on truth of condition
364  *
365  * @condition: if true, exit self test with success, otherwise fail.
366  */
367 #define ksft_exit(condition) do {       \
368         if (!!(condition))              \
369                 ksft_exit_pass();       \
370         else                            \
371                 ksft_exit_fail();       \
372         } while (0)
373
374 /**
375  * ksft_finished() - Exit selftest with success if all tests passed
376  */
377 #define ksft_finished()                 \
378         ksft_exit(ksft_plan ==          \
379                   ksft_cnt.ksft_pass +  \
380                   ksft_cnt.ksft_xfail + \
381                   ksft_cnt.ksft_xskip)
382
383 static inline __noreturn __printf(1, 2) void ksft_exit_fail_msg(const char *msg, ...)
384 {
385         int saved_errno = errno;
386         va_list args;
387
388         va_start(args, msg);
389         printf("Bail out! ");
390         errno = saved_errno;
391         vprintf(msg, args);
392         va_end(args);
393
394         ksft_print_cnts();
395         exit(KSFT_FAIL);
396 }
397
398 static inline __noreturn void ksft_exit_fail_perror(const char *msg)
399 {
400         ksft_exit_fail_msg("%s: %s (%d)\n", msg, strerror(errno), errno);
401 }
402
403 static inline __noreturn void ksft_exit_xfail(void)
404 {
405         ksft_print_cnts();
406         exit(KSFT_XFAIL);
407 }
408
409 static inline __noreturn void ksft_exit_xpass(void)
410 {
411         ksft_print_cnts();
412         exit(KSFT_XPASS);
413 }
414
415 static inline __noreturn __printf(1, 2) void ksft_exit_skip(const char *msg, ...)
416 {
417         int saved_errno = errno;
418         va_list args;
419
420         va_start(args, msg);
421
422         /*
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.
427          */
428         if (ksft_plan || ksft_test_num()) {
429                 ksft_cnt.ksft_xskip++;
430                 printf("ok %u # SKIP ", 1 + ksft_test_num());
431         } else {
432                 printf("1..0 # SKIP ");
433         }
434         if (msg) {
435                 errno = saved_errno;
436                 vprintf(msg, args);
437                 va_end(args);
438         }
439         if (ksft_test_num())
440                 ksft_print_cnts();
441         exit(KSFT_SKIP);
442 }
443
444 static inline int ksft_min_kernel_version(unsigned int min_major,
445                                           unsigned int min_minor)
446 {
447 #ifdef NOLIBC
448         ksft_print_msg("NOLIBC: Can't check kernel version: Function not implemented\n");
449         return 0;
450 #else
451         unsigned int major, minor;
452         struct utsname info;
453
454         if (uname(&info) || sscanf(info.release, "%u.%u.", &major, &minor) != 2)
455                 ksft_exit_fail_msg("Can't parse kernel version\n");
456
457         return major > min_major || (major == min_major && minor >= min_minor);
458 #endif
459 }
460
461 #endif /* __KSELFTEST_H */
This page took 0.054336 seconds and 4 git commands to generate.