tdb: wean CCAN-style unit tests off of tap.
[samba.git] / lib / tdb / test / run-traverse-in-transaction.c
1 #define _XOPEN_SOURCE 500
2 #include "lock-tracking.h"
3 #define fcntl fcntl_with_lockcheck
4 #include "../common/tdb_private.h"
5 #include "../common/io.c"
6 #include "../common/tdb.c"
7 #include "../common/lock.c"
8 #include "../common/freelist.c"
9 #include "../common/traverse.c"
10 #include "../common/transaction.c"
11 #include "../common/error.c"
12 #include "../common/open.c"
13 #include "../common/check.c"
14 #include "../common/hash.c"
15 #include "tap-interface.h"
16 #undef fcntl_with_lockcheck
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 traverse(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 int main(int argc, char *argv[])
46 {
47         struct tdb_context *tdb;
48         TDB_DATA key, data;
49
50         plan_tests(13);
51         agent = prepare_external_agent();
52         if (!agent)
53                 err(1, "preparing agent");
54
55         tdb = tdb_open_ex("run-traverse-in-transaction.tdb",
56                           1024, TDB_CLEAR_IF_FIRST, O_CREAT|O_TRUNC|O_RDWR,
57                           0600, &taplogctx, NULL);
58         ok1(tdb);
59
60         key.dsize = strlen("hi");
61         key.dptr = (void *)"hi";
62         data.dptr = (void *)"world";
63         data.dsize = strlen("world");
64
65         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
66
67         ok1(external_agent_operation(agent, OPEN, tdb_name(tdb)) == SUCCESS);
68
69         ok1(tdb_transaction_start(tdb) == 0);
70         ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
71             == WOULD_HAVE_BLOCKED);
72         tdb_traverse(tdb, traverse, NULL);
73
74         /* That should *not* release the transaction lock! */
75         ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
76             == WOULD_HAVE_BLOCKED);
77         tdb_traverse_read(tdb, traverse, NULL);
78
79         /* That should *not* release the transaction lock! */
80         ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
81             == WOULD_HAVE_BLOCKED);
82         ok1(tdb_transaction_commit(tdb) == 0);
83         /* Now we should be fine. */
84         ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
85             == SUCCESS);
86
87         tdb_close(tdb);
88
89         return exit_status();
90 }