Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * kselftest.h: low-level kselftest framework to include from
0004  *      selftest programs. When possible, please use
0005  *      kselftest_harness.h instead.
0006  *
0007  * Copyright (c) 2014 Shuah Khan <shuahkh@osg.samsung.com>
0008  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
0009  *
0010  * Using this API consists of first counting how many tests your code
0011  * has to run, and then starting up the reporting:
0012  *
0013  *     ksft_print_header();
0014  *     ksft_set_plan(total_number_of_tests);
0015  *
0016  * For each test, report any progress, debugging, etc with:
0017  *
0018  *     ksft_print_msg(fmt, ...);
0019  *
0020  * and finally report the pass/fail/skip/xfail state of the test with one of:
0021  *
0022  *     ksft_test_result(condition, fmt, ...);
0023  *     ksft_test_result_pass(fmt, ...);
0024  *     ksft_test_result_fail(fmt, ...);
0025  *     ksft_test_result_skip(fmt, ...);
0026  *     ksft_test_result_xfail(fmt, ...);
0027  *     ksft_test_result_error(fmt, ...);
0028  *
0029  * When all tests are finished, clean up and exit the program with one of:
0030  *
0031  *    ksft_finished();
0032  *    ksft_exit(condition);
0033  *    ksft_exit_pass();
0034  *    ksft_exit_fail();
0035  *
0036  * If the program wants to report details on why the entire program has
0037  * failed, it can instead exit with a message (this is usually done when
0038  * the program is aborting before finishing all tests):
0039  *
0040  *    ksft_exit_fail_msg(fmt, ...);
0041  *
0042  */
0043 #ifndef __KSELFTEST_H
0044 #define __KSELFTEST_H
0045 
0046 #include <errno.h>
0047 #include <stdlib.h>
0048 #include <unistd.h>
0049 #include <stdarg.h>
0050 #include <stdio.h>
0051 
0052 #ifndef ARRAY_SIZE
0053 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
0054 #endif
0055 
0056 /*
0057  * gcc cpuid.h provides __cpuid_count() since v4.4.
0058  * Clang/LLVM cpuid.h provides  __cpuid_count() since v3.4.0.
0059  *
0060  * Provide local define for tests needing __cpuid_count() because
0061  * selftests need to work in older environments that do not yet
0062  * have __cpuid_count().
0063  */
0064 #ifndef __cpuid_count
0065 #define __cpuid_count(level, count, a, b, c, d)             \
0066     __asm__ __volatile__ ("cpuid\n\t"               \
0067                   : "=a" (a), "=b" (b), "=c" (c), "=d" (d)  \
0068                   : "0" (level), "2" (count))
0069 #endif
0070 
0071 /* define kselftest exit codes */
0072 #define KSFT_PASS  0
0073 #define KSFT_FAIL  1
0074 #define KSFT_XFAIL 2
0075 #define KSFT_XPASS 3
0076 #define KSFT_SKIP  4
0077 
0078 /* counters */
0079 struct ksft_count {
0080     unsigned int ksft_pass;
0081     unsigned int ksft_fail;
0082     unsigned int ksft_xfail;
0083     unsigned int ksft_xpass;
0084     unsigned int ksft_xskip;
0085     unsigned int ksft_error;
0086 };
0087 
0088 static struct ksft_count ksft_cnt;
0089 static unsigned int ksft_plan;
0090 
0091 static inline unsigned int ksft_test_num(void)
0092 {
0093     return ksft_cnt.ksft_pass + ksft_cnt.ksft_fail +
0094         ksft_cnt.ksft_xfail + ksft_cnt.ksft_xpass +
0095         ksft_cnt.ksft_xskip + ksft_cnt.ksft_error;
0096 }
0097 
0098 static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; }
0099 static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; }
0100 static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; }
0101 static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; }
0102 static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; }
0103 static inline void ksft_inc_error_cnt(void) { ksft_cnt.ksft_error++; }
0104 
0105 static inline int ksft_get_pass_cnt(void) { return ksft_cnt.ksft_pass; }
0106 static inline int ksft_get_fail_cnt(void) { return ksft_cnt.ksft_fail; }
0107 static inline int ksft_get_xfail_cnt(void) { return ksft_cnt.ksft_xfail; }
0108 static inline int ksft_get_xpass_cnt(void) { return ksft_cnt.ksft_xpass; }
0109 static inline int ksft_get_xskip_cnt(void) { return ksft_cnt.ksft_xskip; }
0110 static inline int ksft_get_error_cnt(void) { return ksft_cnt.ksft_error; }
0111 
0112 static inline void ksft_print_header(void)
0113 {
0114     if (!(getenv("KSFT_TAP_LEVEL")))
0115         printf("TAP version 13\n");
0116 }
0117 
0118 static inline void ksft_set_plan(unsigned int plan)
0119 {
0120     ksft_plan = plan;
0121     printf("1..%d\n", ksft_plan);
0122 }
0123 
0124 static inline void ksft_print_cnts(void)
0125 {
0126     if (ksft_plan != ksft_test_num())
0127         printf("# Planned tests != run tests (%u != %u)\n",
0128             ksft_plan, ksft_test_num());
0129     printf("# Totals: pass:%d fail:%d xfail:%d xpass:%d skip:%d error:%d\n",
0130         ksft_cnt.ksft_pass, ksft_cnt.ksft_fail,
0131         ksft_cnt.ksft_xfail, ksft_cnt.ksft_xpass,
0132         ksft_cnt.ksft_xskip, ksft_cnt.ksft_error);
0133 }
0134 
0135 static inline void ksft_print_msg(const char *msg, ...)
0136 {
0137     int saved_errno = errno;
0138     va_list args;
0139 
0140     va_start(args, msg);
0141     printf("# ");
0142     errno = saved_errno;
0143     vprintf(msg, args);
0144     va_end(args);
0145 }
0146 
0147 static inline void ksft_test_result_pass(const char *msg, ...)
0148 {
0149     int saved_errno = errno;
0150     va_list args;
0151 
0152     ksft_cnt.ksft_pass++;
0153 
0154     va_start(args, msg);
0155     printf("ok %d ", ksft_test_num());
0156     errno = saved_errno;
0157     vprintf(msg, args);
0158     va_end(args);
0159 }
0160 
0161 static inline void ksft_test_result_fail(const char *msg, ...)
0162 {
0163     int saved_errno = errno;
0164     va_list args;
0165 
0166     ksft_cnt.ksft_fail++;
0167 
0168     va_start(args, msg);
0169     printf("not ok %d ", ksft_test_num());
0170     errno = saved_errno;
0171     vprintf(msg, args);
0172     va_end(args);
0173 }
0174 
0175 /**
0176  * ksft_test_result() - Report test success based on truth of condition
0177  *
0178  * @condition: if true, report test success, otherwise failure.
0179  */
0180 #define ksft_test_result(condition, fmt, ...) do {  \
0181     if (!!(condition))              \
0182         ksft_test_result_pass(fmt, ##__VA_ARGS__);\
0183     else                        \
0184         ksft_test_result_fail(fmt, ##__VA_ARGS__);\
0185     } while (0)
0186 
0187 static inline void ksft_test_result_xfail(const char *msg, ...)
0188 {
0189     int saved_errno = errno;
0190     va_list args;
0191 
0192     ksft_cnt.ksft_xfail++;
0193 
0194     va_start(args, msg);
0195     printf("ok %d # XFAIL ", ksft_test_num());
0196     errno = saved_errno;
0197     vprintf(msg, args);
0198     va_end(args);
0199 }
0200 
0201 static inline void ksft_test_result_skip(const char *msg, ...)
0202 {
0203     int saved_errno = errno;
0204     va_list args;
0205 
0206     ksft_cnt.ksft_xskip++;
0207 
0208     va_start(args, msg);
0209     printf("ok %d # SKIP ", ksft_test_num());
0210     errno = saved_errno;
0211     vprintf(msg, args);
0212     va_end(args);
0213 }
0214 
0215 /* TODO: how does "error" differ from "fail" or "skip"? */
0216 static inline void ksft_test_result_error(const char *msg, ...)
0217 {
0218     int saved_errno = errno;
0219     va_list args;
0220 
0221     ksft_cnt.ksft_error++;
0222 
0223     va_start(args, msg);
0224     printf("not ok %d # error ", ksft_test_num());
0225     errno = saved_errno;
0226     vprintf(msg, args);
0227     va_end(args);
0228 }
0229 
0230 static inline int ksft_exit_pass(void)
0231 {
0232     ksft_print_cnts();
0233     exit(KSFT_PASS);
0234 }
0235 
0236 static inline int ksft_exit_fail(void)
0237 {
0238     ksft_print_cnts();
0239     exit(KSFT_FAIL);
0240 }
0241 
0242 /**
0243  * ksft_exit() - Exit selftest based on truth of condition
0244  *
0245  * @condition: if true, exit self test with success, otherwise fail.
0246  */
0247 #define ksft_exit(condition) do {   \
0248     if (!!(condition))      \
0249         ksft_exit_pass();   \
0250     else                \
0251         ksft_exit_fail();   \
0252     } while (0)
0253 
0254 /**
0255  * ksft_finished() - Exit selftest with success if all tests passed
0256  */
0257 #define ksft_finished()         \
0258     ksft_exit(ksft_plan ==      \
0259           ksft_cnt.ksft_pass +  \
0260           ksft_cnt.ksft_xfail + \
0261           ksft_cnt.ksft_xskip)
0262 
0263 static inline int ksft_exit_fail_msg(const char *msg, ...)
0264 {
0265     int saved_errno = errno;
0266     va_list args;
0267 
0268     va_start(args, msg);
0269     printf("Bail out! ");
0270     errno = saved_errno;
0271     vprintf(msg, args);
0272     va_end(args);
0273 
0274     ksft_print_cnts();
0275     exit(KSFT_FAIL);
0276 }
0277 
0278 static inline int ksft_exit_xfail(void)
0279 {
0280     ksft_print_cnts();
0281     exit(KSFT_XFAIL);
0282 }
0283 
0284 static inline int ksft_exit_xpass(void)
0285 {
0286     ksft_print_cnts();
0287     exit(KSFT_XPASS);
0288 }
0289 
0290 static inline int ksft_exit_skip(const char *msg, ...)
0291 {
0292     int saved_errno = errno;
0293     va_list args;
0294 
0295     va_start(args, msg);
0296 
0297     /*
0298      * FIXME: several tests misuse ksft_exit_skip so produce
0299      * something sensible if some tests have already been run
0300      * or a plan has been printed.  Those tests should use
0301      * ksft_test_result_skip or ksft_exit_fail_msg instead.
0302      */
0303     if (ksft_plan || ksft_test_num()) {
0304         ksft_cnt.ksft_xskip++;
0305         printf("ok %d # SKIP ", 1 + ksft_test_num());
0306     } else {
0307         printf("1..0 # SKIP ");
0308     }
0309     if (msg) {
0310         errno = saved_errno;
0311         vprintf(msg, args);
0312         va_end(args);
0313     }
0314     if (ksft_test_num())
0315         ksft_print_cnts();
0316     exit(KSFT_SKIP);
0317 }
0318 
0319 #endif /* __KSELFTEST_H */