xref: /linux/tools/testing/selftests/kselftest.h (revision 24168c5e6dfbdd5b414f048f47f75d64533296ca)
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 /*
65  * gcc cpuid.h provides __cpuid_count() since v4.4.
66  * Clang/LLVM cpuid.h provides  __cpuid_count() since v3.4.0.
67  *
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().
71  */
72 #ifndef __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))
77 #endif
78 
79 /* define kselftest exit codes */
80 #define KSFT_PASS  0
81 #define KSFT_FAIL  1
82 #define KSFT_XFAIL 2
83 #define KSFT_XPASS 3
84 #define KSFT_SKIP  4
85 
86 #ifndef __noreturn
87 #define __noreturn       __attribute__((__noreturn__))
88 #endif
89 #define __printf(a, b)   __attribute__((format(printf, a, b)))
90 
91 /* counters */
92 struct ksft_count {
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;
99 };
100 
101 static struct ksft_count ksft_cnt;
102 static unsigned int ksft_plan;
103 
104 static inline unsigned int ksft_test_num(void)
105 {
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;
109 }
110 
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++; }
117 
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; }
124 
125 static inline void ksft_print_header(void)
126 {
127 	/*
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.
133 	 */
134 	setvbuf(stdout, NULL, _IOLBF, 0);
135 
136 	if (!(getenv("KSFT_TAP_LEVEL")))
137 		printf("TAP version 13\n");
138 }
139 
140 static inline void ksft_set_plan(unsigned int plan)
141 {
142 	ksft_plan = plan;
143 	printf("1..%u\n", ksft_plan);
144 }
145 
146 static inline void ksft_print_cnts(void)
147 {
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);
155 }
156 
157 static inline __printf(1, 2) void ksft_print_msg(const char *msg, ...)
158 {
159 	int saved_errno = errno;
160 	va_list args;
161 
162 	va_start(args, msg);
163 	printf("# ");
164 	errno = saved_errno;
165 	vprintf(msg, args);
166 	va_end(args);
167 }
168 
169 static inline void ksft_perror(const char *msg)
170 {
171 #ifndef NOLIBC
172 	ksft_print_msg("%s: %s (%d)\n", msg, strerror(errno), errno);
173 #else
174 	/*
175 	 * nolibc doesn't provide strerror() and it seems
176 	 * inappropriate to add one, just print the errno.
177 	 */
178 	ksft_print_msg("%s: %d)\n", msg, errno);
179 #endif
180 }
181 
182 static inline __printf(1, 2) void ksft_test_result_pass(const char *msg, ...)
183 {
184 	int saved_errno = errno;
185 	va_list args;
186 
187 	ksft_cnt.ksft_pass++;
188 
189 	va_start(args, msg);
190 	printf("ok %u ", ksft_test_num());
191 	errno = saved_errno;
192 	vprintf(msg, args);
193 	va_end(args);
194 }
195 
196 static inline __printf(1, 2) void ksft_test_result_fail(const char *msg, ...)
197 {
198 	int saved_errno = errno;
199 	va_list args;
200 
201 	ksft_cnt.ksft_fail++;
202 
203 	va_start(args, msg);
204 	printf("not ok %u ", ksft_test_num());
205 	errno = saved_errno;
206 	vprintf(msg, args);
207 	va_end(args);
208 }
209 
210 /**
211  * ksft_test_result() - Report test success based on truth of condition
212  *
213  * @condition: if true, report test success, otherwise failure.
214  */
215 #define ksft_test_result(condition, fmt, ...) do {	\
216 	if (!!(condition))				\
217 		ksft_test_result_pass(fmt, ##__VA_ARGS__);\
218 	else						\
219 		ksft_test_result_fail(fmt, ##__VA_ARGS__);\
220 	} while (0)
221 
222 static inline __printf(1, 2) void ksft_test_result_xfail(const char *msg, ...)
223 {
224 	int saved_errno = errno;
225 	va_list args;
226 
227 	ksft_cnt.ksft_xfail++;
228 
229 	va_start(args, msg);
230 	printf("ok %u # XFAIL ", ksft_test_num());
231 	errno = saved_errno;
232 	vprintf(msg, args);
233 	va_end(args);
234 }
235 
236 static inline __printf(1, 2) void ksft_test_result_skip(const char *msg, ...)
237 {
238 	int saved_errno = errno;
239 	va_list args;
240 
241 	ksft_cnt.ksft_xskip++;
242 
243 	va_start(args, msg);
244 	printf("ok %u # SKIP ", ksft_test_num());
245 	errno = saved_errno;
246 	vprintf(msg, args);
247 	va_end(args);
248 }
249 
250 /* TODO: how does "error" differ from "fail" or "skip"? */
251 static inline __printf(1, 2) void ksft_test_result_error(const char *msg, ...)
252 {
253 	int saved_errno = errno;
254 	va_list args;
255 
256 	ksft_cnt.ksft_error++;
257 
258 	va_start(args, msg);
259 	printf("not ok %u # error ", ksft_test_num());
260 	errno = saved_errno;
261 	vprintf(msg, args);
262 	va_end(args);
263 }
264 
265 static inline __printf(3, 4)
266 void ksft_test_result_code(int exit_code, const char *test_name,
267 			   const char *msg, ...)
268 {
269 	const char *tap_code = "ok";
270 	const char *directive = "";
271 	int saved_errno = errno;
272 	va_list args;
273 
274 	switch (exit_code) {
275 	case KSFT_PASS:
276 		ksft_cnt.ksft_pass++;
277 		break;
278 	case KSFT_XFAIL:
279 		directive = " # XFAIL ";
280 		ksft_cnt.ksft_xfail++;
281 		break;
282 	case KSFT_XPASS:
283 		directive = " # XPASS ";
284 		ksft_cnt.ksft_xpass++;
285 		break;
286 	case KSFT_SKIP:
287 		directive = " # SKIP ";
288 		ksft_cnt.ksft_xskip++;
289 		break;
290 	case KSFT_FAIL:
291 	default:
292 		tap_code = "not ok";
293 		ksft_cnt.ksft_fail++;
294 		break;
295 	}
296 
297 	/* Docs seem to call for double space if directive is absent */
298 	if (!directive[0] && msg)
299 		directive = " #  ";
300 
301 	printf("%s %u %s%s", tap_code, ksft_test_num(), test_name, directive);
302 	errno = saved_errno;
303 	if (msg) {
304 		va_start(args, msg);
305 		vprintf(msg, args);
306 		va_end(args);
307 	}
308 	printf("\n");
309 }
310 
311 /**
312  * ksft_test_result() - Report test success based on truth of condition
313  *
314  * @condition: if true, report test success, otherwise failure.
315  */
316 #define ksft_test_result_report(result, fmt, ...) do {		\
317 	switch (result) {					\
318 	case KSFT_PASS:						\
319 		ksft_test_result_pass(fmt, ##__VA_ARGS__);	\
320 		break;						\
321 	case KSFT_FAIL:						\
322 		ksft_test_result_fail(fmt, ##__VA_ARGS__);	\
323 		break;						\
324 	case KSFT_XFAIL:					\
325 		ksft_test_result_xfail(fmt, ##__VA_ARGS__);	\
326 		break;						\
327 	case KSFT_SKIP:						\
328 		ksft_test_result_skip(fmt, ##__VA_ARGS__);	\
329 		break;						\
330 	} } while (0)
331 
332 static inline __noreturn void ksft_exit_pass(void)
333 {
334 	ksft_print_cnts();
335 	exit(KSFT_PASS);
336 }
337 
338 static inline __noreturn void ksft_exit_fail(void)
339 {
340 	ksft_print_cnts();
341 	exit(KSFT_FAIL);
342 }
343 
344 /**
345  * ksft_exit() - Exit selftest based on truth of condition
346  *
347  * @condition: if true, exit self test with success, otherwise fail.
348  */
349 #define ksft_exit(condition) do {	\
350 	if (!!(condition))		\
351 		ksft_exit_pass();	\
352 	else				\
353 		ksft_exit_fail();	\
354 	} while (0)
355 
356 /**
357  * ksft_finished() - Exit selftest with success if all tests passed
358  */
359 #define ksft_finished()			\
360 	ksft_exit(ksft_plan ==		\
361 		  ksft_cnt.ksft_pass +	\
362 		  ksft_cnt.ksft_xfail +	\
363 		  ksft_cnt.ksft_xskip)
364 
365 static inline __noreturn __printf(1, 2) void ksft_exit_fail_msg(const char *msg, ...)
366 {
367 	int saved_errno = errno;
368 	va_list args;
369 
370 	va_start(args, msg);
371 	printf("Bail out! ");
372 	errno = saved_errno;
373 	vprintf(msg, args);
374 	va_end(args);
375 
376 	ksft_print_cnts();
377 	exit(KSFT_FAIL);
378 }
379 
380 static inline __noreturn void ksft_exit_fail_perror(const char *msg)
381 {
382 #ifndef NOLIBC
383 	ksft_exit_fail_msg("%s: %s (%d)\n", msg, strerror(errno), errno);
384 #else
385 	/*
386 	 * nolibc doesn't provide strerror() and it seems
387 	 * inappropriate to add one, just print the errno.
388 	 */
389 	ksft_exit_fail_msg("%s: %d)\n", msg, errno);
390 #endif
391 }
392 
393 static inline __noreturn void ksft_exit_xfail(void)
394 {
395 	ksft_print_cnts();
396 	exit(KSFT_XFAIL);
397 }
398 
399 static inline __noreturn void ksft_exit_xpass(void)
400 {
401 	ksft_print_cnts();
402 	exit(KSFT_XPASS);
403 }
404 
405 static inline __noreturn __printf(1, 2) void ksft_exit_skip(const char *msg, ...)
406 {
407 	int saved_errno = errno;
408 	va_list args;
409 
410 	va_start(args, msg);
411 
412 	/*
413 	 * FIXME: several tests misuse ksft_exit_skip so produce
414 	 * something sensible if some tests have already been run
415 	 * or a plan has been printed.  Those tests should use
416 	 * ksft_test_result_skip or ksft_exit_fail_msg instead.
417 	 */
418 	if (ksft_plan || ksft_test_num()) {
419 		ksft_cnt.ksft_xskip++;
420 		printf("ok %d # SKIP ", 1 + ksft_test_num());
421 	} else {
422 		printf("1..0 # SKIP ");
423 	}
424 	if (msg) {
425 		errno = saved_errno;
426 		vprintf(msg, args);
427 		va_end(args);
428 	}
429 	if (ksft_test_num())
430 		ksft_print_cnts();
431 	exit(KSFT_SKIP);
432 }
433 
434 static inline int ksft_min_kernel_version(unsigned int min_major,
435 					  unsigned int min_minor)
436 {
437 #ifdef NOLIBC
438 	ksft_print_msg("NOLIBC: Can't check kernel version: Function not implemented\n");
439 	return 0;
440 #else
441 	unsigned int major, minor;
442 	struct utsname info;
443 
444 	if (uname(&info) || sscanf(info.release, "%u.%u.", &major, &minor) != 2)
445 		ksft_exit_fail_msg("Can't parse kernel version\n");
446 
447 	return major > min_major || (major == min_major && minor >= min_minor);
448 #endif
449 }
450 
451 #endif /* __KSELFTEST_H */
452