ccan/ntdb: demote to junkcode.
[ccan] / junkcode / rusty@rustcorp.com.au-ntdb / test / failtest_helper.c
1 #include "failtest_helper.h"
2 #include "logging.h"
3 #include <string.h>
4 #include "tap-interface.h"
5
6 bool failtest_suppress = false;
7
8 bool failmatch(const struct failtest_call *call,
9                const char *file, int line, enum failtest_call_type type)
10 {
11         return call->type == type
12                 && call->line == line
13                 && ((strcmp(call->file, file) == 0)
14                     || (strends(call->file, file)
15                         && (call->file[strlen(call->file) - strlen(file) - 1]
16                             == '/')));
17 }
18
19 static bool is_nonblocking_lock(const struct failtest_call *call)
20 {
21         return call->type == FAILTEST_FCNTL && call->u.fcntl.cmd == F_SETLK;
22 }
23
24 static bool is_unlock(const struct failtest_call *call)
25 {
26         return call->type == FAILTEST_FCNTL
27                 && call->u.fcntl.arg.fl.l_type == F_UNLCK;
28 }
29
30 bool exit_check_log(struct tlist_calls *history)
31 {
32         const struct failtest_call *i;
33         unsigned int malloc_count = 0;
34
35         tlist_for_each(history, i, list) {
36                 if (!i->fail)
37                         continue;
38                 /* Failing the /dev/urandom open doesn't count: we fall back. */
39                 if (failmatch(i, URANDOM_OPEN))
40                         continue;
41
42                 /* Similarly with read fail. */
43                 if (failmatch(i, URANDOM_READ))
44                         continue;
45
46                 /* Initial allocation of ntdb doesn't log. */
47                 if (i->type == FAILTEST_MALLOC) {
48                         if (malloc_count++ == 0) {
49                                 continue;
50                         }
51                 }
52
53                 /* We don't block "failures" on non-blocking locks. */
54                 if (is_nonblocking_lock(i))
55                         continue;
56
57                 if (!tap_log_messages)
58                         diag("We didn't log for %s:%u", i->file, i->line);
59                 return tap_log_messages != 0;
60         }
61         return true;
62 }
63
64 /* Some places we soldier on despite errors: only fail them once. */
65 enum failtest_result
66 block_repeat_failures(struct tlist_calls *history)
67 {
68         const struct failtest_call *last;
69
70         last = tlist_tail(history, list);
71
72         if (failtest_suppress)
73                 return FAIL_DONT_FAIL;
74
75         if (failmatch(last, URANDOM_OPEN)
76             || failmatch(last, URANDOM_READ)) {
77                 return FAIL_PROBE;
78         }
79
80         /* We handle mmap failing, by falling back to read/write, so
81          * don't try all possible paths. */
82         if (last->type == FAILTEST_MMAP)
83                 return FAIL_PROBE;
84
85         /* Unlock or non-blocking lock is fail-once. */
86         if (is_unlock(last) || is_nonblocking_lock(last))
87                 return FAIL_PROBE;
88
89         return FAIL_OK;
90 }