tdb2: test: import tdb1's tests.
[ccan] / ccan / tdb2 / test / run-tdb1-die-during-transaction.c
1 #include <ccan/tdb2/private.h>
2 #include <unistd.h>
3 #include "tdb1-lock-tracking.h"
4 static ssize_t pwrite_check(int fd, const void *buf, size_t count, off_t offset);
5 static ssize_t write_check(int fd, const void *buf, size_t count);
6 static int ftruncate_check(int fd, off_t length);
7
8 #define pwrite pwrite_check
9 #define write write_check
10 #define fcntl fcntl_with_lockcheck1
11 #define ftruncate ftruncate_check
12
13 #include "tdb2-source.h"
14 #include <ccan/tap/tap.h>
15 #include <stdlib.h>
16 #include <stdbool.h>
17 #include <stdarg.h>
18 #include <err.h>
19 #include <setjmp.h>
20 #include "tdb1-external-agent.h"
21 #include "tdb1-logging.h"
22
23 #undef write
24 #undef pwrite
25 #undef fcntl
26 #undef ftruncate
27
28 static bool in_transaction;
29 static int target, current;
30 static jmp_buf jmpbuf;
31 #define TEST_DBNAME "run-die-during-transaction.tdb"
32 #define KEY_STRING "helloworld"
33
34 static void maybe_die(int fd)
35 {
36         if (in_transaction && current++ == target) {
37                 longjmp(jmpbuf, 1);
38         }
39 }
40
41 static ssize_t pwrite_check(int fd,
42                             const void *buf, size_t count, off_t offset)
43 {
44         ssize_t ret;
45
46         maybe_die(fd);
47
48         ret = pwrite(fd, buf, count, offset);
49         if (ret != count)
50                 return ret;
51
52         maybe_die(fd);
53         return ret;
54 }
55
56 static ssize_t write_check(int fd, const void *buf, size_t count)
57 {
58         ssize_t ret;
59
60         maybe_die(fd);
61
62         ret = write(fd, buf, count);
63         if (ret != count)
64                 return ret;
65
66         maybe_die(fd);
67         return ret;
68 }
69
70 static int ftruncate_check(int fd, off_t length)
71 {
72         int ret;
73
74         maybe_die(fd);
75
76         ret = ftruncate(fd, length);
77
78         maybe_die(fd);
79         return ret;
80 }
81
82 static bool test_death(enum operation op, struct agent *agent)
83 {
84         struct tdb1_context *tdb = NULL;
85         TDB1_DATA key;
86         enum agent_return ret;
87         int needed_recovery = 0;
88
89         current = target = 0;
90 reset:
91         unlink(TEST_DBNAME);
92         tdb = tdb1_open_ex(TEST_DBNAME, 1024, TDB1_NOMMAP,
93                           O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
94
95         if (setjmp(jmpbuf) != 0) {
96                 /* We're partway through.  Simulate our death. */
97                 close(tdb->fd);
98                 forget_locking1();
99                 in_transaction = false;
100
101                 ret = external_agent_operation1(agent, NEEDS_RECOVERY, "");
102                 if (ret == SUCCESS)
103                         needed_recovery++;
104                 else if (ret != FAILED) {
105                         diag("Step %u agent NEEDS_RECOVERY = %s", current,
106                              agent_return_name1(ret));
107                         return false;
108                 }
109
110                 ret = external_agent_operation1(agent, op, KEY_STRING);
111                 if (ret != SUCCESS) {
112                         diag("Step %u op %s failed = %s", current,
113                              operation_name1(op),
114                              agent_return_name1(ret));
115                         return false;
116                 }
117
118                 ret = external_agent_operation1(agent, NEEDS_RECOVERY, "");
119                 if (ret != FAILED) {
120                         diag("Still needs recovery after step %u = %s",
121                              current, agent_return_name1(ret));
122                         return false;
123                 }
124
125                 ret = external_agent_operation1(agent, CHECK, "");
126                 if (ret != SUCCESS) {
127                         diag("Step %u check failed = %s", current,
128                              agent_return_name1(ret));
129                         return false;
130                 }
131
132                 ret = external_agent_operation1(agent, CLOSE, "");
133                 if (ret != SUCCESS) {
134                         diag("Step %u close failed = %s", current,
135                              agent_return_name1(ret));
136                         return false;
137                 }
138
139                 /* Suppress logging as this tries to use closed fd. */
140                 suppress_logging = true;
141                 suppress_lockcheck1 = true;
142                 tdb1_close(tdb);
143                 suppress_logging = false;
144                 suppress_lockcheck1 = false;
145                 target++;
146                 current = 0;
147                 goto reset;
148         }
149
150         /* Put key for agent to fetch. */
151         key.dsize = strlen(KEY_STRING);
152         key.dptr = (void *)KEY_STRING;
153         if (tdb1_store(tdb, key, key, TDB1_INSERT) != 0)
154                 return false;
155
156         /* This is the key we insert in transaction. */
157         key.dsize--;
158
159         ret = external_agent_operation1(agent, OPEN, TEST_DBNAME);
160         if (ret != SUCCESS)
161                 errx(1, "Agent failed to open: %s", agent_return_name1(ret));
162
163         ret = external_agent_operation1(agent, FETCH, KEY_STRING);
164         if (ret != SUCCESS)
165                 errx(1, "Agent failed find key: %s", agent_return_name1(ret));
166
167         in_transaction = true;
168         if (tdb1_transaction_start(tdb) != 0)
169                 return false;
170
171         if (tdb1_store(tdb, key, key, TDB1_INSERT) != 0)
172                 return false;
173
174         if (tdb1_transaction_commit(tdb) != 0)
175                 return false;
176
177         in_transaction = false;
178
179         /* We made it! */
180         diag("Completed %u runs", current);
181         tdb1_close(tdb);
182         ret = external_agent_operation1(agent, CLOSE, "");
183         if (ret != SUCCESS) {
184                 diag("Step %u close failed = %s", current,
185                      agent_return_name1(ret));
186                 return false;
187         }
188
189         ok1(needed_recovery);
190         ok1(locking_errors1 == 0);
191         ok1(forget_locking1() == 0);
192         locking_errors1 = 0;
193         return true;
194 }
195
196 int main(int argc, char *argv[])
197 {
198         enum operation ops[] = { FETCH, STORE, TRANSACTION_START };
199         struct agent *agent;
200         int i;
201
202         plan_tests(12);
203         unlock_callback1 = maybe_die;
204
205         agent = prepare_external_agent1();
206         if (!agent)
207                 err(1, "preparing agent");
208
209         for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
210                 diag("Testing %s after death", operation_name1(ops[i]));
211                 ok1(test_death(ops[i], agent));
212         }
213
214         return exit_status();
215 }