.. | .. |
---|
1 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
---|
2 | 2 | /* |
---|
3 | | - * kselftest.h: kselftest framework return codes to include from |
---|
4 | | - * selftests. |
---|
| 3 | + * kselftest.h: low-level kselftest framework to include from |
---|
| 4 | + * selftest programs. When possible, please use |
---|
| 5 | + * kselftest_harness.h instead. |
---|
5 | 6 | * |
---|
6 | 7 | * Copyright (c) 2014 Shuah Khan <shuahkh@osg.samsung.com> |
---|
7 | 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_exit(condition); |
---|
| 32 | + * ksft_exit_pass(); |
---|
| 33 | + * ksft_exit_fail(); |
---|
| 34 | + * |
---|
| 35 | + * If the program wants to report details on why the entire program has |
---|
| 36 | + * failed, it can instead exit with a message (this is usually done when |
---|
| 37 | + * the program is aborting before finishing all tests): |
---|
| 38 | + * |
---|
| 39 | + * ksft_exit_fail_msg(fmt, ...); |
---|
8 | 40 | * |
---|
9 | 41 | */ |
---|
10 | 42 | #ifndef __KSELFTEST_H |
---|
11 | 43 | #define __KSELFTEST_H |
---|
12 | 44 | |
---|
| 45 | +#include <errno.h> |
---|
13 | 46 | #include <stdlib.h> |
---|
14 | 47 | #include <unistd.h> |
---|
15 | 48 | #include <stdarg.h> |
---|
| 49 | +#include <stdio.h> |
---|
16 | 50 | |
---|
17 | 51 | /* define kselftest exit codes */ |
---|
18 | 52 | #define KSFT_PASS 0 |
---|
.. | .. |
---|
32 | 66 | }; |
---|
33 | 67 | |
---|
34 | 68 | static struct ksft_count ksft_cnt; |
---|
| 69 | +static unsigned int ksft_plan; |
---|
35 | 70 | |
---|
36 | | -static inline int ksft_test_num(void) |
---|
| 71 | +static inline unsigned int ksft_test_num(void) |
---|
37 | 72 | { |
---|
38 | 73 | return ksft_cnt.ksft_pass + ksft_cnt.ksft_fail + |
---|
39 | 74 | ksft_cnt.ksft_xfail + ksft_cnt.ksft_xpass + |
---|
.. | .. |
---|
60 | 95 | printf("TAP version 13\n"); |
---|
61 | 96 | } |
---|
62 | 97 | |
---|
| 98 | +static inline void ksft_set_plan(unsigned int plan) |
---|
| 99 | +{ |
---|
| 100 | + ksft_plan = plan; |
---|
| 101 | + printf("1..%d\n", ksft_plan); |
---|
| 102 | +} |
---|
| 103 | + |
---|
63 | 104 | static inline void ksft_print_cnts(void) |
---|
64 | 105 | { |
---|
65 | | - printf("Pass %d Fail %d Xfail %d Xpass %d Skip %d Error %d\n", |
---|
| 106 | + if (ksft_plan != ksft_test_num()) |
---|
| 107 | + printf("# Planned tests != run tests (%u != %u)\n", |
---|
| 108 | + ksft_plan, ksft_test_num()); |
---|
| 109 | + printf("# Totals: pass:%d fail:%d xfail:%d xpass:%d skip:%d error:%d\n", |
---|
66 | 110 | ksft_cnt.ksft_pass, ksft_cnt.ksft_fail, |
---|
67 | 111 | ksft_cnt.ksft_xfail, ksft_cnt.ksft_xpass, |
---|
68 | 112 | ksft_cnt.ksft_xskip, ksft_cnt.ksft_error); |
---|
69 | | - printf("1..%d\n", ksft_test_num()); |
---|
70 | 113 | } |
---|
71 | 114 | |
---|
72 | 115 | static inline void ksft_print_msg(const char *msg, ...) |
---|
73 | 116 | { |
---|
| 117 | + int saved_errno = errno; |
---|
74 | 118 | va_list args; |
---|
75 | 119 | |
---|
76 | 120 | va_start(args, msg); |
---|
77 | 121 | printf("# "); |
---|
| 122 | + errno = saved_errno; |
---|
78 | 123 | vprintf(msg, args); |
---|
79 | 124 | va_end(args); |
---|
80 | 125 | } |
---|
81 | 126 | |
---|
82 | 127 | static inline void ksft_test_result_pass(const char *msg, ...) |
---|
83 | 128 | { |
---|
| 129 | + int saved_errno = errno; |
---|
84 | 130 | va_list args; |
---|
85 | 131 | |
---|
86 | 132 | ksft_cnt.ksft_pass++; |
---|
87 | 133 | |
---|
88 | 134 | va_start(args, msg); |
---|
89 | 135 | printf("ok %d ", ksft_test_num()); |
---|
| 136 | + errno = saved_errno; |
---|
90 | 137 | vprintf(msg, args); |
---|
91 | 138 | va_end(args); |
---|
92 | 139 | } |
---|
93 | 140 | |
---|
94 | 141 | static inline void ksft_test_result_fail(const char *msg, ...) |
---|
95 | 142 | { |
---|
| 143 | + int saved_errno = errno; |
---|
96 | 144 | va_list args; |
---|
97 | 145 | |
---|
98 | 146 | ksft_cnt.ksft_fail++; |
---|
99 | 147 | |
---|
100 | 148 | va_start(args, msg); |
---|
101 | 149 | printf("not ok %d ", ksft_test_num()); |
---|
| 150 | + errno = saved_errno; |
---|
| 151 | + vprintf(msg, args); |
---|
| 152 | + va_end(args); |
---|
| 153 | +} |
---|
| 154 | + |
---|
| 155 | +/** |
---|
| 156 | + * ksft_test_result() - Report test success based on truth of condition |
---|
| 157 | + * |
---|
| 158 | + * @condition: if true, report test success, otherwise failure. |
---|
| 159 | + */ |
---|
| 160 | +#define ksft_test_result(condition, fmt, ...) do { \ |
---|
| 161 | + if (!!(condition)) \ |
---|
| 162 | + ksft_test_result_pass(fmt, ##__VA_ARGS__);\ |
---|
| 163 | + else \ |
---|
| 164 | + ksft_test_result_fail(fmt, ##__VA_ARGS__);\ |
---|
| 165 | + } while (0) |
---|
| 166 | + |
---|
| 167 | +static inline void ksft_test_result_xfail(const char *msg, ...) |
---|
| 168 | +{ |
---|
| 169 | + int saved_errno = errno; |
---|
| 170 | + va_list args; |
---|
| 171 | + |
---|
| 172 | + ksft_cnt.ksft_xfail++; |
---|
| 173 | + |
---|
| 174 | + va_start(args, msg); |
---|
| 175 | + printf("ok %d # XFAIL ", ksft_test_num()); |
---|
| 176 | + errno = saved_errno; |
---|
102 | 177 | vprintf(msg, args); |
---|
103 | 178 | va_end(args); |
---|
104 | 179 | } |
---|
105 | 180 | |
---|
106 | 181 | static inline void ksft_test_result_skip(const char *msg, ...) |
---|
107 | 182 | { |
---|
| 183 | + int saved_errno = errno; |
---|
108 | 184 | va_list args; |
---|
109 | 185 | |
---|
110 | 186 | ksft_cnt.ksft_xskip++; |
---|
111 | 187 | |
---|
112 | 188 | va_start(args, msg); |
---|
113 | | - printf("ok %d # skip ", ksft_test_num()); |
---|
| 189 | + printf("ok %d # SKIP ", ksft_test_num()); |
---|
| 190 | + errno = saved_errno; |
---|
114 | 191 | vprintf(msg, args); |
---|
115 | 192 | va_end(args); |
---|
116 | 193 | } |
---|
117 | 194 | |
---|
| 195 | +/* TODO: how does "error" differ from "fail" or "skip"? */ |
---|
118 | 196 | static inline void ksft_test_result_error(const char *msg, ...) |
---|
119 | 197 | { |
---|
| 198 | + int saved_errno = errno; |
---|
120 | 199 | va_list args; |
---|
121 | 200 | |
---|
122 | 201 | ksft_cnt.ksft_error++; |
---|
123 | 202 | |
---|
124 | 203 | va_start(args, msg); |
---|
125 | 204 | printf("not ok %d # error ", ksft_test_num()); |
---|
| 205 | + errno = saved_errno; |
---|
126 | 206 | vprintf(msg, args); |
---|
127 | 207 | va_end(args); |
---|
128 | 208 | } |
---|
.. | .. |
---|
135 | 215 | |
---|
136 | 216 | static inline int ksft_exit_fail(void) |
---|
137 | 217 | { |
---|
138 | | - printf("Bail out!\n"); |
---|
139 | 218 | ksft_print_cnts(); |
---|
140 | 219 | exit(KSFT_FAIL); |
---|
141 | 220 | } |
---|
142 | 221 | |
---|
| 222 | +/** |
---|
| 223 | + * ksft_exit() - Exit selftest based on truth of condition |
---|
| 224 | + * |
---|
| 225 | + * @condition: if true, exit self test with success, otherwise fail. |
---|
| 226 | + */ |
---|
| 227 | +#define ksft_exit(condition) do { \ |
---|
| 228 | + if (!!(condition)) \ |
---|
| 229 | + ksft_exit_pass(); \ |
---|
| 230 | + else \ |
---|
| 231 | + ksft_exit_fail(); \ |
---|
| 232 | + } while (0) |
---|
| 233 | + |
---|
143 | 234 | static inline int ksft_exit_fail_msg(const char *msg, ...) |
---|
144 | 235 | { |
---|
| 236 | + int saved_errno = errno; |
---|
145 | 237 | va_list args; |
---|
146 | 238 | |
---|
147 | 239 | va_start(args, msg); |
---|
148 | 240 | printf("Bail out! "); |
---|
| 241 | + errno = saved_errno; |
---|
149 | 242 | vprintf(msg, args); |
---|
150 | 243 | va_end(args); |
---|
151 | 244 | |
---|
.. | .. |
---|
167 | 260 | |
---|
168 | 261 | static inline int ksft_exit_skip(const char *msg, ...) |
---|
169 | 262 | { |
---|
170 | | - if (msg) { |
---|
171 | | - va_list args; |
---|
| 263 | + int saved_errno = errno; |
---|
| 264 | + va_list args; |
---|
172 | 265 | |
---|
173 | | - va_start(args, msg); |
---|
174 | | - printf("1..%d # Skipped: ", ksft_test_num()); |
---|
| 266 | + va_start(args, msg); |
---|
| 267 | + |
---|
| 268 | + /* |
---|
| 269 | + * FIXME: several tests misuse ksft_exit_skip so produce |
---|
| 270 | + * something sensible if some tests have already been run |
---|
| 271 | + * or a plan has been printed. Those tests should use |
---|
| 272 | + * ksft_test_result_skip or ksft_exit_fail_msg instead. |
---|
| 273 | + */ |
---|
| 274 | + if (ksft_plan || ksft_test_num()) { |
---|
| 275 | + ksft_cnt.ksft_xskip++; |
---|
| 276 | + printf("ok %d # SKIP ", 1 + ksft_test_num()); |
---|
| 277 | + } else { |
---|
| 278 | + printf("1..0 # SKIP "); |
---|
| 279 | + } |
---|
| 280 | + if (msg) { |
---|
| 281 | + errno = saved_errno; |
---|
175 | 282 | vprintf(msg, args); |
---|
176 | 283 | va_end(args); |
---|
177 | | - } else { |
---|
178 | | - ksft_print_cnts(); |
---|
179 | 284 | } |
---|
| 285 | + if (ksft_test_num()) |
---|
| 286 | + ksft_print_cnts(); |
---|
180 | 287 | exit(KSFT_SKIP); |
---|
181 | 288 | } |
---|
182 | 289 | |
---|