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 * ksft_perror(msg); 20 * 21 * and finally report the pass/fail/skip/xfail/xpass state of the test 22 * with one of: 23 * 24 * ksft_test_result(condition, fmt, ...); 25 * ksft_test_result_report(result, fmt, ...); 26 * ksft_test_result_pass(fmt, ...); 27 * ksft_test_result_fail(fmt, ...); 28 * ksft_test_result_skip(fmt, ...); 29 * ksft_test_result_xfail(fmt, ...); 30 * ksft_test_result_xpass(fmt, ...); 31 * ksft_test_result_error(fmt, ...); 32 * ksft_test_result_code(exit_code, test_name, fmt, ...); 33 * 34 * When all tests are finished, clean up and exit the program with one of: 35 * 36 * ksft_finished(); 37 * ksft_exit(condition); 38 * ksft_exit_pass(); 39 * ksft_exit_fail(); 40 * 41 * If the program wants to report details on why the entire program has 42 * failed, it can instead exit with a message (this is usually done when 43 * the program is aborting before finishing all tests): 44 * 45 * ksft_exit_fail_msg(fmt, ...); 46 * ksft_exit_fail_perror(msg); 47 * 48 */ 49 #ifndef __KSELFTEST_H 50 #define __KSELFTEST_H 51 52 #ifndef NOLIBC 53 #include <errno.h> 54 #include <stdlib.h> 55 #include <unistd.h> 56 #include <stdarg.h> 57 #include <stdbool.h> 58 #include <string.h> 59 #include <stdio.h> 60 #include <sys/utsname.h> 61 #endif 62 63 #ifndef ARRAY_SIZE 64 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) 65 #endif 66 67 #if defined(__i386__) || defined(__x86_64__) /* arch */ 68 /* 69 * gcc cpuid.h provides __cpuid_count() since v4.4. 70 * Clang/LLVM cpuid.h provides __cpuid_count() since v3.4.0. 71 * 72 * Provide local define for tests needing __cpuid_count() because 73 * selftests need to work in older environments that do not yet 74 * have __cpuid_count(). 75 */ 76 #ifndef __cpuid_count 77 #define __cpuid_count(level, count, a, b, c, d) \ 78 __asm__ __volatile__ ("cpuid\n\t" \ 79 : "=a" (a), "=b" (b), "=c" (c), "=d" (d) \ 80 : "0" (level), "2" (count)) 81 #endif 82 #endif /* end arch */ 83 84 /* define kselftest exit codes */ 85 #define KSFT_PASS 0 86 #define KSFT_FAIL 1 87 #define KSFT_XFAIL 2 88 #define KSFT_XPASS 3 89 #define KSFT_SKIP 4 90 91 #ifndef __noreturn 92 #define __noreturn __attribute__((__noreturn__)) 93 #endif 94 #define __printf(a, b) __attribute__((format(printf, a, b))) 95 96 /* counters */ 97 struct ksft_count { 98 unsigned int ksft_pass; 99 unsigned int ksft_fail; 100 unsigned int ksft_xfail; 101 unsigned int ksft_xpass; 102 unsigned int ksft_xskip; 103 unsigned int ksft_error; 104 }; 105 106 static struct ksft_count ksft_cnt; 107 static unsigned int ksft_plan; 108 static bool ksft_debug_enabled; 109 110 static inline unsigned int ksft_test_num(void) 111 { 112 return ksft_cnt.ksft_pass + ksft_cnt.ksft_fail + 113 ksft_cnt.ksft_xfail + ksft_cnt.ksft_xpass + 114 ksft_cnt.ksft_xskip + ksft_cnt.ksft_error; 115 } 116 117 static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; } 118 static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; } 119 static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; } 120 static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; } 121 static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; } 122 static inline void ksft_inc_error_cnt(void) { ksft_cnt.ksft_error++; } 123 124 static inline int ksft_get_pass_cnt(void) { return ksft_cnt.ksft_pass; } 125 static inline int ksft_get_fail_cnt(void) { return ksft_cnt.ksft_fail; } 126 static inline int ksft_get_xfail_cnt(void) { return ksft_cnt.ksft_xfail; } 127 static inline int ksft_get_xpass_cnt(void) { return ksft_cnt.ksft_xpass; } 128 static inline int ksft_get_xskip_cnt(void) { return ksft_cnt.ksft_xskip; } 129 static inline int ksft_get_error_cnt(void) { return ksft_cnt.ksft_error; } 130 131 static inline void ksft_print_header(void) 132 { 133 /* 134 * Force line buffering; If stdout is not connected to a terminal, it 135 * will otherwise default to fully buffered, which can cause output 136 * duplication if there is content in the buffer when fork()ing. If 137 * there is a crash, line buffering also means the most recent output 138 * line will be visible. 139 */ 140 setvbuf(stdout, NULL, _IOLBF, 0); 141 142 if (!(getenv("KSFT_TAP_LEVEL"))) 143 printf("TAP version 13\n"); 144 } 145 146 static inline void ksft_set_plan(unsigned int plan) 147 { 148 ksft_plan = plan; 149 printf("1..%u\n", ksft_plan); 150 } 151 152 static inline void ksft_print_cnts(void) 153 { 154 if (ksft_cnt.ksft_xskip > 0) 155 printf( 156 "# %u skipped test(s) detected. Consider enabling relevant config options to improve coverage.\n", 157 ksft_cnt.ksft_xskip 158 ); 159 if (ksft_plan != ksft_test_num()) 160 printf("# Planned tests != run tests (%u != %u)\n", 161 ksft_plan, ksft_test_num()); 162 printf("# Totals: pass:%u fail:%u xfail:%u xpass:%u skip:%u error:%u\n", 163 ksft_cnt.ksft_pass, ksft_cnt.ksft_fail, 164 ksft_cnt.ksft_xfail, ksft_cnt.ksft_xpass, 165 ksft_cnt.ksft_xskip, ksft_cnt.ksft_error); 166 } 167 168 static inline __printf(1, 2) void ksft_print_msg(const char *msg, ...) 169 { 170 int saved_errno = errno; 171 va_list args; 172 173 va_start(args, msg); 174 printf("# "); 175 errno = saved_errno; 176 vprintf(msg, args); 177 va_end(args); 178 } 179 180 static inline void ksft_print_dbg_msg(const char *msg, ...) 181 { 182 va_list args; 183 184 if (!ksft_debug_enabled) 185 return; 186 187 va_start(args, msg); 188 ksft_print_msg(msg, args); 189 va_end(args); 190 } 191 192 static inline void ksft_perror(const char *msg) 193 { 194 ksft_print_msg("%s: %s (%d)\n", msg, strerror(errno), errno); 195 } 196 197 static inline __printf(1, 2) void ksft_test_result_pass(const char *msg, ...) 198 { 199 int saved_errno = errno; 200 va_list args; 201 202 ksft_cnt.ksft_pass++; 203 204 va_start(args, msg); 205 printf("ok %u ", ksft_test_num()); 206 errno = saved_errno; 207 vprintf(msg, args); 208 va_end(args); 209 } 210 211 static inline __printf(1, 2) void ksft_test_result_fail(const char *msg, ...) 212 { 213 int saved_errno = errno; 214 va_list args; 215 216 ksft_cnt.ksft_fail++; 217 218 va_start(args, msg); 219 printf("not ok %u ", ksft_test_num()); 220 errno = saved_errno; 221 vprintf(msg, args); 222 va_end(args); 223 } 224 225 /** 226 * ksft_test_result() - Report test success based on truth of condition 227 * 228 * @condition: if true, report test success, otherwise failure. 229 */ 230 #define ksft_test_result(condition, fmt, ...) do { \ 231 if (!!(condition)) \ 232 ksft_test_result_pass(fmt, ##__VA_ARGS__);\ 233 else \ 234 ksft_test_result_fail(fmt, ##__VA_ARGS__);\ 235 } while (0) 236 237 static inline __printf(1, 2) void ksft_test_result_xfail(const char *msg, ...) 238 { 239 int saved_errno = errno; 240 va_list args; 241 242 ksft_cnt.ksft_xfail++; 243 244 va_start(args, msg); 245 printf("ok %u # XFAIL ", ksft_test_num()); 246 errno = saved_errno; 247 vprintf(msg, args); 248 va_end(args); 249 } 250 251 static inline __printf(1, 2) void ksft_test_result_xpass(const char *msg, ...) 252 { 253 int saved_errno = errno; 254 va_list args; 255 256 ksft_cnt.ksft_xpass++; 257 258 va_start(args, msg); 259 printf("ok %u # XPASS ", ksft_test_num()); 260 errno = saved_errno; 261 vprintf(msg, args); 262 va_end(args); 263 } 264 265 static inline __printf(1, 2) void ksft_test_result_skip(const char *msg, ...) 266 { 267 int saved_errno = errno; 268 va_list args; 269 270 ksft_cnt.ksft_xskip++; 271 272 va_start(args, msg); 273 printf("ok %u # SKIP ", ksft_test_num()); 274 errno = saved_errno; 275 vprintf(msg, args); 276 va_end(args); 277 } 278 279 /* TODO: how does "error" differ from "fail" or "skip"? */ 280 static inline __printf(1, 2) void ksft_test_result_error(const char *msg, ...) 281 { 282 int saved_errno = errno; 283 va_list args; 284 285 ksft_cnt.ksft_error++; 286 287 va_start(args, msg); 288 printf("not ok %u # error ", ksft_test_num()); 289 errno = saved_errno; 290 vprintf(msg, args); 291 va_end(args); 292 } 293 294 static inline __printf(3, 4) 295 void ksft_test_result_code(int exit_code, const char *test_name, 296 const char *msg, ...) 297 { 298 const char *tap_code = "ok"; 299 const char *directive = ""; 300 int saved_errno = errno; 301 va_list args; 302 303 switch (exit_code) { 304 case KSFT_PASS: 305 ksft_cnt.ksft_pass++; 306 break; 307 case KSFT_XFAIL: 308 directive = " # XFAIL "; 309 ksft_cnt.ksft_xfail++; 310 break; 311 case KSFT_XPASS: 312 directive = " # XPASS "; 313 ksft_cnt.ksft_xpass++; 314 break; 315 case KSFT_SKIP: 316 directive = " # SKIP "; 317 ksft_cnt.ksft_xskip++; 318 break; 319 case KSFT_FAIL: 320 default: 321 tap_code = "not ok"; 322 ksft_cnt.ksft_fail++; 323 break; 324 } 325 326 /* Docs seem to call for double space if directive is absent */ 327 if (!directive[0] && msg) 328 directive = " # "; 329 330 printf("%s %u %s%s", tap_code, ksft_test_num(), test_name, directive); 331 errno = saved_errno; 332 if (msg) { 333 va_start(args, msg); 334 vprintf(msg, args); 335 va_end(args); 336 } 337 printf("\n"); 338 } 339 340 /** 341 * ksft_test_result() - Report test success based on truth of condition 342 * 343 * @condition: if true, report test success, otherwise failure. 344 */ 345 #define ksft_test_result_report(result, fmt, ...) do { \ 346 switch (result) { \ 347 case KSFT_PASS: \ 348 ksft_test_result_pass(fmt, ##__VA_ARGS__); \ 349 break; \ 350 case KSFT_FAIL: \ 351 ksft_test_result_fail(fmt, ##__VA_ARGS__); \ 352 break; \ 353 case KSFT_XFAIL: \ 354 ksft_test_result_xfail(fmt, ##__VA_ARGS__); \ 355 break; \ 356 case KSFT_XPASS: \ 357 ksft_test_result_xpass(fmt, ##__VA_ARGS__); \ 358 break; \ 359 case KSFT_SKIP: \ 360 ksft_test_result_skip(fmt, ##__VA_ARGS__); \ 361 break; \ 362 } } while (0) 363 364 static inline __noreturn void ksft_exit_pass(void) 365 { 366 ksft_print_cnts(); 367 exit(KSFT_PASS); 368 } 369 370 static inline __noreturn void ksft_exit_fail(void) 371 { 372 ksft_print_cnts(); 373 exit(KSFT_FAIL); 374 } 375 376 /** 377 * ksft_exit() - Exit selftest based on truth of condition 378 * 379 * @condition: if true, exit self test with success, otherwise fail. 380 */ 381 #define ksft_exit(condition) do { \ 382 if (!!(condition)) \ 383 ksft_exit_pass(); \ 384 else \ 385 ksft_exit_fail(); \ 386 } while (0) 387 388 /** 389 * ksft_finished() - Exit selftest with success if all tests passed 390 */ 391 #define ksft_finished() \ 392 ksft_exit(ksft_plan == \ 393 ksft_cnt.ksft_pass + \ 394 ksft_cnt.ksft_xfail + \ 395 ksft_cnt.ksft_xskip) 396 397 static inline __noreturn __printf(1, 2) void ksft_exit_fail_msg(const char *msg, ...) 398 { 399 int saved_errno = errno; 400 va_list args; 401 402 va_start(args, msg); 403 printf("Bail out! "); 404 errno = saved_errno; 405 vprintf(msg, args); 406 va_end(args); 407 408 ksft_print_cnts(); 409 exit(KSFT_FAIL); 410 } 411 412 static inline __noreturn void ksft_exit_fail_perror(const char *msg) 413 { 414 ksft_exit_fail_msg("%s: %s (%d)\n", msg, strerror(errno), errno); 415 } 416 417 static inline __noreturn void ksft_exit_xfail(void) 418 { 419 ksft_print_cnts(); 420 exit(KSFT_XFAIL); 421 } 422 423 static inline __noreturn void ksft_exit_xpass(void) 424 { 425 ksft_print_cnts(); 426 exit(KSFT_XPASS); 427 } 428 429 static inline __noreturn __printf(1, 2) void ksft_exit_skip(const char *msg, ...) 430 { 431 int saved_errno = errno; 432 va_list args; 433 434 va_start(args, msg); 435 436 /* 437 * FIXME: several tests misuse ksft_exit_skip so produce 438 * something sensible if some tests have already been run 439 * or a plan has been printed. Those tests should use 440 * ksft_test_result_skip or ksft_exit_fail_msg instead. 441 */ 442 if (ksft_plan || ksft_test_num()) { 443 ksft_cnt.ksft_xskip++; 444 printf("ok %u # SKIP ", 1 + ksft_test_num()); 445 } else { 446 printf("1..0 # SKIP "); 447 } 448 if (msg) { 449 errno = saved_errno; 450 vprintf(msg, args); 451 va_end(args); 452 } 453 if (ksft_test_num()) 454 ksft_print_cnts(); 455 exit(KSFT_SKIP); 456 } 457 458 static inline int ksft_min_kernel_version(unsigned int min_major, 459 unsigned int min_minor) 460 { 461 unsigned int major, minor; 462 struct utsname info; 463 464 if (uname(&info) || sscanf(info.release, "%u.%u.", &major, &minor) != 2) 465 ksft_exit_fail_msg("Can't parse kernel version\n"); 466 467 return major > min_major || (major == min_major && minor >= min_minor); 468 } 469 470 #endif /* __KSELFTEST_H */ 471