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