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