]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/failtest_helper.c
tdb2: unify tdb1_store into tdb_store
[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, URANDOM_OPEN)
89             || failmatch(last, URANDOM_READ)) {
90                 if (find_repeat(history, last, last))
91                         return FAIL_DONT_FAIL;
92                 return FAIL_PROBE;
93         }
94
95         /* Unlock or non-blocking lock is fail-once. */
96         if (is_unlock(last)) {
97                 /* Find a previous unlock at this point? */
98                 for (i = find_repeat(history, last, last);
99                      i;
100                      i = find_repeat(history, i, last)) {
101                         if (is_unlock(i))
102                                 return FAIL_DONT_FAIL;
103                 }
104                 return FAIL_PROBE;
105         } else if (is_nonblocking_lock(last)) {
106                 /* Find a previous non-blocking lock at this point? */
107                 for (i = find_repeat(history, last, last);
108                      i;
109                      i = find_repeat(history, i, last)) {
110                         if (is_nonblocking_lock(i))
111                                 return FAIL_DONT_FAIL;
112                 }
113                 return FAIL_PROBE;
114         }
115
116         return FAIL_OK;
117 }