tdb2: failtest: use a linked list for history, not an array.
[Samba/gebeck_regimport.git] / lib / tdb2 / test / failtest_helper.c
blobf3ef09a6c3e84b33d0ca1807967505771835f17c
1 #include "failtest_helper.h"
2 #include "logging.h"
3 #include <string.h>
4 #include <ccan/tap/tap.h>
6 bool failtest_suppress = false;
8 /* FIXME: From ccan/str */
9 static inline bool strends(const char *str, const char *postfix)
11 if (strlen(str) < strlen(postfix))
12 return false;
14 return !strcmp(str + strlen(str) - strlen(postfix), postfix);
17 bool failmatch(const struct failtest_call *call,
18 const char *file, int line, enum failtest_call_type type)
20 return call->type == type
21 && call->line == line
22 && ((strcmp(call->file, file) == 0)
23 || (strends(call->file, file)
24 && (call->file[strlen(call->file) - strlen(file) - 1]
25 == '/')));
28 static const struct failtest_call *
29 find_repeat(const struct tlist_calls *history,
30 const struct failtest_call *call)
32 const struct failtest_call *i;
34 tlist_for_each(history, i, list) {
35 if (i != call)
36 continue;
37 if (failmatch(i, call->file, call->line, call->type))
38 return i;
40 return NULL;
43 static bool is_nonblocking_lock(const struct failtest_call *call)
45 return call->type == FAILTEST_FCNTL && call->u.fcntl.cmd == F_SETLK;
48 static bool is_unlock(const struct failtest_call *call)
50 return call->type == FAILTEST_FCNTL
51 && call->u.fcntl.arg.fl.l_type == F_UNLCK;
54 bool exit_check_log(struct tlist_calls *history)
56 const struct failtest_call *i;
58 tlist_for_each(history, i, list) {
59 if (!i->fail)
60 continue;
61 /* Failing the /dev/urandom open doesn't count: we fall back. */
62 if (failmatch(i, URANDOM_OPEN))
63 continue;
65 /* Similarly with read fail. */
66 if (failmatch(i, URANDOM_READ))
67 continue;
69 /* Initial allocation of tdb doesn't log. */
70 if (failmatch(i, INITIAL_TDB_MALLOC))
71 continue;
73 /* We don't block "failures" on non-blocking locks. */
74 if (is_nonblocking_lock(i))
75 continue;
77 if (!tap_log_messages)
78 diag("We didn't log for %s:%u", i->file, i->line);
79 return tap_log_messages != 0;
81 return true;
84 /* Some places we soldier on despite errors: only fail them once. */
85 enum failtest_result
86 block_repeat_failures(struct tlist_calls *history)
88 const struct failtest_call *i, *last;
90 last = tlist_tail(history, struct failtest_call, list);
92 if (failtest_suppress)
93 return FAIL_DONT_FAIL;
95 if (failmatch(last, INITIAL_TDB_MALLOC)
96 || failmatch(last, URANDOM_OPEN)
97 || failmatch(last, URANDOM_READ)) {
98 if (find_repeat(history, last))
99 return FAIL_DONT_FAIL;
100 return FAIL_PROBE;
103 /* Unlock or non-blocking lock is fail-once. */
104 if (is_unlock(last)) {
105 /* Find a previous unlock at this point? */
106 i = find_repeat(history, last);
107 if (i && is_unlock(i))
108 return FAIL_DONT_FAIL;
109 return FAIL_PROBE;
110 } else if (is_nonblocking_lock(last)) {
111 /* Find a previous non-blocking lock at this point? */
112 i = find_repeat(history, last);
113 if (i && is_nonblocking_lock(i))
114 return FAIL_DONT_FAIL;
115 return FAIL_PROBE;
118 return FAIL_OK;