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