tdb: unify logging code in test dir.
[ccan] / ccan / tdb / test / run-open-during-transaction.c
1 #define _XOPEN_SOURCE 500
2 #include <unistd.h>
3 #include "lock-tracking.h"
4
5 static ssize_t pwrite_check(int fd, const void *buf, size_t count, off_t offset);
6 static ssize_t write_check(int fd, const void *buf, size_t count);
7 static int ftruncate_check(int fd, off_t length);
8
9 #define pwrite pwrite_check
10 #define write write_check
11 #define fcntl fcntl_with_lockcheck
12 #define ftruncate ftruncate_check
13
14 #include <ccan/tdb/tdb.h>
15 #include <ccan/tdb/io.c>
16 #include <ccan/tdb/tdb.c>
17 #include <ccan/tdb/lock.c>
18 #include <ccan/tdb/freelist.c>
19 #include <ccan/tdb/traverse.c>
20 #include <ccan/tdb/transaction.c>
21 #include <ccan/tdb/error.c>
22 #include <ccan/tdb/open.c>
23 #include <ccan/tdb/check.c>
24 #include <ccan/tap/tap.h>
25 #include <stdlib.h>
26 #include <stdbool.h>
27 #include <stdarg.h>
28 #include <err.h>
29 #include "external-agent.h"
30 #include "logging.h"
31
32 static struct agent *agent;
33 static bool opened;
34 static int errors = 0;
35 static bool clear_if_first;
36 #define TEST_DBNAME "run-open-during-transaction.tdb"
37
38 #undef write
39 #undef pwrite
40 #undef fcntl
41 #undef ftruncate
42
43 static bool is_same(const char *snapshot, const char *latest, off_t len)
44 {
45         unsigned i;
46
47         for (i = 0; i < len; i++) {
48                 if (snapshot[i] != latest[i])
49                         return false;
50         }
51         return true;
52 }
53
54 static bool compare_file(int fd, const char *snapshot, off_t snapshot_len)
55 {
56         char *contents;
57         bool same;
58
59         /* over-length read serves as length check. */
60         contents = malloc(snapshot_len+1);
61         same = pread(fd, contents, snapshot_len+1, 0) == snapshot_len
62                 && is_same(snapshot, contents, snapshot_len);
63         free(contents);
64         return same;
65 }
66
67 static void check_file_intact(int fd)
68 {
69         enum agent_return ret;
70         struct stat st;
71         char *contents;
72
73         fstat(fd, &st);
74         contents = malloc(st.st_size);
75         if (pread(fd, contents, st.st_size, 0) != st.st_size) {
76                 diag("Read fail");
77                 errors++;
78                 return;
79         }
80
81         /* Ask agent to open file. */
82         ret = external_agent_operation(agent, clear_if_first ?
83                                        OPEN_WITH_CLEAR_IF_FIRST :
84                                        OPEN,
85                                        TEST_DBNAME);
86
87         /* It's OK to open it, but it must not have changed! */
88         if (!compare_file(fd, contents, st.st_size)) {
89                 diag("Agent changed file after opening %s",
90                      agent_return_name(ret));
91                 errors++;
92         }
93
94         if (ret == SUCCESS) {
95                 ret = external_agent_operation(agent, CLOSE, NULL);
96                 if (ret != SUCCESS) {
97                         diag("Agent failed to close tdb: %s",
98                              agent_return_name(ret));
99                         errors++;
100                 }
101         } else if (ret != WOULD_HAVE_BLOCKED) {
102                 diag("Agent opening file gave %s",
103                      agent_return_name(ret));
104                 errors++;
105         }
106
107         free(contents);
108 }
109
110 static void after_unlock(int fd)
111 {
112         if (opened)
113                 check_file_intact(fd);
114 }
115         
116 static ssize_t pwrite_check(int fd,
117                             const void *buf, size_t count, off_t offset)
118 {
119         if (opened)
120                 check_file_intact(fd);
121
122         return pwrite(fd, buf, count, offset);
123 }
124
125 static ssize_t write_check(int fd, const void *buf, size_t count)
126 {
127         if (opened)
128                 check_file_intact(fd);
129
130         return write(fd, buf, count);
131 }
132
133 static int ftruncate_check(int fd, off_t length)
134 {
135         if (opened)
136                 check_file_intact(fd);
137
138         return ftruncate(fd, length);
139
140 }
141
142 int main(int argc, char *argv[])
143 {
144         const int flags[] = { TDB_DEFAULT,
145                               TDB_CLEAR_IF_FIRST,
146                               TDB_NOMMAP, 
147                               TDB_CLEAR_IF_FIRST | TDB_NOMMAP };
148         int i;
149         struct tdb_context *tdb;
150         TDB_DATA key, data;
151
152         plan_tests(20);
153         agent = prepare_external_agent();
154         if (!agent)
155                 err(1, "preparing agent");
156
157         unlock_callback = after_unlock;
158         for (i = 0; i < sizeof(flags)/sizeof(flags[0]); i++) {
159                 clear_if_first = (flags[i] & TDB_CLEAR_IF_FIRST);
160                 diag("Test with %s and %s\n",
161                      clear_if_first ? "CLEAR" : "DEFAULT",
162                      (flags[i] & TDB_NOMMAP) ? "no mmap" : "mmap");
163                 unlink(TEST_DBNAME);
164                 tdb = tdb_open_ex(TEST_DBNAME, 1024, flags[i],
165                                   O_CREAT|O_TRUNC|O_RDWR, 0600,
166                                   &taplogctx, NULL);
167                 ok1(tdb);
168
169                 opened = true;
170                 ok1(tdb_transaction_start(tdb) == 0);
171                 key.dsize = strlen("hi");
172                 key.dptr = (void *)"hi";
173                 data.dptr = (void *)"world";
174                 data.dsize = strlen("world");
175
176                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
177                 ok1(tdb_transaction_commit(tdb) == 0);
178                 ok(!errors, "We had %u open errors", errors);
179
180                 opened = false;
181                 tdb_close(tdb);
182         }
183
184         return exit_status();
185 }