tdb: don't use err.h in tests.
[Samba/gebeck_regimport.git] / lib / tdb / test / run-die-during-transaction.c
blob19c9dbebf93c7ef45184d6708654dc50249eb0db
1 #include "../common/tdb_private.h"
2 #include "lock-tracking.h"
3 static ssize_t pwrite_check(int fd, const void *buf, size_t count, off_t offset);
4 static ssize_t write_check(int fd, const void *buf, size_t count);
5 static int ftruncate_check(int fd, off_t length);
7 #define pwrite pwrite_check
8 #define write write_check
9 #define fcntl fcntl_with_lockcheck
10 #define ftruncate ftruncate_check
12 #include "../common/io.c"
13 #include "../common/tdb.c"
14 #include "../common/lock.c"
15 #include "../common/freelist.c"
16 #include "../common/traverse.c"
17 #include "../common/transaction.c"
18 #include "../common/error.c"
19 #include "../common/open.c"
20 #include "../common/check.c"
21 #include "../common/hash.c"
22 #include "tap-interface.h"
23 #include <stdlib.h>
24 #include <stdbool.h>
25 #include <stdarg.h>
26 #include <setjmp.h>
27 #include "external-agent.h"
28 #include "logging.h"
30 #undef write
31 #undef pwrite
32 #undef fcntl
33 #undef ftruncate
35 static bool in_transaction;
36 static int target, current;
37 static jmp_buf jmpbuf;
38 #define TEST_DBNAME "run-die-during-transaction.tdb"
39 #define KEY_STRING "helloworld"
41 static void maybe_die(int fd)
43 if (in_transaction && current++ == target) {
44 longjmp(jmpbuf, 1);
48 static ssize_t pwrite_check(int fd,
49 const void *buf, size_t count, off_t offset)
51 ssize_t ret;
53 maybe_die(fd);
55 ret = pwrite(fd, buf, count, offset);
56 if (ret != count)
57 return ret;
59 maybe_die(fd);
60 return ret;
63 static ssize_t write_check(int fd, const void *buf, size_t count)
65 ssize_t ret;
67 maybe_die(fd);
69 ret = write(fd, buf, count);
70 if (ret != count)
71 return ret;
73 maybe_die(fd);
74 return ret;
77 static int ftruncate_check(int fd, off_t length)
79 int ret;
81 maybe_die(fd);
83 ret = ftruncate(fd, length);
85 maybe_die(fd);
86 return ret;
89 static bool test_death(enum operation op, struct agent *agent)
91 struct tdb_context *tdb = NULL;
92 TDB_DATA key;
93 enum agent_return ret;
94 int needed_recovery = 0;
96 current = target = 0;
97 reset:
98 unlink(TEST_DBNAME);
99 tdb = tdb_open_ex(TEST_DBNAME, 1024, TDB_NOMMAP,
100 O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
102 if (setjmp(jmpbuf) != 0) {
103 /* We're partway through. Simulate our death. */
104 close(tdb->fd);
105 forget_locking();
106 in_transaction = false;
108 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
109 if (ret == SUCCESS)
110 needed_recovery++;
111 else if (ret != FAILED) {
112 diag("Step %u agent NEEDS_RECOVERY = %s", current,
113 agent_return_name(ret));
114 return false;
117 ret = external_agent_operation(agent, op, KEY_STRING);
118 if (ret != SUCCESS) {
119 diag("Step %u op %s failed = %s", current,
120 operation_name(op),
121 agent_return_name(ret));
122 return false;
125 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
126 if (ret != FAILED) {
127 diag("Still needs recovery after step %u = %s",
128 current, agent_return_name(ret));
129 return false;
132 ret = external_agent_operation(agent, CHECK, "");
133 if (ret != SUCCESS) {
134 diag("Step %u check failed = %s", current,
135 agent_return_name(ret));
136 return false;
139 ret = external_agent_operation(agent, CLOSE, "");
140 if (ret != SUCCESS) {
141 diag("Step %u close failed = %s", current,
142 agent_return_name(ret));
143 return false;
146 /* Suppress logging as this tries to use closed fd. */
147 suppress_logging = true;
148 suppress_lockcheck = true;
149 tdb_close(tdb);
150 suppress_logging = false;
151 suppress_lockcheck = false;
152 target++;
153 current = 0;
154 goto reset;
157 /* Put key for agent to fetch. */
158 key.dsize = strlen(KEY_STRING);
159 key.dptr = (void *)KEY_STRING;
160 if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
161 return false;
163 /* This is the key we insert in transaction. */
164 key.dsize--;
166 ret = external_agent_operation(agent, OPEN, TEST_DBNAME);
167 if (ret != SUCCESS)
168 errx(1, "Agent failed to open: %s", agent_return_name(ret));
170 ret = external_agent_operation(agent, FETCH, KEY_STRING);
171 if (ret != SUCCESS)
172 errx(1, "Agent failed find key: %s", agent_return_name(ret));
174 in_transaction = true;
175 if (tdb_transaction_start(tdb) != 0)
176 return false;
178 if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
179 return false;
181 if (tdb_transaction_commit(tdb) != 0)
182 return false;
184 in_transaction = false;
186 /* We made it! */
187 diag("Completed %u runs", current);
188 tdb_close(tdb);
189 ret = external_agent_operation(agent, CLOSE, "");
190 if (ret != SUCCESS) {
191 diag("Step %u close failed = %s", current,
192 agent_return_name(ret));
193 return false;
196 #ifdef HAVE_INCOHERENT_MMAP
197 /* This means we always mmap, which makes this test a noop. */
198 ok1(1);
199 #else
200 ok1(needed_recovery);
201 #endif
202 ok1(locking_errors == 0);
203 ok1(forget_locking() == 0);
204 locking_errors = 0;
205 return true;
208 int main(int argc, char *argv[])
210 enum operation ops[] = { FETCH, STORE, TRANSACTION_START };
211 struct agent *agent;
212 int i;
214 plan_tests(12);
215 unlock_callback = maybe_die;
217 agent = prepare_external_agent();
218 if (!agent)
219 err(1, "preparing agent");
221 for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
222 diag("Testing %s after death", operation_name(ops[i]));
223 ok1(test_death(ops[i], agent));
226 return exit_status();