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