Skip to content

Commit 245dd60

Browse files
keesshuahkh
authored andcommitted
selftests: Add header documentation and helpers
Add "how to use this API" documentation to kselftest.h, and include some addition helpers and notes to make things easier to use. Additionally removes the incorrect "Bail out!" line from the standard exit path. The TAP13 specification says that "Bail out!" should be used when giving up before all tests have been run. For a "normal" execution run, the selftests should not report "Bail out!". Signed-off-by: Kees Cook <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent eaa163c commit 245dd60

File tree

1 file changed

+71
-2
lines changed

1 file changed

+71
-2
lines changed

tools/testing/selftests/kselftest.h

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,37 @@
66
* Copyright (c) 2014 Shuah Khan <[email protected]>
77
* Copyright (c) 2014 Samsung Electronics Co., Ltd.
88
*
9+
* Using this API consists of first counting how many tests your code
10+
* has to run, and then starting up the reporting:
11+
*
12+
* ksft_print_header();
13+
* ksft_set_plan(total_number_of_tests);
14+
*
15+
* For each test, report any progress, debugging, etc with:
16+
*
17+
* ksft_print_msg(fmt, ...);
18+
*
19+
* and finally report the pass/fail/skip/xfail state of the test with one of:
20+
*
21+
* ksft_test_result(condition, fmt, ...);
22+
* ksft_test_result_pass(fmt, ...);
23+
* ksft_test_result_fail(fmt, ...);
24+
* ksft_test_result_skip(fmt, ...);
25+
* ksft_test_result_xfail(fmt, ...);
26+
* ksft_test_result_error(fmt, ...);
27+
*
28+
* When all tests are finished, clean up and exit the program with one of:
29+
*
30+
* ksft_exit(condition);
31+
* ksft_exit_pass();
32+
* ksft_exit_fail();
33+
*
34+
* If the program wants to report details on why the entire program has
35+
* failed, it can instead exit with a message (this is usually done when
36+
* the program is aborting before finishing all tests):
37+
*
38+
* ksft_exit_fail_msg(fmt, ...);
39+
*
940
*/
1041
#ifndef __KSELFTEST_H
1142
#define __KSELFTEST_H
@@ -74,7 +105,7 @@ static inline void ksft_print_cnts(void)
74105
if (ksft_plan != ksft_test_num())
75106
printf("# Planned tests != run tests (%u != %u)\n",
76107
ksft_plan, ksft_test_num());
77-
printf("# Pass %d Fail %d Xfail %d Xpass %d Skip %d Error %d\n",
108+
printf("# Totals: pass:%d fail:%d xfail:%d xpass:%d skip:%d error:%d\n",
78109
ksft_cnt.ksft_pass, ksft_cnt.ksft_fail,
79110
ksft_cnt.ksft_xfail, ksft_cnt.ksft_xpass,
80111
ksft_cnt.ksft_xskip, ksft_cnt.ksft_error);
@@ -120,6 +151,32 @@ static inline void ksft_test_result_fail(const char *msg, ...)
120151
va_end(args);
121152
}
122153

154+
/**
155+
* ksft_test_result() - Report test success based on truth of condition
156+
*
157+
* @condition: if true, report test success, otherwise failure.
158+
*/
159+
#define ksft_test_result(condition, fmt, ...) do { \
160+
if (!!(condition)) \
161+
ksft_test_result_pass(fmt, ##__VA_ARGS__);\
162+
else \
163+
ksft_test_result_fail(fmt, ##__VA_ARGS__);\
164+
} while (0)
165+
166+
static inline void ksft_test_result_xfail(const char *msg, ...)
167+
{
168+
int saved_errno = errno;
169+
va_list args;
170+
171+
ksft_cnt.ksft_xfail++;
172+
173+
va_start(args, msg);
174+
printf("ok %d # XFAIL ", ksft_test_num());
175+
errno = saved_errno;
176+
vprintf(msg, args);
177+
va_end(args);
178+
}
179+
123180
static inline void ksft_test_result_skip(const char *msg, ...)
124181
{
125182
int saved_errno = errno;
@@ -134,6 +191,7 @@ static inline void ksft_test_result_skip(const char *msg, ...)
134191
va_end(args);
135192
}
136193

194+
/* TODO: how does "error" differ from "fail" or "skip"? */
137195
static inline void ksft_test_result_error(const char *msg, ...)
138196
{
139197
int saved_errno = errno;
@@ -156,11 +214,22 @@ static inline int ksft_exit_pass(void)
156214

157215
static inline int ksft_exit_fail(void)
158216
{
159-
printf("Bail out!\n");
160217
ksft_print_cnts();
161218
exit(KSFT_FAIL);
162219
}
163220

221+
/**
222+
* ksft_exit() - Exit selftest based on truth of condition
223+
*
224+
* @condition: if true, exit self test with success, otherwise fail.
225+
*/
226+
#define ksft_exit(condition) do { \
227+
if (!!(condition)) \
228+
ksft_exit_pass(); \
229+
else \
230+
ksft_exit_fail(); \
231+
} while (0)
232+
164233
static inline int ksft_exit_fail_msg(const char *msg, ...)
165234
{
166235
int saved_errno = errno;

0 commit comments

Comments
 (0)