1 /* Licensed under LGPL - see LICENSE file for details */
2 #ifndef CCAN_FAILTEST_H
3 #define CCAN_FAILTEST_H
5 #if HAVE_FILE_OFFSET_BITS
6 #define _FILE_OFFSET_BITS 64
11 #include <ccan/compiler/compiler.h>
14 * failtest_init - initialize the failtest module
15 * @argc: the number of commandline arguments
16 * @argv: the commandline argument array
18 * This initializes the module, and in particular if argv[1] is "--failpath="
19 * then it ensures that failures follow that pattern. This allows easy
20 * debugging of complex failure paths.
22 void failtest_init(int argc, char *argv[]);
25 * failtest_exit - clean up and exit the test
26 * @status: the status (usually exit_status() from ccan/tap).
28 * This cleans up and changes to files made in this child, and exits the test.
29 * It also calls your failtest_default_hook, if any.
31 * A child which does not exit via failtest_exit() will cause the overall test
34 void NORETURN failtest_exit(int status);
37 * enum failtest_call_type - discriminator for failtest_call.u
39 enum failtest_call_type {
113 * struct failtest_call - description of a call redirected to failtest module
114 * @type: the call type
115 * @file: the filename of the caller
116 * @line: the line number of the caller
117 * @fail: did this call fail
118 * @error: the errno (if any)
119 * @u: the union of call data
121 * This structure is used to represent the ordered history of calls.
124 * failtest_hook, failtest_exit_check
126 struct failtest_call {
127 enum failtest_call_type type;
128 /* Where we were called from. */
133 /* What we set errno to. */
135 /* How do we clean this up? */
136 void (*cleanup)(void *u);
137 /* The actual call data. */
139 struct calloc_call calloc;
140 struct malloc_call malloc;
141 struct realloc_call realloc;
142 struct open_call open;
143 struct close_call close;
144 struct pipe_call pipe;
145 struct read_call read;
146 struct write_call write;
147 struct fcntl_call fcntl;
151 enum failtest_result {
152 /* Yes try failing this call. */
154 /* No, don't try failing this call. */
156 /* Try failing this call but don't go too far down that path. */
161 * failtest_hook - whether a certain call should fail or not.
162 * @history: the ordered history of all failtest calls.
163 * @num: the number of elements in @history (greater than 0)
165 * The default value of this hook is failtest_default_hook(), which returns
166 * FAIL_OK (ie. yes, fail the call).
168 * You can override it, and avoid failing certain calls. The parameters
169 * of the call (but not the return value(s)) will be filled in for the last
173 * static enum failtest_result dont_fail_alloc(struct failtest_call *hist,
176 * if (hist[num-1].type == FAILTEST_MALLOC
177 * || hist[num-1].type == FAILTEST_CALLOC
178 * || hist[num-1].type == FAILTEST_REALLOC)
179 * return FAIL_DONT_FAIL;
183 * failtest_hook = dont_fail_alloc;
185 extern enum failtest_result
186 (*failtest_hook)(struct failtest_call *history, unsigned num);
189 * failtest_exit_check - hook for additional checks on a failed child.
190 * @history: the ordered history of all failtest calls.
191 * @num: the number of elements in @history (greater than 0)
193 * Your program might have additional checks to do on failure, such as
194 * check that a file is not corrupted, or than an error message has been
197 * If this returns false, the path to this failure will be printed and the
198 * overall test will fail.
200 extern bool (*failtest_exit_check)(struct failtest_call *history,
204 * failtest_has_failed - determine if a failure has occurred.
206 * Sometimes you want to exit immediately if you've experienced a failure.
207 * This is useful when you have four separate tests in your test suite,
208 * and you don't want to do the next one if you've had a failure in a
211 extern bool failtest_has_failed(void);
214 * failtest_timeout_ms - how long to wait before killing child.
216 * Default is 20,000 (20 seconds).
218 extern unsigned int failtest_timeout_ms;
219 #endif /* CCAN_FAILTEST_H */