merge
[ccan] / ccan / tdb / test / run-traverse-in-transaction.c
1 #define _XOPEN_SOURCE 500
2 #include "tdb/tdb.h"
3 #include "tdb/io.c"
4 #include "tdb/tdb.c"
5 #include "tdb/lock.c"
6 #include "tdb/freelist.c"
7 #include "tdb/traverse.c"
8 #include "tdb/transaction.c"
9 #include "tdb/error.c"
10 #include "tdb/open.c"
11 #include "tap/tap.h"
12 #include <stdlib.h>
13 #include <stdbool.h>
14 #include <err.h>
15
16 static bool correct_key(TDB_DATA key)
17 {
18         return key.dsize == strlen("hi")
19                 && memcmp(key.dptr, "hi", key.dsize) == 0;
20 }
21
22 static bool correct_data(TDB_DATA data)
23 {
24         return data.dsize == strlen("world")
25                 && memcmp(data.dptr, "world", data.dsize) == 0;
26 }
27
28 static int traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
29                      void *p)
30 {
31         ok1(correct_key(key));
32         ok1(correct_data(data));
33         return 0;
34 }
35
36 int main(int argc, char *argv[])
37 {
38         struct tdb_context *tdb;
39         TDB_DATA key, data;
40
41         plan_tests(12);
42         tdb = tdb_open("/tmp/test.tdb", 1024, TDB_CLEAR_IF_FIRST,
43                        O_CREAT|O_TRUNC|O_RDWR, 0600);
44         ok1(tdb);
45
46         /* Tickle bug on appending zero length buffer to zero length buffer. */
47         key.dsize = strlen("hi");
48         key.dptr = (void *)"hi";
49         data.dptr = (void *)"world";
50         data.dsize = strlen("world");
51
52         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
53
54         ok1(tdb_transaction_start(tdb) == 0);
55         ok(tdb->have_transaction_lock, "Transaction lock in transaction");
56         tdb_traverse(tdb, traverse, NULL);
57
58         /* That should *not* release the transaction lock! */
59         ok(tdb->have_transaction_lock, "Transaction lock after traverse");
60         tdb_traverse_read(tdb, traverse, NULL);
61
62         /* That should *not* release the transaction lock! */
63         ok(tdb->have_transaction_lock, "Transaction lock after traverse_read");
64         ok1(tdb_transaction_commit(tdb) == 0);
65         ok(!tdb->have_transaction_lock, "Transaction unlock");
66
67         tdb_close(tdb);
68
69         return exit_status();
70 }