1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
5 int main(int argc, char *argv[])
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 };
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);
26 if (flags[i] & TDB_VERSION1) {
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);
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));
42 /* Vice versa, with delete. */
43 ok1(tdb_delete(tdb2, key) == 0);
44 ok1(tdb_fetch(tdb, key, &d) == TDB_ERR_NOEXIST);
46 /* OK, now close first one, check second still good. */
47 ok1(tdb_close(tdb) == 0);
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));
55 tdb = tdb_open("run-open-multiple-times.tdb", flags[i],
56 O_RDWR|O_CREAT, 0600, &tap_log_attr);
59 ok1(tdb_transaction_start(tdb2) == 0);
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);
74 /* Transaciton should work as normal. */
75 ok1(tdb_store(tdb2, key, data, TDB_REPLACE) == TDB_SUCCESS);
77 /* Now... try closing with locks held. */
78 ok1(tdb_close(tdb2) == 0);
80 ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
81 ok1(tdb_deq(d, data));
83 ok1(tdb_close(tdb) == 0);
84 ok1(tap_log_messages == 4);