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