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)
296		directive = " #  ";
297
298	printf("%s %u %s%s", tap_code, ksft_test_num(), test_name, directive);
299	errno = saved_errno;
300	if (msg) {
301		va_start(args, msg);
302		vprintf(msg, args);
303		va_end(args);
304	}
305	printf("\n");
306}
307
308static inline __noreturn int ksft_exit_pass(void)
309{
310	ksft_print_cnts();
311	exit(KSFT_PASS);
312}
313
314static inline __noreturn int ksft_exit_fail(void)
315{
316	ksft_print_cnts();
317	exit(KSFT_FAIL);
318}
319
320/**
321 * ksft_exit() - Exit selftest based on truth of condition
322 *
323 * @condition: if true, exit self test with success, otherwise fail.
324 */
325#define ksft_exit(condition) do {	\
326	if (!!(condition))		\
327		ksft_exit_pass();	\
328	else				\
329		ksft_exit_fail();	\
330	} while (0)
331
332/**
333 * ksft_finished() - Exit selftest with success if all tests passed
334 */
335#define ksft_finished()			\
336	ksft_exit(ksft_plan ==		\
337		  ksft_cnt.ksft_pass +	\
338		  ksft_cnt.ksft_xfail +	\
339		  ksft_cnt.ksft_xskip)
340
341static inline __noreturn __printf(1, 2) int ksft_exit_fail_msg(const char *msg, ...)
342{
343	int saved_errno = errno;
344	va_list args;
345
346	va_start(args, msg);
347	printf("Bail out! ");
348	errno = saved_errno;
349	vprintf(msg, args);
350	va_end(args);
351
352	ksft_print_cnts();
353	exit(KSFT_FAIL);
354}
355
356static inline __noreturn int ksft_exit_xfail(void)
357{
358	ksft_print_cnts();
359	exit(KSFT_XFAIL);
360}
361
362static inline __noreturn int ksft_exit_xpass(void)
363{
364	ksft_print_cnts();
365	exit(KSFT_XPASS);
366}
367
368static inline __noreturn __printf(1, 2) int ksft_exit_skip(const char *msg, ...)
369{
370	int saved_errno = errno;
371	va_list args;
372
373	va_start(args, msg);
374
375	/*
376	 * FIXME: several tests misuse ksft_exit_skip so produce
377	 * something sensible if some tests have already been run
378	 * or a plan has been printed.  Those tests should use
379	 * ksft_test_result_skip or ksft_exit_fail_msg instead.
380	 */
381	if (ksft_plan || ksft_test_num()) {
382		ksft_cnt.ksft_xskip++;
383		printf("ok %d # SKIP ", 1 + ksft_test_num());
384	} else {
385		printf("1..0 # SKIP ");
386	}
387	if (msg) {
388		errno = saved_errno;
389		vprintf(msg, args);
390		va_end(args);
391	}
392	if (ksft_test_num())
393		ksft_print_cnts();
394	exit(KSFT_SKIP);
395}
396
397static inline int ksft_min_kernel_version(unsigned int min_major,
398					  unsigned int min_minor)
399{
400#ifdef NOLIBC
401	ksft_print_msg("NOLIBC: Can't check kernel version: Function not implemented\n");
402	return 0;
403#else
404	unsigned int major, minor;
405	struct utsname info;
406
407	if (uname(&info) || sscanf(info.release, "%u.%u.", &major, &minor) != 2)
408		ksft_exit_fail_msg("Can't parse kernel version\n");
409
410	return major > min_major || (major == min_major && minor >= min_minor);
411#endif
412}
413
414#endif /* __KSELFTEST_H */
415