s3:net_util: add some const to sockaddr_storage
[Samba/gebeck_regimport.git] / lib / tdb2 / test / run-56-open-during-transaction.c
blob96107d637e4472b7026b587f26c031191b1c9631
1 #include "config.h"
2 #include <unistd.h>
3 #include "lock-tracking.h"
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);
9 #define pwrite pwrite_check
10 #define write write_check
11 #define fcntl fcntl_with_lockcheck
12 #define ftruncate ftruncate_check
14 #include <ccan/tdb2/tdb.c>
15 #include <ccan/tdb2/open.c>
16 #include <ccan/tdb2/free.c>
17 #include <ccan/tdb2/lock.c>
18 #include <ccan/tdb2/io.c>
19 #include <ccan/tdb2/hash.c>
20 #include <ccan/tdb2/check.c>
21 #include <ccan/tdb2/transaction.c>
22 #include <ccan/tap/tap.h>
23 #include <stdlib.h>
24 #include <stdbool.h>
25 #include <stdarg.h>
26 #include <err.h>
27 #include "external-agent.h"
28 #include "logging.h"
30 static struct agent *agent;
31 static bool opened;
32 static int errors = 0;
33 #define TEST_DBNAME "run-56-open-during-transaction.tdb"
35 #undef write
36 #undef pwrite
37 #undef fcntl
38 #undef ftruncate
40 static bool is_same(const char *snapshot, const char *latest, off_t len)
42 unsigned i;
44 for (i = 0; i < len; i++) {
45 if (snapshot[i] != latest[i])
46 return false;
48 return true;
51 static bool compare_file(int fd, const char *snapshot, off_t snapshot_len)
53 char *contents;
54 bool same;
56 /* over-length read serves as length check. */
57 contents = malloc(snapshot_len+1);
58 same = pread(fd, contents, snapshot_len+1, 0) == snapshot_len
59 && is_same(snapshot, contents, snapshot_len);
60 free(contents);
61 return same;
64 static void check_file_intact(int fd)
66 enum agent_return ret;
67 struct stat st;
68 char *contents;
70 fstat(fd, &st);
71 contents = malloc(st.st_size);
72 if (pread(fd, contents, st.st_size, 0) != st.st_size) {
73 diag("Read fail");
74 errors++;
75 return;
78 /* Ask agent to open file. */
79 ret = external_agent_operation(agent, OPEN, TEST_DBNAME);
81 /* It's OK to open it, but it must not have changed! */
82 if (!compare_file(fd, contents, st.st_size)) {
83 diag("Agent changed file after opening %s",
84 agent_return_name(ret));
85 errors++;
88 if (ret == SUCCESS) {
89 ret = external_agent_operation(agent, CLOSE, NULL);
90 if (ret != SUCCESS) {
91 diag("Agent failed to close tdb: %s",
92 agent_return_name(ret));
93 errors++;
95 } else if (ret != WOULD_HAVE_BLOCKED) {
96 diag("Agent opening file gave %s",
97 agent_return_name(ret));
98 errors++;
101 free(contents);
104 static void after_unlock(int fd)
106 if (opened)
107 check_file_intact(fd);
110 static ssize_t pwrite_check(int fd,
111 const void *buf, size_t count, off_t offset)
113 if (opened)
114 check_file_intact(fd);
116 return pwrite(fd, buf, count, offset);
119 static ssize_t write_check(int fd, const void *buf, size_t count)
121 if (opened)
122 check_file_intact(fd);
124 return write(fd, buf, count);
127 static int ftruncate_check(int fd, off_t length)
129 if (opened)
130 check_file_intact(fd);
132 return ftruncate(fd, length);
136 int main(int argc, char *argv[])
138 const int flags[] = { TDB_DEFAULT,
139 TDB_NOMMAP,
140 TDB_CONVERT,
141 TDB_CONVERT | TDB_NOMMAP };
142 int i;
143 struct tdb_context *tdb;
144 TDB_DATA key, data;
146 plan_tests(20);
147 agent = prepare_external_agent();
148 if (!agent)
149 err(1, "preparing agent");
151 unlock_callback = after_unlock;
152 for (i = 0; i < sizeof(flags)/sizeof(flags[0]); i++) {
153 diag("Test with %s and %s\n",
154 (flags[i] & TDB_CONVERT) ? "CONVERT" : "DEFAULT",
155 (flags[i] & TDB_NOMMAP) ? "no mmap" : "mmap");
156 unlink(TEST_DBNAME);
157 tdb = tdb_open(TEST_DBNAME, flags[i],
158 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
159 ok1(tdb);
161 opened = true;
162 ok1(tdb_transaction_start(tdb) == 0);
163 key = tdb_mkdata("hi", strlen("hi"));
164 data = tdb_mkdata("world", strlen("world"));
166 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
167 ok1(tdb_transaction_commit(tdb) == 0);
168 ok(!errors, "We had %u open errors", errors);
170 opened = false;
171 tdb_close(tdb);
174 return exit_status();