tdb/test: fix up tests for use in SAMBA tdb code.
[ddiss/samba.git] / lib / tdb / test / run-nested-transactions.c
1 #define _XOPEN_SOURCE 500
2 #include "../common/tdb_private.h"
3 #include "../common/io.c"
4 #include "../common/tdb.c"
5 #include "../common/lock.c"
6 #include "../common/freelist.c"
7 #include "../common/traverse.c"
8 #include "../common/transaction.c"
9 #include "../common/error.c"
10 #include "../common/open.c"
11 #include "../common/check.c"
12 #include "../common/hash.c"
13 #include "tap-interface.h"
14 #include <stdlib.h>
15 #include <stdbool.h>
16 #include <err.h>
17 #include "logging.h"
18
19 int main(int argc, char *argv[])
20 {
21         struct tdb_context *tdb;
22         TDB_DATA key, data;
23
24         plan_tests(27);
25         key.dsize = strlen("hi");
26         key.dptr = (void *)"hi";
27
28         tdb = tdb_open_ex("run-nested-transactions.tdb",
29                           1024, TDB_CLEAR_IF_FIRST|TDB_DISALLOW_NESTING,
30                           O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
31         ok1(tdb);
32
33         /* Nesting disallowed. */
34         ok1(tdb_transaction_start(tdb) == 0);
35         data.dptr = (void *)"world";
36         data.dsize = strlen("world");
37         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
38         data = tdb_fetch(tdb, key);
39         ok1(data.dsize == strlen("world"));
40         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
41         free(data.dptr);
42         ok1(tdb_transaction_start(tdb) != 0);
43         ok1(tdb_error(tdb) == TDB_ERR_NESTING);
44
45         data = tdb_fetch(tdb, key);
46         ok1(data.dsize == strlen("world"));
47         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
48         free(data.dptr);
49         ok1(tdb_transaction_commit(tdb) == 0);
50         data = tdb_fetch(tdb, key);
51         ok1(data.dsize == strlen("world"));
52         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
53         free(data.dptr);
54         tdb_close(tdb);
55
56         /* Nesting allowed by default */
57         tdb = tdb_open_ex("run-nested-transactions.tdb",
58                           1024, TDB_DEFAULT, O_RDWR, 0, &taplogctx, NULL);
59         ok1(tdb);
60
61         ok1(tdb_transaction_start(tdb) == 0);
62         ok1(tdb_transaction_start(tdb) == 0);
63         ok1(tdb_delete(tdb, key) == 0);
64         ok1(tdb_transaction_commit(tdb) == 0);
65         ok1(!tdb_exists(tdb, key));
66         ok1(tdb_transaction_cancel(tdb) == 0);
67         /* Surprise! Kills inner "committed" transaction. */
68         ok1(tdb_exists(tdb, key));
69
70         ok1(tdb_transaction_start(tdb) == 0);
71         ok1(tdb_transaction_start(tdb) == 0);
72         ok1(tdb_delete(tdb, key) == 0);
73         ok1(tdb_transaction_commit(tdb) == 0);
74         ok1(!tdb_exists(tdb, key));
75         ok1(tdb_transaction_commit(tdb) == 0);
76         ok1(!tdb_exists(tdb, key));
77         tdb_close(tdb);
78
79         return exit_status();
80 }