xref: /openbmc/linux/tools/testing/selftests/kselftest.h (revision b217945eab7a0c37738cf37b89fdcc5b17d64c40)
17c466b97SShuah Khan /* SPDX-License-Identifier: GPL-2.0 */
27fb2c3eaSShuah Khan /*
3e80068beSKees Cook  * kselftest.h:	low-level kselftest framework to include from
4e80068beSKees Cook  *		selftest programs. When possible, please use
5e80068beSKees Cook  *		kselftest_harness.h instead.
67fb2c3eaSShuah Khan  *
77fb2c3eaSShuah Khan  * Copyright (c) 2014 Shuah Khan <shuahkh@osg.samsung.com>
87fb2c3eaSShuah Khan  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
97fb2c3eaSShuah Khan  *
10245dd604SKees Cook  * Using this API consists of first counting how many tests your code
11245dd604SKees Cook  * has to run, and then starting up the reporting:
12245dd604SKees Cook  *
13245dd604SKees Cook  *     ksft_print_header();
14245dd604SKees Cook  *     ksft_set_plan(total_number_of_tests);
15245dd604SKees Cook  *
16245dd604SKees Cook  * For each test, report any progress, debugging, etc with:
17245dd604SKees Cook  *
18245dd604SKees Cook  *     ksft_print_msg(fmt, ...);
19245dd604SKees Cook  *
20245dd604SKees Cook  * and finally report the pass/fail/skip/xfail state of the test with one of:
21245dd604SKees Cook  *
22245dd604SKees Cook  *     ksft_test_result(condition, fmt, ...);
23245dd604SKees Cook  *     ksft_test_result_pass(fmt, ...);
24245dd604SKees Cook  *     ksft_test_result_fail(fmt, ...);
25245dd604SKees Cook  *     ksft_test_result_skip(fmt, ...);
26245dd604SKees Cook  *     ksft_test_result_xfail(fmt, ...);
27245dd604SKees Cook  *     ksft_test_result_error(fmt, ...);
28245dd604SKees Cook  *
29245dd604SKees Cook  * When all tests are finished, clean up and exit the program with one of:
30245dd604SKees Cook  *
3125fd2d41SKees Cook  *    ksft_finished();
32245dd604SKees Cook  *    ksft_exit(condition);
33245dd604SKees Cook  *    ksft_exit_pass();
34245dd604SKees Cook  *    ksft_exit_fail();
35245dd604SKees Cook  *
36245dd604SKees Cook  * If the program wants to report details on why the entire program has
37245dd604SKees Cook  * failed, it can instead exit with a message (this is usually done when
38245dd604SKees Cook  * the program is aborting before finishing all tests):
39245dd604SKees Cook  *
40245dd604SKees Cook  *    ksft_exit_fail_msg(fmt, ...);
41245dd604SKees Cook  *
427fb2c3eaSShuah Khan  */
437fb2c3eaSShuah Khan #ifndef __KSELFTEST_H
447fb2c3eaSShuah Khan #define __KSELFTEST_H
457fb2c3eaSShuah Khan 
469e38be73SMark Brown #ifndef NOLIBC
47fc2e634eSAleksa Sarai #include <errno.h>
487fb2c3eaSShuah Khan #include <stdlib.h>
497fb2c3eaSShuah Khan #include <unistd.h>
50151b2732SPaul Elder #include <stdarg.h>
51*f70d849bSMark Brown #include <string.h>
52a18261d7STycho Andersen #include <stdio.h>
53c626db71SOleg Nesterov #include <sys/utsname.h>
549e38be73SMark Brown #endif
557fb2c3eaSShuah Khan 
56066b34aaSShuah Khan #ifndef ARRAY_SIZE
57066b34aaSShuah Khan #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
58066b34aaSShuah Khan #endif
59066b34aaSShuah Khan 
60a23039c7SReinette Chatre /*
61a23039c7SReinette Chatre  * gcc cpuid.h provides __cpuid_count() since v4.4.
62a23039c7SReinette Chatre  * Clang/LLVM cpuid.h provides  __cpuid_count() since v3.4.0.
63a23039c7SReinette Chatre  *
64a23039c7SReinette Chatre  * Provide local define for tests needing __cpuid_count() because
65a23039c7SReinette Chatre  * selftests need to work in older environments that do not yet
66a23039c7SReinette Chatre  * have __cpuid_count().
67a23039c7SReinette Chatre  */
68a23039c7SReinette Chatre #ifndef __cpuid_count
69a23039c7SReinette Chatre #define __cpuid_count(level, count, a, b, c, d)				\
70a23039c7SReinette Chatre 	__asm__ __volatile__ ("cpuid\n\t"				\
71a23039c7SReinette Chatre 			      : "=a" (a), "=b" (b), "=c" (c), "=d" (d)	\
72a23039c7SReinette Chatre 			      : "0" (level), "2" (count))
73a23039c7SReinette Chatre #endif
74a23039c7SReinette Chatre 
754100e675SDarren Hart /* define kselftest exit codes */
764100e675SDarren Hart #define KSFT_PASS  0
774100e675SDarren Hart #define KSFT_FAIL  1
784100e675SDarren Hart #define KSFT_XFAIL 2
794100e675SDarren Hart #define KSFT_XPASS 3
803c07aaefSShuah Khan (Samsung OSG) #define KSFT_SKIP  4
814100e675SDarren Hart 
827fb2c3eaSShuah Khan /* counters */
837fb2c3eaSShuah Khan struct ksft_count {
847fb2c3eaSShuah Khan 	unsigned int ksft_pass;
857fb2c3eaSShuah Khan 	unsigned int ksft_fail;
867fb2c3eaSShuah Khan 	unsigned int ksft_xfail;
877fb2c3eaSShuah Khan 	unsigned int ksft_xpass;
887fb2c3eaSShuah Khan 	unsigned int ksft_xskip;
89c0bb2cf4SShuah Khan 	unsigned int ksft_error;
907fb2c3eaSShuah Khan };
917fb2c3eaSShuah Khan 
927fb2c3eaSShuah Khan static struct ksft_count ksft_cnt;
935821ba96SKees Cook static unsigned int ksft_plan;
947fb2c3eaSShuah Khan 
ksft_test_num(void)953c01655aSPaolo Bonzini static inline unsigned int ksft_test_num(void)
96b6a4b66dSPaul Elder {
97b6a4b66dSPaul Elder 	return ksft_cnt.ksft_pass + ksft_cnt.ksft_fail +
98b6a4b66dSPaul Elder 		ksft_cnt.ksft_xfail + ksft_cnt.ksft_xpass +
99c0bb2cf4SShuah Khan 		ksft_cnt.ksft_xskip + ksft_cnt.ksft_error;
100b6a4b66dSPaul Elder }
101b6a4b66dSPaul Elder 
ksft_inc_pass_cnt(void)1027fb2c3eaSShuah Khan static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; }
ksft_inc_fail_cnt(void)1037fb2c3eaSShuah Khan static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; }
ksft_inc_xfail_cnt(void)1047fb2c3eaSShuah Khan static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; }
ksft_inc_xpass_cnt(void)1057fb2c3eaSShuah Khan static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; }
ksft_inc_xskip_cnt(void)1067fb2c3eaSShuah Khan static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; }
ksft_inc_error_cnt(void)107c0bb2cf4SShuah Khan static inline void ksft_inc_error_cnt(void) { ksft_cnt.ksft_error++; }
1087fb2c3eaSShuah Khan 
ksft_get_pass_cnt(void)1091d3ee8beSShuah Khan static inline int ksft_get_pass_cnt(void) { return ksft_cnt.ksft_pass; }
ksft_get_fail_cnt(void)1101d3ee8beSShuah Khan static inline int ksft_get_fail_cnt(void) { return ksft_cnt.ksft_fail; }
ksft_get_xfail_cnt(void)1111d3ee8beSShuah Khan static inline int ksft_get_xfail_cnt(void) { return ksft_cnt.ksft_xfail; }
ksft_get_xpass_cnt(void)1121d3ee8beSShuah Khan static inline int ksft_get_xpass_cnt(void) { return ksft_cnt.ksft_xpass; }
ksft_get_xskip_cnt(void)1131d3ee8beSShuah Khan static inline int ksft_get_xskip_cnt(void) { return ksft_cnt.ksft_xskip; }
ksft_get_error_cnt(void)114c0bb2cf4SShuah Khan static inline int ksft_get_error_cnt(void) { return ksft_cnt.ksft_error; }
1151d3ee8beSShuah Khan 
ksft_print_header(void)116b6a4b66dSPaul Elder static inline void ksft_print_header(void)
117b6a4b66dSPaul Elder {
11858e2847aSRyan Roberts 	/*
11958e2847aSRyan Roberts 	 * Force line buffering; If stdout is not connected to a terminal, it
12058e2847aSRyan Roberts 	 * will otherwise default to fully buffered, which can cause output
12158e2847aSRyan Roberts 	 * duplication if there is content in the buffer when fork()ing. If
12258e2847aSRyan Roberts 	 * there is a crash, line buffering also means the most recent output
12358e2847aSRyan Roberts 	 * line will be visible.
12458e2847aSRyan Roberts 	 */
12558e2847aSRyan Roberts 	setvbuf(stdout, NULL, _IOLBF, 0);
12658e2847aSRyan Roberts 
12710f531f6SShuah Khan 	if (!(getenv("KSFT_TAP_LEVEL")))
128b6a4b66dSPaul Elder 		printf("TAP version 13\n");
129b6a4b66dSPaul Elder }
130b6a4b66dSPaul Elder 
ksft_set_plan(unsigned int plan)1315821ba96SKees Cook static inline void ksft_set_plan(unsigned int plan)
1325821ba96SKees Cook {
1335821ba96SKees Cook 	ksft_plan = plan;
1345821ba96SKees Cook 	printf("1..%d\n", ksft_plan);
1355821ba96SKees Cook }
1365821ba96SKees Cook 
ksft_print_cnts(void)1377fb2c3eaSShuah Khan static inline void ksft_print_cnts(void)
1387fb2c3eaSShuah Khan {
1395821ba96SKees Cook 	if (ksft_plan != ksft_test_num())
1405821ba96SKees Cook 		printf("# Planned tests != run tests (%u != %u)\n",
1415821ba96SKees Cook 			ksft_plan, ksft_test_num());
142245dd604SKees Cook 	printf("# Totals: pass:%d fail:%d xfail:%d xpass:%d skip:%d error:%d\n",
1431d3ee8beSShuah Khan 		ksft_cnt.ksft_pass, ksft_cnt.ksft_fail,
1441d3ee8beSShuah Khan 		ksft_cnt.ksft_xfail, ksft_cnt.ksft_xpass,
145c0bb2cf4SShuah Khan 		ksft_cnt.ksft_xskip, ksft_cnt.ksft_error);
146b6a4b66dSPaul Elder }
147b6a4b66dSPaul Elder 
ksft_print_msg(const char * msg,...)148ab52a484SPaul Elder static inline void ksft_print_msg(const char *msg, ...)
149ab52a484SPaul Elder {
150fc2e634eSAleksa Sarai 	int saved_errno = errno;
151ab52a484SPaul Elder 	va_list args;
152ab52a484SPaul Elder 
153ab52a484SPaul Elder 	va_start(args, msg);
154ab52a484SPaul Elder 	printf("# ");
155fc2e634eSAleksa Sarai 	errno = saved_errno;
156ab52a484SPaul Elder 	vprintf(msg, args);
157ab52a484SPaul Elder 	va_end(args);
158ab52a484SPaul Elder }
159ab52a484SPaul Elder 
ksft_perror(const char * msg)160*f70d849bSMark Brown static inline void ksft_perror(const char *msg)
161*f70d849bSMark Brown {
162*f70d849bSMark Brown #ifndef NOLIBC
163*f70d849bSMark Brown 	ksft_print_msg("%s: %s (%d)\n", msg, strerror(errno), errno);
164*f70d849bSMark Brown #else
165*f70d849bSMark Brown 	/*
166*f70d849bSMark Brown 	 * nolibc doesn't provide strerror() and it seems
167*f70d849bSMark Brown 	 * inappropriate to add one, just print the errno.
168*f70d849bSMark Brown 	 */
169*f70d849bSMark Brown 	ksft_print_msg("%s: %d)\n", msg, errno);
170*f70d849bSMark Brown #endif
171*f70d849bSMark Brown }
172*f70d849bSMark Brown 
ksft_test_result_pass(const char * msg,...)173151b2732SPaul Elder static inline void ksft_test_result_pass(const char *msg, ...)
174b6a4b66dSPaul Elder {
175fc2e634eSAleksa Sarai 	int saved_errno = errno;
176151b2732SPaul Elder 	va_list args;
177151b2732SPaul Elder 
178b6a4b66dSPaul Elder 	ksft_cnt.ksft_pass++;
179151b2732SPaul Elder 
180151b2732SPaul Elder 	va_start(args, msg);
181151b2732SPaul Elder 	printf("ok %d ", ksft_test_num());
182fc2e634eSAleksa Sarai 	errno = saved_errno;
183151b2732SPaul Elder 	vprintf(msg, args);
184151b2732SPaul Elder 	va_end(args);
185b6a4b66dSPaul Elder }
186b6a4b66dSPaul Elder 
ksft_test_result_fail(const char * msg,...)187151b2732SPaul Elder static inline void ksft_test_result_fail(const char *msg, ...)
188b6a4b66dSPaul Elder {
189fc2e634eSAleksa Sarai 	int saved_errno = errno;
190151b2732SPaul Elder 	va_list args;
191151b2732SPaul Elder 
192b6a4b66dSPaul Elder 	ksft_cnt.ksft_fail++;
193151b2732SPaul Elder 
194151b2732SPaul Elder 	va_start(args, msg);
195151b2732SPaul Elder 	printf("not ok %d ", ksft_test_num());
196fc2e634eSAleksa Sarai 	errno = saved_errno;
197151b2732SPaul Elder 	vprintf(msg, args);
198151b2732SPaul Elder 	va_end(args);
199b6a4b66dSPaul Elder }
200b6a4b66dSPaul Elder 
201245dd604SKees Cook /**
202245dd604SKees Cook  * ksft_test_result() - Report test success based on truth of condition
203245dd604SKees Cook  *
204245dd604SKees Cook  * @condition: if true, report test success, otherwise failure.
205245dd604SKees Cook  */
206245dd604SKees Cook #define ksft_test_result(condition, fmt, ...) do {	\
207245dd604SKees Cook 	if (!!(condition))				\
208245dd604SKees Cook 		ksft_test_result_pass(fmt, ##__VA_ARGS__);\
209245dd604SKees Cook 	else						\
210245dd604SKees Cook 		ksft_test_result_fail(fmt, ##__VA_ARGS__);\
211245dd604SKees Cook 	} while (0)
212245dd604SKees Cook 
ksft_test_result_xfail(const char * msg,...)213245dd604SKees Cook static inline void ksft_test_result_xfail(const char *msg, ...)
214245dd604SKees Cook {
215245dd604SKees Cook 	int saved_errno = errno;
216245dd604SKees Cook 	va_list args;
217245dd604SKees Cook 
218245dd604SKees Cook 	ksft_cnt.ksft_xfail++;
219245dd604SKees Cook 
220245dd604SKees Cook 	va_start(args, msg);
221245dd604SKees Cook 	printf("ok %d # XFAIL ", ksft_test_num());
222245dd604SKees Cook 	errno = saved_errno;
223245dd604SKees Cook 	vprintf(msg, args);
224245dd604SKees Cook 	va_end(args);
225245dd604SKees Cook }
226245dd604SKees Cook 
ksft_test_result_skip(const char * msg,...)227151b2732SPaul Elder static inline void ksft_test_result_skip(const char *msg, ...)
228b6a4b66dSPaul Elder {
229fc2e634eSAleksa Sarai 	int saved_errno = errno;
230151b2732SPaul Elder 	va_list args;
231151b2732SPaul Elder 
232b6a4b66dSPaul Elder 	ksft_cnt.ksft_xskip++;
233151b2732SPaul Elder 
234151b2732SPaul Elder 	va_start(args, msg);
235b85d387cSPaolo Bonzini 	printf("ok %d # SKIP ", ksft_test_num());
236fc2e634eSAleksa Sarai 	errno = saved_errno;
237151b2732SPaul Elder 	vprintf(msg, args);
238151b2732SPaul Elder 	va_end(args);
2397fb2c3eaSShuah Khan }
2407fb2c3eaSShuah Khan 
241245dd604SKees Cook /* TODO: how does "error" differ from "fail" or "skip"? */
ksft_test_result_error(const char * msg,...)242c0bb2cf4SShuah Khan static inline void ksft_test_result_error(const char *msg, ...)
243c0bb2cf4SShuah Khan {
244fc2e634eSAleksa Sarai 	int saved_errno = errno;
245c0bb2cf4SShuah Khan 	va_list args;
246c0bb2cf4SShuah Khan 
247c0bb2cf4SShuah Khan 	ksft_cnt.ksft_error++;
248c0bb2cf4SShuah Khan 
249c0bb2cf4SShuah Khan 	va_start(args, msg);
250c0bb2cf4SShuah Khan 	printf("not ok %d # error ", ksft_test_num());
251fc2e634eSAleksa Sarai 	errno = saved_errno;
252c0bb2cf4SShuah Khan 	vprintf(msg, args);
253c0bb2cf4SShuah Khan 	va_end(args);
254c0bb2cf4SShuah Khan }
255c0bb2cf4SShuah Khan 
ksft_exit_pass(void)2567fb2c3eaSShuah Khan static inline int ksft_exit_pass(void)
2577fb2c3eaSShuah Khan {
258b6a4b66dSPaul Elder 	ksft_print_cnts();
2594100e675SDarren Hart 	exit(KSFT_PASS);
2607fb2c3eaSShuah Khan }
261b6a4b66dSPaul Elder 
ksft_exit_fail(void)2627fb2c3eaSShuah Khan static inline int ksft_exit_fail(void)
2637fb2c3eaSShuah Khan {
264b6a4b66dSPaul Elder 	ksft_print_cnts();
2654100e675SDarren Hart 	exit(KSFT_FAIL);
2667fb2c3eaSShuah Khan }
267b6a4b66dSPaul Elder 
268245dd604SKees Cook /**
269245dd604SKees Cook  * ksft_exit() - Exit selftest based on truth of condition
270245dd604SKees Cook  *
271245dd604SKees Cook  * @condition: if true, exit self test with success, otherwise fail.
272245dd604SKees Cook  */
273245dd604SKees Cook #define ksft_exit(condition) do {	\
274245dd604SKees Cook 	if (!!(condition))		\
275245dd604SKees Cook 		ksft_exit_pass();	\
276245dd604SKees Cook 	else				\
277245dd604SKees Cook 		ksft_exit_fail();	\
278245dd604SKees Cook 	} while (0)
279245dd604SKees Cook 
28025fd2d41SKees Cook /**
28125fd2d41SKees Cook  * ksft_finished() - Exit selftest with success if all tests passed
28225fd2d41SKees Cook  */
28325fd2d41SKees Cook #define ksft_finished()			\
28425fd2d41SKees Cook 	ksft_exit(ksft_plan ==		\
28525fd2d41SKees Cook 		  ksft_cnt.ksft_pass +	\
28625fd2d41SKees Cook 		  ksft_cnt.ksft_xfail +	\
28725fd2d41SKees Cook 		  ksft_cnt.ksft_xskip)
28825fd2d41SKees Cook 
ksft_exit_fail_msg(const char * msg,...)289151b2732SPaul Elder static inline int ksft_exit_fail_msg(const char *msg, ...)
290b6a4b66dSPaul Elder {
291fc2e634eSAleksa Sarai 	int saved_errno = errno;
292151b2732SPaul Elder 	va_list args;
293151b2732SPaul Elder 
294151b2732SPaul Elder 	va_start(args, msg);
295151b2732SPaul Elder 	printf("Bail out! ");
296fc2e634eSAleksa Sarai 	errno = saved_errno;
297151b2732SPaul Elder 	vprintf(msg, args);
298151b2732SPaul Elder 	va_end(args);
299151b2732SPaul Elder 
300b6a4b66dSPaul Elder 	ksft_print_cnts();
301b6a4b66dSPaul Elder 	exit(KSFT_FAIL);
302b6a4b66dSPaul Elder }
303b6a4b66dSPaul Elder 
ksft_exit_xfail(void)3047fb2c3eaSShuah Khan static inline int ksft_exit_xfail(void)
3057fb2c3eaSShuah Khan {
306b6a4b66dSPaul Elder 	ksft_print_cnts();
3074100e675SDarren Hart 	exit(KSFT_XFAIL);
3087fb2c3eaSShuah Khan }
309b6a4b66dSPaul Elder 
ksft_exit_xpass(void)3107fb2c3eaSShuah Khan static inline int ksft_exit_xpass(void)
3117fb2c3eaSShuah Khan {
312b6a4b66dSPaul Elder 	ksft_print_cnts();
3134100e675SDarren Hart 	exit(KSFT_XPASS);
3147fb2c3eaSShuah Khan }
315b6a4b66dSPaul Elder 
ksft_exit_skip(const char * msg,...)316151b2732SPaul Elder static inline int ksft_exit_skip(const char *msg, ...)
3177fb2c3eaSShuah Khan {
318fc2e634eSAleksa Sarai 	int saved_errno = errno;
319151b2732SPaul Elder 	va_list args;
320151b2732SPaul Elder 
321151b2732SPaul Elder 	va_start(args, msg);
322b85d387cSPaolo Bonzini 
323b85d387cSPaolo Bonzini 	/*
324b85d387cSPaolo Bonzini 	 * FIXME: several tests misuse ksft_exit_skip so produce
325b85d387cSPaolo Bonzini 	 * something sensible if some tests have already been run
326b85d387cSPaolo Bonzini 	 * or a plan has been printed.  Those tests should use
327b85d387cSPaolo Bonzini 	 * ksft_test_result_skip or ksft_exit_fail_msg instead.
328b85d387cSPaolo Bonzini 	 */
329b85d387cSPaolo Bonzini 	if (ksft_plan || ksft_test_num()) {
330b85d387cSPaolo Bonzini 		ksft_cnt.ksft_xskip++;
331b85d387cSPaolo Bonzini 		printf("ok %d # SKIP ", 1 + ksft_test_num());
332b85d387cSPaolo Bonzini 	} else {
333b85d387cSPaolo Bonzini 		printf("1..0 # SKIP ");
334b85d387cSPaolo Bonzini 	}
335b85d387cSPaolo Bonzini 	if (msg) {
336fc2e634eSAleksa Sarai 		errno = saved_errno;
337151b2732SPaul Elder 		vprintf(msg, args);
338151b2732SPaul Elder 		va_end(args);
339151b2732SPaul Elder 	}
340b85d387cSPaolo Bonzini 	if (ksft_test_num())
341b85d387cSPaolo Bonzini 		ksft_print_cnts();
3424100e675SDarren Hart 	exit(KSFT_SKIP);
3437fb2c3eaSShuah Khan }
3447fb2c3eaSShuah Khan 
ksft_min_kernel_version(unsigned int min_major,unsigned int min_minor)345c626db71SOleg Nesterov static inline int ksft_min_kernel_version(unsigned int min_major,
346c626db71SOleg Nesterov 					  unsigned int min_minor)
347c626db71SOleg Nesterov {
34863cc4f14SOleg Nesterov #ifdef NOLIBC
34963cc4f14SOleg Nesterov 	ksft_print_msg("NOLIBC: Can't check kernel version: Function not implemented\n");
35063cc4f14SOleg Nesterov 	return 0;
35163cc4f14SOleg Nesterov #else
352c626db71SOleg Nesterov 	unsigned int major, minor;
353c626db71SOleg Nesterov 	struct utsname info;
354c626db71SOleg Nesterov 
355c626db71SOleg Nesterov 	if (uname(&info) || sscanf(info.release, "%u.%u.", &major, &minor) != 2)
356c626db71SOleg Nesterov 		ksft_exit_fail_msg("Can't parse kernel version\n");
357c626db71SOleg Nesterov 
358c626db71SOleg Nesterov 	return major > min_major || (major == min_major && minor >= min_minor);
35963cc4f14SOleg Nesterov #endif
360c626db71SOleg Nesterov }
361c626db71SOleg Nesterov 
3627fb2c3eaSShuah Khan #endif /* __KSELFTEST_H */
363