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