tdb: enforce hashing, via example hash in old rwlocks entry in header.
[ccan] / ccan / tdb / test / run-traverse-in-transaction.c
1 #define _XOPEN_SOURCE 500
2 #include <ccan/tdb/tdb.h>
3 #include <ccan/tdb/io.c>
4 #include <ccan/tdb/tdb.c>
5 #include <ccan/tdb/lock.c>
6 #include <ccan/tdb/freelist.c>
7 #include <ccan/tdb/traverse.c>
8 #include <ccan/tdb/transaction.c>
9 #include <ccan/tdb/error.c>
10 #include <ccan/tdb/open.c>
11 #include <ccan/tdb/check.c>
12 #include <ccan/tap/tap.h>
13 #include <stdlib.h>
14 #include <stdbool.h>
15 #include <err.h>
16 #include "external-transaction.h"
17
18 static struct agent *agent;
19
20 static bool correct_key(TDB_DATA key)
21 {
22         return key.dsize == strlen("hi")
23                 && memcmp(key.dptr, "hi", key.dsize) == 0;
24 }
25
26 static bool correct_data(TDB_DATA data)
27 {
28         return data.dsize == strlen("world")
29                 && memcmp(data.dptr, "world", data.dsize) == 0;
30 }
31
32 static int traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
33                      void *p)
34 {
35         ok1(correct_key(key));
36         ok1(correct_data(data));
37         return 0;
38 }
39
40 int main(int argc, char *argv[])
41 {
42         struct tdb_context *tdb;
43         TDB_DATA key, data;
44
45         plan_tests(13);
46         agent = prepare_external_agent();
47         if (!agent)
48                 err(1, "preparing agent");
49
50         tdb = tdb_open("run-traverse-in-transaction.tdb",
51                        1024, TDB_CLEAR_IF_FIRST, O_CREAT|O_TRUNC|O_RDWR, 0600);
52         ok1(tdb);
53
54         key.dsize = strlen("hi");
55         key.dptr = (void *)"hi";
56         data.dptr = (void *)"world";
57         data.dsize = strlen("world");
58
59         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
60
61         ok1(external_agent_operation(agent, TRANSACTION, tdb_name(tdb)));
62
63         ok1(tdb_transaction_start(tdb) == 0);
64         ok1(!external_agent_operation(agent, TRANSACTION, tdb_name(tdb)));
65         tdb_traverse(tdb, traverse, NULL);
66
67         /* That should *not* release the transaction lock! */
68         ok1(!external_agent_operation(agent, TRANSACTION, tdb_name(tdb)));
69         tdb_traverse_read(tdb, traverse, NULL);
70
71         /* That should *not* release the transaction lock! */
72         ok1(!external_agent_operation(agent, TRANSACTION, tdb_name(tdb)));
73         ok1(tdb_transaction_commit(tdb) == 0);
74         /* Now we should be fine. */
75         ok1(external_agent_operation(agent, TRANSACTION, tdb_name(tdb)));
76
77         tdb_close(tdb);
78
79         return exit_status();
80 }