s3:registry: do not use regdb functions during db upgrade
[Samba/gebeck_regimport.git] / lib / tdb2 / test / failtest_helper.c
blob1358a6c6b261d3b296a296d123c36693afe07ff1
1 #include "failtest_helper.h"
2 #include "logging.h"
3 #include <string.h>
4 #include <ccan/tap/tap.h>
6 /* FIXME: From ccan/str */
7 static inline bool strends(const char *str, const char *postfix)
9 if (strlen(str) < strlen(postfix))
10 return false;
12 return !strcmp(str + strlen(str) - strlen(postfix), postfix);
15 bool failmatch(const struct failtest_call *call,
16 const char *file, int line, enum failtest_call_type type)
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 == '/')));
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)
30 const struct failtest_call *i;
32 for (i = start; i < end; i++) {
33 if (failmatch(i, call->file, call->line, call->type))
34 return i;
36 return NULL;
39 static bool is_nonblocking_lock(const struct failtest_call *call)
41 return call->type == FAILTEST_FCNTL && call->u.fcntl.cmd == F_SETLK;
44 static bool is_unlock(const struct failtest_call *call)
46 return call->type == FAILTEST_FCNTL
47 && call->u.fcntl.arg.fl.l_type == F_UNLCK;
50 bool exit_check_log(struct failtest_call *history, unsigned num)
52 unsigned int i;
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;
61 /* Similarly with read fail. */
62 if (failmatch(&history[i], URANDOM_READ))
63 continue;
65 /* Initial allocation of tdb doesn't log. */
66 if (failmatch(&history[i], INITIAL_TDB_MALLOC))
67 continue;
69 /* We don't block "failures" on non-blocking locks. */
70 if (is_nonblocking_lock(&history[i]))
71 continue;
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;
78 return true;
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)
85 const struct failtest_call *i, *last = &history[num-1];
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;
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);
100 i = find_repeat(history, i, last)) {
101 if (is_unlock(i))
102 return FAIL_DONT_FAIL;
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);
109 i = find_repeat(history, i, last)) {
110 if (is_nonblocking_lock(i))
111 return FAIL_DONT_FAIL;
113 return FAIL_PROBE;
116 return FAIL_OK;