tdb2: failtest on tdb_expand
[ccan] / ccan / tdb2 / test / failtest_helper.c
1 #include "failtest_helper.h"
2 #include "logging.h"
3 #include <string.h>
4 #include <ccan/tap/tap.h>
5
6 /* FIXME: From ccan/str */
7 static inline bool strends(const char *str, const char *postfix)
8 {
9         if (strlen(str) < strlen(postfix))
10                 return false;
11
12         return !strcmp(str + strlen(str) - strlen(postfix), postfix);
13 }
14
15 bool failmatch(const struct failtest_call *call,
16                const char *file, int line, enum failtest_call_type type)
17 {
18         return call->type == type
19                 && call->line == line
20                 && ((strcmp(call->file, file) == 0)
21                     || (strends(call->file, file)
22                         && (call->file[strlen(call->file) - strlen(file) - 1]
23                             == '/')));
24 }
25
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)
29 {
30         const struct failtest_call *i;
31
32         for (i = start; i < end; i++) {
33                 if (failmatch(i, call->file, call->line, call->type))
34                         return i;
35         }
36         return NULL;
37 }
38
39 static bool is_nonblocking_lock(const struct failtest_call *call)
40 {
41         return call->type == FAILTEST_FCNTL && call->u.fcntl.cmd == F_SETLK;
42 }
43
44 static bool is_unlock(const struct failtest_call *call)
45 {
46         return call->type == FAILTEST_FCNTL
47                 && call->u.fcntl.arg.fl.l_type == F_UNLCK;
48 }
49
50 bool exit_check_log(struct failtest_call *history, unsigned num)
51 {
52         unsigned int i;
53
54         for (i = 0; i < num; i++) {
55                 if (!history[i].fail)
56                         continue;
57                 /* Failing the /dev/urandom open doesn't count: we fall back. */
58                 if (failmatch(&history[i], URANDOM_OPEN))
59                         continue;
60
61                 /* Similarly with read fail. */
62                 if (failmatch(&history[i], URANDOM_READ))
63                         continue;
64
65                 /* Initial allocation of tdb doesn't log. */
66                 if (failmatch(&history[i], INITIAL_TDB_MALLOC))
67                         continue;
68
69                 /* We don't block "failures" on non-blocking locks. */
70                 if (is_nonblocking_lock(&history[i]))
71                         continue;
72
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;
77         }
78         return true;
79 }
80
81 /* Some places we soldier on despite errors: only fail them once. */
82 enum failtest_result
83 block_repeat_failures(struct failtest_call *history, unsigned num)
84 {
85         const struct failtest_call *i, *last = &history[num-1];
86
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;
93                 return FAIL_PROBE;
94         }
95
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);
100                      i;
101                      i = find_repeat(history, i, last)) {
102                         if (is_unlock(i))
103                                 return FAIL_DONT_FAIL;
104                 }
105                 return FAIL_PROBE;
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);
109                      i;
110                      i = find_repeat(history, i, last)) {
111                         if (is_nonblocking_lock(i))
112                                 return FAIL_DONT_FAIL;
113                 }
114                 return FAIL_PROBE;
115         }
116
117         return FAIL_OK;
118 }