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