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