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