docs: Update man 7 samba.
[Samba/gebeck_regimport.git] / lib / ntdb / test / failtest_helper.c
blob45b24512e9936ae8b01ba8b45e7947764cc94886
1 #include "failtest_helper.h"
2 #include "logging.h"
3 #include <string.h>
4 #include "tap-interface.h"
6 bool failtest_suppress = false;
8 /* FIXME: From ccan/str */
9 static inline bool strends(const char *str, const char *postfix)
11 if (strlen(str) < strlen(postfix))
12 return false;
14 return !strcmp(str + strlen(str) - strlen(postfix), postfix);
17 bool failmatch(const struct failtest_call *call,
18 const char *file, int line, enum failtest_call_type type)
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 == '/')));
28 static bool is_nonblocking_lock(const struct failtest_call *call)
30 return call->type == FAILTEST_FCNTL && call->u.fcntl.cmd == F_SETLK;
33 static bool is_unlock(const struct failtest_call *call)
35 return call->type == FAILTEST_FCNTL
36 && call->u.fcntl.arg.fl.l_type == F_UNLCK;
39 bool exit_check_log(struct tlist_calls *history)
41 const struct failtest_call *i;
42 unsigned int malloc_count = 0;
44 tlist_for_each(history, i, list) {
45 if (!i->fail)
46 continue;
47 /* Failing the /dev/urandom open doesn't count: we fall back. */
48 if (failmatch(i, URANDOM_OPEN))
49 continue;
51 /* Similarly with read fail. */
52 if (failmatch(i, URANDOM_READ))
53 continue;
55 /* Initial allocation of ntdb doesn't log. */
56 if (i->type == FAILTEST_MALLOC) {
57 if (malloc_count++ == 0) {
58 continue;
62 /* We don't block "failures" on non-blocking locks. */
63 if (is_nonblocking_lock(i))
64 continue;
66 if (!tap_log_messages)
67 diag("We didn't log for %s:%u", i->file, i->line);
68 return tap_log_messages != 0;
70 return true;
73 /* Some places we soldier on despite errors: only fail them once. */
74 enum failtest_result
75 block_repeat_failures(struct tlist_calls *history)
77 const struct failtest_call *last;
79 last = tlist_tail(history, list);
81 if (failtest_suppress)
82 return FAIL_DONT_FAIL;
84 if (failmatch(last, URANDOM_OPEN)
85 || failmatch(last, URANDOM_READ)) {
86 return FAIL_PROBE;
89 /* We handle mmap failing, by falling back to read/write, so
90 * don't try all possible paths. */
91 if (last->type == FAILTEST_MMAP)
92 return FAIL_PROBE;
94 /* Unlock or non-blocking lock is fail-once. */
95 if (is_unlock(last) || is_nonblocking_lock(last))
96 return FAIL_PROBE;
98 return FAIL_OK;