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