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