]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/failtest_helper.c
tdb2: suppress failtest more than once on mmap.
[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 bool is_nonblocking_lock(const struct failtest_call *call)
29 {
30         return call->type == FAILTEST_FCNTL && call->u.fcntl.cmd == F_SETLK;
31 }
32
33 static bool is_unlock(const struct failtest_call *call)
34 {
35         return call->type == FAILTEST_FCNTL
36                 && call->u.fcntl.arg.fl.l_type == F_UNLCK;
37 }
38
39 bool exit_check_log(struct tlist_calls *history)
40 {
41         const struct failtest_call *i;
42
43         tlist_for_each(history, i, list) {
44                 if (!i->fail)
45                         continue;
46                 /* Failing the /dev/urandom open doesn't count: we fall back. */
47                 if (failmatch(i, URANDOM_OPEN))
48                         continue;
49
50                 /* Similarly with read fail. */
51                 if (failmatch(i, URANDOM_READ))
52                         continue;
53
54                 /* Initial allocation of tdb doesn't log. */
55                 if (failmatch(i, INITIAL_TDB_MALLOC))
56                         continue;
57
58                 /* We don't block "failures" on non-blocking locks. */
59                 if (is_nonblocking_lock(i))
60                         continue;
61
62                 if (!tap_log_messages)
63                         diag("We didn't log for %s:%u", i->file, i->line);
64                 return tap_log_messages != 0;
65         }
66         return true;
67 }
68
69 /* Some places we soldier on despite errors: only fail them once. */
70 enum failtest_result
71 block_repeat_failures(struct tlist_calls *history)
72 {
73         const struct failtest_call *last;
74
75         last = tlist_tail(history, struct failtest_call, list);
76
77         if (failtest_suppress)
78                 return FAIL_DONT_FAIL;
79
80         if (failmatch(last, INITIAL_TDB_MALLOC)
81             || failmatch(last, URANDOM_OPEN)
82             || failmatch(last, URANDOM_READ)) {
83                 return FAIL_PROBE;
84         }
85
86         /* We handle mmap failing, by falling back to read/write, so
87          * don't try all possible paths. */
88         if (last->type == FAILTEST_MMAP)
89                 return FAIL_PROBE;
90
91         /* Unlock or non-blocking lock is fail-once. */
92         if (is_unlock(last) || is_nonblocking_lock(last))
93                 return FAIL_PROBE;
94
95         return FAIL_OK;
96 }