• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ARRAY_SIZE
52 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
53 #endif
54 
55 /*
56  * gcc cpuid.h provides __cpuid_count() since v4.4.
57  * Clang/LLVM cpuid.h provides  __cpuid_count() since v3.4.0.
58  *
59  * Provide local define for tests needing __cpuid_count() because
60  * selftests need to work in older environments that do not yet
61  * have __cpuid_count().
62  */
63 #ifndef __cpuid_count
64 #define __cpuid_count(level, count, a, b, c, d)				\
65 	__asm__ __volatile__ ("cpuid\n\t"				\
66 			      : "=a" (a), "=b" (b), "=c" (c), "=d" (d)	\
67 			      : "0" (level), "2" (count))
68 #endif
69 
70 /* define kselftest exit codes */
71 #define KSFT_PASS  0
72 #define KSFT_FAIL  1
73 #define KSFT_XFAIL 2
74 #define KSFT_XPASS 3
75 #define KSFT_SKIP  4
76 
77 /* counters */
78 struct ksft_count {
79 	unsigned int ksft_pass;
80 	unsigned int ksft_fail;
81 	unsigned int ksft_xfail;
82 	unsigned int ksft_xpass;
83 	unsigned int ksft_xskip;
84 	unsigned int ksft_error;
85 };
86 
87 static struct ksft_count ksft_cnt;
88 static unsigned int ksft_plan;
89 
ksft_test_num(void)90 static inline unsigned int ksft_test_num(void)
91 {
92 	return ksft_cnt.ksft_pass + ksft_cnt.ksft_fail +
93 		ksft_cnt.ksft_xfail + ksft_cnt.ksft_xpass +
94 		ksft_cnt.ksft_xskip + ksft_cnt.ksft_error;
95 }
96 
ksft_inc_pass_cnt(void)97 static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; }
ksft_inc_fail_cnt(void)98 static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; }
ksft_inc_xfail_cnt(void)99 static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; }
ksft_inc_xpass_cnt(void)100 static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; }
ksft_inc_xskip_cnt(void)101 static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; }
ksft_inc_error_cnt(void)102 static inline void ksft_inc_error_cnt(void) { ksft_cnt.ksft_error++; }
103 
ksft_get_pass_cnt(void)104 static inline int ksft_get_pass_cnt(void) { return ksft_cnt.ksft_pass; }
ksft_get_fail_cnt(void)105 static inline int ksft_get_fail_cnt(void) { return ksft_cnt.ksft_fail; }
ksft_get_xfail_cnt(void)106 static inline int ksft_get_xfail_cnt(void) { return ksft_cnt.ksft_xfail; }
ksft_get_xpass_cnt(void)107 static inline int ksft_get_xpass_cnt(void) { return ksft_cnt.ksft_xpass; }
ksft_get_xskip_cnt(void)108 static inline int ksft_get_xskip_cnt(void) { return ksft_cnt.ksft_xskip; }
ksft_get_error_cnt(void)109 static inline int ksft_get_error_cnt(void) { return ksft_cnt.ksft_error; }
110 
ksft_print_header(void)111 static inline void ksft_print_header(void)
112 {
113 	if (!(getenv("KSFT_TAP_LEVEL")))
114 		printf("TAP version 13\n");
115 }
116 
ksft_set_plan(unsigned int plan)117 static inline void ksft_set_plan(unsigned int plan)
118 {
119 	ksft_plan = plan;
120 	printf("1..%d\n", ksft_plan);
121 }
122 
ksft_print_cnts(void)123 static inline void ksft_print_cnts(void)
124 {
125 	if (ksft_plan != ksft_test_num())
126 		printf("# Planned tests != run tests (%u != %u)\n",
127 			ksft_plan, ksft_test_num());
128 	printf("# Totals: pass:%d fail:%d xfail:%d xpass:%d skip:%d error:%d\n",
129 		ksft_cnt.ksft_pass, ksft_cnt.ksft_fail,
130 		ksft_cnt.ksft_xfail, ksft_cnt.ksft_xpass,
131 		ksft_cnt.ksft_xskip, ksft_cnt.ksft_error);
132 }
133 
ksft_print_msg(const char * msg,...)134 static inline void ksft_print_msg(const char *msg, ...)
135 {
136 	int saved_errno = errno;
137 	va_list args;
138 
139 	va_start(args, msg);
140 	printf("# ");
141 	errno = saved_errno;
142 	vprintf(msg, args);
143 	va_end(args);
144 }
145 
ksft_test_result_pass(const char * msg,...)146 static inline void ksft_test_result_pass(const char *msg, ...)
147 {
148 	int saved_errno = errno;
149 	va_list args;
150 
151 	ksft_cnt.ksft_pass++;
152 
153 	va_start(args, msg);
154 	printf("ok %d ", ksft_test_num());
155 	errno = saved_errno;
156 	vprintf(msg, args);
157 	va_end(args);
158 }
159 
ksft_test_result_fail(const char * msg,...)160 static inline void ksft_test_result_fail(const char *msg, ...)
161 {
162 	int saved_errno = errno;
163 	va_list args;
164 
165 	ksft_cnt.ksft_fail++;
166 
167 	va_start(args, msg);
168 	printf("not ok %d ", ksft_test_num());
169 	errno = saved_errno;
170 	vprintf(msg, args);
171 	va_end(args);
172 }
173 
174 /**
175  * ksft_test_result() - Report test success based on truth of condition
176  *
177  * @condition: if true, report test success, otherwise failure.
178  */
179 #define ksft_test_result(condition, fmt, ...) do {	\
180 	if (!!(condition))				\
181 		ksft_test_result_pass(fmt, ##__VA_ARGS__);\
182 	else						\
183 		ksft_test_result_fail(fmt, ##__VA_ARGS__);\
184 	} while (0)
185 
ksft_test_result_xfail(const char * msg,...)186 static inline void ksft_test_result_xfail(const char *msg, ...)
187 {
188 	int saved_errno = errno;
189 	va_list args;
190 
191 	ksft_cnt.ksft_xfail++;
192 
193 	va_start(args, msg);
194 	printf("ok %d # XFAIL ", ksft_test_num());
195 	errno = saved_errno;
196 	vprintf(msg, args);
197 	va_end(args);
198 }
199 
ksft_test_result_skip(const char * msg,...)200 static inline void ksft_test_result_skip(const char *msg, ...)
201 {
202 	int saved_errno = errno;
203 	va_list args;
204 
205 	ksft_cnt.ksft_xskip++;
206 
207 	va_start(args, msg);
208 	printf("ok %d # SKIP ", ksft_test_num());
209 	errno = saved_errno;
210 	vprintf(msg, args);
211 	va_end(args);
212 }
213 
214 /* TODO: how does "error" differ from "fail" or "skip"? */
ksft_test_result_error(const char * msg,...)215 static inline void ksft_test_result_error(const char *msg, ...)
216 {
217 	int saved_errno = errno;
218 	va_list args;
219 
220 	ksft_cnt.ksft_error++;
221 
222 	va_start(args, msg);
223 	printf("not ok %d # error ", ksft_test_num());
224 	errno = saved_errno;
225 	vprintf(msg, args);
226 	va_end(args);
227 }
228 
ksft_exit_pass(void)229 static inline int ksft_exit_pass(void)
230 {
231 	ksft_print_cnts();
232 	exit(KSFT_PASS);
233 }
234 
ksft_exit_fail(void)235 static inline int ksft_exit_fail(void)
236 {
237 	ksft_print_cnts();
238 	exit(KSFT_FAIL);
239 }
240 
241 /**
242  * ksft_exit() - Exit selftest based on truth of condition
243  *
244  * @condition: if true, exit self test with success, otherwise fail.
245  */
246 #define ksft_exit(condition) do {	\
247 	if (!!(condition))		\
248 		ksft_exit_pass();	\
249 	else				\
250 		ksft_exit_fail();	\
251 	} while (0)
252 
ksft_exit_fail_msg(const char * msg,...)253 static inline int ksft_exit_fail_msg(const char *msg, ...)
254 {
255 	int saved_errno = errno;
256 	va_list args;
257 
258 	va_start(args, msg);
259 	printf("Bail out! ");
260 	errno = saved_errno;
261 	vprintf(msg, args);
262 	va_end(args);
263 
264 	ksft_print_cnts();
265 	exit(KSFT_FAIL);
266 }
267 
ksft_exit_xfail(void)268 static inline int ksft_exit_xfail(void)
269 {
270 	ksft_print_cnts();
271 	exit(KSFT_XFAIL);
272 }
273 
ksft_exit_xpass(void)274 static inline int ksft_exit_xpass(void)
275 {
276 	ksft_print_cnts();
277 	exit(KSFT_XPASS);
278 }
279 
ksft_exit_skip(const char * msg,...)280 static inline int ksft_exit_skip(const char *msg, ...)
281 {
282 	int saved_errno = errno;
283 	va_list args;
284 
285 	va_start(args, msg);
286 
287 	/*
288 	 * FIXME: several tests misuse ksft_exit_skip so produce
289 	 * something sensible if some tests have already been run
290 	 * or a plan has been printed.  Those tests should use
291 	 * ksft_test_result_skip or ksft_exit_fail_msg instead.
292 	 */
293 	if (ksft_plan || ksft_test_num()) {
294 		ksft_cnt.ksft_xskip++;
295 		printf("ok %d # SKIP ", 1 + ksft_test_num());
296 	} else {
297 		printf("1..0 # SKIP ");
298 	}
299 	if (msg) {
300 		errno = saved_errno;
301 		vprintf(msg, args);
302 		va_end(args);
303 	}
304 	if (ksft_test_num())
305 		ksft_print_cnts();
306 	exit(KSFT_SKIP);
307 }
308 
309 #endif /* __KSELFTEST_H */
310