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 #include <errno.h> 47 #include <stdlib.h> 48 #include <unistd.h> 49 #include <stdarg.h> 50 #include <stdio.h> 51 52 #ifndef ARRAY_SIZE 53 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) 54 #endif 55 56 /* define kselftest exit codes */ 57 #define KSFT_PASS 0 58 #define KSFT_FAIL 1 59 #define KSFT_XFAIL 2 60 #define KSFT_XPASS 3 61 #define KSFT_SKIP 4 62 63 /* counters */ 64 struct ksft_count { 65 unsigned int ksft_pass; 66 unsigned int ksft_fail; 67 unsigned int ksft_xfail; 68 unsigned int ksft_xpass; 69 unsigned int ksft_xskip; 70 unsigned int ksft_error; 71 }; 72 73 static struct ksft_count ksft_cnt; 74 static unsigned int ksft_plan; 75 76 static inline unsigned int ksft_test_num(void) 77 { 78 return ksft_cnt.ksft_pass + ksft_cnt.ksft_fail + 79 ksft_cnt.ksft_xfail + ksft_cnt.ksft_xpass + 80 ksft_cnt.ksft_xskip + ksft_cnt.ksft_error; 81 } 82 83 static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; } 84 static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; } 85 static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; } 86 static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; } 87 static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; } 88 static inline void ksft_inc_error_cnt(void) { ksft_cnt.ksft_error++; } 89 90 static inline int ksft_get_pass_cnt(void) { return ksft_cnt.ksft_pass; } 91 static inline int ksft_get_fail_cnt(void) { return ksft_cnt.ksft_fail; } 92 static inline int ksft_get_xfail_cnt(void) { return ksft_cnt.ksft_xfail; } 93 static inline int ksft_get_xpass_cnt(void) { return ksft_cnt.ksft_xpass; } 94 static inline int ksft_get_xskip_cnt(void) { return ksft_cnt.ksft_xskip; } 95 static inline int ksft_get_error_cnt(void) { return ksft_cnt.ksft_error; } 96 97 static inline void ksft_print_header(void) 98 { 99 if (!(getenv("KSFT_TAP_LEVEL"))) 100 printf("TAP version 13\n"); 101 } 102 103 static inline void ksft_set_plan(unsigned int plan) 104 { 105 ksft_plan = plan; 106 printf("1..%d\n", ksft_plan); 107 } 108 109 static inline void ksft_print_cnts(void) 110 { 111 if (ksft_plan != ksft_test_num()) 112 printf("# Planned tests != run tests (%u != %u)\n", 113 ksft_plan, ksft_test_num()); 114 printf("# Totals: pass:%d fail:%d xfail:%d xpass:%d skip:%d error:%d\n", 115 ksft_cnt.ksft_pass, ksft_cnt.ksft_fail, 116 ksft_cnt.ksft_xfail, ksft_cnt.ksft_xpass, 117 ksft_cnt.ksft_xskip, ksft_cnt.ksft_error); 118 } 119 120 static inline void ksft_print_msg(const char *msg, ...) 121 { 122 int saved_errno = errno; 123 va_list args; 124 125 va_start(args, msg); 126 printf("# "); 127 errno = saved_errno; 128 vprintf(msg, args); 129 va_end(args); 130 } 131 132 static inline void ksft_test_result_pass(const char *msg, ...) 133 { 134 int saved_errno = errno; 135 va_list args; 136 137 ksft_cnt.ksft_pass++; 138 139 va_start(args, msg); 140 printf("ok %d ", ksft_test_num()); 141 errno = saved_errno; 142 vprintf(msg, args); 143 va_end(args); 144 } 145 146 static inline void ksft_test_result_fail(const char *msg, ...) 147 { 148 int saved_errno = errno; 149 va_list args; 150 151 ksft_cnt.ksft_fail++; 152 153 va_start(args, msg); 154 printf("not ok %d ", ksft_test_num()); 155 errno = saved_errno; 156 vprintf(msg, args); 157 va_end(args); 158 } 159 160 /** 161 * ksft_test_result() - Report test success based on truth of condition 162 * 163 * @condition: if true, report test success, otherwise failure. 164 */ 165 #define ksft_test_result(condition, fmt, ...) do { \ 166 if (!!(condition)) \ 167 ksft_test_result_pass(fmt, ##__VA_ARGS__);\ 168 else \ 169 ksft_test_result_fail(fmt, ##__VA_ARGS__);\ 170 } while (0) 171 172 static inline void ksft_test_result_xfail(const char *msg, ...) 173 { 174 int saved_errno = errno; 175 va_list args; 176 177 ksft_cnt.ksft_xfail++; 178 179 va_start(args, msg); 180 printf("ok %d # XFAIL ", ksft_test_num()); 181 errno = saved_errno; 182 vprintf(msg, args); 183 va_end(args); 184 } 185 186 static inline void ksft_test_result_skip(const char *msg, ...) 187 { 188 int saved_errno = errno; 189 va_list args; 190 191 ksft_cnt.ksft_xskip++; 192 193 va_start(args, msg); 194 printf("ok %d # SKIP ", ksft_test_num()); 195 errno = saved_errno; 196 vprintf(msg, args); 197 va_end(args); 198 } 199 200 /* TODO: how does "error" differ from "fail" or "skip"? */ 201 static inline void ksft_test_result_error(const char *msg, ...) 202 { 203 int saved_errno = errno; 204 va_list args; 205 206 ksft_cnt.ksft_error++; 207 208 va_start(args, msg); 209 printf("not ok %d # error ", ksft_test_num()); 210 errno = saved_errno; 211 vprintf(msg, args); 212 va_end(args); 213 } 214 215 static inline int ksft_exit_pass(void) 216 { 217 ksft_print_cnts(); 218 exit(KSFT_PASS); 219 } 220 221 static inline int ksft_exit_fail(void) 222 { 223 ksft_print_cnts(); 224 exit(KSFT_FAIL); 225 } 226 227 /** 228 * ksft_exit() - Exit selftest based on truth of condition 229 * 230 * @condition: if true, exit self test with success, otherwise fail. 231 */ 232 #define ksft_exit(condition) do { \ 233 if (!!(condition)) \ 234 ksft_exit_pass(); \ 235 else \ 236 ksft_exit_fail(); \ 237 } while (0) 238 239 /** 240 * ksft_finished() - Exit selftest with success if all tests passed 241 */ 242 #define ksft_finished() \ 243 ksft_exit(ksft_plan == \ 244 ksft_cnt.ksft_pass + \ 245 ksft_cnt.ksft_xfail + \ 246 ksft_cnt.ksft_xskip) 247 248 static inline int ksft_exit_fail_msg(const char *msg, ...) 249 { 250 int saved_errno = errno; 251 va_list args; 252 253 va_start(args, msg); 254 printf("Bail out! "); 255 errno = saved_errno; 256 vprintf(msg, args); 257 va_end(args); 258 259 ksft_print_cnts(); 260 exit(KSFT_FAIL); 261 } 262 263 static inline int ksft_exit_xfail(void) 264 { 265 ksft_print_cnts(); 266 exit(KSFT_XFAIL); 267 } 268 269 static inline int ksft_exit_xpass(void) 270 { 271 ksft_print_cnts(); 272 exit(KSFT_XPASS); 273 } 274 275 static inline int ksft_exit_skip(const char *msg, ...) 276 { 277 int saved_errno = errno; 278 va_list args; 279 280 va_start(args, msg); 281 282 /* 283 * FIXME: several tests misuse ksft_exit_skip so produce 284 * something sensible if some tests have already been run 285 * or a plan has been printed. Those tests should use 286 * ksft_test_result_skip or ksft_exit_fail_msg instead. 287 */ 288 if (ksft_plan || ksft_test_num()) { 289 ksft_cnt.ksft_xskip++; 290 printf("ok %d # SKIP ", 1 + ksft_test_num()); 291 } else { 292 printf("1..0 # SKIP "); 293 } 294 if (msg) { 295 errno = saved_errno; 296 vprintf(msg, args); 297 va_end(args); 298 } 299 if (ksft_test_num()) 300 ksft_print_cnts(); 301 exit(KSFT_SKIP); 302 } 303 304 #endif /* __KSELFTEST_H */ 305