strsplit: remove nump argument
[ccan] / ccan / tdb / test / run-nested-traverse.c
1 #define _XOPEN_SOURCE 500
2 #include "lock-tracking.h"
3 #define fcntl fcntl_with_lockcheck
4 #include <ccan/tdb/tdb.h>
5 #include <ccan/tdb/io.c>
6 #include <ccan/tdb/tdb.c>
7 #include <ccan/tdb/lock.c>
8 #include <ccan/tdb/freelist.c>
9 #include <ccan/tdb/traverse.c>
10 #include <ccan/tdb/transaction.c>
11 #include <ccan/tdb/error.c>
12 #include <ccan/tdb/open.c>
13 #include <ccan/tdb/check.c>
14 #include <ccan/tdb/hash.c>
15 #include <ccan/tap/tap.h>
16 #undef fcntl
17 #include <stdlib.h>
18 #include <stdbool.h>
19 #include <err.h>
20 #include "external-agent.h"
21 #include "logging.h"
22
23 static struct agent *agent;
24
25 static bool correct_key(TDB_DATA key)
26 {
27         return key.dsize == strlen("hi")
28                 && memcmp(key.dptr, "hi", key.dsize) == 0;
29 }
30
31 static bool correct_data(TDB_DATA data)
32 {
33         return data.dsize == strlen("world")
34                 && memcmp(data.dptr, "world", data.dsize) == 0;
35 }
36
37 static int traverse2(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
38                      void *p)
39 {
40         ok1(correct_key(key));
41         ok1(correct_data(data));
42         return 0;
43 }
44
45 static int traverse1(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
46                      void *p)
47 {
48         ok1(correct_key(key));
49         ok1(correct_data(data));
50         ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
51             == WOULD_HAVE_BLOCKED);
52         tdb_traverse(tdb, traverse2, NULL);
53
54         /* That should *not* release the transaction lock! */
55         ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
56             == WOULD_HAVE_BLOCKED);
57         return 0;
58 }
59
60 int main(int argc, char *argv[])
61 {
62         struct tdb_context *tdb;
63         TDB_DATA key, data;
64
65         plan_tests(17);
66         agent = prepare_external_agent();
67         if (!agent)
68                 err(1, "preparing agent");
69
70         tdb = tdb_open_ex("run-nested-traverse.tdb", 1024, TDB_CLEAR_IF_FIRST,
71                           O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
72         ok1(tdb);
73
74         ok1(external_agent_operation(agent, OPEN, tdb_name(tdb)) == SUCCESS);
75         ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
76             == SUCCESS);
77         ok1(external_agent_operation(agent, TRANSACTION_COMMIT, tdb_name(tdb))
78             == SUCCESS);
79
80         key.dsize = strlen("hi");
81         key.dptr = (void *)"hi";
82         data.dptr = (void *)"world";
83         data.dsize = strlen("world");
84
85         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
86         tdb_traverse(tdb, traverse1, NULL);
87         tdb_traverse_read(tdb, traverse1, NULL);
88         tdb_close(tdb);
89
90         return exit_status();
91 }