s3:libsmb/ntlmssp: an empty string should mean no password
[Samba/gebeck_regimport.git] / lib / tdb / test / run-open-during-transaction.c
blobf4a00525d8d9d65237e60523f3f9c7446648198f
1 #include "../common/tdb_private.h"
2 #include "lock-tracking.h"
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);
8 #define pwrite pwrite_check
9 #define write write_check
10 #define fcntl fcntl_with_lockcheck
11 #define ftruncate ftruncate_check
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 <err.h>
28 #include "external-agent.h"
29 #include "logging.h"
31 static struct agent *agent;
32 static bool opened;
33 static int errors = 0;
34 static bool clear_if_first;
35 #define TEST_DBNAME "run-open-during-transaction.tdb"
37 #undef write
38 #undef pwrite
39 #undef fcntl
40 #undef ftruncate
42 static bool is_same(const char *snapshot, const char *latest, off_t len)
44 unsigned i;
46 for (i = 0; i < len; i++) {
47 if (snapshot[i] != latest[i])
48 return false;
50 return true;
53 static bool compare_file(int fd, const char *snapshot, off_t snapshot_len)
55 char *contents;
56 bool same;
58 /* over-length read serves as length check. */
59 contents = malloc(snapshot_len+1);
60 same = pread(fd, contents, snapshot_len+1, 0) == snapshot_len
61 && is_same(snapshot, contents, snapshot_len);
62 free(contents);
63 return same;
66 static void check_file_intact(int fd)
68 enum agent_return ret;
69 struct stat st;
70 char *contents;
72 fstat(fd, &st);
73 contents = malloc(st.st_size);
74 if (pread(fd, contents, st.st_size, 0) != st.st_size) {
75 diag("Read fail");
76 errors++;
77 return;
80 /* Ask agent to open file. */
81 ret = external_agent_operation(agent, clear_if_first ?
82 OPEN_WITH_CLEAR_IF_FIRST :
83 OPEN,
84 TEST_DBNAME);
86 /* It's OK to open it, but it must not have changed! */
87 if (!compare_file(fd, contents, st.st_size)) {
88 diag("Agent changed file after opening %s",
89 agent_return_name(ret));
90 errors++;
93 if (ret == SUCCESS) {
94 ret = external_agent_operation(agent, CLOSE, NULL);
95 if (ret != SUCCESS) {
96 diag("Agent failed to close tdb: %s",
97 agent_return_name(ret));
98 errors++;
100 } else if (ret != WOULD_HAVE_BLOCKED) {
101 diag("Agent opening file gave %s",
102 agent_return_name(ret));
103 errors++;
106 free(contents);
109 static void after_unlock(int fd)
111 if (opened)
112 check_file_intact(fd);
115 static ssize_t pwrite_check(int fd,
116 const void *buf, size_t count, off_t offset)
118 if (opened)
119 check_file_intact(fd);
121 return pwrite(fd, buf, count, offset);
124 static ssize_t write_check(int fd, const void *buf, size_t count)
126 if (opened)
127 check_file_intact(fd);
129 return write(fd, buf, count);
132 static int ftruncate_check(int fd, off_t length)
134 if (opened)
135 check_file_intact(fd);
137 return ftruncate(fd, length);
141 int main(int argc, char *argv[])
143 const int flags[] = { TDB_DEFAULT,
144 TDB_CLEAR_IF_FIRST,
145 TDB_NOMMAP,
146 TDB_CLEAR_IF_FIRST | TDB_NOMMAP };
147 int i;
148 struct tdb_context *tdb;
149 TDB_DATA key, data;
151 plan_tests(20);
152 agent = prepare_external_agent();
153 if (!agent)
154 err(1, "preparing agent");
156 unlock_callback = after_unlock;
157 for (i = 0; i < sizeof(flags)/sizeof(flags[0]); i++) {
158 clear_if_first = (flags[i] & TDB_CLEAR_IF_FIRST);
159 diag("Test with %s and %s\n",
160 clear_if_first ? "CLEAR" : "DEFAULT",
161 (flags[i] & TDB_NOMMAP) ? "no mmap" : "mmap");
162 unlink(TEST_DBNAME);
163 tdb = tdb_open_ex(TEST_DBNAME, 1024, flags[i],
164 O_CREAT|O_TRUNC|O_RDWR, 0600,
165 &taplogctx, NULL);
166 ok1(tdb);
168 opened = true;
169 ok1(tdb_transaction_start(tdb) == 0);
170 key.dsize = strlen("hi");
171 key.dptr = (void *)"hi";
172 data.dptr = (void *)"world";
173 data.dsize = strlen("world");
175 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
176 ok1(tdb_transaction_commit(tdb) == 0);
177 ok(!errors, "We had %u open errors", errors);
179 opened = false;
180 tdb_close(tdb);
183 return exit_status();