]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/failtest_helper.c
d24ac4c42b11077693f5126293e095eb2963e4ea
[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 bool failtest_suppress = false;
7
8 /* FIXME: From ccan/str */
9 static inline bool strends(const char *str, const char *postfix)
10 {
11         if (strlen(str) < strlen(postfix))
12                 return false;
13
14         return !strcmp(str + strlen(str) - strlen(postfix), postfix);
15 }
16
17 bool failmatch(const struct failtest_call *call,
18                const char *file, int line, enum failtest_call_type type)
19 {
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                             == '/')));
26 }
27
28 static const struct failtest_call *
29 find_repeat(const struct failtest_call *start, const struct failtest_call *end,
30             const struct failtest_call *call)
31 {
32         const struct failtest_call *i;
33
34         for (i = start; i < end; i++) {
35                 if (failmatch(i, call->file, call->line, call->type))
36                         return i;
37         }
38         return NULL;
39 }
40
41 static bool is_nonblocking_lock(const struct failtest_call *call)
42 {
43         return call->type == FAILTEST_FCNTL && call->u.fcntl.cmd == F_SETLK;
44 }
45
46 static bool is_unlock(const struct failtest_call *call)
47 {
48         return call->type == FAILTEST_FCNTL
49                 && call->u.fcntl.arg.fl.l_type == F_UNLCK;
50 }
51
52 bool exit_check_log(struct failtest_call *history, unsigned num)
53 {
54         unsigned int i;
55
56         for (i = 0; i < num; i++) {
57                 if (!history[i].fail)
58                         continue;
59                 /* Failing the /dev/urandom open doesn't count: we fall back. */
60                 if (failmatch(&history[i], URANDOM_OPEN))
61                         continue;
62
63                 /* Similarly with read fail. */
64                 if (failmatch(&history[i], URANDOM_READ))
65                         continue;
66
67                 /* Initial allocation of tdb doesn't log. */
68                 if (failmatch(&history[i], INITIAL_TDB_MALLOC))
69                         continue;
70
71                 /* We don't block "failures" on non-blocking locks. */
72                 if (is_nonblocking_lock(&history[i]))
73                         continue;
74
75                 if (!tap_log_messages)
76                         diag("We didn't log for %u (%s:%u)",
77                              i, history[i].file, history[i].line);
78                 return tap_log_messages != 0;
79         }
80         return true;
81 }
82
83 /* Some places we soldier on despite errors: only fail them once. */
84 enum failtest_result
85 block_repeat_failures(struct failtest_call *history, unsigned num)
86 {
87         const struct failtest_call *i, *last = &history[num-1];
88
89         if (failtest_suppress)
90                 return FAIL_DONT_FAIL;
91
92         if (failmatch(last, INITIAL_TDB_MALLOC)
93             || failmatch(last, URANDOM_OPEN)
94             || failmatch(last, URANDOM_READ)) {
95                 if (find_repeat(history, last, last))
96                         return FAIL_DONT_FAIL;
97                 return FAIL_PROBE;
98         }
99
100         /* Unlock or non-blocking lock is fail-once. */
101         if (is_unlock(last)) {
102                 /* Find a previous unlock at this point? */
103                 for (i = find_repeat(history, last, last);
104                      i;
105                      i = find_repeat(history, i, last)) {
106                         if (is_unlock(i))
107                                 return FAIL_DONT_FAIL;
108                 }
109                 return FAIL_PROBE;
110         } else if (is_nonblocking_lock(last)) {
111                 /* Find a previous non-blocking lock at this point? */
112                 for (i = find_repeat(history, last, last);
113                      i;
114                      i = find_repeat(history, i, last)) {
115                         if (is_nonblocking_lock(i))
116                                 return FAIL_DONT_FAIL;
117                 }
118                 return FAIL_PROBE;
119         }
120
121         return FAIL_OK;
122 }