selftests/timers/posix_timers: Reimplement check_timer_distribution()
[sfrench/cifs-2.6.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 <shuahkh@osg.samsung.com>
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  *
20  * and finally report the pass/fail/skip/xfail state of the test with one of:
21  *
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, ...);
29  *
30  * When all tests are finished, clean up and exit the program with one of:
31  *
32  *    ksft_finished();
33  *    ksft_exit(condition);
34  *    ksft_exit_pass();
35  *    ksft_exit_fail();
36  *
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):
40  *
41  *    ksft_exit_fail_msg(fmt, ...);
42  *
43  */
44 #ifndef __KSELFTEST_H
45 #define __KSELFTEST_H
46
47 #ifndef NOLIBC
48 #include <errno.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <stdarg.h>
52 #include <string.h>
53 #include <stdio.h>
54 #include <sys/utsname.h>
55 #endif
56
57 #ifndef ARRAY_SIZE
58 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
59 #endif
60
61 /*
62  * gcc cpuid.h provides __cpuid_count() since v4.4.
63  * Clang/LLVM cpuid.h provides  __cpuid_count() since v3.4.0.
64  *
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().
68  */
69 #ifndef __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))
74 #endif
75
76 /* define kselftest exit codes */
77 #define KSFT_PASS  0
78 #define KSFT_FAIL  1
79 #define KSFT_XFAIL 2
80 #define KSFT_XPASS 3
81 #define KSFT_SKIP  4
82
83 #define __printf(a, b)   __attribute__((format(printf, a, b)))
84
85 /* counters */
86 struct ksft_count {
87         unsigned int ksft_pass;
88         unsigned int ksft_fail;
89         unsigned int ksft_xfail;
90         unsigned int ksft_xpass;
91         unsigned int ksft_xskip;
92         unsigned int ksft_error;
93 };
94
95 static struct ksft_count ksft_cnt;
96 static unsigned int ksft_plan;
97
98 static inline unsigned int ksft_test_num(void)
99 {
100         return ksft_cnt.ksft_pass + ksft_cnt.ksft_fail +
101                 ksft_cnt.ksft_xfail + ksft_cnt.ksft_xpass +
102                 ksft_cnt.ksft_xskip + ksft_cnt.ksft_error;
103 }
104
105 static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; }
106 static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; }
107 static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; }
108 static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; }
109 static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; }
110 static inline void ksft_inc_error_cnt(void) { ksft_cnt.ksft_error++; }
111
112 static inline int ksft_get_pass_cnt(void) { return ksft_cnt.ksft_pass; }
113 static inline int ksft_get_fail_cnt(void) { return ksft_cnt.ksft_fail; }
114 static inline int ksft_get_xfail_cnt(void) { return ksft_cnt.ksft_xfail; }
115 static inline int ksft_get_xpass_cnt(void) { return ksft_cnt.ksft_xpass; }
116 static inline int ksft_get_xskip_cnt(void) { return ksft_cnt.ksft_xskip; }
117 static inline int ksft_get_error_cnt(void) { return ksft_cnt.ksft_error; }
118
119 static inline void ksft_print_header(void)
120 {
121         /*
122          * Force line buffering; If stdout is not connected to a terminal, it
123          * will otherwise default to fully buffered, which can cause output
124          * duplication if there is content in the buffer when fork()ing. If
125          * there is a crash, line buffering also means the most recent output
126          * line will be visible.
127          */
128         setvbuf(stdout, NULL, _IOLBF, 0);
129
130         if (!(getenv("KSFT_TAP_LEVEL")))
131                 printf("TAP version 13\n");
132 }
133
134 static inline void ksft_set_plan(unsigned int plan)
135 {
136         ksft_plan = plan;
137         printf("1..%u\n", ksft_plan);
138 }
139
140 static inline void ksft_print_cnts(void)
141 {
142         if (ksft_plan != ksft_test_num())
143                 printf("# Planned tests != run tests (%u != %u)\n",
144                         ksft_plan, ksft_test_num());
145         printf("# Totals: pass:%u fail:%u xfail:%u xpass:%u skip:%u error:%u\n",
146                 ksft_cnt.ksft_pass, ksft_cnt.ksft_fail,
147                 ksft_cnt.ksft_xfail, ksft_cnt.ksft_xpass,
148                 ksft_cnt.ksft_xskip, ksft_cnt.ksft_error);
149 }
150
151 static inline __printf(1, 2) void ksft_print_msg(const char *msg, ...)
152 {
153         int saved_errno = errno;
154         va_list args;
155
156         va_start(args, msg);
157         printf("# ");
158         errno = saved_errno;
159         vprintf(msg, args);
160         va_end(args);
161 }
162
163 static inline void ksft_perror(const char *msg)
164 {
165 #ifndef NOLIBC
166         ksft_print_msg("%s: %s (%d)\n", msg, strerror(errno), errno);
167 #else
168         /*
169          * nolibc doesn't provide strerror() and it seems
170          * inappropriate to add one, just print the errno.
171          */
172         ksft_print_msg("%s: %d)\n", msg, errno);
173 #endif
174 }
175
176 static inline __printf(1, 2) void ksft_test_result_pass(const char *msg, ...)
177 {
178         int saved_errno = errno;
179         va_list args;
180
181         ksft_cnt.ksft_pass++;
182
183         va_start(args, msg);
184         printf("ok %u ", ksft_test_num());
185         errno = saved_errno;
186         vprintf(msg, args);
187         va_end(args);
188 }
189
190 static inline __printf(1, 2) void ksft_test_result_fail(const char *msg, ...)
191 {
192         int saved_errno = errno;
193         va_list args;
194
195         ksft_cnt.ksft_fail++;
196
197         va_start(args, msg);
198         printf("not ok %u ", ksft_test_num());
199         errno = saved_errno;
200         vprintf(msg, args);
201         va_end(args);
202 }
203
204 /**
205  * ksft_test_result() - Report test success based on truth of condition
206  *
207  * @condition: if true, report test success, otherwise failure.
208  */
209 #define ksft_test_result(condition, fmt, ...) do {      \
210         if (!!(condition))                              \
211                 ksft_test_result_pass(fmt, ##__VA_ARGS__);\
212         else                                            \
213                 ksft_test_result_fail(fmt, ##__VA_ARGS__);\
214         } while (0)
215
216 static inline __printf(1, 2) void ksft_test_result_xfail(const char *msg, ...)
217 {
218         int saved_errno = errno;
219         va_list args;
220
221         ksft_cnt.ksft_xfail++;
222
223         va_start(args, msg);
224         printf("ok %u # XFAIL ", ksft_test_num());
225         errno = saved_errno;
226         vprintf(msg, args);
227         va_end(args);
228 }
229
230 static inline __printf(1, 2) void ksft_test_result_skip(const char *msg, ...)
231 {
232         int saved_errno = errno;
233         va_list args;
234
235         ksft_cnt.ksft_xskip++;
236
237         va_start(args, msg);
238         printf("ok %u # SKIP ", ksft_test_num());
239         errno = saved_errno;
240         vprintf(msg, args);
241         va_end(args);
242 }
243
244 /* TODO: how does "error" differ from "fail" or "skip"? */
245 static inline __printf(1, 2) void ksft_test_result_error(const char *msg, ...)
246 {
247         int saved_errno = errno;
248         va_list args;
249
250         ksft_cnt.ksft_error++;
251
252         va_start(args, msg);
253         printf("not ok %u # error ", ksft_test_num());
254         errno = saved_errno;
255         vprintf(msg, args);
256         va_end(args);
257 }
258
259 static inline __printf(3, 4)
260 void ksft_test_result_code(int exit_code, const char *test_name,
261                            const char *msg, ...)
262 {
263         const char *tap_code = "ok";
264         const char *directive = "";
265         int saved_errno = errno;
266         va_list args;
267
268         switch (exit_code) {
269         case KSFT_PASS:
270                 ksft_cnt.ksft_pass++;
271                 break;
272         case KSFT_XFAIL:
273                 directive = " # XFAIL ";
274                 ksft_cnt.ksft_xfail++;
275                 break;
276         case KSFT_XPASS:
277                 directive = " # XPASS ";
278                 ksft_cnt.ksft_xpass++;
279                 break;
280         case KSFT_SKIP:
281                 directive = " # SKIP ";
282                 ksft_cnt.ksft_xskip++;
283                 break;
284         case KSFT_FAIL:
285         default:
286                 tap_code = "not ok";
287                 ksft_cnt.ksft_fail++;
288                 break;
289         }
290
291         /* Docs seem to call for double space if directive is absent */
292         if (!directive[0] && msg[0])
293                 directive = " #  ";
294
295         va_start(args, msg);
296         printf("%s %u %s%s", tap_code, ksft_test_num(), test_name, directive);
297         errno = saved_errno;
298         vprintf(msg, args);
299         printf("\n");
300         va_end(args);
301 }
302
303 static inline int ksft_exit_pass(void)
304 {
305         ksft_print_cnts();
306         exit(KSFT_PASS);
307 }
308
309 static inline int ksft_exit_fail(void)
310 {
311         ksft_print_cnts();
312         exit(KSFT_FAIL);
313 }
314
315 /**
316  * ksft_exit() - Exit selftest based on truth of condition
317  *
318  * @condition: if true, exit self test with success, otherwise fail.
319  */
320 #define ksft_exit(condition) do {       \
321         if (!!(condition))              \
322                 ksft_exit_pass();       \
323         else                            \
324                 ksft_exit_fail();       \
325         } while (0)
326
327 /**
328  * ksft_finished() - Exit selftest with success if all tests passed
329  */
330 #define ksft_finished()                 \
331         ksft_exit(ksft_plan ==          \
332                   ksft_cnt.ksft_pass +  \
333                   ksft_cnt.ksft_xfail + \
334                   ksft_cnt.ksft_xskip)
335
336 static inline __printf(1, 2) int ksft_exit_fail_msg(const char *msg, ...)
337 {
338         int saved_errno = errno;
339         va_list args;
340
341         va_start(args, msg);
342         printf("Bail out! ");
343         errno = saved_errno;
344         vprintf(msg, args);
345         va_end(args);
346
347         ksft_print_cnts();
348         exit(KSFT_FAIL);
349 }
350
351 static inline int ksft_exit_xfail(void)
352 {
353         ksft_print_cnts();
354         exit(KSFT_XFAIL);
355 }
356
357 static inline int ksft_exit_xpass(void)
358 {
359         ksft_print_cnts();
360         exit(KSFT_XPASS);
361 }
362
363 static inline __printf(1, 2) int ksft_exit_skip(const char *msg, ...)
364 {
365         int saved_errno = errno;
366         va_list args;
367
368         va_start(args, msg);
369
370         /*
371          * FIXME: several tests misuse ksft_exit_skip so produce
372          * something sensible if some tests have already been run
373          * or a plan has been printed.  Those tests should use
374          * ksft_test_result_skip or ksft_exit_fail_msg instead.
375          */
376         if (ksft_plan || ksft_test_num()) {
377                 ksft_cnt.ksft_xskip++;
378                 printf("ok %d # SKIP ", 1 + ksft_test_num());
379         } else {
380                 printf("1..0 # SKIP ");
381         }
382         if (msg) {
383                 errno = saved_errno;
384                 vprintf(msg, args);
385                 va_end(args);
386         }
387         if (ksft_test_num())
388                 ksft_print_cnts();
389         exit(KSFT_SKIP);
390 }
391
392 static inline int ksft_min_kernel_version(unsigned int min_major,
393                                           unsigned int min_minor)
394 {
395         unsigned int major, minor;
396         struct utsname info;
397
398         if (uname(&info) || sscanf(info.release, "%u.%u.", &major, &minor) != 2)
399                 ksft_exit_fail_msg("Can't parse kernel version\n");
400
401         return major > min_major || (major == min_major && minor >= min_minor);
402 }
403
404 #endif /* __KSELFTEST_H */