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