tdb2: test: try (almost) all tests with TDB_VERSION1 flag.
[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, extra_messages;
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                         TDB_VERSION1, TDB_NOMMAP|TDB_VERSION1,
15                         TDB_CONVERT|TDB_VERSION1,
16                         TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
17
18         plan_tests(sizeof(flags) / sizeof(flags[0]) * 28);
19         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
20                 tdb = tdb_open("run-open-multiple-times.tdb", flags[i],
21                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
22                 ok1(tdb);
23                 if (!tdb)
24                         continue;
25
26                 if (flags[i] & TDB_VERSION1) {
27                         extra_messages = 1;
28                 } else {
29                         extra_messages = 0;
30                 }
31                 tdb2 = tdb_open("run-open-multiple-times.tdb", flags[i],
32                                 O_RDWR|O_CREAT, 0600, &tap_log_attr);
33                 ok1(tdb_check(tdb, NULL, NULL) == 0);
34                 ok1(tdb_check(tdb2, NULL, NULL) == 0);
35
36                 /* Store in one, fetch in the other. */
37                 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == 0);
38                 ok1(tdb_fetch(tdb2, key, &d) == TDB_SUCCESS);
39                 ok1(tdb_deq(d, data));
40                 free(d.dptr);
41
42                 /* Vice versa, with delete. */
43                 ok1(tdb_delete(tdb2, key) == 0);
44                 ok1(tdb_fetch(tdb, key, &d) == TDB_ERR_NOEXIST);
45
46                 /* OK, now close first one, check second still good. */
47                 ok1(tdb_close(tdb) == 0);
48
49                 ok1(tdb_store(tdb2, key, data, TDB_REPLACE) == 0);
50                 ok1(tdb_fetch(tdb2, key, &d) == TDB_SUCCESS);
51                 ok1(tdb_deq(d, data));
52                 free(d.dptr);
53
54                 /* Reopen */
55                 tdb = tdb_open("run-open-multiple-times.tdb", flags[i],
56                                O_RDWR|O_CREAT, 0600, &tap_log_attr);
57                 ok1(tdb);
58
59                 ok1(tdb_transaction_start(tdb2) == 0);
60
61                 /* Anything in the other one should fail. */
62                 ok1(tdb_fetch(tdb, key, &d) == TDB_ERR_LOCK);
63                 tap_log_messages -= extra_messages;
64                 ok1(tap_log_messages == 1);
65                 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == TDB_ERR_LOCK);
66                 tap_log_messages -= extra_messages;
67                 ok1(tap_log_messages == 2);
68                 ok1(tdb_transaction_start(tdb) == TDB_ERR_LOCK);
69                 ok1(tap_log_messages == 3);
70                 ok1(tdb_chainlock(tdb, key) == TDB_ERR_LOCK);
71                 tap_log_messages -= extra_messages;
72                 ok1(tap_log_messages == 4);
73
74                 /* Transaciton should work as normal. */
75                 ok1(tdb_store(tdb2, key, data, TDB_REPLACE) == TDB_SUCCESS);
76
77                 /* Now... try closing with locks held. */
78                 ok1(tdb_close(tdb2) == 0);
79
80                 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
81                 ok1(tdb_deq(d, data));
82                 free(d.dptr);
83                 ok1(tdb_close(tdb) == 0);
84                 ok1(tap_log_messages == 4);
85                 tap_log_messages = 0;
86         }
87
88         return exit_status();
89 }