tdb: don't use err.h in tests.
[ddiss/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         if (!agent)
51                 err(1, "preparing agent");
52
53         tdb = tdb_open_ex("run-traverse-in-transaction.tdb",
54                           1024, TDB_CLEAR_IF_FIRST, O_CREAT|O_TRUNC|O_RDWR,
55                           0600, &taplogctx, NULL);
56         ok1(tdb);
57
58         key.dsize = strlen("hi");
59         key.dptr = (void *)"hi";
60         data.dptr = (void *)"world";
61         data.dsize = strlen("world");
62
63         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
64
65         ok1(external_agent_operation(agent, OPEN, tdb_name(tdb)) == SUCCESS);
66
67         ok1(tdb_transaction_start(tdb) == 0);
68         ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
69             == WOULD_HAVE_BLOCKED);
70         tdb_traverse(tdb, traverse, NULL);
71
72         /* That should *not* release the transaction lock! */
73         ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
74             == WOULD_HAVE_BLOCKED);
75         tdb_traverse_read(tdb, traverse, NULL);
76
77         /* That should *not* release the transaction lock! */
78         ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
79             == WOULD_HAVE_BLOCKED);
80         ok1(tdb_transaction_commit(tdb) == 0);
81         /* Now we should be fine. */
82         ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
83             == SUCCESS);
84
85         tdb_close(tdb);
86
87         return exit_status();
88 }