tdb2: make tests include a single mega-header to simplify future patches
[ccan] / ccan / tdb2 / test / run-open-multiple-times.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include "logging.h"
4
5 int main(int argc, char *argv[])
6 {
7         unsigned int i;
8         struct tdb_context *tdb, *tdb2;
9         struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
10         struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
11         struct tdb_data d = { NULL, 0 }; /* Bogus GCC warning */
12         int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
13                         TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
14
15         plan_tests(sizeof(flags) / sizeof(flags[0]) * 28);
16         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
17                 tdb = tdb_open("run-open-multiple-times.tdb", flags[i],
18                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
19                 ok1(tdb);
20                 if (!tdb)
21                         continue;
22                 tdb2 = tdb_open("run-open-multiple-times.tdb", flags[i],
23                                 O_RDWR|O_CREAT, 0600, &tap_log_attr);
24                 ok1(tdb_check(tdb, NULL, NULL) == 0);
25                 ok1(tdb_check(tdb2, NULL, NULL) == 0);
26
27                 /* Store in one, fetch in the other. */
28                 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == 0);
29                 ok1(tdb_fetch(tdb2, key, &d) == TDB_SUCCESS);
30                 ok1(tdb_deq(d, data));
31                 free(d.dptr);
32
33                 /* Vice versa, with delete. */
34                 ok1(tdb_delete(tdb2, key) == 0);
35                 ok1(tdb_fetch(tdb, key, &d) == TDB_ERR_NOEXIST);
36
37                 /* OK, now close first one, check second still good. */
38                 ok1(tdb_close(tdb) == 0);
39
40                 ok1(tdb_store(tdb2, key, data, TDB_REPLACE) == 0);
41                 ok1(tdb_fetch(tdb2, key, &d) == TDB_SUCCESS);
42                 ok1(tdb_deq(d, data));
43                 free(d.dptr);
44
45                 /* Reopen */
46                 tdb = tdb_open("run-open-multiple-times.tdb", flags[i],
47                                O_RDWR|O_CREAT, 0600, &tap_log_attr);
48                 ok1(tdb);
49
50                 ok1(tdb_transaction_start(tdb2) == 0);
51
52                 /* Anything in the other one should fail. */
53                 ok1(tdb_fetch(tdb, key, &d) == TDB_ERR_LOCK);
54                 ok1(tap_log_messages == 1);
55                 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == TDB_ERR_LOCK);
56                 ok1(tap_log_messages == 2);
57                 ok1(tdb_transaction_start(tdb) == TDB_ERR_LOCK);
58                 ok1(tap_log_messages == 3);
59                 ok1(tdb_chainlock(tdb, key) == TDB_ERR_LOCK);
60                 ok1(tap_log_messages == 4);
61
62                 /* Transaciton should work as normal. */
63                 ok1(tdb_store(tdb2, key, data, TDB_REPLACE) == TDB_SUCCESS);
64
65                 /* Now... try closing with locks held. */
66                 ok1(tdb_close(tdb2) == 0);
67
68                 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
69                 ok1(tdb_deq(d, data));
70                 free(d.dptr);
71                 ok1(tdb_close(tdb) == 0);
72                 ok1(tap_log_messages == 4);
73                 tap_log_messages = 0;
74         }
75
76         return exit_status();
77 }