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