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