1 #include "failtest_helper.h"
4 #include <ccan/tap/tap.h>
6 /* FIXME: From ccan/str */
7 static inline bool strends(const char *str, const char *postfix)
9 if (strlen(str) < strlen(postfix))
12 return !strcmp(str + strlen(str) - strlen(postfix), postfix);
15 bool failmatch(const struct failtest_call *call,
16 const char *file, int line, enum failtest_call_type type)
18 return call->type == type
20 && ((strcmp(call->file, file) == 0)
21 || (strends(call->file, file)
22 && (call->file[strlen(call->file) - strlen(file) - 1]
26 static const struct failtest_call *
27 find_repeat(const struct failtest_call *start, const struct failtest_call *end,
28 const struct failtest_call *call)
30 const struct failtest_call *i;
32 for (i = start; i < end; i++) {
33 if (failmatch(i, call->file, call->line, call->type))
39 static bool is_nonblocking_lock(const struct failtest_call *call)
41 return call->type == FAILTEST_FCNTL && call->u.fcntl.cmd == F_SETLK;
44 static bool is_unlock(const struct failtest_call *call)
46 return call->type == FAILTEST_FCNTL
47 && call->u.fcntl.arg.fl.l_type == F_UNLCK;
50 bool exit_check_log(struct failtest_call *history, unsigned num)
54 for (i = 0; i < num; i++) {
57 /* Failing the /dev/urandom open doesn't count: we fall back. */
58 if (failmatch(&history[i], URANDOM_OPEN))
61 /* Similarly with read fail. */
62 if (failmatch(&history[i], URANDOM_READ))
65 /* Initial allocation of tdb doesn't log. */
66 if (failmatch(&history[i], INITIAL_TDB_MALLOC))
69 /* We don't block "failures" on non-blocking locks. */
70 if (is_nonblocking_lock(&history[i]))
73 if (!tap_log_messages)
74 diag("We didn't log for %u (%s:%u)",
75 i, history[i].file, history[i].line);
76 return tap_log_messages != 0;
81 /* Some places we soldier on despite errors: only fail them once. */
83 block_repeat_failures(struct failtest_call *history, unsigned num)
85 const struct failtest_call *i, *last = &history[num-1];
87 if (failmatch(last, INITIAL_TDB_MALLOC)
88 || failmatch(last, LOGGING_MALLOC)
89 || failmatch(last, URANDOM_OPEN)
90 || failmatch(last, URANDOM_READ)) {
91 if (find_repeat(history, last, last))
92 return FAIL_DONT_FAIL;
96 /* Unlock or non-blocking lock is fail-once. */
97 if (is_unlock(last)) {
98 /* Find a previous unlock at this point? */
99 for (i = find_repeat(history, last, last);
101 i = find_repeat(history, i, last)) {
103 return FAIL_DONT_FAIL;
106 } else if (is_nonblocking_lock(last)) {
107 /* Find a previous non-blocking lock at this point? */
108 for (i = find_repeat(history, last, last);
110 i = find_repeat(history, i, last)) {
111 if (is_nonblocking_lock(i))
112 return FAIL_DONT_FAIL;