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>
12 #include <ccan/tlist/tlist.h>
15 * failtest_init - initialize the failtest module
16 * @argc: the number of commandline arguments
17 * @argv: the commandline argument array
19 * This initializes the module, and in particular if argv[1] is "--failpath="
20 * then it ensures that failures follow that pattern. This allows easy
21 * debugging of complex failure paths.
23 void failtest_init(int argc
, char *argv
[]);
26 * failtest_exit - clean up and exit the test
27 * @status: the status (usually exit_status() from ccan/tap).
29 * This cleans up and changes to files made in this child, and exits the test.
30 * It also calls your failtest_default_hook, if any.
32 * A child which does not exit via failtest_exit() will cause the overall test
35 void NORETURN
failtest_exit(int status
);
38 * enum failtest_call_type - discriminator for failtest_call.u
40 enum failtest_call_type
{
78 /* This is used for O_TRUNC opens on existing files. */
79 struct contents_saved
*saved
;
107 struct failtest_call
*opener
;
108 struct contents_saved
*saved
;
130 struct failtest_call
*opener
;
131 struct contents_saved
*saved
;
143 * struct failtest_call - description of a call redirected to failtest module
144 * @type: the call type
145 * @file: the filename of the caller
146 * @line: the line number of the caller
147 * @fail: did this call fail
148 * @error: the errno (if any)
149 * @u: the union of call data
151 * This structure is used to represent the ordered history of calls.
154 * failtest_hook, failtest_exit_check
156 struct failtest_call
{
157 /* We're in the history list. */
158 struct ccan_list_node list
;
159 enum failtest_call_type type
;
160 /* Where we were called from. */
165 /* What we set errno to. */
167 /* How do we clean this up? */
168 void (*cleanup
)(void *u
, bool restore
);
169 /* Should their program have cleaned up? */
171 /* Backtrace of call chain. */
173 unsigned int backtrace_num
;
174 /* The actual call data. */
176 struct calloc_call calloc
;
177 struct malloc_call malloc
;
178 struct realloc_call realloc
;
179 struct open_call open
;
180 struct close_call close
;
181 struct pipe_call pipe
;
182 struct read_call read
;
183 struct write_call write
;
184 struct fcntl_call fcntl
;
185 struct mmap_call mmap
;
186 struct lseek_call lseek
;
190 /* This defines struct tlist_calls. */
191 TLIST_TYPE(calls
, struct failtest_call
);
193 enum failtest_result
{
194 /* Yes try failing this call. */
196 /* No, don't try failing this call. */
198 /* Try failing this call but don't go too far down that path. */
203 * failtest_hook - whether a certain call should fail or not.
204 * @history: the ordered history of all failtest calls.
206 * The default value of this hook is failtest_default_hook(), which returns
207 * FAIL_OK (ie. yes, fail the call).
209 * You can override it, and avoid failing certain calls. The parameters
210 * of the call (but not the return value(s)) will be filled in for the last
214 * static enum failtest_result dont_fail_alloc(struct tlist_calls *history)
216 * struct failtest_call *call;
217 * call = tlist_tail(history, list);
218 * if (call->type == FAILTEST_MALLOC
219 * || call->type == FAILTEST_CALLOC
220 * || call->type == FAILTEST_REALLOC)
221 * return FAIL_DONT_FAIL;
225 * failtest_hook = dont_fail_alloc;
227 extern enum failtest_result (*failtest_hook
)(struct tlist_calls
*history
);
230 * failtest_exit_check - hook for additional checks on a failed child.
231 * @history: the ordered history of all failtest calls.
233 * Your program might have additional checks to do on failure, such as
234 * check that a file is not corrupted, or than an error message has been
237 * If this returns false, the path to this failure will be printed and the
238 * overall test will fail.
240 extern bool (*failtest_exit_check
)(struct tlist_calls
*history
);
243 * failtest_has_failed - determine if a failure has occurred.
245 * Sometimes you want to exit immediately if you've experienced an
246 * injected failure. This is useful when you have four separate tests
247 * in your test suite, and you don't want to do the next one if you've
248 * had a failure in a previous one.
250 extern bool failtest_has_failed(void);
253 * failtest_timeout_ms - how long to wait before killing child.
255 * Default is 20,000 (20 seconds).
257 extern unsigned int failtest_timeout_ms
;
258 #endif /* CCAN_FAILTEST_H */