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