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