| 1 |
/* |
| 2 |
* Copyright © 2024 Red Hat, Inc. |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice (including the next |
| 12 |
* paragraph) shall be included in all copies or substantial portions of the |
| 13 |
* Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 |
* DEALINGS IN THE SOFTWARE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#pragma once |
| 25 |
|
| 26 |
#include "config.h" |
| 27 |
|
| 28 |
#include "util-mem.h" |
| 29 |
#include "util-range.h" |
| 30 |
|
| 31 |
#include "litest.h" |
| 32 |
|
| 33 |
#define LITEST_RUNNER_DEFAULT_TIMEOUT 30 |
| 34 |
|
| 35 |
/** |
| 36 |
* Result returned from tests or suites. |
| 37 |
*/ |
| 38 |
enum litest_runner_result { |
| 39 |
LITEST_PASS = 75, /**< test successful */ |
| 40 |
LITEST_FAIL = 76, /**< test failed. Should not be returned directly, |
| 41 |
Use the litest_ macros instead */ |
| 42 |
LITEST_SKIP = 77, /**< test was skipped */ |
| 43 |
LITEST_NOT_APPLICABLE = 78, /**< test does not apply */ |
| 44 |
LITEST_TIMEOUT = 79, /**< test aborted after timeout */ |
| 45 |
LITEST_SYSTEM_ERROR = 80, /**< unrelated error occurred */ |
| 46 |
}; |
| 47 |
|
| 48 |
/* For parametrized tests (litest_add_parametrized and friends) |
| 49 |
* a list of these is passed to every test. This struct isn't used |
| 50 |
* directly, use litest_test_param_fetch() instead. |
| 51 |
*/ |
| 52 |
struct litest_test_param { |
| 53 |
struct list link; |
| 54 |
char name[128]; |
| 55 |
struct multivalue value; |
| 56 |
}; |
| 57 |
|
| 58 |
struct litest_test_parameters { |
| 59 |
int refcnt; |
| 60 |
struct list test_params; |
| 61 |
}; |
| 62 |
|
| 63 |
struct litest_test_parameters * |
| 64 |
litest_test_parameters_new(void); |
| 65 |
|
| 66 |
struct litest_test_parameters * |
| 67 |
litest_test_parameters_unref(struct litest_test_parameters *params); |
| 68 |
|
| 69 |
#define litest_test_param_fetch(...) \ |
| 70 |
_litest_test_param_fetch(__VA_ARGS__, NULL) |
| 71 |
|
| 72 |
void |
| 73 |
_litest_test_param_fetch(const struct litest_test_parameters *params, ...); |
| 74 |
|
| 75 |
static inline const char * |
| 76 |
litest_test_param_get_string(const struct litest_test_parameters *params, |
| 77 |
const char *name) |
| 78 |
{ |
| 79 |
const char *p; |
| 80 |
litest_test_param_fetch(params, name, 's', &p); |
| 81 |
return p; |
| 82 |
} |
| 83 |
|
| 84 |
static inline bool |
| 85 |
litest_test_param_get_bool(const struct litest_test_parameters *params, |
| 86 |
const char *name) |
| 87 |
{ |
| 88 |
bool p; |
| 89 |
litest_test_param_fetch(params, name, 'b', &p); |
| 90 |
return p; |
| 91 |
} |
| 92 |
|
| 93 |
static inline int32_t |
| 94 |
litest_test_param_get_i32(const struct litest_test_parameters *params, const char *name) |
| 95 |
{ |
| 96 |
int32_t p; |
| 97 |
litest_test_param_fetch(params, name, 'i', &p); |
| 98 |
return p; |
| 99 |
} |
| 100 |
|
| 101 |
static inline uint32_t |
| 102 |
litest_test_param_get_u32(const struct litest_test_parameters *params, const char *name) |
| 103 |
{ |
| 104 |
uint32_t p; |
| 105 |
litest_test_param_fetch(params, name, 'u', &p); |
| 106 |
return p; |
| 107 |
} |
| 108 |
|
| 109 |
static inline char |
| 110 |
litest_test_param_get_char(const struct litest_test_parameters *params, |
| 111 |
const char *name) |
| 112 |
{ |
| 113 |
char p; |
| 114 |
litest_test_param_fetch(params, name, 'c', &p); |
| 115 |
return p; |
| 116 |
} |
| 117 |
|
| 118 |
static inline double |
| 119 |
litest_test_param_get_double(const struct litest_test_parameters *params, |
| 120 |
const char *name) |
| 121 |
{ |
| 122 |
double p; |
| 123 |
litest_test_param_fetch(params, name, 'd', &p); |
| 124 |
return p; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* This struct is passed into every test. |
| 129 |
*/ |
| 130 |
struct litest_runner_test_env { |
| 131 |
int rangeval; /* The current value within the args.range (or 0) */ |
| 132 |
const struct litest_test_parameters *params; |
| 133 |
}; |
| 134 |
|
| 135 |
struct litest_runner_test_description { |
| 136 |
char name[512]; /* The name of the test */ |
| 137 |
int rangeval; /* The current value within the args.range (or 0) */ |
| 138 |
|
| 139 |
struct litest_test_parameters *params; |
| 140 |
|
| 141 |
/* test function and corresponding setup/teardown, if any */ |
| 142 |
enum litest_runner_result (*func)(const struct litest_runner_test_env *); |
| 143 |
void (*setup)(const struct litest_runner_test_description *); |
| 144 |
void (*teardown)(const struct litest_runner_test_description *); |
| 145 |
|
| 146 |
struct { |
| 147 |
struct range range; /* The range this test applies to */ |
| 148 |
int signal; /* expected signal for fail tests */ |
| 149 |
} args; |
| 150 |
}; |
| 151 |
|
| 152 |
struct litest_runner; |
| 153 |
|
| 154 |
struct litest_runner * |
| 155 |
litest_runner_new(void); |
| 156 |
|
| 157 |
/** |
| 158 |
* Default is nprocs * 2. |
| 159 |
* Setting this to 0 means *no* forking. Setting this to 1 means only one test |
| 160 |
* is run at a time but in a child process. |
| 161 |
*/ |
| 162 |
void |
| 163 |
litest_runner_set_num_parallel(struct litest_runner *runner, size_t num_jobs); |
| 164 |
void |
| 165 |
litest_runner_set_timeout(struct litest_runner *runner, unsigned int timeout); |
| 166 |
void |
| 167 |
litest_runner_set_verbose(struct litest_runner *runner, bool verbose); |
| 168 |
void |
| 169 |
litest_runner_set_use_colors(struct litest_runner *runner, bool use_colors); |
| 170 |
void |
| 171 |
litest_runner_set_exit_on_fail(struct litest_runner *runner, bool do_exit); |
| 172 |
void |
| 173 |
litest_runner_set_output_file(struct litest_runner *runner, FILE *fp); |
| 174 |
void |
| 175 |
litest_runner_add_test(struct litest_runner *runner, |
| 176 |
const struct litest_runner_test_description *t); |
| 177 |
enum litest_runner_result |
| 178 |
litest_runner_run_tests(struct litest_runner *runner); |
| 179 |
|
| 180 |
typedef enum litest_runner_result (*litest_runner_global_setup_func_t)(void *userdata); |
| 181 |
typedef void (*litest_runner_global_teardown_func_t)(void *userdata); |
| 182 |
|
| 183 |
void |
| 184 |
litest_runner_set_setup_funcs(struct litest_runner *runner, |
| 185 |
litest_runner_global_setup_func_t setup, |
| 186 |
litest_runner_global_teardown_func_t teardown, |
| 187 |
void *userdata); |
| 188 |
|
| 189 |
void |
| 190 |
litest_runner_destroy(struct litest_runner *runner); |
| 191 |
|
| 192 |
DEFINE_DESTROY_CLEANUP_FUNC(litest_runner); |
| 193 |
|
| 194 |
/* |
| 195 |
* Function to call abort(). Depending on the number of forks permitted, |
| 196 |
* this function may simply abort() or it may longjmp back out to collect |
| 197 |
* errors from non-forking tests. |
| 198 |
*/ |
| 199 |
__attribute__((noreturn)) void |
| 200 |
litest_runner_abort(void); |